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"
14 #include "nsPIDOMWindow.h"
16 #include "nsCycleCollectionParticipant.h"
17 #include "nsAutoPtr.h"
18 #include "mozilla/dom/EventBinding.h"
19 #include "nsIScriptGlobalObject.h"
21 #include "js/TypeDecls.h"
24 class nsIDOMEventTarget
;
34 // Dummy class so we can cast through it to get from nsISupports to
35 // Event subclasses with only two non-ambiguous static casts.
36 class EventBase
: public nsIDOMEvent
40 class Event
: public EventBase
,
44 Event(EventTarget
* aOwner
,
45 nsPresContext
* aPresContext
,
47 Event(nsPIDOMWindow
* aWindow
);
51 void ConstructorInit(EventTarget
* aOwner
,
52 nsPresContext
* aPresContext
,
56 void GetParentObject(nsIScriptGlobalObject
** aParentObject
)
59 CallQueryInterface(mOwner
, aParentObject
);
61 *aParentObject
= nullptr;
65 static Event
* FromSupports(nsISupports
* aSupports
)
68 static_cast<nsIDOMEvent
*>(aSupports
);
71 nsCOMPtr
<nsIDOMEvent
> target_qi
=
72 do_QueryInterface(aSupports
);
74 // If this assertion fires the QI implementation for the object in
75 // question doesn't use the nsIDOMEvent pointer as the
76 // nsISupports pointer. That must be fixed, or we'll crash...
77 MOZ_ASSERT(target_qi
== event
, "Uh, fix QI!");
80 return static_cast<Event
*>(event
);
83 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
84 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event
)
86 nsISupports
* GetParentObject()
91 virtual JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
93 return EventBinding::Wrap(aCx
, this);
96 virtual ErrorEvent
* AsErrorEvent()
101 virtual ProgressEvent
* AsProgressEvent()
106 // nsIDOMEvent Interface
109 void InitPresContextData(nsPresContext
* aPresContext
);
111 // Returns true if the event should be trusted.
112 bool Init(EventTarget
* aGlobal
);
114 static PopupControlState
GetEventPopupControlState(WidgetEvent
* aEvent
);
116 static void PopupAllowedEventsChanged();
118 static void Shutdown();
120 static const char* GetEventName(uint32_t aEventType
);
121 static CSSIntPoint
GetClientCoords(nsPresContext
* aPresContext
,
123 LayoutDeviceIntPoint aPoint
,
124 CSSIntPoint aDefaultPoint
);
125 static CSSIntPoint
GetPageCoords(nsPresContext
* aPresContext
,
127 LayoutDeviceIntPoint aPoint
,
128 CSSIntPoint aDefaultPoint
);
129 static nsIntPoint
GetScreenCoords(nsPresContext
* aPresContext
,
131 LayoutDeviceIntPoint aPoint
);
133 static already_AddRefed
<Event
> Constructor(const GlobalObject
& aGlobal
,
134 const nsAString
& aType
,
135 const EventInit
& aParam
,
138 // Implemented as xpidl method
139 // void GetType(nsString& aRetval) {}
141 EventTarget
* GetTarget() const;
142 EventTarget
* GetCurrentTarget() const;
144 uint16_t EventPhase() const;
146 // xpidl implementation
147 // void StopPropagation();
149 // xpidl implementation
150 // void StopImmediatePropagation();
154 return mEvent
->mFlags
.mBubbles
;
157 bool Cancelable() const
159 return mEvent
->mFlags
.mCancelable
;
162 // xpidl implementation
163 // void PreventDefault();
165 // You MUST NOT call PreventDefaultJ(JSContext*) from C++ code. A call of
166 // this method always sets Event.defaultPrevented true for web contents.
167 // If default action handler calls this, web applications meet wrong
168 // defaultPrevented value.
169 void PreventDefault(JSContext
* aCx
);
171 // You MUST NOT call DefaultPrevented(JSContext*) from C++ code. This may
172 // return false even if PreventDefault() has been called.
173 // See comments in its implementation for the detail.
174 bool DefaultPrevented(JSContext
* aCx
) const;
176 bool DefaultPrevented() const
178 return mEvent
->mFlags
.mDefaultPrevented
;
181 bool MultipleActionsPrevented() const
183 return mEvent
->mFlags
.mMultipleActionsPrevented
;
186 bool IsTrusted() const
188 return mEvent
->mFlags
.mIsTrusted
;
191 bool IsSynthesized() const
193 return mEvent
->mFlags
.mIsSynthesizedForTests
;
196 double TimeStamp() const;
198 void InitEvent(const nsAString
& aType
, bool aBubbles
, bool aCancelable
,
201 aRv
= InitEvent(aType
, aBubbles
, aCancelable
);
204 EventTarget
* GetOriginalTarget() const;
205 EventTarget
* GetExplicitOriginalTarget() const;
207 bool GetPreventDefault() const;
210 * @param aCalledByDefaultHandler Should be true when this is called by
211 * C++ or Chrome. Otherwise, e.g., called
212 * by a call of Event.preventDefault() in
213 * content script, false.
215 void PreventDefaultInternal(bool aCalledByDefaultHandler
);
217 bool IsMainThreadEvent()
219 return mIsMainThreadEvent
;
224 // Internal helper functions
225 void SetEventType(const nsAString
& aEventTypeArg
);
226 already_AddRefed
<nsIContent
> GetTargetFromFrame();
229 * IsChrome() returns true if aCx is chrome context or the event is created
230 * in chrome's thread. Otherwise, false.
232 bool IsChrome(JSContext
* aCx
) const;
234 mozilla::WidgetEvent
* mEvent
;
235 nsRefPtr
<nsPresContext
> mPresContext
;
236 nsCOMPtr
<EventTarget
> mExplicitOriginalTarget
;
237 nsCOMPtr
<nsPIDOMWindow
> mOwner
; // nsPIDOMWindow for now.
238 bool mEventIsInternal
;
239 bool mPrivateDataDuplicated
;
240 bool mIsMainThreadEvent
;
244 } // namespace mozilla
246 #define NS_FORWARD_TO_EVENT \
247 NS_FORWARD_NSIDOMEVENT(Event::)
249 #define NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(_to) \
250 NS_IMETHOD GetType(nsAString& aType){ return _to GetType(aType); } \
251 NS_IMETHOD GetTarget(nsIDOMEventTarget * *aTarget) { return _to GetTarget(aTarget); } \
252 NS_IMETHOD GetCurrentTarget(nsIDOMEventTarget * *aCurrentTarget) { return _to GetCurrentTarget(aCurrentTarget); } \
253 NS_IMETHOD GetEventPhase(uint16_t *aEventPhase) { return _to GetEventPhase(aEventPhase); } \
254 NS_IMETHOD GetBubbles(bool *aBubbles) { return _to GetBubbles(aBubbles); } \
255 NS_IMETHOD GetCancelable(bool *aCancelable) { return _to GetCancelable(aCancelable); } \
256 NS_IMETHOD GetTimeStamp(DOMTimeStamp *aTimeStamp) { return _to GetTimeStamp(aTimeStamp); } \
257 NS_IMETHOD StopPropagation(void) { return _to StopPropagation(); } \
258 NS_IMETHOD PreventDefault(void) { return _to PreventDefault(); } \
259 NS_IMETHOD InitEvent(const nsAString & eventTypeArg, bool canBubbleArg, bool cancelableArg) { return _to InitEvent(eventTypeArg, canBubbleArg, cancelableArg); } \
260 NS_IMETHOD GetDefaultPrevented(bool *aDefaultPrevented) { return _to GetDefaultPrevented(aDefaultPrevented); } \
261 NS_IMETHOD StopImmediatePropagation(void) { return _to StopImmediatePropagation(); } \
262 NS_IMETHOD GetOriginalTarget(nsIDOMEventTarget** aOriginalTarget) { return _to GetOriginalTarget(aOriginalTarget); } \
263 NS_IMETHOD GetExplicitOriginalTarget(nsIDOMEventTarget** aExplicitOriginalTarget) { return _to GetExplicitOriginalTarget(aExplicitOriginalTarget); } \
264 NS_IMETHOD GetPreventDefault(bool* aRetval) { return _to GetPreventDefault(aRetval); } \
265 NS_IMETHOD GetIsTrusted(bool* aIsTrusted) { return _to GetIsTrusted(aIsTrusted); } \
266 NS_IMETHOD SetTarget(nsIDOMEventTarget *aTarget) { return _to SetTarget(aTarget); } \
267 NS_IMETHOD_(bool) IsDispatchStopped(void) { return _to IsDispatchStopped(); } \
268 NS_IMETHOD_(WidgetEvent*) GetInternalNSEvent(void) { return _to GetInternalNSEvent(); } \
269 NS_IMETHOD_(void) SetTrusted(bool aTrusted) { _to SetTrusted(aTrusted); } \
270 NS_IMETHOD_(void) SetOwner(EventTarget* aOwner) { _to SetOwner(aOwner); } \
271 NS_IMETHOD_(Event*) InternalDOMEvent() { return _to InternalDOMEvent(); }
273 #define NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION \
274 NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(Event::)
277 ToSupports(mozilla::dom::Event
* e
)
279 return static_cast<nsIDOMEvent
*>(e
);
283 ToCanonicalSupports(mozilla::dom::Event
* e
)
285 return static_cast<nsIDOMEvent
*>(e
);
288 #endif // mozilla_dom_Event_h_