RuntimeService.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_workers_runtimeservice_h__
  6. #define mozilla_dom_workers_runtimeservice_h__
  7. #include "Workers.h"
  8. #include "nsIObserver.h"
  9. #include "mozilla/dom/BindingDeclarations.h"
  10. #include "nsClassHashtable.h"
  11. #include "nsHashKeys.h"
  12. #include "nsTArray.h"
  13. class nsITimer;
  14. class nsPIDOMWindowInner;
  15. BEGIN_WORKERS_NAMESPACE
  16. class SharedWorker;
  17. class WorkerThread;
  18. class RuntimeService final : public nsIObserver
  19. {
  20. struct SharedWorkerInfo
  21. {
  22. WorkerPrivate* mWorkerPrivate;
  23. nsCString mScriptSpec;
  24. nsCString mName;
  25. SharedWorkerInfo(WorkerPrivate* aWorkerPrivate,
  26. const nsACString& aScriptSpec,
  27. const nsACString& aName)
  28. : mWorkerPrivate(aWorkerPrivate), mScriptSpec(aScriptSpec), mName(aName)
  29. { }
  30. };
  31. struct WorkerDomainInfo
  32. {
  33. nsCString mDomain;
  34. nsTArray<WorkerPrivate*> mActiveWorkers;
  35. nsTArray<WorkerPrivate*> mActiveServiceWorkers;
  36. nsTArray<WorkerPrivate*> mQueuedWorkers;
  37. nsClassHashtable<nsCStringHashKey, SharedWorkerInfo> mSharedWorkerInfos;
  38. uint32_t mChildWorkerCount;
  39. WorkerDomainInfo()
  40. : mActiveWorkers(1), mChildWorkerCount(0)
  41. { }
  42. uint32_t
  43. ActiveWorkerCount() const
  44. {
  45. return mActiveWorkers.Length() +
  46. mChildWorkerCount;
  47. }
  48. uint32_t
  49. ActiveServiceWorkerCount() const
  50. {
  51. return mActiveServiceWorkers.Length();
  52. }
  53. bool
  54. HasNoWorkers() const
  55. {
  56. return ActiveWorkerCount() == 0 &&
  57. ActiveServiceWorkerCount() == 0;
  58. }
  59. };
  60. struct IdleThreadInfo;
  61. mozilla::Mutex mMutex;
  62. // Protected by mMutex.
  63. nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
  64. // Protected by mMutex.
  65. nsTArray<IdleThreadInfo> mIdleThreadArray;
  66. // *Not* protected by mMutex.
  67. nsClassHashtable<nsPtrHashKey<nsPIDOMWindowInner>,
  68. nsTArray<WorkerPrivate*> > mWindowMap;
  69. // Only used on the main thread.
  70. nsCOMPtr<nsITimer> mIdleThreadTimer;
  71. static JSSettings sDefaultJSSettings;
  72. static bool sDefaultPreferences[WORKERPREF_COUNT];
  73. public:
  74. struct NavigatorProperties
  75. {
  76. nsString mAppName;
  77. nsString mAppNameOverridden;
  78. nsString mAppVersion;
  79. nsString mAppVersionOverridden;
  80. nsString mPlatform;
  81. nsString mPlatformOverridden;
  82. nsTArray<nsString> mLanguages;
  83. };
  84. private:
  85. NavigatorProperties mNavigatorProperties;
  86. // True when the observer service holds a reference to this object.
  87. bool mObserved;
  88. bool mShuttingDown;
  89. bool mNavigatorPropertiesLoaded;
  90. public:
  91. NS_DECL_ISUPPORTS
  92. NS_DECL_NSIOBSERVER
  93. static RuntimeService*
  94. GetOrCreateService();
  95. static RuntimeService*
  96. GetService();
  97. bool
  98. RegisterWorker(WorkerPrivate* aWorkerPrivate);
  99. void
  100. UnregisterWorker(WorkerPrivate* aWorkerPrivate);
  101. void
  102. RemoveSharedWorker(WorkerDomainInfo* aDomainInfo,
  103. WorkerPrivate* aWorkerPrivate);
  104. void
  105. CancelWorkersForWindow(nsPIDOMWindowInner* aWindow);
  106. void
  107. FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow);
  108. void
  109. ThawWorkersForWindow(nsPIDOMWindowInner* aWindow);
  110. void
  111. SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow);
  112. void
  113. ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow);
  114. nsresult
  115. CreateSharedWorker(const GlobalObject& aGlobal,
  116. const nsAString& aScriptURL,
  117. const nsACString& aName,
  118. SharedWorker** aSharedWorker);
  119. void
  120. ForgetSharedWorker(WorkerPrivate* aWorkerPrivate);
  121. const NavigatorProperties&
  122. GetNavigatorProperties() const
  123. {
  124. return mNavigatorProperties;
  125. }
  126. void
  127. NoteIdleThread(WorkerThread* aThread);
  128. static void
  129. GetDefaultJSSettings(JSSettings& aSettings)
  130. {
  131. AssertIsOnMainThread();
  132. aSettings = sDefaultJSSettings;
  133. }
  134. static void
  135. GetDefaultPreferences(bool aPreferences[WORKERPREF_COUNT])
  136. {
  137. AssertIsOnMainThread();
  138. memcpy(aPreferences, sDefaultPreferences, WORKERPREF_COUNT * sizeof(bool));
  139. }
  140. static void
  141. SetDefaultContextOptions(const JS::ContextOptions& aContextOptions)
  142. {
  143. AssertIsOnMainThread();
  144. sDefaultJSSettings.contextOptions = aContextOptions;
  145. }
  146. void
  147. UpdateAppNameOverridePreference(const nsAString& aValue);
  148. void
  149. UpdateAppVersionOverridePreference(const nsAString& aValue);
  150. void
  151. UpdatePlatformOverridePreference(const nsAString& aValue);
  152. void
  153. UpdateAllWorkerContextOptions();
  154. void
  155. UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
  156. void
  157. UpdateAllWorkerPreference(WorkerPreference aPref, bool aValue);
  158. static void
  159. SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue)
  160. {
  161. AssertIsOnMainThread();
  162. sDefaultJSSettings.ApplyGCSetting(aKey, aValue);
  163. }
  164. void
  165. UpdateAllWorkerMemoryParameter(JSGCParamKey aKey, uint32_t aValue);
  166. void
  167. GarbageCollectAllWorkers(bool aShrinking);
  168. void
  169. CycleCollectAllWorkers();
  170. void
  171. SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
  172. void
  173. MemoryPressureAllWorkers();
  174. uint32_t ClampedHardwareConcurrency() const;
  175. private:
  176. RuntimeService();
  177. ~RuntimeService();
  178. nsresult
  179. Init();
  180. void
  181. Shutdown();
  182. void
  183. Cleanup();
  184. void
  185. AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers);
  186. void
  187. GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
  188. nsTArray<WorkerPrivate*>& aWorkers);
  189. bool
  190. ScheduleWorker(WorkerPrivate* aWorkerPrivate);
  191. static void
  192. ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
  193. static void
  194. WorkerPrefChanged(const char* aPrefName, void* aClosure);
  195. static void
  196. JSVersionChanged(const char* aPrefName, void* aClosure);
  197. nsresult
  198. CreateSharedWorkerFromLoadInfo(JSContext* aCx,
  199. WorkerLoadInfo* aLoadInfo,
  200. const nsAString& aScriptURL,
  201. const nsACString& aName,
  202. SharedWorker** aSharedWorker);
  203. };
  204. END_WORKERS_NAMESPACE
  205. #endif /* mozilla_dom_workers_runtimeservice_h__ */