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/GlobalTeardownObserver.h"
13 #include "mozilla/LinkedList.h"
14 #include "mozilla/RefPtr.h"
17 #include "nsCycleCollectionParticipant.h"
19 #include "nsGkAtoms.h"
21 #include "nsIGlobalObject.h"
22 #include "nsIScriptGlobalObject.h"
23 #include "nsISupports.h"
24 #include "nsISupportsUtils.h"
25 #include "nsPIDOMWindow.h"
26 #include "nsStringFwd.h"
29 class nsCycleCollectionTraversalCallback
;
34 class EventChainPostVisitor
;
35 class EventChainPreVisitor
;
36 class EventListenerManager
;
41 enum class CallerType
: uint32_t;
44 #define NS_DOMEVENTTARGETHELPER_IID \
46 0xa28385c6, 0x9451, 0x4d7e, { \
47 0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 \
51 class DOMEventTargetHelper
: public dom::EventTarget
,
52 public GlobalTeardownObserver
{
54 DOMEventTargetHelper();
55 explicit DOMEventTargetHelper(nsPIDOMWindowInner
* aWindow
);
56 explicit DOMEventTargetHelper(nsIGlobalObject
* aGlobalObject
);
57 explicit DOMEventTargetHelper(DOMEventTargetHelper
* aOther
);
59 NS_DECL_ISUPPORTS_INHERITED
60 NS_IMETHOD_(void) DeleteCycleCollectable() override
;
61 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS_AMBIGUOUS(
62 DOMEventTargetHelper
, dom::EventTarget
)
64 virtual EventListenerManager
* GetExistingListenerManager() const override
;
65 virtual EventListenerManager
* GetOrCreateListenerManager() override
;
67 bool ComputeDefaultWantsUntrusted(ErrorResult
& aRv
) override
;
69 using EventTarget::DispatchEvent
;
70 // TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230)
71 MOZ_CAN_RUN_SCRIPT_BOUNDARY
bool DispatchEvent(dom::Event
& aEvent
,
72 dom::CallerType aCallerType
,
73 ErrorResult
& aRv
) override
;
75 void GetEventTargetParent(EventChainPreVisitor
& aVisitor
) override
;
77 nsresult
PostHandleEvent(EventChainPostVisitor
& aVisitor
) override
;
79 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID
)
81 nsIGlobalObject
* GetOwnerGlobal() const override
{
82 return GlobalTeardownObserver::GetOwnerGlobal();
85 static DOMEventTargetHelper
* FromSupports(nsISupports
* aSupports
) {
86 dom::EventTarget
* target
= static_cast<dom::EventTarget
*>(aSupports
);
89 nsCOMPtr
<dom::EventTarget
> target_qi
= do_QueryInterface(aSupports
);
91 // If this assertion fires the QI implementation for the object in
92 // question doesn't use the EventTarget pointer as the
93 // nsISupports pointer. That must be fixed, or we'll crash...
94 NS_ASSERTION(target_qi
== target
, "Uh, fix QI!");
98 return static_cast<DOMEventTargetHelper
*>(target
);
101 bool HasListenersFor(const nsAString
& aType
) const;
103 bool HasListenersFor(nsAtom
* aTypeWithOn
) const;
105 virtual nsPIDOMWindowOuter
* GetOwnerGlobalForBindingsInternal() override
{
106 return nsPIDOMWindowOuter::GetFromCurrentInner(GetOwner());
109 // Like GetOwner, but only returns non-null if the window being returned is
110 // current (in the "current document" sense of the HTML spec).
111 nsPIDOMWindowInner
* GetWindowIfCurrent() const;
112 // Returns the document associated with this event target, if that document is
113 // the current document of its browsing context. Will return null otherwise.
114 mozilla::dom::Document
* GetDocumentIfCurrent() const;
116 void DisconnectFromOwner() override
;
117 using EventTarget::GetParentObject
;
119 virtual void EventListenerAdded(nsAtom
* aType
) override
;
121 virtual void EventListenerRemoved(nsAtom
* aType
) override
;
123 // Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
124 nsresult
DispatchTrustedEvent(const nsAString
& aEventName
);
127 virtual ~DOMEventTargetHelper();
129 nsresult
WantsUntrusted(bool* aRetVal
);
131 void MaybeUpdateKeepAlive();
132 void MaybeDontKeepAlive();
134 // If this method returns true your object is kept alive until it returns
135 // false. You can use this method instead using
136 // NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN macro.
137 virtual bool IsCertainlyAliveForCC() const { return mIsKeptAlive
; }
139 RefPtr
<EventListenerManager
> mListenerManager
;
140 // Make |event| trusted and dispatch |aEvent| to |this|.
141 nsresult
DispatchTrustedEvent(dom::Event
* aEvent
);
143 virtual void LastRelease() {}
145 void KeepAliveIfHasListenersFor(nsAtom
* aType
);
147 void IgnoreKeepAliveIfHasListenersFor(nsAtom
* aType
);
150 nsTArray
<RefPtr
<nsAtom
>> mKeepingAliveTypes
;
152 bool mIsKeptAlive
= false;
155 NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper
, NS_DOMEVENTTARGETHELPER_IID
)
157 } // namespace mozilla
159 // WebIDL event handlers
160 #define IMPL_EVENT_HANDLER(_event) \
161 inline mozilla::dom::EventHandlerNonNull* GetOn##_event() { \
162 return GetEventHandler(nsGkAtoms::on##_event); \
164 inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) { \
165 SetEventHandler(nsGkAtoms::on##_event, aCallback); \
168 #endif // mozilla_DOMEventTargetHelper_h_