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"
|
|
|
|
|
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 {
|
|
|
|
class SharedWorker;
|
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
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
struct SharedWorkerInfo
|
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsCString mScriptSpec;
|
2017-05-17 17:49:34 +03:00
|
|
|
nsString mName;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
SharedWorkerInfo(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aScriptSpec,
|
2017-05-17 17:49:34 +03:00
|
|
|
const nsAString& aName)
|
2013-06-05 18:04:23 +04:00
|
|
|
: mWorkerPrivate(aWorkerPrivate), mScriptSpec(aScriptSpec), mName(aName)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
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;
|
2017-11-27 19:07:39 +03:00
|
|
|
nsTArray<UniquePtr<SharedWorkerInfo>> mSharedWorkerInfos;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mChildWorkerCount;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerDomainInfo()
|
2016-08-19 09:38:58 +03:00
|
|
|
: mActiveWorkers(1), mChildWorkerCount(0)
|
2013-06-05 18:04:23 +04:00
|
|
|
{ }
|
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>,
|
2013-06-05 18:04:23 +04:00
|
|
|
nsTArray<WorkerPrivate*> > 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();
|
|
|
|
|
|
|
|
bool
|
2016-03-28 20:28:14 +03:00
|
|
|
RegisterWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
void
|
2016-03-28 20:28:14 +03:00
|
|
|
UnregisterWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-10-27 02:07:45 +03:00
|
|
|
void
|
|
|
|
RemoveSharedWorker(WorkerDomainInfo* aDomainInfo,
|
|
|
|
WorkerPrivate* aWorkerPrivate);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
CancelWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
FreezeWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
ThawWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-10-07 13:20:59 +03:00
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
SuspendWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2015-10-07 13:20:59 +03:00
|
|
|
|
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
ResumeWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
2015-10-07 13:20:59 +03:00
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
void
|
|
|
|
PropagateFirstPartyStorageAccessGranted(nsPIDOMWindowInner* aWindow);
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsresult
|
2013-11-05 18:16:24 +04:00
|
|
|
CreateSharedWorker(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
2017-05-17 17:49:34 +03:00
|
|
|
const nsAString& aName,
|
2015-10-01 02:11:03 +03:00
|
|
|
SharedWorker** aSharedWorker);
|
2014-07-03 04:48:50 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
void
|
|
|
|
ForgetSharedWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
const NavigatorProperties&
|
|
|
|
GetNavigatorProperties() const
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-11-20 03:08:50 +04:00
|
|
|
return mNavigatorProperties;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
void
|
2013-10-23 17:16:49 +04:00
|
|
|
NoteIdleThread(WorkerThread* aThread);
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
static void
|
2018-01-31 10:24:59 +03:00
|
|
|
GetDefaultJSSettings(workerinternals::JSSettings& aSettings)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-07-07 09:15:15 +03:00
|
|
|
SetDefaultContextOptions(const JS::ContextOptions& aContextOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
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);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
2016-07-07 09:15:15 +03:00
|
|
|
UpdateAllWorkerContextOptions();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
void
|
|
|
|
UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
|
|
|
|
|
2012-01-04 23:11:32 +04:00
|
|
|
static void
|
2013-05-17 02:49:43 +04:00
|
|
|
SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue)
|
2012-01-04 23:11:32 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-17 02:49:43 +04:00
|
|
|
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
|
2011-07-17 23:09:13 +04:00
|
|
|
static void
|
2013-05-17 02:49:43 +04:00
|
|
|
SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
GetWorkersForWindow(nsPIDOMWindowInner* aWindow,
|
2011-07-17 23:09:13 +04:00
|
|
|
nsTArray<WorkerPrivate*>& aWorkers);
|
|
|
|
|
|
|
|
bool
|
2016-03-28 20:28:14 +03:00
|
|
|
ScheduleWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-26 05:49:16 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
|
2013-11-24 23:27:15 +04:00
|
|
|
|
2014-07-03 04:48:50 +04:00
|
|
|
nsresult
|
|
|
|
CreateSharedWorkerFromLoadInfo(JSContext* aCx,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo* aLoadInfo,
|
2014-07-03 04:48:50 +04:00
|
|
|
const nsAString& aScriptURL,
|
2017-05-17 17:49:34 +03:00
|
|
|
const nsAString& aName,
|
2014-07-03 04:48:50 +04:00
|
|
|
SharedWorker** aSharedWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
} // workerinternals namespace
|
2018-01-31 10:24:30 +03:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_runtimeservice_h__ */
|