forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Event.h
350 lines (287 loc) · 11.9 KB
/
Event.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_Event_h_
#define mozilla_dom_Event_h_
#include "mozilla/Attributes.h"
#include "mozilla/BasicEvents.h"
#include "nsIDOMEvent.h"
#include "nsISupports.h"
#include "nsCOMPtr.h"
#include "nsPIDOMWindow.h"
#include "nsPoint.h"
#include "nsCycleCollectionParticipant.h"
#include "nsAutoPtr.h"
#include "mozilla/dom/EventBinding.h"
#include "nsIScriptGlobalObject.h"
#include "Units.h"
#include "js/TypeDecls.h"
#include "nsIGlobalObject.h"
class nsIContent;
class nsIDOMEventTarget;
class nsPresContext;
namespace mozilla {
namespace dom {
class EventTarget;
class WantsPopupControlCheck;
#define GENERATED_EVENT(EventClass_) class EventClass_;
#include "mozilla/dom/GeneratedEventList.h"
#undef GENERATED_EVENT
// ExtendableEvent is a ServiceWorker event that is not
// autogenerated since it has some extra methods.
namespace workers {
class ExtendableEvent;
} // namespace workers
// Dummy class so we can cast through it to get from nsISupports to
// Event subclasses with only two non-ambiguous static casts.
class EventBase : public nsIDOMEvent
{
};
class Event : public EventBase,
public nsWrapperCache
{
public:
Event(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetEvent* aEvent);
explicit Event(nsPIDOMWindow* aWindow);
protected:
virtual ~Event();
private:
void ConstructorInit(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetEvent* aEvent);
public:
static Event* FromSupports(nsISupports* aSupports)
{
nsIDOMEvent* event =
static_cast<nsIDOMEvent*>(aSupports);
#ifdef DEBUG
{
nsCOMPtr<nsIDOMEvent> target_qi =
do_QueryInterface(aSupports);
// If this assertion fires the QI implementation for the object in
// question doesn't use the nsIDOMEvent pointer as the
// nsISupports pointer. That must be fixed, or we'll crash...
MOZ_ASSERT(target_qi == event, "Uh, fix QI!");
}
#endif
return static_cast<Event*>(event);
}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event)
nsIGlobalObject* GetParentObject()
{
return mOwner;
}
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override final;
virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto);
#define GENERATED_EVENT(EventClass_) \
virtual EventClass_* As##EventClass_() \
{ \
return nullptr; \
}
#include "mozilla/dom/GeneratedEventList.h"
#undef GENERATED_EVENT
// ExtendableEvent is a ServiceWorker event that is not
// autogenerated since it has some extra methods.
virtual workers::ExtendableEvent* AsExtendableEvent()
{
return nullptr;
}
// nsIDOMEvent Interface
NS_DECL_NSIDOMEVENT
void InitPresContextData(nsPresContext* aPresContext);
// Returns true if the event should be trusted.
bool Init(EventTarget* aGlobal);
static PopupControlState GetEventPopupControlState(WidgetEvent* aEvent,
nsIDOMEvent* aDOMEvent = nullptr);
static void PopupAllowedEventsChanged();
static void Shutdown();
static const char* GetEventName(EventMessage aEventType);
static CSSIntPoint GetClientCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint,
CSSIntPoint aDefaultPoint);
static CSSIntPoint GetPageCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint,
CSSIntPoint aDefaultPoint);
static LayoutDeviceIntPoint GetScreenCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint);
static CSSIntPoint GetOffsetCoords(nsPresContext* aPresContext,
WidgetEvent* aEvent,
LayoutDeviceIntPoint aPoint,
CSSIntPoint aDefaultPoint);
static already_AddRefed<Event> Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const EventInit& aParam,
ErrorResult& aRv);
// Implemented as xpidl method
// void GetType(nsString& aRetval) {}
EventTarget* GetTarget() const;
EventTarget* GetCurrentTarget() const;
uint16_t EventPhase() const;
// xpidl implementation
// void StopPropagation();
// xpidl implementation
// void StopImmediatePropagation();
bool Bubbles() const
{
return mEvent->mFlags.mBubbles;
}
bool Cancelable() const
{
return mEvent->mFlags.mCancelable;
}
// xpidl implementation
// void PreventDefault();
// You MUST NOT call PreventDefaultJ(JSContext*) from C++ code. A call of
// this method always sets Event.defaultPrevented true for web contents.
// If default action handler calls this, web applications meet wrong
// defaultPrevented value.
virtual void PreventDefault(JSContext* aCx);
// You MUST NOT call DefaultPrevented(JSContext*) from C++ code. This may
// return false even if PreventDefault() has been called.
// See comments in its implementation for the detail.
bool DefaultPrevented(JSContext* aCx) const;
bool DefaultPrevented() const
{
return mEvent->mFlags.mDefaultPrevented;
}
bool MultipleActionsPrevented() const
{
return mEvent->mFlags.mMultipleActionsPrevented;
}
bool IsTrusted() const
{
return mEvent->mFlags.mIsTrusted;
}
bool IsSynthesized() const
{
return mEvent->mFlags.mIsSynthesizedForTests;
}
double TimeStamp() const;
void InitEvent(const nsAString& aType, bool aBubbles, bool aCancelable,
ErrorResult& aRv)
{
aRv = InitEvent(aType, aBubbles, aCancelable);
}
EventTarget* GetOriginalTarget() const;
EventTarget* GetExplicitOriginalTarget() const;
EventTarget* GetComposedTarget() const;
bool GetPreventDefault() const;
/**
* @param aCalledByDefaultHandler Should be true when this is called by
* C++ or Chrome. Otherwise, e.g., called
* by a call of Event.preventDefault() in
* content script, false.
*/
void PreventDefaultInternal(bool aCalledByDefaultHandler);
bool IsMainThreadEvent()
{
return mIsMainThreadEvent;
}
/**
* For a given current target, returns the related target adjusted with
* shadow DOM retargeting rules. Returns nullptr if related target
* is not adjusted.
*/
static nsIContent* GetShadowRelatedTarget(nsIContent* aCurrentTarget,
nsIContent* aRelatedTarget);
protected:
// Internal helper functions
void SetEventType(const nsAString& aEventTypeArg);
already_AddRefed<nsIContent> GetTargetFromFrame();
friend class WantsPopupControlCheck;
void SetWantsPopupControlCheck(bool aCheck)
{
mWantsPopupControlCheck = aCheck;
}
bool GetWantsPopupControlCheck()
{
return IsTrusted() && mWantsPopupControlCheck;
}
/**
* IsChrome() returns true if aCx is chrome context or the event is created
* in chrome's thread. Otherwise, false.
*/
bool IsChrome(JSContext* aCx) const;
mozilla::WidgetEvent* mEvent;
RefPtr<nsPresContext> mPresContext;
nsCOMPtr<EventTarget> mExplicitOriginalTarget;
nsCOMPtr<nsIGlobalObject> mOwner;
bool mEventIsInternal;
bool mPrivateDataDuplicated;
bool mIsMainThreadEvent;
// True when popup control check should rely on event.type, not
// WidgetEvent.mMessage.
bool mWantsPopupControlCheck;
};
class MOZ_STACK_CLASS WantsPopupControlCheck
{
public:
explicit WantsPopupControlCheck(nsIDOMEvent* aEvent) :
mEvent(aEvent->InternalDOMEvent())
{
mOriginalWantsPopupControlCheck = mEvent->GetWantsPopupControlCheck();
mEvent->SetWantsPopupControlCheck(mEvent->IsTrusted());
}
~WantsPopupControlCheck()
{
mEvent->SetWantsPopupControlCheck(mOriginalWantsPopupControlCheck);
}
private:
Event* mEvent;
bool mOriginalWantsPopupControlCheck;
};
} // namespace dom
} // namespace mozilla
#define NS_FORWARD_TO_EVENT \
NS_FORWARD_NSIDOMEVENT(Event::) \
virtual void PreventDefault(JSContext* aCx) override { Event::PreventDefault(aCx); }
#define NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(_to) \
NS_IMETHOD GetType(nsAString& aType) override { return _to GetType(aType); } \
NS_IMETHOD GetTarget(nsIDOMEventTarget** aTarget) override { return _to GetTarget(aTarget); } \
NS_IMETHOD GetCurrentTarget(nsIDOMEventTarget** aCurrentTarget) override { return _to GetCurrentTarget(aCurrentTarget); } \
NS_IMETHOD GetEventPhase(uint16_t* aEventPhase) override { return _to GetEventPhase(aEventPhase); } \
NS_IMETHOD GetBubbles(bool* aBubbles) override { return _to GetBubbles(aBubbles); } \
NS_IMETHOD GetCancelable(bool* aCancelable) override { return _to GetCancelable(aCancelable); } \
NS_IMETHOD GetTimeStamp(DOMTimeStamp* aTimeStamp) override { return _to GetTimeStamp(aTimeStamp); } \
NS_IMETHOD StopPropagation(void) override { return _to StopPropagation(); } \
NS_IMETHOD StopCrossProcessForwarding(void) override { return _to StopCrossProcessForwarding(); } \
NS_IMETHOD PreventDefault(void) override { return _to PreventDefault(); } \
NS_IMETHOD InitEvent(const nsAString& eventTypeArg, bool canBubbleArg, bool cancelableArg) override { return _to InitEvent(eventTypeArg, canBubbleArg, cancelableArg); } \
NS_IMETHOD GetDefaultPrevented(bool* aDefaultPrevented) override { return _to GetDefaultPrevented(aDefaultPrevented); } \
NS_IMETHOD StopImmediatePropagation(void) override { return _to StopImmediatePropagation(); } \
NS_IMETHOD GetOriginalTarget(nsIDOMEventTarget** aOriginalTarget) override { return _to GetOriginalTarget(aOriginalTarget); } \
NS_IMETHOD GetExplicitOriginalTarget(nsIDOMEventTarget** aExplicitOriginalTarget) override { return _to GetExplicitOriginalTarget(aExplicitOriginalTarget); } \
NS_IMETHOD GetPreventDefault(bool* aRetval) override { return _to GetPreventDefault(aRetval); } \
NS_IMETHOD GetIsTrusted(bool* aIsTrusted) override { return _to GetIsTrusted(aIsTrusted); } \
NS_IMETHOD SetTarget(nsIDOMEventTarget* aTarget) override { return _to SetTarget(aTarget); } \
NS_IMETHOD_(bool) IsDispatchStopped(void) override { return _to IsDispatchStopped(); } \
NS_IMETHOD_(WidgetEvent*) GetInternalNSEvent(void) override { return _to GetInternalNSEvent(); } \
NS_IMETHOD_(void) SetTrusted(bool aTrusted) override { _to SetTrusted(aTrusted); } \
NS_IMETHOD_(void) SetOwner(EventTarget* aOwner) override { _to SetOwner(aOwner); } \
NS_IMETHOD_(Event*) InternalDOMEvent() override { return _to InternalDOMEvent(); }
#define NS_FORWARD_TO_EVENT_NO_SERIALIZATION_NO_DUPLICATION \
NS_FORWARD_NSIDOMEVENT_NO_SERIALIZATION_NO_DUPLICATION(Event::) \
virtual void PreventDefault(JSContext* aCx) override { Event::PreventDefault(aCx); }
inline nsISupports*
ToSupports(mozilla::dom::Event* e)
{
return static_cast<nsIDOMEvent*>(e);
}
inline nsISupports*
ToCanonicalSupports(mozilla::dom::Event* e)
{
return static_cast<nsIDOMEvent*>(e);
}
already_AddRefed<mozilla::dom::Event>
NS_NewDOMEvent(mozilla::dom::EventTarget* aOwner,
nsPresContext* aPresContext,
mozilla::WidgetEvent* aEvent);
#endif // mozilla_dom_Event_h_