AbortSignal.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_AbortSignal_h
  6. #define mozilla_dom_AbortSignal_h
  7. #include "mozilla/DOMEventTargetHelper.h"
  8. namespace mozilla {
  9. namespace dom {
  10. class AbortController;
  11. class AbortSignal;
  12. class AbortSignal final : public DOMEventTargetHelper
  13. {
  14. public:
  15. // This class must be implemented by objects who want to follow a AbortSignal.
  16. class Follower
  17. {
  18. public:
  19. virtual void Aborted() = 0;
  20. protected:
  21. virtual ~Follower();
  22. void
  23. Follow(AbortSignal* aSignal);
  24. void
  25. Unfollow();
  26. RefPtr<AbortSignal> mFollowingSignal;
  27. };
  28. NS_DECL_ISUPPORTS_INHERITED
  29. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AbortSignal, DOMEventTargetHelper)
  30. AbortSignal(AbortController* aController, bool aAborted);
  31. explicit AbortSignal(bool aAborted);
  32. JSObject*
  33. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  34. bool
  35. Aborted() const;
  36. void
  37. Abort();
  38. IMPL_EVENT_HANDLER(abort);
  39. void
  40. AddFollower(Follower* aFollower);
  41. void
  42. RemoveFollower(Follower* aFollower);
  43. private:
  44. ~AbortSignal() = default;
  45. RefPtr<AbortController> mController;
  46. // Raw pointers. Follower unregisters itself in the DTOR.
  47. nsTArray<Follower*> mFollowers;
  48. bool mAborted;
  49. };
  50. } // dom namespace
  51. } // mozilla namespace
  52. #endif // mozilla_dom_AbortSignal_h