AbortController.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "AbortController.h"
  6. #include "AbortSignal.h"
  7. #include "mozilla/dom/AbortControllerBinding.h"
  8. #include "WorkerPrivate.h"
  9. namespace mozilla {
  10. namespace dom {
  11. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AbortController, mGlobal, mSignal)
  12. NS_IMPL_CYCLE_COLLECTING_ADDREF(AbortController)
  13. NS_IMPL_CYCLE_COLLECTING_RELEASE(AbortController)
  14. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbortController)
  15. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  16. NS_INTERFACE_MAP_ENTRY(nsISupports)
  17. NS_INTERFACE_MAP_END
  18. /* static */ bool
  19. AbortController::IsEnabled(JSContext* aCx, JSObject* aGlobal)
  20. {
  21. if (NS_IsMainThread()) {
  22. return Preferences::GetBool("dom.abortController.enabled", false);
  23. }
  24. using namespace workers;
  25. // Otherwise, check the pref via the WorkerPrivate
  26. WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
  27. if (!workerPrivate) {
  28. return false;
  29. }
  30. return workerPrivate->AbortControllerEnabled();
  31. }
  32. /* static */ already_AddRefed<AbortController>
  33. AbortController::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
  34. {
  35. nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
  36. if (!global) {
  37. aRv.Throw(NS_ERROR_FAILURE);
  38. return nullptr;
  39. }
  40. RefPtr<AbortController> abortController = new AbortController(global);
  41. return abortController.forget();
  42. }
  43. AbortController::AbortController(nsIGlobalObject* aGlobal)
  44. : mGlobal(aGlobal)
  45. , mAborted(false)
  46. {}
  47. JSObject*
  48. AbortController::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  49. {
  50. return AbortControllerBinding::Wrap(aCx, this, aGivenProto);
  51. }
  52. nsIGlobalObject*
  53. AbortController::GetParentObject() const
  54. {
  55. return mGlobal;
  56. }
  57. AbortSignal*
  58. AbortController::Signal()
  59. {
  60. if (!mSignal) {
  61. mSignal = new AbortSignal(this, mAborted);
  62. }
  63. return mSignal;
  64. }
  65. void
  66. AbortController::Abort()
  67. {
  68. if (mAborted) {
  69. return;
  70. }
  71. mAborted = true;
  72. if (mSignal) {
  73. mSignal->Abort();
  74. }
  75. }
  76. } // dom namespace
  77. } // mozilla namespace