Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/workers/RuntimeService.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef mozilla_dom_workers_runtimeservice_h__
8
#define mozilla_dom_workers_runtimeservice_h__
9
10
#include "mozilla/dom/WorkerCommon.h"
11
12
#include "nsIObserver.h"
13
14
#include "mozilla/dom/BindingDeclarations.h"
15
#include "mozilla/dom/workerinternals/JSSettings.h"
16
#include "mozilla/Mutex.h"
17
#include "nsClassHashtable.h"
18
#include "nsHashKeys.h"
19
#include "nsTArray.h"
20
21
class nsITimer;
22
class nsPIDOMWindowInner;
23
24
namespace mozilla {
25
namespace dom {
26
class SharedWorker;
27
struct WorkerLoadInfo;
28
class WorkerThread;
29
30
namespace workerinternals {
31
32
class RuntimeService final : public nsIObserver
33
{
34
  struct SharedWorkerInfo
35
  {
36
    WorkerPrivate* mWorkerPrivate;
37
    nsCString mScriptSpec;
38
    nsString mName;
39
40
    SharedWorkerInfo(WorkerPrivate* aWorkerPrivate,
41
                     const nsACString& aScriptSpec,
42
                     const nsAString& aName)
43
    : mWorkerPrivate(aWorkerPrivate), mScriptSpec(aScriptSpec), mName(aName)
44
0
    { }
45
  };
46
47
  struct WorkerDomainInfo
48
  {
49
    nsCString mDomain;
50
    nsTArray<WorkerPrivate*> mActiveWorkers;
51
    nsTArray<WorkerPrivate*> mActiveServiceWorkers;
52
    nsTArray<WorkerPrivate*> mQueuedWorkers;
53
    nsTArray<UniquePtr<SharedWorkerInfo>> mSharedWorkerInfos;
54
    uint32_t mChildWorkerCount;
55
56
    WorkerDomainInfo()
57
    : mActiveWorkers(1), mChildWorkerCount(0)
58
0
    { }
59
60
    uint32_t
61
    ActiveWorkerCount() const
62
0
    {
63
0
      return mActiveWorkers.Length() +
64
0
             mChildWorkerCount;
65
0
    }
66
67
    uint32_t
68
    ActiveServiceWorkerCount() const
69
0
    {
70
0
      return mActiveServiceWorkers.Length();
71
0
    }
72
73
    bool
74
    HasNoWorkers() const
75
0
    {
76
0
      return ActiveWorkerCount() == 0 &&
77
0
             ActiveServiceWorkerCount() == 0;
78
0
    }
79
  };
80
81
  struct IdleThreadInfo;
82
83
  mozilla::Mutex mMutex;
84
85
  // Protected by mMutex.
86
  nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
87
88
  // Protected by mMutex.
89
  nsTArray<IdleThreadInfo> mIdleThreadArray;
90
91
  // *Not* protected by mMutex.
92
  nsClassHashtable<nsPtrHashKey<nsPIDOMWindowInner>,
93
                   nsTArray<WorkerPrivate*> > mWindowMap;
94
95
  // Only used on the main thread.
96
  nsCOMPtr<nsITimer> mIdleThreadTimer;
97
98
  static workerinternals::JSSettings sDefaultJSSettings;
99
100
public:
101
  struct NavigatorProperties
102
  {
103
    nsString mAppName;
104
    nsString mAppNameOverridden;
105
    nsString mAppVersion;
106
    nsString mAppVersionOverridden;
107
    nsString mPlatform;
108
    nsString mPlatformOverridden;
109
    nsTArray<nsString> mLanguages;
110
  };
111
112
private:
113
  NavigatorProperties mNavigatorProperties;
114
115
  // True when the observer service holds a reference to this object.
116
  bool mObserved;
117
  bool mShuttingDown;
118
  bool mNavigatorPropertiesLoaded;
119
120
public:
121
  NS_DECL_ISUPPORTS
122
  NS_DECL_NSIOBSERVER
123
124
  static RuntimeService*
125
  GetOrCreateService();
126
127
  static RuntimeService*
128
  GetService();
129
130
  bool
131
  RegisterWorker(WorkerPrivate* aWorkerPrivate);
132
133
  void
134
  UnregisterWorker(WorkerPrivate* aWorkerPrivate);
135
136
  void
137
  RemoveSharedWorker(WorkerDomainInfo* aDomainInfo,
138
                     WorkerPrivate* aWorkerPrivate);
139
140
  void
141
  CancelWorkersForWindow(nsPIDOMWindowInner* aWindow);
142
143
  void
144
  FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow);
145
146
  void
147
  ThawWorkersForWindow(nsPIDOMWindowInner* aWindow);
148
149
  void
150
  SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow);
151
152
  void
153
  ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow);
154
155
  void
156
  PropagateFirstPartyStorageAccessGranted(nsPIDOMWindowInner* aWindow);
157
158
  nsresult
159
  CreateSharedWorker(const GlobalObject& aGlobal,
160
                     const nsAString& aScriptURL,
161
                     const nsAString& aName,
162
                     SharedWorker** aSharedWorker);
163
164
  void
165
  ForgetSharedWorker(WorkerPrivate* aWorkerPrivate);
166
167
  const NavigatorProperties&
168
  GetNavigatorProperties() const
169
0
  {
170
0
    return mNavigatorProperties;
171
0
  }
172
173
  void
174
  NoteIdleThread(WorkerThread* aThread);
175
176
  static void
177
  GetDefaultJSSettings(workerinternals::JSSettings& aSettings)
178
0
  {
179
0
    AssertIsOnMainThread();
180
0
    aSettings = sDefaultJSSettings;
181
0
  }
182
183
  static void
184
  SetDefaultContextOptions(const JS::ContextOptions& aContextOptions)
185
0
  {
186
0
    AssertIsOnMainThread();
187
0
    sDefaultJSSettings.contextOptions = aContextOptions;
188
0
  }
189
190
  void
191
  UpdateAppNameOverridePreference(const nsAString& aValue);
192
193
  void
194
  UpdateAppVersionOverridePreference(const nsAString& aValue);
195
196
  void
197
  UpdatePlatformOverridePreference(const nsAString& aValue);
198
199
  void
200
  UpdateAllWorkerContextOptions();
201
202
  void
203
  UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
204
205
  static void
206
  SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue)
