AbortSignal.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "AbortSignal.h"
  6. #include "AbortController.h"
  7. #include "mozilla/dom/Event.h"
  8. #include "mozilla/dom/AbortSignalBinding.h"
  9. namespace mozilla {
  10. namespace dom {
  11. NS_IMPL_CYCLE_COLLECTION_CLASS(AbortSignal)
  12. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AbortSignal,
  13. DOMEventTargetHelper)
  14. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mController)
  15. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  16. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AbortSignal,
  17. DOMEventTargetHelper)
  18. NS_IMPL_CYCLE_COLLECTION_UNLINK(mController)
  19. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  20. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AbortSignal)
  21. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  22. NS_IMPL_ADDREF_INHERITED(AbortSignal, DOMEventTargetHelper)
  23. NS_IMPL_RELEASE_INHERITED(AbortSignal, DOMEventTargetHelper)
  24. AbortSignal::AbortSignal(AbortController* aController,
  25. bool aAborted)
  26. : DOMEventTargetHelper(aController->GetParentObject())
  27. , mController(aController)
  28. , mAborted(aAborted)
  29. {}
  30. AbortSignal::AbortSignal(bool aAborted)
  31. : mAborted(aAborted)
  32. {}
  33. JSObject*
  34. AbortSignal::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  35. {
  36. return AbortSignalBinding::Wrap(aCx, this, aGivenProto);
  37. }
  38. bool
  39. AbortSignal::Aborted() const
  40. {
  41. return mAborted;
  42. }
  43. void
  44. AbortSignal::Abort()
  45. {
  46. // Re-entrancy guard
  47. if (mAborted) {
  48. return;
  49. }
  50. mAborted = true;
  51. // We might be deleted as a result of aborting a follower, so ensure we
  52. // stay alive until all followers have been aborted.
  53. RefPtr<AbortSignal> pinThis = this;
  54. // Let's inform the followers.
  55. for (uint32_t i = 0; i < mFollowers.Length(); ++i) {
  56. mFollowers[i]->Aborted();
  57. }
  58. EventInit init;
  59. init.mBubbles = false;
  60. init.mCancelable = false;
  61. RefPtr<Event> event =
  62. Event::Constructor(this, NS_LITERAL_STRING("abort"), init);
  63. event->SetTrusted(true);
  64. bool dummy;
  65. DispatchEvent(event, &dummy);
  66. }
  67. void
  68. AbortSignal::AddFollower(AbortSignal::Follower* aFollower)
  69. {
  70. MOZ_DIAGNOSTIC_ASSERT(aFollower);
  71. if (!mFollowers.Contains(aFollower)) {
  72. mFollowers.AppendElement(aFollower);
  73. }
  74. }
  75. void
  76. AbortSignal::RemoveFollower(AbortSignal::Follower* aFollower)
  77. {
  78. MOZ_DIAGNOSTIC_ASSERT(aFollower);
  79. mFollowers.RemoveElement(aFollower);
  80. }
  81. // AbortSignal::Follower
  82. // ----------------------------------------------------------------------------
  83. AbortSignal::Follower::~Follower()
  84. {
  85. Unfollow();
  86. }
  87. void
  88. AbortSignal::Follower::Follow(AbortSignal* aSignal)
  89. {
  90. MOZ_DIAGNOSTIC_ASSERT(aSignal);
  91. Unfollow();
  92. mFollowingSignal = aSignal;
  93. aSignal->AddFollower(this);
  94. }
  95. void
  96. AbortSignal::Follower::Unfollow()
  97. {
  98. if (mFollowingSignal) {
  99. mFollowingSignal->RemoveFollower(this);
  100. mFollowingSignal = nullptr;
  101. }
  102. }
  103. } // dom namespace
  104. } // mozilla namespace