Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/Event.h
Line
Count
Source (jump to first uncovered line)
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 mozilla_dom_Event_h_
8
#define mozilla_dom_Event_h_
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/BasicEvents.h"
12
#include "nsISupports.h"
13
#include "nsCOMPtr.h"
14
#include "nsPIDOMWindow.h"
15
#include "nsPoint.h"
16
#include "nsCycleCollectionParticipant.h"
17
#include "mozilla/dom/BindingDeclarations.h"
18
#include "mozilla/dom/EventBinding.h"
19
#include "nsIScriptGlobalObject.h"
20
#include "Units.h"
21
#include "js/TypeDecls.h"
22
#include "nsIGlobalObject.h"
23
24
class nsIContent;
25
class nsPresContext;
26
class PickleIterator;
27
28
namespace IPC {
29
class Message;
30
} // namespace IPC
31
32
namespace mozilla {
33
namespace dom {
34
35
class BeforeUnloadEvent;
36
class CustomEvent;
37
class DragEvent;
38
class EventTarget;
39
class EventMessageAutoOverride;
40
// ExtendableEvent is a ServiceWorker event that is not
41
// autogenerated since it has some extra methods.
42
class ExtendableEvent;
43
class KeyboardEvent;
44
class MouseEvent;
45
class TimeEvent;
46
class UIEvent;
47
class WantsPopupControlCheck;
48
class XULCommandEvent;
49
#define GENERATED_EVENT(EventClass_) class EventClass_;
50
#include "mozilla/dom/GeneratedEventList.h"
51
#undef GENERATED_EVENT
52
53
// IID for Event
54
#define NS_EVENT_IID                                            \
55
  { 0x71139716, 0x4d91, 0x4dee,                                 \
56
      { 0xba, 0xf9, 0xe3, 0x3b, 0x80, 0xc1, 0x61, 0x61 } }
57
58
class Event : public nsISupports
59
            , public nsWrapperCache
60
{
61
public:
62
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_EVENT_IID)
63
64
  Event(EventTarget* aOwner,
65
        nsPresContext* aPresContext,
66
        WidgetEvent* aEvent);
67
  explicit Event(nsPIDOMWindowInner* aWindow);
68
69
protected:
70
  virtual ~Event();
71
72
private:
73
  void ConstructorInit(EventTarget* aOwner,
74
                       nsPresContext* aPresContext,
75
                       WidgetEvent* aEvent);
76
77
public:
78
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
79
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Event)
80
81
  nsIGlobalObject* GetParentObject()
82
0
  {
83
0
    return mOwner;
84
0
  }
85
86
  JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) final;
87
88
  virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto);
89
90
#define GENERATED_EVENT(EventClass_) \
91
  virtual EventClass_* As##EventClass_()  \
92
0
  {                                       \
93
0
    return nullptr;                       \
94
0
  }
