|
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/. */ |
|
5 |
|
6 #ifndef mozilla_dom_Event_h_ |
|
7 #define mozilla_dom_Event_h_ |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 #include "mozilla/BasicEvents.h" |
|
11 #include "nsIDOMEvent.h" |
|
12 #include "nsISupports.h" |
|
13 #include "nsCOMPtr.h" |
|
14 #include "nsPIDOMWindow.h" |
|
15 #include "nsPoint.h" |
|
16 #include "nsCycleCollectionParticipant.h" |
|
17 #include "nsAutoPtr.h" |
|
18 #include "mozilla/dom/EventBinding.h" |
|
19 #include "nsIScriptGlobalObject.h" |
|
20 #include "Units.h" |
|
21 #include "js/TypeDecls.h" |
|
22 |
|
23 class nsIContent; |
|
24 class nsIDOMEventTarget; |
|
25 class nsPresContext; |
|
26 |
|
27 namespace mozilla { |
|
28 namespace dom { |
|
29 |
|
30 class EventTarget; |
|
31 class ErrorEvent; |
|
32 |
|
33 // Dummy class so we can cast through it to get from nsISupports to |
|
34 // Event subclasses with only two non-ambiguous static casts. |
|
35 class EventBase : public nsIDOMEvent |
|
36 { |
|
37 }; |
|
38 |
|
39 class Event : public EventBase, |
|
40 public nsWrapperCache |
|
41 { |
|
42 public: |
|
43 Event(EventTarget* aOwner, |
|
44 nsPresContext* aPresContext, |
|
45 WidgetEvent* aEvent); |
|
46 Event(nsPIDOMWindow* aWindow); |
|
47 virtual ~Event(); |
|
48 |
|
49 private: |
|
50 void ConstructorInit(EventTarget* aOwner, |
|
51 nsPresContext* aPresContext, |
|
52 WidgetEvent* aEvent); |
|
53 |
|
54 public: |
|
55 void GetParentObject(nsIScriptGlobalObject** aParentObject) |
|
56 { |
|
57 if (mOwner) { |
|
58 CallQueryInterface(mOwner, aParentObject); |
|
59 } else { |
|
60 *aParentObject = nullptr; |
|
61 } |
|
62 } |
|
63 |
|
64 static Event* FromSupports(nsISupports* aSupports) |
|
65 { |
|
66 nsIDOMEvent* event = |
|
67 static_cast<nsIDOMEvent*>(aSupports); |
|
68 #ifdef DEBUG |
|
69 { |
|
70 nsCOMPtr<nsIDOMEvent> target_qi = |
|
71 do_QueryInterface(aSupports); |
|
72 |
|
73 // If this assertion fires the QI implementation for the object in |
|
74 // question doesn't use the nsIDOMEvent pointer as the |
|
75 // nsISupports pointer. That must be fixed, or we'll crash... |
|
76 MOZ_ASSERT(target_qi == event, "Uh, fix QI!"); |
|
77 } |
|
78 #endif |
|
79 return static_cast<Event*>(event); |
|
80 } |
|
81 |
|
82 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
83 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event) |
|
84 |
|
85 nsISupports* GetParentObject() |
|
86 { |
|
87 return mOwner; |
|
88 } |
|
89 |
|
90 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE |
|
91 { |
|
92 return EventBinding::Wrap(aCx, this); |
|
93 } |
|
94 |
|
95 virtual ErrorEvent* AsErrorEvent() |
|
96 { |
|
97 return nullptr; |
|
98 } |
|
99 |
|
100 // nsIDOMEvent Interface |
|
101 NS_DECL_NSIDOMEVENT |
|
102 |
|
103 void InitPresContextData(nsPresContext* aPresContext); |
|
104 |
|
105 // Returns true if the event should be trusted. |
|
106 bool Init(EventTarget* aGlobal); |
|
107 |
|
108 static PopupControlState GetEventPopupControlState(WidgetEvent* aEvent); |
|
109 |
|
110 static void PopupAllowedEventsChanged(); |
|
111 |
|
112 static void Shutdown(); |
|
113 |
|
114 static const char* GetEventName(uint32_t aEventType); |
|
115 static CSSIntPoint GetClientCoords(nsPresContext* aPresContext, |
|
116 WidgetEvent* aEvent, |
|
117 LayoutDeviceIntPoint aPoint, |
|
118 CSSIntPoint aDefaultPoint); |
|
119 static CSSIntPoint GetPageCoords(nsPresContext* aPresContext, |
|
120 WidgetEvent* aEvent, |
|
121 LayoutDeviceIntPoint aPoint, |
|
122 CSSIntPoint aDefaultPoint); |
|
123 static nsIntPoint GetScreenCoords(nsPresContext* aPresContext, |
|
124 WidgetEvent* aEvent, |
|
125 LayoutDeviceIntPoint aPoint); |
|
126 |
|
127 static already_AddRefed<Event> Constructor(const GlobalObject& aGlobal, |
|
128 const nsAString& aType, |
|
129 const EventInit& aParam, |
|
130 ErrorResult& aRv); |
|
131 |
|
132 // Implemented as xpidl method |
|
133 // void GetType(nsString& aRetval) {} |
|
134 |
|
135 EventTarget* GetTarget() const; |
|
136 EventTarget* GetCurrentTarget() const; |
|
137 |
|
138 uint16_t EventPhase() const; |
|
139 |
|
140 // xpidl implementation |
|
141 // void StopPropagation(); |
|
142 |
|
143 // xpidl implementation |
|
144 // void StopImmediatePropagation(); |
|
145 |
|
146 bool Bubbles() const |
|
147 { |
|
148 return mEvent->mFlags.mBubbles; |
|
149 } |
|
150 |
|
151 bool Cancelable() const |
|
152 { |
|
153 return mEvent->mFlags.mCancelable; |
|
154 } |
|
155 |
|
156 // xpidl implementation |
|
157 // void PreventDefault(); |
|
158 |
|
159 // You MUST NOT call PreventDefaultJ(JSContext*) from C++ code. A call of |
|
160 // this method always sets Event.defaultPrevented true for web contents. |
|
161 // If default action handler calls this, web applications meet wrong |
|
162 // defaultPrevented value. |
|
163 void PreventDefault(JSContext* aCx); |
|
164 |
|
165 // You MUST NOT call DefaultPrevented(JSContext*) from C++ code. This may |
|
166 // return false even if PreventDefault() has been called. |
|
167 // See comments in its implementation for the detail. |
|
168 bool DefaultPrevented(JSContext* aCx) const; |
|
169 |
|
170 bool DefaultPrevented() const |
|
171 { |
|
172 return mEvent->mFlags.mDefaultPrevented; |
|
173 } |
|
174 |
|
175 bool MultipleActionsPrevented() const |
|
176 { |
|
177 return mEvent->mFlags.mMultipleActionsPrevented; |
|
178 } |
|
179 |
|
180 bool IsTrusted() const |
|
181 { |
|
182 return mEvent->mFlags.mIsTrusted; |
|
183 } |
|
184 |
|
185 bool IsSynthesized() const |
|
186 { |
|
187 return mEvent->mFlags.mIsSynthesizedForTests; |
|
188 } |
|
189 |
|
190 uint64_t TimeStamp() const |
|
191 { |
|
192 return mEvent->time; |
|
193 } |
|
194 |
|
195 void InitEvent(const nsAString& aType, bool aBubbles, bool aCancelable, |
|
196 ErrorResult& aRv) |
|
197 { |
|
198 aRv = InitEvent(aType, aBubbles, aCancelable); |
|
199 } |
|
200 |
|
201 EventTarget* GetOriginalTarget() const; |
|
202 EventTarget* GetExplicitOriginalTarget() const; |
|
203 |
|
204 bool GetPreventDefault() const; |
|
205 |
|
206 /** |
|
207 * @param aCalledByDefaultHandler Should be true when this is called by |
|
208 * C++ or Chrome. Otherwise, e.g., called |
|
209 * by a call of Event.preventDefault() in |
|
210 * content script, false. |
|
211 */ |
|
212 void PreventDefaultInternal(bool aCalledByDefaultHandler); |
|
213 |
|
214 bool IsMainThreadEvent() |
|
215 { |
|
216 return mIsMainThreadEvent; |
|
217 } |
|
218 |
|
219 protected: |
|
220 |
|
221 // Internal helper functions |
|
222 void SetEventType(const nsAString& aEventTypeArg); |
|
223 already_AddRefed<nsIContent> GetTargetFromFrame(); |
|
224 |
|
225 /** |
|
226 * IsChrome() returns true if aCx is chrome context or the event is created |
|
227 * in chrome's thread. Otherwise, false. |
|
228 */ |
|
229 bool IsChrome(JSContext* aCx) const; |
|
230 |
|
231 mozilla::WidgetEvent* mEvent; |
|
232 nsRefPtr<nsPresContext> mPresContext; |
|
233 nsCOMPtr<EventTarget> mExplicitOriginalTarget; |
|
234 nsCOMPtr<nsPIDOMWindow> mOwner; // nsPIDOMWindow for now. |
|
235 bool mEventIsInternal; |
|
236 bool mPrivateDataDuplicated; |
|
237 bool mIsMainThreadEvent; |
|
238 }; |
|
239 |
|
240 } // namespace dom |
|
241 } // namespace mozilla |
|
242 |
|
243 #define NS_FORWARD_TO_EVENT \ |
|
244 NS_FORWARD_NSIDOMEVENT(Event::) |
|
245 |
|
246 #define NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(_to) \ |
|
247 NS_IMETHOD GetType(nsAString& aType){ return _to GetType(aType); } \ |
|
248 NS_IMETHOD GetTarget(nsIDOMEventTarget * *aTarget) { return _to GetTarget(aTarget); } \ |
|
249 NS_IMETHOD GetCurrentTarget(nsIDOMEventTarget * *aCurrentTarget) { return _to GetCurrentTarget(aCurrentTarget); } \ |
|
250 NS_IMETHOD GetEventPhase(uint16_t *aEventPhase) { return _to GetEventPhase(aEventPhase); } \ |
|
251 NS_IMETHOD GetBubbles(bool *aBubbles) { return _to GetBubbles(aBubbles); } \ |
|
252 NS_IMETHOD GetCancelable(bool *aCancelable) { return _to GetCancelable(aCancelable); } \ |
|
253 NS_IMETHOD GetTimeStamp(DOMTimeStamp *aTimeStamp) { return _to GetTimeStamp(aTimeStamp); } \ |
|
254 NS_IMETHOD StopPropagation(void) { return _to StopPropagation(); } \ |
|
255 NS_IMETHOD PreventDefault(void) { return _to PreventDefault(); } \ |
|
256 NS_IMETHOD InitEvent(const nsAString & eventTypeArg, bool canBubbleArg, bool cancelableArg) { return _to InitEvent(eventTypeArg, canBubbleArg, cancelableArg); } \ |
|
257 NS_IMETHOD GetDefaultPrevented(bool *aDefaultPrevented) { return _to GetDefaultPrevented(aDefaultPrevented); } \ |
|
258 NS_IMETHOD StopImmediatePropagation(void) { return _to StopImmediatePropagation(); } \ |
|
259 NS_IMETHOD GetOriginalTarget(nsIDOMEventTarget** aOriginalTarget) { return _to GetOriginalTarget(aOriginalTarget); } \ |
|
260 NS_IMETHOD GetExplicitOriginalTarget(nsIDOMEventTarget** aExplicitOriginalTarget) { return _to GetExplicitOriginalTarget(aExplicitOriginalTarget); } \ |
|
261 NS_IMETHOD GetPreventDefault(bool* aRetval) { return _to GetPreventDefault(aRetval); } \ |
|
262 NS_IMETHOD GetIsTrusted(bool* aIsTrusted) { return _to GetIsTrusted(aIsTrusted); } \ |
|
263 NS_IMETHOD SetTarget(nsIDOMEventTarget *aTarget) { return _to SetTarget(aTarget); } \ |
|
264 NS_IMETHOD_(bool) IsDispatchStopped(void) { return _to IsDispatchStopped(); } \ |
|
265 NS_IMETHOD_(WidgetEvent*) GetInternalNSEvent(void) { return _to GetInternalNSEvent(); } \ |
|
266 NS_IMETHOD_(void) SetTrusted(bool aTrusted) { _to SetTrusted(aTrusted); } \ |
|
267 NS_IMETHOD_(void) SetOwner(EventTarget* aOwner) { _to SetOwner(aOwner); } \ |
|
268 NS_IMETHOD_(Event*) InternalDOMEvent() { return _to InternalDOMEvent(); } |
|
269 |
|
270 #define NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION \ |
|
271 NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(Event::) |
|
272 |
|
273 inline nsISupports* |
|
274 ToSupports(mozilla::dom::Event* e) |
|
275 { |
|
276 return static_cast<nsIDOMEvent*>(e); |
|
277 } |
|
278 |
|
279 inline nsISupports* |
|
280 ToCanonicalSupports(mozilla::dom::Event* e) |
|
281 { |
|
282 return static_cast<nsIDOMEvent*>(e); |
|
283 } |
|
284 |
|
285 #endif // mozilla_dom_Event_h_ |