EventTarget.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_dom_EventTarget_h_
  6. #define mozilla_dom_EventTarget_h_
  7. #include "nsIDOMEventTarget.h"
  8. #include "nsWrapperCache.h"
  9. #include "nsIAtom.h"
  10. class nsPIDOMWindowOuter;
  11. class nsIGlobalObject;
  12. namespace mozilla {
  13. class AsyncEventDispatcher;
  14. class ErrorResult;
  15. class EventListenerManager;
  16. namespace dom {
  17. class AddEventListenerOptionsOrBoolean;
  18. class Event;
  19. class EventListener;
  20. class EventListenerOptionsOrBoolean;
  21. class EventHandlerNonNull;
  22. class GlobalObject;
  23. template <class T> struct Nullable;
  24. // IID for the dom::EventTarget interface
  25. #define NS_EVENTTARGET_IID \
  26. { 0xde651c36, 0x0053, 0x4c67, \
  27. { 0xb1, 0x3d, 0x67, 0xb9, 0x40, 0xfc, 0x82, 0xe4 } }
  28. class EventTarget : public nsIDOMEventTarget,
  29. public nsWrapperCache
  30. {
  31. public:
  32. NS_DECLARE_STATIC_IID_ACCESSOR(NS_EVENTTARGET_IID)
  33. // WebIDL API
  34. static already_AddRefed<EventTarget> Constructor(const GlobalObject& aGlobal,
  35. ErrorResult& aRv);
  36. using nsIDOMEventTarget::AddEventListener;
  37. using nsIDOMEventTarget::RemoveEventListener;
  38. using nsIDOMEventTarget::DispatchEvent;
  39. virtual void AddEventListener(const nsAString& aType,
  40. EventListener* aCallback,
  41. const AddEventListenerOptionsOrBoolean& aOptions,
  42. const Nullable<bool>& aWantsUntrusted,
  43. ErrorResult& aRv) = 0;
  44. virtual void RemoveEventListener(const nsAString& aType,
  45. EventListener* aCallback,
  46. const EventListenerOptionsOrBoolean& aOptions,
  47. ErrorResult& aRv);
  48. bool DispatchEvent(JSContext* aCx, Event& aEvent, ErrorResult& aRv);
  49. nsIGlobalObject* GetParentObject() const
  50. {
  51. return GetOwnerGlobal();
  52. }
  53. // Note, this takes the type in onfoo form!
  54. EventHandlerNonNull* GetEventHandler(const nsAString& aType)
  55. {
  56. nsCOMPtr<nsIAtom> type = NS_Atomize(aType);
  57. return GetEventHandler(type, EmptyString());
  58. }
  59. // Note, this takes the type in onfoo form!
  60. void SetEventHandler(const nsAString& aType, EventHandlerNonNull* aHandler,
  61. ErrorResult& rv);
  62. // Note, for an event 'foo' aType will be 'onfoo'.
  63. virtual void EventListenerAdded(nsIAtom* aType) {}
  64. virtual void EventListenerRemoved(nsIAtom* aType) {}
  65. // Returns an outer window that corresponds to the inner window this event
  66. // target is associated with. Will return null if the inner window is not the
  67. // current inner or if there is no window around at all.
  68. virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindings() = 0;
  69. // The global object this event target is associated with, if any.
  70. // This may be an inner window or some other global object. This
  71. // will never be an outer window.
  72. virtual nsIGlobalObject* GetOwnerGlobal() const = 0;
  73. /**
  74. * Get the event listener manager, creating it if it does not already exist.
  75. */
  76. virtual EventListenerManager* GetOrCreateListenerManager() = 0;
  77. /**
  78. * Get the event listener manager, returning null if it does not already
  79. * exist.
  80. */
  81. virtual EventListenerManager* GetExistingListenerManager() const = 0;
  82. // Called from AsyncEventDispatcher to notify it is running.
  83. virtual void AsyncEventRunning(AsyncEventDispatcher* aEvent) {}
  84. virtual bool IsApzAware() const;
  85. protected:
  86. EventHandlerNonNull* GetEventHandler(nsIAtom* aType,
  87. const nsAString& aTypeString);
  88. void SetEventHandler(nsIAtom* aType, const nsAString& aTypeString,
  89. EventHandlerNonNull* aHandler);
  90. };
  91. NS_DEFINE_STATIC_IID_ACCESSOR(EventTarget, NS_EVENTTARGET_IID)
  92. } // namespace dom
  93. } // namespace mozilla
  94. #endif // mozilla_dom_EventTarget_h_