Unexecuted instantiation: mozilla::dom::Event::AsAddonEvent()
Unexecuted instantiation: mozilla::dom::Event::AsAnimationPlaybackEvent()
Unexecuted instantiation: mozilla::dom::Event::AsBlobEvent()
Unexecuted instantiation: mozilla::dom::Event::AsCaretStateChangedEvent()
Unexecuted instantiation: mozilla::dom::Event::AsCloseEvent()
Unexecuted instantiation: mozilla::dom::Event::AsDeviceLightEvent()
Unexecuted instantiation: mozilla::dom::Event::AsDeviceOrientationEvent()
Unexecuted instantiation: mozilla::dom::Event::AsDeviceProximityEvent()
Unexecuted instantiation: mozilla::dom::Event::AsErrorEvent()
Unexecuted instantiation: mozilla::dom::Event::AsFontFaceSetLoadEvent()
Unexecuted instantiation: mozilla::dom::Event::AsGamepadAxisMoveEvent()
Unexecuted instantiation: mozilla::dom::Event::AsGamepadButtonEvent()
Unexecuted instantiation: mozilla::dom::Event::AsGamepadEvent()
Unexecuted instantiation: mozilla::dom::Event::AsGroupedHistoryEvent()
Unexecuted instantiation: mozilla::dom::Event::AsHashChangeEvent()
Unexecuted instantiation: mozilla::dom::Event::AsHiddenPluginEvent()
Unexecuted instantiation: mozilla::dom::Event::AsImageCaptureErrorEvent()
Unexecuted instantiation: mozilla::dom::Event::AsMIDIConnectionEvent()
Unexecuted instantiation: mozilla::dom::Event::AsMediaQueryListEvent()
Unexecuted instantiation: mozilla::dom::Event::AsMediaRecorderErrorEvent()
Unexecuted instantiation: mozilla::dom::Event::AsMediaStreamEvent()
Unexecuted instantiation: mozilla::dom::Event::AsMediaStreamTrackEvent()
Unexecuted instantiation: mozilla::dom::Event::AsOfflineAudioCompletionEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPageTransitionEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPerformanceEntryEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPluginCrashedEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPopStateEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPopupBlockedEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPresentationConnectionAvailableEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPresentationConnectionCloseEvent()
Unexecuted instantiation: mozilla::dom::Event::AsProgressEvent()
Unexecuted instantiation: mozilla::dom::Event::AsPromiseRejectionEvent()
Unexecuted instantiation: mozilla::dom::Event::AsRTCDTMFToneChangeEvent()
Unexecuted instantiation: mozilla::dom::Event::AsRTCDataChannelEvent()
Unexecuted instantiation: mozilla::dom::Event::AsRTCPeerConnectionIceEvent()
Unexecuted instantiation: mozilla::dom::Event::AsRTCTrackEvent()
Unexecuted instantiation: mozilla::dom::Event::AsScrollViewChangeEvent()
Unexecuted instantiation: mozilla::dom::Event::AsSecurityPolicyViolationEvent()
Unexecuted instantiation: mozilla::dom::Event::AsSpeechRecognitionEvent()
Unexecuted instantiation: mozilla::dom::Event::AsSpeechSynthesisErrorEvent()
Unexecuted instantiation: mozilla::dom::Event::AsSpeechSynthesisEvent()
Unexecuted instantiation: mozilla::dom::Event::AsStyleRuleChangeEvent()
Unexecuted instantiation: mozilla::dom::Event::AsStyleSheetApplicableStateChangeEvent()
Unexecuted instantiation: mozilla::dom::Event::AsStyleSheetChangeEvent()
Unexecuted instantiation: mozilla::dom::Event::AsTCPServerSocketEvent()
Unexecuted instantiation: mozilla::dom::Event::AsTCPSocketErrorEvent()
Unexecuted instantiation: mozilla::dom::Event::AsTCPSocketEvent()
Unexecuted instantiation: mozilla::dom::Event::AsTrackEvent()
Unexecuted instantiation: mozilla::dom::Event::AsUDPMessageEvent()
Unexecuted instantiation: mozilla::dom::Event::AsUserProximityEvent()
Unexecuted instantiation: mozilla::dom::Event::AsWebGLContextEvent()
95
#include "mozilla/dom/GeneratedEventList.h"
96
#undef GENERATED_EVENT
97
98
  // ExtendableEvent is a ServiceWorker event that is not
99
  // autogenerated since it has some extra methods.
100
  virtual ExtendableEvent* AsExtendableEvent()
101
0
  {
102
0
    return nullptr;
103
0
  }
104
105
0
  virtual TimeEvent* AsTimeEvent() { return nullptr; }
106
107
  // BeforeUnloadEvent is not autogenerated because it has a setter.
108
  virtual BeforeUnloadEvent* AsBeforeUnloadEvent()
109
0
  {
110
0
    return nullptr;
111
0
  }
112
113
  // KeyboardEvent has all sorts of non-autogeneratable bits so far.
114
  virtual KeyboardEvent* AsKeyboardEvent()
115
0
  {
116
0
    return nullptr;
117
0
  }
118
119
  // DragEvent has a non-autogeneratable initDragEvent.
120
  virtual DragEvent* AsDragEvent()
121
0
  {
122
0
    return nullptr;
123
0
  }
124
125
  // XULCommandEvent has a non-autogeneratable initCommandEvent.
126
  virtual XULCommandEvent* AsXULCommandEvent()
127
0
  {
128
0
    return nullptr;
129
0
  }
130
131
  // MouseEvent has a non-autogeneratable initMouseEvent and other
