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__
|
|
|
|
|
|
|
|
#include "Workers.h"
|
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.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
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
class SharedWorker;
|
2014-11-15 05:47:30 +03:00
|
|
|
class WorkerThread;
|
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;
|
2014-02-06 18:39:10 +04:00
|
|
|
nsCString mName;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
SharedWorkerInfo(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aScriptSpec,
|
2014-02-06 18:39:10 +04:00
|
|
|
const nsACString& 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;
|
|
|
|
nsTArray<WorkerPrivate*> mActiveWorkers;
|
2015-06-27 01:51:56 +03:00
|
|
|
nsTArray<WorkerPrivate*> mActiveServiceWorkers;
|
2011-07-17 23:09:13 +04:00
|
|
|
nsTArray<WorkerPrivate*> mQueuedWorkers;
|
2013-06-05 18:04:23 +04:00
|
|
|
nsClassHashtable<nsCStringHashKey, 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()
|
|
|
|
: mActiveWorkers(1), mChildWorkerCount(0)
|
|
|
|
{ }
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2011-07-17 23:09:13 +04:00
|
|
|
ActiveWorkerCount() const
|
|
|
|
{
|
2015-06-27 01:51:56 +03:00
|
|
|
return mActiveWorkers.Length() +
|
|
|
|
mChildWorkerCount;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2015-07-02 00:32:52 +03:00
|
|
|
|
|
|
|
uint32_t
|
|
|
|
ActiveServiceWorkerCount() const
|
|
|
|
{
|
|
|
|
return mActiveServiceWorkers.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasNoWorkers() const
|
|
|
|
{
|
|
|
|
return ActiveWorkerCount() == 0 &&
|
|
|
|
ActiveServiceWorkerCount() == 0;
|
|
|
|
}
|
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;
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
static JSSettings sDefaultJSSettings;
|
2013-11-24 23:27:15 +04:00
|
|
|
static bool sDefaultPreferences[WORKERPREF_COUNT];
|
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
|
|
|
|
RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
|
|
|
|
|
|
|
void
|
|
|
|
UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
|
|
|
|
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
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsresult
|
2013-11-05 18:16:24 +04:00
|
|
|
CreateSharedWorker(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
2014-02-06 18:39:10 +04:00
|
|
|
const nsACString& 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
|
|
|
|
GetDefaultJSSettings(JSSettings& aSettings)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
aSettings = sDefaultJSSettings;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:27:15 +04:00
|
|
|
static void
|
|
|
|
GetDefaultPreferences(bool aPreferences[WORKERPREF_COUNT])
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
memcpy(aPreferences, sDefaultPreferences, WORKERPREF_COUNT * sizeof(bool));
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
static void
|
2014-07-30 08:00:37 +04:00
|
|
|
SetDefaultRuntimeOptions(const JS::RuntimeOptions& aRuntimeOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2014-02-26 13:25:36 +04:00
|
|
|
sDefaultJSSettings.runtimeOptions = aRuntimeOptions;
|
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
|
2014-07-30 08:00:37 +04:00
|
|
|
UpdateAllWorkerRuntimeOptions();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
void
|
|
|
|
UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages);
|
|
|
|
|
2013-11-24 23:27:15 +04:00
|
|
|
void
|
|
|
|
UpdateAllWorkerPreference(WorkerPreference aPref, bool aValue);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t
|
2013-05-17 02:49:43 +04:00
|
|
|
GetContentCloseHandlerTimeoutSeconds()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-05-17 02:49:43 +04:00
|
|
|
return sDefaultJSSettings.content.maxScriptRuntime;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
static uint32_t
|
|
|
|
GetChromeCloseHandlerTimeoutSeconds()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-05-17 02:49:43 +04:00
|
|
|
return sDefaultJSSettings.chrome.maxScriptRuntime;
|
2011-07-17 23:09:13 +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
|
|
|
{
|
|
|
|
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-16 22:41:38 +03:00
|
|
|
uint32_t ClampedHardwareConcurrency() const;
|
|
|
|
|
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
|
|
|
|
ScheduleWorker(JSContext* aCx, 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
|
|
|
|
2013-12-11 03:10:01 +04:00
|
|
|
static void
|
2013-11-24 23:27:15 +04:00
|
|
|
WorkerPrefChanged(const char* aPrefName, void* aClosure);
|
2013-12-10 15:43:16 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
JSVersionChanged(const char* aPrefName, void* aClosure);
|
2014-05-29 20:19:00 +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,
|
|
|
|
const nsACString& aName,
|
|
|
|
SharedWorker** aSharedWorker);
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_runtimeservice_h__ */
|