2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-15 20:40:38 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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 12:09:13 -07:00
|
|
|
|
|
|
|
#ifndef mozilla_dom_workers_runtimeservice_h__
|
|
|
|
#define mozilla_dom_workers_runtimeservice_h__
|
|
|
|
|
|
|
|
#include "Workers.h"
|
|
|
|
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
class nsIRunnable;
|
2011-07-17 12:09:13 -07:00
|
|
|
class nsIThread;
|
2011-07-25 18:49:16 -07:00
|
|
|
class nsITimer;
|
2011-07-17 12:09:13 -07:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
class SharedWorker;
|
2011-07-17 12:09:13 -07:00
|
|
|
class WorkerPrivate;
|
|
|
|
|
2012-06-14 19:31:55 -07:00
|
|
|
class RuntimeService MOZ_FINAL : public nsIObserver
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
public:
|
|
|
|
class WorkerThread;
|
|
|
|
|
|
|
|
private:
|
2013-06-05 07:04:23 -07:00
|
|
|
struct SharedWorkerInfo
|
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsCString mScriptSpec;
|
|
|
|
nsString mName;
|
|
|
|
|
|
|
|
SharedWorkerInfo(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aScriptSpec,
|
|
|
|
const nsAString& aName)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mScriptSpec(aScriptSpec), mName(aName)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
struct WorkerDomainInfo
|
|
|
|
{
|
|
|
|
nsCString mDomain;
|
|
|
|
nsTArray<WorkerPrivate*> mActiveWorkers;
|
|
|
|
nsTArray<WorkerPrivate*> mQueuedWorkers;
|
2013-06-05 07:04:23 -07:00
|
|
|
nsClassHashtable<nsCStringHashKey, SharedWorkerInfo> mSharedWorkerInfos;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mChildWorkerCount;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
WorkerDomainInfo()
|
|
|
|
: mActiveWorkers(1), mChildWorkerCount(0)
|
|
|
|
{ }
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2011-07-17 12:09:13 -07:00
|
|
|
ActiveWorkerCount() const
|
|
|
|
{
|
|
|
|
return mActiveWorkers.Length() + mChildWorkerCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
struct IdleThreadInfo
|
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
nsRefPtr<WorkerThread> mThread;
|
2011-07-25 18:49:16 -07:00
|
|
|
mozilla::TimeStamp mExpirationTime;
|
|
|
|
};
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
struct MatchSharedWorkerInfo
|
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
SharedWorkerInfo* mSharedWorkerInfo;
|
|
|
|
|
|
|
|
MatchSharedWorkerInfo(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mSharedWorkerInfo(nullptr)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
mozilla::Mutex mMutex;
|
|
|
|
|
|
|
|
// Protected by mMutex.
|
2011-07-17 12:09:13 -07:00
|
|
|
nsClassHashtable<nsCStringHashKey, WorkerDomainInfo> mDomainMap;
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
// Protected by mMutex.
|
|
|
|
nsTArray<IdleThreadInfo> mIdleThreadArray;
|
|
|
|
|
|
|
|
// *Not* protected by mMutex.
|
2013-06-05 07:04:23 -07:00
|
|
|
nsClassHashtable<nsPtrHashKey<nsPIDOMWindow>,
|
|
|
|
nsTArray<WorkerPrivate*> > mWindowMap;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
// Only used on the main thread.
|
|
|
|
nsCOMPtr<nsITimer> mIdleThreadTimer;
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
static JSSettings sDefaultJSSettings;
|
2013-11-24 11:27:15 -08:00
|
|
|
static bool sDefaultPreferences[WORKERPREF_COUNT];
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
public:
|
2013-11-19 15:08:50 -08:00
|
|
|
struct NavigatorProperties
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
nsString mAppName;
|
|
|
|
nsString mAppVersion;
|
|
|
|
nsString mPlatform;
|
|
|
|
nsString mUserAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-11-19 15:08:50 -08:00
|
|
|
NavigatorProperties mNavigatorProperties;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
// True when the observer service holds a reference to this object.
|
|
|
|
bool mObserved;
|
|
|
|
bool mShuttingDown;
|
2013-11-19 15:08:50 -08:00
|
|
|
bool mNavigatorPropertiesLoaded;
|
2011-07-17 12:09:13 -07: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);
|
|
|
|
|
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
CancelWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
SuspendWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
ResumeWorkersForWindow(nsPIDOMWindow* aWindow);
|
|
|
|
|
|
|
|
nsresult
|
2013-11-05 06:16:24 -08:00
|
|
|
CreateSharedWorker(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
const nsAString& aName,
|
2013-06-05 07:04:23 -07:00
|
|
|
SharedWorker** aSharedWorker);
|
|
|
|
|
|
|
|
void
|
|
|
|
ForgetSharedWorker(WorkerPrivate* aWorkerPrivate);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
const NavigatorProperties&
|
|
|
|
GetNavigatorProperties() const
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-11-19 15:08:50 -08:00
|
|
|
return mNavigatorProperties;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
void
|
2013-10-23 06:16:49 -07:00
|
|
|
NoteIdleThread(WorkerThread* aThread);
|
2011-07-25 18:49:16 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
static void
|
|
|
|
GetDefaultJSSettings(JSSettings& aSettings)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-05-16 15:49:43 -07:00
|
|
|
aSettings = sDefaultJSSettings;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-11-24 11:27:15 -08:00
|
|
|
static void
|
|
|
|
GetDefaultPreferences(bool aPreferences[WORKERPREF_COUNT])
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
memcpy(aPreferences, sDefaultPreferences, WORKERPREF_COUNT * sizeof(bool));
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
static void
|
2013-10-28 04:51:05 -07:00
|
|
|
SetDefaultJSContextOptions(const JS::ContextOptions& aContentOptions,
|
|
|
|
const JS::ContextOptions& aChromeOptions)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-12-10 03:43:16 -08:00
|
|
|
sDefaultJSSettings.content.contextOptions = aContentOptions;
|
|
|
|
sDefaultJSSettings.chrome.contextOptions = aChromeOptions;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateAllWorkerJSContextOptions();
|
|
|
|
|
2013-11-24 11:27:15 -08:00
|
|
|
void
|
|
|
|
UpdateAllWorkerPreference(WorkerPreference aPref, bool aValue);
|
|
|
|
|
2012-01-04 11:11:32 -08:00
|
|
|
static void
|
2013-05-16 15:49:43 -07:00
|
|
|
SetDefaultJSGCSettings(JSGCParamKey aKey, uint32_t aValue)
|
2012-01-04 11:11:32 -08:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-05-16 15:49:43 -07:00
|
|
|
sDefaultJSSettings.ApplyGCSetting(aKey, aValue);
|
2012-01-04 11:11:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateAllWorkerMemoryParameter(JSGCParamKey aKey, uint32_t aValue);
|
2012-01-04 11:11:32 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t
|
2013-05-16 15:49:43 -07:00
|
|
|
GetContentCloseHandlerTimeoutSeconds()
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-16 15:49:43 -07:00
|
|
|
return sDefaultJSSettings.content.maxScriptRuntime;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
static uint32_t
|
|
|
|
GetChromeCloseHandlerTimeoutSeconds()
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-16 15:49:43 -07:00
|
|
|
return sDefaultJSSettings.chrome.maxScriptRuntime;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
#ifdef JS_GC_ZEAL
|
2011-07-17 12:09:13 -07:00
|
|
|
static void
|
2013-05-16 15:49:43 -07:00
|
|
|
SetDefaultGCZeal(uint8_t aGCZeal, uint32_t aFrequency)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-05-16 15:49:43 -07:00
|
|
|
sDefaultJSSettings.gcZeal = aGCZeal;
|
|
|
|
sDefaultJSSettings.gcZealFrequency = aFrequency;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateAllWorkerGCZeal();
|
|
|
|
#endif
|
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
void
|
|
|
|
GarbageCollectAllWorkers(bool aShrinking);
|
|
|
|
|
2013-12-02 20:07:02 -08:00
|
|
|
void
|
|
|
|
CycleCollectAllWorkers();
|
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
void
|
|
|
|
SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline);
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
private:
|
|
|
|
RuntimeService();
|
|
|
|
~RuntimeService();
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Init();
|
|
|
|
|
2013-09-09 10:54:05 -07:00
|
|
|
void
|
|
|
|
Shutdown();
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
void
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
AddAllTopLevelWorkersToArray(const nsACString& aKey,
|
|
|
|
WorkerDomainInfo* aData,
|
|
|
|
void* aUserArg);
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
static PLDHashOperator
|
|
|
|
RemoveSharedWorkerFromWindowMap(nsPIDOMWindow* aKey,
|
|
|
|
nsAutoPtr<nsTArray<WorkerPrivate*> >& aData,
|
|
|
|
void* aUserArg);
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
FindSharedWorkerInfo(const nsACString& aKey,
|
|
|
|
SharedWorkerInfo* aData,
|
|
|
|
void* aUserArg);
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
void
|
|
|
|
GetWorkersForWindow(nsPIDOMWindow* aWindow,
|
|
|
|
nsTArray<WorkerPrivate*>& aWorkers);
|
|
|
|
|
|
|
|
bool
|
|
|
|
ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
2011-07-25 18:49:16 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
ShutdownIdleThreads(nsITimer* aTimer, void* aClosure);
|
2013-11-24 11:27:15 -08:00
|
|
|
|
2013-12-10 15:10:01 -08:00
|
|
|
static void
|
2013-11-24 11:27:15 -08:00
|
|
|
WorkerPrefChanged(const char* aPrefName, void* aClosure);
|
2013-12-10 03:43:16 -08:00
|
|
|
|
|
|
|
static void
|
|
|
|
JSVersionChanged(const char* aPrefName, void* aClosure);
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_runtimeservice_h__ */
|