EventTarget.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "mozilla/EventListenerManager.h"
  6. #include "mozilla/dom/Event.h"
  7. #include "mozilla/dom/EventTarget.h"
  8. #include "mozilla/dom/EventTargetBinding.h"
  9. #include "mozilla/dom/ConstructibleEventTarget.h"
  10. #include "nsIGlobalObject.h"
  11. #include "nsThreadUtils.h"
  12. namespace mozilla {
  13. namespace dom {
  14. /* static */
  15. already_AddRefed<EventTarget>
  16. EventTarget::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
  17. {
  18. nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
  19. if (!global) {
  20. aRv.Throw(NS_ERROR_UNEXPECTED);
  21. return nullptr;
  22. }
  23. RefPtr<EventTarget> target = new ConstructibleEventTarget(global);
  24. return target.forget();
  25. }
  26. void
  27. EventTarget::RemoveEventListener(const nsAString& aType,
  28. EventListener* aListener,
  29. const EventListenerOptionsOrBoolean& aOptions,
  30. ErrorResult& aRv)
  31. {
  32. EventListenerManager* elm = GetExistingListenerManager();
  33. if (elm) {
  34. elm->RemoveEventListener(aType, aListener, aOptions);
  35. }
  36. }
  37. EventHandlerNonNull*
  38. EventTarget::GetEventHandler(nsIAtom* aType, const nsAString& aTypeString)
  39. {
  40. EventListenerManager* elm = GetExistingListenerManager();
  41. return elm ? elm->GetEventHandler(aType, aTypeString) : nullptr;
  42. }
  43. void
  44. EventTarget::SetEventHandler(const nsAString& aType,
  45. EventHandlerNonNull* aHandler,
  46. ErrorResult& aRv)
  47. {
  48. if (!StringBeginsWith(aType, NS_LITERAL_STRING("on"))) {
  49. aRv.Throw(NS_ERROR_INVALID_ARG);
  50. return;
  51. }
  52. if (NS_IsMainThread()) {
  53. nsCOMPtr<nsIAtom> type = NS_Atomize(aType);
  54. SetEventHandler(type, EmptyString(), aHandler);
  55. return;
  56. }
  57. SetEventHandler(nullptr,
  58. Substring(aType, 2), // Remove "on"
  59. aHandler);
  60. }
  61. void
  62. EventTarget::SetEventHandler(nsIAtom* aType, const nsAString& aTypeString,
  63. EventHandlerNonNull* aHandler)
  64. {
  65. GetOrCreateListenerManager()->SetEventHandler(aType, aTypeString, aHandler);
  66. }
  67. bool
  68. EventTarget::IsApzAware() const
  69. {
  70. EventListenerManager* elm = GetExistingListenerManager();
  71. return elm && elm->HasApzAwareListeners();
  72. }
  73. bool
  74. EventTarget::DispatchEvent(JSContext* aCx,
  75. Event& aEvent,
  76. ErrorResult& aRv)
  77. {
  78. bool result = false;
  79. aRv = DispatchEvent(&aEvent, &result);
  80. return !aEvent.DefaultPrevented(aCx);
  81. }
  82. } // namespace dom
  83. } // namespace mozilla