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/. */
7 #ifndef mozilla_DOMEventTargetHelper_h_
8 #define mozilla_DOMEventTargetHelper_h_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/EventTarget.h"
12 #include "mozilla/LinkedList.h"
13 #include "mozilla/RefPtr.h"
16 #include "nsCycleCollectionParticipant.h"
18 #include "nsGkAtoms.h"
20 #include "nsIGlobalObject.h"
21 #include "nsIScriptGlobalObject.h"
22 #include "nsISupports.h"
23 #include "nsISupportsUtils.h"
24 #include "nsPIDOMWindow.h"
25 #include "nsStringFwd.h"
28 class nsCycleCollectionTraversalCallback
;
33 class EventChainPostVisitor
;
34 class EventChainPreVisitor
;
35 class EventListenerManager
;
40 enum class CallerType
: uint32_t;
43 #define NS_DOMEVENTTARGETHELPER_IID \
45 0xa28385c6, 0x9451, 0x4d7e, { \
46 0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 \
50 class DOMEventTargetHelper
: public dom::EventTarget
,
51 public LinkedListElement
<DOMEventTargetHelper
> {
53 DOMEventTargetHelper();
54 explicit DOMEventTargetHelper(nsPIDOMWindowInner
* aWindow
);
55 explicit DOMEventTargetHelper(nsIGlobalObject
* aGlobalObject
);
56 explicit DOMEventTargetHelper(DOMEventTargetHelper
* aOther
);
58 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
59 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(DOMEventTargetHelper
)
61 virtual EventListenerManager
* GetExistingListenerManager() const override
;
62 virtual EventListenerManager
* GetOrCreateListenerManager() override
;
64 bool ComputeDefaultWantsUntrusted(ErrorResult
& aRv
) override
;
66 using EventTarget::DispatchEvent
;
67 // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230)
68 MOZ_CAN_RUN_SCRIPT_BOUNDARY
bool DispatchEvent(dom::Event
& aEvent
,
69 dom::CallerType aCallerType
,
70 ErrorResult
& aRv
) override
;
72 void GetEventTargetParent(EventChainPreVisitor
& aVisitor
) override
;
74 nsresult
PostHandleEvent(EventChainPostVisitor
& aVisitor
) override
;
76 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID
)
78 void GetParentObject(nsIScriptGlobalObject
** aParentObject
) {
80 CallQueryInterface(mParentObject
, aParentObject
);
82 *aParentObject
= nullptr;
86 static DOMEventTargetHelper
* FromSupports(nsISupports
* aSupports
) {
87 dom::EventTarget
* target
= static_cast<dom::EventTarget
*>(aSupports
);
90 nsCOMPtr
<dom::EventTarget
> target_qi
= do_QueryInterface(aSupports
);
92 // If this assertion fires the QI implementation for the object in
93 // question doesn't use the EventTarget pointer as the
94 // nsISupports pointer. That must be fixed, or we'll crash...
95 NS_ASSERTION(target_qi
== target
, "Uh, fix QI!");
99 return static_cast<DOMEventTargetHelper
*>(target
);
102 bool HasListenersFor(const nsAString
& aType
) const;
104 bool HasListenersFor(nsAtom
* aTypeWithOn
) const;
106 virtual nsPIDOMWindowOuter
* GetOwnerGlobalForBindingsInternal() override
{
107 return nsPIDOMWindowOuter::GetFromCurrentInner(GetOwner());
110 // A global permanently becomes invalid when DisconnectEventTargetObjects() is
111 // called. Normally this means:
112 // - For the main thread, when nsGlobalWindowInner::FreeInnerObjects is
114 // - For a worker thread, when clearing the main event queue. (Which we do
115 // slightly later than when the spec notionally calls for it to be done.)
117 // A global may also become temporarily invalid when:
118 // - For the main thread, if the window is no longer the WindowProxy's current
119 // inner window due to being placed in the bfcache.
120 nsresult
CheckCurrentGlobalCorrectness() const;
122 nsPIDOMWindowInner
* GetOwner() const { return mOwnerWindow
; }
123 // Like GetOwner, but only returns non-null if the window being returned is
124 // current (in the "current document" sense of the HTML spec).
125 nsPIDOMWindowInner
* GetWindowIfCurrent() const;
126 // Returns the document associated with this event target, if that document is
127 // the current document of its browsing context. Will return null otherwise.
128 mozilla::dom::Document
* GetDocumentIfCurrent() const;
130 virtual void DisconnectFromOwner();
131 using EventTarget::GetParentObject
;
132 nsIGlobalObject
* GetOwnerGlobal() const final
{ return mParentObject
; }
133 bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow
; }
135 virtual void EventListenerAdded(nsAtom
* aType
) override
;
137 virtual void EventListenerRemoved(nsAtom
* aType
) override
;
139 // Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
140 nsresult
DispatchTrustedEvent(const nsAString
& aEventName
);
143 virtual ~DOMEventTargetHelper();
145 nsresult
WantsUntrusted(bool* aRetVal
);
147 void MaybeUpdateKeepAlive();
148 void MaybeDontKeepAlive();
150 // If this method returns true your object is kept alive until it returns
151 // false. You can use this method instead using
152 // NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN macro.
153 virtual bool IsCertainlyAliveForCC() const { return mIsKeptAlive
; }
155 RefPtr
<EventListenerManager
> mListenerManager
;
156 // Make |event| trusted and dispatch |aEvent| to |this|.
157 nsresult
DispatchTrustedEvent(dom::Event
* aEvent
);
159 virtual void LastRelease() {}
161 void KeepAliveIfHasListenersFor(nsAtom
* aType
);
163 void IgnoreKeepAliveIfHasListenersFor(nsAtom
* aType
);
165 void BindToOwner(nsIGlobalObject
* aOwner
);
168 // The parent global object. The global will clear this when
169 // it is destroyed by calling DisconnectFromOwner().
170 nsIGlobalObject
* MOZ_NON_OWNING_REF mParentObject
;
171 // mParentObject pre QI-ed and cached (inner window)
172 // (it is needed for off main thread access)
173 // It is obtained in BindToOwner and reset in DisconnectFromOwner.
174 nsPIDOMWindowInner
* MOZ_NON_OWNING_REF mOwnerWindow
;
175 bool mHasOrHasHadOwnerWindow
;
177 nsTArray
<RefPtr
<nsAtom
>> mKeepingAliveTypes
;
182 NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper
, NS_DOMEVENTTARGETHELPER_IID
)
184 } // namespace mozilla
186 // WebIDL event handlers
187 #define IMPL_EVENT_HANDLER(_event) \
188 inline mozilla::dom::EventHandlerNonNull* GetOn##_event() { \
189 return GetEventHandler(nsGkAtoms::on##_event); \
191 inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) { \
192 SetEventHandler(nsGkAtoms::on##_event, aCallback); \
195 #endif // mozilla_DOMEventTargetHelper_h_