2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_dom_workers_runtimeservice_h__
|
|
|
|
#define mozilla_dom_workers_runtimeservice_h__
|
|
|
|
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
2019-01-24 03:56:56 +03:00
|
|
|
#include "js/ContextOptions.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2018-01-31 10:24:59 +03:00
|
|
|
#include "mozilla/dom/workerinternals/JSSettings.h"
|
2018-02-02 19:35:28 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
class nsITimer;
|
2016-01-30 20:05:36 +03:00
|
|
|
class nsPIDOMWindowInner;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2018-01-31 10:19:10 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2018-01-31 10:21:46 +03:00
|
|
|
struct WorkerLoadInfo;
|
2018-01-31 10:20:07 +03:00
|
|
|
class WorkerThread;
|
2018-01-31 10:19:10 +03:00
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
namespace workerinternals {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class RuntimeService final : public nsIObserver {
|
2011-07-17 23:09:13 +04:00
|
|
|
struct WorkerDomainInfo {
|
|
|
|
nsCString mDomain;
|
2016-08-19 09:38:58 +03:00
|
|
|
nsTArray<WorkerPrivate*> mActiveWorkers;
|
|
|
|
nsTArray<WorkerPrivate*> mActiveServiceWorkers;
|
|
|
|
nsTArray<WorkerPrivate*> mQueuedWorkers;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mChildWorkerCount;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
WorkerDomainInfo() : mActiveWorkers(1), mChildWorkerCount(0) {}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-08-19 09:38:58 +03:00
|
|
|
uint32_t ActiveWorkerCount() const {
|
|
|
|
return mActiveWorkers.Length() + mChildWorkerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ActiveServiceWorkerCount() const {
|
|
|
|
return mActiveServiceWorkers.Length();
|
|
|
|
}
|
|
|
|
|
2015-07-02 00:32:52 +03:00
|
|
|
bool HasNoWorkers() const {
|
2016-08-19 09:38:58 +03:00
|
|
|
return ActiveWorkerCount() == 0 && ActiveServiceWorkerCount() == 0;
|
2015-07-02 00:32:52 +03:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2014-11-15 05:47:30 +03:00
|
|
|
struct IdleThreadInfo;
|
2011-07-26 05:49:16 +04:00
|
|
|
|
|
|
|
mozilla::Mutex mMutex;
|
|
|
|
|
|
|
|
// Protected by mMutex.
|
2011-07-17 23:09:13 +04:00
|
|
|
nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
// Protected by mMutex.
|
|
|
|
nsTArray<IdleThreadInfo> mIdleThreadArray;
|
|
|
|
|
|
|
|
// *Not* protected by mMutex.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsClassHashtable<nsPtrHashKey<nsPIDOMWindowInner>, nsTArray<WorkerPrivate*> >
|
2013-06-05 18:04:23 +04:00
|
|
|
mWindowMap;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
// Only used on the main thread.
|
|
|
|
nsCOMPtr<nsITimer> mIdleThreadTimer;
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
static workerinternals::JSSettings sDefaultJSSettings;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
2013-11-20 03:08:50 +04:00
|
|
|
struct NavigatorProperties {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsString mAppName;
|
2014-09-23 23:11:18 +04:00
|
|
|
nsString mAppNameOverridden;
|
2011-07-17 23:09:13 +04:00
|
|
|
nsString mAppVersion;
|
2014-09-23 23:11:18 +04:00
|
|
|
nsString mAppVersionOverridden;
|
2011-07-17 23:09:13 +04:00
|
|
|
nsString mPlatform;
|
2014-09-23 23:11:18 +04:00
|
|
|
nsString mPlatformOverridden;
|
2014-09-05 18:26:34 +04:00
|
|
|
nsTArray<nsString> mLanguages;
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-11-20 03:08:50 +04:00
|
|
|
NavigatorProperties mNavigatorProperties;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// True when the observer service holds a reference to this object.
|
|
|
|
bool mObserved;
|
|
|
|
bool mShuttingDown;
|
2013-11-20 03:08:50 +04:00
|
|
|
bool mNavigatorPropertiesLoaded;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
static RuntimeService* GetOrCreateService();
|
|
|
|
|
|
|
|
static RuntimeService* GetService();
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
bool RegisterWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
void UnregisterWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void CancelWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void ThawWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2015-10-07 13:20:59 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2015-10-07 13:20:59 +03:00
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
void PropagateFirstPartyStorageAccessGranted(nsPIDOMWindowInner* aWindow);
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
const NavigatorProperties& GetNavigatorProperties() const {
|
|
|
|
return mNavigatorProperties;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
void NoteIdleThread(WorkerThread* aThread);
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
static void GetDefaultJSSettings(workerinternals::JSSettings& aSettings) {
|
2018-02-05 21:55:07 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
aSettings = sDefaultJSSettings;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
static void SetDefaultContextOptions(
|
|
|
|
const JS::ContextOptions& aContextOptions) {
|
2018-02-05 21:55:07 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-07-07 09:15:15 +03:00
|
|
|
sDefaultJSSettings.contextOptions = aContextOptions;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:11:18 +04:00
|
|
|
void UpdateAppNameOverridePreference(const nsAString& aValue);
|
|
|
|
|
|
|
|
void UpdateAppVersionOverridePreference(const nsAString& aValue);
|
|
|
|
|
|
|
|
void UpdatePlatformOverridePreference(const nsAString& aValue);
|
|
|
|
|
2016-07-07 09:15:15 +03:00
|
|
|
void UpdateAllWorkerContextOptions();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
void UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
static void SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue) {
|
2018-02-05 21:55:07 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
sDefaultJSSettings.ApplyGCSetting(aKey, aValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
void UpdateAllWorkerMemoryParameter(JSGCParamKey aKey, uint32_t aValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
static void SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency) {
|
2018-02-05 21:55:07 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
sDefaultJSSettings.gcZeal = aGCZeal;
|
|
|
|
sDefaultJSSettings.gcZealFrequency = aFrequency;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateAllWorkerGCZeal();
|
|
|
|
#endif
|
|
|
|
|
2019-06-24 20:24:47 +03:00
|
|
|
void SetLowMemoryStateAllWorkers(bool aState);
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
void GarbageCollectAllWorkers(bool aShrinking);
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
void CycleCollectAllWorkers();
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
void SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void MemoryPressureAllWorkers();
|
|
|
|
|
2016-03-16 22:41:38 +03:00
|
|
|
uint32_t ClampedHardwareConcurrency() const;
|
|
|
|
|
2017-11-15 09:58:38 +03:00
|
|
|
void CrashIfHanging();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
private:
|
|
|
|
RuntimeService();
|
|
|
|
~RuntimeService();
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2013-09-09 21:54:05 +04:00
|
|
|
void Shutdown();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void Cleanup();
|
|
|
|
|
2015-10-27 02:41:55 +03:00
|
|
|
void AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
|
2011-07-17 23:09:13 +04:00
|
|
|
nsTArray<WorkerPrivate*>& aWorkers);
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
bool ScheduleWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-26 05:49:16 +04:00
|
|
|
|
|
|
|
static void ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
} // namespace workerinternals
|
2018-01-31 10:24:30 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_runtimeservice_h__ */
|