132
  // non-autogeneratable methods.
133
  virtual MouseEvent* AsMouseEvent()
134
0
  {
135
0
    return nullptr;
136
0
  }
137
138
  // UIEvent has a non-autogeneratable initUIEvent.
139
  virtual UIEvent* AsUIEvent()
140
0
  {
141
0
    return nullptr;
142
0
  }
143
144
  // CustomEvent has a non-autogeneratable initCustomEvent.
145
  virtual CustomEvent* AsCustomEvent()
146
0
  {
147
0
    return nullptr;
148
0
  }
149
150
  void InitEvent(const nsAString& aEventTypeArg,
151
                 bool aCanBubble,
152
                 bool aCancelable)
153
0
  {
154
0
    InitEvent(aEventTypeArg,
155
0
              aCanBubble ? CanBubble::eYes : CanBubble::eNo,
156
0
              aCancelable ? Cancelable::eYes : Cancelable::eNo);
157
0
  }
158
159
  void InitEvent(const nsAString& aEventTypeArg,
160
                 mozilla::CanBubble,
161
                 mozilla::Cancelable,
162
                 mozilla::Composed = mozilla::Composed::eDefault);
163
164
  void SetTarget(EventTarget* aTarget);
165
  virtual void DuplicatePrivateData();
166
  bool IsDispatchStopped();
167
  WidgetEvent* WidgetEventPtr();
168
  virtual void Serialize(IPC::Message* aMsg, bool aSerializeInterfaceType);
169
  virtual bool Deserialize(const IPC::Message* aMsg, PickleIterator* aIter);
170
  void SetOwner(EventTarget* aOwner);
171
  void StopCrossProcessForwarding();
172
  void SetTrusted(bool aTrusted);
173
174
  void InitPresContextData(nsPresContext* aPresContext);
175
176
  // Returns true if the event should be trusted.
177
  bool Init(EventTarget* aGlobal);
178
179
  static PopupControlState GetEventPopupControlState(WidgetEvent* aEvent,
180
                                                     Event* aDOMEvent = nullptr);
181
182
  static void PopupAllowedEventsChanged();
183
184
  static void Shutdown();
185
186
  static const char* GetEventName(EventMessage aEventType);
187
  static CSSIntPoint GetClientCoords(nsPresContext* aPresContext,
188
                                     WidgetEvent* aEvent,
189
                                     LayoutDeviceIntPoint aPoint,
190
                                     CSSIntPoint aDefaultPoint);
191
  static CSSIntPoint GetPageCoords(nsPresContext* aPresContext,
192
                                   WidgetEvent* aEvent,
193
                                   LayoutDeviceIntPoint aPoint,
194
                                   CSSIntPoint aDefaultPoint);
195
  static CSSIntPoint GetScreenCoords(nsPresContext* aPresContext,
196
                                     WidgetEvent* aEvent,
197
                                     LayoutDeviceIntPoint aPoint);
198
  static CSSIntPoint GetOffsetCoords(nsPresContext* aPresContext,
199
                                     WidgetEvent* aEvent,
200
                                     LayoutDeviceIntPoint aPoint,
201
                                     CSSIntPoint aDefaultPoint);
202
203
  static already_AddRefed<Event> Constructor(EventTarget* aEventTarget,
204
                                             const nsAString& aType,
205
                                             const EventInit& aParam);
206
207
  static already_AddRefed<Event> Constructor(const GlobalObject& aGlobal,
208
                                             const nsAString& aType,
209
                                             const EventInit& aParam,
210
                                             ErrorResult& aRv);
211
212
  void GetType(nsAString& aType) const;
213
214
  EventTarget* GetTarget() const;
215
  EventTarget* GetCurrentTarget() const;
216
217
  void ComposedPath(nsTArray<RefPtr<EventTarget>>& aPath);
218
219
  uint16_t EventPhase() const;
220
221
  void StopPropagation();
222
223
  void StopImmediatePropagation();
224
225
  bool Bubbles() const
226
0
  {
227
0
    return mEvent->mFlags.mBubbles;
228
0
  }
229
230
  bool Cancelable() const
231
0
  {
232
0
    return mEvent->mFlags.mCancelable;
233
0
  }
234
235
  bool Composed() const
