1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xbl/nsXBLPrototypeHandler.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,227 @@ 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 +#ifndef nsXBLPrototypeHandler_h__ 1.10 +#define nsXBLPrototypeHandler_h__ 1.11 + 1.12 +#include "nsIAtom.h" 1.13 +#include "nsString.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsIController.h" 1.16 +#include "nsAutoPtr.h" 1.17 +#include "nsXBLEventHandler.h" 1.18 +#include "nsIWeakReference.h" 1.19 +#include "nsIScriptGlobalObject.h" 1.20 +#include "nsCycleCollectionParticipant.h" 1.21 +#include "js/TypeDecls.h" 1.22 + 1.23 +class nsIDOMEvent; 1.24 +class nsIContent; 1.25 +class nsIDOMUIEvent; 1.26 +class nsIDOMKeyEvent; 1.27 +class nsIDOMMouseEvent; 1.28 +class nsIObjectInputStream; 1.29 +class nsIObjectOutputStream; 1.30 +class nsXBLPrototypeBinding; 1.31 + 1.32 +namespace mozilla { 1.33 +namespace dom { 1.34 +class EventTarget; 1.35 +} 1.36 +} 1.37 + 1.38 +#define NS_HANDLER_TYPE_XBL_JS (1 << 0) 1.39 +#define NS_HANDLER_TYPE_XBL_COMMAND (1 << 1) 1.40 +#define NS_HANDLER_TYPE_XUL (1 << 2) 1.41 +#define NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR (1 << 4) 1.42 +#define NS_HANDLER_ALLOW_UNTRUSTED (1 << 5) 1.43 +#define NS_HANDLER_TYPE_SYSTEM (1 << 6) 1.44 +#define NS_HANDLER_TYPE_PREVENTDEFAULT (1 << 7) 1.45 + 1.46 +// XXX Use nsIDOMEvent:: codes? 1.47 +#define NS_PHASE_CAPTURING 1 1.48 +#define NS_PHASE_TARGET 2 1.49 +#define NS_PHASE_BUBBLING 3 1.50 + 1.51 +class nsXBLPrototypeHandler 1.52 +{ 1.53 +public: 1.54 + // This constructor is used by XBL handlers (both the JS and command shorthand variety) 1.55 + nsXBLPrototypeHandler(const char16_t* aEvent, const char16_t* aPhase, 1.56 + const char16_t* aAction, const char16_t* aCommand, 1.57 + const char16_t* aKeyCode, const char16_t* aCharCode, 1.58 + const char16_t* aModifiers, const char16_t* aButton, 1.59 + const char16_t* aClickCount, const char16_t* aGroup, 1.60 + const char16_t* aPreventDefault, 1.61 + const char16_t* aAllowUntrusted, 1.62 + nsXBLPrototypeBinding* aBinding, 1.63 + uint32_t aLineNumber); 1.64 + 1.65 + // This constructor is used only by XUL key handlers (e.g., <key>) 1.66 + nsXBLPrototypeHandler(nsIContent* aKeyElement); 1.67 + 1.68 + // This constructor is used for handlers loaded from the cache 1.69 + nsXBLPrototypeHandler(nsXBLPrototypeBinding* aBinding); 1.70 + 1.71 + ~nsXBLPrototypeHandler(); 1.72 + 1.73 + // if aCharCode is not zero, it is used instead of the charCode of aKeyEvent. 1.74 + bool KeyEventMatched(nsIDOMKeyEvent* aKeyEvent, 1.75 + uint32_t aCharCode = 0, 1.76 + bool aIgnoreShiftKey = false); 1.77 + inline bool KeyEventMatched(nsIAtom* aEventType, 1.78 + nsIDOMKeyEvent* aEvent, 1.79 + uint32_t aCharCode = 0, 1.80 + bool aIgnoreShiftKey = false) 1.81 + { 1.82 + if (aEventType != mEventName) 1.83 + return false; 1.84 + 1.85 + return KeyEventMatched(aEvent, aCharCode, aIgnoreShiftKey); 1.86 + } 1.87 + 1.88 + bool MouseEventMatched(nsIDOMMouseEvent* aMouseEvent); 1.89 + inline bool MouseEventMatched(nsIAtom* aEventType, 1.90 + nsIDOMMouseEvent* aEvent) 1.91 + { 1.92 + if (aEventType != mEventName) 1.93 + return false; 1.94 + 1.95 + return MouseEventMatched(aEvent); 1.96 + } 1.97 + 1.98 + already_AddRefed<nsIContent> GetHandlerElement(); 1.99 + 1.100 + void AppendHandlerText(const nsAString& aText); 1.101 + 1.102 + uint8_t GetPhase() { return mPhase; } 1.103 + uint8_t GetType() { return mType; } 1.104 + 1.105 + nsXBLPrototypeHandler* GetNextHandler() { return mNextHandler; } 1.106 + void SetNextHandler(nsXBLPrototypeHandler* aHandler) { mNextHandler = aHandler; } 1.107 + 1.108 + nsresult ExecuteHandler(mozilla::dom::EventTarget* aTarget, nsIDOMEvent* aEvent); 1.109 + 1.110 + already_AddRefed<nsIAtom> GetEventName(); 1.111 + void SetEventName(nsIAtom* aName) { mEventName = aName; } 1.112 + 1.113 + nsXBLEventHandler* GetEventHandler() 1.114 + { 1.115 + if (!mHandler) { 1.116 + NS_NewXBLEventHandler(this, mEventName, getter_AddRefs(mHandler)); 1.117 + // XXX Need to signal out of memory? 1.118 + } 1.119 + 1.120 + return mHandler; 1.121 + } 1.122 + 1.123 + nsXBLEventHandler* GetCachedEventHandler() 1.124 + { 1.125 + return mHandler; 1.126 + } 1.127 + 1.128 + bool HasAllowUntrustedAttr() 1.129 + { 1.130 + return (mType & NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR) != 0; 1.131 + } 1.132 + 1.133 + // This returns a valid value only if HasAllowUntrustedEventsAttr returns 1.134 + // true. 1.135 + bool AllowUntrustedEvents() 1.136 + { 1.137 + return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0; 1.138 + } 1.139 + 1.140 + nsresult Read(nsIObjectInputStream* aStream); 1.141 + nsresult Write(nsIObjectOutputStream* aStream); 1.142 + 1.143 +public: 1.144 + static uint32_t gRefCnt; 1.145 + 1.146 +protected: 1.147 + void Init() { 1.148 + ++gRefCnt; 1.149 + if (gRefCnt == 1) 1.150 + // Get the primary accelerator key. 1.151 + InitAccessKeys(); 1.152 + } 1.153 + 1.154 + already_AddRefed<nsIController> GetController(mozilla::dom::EventTarget* aTarget); 1.155 + 1.156 + inline int32_t GetMatchingKeyCode(const nsAString& aKeyName); 1.157 + void ConstructPrototype(nsIContent* aKeyElement, 1.158 + const char16_t* aEvent=nullptr, const char16_t* aPhase=nullptr, 1.159 + const char16_t* aAction=nullptr, const char16_t* aCommand=nullptr, 1.160 + const char16_t* aKeyCode=nullptr, const char16_t* aCharCode=nullptr, 1.161 + const char16_t* aModifiers=nullptr, const char16_t* aButton=nullptr, 1.162 + const char16_t* aClickCount=nullptr, const char16_t* aGroup=nullptr, 1.163 + const char16_t* aPreventDefault=nullptr, 1.164 + const char16_t* aAllowUntrusted=nullptr); 1.165 + 1.166 + void ReportKeyConflict(const char16_t* aKey, const char16_t* aModifiers, nsIContent* aElement, const char *aMessageName); 1.167 + void GetEventType(nsAString& type); 1.168 + bool ModifiersMatchMask(nsIDOMUIEvent* aEvent, 1.169 + bool aIgnoreShiftKey = false); 1.170 + nsresult DispatchXBLCommand(mozilla::dom::EventTarget* aTarget, nsIDOMEvent* aEvent); 1.171 + nsresult DispatchXULKeyCommand(nsIDOMEvent* aEvent); 1.172 + nsresult EnsureEventHandler(nsIScriptGlobalObject* aGlobal, 1.173 + nsIScriptContext *aBoundContext, nsIAtom *aName, 1.174 + JS::MutableHandle<JSObject*> aHandler); 1.175 + static int32_t KeyToMask(int32_t key); 1.176 + 1.177 + static int32_t kAccelKey; 1.178 + static int32_t kMenuAccessKey; 1.179 + static void InitAccessKeys(); 1.180 + 1.181 + static const int32_t cShift; 1.182 + static const int32_t cAlt; 1.183 + static const int32_t cControl; 1.184 + static const int32_t cMeta; 1.185 + static const int32_t cOS; 1.186 + 1.187 + static const int32_t cShiftMask; 1.188 + static const int32_t cAltMask; 1.189 + static const int32_t cControlMask; 1.190 + static const int32_t cMetaMask; 1.191 + static const int32_t cOSMask; 1.192 + 1.193 + static const int32_t cAllModifiers; 1.194 + 1.195 +protected: 1.196 + union { 1.197 + nsIWeakReference* mHandlerElement; // For XUL <key> element handlers. [STRONG] 1.198 + char16_t* mHandlerText; // For XBL handlers (we don't build an 1.199 + // element for the <handler>, and instead 1.200 + // we cache the JS text or command name 1.201 + // that we should use. 1.202 + }; 1.203 + 1.204 + uint32_t mLineNumber; // The line number we started at in the XBL file 1.205 + 1.206 + // The following four values make up 32 bits. 1.207 + uint8_t mPhase; // The phase (capturing, bubbling) 1.208 + uint8_t mType; // The type of the handler. The handler is either a XUL key 1.209 + // handler, an XBL "command" event, or a normal XBL event with 1.210 + // accompanying JavaScript. The high bit is used to indicate 1.211 + // whether this handler should prevent the default action. 1.212 + uint8_t mMisc; // Miscellaneous extra information. For key events, 1.213 + // stores whether or not we're a key code or char code. 1.214 + // For mouse events, stores the clickCount. 1.215 + 1.216 + int32_t mKeyMask; // Which modifier keys this event handler expects to have down 1.217 + // in order to be matched. 1.218 + 1.219 + // The primary filter information for mouse/key events. 1.220 + int32_t mDetail; // For key events, contains a charcode or keycode. For 1.221 + // mouse events, stores the button info. 1.222 + 1.223 + // Prototype handlers are chained. We own the next handler in the chain. 1.224 + nsXBLPrototypeHandler* mNextHandler; 1.225 + nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress" 1.226 + nsRefPtr<nsXBLEventHandler> mHandler; 1.227 + nsXBLPrototypeBinding* mPrototypeBinding; // the binding owns us 1.228 +}; 1.229 + 1.230 +#endif