2015-05-03 12:32:37 -07: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 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
|
|
|
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "nsIChannel.h"
|
2012-09-15 11:51:55 -07:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "nsIDocument.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsIDOMChromeWindow.h"
|
|
|
|
#include "nsIEffectiveTLDService.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "nsIScriptContext.h"
|
2015-03-25 10:17:00 -07:00
|
|
|
#include "nsIScriptError.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsISupportsPriority.h"
|
2011-07-25 18:49:16 -07:00
|
|
|
#include "nsITimer.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "nsIURI.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
#include <algorithm>
|
2014-07-09 14:39:18 -07:00
|
|
|
#include "BackgroundChild.h"
|
2013-05-16 15:49:43 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2012-12-30 10:21:52 -08:00
|
|
|
#include "jsfriendapi.h"
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-08-03 16:55:40 -07:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2015-03-25 10:17:00 -07:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-11-14 18:47:30 -08:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-12-03 11:31:30 -08:00
|
|
|
#include "mozilla/dom/asmjscache/AsmJSCache.h"
|
2013-08-22 22:17:09 -07:00
|
|
|
#include "mozilla/dom/AtomList.h"
|
2013-05-16 15:49:43 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-11-05 06:16:24 -08:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/EventTargetBinding.h"
|
2013-11-05 06:16:24 -08:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
|
|
|
#include "mozilla/dom/WorkerBinding.h"
|
2014-06-30 08:21:27 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-12-16 22:26:15 -08:00
|
|
|
#include "mozilla/dom/indexedDB/IndexedDatabaseManager.h"
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
2013-05-16 15:49:43 -07:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-11-19 11:10:15 -08:00
|
|
|
#include "mozilla/dom/Navigator.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-03 16:55:40 -07:00
|
|
|
#include "nsCycleCollector.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsDOMJSUtils.h"
|
2014-07-09 14:39:18 -07:00
|
|
|
#include "nsIIPCBackgroundChildCreateCallback.h"
|
2014-02-26 13:36:35 -08:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-05-16 15:49:43 -07:00
|
|
|
#include "nsLayoutStatics.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsXPCOMPrivate.h"
|
2013-05-16 15:49:43 -07:00
|
|
|
#include "OSFileConstants.h"
|
2011-08-01 21:06:17 -07:00
|
|
|
#include "xpcpublic.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-06-03 03:14:42 -07:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
|
|
|
|
2014-09-15 08:49:11 -07:00
|
|
|
#include "Principal.h"
|
2013-10-23 06:16:49 -07:00
|
|
|
#include "SharedWorker.h"
|
2015-03-17 03:15:19 -07:00
|
|
|
#include "WorkerDebuggerManager.h"
|
2013-10-23 06:16:49 -07:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
2014-11-14 18:47:30 -08:00
|
|
|
#include "WorkerThread.h"
|
2013-10-23 06:16:49 -07:00
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
#ifdef ENABLE_TESTS
|
|
|
|
#include "BackgroundChildImpl.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "prrng.h"
|
|
|
|
#endif
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
2012-05-02 21:35:38 -07:00
|
|
|
using namespace mozilla::dom;
|
2014-12-16 22:26:15 -08:00
|
|
|
using namespace mozilla::ipc;
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
using mozilla::MutexAutoLock;
|
|
|
|
using mozilla::MutexAutoUnlock;
|
|
|
|
using mozilla::Preferences;
|
2014-12-16 22:26:15 -08:00
|
|
|
using mozilla::dom::indexedDB::IndexedDatabaseManager;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-01-04 11:11:32 -08:00
|
|
|
// The size of the worker runtime heaps in bytes. May be changed via pref.
|
|
|
|
#define WORKER_DEFAULT_RUNTIME_HEAPSIZE 32 * 1024 * 1024
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-07-29 15:38:14 -07:00
|
|
|
// The size of the generational GC nursery for workers, in bytes.
|
2014-07-29 15:38:14 -07:00
|
|
|
#define WORKER_DEFAULT_NURSERY_SIZE 1 * 1024 * 1024
|
2014-07-29 15:38:14 -07:00
|
|
|
|
2013-01-10 14:50:40 -08:00
|
|
|
// The size of the worker JS allocation threshold in MB. May be changed via pref.
|
|
|
|
#define WORKER_DEFAULT_ALLOCATION_THRESHOLD 30
|
|
|
|
|
2012-03-07 15:11:15 -08:00
|
|
|
// Half the size of the actual C stack, to be safe.
|
2011-08-15 22:53:33 -07:00
|
|
|
#define WORKER_CONTEXT_NATIVE_STACK_LIMIT 128 * sizeof(size_t) * 1024
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
// The maximum number of threads to use for workers, overridable via pref.
|
|
|
|
#define MAX_WORKERS_PER_DOMAIN 10
|
|
|
|
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(MAX_WORKERS_PER_DOMAIN >= 1,
|
|
|
|
"We should allow at least one worker per domain.");
|
2012-01-17 12:05:25 -08:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
// The default number of seconds that close handlers will be allowed to run for
|
|
|
|
// content workers.
|
2011-07-17 12:09:13 -07:00
|
|
|
#define MAX_SCRIPT_RUN_TIME_SEC 10
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
// The number of seconds that idle threads can hang around before being killed.
|
|
|
|
#define IDLE_THREAD_TIMEOUT_SEC 30
|
|
|
|
|
|
|
|
// The maximum number of threads that can be idle at one time.
|
|
|
|
#define MAX_IDLE_THREADS 20
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
#define PREF_WORKERS_PREFIX "dom.workers."
|
|
|
|
#define PREF_WORKERS_MAX_PER_DOMAIN PREF_WORKERS_PREFIX "maxPerDomain"
|
|
|
|
|
|
|
|
#define PREF_MAX_SCRIPT_RUN_TIME_CONTENT "dom.max_script_run_time"
|
|
|
|
#define PREF_MAX_SCRIPT_RUN_TIME_CHROME "dom.max_chrome_script_run_time"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
#define GC_REQUEST_OBSERVER_TOPIC "child-gc-request"
|
2013-12-02 20:07:02 -08:00
|
|
|
#define CC_REQUEST_OBSERVER_TOPIC "child-cc-request"
|
2012-01-17 12:05:25 -08:00
|
|
|
#define MEMORY_PRESSURE_OBSERVER_TOPIC "memory-pressure"
|
|
|
|
|
2014-09-23 12:11:18 -07:00
|
|
|
#define PREF_GENERAL_APPNAME_OVERRIDE "general.appname.override"
|
|
|
|
#define PREF_GENERAL_APPVERSION_OVERRIDE "general.appversion.override"
|
|
|
|
#define PREF_GENERAL_PLATFORM_OVERRIDE "general.platform.override"
|
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
#define BROADCAST_ALL_WORKERS(_func, ...) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
AssertIsOnMainThread(); \
|
|
|
|
\
|
|
|
|
nsAutoTArray<WorkerPrivate*, 100> workers; \
|
|
|
|
{ \
|
|
|
|
MutexAutoLock lock(mMutex); \
|
|
|
|
\
|
|
|
|
mDomainMap.EnumerateRead(AddAllTopLevelWorkersToArray, &workers); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (!workers.IsEmpty()) { \
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx; \
|
2013-04-18 08:36:04 -07:00
|
|
|
JSAutoRequest ar(cx); \
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) { \
|
2012-08-23 16:57:51 -07:00
|
|
|
workers[index]-> _func (cx, __VA_ARGS__); \
|
2012-01-17 12:05:25 -08:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
// Prefixes for observing preference changes.
|
|
|
|
#define PREF_JS_OPTIONS_PREFIX "javascript.options."
|
|
|
|
#define PREF_WORKERS_OPTIONS_PREFIX PREF_WORKERS_PREFIX "options."
|
|
|
|
#define PREF_MEM_OPTIONS_PREFIX "mem."
|
|
|
|
#define PREF_GCZEAL "gcZeal"
|
|
|
|
|
2013-12-10 03:43:16 -08:00
|
|
|
#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP))
|
|
|
|
#define DUMP_CONTROLLED_BY_PREF 1
|
|
|
|
#define PREF_DOM_WINDOW_DUMP_ENABLED "browser.dom.window.dump.enabled"
|
|
|
|
#endif
|
|
|
|
|
2015-03-02 05:12:00 -08:00
|
|
|
#define PREF_DOM_CACHES_ENABLED "dom.caches.enabled"
|
2015-06-27 20:19:23 -07:00
|
|
|
#define PREF_DOM_CACHES_TESTING_ENABLED "dom.caches.testing.enabled"
|
2015-07-28 09:33:37 -07:00
|
|
|
#define PREF_WORKERS_PERFORMANCE_LOGGING_ENABLED "dom.performance.enable_user_timing_logging"
|
2015-06-25 18:53:02 -07:00
|
|
|
#define PREF_DOM_WORKERNOTIFICATION_ENABLED "dom.webnotifications.enabled"
|
2013-12-10 03:43:16 -08:00
|
|
|
#define PREF_WORKERS_LATEST_JS_VERSION "dom.workers.latestJSVersion"
|
2014-09-05 07:26:34 -07:00
|
|
|
#define PREF_INTL_ACCEPT_LANGUAGES "intl.accept_languages"
|
2015-04-03 22:46:07 -07:00
|
|
|
#define PREF_SERVICEWORKERS_ENABLED "dom.serviceWorkers.enabled"
|
2015-06-27 20:19:24 -07:00
|
|
|
#define PREF_SERVICEWORKERS_TESTING_ENABLED "dom.serviceWorkers.testing.enabled"
|
2015-06-12 16:28:13 -07:00
|
|
|
#define PREF_INTERCEPTION_ENABLED "dom.serviceWorkers.interception.enabled"
|
2015-07-02 20:09:20 -07:00
|
|
|
#define PREF_INTERCEPTION_OPAQUE_ENABLED "dom.serviceWorkers.interception.opaque.enabled"
|
2015-07-27 12:35:28 -07:00
|
|
|
#define PREF_PUSH_ENABLED "dom.push.enabled"
|
2015-07-27 14:57:34 -07:00
|
|
|
#define PREF_REQUESTCONTEXT_ENABLED "dom.requestcontext.enabled"
|
2013-12-10 03:43:16 -08:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
namespace {
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t kNoIndex = uint32_t(-1);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-02-26 01:25:36 -08:00
|
|
|
const JS::ContextOptions kRequiredContextOptions =
|
2014-07-31 09:55:20 -07:00
|
|
|
JS::ContextOptions().setDontReportUncaught(true);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t gMaxWorkersPerDomain = MAX_WORKERS_PER_DOMAIN;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
// Does not hold an owning reference.
|
2012-07-30 07:20:58 -07:00
|
|
|
RuntimeService* gRuntimeService = nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-10-27 02:38:14 -07:00
|
|
|
// Only true during the call to Init.
|
|
|
|
bool gRuntimeServiceDuringInit = false;
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
#ifdef ENABLE_TESTS
|
|
|
|
bool gTestPBackground = false;
|
|
|
|
#endif // ENABLE_TESTS
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
class LiteralRebindingCString : public nsDependentCString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<int N>
|
|
|
|
void RebindLiteral(const char (&aStr)[N])
|
|
|
|
{
|
|
|
|
Rebind(aStr, N-1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct PrefTraits;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct PrefTraits<bool>
|
|
|
|
{
|
|
|
|
typedef bool PrefValueType;
|
|
|
|
|
|
|
|
static const PrefValueType kDefaultValue = false;
|
|
|
|
|
|
|
|
static inline PrefValueType
|
|
|
|
Get(const char* aPref)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetBool(aPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
Exists(const char* aPref)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetType(aPref) == nsIPrefBranch::PREF_BOOL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct PrefTraits<int32_t>
|
|
|
|
{
|
|
|
|
typedef int32_t PrefValueType;
|
|
|
|
|
|
|
|
static inline PrefValueType
|
|
|
|
Get(const char* aPref)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetInt(aPref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
Exists(const char* aPref)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return Preferences::GetType(aPref) == nsIPrefBranch::PREF_INT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T
|
|
|
|
GetWorkerPref(const nsACString& aPref,
|
|
|
|
const T aDefault = PrefTraits<T>::kDefaultValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
typedef PrefTraits<T> PrefHelper;
|
|
|
|
|
|
|
|
T result;
|
|
|
|
|
|
|
|
nsAutoCString prefName;
|
|
|
|
prefName.AssignLiteral(PREF_WORKERS_OPTIONS_PREFIX);
|
|
|
|
prefName.Append(aPref);
|
|
|
|
|
|
|
|
if (PrefHelper::Exists(prefName.get())) {
|
|
|
|
result = PrefHelper::Get(prefName.get());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prefName.AssignLiteral(PREF_JS_OPTIONS_PREFIX);
|
|
|
|
prefName.Append(aPref);
|
|
|
|
|
|
|
|
if (PrefHelper::Exists(prefName.get())) {
|
|
|
|
result = PrefHelper::Get(prefName.get());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = aDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-10 08:44:04 -07:00
|
|
|
// This function creates a key for a SharedWorker composed by "shared|name|scriptSpec"
|
2015-03-19 16:40:52 -07:00
|
|
|
// and a key for a ServiceWorker composed by "service|scope|cache|scriptSpec".
|
2014-02-06 06:39:10 -08:00
|
|
|
// If the name contains a '|', this will be replaced by '||'.
|
|
|
|
void
|
|
|
|
GenerateSharedWorkerKey(const nsACString& aScriptSpec, const nsACString& aName,
|
2015-03-19 16:40:52 -07:00
|
|
|
const nsACString& aCacheName, WorkerType aWorkerType,
|
2015-06-26 11:18:18 -07:00
|
|
|
bool aPrivateBrowsing, nsCString& aKey)
|
2014-02-06 06:39:10 -08:00
|
|
|
{
|
|
|
|
aKey.Truncate();
|
2015-03-10 08:44:04 -07:00
|
|
|
NS_NAMED_LITERAL_CSTRING(sharedPrefix, "shared|");
|
|
|
|
NS_NAMED_LITERAL_CSTRING(servicePrefix, "service|");
|
|
|
|
MOZ_ASSERT(servicePrefix.Length() > sharedPrefix.Length());
|
|
|
|
MOZ_ASSERT(aWorkerType == WorkerTypeShared ||
|
|
|
|
aWorkerType == WorkerTypeService);
|
2015-03-19 16:40:52 -07:00
|
|
|
MOZ_ASSERT_IF(aWorkerType == WorkerTypeShared, aCacheName.IsEmpty());
|
|
|
|
MOZ_ASSERT_IF(aWorkerType == WorkerTypeService, !aCacheName.IsEmpty());
|
2015-06-26 11:18:18 -07:00
|
|
|
MOZ_ASSERT_IF(aWorkerType == WorkerTypeService, !aPrivateBrowsing);
|
2015-03-10 08:44:04 -07:00
|
|
|
aKey.SetCapacity(servicePrefix.Length() + aScriptSpec.Length() +
|
2015-06-26 11:18:18 -07:00
|
|
|
aName.Length() + aCacheName.Length() + 3);
|
2015-03-10 08:44:04 -07:00
|
|
|
|
|
|
|
aKey.Append(aWorkerType == WorkerTypeService ? servicePrefix : sharedPrefix);
|
2015-06-26 11:18:18 -07:00
|
|
|
aKey.Append(aPrivateBrowsing ? "1|" : "0|");
|
2014-02-06 06:39:10 -08:00
|
|
|
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
aName.BeginReading(start);
|
|
|
|
aName.EndReading(end);
|
|
|
|
for (; start != end; ++start) {
|
|
|
|
if (*start == '|') {
|
|
|
|
aKey.AppendASCII("||");
|
|
|
|
} else {
|
|
|
|
aKey.Append(*start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 16:40:52 -07:00
|
|
|
if (aWorkerType == WorkerTypeService) {
|
|
|
|
aKey.Append('|');
|
|
|
|
aKey.Append(aCacheName);
|
|
|
|
}
|
|
|
|
|
2014-02-06 06:39:10 -08:00
|
|
|
aKey.Append('|');
|
|
|
|
aKey.Append(aScriptSpec);
|
|
|
|
}
|
|
|
|
|
2013-12-10 15:10:01 -08:00
|
|
|
void
|
2014-07-29 21:00:37 -07:00
|
|
|
LoadRuntimeOptions(const char* aPrefName, void* /* aClosure */)
|
2013-05-16 15:49:43 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
2014-10-08 05:56:50 -07:00
|
|
|
if (!rts) {
|
2013-05-16 15:49:43 -07:00
|
|
|
// May be shutting down, just bail.
|
2013-12-10 15:10:01 -08:00
|
|
|
return;
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsDependentCString prefName(aPrefName);
|
|
|
|
|
|
|
|
// Several other pref branches will get included here so bail out if there is
|
|
|
|
// another callback that will handle this change.
|
|
|
|
if (StringBeginsWith(prefName,
|
|
|
|
NS_LITERAL_CSTRING(PREF_JS_OPTIONS_PREFIX
|
|
|
|
PREF_MEM_OPTIONS_PREFIX)) ||
|
|
|
|
StringBeginsWith(prefName,
|
|
|
|
NS_LITERAL_CSTRING(PREF_WORKERS_OPTIONS_PREFIX
|
2013-12-25 00:21:59 -08:00
|
|
|
PREF_MEM_OPTIONS_PREFIX))) {
|
2013-12-10 15:10:01 -08:00
|
|
|
return;
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
2013-05-16 15:49:43 -07:00
|
|
|
if (prefName.EqualsLiteral(PREF_JS_OPTIONS_PREFIX PREF_GCZEAL) ||
|
|
|
|
prefName.EqualsLiteral(PREF_WORKERS_OPTIONS_PREFIX PREF_GCZEAL)) {
|
2013-12-10 15:10:01 -08:00
|
|
|
return;
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
#endif
|
|
|
|
|
2014-02-26 01:25:36 -08:00
|
|
|
// Runtime options.
|
|
|
|
JS::RuntimeOptions runtimeOptions;
|
2015-06-09 02:17:09 -07:00
|
|
|
runtimeOptions.setAsmJS(GetWorkerPref<bool>(NS_LITERAL_CSTRING("asmjs")))
|
|
|
|
.setBaseline(GetWorkerPref<bool>(NS_LITERAL_CSTRING("baselinejit")))
|
|
|
|
.setIon(GetWorkerPref<bool>(NS_LITERAL_CSTRING("ion")))
|
|
|
|
.setNativeRegExp(GetWorkerPref<bool>(NS_LITERAL_CSTRING("native_regexp")))
|
|
|
|
.setAsyncStack(GetWorkerPref<bool>(NS_LITERAL_CSTRING("asyncstack")))
|
|
|
|
.setWerror(GetWorkerPref<bool>(NS_LITERAL_CSTRING("werror")))
|
|
|
|
.setExtraWarnings(GetWorkerPref<bool>(NS_LITERAL_CSTRING("strict")));
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-07-29 21:00:37 -07:00
|
|
|
RuntimeService::SetDefaultRuntimeOptions(runtimeOptions);
|
2013-05-16 15:49:43 -07:00
|
|
|
|
|
|
|
if (rts) {
|
2014-07-29 21:00:37 -07:00
|
|
|
rts->UpdateAllWorkerRuntimeOptions();
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
2013-12-10 15:10:01 -08:00
|
|
|
void
|
2013-05-16 15:49:43 -07:00
|
|
|
LoadGCZealOptions(const char* /* aPrefName */, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
2014-10-08 05:56:50 -07:00
|
|
|
if (!rts) {
|
2013-05-16 15:49:43 -07:00
|
|
|
// May be shutting down, just bail.
|
2013-12-10 15:10:01 -08:00
|
|
|
return;
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t gczeal = GetWorkerPref<int32_t>(NS_LITERAL_CSTRING(PREF_GCZEAL), -1);
|
|
|
|
if (gczeal < 0) {
|
|
|
|
gczeal = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t frequency =
|
|
|
|
GetWorkerPref<int32_t>(NS_LITERAL_CSTRING("gcZeal.frequency"), -1);
|
|
|
|
if (frequency < 0) {
|
|
|
|
frequency = JS_DEFAULT_ZEAL_FREQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeService::SetDefaultGCZeal(uint8_t(gczeal), uint32_t(frequency));
|
|
|
|
|
|
|
|
if (rts) {
|
|
|
|
rts->UpdateAllWorkerGCZeal();
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
#endif
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
void
|
|
|
|
UpdateCommonJSGCMemoryOption(RuntimeService* aRuntimeService,
|
|
|
|
const nsACString& aPrefName, JSGCParamKey aKey)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
NS_ASSERTION(!aPrefName.IsEmpty(), "Empty pref name!");
|
|
|
|
|
|
|
|
int32_t prefValue = GetWorkerPref(aPrefName, -1);
|
|
|
|
uint32_t value =
|
2013-06-26 23:40:17 -07:00
|
|
|
(prefValue < 0 || prefValue >= 10000) ? 0 : uint32_t(prefValue);
|
2013-05-16 15:49:43 -07:00
|
|
|
|
|
|
|
RuntimeService::SetDefaultJSGCSettings(aKey, value);
|
|
|
|
|
|
|
|
if (aRuntimeService) {
|
|
|
|
aRuntimeService->UpdateAllWorkerMemoryParameter(aKey, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-02 06:39:34 -08:00
|
|
|
UpdateOtherJSGCMemoryOption(RuntimeService* aRuntimeService,
|
|
|
|
JSGCParamKey aKey, uint32_t aValue)
|
2013-05-16 15:49:43 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService::SetDefaultJSGCSettings(aKey, aValue);
|
|
|
|
|
|
|
|
if (aRuntimeService) {
|
|
|
|
aRuntimeService->UpdateAllWorkerMemoryParameter(aKey, aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-12-10 15:10:01 -08:00
|
|
|
void
|
2013-05-16 15:49:43 -07:00
|
|
|
LoadJSGCMemoryOptions(const char* aPrefName, void* /* aClosure */)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
|
|
|
|
2014-10-08 05:56:50 -07:00
|
|
|
if (!rts) {
|
2013-05-16 15:49:43 -07:00
|
|
|
// May be shutting down, just bail.
|
2013-12-10 15:10:01 -08:00
|
|
|
return;
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING(jsPrefix, PREF_JS_OPTIONS_PREFIX);
|
|
|
|
NS_NAMED_LITERAL_CSTRING(workersPrefix, PREF_WORKERS_OPTIONS_PREFIX);
|
|
|
|
|
|
|
|
const nsDependentCString fullPrefName(aPrefName);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
// Pull out the string that actually distinguishes the parameter we need to
|
|
|
|
// change.
|
|
|
|
nsDependentCSubstring memPrefName;
|
|
|
|
if (StringBeginsWith(fullPrefName, jsPrefix)) {
|
|
|
|
memPrefName.Rebind(fullPrefName, jsPrefix.Length());
|
|
|
|
}
|
|
|
|
else if (StringBeginsWith(fullPrefName, workersPrefix)) {
|
|
|
|
memPrefName.Rebind(fullPrefName, workersPrefix.Length());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ERROR("Unknown pref name!");
|
2013-12-10 15:10:01 -08:00
|
|
|
return;
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// During Init() we get called back with a branch string here, so there should
|
|
|
|
// be no just a "mem." pref here.
|
|
|
|
if (!rts) {
|
|
|
|
NS_ASSERTION(memPrefName.EqualsLiteral(PREF_MEM_OPTIONS_PREFIX), "Huh?!");
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
// If we're running in Init() then do this for every pref we care about.
|
|
|
|
// Otherwise we just want to update the parameter that changed.
|
2014-10-27 02:38:14 -07:00
|
|
|
for (uint32_t index = !gRuntimeServiceDuringInit
|
|
|
|
? JSSettings::kGCSettingsArraySize - 1 : 0;
|
2013-05-16 15:49:43 -07:00
|
|
|
index < JSSettings::kGCSettingsArraySize;
|
|
|
|
index++) {
|
|
|
|
LiteralRebindingCString matchName;
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "max");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 0)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
int32_t prefValue = GetWorkerPref(matchName, -1);
|
|
|
|
uint32_t value = (prefValue <= 0 || prefValue >= 0x1000) ?
|
|
|
|
uint32_t(-1) :
|
|
|
|
uint32_t(prefValue) * 1024 * 1024;
|
2015-02-02 06:39:34 -08:00
|
|
|
UpdateOtherJSGCMemoryOption(rts, JSGC_MAX_BYTES, value);
|
2013-05-16 15:49:43 -07:00
|
|
|
continue;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "high_water_mark");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 1)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
int32_t prefValue = GetWorkerPref(matchName, 128);
|
2015-02-02 06:39:34 -08:00
|
|
|
UpdateOtherJSGCMemoryOption(rts, JSGC_MAX_MALLOC_BYTES,
|
2013-05-16 15:49:43 -07:00
|
|
|
uint32_t(prefValue) * 1024 * 1024);
|
|
|
|
continue;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
|
2014-05-12 09:11:15 -07:00
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
2013-05-16 15:49:43 -07:00
|
|
|
"gc_high_frequency_time_limit_ms");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 2)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName,
|
|
|
|
JSGC_HIGH_FREQUENCY_TIME_LIMIT);
|
|
|
|
continue;
|
2012-01-05 17:51:21 -08:00
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
|
|
|
"gc_low_frequency_heap_growth");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 3)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName,
|
|
|
|
JSGC_LOW_FREQUENCY_HEAP_GROWTH);
|
|
|
|
continue;
|
2013-02-28 08:38:20 -08:00
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
|
|
|
"gc_high_frequency_heap_growth_min");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 4)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName,
|
|
|
|
JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MIN);
|
|
|
|
continue;
|
2012-04-19 15:02:47 -07:00
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
|
|
|
"gc_high_frequency_heap_growth_max");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 5)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName,
|
|
|
|
JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MAX);
|
|
|
|
continue;
|
2013-03-15 02:29:02 -07:00
|
|
|
}
|
2012-01-05 17:51:21 -08:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
|
|
|
"gc_high_frequency_low_limit_mb");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 6)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName,
|
|
|
|
JSGC_HIGH_FREQUENCY_LOW_LIMIT);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
|
|
|
"gc_high_frequency_high_limit_mb");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 7)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName,
|
|
|
|
JSGC_HIGH_FREQUENCY_HIGH_LIMIT);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX
|
|
|
|
"gc_allocation_threshold_mb");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 8)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName, JSGC_ALLOCATION_THRESHOLD);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "gc_incremental_slice_ms");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName || (gRuntimeServiceDuringInit && index == 9)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
int32_t prefValue = GetWorkerPref(matchName, -1);
|
|
|
|
uint32_t value =
|
|
|
|
(prefValue <= 0 || prefValue >= 100000) ? 0 : uint32_t(prefValue);
|
2015-02-02 06:39:34 -08:00
|
|
|
UpdateOtherJSGCMemoryOption(rts, JSGC_SLICE_TIME_BUDGET, value);
|
2013-05-16 15:49:43 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "gc_dynamic_heap_growth");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName ||
|
|
|
|
(gRuntimeServiceDuringInit && index == 10)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
bool prefValue = GetWorkerPref(matchName, false);
|
2015-02-02 06:39:34 -08:00
|
|
|
UpdateOtherJSGCMemoryOption(rts, JSGC_DYNAMIC_HEAP_GROWTH,
|
2013-05-16 15:49:43 -07:00
|
|
|
prefValue ? 0 : 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "gc_dynamic_mark_slice");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName ||
|
|
|
|
(gRuntimeServiceDuringInit && index == 11)) {
|
2013-05-16 15:49:43 -07:00
|
|
|
bool prefValue = GetWorkerPref(matchName, false);
|
2015-02-02 06:39:34 -08:00
|
|
|
UpdateOtherJSGCMemoryOption(rts, JSGC_DYNAMIC_MARK_SLICE,
|
2013-05-16 15:49:43 -07:00
|
|
|
prefValue ? 0 : 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-16 02:01:20 -07:00
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "gc_min_empty_chunk_count");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName ||
|
|
|
|
(gRuntimeServiceDuringInit && index == 12)) {
|
2014-07-16 02:01:20 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName, JSGC_MIN_EMPTY_CHUNK_COUNT);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "gc_max_empty_chunk_count");
|
2014-10-27 02:38:14 -07:00
|
|
|
if (memPrefName == matchName ||
|
|
|
|
(gRuntimeServiceDuringInit && index == 13)) {
|
2014-07-16 02:01:20 -07:00
|
|
|
UpdateCommonJSGCMemoryOption(rts, matchName, JSGC_MAX_EMPTY_CHUNK_COUNT);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-02 06:39:34 -08:00
|
|
|
matchName.RebindLiteral(PREF_MEM_OPTIONS_PREFIX "gc_compacting");
|
|
|
|
if (memPrefName == matchName ||
|
|
|
|
(gRuntimeServiceDuringInit && index == 14)) {
|
|
|
|
bool prefValue = GetWorkerPref(matchName, false);
|
|
|
|
UpdateOtherJSGCMemoryOption(rts, JSGC_COMPACTING_ENABLED,
|
|
|
|
prefValue ? 0 : 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoCString message("Workers don't support the 'mem.");
|
|
|
|
message.Append(memPrefName);
|
|
|
|
message.AppendLiteral("' preference!");
|
|
|
|
NS_WARNING(message.get());
|
|
|
|
#endif
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
void
|
|
|
|
ErrorReporter(JSContext* aCx, const char* aMessage, JSErrorReport* aReport)
|
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
return worker->ReportError(aCx, aMessage, aReport);
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2014-03-10 14:28:43 -07:00
|
|
|
InterruptCallback(JSContext* aCx)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(worker);
|
2013-04-03 15:59:17 -07:00
|
|
|
|
|
|
|
// Now is a good time to turn on profiling if it's pending.
|
|
|
|
profiler_js_operation_callback();
|
|
|
|
|
2014-03-10 14:28:43 -07:00
|
|
|
return worker->InterruptCallback(aCx);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class LogViolationDetailsRunnable final : public nsRunnable
|
2012-09-15 11:51:55 -07:00
|
|
|
{
|
2012-09-22 05:45:00 -07:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-10-23 06:16:49 -07:00
|
|
|
nsCOMPtr<nsIEventTarget> mSyncLoopTarget;
|
2012-09-15 11:51:55 -07:00
|
|
|
nsString mFileName;
|
|
|
|
uint32_t mLineNum;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LogViolationDetailsRunnable(WorkerPrivate* aWorker,
|
|
|
|
const nsString& aFileName,
|
|
|
|
uint32_t aLineNum)
|
2013-10-23 06:16:49 -07:00
|
|
|
: mWorkerPrivate(aWorker), mFileName(aFileName), mLineNum(aLineNum)
|
2012-09-15 11:51:55 -07:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(aWorker);
|
2012-09-15 11:51:55 -07:00
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2012-09-22 05:45:00 -07:00
|
|
|
bool
|
|
|
|
Dispatch(JSContext* aCx)
|
|
|
|
{
|
2012-12-21 12:14:47 -08:00
|
|
|
AutoSyncLoopHolder syncLoop(mWorkerPrivate);
|
2013-10-23 06:16:49 -07:00
|
|
|
|
|
|
|
mSyncLoopTarget = syncLoop.EventTarget();
|
|
|
|
MOZ_ASSERT(mSyncLoopTarget);
|
2012-09-22 05:45:00 -07:00
|
|
|
|
2014-05-23 12:53:17 -07:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this))) {
|
2012-09-22 05:45:00 -07:00
|
|
|
JS_ReportError(aCx, "Failed to dispatch to main thread!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
return syncLoop.Run();
|
2012-09-22 05:45:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
private:
|
2014-07-08 14:23:17 -07:00
|
|
|
~LogViolationDetailsRunnable() {}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2012-09-15 11:51:55 -07:00
|
|
|
};
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2012-09-15 11:51:55 -07:00
|
|
|
ContentSecurityPolicyAllows(JSContext* aCx)
|
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
2012-10-15 13:54:58 -07:00
|
|
|
if (worker->GetReportCSPViolations()) {
|
|
|
|
nsString fileName;
|
|
|
|
uint32_t lineNum = 0;
|
|
|
|
|
2014-02-25 07:43:14 -08:00
|
|
|
JS::AutoFilename file;
|
|
|
|
if (JS::DescribeScriptedCaller(aCx, &file, &lineNum) && file.get()) {
|
|
|
|
fileName = NS_ConvertUTF8toUTF16(file.get());
|
2012-10-15 13:54:58 -07:00
|
|
|
} else {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
2012-09-15 11:51:55 -07:00
|
|
|
|
2012-10-15 13:54:58 -07:00
|
|
|
nsRefPtr<LogViolationDetailsRunnable> runnable =
|
|
|
|
new LogViolationDetailsRunnable(worker, fileName, lineNum);
|
2012-09-22 05:45:00 -07:00
|
|
|
|
2012-10-15 13:54:58 -07:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
2012-09-15 11:51:55 -07:00
|
|
|
}
|
|
|
|
|
2012-10-15 13:54:58 -07:00
|
|
|
return worker->IsEvalAllowed();
|
2012-09-15 11:51:55 -07:00
|
|
|
}
|
|
|
|
|
2012-12-30 10:21:52 -08:00
|
|
|
void
|
|
|
|
CTypesActivityCallback(JSContext* aCx,
|
|
|
|
js::CTypesActivityType aType)
|
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case js::CTYPES_CALL_BEGIN:
|
|
|
|
worker->BeginCTypesCall();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case js::CTYPES_CALL_END:
|
|
|
|
worker->EndCTypesCall();
|
|
|
|
break;
|
|
|
|
|
2013-01-08 04:57:44 -08:00
|
|
|
case js::CTYPES_CALLBACK_BEGIN:
|
|
|
|
worker->BeginCTypesCallback();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case js::CTYPES_CALLBACK_END:
|
|
|
|
worker->EndCTypesCallback();
|
|
|
|
break;
|
|
|
|
|
2012-12-30 10:21:52 -08:00
|
|
|
default:
|
2013-06-28 18:38:30 -07:00
|
|
|
MOZ_CRASH("Unknown type flag!");
|
2012-12-30 10:21:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 11:31:30 -08:00
|
|
|
static nsIPrincipal*
|
|
|
|
GetPrincipalForAsmJSCacheOp()
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
if (!workerPrivate) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// asmjscache::OpenEntryForX guarnatee to only access the given nsIPrincipal
|
|
|
|
// from the main thread.
|
|
|
|
return workerPrivate->GetPrincipalDontAssertMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
AsmJSCacheOpenEntryForRead(JS::Handle<JSObject*> aGlobal,
|
2014-07-21 21:43:21 -07:00
|
|
|
const char16_t* aBegin,
|
|
|
|
const char16_t* aLimit,
|
2013-12-03 11:31:30 -08:00
|
|
|
size_t* aSize,
|
|
|
|
const uint8_t** aMemory,
|
|
|
|
intptr_t *aHandle)
|
|
|
|
{
|
|
|
|
nsIPrincipal* principal = GetPrincipalForAsmJSCacheOp();
|
|
|
|
if (!principal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return asmjscache::OpenEntryForRead(principal, aBegin, aLimit, aSize, aMemory,
|
|
|
|
aHandle);
|
|
|
|
}
|
|
|
|
|
2014-10-22 15:28:07 -07:00
|
|
|
static JS::AsmJSCacheResult
|
2013-12-03 11:31:30 -08:00
|
|
|
AsmJSCacheOpenEntryForWrite(JS::Handle<JSObject*> aGlobal,
|
2014-03-05 12:47:10 -08:00
|
|
|
bool aInstalled,
|
2014-07-21 21:43:21 -07:00
|
|
|
const char16_t* aBegin,
|
|
|
|
const char16_t* aEnd,
|
2013-12-03 11:31:30 -08:00
|
|
|
size_t aSize,
|
|
|
|
uint8_t** aMemory,
|
|
|
|
intptr_t* aHandle)
|
|
|
|
{
|
|
|
|
nsIPrincipal* principal = GetPrincipalForAsmJSCacheOp();
|
|
|
|
if (!principal) {
|
2014-10-22 15:28:07 -07:00
|
|
|
return JS::AsmJSCache_InternalError;
|
2013-12-03 11:31:30 -08:00
|
|
|
}
|
|
|
|
|
2014-03-05 12:47:10 -08:00
|
|
|
return asmjscache::OpenEntryForWrite(principal, aInstalled, aBegin, aEnd,
|
|
|
|
aSize, aMemory, aHandle);
|
2013-12-03 11:31:30 -08:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:21:26 -07:00
|
|
|
class WorkerJSRuntime;
|
|
|
|
|
|
|
|
class WorkerThreadRuntimePrivate : private PerThreadAtomCache
|
2013-08-22 22:17:09 -07:00
|
|
|
{
|
2015-04-08 15:21:26 -07:00
|
|
|
friend class WorkerJSRuntime;
|
|
|
|
|
2013-08-22 22:17:09 -07:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2015-04-08 15:21:26 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
// This can't return null, but we can't lose the "Get" prefix in the name or
|
|
|
|
// it will be ambiguous with the WorkerPrivate class name.
|
|
|
|
WorkerPrivate*
|
|
|
|
GetWorkerPrivate() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-04-08 18:49:38 -07:00
|
|
|
explicit
|
2015-04-08 15:21:26 -07:00
|
|
|
WorkerThreadRuntimePrivate(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
// Zero out the base class members.
|
|
|
|
memset(this, 0, sizeof(PerThreadAtomCache));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerThreadRuntimePrivate()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerThreadRuntimePrivate(const WorkerThreadRuntimePrivate&) = delete;
|
|
|
|
|
|
|
|
WorkerThreadRuntimePrivate&
|
|
|
|
operator=(const WorkerThreadRuntimePrivate&) = delete;
|
2013-08-22 22:17:09 -07:00
|
|
|
};
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
JSContext*
|
2013-08-03 16:55:40 -07:00
|
|
|
CreateJSContextForWorker(WorkerPrivate* aWorkerPrivate, JSRuntime* aRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(!aWorkerPrivate->GetJSContext(), "Already has a context!");
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
JSSettings settings;
|
|
|
|
aWorkerPrivate->CopyJSSettings(settings);
|
|
|
|
|
2014-02-26 01:25:36 -08:00
|
|
|
JS::RuntimeOptionsRef(aRuntime) = settings.runtimeOptions;
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
JSSettings::JSGCSettingsArray& gcSettings = settings.gcSettings;
|
|
|
|
|
2012-01-04 11:11:32 -08:00
|
|
|
// This is the real place where we set the max memory for the runtime.
|
2013-05-16 15:49:43 -07:00
|
|
|
for (uint32_t index = 0; index < ArrayLength(gcSettings); index++) {
|
|
|
|
const JSSettings::JSGCSetting& setting = gcSettings[index];
|
|
|
|
if (setting.IsSet()) {
|
|
|
|
NS_ASSERTION(setting.value, "Can't handle 0 values!");
|
2013-08-03 16:55:40 -07:00
|
|
|
JS_SetGCParameter(aRuntime, setting.key, setting.value);
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
|
|
|
}
|
2012-01-04 11:11:32 -08:00
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
JS_SetNativeStackQuota(aRuntime, WORKER_CONTEXT_NATIVE_STACK_LIMIT);
|
2012-01-31 14:28:22 -08:00
|
|
|
|
2012-09-15 11:51:55 -07:00
|
|
|
// Security policy:
|
2014-12-16 16:30:39 -08:00
|
|
|
static const JSSecurityCallbacks securityCallbacks = {
|
2012-09-15 11:51:55 -07:00
|
|
|
ContentSecurityPolicyAllows
|
|
|
|
};
|
2013-08-03 16:55:40 -07:00
|
|
|
JS_SetSecurityCallbacks(aRuntime, &securityCallbacks);
|
2012-09-15 11:51:55 -07:00
|
|
|
|
2013-12-03 11:31:30 -08:00
|
|
|
// Set up the asm.js cache callbacks
|
2014-12-16 16:30:39 -08:00
|
|
|
static const JS::AsmJSCacheOps asmJSCacheOps = {
|
2013-12-03 11:31:30 -08:00
|
|
|
AsmJSCacheOpenEntryForRead,
|
|
|
|
asmjscache::CloseEntryForRead,
|
|
|
|
AsmJSCacheOpenEntryForWrite,
|
|
|
|
asmjscache::CloseEntryForWrite,
|
|
|
|
asmjscache::GetBuildId
|
|
|
|
};
|
|
|
|
JS::SetAsmJSCacheOps(aRuntime, &asmJSCacheOps);
|
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
JSContext* workerCx = JS_NewContext(aRuntime, 0);
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!workerCx) {
|
|
|
|
NS_WARNING("Could not create new context!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-09-09 16:02:10 -07:00
|
|
|
JS_SetErrorReporter(aRuntime, ErrorReporter);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-03-10 14:28:43 -07:00
|
|
|
JS_SetInterruptCallback(aRuntime, InterruptCallback);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
js::SetCTypesActivityCallback(aRuntime, CTypesActivityCallback);
|
2012-12-30 10:21:52 -08:00
|
|
|
|
2014-07-29 21:00:37 -07:00
|
|
|
JS::ContextOptionsRef(workerCx) = kRequiredContextOptions;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
JS_SetGCZeal(workerCx, settings.gcZeal, settings.gcZealFrequency);
|
2011-07-17 12:09:13 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return workerCx;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:27:55 -07:00
|
|
|
static bool
|
|
|
|
PreserveWrapper(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
MOZ_ASSERT(obj);
|
|
|
|
MOZ_ASSERT(mozilla::dom::IsDOMObject(obj));
|
|
|
|
|
|
|
|
return mozilla::dom::TryPreserveWrapper(obj);
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:51:53 -08:00
|
|
|
JSObject*
|
|
|
|
Wrap(JSContext *cx, JS::HandleObject existing, JS::HandleObject obj)
|
|
|
|
{
|
|
|
|
JSObject* targetGlobal = JS::CurrentGlobalOrNull(cx);
|
2015-03-31 03:22:40 -07:00
|
|
|
if (!IsDebuggerGlobal(targetGlobal) && !IsDebuggerSandbox(targetGlobal)) {
|
2015-03-03 15:51:53 -08:00
|
|
|
MOZ_CRASH("There should be no edges from the debuggee to the debugger.");
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* originGlobal = js::GetGlobalForObjectCrossCompartment(obj);
|
|
|
|
|
|
|
|
const js::Wrapper* wrapper = nullptr;
|
2015-03-31 03:22:40 -07:00
|
|
|
if (IsDebuggerGlobal(originGlobal) || IsDebuggerSandbox(originGlobal)) {
|
2015-03-03 15:51:53 -08:00
|
|
|
wrapper = &js::CrossCompartmentWrapper::singleton;
|
|
|
|
} else {
|
2015-06-02 07:54:54 -07:00
|
|
|
wrapper = &js::OpaqueCrossCompartmentWrapper::singleton;
|
2015-03-03 15:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
js::Wrapper::Renew(cx, existing, obj, wrapper);
|
|
|
|
}
|
|
|
|
return js::Wrapper::New(cx, obj, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const JSWrapObjectCallbacks WrapObjectCallbacks = {
|
|
|
|
Wrap,
|
|
|
|
nullptr,
|
|
|
|
};
|
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
class WorkerJSRuntime : public mozilla::CycleCollectedJSRuntime
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// The heap size passed here doesn't matter, we will change it later in the
|
|
|
|
// call to JS_SetGCParameter inside CreateJSContextForWorker.
|
2014-02-19 08:02:13 -08:00
|
|
|
WorkerJSRuntime(JSRuntime* aParentRuntime, WorkerPrivate* aWorkerPrivate)
|
|
|
|
: CycleCollectedJSRuntime(aParentRuntime,
|
2014-07-29 15:38:14 -07:00
|
|
|
WORKER_DEFAULT_RUNTIME_HEAPSIZE,
|
|
|
|
WORKER_DEFAULT_NURSERY_SIZE),
|
2013-08-13 17:07:40 -07:00
|
|
|
mWorkerPrivate(aWorkerPrivate)
|
2014-02-19 08:02:13 -08:00
|
|
|
{
|
2015-04-08 15:21:26 -07:00
|
|
|
JSRuntime* rt = Runtime();
|
|
|
|
MOZ_ASSERT(rt);
|
|
|
|
|
|
|
|
JS_SetRuntimePrivate(rt, new WorkerThreadRuntimePrivate(aWorkerPrivate));
|
|
|
|
|
|
|
|
js::SetPreserveWrapperCallback(rt, PreserveWrapper);
|
|
|
|
JS_InitDestroyPrincipalsCallback(rt, DestroyWorkerPrincipals);
|
|
|
|
JS_SetWrapObjectCallbacks(rt, &WrapObjectCallbacks);
|
2014-02-19 08:02:13 -08:00
|
|
|
}
|
2013-08-03 16:55:40 -07:00
|
|
|
|
|
|
|
~WorkerJSRuntime()
|
|
|
|
{
|
2015-04-08 15:21:26 -07:00
|
|
|
JSRuntime* rt = Runtime();
|
|
|
|
MOZ_ASSERT(rt);
|
|
|
|
|
|
|
|
delete static_cast<WorkerThreadRuntimePrivate*>(JS_GetRuntimePrivate(rt));
|
|
|
|
JS_SetRuntimePrivate(rt, nullptr);
|
2013-08-22 22:17:09 -07:00
|
|
|
|
2013-09-24 08:03:23 -07:00
|
|
|
// The worker global should be unrooted and the shutdown cycle collection
|
|
|
|
// should break all remaining cycles. The superclass destructor will run
|
|
|
|
// the GC one final time and finalize any JSObjects that were participating
|
2013-08-03 16:55:40 -07:00
|
|
|
// in cycles that were broken during CC shutdown.
|
|
|
|
nsCycleCollector_shutdown();
|
2013-08-13 17:07:40 -07:00
|
|
|
|
2013-09-24 08:03:23 -07:00
|
|
|
// The CC is shut down, and the superclass destructor will GC, so make sure
|
|
|
|
// we don't try to CC again.
|
2013-08-13 17:07:40 -07:00
|
|
|
mWorkerPrivate = nullptr;
|
2013-08-03 16:55:40 -07:00
|
|
|
}
|
|
|
|
|
2013-11-20 14:35:16 -08:00
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
PrepareForForgetSkippable() override
|
2013-11-20 14:35:16 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
BeginCycleCollectionCallback() override
|
2013-11-20 14:35:16 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
EndCycleCollectionCallback(CycleCollectorResults &aResults) override
|
2013-11-20 14:35:16 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
void
|
2015-03-21 09:28:04 -07:00
|
|
|
DispatchDeferredDeletion(bool aContinuation) override
|
2013-08-03 16:55:40 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aContinuation);
|
|
|
|
|
|
|
|
// Do it immediately, no need for asynchronous behavior here.
|
|
|
|
nsCycleCollector_doDeferredDeletion();
|
|
|
|
}
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void CustomGCCallback(JSGCStatus aStatus) override
|
2013-08-13 17:07:40 -07:00
|
|
|
{
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
// We're shutting down, no need to do anything.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus == JSGC_END) {
|
2013-11-20 14:35:17 -08:00
|
|
|
nsCycleCollector_collect(nullptr);
|
2013-08-13 17:07:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 06:10:46 -07:00
|
|
|
virtual void AfterProcessTask(uint32_t aRecursionDepth) override
|
|
|
|
{
|
|
|
|
// Only perform the Promise microtask checkpoint on the outermost event
|
|
|
|
// loop. Don't run it, for example, during sync XHR or importScripts.
|
|
|
|
if (aRecursionDepth == 2) {
|
|
|
|
CycleCollectedJSRuntime::AfterProcessTask(aRecursionDepth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 16:55:40 -07:00
|
|
|
private:
|
2013-08-13 17:07:40 -07:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-08-03 16:55:40 -07:00
|
|
|
};
|
|
|
|
|
2014-11-14 18:47:30 -08:00
|
|
|
#ifdef ENABLE_TESTS
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class TestPBackgroundCreateCallback final :
|
2014-11-14 18:47:30 -08:00
|
|
|
public nsIIPCBackgroundChildCreateCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
ActorCreated(PBackgroundChild* aActor) override
|
2014-11-14 18:47:30 -08:00
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aActor);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
ActorFailed() override
|
2014-11-14 18:47:30 -08:00
|
|
|
{
|
|
|
|
MOZ_CRASH("TestPBackground() should not fail "
|
|
|
|
"GetOrCreateForCurrentThread()");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~TestPBackgroundCreateCallback()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(TestPBackgroundCreateCallback,
|
|
|
|
nsIIPCBackgroundChildCreateCallback);
|
|
|
|
|
|
|
|
void
|
|
|
|
TestPBackground()
|
|
|
|
{
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
if (gTestPBackground) {
|
|
|
|
// Randomize value to validate workers are not cross-posting messages.
|
|
|
|
uint32_t testValue;
|
|
|
|
size_t randomSize = PR_GetRandomNoise(&testValue, sizeof(testValue));
|
|
|
|
MOZ_RELEASE_ASSERT(randomSize == sizeof(testValue));
|
|
|
|
nsCString testStr;
|
|
|
|
testStr.AppendInt(testValue);
|
|
|
|
testStr.AppendInt(reinterpret_cast<int64_t>(PR_GetCurrentThread()));
|
|
|
|
PBackgroundChild* existingBackgroundChild =
|
|
|
|
BackgroundChild::GetForCurrentThread();
|
|
|
|
MOZ_RELEASE_ASSERT(existingBackgroundChild);
|
|
|
|
bool ok = existingBackgroundChild->SendPBackgroundTestConstructor(testStr);
|
|
|
|
MOZ_RELEASE_ASSERT(ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_TESTS
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class WorkerBackgroundChildCallback final :
|
2014-07-09 14:39:18 -07:00
|
|
|
public nsIIPCBackgroundChildCreateCallback
|
|
|
|
{
|
|
|
|
bool* mDone;
|
|
|
|
|
|
|
|
public:
|
2014-09-01 15:26:43 -07:00
|
|
|
explicit WorkerBackgroundChildCallback(bool* aDone)
|
2014-07-09 14:39:18 -07:00
|
|
|
: mDone(aDone)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mDone);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
private:
|
|
|
|
~WorkerBackgroundChildCallback()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
ActorCreated(PBackgroundChild* aActor) override
|
2014-07-09 14:39:18 -07:00
|
|
|
{
|
|
|
|
*mDone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
ActorFailed() override
|
2014-07-09 14:39:18 -07:00
|
|
|
{
|
|
|
|
*mDone = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class WorkerThreadPrimaryRunnable final : public nsRunnable
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2014-11-14 18:47:30 -08:00
|
|
|
nsRefPtr<WorkerThread> mThread;
|
2014-02-19 08:02:13 -08:00
|
|
|
JSRuntime* mParentRuntime;
|
2013-10-23 06:16:49 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class FinishedRunnable final : public nsRunnable
|
2013-10-23 06:16:49 -07:00
|
|
|
{
|
2014-11-14 18:47:30 -08:00
|
|
|
nsRefPtr<WorkerThread> mThread;
|
2013-10-23 06:16:49 -07:00
|
|
|
|
|
|
|
public:
|
2014-11-14 18:47:30 -08:00
|
|
|
explicit FinishedRunnable(already_AddRefed<WorkerThread> aThread)
|
2013-10-23 06:16:49 -07:00
|
|
|
: mThread(aThread)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~FinishedRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
public:
|
2013-10-23 06:16:49 -07:00
|
|
|
WorkerThreadPrimaryRunnable(WorkerPrivate* aWorkerPrivate,
|
2014-11-14 18:47:30 -08:00
|
|
|
WorkerThread* aThread,
|
2014-02-19 08:02:13 -08:00
|
|
|
JSRuntime* aParentRuntime)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mThread(aThread), mParentRuntime(aParentRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aThread);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~WorkerThreadPrimaryRunnable()
|
|
|
|
{ }
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
nsresult
|
|
|
|
SynchronouslyCreatePBackground();
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class WorkerTaskRunnable final : public WorkerRunnable
|
2013-10-23 06:16:49 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<WorkerTask> mTask;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerTaskRunnable(WorkerPrivate* aWorkerPrivate, WorkerTask* aTask)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount), mTask(aTask)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 09:28:04 -07:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 06:16:49 -07:00
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
virtual void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 09:28:04 -07:00
|
|
|
bool aDispatchResult) override
|
2013-10-23 06:16:49 -07:00
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
}
|
2013-08-03 16:55:40 -07:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
virtual bool
|
2015-03-21 09:28:04 -07:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 06:16:49 -07:00
|
|
|
{
|
|
|
|
return mTask->RunTask(aCx);
|
|
|
|
}
|
|
|
|
};
|
2013-08-03 16:55:40 -07:00
|
|
|
|
2014-09-05 07:26:34 -07:00
|
|
|
void
|
|
|
|
PrefLanguagesChanged(const char* /* aPrefName */, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsTArray<nsString> languages;
|
|
|
|
Navigator::GetAcceptLanguages(languages);
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdateAllWorkerLanguages(languages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:11:18 -07:00
|
|
|
void
|
|
|
|
AppNameOverrideChanged(const char* /* aPrefName */, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
const nsAdoptingString& override =
|
|
|
|
mozilla::Preferences::GetString(PREF_GENERAL_APPNAME_OVERRIDE);
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdateAppNameOverridePreference(override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AppVersionOverrideChanged(const char* /* aPrefName */, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
const nsAdoptingString& override =
|
|
|
|
mozilla::Preferences::GetString(PREF_GENERAL_APPVERSION_OVERRIDE);
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdateAppVersionOverridePreference(override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PlatformOverrideChanged(const char* /* aPrefName */, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
const nsAdoptingString& override =
|
|
|
|
mozilla::Preferences::GetString(PREF_GENERAL_PLATFORM_OVERRIDE);
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
|
|
|
runtime->UpdatePlatformOverridePreference(override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class BackgroundChildCallback final
|
2014-12-16 22:26:15 -08:00
|
|
|
: public nsIIPCBackgroundChildCreateCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BackgroundChildCallback()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
private:
|
|
|
|
~BackgroundChildCallback()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
ActorCreated(PBackgroundChild* aActor) override
|
2014-12-16 22:26:15 -08:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aActor);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 09:28:04 -07:00
|
|
|
ActorFailed() override
|
2014-12-16 22:26:15 -08:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_CRASH("Unable to connect PBackground actor for the main thread!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(BackgroundChildCallback, nsIIPCBackgroundChildCreateCallback)
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
} /* anonymous namespace */
|
2013-08-03 16:55:40 -07:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
CancelWorkersForWindow(nsPIDOMWindow* aWindow)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
2013-06-05 07:04:23 -07:00
|
|
|
runtime->CancelWorkersForWindow(aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-01 02:00:19 -07:00
|
|
|
FreezeWorkersForWindow(nsPIDOMWindow* aWindow)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
2015-04-01 02:00:19 -07:00
|
|
|
runtime->FreezeWorkersForWindow(aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-01 02:00:19 -07:00
|
|
|
ThawWorkersForWindow(nsPIDOMWindow* aWindow)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
if (runtime) {
|
2015-04-01 02:00:19 -07:00
|
|
|
runtime->ThawWorkersForWindow(aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
WorkerCrossThreadDispatcher::WorkerCrossThreadDispatcher(
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mMutex("WorkerCrossThreadDispatcher::mMutex"),
|
|
|
|
mWorkerPrivate(aWorkerPrivate)
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerCrossThreadDispatcher::PostTask(WorkerTask* aTask)
|
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
NS_WARNING("Posted a task to a WorkerCrossThreadDispatcher that is no "
|
|
|
|
"longer accepting tasks!");
|
2011-12-04 23:58:27 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
nsRefPtr<WorkerTaskRunnable> runnable =
|
|
|
|
new WorkerTaskRunnable(mWorkerPrivate, aTask);
|
|
|
|
return runnable->Dispatch(nullptr);
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
2013-08-22 22:17:09 -07:00
|
|
|
WorkerPrivate*
|
|
|
|
GetWorkerPrivateFromContext(JSContext* aCx)
|
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
|
|
|
JSRuntime* rt = JS_GetRuntime(aCx);
|
|
|
|
MOZ_ASSERT(rt);
|
|
|
|
|
|
|
|
void* rtPrivate = JS_GetRuntimePrivate(rt);
|
|
|
|
MOZ_ASSERT(rtPrivate);
|
|
|
|
|
2015-04-08 15:21:26 -07:00
|
|
|
return
|
|
|
|
static_cast<WorkerThreadRuntimePrivate*>(rtPrivate)->GetWorkerPrivate();
|
2013-08-22 22:17:09 -07:00
|
|
|
}
|
|
|
|
|
2013-09-08 20:29:21 -07:00
|
|
|
WorkerPrivate*
|
|
|
|
GetCurrentThreadWorkerPrivate()
|
2013-08-30 02:47:19 -07:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2013-09-08 20:28:50 -07:00
|
|
|
CycleCollectedJSRuntime* ccrt = CycleCollectedJSRuntime::Get();
|
2013-08-30 02:47:19 -07:00
|
|
|
if (!ccrt) {
|
2013-09-08 20:29:21 -07:00
|
|
|
return nullptr;
|
2013-08-30 02:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSRuntime* rt = ccrt->Runtime();
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(rt);
|
|
|
|
|
|
|
|
void* rtPrivate = JS_GetRuntimePrivate(rt);
|
|
|
|
MOZ_ASSERT(rtPrivate);
|
|
|
|
|
2015-04-08 15:21:26 -07:00
|
|
|
return
|
|
|
|
static_cast<WorkerThreadRuntimePrivate*>(rtPrivate)->GetWorkerPrivate();
|
2013-09-08 20:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsCurrentThreadRunningChromeWorker()
|
|
|
|
{
|
|
|
|
return GetCurrentThreadWorkerPrivate()->UsesSystemPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext*
|
|
|
|
GetCurrentThreadJSContext()
|
|
|
|
{
|
|
|
|
return GetCurrentThreadWorkerPrivate()->GetJSContext();
|
2013-08-30 02:47:19 -07:00
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
2014-11-14 18:47:30 -08:00
|
|
|
struct RuntimeService::IdleThreadInfo
|
|
|
|
{
|
|
|
|
nsRefPtr<WorkerThread> mThread;
|
|
|
|
mozilla::TimeStamp mExpirationTime;
|
|
|
|
};
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
// This is only touched on the main thread. Initialized in Init() below.
|
|
|
|
JSSettings RuntimeService::sDefaultJSSettings;
|
2013-11-24 11:27:15 -08:00
|
|
|
bool RuntimeService::sDefaultPreferences[WORKERPREF_COUNT] = { false };
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
RuntimeService::RuntimeService()
|
2011-07-25 18:49:16 -07:00
|
|
|
: mMutex("RuntimeService::mMutex"), mObserved(false),
|
2013-11-19 15:08:50 -08:00
|
|
|
mShuttingDown(false), mNavigatorPropertiesLoaded(false)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
NS_ASSERTION(!gRuntimeService, "More than one service!");
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeService::~RuntimeService()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// gRuntimeService can be null if Init() fails.
|
|
|
|
NS_ASSERTION(!gRuntimeService || gRuntimeService == this,
|
|
|
|
"More than one service!");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
gRuntimeService = nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
RuntimeService*
|
|
|
|
RuntimeService::GetOrCreateService()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!gRuntimeService) {
|
2014-10-08 05:56:50 -07:00
|
|
|
// The observer service now owns us until shutdown.
|
|
|
|
gRuntimeService = new RuntimeService();
|
|
|
|
if (NS_FAILED(gRuntimeService->Init())) {
|
2011-07-17 12:09:13 -07:00
|
|
|
NS_WARNING("Failed to initialize!");
|
2014-10-08 05:56:50 -07:00
|
|
|
gRuntimeService->Cleanup();
|
|
|
|
gRuntimeService = nullptr;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
#ifdef ENABLE_TESTS
|
|
|
|
gTestPBackground = mozilla::Preferences::GetBool("pbackground.testing", false);
|
|
|
|
#endif // ENABLE_TESTS
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return gRuntimeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
RuntimeService*
|
|
|
|
RuntimeService::GetService()
|
|
|
|
{
|
|
|
|
return gRuntimeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RuntimeService::RegisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
|
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mShuttingDown) {
|
|
|
|
JS_ReportError(aCx, "Cannot create worker during shutdown!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
nsCString sharedWorkerScriptSpec;
|
|
|
|
|
2015-03-25 10:17:00 -07:00
|
|
|
const bool isServiceWorker = aWorkerPrivate->IsServiceWorker();
|
|
|
|
if (isServiceWorker) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
Telemetry::Accumulate(Telemetry::SERVICE_WORKER_SPAWN_ATTEMPTS, 1);
|
|
|
|
}
|
|
|
|
|
2015-08-12 03:00:00 -07:00
|
|
|
const bool isSharedWorker = aWorkerPrivate->IsSharedWorker();
|
|
|
|
if (isSharedWorker || isServiceWorker) {
|
2013-06-05 07:04:23 -07:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scriptURI = aWorkerPrivate->GetResolvedScriptURI();
|
|
|
|
NS_ASSERTION(scriptURI, "Null script URI!");
|
|
|
|
|
|
|
|
nsresult rv = scriptURI->GetSpec(sharedWorkerScriptSpec);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("GetSpec failed?!");
|
|
|
|
xpc::Throw(aCx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!sharedWorkerScriptSpec.IsEmpty(), "Empty spec!");
|
|
|
|
}
|
|
|
|
|
2015-03-03 14:30:58 -08:00
|
|
|
bool exemptFromPerDomainMax = false;
|
2015-08-12 03:00:00 -07:00
|
|
|
if (isServiceWorker) {
|
2015-03-03 14:30:58 -08:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
exemptFromPerDomainMax = Preferences::GetBool("dom.serviceWorkers.exemptFromPerDomainMax",
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
const nsCString& domain = aWorkerPrivate->Domain();
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
WorkerDomainInfo* domainInfo;
|
|
|
|
bool queued = false;
|
|
|
|
{
|
2011-07-25 18:49:16 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
if (!mDomainMap.Get(domain, &domainInfo)) {
|
|
|
|
NS_ASSERTION(!parent, "Shouldn't have a parent here!");
|
|
|
|
|
|
|
|
domainInfo = new WorkerDomainInfo();
|
|
|
|
domainInfo->mDomain = domain;
|
2012-05-18 10:30:49 -07:00
|
|
|
mDomainMap.Put(domain, domainInfo);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
queued = gMaxWorkersPerDomain &&
|
|
|
|
domainInfo->ActiveWorkerCount() >= gMaxWorkersPerDomain &&
|
2015-03-03 14:30:58 -08:00
|
|
|
!domain.IsEmpty() &&
|
|
|
|
!exemptFromPerDomainMax;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
if (queued) {
|
|
|
|
domainInfo->mQueuedWorkers.AppendElement(aWorkerPrivate);
|
2015-08-12 03:00:00 -07:00
|
|
|
if (isServiceWorker || isSharedWorker) {
|
2015-03-25 10:17:00 -07:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
// ServiceWorker spawn gets queued due to hitting max workers per domain
|
|
|
|
// limit so let's log a warning.
|
|
|
|
// Note: aWorkerPrivate->GetDocument() call might result nullptr due to
|
|
|
|
// no window so the message warning will show up in the browser console.
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
NS_LITERAL_CSTRING("DOM"),
|
|
|
|
aWorkerPrivate->GetDocument(),
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"HittingMaxWorkersPerDomain");
|
2015-08-12 03:00:00 -07:00
|
|
|
Telemetry::Accumulate(isSharedWorker ? Telemetry::SHARED_WORKER_SPAWN_GETS_QUEUED
|
|
|
|
: Telemetry::SERVICE_WORKER_SPAWN_GETS_QUEUED, 1);
|
2015-03-25 10:17:00 -07:00
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
else if (parent) {
|
|
|
|
domainInfo->mChildWorkerCount++;
|
|
|
|
}
|
2015-06-26 15:51:56 -07:00
|
|
|
else if (isServiceWorker) {
|
|
|
|
domainInfo->mActiveServiceWorkers.AppendElement(aWorkerPrivate);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
else {
|
|
|
|
domainInfo->mActiveWorkers.AppendElement(aWorkerPrivate);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2015-08-12 03:00:00 -07:00
|
|
|
if (isSharedWorker || isServiceWorker) {
|
2014-05-12 09:11:15 -07:00
|
|
|
const nsCString& sharedWorkerName = aWorkerPrivate->SharedWorkerName();
|
2015-03-19 16:40:52 -07:00
|
|
|
const nsCString& cacheName =
|
|
|
|
aWorkerPrivate->IsServiceWorker() ?
|
|
|
|
NS_ConvertUTF16toUTF8(aWorkerPrivate->ServiceWorkerCacheName()) :
|
|
|
|
EmptyCString();
|
2014-05-12 09:11:15 -07:00
|
|
|
|
2014-02-06 06:39:10 -08:00
|
|
|
nsAutoCString key;
|
2015-03-10 08:44:04 -07:00
|
|
|
GenerateSharedWorkerKey(sharedWorkerScriptSpec, sharedWorkerName,
|
2015-06-26 11:18:18 -07:00
|
|
|
cacheName, aWorkerPrivate->Type(),
|
|
|
|
aWorkerPrivate->IsInPrivateBrowsing(), key);
|
2014-02-06 06:39:10 -08:00
|
|
|
MOZ_ASSERT(!domainInfo->mSharedWorkerInfos.Get(key));
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
SharedWorkerInfo* sharedWorkerInfo =
|
|
|
|
new SharedWorkerInfo(aWorkerPrivate, sharedWorkerScriptSpec,
|
|
|
|
sharedWorkerName);
|
2014-02-06 06:39:10 -08:00
|
|
|
domainInfo->mSharedWorkerInfos.Put(key, sharedWorkerInfo);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// From here on out we must call UnregisterWorker if something fails!
|
|
|
|
if (parent) {
|
|
|
|
if (!parent->AddChildWorker(aCx, aWorkerPrivate)) {
|
|
|
|
UnregisterWorker(aCx, aWorkerPrivate);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-11-19 15:08:50 -08:00
|
|
|
if (!mNavigatorPropertiesLoaded) {
|
2014-09-23 12:11:18 -07:00
|
|
|
Navigator::AppName(mNavigatorProperties.mAppName,
|
|
|
|
false /* aUsePrefOverriddenValue */);
|
|
|
|
if (NS_FAILED(Navigator::GetAppVersion(mNavigatorProperties.mAppVersion,
|
|
|
|
false /* aUsePrefOverriddenValue */)) ||
|
|
|
|
NS_FAILED(Navigator::GetPlatform(mNavigatorProperties.mPlatform,
|
2014-09-24 09:09:50 -07:00
|
|
|
false /* aUsePrefOverriddenValue */))) {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_ReportError(aCx, "Failed to load navigator strings!");
|
|
|
|
UnregisterWorker(aCx, aWorkerPrivate);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:11:18 -07:00
|
|
|
// The navigator overridden properties should have already been read.
|
|
|
|
|
2014-09-05 07:26:34 -07:00
|
|
|
Navigator::GetAcceptLanguages(mNavigatorProperties.mLanguages);
|
2013-11-19 15:08:50 -08:00
|
|
|
mNavigatorPropertiesLoaded = true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* window = aWorkerPrivate->GetWindow();
|
|
|
|
|
|
|
|
nsTArray<WorkerPrivate*>* windowArray;
|
|
|
|
if (!mWindowMap.Get(window, &windowArray)) {
|
|
|
|
windowArray = new nsTArray<WorkerPrivate*>(1);
|
2012-05-18 10:30:49 -07:00
|
|
|
mWindowMap.Put(window, windowArray);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
if (!windowArray->Contains(aWorkerPrivate)) {
|
|
|
|
windowArray->AppendElement(aWorkerPrivate);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsSharedWorker());
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!queued && !ScheduleWorker(aCx, aWorkerPrivate)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:17:00 -07:00
|
|
|
if (isServiceWorker) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
Telemetry::Accumulate(Telemetry::SERVICE_WORKER_WAS_SPAWNED, 1);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuntimeService::UnregisterWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
|
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
const nsCString& domain = aWorkerPrivate->Domain();
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
WorkerPrivate* queuedWorker = nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2011-07-25 18:49:16 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
WorkerDomainInfo* domainInfo;
|
|
|
|
if (!mDomainMap.Get(domain, &domainInfo)) {
|
|
|
|
NS_ERROR("Don't have an entry for this domain!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old worker from everywhere.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t index = domainInfo->mQueuedWorkers.IndexOf(aWorkerPrivate);
|
2011-07-17 12:09:13 -07:00
|
|
|
if (index != kNoIndex) {
|
|
|
|
// Was queued, remove from the list.
|
|
|
|
domainInfo->mQueuedWorkers.RemoveElementAt(index);
|
|
|
|
}
|
|
|
|
else if (parent) {
|
2015-06-26 15:51:56 -07:00
|
|
|
MOZ_ASSERT(domainInfo->mChildWorkerCount, "Must be non-zero!");
|
2011-07-17 12:09:13 -07:00
|
|
|
domainInfo->mChildWorkerCount--;
|
|
|
|
}
|
2015-06-26 15:51:56 -07:00
|
|
|
else if (aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
MOZ_ASSERT(domainInfo->mActiveServiceWorkers.Contains(aWorkerPrivate),
|
|
|
|
"Don't know about this worker!");
|
|
|
|
domainInfo->mActiveServiceWorkers.RemoveElement(aWorkerPrivate);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
else {
|
2015-06-26 15:51:56 -07:00
|
|
|
MOZ_ASSERT(domainInfo->mActiveWorkers.Contains(aWorkerPrivate),
|
|
|
|
"Don't know about this worker!");
|
2011-07-17 12:09:13 -07:00
|
|
|
domainInfo->mActiveWorkers.RemoveElement(aWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
2014-05-29 09:19:00 -07:00
|
|
|
|
|
|
|
if (aWorkerPrivate->IsSharedWorker() ||
|
|
|
|
aWorkerPrivate->IsServiceWorker()) {
|
2013-06-05 07:04:23 -07:00
|
|
|
MatchSharedWorkerInfo match(aWorkerPrivate);
|
|
|
|
domainInfo->mSharedWorkerInfos.EnumerateRead(FindSharedWorkerInfo,
|
|
|
|
&match);
|
|
|
|
|
|
|
|
if (match.mSharedWorkerInfo) {
|
2014-02-06 06:39:10 -08:00
|
|
|
nsAutoCString key;
|
2015-03-19 16:40:52 -07:00
|
|
|
const nsCString& cacheName =
|
|
|
|
aWorkerPrivate->IsServiceWorker() ?
|
|
|
|
NS_ConvertUTF16toUTF8(aWorkerPrivate->ServiceWorkerCacheName()) :
|
|
|
|
EmptyCString();
|
2014-02-06 06:39:10 -08:00
|
|
|
GenerateSharedWorkerKey(match.mSharedWorkerInfo->mScriptSpec,
|
2015-03-10 08:44:04 -07:00
|
|
|
match.mSharedWorkerInfo->mName,
|
2015-06-26 11:18:18 -07:00
|
|
|
cacheName, aWorkerPrivate->Type(),
|
|
|
|
aWorkerPrivate->IsInPrivateBrowsing(), key);
|
2014-02-06 06:39:10 -08:00
|
|
|
domainInfo->mSharedWorkerInfos.Remove(key);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
// See if there's a queued worker we can schedule.
|
|
|
|
if (domainInfo->ActiveWorkerCount() < gMaxWorkersPerDomain &&
|
|
|
|
!domainInfo->mQueuedWorkers.IsEmpty()) {
|
|
|
|
queuedWorker = domainInfo->mQueuedWorkers[0];
|
|
|
|
domainInfo->mQueuedWorkers.RemoveElementAt(0);
|
|
|
|
|
|
|
|
if (queuedWorker->GetParent()) {
|
|
|
|
domainInfo->mChildWorkerCount++;
|
|
|
|
}
|
2015-06-26 15:51:56 -07:00
|
|
|
else if (queuedWorker->IsServiceWorker()) {
|
|
|
|
domainInfo->mActiveServiceWorkers.AppendElement(queuedWorker);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
else {
|
|
|
|
domainInfo->mActiveWorkers.AppendElement(queuedWorker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 14:32:52 -07:00
|
|
|
if (domainInfo->HasNoWorkers()) {
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(domainInfo->mQueuedWorkers.IsEmpty());
|
2011-07-17 12:09:13 -07:00
|
|
|
mDomainMap.Remove(domain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 23:11:18 -07:00
|
|
|
if (aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
Telemetry::AccumulateTimeDelta(Telemetry::SERVICE_WORKER_LIFE_TIME,
|
|
|
|
aWorkerPrivate->CreationTimeStamp());
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
if (aWorkerPrivate->IsSharedWorker()) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsAutoTArray<nsRefPtr<SharedWorker>, 5> sharedWorkersToNotify;
|
|
|
|
aWorkerPrivate->GetAllSharedWorkers(sharedWorkersToNotify);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < sharedWorkersToNotify.Length(); index++) {
|
|
|
|
MOZ_ASSERT(sharedWorkersToNotify[index]);
|
|
|
|
sharedWorkersToNotify[index]->NoteDeadWorker(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
if (parent) {
|
|
|
|
parent->RemoveChildWorker(aCx, aWorkerPrivate);
|
|
|
|
}
|
2014-05-12 09:11:15 -07:00
|
|
|
else if (aWorkerPrivate->IsSharedWorker() || aWorkerPrivate->IsServiceWorker()) {
|
2013-06-05 07:04:23 -07:00
|
|
|
mWindowMap.Enumerate(RemoveSharedWorkerFromWindowMap, aWorkerPrivate);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
else {
|
2013-06-05 07:04:23 -07:00
|
|
|
// May be null.
|
2011-07-17 12:09:13 -07:00
|
|
|
nsPIDOMWindow* window = aWorkerPrivate->GetWindow();
|
|
|
|
|
|
|
|
nsTArray<WorkerPrivate*>* windowArray;
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ALWAYS_TRUE(mWindowMap.Get(window, &windowArray));
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ALWAYS_TRUE(windowArray->RemoveElement(aWorkerPrivate));
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
if (windowArray->IsEmpty()) {
|
|
|
|
mWindowMap.Remove(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queuedWorker && !ScheduleWorker(aCx, queuedWorker)) {
|
|
|
|
UnregisterWorker(aCx, queuedWorker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RuntimeService::ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (!aWorkerPrivate->Start()) {
|
|
|
|
// This is ok, means that we didn't need to make a thread for this worker.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
nsRefPtr<WorkerThread> thread;
|
2011-07-25 18:49:16 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mIdleThreadArray.IsEmpty()) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t index = mIdleThreadArray.Length() - 1;
|
2011-07-25 18:49:16 -07:00
|
|
|
mIdleThreadArray[index].mThread.swap(thread);
|
|
|
|
mIdleThreadArray.RemoveElementAt(index);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-11-17 11:55:37 -08:00
|
|
|
const WorkerThreadFriendKey friendKey;
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
if (!thread) {
|
2014-11-17 11:55:37 -08:00
|
|
|
thread = WorkerThread::Create(friendKey);
|
2013-10-23 06:16:49 -07:00
|
|
|
if (!thread) {
|
2011-07-25 18:49:16 -07:00
|
|
|
UnregisterWorker(aCx, aWorkerPrivate);
|
|
|
|
JS_ReportError(aCx, "Could not create new thread!");
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-16 15:49:43 -07:00
|
|
|
}
|
2011-07-25 18:49:16 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
int32_t priority = aWorkerPrivate->IsChromeWorker() ?
|
|
|
|
nsISupportsPriority::PRIORITY_NORMAL :
|
|
|
|
nsISupportsPriority::PRIORITY_LOW;
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
if (NS_FAILED(thread->SetPriority(priority))) {
|
2013-05-16 15:49:43 -07:00
|
|
|
NS_WARNING("Could not set the thread's priority!");
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
2014-02-19 08:02:13 -08:00
|
|
|
new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread, JS_GetParentRuntime(aCx));
|
2015-07-09 20:21:46 -07:00
|
|
|
if (NS_FAILED(thread->DispatchPrimaryRunnable(friendKey, runnable.forget()))) {
|
2011-07-17 12:09:13 -07:00
|
|
|
UnregisterWorker(aCx, aWorkerPrivate);
|
2011-07-25 18:49:16 -07:00
|
|
|
JS_ReportError(aCx, "Could not dispatch to thread!");
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
RuntimeService::ShutdownIdleThreads(nsITimer* aTimer, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
|
|
|
|
|
|
|
NS_ASSERTION(aTimer == runtime->mIdleThreadTimer, "Wrong timer!");
|
|
|
|
|
|
|
|
// Cheat a little and grab all threads that expire within one second of now.
|
2015-05-07 10:35:57 -07:00
|
|
|
TimeStamp now = TimeStamp::NowLoRes() + TimeDuration::FromSeconds(1);
|
2011-07-25 18:49:16 -07:00
|
|
|
|
|
|
|
TimeStamp nextExpiration;
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
nsAutoTArray<nsRefPtr<WorkerThread>, 20> expiredThreads;
|
2011-07-25 18:49:16 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(runtime->mMutex);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < runtime->mIdleThreadArray.Length();
|
2011-07-25 18:49:16 -07:00
|
|
|
index++) {
|
|
|
|
IdleThreadInfo& info = runtime->mIdleThreadArray[index];
|
|
|
|
if (info.mExpirationTime > now) {
|
|
|
|
nextExpiration = info.mExpirationTime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
nsRefPtr<WorkerThread>* thread = expiredThreads.AppendElement();
|
2011-07-25 18:49:16 -07:00
|
|
|
thread->swap(info.mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!expiredThreads.IsEmpty()) {
|
|
|
|
runtime->mIdleThreadArray.RemoveElementsAt(0, expiredThreads.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nextExpiration.IsNull()) {
|
2015-05-07 10:35:57 -07:00
|
|
|
TimeDuration delta = nextExpiration - TimeStamp::NowLoRes();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t delay(delta > TimeDuration(0) ? delta.ToMilliseconds() : 0);
|
2011-07-25 18:49:16 -07:00
|
|
|
|
|
|
|
// Reschedule the timer.
|
2015-05-07 10:35:57 -07:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
aTimer->InitWithFuncCallback(ShutdownIdleThreads,
|
|
|
|
nullptr,
|
|
|
|
delay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT)));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < expiredThreads.Length(); index++) {
|
|
|
|
if (NS_FAILED(expiredThreads[index]->Shutdown())) {
|
|
|
|
NS_WARNING("Failed to shutdown thread!");
|
2011-07-25 18:49:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
nsresult
|
|
|
|
RuntimeService::Init()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-05-16 15:49:43 -07:00
|
|
|
|
2012-11-04 23:30:00 -08:00
|
|
|
nsLayoutStatics::AddRef();
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-12-16 22:26:15 -08:00
|
|
|
// Make sure PBackground actors are connected as soon as possible for the main
|
|
|
|
// thread in case workers clone remote blobs here.
|
|
|
|
if (!BackgroundChild::GetForCurrentThread()) {
|
|
|
|
nsRefPtr<BackgroundChildCallback> callback = new BackgroundChildCallback();
|
|
|
|
if (!BackgroundChild::GetOrCreateForCurrentThread(callback)) {
|
|
|
|
MOZ_CRASH("Unable to connect PBackground actor for the main thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
// Initialize JSSettings.
|
|
|
|
if (!sDefaultJSSettings.gcSettings[0].IsSet()) {
|
2014-02-26 01:25:36 -08:00
|
|
|
sDefaultJSSettings.runtimeOptions = JS::RuntimeOptions();
|
2013-05-16 15:49:43 -07:00
|
|
|
sDefaultJSSettings.chrome.maxScriptRuntime = -1;
|
2013-12-10 03:43:16 -08:00
|
|
|
sDefaultJSSettings.chrome.compartmentOptions.setVersion(JSVERSION_LATEST);
|
2013-05-16 15:49:43 -07:00
|
|
|
sDefaultJSSettings.content.maxScriptRuntime = MAX_SCRIPT_RUN_TIME_SEC;
|
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
sDefaultJSSettings.gcZealFrequency = JS_DEFAULT_ZEAL_FREQ;
|
|
|
|
sDefaultJSSettings.gcZeal = 0;
|
|
|
|
#endif
|
|
|
|
SetDefaultJSGCSettings(JSGC_MAX_BYTES, WORKER_DEFAULT_RUNTIME_HEAPSIZE);
|
|
|
|
SetDefaultJSGCSettings(JSGC_ALLOCATION_THRESHOLD,
|
|
|
|
WORKER_DEFAULT_ALLOCATION_THRESHOLD);
|
|
|
|
}
|
|
|
|
|
2013-11-24 11:27:15 -08:00
|
|
|
// If dump is not controlled by pref, it's set to true.
|
|
|
|
#ifndef DUMP_CONTROLLED_BY_PREF
|
|
|
|
sDefaultPreferences[WORKERPREF_DUMP] = true;
|
|
|
|
#endif
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
mIdleThreadTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
NS_ENSURE_STATE(mIdleThreadTimer);
|
|
|
|
|
2011-12-29 10:28:09 -08:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_ENSURE_TRUE(obs, NS_ERROR_FAILURE);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-12-29 10:28:09 -08:00
|
|
|
nsresult rv =
|
|
|
|
obs->AddObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID, false);
|
2011-07-17 12:09:13 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-09 10:54:05 -07:00
|
|
|
rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
mObserved = true;
|
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, GC_REQUEST_OBSERVER_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for GC request notifications!");
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:07:02 -08:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, CC_REQUEST_OBSERVER_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for CC request notifications!");
|
|
|
|
}
|
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, MEMORY_PRESSURE_OBSERVER_TOPIC,
|
|
|
|
false))) {
|
|
|
|
NS_WARNING("Failed to register for memory pressure notifications!");
|
|
|
|
}
|
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
if (NS_FAILED(obs->AddObserver(this, NS_IOSERVICE_OFFLINE_STATUS_TOPIC, false))) {
|
|
|
|
NS_WARNING("Failed to register for offline notification event!");
|
|
|
|
}
|
|
|
|
|
2014-10-27 02:38:14 -07:00
|
|
|
MOZ_ASSERT(!gRuntimeServiceDuringInit, "This should be false!");
|
|
|
|
gRuntimeServiceDuringInit = true;
|
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
if (NS_FAILED(Preferences::RegisterCallback(
|
|
|
|
LoadJSGCMemoryOptions,
|
|
|
|
PREF_JS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
LoadJSGCMemoryOptions,
|
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
NS_FAILED(Preferences::RegisterCallback(
|
|
|
|
LoadGCZealOptions,
|
|
|
|
PREF_JS_OPTIONS_PREFIX PREF_GCZEAL,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
LoadGCZealOptions,
|
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_GCZEAL,
|
|
|
|
nullptr)) ||
|
2013-10-30 14:40:34 -07:00
|
|
|
#endif
|
|
|
|
#if DUMP_CONTROLLED_BY_PREF
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
2013-12-10 03:43:16 -08:00
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_WINDOW_DUMP_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DUMP))) ||
|
2013-05-16 15:49:43 -07:00
|
|
|
#endif
|
2015-03-02 05:12:00 -08:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_CACHES_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DOM_CACHES))) ||
|
2015-06-25 18:53:02 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_WORKERNOTIFICATION_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DOM_WORKERNOTIFICATION))) ||
|
2015-04-03 22:46:07 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_SERVICEWORKERS_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_SERVICEWORKERS))) ||
|
2015-06-12 16:28:13 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_INTERCEPTION_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_INTERCEPTION_ENABLED))) ||
|
2015-07-02 20:09:20 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_INTERCEPTION_OPAQUE_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_INTERCEPTION_OPAQUE_ENABLED))) ||
|
2015-06-27 20:19:23 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_CACHES_TESTING_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DOM_CACHES_TESTING))) ||
|
2015-07-28 09:33:37 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_WORKERS_PERFORMANCE_LOGGING_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_PERFORMANCE_LOGGING_ENABLED))) ||
|
2015-06-27 20:19:24 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_SERVICEWORKERS_TESTING_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_SERVICEWORKERS_TESTING))) ||
|
2015-07-27 12:35:28 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_PUSH_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_PUSH))) ||
|
2015-07-27 14:57:34 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_REQUESTCONTEXT_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_REQUESTCONTEXT))) ||
|
2014-07-29 21:00:37 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallback(LoadRuntimeOptions,
|
2013-05-16 15:49:43 -07:00
|
|
|
PREF_JS_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
2014-07-29 21:00:37 -07:00
|
|
|
LoadRuntimeOptions,
|
2014-02-26 01:25:36 -08:00
|
|
|
PREF_WORKERS_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
2014-09-05 07:26:34 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(PrefLanguagesChanged,
|
|
|
|
PREF_INTL_ACCEPT_LANGUAGES,
|
|
|
|
nullptr)) ||
|
2014-09-23 12:11:18 -07:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
AppNameOverrideChanged,
|
|
|
|
PREF_GENERAL_APPNAME_OVERRIDE,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
AppVersionOverrideChanged,
|
|
|
|
PREF_GENERAL_APPVERSION_OVERRIDE,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
PlatformOverrideChanged,
|
|
|
|
PREF_GENERAL_PLATFORM_OVERRIDE,
|
|
|
|
nullptr)) ||
|
2013-12-10 03:43:16 -08:00
|
|
|
NS_FAILED(Preferences::RegisterCallbackAndCall(
|
|
|
|
JSVersionChanged,
|
|
|
|
PREF_WORKERS_LATEST_JS_VERSION,
|
|
|
|
nullptr))) {
|
2013-05-16 15:49:43 -07:00
|
|
|
NS_WARNING("Failed to register pref callbacks!");
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 02:38:14 -07:00
|
|
|
MOZ_ASSERT(gRuntimeServiceDuringInit, "Should be true!");
|
|
|
|
gRuntimeServiceDuringInit = false;
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
// We assume atomic 32bit reads/writes. If this assumption doesn't hold on
|
|
|
|
// some wacky platform then the worst that could happen is that the close
|
|
|
|
// handler will run for a slightly different amount of time.
|
2013-05-16 15:49:43 -07:00
|
|
|
if (NS_FAILED(Preferences::AddIntVarCache(
|
|
|
|
&sDefaultJSSettings.content.maxScriptRuntime,
|
|
|
|
PREF_MAX_SCRIPT_RUN_TIME_CONTENT,
|
|
|
|
MAX_SCRIPT_RUN_TIME_SEC)) ||
|
|
|
|
NS_FAILED(Preferences::AddIntVarCache(
|
|
|
|
&sDefaultJSSettings.chrome.maxScriptRuntime,
|
|
|
|
PREF_MAX_SCRIPT_RUN_TIME_CHROME, -1))) {
|
|
|
|
NS_WARNING("Failed to register timeout cache!");
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t maxPerDomain = Preferences::GetInt(PREF_WORKERS_MAX_PER_DOMAIN,
|
2011-07-17 12:09:13 -07:00
|
|
|
MAX_WORKERS_PER_DOMAIN);
|
2013-01-15 04:22:03 -08:00
|
|
|
gMaxWorkersPerDomain = std::max(0, maxPerDomain);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-06-28 18:56:30 -07:00
|
|
|
rv = InitOSFileConstants();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:26:15 -08:00
|
|
|
if (NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-09 10:54:05 -07:00
|
|
|
RuntimeService::Shutdown()
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-09-09 10:54:05 -07:00
|
|
|
MOZ_ASSERT(!mShuttingDown);
|
|
|
|
// That's it, no more workers.
|
|
|
|
mShuttingDown = true;
|
|
|
|
|
2015-03-17 03:15:19 -07:00
|
|
|
// Remove all listeners from the worker debugger manager to ensure that it
|
|
|
|
// gets properly destroyed.
|
|
|
|
if (NS_FAILED(ClearWorkerDebuggerManagerListeners())) {
|
|
|
|
NS_WARNING("Failed to clear worker debugger manager listeners!");
|
|
|
|
}
|
|
|
|
|
2011-12-29 10:28:09 -08:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_WARN_IF_FALSE(obs, "Failed to get observer service?!");
|
|
|
|
|
|
|
|
// Tell anyone that cares that they're about to lose worker support.
|
2012-07-30 07:20:58 -07:00
|
|
|
if (obs && NS_FAILED(obs->NotifyObservers(nullptr, WORKERS_SHUTDOWN_TOPIC,
|
|
|
|
nullptr))) {
|
2011-12-29 10:28:09 -08:00
|
|
|
NS_WARNING("NotifyObservers failed!");
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:41:57 -07:00
|
|
|
{
|
2011-07-25 18:49:16 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
nsAutoTArray<WorkerPrivate*, 100> workers;
|
|
|
|
mDomainMap.EnumerateRead(AddAllTopLevelWorkersToArray, &workers);
|
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
|
|
|
// Cancel all top-level workers.
|
|
|
|
{
|
2011-07-25 18:49:16 -07:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-05-06 06:04:17 -07:00
|
|
|
AutoSafeJSContext cx;
|
2013-04-18 08:36:04 -07:00
|
|
|
JSAutoRequest ar(cx);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!workers[index]->Kill(cx)) {
|
|
|
|
NS_WARNING("Failed to cancel worker!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 10:54:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This spins the event loop until all workers are finished and their threads
|
|
|
|
// have been joined.
|
|
|
|
void
|
|
|
|
RuntimeService::Cleanup()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
NS_WARN_IF_FALSE(obs, "Failed to get observer service?!");
|
|
|
|
|
|
|
|
if (mIdleThreadTimer) {
|
|
|
|
if (NS_FAILED(mIdleThreadTimer->Cancel())) {
|
|
|
|
NS_WARNING("Failed to cancel idle timer!");
|
|
|
|
}
|
|
|
|
mIdleThreadTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
nsAutoTArray<WorkerPrivate*, 100> workers;
|
|
|
|
mDomainMap.EnumerateRead(AddAllTopLevelWorkersToArray, &workers);
|
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(currentThread, "This should never be null!");
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
// Shut down any idle threads.
|
|
|
|
if (!mIdleThreadArray.IsEmpty()) {
|
2013-10-23 06:16:49 -07:00
|
|
|
nsAutoTArray<nsRefPtr<WorkerThread>, 20> idleThreads;
|
2011-07-25 18:49:16 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t idleThreadCount = mIdleThreadArray.Length();
|
2011-07-25 18:49:16 -07:00
|
|
|
idleThreads.SetLength(idleThreadCount);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < idleThreadCount; index++) {
|
2011-07-25 18:49:16 -07:00
|
|
|
NS_ASSERTION(mIdleThreadArray[index].mThread, "Null thread!");
|
|
|
|
idleThreads[index].swap(mIdleThreadArray[index].mThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
mIdleThreadArray.Clear();
|
|
|
|
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < idleThreadCount; index++) {
|
2011-07-25 18:49:16 -07:00
|
|
|
if (NS_FAILED(idleThreads[index]->Shutdown())) {
|
|
|
|
NS_WARNING("Failed to shutdown thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
// And make sure all their final messages have run and all their threads
|
|
|
|
// have joined.
|
|
|
|
while (mDomainMap.Count()) {
|
2011-07-25 18:49:16 -07:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
if (!NS_ProcessNextEvent(currentThread)) {
|
2011-07-17 12:09:13 -07:00
|
|
|
NS_WARNING("Something bad happened!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-02 01:41:57 -07:00
|
|
|
NS_ASSERTION(!mWindowMap.Count(), "All windows should have been released!");
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
if (mObserved) {
|
2013-12-10 03:43:16 -08:00
|
|
|
if (NS_FAILED(Preferences::UnregisterCallback(JSVersionChanged,
|
|
|
|
PREF_WORKERS_LATEST_JS_VERSION,
|
|
|
|
nullptr)) ||
|
2014-09-23 12:11:18 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(AppNameOverrideChanged,
|
|
|
|
PREF_GENERAL_APPNAME_OVERRIDE,
|
2014-09-05 07:26:34 -07:00
|
|
|
nullptr)) ||
|
2014-09-23 12:11:18 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
AppVersionOverrideChanged,
|
|
|
|
PREF_GENERAL_APPVERSION_OVERRIDE,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
PlatformOverrideChanged,
|
|
|
|
PREF_GENERAL_PLATFORM_OVERRIDE,
|
|
|
|
nullptr)) ||
|
2014-07-29 21:00:37 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(LoadRuntimeOptions,
|
2013-05-16 15:49:43 -07:00
|
|
|
PREF_JS_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
2014-07-29 21:00:37 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(LoadRuntimeOptions,
|
2013-05-16 15:49:43 -07:00
|
|
|
PREF_WORKERS_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
2015-06-27 20:19:24 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_SERVICEWORKERS_TESTING_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_SERVICEWORKERS_TESTING))) ||
|
2015-06-27 20:19:23 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_CACHES_TESTING_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DOM_CACHES_TESTING))) ||
|
2015-07-28 09:33:37 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_WORKERS_PERFORMANCE_LOGGING_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_PERFORMANCE_LOGGING_ENABLED))) ||
|
2015-07-02 20:09:20 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_INTERCEPTION_OPAQUE_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_INTERCEPTION_OPAQUE_ENABLED))) ||
|
2015-06-12 16:28:13 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_INTERCEPTION_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_INTERCEPTION_ENABLED))) ||
|
2015-04-03 22:46:07 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_SERVICEWORKERS_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_SERVICEWORKERS))) ||
|
2015-03-02 05:12:00 -08:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_CACHES_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DOM_CACHES))) ||
|
2015-06-25 18:53:02 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_WORKERNOTIFICATION_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DOM_WORKERNOTIFICATION))) ||
|
2015-07-27 12:35:28 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_PUSH_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_PUSH))) ||
|
2015-07-27 14:57:34 -07:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_REQUESTCONTEXT_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_REQUESTCONTEXT))) ||
|
2013-10-30 14:40:34 -07:00
|
|
|
#if DUMP_CONTROLLED_BY_PREF
|
2013-12-10 03:43:16 -08:00
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
WorkerPrefChanged,
|
|
|
|
PREF_DOM_WINDOW_DUMP_ENABLED,
|
|
|
|
reinterpret_cast<void *>(WORKERPREF_DUMP))) ||
|
2013-10-30 14:40:34 -07:00
|
|
|
#endif
|
2013-05-16 15:49:43 -07:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
LoadGCZealOptions,
|
|
|
|
PREF_JS_OPTIONS_PREFIX PREF_GCZEAL,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
LoadGCZealOptions,
|
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_GCZEAL,
|
|
|
|
nullptr)) ||
|
|
|
|
#endif
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
LoadJSGCMemoryOptions,
|
|
|
|
PREF_JS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX,
|
|
|
|
nullptr)) ||
|
|
|
|
NS_FAILED(Preferences::UnregisterCallback(
|
|
|
|
LoadJSGCMemoryOptions,
|
|
|
|
PREF_WORKERS_OPTIONS_PREFIX PREF_MEM_OPTIONS_PREFIX,
|
2013-12-25 00:21:59 -08:00
|
|
|
nullptr))) {
|
2013-05-16 15:49:43 -07:00
|
|
|
NS_WARNING("Failed to unregister pref callbacks!");
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (obs) {
|
2012-01-17 12:05:25 -08:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, GC_REQUEST_OBSERVER_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for GC request notifications!");
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:07:02 -08:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this, CC_REQUEST_OBSERVER_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for CC request notifications!");
|
|
|
|
}
|
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this,
|
|
|
|
MEMORY_PRESSURE_OBSERVER_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for memory pressure notifications!");
|
|
|
|
}
|
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
if (NS_FAILED(obs->RemoveObserver(this,
|
|
|
|
NS_IOSERVICE_OFFLINE_STATUS_TOPIC))) {
|
|
|
|
NS_WARNING("Failed to unregister for offline notification event!");
|
|
|
|
}
|
2013-09-09 10:54:05 -07:00
|
|
|
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID);
|
|
|
|
obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
|
|
|
|
mObserved = false;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-28 18:56:30 -07:00
|
|
|
|
2012-07-04 05:30:58 -07:00
|
|
|
CleanupOSFileConstants();
|
2012-11-04 23:30:00 -08:00
|
|
|
nsLayoutStatics::Release();
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
PLDHashOperator
|
|
|
|
RuntimeService::AddAllTopLevelWorkersToArray(const nsACString& aKey,
|
|
|
|
WorkerDomainInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
nsTArray<WorkerPrivate*>* array =
|
|
|
|
static_cast<nsTArray<WorkerPrivate*>*>(aUserArg);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < aData->mActiveWorkers.Length(); index++) {
|
2015-06-26 15:51:56 -07:00
|
|
|
MOZ_ASSERT(!aData->mActiveWorkers[index]->GetParent(),
|
|
|
|
"Shouldn't have a parent in this list!");
|
|
|
|
}
|
|
|
|
for (uint32_t index = 0; index < aData->mActiveServiceWorkers.Length(); index++) {
|
|
|
|
MOZ_ASSERT(!aData->mActiveServiceWorkers[index]->GetParent(),
|
|
|
|
"Shouldn't have a parent in this list!");
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
array->AppendElements(aData->mActiveWorkers);
|
2015-06-26 15:51:56 -07:00
|
|
|
array->AppendElements(aData->mActiveServiceWorkers);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
// These might not be top-level workers...
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < aData->mQueuedWorkers.Length(); index++) {
|
2011-07-17 12:09:13 -07:00
|
|
|
WorkerPrivate* worker = aData->mQueuedWorkers[index];
|
|
|
|
if (!worker->GetParent()) {
|
|
|
|
array->AppendElement(worker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
// static
|
|
|
|
PLDHashOperator
|
|
|
|
RuntimeService::RemoveSharedWorkerFromWindowMap(
|
|
|
|
nsPIDOMWindow* aKey,
|
|
|
|
nsAutoPtr<nsTArray<WorkerPrivate*> >& aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aData.get());
|
|
|
|
MOZ_ASSERT(aUserArg);
|
|
|
|
|
|
|
|
auto workerPrivate = static_cast<WorkerPrivate*>(aUserArg);
|
|
|
|
|
2014-05-12 09:11:15 -07:00
|
|
|
MOZ_ASSERT(workerPrivate->IsSharedWorker() || workerPrivate->IsServiceWorker());
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
if (aData->RemoveElement(workerPrivate)) {
|
|
|
|
MOZ_ASSERT(!aData->Contains(workerPrivate), "Added worker more than once!");
|
|
|
|
|
|
|
|
if (aData->IsEmpty()) {
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
PLDHashOperator
|
|
|
|
RuntimeService::FindSharedWorkerInfo(const nsACString& aKey,
|
|
|
|
SharedWorkerInfo* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
auto match = static_cast<MatchSharedWorkerInfo*>(aUserArg);
|
|
|
|
|
|
|
|
if (aData->mWorkerPrivate == match->mWorkerPrivate) {
|
|
|
|
match->mSharedWorkerInfo = aData;
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
void
|
|
|
|
RuntimeService::GetWorkersForWindow(nsPIDOMWindow* aWindow,
|
|
|
|
nsTArray<WorkerPrivate*>& aWorkers)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsTArray<WorkerPrivate*>* workers;
|
|
|
|
if (mWindowMap.Get(aWindow, &workers)) {
|
|
|
|
NS_ASSERTION(!workers->IsEmpty(), "Should have been removed!");
|
|
|
|
aWorkers.AppendElements(*workers);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(aWorkers.IsEmpty(), "Should be empty!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
RuntimeService::CancelWorkersForWindow(nsPIDOMWindow* aWindow)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
nsAutoTArray<WorkerPrivate*, MAX_WORKERS_PER_DOMAIN> workers;
|
2011-07-17 12:09:13 -07:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
2014-06-30 08:21:27 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(aWindow))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2013-06-05 07:04:23 -07:00
|
|
|
WorkerPrivate*& worker = workers[index];
|
|
|
|
|
2014-08-23 20:25:08 -07:00
|
|
|
if (worker->IsSharedWorker() || worker->IsServiceWorker()) {
|
2013-06-05 07:04:23 -07:00
|
|
|
worker->CloseSharedWorkersForWindow(aWindow);
|
|
|
|
} else if (!worker->Cancel(cx)) {
|
|
|
|
JS_ReportPendingException(cx);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-01 02:00:19 -07:00
|
|
|
RuntimeService::FreezeWorkersForWindow(nsPIDOMWindow* aWindow)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
MOZ_ASSERT(aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
nsAutoTArray<WorkerPrivate*, MAX_WORKERS_PER_DOMAIN> workers;
|
2011-07-17 12:09:13 -07:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
2014-06-30 12:17:09 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(aWindow))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2015-04-01 02:00:19 -07:00
|
|
|
if (!workers[index]->Freeze(cx, aWindow)) {
|
2013-06-05 07:04:23 -07:00
|
|
|
JS_ReportPendingException(cx);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-01 02:00:19 -07:00
|
|
|
RuntimeService::ThawWorkersForWindow(nsPIDOMWindow* aWindow)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
MOZ_ASSERT(aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
nsAutoTArray<WorkerPrivate*, MAX_WORKERS_PER_DOMAIN> workers;
|
2011-07-17 12:09:13 -07:00
|
|
|
GetWorkersForWindow(aWindow, workers);
|
|
|
|
|
|
|
|
if (!workers.IsEmpty()) {
|
2014-06-30 12:37:25 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(aWindow))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < workers.Length(); index++) {
|
2015-04-01 02:00:19 -07:00
|
|
|
if (!workers[index]->Thaw(cx, aWindow)) {
|
2013-06-05 07:04:23 -07:00
|
|
|
JS_ReportPendingException(cx);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 09:19:00 -07:00
|
|
|
nsresult
|
|
|
|
RuntimeService::CreateSharedWorkerInternal(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
const nsACString& aName,
|
|
|
|
WorkerType aType,
|
|
|
|
SharedWorker** aSharedWorker)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2014-05-29 09:19:00 -07:00
|
|
|
MOZ_ASSERT(aType == WorkerTypeShared || aType == WorkerTypeService);
|
2013-11-05 06:16:24 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
MOZ_ASSERT(window);
|
|
|
|
|
2014-06-16 11:08:00 -07:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2015-02-12 01:50:05 -08:00
|
|
|
WorkerLoadInfo loadInfo;
|
2013-11-05 06:16:24 -08:00
|
|
|
nsresult rv = WorkerPrivate::GetLoadInfo(cx, window, nullptr, aScriptURL,
|
2015-02-21 07:09:17 -08:00
|
|
|
false,
|
|
|
|
WorkerPrivate::OverrideLoadGroup,
|
2015-06-16 18:21:08 -07:00
|
|
|
aType, &loadInfo);
|
2013-06-05 07:04:23 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-12-16 22:26:15 -08:00
|
|
|
return CreateSharedWorkerFromLoadInfo(cx, &loadInfo, aScriptURL, aName, aType,
|
2014-07-02 17:48:50 -07:00
|
|
|
aSharedWorker);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2014-07-02 17:48:50 -07:00
|
|
|
nsresult
|
|
|
|
RuntimeService::CreateSharedWorkerFromLoadInfo(JSContext* aCx,
|
2015-02-12 01:50:05 -08:00
|
|
|
WorkerLoadInfo* aLoadInfo,
|
2014-07-02 17:48:50 -07:00
|
|
|
const nsAString& aScriptURL,
|
|
|
|
const nsACString& aName,
|
|
|
|
WorkerType aType,
|
|
|
|
SharedWorker** aSharedWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2014-12-16 22:26:15 -08:00
|
|
|
MOZ_ASSERT(aLoadInfo);
|
|
|
|
MOZ_ASSERT(aLoadInfo->mResolvedScriptURI);
|
2015-05-14 12:41:42 -07:00
|
|
|
MOZ_ASSERT_IF(aType == WorkerTypeService, aLoadInfo->mServiceWorkerID > 0);
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:24 -08:00
|
|
|
nsRefPtr<WorkerPrivate> workerPrivate;
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
WorkerDomainInfo* domainInfo;
|
|
|
|
SharedWorkerInfo* sharedWorkerInfo;
|
|
|
|
|
2014-07-02 17:48:50 -07:00
|
|
|
nsCString scriptSpec;
|
2014-12-16 22:26:15 -08:00
|
|
|
nsresult rv = aLoadInfo->mResolvedScriptURI->GetSpec(scriptSpec);
|
2014-07-02 17:48:50 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-02-06 06:39:10 -08:00
|
|
|
nsAutoCString key;
|
2015-03-19 16:40:52 -07:00
|
|
|
GenerateSharedWorkerKey(scriptSpec, aName,
|
|
|
|
NS_ConvertUTF16toUTF8(aLoadInfo->mServiceWorkerCacheName),
|
2015-06-26 11:18:18 -07:00
|
|
|
aType, aLoadInfo->mPrivateBrowsing, key);
|
2014-02-06 06:39:10 -08:00
|
|
|
|
2014-12-16 22:26:15 -08:00
|
|
|
if (mDomainMap.Get(aLoadInfo->mDomain, &domainInfo) &&
|
2014-02-06 06:39:10 -08:00
|
|
|
domainInfo->mSharedWorkerInfos.Get(key, &sharedWorkerInfo)) {
|
2013-06-05 07:04:23 -07:00
|
|
|
workerPrivate = sharedWorkerInfo->mWorkerPrivate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:48:50 -07:00
|
|
|
// Keep a reference to the window before spawning the worker. If the worker is
|
|
|
|
// a Shared/Service worker and the worker script loads and executes before
|
|
|
|
// the SharedWorker object itself is created before then WorkerScriptLoaded()
|
|
|
|
// will reset the loadInfo's window.
|
2014-12-16 22:26:15 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = aLoadInfo->mWindow;
|
2014-07-01 20:29:08 -07:00
|
|
|
|
2014-07-02 17:48:50 -07:00
|
|
|
bool created = false;
|
2013-06-05 07:04:23 -07:00
|
|
|
if (!workerPrivate) {
|
2013-11-05 06:16:24 -08:00
|
|
|
ErrorResult rv;
|
|
|
|
workerPrivate =
|
2014-07-02 17:48:50 -07:00
|
|
|
WorkerPrivate::Constructor(aCx, aScriptURL, false,
|
2014-12-16 22:26:15 -08:00
|
|
|
aType, aName, aLoadInfo, rv);
|
2015-04-27 06:18:52 -07:00
|
|
|
NS_ENSURE_TRUE(workerPrivate, rv.StealNSResult());
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
created = true;
|
2015-02-21 07:09:17 -08:00
|
|
|
} else {
|
|
|
|
// If we're attaching to an existing SharedWorker private, then we
|
|
|
|
// must update the overriden load group to account for our document's
|
|
|
|
// load group.
|
|
|
|
workerPrivate->UpdateOverridenLoadGroup(aLoadInfo->mLoadGroup);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2014-05-29 09:19:00 -07:00
|
|
|
nsRefPtr<SharedWorker> sharedWorker = new SharedWorker(window, workerPrivate);
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2014-07-02 17:48:50 -07:00
|
|
|
if (!workerPrivate->RegisterSharedWorker(aCx, sharedWorker)) {
|
2013-06-05 07:04:23 -07:00
|
|
|
NS_WARNING("Worker is unreachable, this shouldn't happen!");
|
|
|
|
sharedWorker->Close();
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is normally handled in RegisterWorker, but that wasn't called if the
|
|
|
|
// worker already existed.
|
|
|
|
if (!created) {
|
|
|
|
nsTArray<WorkerPrivate*>* windowArray;
|
2013-11-05 06:16:24 -08:00
|
|
|
if (!mWindowMap.Get(window, &windowArray)) {
|
2013-06-05 07:04:23 -07:00
|
|
|
windowArray = new nsTArray<WorkerPrivate*>(1);
|
2013-11-05 06:16:24 -08:00
|
|
|
mWindowMap.Put(window, windowArray);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!windowArray->Contains(workerPrivate)) {
|
|
|
|
windowArray->AppendElement(workerPrivate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedWorker.forget(aSharedWorker);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuntimeService::ForgetSharedWorker(WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2014-05-12 09:11:15 -07:00
|
|
|
MOZ_ASSERT(aWorkerPrivate->IsSharedWorker() ||
|
|
|
|
aWorkerPrivate->IsServiceWorker());
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
WorkerDomainInfo* domainInfo;
|
|
|
|
if (mDomainMap.Get(aWorkerPrivate->Domain(), &domainInfo)) {
|
|
|
|
MatchSharedWorkerInfo match(aWorkerPrivate);
|
|
|
|
domainInfo->mSharedWorkerInfos.EnumerateRead(FindSharedWorkerInfo,
|
|
|
|
&match);
|
|
|
|
|
|
|
|
if (match.mSharedWorkerInfo) {
|
2014-02-06 06:39:10 -08:00
|
|
|
nsAutoCString key;
|
2015-03-19 16:40:52 -07:00
|
|
|
const nsCString& cacheName =
|
|
|
|
aWorkerPrivate->IsServiceWorker() ?
|
|
|
|
NS_ConvertUTF16toUTF8(aWorkerPrivate->ServiceWorkerCacheName()) :
|
|
|
|
EmptyCString();
|
2014-02-06 06:39:10 -08:00
|
|
|
GenerateSharedWorkerKey(match.mSharedWorkerInfo->mScriptSpec,
|
2015-03-10 08:44:04 -07:00
|
|
|
match.mSharedWorkerInfo->mName,
|
2015-06-26 11:18:18 -07:00
|
|
|
cacheName, aWorkerPrivate->Type(),
|
|
|
|
aWorkerPrivate->IsInPrivateBrowsing(), key);
|
2014-02-06 06:39:10 -08:00
|
|
|
domainInfo->mSharedWorkerInfos.Remove(key);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
void
|
2013-10-23 06:16:49 -07:00
|
|
|
RuntimeService::NoteIdleThread(WorkerThread* aThread)
|
2011-07-25 18:49:16 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(aThread);
|
|
|
|
|
2015-05-07 10:35:57 -07:00
|
|
|
bool shutdownThread = mShuttingDown;
|
|
|
|
bool scheduleTimer = false;
|
2011-07-25 18:49:16 -07:00
|
|
|
|
2015-05-07 10:35:57 -07:00
|
|
|
if (!shutdownThread) {
|
|
|
|
static TimeDuration timeout =
|
|
|
|
TimeDuration::FromSeconds(IDLE_THREAD_TIMEOUT_SEC);
|
|
|
|
|
|
|
|
TimeStamp expirationTime = TimeStamp::NowLoRes() + timeout;
|
2011-07-25 18:49:16 -07:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2015-05-07 10:35:57 -07:00
|
|
|
uint32_t previousIdleCount = mIdleThreadArray.Length();
|
|
|
|
|
|
|
|
if (previousIdleCount < MAX_IDLE_THREADS) {
|
2011-07-25 18:49:16 -07:00
|
|
|
IdleThreadInfo* info = mIdleThreadArray.AppendElement();
|
|
|
|
info->mThread = aThread;
|
|
|
|
info->mExpirationTime = expirationTime;
|
2015-05-07 10:35:57 -07:00
|
|
|
|
|
|
|
scheduleTimer = previousIdleCount == 0;
|
|
|
|
} else {
|
|
|
|
shutdownThread = true;
|
2011-07-25 18:49:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:35:57 -07:00
|
|
|
MOZ_ASSERT_IF(shutdownThread, !scheduleTimer);
|
|
|
|
MOZ_ASSERT_IF(scheduleTimer, !shutdownThread);
|
|
|
|
|
2011-07-25 18:49:16 -07:00
|
|
|
// Too many idle threads, just shut this one down.
|
2015-05-07 10:35:57 -07:00
|
|
|
if (shutdownThread) {
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(aThread->Shutdown()));
|
2015-05-07 10:35:57 -07:00
|
|
|
} else if (scheduleTimer) {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
mIdleThreadTimer->InitWithFuncCallback(ShutdownIdleThreads,
|
|
|
|
nullptr,
|
|
|
|
IDLE_THREAD_TIMEOUT_SEC * 1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT)));
|
2011-07-25 18:49:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
void
|
2014-07-29 21:00:37 -07:00
|
|
|
RuntimeService::UpdateAllWorkerRuntimeOptions()
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2014-07-29 21:00:37 -07:00
|
|
|
BROADCAST_ALL_WORKERS(UpdateRuntimeOptions, sDefaultJSSettings.runtimeOptions);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-09-23 12:11:18 -07:00
|
|
|
void
|
|
|
|
RuntimeService::UpdateAppNameOverridePreference(const nsAString& aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mNavigatorProperties.mAppNameOverridden = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuntimeService::UpdateAppVersionOverridePreference(const nsAString& aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mNavigatorProperties.mAppVersionOverridden = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RuntimeService::UpdatePlatformOverridePreference(const nsAString& aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mNavigatorProperties.mPlatformOverridden = aValue;
|
|
|
|
}
|
|
|
|
|
2013-11-24 11:27:15 -08:00
|
|
|
void
|
|
|
|
RuntimeService::UpdateAllWorkerPreference(WorkerPreference aPref, bool aValue)
|
|
|
|
{
|
|
|
|
BROADCAST_ALL_WORKERS(UpdatePreference, aPref, aValue);
|
|
|
|
}
|
|
|
|
|
2014-09-05 07:26:34 -07:00
|
|
|
void
|
|
|
|
RuntimeService::UpdateAllWorkerLanguages(const nsTArray<nsString>& aLanguages)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mNavigatorProperties.mLanguages = aLanguages;
|
|
|
|
BROADCAST_ALL_WORKERS(UpdateLanguages, aLanguages);
|
|
|
|
}
|
|
|
|
|
2012-01-04 11:11:32 -08:00
|
|
|
void
|
2013-05-16 15:49:43 -07:00
|
|
|
RuntimeService::UpdateAllWorkerMemoryParameter(JSGCParamKey aKey,
|
|
|
|
uint32_t aValue)
|
2012-01-04 11:11:32 -08:00
|
|
|
{
|
2013-05-16 15:49:43 -07:00
|
|
|
BROADCAST_ALL_WORKERS(UpdateJSWorkerMemoryParameter, aKey, aValue);
|
2012-01-04 11:11:32 -08:00
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
void
|
|
|
|
RuntimeService::UpdateAllWorkerGCZeal()
|
|
|
|
{
|
2013-05-16 15:49:43 -07:00
|
|
|
BROADCAST_ALL_WORKERS(UpdateGCZeal, sDefaultJSSettings.gcZeal,
|
|
|
|
sDefaultJSSettings.gcZealFrequency);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-17 12:05:25 -08:00
|
|
|
void
|
|
|
|
RuntimeService::GarbageCollectAllWorkers(bool aShrinking)
|
|
|
|
{
|
|
|
|
BROADCAST_ALL_WORKERS(GarbageCollect, aShrinking);
|
|
|
|
}
|
|
|
|
|
2013-12-02 20:07:02 -08:00
|
|
|
void
|
|
|
|
RuntimeService::CycleCollectAllWorkers()
|
|
|
|
{
|
|
|
|
BROADCAST_ALL_WORKERS(CycleCollect, /* dummy = */ false);
|
|
|
|
}
|
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
void
|
|
|
|
RuntimeService::SendOfflineStatusChangeEventToAllWorkers(bool aIsOffline)
|
|
|
|
{
|
|
|
|
BROADCAST_ALL_WORKERS(OfflineStatusChangeEvent, aIsOffline);
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
// nsISupports
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(RuntimeService, nsIObserver)
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RuntimeService::Observe(nsISupports* aSubject, const char* aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aData)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-09-09 10:54:05 -07:00
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_THREADS_OBSERVER_ID)) {
|
|
|
|
Cleanup();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-17 12:05:25 -08:00
|
|
|
if (!strcmp(aTopic, GC_REQUEST_OBSERVER_TOPIC)) {
|
2013-12-02 20:07:02 -08:00
|
|
|
GarbageCollectAllWorkers(/* shrinking = */ false);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!strcmp(aTopic, CC_REQUEST_OBSERVER_TOPIC)) {
|
|
|
|
CycleCollectAllWorkers();
|
2012-01-17 12:05:25 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!strcmp(aTopic, MEMORY_PRESSURE_OBSERVER_TOPIC)) {
|
2013-12-02 20:07:02 -08:00
|
|
|
GarbageCollectAllWorkers(/* shrinking = */ true);
|
2013-12-02 20:07:02 -08:00
|
|
|
CycleCollectAllWorkers();
|
2012-01-17 12:05:25 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-19 15:08:50 -08:00
|
|
|
if (!strcmp(aTopic, NS_IOSERVICE_OFFLINE_STATUS_TOPIC)) {
|
|
|
|
SendOfflineStatusChangeEventToAllWorkers(NS_IsOffline());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-08-22 20:06:56 -07:00
|
|
|
if (!strcmp(aTopic, NS_IOSERVICE_APP_OFFLINE_STATUS_TOPIC)) {
|
2014-08-26 19:42:13 -07:00
|
|
|
BROADCAST_ALL_WORKERS(OfflineStatusChangeEvent,
|
|
|
|
NS_IsOffline() ||
|
|
|
|
NS_IsAppOffline(workers[index]->GetPrincipal()));
|
|
|
|
return NS_OK;
|
2014-08-22 20:06:56 -07:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
NS_NOTREACHED("Unknown observer topic!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-30 14:40:34 -07:00
|
|
|
|
2013-12-10 15:10:01 -08:00
|
|
|
/* static */ void
|
2013-11-24 11:27:15 -08:00
|
|
|
RuntimeService::WorkerPrefChanged(const char* aPrefName, void* aClosure)
|
2013-10-30 14:40:34 -07:00
|
|
|
{
|
2013-11-24 11:27:15 -08:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-06-30 11:16:03 -07:00
|
|
|
const WorkerPreference key =
|
|
|
|
static_cast<WorkerPreference>(reinterpret_cast<uintptr_t>(aClosure));
|
2013-11-24 11:27:15 -08:00
|
|
|
|
2015-06-30 11:16:03 -07:00
|
|
|
switch (key) {
|
|
|
|
case WORKERPREF_DOM_CACHES:
|
|
|
|
case WORKERPREF_DOM_CACHES_TESTING:
|
|
|
|
case WORKERPREF_DOM_WORKERNOTIFICATION:
|
2015-07-28 09:33:37 -07:00
|
|
|
case WORKERPREF_PERFORMANCE_LOGGING_ENABLED:
|
2013-11-24 11:27:15 -08:00
|
|
|
#ifdef DUMP_CONTROLLED_BY_PREF
|
2015-06-30 11:16:03 -07:00
|
|
|
case WORKERPREF_DUMP:
|
2014-01-30 13:14:00 -08:00
|
|
|
#endif
|
2015-06-30 11:16:03 -07:00
|
|
|
case WORKERPREF_INTERCEPTION_ENABLED:
|
2015-07-02 20:09:20 -07:00
|
|
|
case WORKERPREF_INTERCEPTION_OPAQUE_ENABLED:
|
2015-06-30 11:16:03 -07:00
|
|
|
case WORKERPREF_SERVICEWORKERS:
|
|
|
|
case WORKERPREF_SERVICEWORKERS_TESTING:
|
2015-07-27 12:35:28 -07:00
|
|
|
case WORKERPREF_PUSH:
|
2015-07-27 14:57:34 -07:00
|
|
|
case WORKERPREF_REQUESTCONTEXT:
|
2015-06-30 11:16:03 -07:00
|
|
|
sDefaultPreferences[key] = Preferences::GetBool(aPrefName, false);
|
|
|
|
break;
|
2013-11-24 11:26:07 -08:00
|
|
|
|
2015-06-30 11:16:03 -07:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid pref key");
|
|
|
|
break;
|
2015-03-02 05:12:00 -08:00
|
|
|
}
|
2013-11-24 11:27:15 -08:00
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
|
|
|
if (rts) {
|
|
|
|
rts->UpdateAllWorkerPreference(key, sDefaultPreferences[key]);
|
|
|
|
}
|
2013-11-24 11:26:07 -08:00
|
|
|
}
|
2013-12-10 03:43:16 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
RuntimeService::JSVersionChanged(const char* /* aPrefName */, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
bool useLatest = Preferences::GetBool(PREF_WORKERS_LATEST_JS_VERSION, false);
|
|
|
|
JS::CompartmentOptions& options = sDefaultJSSettings.content.compartmentOptions;
|
|
|
|
options.setVersion(useLatest ? JSVERSION_LATEST : JSVERSION_DEFAULT);
|
|
|
|
}
|
2013-10-23 06:16:49 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(LogViolationDetailsRunnable, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LogViolationDetailsRunnable::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsIContentSecurityPolicy* csp = mWorkerPrivate->GetCSP();
|
|
|
|
if (csp) {
|
|
|
|
NS_NAMED_LITERAL_STRING(scriptSample,
|
|
|
|
"Call to eval() or related function blocked by CSP.");
|
|
|
|
if (mWorkerPrivate->GetReportCSPViolations()) {
|
|
|
|
csp->LogViolationDetails(nsIContentSecurityPolicy::VIOLATION_TYPE_EVAL,
|
|
|
|
mFileName, scriptSample, mLineNum,
|
2014-01-02 11:14:06 -08:00
|
|
|
EmptyString(), EmptyString());
|
2013-10-23 06:16:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MainThreadStopSyncLoopRunnable> response =
|
|
|
|
new MainThreadStopSyncLoopRunnable(mWorkerPrivate, mSyncLoopTarget.forget(),
|
|
|
|
true);
|
|
|
|
MOZ_ALWAYS_TRUE(response->Dispatch(nullptr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
NS_IMPL_ISUPPORTS(WorkerBackgroundChildCallback, nsIIPCBackgroundChildCreateCallback)
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThreadPrimaryRunnable, nsRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThreadPrimaryRunnable::Run()
|
|
|
|
{
|
2014-07-09 14:39:18 -07:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
if (IsNuwaProcess()) {
|
|
|
|
NuwaMarkCurrentThread(nullptr, nullptr);
|
|
|
|
NuwaFreezeCurrentThread();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char stackBaseGuess;
|
|
|
|
|
2014-11-17 11:55:37 -08:00
|
|
|
PR_SetCurrentThreadName("DOM Worker");
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
nsAutoCString threadName;
|
2014-11-17 11:55:37 -08:00
|
|
|
threadName.AssignLiteral("DOM Worker '");
|
2013-10-23 06:16:49 -07:00
|
|
|
threadName.Append(NS_LossyConvertUTF16toASCII(mWorkerPrivate->ScriptURL()));
|
|
|
|
threadName.Append('\'');
|
|
|
|
|
|
|
|
profiler_register_thread(threadName.get(), &stackBaseGuess);
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
// Note: SynchronouslyCreatePBackground() must be called prior to
|
2014-11-17 11:55:37 -08:00
|
|
|
// mWorkerPrivate->SetThread() in order to avoid accidentally consuming
|
2014-07-09 14:39:18 -07:00
|
|
|
// worker messages here.
|
|
|
|
nsresult rv = SynchronouslyCreatePBackground();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// XXX need to fire an error at parent.
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-17 11:55:37 -08:00
|
|
|
mWorkerPrivate->SetThread(mThread);
|
2014-08-20 16:42:00 -07:00
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
#ifdef ENABLE_TESTS
|
2014-11-14 18:47:30 -08:00
|
|
|
TestPBackground();
|
2014-07-09 14:39:18 -07:00
|
|
|
#endif
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
nsCycleCollector_startup();
|
|
|
|
|
2014-02-19 08:02:13 -08:00
|
|
|
WorkerJSRuntime runtime(mParentRuntime, mWorkerPrivate);
|
2013-10-23 06:16:49 -07:00
|
|
|
JSRuntime* rt = runtime.Runtime();
|
|
|
|
|
|
|
|
JSContext* cx = CreateJSContextForWorker(mWorkerPrivate, rt);
|
|
|
|
if (!cx) {
|
|
|
|
// XXX need to fire an error at parent.
|
|
|
|
NS_ERROR("Failed to create runtime and context!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
|
|
|
PseudoStack* stack = mozilla_get_pseudo_stack();
|
|
|
|
if (stack) {
|
|
|
|
stack->sampleRuntime(rt);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
mWorkerPrivate->DoRunLoop(cx);
|
|
|
|
|
|
|
|
JS_ReportPendingException(cx);
|
|
|
|
}
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
#ifdef ENABLE_TESTS
|
2014-11-14 18:47:30 -08:00
|
|
|
TestPBackground();
|
2014-07-09 14:39:18 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
BackgroundChild::CloseForCurrentThread();
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
#ifdef MOZ_ENABLE_PROFILER_SPS
|
|
|
|
if (stack) {
|
|
|
|
stack->sampleRuntime(nullptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:20:59 -07:00
|
|
|
// Destroy the main context. This will unroot the main worker global and GC,
|
|
|
|
// which should break all cycles that touch JS.
|
2013-10-23 06:16:49 -07:00
|
|
|
JS_DestroyContext(cx);
|
|
|
|
|
2015-04-08 15:20:59 -07:00
|
|
|
// Before shutting down the cycle collector we need to do one more pass
|
|
|
|
// through the event loop to clean up any C++ objects that need deferred
|
|
|
|
// cleanup.
|
|
|
|
mWorkerPrivate->ClearMainEventQueue(WorkerPrivate::WorkerRan);
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
// Now WorkerJSRuntime goes out of scope and its destructor will shut
|
2015-04-08 15:20:59 -07:00
|
|
|
// down the cycle collector. This breaks any remaining cycles and collects
|
|
|
|
// any remaining C++ objects.
|
2013-10-23 06:16:49 -07:00
|
|
|
}
|
|
|
|
|
2014-11-17 11:55:37 -08:00
|
|
|
mWorkerPrivate->SetThread(nullptr);
|
2013-10-23 06:16:49 -07:00
|
|
|
|
2014-03-04 15:09:23 -08:00
|
|
|
mWorkerPrivate->ScheduleDeletion(WorkerPrivate::WorkerRan);
|
2013-10-23 06:16:49 -07:00
|
|
|
|
|
|
|
// It is no longer safe to touch mWorkerPrivate.
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
|
|
|
|
// Now recycle this thread.
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
MOZ_ASSERT(mainThread);
|
|
|
|
|
|
|
|
nsRefPtr<FinishedRunnable> finishedRunnable =
|
|
|
|
new FinishedRunnable(mThread.forget());
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mainThread->Dispatch(finishedRunnable,
|
|
|
|
NS_DISPATCH_NORMAL)));
|
|
|
|
|
|
|
|
profiler_unregister_thread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-09 14:39:18 -07:00
|
|
|
nsresult
|
|
|
|
WorkerThreadPrimaryRunnable::SynchronouslyCreatePBackground()
|
|
|
|
{
|
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
|
|
|
|
MOZ_ASSERT(!BackgroundChild::GetForCurrentThread());
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
nsCOMPtr<nsIIPCBackgroundChildCreateCallback> callback =
|
|
|
|
new WorkerBackgroundChildCallback(&done);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!BackgroundChild::GetOrCreateForCurrentThread(callback))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
if (NS_WARN_IF(!NS_ProcessNextEvent(mThread, true /* aMayWait */))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!BackgroundChild::GetForCurrentThread())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThreadPrimaryRunnable::FinishedRunnable,
|
|
|
|
nsRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerThreadPrimaryRunnable::FinishedRunnable::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-11-14 18:47:30 -08:00
|
|
|
nsRefPtr<WorkerThread> thread;
|
2013-10-23 06:16:49 -07:00
|
|
|
mThread.swap(thread);
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
|
|
|
if (rts) {
|
|
|
|
rts->NoteIdleThread(thread);
|
|
|
|
}
|
|
|
|
else if (thread->ShutdownRequired()) {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(thread->Shutdown()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|