Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 mozilla_dom_Event_h_
7 #define mozilla_dom_Event_h_
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"
23 class nsIContent;
24 class nsIDOMEventTarget;
25 class nsPresContext;
27 namespace mozilla {
28 namespace dom {
30 class EventTarget;
31 class ErrorEvent;
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 };
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();
49 private:
50 void ConstructorInit(EventTarget* aOwner,
51 nsPresContext* aPresContext,
52 WidgetEvent* aEvent);
54 public:
55 void GetParentObject(nsIScriptGlobalObject** aParentObject)
56 {
57 if (mOwner) {
58 CallQueryInterface(mOwner, aParentObject);
59 } else {
60 *aParentObject = nullptr;
61 }
62 }
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);
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 }
82 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
83 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event)
85 nsISupports* GetParentObject()
86 {
87 return mOwner;
88 }
90 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE
91 {
92 return EventBinding::Wrap(aCx, this);
93 }
95 virtual ErrorEvent* AsErrorEvent()
96 {
97 return nullptr;
98 }
100 // nsIDOMEvent Interface
101 NS_DECL_NSIDOMEVENT
103 void InitPresContextData(nsPresContext* aPresContext);
105 // Returns true if the event should be trusted.
106 bool Init(EventTarget* aGlobal);
108 static PopupControlState GetEventPopupControlState(WidgetEvent* aEvent);
110 static void PopupAllowedEventsChanged();
112 static void Shutdown();
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);
127 static already_AddRefed<Event> Constructor(const GlobalObject& aGlobal,
128 const nsAString& aType,
129 const EventInit& aParam,
130 ErrorResult& aRv);
132 // Implemented as xpidl method
133 // void GetType(nsString& aRetval) {}
135 EventTarget* GetTarget() const;
136 EventTarget* GetCurrentTarget() const;
138 uint16_t EventPhase() const;
140 // xpidl implementation
141 // void StopPropagation();
143 // xpidl implementation
144 // void StopImmediatePropagation();
146 bool Bubbles() const
147 {
148 return mEvent->mFlags.mBubbles;
149 }
151 bool Cancelable() const
152 {
153 return mEvent->mFlags.mCancelable;
154 }
156 // xpidl implementation
157 // void PreventDefault();
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);
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;
170 bool DefaultPrevented() const
171 {
172 return mEvent->mFlags.mDefaultPrevented;
173 }
175 bool MultipleActionsPrevented() const
176 {
177 return mEvent->mFlags.mMultipleActionsPrevented;
178 }
180 bool IsTrusted() const
181 {
182 return mEvent->mFlags.mIsTrusted;
183 }
185 bool IsSynthesized() const
186 {
187 return mEvent->mFlags.mIsSynthesizedForTests;
188 }
190 uint64_t TimeStamp() const
191 {
192 return mEvent->time;
193 }
195 void InitEvent(const nsAString& aType, bool aBubbles, bool aCancelable,
196 ErrorResult& aRv)
197 {
198 aRv = InitEvent(aType, aBubbles, aCancelable);
199 }
201 EventTarget* GetOriginalTarget() const;
202 EventTarget* GetExplicitOriginalTarget() const;
204 bool GetPreventDefault() const;
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);
214 bool IsMainThreadEvent()
215 {
216 return mIsMainThreadEvent;
217 }
219 protected:
221 // Internal helper functions
222 void SetEventType(const nsAString& aEventTypeArg);
223 already_AddRefed<nsIContent> GetTargetFromFrame();
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;
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 };
240 } // namespace dom
241 } // namespace mozilla
243 #define NS_FORWARD_TO_EVENT \
244 NS_FORWARD_NSIDOMEVENT(Event::)
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(); }
270 #define NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION \
271 NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(Event::)
273 inline nsISupports*
274 ToSupports(mozilla::dom::Event* e)
275 {
276 return static_cast<nsIDOMEvent*>(e);
277 }
279 inline nsISupports*
280 ToCanonicalSupports(mozilla::dom::Event* e)
281 {
282 return static_cast<nsIDOMEvent*>(e);
283 }
285 #endif // mozilla_dom_Event_h_