Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / workers / RuntimeService.h
blob534bbe9ec4f0261189eb3322c1229c1eb5d8802e
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"
24 #include "nsTArray.h"
26 class nsPIDOMWindowInner;
28 namespace mozilla::dom {
29 struct WorkerLoadInfo;
30 class WorkerThread;
32 namespace workerinternals {
34 class RuntimeService final : public nsIObserver {
35 struct WorkerDomainInfo {
36 nsCString mDomain;
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*> >
66 mWindowMap;
68 static StaticAutoPtr<workerinternals::JSSettings> sDefaultJSSettings;
70 public:
71 struct NavigatorProperties {
72 nsString mAppVersion;
73 nsString mAppVersionOverridden;
74 nsString mPlatform;
75 nsString mPlatformOverridden;
76 CopyableTArray<nsString> mLanguages;
79 private:
80 NavigatorProperties mNavigatorProperties;
82 // True when the observer service holds a reference to this object.
83 bool mObserved;
84 bool mShuttingDown;
85 bool mNavigatorPropertiesLoaded;
87 public:
88 NS_DECL_ISUPPORTS
89 NS_DECL_NSIOBSERVER
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 UpdateWorkersBackgroundState(const nsPIDOMWindowInner& aWindow,
102 bool aIsBackground);
104 void FreezeWorkersForWindow(const nsPIDOMWindowInner& aWindow);
106 void ThawWorkersForWindow(const nsPIDOMWindowInner& aWindow);
108 void SuspendWorkersForWindow(const nsPIDOMWindowInner& aWindow);
110 void ResumeWorkersForWindow(const nsPIDOMWindowInner& aWindow);
112 void PropagateStorageAccessPermissionGranted(
113 const nsPIDOMWindowInner& aWindow);
115 const NavigatorProperties& GetNavigatorProperties() const {
116 return mNavigatorProperties;
119 static void GetDefaultJSSettings(workerinternals::JSSettings& aSettings) {
120 AssertIsOnMainThread();
121 aSettings = *sDefaultJSSettings;
124 static void SetDefaultContextOptions(
125 const JS::ContextOptions& aContextOptions) {
126 AssertIsOnMainThread();
127 sDefaultJSSettings->contextOptions = aContextOptions;
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);
147 #ifdef JS_GC_ZEAL
148 static void SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency) {
149 AssertIsOnMainThread();
150 sDefaultJSSettings->gcZeal = aGCZeal;
151 sDefaultJSSettings->gcZealFrequency = aFrequency;
154 void UpdateAllWorkerGCZeal();
155 #endif
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();
175 private:
176 RuntimeService();
177 ~RuntimeService();
179 nsresult Init();
181 void Shutdown();
183 void Cleanup();
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__ */