207
0
  {
208
0
    AssertIsOnMainThread();
209
0
    sDefaultJSSettings.ApplyGCSetting(aKey, aValue);
210
0
  }
211
212
  void
213
  UpdateAllWorkerMemoryParameter(JSGCParamKey aKey, uint32_t aValue);
214
215
#ifdef JS_GC_ZEAL
216
  static void
217
  SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency)
218
  {
219
    AssertIsOnMainThread();
220
    sDefaultJSSettings.gcZeal = aGCZeal;
221
    sDefaultJSSettings.gcZealFrequency = aFrequency;
222
  }
223
224
  void
225
  UpdateAllWorkerGCZeal();
226
#endif
227
228
  void
229
  GarbageCollectAllWorkers(bool aShrinking);
230
231
  void
232
  CycleCollectAllWorkers();
233
234
  void
235
  SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
236
237
  void
238
  MemoryPressureAllWorkers();
239
240
  uint32_t ClampedHardwareConcurrency() const;
241
242
  void CrashIfHanging();
243
244
private:
245
  RuntimeService();
246
  ~RuntimeService();
247
248
  nsresult
249
  Init();
250
251
  void
252
  Shutdown();
253
254
  void
255
  Cleanup();
256
257
  void
258
  AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers);
259
260
  void
261
  GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
262
                      nsTArray<WorkerPrivate*>& aWorkers);
263
264
  bool
265
  ScheduleWorker(WorkerPrivate* aWorkerPrivate);
266
267
  static void
268
  ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
269
270
  nsresult
271
  CreateSharedWorkerFromLoadInfo(JSContext* aCx,
272
                                 WorkerLoadInfo* aLoadInfo,
273
                                 const nsAString& aScriptURL,
274
                                 const nsAString& aName,
275
                                 SharedWorker** aSharedWorker);
276
};
277
278
} // workerinternals namespace
279
} // dom namespace
280
} // mozilla namespace
281
282
#endif /* mozilla_dom_workers_runtimeservice_h__ */