JSEventHandler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_JSEventHandler_h_
  6. #define mozilla_JSEventHandler_h_
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/MemoryReporting.h"
  9. #include "mozilla/dom/EventHandlerBinding.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsCycleCollectionParticipant.h"
  12. #include "nsIAtom.h"
  13. #include "nsIDOMKeyEvent.h"
  14. #include "nsIDOMEventListener.h"
  15. #include "nsIScriptContext.h"
  16. namespace mozilla {
  17. class TypedEventHandler
  18. {
  19. public:
  20. enum HandlerType
  21. {
  22. eUnset = 0,
  23. eNormal = 0x1,
  24. eOnError = 0x2,
  25. eOnBeforeUnload = 0x3,
  26. eTypeBits = 0x3
  27. };
  28. TypedEventHandler()
  29. : mBits(0)
  30. {
  31. }
  32. explicit TypedEventHandler(dom::EventHandlerNonNull* aHandler)
  33. : mBits(0)
  34. {
  35. Assign(aHandler, eNormal);
  36. }
  37. explicit TypedEventHandler(dom::OnErrorEventHandlerNonNull* aHandler)
  38. : mBits(0)
  39. {
  40. Assign(aHandler, eOnError);
  41. }
  42. explicit TypedEventHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
  43. : mBits(0)
  44. {
  45. Assign(aHandler, eOnBeforeUnload);
  46. }
  47. TypedEventHandler(const TypedEventHandler& aOther)
  48. {
  49. if (aOther.HasEventHandler()) {
  50. // Have to make sure we take our own ref
  51. Assign(aOther.Ptr(), aOther.Type());
  52. } else {
  53. mBits = 0;
  54. }
  55. }
  56. ~TypedEventHandler()
  57. {
  58. ReleaseHandler();
  59. }
  60. HandlerType Type() const
  61. {
  62. return HandlerType(mBits & eTypeBits);
  63. }
  64. bool HasEventHandler() const
  65. {
  66. return !!Ptr();
  67. }
  68. void SetHandler(const TypedEventHandler& aHandler)
  69. {
  70. if (aHandler.HasEventHandler()) {
  71. ReleaseHandler();
  72. Assign(aHandler.Ptr(), aHandler.Type());
  73. } else {
  74. ForgetHandler();
  75. }
  76. }
  77. dom::EventHandlerNonNull* NormalEventHandler() const
  78. {
  79. MOZ_ASSERT(Type() == eNormal && Ptr());
  80. return reinterpret_cast<dom::EventHandlerNonNull*>(Ptr());
  81. }
  82. void SetHandler(dom::EventHandlerNonNull* aHandler)
  83. {
  84. ReleaseHandler();
  85. Assign(aHandler, eNormal);
  86. }
  87. dom::OnBeforeUnloadEventHandlerNonNull* OnBeforeUnloadEventHandler() const
  88. {
  89. MOZ_ASSERT(Type() == eOnBeforeUnload);
  90. return reinterpret_cast<dom::OnBeforeUnloadEventHandlerNonNull*>(Ptr());
  91. }
  92. void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
  93. {
  94. ReleaseHandler();
  95. Assign(aHandler, eOnBeforeUnload);
  96. }
  97. dom::OnErrorEventHandlerNonNull* OnErrorEventHandler() const
  98. {
  99. MOZ_ASSERT(Type() == eOnError);
  100. return reinterpret_cast<dom::OnErrorEventHandlerNonNull*>(Ptr());
  101. }
  102. void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler)
  103. {
  104. ReleaseHandler();
  105. Assign(aHandler, eOnError);
  106. }
  107. dom::CallbackFunction* Ptr() const
  108. {
  109. // Have to cast eTypeBits so we don't have to worry about
  110. // promotion issues after the bitflip.
  111. return reinterpret_cast<dom::CallbackFunction*>(mBits &
  112. ~uintptr_t(eTypeBits));
  113. }
  114. void ForgetHandler()
  115. {
  116. ReleaseHandler();
  117. mBits = 0;
  118. }
  119. bool operator==(const TypedEventHandler& aOther) const
  120. {
  121. return
  122. Ptr() && aOther.Ptr() &&
  123. Ptr()->CallbackPreserveColor() == aOther.Ptr()->CallbackPreserveColor();
  124. }
  125. private:
  126. void operator=(const TypedEventHandler&) = delete;
  127. void ReleaseHandler()
  128. {
  129. nsISupports* ptr = Ptr();
  130. NS_IF_RELEASE(ptr);
  131. }
  132. void Assign(nsISupports* aHandler, HandlerType aType)
  133. {
  134. MOZ_ASSERT(aHandler, "Must have handler");
  135. NS_ADDREF(aHandler);
  136. mBits = uintptr_t(aHandler) | uintptr_t(aType);
  137. }
  138. uintptr_t mBits;
  139. };
  140. /**
  141. * Implemented by script event listeners. Used to retrieve the script object
  142. * corresponding to the event target and the handler itself.
  143. *
  144. * Note, mTarget is a raw pointer and the owner of the JSEventHandler object
  145. * is expected to call Disconnect()!
  146. */
  147. #define NS_JSEVENTHANDLER_IID \
  148. { 0x4f486881, 0x1956, 0x4079, \
  149. { 0x8c, 0xa0, 0xf3, 0xbd, 0x60, 0x5c, 0xc2, 0x79 } }
  150. class JSEventHandler : public nsIDOMEventListener
  151. {
  152. public:
  153. NS_DECLARE_STATIC_IID_ACCESSOR(NS_JSEVENTHANDLER_IID)
  154. JSEventHandler(nsISupports* aTarget, nsIAtom* aType,
  155. const TypedEventHandler& aTypedHandler);
  156. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  157. // nsIDOMEventListener interface
  158. NS_DECL_NSIDOMEVENTLISTENER
  159. nsISupports* GetEventTarget() const
  160. {
  161. return mTarget;
  162. }
  163. void Disconnect()
  164. {
  165. mTarget = nullptr;
  166. }
  167. const TypedEventHandler& GetTypedEventHandler() const
  168. {
  169. return mTypedHandler;
  170. }
  171. void ForgetHandler()
  172. {
  173. mTypedHandler.ForgetHandler();
  174. }
  175. nsIAtom* EventName() const
  176. {
  177. return mEventName;
  178. }
  179. // Set a handler for this event listener. The handler must already
  180. // be bound to the right target.
  181. void SetHandler(const TypedEventHandler& aTypedHandler)
  182. {
  183. mTypedHandler.SetHandler(aTypedHandler);
  184. }
  185. void SetHandler(dom::EventHandlerNonNull* aHandler)
  186. {
  187. mTypedHandler.SetHandler(aHandler);
  188. }
  189. void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
  190. {
  191. mTypedHandler.SetHandler(aHandler);
  192. }
  193. void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler)
  194. {
  195. mTypedHandler.SetHandler(aHandler);
  196. }
  197. size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
  198. {
  199. return 0;
  200. // Measurement of the following members may be added later if DMD finds it
  201. // is worthwhile:
  202. // - mTarget
  203. //
  204. // The following members are not measured:
  205. // - mTypedHandler: may be shared with others
  206. // - mEventName: shared with others
  207. }
  208. size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
  209. {
  210. return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  211. }
  212. NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS(JSEventHandler)
  213. bool IsBlackForCC();
  214. protected:
  215. virtual ~JSEventHandler();
  216. nsISupports* mTarget;
  217. nsCOMPtr<nsIAtom> mEventName;
  218. TypedEventHandler mTypedHandler;
  219. };
  220. NS_DEFINE_STATIC_IID_ACCESSOR(JSEventHandler, NS_JSEVENTHANDLER_IID)
  221. } // namespace mozilla
  222. /**
  223. * Factory function. aHandler must already be bound to aTarget.
  224. * aContext is allowed to be null if aHandler is already set up.
  225. */
  226. nsresult NS_NewJSEventHandler(nsISupports* aTarget,
  227. nsIAtom* aType,
  228. const mozilla::TypedEventHandler& aTypedHandler,
  229. mozilla::JSEventHandler** aReturn);
  230. #endif // mozilla_JSEventHandler_h_