Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/EventTarget.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_EventTarget_h_
8
#define mozilla_dom_EventTarget_h_
9
10
#include "mozilla/dom/BindingDeclarations.h"
11
#include "mozilla/dom/Nullable.h"
12
#include "nsISupports.h"
13
#include "nsWrapperCache.h"
14
#include "nsAtom.h"
15
16
class nsPIDOMWindowOuter;
17
class nsIGlobalObject;
18
class nsIDOMEventListener;
19
20
namespace mozilla {
21
22
class AsyncEventDispatcher;
23
class ErrorResult;
24
class EventChainPostVisitor;
25
class EventChainPreVisitor;
26
class EventChainVisitor;
27
class EventListenerManager;
28
29
namespace dom {
30
31
class AddEventListenerOptionsOrBoolean;
32
class Event;
33
class EventListener;
34
class EventListenerOptionsOrBoolean;
35
class EventHandlerNonNull;
36
class GlobalObject;
37
38
// IID for the dom::EventTarget interface
39
#define NS_EVENTTARGET_IID \
40
{ 0xde651c36, 0x0053, 0x4c67, \
41
  { 0xb1, 0x3d, 0x67, 0xb9, 0x40, 0xfc, 0x82, 0xe4 } }
42
43
class EventTarget : public nsISupports,
44
                    public nsWrapperCache
45
{
46
public:
47
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_EVENTTARGET_IID)
48
49
  // WebIDL API
50
  static already_AddRefed<EventTarget> Constructor(const GlobalObject& aGlobal,
51
                                                   ErrorResult& aRv);
52
  void AddEventListener(const nsAString& aType,
53
                        EventListener* aCallback,
54
                        const AddEventListenerOptionsOrBoolean& aOptions,
55
                        const Nullable<bool>& aWantsUntrusted,
56
                        ErrorResult& aRv);
57
  void RemoveEventListener(const nsAString& aType,
58
                           EventListener* aCallback,
59
                           const EventListenerOptionsOrBoolean& aOptions,
60
                           ErrorResult& aRv);
61
62
protected:
63
  /**
64
   * This method allows addition of event listeners represented by
65
   * nsIDOMEventListener, with almost the same semantics as the
66
   * standard AddEventListener.  The one difference is that it just
67
   * has a "use capture" boolean, not an EventListenerOptions.
68
   */
69
  nsresult AddEventListener(const nsAString& aType,
70
                            nsIDOMEventListener* aListener,
71
                            bool aUseCapture,
72
                            const Nullable<bool>& aWantsUntrusted);
73
74
public:
75
  /**
76
   * Helper methods to make the nsIDOMEventListener version of
77
   * AddEventListener simpler to call for consumers.
78
   */
79
  nsresult AddEventListener(const nsAString& aType,
80
                            nsIDOMEventListener* aListener,
81
                            bool aUseCapture)
82
0
  {
83
0
    return AddEventListener(aType, aListener, aUseCapture, Nullable<bool>());
84
0
  }
85
  nsresult AddEventListener(const nsAString& aType,
86
                            nsIDOMEventListener* aListener,
87
                            bool aUseCapture,
88
                            bool aWantsUntrusted)
89
0
  {
90
0
    return AddEventListener(aType, aListener, aUseCapture,
91
0
                            Nullable<bool>(aWantsUntrusted));
92
0
  }
93
94
  /**
95
   * This method allows the removal of event listeners represented by
96
   * nsIDOMEventListener from the event target, with the same semantics as the
97
   * standard RemoveEventListener.
98
   */
99
  void RemoveEventListener(const nsAString& aType,
100
                           nsIDOMEventListener* aListener,
101
                           bool aUseCapture);
102
  /**
103
   * RemoveSystemEventListener() should be used if you have used
104
   * AddSystemEventListener().
105
   */
106
  void RemoveSystemEventListener(const nsAString& aType,
107
                                 nsIDOMEventListener* aListener,
108
                                 bool aUseCapture);
109
110
  /**
111
   * Add a system event listener with the default wantsUntrusted value.
112
   */
113
  nsresult AddSystemEventListener(const nsAString& aType,
114
                                  nsIDOMEventListener* aListener,
115
                                  bool aUseCapture)
116
0
  {
117
0
    return AddSystemEventListener(aType, aListener, aUseCapture, Nullable<bool>());
118
0
  }
119
120
  /**
121
   * Add a system event listener with the given wantsUntrusted value.
122
   */
123
  nsresult AddSystemEventListener(const nsAString& aType,
124
                                  nsIDOMEventListener* aListener,
125
                                  bool aUseCapture,
126
                                  bool aWantsUntrusted)
127
0
  {
128
0
    return AddSystemEventListener(aType, aListener, aUseCapture,
129
0
                                  Nullable<bool>(aWantsUntrusted));
130
0
  }
131
132
  /**
133
   * Returns the EventTarget object which should be used as the target
134
   * of DOMEvents.
135
   * Usually |this| is returned, but for example Window (inner windw) returns
136
   * the WindowProxy (outer window).
137
   */
138
  virtual EventTarget* GetTargetForDOMEvent()
139
0
  {
140
0
    return this;
141
0
  };
142
143
  /**
144
   * Returns the EventTarget object which should be used as the target
145
   * of the event and when constructing event target chain.
146
   * Usually |this| is returned, but for example WindowProxy (outer window) returns
147
   * the Window (inner window).
148
   */
149
  virtual EventTarget* GetTargetForEventTargetChain()
150
0
  {
151
0
    return this;
152
0
  }
153
154
  /**
155
   * The most general DispatchEvent method.  This is the one the bindings call.
156
   */
157
  virtual bool DispatchEvent(Event& aEvent, CallerType aCallerType,
158
                             ErrorResult& aRv) = 0;
159
160
  /**
161
   * A version of DispatchEvent you can use if you really don't care whether it
162
   * succeeds or not and whether default is prevented or not.
163
   */
164
  void DispatchEvent(Event& aEvent);
165
166
  /**
167
   * A version of DispatchEvent you can use if you really don't care whether
168
   * default is prevented or not.
169
   */
170
  void DispatchEvent(Event& aEvent, ErrorResult& aRv);
171
172
  nsIGlobalObject* GetParentObject() const
173
0
  {
174
0
    return GetOwnerGlobal();
175
0
  }
176
177
  // Note, this takes the type in onfoo form!
178
  EventHandlerNonNull* GetEventHandler(const nsAString& aType)
179
0
  {
180
0
    RefPtr<nsAtom> type = NS_Atomize(aType);
181
0
    return GetEventHandler(type);
182
0
  }
183
184
  // Note, this takes the type in onfoo form!
185
  void SetEventHandler(const nsAString& aType, EventHandlerNonNull* aHandler,
186
                       ErrorResult& rv);
187
188
  // For an event 'foo' aType will be 'onfoo'.
189
0
  virtual void EventListenerAdded(nsAtom* aType) {}
190
191
  // For an event 'foo' aType will be 'onfoo'.
192
0
  virtual void EventListenerRemoved(nsAtom* aType) {}
193
194
  // Returns an outer window that corresponds to the inner window this event
195
  // target is associated with.  Will return null if the inner window is not the
196
  // current inner or if there is no window around at all.
197
  virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindings() = 0;
198
199
  // The global object this event target is associated with, if any.
200
  // This may be an inner window or some other global object.  This
201
  // will never be an outer window.
202
  virtual nsIGlobalObject* GetOwnerGlobal() const = 0;
203
204
  /**
205
   * Get the event listener manager, creating it if it does not already exist.
206
   */
207
  virtual EventListenerManager* GetOrCreateListenerManager() = 0;
208
209
  /**
210
   * Get the event listener manager, returning null if it does not already
211
   * exist.
212
   */
213
  virtual EventListenerManager* GetExistingListenerManager() const = 0;
214
215
  // Called from AsyncEventDispatcher to notify it is running.
216
0
  virtual void AsyncEventRunning(AsyncEventDispatcher* aEvent) {}
217
218
  // Used by APZ to determine whether this event target has non-chrome event
219
  // listeners for untrusted key events.
220
  bool HasNonSystemGroupListenersForUntrustedKeyEvents() const;
221
222
  // Used by APZ to determine whether this event target has non-chrome and
223
  // non-passive event listeners for untrusted key events.
224
  bool HasNonPassiveNonSystemGroupListenersForUntrustedKeyEvents() const;
225
226
  virtual bool IsApzAware() const;
227
228
  /**
229
   * Called before the capture phase of the event flow.
230
   * This is used to create the event target chain and implementations
231
   * should set the necessary members of EventChainPreVisitor.
232
   * At least aVisitor.mCanHandle must be set,
233
   * usually also aVisitor.mParentTarget if mCanHandle is true.
234
   * mCanHandle says that this object can handle the aVisitor.mEvent event and
235
   * the mParentTarget is the possible parent object for the event target chain.
236
   * @see EventDispatcher.h for more documentation about aVisitor.
237
   *
238
   * @param aVisitor the visitor object which is used to create the
239
   *                 event target chain for event dispatching.
240
   *
241
   * @note Only EventDispatcher should call this method.
242
   */
243
  virtual void GetEventTargetParent(EventChainPreVisitor& aVisitor) = 0;
244
245
  /**
246
   * Called before the capture phase of the event flow and after event target
247
   * chain creation. This is used to handle things that must be executed before
248
   * dispatching the event to DOM.
249
   */
250
  virtual nsresult PreHandleEvent(EventChainVisitor& aVisitor)
251
0
  {
252
0
    return NS_OK;
253
0
  }
254
255
  /**
256
   * If EventChainPreVisitor.mWantsWillHandleEvent is set true,
257
   * called just before possible event handlers on this object will be called.
258
   */
259
  virtual void WillHandleEvent(EventChainPostVisitor& aVisitor)
260
0
  {
261
0
  }
262
263
  /**
264
   * Called after the bubble phase of the system event group.
265
   * The default handling of the event should happen here.
266
   * @param aVisitor the visitor object which is used during post handling.
267
   *
268
   * @see EventDispatcher.h for documentation about aVisitor.
269
   * @note Only EventDispatcher should call this method.
270
   */
271
  virtual nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) = 0;
272
273
protected:
274
  EventHandlerNonNull* GetEventHandler(nsAtom* aType);
275
  void SetEventHandler(nsAtom* aType, EventHandlerNonNull* aHandler);
276
277
  /**
278
   * Hook for AddEventListener that allows it to compute the right
279
   * wantsUntrusted boolean when one is not provided.  If this returns failure,
280
   * the listener will not be added.
281
   *
282
   * This hook will NOT be called unless aWantsUntrusted is null in
283
   * AddEventListener.  If you need to take action when event listeners are
284
   * added, use EventListenerAdded.  Especially because not all event listener
285
   * additions go through AddEventListener!
286
   */
287
  virtual bool ComputeDefaultWantsUntrusted(ErrorResult& aRv) = 0;
288
289
  /**
290
   * A method to compute the right wantsUntrusted value for AddEventListener.
291
   * This will call the above hook as needed.
292
   *
293
   * If aOptions is non-null, and it contains a value for mWantUntrusted, that
294
   * value takes precedence over aWantsUntrusted.
295
   */
296
  bool ComputeWantsUntrusted(const Nullable<bool>& aWantsUntrusted,
297
                             const AddEventListenerOptionsOrBoolean* aOptions,
298
                             ErrorResult& aRv);
299
300
  /**
301
   * addSystemEventListener() adds an event listener of aType to the system
302
   * group.  Typically, core code should use the system group for listening to
303
   * content (i.e., non-chrome) element's events.  If core code uses
304
   * EventTarget::AddEventListener for a content node, it means
305
   * that the listener cannot listen to the event when web content calls
306
   * stopPropagation() of the event.
307
   *
308
   * @param aType            An event name you're going to handle.
309
   * @param aListener        An event listener.
310
   * @param aUseCapture      true if you want to listen the event in capturing
311
   *                         phase.  Otherwise, false.
312
   * @param aWantsUntrusted  true if you want to handle untrusted events.
313
   *                         false if not.
314
   *                         Null if you want the default behavior.
315
   */
316
  nsresult AddSystemEventListener(const nsAString& aType,
317
                                  nsIDOMEventListener* aListener,
318
                                  bool aUseCapture,
319
                                  const Nullable<bool>& aWantsUntrusted);
320
};
321
322
NS_DEFINE_STATIC_IID_ACCESSOR(EventTarget, NS_EVENTTARGET_IID)
323
324
} // namespace dom
325
} // namespace mozilla
326
327
#endif // mozilla_dom_EventTarget_h_