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