236
0
  {
237
0
    return mEvent->mFlags.mComposed;
238
0
  }
239
240
  bool CancelBubble() const
241
0
  {
242
0
    return mEvent->PropagationStopped();
243
0
  }
244
  void SetCancelBubble(bool aCancelBubble)
245
0
  {
246
0
    if (aCancelBubble)  {
247
0
      mEvent->StopPropagation();
248
0
    }
249
0
  }
250
251
  // For C++ consumers only!
252
  void PreventDefault();
253
254
  // You MUST NOT call PreventDefault(JSContext*, CallerType) from C++ code.  A
255
  // call of this method always sets Event.defaultPrevented true for web
256
  // contents.  If default action handler calls this, web applications see wrong
257
  // defaultPrevented value.
258
  virtual void PreventDefault(JSContext* aCx, CallerType aCallerType);
259
260
  // You MUST NOT call DefaultPrevented(CallerType) from C++ code.  This may
261
  // return false even if PreventDefault() has been called.
262
  // See comments in its implementation for the details.
263
  bool DefaultPrevented(CallerType aCallerType) const;
264
265
  bool DefaultPrevented() const
266
0
  {
267
0
    return mEvent->DefaultPrevented();
268
0
  }
269
270
  bool DefaultPreventedByChrome() const
271
0
  {
272
0
    return mEvent->mFlags.mDefaultPreventedByChrome;
273
0
  }
274
275
  bool DefaultPreventedByContent() const
276
0
  {
277
0
    return mEvent->mFlags.mDefaultPreventedByContent;
278
0
  }
279
280
  bool MultipleActionsPrevented() const
281
0
  {
282
0
    return mEvent->mFlags.mMultipleActionsPrevented;
283
0
  }
284
285
  bool ReturnValue(CallerType aCallerType) const;
286
287
  void SetReturnValue(bool aReturnValue, CallerType aCallerType);
288
289
  bool IsTrusted() const
290
0
  {
291
0
    return mEvent->IsTrusted();
292
0
  }
293
294
  bool IsSynthesized() const
295
0
  {
296
0
    return mEvent->mFlags.mIsSynthesizedForTests;
297
0
  }
298
299
  bool IsSafeToBeDispatchedAsynchronously() const
300
0
  {
301
0
    // If mEvent is not created by dom::Event nor its subclasses, its lifetime
302
0
    // is not guaranteed.  So, only when mEventIsInternal is true, it's safe
303
0
    // to be dispatched asynchronously.
304
0
    return mEventIsInternal;
305
0
  }
306
307
  double TimeStamp();
308
309
  EventTarget* GetOriginalTarget() const;
310
  EventTarget* GetExplicitOriginalTarget() const;
311
  EventTarget* GetComposedTarget() const;
312
313
  /**
314
   * @param aCalledByDefaultHandler     Should be true when this is called by
315
   *                                    C++ or Chrome.  Otherwise, e.g., called
316
   *                                    by a call of Event.preventDefault() in
317
   *                                    content script, false.
318
   */
319
  void PreventDefaultInternal(bool aCalledByDefaultHandler,
320
                              nsIPrincipal* aPrincipal = nullptr);
321
322
  bool IsMainThreadEvent()
323
0
  {
324
0
    return mIsMainThreadEvent;
325
0
  }
326
327
  void MarkUninitialized()
328
0
  {
329
0
    mEvent->mMessage = eVoidEvent;
330
0
    mEvent->mSpecifiedEventTypeString.Truncate();
331
0
    mEvent->mSpecifiedEventType = nullptr;
332
0
  }
333
334
  /**
335
   * For WidgetEvent, return it's type in string.
336
   *
337
   * @param aEvent is a WidgetEvent to get its type.
338
   * @param aType is a string where to return the type.
339
   */
340
  static void GetWidgetEventType(WidgetEvent* aEvent, nsAString& aType);
341
342
protected:
343
344
  // Internal helper functions
345
  void SetEventType(const nsAString& aEventTypeArg);
346
  already_AddRefed<nsIContent> GetTargetFromFrame();
347
348
  friend class EventMessageAutoOverride;
349
  friend class WantsPopupControlCheck;
350
  void SetWantsPopupControlCheck(bool aCheck)
