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_workers_runtimeservice_h__
8 #define mozilla_dom_workers_runtimeservice_h__
10 #include "mozilla/dom/WorkerCommon.h"
12 #include "nsIObserver.h"
14 #include "js/ContextOptions.h"
15 #include "MainThreadUtils.h"
16 #include "mozilla/dom/BindingDeclarations.h"
17 #include "mozilla/dom/SafeRefPtr.h"
18 #include "mozilla/dom/workerinternals/JSSettings.h"
19 #include "mozilla/Atomics.h"
20 #include "mozilla/Mutex.h"
21 #include "nsClassHashtable.h"
22 #include "nsHashKeys.h"
25 class nsPIDOMWindowInner
;
27 namespace mozilla::dom
{
28 struct WorkerLoadInfo
;
31 namespace workerinternals
{
33 class RuntimeService final
: public nsIObserver
{
34 struct WorkerDomainInfo
{
36 nsTArray
<WorkerPrivate
*> mActiveWorkers
;
37 nsTArray
<WorkerPrivate
*> mActiveServiceWorkers
;
38 nsTArray
<WorkerPrivate
*> mQueuedWorkers
;
39 uint32_t mChildWorkerCount
;
41 WorkerDomainInfo() : mActiveWorkers(1), mChildWorkerCount(0) {}
43 uint32_t ActiveWorkerCount() const {
44 return mActiveWorkers
.Length() + mChildWorkerCount
;
47 uint32_t ActiveServiceWorkerCount() const {
48 return mActiveServiceWorkers
.Length();
51 bool HasNoWorkers() const {
52 return ActiveWorkerCount() == 0 && ActiveServiceWorkerCount() == 0;
56 mozilla::Mutex mMutex
;
58 // Protected by mMutex.
59 nsClassHashtable
<nsCStringHashKey
, WorkerDomainInfo
> mDomainMap
60 MOZ_GUARDED_BY(mMutex
);
62 // *Not* protected by mMutex.
63 nsClassHashtable
<nsPtrHashKey
<const nsPIDOMWindowInner
>,
64 nsTArray
<WorkerPrivate
*> >
67 static UniquePtr
<workerinternals::JSSettings
> sDefaultJSSettings
;
70 struct NavigatorProperties
{
72 nsString mAppNameOverridden
;
74 nsString mAppVersionOverridden
;
76 nsString mPlatformOverridden
;
77 CopyableTArray
<nsString
> mLanguages
;
81 NavigatorProperties mNavigatorProperties
;
83 // True when the observer service holds a reference to this object.
86 bool mNavigatorPropertiesLoaded
;
92 static RuntimeService
* GetOrCreateService();
94 static RuntimeService
* GetService();
96 bool RegisterWorker(WorkerPrivate
& aWorkerPrivate
);
98 void UnregisterWorker(WorkerPrivate
& aWorkerPrivate
);
100 void CancelWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
102 void FreezeWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
104 void ThawWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
106 void SuspendWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
108 void ResumeWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
110 void PropagateStorageAccessPermissionGranted(
111 const nsPIDOMWindowInner
& aWindow
);
113 const NavigatorProperties
& GetNavigatorProperties() const {
114 return mNavigatorProperties
;
117 static void GetDefaultJSSettings(workerinternals::JSSettings
& aSettings
) {
118 AssertIsOnMainThread();
119 aSettings
= *sDefaultJSSettings
;
122 static void SetDefaultContextOptions(
123 const JS::ContextOptions
& aContextOptions
) {
124 AssertIsOnMainThread();
125 sDefaultJSSettings
->contextOptions
= aContextOptions
;
128 void UpdateAppNameOverridePreference(const nsAString
& aValue
);
130 void UpdateAppVersionOverridePreference(const nsAString
& aValue
);
132 void UpdatePlatformOverridePreference(const nsAString
& aValue
);
134 void UpdateAllWorkerContextOptions();
136 void UpdateAllWorkerLanguages(const nsTArray
<nsString
>& aLanguages
);
138 static void SetDefaultJSGCSettings(JSGCParamKey aKey
,
139 Maybe
<uint32_t> aValue
) {
140 AssertIsOnMainThread();
141 sDefaultJSSettings
->ApplyGCSetting(aKey
, aValue
);
144 void UpdateAllWorkerMemoryParameter(JSGCParamKey aKey
,
145 Maybe
<uint32_t> aValue
);
148 static void SetDefaultGCZeal(uint8_t aGCZeal
, uint32_t aFrequency
) {
149 AssertIsOnMainThread();
150 sDefaultJSSettings
->gcZeal
= aGCZeal
;
151 sDefaultJSSettings
->gcZealFrequency
= aFrequency
;
154 void UpdateAllWorkerGCZeal();
157 void SetLowMemoryStateAllWorkers(bool aState
);
159 void GarbageCollectAllWorkers(bool aShrinking
);
161 void CycleCollectAllWorkers();
163 void SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline
);
165 void MemoryPressureAllWorkers();
167 uint32_t ClampedHardwareConcurrency(bool aShouldResistFingerprinting
) const;
169 void CrashIfHanging();
171 bool IsShuttingDown() const { return mShuttingDown
; }
173 void DumpRunningWorkers();
185 void AddAllTopLevelWorkersToArray(nsTArray
<WorkerPrivate
*>& aWorkers
)
186 MOZ_REQUIRES(mMutex
);
188 nsTArray
<WorkerPrivate
*> GetWorkersForWindow(
189 const nsPIDOMWindowInner
& aWindow
) const;
191 bool ScheduleWorker(WorkerPrivate
& aWorkerPrivate
);
193 template <typename Func
>
194 void BroadcastAllWorkers(const Func
& aFunc
);
197 } // namespace workerinternals
198 } // namespace mozilla::dom
200 #endif /* mozilla_dom_workers_runtimeservice_h__ */