Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_dom_workers_runtimeservice_h__ |
michael@0 | 8 | #define mozilla_dom_workers_runtimeservice_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "Workers.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsIObserver.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "mozilla/TimeStamp.h" |
michael@0 | 15 | #include "mozilla/dom/BindingDeclarations.h" |
michael@0 | 16 | #include "nsClassHashtable.h" |
michael@0 | 17 | #include "nsHashKeys.h" |
michael@0 | 18 | #include "nsTArray.h" |
michael@0 | 19 | |
michael@0 | 20 | class nsIRunnable; |
michael@0 | 21 | class nsIThread; |
michael@0 | 22 | class nsITimer; |
michael@0 | 23 | class nsPIDOMWindow; |
michael@0 | 24 | |
michael@0 | 25 | BEGIN_WORKERS_NAMESPACE |
michael@0 | 26 | |
michael@0 | 27 | class SharedWorker; |
michael@0 | 28 | class WorkerPrivate; |
michael@0 | 29 | |
michael@0 | 30 | class RuntimeService MOZ_FINAL : public nsIObserver |
michael@0 | 31 | { |
michael@0 | 32 | public: |
michael@0 | 33 | class WorkerThread; |
michael@0 | 34 | |
michael@0 | 35 | private: |
michael@0 | 36 | struct SharedWorkerInfo |
michael@0 | 37 | { |
michael@0 | 38 | WorkerPrivate* mWorkerPrivate; |
michael@0 | 39 | nsCString mScriptSpec; |
michael@0 | 40 | nsCString mName; |
michael@0 | 41 | |
michael@0 | 42 | SharedWorkerInfo(WorkerPrivate* aWorkerPrivate, |
michael@0 | 43 | const nsACString& aScriptSpec, |
michael@0 | 44 | const nsACString& aName) |
michael@0 | 45 | : mWorkerPrivate(aWorkerPrivate), mScriptSpec(aScriptSpec), mName(aName) |
michael@0 | 46 | { } |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | struct WorkerDomainInfo |
michael@0 | 50 | { |
michael@0 | 51 | nsCString mDomain; |
michael@0 | 52 | nsTArray<WorkerPrivate*> mActiveWorkers; |
michael@0 | 53 | nsTArray<WorkerPrivate*> mQueuedWorkers; |
michael@0 | 54 | nsClassHashtable<nsCStringHashKey, SharedWorkerInfo> mSharedWorkerInfos; |
michael@0 | 55 | uint32_t mChildWorkerCount; |
michael@0 | 56 | |
michael@0 | 57 | WorkerDomainInfo() |
michael@0 | 58 | : mActiveWorkers(1), mChildWorkerCount(0) |
michael@0 | 59 | { } |
michael@0 | 60 | |
michael@0 | 61 | uint32_t |
michael@0 | 62 | ActiveWorkerCount() const |
michael@0 | 63 | { |
michael@0 | 64 | return mActiveWorkers.Length() + mChildWorkerCount; |
michael@0 | 65 | } |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | struct IdleThreadInfo |
michael@0 | 69 | { |
michael@0 | 70 | nsRefPtr<WorkerThread> mThread; |
michael@0 | 71 | mozilla::TimeStamp mExpirationTime; |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | struct MatchSharedWorkerInfo |
michael@0 | 75 | { |
michael@0 | 76 | WorkerPrivate* mWorkerPrivate; |
michael@0 | 77 | SharedWorkerInfo* mSharedWorkerInfo; |
michael@0 | 78 | |
michael@0 | 79 | MatchSharedWorkerInfo(WorkerPrivate* aWorkerPrivate) |
michael@0 | 80 | : mWorkerPrivate(aWorkerPrivate), mSharedWorkerInfo(nullptr) |
michael@0 | 81 | { } |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | mozilla::Mutex mMutex; |
michael@0 | 85 | |
michael@0 | 86 | // Protected by mMutex. |
michael@0 | 87 | nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap; |
michael@0 | 88 | |
michael@0 | 89 | // Protected by mMutex. |
michael@0 | 90 | nsTArray<IdleThreadInfo> mIdleThreadArray; |
michael@0 | 91 | |
michael@0 | 92 | // *Not* protected by mMutex. |
michael@0 | 93 | nsClassHashtable<nsPtrHashKey<nsPIDOMWindow>, |
michael@0 | 94 | nsTArray<WorkerPrivate*> > mWindowMap; |
michael@0 | 95 | |
michael@0 | 96 | // Only used on the main thread. |
michael@0 | 97 | nsCOMPtr<nsITimer> mIdleThreadTimer; |
michael@0 | 98 | |
michael@0 | 99 | static JSSettings sDefaultJSSettings; |
michael@0 | 100 | static bool sDefaultPreferences[WORKERPREF_COUNT]; |
michael@0 | 101 | |
michael@0 | 102 | public: |
michael@0 | 103 | struct NavigatorProperties |
michael@0 | 104 | { |
michael@0 | 105 | nsString mAppName; |
michael@0 | 106 | nsString mAppNameOverridden; |
michael@0 | 107 | nsString mAppVersion; |
michael@0 | 108 | nsString mAppVersionOverridden; |
michael@0 | 109 | nsString mPlatform; |
michael@0 | 110 | nsString mPlatformOverridden; |
michael@0 | 111 | nsString mUserAgent; |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | private: |
michael@0 | 115 | NavigatorProperties mNavigatorProperties; |
michael@0 | 116 | |
michael@0 | 117 | // True when the observer service holds a reference to this object. |
michael@0 | 118 | bool mObserved; |
michael@0 | 119 | bool mShuttingDown; |
michael@0 | 120 | bool mNavigatorPropertiesLoaded; |
michael@0 | 121 | |
michael@0 | 122 | public: |
michael@0 | 123 | NS_DECL_ISUPPORTS |
michael@0 | 124 | NS_DECL_NSIOBSERVER |
michael@0 | 125 | |
michael@0 | 126 | static RuntimeService* |
michael@0 | 127 | GetOrCreateService(); |
michael@0 | 128 | |
michael@0 | 129 | static RuntimeService* |
michael@0 | 130 | GetService(); |
michael@0 | 131 | |
michael@0 | 132 | bool |
michael@0 | 133 | RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate); |
michael@0 | 134 | |
michael@0 | 135 | void |
michael@0 | 136 | UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate); |
michael@0 | 137 | |
michael@0 | 138 | void |
michael@0 | 139 | CancelWorkersForWindow(nsPIDOMWindow* aWindow); |
michael@0 | 140 | |
michael@0 | 141 | void |
michael@0 | 142 | SuspendWorkersForWindow(nsPIDOMWindow* aWindow); |
michael@0 | 143 | |
michael@0 | 144 | void |
michael@0 | 145 | ResumeWorkersForWindow(nsPIDOMWindow* aWindow); |
michael@0 | 146 | |
michael@0 | 147 | nsresult |
michael@0 | 148 | CreateSharedWorker(const GlobalObject& aGlobal, |
michael@0 | 149 | const nsAString& aScriptURL, |
michael@0 | 150 | const nsACString& aName, |
michael@0 | 151 | SharedWorker** aSharedWorker); |
michael@0 | 152 | |
michael@0 | 153 | void |
michael@0 | 154 | ForgetSharedWorker(WorkerPrivate* aWorkerPrivate); |
michael@0 | 155 | |
michael@0 | 156 | const NavigatorProperties& |
michael@0 | 157 | GetNavigatorProperties() const |
michael@0 | 158 | { |
michael@0 | 159 | return mNavigatorProperties; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | void |
michael@0 | 163 | NoteIdleThread(WorkerThread* aThread); |
michael@0 | 164 | |
michael@0 | 165 | static void |
michael@0 | 166 | GetDefaultJSSettings(JSSettings& aSettings) |
michael@0 | 167 | { |
michael@0 | 168 | AssertIsOnMainThread(); |
michael@0 | 169 | aSettings = sDefaultJSSettings; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | static void |
michael@0 | 173 | GetDefaultPreferences(bool aPreferences[WORKERPREF_COUNT]) |
michael@0 | 174 | { |
michael@0 | 175 | AssertIsOnMainThread(); |
michael@0 | 176 | memcpy(aPreferences, sDefaultPreferences, WORKERPREF_COUNT * sizeof(bool)); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | static void |
michael@0 | 180 | SetDefaultRuntimeAndContextOptions( |
michael@0 | 181 | const JS::RuntimeOptions& aRuntimeOptions, |
michael@0 | 182 | const JS::ContextOptions& aContentCxOptions, |
michael@0 | 183 | const JS::ContextOptions& aChromeCxOptions) |
michael@0 | 184 | { |
michael@0 | 185 | AssertIsOnMainThread(); |
michael@0 | 186 | sDefaultJSSettings.runtimeOptions = aRuntimeOptions; |
michael@0 | 187 | sDefaultJSSettings.content.contextOptions = aContentCxOptions; |
michael@0 | 188 | sDefaultJSSettings.chrome.contextOptions = aChromeCxOptions; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | void |
michael@0 | 192 | UpdateAppNameOverridePreference(const nsAString& aValue); |
michael@0 | 193 | |
michael@0 | 194 | void |
michael@0 | 195 | UpdateAppVersionOverridePreference(const nsAString& aValue); |
michael@0 | 196 | |
michael@0 | 197 | void |
michael@0 | 198 | UpdatePlatformOverridePreference(const nsAString& aValue); |
michael@0 | 199 | |
michael@0 | 200 | void |
michael@0 | 201 | UpdateAllWorkerRuntimeAndContextOptions(); |
michael@0 | 202 | |
michael@0 | 203 | void |
michael@0 | 204 | UpdateAllWorkerPreference(WorkerPreference aPref, bool aValue); |
michael@0 | 205 | |
michael@0 | 206 | static void |
michael@0 | 207 | SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue) |
michael@0 | 208 | { |
michael@0 | 209 | AssertIsOnMainThread(); |
michael@0 | 210 | sDefaultJSSettings.ApplyGCSetting(aKey, aValue); |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | void |
michael@0 | 214 | UpdateAllWorkerMemoryParameter(JSGCParamKey aKey, uint32_t aValue); |
michael@0 | 215 | |
michael@0 | 216 | static uint32_t |
michael@0 | 217 | GetContentCloseHandlerTimeoutSeconds() |
michael@0 | 218 | { |
michael@0 | 219 | return sDefaultJSSettings.content.maxScriptRuntime; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | static uint32_t |
michael@0 | 223 | GetChromeCloseHandlerTimeoutSeconds() |
michael@0 | 224 | { |
michael@0 | 225 | return sDefaultJSSettings.chrome.maxScriptRuntime; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | #ifdef JS_GC_ZEAL |
michael@0 | 229 | static void |
michael@0 | 230 | SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency) |
michael@0 | 231 | { |
michael@0 | 232 | AssertIsOnMainThread(); |
michael@0 | 233 | sDefaultJSSettings.gcZeal = aGCZeal; |
michael@0 | 234 | sDefaultJSSettings.gcZealFrequency = aFrequency; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | void |
michael@0 | 238 | UpdateAllWorkerGCZeal(); |
michael@0 | 239 | #endif |
michael@0 | 240 | |
michael@0 | 241 | void |
michael@0 | 242 | GarbageCollectAllWorkers(bool aShrinking); |
michael@0 | 243 | |
michael@0 | 244 | void |
michael@0 | 245 | CycleCollectAllWorkers(); |
michael@0 | 246 | |
michael@0 | 247 | void |
michael@0 | 248 | SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline); |
michael@0 | 249 | |
michael@0 | 250 | private: |
michael@0 | 251 | RuntimeService(); |
michael@0 | 252 | ~RuntimeService(); |
michael@0 | 253 | |
michael@0 | 254 | nsresult |
michael@0 | 255 | Init(); |
michael@0 | 256 | |
michael@0 | 257 | void |
michael@0 | 258 | Shutdown(); |
michael@0 | 259 | |
michael@0 | 260 | void |
michael@0 | 261 | Cleanup(); |
michael@0 | 262 | |
michael@0 | 263 | static PLDHashOperator |
michael@0 | 264 | AddAllTopLevelWorkersToArray(const nsACString& aKey, |
michael@0 | 265 | WorkerDomainInfo* aData, |
michael@0 | 266 | void* aUserArg); |
michael@0 | 267 | |
michael@0 | 268 | static PLDHashOperator |
michael@0 | 269 | RemoveSharedWorkerFromWindowMap(nsPIDOMWindow* aKey, |
michael@0 | 270 | nsAutoPtr<nsTArray<WorkerPrivate*> >& aData, |
michael@0 | 271 | void* aUserArg); |
michael@0 | 272 | |
michael@0 | 273 | static PLDHashOperator |
michael@0 | 274 | FindSharedWorkerInfo(const nsACString& aKey, |
michael@0 | 275 | SharedWorkerInfo* aData, |
michael@0 | 276 | void* aUserArg); |
michael@0 | 277 | |
michael@0 | 278 | void |
michael@0 | 279 | GetWorkersForWindow(nsPIDOMWindow* aWindow, |
michael@0 | 280 | nsTArray<WorkerPrivate*>& aWorkers); |
michael@0 | 281 | |
michael@0 | 282 | bool |
michael@0 | 283 | ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate); |
michael@0 | 284 | |
michael@0 | 285 | static void |
michael@0 | 286 | ShutdownIdleThreads(nsITimer* aTimer, void* aClosure); |
michael@0 | 287 | |
michael@0 | 288 | static void |
michael@0 | 289 | WorkerPrefChanged(const char* aPrefName, void* aClosure); |
michael@0 | 290 | |
michael@0 | 291 | static void |
michael@0 | 292 | JSVersionChanged(const char* aPrefName, void* aClosure); |
michael@0 | 293 | }; |
michael@0 | 294 | |
michael@0 | 295 | END_WORKERS_NAMESPACE |
michael@0 | 296 | |
michael@0 | 297 | #endif /* mozilla_dom_workers_runtimeservice_h__ */ |