1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/WorkerNavigator.h"
#include <utility>
#include "ErrorList.h"
#include "MainThreadUtils.h"
#include "RuntimeService.h"
#include "WorkerRunnable.h"
#include "WorkerScope.h"
#include "mozilla/dom/LockManager.h"
#include "mozilla/dom/MediaCapabilities.h"
#include "mozilla/dom/Navigator.h"
#include "mozilla/dom/StorageManager.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerNavigatorBinding.h"
#include "mozilla/dom/WorkerStatus.h"
#include "mozilla/dom/network/Connection.h"
#include "mozilla/webgpu/Instance.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsIGlobalObject.h"
#include "nsLiteralString.h"
#include "nsPIDOMWindow.h"
#include "nsRFPService.h"
#include "nsString.h"
class JSObject;
struct JSContext;
namespace mozilla::dom {
using namespace workerinternals;
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(WorkerNavigator)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(WorkerNavigator)
tmp->Invalidate();
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(WorkerNavigator)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStorageManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConnection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMediaCapabilities)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWebGpu)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLocks)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
WorkerNavigator::WorkerNavigator(const NavigatorProperties& aProperties,
bool aOnline)
: mProperties(aProperties), mOnline(aOnline) {}
WorkerNavigator::~WorkerNavigator() { Invalidate(); }
/* static */
already_AddRefed<WorkerNavigator> WorkerNavigator::Create(bool aOnLine) {
RuntimeService* rts = RuntimeService::GetService();
MOZ_ASSERT(rts);
const RuntimeService::NavigatorProperties& properties =
rts->GetNavigatorProperties();
RefPtr<WorkerNavigator> navigator = new WorkerNavigator(properties, aOnLine);
return navigator.forget();
}
void WorkerNavigator::Invalidate() {
if (mStorageManager) {
mStorageManager->Shutdown();
mStorageManager = nullptr;
}
mConnection = nullptr;
mMediaCapabilities = nullptr;
mWebGpu = nullptr;
mLocks = nullptr;
}
JSObject* WorkerNavigator::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return WorkerNavigator_Binding::Wrap(aCx, this, aGivenProto);
}
bool WorkerNavigator::GlobalPrivacyControl() const {
bool gpcStatus = StaticPrefs::privacy_globalprivacycontrol_enabled();
if (!gpcStatus) {
JSObject* jso = GetWrapper();
if (const nsCOMPtr<nsIGlobalObject> global = xpc::NativeGlobal(jso)) {
if (const nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull()) {
gpcStatus = principal->GetPrivateBrowsingId() > 0 &&
StaticPrefs::privacy_globalprivacycontrol_pbmode_enabled();
}
}
}
return StaticPrefs::privacy_globalprivacycontrol_functionality_enabled() &&
gpcStatus;
}
void WorkerNavigator::SetLanguages(const nsTArray<nsString>& aLanguages) {
WorkerNavigator_Binding::ClearCachedLanguagesValue(this);
mProperties.mLanguages = aLanguages.Clone();
}
void WorkerNavigator::GetAppVersion(nsString& aAppVersion,
CallerType aCallerType,
ErrorResult& aRv) const {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
if (aCallerType != CallerType::System) {
if (workerPrivate->ShouldResistFingerprinting(
RFPTarget::NavigatorAppVersion)) {
// See nsRFPService.h for spoofed value.
aAppVersion.AssignLiteral(SPOOFED_APPVERSION);
return;
}
if (!mProperties.mAppVersionOverridden.IsEmpty()) {
aAppVersion = mProperties.mAppVersionOverridden;
return;
}
}
aAppVersion = mProperties.mAppVersion;
}
void WorkerNavigator::GetPlatform(nsString& aPlatform, CallerType aCallerType,
ErrorResult& aRv) const {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
if (aCallerType != CallerType::System) {
if (workerPrivate->ShouldResistFingerprinting(
RFPTarget::NavigatorPlatform)) {
// See nsRFPService.h for spoofed value.
aPlatform.AssignLiteral(SPOOFED_PLATFORM);
return;
}
if (!mProperties.mPlatformOverridden.IsEmpty()) {
aPlatform = mProperties.mPlatformOverridden;
return;
}
}
aPlatform = mProperties.mPlatform;
}
namespace {
/*
* This Worker Runnable needs to check RFP; but our standard way of doing so
* relies on accessing GlobalScope() - which can only be accessed on the worker
* thread. So we need to pass it in.
*/
class GetUserAgentRunnable final : public WorkerMainThreadRunnable {
nsString& mUA;
bool mShouldResistFingerprinting;
public:
GetUserAgentRunnable(WorkerPrivate* aWorkerPrivate, nsString& aUA,
bool aShouldResistFingerprinting)
: WorkerMainThreadRunnable(aWorkerPrivate, "UserAgent getter"_ns),
mUA(aUA),
mShouldResistFingerprinting(aShouldResistFingerprinting) {
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
}
virtual bool MainThreadRun() override {
AssertIsOnMainThread();
nsCOMPtr<nsPIDOMWindowInner> window = mWorkerPrivate->GetWindow();
nsresult rv =
dom::Navigator::GetUserAgent(window, mWorkerPrivate->GetDocument(),
Some(mShouldResistFingerprinting), mUA);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to retrieve user-agent from the worker thread.");
}
return true;
}
};
} // namespace
void WorkerNavigator::GetUserAgent(nsString& aUserAgent, CallerType aCallerType,
ErrorResult& aRv) const {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
RefPtr<GetUserAgentRunnable> runnable = new GetUserAgentRunnable(
workerPrivate, aUserAgent,
workerPrivate->ShouldResistFingerprinting(RFPTarget::NavigatorUserAgent));
runnable->Dispatch(Canceling, aRv);
}
uint64_t WorkerNavigator::HardwareConcurrency() const {
RuntimeService* rts = RuntimeService::GetService();
MOZ_ASSERT(rts);
WorkerPrivate* aWorkerPrivate = GetCurrentThreadWorkerPrivate();
bool rfp = aWorkerPrivate->ShouldResistFingerprinting(
RFPTarget::NavigatorHWConcurrency);
return rts->ClampedHardwareConcurrency(rfp);
}
StorageManager* WorkerNavigator::Storage() {
if (!mStorageManager) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
RefPtr<nsIGlobalObject> global = workerPrivate->GlobalScope();
MOZ_ASSERT(global);
mStorageManager = new StorageManager(global);
}
return mStorageManager;
}
network::Connection* WorkerNavigator::GetConnection(ErrorResult& aRv) {
if (!mConnection) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
mConnection = network::Connection::CreateForWorker(workerPrivate, aRv);
}
return mConnection;
}
dom::MediaCapabilities* WorkerNavigator::MediaCapabilities() {
if (!mMediaCapabilities) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
nsIGlobalObject* global = workerPrivate->GlobalScope();
MOZ_ASSERT(global);
mMediaCapabilities = new dom::MediaCapabilities(global);
}
return mMediaCapabilities;
}
webgpu::Instance* WorkerNavigator::Gpu() {
if (!mWebGpu) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
nsIGlobalObject* global = workerPrivate->GlobalScope();
MOZ_ASSERT(global);
mWebGpu = webgpu::Instance::Create(global);
}
return mWebGpu;
}
dom::LockManager* WorkerNavigator::Locks() {
if (!mLocks) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
nsIGlobalObject* global = workerPrivate->GlobalScope();
MOZ_ASSERT(global);
mLocks = dom::LockManager::Create(*global);
}
return mLocks;
}
} // namespace mozilla::dom
|