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 "mozilla/StaticPtr.h"
22 #include "nsClassHashtable.h"
23 #include "nsHashKeys.h"
26 class nsPIDOMWindowInner
;
28 namespace mozilla::dom
{
29 struct WorkerLoadInfo
;
32 namespace workerinternals
{
34 class RuntimeService final
: public nsIObserver
{
35 struct WorkerDomainInfo
{
37 nsTArray
<WorkerPrivate
*> mActiveWorkers
;
38 nsTArray
<WorkerPrivate
*> mActiveServiceWorkers
;
39 nsTArray
<WorkerPrivate
*> mQueuedWorkers
;
40 uint32_t mChildWorkerCount
;
42 WorkerDomainInfo() : mActiveWorkers(1), mChildWorkerCount(0) {}
44 uint32_t ActiveWorkerCount() const {
45 return mActiveWorkers
.Length() + mChildWorkerCount
;
48 uint32_t ActiveServiceWorkerCount() const {
49 return mActiveServiceWorkers
.Length();
52 bool HasNoWorkers() const {
53 return ActiveWorkerCount() == 0 && ActiveServiceWorkerCount() == 0;
57 mozilla::Mutex mMutex
;
59 // Protected by mMutex.
60 nsClassHashtable
<nsCStringHashKey
, WorkerDomainInfo
> mDomainMap
61 MOZ_GUARDED_BY(mMutex
);
63 // *Not* protected by mMutex.
64 nsClassHashtable
<nsPtrHashKey
<const nsPIDOMWindowInner
>,
65 nsTArray
<WorkerPrivate
*> >
68 static StaticAutoPtr
<workerinternals::JSSettings
> sDefaultJSSettings
;
71 struct NavigatorProperties
{
73 nsString mAppVersionOverridden
;
75 nsString mPlatformOverridden
;
76 CopyableTArray
<nsString
> mLanguages
;
80 NavigatorProperties mNavigatorProperties
;
82 // True when the observer service holds a reference to this object.
85 bool mNavigatorPropertiesLoaded
;
91 static RuntimeService
* GetOrCreateService();
93 static RuntimeService
* GetService();
95 bool RegisterWorker(WorkerPrivate
& aWorkerPrivate
);
97 void UnregisterWorker(WorkerPrivate
& aWorkerPrivate
);
99 void CancelWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
101 void FreezeWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
103 void ThawWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
105 void SuspendWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
107 void ResumeWorkersForWindow(const nsPIDOMWindowInner
& aWindow
);
109 void PropagateStorageAccessPermissionGranted(
110 const nsPIDOMWindowInner
& aWindow
);
112 const NavigatorProperties
& GetNavigatorProperties() const {
113 return mNavigatorProperties
;
116 static void GetDefaultJSSettings(workerinternals::JSSettings
& aSettings
) {
117 AssertIsOnMainThread();
118 aSettings
= *sDefaultJSSettings
;
121 static void SetDefaultContextOptions(
122 const JS::ContextOptions
& aContextOptions
) {
123 AssertIsOnMainThread();
124 sDefaultJSSettings
->contextOptions
= aContextOptions
;
127 void UpdateAppVersionOverridePreference(const nsAString
& aValue
);
129 void UpdatePlatformOverridePreference(const nsAString
& aValue
);
131 void UpdateAllWorkerContextOptions();
133 void UpdateAllWorkerLanguages(const nsTArray
<nsString
>& aLanguages
);
135 static void SetDefaultJSGCSettings(JSGCParamKey aKey
,
136 Maybe
<uint32_t> aValue
) {
137 AssertIsOnMainThread();
138 sDefaultJSSettings
->ApplyGCSetting(aKey
, aValue
);
141 void UpdateAllWorkerMemoryParameter(JSGCParamKey aKey
,
142 Maybe
<uint32_t> aValue
);
145 static void SetDefaultGCZeal(uint8_t aGCZeal
, uint32_t aFrequency
) {
146 AssertIsOnMainThread();
147 sDefaultJSSettings
->gcZeal
= aGCZeal
;
148 sDefaultJSSettings
->gcZealFrequency
= aFrequency
;
151 void UpdateAllWorkerGCZeal();
154 void SetLowMemoryStateAllWorkers(bool aState
);
156 void GarbageCollectAllWorkers(bool aShrinking
);
158 void CycleCollectAllWorkers();
160 void SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline
);
162 void MemoryPressureAllWorkers();
164 uint32_t ClampedHardwareConcurrency(bool aShouldResistFingerprinting
) const;
166 void CrashIfHanging();
168 bool IsShuttingDown() const { return mShuttingDown
; }
170 void DumpRunningWorkers();
182 void AddAllTopLevelWorkersToArray(nsTArray
<WorkerPrivate
*>& aWorkers
)
183 MOZ_REQUIRES(mMutex
);
185 nsTArray
<WorkerPrivate
*> GetWorkersForWindow(
186 const nsPIDOMWindowInner
& aWindow
) const;
188 bool ScheduleWorker(WorkerPrivate
& aWorkerPrivate
);
190 template <typename Func
>
191 void BroadcastAllWorkers(const Func
& aFunc
);
194 } // namespace workerinternals
195 } // namespace mozilla::dom
197 #endif /* mozilla_dom_workers_runtimeservice_h__ */