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/TrustedTypePolicyFactory.h"
26 #include "mozilla/dom/WorkerPrivate.h"
28 #include "nsCycleCollectionParticipant.h"
29 #include "nsIGlobalObject.h"
30 #include "nsISupports.h"
31 #include "nsWeakReference.h"
38 class nsISerialEventTarget
;
44 namespace extensions
{
46 class ExtensionBrowser
;
48 } // namespace extensions
53 enum class CallerType
: uint32_t;
60 class DebuggerNotificationManager
;
61 enum class EventCallbackDebuggerNotificationType
: uint8_t;
62 class EventHandlerNonNull
;
66 class OnErrorEventHandlerNonNull
;
71 class RequestOrUTF8String
;
74 class ServiceWorkerDescriptor
;
75 class ServiceWorkerRegistration
;
76 class ServiceWorkerRegistrationDescriptor
;
77 struct StructuredSerializeOptions
;
78 class WorkerDocumentListener
;
80 class WorkerNavigator
;
82 class VsyncWorkerChild
;
83 class WebTaskScheduler
;
84 class WebTaskSchedulerWorker
;
93 class WorkerGlobalScopeBase
: public DOMEventTargetHelper
,
94 public nsSupportsWeakReference
,
95 public nsIGlobalObject
{
96 friend class WorkerPrivate
;
99 NS_DECL_ISUPPORTS_INHERITED
100 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerGlobalScopeBase
,
101 DOMEventTargetHelper
)
103 WorkerGlobalScopeBase(WorkerPrivate
* aWorkerPrivate
,
104 UniquePtr
<ClientSource
> aClientSource
);
106 virtual bool WrapGlobalObject(JSContext
* aCx
,
107 JS::MutableHandle
<JSObject
*> aReflector
) = 0;
109 // EventTarget implementation
110 JSObject
* WrapObject(JSContext
* aCx
,
111 JS::Handle
<JSObject
*> aGivenProto
) final
{
112 MOZ_CRASH("WrapObject not supported; use WrapGlobalObject.");
115 // nsIGlobalObject implementation
116 JSObject
* GetGlobalJSObject() final
;
118 JSObject
* GetGlobalJSObjectPreserveColor() const final
;
120 bool IsSharedMemoryAllowed() const final
;
122 bool ShouldResistFingerprinting(RFPTarget aTarget
) const final
;
124 OriginTrials
Trials() const final
;
126 StorageAccess
GetStorageAccess() final
;
128 Maybe
<ClientInfo
> GetClientInfo() const final
;
130 Maybe
<ServiceWorkerDescriptor
> GetController() const final
;
132 mozilla::Result
<mozilla::ipc::PrincipalInfo
, nsresult
> GetStorageKey() final
;
134 virtual void Control(const ServiceWorkerDescriptor
& aServiceWorker
);
136 // DispatcherTrait implementation
137 nsresult
Dispatch(already_AddRefed
<nsIRunnable
>&& aRunnable
) const final
;
138 nsISerialEventTarget
* SerialEventTarget() const final
;
141 void ReportError(JSContext
* aCx
, JS::Handle
<JS::Value
> aError
,
142 CallerType aCallerType
, ErrorResult
& aRv
);
144 // atob, btoa, and dump are declared (separately) by both WorkerGlobalScope
145 // and WorkerDebuggerGlobalScope WebIDL interfaces
146 void Atob(const nsAString
& aAtob
, nsAString
& aOut
, ErrorResult
& aRv
) const;
148 void Btoa(const nsAString
& aBtoa
, nsAString
& aOut
, ErrorResult
& aRv
) const;
150 already_AddRefed
<Console
> GetConsole(ErrorResult
& aRv
);
152 Console
* GetConsoleIfExists() const { return mConsole
; }
154 void InitModuleLoader(JS::loader::ModuleLoaderBase
* aModuleLoader
) {
155 if (!mModuleLoader
) {
156 mModuleLoader
= aModuleLoader
;
160 // The nullptr here is not used, but is required to make the override method
161 // have the same signature as other GetModuleLoader methods on globals.
162 JS::loader::ModuleLoaderBase
* GetModuleLoader(
163 JSContext
* aCx
= nullptr) override
{
164 return mModuleLoader
;
167 uint64_t WindowID() const;
169 // Usually global scope dies earlier than the WorkerPrivate, but if we see
170 // it leak at least we can tell it to not carry away a dead pointer.
171 void NoteWorkerTerminated() { mWorkerPrivate
= nullptr; }
173 ClientSource
& MutableClientSourceRef() const { return *mClientSource
; }
175 // WorkerPrivate wants to be able to forbid script when its state machine
177 void WorkerPrivateSaysForbidScript() { StartForbiddingScript(); }
178 void WorkerPrivateSaysAllowScript() { StopForbiddingScript(); }
181 ~WorkerGlobalScopeBase();
183 CheckedUnsafePtr
<WorkerPrivate
> mWorkerPrivate
;
185 void AssertIsOnWorkerThread() const {
186 MOZ_ASSERT(mWorkerThreadUsedOnlyForAssert
== PR_GetCurrentThread());
190 RefPtr
<Console
> mConsole
;
191 RefPtr
<JS::loader::ModuleLoaderBase
> mModuleLoader
;
192 const UniquePtr
<ClientSource
> mClientSource
;
193 nsCOMPtr
<nsISerialEventTarget
> mSerialEventTarget
;
195 PRThread
* mWorkerThreadUsedOnlyForAssert
;
199 namespace workerinternals
{
201 class NamedWorkerGlobalScopeMixin
{
203 explicit NamedWorkerGlobalScopeMixin(const nsAString
& aName
) : mName(aName
) {}
205 void GetName(DOMString
& aName
) const;
208 ~NamedWorkerGlobalScopeMixin() = default;
211 const nsString mName
;
214 } // namespace workerinternals
216 class WorkerGlobalScope
: public WorkerGlobalScopeBase
{
218 NS_DECL_ISUPPORTS_INHERITED
219 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WorkerGlobalScope
,
220 WorkerGlobalScopeBase
)
222 using WorkerGlobalScopeBase::WorkerGlobalScopeBase
;
224 void NoteTerminating();
226 void NoteShuttingDown();
228 // nsIGlobalObject implementation
229 RefPtr
<ServiceWorkerRegistration
> GetServiceWorkerRegistration(
230 const ServiceWorkerRegistrationDescriptor
& aDescriptor
) const final
;
232 RefPtr
<ServiceWorkerRegistration
> GetOrCreateServiceWorkerRegistration(
233 const ServiceWorkerRegistrationDescriptor
& aDescriptor
) final
;
235 DebuggerNotificationManager
* GetOrCreateDebuggerNotificationManager() final
;
237 DebuggerNotificationManager
* GetExistingDebuggerNotificationManager() final
;
239 Maybe
<EventCallbackDebuggerNotificationType
> GetDebuggerNotificationType()
242 mozilla::dom::StorageManager
* GetStorageManager() final
;
244 void SetIsNotEligibleForMessaging() { mIsEligibleForMessaging
= false; }
246 bool IsEligibleForMessaging() final
;
248 // WorkerGlobalScope WebIDL implementation
249 WorkerGlobalScope
* Self() { return this; }
251 already_AddRefed
<WorkerLocation
> Location();
253 already_AddRefed
<WorkerNavigator
> Navigator();
255 already_AddRefed
<WorkerNavigator
> GetExistingNavigator() const;
257 FontFaceSet
* GetFonts(ErrorResult
&);
258 FontFaceSet
* GetFonts() final
{ return GetFonts(IgnoreErrors()); }
260 void ImportScripts(JSContext
* aCx
, const Sequence
<nsString
>& aScriptURLs
,
263 OnErrorEventHandlerNonNull
* GetOnerror();
265 void SetOnerror(OnErrorEventHandlerNonNull
* aHandler
);
267 IMPL_EVENT_HANDLER(languagechange
)
268 IMPL_EVENT_HANDLER(offline
)
269 IMPL_EVENT_HANDLER(online
)
270 IMPL_EVENT_HANDLER(rejectionhandled
)
271 IMPL_EVENT_HANDLER(unhandledrejection
)
273 void Dump(const Optional
<nsAString
>& aString
) const;
275 Performance
* GetPerformance();
277 Performance
* GetPerformanceIfExists() const { return mPerformance
; }
279 static bool IsInAutomation(JSContext
* aCx
, JSObject
*);
281 void GetJSTestingFunctions(JSContext
* aCx
,
282 JS::MutableHandle
<JSObject
*> aFunctions
,
285 // GlobalCrypto WebIDL implementation
286 Crypto
* GetCrypto(ErrorResult
& aError
);
288 // WindowOrWorkerGlobalScope WebIDL implementation
289 void GetOrigin(nsAString
& aOrigin
) const;
291 bool CrossOriginIsolated() const final
;
294 int32_t SetTimeout(JSContext
* aCx
, Function
& aHandler
, int32_t aTimeout
,
295 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aRv
);
298 int32_t SetTimeout(JSContext
* aCx
, const nsAString
& aHandler
,
299 int32_t aTimeout
, const Sequence
<JS::Value
>&,
303 void ClearTimeout(int32_t aHandle
);
306 int32_t SetInterval(JSContext
* aCx
, Function
& aHandler
, int32_t aTimeout
,
307 const Sequence
<JS::Value
>& aArguments
, ErrorResult
& aRv
);
309 int32_t SetInterval(JSContext
* aCx
, const nsAString
& aHandler
,
310 int32_t aTimeout
, const Sequence
<JS::Value
>&,
314 void ClearInterval(int32_t aHandle
);
316 already_AddRefed
<Promise
> CreateImageBitmap(
317 const ImageBitmapSource
& aImage
, const ImageBitmapOptions
& aOptions
,
320 already_AddRefed
<Promise
> CreateImageBitmap(
321 const ImageBitmapSource
& aImage
, int32_t aSx
, int32_t aSy
, int32_t aSw
,
322 int32_t aSh
, const ImageBitmapOptions
& aOptions
, ErrorResult
& aRv
);
324 void StructuredClone(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
325 const StructuredSerializeOptions
& aOptions
,
326 JS::MutableHandle
<JS::Value
> aRetval
,
327 ErrorResult
& aError
);
329 already_AddRefed
<Promise
> Fetch(const RequestOrUTF8String
& aInput
,
330 const RequestInit
& aInit
,
331 CallerType aCallerType
, ErrorResult
& aRv
);
333 bool IsSecureContext() const;
335 already_AddRefed
<IDBFactory
> GetIndexedDB(JSContext
* aCx
,
336 ErrorResult
& aErrorResult
);
338 already_AddRefed
<cache::CacheStorage
> GetCaches(ErrorResult
& aRv
);
340 WebTaskScheduler
* Scheduler();
341 WebTaskScheduler
* GetExistingScheduler() const;
343 bool WindowInteractionAllowed() const;
345 void AllowWindowInteraction();
347 void ConsumeWindowInteraction();
349 void StorageAccessPermissionGranted();
351 virtual void OnDocumentVisible(bool aVisible
) {}
353 MOZ_CAN_RUN_SCRIPT_BOUNDARY
354 virtual void OnVsync(const VsyncEvent
& aVsync
) {}
356 TrustedTypePolicyFactory
* TrustedTypes();
359 ~WorkerGlobalScope();
363 int32_t SetTimeoutOrInterval(JSContext
* aCx
, Function
& aHandler
,
365 const Sequence
<JS::Value
>& aArguments
,
366 bool aIsInterval
, ErrorResult
& aRv
);
369 int32_t SetTimeoutOrInterval(JSContext
* aCx
, const nsAString
& aHandler
,
370 int32_t aTimeout
, bool aIsInterval
,
373 RefPtr
<Crypto
> mCrypto
;
374 RefPtr
<WorkerLocation
> mLocation
;
375 RefPtr
<WorkerNavigator
> mNavigator
;
376 RefPtr
<FontFaceSet
> mFontFaceSet
;
377 RefPtr
<Performance
> mPerformance
;
378 RefPtr
<IDBFactory
> mIndexedDB
;
379 RefPtr
<cache::CacheStorage
> mCacheStorage
;
380 RefPtr
<DebuggerNotificationManager
> mDebuggerNotificationManager
;
381 RefPtr
<WebTaskSchedulerWorker
> mWebTaskScheduler
;
382 RefPtr
<TrustedTypePolicyFactory
> mTrustedTypePolicyFactory
;
383 uint32_t mWindowInteractionsAllowed
= 0;
384 bool mIsEligibleForMessaging
{true};
387 class DedicatedWorkerGlobalScope final
388 : public WorkerGlobalScope
,
389 public workerinternals::NamedWorkerGlobalScopeMixin
{
391 NS_DECL_ISUPPORTS_INHERITED
392 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
393 DedicatedWorkerGlobalScope
, WorkerGlobalScope
)
395 DedicatedWorkerGlobalScope(WorkerPrivate
* aWorkerPrivate
,
396 UniquePtr
<ClientSource
> aClientSource
,
397 const nsString
& aName
);
399 bool WrapGlobalObject(JSContext
* aCx
,
400 JS::MutableHandle
<JSObject
*> aReflector
) override
;
402 void PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
403 const Sequence
<JSObject
*>& aTransferable
, ErrorResult
& aRv
);
405 void PostMessage(JSContext
* aCx
, JS::Handle
<JS::Value
> aMessage
,
406 const StructuredSerializeOptions
& aOptions
,
412 int32_t RequestAnimationFrame(FrameRequestCallback
& aCallback
,
413 ErrorResult
& aError
);
416 void CancelAnimationFrame(int32_t aHandle
, ErrorResult
& aError
);
418 void OnDocumentVisible(bool aVisible
) override
;
420 MOZ_CAN_RUN_SCRIPT_BOUNDARY
421 void OnVsync(const VsyncEvent
& aVsync
) override
;
423 IMPL_EVENT_HANDLER(message
)
424 IMPL_EVENT_HANDLER(messageerror
)
425 IMPL_EVENT_HANDLER(rtctransform
)
428 ~DedicatedWorkerGlobalScope() = default;
430 FrameRequestManager mFrameRequestManager
;
431 RefPtr
<VsyncWorkerChild
> mVsyncChild
;
432 RefPtr
<WorkerDocumentListener
> mDocListener
;
433 bool mDocumentVisible
= false;
436 class SharedWorkerGlobalScope final
437 : public WorkerGlobalScope
,
438 public workerinternals::NamedWorkerGlobalScopeMixin
{
440 SharedWorkerGlobalScope(WorkerPrivate
* aWorkerPrivate
,
441 UniquePtr
<ClientSource
> aClientSource
,
442 const nsString
& aName
);
444 bool WrapGlobalObject(JSContext
* aCx
,
445 JS::MutableHandle
<JSObject
*> aReflector
) override
;
449 IMPL_EVENT_HANDLER(connect
)
452 ~SharedWorkerGlobalScope() = default;
455 class ServiceWorkerGlobalScope final
: public WorkerGlobalScope
{
457 NS_DECL_ISUPPORTS_INHERITED
458 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerGlobalScope
,
461 ServiceWorkerGlobalScope(
462 WorkerPrivate
* aWorkerPrivate
, UniquePtr
<ClientSource
> aClientSource
,
463 const ServiceWorkerRegistrationDescriptor
& aRegistrationDescriptor
);
465 bool WrapGlobalObject(JSContext
* aCx
,
466 JS::MutableHandle
<JSObject
*> aReflector
) override
;
468 already_AddRefed
<Clients
> GetClients();
470 ServiceWorkerRegistration
* Registration();
472 already_AddRefed
<Promise
> SkipWaiting(ErrorResult
& aRv
);
474 SafeRefPtr
<extensions::ExtensionBrowser
> AcquireExtensionBrowser();
476 IMPL_EVENT_HANDLER(install
)
477 IMPL_EVENT_HANDLER(activate
)
479 EventHandlerNonNull
* GetOnfetch();
481 void SetOnfetch(EventHandlerNonNull
* aCallback
);
483 void EventListenerAdded(nsAtom
* aType
) override
;
485 IMPL_EVENT_HANDLER(message
)
486 IMPL_EVENT_HANDLER(messageerror
)
488 IMPL_EVENT_HANDLER(notificationclick
)
489 IMPL_EVENT_HANDLER(notificationclose
)
491 IMPL_EVENT_HANDLER(push
)
492 IMPL_EVENT_HANDLER(pushsubscriptionchange
)
495 ~ServiceWorkerGlobalScope();
497 void NoteFetchHandlerWasAdded() const;
499 RefPtr
<Clients
> mClients
;
500 const nsString mScope
;
501 RefPtr
<ServiceWorkerRegistration
> mRegistration
;
502 SafeRefPtr
<extensions::ExtensionBrowser
> mExtensionBrowser
;
505 class WorkerDebuggerGlobalScope final
: public WorkerGlobalScopeBase
{
507 using WorkerGlobalScopeBase::WorkerGlobalScopeBase
;
509 bool WrapGlobalObject(JSContext
* aCx
,
510 JS::MutableHandle
<JSObject
*> aReflector
) override
;
512 void Control(const ServiceWorkerDescriptor
& aServiceWorker
) override
{
513 MOZ_CRASH("Can't control debugger workers.");
516 void GetGlobal(JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aGlobal
,
519 void CreateSandbox(JSContext
* aCx
, const nsAString
& aName
,
520 JS::Handle
<JSObject
*> aPrototype
,
521 JS::MutableHandle
<JSObject
*> aResult
, ErrorResult
& aRv
);
523 void LoadSubScript(JSContext
* aCx
, const nsAString
& aUrl
,
524 const Optional
<JS::Handle
<JSObject
*>>& aSandbox
,
527 MOZ_CAN_RUN_SCRIPT
void EnterEventLoop();
529 void LeaveEventLoop();
531 void PostMessage(const nsAString
& aMessage
);
533 void SetImmediate(Function
& aHandler
, ErrorResult
& aRv
);
535 void ReportError(JSContext
* aCx
, const nsAString
& aMessage
);
537 void RetrieveConsoleEvents(JSContext
* aCx
, nsTArray
<JS::Value
>& aEvents
,
540 void ClearConsoleEvents(JSContext
* aCx
, ErrorResult
& aRv
);
542 void SetConsoleEventHandler(JSContext
* aCx
, AnyCallback
* aHandler
,
545 void Dump(JSContext
* aCx
, const Optional
<nsAString
>& aString
) const;
547 IMPL_EVENT_HANDLER(message
)
548 IMPL_EVENT_HANDLER(messageerror
)
551 ~WorkerDebuggerGlobalScope() = default;
555 } // namespace mozilla
557 inline nsISupports
* ToSupports(mozilla::dom::WorkerGlobalScope
* aScope
) {
558 return static_cast<mozilla::dom::EventTarget
*>(aScope
);
561 #endif /* mozilla_dom_workerscope_h__ */