DOMEventTargetHelper.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_DOMEventTargetHelper_h_
  6. #define mozilla_DOMEventTargetHelper_h_
  7. #include "nsCOMPtr.h"
  8. #include "nsGkAtoms.h"
  9. #include "nsCycleCollectionParticipant.h"
  10. #include "nsPIDOMWindow.h"
  11. #include "nsIScriptGlobalObject.h"
  12. #include "nsIScriptContext.h"
  13. #include "nsIWeakReferenceUtils.h"
  14. #include "MainThreadUtils.h"
  15. #include "mozilla/Attributes.h"
  16. #include "mozilla/EventListenerManager.h"
  17. #include "mozilla/dom/EventTarget.h"
  18. struct JSCompartment;
  19. class nsIDocument;
  20. namespace mozilla {
  21. class ErrorResult;
  22. #define NS_DOMEVENTTARGETHELPER_IID \
  23. { 0xa28385c6, 0x9451, 0x4d7e, \
  24. { 0xa3, 0xdd, 0xf4, 0xb6, 0x87, 0x2f, 0xa4, 0x76 } }
  25. class DOMEventTargetHelper : public dom::EventTarget
  26. {
  27. public:
  28. DOMEventTargetHelper()
  29. : mParentObject(nullptr)
  30. , mOwnerWindow(nullptr)
  31. , mHasOrHasHadOwnerWindow(false)
  32. {
  33. }
  34. explicit DOMEventTargetHelper(nsPIDOMWindowInner* aWindow)
  35. : mParentObject(nullptr)
  36. , mOwnerWindow(nullptr)
  37. , mHasOrHasHadOwnerWindow(false)
  38. {
  39. BindToOwner(aWindow);
  40. }
  41. explicit DOMEventTargetHelper(nsIGlobalObject* aGlobalObject)
  42. : mParentObject(nullptr)
  43. , mOwnerWindow(nullptr)
  44. , mHasOrHasHadOwnerWindow(false)
  45. {
  46. BindToOwner(aGlobalObject);
  47. }
  48. explicit DOMEventTargetHelper(DOMEventTargetHelper* aOther)
  49. : mParentObject(nullptr)
  50. , mOwnerWindow(nullptr)
  51. , mHasOrHasHadOwnerWindow(false)
  52. {
  53. BindToOwner(aOther);
  54. }
  55. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  56. NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(DOMEventTargetHelper)
  57. NS_DECL_NSIDOMEVENTTARGET
  58. virtual EventListenerManager* GetExistingListenerManager() const override;
  59. virtual EventListenerManager* GetOrCreateListenerManager() override;
  60. using dom::EventTarget::RemoveEventListener;
  61. virtual void AddEventListener(const nsAString& aType,
  62. dom::EventListener* aListener,
  63. const dom::AddEventListenerOptionsOrBoolean& aOptions,
  64. const dom::Nullable<bool>& aWantsUntrusted,
  65. ErrorResult& aRv) override;
  66. NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMEVENTTARGETHELPER_IID)
  67. void GetParentObject(nsIScriptGlobalObject **aParentObject)
  68. {
  69. if (mParentObject) {
  70. CallQueryInterface(mParentObject, aParentObject);
  71. } else {
  72. *aParentObject = nullptr;
  73. }
  74. }
  75. static DOMEventTargetHelper* FromSupports(nsISupports* aSupports)
  76. {
  77. dom::EventTarget* target = static_cast<dom::EventTarget*>(aSupports);
  78. #ifdef DEBUG
  79. {
  80. nsCOMPtr<dom::EventTarget> target_qi = do_QueryInterface(aSupports);
  81. // If this assertion fires the QI implementation for the object in
  82. // question doesn't use the EventTarget pointer as the
  83. // nsISupports pointer. That must be fixed, or we'll crash...
  84. NS_ASSERTION(target_qi == target, "Uh, fix QI!");
  85. }
  86. #endif
  87. return static_cast<DOMEventTargetHelper*>(target);
  88. }
  89. bool HasListenersFor(const nsAString& aType)
  90. {
  91. return mListenerManager && mListenerManager->HasListenersFor(aType);
  92. }
  93. bool HasListenersFor(nsIAtom* aTypeWithOn)
  94. {
  95. return mListenerManager && mListenerManager->HasListenersFor(aTypeWithOn);
  96. }
  97. nsresult SetEventHandler(nsIAtom* aType,
  98. JSContext* aCx,
  99. const JS::Value& aValue);
  100. using dom::EventTarget::SetEventHandler;
  101. void GetEventHandler(nsIAtom* aType,
  102. JSContext* aCx,
  103. JS::Value* aValue);
  104. using dom::EventTarget::GetEventHandler;
  105. virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindings() override
  106. {
  107. return nsPIDOMWindowOuter::GetFromCurrentInner(GetOwner());
  108. }
  109. nsresult CheckInnerWindowCorrectness() const
  110. {
  111. NS_ENSURE_STATE(!mHasOrHasHadOwnerWindow || mOwnerWindow);
  112. if (mOwnerWindow && !mOwnerWindow->IsCurrentInnerWindow()) {
  113. return NS_ERROR_FAILURE;
  114. }
  115. return NS_OK;
  116. }
  117. nsPIDOMWindowInner* GetOwner() const { return mOwnerWindow; }
  118. // Like GetOwner, but only returns non-null if the window being returned is
  119. // current (in the "current document" sense of the HTML spec).
  120. nsPIDOMWindowInner* GetWindowIfCurrent() const;
  121. // Returns the document associated with this event target, if that document is
  122. // the current document of its browsing context. Will return null otherwise.
  123. nsIDocument* GetDocumentIfCurrent() const;
  124. void BindToOwner(nsIGlobalObject* aOwner);
  125. void BindToOwner(nsPIDOMWindowInner* aOwner);
  126. void BindToOwner(DOMEventTargetHelper* aOther);
  127. virtual void DisconnectFromOwner();
  128. using EventTarget::GetParentObject;
  129. virtual nsIGlobalObject* GetOwnerGlobal() const override
  130. {
  131. nsCOMPtr<nsIGlobalObject> parentObject = do_QueryReferent(mParentObject);
  132. return parentObject;
  133. }
  134. bool HasOrHasHadOwner() { return mHasOrHasHadOwnerWindow; }
  135. virtual void EventListenerAdded(nsIAtom* aType) override;
  136. virtual void EventListenerRemoved(nsIAtom* aType) override;
  137. virtual void EventListenerWasAdded(const nsAString& aType,
  138. ErrorResult& aRv,
  139. JSCompartment* aCompartment = nullptr) {}
  140. virtual void EventListenerWasRemoved(const nsAString& aType,
  141. ErrorResult& aRv,
  142. JSCompartment* aCompartment = nullptr) {}
  143. // Dispatch a trusted, non-cancellable and non-bubbling event to |this|.
  144. nsresult DispatchTrustedEvent(const nsAString& aEventName);
  145. protected:
  146. virtual ~DOMEventTargetHelper();
  147. nsresult WantsUntrusted(bool* aRetVal);
  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
  152. {
  153. return false;
  154. }
  155. RefPtr<EventListenerManager> mListenerManager;
  156. // Make |event| trusted and dispatch |aEvent| to |this|.
  157. nsresult DispatchTrustedEvent(nsIDOMEvent* aEvent);
  158. virtual void LastRelease() {}
  159. private:
  160. // Inner window or sandbox.
  161. nsWeakPtr mParentObject;
  162. // mParentObject pre QI-ed and cached (inner window)
  163. // (it is needed for off main thread access)
  164. // It is obtained in BindToOwner and reset in DisconnectFromOwner.
  165. nsPIDOMWindowInner* MOZ_NON_OWNING_REF mOwnerWindow;
  166. bool mHasOrHasHadOwnerWindow;
  167. };
  168. NS_DEFINE_STATIC_IID_ACCESSOR(DOMEventTargetHelper,
  169. NS_DOMEVENTTARGETHELPER_IID)
  170. } // namespace mozilla
  171. // XPIDL event handlers
  172. #define NS_IMPL_EVENT_HANDLER(_class, _event) \
  173. NS_IMETHODIMP _class::GetOn##_event(JSContext* aCx, \
  174. JS::MutableHandle<JS::Value> aValue) \
  175. { \
  176. GetEventHandler(nsGkAtoms::on##_event, aCx, aValue.address()); \
  177. return NS_OK; \
  178. } \
  179. NS_IMETHODIMP _class::SetOn##_event(JSContext* aCx, \
  180. JS::Handle<JS::Value> aValue) \
  181. { \
  182. return SetEventHandler(nsGkAtoms::on##_event, aCx, aValue); \
  183. }
  184. #define NS_IMPL_FORWARD_EVENT_HANDLER(_class, _event, _baseclass) \
  185. NS_IMETHODIMP _class::GetOn##_event(JSContext* aCx, \
  186. JS::MutableHandle<JS::Value> aValue) \
  187. { \
  188. return _baseclass::GetOn##_event(aCx, aValue); \
  189. } \
  190. NS_IMETHODIMP _class::SetOn##_event(JSContext* aCx, \
  191. JS::Handle<JS::Value> aValue) \
  192. { \
  193. return _baseclass::SetOn##_event(aCx, aValue); \
  194. }
  195. // WebIDL event handlers
  196. #define IMPL_EVENT_HANDLER(_event) \
  197. inline mozilla::dom::EventHandlerNonNull* GetOn##_event() \
  198. { \
  199. if (NS_IsMainThread()) { \
  200. return GetEventHandler(nsGkAtoms::on##_event, EmptyString()); \
  201. } \
  202. return GetEventHandler(nullptr, NS_LITERAL_STRING(#_event)); \
  203. } \
  204. inline void SetOn##_event(mozilla::dom::EventHandlerNonNull* aCallback) \
  205. { \
  206. if (NS_IsMainThread()) { \
  207. SetEventHandler(nsGkAtoms::on##_event, EmptyString(), aCallback); \
  208. } else { \
  209. SetEventHandler(nullptr, NS_LITERAL_STRING(#_event), aCallback); \
  210. } \
  211. }
  212. /* Use this macro to declare functions that forward the behavior of this
  213. * interface to another object.
  214. * This macro doesn't forward GetEventTargetParent because sometimes subclasses
  215. * want to override it.
  216. */
  217. #define NS_FORWARD_NSIDOMEVENTTARGET_NOGETEVENTTARGETPARENT(_to) \
  218. NS_IMETHOD AddEventListener(const nsAString & type, nsIDOMEventListener *listener, bool useCapture, bool wantsUntrusted, uint8_t _argc) { \
  219. return _to AddEventListener(type, listener, useCapture, wantsUntrusted, _argc); \
  220. } \
  221. NS_IMETHOD AddSystemEventListener(const nsAString & type, nsIDOMEventListener *listener, bool aUseCapture, bool aWantsUntrusted, uint8_t _argc) { \
  222. return _to AddSystemEventListener(type, listener, aUseCapture, aWantsUntrusted, _argc); \
  223. } \
  224. NS_IMETHOD RemoveEventListener(const nsAString & type, nsIDOMEventListener *listener, bool useCapture) { \
  225. return _to RemoveEventListener(type, listener, useCapture); \
  226. } \
  227. NS_IMETHOD RemoveSystemEventListener(const nsAString & type, nsIDOMEventListener *listener, bool aUseCapture) { \
  228. return _to RemoveSystemEventListener(type, listener, aUseCapture); \
  229. } \
  230. NS_IMETHOD DispatchEvent(nsIDOMEvent *evt, bool *_retval) { \
  231. return _to DispatchEvent(evt, _retval); \
  232. } \
  233. virtual mozilla::dom::EventTarget* GetTargetForDOMEvent() { \
  234. return _to GetTargetForDOMEvent(); \
  235. } \
  236. virtual mozilla::dom::EventTarget* GetTargetForEventTargetChain() { \
  237. return _to GetTargetForEventTargetChain(); \
  238. } \
  239. virtual nsresult WillHandleEvent( \
  240. mozilla::EventChainPostVisitor & aVisitor) { \
  241. return _to WillHandleEvent(aVisitor); \
  242. } \
  243. virtual nsresult PostHandleEvent( \
  244. mozilla::EventChainPostVisitor & aVisitor) { \
  245. return _to PostHandleEvent(aVisitor); \
  246. } \
  247. virtual nsresult DispatchDOMEvent(mozilla::WidgetEvent* aEvent, nsIDOMEvent* aDOMEvent, nsPresContext* aPresContext, nsEventStatus* aEventStatus) { \
  248. return _to DispatchDOMEvent(aEvent, aDOMEvent, aPresContext, aEventStatus); \
  249. } \
  250. virtual mozilla::EventListenerManager* GetOrCreateListenerManager() { \
  251. return _to GetOrCreateListenerManager(); \
  252. } \
  253. virtual mozilla::EventListenerManager* GetExistingListenerManager() const { \
  254. return _to GetExistingListenerManager(); \
  255. } \
  256. virtual nsIScriptContext * GetContextForEventHandlers(nsresult *aRv) { \
  257. return _to GetContextForEventHandlers(aRv); \
  258. }
  259. #define NS_REALLY_FORWARD_NSIDOMEVENTTARGET(_class) \
  260. using _class::AddEventListener; \
  261. using _class::RemoveEventListener; \
  262. NS_FORWARD_NSIDOMEVENTTARGET(_class::) \
  263. virtual mozilla::EventListenerManager* \
  264. GetOrCreateListenerManager() override { \
  265. return _class::GetOrCreateListenerManager(); \
  266. } \
  267. virtual mozilla::EventListenerManager* \
  268. GetExistingListenerManager() const override { \
  269. return _class::GetExistingListenerManager(); \
  270. }
  271. #endif // mozilla_DOMEventTargetHelper_h_