/src/mozilla-central/dom/workers/WorkerScope.h
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 | | #ifndef mozilla_dom_workerscope_h__ |
8 | | #define mozilla_dom_workerscope_h__ |
9 | | |
10 | | #include "mozilla/dom/WorkerCommon.h" |
11 | | #include "mozilla/DOMEventTargetHelper.h" |
12 | | #include "mozilla/dom/DOMPrefs.h" |
13 | | #include "mozilla/dom/Headers.h" |
14 | | #include "mozilla/dom/RequestBinding.h" |
15 | | #include "nsWeakReference.h" |
16 | | #include "mozilla/dom/ImageBitmapSource.h" |
17 | | |
18 | | #ifdef XP_WIN |
19 | | #undef PostMessage |
20 | | #endif |
21 | | |
22 | | namespace mozilla { |
23 | | namespace dom { |
24 | | |
25 | | class AnyCallback; |
26 | | struct ChannelPixelLayout; |
27 | | class ClientInfo; |
28 | | class Clients; |
29 | | class ClientState; |
30 | | class Console; |
31 | | class Crypto; |
32 | | class Function; |
33 | | class IDBFactory; |
34 | | enum class ImageBitmapFormat : uint8_t; |
35 | | class Performance; |
36 | | class Promise; |
37 | | class RequestOrUSVString; |
38 | | class WorkerLocation; |
39 | | class WorkerNavigator; |
40 | | class WorkerPrivate; |
41 | | enum class CallerType : uint32_t; |
42 | | |
43 | | namespace cache { |
44 | | |
45 | | class CacheStorage; |
46 | | |
47 | | } // namespace cache |
48 | | |
49 | | class WorkerGlobalScope : public DOMEventTargetHelper, |
50 | | public nsIGlobalObject, |
51 | | public nsSupportsWeakReference |
52 | | { |
53 | | typedef mozilla::dom::IDBFactory IDBFactory; |
54 | | |
55 | | RefPtr<Console> mConsole; |
56 | | RefPtr<Crypto> mCrypto; |
57 | | RefPtr<WorkerLocation> mLocation; |
58 | | RefPtr<WorkerNavigator> mNavigator; |
59 | | RefPtr<Performance> mPerformance; |
60 | | RefPtr<IDBFactory> mIndexedDB; |
61 | | RefPtr<cache::CacheStorage> mCacheStorage; |
62 | | nsCOMPtr<nsISerialEventTarget> mSerialEventTarget; |
63 | | |
64 | | uint32_t mWindowInteractionsAllowed; |
65 | | |
66 | | protected: |
67 | | WorkerPrivate* mWorkerPrivate; |
68 | | |
69 | | explicit WorkerGlobalScope(WorkerPrivate* aWorkerPrivate); |
70 | | virtual ~WorkerGlobalScope(); |
71 | | |
72 | | public: |
73 | | virtual JSObject* |
74 | | WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
75 | | |
76 | | virtual bool |
77 | | WrapGlobalObject(JSContext* aCx, JS::MutableHandle<JSObject*> aReflector) = 0; |
78 | | |
79 | | virtual JSObject* |
80 | | GetGlobalJSObject(void) override |
81 | 0 | { |
82 | 0 | return GetWrapper(); |
83 | 0 | } |
84 | | |
85 | | NS_DECL_ISUPPORTS_INHERITED |
86 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerGlobalScope, |
87 | | DOMEventTargetHelper) |
88 | | |
89 | | WorkerGlobalScope* |
90 | | Self() |
91 | | { |
92 | | return this; |
93 | | } |
94 | | |
95 | | void |
96 | | NoteTerminating(); |
97 | | |
98 | | already_AddRefed<Console> |
99 | | GetConsole(ErrorResult& aRv); |
100 | | |
101 | | Console* |
102 | | GetConsoleIfExists() const |
103 | 0 | { |
104 | 0 | return mConsole; |
105 | 0 | } |
106 | | |
107 | | Crypto* |
108 | | GetCrypto(ErrorResult& aError); |
109 | | |
110 | | already_AddRefed<WorkerLocation> |
111 | | Location(); |
112 | | |
113 | | already_AddRefed<WorkerNavigator> |
114 | | Navigator(); |
115 | | |
116 | | already_AddRefed<WorkerNavigator> |
117 | | GetExistingNavigator() const; |
118 | | |
119 | | OnErrorEventHandlerNonNull* |
120 | | GetOnerror(); |
121 | | void |
122 | | SetOnerror(OnErrorEventHandlerNonNull* aHandler); |
123 | | |
124 | | void |
125 | | ImportScripts(const Sequence<nsString>& aScriptURLs, ErrorResult& aRv); |
126 | | |
127 | | int32_t |
128 | | SetTimeout(JSContext* aCx, Function& aHandler, const int32_t aTimeout, |
129 | | const Sequence<JS::Value>& aArguments, ErrorResult& aRv); |
130 | | int32_t |
131 | | SetTimeout(JSContext* aCx, const nsAString& aHandler, const int32_t aTimeout, |
132 | | const Sequence<JS::Value>& /* unused */, ErrorResult& aRv); |
133 | | void |
134 | | ClearTimeout(int32_t aHandle); |
135 | | int32_t |
136 | | SetInterval(JSContext* aCx, Function& aHandler, |
137 | | const int32_t aTimeout, |
138 | | const Sequence<JS::Value>& aArguments, ErrorResult& aRv); |
139 | | int32_t |
140 | | SetInterval(JSContext* aCx, const nsAString& aHandler, |
141 | | const int32_t aTimeout, |
142 | | const Sequence<JS::Value>& /* unused */, ErrorResult& aRv); |
143 | | void |
144 | | ClearInterval(int32_t aHandle); |
145 | | |
146 | | void |
147 | | GetOrigin(nsAString& aOrigin) const; |
148 | | |
149 | | void |
150 | | Atob(const nsAString& aAtob, nsAString& aOutput, ErrorResult& aRv) const; |
151 | | void |
152 | | Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv) const; |
153 | | |
154 | | IMPL_EVENT_HANDLER(online) |
155 | | IMPL_EVENT_HANDLER(offline) |
156 | | |
157 | | void |
158 | | Dump(const Optional<nsAString>& aString) const; |
159 | | |
160 | | Performance* GetPerformance(); |
161 | | |
162 | | Performance* GetPerformanceIfExists() const |
163 | 0 | { |
164 | 0 | return mPerformance; |
165 | 0 | } |
166 | | |
167 | | static bool IsInAutomation(JSContext* aCx, JSObject* /* unused */); |
168 | | void GetJSTestingFunctions(JSContext* aCx, |
169 | | JS::MutableHandle<JSObject*> aFunctions, |
170 | | ErrorResult& aRv); |
171 | | |
172 | | already_AddRefed<Promise> |
173 | | Fetch(const RequestOrUSVString& aInput, const RequestInit& aInit, |
174 | | CallerType aCallerType, ErrorResult& aRv); |
175 | | |
176 | | already_AddRefed<IDBFactory> |
177 | | GetIndexedDB(ErrorResult& aErrorResult); |
178 | | |
179 | | already_AddRefed<cache::CacheStorage> |
180 | | GetCaches(ErrorResult& aRv); |
181 | | |
182 | | bool IsSecureContext() const; |
183 | | |
184 | | already_AddRefed<Promise> |
185 | | CreateImageBitmap(JSContext* aCx, |
186 | | const ImageBitmapSource& aImage, ErrorResult& aRv); |
187 | | |
188 | | already_AddRefed<Promise> |
189 | | CreateImageBitmap(JSContext* aCx, |
190 | | const ImageBitmapSource& aImage, |
191 | | int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh, |
192 | | ErrorResult& aRv); |
193 | | |
194 | | already_AddRefed<mozilla::dom::Promise> |
195 | | CreateImageBitmap(JSContext* aCx, |
196 | | const ImageBitmapSource& aImage, |
197 | | int32_t aOffset, int32_t aLength, |
198 | | mozilla::dom::ImageBitmapFormat aFormat, |
199 | | const mozilla::dom::Sequence<mozilla::dom::ChannelPixelLayout>& aLayout, |
200 | | mozilla::ErrorResult& aRv); |
201 | | |
202 | | bool |
203 | | WindowInteractionAllowed() const |
204 | | { |
205 | | return mWindowInteractionsAllowed > 0; |
206 | | } |
207 | | |
208 | | void |
209 | | AllowWindowInteraction() |
210 | | { |
211 | | mWindowInteractionsAllowed++; |
212 | | } |
213 | | |
214 | | void |
215 | | ConsumeWindowInteraction() |
216 | | { |
217 | | MOZ_ASSERT(mWindowInteractionsAllowed > 0); |
218 | | mWindowInteractionsAllowed--; |
219 | | } |
220 | | |
221 | | // Override DispatchTrait API to target the worker thread. Dispatch may |
222 | | // return failure if the worker thread is not alive. |
223 | | nsresult |
224 | | Dispatch(TaskCategory aCategory, |
225 | | already_AddRefed<nsIRunnable>&& aRunnable) override; |
226 | | |
227 | | nsISerialEventTarget* |
228 | | EventTargetFor(TaskCategory aCategory) const override; |
229 | | |
230 | | AbstractThread* |
231 | | AbstractMainThreadFor(TaskCategory aCategory) override; |
232 | | |
233 | | Maybe<ClientInfo> |
234 | | GetClientInfo() const override; |
235 | | |
236 | | Maybe<ClientState> |
237 | | GetClientState() const; |
238 | | |
239 | | Maybe<ServiceWorkerDescriptor> |
240 | | GetController() const override; |
241 | | |
242 | | RefPtr<mozilla::dom::ServiceWorkerRegistration> |
243 | | GetServiceWorkerRegistration(const ServiceWorkerRegistrationDescriptor& aDescriptor) const override; |
244 | | |
245 | | RefPtr<mozilla::dom::ServiceWorkerRegistration> |
246 | | GetOrCreateServiceWorkerRegistration(const ServiceWorkerRegistrationDescriptor& aDescriptor) override; |
247 | | }; |
248 | | |
249 | | class DedicatedWorkerGlobalScope final : public WorkerGlobalScope |
250 | | { |
251 | | const nsString mName; |
252 | | |
253 | 0 | ~DedicatedWorkerGlobalScope() { } |
254 | | |
255 | | public: |
256 | | DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, |
257 | | const nsString& aName); |
258 | | |
259 | | virtual bool |
260 | | WrapGlobalObject(JSContext* aCx, |
261 | | JS::MutableHandle<JSObject*> aReflector) override; |
262 | | |
263 | | void GetName(DOMString& aName) const |
264 | | { |
265 | | aName.AsAString() = mName; |
266 | | } |
267 | | |
268 | | void |
269 | | PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, |
270 | | const Sequence<JSObject*>& aTransferable, |
271 | | ErrorResult& aRv); |
272 | | |
273 | | void |
274 | | Close(); |
275 | | |
276 | | IMPL_EVENT_HANDLER(message) |
277 | | IMPL_EVENT_HANDLER(messageerror) |
278 | | }; |
279 | | |
280 | | class SharedWorkerGlobalScope final : public WorkerGlobalScope |
281 | | { |
282 | | const nsString mName; |
283 | | |
284 | 0 | ~SharedWorkerGlobalScope() { } |
285 | | |
286 | | public: |
287 | | SharedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, |
288 | | const nsString& aName); |
289 | | |
290 | | virtual bool |
291 | | WrapGlobalObject(JSContext* aCx, |
292 | | JS::MutableHandle<JSObject*> aReflector) override; |
293 | | |
294 | | void GetName(DOMString& aName) const |
295 | | { |
296 | | aName.AsAString() = mName; |
297 | | } |
298 | | |
299 | | void |
300 | | Close(); |
301 | | |
302 | | IMPL_EVENT_HANDLER(connect) |
303 | | }; |
304 | | |
305 | | class ServiceWorkerGlobalScope final : public WorkerGlobalScope |
306 | | { |
307 | | const nsString mScope; |
308 | | RefPtr<Clients> mClients; |
309 | | RefPtr<ServiceWorkerRegistration> mRegistration; |
310 | | |
311 | | ~ServiceWorkerGlobalScope(); |
312 | | |
313 | | public: |
314 | | NS_DECL_ISUPPORTS_INHERITED |
315 | | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerGlobalScope, |
316 | | WorkerGlobalScope) |
317 | | IMPL_EVENT_HANDLER(notificationclick) |
318 | | IMPL_EVENT_HANDLER(notificationclose) |
319 | | |
320 | | ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, |
321 | | const ServiceWorkerRegistrationDescriptor& aRegistrationDescriptor); |
322 | | |
323 | | virtual bool |
324 | | WrapGlobalObject(JSContext* aCx, |
325 | | JS::MutableHandle<JSObject*> aReflector) override; |
326 | | |
327 | | void |
328 | | GetScope(nsString& aScope) const |
329 | | { |
330 | | aScope = mScope; |
331 | | } |
332 | | |
333 | | already_AddRefed<Clients> |
334 | | GetClients(); |
335 | | |
336 | | ServiceWorkerRegistration* |
337 | | Registration(); |
338 | | |
339 | | already_AddRefed<Promise> |
340 | | SkipWaiting(ErrorResult& aRv); |
341 | | |
342 | | IMPL_EVENT_HANDLER(activate) |
343 | | IMPL_EVENT_HANDLER(install) |
344 | | IMPL_EVENT_HANDLER(message) |
345 | | |
346 | | IMPL_EVENT_HANDLER(push) |
347 | | IMPL_EVENT_HANDLER(pushsubscriptionchange) |
348 | | |
349 | | EventHandlerNonNull* |
350 | | GetOnfetch(); |
351 | | |
352 | | void |
353 | | SetOnfetch(mozilla::dom::EventHandlerNonNull* aCallback); |
354 | | |
355 | | void EventListenerAdded(nsAtom* aType) override; |
356 | | }; |
357 | | |
358 | | class WorkerDebuggerGlobalScope final : public DOMEventTargetHelper, |
359 | | public nsIGlobalObject |
360 | | { |
361 | | WorkerPrivate* mWorkerPrivate; |
362 | | RefPtr<Console> mConsole; |
363 | | nsCOMPtr<nsISerialEventTarget> mSerialEventTarget; |
364 | | |
365 | | public: |
366 | | explicit WorkerDebuggerGlobalScope(WorkerPrivate* aWorkerPrivate); |
367 | | |
368 | | NS_DECL_ISUPPORTS_INHERITED |
369 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerDebuggerGlobalScope, |
370 | | DOMEventTargetHelper) |
371 | | |
372 | | virtual JSObject* |
373 | | WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override |
374 | 0 | { |
375 | 0 | MOZ_CRASH("Shouldn't get here!"); |
376 | 0 | } |
377 | | |
378 | | virtual bool |
379 | | WrapGlobalObject(JSContext* aCx, |
380 | | JS::MutableHandle<JSObject*> aReflector); |
381 | | |
382 | | virtual JSObject* |
383 | | GetGlobalJSObject(void) override |
384 | 0 | { |
385 | 0 | return GetWrapper(); |
386 | 0 | } |
387 | | |
388 | | void |
389 | | GetGlobal(JSContext* aCx, JS::MutableHandle<JSObject*> aGlobal, |
390 | | ErrorResult& aRv); |
391 | | |
392 | | void |
393 | | CreateSandbox(JSContext* aCx, const nsAString& aName, |
394 | | JS::Handle<JSObject*> aPrototype, |
395 | | JS::MutableHandle<JSObject*> aResult, |
396 | | ErrorResult& aRv); |
397 | | |
398 | | void |
399 | | LoadSubScript(JSContext* aCx, const nsAString& aURL, |
400 | | const Optional<JS::Handle<JSObject*>>& aSandbox, |
401 | | ErrorResult& aRv); |
402 | | |
403 | | void |
404 | | EnterEventLoop(); |
405 | | |
406 | | void |
407 | | LeaveEventLoop(); |
408 | | |
409 | | void |
410 | | PostMessage(const nsAString& aMessage); |
411 | | |
412 | | IMPL_EVENT_HANDLER(message) |
413 | | IMPL_EVENT_HANDLER(messageerror) |
414 | | |
415 | | void |
416 | | SetImmediate(Function& aHandler, ErrorResult& aRv); |
417 | | |
418 | | void |
419 | | ReportError(JSContext* aCx, const nsAString& aMessage); |
420 | | |
421 | | void |
422 | | RetrieveConsoleEvents(JSContext* aCx, nsTArray<JS::Value>& aEvents, |
423 | | ErrorResult& aRv); |
424 | | |
425 | | void |
426 | | SetConsoleEventHandler(JSContext* aCx, AnyCallback* aHandler, |
427 | | ErrorResult& aRv); |
428 | | |
429 | | already_AddRefed<Console> |
430 | | GetConsole(ErrorResult& aRv); |
431 | | |
432 | | Console* |
433 | | GetConsoleIfExists() const |
434 | 0 | { |
435 | 0 | return mConsole; |
436 | 0 | } |
437 | | |
438 | | void |
439 | | Dump(JSContext* aCx, const Optional<nsAString>& aString) const; |
440 | | |
441 | | // Override DispatchTrait API to target the worker thread. Dispatch may |
442 | | // return failure if the worker thread is not alive. |
443 | | nsresult |
444 | | Dispatch(TaskCategory aCategory, |
445 | | already_AddRefed<nsIRunnable>&& aRunnable) override; |
446 | | |
447 | | nsISerialEventTarget* |
448 | | EventTargetFor(TaskCategory aCategory) const override; |
449 | | |
450 | | AbstractThread* |
451 | | AbstractMainThreadFor(TaskCategory aCategory) override; |
452 | | |
453 | | private: |
454 | | virtual ~WorkerDebuggerGlobalScope(); |
455 | | }; |
456 | | |
457 | | } // namespace dom |
458 | | } // namespace mozilla |
459 | | |
460 | | inline nsISupports* |
461 | | ToSupports(mozilla::dom::WorkerGlobalScope* aScope) |
462 | 0 | { |
463 | 0 | return static_cast<mozilla::dom::EventTarget*>(aScope); |
464 | 0 | } |
465 | | |
466 | | #endif /* mozilla_dom_workerscope_h__ */ |