/src/mozilla-central/dom/workers/WorkerNavigator.cpp
Line | Count | Source (jump to first uncovered line) |
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 "mozilla/dom/BindingUtils.h" |
8 | | #include "mozilla/dom/MediaCapabilities.h" |
9 | | #include "mozilla/dom/Promise.h" |
10 | | #include "mozilla/dom/PromiseWorkerProxy.h" |
11 | | #include "mozilla/dom/StorageManager.h" |
12 | | #include "mozilla/dom/WorkerNavigator.h" |
13 | | #include "mozilla/dom/WorkerNavigatorBinding.h" |
14 | | #include "mozilla/dom/network/Connection.h" |
15 | | #include "mozilla/StaticPrefs.h" |
16 | | |
17 | | #include "nsProxyRelease.h" |
18 | | #include "nsRFPService.h" |
19 | | #include "RuntimeService.h" |
20 | | |
21 | | #include "nsIDocument.h" |
22 | | |
23 | | #include "WorkerPrivate.h" |
24 | | #include "WorkerRunnable.h" |
25 | | #include "WorkerScope.h" |
26 | | |
27 | | #include "mozilla/dom/Navigator.h" |
28 | | |
29 | | namespace mozilla { |
30 | | namespace dom { |
31 | | |
32 | | using namespace workerinternals; |
33 | | |
34 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(WorkerNavigator, mStorageManager, |
35 | | mConnection, mMediaCapabilities); |
36 | | |
37 | | NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WorkerNavigator, AddRef) |
38 | | NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WorkerNavigator, Release) |
39 | | |
40 | | WorkerNavigator::WorkerNavigator(const NavigatorProperties& aProperties, |
41 | | bool aOnline) |
42 | | : mProperties(aProperties) |
43 | | , mOnline(aOnline) |
44 | 0 | { |
45 | 0 | } |
46 | | |
47 | | WorkerNavigator::~WorkerNavigator() |
48 | 0 | { |
49 | 0 | } |
50 | | |
51 | | /* static */ already_AddRefed<WorkerNavigator> |
52 | | WorkerNavigator::Create(bool aOnLine) |
53 | 0 | { |
54 | 0 | RuntimeService* rts = RuntimeService::GetService(); |
55 | 0 | MOZ_ASSERT(rts); |
56 | 0 |
|
57 | 0 | const RuntimeService::NavigatorProperties& properties = |
58 | 0 | rts->GetNavigatorProperties(); |
59 | 0 |
|
60 | 0 | RefPtr<WorkerNavigator> navigator = |
61 | 0 | new WorkerNavigator(properties, aOnLine); |
62 | 0 |
|
63 | 0 | return navigator.forget(); |
64 | 0 | } |
65 | | |
66 | | JSObject* |
67 | | WorkerNavigator::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
68 | 0 | { |
69 | 0 | return WorkerNavigator_Binding::Wrap(aCx, this, aGivenProto); |
70 | 0 | } |
71 | | |
72 | | void |
73 | | WorkerNavigator::SetLanguages(const nsTArray<nsString>& aLanguages) |
74 | 0 | { |
75 | 0 | WorkerNavigator_Binding::ClearCachedLanguagesValue(this); |
76 | 0 | mProperties.mLanguages = aLanguages; |
77 | 0 | } |
78 | | |
79 | | void |
80 | | WorkerNavigator::GetAppName(nsString& aAppName, CallerType aCallerType) const |
81 | 0 | { |
82 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
83 | 0 | MOZ_ASSERT(workerPrivate); |
84 | 0 |
|
85 | 0 | if ((!mProperties.mAppNameOverridden.IsEmpty() || |
86 | 0 | StaticPrefs::privacy_resistFingerprinting()) && |
87 | 0 | !workerPrivate->UsesSystemPrincipal()) { |
88 | 0 | // We will spoof this value when 'privacy.resistFingerprinting' is true. |
89 | 0 | // See nsRFPService.h for spoofed value. |
90 | 0 | aAppName = StaticPrefs::privacy_resistFingerprinting() ? |
91 | 0 | NS_LITERAL_STRING(SPOOFED_APPNAME) : mProperties.mAppNameOverridden; |
92 | 0 | } else { |
93 | 0 | aAppName = mProperties.mAppName; |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | void |
98 | | WorkerNavigator::GetAppVersion(nsString& aAppVersion, CallerType aCallerType, |
99 | | ErrorResult& aRv) const |
100 | 0 | { |
101 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
102 | 0 | MOZ_ASSERT(workerPrivate); |
103 | 0 |
|
104 | 0 | if ((!mProperties.mAppVersionOverridden.IsEmpty() || |
105 | 0 | StaticPrefs::privacy_resistFingerprinting()) && |
106 | 0 | !workerPrivate->UsesSystemPrincipal()) { |
107 | 0 | // We will spoof this value when 'privacy.resistFingerprinting' is true. |
108 | 0 | // See nsRFPService.h for spoofed value. |
109 | 0 | aAppVersion = StaticPrefs::privacy_resistFingerprinting() ? |
110 | 0 | NS_LITERAL_STRING(SPOOFED_APPVERSION) : mProperties.mAppVersionOverridden; |
111 | 0 | } else { |
112 | 0 | aAppVersion = mProperties.mAppVersion; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | void |
117 | | WorkerNavigator::GetPlatform(nsString& aPlatform, CallerType aCallerType, |
118 | | ErrorResult& aRv) const |
119 | 0 | { |
120 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
121 | 0 | MOZ_ASSERT(workerPrivate); |
122 | 0 |
|
123 | 0 | if ((!mProperties.mPlatformOverridden.IsEmpty() || |
124 | 0 | StaticPrefs::privacy_resistFingerprinting()) && |
125 | 0 | !workerPrivate->UsesSystemPrincipal()) { |
126 | 0 | // We will spoof this value when 'privacy.resistFingerprinting' is true. |
127 | 0 | // See nsRFPService.h for spoofed value. |
128 | 0 | aPlatform = StaticPrefs::privacy_resistFingerprinting() ? |
129 | 0 | NS_LITERAL_STRING(SPOOFED_PLATFORM) : mProperties.mPlatformOverridden; |
130 | 0 | } else { |
131 | 0 | aPlatform = mProperties.mPlatform; |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | namespace { |
136 | | |
137 | | class GetUserAgentRunnable final : public WorkerMainThreadRunnable |
138 | | { |
139 | | nsString& mUA; |
140 | | |
141 | | public: |
142 | | GetUserAgentRunnable(WorkerPrivate* aWorkerPrivate, nsString& aUA) |
143 | | : WorkerMainThreadRunnable(aWorkerPrivate, |
144 | | NS_LITERAL_CSTRING("UserAgent getter")) |
145 | | , mUA(aUA) |
146 | 0 | { |
147 | 0 | MOZ_ASSERT(aWorkerPrivate); |
148 | 0 | aWorkerPrivate->AssertIsOnWorkerThread(); |
149 | 0 | } |
150 | | |
151 | | virtual bool MainThreadRun() override |
152 | 0 | { |
153 | 0 | AssertIsOnMainThread(); |
154 | 0 |
|
155 | 0 | nsCOMPtr<nsPIDOMWindowInner> window = mWorkerPrivate->GetWindow(); |
156 | 0 |
|
157 | 0 | bool isCallerChrome = mWorkerPrivate->UsesSystemPrincipal(); |
158 | 0 | nsresult rv = dom::Navigator::GetUserAgent(window, isCallerChrome, mUA); |
159 | 0 | if (NS_FAILED(rv)) { |
160 | 0 | NS_WARNING("Failed to retrieve user-agent from the worker thread."); |
161 | 0 | } |
162 | 0 |
|
163 | 0 | return true; |
164 | 0 | } |
165 | | }; |
166 | | |
167 | | } // namespace |
168 | | |
169 | | void |
170 | | WorkerNavigator::GetUserAgent(nsString& aUserAgent, CallerType aCallerType, |
171 | | ErrorResult& aRv) const |
172 | 0 | { |
173 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
174 | 0 | MOZ_ASSERT(workerPrivate); |
175 | 0 |
|
176 | 0 | RefPtr<GetUserAgentRunnable> runnable = |
177 | 0 | new GetUserAgentRunnable(workerPrivate, aUserAgent); |
178 | 0 |
|
179 | 0 | runnable->Dispatch(Canceling, aRv); |
180 | 0 | } |
181 | | |
182 | | uint64_t |
183 | | WorkerNavigator::HardwareConcurrency() const |
184 | 0 | { |
185 | 0 | RuntimeService* rts = RuntimeService::GetService(); |
186 | 0 | MOZ_ASSERT(rts); |
187 | 0 |
|
188 | 0 | return rts->ClampedHardwareConcurrency(); |
189 | 0 | } |
190 | | |
191 | | StorageManager* |
192 | | WorkerNavigator::Storage() |
193 | 0 | { |
194 | 0 | if (!mStorageManager) { |
195 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
196 | 0 | MOZ_ASSERT(workerPrivate); |
197 | 0 |
|
198 | 0 | RefPtr<nsIGlobalObject> global = workerPrivate->GlobalScope(); |
199 | 0 | MOZ_ASSERT(global); |
200 | 0 |
|
201 | 0 | mStorageManager = new StorageManager(global); |
202 | 0 | } |
203 | 0 |
|
204 | 0 | return mStorageManager; |
205 | 0 | } |
206 | | |
207 | | network::Connection* |
208 | | WorkerNavigator::GetConnection(ErrorResult& aRv) |
209 | 0 | { |
210 | 0 | if (!mConnection) { |
211 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
212 | 0 | MOZ_ASSERT(workerPrivate); |
213 | 0 |
|
214 | 0 | mConnection = network::Connection::CreateForWorker(workerPrivate, aRv); |
215 | 0 | } |
216 | 0 |
|
217 | 0 | return mConnection; |
218 | 0 | } |
219 | | |
220 | | dom::MediaCapabilities* |
221 | | WorkerNavigator::MediaCapabilities() |
222 | 0 | { |
223 | 0 | if (!mMediaCapabilities) { |
224 | 0 | WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); |
225 | 0 | MOZ_ASSERT(workerPrivate); |
226 | 0 |
|
227 | 0 | nsIGlobalObject* global = workerPrivate->GlobalScope(); |
228 | 0 | MOZ_ASSERT(global); |
229 | 0 |
|
230 | 0 | mMediaCapabilities = new dom::MediaCapabilities(global); |
231 | 0 | } |
232 | 0 | return mMediaCapabilities; |
233 | 0 | } |
234 | | |
235 | | } // namespace dom |
236 | | } // namespace mozilla |