351
0
  {
352
0
    mWantsPopupControlCheck = aCheck;
353
0
  }
354
355
  bool GetWantsPopupControlCheck()
356
0
  {
357
0
    return IsTrusted() && mWantsPopupControlCheck;
358
0
  }
359
360
  void SetComposed(bool aComposed)
361
0
  {
362
0
    mEvent->SetComposed(aComposed);
363
0
  }
364
365
  already_AddRefed<EventTarget>
366
  EnsureWebAccessibleRelatedTarget(EventTarget* aRelatedTarget);
367
368
  mozilla::WidgetEvent*       mEvent;
369
  RefPtr<nsPresContext>     mPresContext;
370
  nsCOMPtr<EventTarget>       mExplicitOriginalTarget;
371
  nsCOMPtr<nsIGlobalObject>   mOwner;
372
  bool                        mEventIsInternal;
373
  bool                        mPrivateDataDuplicated;
374
  bool                        mIsMainThreadEvent;
375
  // True when popup control check should rely on event.type, not
376
  // WidgetEvent.mMessage.
377
  bool                        mWantsPopupControlCheck;
378
};
379
380
/**
381
 * RAII helper-class to override an event's message (i.e. its DOM-exposed
382
 * type), for as long as the object is alive.  Restores the original
383
 * EventMessage when destructed.
384
 *
385
 * Notable requirements:
386
 *  - The original & overriding messages must be known (not eUnidentifiedEvent).
387
 *  - The original & overriding messages must be different.
388
 *  - The passed-in Event must outlive this RAII helper.
389
 */
390
class MOZ_RAII EventMessageAutoOverride
391
{
392
public:
393
  explicit EventMessageAutoOverride(Event* aEvent,
394
                                    EventMessage aOverridingMessage)
395
    : mEvent(aEvent),
396
      mOrigMessage(mEvent->mEvent->mMessage)
397
0
  {
398
0
    MOZ_ASSERT(aOverridingMessage != mOrigMessage,
399
0
               "Don't use this class if you're not actually overriding");
400
0
    MOZ_ASSERT(aOverridingMessage != eUnidentifiedEvent,
401
0
               "Only use this class with a valid overriding EventMessage");
402
0
    MOZ_ASSERT(mOrigMessage != eUnidentifiedEvent &&
403
0
               mEvent->mEvent->mSpecifiedEventTypeString.IsEmpty(),
404
0
               "Only use this class on events whose overridden type is "
405
0
               "known (so we can restore it properly)");
406
0
407
0
    mEvent->mEvent->mMessage = aOverridingMessage;
408
0
  }
409
410
  ~EventMessageAutoOverride()
411
0
  {
412
0
    mEvent->mEvent->mMessage = mOrigMessage;
413
0
  }
414
415
protected:
416
  // Non-owning ref, which should be safe since we're a stack-allocated object
417
  // with limited lifetime. Whoever creates us should keep mEvent alive.
418
  Event* const MOZ_NON_OWNING_REF mEvent;
419
  const EventMessage mOrigMessage;
420
};
421
422
class MOZ_STACK_CLASS WantsPopupControlCheck
423
{
424
public:
425
  explicit WantsPopupControlCheck(Event* aEvent) :
426
    mEvent(aEvent)
427
0
  {
428
0
    mOriginalWantsPopupControlCheck = mEvent->GetWantsPopupControlCheck();
429
0
    mEvent->SetWantsPopupControlCheck(mEvent->IsTrusted());
430
0
  }
431
432
  ~WantsPopupControlCheck()
433
0
  {
434
0
    mEvent->SetWantsPopupControlCheck(mOriginalWantsPopupControlCheck);
435
0
  }
436
437
private:
438
  Event* mEvent;
439
  bool mOriginalWantsPopupControlCheck;
440
};
441
442
NS_DEFINE_STATIC_IID_ACCESSOR(Event, NS_EVENT_IID)
443
444
} // namespace dom
445
} // namespace mozilla
446
447
already_AddRefed<mozilla::dom::Event>
448
NS_NewDOMEvent(mozilla::dom::EventTarget* aOwner,
449
               nsPresContext* aPresContext,
450
               mozilla::WidgetEvent* aEvent);
451
452
#endif // mozilla_dom_Event_h_