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/. */
7 #ifndef mozilla_dom_workerscope_h__
8 #define mozilla_dom_workerscope_h__
10 #include "js/TypeDecls.h"
11 #include "js/loader/ModuleLoaderBase.h"
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/DOMEventTargetHelper.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/NotNull.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/UniquePtr.h"
19 #include "mozilla/TimeStamp.h"
20 #include "mozilla/dom/AnimationFrameProvider.h"
21 #include "mozilla/dom/ImageBitmapBinding.h"
22 #include "mozilla/dom/ImageBitmapSource.h"
23 #include "mozilla/dom/PerformanceWorker.h"
24 #include "mozilla/dom/SafeRefPtr.h"
25 #include "mozilla/dom/WorkerPrivate.h"
27 #include "nsCycleCollectionParticipant.h"
28 #include "nsIGlobalObject.h"
29 #include "nsISupports.h"
30 #include "nsWeakReference.h"
37 class nsISerialEventTarget
;
43 namespace extensions
{
45 class ExtensionBrowser
;
47 } // namespace extensions
52 enum class CallerType
: uint32_t;
59 class DebuggerNotificationManager
;
60 enum class EventCallbackDebuggerNotificationType
: uint8_t;
61 class EventHandlerNonNull
;
65 class OnErrorEventHandlerNonNull
;
70 class RequestOrUSVString
;
73 class ServiceWorkerDescriptor
;
74 class ServiceWorkerRegistration
;
75 class ServiceWorkerRegistrationDescriptor
;
76 struct StructuredSerializeOptions
;
77 class WorkerDocumentListener
;
79 class WorkerNavigator
;
81 class VsyncWorkerChild
;
82 class WebTaskScheduler
;
83 class WebTaskSchedulerWorker
;
92 class WorkerGlobalScopeBase
: public DOMEventTargetHelper
,
93 public nsSupportsWeakReference
,
94 public nsIGlobalObject
{
95 friend class WorkerPrivate
;
98 NS_DECL_ISUPPORTS_INHERITED
99 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerGlobalScopeBase
,
100 DOMEventTargetHelper
)
102 WorkerGlobalScopeBase(WorkerPrivate
* aWorkerPrivate
,
103 UniquePtr
<ClientSource
> aClientSource
);
105 virtual bool WrapGlobalObject(JSContext
* aCx
,
106 JS::MutableHandle
<JSObject
*> aReflector
) = 0;
108 // EventTarget implementation
109 JSObject
* WrapObject(JSContext
* aCx
,
110 JS::Handle
<JSObject
*> aGivenProto
) final
{
111 MOZ_CRASH("WrapObject not supported; use WrapGlobalObject.");
114 // nsIGlobalObject implementation
115 JSObject
* GetGlobalJSObject() final
;
117 JSObject
* GetGlobalJSObjectPreserveColor() const final
;
119 bool IsSharedMemoryAllowed() const final
;
121 bool ShouldResistFingerprinting(RFPTarget aTarget
) const final
;
123 OriginTrials
Trials() const final
;
125 StorageAccess
GetStorageAccess() final
;
127 Maybe
<ClientInfo
> GetClientInfo() const final
;
129 Maybe
<ServiceWorkerDescriptor
> GetController() const final
;
131 mozilla::Result
<mozilla::ipc::PrincipalInfo
, nsresult
> GetStorageKey() final
;
133 virtual void Control(const ServiceWorkerDescriptor
& aServiceWorker
);
135 // DispatcherTrait implementation
136 nsresult
Dispatch(already_AddRefed
<nsIRunnable
>&& aRunnable
) const final
;
137 nsISerialEventTarget
* SerialEventTarget() const final
;
140 void ReportError(JSContext
* aCx
, JS::Handle
<JS::Value
> aError
,
141 CallerType aCallerType
, ErrorResult
& aRv
);
143 // atob, btoa, and dump are declared (separately) by both WorkerGlobalScope
144 // and WorkerDebuggerGlobalScope WebIDL interfaces
145 void Atob(const nsAString
& aAtob
, nsAString
& aOut
, ErrorResult
& aRv
) const;
147 void Btoa(const nsAString
& aBtoa
, nsAString
& aOut
, ErrorResult
& aRv
) const;
149 already_AddRefed
<Console
> GetConsole(ErrorResult
& aRv
);
151 Console
* GetConsoleIfExists() const { return mConsole
; }
153 void InitModuleLoader(JS::loader::ModuleLoaderBase
* aModuleLoader
) {
154 if (!mModuleLoader
) {
155 mModuleLoader
= aModuleLoader
;
159 // The nullptr here is not used, but is required to make the override method
160 // have the same signature as other GetModuleLoader methods on globals.
161 JS::loader::ModuleLoaderBase
* GetModuleLoader(
162 JSContext
* aCx
= nullptr) override
{
163 return mModuleLoader
;
166 uint64_t WindowID() const;
168 // Usually global scope dies earlier than the WorkerPrivate, but if we see
169 // it leak at least we can tell it to not carry away a dead pointer.
170 void NoteWorkerTerminated() { mWorkerPrivate
= nullptr; }
172 ClientSource
& MutableClientSourceRef() const { return *mClientSource
; }
174 // WorkerPrivate wants to be able to forbid script when its state machine
176 void WorkerPrivateSaysForbidScript() { StartForbiddingScript(); }
177 void WorkerPrivateSaysAllowScript() { StopForbiddingScript(); }
180 ~WorkerGlobalScopeBase();
182 CheckedUnsafePtr
<WorkerPrivate
> mWorkerPrivate
;
184 void AssertIsOnWorkerThread() const {
185 MOZ_ASSERT(mWorkerThreadUsedOnlyForAssert
== PR_GetCurrentThread());
189 RefPtr
<Console
> mConsole
;
190 RefPtr
<JS::loader::ModuleLoaderBase
> mModuleLoader
;
191 const UniquePtr
<ClientSource
> mClientSource
;
192 nsCOMPtr
<nsISerialEventTarget
> mSerialEventTarget
;
194 PRThread
* mWorkerThreadUsedOnlyForAssert
;
198 namespace workerinternals
{
200 class NamedWorkerGlobalScopeMixin
{
202 explicit NamedWorkerGlobalScopeMixin(const nsAString
& aName
) : mName(aName
) {}
204 void GetName(DOMString
& aName
) const;
207 ~NamedWorkerGlobalScopeMixin() = default;
210 const nsString mName
;
213 } // namespace workerinternals
215 class WorkerGlobalScope
: public WorkerGlobalScopeBase
{
217 NS_DECL_ISUPPORTS_INHERITED
218 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WorkerGlobalScope
,
219 WorkerGlobalScopeBase
)
221 using WorkerGlobalScopeBase::WorkerGlobalScopeBase
;
223 void NoteTerminating();
225 void NoteShuttingDown();
227 // nsIGlobalObject implementation
228 RefPtr
<ServiceWorkerRegistration
> GetServiceWorkerRegistration(
229 const ServiceWorkerRegistrationDescriptor
& aDescriptor
) const final
;
231 RefPtr
<ServiceWorkerRegistration
> GetOrCreateServiceWorkerRegistration(
232 const ServiceWorkerRegistrationDescriptor
& aDescriptor
) final
;
234 DebuggerNotificationManager
* GetOrCreateDebuggerNotificationManager() final
;
236 DebuggerNotificationManager
* GetExistingDebuggerNotificationManager() final
;
238 Maybe
<EventCallbackDebuggerNotificationType
> GetDebuggerNotificationType()
241 mozilla::dom::StorageManager
* GetStorageManager() final
;
243 void SetIsNotEligibleForMessaging() { mIsEligibleForMessaging
= false; }
245 bool IsEligibleForMessaging() final
;
247 // WorkerGlobalScope WebIDL implementation
248 WorkerGlobalScope
* Self() { return this; }
250 already_AddRefed
<WorkerLocation
> Location();
252 already_AddRefed
<WorkerNavigator
> Navigator();
254 already_AddRefed
<WorkerNavigator
> GetExistingNavigator() const;
256 FontFaceSet
* GetFonts(ErrorResult
&);
257 FontFaceSet
* GetFonts() final
{ return GetFonts(IgnoreErrors()); }
259 void ImportScripts(JSContext
* aCx
, const Sequence
<nsString
>& aScriptURLs
,
262 OnErrorEventHandlerNonNull
* GetOnerror();
264 void SetOnerror(OnErrorEventHandlerNonNull
* aHandler
);
266 IMPL_EVENT_HANDLER(languagechange
)
267 IMPL_EVENT_HANDLER(offline
)
268 IMPL_EVENT_HANDLER(online
)
269 IMPL_EVENT_HANDLER(rejectionhandled
)
270 IMPL_EVENT_HANDLER(unhandledrejection
)
272 void Dump(const Optional
<nsAString
>& aString
) const;
274 Performance
* GetPerformance();
276 Performance
* GetPerformanceIfExists() const { return mPerformance
; }
278 static bool IsInAutomation(JSContext
* aCx
, JSObject
*);
280 void GetJSTestingFunctions(JSContext
* aCx
,
281 JS::MutableHandle
<JSObject
*> aFunctions
,
284 // GlobalCrypto WebIDL implementation
285 Crypto
* GetCrypto(ErrorResult
& aError
);
287 // WindowOrWorkerGlobalScope WebIDL implementation
288 void GetOrigin(nsAString
& aOrigin
) const;
290 bool CrossOriginIsolated() const final
;
293 int32_t SetTimeout(JSContext
* aCx
, Function
& aHandler
, int32_t aTimeout
,
294 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aRv
);
297 int32_t SetTimeout(JSContext
* aCx
, const nsAString
& aHandler
,
298 int32_t aTimeout
, const Sequence
<JS::Value
>&,
302 void ClearTimeout(int32_t aHandle
);
305 int32_t SetInterval(JSContext
* aCx
, Function
& aHandler
, int32_t aTimeout
,
306 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aRv
);
308 int32_t SetInterval(JSContext
* aCx
, const nsAString
& aHandler
,
309 int32_t aTimeout
, const Sequence
<JS::Value
>&,
313 void ClearInterval(int32_t aHandle
);
315 already_AddRefed
<Promise
> CreateImageBitmap(
316 const ImageBitmapSource
& aImage
, const ImageBitmapOptions
& aOptions
,
319 already_AddRefed
<Promise
> CreateImageBitmap(
320 const ImageBitmapSource
& aImage
, int32_t aSx
, int32_t aSy
, int32_t aSw
,
321 int32_t aSh
, const ImageBitmapOptions
& aOptions
, ErrorResult
& aRv
);
323 void StructuredClone(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
324 const StructuredSerializeOptions
& aOptions
,
325 JS::MutableHandle
<JS::Value
> aRetval
,
326 ErrorResult
& aError
);
328 already_AddRefed
<Promise
> Fetch(const RequestOrUSVString
& aInput
,
329 const RequestInit
& aInit
,
330 CallerType aCallerType
, ErrorResult
& aRv
);
332 bool IsSecureContext() const;
334 already_AddRefed
<IDBFactory
> GetIndexedDB(JSContext
* aCx
,
335 ErrorResult
& aErrorResult
);
337 already_AddRefed
<cache::CacheStorage
> GetCaches(ErrorResult
& aRv
);
339 WebTaskScheduler
* Scheduler();
340 WebTaskScheduler
* GetExistingScheduler() const;
342 bool WindowInteractionAllowed() const;
344 void AllowWindowInteraction();
346 void ConsumeWindowInteraction();
348 void StorageAccessPermissionGranted();
350 virtual void OnDocumentVisible(bool aVisible
) {}
352 MOZ_CAN_RUN_SCRIPT_BOUNDARY
353 virtual void OnVsync(const VsyncEvent
& aVsync
) {}
356 ~WorkerGlobalScope();
360 int32_t SetTimeoutOrInterval(JSContext
* aCx
, Function
& aHandler
,
362 const Sequence
<JS::Value
>& aArguments
,
363 bool aIsInterval
, ErrorResult
& aRv
);
366 int32_t SetTimeoutOrInterval(JSContext
* aCx
, const nsAString
& aHandler
,
367 int32_t aTimeout
, bool aIsInterval
,
370 RefPtr
<Crypto
> mCrypto
;
371 RefPtr
<WorkerLocation
> mLocation
;
372 RefPtr
<WorkerNavigator
> mNavigator
;
373 RefPtr
<FontFaceSet
> mFontFaceSet
;
374 RefPtr
<Performance
> mPerformance
;
375 RefPtr
<IDBFactory
> mIndexedDB
;
376 RefPtr
<cache::CacheStorage
> mCacheStorage
;
377 RefPtr
<DebuggerNotificationManager
> mDebuggerNotificationManager
;
378 RefPtr
<WebTaskSchedulerWorker
> mWebTaskScheduler
;
379 uint32_t mWindowInteractionsAllowed
= 0;
380 bool mIsEligibleForMessaging
{true};
383 class DedicatedWorkerGlobalScope final
384 : public WorkerGlobalScope
,
385 public workerinternals::NamedWorkerGlobalScopeMixin
{
387 NS_DECL_ISUPPORTS_INHERITED
388 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
389 DedicatedWorkerGlobalScope
, WorkerGlobalScope
)
391 DedicatedWorkerGlobalScope(WorkerPrivate
* aWorkerPrivate
,
392 UniquePtr
<ClientSource
> aClientSource
,
393 const nsString
& aName
);
395 bool WrapGlobalObject(JSContext
* aCx
,
396 JS::MutableHandle
<JSObject
*> aReflector
) override
;
398 void PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
399 const Sequence
<JSObject
*>& aTransferable
, ErrorResult
& aRv
);
401 void PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
402 const StructuredSerializeOptions
& aOptions
,
408 int32_t RequestAnimationFrame(FrameRequestCallback
& aCallback
,
409 ErrorResult
& aError
);
412 void CancelAnimationFrame(int32_t aHandle
, ErrorResult
& aError
);
414 void OnDocumentVisible(bool aVisible
) override
;
416 MOZ_CAN_RUN_SCRIPT_BOUNDARY
417 void OnVsync(const VsyncEvent
& aVsync
) override
;
419 IMPL_EVENT_HANDLER(message
)
420 IMPL_EVENT_HANDLER(messageerror
)
421 IMPL_EVENT_HANDLER(rtctransform
)
424 ~DedicatedWorkerGlobalScope() = default;
426 FrameRequestManager mFrameRequestManager
;
427 RefPtr
<VsyncWorkerChild
> mVsyncChild
;
428 RefPtr
<WorkerDocumentListener
> mDocListener
;
429 bool mDocumentVisible
= false;
432 class SharedWorkerGlobalScope final
433 : public WorkerGlobalScope
,
434 public workerinternals::NamedWorkerGlobalScopeMixin
{
436 SharedWorkerGlobalScope(WorkerPrivate
* aWorkerPrivate
,
437 UniquePtr
<ClientSource
> aClientSource
,
438 const nsString
& aName
);
440 bool WrapGlobalObject(JSContext
* aCx
,
441 JS::MutableHandle
<JSObject
*> aReflector
) override
;
445 IMPL_EVENT_HANDLER(connect
)
448 ~SharedWorkerGlobalScope() = default;
451 class ServiceWorkerGlobalScope final
: public WorkerGlobalScope
{
453 NS_DECL_ISUPPORTS_INHERITED
454 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerGlobalScope
,
457 ServiceWorkerGlobalScope(
458 WorkerPrivate
* aWorkerPrivate
, UniquePtr
<ClientSource
> aClientSource
,
459 const ServiceWorkerRegistrationDescriptor
& aRegistrationDescriptor
);
461 bool WrapGlobalObject(JSContext
* aCx
,
462 JS::MutableHandle
<JSObject
*> aReflector
) override
;
464 already_AddRefed
<Clients
> GetClients();
466 ServiceWorkerRegistration
* Registration();
468 already_AddRefed
<Promise
> SkipWaiting(ErrorResult
& aRv
);
470 SafeRefPtr
<extensions::ExtensionBrowser
> AcquireExtensionBrowser();
472 IMPL_EVENT_HANDLER(install
)
473 IMPL_EVENT_HANDLER(activate
)
475 EventHandlerNonNull
* GetOnfetch();
477 void SetOnfetch(EventHandlerNonNull
* aCallback
);
479 void EventListenerAdded(nsAtom
* aType
) override
;
481 IMPL_EVENT_HANDLER(message
)
482 IMPL_EVENT_HANDLER(messageerror
)
484 IMPL_EVENT_HANDLER(notificationclick
)
485 IMPL_EVENT_HANDLER(notificationclose
)
487 IMPL_EVENT_HANDLER(push
)
488 IMPL_EVENT_HANDLER(pushsubscriptionchange
)
491 ~ServiceWorkerGlobalScope();
493 void NoteFetchHandlerWasAdded() const;
495 RefPtr
<Clients
> mClients
;
496 const nsString mScope
;
497 RefPtr
<ServiceWorkerRegistration
> mRegistration
;
498 SafeRefPtr
<extensions::ExtensionBrowser
> mExtensionBrowser
;
501 class WorkerDebuggerGlobalScope final
: public WorkerGlobalScopeBase
{
503 using WorkerGlobalScopeBase::WorkerGlobalScopeBase
;
505 bool WrapGlobalObject(JSContext
* aCx
,
506 JS::MutableHandle
<JSObject
*> aReflector
) override
;
508 void Control(const ServiceWorkerDescriptor
& aServiceWorker
) override
{
509 MOZ_CRASH("Can't control debugger workers.");
512 void GetGlobal(JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aGlobal
,
515 void CreateSandbox(JSContext
* aCx
, const nsAString
& aName
,
516 JS::Handle
<JSObject
*> aPrototype
,
517 JS::MutableHandle
<JSObject
*> aResult
, ErrorResult
& aRv
);
519 void LoadSubScript(JSContext
* aCx
, const nsAString
& aUrl
,
520 const Optional
<JS::Handle
<JSObject
*>>& aSandbox
,
523 MOZ_CAN_RUN_SCRIPT
void EnterEventLoop();
525 void LeaveEventLoop();
527 void PostMessage(const nsAString
& aMessage
);
529 void SetImmediate(Function
& aHandler
, ErrorResult
& aRv
);
531 void ReportError(JSContext
* aCx
, const nsAString
& aMessage
);
533 void RetrieveConsoleEvents(JSContext
* aCx
, nsTArray
<JS::Value
>& aEvents
,
536 void ClearConsoleEvents(JSContext
* aCx
, ErrorResult
& aRv
);
538 void SetConsoleEventHandler(JSContext
* aCx
, AnyCallback
* aHandler
,
541 void Dump(JSContext
* aCx
, const Optional
<nsAString
>& aString
) const;
543 IMPL_EVENT_HANDLER(message
)
544 IMPL_EVENT_HANDLER(messageerror
)
547 ~WorkerDebuggerGlobalScope() = default;
551 } // namespace mozilla
553 inline nsISupports
* ToSupports(mozilla::dom::WorkerGlobalScope
* aScope
) {
554 return static_cast<mozilla::dom::EventTarget
*>(aScope
);
557 #endif /* mozilla_dom_workerscope_h__ */