Bumping manifests a=b2g-bump
[gecko.git] / dom / xbl / nsXBLPrototypeHandler.h
blobd293c237a5c671e3a786ab31d00f5141fbd4cca5
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 #ifndef nsXBLPrototypeHandler_h__
7 #define nsXBLPrototypeHandler_h__
9 #include "nsIAtom.h"
10 #include "nsString.h"
11 #include "nsCOMPtr.h"
12 #include "nsIController.h"
13 #include "nsAutoPtr.h"
14 #include "nsXBLEventHandler.h"
15 #include "nsIWeakReference.h"
16 #include "nsCycleCollectionParticipant.h"
17 #include "js/TypeDecls.h"
19 class nsIDOMEvent;
20 class nsIContent;
21 class nsIDOMUIEvent;
22 class nsIDOMKeyEvent;
23 class nsIDOMMouseEvent;
24 class nsIObjectInputStream;
25 class nsIObjectOutputStream;
26 class nsXBLPrototypeBinding;
28 namespace mozilla {
29 namespace dom {
30 class AutoJSAPI;
31 class EventTarget;
35 #define NS_HANDLER_TYPE_XBL_JS (1 << 0)
36 #define NS_HANDLER_TYPE_XBL_COMMAND (1 << 1)
37 #define NS_HANDLER_TYPE_XUL (1 << 2)
38 #define NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR (1 << 4)
39 #define NS_HANDLER_ALLOW_UNTRUSTED (1 << 5)
40 #define NS_HANDLER_TYPE_SYSTEM (1 << 6)
41 #define NS_HANDLER_TYPE_PREVENTDEFAULT (1 << 7)
43 // XXX Use nsIDOMEvent:: codes?
44 #define NS_PHASE_CAPTURING 1
45 #define NS_PHASE_TARGET 2
46 #define NS_PHASE_BUBBLING 3
48 namespace mozilla {
49 namespace dom {
51 struct IgnoreModifierState
53 // When mShift is true, Shift key state will be ignored.
54 bool mShift;
55 // When mOS is true, OS key state will be ignored.
56 bool mOS;
58 IgnoreModifierState()
59 : mShift(false)
60 , mOS(false)
65 } // namespace dom
66 } // namespace mozilla
68 class nsXBLPrototypeHandler
70 typedef mozilla::dom::IgnoreModifierState IgnoreModifierState;
72 public:
73 // This constructor is used by XBL handlers (both the JS and command shorthand variety)
74 nsXBLPrototypeHandler(const char16_t* aEvent, const char16_t* aPhase,
75 const char16_t* aAction, const char16_t* aCommand,
76 const char16_t* aKeyCode, const char16_t* aCharCode,
77 const char16_t* aModifiers, const char16_t* aButton,
78 const char16_t* aClickCount, const char16_t* aGroup,
79 const char16_t* aPreventDefault,
80 const char16_t* aAllowUntrusted,
81 nsXBLPrototypeBinding* aBinding,
82 uint32_t aLineNumber);
84 // This constructor is used only by XUL key handlers (e.g., <key>)
85 explicit nsXBLPrototypeHandler(nsIContent* aKeyElement);
87 // This constructor is used for handlers loaded from the cache
88 explicit nsXBLPrototypeHandler(nsXBLPrototypeBinding* aBinding);
90 ~nsXBLPrototypeHandler();
92 // if aCharCode is not zero, it is used instead of the charCode of aKeyEvent.
93 bool KeyEventMatched(nsIDOMKeyEvent* aKeyEvent,
94 uint32_t aCharCode,
95 const IgnoreModifierState& aIgnoreModifierState);
96 inline bool KeyEventMatched(nsIAtom* aEventType,
97 nsIDOMKeyEvent* aEvent,
98 uint32_t aCharCode,
99 const IgnoreModifierState& aIgnoreModifierState)
101 if (aEventType != mEventName)
102 return false;
104 return KeyEventMatched(aEvent, aCharCode, aIgnoreModifierState);
107 bool MouseEventMatched(nsIDOMMouseEvent* aMouseEvent);
108 inline bool MouseEventMatched(nsIAtom* aEventType,
109 nsIDOMMouseEvent* aEvent)
111 if (aEventType != mEventName)
112 return false;
114 return MouseEventMatched(aEvent);
117 already_AddRefed<nsIContent> GetHandlerElement();
119 void AppendHandlerText(const nsAString& aText);
121 uint8_t GetPhase() { return mPhase; }
122 uint8_t GetType() { return mType; }
124 nsXBLPrototypeHandler* GetNextHandler() { return mNextHandler; }
125 void SetNextHandler(nsXBLPrototypeHandler* aHandler) { mNextHandler = aHandler; }
127 nsresult ExecuteHandler(mozilla::dom::EventTarget* aTarget, nsIDOMEvent* aEvent);
129 already_AddRefed<nsIAtom> GetEventName();
130 void SetEventName(nsIAtom* aName) { mEventName = aName; }
132 nsXBLEventHandler* GetEventHandler()
134 if (!mHandler) {
135 NS_NewXBLEventHandler(this, mEventName, getter_AddRefs(mHandler));
136 // XXX Need to signal out of memory?
139 return mHandler;
142 nsXBLEventHandler* GetCachedEventHandler()
144 return mHandler;
147 bool HasAllowUntrustedAttr()
149 return (mType & NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR) != 0;
152 // This returns a valid value only if HasAllowUntrustedEventsAttr returns
153 // true.
154 bool AllowUntrustedEvents()
156 return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0;
159 nsresult Read(nsIObjectInputStream* aStream);
160 nsresult Write(nsIObjectOutputStream* aStream);
162 public:
163 static uint32_t gRefCnt;
165 protected:
166 void Init() {
167 ++gRefCnt;
168 if (gRefCnt == 1)
169 // Get the primary accelerator key.
170 InitAccessKeys();
173 already_AddRefed<nsIController> GetController(mozilla::dom::EventTarget* aTarget);
175 inline int32_t GetMatchingKeyCode(const nsAString& aKeyName);
176 void ConstructPrototype(nsIContent* aKeyElement,
177 const char16_t* aEvent=nullptr, const char16_t* aPhase=nullptr,
178 const char16_t* aAction=nullptr, const char16_t* aCommand=nullptr,
179 const char16_t* aKeyCode=nullptr, const char16_t* aCharCode=nullptr,
180 const char16_t* aModifiers=nullptr, const char16_t* aButton=nullptr,
181 const char16_t* aClickCount=nullptr, const char16_t* aGroup=nullptr,
182 const char16_t* aPreventDefault=nullptr,
183 const char16_t* aAllowUntrusted=nullptr);
185 void ReportKeyConflict(const char16_t* aKey, const char16_t* aModifiers, nsIContent* aElement, const char *aMessageName);
186 void GetEventType(nsAString& type);
187 bool ModifiersMatchMask(nsIDOMUIEvent* aEvent,
188 const IgnoreModifierState& aIgnoreModifierState);
189 nsresult DispatchXBLCommand(mozilla::dom::EventTarget* aTarget, nsIDOMEvent* aEvent);
190 nsresult DispatchXULKeyCommand(nsIDOMEvent* aEvent);
191 nsresult EnsureEventHandler(mozilla::dom::AutoJSAPI& jsapi, nsIAtom* aName,
192 JS::MutableHandle<JSObject*> aHandler);
193 static int32_t KeyToMask(int32_t key);
194 static int32_t AccelKeyMask();
196 static int32_t kMenuAccessKey;
197 static void InitAccessKeys();
199 static const int32_t cShift;
200 static const int32_t cAlt;
201 static const int32_t cControl;
202 static const int32_t cMeta;
203 static const int32_t cOS;
205 static const int32_t cShiftMask;
206 static const int32_t cAltMask;
207 static const int32_t cControlMask;
208 static const int32_t cMetaMask;
209 static const int32_t cOSMask;
211 static const int32_t cAllModifiers;
213 protected:
214 union {
215 nsIWeakReference* mHandlerElement; // For XUL <key> element handlers. [STRONG]
216 char16_t* mHandlerText; // For XBL handlers (we don't build an
217 // element for the <handler>, and instead
218 // we cache the JS text or command name
219 // that we should use.
222 uint32_t mLineNumber; // The line number we started at in the XBL file
224 // The following four values make up 32 bits.
225 uint8_t mPhase; // The phase (capturing, bubbling)
226 uint8_t mType; // The type of the handler. The handler is either a XUL key
227 // handler, an XBL "command" event, or a normal XBL event with
228 // accompanying JavaScript. The high bit is used to indicate
229 // whether this handler should prevent the default action.
230 uint8_t mMisc; // Miscellaneous extra information. For key events,
231 // stores whether or not we're a key code or char code.
232 // For mouse events, stores the clickCount.
234 int32_t mKeyMask; // Which modifier keys this event handler expects to have down
235 // in order to be matched.
237 // The primary filter information for mouse/key events.
238 int32_t mDetail; // For key events, contains a charcode or keycode. For
239 // mouse events, stores the button info.
241 // Prototype handlers are chained. We own the next handler in the chain.
242 nsXBLPrototypeHandler* mNextHandler;
243 nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress"
244 nsRefPtr<nsXBLEventHandler> mHandler;
245 nsXBLPrototypeBinding* mPrototypeBinding; // the binding owns us
248 #endif