123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- #ifndef mozilla_dom_workers_runtimeservice_h__
- #define mozilla_dom_workers_runtimeservice_h__
- #include "Workers.h"
- #include "nsIObserver.h"
- #include "mozilla/dom/BindingDeclarations.h"
- #include "nsClassHashtable.h"
- #include "nsHashKeys.h"
- #include "nsTArray.h"
- class nsITimer;
- class nsPIDOMWindowInner;
- BEGIN_WORKERS_NAMESPACE
- class SharedWorker;
- class WorkerThread;
- class RuntimeService final : public nsIObserver
- {
- struct SharedWorkerInfo
- {
- WorkerPrivate* mWorkerPrivate;
- nsCString mScriptSpec;
- nsCString mName;
- SharedWorkerInfo(WorkerPrivate* aWorkerPrivate,
- const nsACString& aScriptSpec,
- const nsACString& aName)
- : mWorkerPrivate(aWorkerPrivate), mScriptSpec(aScriptSpec), mName(aName)
- { }
- };
- struct WorkerDomainInfo
- {
- nsCString mDomain;
- nsTArray<WorkerPrivate*> mActiveWorkers;
- nsTArray<WorkerPrivate*> mActiveServiceWorkers;
- nsTArray<WorkerPrivate*> mQueuedWorkers;
- nsClassHashtable<nsCStringHashKey, SharedWorkerInfo> mSharedWorkerInfos;
- uint32_t mChildWorkerCount;
- WorkerDomainInfo()
- : mActiveWorkers(1), mChildWorkerCount(0)
- { }
- uint32_t
- ActiveWorkerCount() const
- {
- return mActiveWorkers.Length() +
- mChildWorkerCount;
- }
- uint32_t
- ActiveServiceWorkerCount() const
- {
- return mActiveServiceWorkers.Length();
- }
- bool
- HasNoWorkers() const
- {
- return ActiveWorkerCount() == 0 &&
- ActiveServiceWorkerCount() == 0;
- }
- };
- struct IdleThreadInfo;
- mozilla::Mutex mMutex;
- // Protected by mMutex.
- nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
- // Protected by mMutex.
- nsTArray<IdleThreadInfo> mIdleThreadArray;
- // *Not* protected by mMutex.
- nsClassHashtable<nsPtrHashKey<nsPIDOMWindowInner>,
- nsTArray<WorkerPrivate*> > mWindowMap;
- // Only used on the main thread.
- nsCOMPtr<nsITimer> mIdleThreadTimer;
- static JSSettings sDefaultJSSettings;
- static bool sDefaultPreferences[WORKERPREF_COUNT];
- public:
- struct NavigatorProperties
- {
- nsString mAppName;
- nsString mAppNameOverridden;
- nsString mAppVersion;
- nsString mAppVersionOverridden;
- nsString mPlatform;
- nsString mPlatformOverridden;
- nsTArray<nsString> mLanguages;
- };
- private:
- NavigatorProperties mNavigatorProperties;
- // True when the observer service holds a reference to this object.
- bool mObserved;
- bool mShuttingDown;
- bool mNavigatorPropertiesLoaded;
- public:
- NS_DECL_ISUPPORTS
- NS_DECL_NSIOBSERVER
- static RuntimeService*
- GetOrCreateService();
- static RuntimeService*
- GetService();
- bool
- RegisterWorker(WorkerPrivate* aWorkerPrivate);
- void
- UnregisterWorker(WorkerPrivate* aWorkerPrivate);
- void
- RemoveSharedWorker(WorkerDomainInfo* aDomainInfo,
- WorkerPrivate* aWorkerPrivate);
- void
- CancelWorkersForWindow(nsPIDOMWindowInner* aWindow);
- void
- FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow);
- void
- ThawWorkersForWindow(nsPIDOMWindowInner* aWindow);
- void
- SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow);
- void
- ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow);
- nsresult
- CreateSharedWorker(const GlobalObject& aGlobal,
- const nsAString& aScriptURL,
- const nsACString& aName,
- SharedWorker** aSharedWorker);
- void
- ForgetSharedWorker(WorkerPrivate* aWorkerPrivate);
- const NavigatorProperties&
- GetNavigatorProperties() const
- {
- return mNavigatorProperties;
- }
- void
- NoteIdleThread(WorkerThread* aThread);
- static void
- GetDefaultJSSettings(JSSettings& aSettings)
- {
- AssertIsOnMainThread();
- aSettings = sDefaultJSSettings;
- }
- static void
- GetDefaultPreferences(bool aPreferences[WORKERPREF_COUNT])
- {
- AssertIsOnMainThread();
- memcpy(aPreferences, sDefaultPreferences, WORKERPREF_COUNT * sizeof(bool));
- }
- static void
- SetDefaultContextOptions(const JS::ContextOptions& aContextOptions)
- {
- AssertIsOnMainThread();
- sDefaultJSSettings.contextOptions = aContextOptions;
- }
- void
- UpdateAppNameOverridePreference(const nsAString& aValue);
- void
- UpdateAppVersionOverridePreference(const nsAString& aValue);
- void
- UpdatePlatformOverridePreference(const nsAString& aValue);
- void
- UpdateAllWorkerContextOptions();
- void
- UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
- void
- UpdateAllWorkerPreference(WorkerPreference aPref, bool aValue);
- static void
- SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue)
- {
- AssertIsOnMainThread();
- sDefaultJSSettings.ApplyGCSetting(aKey, aValue);
- }
- void
- UpdateAllWorkerMemoryParameter(JSGCParamKey aKey, uint32_t aValue);
- void
- GarbageCollectAllWorkers(bool aShrinking);
- void
- CycleCollectAllWorkers();
- void
- SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
- void
- MemoryPressureAllWorkers();
- uint32_t ClampedHardwareConcurrency() const;
- private:
- RuntimeService();
- ~RuntimeService();
- nsresult
- Init();
- void
- Shutdown();
- void
- Cleanup();
- void
- AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers);
- void
- GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
- nsTArray<WorkerPrivate*>& aWorkers);
- bool
- ScheduleWorker(WorkerPrivate* aWorkerPrivate);
- static void
- ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
- static void
- WorkerPrefChanged(const char* aPrefName, void* aClosure);
- static void
- JSVersionChanged(const char* aPrefName, void* aClosure);
- nsresult
- CreateSharedWorkerFromLoadInfo(JSContext* aCx,
- WorkerLoadInfo* aLoadInfo,
- const nsAString& aScriptURL,
- const nsACString& aName,
- SharedWorker** aSharedWorker);
- };
- END_WORKERS_NAMESPACE
- #endif /* mozilla_dom_workers_runtimeservice_h__ */
|