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 "FetchController.h"
8 : #include "FetchSignal.h"
9 : #include "mozilla/dom/FetchControllerBinding.h"
10 :
11 : namespace mozilla {
12 : namespace dom {
13 :
14 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FetchController, mGlobal, mSignal,
15 : mFollowingSignal)
16 :
17 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(FetchController)
18 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(FetchController)
19 :
20 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FetchController)
21 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
22 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
23 0 : NS_INTERFACE_MAP_END
24 :
25 : /* static */ bool
26 2 : FetchController::IsEnabled(JSContext* aCx, JSObject* aGlobal)
27 : {
28 2 : if (NS_IsMainThread()) {
29 0 : return Preferences::GetBool("dom.fetchController.enabled", false);
30 : }
31 :
32 : using namespace workers;
33 :
34 : // Otherwise, check the pref via the WorkerPrivate
35 2 : WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
36 2 : if (!workerPrivate) {
37 0 : return false;
38 : }
39 :
40 2 : return workerPrivate->FetchControllerEnabled();
41 : }
42 :
43 : /* static */ already_AddRefed<FetchController>
44 0 : FetchController::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
45 : {
46 0 : nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
47 0 : if (!global) {
48 0 : aRv.Throw(NS_ERROR_FAILURE);
49 0 : return nullptr;
50 : }
51 :
52 0 : RefPtr<FetchController> fetchController = new FetchController(global);
53 0 : return fetchController.forget();
54 : }
55 :
56 0 : FetchController::FetchController(nsIGlobalObject* aGlobal)
57 : : mGlobal(aGlobal)
58 0 : , mAborted(false)
59 0 : {}
60 :
61 : JSObject*
62 0 : FetchController::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
63 : {
64 0 : return FetchControllerBinding::Wrap(aCx, this, aGivenProto);
65 : }
66 :
67 : nsIGlobalObject*
68 0 : FetchController::GetParentObject() const
69 : {
70 0 : return mGlobal;
71 : }
72 :
73 : FetchSignal*
74 0 : FetchController::Signal()
75 : {
76 0 : if (!mSignal) {
77 0 : mSignal = new FetchSignal(this, mAborted);
78 : }
79 :
80 0 : return mSignal;
81 : }
82 :
83 : void
84 0 : FetchController::Abort()
85 : {
86 0 : if (mAborted) {
87 0 : return;
88 : }
89 :
90 0 : mAborted = true;
91 :
92 0 : if (mSignal) {
93 0 : mSignal->Abort();
94 : }
95 : }
96 :
97 : void
98 0 : FetchController::Follow(FetchSignal& aSignal)
99 : {
100 0 : FetchSignal::Follower::Follow(&aSignal);
101 0 : }
102 :
103 : void
104 0 : FetchController::Unfollow(FetchSignal& aSignal)
105 : {
106 0 : if (mFollowingSignal != &aSignal) {
107 0 : return;
108 : }
109 :
110 0 : FetchSignal::Follower::Unfollow();
111 : }
112 :
113 : FetchSignal*
114 0 : FetchController::Following() const
115 : {
116 0 : return mFollowingSignal;
117 : }
118 :
119 : void
120 0 : FetchController::Aborted()
121 : {
122 0 : Abort();
123 0 : }
124 :
125 : } // dom namespace
126 : } // mozilla namespace
|