2015-05-03 19:32:37 +00: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 11:12:37 +00: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 19:09:13 +00:00
|
|
|
|
|
|
|
#ifndef mozilla_dom_workers_runtimeservice_h__
|
|
|
|
#define mozilla_dom_workers_runtimeservice_h__
|
|
|
|
|
2018-01-31 07:25:30 +00:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
2019-01-24 00:56:56 +00:00
|
|
|
#include "js/ContextOptions.h"
|
2020-07-15 12:45:12 +00:00
|
|
|
#include "MainThreadUtils.h"
|
2013-06-05 14:04:23 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2020-06-02 08:53:52 +00:00
|
|
|
#include "mozilla/dom/SafeRefPtr.h"
|
2018-01-31 07:24:59 +00:00
|
|
|
#include "mozilla/dom/workerinternals/JSSettings.h"
|
2021-03-02 09:57:41 +00:00
|
|
|
#include "mozilla/Atomics.h"
|
2018-02-02 16:35:28 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
2024-01-23 19:00:49 +00:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
class nsPIDOMWindowInner;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2022-05-09 20:41:20 +00:00
|
|
|
namespace mozilla::dom {
|
2018-01-31 07:21:46 +00:00
|
|
|
struct WorkerLoadInfo;
|
2018-01-31 07:20:07 +00:00
|
|
|
class WorkerThread;
|
2018-01-31 07:19:10 +00:00
|
|
|
|
2018-01-31 07:24:59 +00:00
|
|
|
namespace workerinternals {
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class RuntimeService final : public nsIObserver {
|
2011-07-17 19:09:13 +00:00
|
|
|
struct WorkerDomainInfo {
|
|
|
|
nsCString mDomain;
|
2016-08-19 06:38:58 +00:00
|
|
|
nsTArray<WorkerPrivate*> mActiveWorkers;
|
|
|
|
nsTArray<WorkerPrivate*> mActiveServiceWorkers;
|
|
|
|
nsTArray<WorkerPrivate*> mQueuedWorkers;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mChildWorkerCount;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-08-19 06:38:58 +00:00
|
|
|
WorkerDomainInfo() : mActiveWorkers(1), mChildWorkerCount(0) {}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-08-19 06:38:58 +00:00
|
|
|
uint32_t ActiveWorkerCount() const {
|
|
|
|
return mActiveWorkers.Length() + mChildWorkerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ActiveServiceWorkerCount() const {
|
|
|
|
return mActiveServiceWorkers.Length();
|
|
|
|
}
|
|
|
|
|
2015-07-01 21:32:52 +00:00
|
|
|
bool HasNoWorkers() const {
|
2016-08-19 06:38:58 +00:00
|
|
|
return ActiveWorkerCount() == 0 && ActiveServiceWorkerCount() == 0;
|
2015-07-01 21:32:52 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2022-04-01 15:44:23 +00:00
|
|
|
mozilla::Mutex mMutex;
|
2011-07-26 01:49:16 +00:00
|
|
|
|
|
|
|
// Protected by mMutex.
|
2022-04-01 15:44:23 +00:00
|
|
|
nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap
|
2022-08-03 16:39:41 +00:00
|
|
|
MOZ_GUARDED_BY(mMutex);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2011-07-26 01:49:16 +00:00
|
|
|
// *Not* protected by mMutex.
|
2020-06-02 08:53:52 +00:00
|
|
|
nsClassHashtable<nsPtrHashKey<const nsPIDOMWindowInner>,
|
|
|
|
nsTArray<WorkerPrivate*> >
|
2013-06-05 14:04:23 +00:00
|
|
|
mWindowMap;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2024-01-23 19:00:49 +00:00
|
|
|
static StaticAutoPtr<workerinternals::JSSettings> sDefaultJSSettings;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
public:
|
2013-11-19 23:08:50 +00:00
|
|
|
struct NavigatorProperties {
|
2011-07-17 19:09:13 +00:00
|
|
|
nsString mAppVersion;
|
2014-09-23 19:11:18 +00:00
|
|
|
nsString mAppVersionOverridden;
|
2011-07-17 19:09:13 +00:00
|
|
|
nsString mPlatform;
|
2014-09-23 19:11:18 +00:00
|
|
|
nsString mPlatformOverridden;
|
2020-05-11 08:22:17 +00:00
|
|
|
CopyableTArray<nsString> mLanguages;
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-11-19 23:08:50 +00:00
|
|
|
NavigatorProperties mNavigatorProperties;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// True when the observer service holds a reference to this object.
|
|
|
|
bool mObserved;
|
|
|
|
bool mShuttingDown;
|
2013-11-19 23:08:50 +00:00
|
|
|
bool mNavigatorPropertiesLoaded;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
static RuntimeService* GetOrCreateService();
|
|
|
|
|
|
|
|
static RuntimeService* GetService();
|
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
bool RegisterWorker(WorkerPrivate& aWorkerPrivate);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
void UnregisterWorker(WorkerPrivate& aWorkerPrivate);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
void CancelWorkersForWindow(const nsPIDOMWindowInner& aWindow);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2024-06-27 12:39:47 +00:00
|
|
|
void UpdateWorkersBackgroundState(const nsPIDOMWindowInner& aWindow,
|
|
|
|
bool aIsBackground);
|
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
void FreezeWorkersForWindow(const nsPIDOMWindowInner& aWindow);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
void ThawWorkersForWindow(const nsPIDOMWindowInner& aWindow);
|
2013-06-05 14:04:23 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
void SuspendWorkersForWindow(const nsPIDOMWindowInner& aWindow);
|
2015-10-07 10:20:59 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
void ResumeWorkersForWindow(const nsPIDOMWindowInner& aWindow);
|
2015-10-07 10:20:59 +00:00
|
|
|
|
2020-06-03 06:10:58 +00:00
|
|
|
void PropagateStorageAccessPermissionGranted(
|
2020-06-02 08:53:52 +00:00
|
|
|
const nsPIDOMWindowInner& aWindow);
|
2018-07-10 08:09:59 +00:00
|
|
|
|
2013-11-19 23:08:50 +00:00
|
|
|
const NavigatorProperties& GetNavigatorProperties() const {
|
|
|
|
return mNavigatorProperties;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 07:24:59 +00:00
|
|
|
static void GetDefaultJSSettings(workerinternals::JSSettings& aSettings) {
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2020-04-30 09:18:27 +00:00
|
|
|
aSettings = *sDefaultJSSettings;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 06:15:15 +00:00
|
|
|
static void SetDefaultContextOptions(
|
|
|
|
const JS::ContextOptions& aContextOptions) {
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2020-04-30 09:18:27 +00:00
|
|
|
sDefaultJSSettings->contextOptions = aContextOptions;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 19:11:18 +00:00
|
|
|
void UpdateAppVersionOverridePreference(const nsAString& aValue);
|
|
|
|
|
|
|
|
void UpdatePlatformOverridePreference(const nsAString& aValue);
|
|
|
|
|
2016-07-07 06:15:15 +00:00
|
|
|
void UpdateAllWorkerContextOptions();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2014-09-05 14:26:34 +00:00
|
|
|
void UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
|
|
|
|
|
2020-04-30 09:18:27 +00:00
|
|
|
static void SetDefaultJSGCSettings(JSGCParamKey aKey,
|
|
|
|
Maybe<uint32_t> aValue) {
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2020-04-30 09:18:27 +00:00
|
|
|
sDefaultJSSettings->ApplyGCSetting(aKey, aValue);
|
2012-01-04 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:18:27 +00:00
|
|
|
void UpdateAllWorkerMemoryParameter(JSGCParamKey aKey,
|
|
|
|
Maybe<uint32_t> aValue);
|
2012-01-04 19:11:32 +00:00
|
|
|
|
2013-05-16 22:49:43 +00:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
static void SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency) {
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2020-04-30 09:18:27 +00:00
|
|
|
sDefaultJSSettings->gcZeal = aGCZeal;
|
|
|
|
sDefaultJSSettings->gcZealFrequency = aFrequency;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateAllWorkerGCZeal();
|
|
|
|
#endif
|
|
|
|
|
2019-06-24 17:24:47 +00:00
|
|
|
void SetLowMemoryStateAllWorkers(bool aState);
|
|
|
|
|
2012-01-17 20:05:25 +00:00
|
|
|
void GarbageCollectAllWorkers(bool aShrinking);
|
|
|
|
|
2013-12-03 04:07:02 +00:00
|
|
|
void CycleCollectAllWorkers();
|
|
|
|
|
2013-11-19 23:08:50 +00:00
|
|
|
void SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
|
|
|
|
|
2016-03-23 21:55:07 +00:00
|
|
|
void MemoryPressureAllWorkers();
|
|
|
|
|
2022-05-10 02:20:02 +00:00
|
|
|
uint32_t ClampedHardwareConcurrency(bool aShouldResistFingerprinting) const;
|
2016-03-16 19:41:38 +00:00
|
|
|
|
2017-11-15 06:58:38 +00:00
|
|
|
void CrashIfHanging();
|
|
|
|
|
2021-03-02 09:57:41 +00:00
|
|
|
bool IsShuttingDown() const { return mShuttingDown; }
|
|
|
|
|
2023-03-30 13:44:34 +00:00
|
|
|
void DumpRunningWorkers();
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
private:
|
|
|
|
RuntimeService();
|
|
|
|
~RuntimeService();
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2013-09-09 17:54:05 +00:00
|
|
|
void Shutdown();
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void Cleanup();
|
|
|
|
|
2022-04-01 15:44:23 +00:00
|
|
|
void AddAllTopLevelWorkersToArray(nsTArray<WorkerPrivate*>& aWorkers)
|
2022-08-03 16:39:41 +00:00
|
|
|
MOZ_REQUIRES(mMutex);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
nsTArray<WorkerPrivate*> GetWorkersForWindow(
|
|
|
|
const nsPIDOMWindowInner& aWindow) const;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2020-06-02 08:53:52 +00:00
|
|
|
bool ScheduleWorker(WorkerPrivate& aWorkerPrivate);
|
2011-07-26 01:49:16 +00:00
|
|
|
|
2020-06-26 14:51:55 +00:00
|
|
|
template <typename Func>
|
|
|
|
void BroadcastAllWorkers(const Func& aFunc);
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2018-01-31 07:24:59 +00:00
|
|
|
} // namespace workerinternals
|
2022-05-09 20:41:20 +00:00
|
|
|
} // namespace mozilla::dom
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_runtimeservice_h__ */
|