FetchObserver.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "FetchObserver.h"
  6. #include "WorkerPrivate.h"
  7. #include "mozilla/dom/Event.h"
  8. namespace mozilla {
  9. namespace dom {
  10. NS_IMPL_CYCLE_COLLECTION_CLASS(FetchObserver)
  11. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FetchObserver,
  12. DOMEventTargetHelper)
  13. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  14. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchObserver,
  15. DOMEventTargetHelper)
  16. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  17. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchObserver)
  18. NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
  19. NS_IMPL_ADDREF_INHERITED(FetchObserver, DOMEventTargetHelper)
  20. NS_IMPL_RELEASE_INHERITED(FetchObserver, DOMEventTargetHelper)
  21. /* static */ bool
  22. FetchObserver::IsEnabled(JSContext* aCx, JSObject* aGlobal)
  23. {
  24. if (NS_IsMainThread()) {
  25. return Preferences::GetBool("dom.fetchObserver.enabled", false);
  26. }
  27. using namespace workers;
  28. // Otherwise, check the pref via the WorkerPrivate
  29. WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
  30. if (!workerPrivate) {
  31. return false;
  32. }
  33. return workerPrivate->FetchObserverEnabled();
  34. }
  35. FetchObserver::FetchObserver(nsIGlobalObject* aGlobal,
  36. AbortSignal* aSignal)
  37. : DOMEventTargetHelper(aGlobal)
  38. , mState(FetchState::Requesting)
  39. {
  40. if (aSignal) {
  41. Follow(aSignal);
  42. }
  43. }
  44. JSObject*
  45. FetchObserver::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  46. {
  47. return FetchObserverBinding::Wrap(aCx, this, aGivenProto);
  48. }
  49. FetchState
  50. FetchObserver::State() const
  51. {
  52. return mState;
  53. }
  54. void
  55. FetchObserver::Aborted()
  56. {
  57. SetState(FetchState::Aborted);
  58. }
  59. void
  60. FetchObserver::SetState(FetchState aState)
  61. {
  62. MOZ_ASSERT(mState < aState);
  63. if (mState == FetchState::Aborted ||
  64. mState == FetchState::Errored ||
  65. mState == FetchState::Complete) {
  66. // We are already in a final state.
  67. return;
  68. }
  69. // We cannot pass from Requesting to Complete directly.
  70. if (mState == FetchState::Requesting &&
  71. aState == FetchState::Complete) {
  72. SetState(FetchState::Responding);
  73. }
  74. mState = aState;
  75. if (mState == FetchState::Aborted ||
  76. mState == FetchState::Errored ||
  77. mState == FetchState::Complete) {
  78. Unfollow();
  79. }
  80. EventInit init;
  81. init.mBubbles = false;
  82. init.mCancelable = false;
  83. // TODO which kind of event should we dispatch here?
  84. RefPtr<Event> event =
  85. Event::Constructor(this, NS_LITERAL_STRING("statechange"), init);
  86. event->SetTrusted(true);
  87. bool dummy;
  88. DispatchEvent(event, &dummy);
  89. }
  90. } // dom namespace
  91. } // mozilla namespace