dom/xbl/nsXBLEventHandler.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsCOMPtr.h"
     7 #include "nsIAtom.h"
     8 #include "nsIDOMEventListener.h"
     9 #include "nsIDOMKeyEvent.h"
    10 #include "nsIDOMMouseEvent.h"
    11 #include "nsXBLPrototypeHandler.h"
    12 #include "nsContentUtils.h"
    13 #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
    14 #include "mozilla/dom/EventTarget.h"
    16 using namespace mozilla::dom;
    18 nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler* aHandler)
    19   : mProtoHandler(aHandler)
    20 {
    21 }
    23 nsXBLEventHandler::~nsXBLEventHandler()
    24 {
    25 }
    27 NS_IMPL_ISUPPORTS(nsXBLEventHandler, nsIDOMEventListener)
    29 NS_IMETHODIMP
    30 nsXBLEventHandler::HandleEvent(nsIDOMEvent* aEvent)
    31 {
    32   if (!mProtoHandler)
    33     return NS_ERROR_FAILURE;
    35   uint8_t phase = mProtoHandler->GetPhase();
    36   if (phase == NS_PHASE_TARGET) {
    37     uint16_t eventPhase;
    38     aEvent->GetEventPhase(&eventPhase);
    39     if (eventPhase != nsIDOMEvent::AT_TARGET)
    40       return NS_OK;
    41   }
    43   if (!EventMatched(aEvent))
    44     return NS_OK;
    46   mProtoHandler->ExecuteHandler(aEvent->InternalDOMEvent()->GetCurrentTarget(),
    47                                 aEvent);
    49   return NS_OK;
    50 }
    52 nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler)
    53   : nsXBLEventHandler(aHandler)
    54 {
    55 }
    57 nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
    58 {
    59 }
    61 bool
    62 nsXBLMouseEventHandler::EventMatched(nsIDOMEvent* aEvent)
    63 {
    64   nsCOMPtr<nsIDOMMouseEvent> mouse(do_QueryInterface(aEvent));
    65   return mouse && mProtoHandler->MouseEventMatched(mouse);
    66 }
    68 nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom* aEventType, uint8_t aPhase,
    69                                            uint8_t aType)
    70   : mEventType(aEventType),
    71     mPhase(aPhase),
    72     mType(aType),
    73     mIsBoundToChrome(false),
    74     mUsingXBLScope(false)
    75 {
    76 }
    78 nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
    79 {
    80 }
    82 NS_IMPL_ISUPPORTS(nsXBLKeyEventHandler, nsIDOMEventListener)
    84 bool
    85 nsXBLKeyEventHandler::ExecuteMatchedHandlers(nsIDOMKeyEvent* aKeyEvent,
    86                                              uint32_t aCharCode,
    87                                              bool aIgnoreShiftKey)
    88 {
    89   bool trustedEvent = false;
    90   aKeyEvent->GetIsTrusted(&trustedEvent);
    92   nsCOMPtr<EventTarget> target = aKeyEvent->InternalDOMEvent()->GetCurrentTarget();
    94   bool executed = false;
    95   for (uint32_t i = 0; i < mProtoHandlers.Length(); ++i) {
    96     nsXBLPrototypeHandler* handler = mProtoHandlers[i];
    97     bool hasAllowUntrustedAttr = handler->HasAllowUntrustedAttr();
    98     if ((trustedEvent ||
    99         (hasAllowUntrustedAttr && handler->AllowUntrustedEvents()) ||
   100         (!hasAllowUntrustedAttr && !mIsBoundToChrome && !mUsingXBLScope)) &&
   101         handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreShiftKey)) {
   102       handler->ExecuteHandler(target, aKeyEvent);
   103       executed = true;
   104     }
   105   }
   106   return executed;
   107 }
   109 NS_IMETHODIMP
   110 nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent)
   111 {
   112   uint32_t count = mProtoHandlers.Length();
   113   if (count == 0)
   114     return NS_ERROR_FAILURE;
   116   if (mPhase == NS_PHASE_TARGET) {
   117     uint16_t eventPhase;
   118     aEvent->GetEventPhase(&eventPhase);
   119     if (eventPhase != nsIDOMEvent::AT_TARGET)
   120       return NS_OK;
   121   }
   123   nsCOMPtr<nsIDOMKeyEvent> key(do_QueryInterface(aEvent));
   124   if (!key)
   125     return NS_OK;
   127   nsAutoTArray<nsShortcutCandidate, 10> accessKeys;
   128   nsContentUtils::GetAccelKeyCandidates(key, accessKeys);
   130   if (accessKeys.IsEmpty()) {
   131     ExecuteMatchedHandlers(key, 0, false);
   132     return NS_OK;
   133   }
   135   for (uint32_t i = 0; i < accessKeys.Length(); ++i) {
   136     if (ExecuteMatchedHandlers(key, accessKeys[i].mCharCode,
   137                                accessKeys[i].mIgnoreShift))
   138       return NS_OK;
   139   }
   140   return NS_OK;
   141 }
   143 ///////////////////////////////////////////////////////////////////////////////////
   145 nsresult
   146 NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler,
   147                       nsIAtom* aEventType,
   148                       nsXBLEventHandler** aResult)
   149 {
   150   switch (nsContentUtils::GetEventCategory(nsDependentAtomString(aEventType))) {
   151     case NS_DRAG_EVENT:
   152     case NS_MOUSE_EVENT:
   153     case NS_MOUSE_SCROLL_EVENT:
   154     case NS_WHEEL_EVENT:
   155     case NS_SIMPLE_GESTURE_EVENT:
   156       *aResult = new nsXBLMouseEventHandler(aHandler);
   157       break;
   158     default:
   159       *aResult = new nsXBLEventHandler(aHandler);
   160       break;
   161   }
   163   if (!*aResult)
   164     return NS_ERROR_OUT_OF_MEMORY;
   166   NS_ADDREF(*aResult);
   168   return NS_OK;
   169 }
   171 nsresult
   172 NS_NewXBLKeyEventHandler(nsIAtom* aEventType, uint8_t aPhase, uint8_t aType,
   173                          nsXBLKeyEventHandler** aResult)
   174 {
   175   *aResult = new nsXBLKeyEventHandler(aEventType, aPhase, aType);
   177   if (!*aResult)
   178     return NS_ERROR_OUT_OF_MEMORY;
   180   NS_ADDREF(*aResult);
   182   return NS_OK;
   183 }

mercurial