Bug 1708422: part 14) Extend documentation of `mozInlineSpellChecker::SpellCheckerTim...
[gecko.git] / dom / events / DOMEventTargetHelper.h
blobd44283113ac2f542f98852a8f9c048cbec96871e
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"
14 #include "nsAtom.h"
15 #include "nsCOMPtr.h"
16 #include "nsCycleCollectionParticipant.h"
17 #include "nsDebug.h"
18 #include "nsGkAtoms.h"
19 #include "nsID.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"
26 #include "nsTArray.h"
28 class nsCycleCollectionTraversalCallback;
30 namespace mozilla {
32 class ErrorResult;
33 class EventChainPostVisitor;
34 class EventChainPreVisitor;
35 class EventListenerManager;
37 namespace dom {
38 class Document;
39 class Event;
40 enum class CallerType : uint32_t;
41 } // namespace dom
43 #define NS_DOMEVENTTARGETHELPER_IID \
44 { \
45 0xa28385c6, 0x9451, 0x4d7e, { \
46 0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 \
47 } \
50 class DOMEventTargetHelper : public dom::EventTarget,
51 public LinkedListElement<DOMEventTargetHelper> {
52 public:
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_SCRIPT_HOLDER_CLASS(DOMEventTargetHelper)
61 virtual EventListenerManager* GetExistingListenerManager() const override;
62 virtual EventListenerManager* GetOrCreateListenerManager() override;
64 bool ComputeDefaultWantsUntrusted(ErrorResult& aRv) override;
66 using EventTarget::DispatchEvent;
67 bool DispatchEvent(dom::Event& aEvent, dom::CallerType aCallerType,
68 ErrorResult& aRv) override;
70 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
72 nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
74 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID)
76 void GetParentObject(nsIScriptGlobalObject** aParentObject) {
77 if (mParentObject) {
78 CallQueryInterface(mParentObject, aParentObject);
79 } else {
80 *aParentObject = nullptr;
84 static DOMEventTargetHelper* FromSupports(nsISupports* aSupports) {
85 dom::EventTarget* target = static_cast<dom::EventTarget*>(aSupports);
86 #ifdef DEBUG
88 nsCOMPtr<dom::EventTarget> target_qi = do_QueryInterface(aSupports);
90 // If this assertion fires the QI implementation for the object in
91 // question doesn't use the EventTarget pointer as the
92 // nsISupports pointer. That must be fixed, or we'll crash...
93 NS_ASSERTION(target_qi == target, "Uh, fix QI!");
95 #endif
97 return static_cast<DOMEventTargetHelper*>(target);
100 bool HasListenersFor(const nsAString& aType) const;
102 bool HasListenersFor(nsAtom* aTypeWithOn) const;
104 virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindingsInternal() override {
105 return nsPIDOMWindowOuter::GetFromCurrentInner(GetOwner());
108 // A global permanently becomes invalid when DisconnectEventTargetObjects() is
109 // called. Normally this means:
110 // - For the main thread, when nsGlobalWindowInner::FreeInnerObjects is
111 // called.
112 // - For a worker thread, when clearing the main event queue. (Which we do
113 // slightly later than when the spec notionally calls for it to be done.)
115 // A global may also become temporarily invalid when:
116 // - For the main thread, if the window is no longer the WindowProxy's current
117 // inner window due to being placed in the bfcache.
118 nsresult CheckCurrentGlobalCorrectness() const;
120 nsPIDOMWindowInner* GetOwner() const { return mOwnerWindow; }
121 // Like GetOwner, but only returns non-null if the window being returned is
122 // current (in the "current document" sense of the HTML spec).
123 nsPIDOMWindowInner* GetWindowIfCurrent() const;
124 // Returns the document associated with this event target, if that document is
125 // the current document of its browsing context. Will return null otherwise.
126 mozilla::dom::Document* GetDocumentIfCurrent() const;
128 virtual void DisconnectFromOwner();
129 using EventTarget::GetParentObject;
130 nsIGlobalObject* GetOwnerGlobal() const final { return mParentObject; }
131 bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow; }
133 virtual void EventListenerAdded(nsAtom* aType) override;
135 virtual void EventListenerRemoved(nsAtom* aType) override;
137 // Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
138 nsresult DispatchTrustedEvent(const nsAString& aEventName);
140 protected:
141 virtual ~DOMEventTargetHelper();
143 nsresult WantsUntrusted(bool* aRetVal);
145 void MaybeUpdateKeepAlive();
146 void MaybeDontKeepAlive();
148 // If this method returns true your object is kept alive until it returns
149 // false. You can use this method instead using
150 // NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN macro.
151 virtual bool IsCertainlyAliveForCC() const { return mIsKeptAlive; }
153 RefPtr<EventListenerManager> mListenerManager;
154 // Make |event| trusted and dispatch |aEvent| to |this|.
155 nsresult DispatchTrustedEvent(dom::Event* aEvent);
157 virtual void LastRelease() {}
159 void KeepAliveIfHasListenersFor(const nsAString& aType);
160 void KeepAliveIfHasListenersFor(nsAtom* aType);
162 void IgnoreKeepAliveIfHasListenersFor(const nsAString& aType);
163 void IgnoreKeepAliveIfHasListenersFor(nsAtom* aType);
165 void BindToOwner(nsIGlobalObject* aOwner);
167 private:
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 struct {
178 nsTArray<nsString> mStrings;
179 nsTArray<RefPtr<nsAtom>> mAtoms;
180 } mKeepingAliveTypes;
182 bool mIsKeptAlive;
185 NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper, NS_DOMEVENTTARGETHELPER_IID)
187 } // namespace mozilla
189 // WebIDL event handlers
190 #define IMPL_EVENT_HANDLER(_event) \
191 inline mozilla::dom::EventHandlerNonNull* GetOn##_event() { \
192 return GetEventHandler(nsGkAtoms::on##_event); \
194 inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) { \
195 SetEventHandler(nsGkAtoms::on##_event, aCallback); \
198 #endif // mozilla_DOMEventTargetHelper_h_