Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef nsXBLPrototypeHandler_h__
8 : #define nsXBLPrototypeHandler_h__
9 :
10 : #include "mozilla/EventForwards.h"
11 : #include "nsIAtom.h"
12 : #include "nsString.h"
13 : #include "nsCOMPtr.h"
14 : #include "nsIController.h"
15 : #include "nsAutoPtr.h"
16 : #include "nsXBLEventHandler.h"
17 : #include "nsIWeakReference.h"
18 : #include "nsCycleCollectionParticipant.h"
19 : #include "js/TypeDecls.h"
20 :
21 : class nsIDOMEvent;
22 : class nsIContent;
23 : class nsIDOMUIEvent;
24 : class nsIDOMKeyEvent;
25 : class nsIDOMMouseEvent;
26 : class nsIObjectInputStream;
27 : class nsIObjectOutputStream;
28 : class nsXBLPrototypeBinding;
29 :
30 : namespace mozilla {
31 :
32 : struct IgnoreModifierState;
33 :
34 : namespace dom {
35 : class AutoJSAPI;
36 : class EventTarget;
37 : } // namespace dom
38 :
39 : namespace layers {
40 : class KeyboardShortcut;
41 : } // namespace layers
42 :
43 : } // namespace mozilla
44 :
45 : #define NS_HANDLER_TYPE_XBL_JS (1 << 0)
46 : #define NS_HANDLER_TYPE_XBL_COMMAND (1 << 1)
47 : #define NS_HANDLER_TYPE_XUL (1 << 2)
48 : #define NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR (1 << 4)
49 : #define NS_HANDLER_ALLOW_UNTRUSTED (1 << 5)
50 : #define NS_HANDLER_TYPE_SYSTEM (1 << 6)
51 : #define NS_HANDLER_TYPE_PREVENTDEFAULT (1 << 7)
52 :
53 : // XXX Use nsIDOMEvent:: codes?
54 : #define NS_PHASE_CAPTURING 1
55 : #define NS_PHASE_TARGET 2
56 : #define NS_PHASE_BUBBLING 3
57 :
58 : class nsXBLPrototypeHandler
59 : {
60 : typedef mozilla::IgnoreModifierState IgnoreModifierState;
61 : typedef mozilla::layers::KeyboardShortcut KeyboardShortcut;
62 : typedef mozilla::Modifiers Modifiers;
63 :
64 : public:
65 : // This constructor is used by XBL handlers (both the JS and command shorthand variety)
66 : nsXBLPrototypeHandler(const char16_t* aEvent, const char16_t* aPhase,
67 : const char16_t* aAction, const char16_t* aCommand,
68 : const char16_t* aKeyCode, const char16_t* aCharCode,
69 : const char16_t* aModifiers, const char16_t* aButton,
70 : const char16_t* aClickCount, const char16_t* aGroup,
71 : const char16_t* aPreventDefault,
72 : const char16_t* aAllowUntrusted,
73 : nsXBLPrototypeBinding* aBinding,
74 : uint32_t aLineNumber);
75 :
76 : // This constructor is used only by XUL key handlers (e.g., <key>)
77 : explicit nsXBLPrototypeHandler(nsIContent* aKeyElement, bool aReserved);
78 :
79 : // This constructor is used for handlers loaded from the cache
80 : explicit nsXBLPrototypeHandler(nsXBLPrototypeBinding* aBinding);
81 :
82 : ~nsXBLPrototypeHandler();
83 :
84 : /**
85 : * Try and convert this XBL handler into an APZ KeyboardShortcut for handling
86 : * key events on the compositor thread. This only works for XBL handlers that
87 : * represent scroll commands.
88 : *
89 : * @param aOut the converted KeyboardShortcut, must be non null
90 : * @return whether the handler was converted into a KeyboardShortcut
91 : */
92 : bool TryConvertToKeyboardShortcut(
93 : KeyboardShortcut* aOut) const;
94 :
95 0 : bool EventTypeEquals(nsIAtom* aEventType) const
96 : {
97 0 : return mEventName == aEventType;
98 : }
99 :
100 : // if aCharCode is not zero, it is used instead of the charCode of aKeyEvent.
101 : bool KeyEventMatched(nsIDOMKeyEvent* aKeyEvent,
102 : uint32_t aCharCode,
103 : const IgnoreModifierState& aIgnoreModifierState);
104 :
105 : bool MouseEventMatched(nsIDOMMouseEvent* aMouseEvent);
106 : inline bool MouseEventMatched(nsIAtom* aEventType,
107 : nsIDOMMouseEvent* aEvent)
108 : {
109 : if (!EventTypeEquals(aEventType)) {
110 : return false;
111 : }
112 : return MouseEventMatched(aEvent);
113 : }
114 :
115 : already_AddRefed<nsIContent> GetHandlerElement();
116 :
117 : void AppendHandlerText(const nsAString& aText);
118 :
119 481 : uint8_t GetPhase() { return mPhase; }
120 475 : uint8_t GetType() { return mType; }
121 0 : bool GetIsReserved() { return mReserved; }
122 :
123 734 : nsXBLPrototypeHandler* GetNextHandler() { return mNextHandler; }
124 229 : void SetNextHandler(nsXBLPrototypeHandler* aHandler) { mNextHandler = aHandler; }
125 :
126 : nsresult ExecuteHandler(mozilla::dom::EventTarget* aTarget, nsIDOMEvent* aEvent);
127 :
128 : already_AddRefed<nsIAtom> GetEventName();
129 : void SetEventName(nsIAtom* aName) { mEventName = aName; }
130 :
131 432 : nsXBLEventHandler* GetEventHandler()
132 : {
133 432 : if (!mHandler) {
134 133 : mHandler = NS_NewXBLEventHandler(this, mEventName);
135 : }
136 :
137 432 : return mHandler;
138 : }
139 :
140 16 : nsXBLEventHandler* GetCachedEventHandler()
141 : {
142 16 : return mHandler;
143 : }
144 :
145 432 : bool HasAllowUntrustedAttr()
146 : {
147 432 : return (mType & NS_HANDLER_HAS_ALLOW_UNTRUSTED_ATTR) != 0;
148 : }
149 :
150 : // This returns a valid value only if HasAllowUntrustedEventsAttr returns
151 : // true.
152 0 : bool AllowUntrustedEvents()
153 : {
154 0 : return (mType & NS_HANDLER_ALLOW_UNTRUSTED) != 0;
155 : }
156 :
157 : nsresult Read(nsIObjectInputStream* aStream);
158 : nsresult Write(nsIObjectOutputStream* aStream);
159 :
160 : public:
161 : static uint32_t gRefCnt;
162 :
163 : protected:
164 284 : void Init() {
165 284 : ++gRefCnt;
166 284 : if (gRefCnt == 1)
167 : // Get the primary accelerator key.
168 2 : InitAccessKeys();
169 284 : }
170 :
171 : already_AddRefed<nsIController> GetController(mozilla::dom::EventTarget* aTarget);
172 :
173 : inline int32_t GetMatchingKeyCode(const nsAString& aKeyName);
174 : void ConstructPrototype(nsIContent* aKeyElement,
175 : const char16_t* aEvent=nullptr, const char16_t* aPhase=nullptr,
176 : const char16_t* aAction=nullptr, const char16_t* aCommand=nullptr,
177 : const char16_t* aKeyCode=nullptr, const char16_t* aCharCode=nullptr,
178 : const char16_t* aModifiers=nullptr, const char16_t* aButton=nullptr,
179 : const char16_t* aClickCount=nullptr, const char16_t* aGroup=nullptr,
180 : const char16_t* aPreventDefault=nullptr,
181 : const char16_t* aAllowUntrusted=nullptr);
182 :
183 : void ReportKeyConflict(const char16_t* aKey, const char16_t* aModifiers, nsIContent* aElement, const char *aMessageName);
184 : void GetEventType(nsAString& type);
185 : bool ModifiersMatchMask(nsIDOMUIEvent* aEvent,
186 : const IgnoreModifierState& aIgnoreModifierState);
187 : nsresult DispatchXBLCommand(mozilla::dom::EventTarget* aTarget, nsIDOMEvent* aEvent);
188 : nsresult DispatchXULKeyCommand(nsIDOMEvent* aEvent);
189 : nsresult EnsureEventHandler(mozilla::dom::AutoJSAPI& jsapi, nsIAtom* aName,
190 : JS::MutableHandle<JSObject*> aHandler);
191 :
192 : Modifiers GetModifiers() const;
193 : Modifiers GetModifiersMask() const;
194 :
195 : static int32_t KeyToMask(int32_t key);
196 : static int32_t AccelKeyMask();
197 :
198 : static int32_t kMenuAccessKey;
199 : static void InitAccessKeys();
200 :
201 : static const int32_t cShift;
202 : static const int32_t cAlt;
203 : static const int32_t cControl;
204 : static const int32_t cMeta;
205 : static const int32_t cOS;
206 :
207 : static const int32_t cShiftMask;
208 : static const int32_t cAltMask;
209 : static const int32_t cControlMask;
210 : static const int32_t cMetaMask;
211 : static const int32_t cOSMask;
212 :
213 : static const int32_t cAllModifiers;
214 :
215 : protected:
216 : union {
217 : nsIWeakReference* mHandlerElement; // For XUL <key> element handlers. [STRONG]
218 : char16_t* mHandlerText; // For XBL handlers (we don't build an
219 : // element for the <handler>, and instead
220 : // we cache the JS text or command name
221 : // that we should use.
222 : };
223 :
224 : uint32_t mLineNumber; // The line number we started at in the XBL file
225 :
226 : // The following four values make up 32 bits.
227 : uint8_t mPhase; // The phase (capturing, bubbling)
228 : uint8_t mType; // The type of the handler. The handler is either a XUL key
229 : // handler, an XBL "command" event, or a normal XBL event with
230 : // accompanying JavaScript. The high bit is used to indicate
231 : // whether this handler should prevent the default action.
232 : uint8_t mMisc; // Miscellaneous extra information. For key events,
233 : // stores whether or not we're a key code or char code.
234 : // For mouse events, stores the clickCount.
235 :
236 : bool mReserved; // <key> is reserved for chrome. Not used by handlers.
237 :
238 : int32_t mKeyMask; // Which modifier keys this event handler expects to have down
239 : // in order to be matched.
240 :
241 : // The primary filter information for mouse/key events.
242 : int32_t mDetail; // For key events, contains a charcode or keycode. For
243 : // mouse events, stores the button info.
244 :
245 : // Prototype handlers are chained. We own the next handler in the chain.
246 : nsXBLPrototypeHandler* mNextHandler;
247 : nsCOMPtr<nsIAtom> mEventName; // The type of the event, e.g., "keypress"
248 : RefPtr<nsXBLEventHandler> mHandler;
249 : nsXBLPrototypeBinding* mPrototypeBinding; // the binding owns us
250 : };
251 :
252 : #endif
|