Bug 1655413 [wpt PR 24763] - Make CSP default-src without 'unsafe-eval' block eval...
[gecko.git] / dom / workers / RuntimeService.h
blob67eac10dabd93da344a1366dee5d1c4428a4352c
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/Mutex.h"
20 #include "nsClassHashtable.h"
21 #include "nsHashKeys.h"
22 #include "nsTArray.h"
24 class nsITimer;
25 class nsPIDOMWindowInner;
27 namespace mozilla {
28 namespace 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 struct IdleThreadInfo {
58 SafeRefPtr<WorkerThread> mThread;
59 mozilla::TimeStamp mExpirationTime;
62 mozilla::Mutex mMutex;
64 // Protected by mMutex.
65 nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
67 // Protected by mMutex.
68 nsTArray<IdleThreadInfo> mIdleThreadArray;
70 // *Not* protected by mMutex.
71 nsClassHashtable<nsPtrHashKey<const nsPIDOMWindowInner>,
72 nsTArray<WorkerPrivate*> >
73 mWindowMap;
75 // Only used on the main thread.
76 nsCOMPtr<nsITimer> mIdleThreadTimer;
78 static UniquePtr<workerinternals::JSSettings> sDefaultJSSettings;
80 public:
81 struct NavigatorProperties {
82 nsString mAppName;
83 nsString mAppNameOverridden;
84 nsString mAppVersion;
85 nsString mAppVersionOverridden;
86 nsString mPlatform;
87 nsString mPlatformOverridden;
88 CopyableTArray<nsString> mLanguages;
91 private:
92 NavigatorProperties mNavigatorProperties;
94 // True when the observer service holds a reference to this object.
95 bool mObserved;
96 bool mShuttingDown;
97 bool mNavigatorPropertiesLoaded;
99 public:
100 NS_DECL_ISUPPORTS
101 NS_DECL_NSIOBSERVER
103 static RuntimeService* GetOrCreateService();
105 static RuntimeService* GetService();
107 bool RegisterWorker(WorkerPrivate& aWorkerPrivate);
109 void UnregisterWorker(WorkerPrivate& aWorkerPrivate);
111 void CancelWorkersForWindow(const nsPIDOMWindowInner& aWindow);
113 void FreezeWorkersForWindow(const nsPIDOMWindowInner& aWindow);
115 void ThawWorkersForWindow(const nsPIDOMWindowInner& aWindow);
117 void SuspendWorkersForWindow(const nsPIDOMWindowInner& aWindow);
119 void ResumeWorkersForWindow(const nsPIDOMWindowInner& aWindow);
121 void PropagateStorageAccessPermissionGranted(
122 const nsPIDOMWindowInner& aWindow);
124 const NavigatorProperties& GetNavigatorProperties() const {
125 return mNavigatorProperties;
128 void NoteIdleThread(SafeRefPtr<WorkerThread> aThread);
130 static void GetDefaultJSSettings(workerinternals::JSSettings& aSettings) {
131 AssertIsOnMainThread();
132 aSettings = *sDefaultJSSettings;
135 static void SetDefaultContextOptions(
136 const JS::ContextOptions& aContextOptions) {
137 AssertIsOnMainThread();
138 sDefaultJSSettings->contextOptions = aContextOptions;
141 void UpdateAppNameOverridePreference(const nsAString& aValue);
143 void UpdateAppVersionOverridePreference(const nsAString& aValue);
145 void UpdatePlatformOverridePreference(const nsAString& aValue);
147 void UpdateAllWorkerContextOptions();
149 void UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
151 static void SetDefaultJSGCSettings(JSGCParamKey aKey,
152 Maybe<uint32_t> aValue) {
153 AssertIsOnMainThread();
154 sDefaultJSSettings->ApplyGCSetting(aKey, aValue);
157 void UpdateAllWorkerMemoryParameter(JSGCParamKey aKey,
158 Maybe<uint32_t> aValue);
160 #ifdef JS_GC_ZEAL
161 static void SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency) {
162 AssertIsOnMainThread();
163 sDefaultJSSettings->gcZeal = aGCZeal;
164 sDefaultJSSettings->gcZealFrequency = aFrequency;
167 void UpdateAllWorkerGCZeal();
168 #endif
170 void SetLowMemoryStateAllWorkers(bool aState);
172 void GarbageCollectAllWorkers(bool aShrinking);
174 void CycleCollectAllWorkers();
176 void SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
178 void MemoryPressureAllWorkers();
180 uint32_t ClampedHardwareConcurrency() const;
182 void CrashIfHanging();
184 private:
185 RuntimeService();
186 ~RuntimeService();
188 nsresult Init();
190 void Shutdown();
192 void Cleanup();
194 void AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers);
196 nsTArray<WorkerPrivate*> GetWorkersForWindow(
197 const nsPIDOMWindowInner& aWindow) const;
199 bool ScheduleWorker(WorkerPrivate& aWorkerPrivate);
201 static void ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
203 template <typename Func>
204 void BroadcastAllWorkers(const Func& aFunc);
207 } // namespace workerinternals
208 } // namespace dom
209 } // namespace mozilla
211 #endif /* mozilla_dom_workers_runtimeservice_h__ */