dom/xbl/nsXBLEventHandler.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xbl/nsXBLEventHandler.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsCOMPtr.h"
    1.10 +#include "nsIAtom.h"
    1.11 +#include "nsIDOMEventListener.h"
    1.12 +#include "nsIDOMKeyEvent.h"
    1.13 +#include "nsIDOMMouseEvent.h"
    1.14 +#include "nsXBLPrototypeHandler.h"
    1.15 +#include "nsContentUtils.h"
    1.16 +#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
    1.17 +#include "mozilla/dom/EventTarget.h"
    1.18 +
    1.19 +using namespace mozilla::dom;
    1.20 +
    1.21 +nsXBLEventHandler::nsXBLEventHandler(nsXBLPrototypeHandler* aHandler)
    1.22 +  : mProtoHandler(aHandler)
    1.23 +{
    1.24 +}
    1.25 +
    1.26 +nsXBLEventHandler::~nsXBLEventHandler()
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +NS_IMPL_ISUPPORTS(nsXBLEventHandler, nsIDOMEventListener)
    1.31 +
    1.32 +NS_IMETHODIMP
    1.33 +nsXBLEventHandler::HandleEvent(nsIDOMEvent* aEvent)
    1.34 +{
    1.35 +  if (!mProtoHandler)
    1.36 +    return NS_ERROR_FAILURE;
    1.37 +
    1.38 +  uint8_t phase = mProtoHandler->GetPhase();
    1.39 +  if (phase == NS_PHASE_TARGET) {
    1.40 +    uint16_t eventPhase;
    1.41 +    aEvent->GetEventPhase(&eventPhase);
    1.42 +    if (eventPhase != nsIDOMEvent::AT_TARGET)
    1.43 +      return NS_OK;
    1.44 +  }
    1.45 +
    1.46 +  if (!EventMatched(aEvent))
    1.47 +    return NS_OK;
    1.48 +
    1.49 +  mProtoHandler->ExecuteHandler(aEvent->InternalDOMEvent()->GetCurrentTarget(),
    1.50 +                                aEvent);
    1.51 +
    1.52 +  return NS_OK;
    1.53 +}
    1.54 +
    1.55 +nsXBLMouseEventHandler::nsXBLMouseEventHandler(nsXBLPrototypeHandler* aHandler)
    1.56 +  : nsXBLEventHandler(aHandler)
    1.57 +{
    1.58 +}
    1.59 +
    1.60 +nsXBLMouseEventHandler::~nsXBLMouseEventHandler()
    1.61 +{
    1.62 +}
    1.63 +
    1.64 +bool
    1.65 +nsXBLMouseEventHandler::EventMatched(nsIDOMEvent* aEvent)
    1.66 +{
    1.67 +  nsCOMPtr<nsIDOMMouseEvent> mouse(do_QueryInterface(aEvent));
    1.68 +  return mouse && mProtoHandler->MouseEventMatched(mouse);
    1.69 +}
    1.70 +
    1.71 +nsXBLKeyEventHandler::nsXBLKeyEventHandler(nsIAtom* aEventType, uint8_t aPhase,
    1.72 +                                           uint8_t aType)
    1.73 +  : mEventType(aEventType),
    1.74 +    mPhase(aPhase),
    1.75 +    mType(aType),
    1.76 +    mIsBoundToChrome(false),
    1.77 +    mUsingXBLScope(false)
    1.78 +{
    1.79 +}
    1.80 +
    1.81 +nsXBLKeyEventHandler::~nsXBLKeyEventHandler()
    1.82 +{
    1.83 +}
    1.84 +
    1.85 +NS_IMPL_ISUPPORTS(nsXBLKeyEventHandler, nsIDOMEventListener)
    1.86 +
    1.87 +bool
    1.88 +nsXBLKeyEventHandler::ExecuteMatchedHandlers(nsIDOMKeyEvent* aKeyEvent,
    1.89 +                                             uint32_t aCharCode,
    1.90 +                                             bool aIgnoreShiftKey)
    1.91 +{
    1.92 +  bool trustedEvent = false;
    1.93 +  aKeyEvent->GetIsTrusted(&trustedEvent);
    1.94 +
    1.95 +  nsCOMPtr<EventTarget> target = aKeyEvent->InternalDOMEvent()->GetCurrentTarget();
    1.96 +
    1.97 +  bool executed = false;
    1.98 +  for (uint32_t i = 0; i < mProtoHandlers.Length(); ++i) {
    1.99 +    nsXBLPrototypeHandler* handler = mProtoHandlers[i];
   1.100 +    bool hasAllowUntrustedAttr = handler->HasAllowUntrustedAttr();
   1.101 +    if ((trustedEvent ||
   1.102 +        (hasAllowUntrustedAttr && handler->AllowUntrustedEvents()) ||
   1.103 +        (!hasAllowUntrustedAttr && !mIsBoundToChrome && !mUsingXBLScope)) &&
   1.104 +        handler->KeyEventMatched(aKeyEvent, aCharCode, aIgnoreShiftKey)) {
   1.105 +      handler->ExecuteHandler(target, aKeyEvent);
   1.106 +      executed = true;
   1.107 +    }
   1.108 +  }
   1.109 +  return executed;
   1.110 +}
   1.111 +
   1.112 +NS_IMETHODIMP
   1.113 +nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent)
   1.114 +{
   1.115 +  uint32_t count = mProtoHandlers.Length();
   1.116 +  if (count == 0)
   1.117 +    return NS_ERROR_FAILURE;
   1.118 +
   1.119 +  if (mPhase == NS_PHASE_TARGET) {
   1.120 +    uint16_t eventPhase;
   1.121 +    aEvent->GetEventPhase(&eventPhase);
   1.122 +    if (eventPhase != nsIDOMEvent::AT_TARGET)
   1.123 +      return NS_OK;
   1.124 +  }
   1.125 +
   1.126 +  nsCOMPtr<nsIDOMKeyEvent> key(do_QueryInterface(aEvent));
   1.127 +  if (!key)
   1.128 +    return NS_OK;
   1.129 +
   1.130 +  nsAutoTArray<nsShortcutCandidate, 10> accessKeys;
   1.131 +  nsContentUtils::GetAccelKeyCandidates(key, accessKeys);
   1.132 +
   1.133 +  if (accessKeys.IsEmpty()) {
   1.134 +    ExecuteMatchedHandlers(key, 0, false);
   1.135 +    return NS_OK;
   1.136 +  }
   1.137 +
   1.138 +  for (uint32_t i = 0; i < accessKeys.Length(); ++i) {
   1.139 +    if (ExecuteMatchedHandlers(key, accessKeys[i].mCharCode,
   1.140 +                               accessKeys[i].mIgnoreShift))
   1.141 +      return NS_OK;
   1.142 +  }
   1.143 +  return NS_OK;
   1.144 +}
   1.145 +
   1.146 +///////////////////////////////////////////////////////////////////////////////////
   1.147 +
   1.148 +nsresult
   1.149 +NS_NewXBLEventHandler(nsXBLPrototypeHandler* aHandler,
   1.150 +                      nsIAtom* aEventType,
   1.151 +                      nsXBLEventHandler** aResult)
   1.152 +{
   1.153 +  switch (nsContentUtils::GetEventCategory(nsDependentAtomString(aEventType))) {
   1.154 +    case NS_DRAG_EVENT:
   1.155 +    case NS_MOUSE_EVENT:
   1.156 +    case NS_MOUSE_SCROLL_EVENT:
   1.157 +    case NS_WHEEL_EVENT:
   1.158 +    case NS_SIMPLE_GESTURE_EVENT:
   1.159 +      *aResult = new nsXBLMouseEventHandler(aHandler);
   1.160 +      break;
   1.161 +    default:
   1.162 +      *aResult = new nsXBLEventHandler(aHandler);
   1.163 +      break;
   1.164 +  }
   1.165 +
   1.166 +  if (!*aResult)
   1.167 +    return NS_ERROR_OUT_OF_MEMORY;
   1.168 +
   1.169 +  NS_ADDREF(*aResult);
   1.170 +
   1.171 +  return NS_OK;
   1.172 +}
   1.173 +
   1.174 +nsresult
   1.175 +NS_NewXBLKeyEventHandler(nsIAtom* aEventType, uint8_t aPhase, uint8_t aType,
   1.176 +                         nsXBLKeyEventHandler** aResult)
   1.177 +{
   1.178 +  *aResult = new nsXBLKeyEventHandler(aEventType, aPhase, aType);
   1.179 +
   1.180 +  if (!*aResult)
   1.181 +    return NS_ERROR_OUT_OF_MEMORY;
   1.182 +
   1.183 +  NS_ADDREF(*aResult);
   1.184 +
   1.185 +  return NS_OK;
   1.186 +}

mercurial