Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "FetchObserver.h"
8 : #include "mozilla/dom/Event.h"
9 :
10 : namespace mozilla {
11 : namespace dom {
12 :
13 : NS_IMPL_CYCLE_COLLECTION_CLASS(FetchObserver)
14 :
15 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(FetchObserver,
16 : DOMEventTargetHelper)
17 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
18 :
19 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(FetchObserver,
20 : DOMEventTargetHelper)
21 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
22 :
23 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchObserver)
24 0 : NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
25 :
26 0 : NS_IMPL_ADDREF_INHERITED(FetchObserver, DOMEventTargetHelper)
27 0 : NS_IMPL_RELEASE_INHERITED(FetchObserver, DOMEventTargetHelper)
28 :
29 : /* static */ bool
30 1 : FetchObserver::IsEnabled(JSContext* aCx, JSObject* aGlobal)
31 : {
32 1 : if (NS_IsMainThread()) {
33 0 : return Preferences::GetBool("dom.fetchObserver.enabled", false);
34 : }
35 :
36 : using namespace workers;
37 :
38 : // Otherwise, check the pref via the WorkerPrivate
39 1 : WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
40 1 : if (!workerPrivate) {
41 0 : return false;
42 : }
43 :
44 1 : return workerPrivate->FetchObserverEnabled();
45 : }
46 :
47 0 : FetchObserver::FetchObserver(nsIGlobalObject* aGlobal,
48 0 : FetchSignal* aSignal)
49 : : DOMEventTargetHelper(aGlobal)
50 0 : , mState(FetchState::Requesting)
51 : {
52 0 : if (aSignal) {
53 0 : Follow(aSignal);
54 : }
55 0 : }
56 :
57 : JSObject*
58 0 : FetchObserver::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
59 : {
60 0 : return FetchObserverBinding::Wrap(aCx, this, aGivenProto);
61 : }
62 :
63 : FetchState
64 0 : FetchObserver::State() const
65 : {
66 0 : return mState;
67 : }
68 :
69 : void
70 0 : FetchObserver::Aborted()
71 : {
72 0 : SetState(FetchState::Aborted);
73 0 : }
74 :
75 : void
76 0 : FetchObserver::SetState(FetchState aState)
77 : {
78 0 : MOZ_ASSERT(mState < aState);
79 :
80 0 : if (mState == FetchState::Aborted ||
81 0 : mState == FetchState::Errored ||
82 0 : mState == FetchState::Complete) {
83 : // We are already in a final state.
84 0 : return;
85 : }
86 :
87 : // We cannot pass from Requesting to Complete directly.
88 0 : if (mState == FetchState::Requesting &&
89 : aState == FetchState::Complete) {
90 0 : SetState(FetchState::Responding);
91 : }
92 :
93 0 : mState = aState;
94 :
95 0 : if (mState == FetchState::Aborted ||
96 0 : mState == FetchState::Errored ||
97 0 : mState == FetchState::Complete) {
98 0 : Unfollow();
99 : }
100 :
101 0 : EventInit init;
102 0 : init.mBubbles = false;
103 0 : init.mCancelable = false;
104 :
105 : // TODO which kind of event should we dispatch here?
106 :
107 : RefPtr<Event> event =
108 0 : Event::Constructor(this, NS_LITERAL_STRING("statechange"), init);
109 0 : event->SetTrusted(true);
110 :
111 : bool dummy;
112 0 : DispatchEvent(event, &dummy);
113 : }
114 :
115 : } // dom namespace
116 : } // mozilla namespace
|