2011-07-17 23:09:13 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-16 07:40:38 +04:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
2013-06-15 06:48:28 +04:00
|
|
|
#include "amIAddonManager.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIClassInfo.h"
|
2012-09-15 22:51:55 +04:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIConsoleService.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "nsIDOMDOMException.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsIDOMEvent.h"
|
2011-08-16 07:40:38 +04:00
|
|
|
#include "nsIDOMFile.h"
|
2013-10-14 15:58:05 +04:00
|
|
|
#include "nsIDOMMessageEvent.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIDocument.h"
|
2012-08-20 22:34:32 +04:00
|
|
|
#include "nsIDocShell.h"
|
2011-09-09 04:03:03 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2012-09-17 04:20:16 +04:00
|
|
|
#include "nsIPermissionManager.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsITextToSubURI.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#include <algorithm>
|
2012-01-15 12:13:08 +04:00
|
|
|
#include "jsfriendapi.h"
|
2013-08-27 06:05:20 +04:00
|
|
|
#include "js/OldDebugAPI.h"
|
2012-01-11 12:23:08 +04:00
|
|
|
#include "js/MemoryMetrics.h"
|
2013-09-25 15:21:20 +04:00
|
|
|
#include "mozilla/ContentEvents.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2013-08-04 03:55:40 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/dom/ErrorEvent.h"
|
2013-10-14 15:58:05 +04:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2013-08-04 03:55:40 +04:00
|
|
|
#include "mozilla/dom/ImageData.h"
|
|
|
|
#include "mozilla/dom/ImageDataBinding.h"
|
2013-10-14 15:58:05 +04:00
|
|
|
#include "mozilla/dom/WorkerBinding.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/Util.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsAlgorithm.h"
|
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 20:05:26 +04:00
|
|
|
#include "nsCxPusher.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2013-10-14 15:58:05 +04:00
|
|
|
#include "nsEventDispatcher.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsDOMMessageEvent.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2013-03-13 00:33:40 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2012-08-20 22:34:32 +04:00
|
|
|
#include "nsSandboxFlags.h"
|
2011-08-07 05:03:46 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "DOMBindingInlines.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "Events.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2011-08-16 07:40:38 +04:00
|
|
|
#include "File.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "MessagePort.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "Principal.h"
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "ScriptLoader.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "SharedWorker.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "WorkerFeature.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "WorkerMessagePort.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
// GC will run once every thirty seconds during normal execution.
|
|
|
|
#define NORMAL_GC_TIMER_DELAY_MS 30000
|
|
|
|
|
|
|
|
// GC will run five seconds after the last event is processed.
|
|
|
|
#define IDLE_GC_TIMER_DELAY_MS 5000
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
#define PREF_WORKERS_ENABLED "dom.workers.enabled"
|
|
|
|
|
2013-09-27 10:20:56 +04:00
|
|
|
using mozilla::InternalScriptErrorEvent;
|
2011-07-17 23:09:13 +04:00
|
|
|
using mozilla::MutexAutoLock;
|
|
|
|
using mozilla::TimeDuration;
|
|
|
|
using mozilla::TimeStamp;
|
2013-09-09 07:29:21 +04:00
|
|
|
using mozilla::dom::Throw;
|
2013-06-05 18:04:23 +04:00
|
|
|
using mozilla::AutoCxPusher;
|
2013-06-06 21:28:47 +04:00
|
|
|
using mozilla::AutoPushJSContext;
|
2013-05-06 17:04:17 +04:00
|
|
|
using mozilla::AutoSafeJSContext;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
2012-03-31 08:42:20 +04:00
|
|
|
using namespace mozilla::dom::workers::events;
|
2012-04-24 14:58:08 +04:00
|
|
|
using namespace mozilla::dom;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-12-24 04:59:51 +04:00
|
|
|
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(JsWorkerMallocSizeOf)
|
2012-07-13 10:51:01 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class AutoPtrComparator
|
|
|
|
{
|
|
|
|
typedef nsAutoPtr<T> A;
|
|
|
|
typedef T* B;
|
|
|
|
|
|
|
|
public:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const A& a, const B& b) const {
|
2011-07-17 23:09:13 +04:00
|
|
|
return a && b ? *a == *b : !a && !b ? true : false;
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool LessThan(const A& a, const B& b) const {
|
2011-07-17 23:09:13 +04:00
|
|
|
return a && b ? *a < *b : b ? true : false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline AutoPtrComparator<T>
|
|
|
|
GetAutoPtrComparator(const nsTArray<nsAutoPtr<T> >&)
|
|
|
|
{
|
|
|
|
return AutoPtrComparator<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specialize this if there's some class that has multiple nsISupports bases.
|
|
|
|
template <class T>
|
|
|
|
struct ISupportsBaseInfo
|
|
|
|
{
|
|
|
|
typedef T ISupportsBase;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <template <class> class SmartPtr, class T>
|
|
|
|
inline void
|
|
|
|
SwapToISupportsArray(SmartPtr<T>& aSrc,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aDest)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports>* dest = aDest.AppendElement();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
T* raw = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
aSrc.swap(raw);
|
|
|
|
|
|
|
|
nsISupports* rawSupports =
|
|
|
|
static_cast<typename ISupportsBaseInfo<T>::ISupportsBase*>(raw);
|
|
|
|
dest->swap(rawSupports);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
struct WindowAction
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
JSContext* mJSContext;
|
|
|
|
bool mDefaultAction;
|
|
|
|
|
|
|
|
WindowAction(nsPIDOMWindow* aWindow, JSContext* aJSContext)
|
|
|
|
: mWindow(aWindow), mJSContext(aJSContext), mDefaultAction(true)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aJSContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowAction(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow), mJSContext(nullptr), mDefaultAction(true)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const WindowAction& aOther) const
|
|
|
|
{
|
|
|
|
return mWindow == aOther.mWindow;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
LogErrorToConsole(const nsAString& aMessage,
|
|
|
|
const nsAString& aFilename,
|
|
|
|
const nsAString& aLine,
|
|
|
|
uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint64_t aInnerWindowId)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(scriptError, "Failed to create script error!");
|
|
|
|
|
|
|
|
if (scriptError) {
|
|
|
|
if (NS_FAILED(scriptError->InitWithWindowID(aMessage, aFilename, aLine,
|
|
|
|
aLineNumber, aColumnNumber,
|
|
|
|
aFlags, "Web Worker",
|
|
|
|
aInnerWindowId))) {
|
|
|
|
NS_WARNING("Failed to init script error!");
|
|
|
|
scriptError = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(consoleService, "Failed to get console service!");
|
|
|
|
|
|
|
|
if (consoleService) {
|
|
|
|
if (scriptError) {
|
|
|
|
if (NS_SUCCEEDED(consoleService->LogMessage(scriptError))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_WARNING("LogMessage failed!");
|
|
|
|
} else if (NS_SUCCEEDED(consoleService->LogStringMessage(
|
|
|
|
aMessage.BeginReading()))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NS_WARNING("LogStringMessage failed!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 msg(aMessage);
|
|
|
|
NS_ConvertUTF16toUTF8 filename(aFilename);
|
|
|
|
|
|
|
|
static const char kErrorString[] = "JS error in Web Worker: %s [%s:%u]";
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", kErrorString, msg.get(),
|
|
|
|
filename.get(), aLineNumber);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fprintf(stderr, kErrorString, msg.get(), filename.get(), aLineNumber);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
struct WorkerStructuredCloneCallbacks
|
|
|
|
{
|
|
|
|
static JSObject*
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Read(JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
uint32_t aData, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-08-16 07:40:38 +04:00
|
|
|
// See if object is a nsIDOMFile pointer.
|
|
|
|
if (aTag == DOMWORKER_SCTAG_FILE) {
|
|
|
|
JS_ASSERT(!aData);
|
|
|
|
|
|
|
|
nsIDOMFile* file;
|
|
|
|
if (JS_ReadBytes(aReader, &file, sizeof(file))) {
|
|
|
|
JS_ASSERT(file);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
// File should not be mutable.
|
|
|
|
nsCOMPtr<nsIMutable> mutableFile = do_QueryInterface(file);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMutable;
|
2011-08-16 07:40:38 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mutableFile->GetMutable(&isMutable)) &&
|
|
|
|
!isMutable,
|
|
|
|
"Only immutable file should be passed to worker");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// nsIDOMFiles should be threadsafe, thus we will use the same instance
|
|
|
|
// in the worker.
|
|
|
|
JSObject* jsFile = file::CreateFile(aCx, file);
|
|
|
|
return jsFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// See if object is a nsIDOMBlob pointer.
|
|
|
|
else if (aTag == DOMWORKER_SCTAG_BLOB) {
|
|
|
|
JS_ASSERT(!aData);
|
|
|
|
|
|
|
|
nsIDOMBlob* blob;
|
|
|
|
if (JS_ReadBytes(aReader, &blob, sizeof(blob))) {
|
|
|
|
JS_ASSERT(blob);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
// Blob should not be mutable.
|
|
|
|
nsCOMPtr<nsIMutable> mutableBlob = do_QueryInterface(blob);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMutable;
|
2011-08-16 07:40:38 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mutableBlob->GetMutable(&isMutable)) &&
|
|
|
|
!isMutable,
|
|
|
|
"Only immutable blob should be passed to worker");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// nsIDOMBlob should be threadsafe, thus we will use the same instance
|
|
|
|
// in the worker.
|
|
|
|
JSObject* jsBlob = file::CreateBlob(aCx, blob);
|
|
|
|
return jsBlob;
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 14:58:08 +04:00
|
|
|
// See if the object is an ImageData.
|
|
|
|
else if (aTag == SCTAG_DOM_IMAGEDATA) {
|
|
|
|
JS_ASSERT(!aData);
|
|
|
|
|
|
|
|
// Read the information out of the stream.
|
|
|
|
uint32_t width, height;
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Rooted<JS::Value> dataArray(aCx);
|
2012-04-24 14:58:08 +04:00
|
|
|
if (!JS_ReadUint32Pair(aReader, &width, &height) ||
|
2013-05-04 11:53:00 +04:00
|
|
|
!JS_ReadTypedArray(aReader, dataArray.address()))
|
2012-04-24 14:58:08 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-04-24 14:58:08 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(dataArray.isObject());
|
|
|
|
|
|
|
|
// Construct the ImageData.
|
2013-08-04 03:55:40 +04:00
|
|
|
nsRefPtr<ImageData> imageData = new ImageData(width, height,
|
|
|
|
dataArray.toObject());
|
|
|
|
// Wrap it in a JS::Value.
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!global) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return imageData->WrapObject(aCx, global);
|
2012-04-24 14:58:08 +04:00
|
|
|
}
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
Error(aCx, 0);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2013-05-09 11:27:40 +04:00
|
|
|
Write(JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-08-16 07:40:38 +04:00
|
|
|
NS_ASSERTION(aClosure, "Null pointer!");
|
|
|
|
|
|
|
|
// We'll stash any nsISupports pointers that need to be AddRef'd here.
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> >* clonedObjects =
|
|
|
|
static_cast<nsTArray<nsCOMPtr<nsISupports> >*>(aClosure);
|
|
|
|
|
|
|
|
// See if this is a File object.
|
|
|
|
{
|
2012-02-06 00:07:23 +04:00
|
|
|
nsIDOMFile* file = file::GetDOMFileFromJSObject(aObj);
|
2011-08-16 07:40:38 +04:00
|
|
|
if (file) {
|
|
|
|
if (JS_WriteUint32Pair(aWriter, DOMWORKER_SCTAG_FILE, 0) &&
|
|
|
|
JS_WriteBytes(aWriter, &file, sizeof(file))) {
|
|
|
|
clonedObjects->AppendElement(file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if this is a Blob object.
|
|
|
|
{
|
2012-02-06 00:07:23 +04:00
|
|
|
nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aObj);
|
2011-08-16 07:40:38 +04:00
|
|
|
if (blob) {
|
|
|
|
nsCOMPtr<nsIMutable> mutableBlob = do_QueryInterface(blob);
|
2011-10-17 18:59:28 +04:00
|
|
|
if (mutableBlob && NS_SUCCEEDED(mutableBlob->SetMutable(false)) &&
|
2011-08-16 07:40:38 +04:00
|
|
|
JS_WriteUint32Pair(aWriter, DOMWORKER_SCTAG_BLOB, 0) &&
|
|
|
|
JS_WriteBytes(aWriter, &blob, sizeof(blob))) {
|
|
|
|
clonedObjects->AppendElement(blob);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:58:08 +04:00
|
|
|
// See if this is an ImageData object.
|
2013-08-04 03:55:40 +04:00
|
|
|
{
|
|
|
|
ImageData* imageData = nullptr;
|
2013-08-24 06:42:41 +04:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageData, aCx, aObj, imageData))) {
|
2013-08-04 03:55:40 +04:00
|
|
|
// Prepare the ImageData internals.
|
|
|
|
uint32_t width = imageData->Width();
|
|
|
|
uint32_t height = imageData->Height();
|
|
|
|
JS::Rooted<JSObject*> dataArray(aCx, imageData->GetDataObject());
|
|
|
|
|
|
|
|
// Write the internals to the stream.
|
|
|
|
JSAutoCompartment ac(aCx, dataArray);
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) &&
|
|
|
|
JS_WriteUint32Pair(aWriter, width, height) &&
|
|
|
|
JS_WriteTypedArray(aWriter, JS::ObjectValue(*dataArray));
|
|
|
|
}
|
2012-04-24 14:58:08 +04:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
Error(aCx, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Error(JSContext* aCx, uint32_t /* aErrorId */)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-09-09 07:29:21 +04:00
|
|
|
Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks gWorkerStructuredCloneCallbacks = {
|
|
|
|
WorkerStructuredCloneCallbacks::Read,
|
|
|
|
WorkerStructuredCloneCallbacks::Write,
|
|
|
|
WorkerStructuredCloneCallbacks::Error
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MainThreadWorkerStructuredCloneCallbacks
|
|
|
|
{
|
|
|
|
static JSObject*
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Read(JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
uint32_t aData, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
// See if object is a nsIDOMFile pointer.
|
|
|
|
if (aTag == DOMWORKER_SCTAG_FILE) {
|
|
|
|
JS_ASSERT(!aData);
|
|
|
|
|
|
|
|
nsIDOMFile* file;
|
|
|
|
if (JS_ReadBytes(aReader, &file, sizeof(file))) {
|
|
|
|
JS_ASSERT(file);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
// File should not be mutable.
|
|
|
|
nsCOMPtr<nsIMutable> mutableFile = do_QueryInterface(file);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMutable;
|
2011-08-16 07:40:38 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mutableFile->GetMutable(&isMutable)) &&
|
|
|
|
!isMutable,
|
|
|
|
"Only immutable file should be passed to worker");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// nsIDOMFiles should be threadsafe, thus we will use the same instance
|
|
|
|
// on the main thread.
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> wrappedFile(aCx);
|
2013-07-30 03:45:27 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-05-02 13:12:47 +04:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, global, file,
|
|
|
|
&NS_GET_IID(nsIDOMFile),
|
|
|
|
wrappedFile.address());
|
2011-08-16 07:40:38 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Error(aCx, nsIDOMDOMException::DATA_CLONE_ERR);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:23:40 +04:00
|
|
|
return &wrappedFile.toObject();
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// See if object is a nsIDOMBlob pointer.
|
|
|
|
else if (aTag == DOMWORKER_SCTAG_BLOB) {
|
|
|
|
JS_ASSERT(!aData);
|
|
|
|
|
|
|
|
nsIDOMBlob* blob;
|
|
|
|
if (JS_ReadBytes(aReader, &blob, sizeof(blob))) {
|
|
|
|
JS_ASSERT(blob);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
// Blob should not be mutable.
|
|
|
|
nsCOMPtr<nsIMutable> mutableBlob = do_QueryInterface(blob);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMutable;
|
2011-08-16 07:40:38 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(mutableBlob->GetMutable(&isMutable)) &&
|
|
|
|
!isMutable,
|
|
|
|
"Only immutable blob should be passed to worker");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// nsIDOMBlobs should be threadsafe, thus we will use the same instance
|
|
|
|
// on the main thread.
|
2013-05-02 13:12:47 +04:00
|
|
|
JS::Rooted<JS::Value> wrappedBlob(aCx);
|
2013-07-30 03:45:27 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-05-02 13:12:47 +04:00
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, global, blob,
|
|
|
|
&NS_GET_IID(nsIDOMBlob),
|
|
|
|
wrappedBlob.address());
|
2011-08-16 07:40:38 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
Error(aCx, nsIDOMDOMException::DATA_CLONE_ERR);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
|
2013-02-20 22:23:40 +04:00
|
|
|
return &wrappedBlob.toObject();
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ClearPendingException(aCx);
|
2012-07-30 18:20:58 +04:00
|
|
|
return NS_DOMReadStructuredClone(aCx, aReader, aTag, aData, nullptr);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2013-05-09 11:27:40 +04:00
|
|
|
Write(JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
NS_ASSERTION(aClosure, "Null pointer!");
|
|
|
|
|
|
|
|
// We'll stash any nsISupports pointers that need to be AddRef'd here.
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> >* clonedObjects =
|
|
|
|
static_cast<nsTArray<nsCOMPtr<nsISupports> >*>(aClosure);
|
|
|
|
|
|
|
|
// See if this is a wrapped native.
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrappedNative;
|
|
|
|
nsContentUtils::XPConnect()->
|
|
|
|
GetWrappedNativeOfJSObject(aCx, aObj, getter_AddRefs(wrappedNative));
|
|
|
|
|
|
|
|
if (wrappedNative) {
|
|
|
|
// Get the raw nsISupports out of it.
|
|
|
|
nsISupports* wrappedObject = wrappedNative->Native();
|
|
|
|
NS_ASSERTION(wrappedObject, "Null pointer?!");
|
|
|
|
|
2012-08-02 09:34:26 +04:00
|
|
|
nsISupports* ccISupports = nullptr;
|
|
|
|
wrappedObject->QueryInterface(NS_GET_IID(nsCycleCollectionISupports),
|
|
|
|
reinterpret_cast<void**>(&ccISupports));
|
|
|
|
if (ccISupports) {
|
|
|
|
NS_WARNING("Cycle collected objects are not supported!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// See if the wrapped native is a nsIDOMFile.
|
|
|
|
nsCOMPtr<nsIDOMFile> file = do_QueryInterface(wrappedObject);
|
|
|
|
if (file) {
|
|
|
|
nsCOMPtr<nsIMutable> mutableFile = do_QueryInterface(file);
|
|
|
|
if (mutableFile && NS_SUCCEEDED(mutableFile->SetMutable(false))) {
|
|
|
|
nsIDOMFile* filePtr = file;
|
|
|
|
if (JS_WriteUint32Pair(aWriter, DOMWORKER_SCTAG_FILE, 0) &&
|
|
|
|
JS_WriteBytes(aWriter, &filePtr, sizeof(filePtr))) {
|
|
|
|
clonedObjects->AppendElement(file);
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:34:26 +04:00
|
|
|
// See if the wrapped native is a nsIDOMBlob.
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = do_QueryInterface(wrappedObject);
|
|
|
|
if (blob) {
|
|
|
|
nsCOMPtr<nsIMutable> mutableBlob = do_QueryInterface(blob);
|
|
|
|
if (mutableBlob && NS_SUCCEEDED(mutableBlob->SetMutable(false))) {
|
|
|
|
nsIDOMBlob* blobPtr = blob;
|
|
|
|
if (JS_WriteUint32Pair(aWriter, DOMWORKER_SCTAG_BLOB, 0) &&
|
|
|
|
JS_WriteBytes(aWriter, &blobPtr, sizeof(blobPtr))) {
|
|
|
|
clonedObjects->AppendElement(blob);
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ClearPendingException(aCx);
|
2012-07-30 18:20:58 +04:00
|
|
|
return NS_DOMWriteStructuredClone(aCx, aWriter, aObj, nullptr);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Error(JSContext* aCx, uint32_t aErrorId)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
NS_DOMStructuredCloneError(aCx, aErrorId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks gMainThreadWorkerStructuredCloneCallbacks = {
|
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Read,
|
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Write,
|
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Error
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ChromeWorkerStructuredCloneCallbacks
|
|
|
|
{
|
|
|
|
static JSObject*
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Read(JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
uint32_t aData, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
return WorkerStructuredCloneCallbacks::Read(aCx, aReader, aTag, aData,
|
|
|
|
aClosure);
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2013-05-09 11:27:40 +04:00
|
|
|
Write(JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
return WorkerStructuredCloneCallbacks::Write(aCx, aWriter, aObj, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Error(JSContext* aCx, uint32_t aErrorId)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
return WorkerStructuredCloneCallbacks::Error(aCx, aErrorId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks gChromeWorkerStructuredCloneCallbacks = {
|
|
|
|
ChromeWorkerStructuredCloneCallbacks::Read,
|
|
|
|
ChromeWorkerStructuredCloneCallbacks::Write,
|
|
|
|
ChromeWorkerStructuredCloneCallbacks::Error
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MainThreadChromeWorkerStructuredCloneCallbacks
|
|
|
|
{
|
|
|
|
static JSObject*
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Read(JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
uint32_t aData, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
JSObject* clone =
|
2011-11-08 05:01:29 +04:00
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Read(aCx, aReader, aTag, aData,
|
|
|
|
aClosure);
|
|
|
|
if (clone) {
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone =
|
2011-07-17 23:09:13 +04:00
|
|
|
ChromeWorkerStructuredCloneCallbacks::Read(aCx, aReader, aTag, aData,
|
|
|
|
aClosure);
|
|
|
|
if (clone) {
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ClearPendingException(aCx);
|
2012-07-30 18:20:58 +04:00
|
|
|
return NS_DOMReadStructuredClone(aCx, aReader, aTag, aData, nullptr);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2013-05-09 11:27:40 +04:00
|
|
|
Write(JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj, void* aClosure)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2011-11-08 05:01:29 +04:00
|
|
|
if (MainThreadWorkerStructuredCloneCallbacks::Write(aCx, aWriter, aObj,
|
|
|
|
aClosure) ||
|
|
|
|
ChromeWorkerStructuredCloneCallbacks::Write(aCx, aWriter, aObj,
|
|
|
|
aClosure) ||
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_DOMWriteStructuredClone(aCx, aWriter, aObj, nullptr)) {
|
2011-11-08 05:01:29 +04:00
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2011-11-08 05:01:29 +04:00
|
|
|
return false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
Error(JSContext* aCx, uint32_t aErrorId)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
NS_DOMStructuredCloneError(aCx, aErrorId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks gMainThreadChromeWorkerStructuredCloneCallbacks = {
|
|
|
|
MainThreadChromeWorkerStructuredCloneCallbacks::Read,
|
|
|
|
MainThreadChromeWorkerStructuredCloneCallbacks::Write,
|
|
|
|
MainThreadChromeWorkerStructuredCloneCallbacks::Error
|
|
|
|
};
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
class MainThreadReleaseRunnable : public nsRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2012-03-31 08:42:20 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mDoomed;
|
2013-06-23 01:48:00 +04:00
|
|
|
nsTArray<nsCString> mHostObjectURIs;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
|
|
|
MainThreadReleaseRunnable(nsCOMPtr<nsIThread>& aThread,
|
2013-06-23 01:48:00 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aDoomed,
|
|
|
|
nsTArray<nsCString>& aHostObjectURIs)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
mThread.swap(aThread);
|
|
|
|
mDoomed.SwapElements(aDoomed);
|
2013-06-23 01:48:00 +04:00
|
|
|
mHostObjectURIs.SwapElements(aHostObjectURIs);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-23 01:48:00 +04:00
|
|
|
MainThreadReleaseRunnable(nsTArray<nsCOMPtr<nsISupports> >& aDoomed,
|
|
|
|
nsTArray<nsCString>& aHostObjectURIs)
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
mDoomed.SwapElements(aDoomed);
|
2013-06-23 01:48:00 +04:00
|
|
|
mHostObjectURIs.SwapElements(aHostObjectURIs);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
mDoomed.Clear();
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (mThread) {
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
runtime->NoteIdleThread(mThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2013-06-23 01:48:00 +04:00
|
|
|
for (uint32_t i = 0, len = mHostObjectURIs.Length(); i < len; ++i) {
|
|
|
|
nsHostObjectProtocolHandler::RemoveDataEntry(mHostObjectURIs[i]);
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WorkerFinishedRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
WorkerPrivate* mFinishedWorker;
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerFinishedRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerPrivate* aFinishedWorker,
|
|
|
|
nsIThread* aFinishedThread)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
|
|
|
mFinishedWorker(aFinishedWorker), mThread(aFinishedThread)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > doomed;
|
|
|
|
mFinishedWorker->ForgetMainThreadObjects(doomed);
|
|
|
|
|
2013-06-23 01:48:00 +04:00
|
|
|
nsTArray<nsCString> hostObjectURIs;
|
|
|
|
mFinishedWorker->StealHostObjectURIs(hostObjectURIs);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
nsRefPtr<MainThreadReleaseRunnable> runnable =
|
2013-06-23 01:48:00 +04:00
|
|
|
new MainThreadReleaseRunnable(mThread, doomed, hostObjectURIs);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch, going to leak!");
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
mFinishedWorker->Finish(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
|
|
|
|
|
|
|
runtime->UnregisterWorker(aCx, mFinishedWorker);
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
mFinishedWorker->Release();
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TopLevelWorkerFinishedRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
WorkerPrivate* mFinishedWorker;
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TopLevelWorkerFinishedRunnable(WorkerPrivate* aFinishedWorker,
|
|
|
|
nsIThread* aFinishedThread)
|
|
|
|
: mFinishedWorker(aFinishedWorker), mThread(aFinishedThread)
|
|
|
|
{
|
|
|
|
aFinishedWorker->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-05-06 17:04:17 +04:00
|
|
|
AutoSafeJSContext cx;
|
2013-04-18 19:36:04 +04:00
|
|
|
JSAutoRequest ar(cx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
mFinishedWorker->Finish(cx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
|
|
|
|
|
|
|
runtime->UnregisterWorker(cx, mFinishedWorker);
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > doomed;
|
|
|
|
mFinishedWorker->ForgetMainThreadObjects(doomed);
|
|
|
|
|
2013-06-23 01:48:00 +04:00
|
|
|
nsTArray<nsCString> hostObjectURIs;
|
|
|
|
mFinishedWorker->StealHostObjectURIs(hostObjectURIs);
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
nsRefPtr<MainThreadReleaseRunnable> runnable =
|
2013-06-23 01:48:00 +04:00
|
|
|
new MainThreadReleaseRunnable(doomed, hostObjectURIs);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch, going to leak!");
|
|
|
|
}
|
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
if (mThread) {
|
|
|
|
runtime->NoteIdleThread(mThread);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
mFinishedWorker->Release();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-07-26 05:49:16 +04:00
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModifyBusyCountRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
bool mIncrease;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ModifyBusyCountRunnable(WorkerPrivate* aWorkerPrivate, bool aIncrease)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, ParentThread, UnchangedBusyCount),
|
|
|
|
mIncrease(aIncrease)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, mIncrease);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
{
|
|
|
|
if (mIncrease) {
|
|
|
|
WorkerControlRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't do anything here as it's possible that aWorkerPrivate has been
|
|
|
|
// deleted.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CompileScriptRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CompileScriptRunnable(WorkerPrivate* aWorkerPrivate)
|
2012-02-10 08:54:47 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThread, ModifyBusyCount,
|
|
|
|
SkipWhenClearing)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, CreateGlobalScope(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!global) {
|
|
|
|
NS_WARNING("Failed to make global!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(aCx, global);
|
2011-07-17 23:09:13 +04:00
|
|
|
return scriptloader::LoadWorkerScript(aCx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CloseEventRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CloseEventRunnable(WorkerPrivate* aWorkerPrivate)
|
2012-02-10 08:54:47 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount,
|
|
|
|
SkipWhenClearing)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2013-07-30 03:45:27 +04:00
|
|
|
JS::Rooted<JSObject*> target(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(target, "This must never be null!");
|
|
|
|
|
|
|
|
aWorkerPrivate->CloseHandlerStarted();
|
|
|
|
|
2013-05-09 11:27:40 +04:00
|
|
|
JS::Rooted<JSString*> type(aCx, JS_InternString(aCx, "close"));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:27:40 +04:00
|
|
|
JS::Rooted<JSObject*> event(aCx, CreateGenericEvent(aCx, type, false, false, false));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignored;
|
2012-03-31 08:42:20 +04:00
|
|
|
return DispatchEventToTarget(aCx, target, event, &ignored);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
{
|
|
|
|
// Report errors.
|
|
|
|
WorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
|
|
|
|
// Match the busy count increase from NotifyRunnable.
|
|
|
|
if (!aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->CloseHandlerFinished();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MessageEventRunnable : public WorkerRunnable
|
|
|
|
{
|
2013-06-12 01:45:15 +04:00
|
|
|
JSAutoStructuredCloneBuffer mBuffer;
|
2011-08-16 07:40:38 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
2013-06-05 18:04:23 +04:00
|
|
|
uint64_t mMessagePortSerial;
|
|
|
|
bool mToMessagePort;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
MessageEventRunnable(WorkerPrivate* aWorkerPrivate, Target aTarget,
|
2011-08-16 07:40:38 +04:00
|
|
|
JSAutoStructuredCloneBuffer& aData,
|
2013-06-05 18:04:23 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
|
|
|
bool aToMessagePort, uint64_t aMessagePortSerial)
|
2011-07-17 23:09:13 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, aTarget, aTarget == WorkerThread ?
|
|
|
|
ModifyBusyCount :
|
2012-02-10 08:54:47 +04:00
|
|
|
UnchangedBusyCount,
|
2013-06-05 18:04:23 +04:00
|
|
|
SkipWhenClearing),
|
|
|
|
mMessagePortSerial(aMessagePortSerial), mToMessagePort(aToMessagePort)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-06-12 01:45:15 +04:00
|
|
|
mBuffer.swap(aData);
|
2013-04-01 21:35:13 +04:00
|
|
|
mClonedObjects.SwapElements(aClonedObjects);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT_IF(mToMessagePort, aWorkerPrivate->IsSharedWorker());
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (mTarget == ParentThread) {
|
2012-03-31 08:42:20 +04:00
|
|
|
// Don't fire this event if the JS object has been disconnected from the
|
2011-07-17 23:09:13 +04:00
|
|
|
// private object.
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!aWorkerPrivate->IsAcceptingEvents()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (mToMessagePort) {
|
|
|
|
return
|
|
|
|
aWorkerPrivate->DispatchMessageEventToMessagePort(aCx,
|
|
|
|
mMessagePortSerial,
|
|
|
|
mBuffer,
|
|
|
|
mClonedObjects);
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aWorkerPrivate->IsSuspended()) {
|
|
|
|
aWorkerPrivate->QueueRunnable(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->AssertInnerWindowIsCorrect();
|
2013-10-14 15:58:05 +04:00
|
|
|
|
|
|
|
// Release reference to objects that were AddRef'd for
|
|
|
|
// cloning into worker when array goes out of scope.
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
clonedObjects.SwapElements(mClonedObjects);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> messageData(aCx);
|
|
|
|
if (!mBuffer.read(aCx, messageData.address(),
|
|
|
|
workers::WorkerStructuredCloneCallbacks(!aWorkerPrivate->GetParent()))) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMMessageEvent> event =
|
|
|
|
new nsDOMMessageEvent(aWorkerPrivate, nullptr, nullptr);
|
|
|
|
nsresult rv =
|
|
|
|
event->InitMessageEvent(NS_LITERAL_STRING("message"),
|
|
|
|
false /* non-bubbling */,
|
|
|
|
true /* cancelable */,
|
|
|
|
messageData,
|
|
|
|
EmptyString(),
|
|
|
|
EmptyString(),
|
|
|
|
nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(aCx, rv);
|
|
|
|
return false;
|
2013-10-14 07:10:43 +04:00
|
|
|
}
|
2013-10-14 08:59:58 +04:00
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> domEvent = do_QueryObject(event);
|
|
|
|
|
|
|
|
nsEventStatus dummy = nsEventStatus_eIgnore;
|
|
|
|
aWorkerPrivate->DispatchDOMEvent(nullptr, domEvent, nullptr, &dummy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate == GetWorkerPrivateFromContext(aCx));
|
|
|
|
if (mToMessagePort) {
|
|
|
|
nsRefPtr<WorkerMessagePort> port =
|
|
|
|
aWorkerPrivate->GetMessagePort(mMessagePortSerial);
|
|
|
|
if (!port) {
|
|
|
|
// Must have been closed already.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return port->MaybeDispatchEvent(aCx, mBuffer, mClonedObjects);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
JS::Rooted<JSObject*> target(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(target, "This should never be null!");
|
|
|
|
|
2013-05-09 11:27:40 +04:00
|
|
|
JS::Rooted<JSObject*> event(aCx,
|
2013-10-14 15:58:05 +04:00
|
|
|
CreateMessageEvent(aCx, mBuffer, mClonedObjects, false));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dummy;
|
2012-03-31 08:42:20 +04:00
|
|
|
return DispatchEventToTarget(aCx, target, event, &dummy);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-05-04 11:22:18 +04:00
|
|
|
|
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
{
|
|
|
|
WorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class NotifyRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
Status mStatus;
|
|
|
|
|
|
|
|
public:
|
2012-08-08 01:38:46 +04:00
|
|
|
NotifyRunnable(WorkerPrivate* aWorkerPrivate, Status aStatus)
|
2011-07-17 23:09:13 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
2012-08-08 01:38:46 +04:00
|
|
|
mStatus(aStatus)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aStatus == Terminating || aStatus == Canceling ||
|
|
|
|
aStatus == Killing, "Bad status!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// Modify here, but not in PostRun! This busy count addition will be matched
|
2012-08-08 01:38:46 +04:00
|
|
|
// by the CloseEventRunnable.
|
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, true);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return aWorkerPrivate->NotifyInternal(aCx, mStatus);
|
|
|
|
}
|
2013-10-14 15:58:05 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
if (!aDispatchResult) {
|
|
|
|
// We couldn't dispatch to the worker, which means it's already dead.
|
|
|
|
// Undo the busy count modification.
|
|
|
|
aWorkerPrivate->ModifyBusyCount(aCx, false);
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2012-06-15 06:31:55 +04:00
|
|
|
class CloseRunnable MOZ_FINAL : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CloseRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, ParentThread, UnchangedBusyCount)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// This busy count will be matched by the CloseEventRunnable.
|
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, true) &&
|
|
|
|
aWorkerPrivate->Close(aCx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SuspendRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SuspendRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return aWorkerPrivate->SuspendInternal(aCx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ResumeRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResumeRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return aWorkerPrivate->ResumeInternal(aCx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReportErrorRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
nsString mMessage;
|
|
|
|
nsString mFilename;
|
|
|
|
nsString mLine;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLineNumber;
|
|
|
|
uint32_t mColumnNumber;
|
|
|
|
uint32_t mFlags;
|
|
|
|
uint32_t mErrorNumber;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
ReportErrorRunnable(WorkerPrivate* aWorkerPrivate, const nsString& aMessage,
|
|
|
|
const nsString& aFilename, const nsString& aLine,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLineNumber, uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags, uint32_t aErrorNumber)
|
2012-02-10 08:54:47 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, ParentThread, UnchangedBusyCount,
|
|
|
|
SkipWhenClearing),
|
2011-07-17 23:09:13 +04:00
|
|
|
mMessage(aMessage), mFilename(aFilename), mLine(aLine),
|
|
|
|
mLineNumber(aLineNumber), mColumnNumber(aColumnNumber), mFlags(aFlags),
|
|
|
|
mErrorNumber(aErrorNumber)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// Dispatch may fail if the worker was canceled, no need to report that as
|
|
|
|
// an error, so don't call base class PostDispatch.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2012-12-31 19:48:57 +04:00
|
|
|
// Don't fire this event if the JS object has been disconnected from the
|
|
|
|
// private object.
|
|
|
|
if (!aWorkerPrivate->IsAcceptingEvents()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
JS::Rooted<JSObject*> target(aCx, aWorkerPrivate->GetWrapper());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t innerWindowId;
|
2013-06-05 18:04:23 +04:00
|
|
|
bool fireAtScope = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
if (parent) {
|
2011-08-25 00:44:35 +04:00
|
|
|
innerWindowId = 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (aWorkerPrivate->IsSuspended()) {
|
|
|
|
aWorkerPrivate->QueueRunnable(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (aWorkerPrivate->IsSharedWorker()) {
|
|
|
|
aWorkerPrivate->BroadcastErrorToSharedWorkers(aCx, mMessage, mFilename,
|
|
|
|
mLine, mLineNumber,
|
|
|
|
mColumnNumber, mFlags);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-06 19:29:03 +04:00
|
|
|
aWorkerPrivate->AssertInnerWindowIsCorrect();
|
|
|
|
|
2011-08-25 00:44:35 +04:00
|
|
|
innerWindowId = aWorkerPrivate->GetInnerWindowId();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
return ReportErrorRunnable::ReportError(aCx, parent, fireAtScope,
|
|
|
|
aWorkerPrivate, mMessage,
|
|
|
|
mFilename, mLine, mLineNumber,
|
|
|
|
mColumnNumber, mFlags,
|
2011-08-25 00:44:35 +04:00
|
|
|
mErrorNumber, innerWindowId);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-05-04 11:22:18 +04:00
|
|
|
void PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
|
|
|
{
|
|
|
|
WorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
// aWorkerPrivate is the worker thread we're on (or the main thread, if null)
|
|
|
|
// aTarget is the worker object that we are going to fire an error at
|
|
|
|
// (if any).
|
2011-07-17 23:09:13 +04:00
|
|
|
static bool
|
|
|
|
ReportError(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2013-10-14 15:58:05 +04:00
|
|
|
bool aFireAtScope, WorkerPrivate* aTarget,
|
|
|
|
const nsString& aMessage, const nsString& aFilename,
|
|
|
|
const nsString& aLine, uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber, uint32_t aFlags,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aErrorNumber, uint64_t aInnerWindowId)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
if (aWorkerPrivate) {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Rooted<JSString*> message(aCx, JS_NewUCStringCopyN(aCx, aMessage.get(),
|
|
|
|
aMessage.Length()));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!message) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Rooted<JSString*> filename(aCx, JS_NewUCStringCopyN(aCx, aFilename.get(),
|
|
|
|
aFilename.Length()));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!filename) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-08 04:35:44 +04:00
|
|
|
// We should not fire error events for warnings but instead make sure that
|
|
|
|
// they show up in the error console.
|
|
|
|
if (!JSREPORT_IS_WARNING(aFlags)) {
|
|
|
|
// First fire an ErrorEvent at the worker.
|
2013-10-14 15:58:05 +04:00
|
|
|
if (aTarget) {
|
|
|
|
ErrorEventInit init;
|
|
|
|
init.mMessage = aMessage;
|
|
|
|
init.mFilename = aFilename;
|
|
|
|
init.mLineno = aLineNumber;
|
|
|
|
init.mCancelable = true;
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
nsRefPtr<ErrorEvent> event =
|
|
|
|
ErrorEvent::Constructor(aTarget, NS_LITERAL_STRING("error"), init);
|
2013-10-14 07:10:43 +04:00
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
aTarget->DispatchDOMEvent(nullptr, event, nullptr, &status);
|
|
|
|
|
|
|
|
if (status == nsEventStatus_eConsumeNoDefault) {
|
2012-12-08 04:35:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-12-08 04:35:44 +04:00
|
|
|
|
|
|
|
// Now fire an event at the global object, but don't do that if the error
|
|
|
|
// code is too much recursion and this is the same script threw the error.
|
2013-10-14 15:58:05 +04:00
|
|
|
if (aFireAtScope && (aTarget || aErrorNumber != JSMSG_OVER_RECURSED)) {
|
|
|
|
JS::Rooted<JSObject*> target(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_ASSERTION(target, "This should never be null!");
|
2012-12-08 04:35:44 +04:00
|
|
|
|
|
|
|
bool preventDefaultCalled;
|
|
|
|
nsIScriptGlobalObject* sgo;
|
|
|
|
|
|
|
|
if (aWorkerPrivate ||
|
2013-06-05 18:04:23 +04:00
|
|
|
!(sgo = nsJSUtils::GetStaticScriptGlobal(target))) {
|
2012-12-08 04:35:44 +04:00
|
|
|
// Fire a normal ErrorEvent if we're running on a worker thread.
|
2013-05-09 11:27:40 +04:00
|
|
|
JS::Rooted<JSObject*> event(aCx,
|
|
|
|
CreateErrorEvent(aCx, message, filename, aLineNumber, false));
|
2012-12-08 04:35:44 +04:00
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!DispatchEventToTarget(aCx, target, event,
|
2012-12-08 04:35:44 +04:00
|
|
|
&preventDefaultCalled)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-12-08 04:35:44 +04:00
|
|
|
else {
|
2013-09-27 10:20:56 +04:00
|
|
|
// Icky, we have to fire an InternalScriptErrorEvent...
|
|
|
|
InternalScriptErrorEvent event(true, NS_LOAD_ERROR);
|
2012-12-08 04:35:44 +04:00
|
|
|
event.lineNr = aLineNumber;
|
|
|
|
event.errorMsg = aMessage.get();
|
|
|
|
event.fileName = aFilename.get();
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
if (NS_FAILED(sgo->HandleScriptError(&event, &status))) {
|
|
|
|
NS_WARNING("Failed to dispatch main thread error event!");
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-12-08 04:35:44 +04:00
|
|
|
preventDefaultCalled = status == nsEventStatus_eConsumeNoDefault;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-12-08 04:35:44 +04:00
|
|
|
if (preventDefaultCalled) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now fire a runnable to do the same on the parent's thread if we can.
|
|
|
|
if (aWorkerPrivate) {
|
|
|
|
nsRefPtr<ReportErrorRunnable> runnable =
|
|
|
|
new ReportErrorRunnable(aWorkerPrivate, aMessage, aFilename, aLine,
|
|
|
|
aLineNumber, aColumnNumber, aFlags,
|
|
|
|
aErrorNumber);
|
|
|
|
return runnable->Dispatch(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise log an error to the error console.
|
2013-06-05 18:04:23 +04:00
|
|
|
LogErrorToConsole(aMessage, aFilename, aLine, aLineNumber, aColumnNumber,
|
|
|
|
aFlags, aInnerWindowId);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TimerRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerRunnable(WorkerPrivate* aWorkerPrivate)
|
2012-02-10 08:54:47 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount,
|
|
|
|
SkipWhenClearing)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return aWorkerPrivate->RunExpiredTimeouts(aCx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
DummyCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
// Nothing!
|
|
|
|
}
|
|
|
|
|
2012-06-15 06:31:55 +04:00
|
|
|
class WorkerRunnableEventTarget MOZ_FINAL : public nsIEventTarget
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
nsRefPtr<WorkerRunnable> mWorkerRunnable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerRunnableEventTarget(WorkerRunnable* aWorkerRunnable)
|
|
|
|
: mWorkerRunnable(aWorkerRunnable)
|
|
|
|
{ }
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
NS_IMETHOD
|
2012-08-22 19:56:38 +04:00
|
|
|
Dispatch(nsIRunnable* aRunnable, uint32_t aFlags)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aFlags == nsIEventTarget::DISPATCH_NORMAL, "Don't call me!");
|
|
|
|
|
|
|
|
nsRefPtr<WorkerRunnableEventTarget> kungFuDeathGrip = this;
|
|
|
|
|
2012-03-23 21:46:16 +04:00
|
|
|
// Run the runnable we're given now (should just call DummyCallback()),
|
|
|
|
// otherwise the timer thread will leak it... If we run this after
|
|
|
|
// dispatch running the event can race against resetting the timer.
|
|
|
|
aRunnable->Run();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// This can fail if we're racing to terminate or cancel, should be handled
|
|
|
|
// by the terminate or cancel code.
|
2012-07-30 18:20:58 +04:00
|
|
|
mWorkerRunnable->Dispatch(nullptr);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-23 21:46:16 +04:00
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2011-09-29 10:19:26 +04:00
|
|
|
IsOnCurrentThread(bool* aIsOnCurrentThread)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
*aIsOnCurrentThread = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(WorkerRunnableEventTarget, nsIEventTarget)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
class KillCloseEventRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
|
|
|
class KillScriptRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KillScriptRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// Kill running script.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
KillCloseEventRunnable(WorkerPrivate* aWorkerPrivate)
|
2012-02-10 08:54:47 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount,
|
|
|
|
SkipWhenClearing)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
~KillCloseEventRunnable()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Not meant to be dispatched!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
SetTimeout(JSContext* aCx, uint32_t aDelayMS)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (!timer) {
|
|
|
|
JS_ReportError(aCx, "Failed to create timer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<KillScriptRunnable> runnable =
|
|
|
|
new KillScriptRunnable(mWorkerPrivate);
|
|
|
|
|
|
|
|
nsRefPtr<WorkerRunnableEventTarget> target =
|
|
|
|
new WorkerRunnableEventTarget(runnable);
|
|
|
|
|
|
|
|
if (NS_FAILED(timer->SetTarget(target))) {
|
|
|
|
JS_ReportError(aCx, "Failed to set timer's target!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (NS_FAILED(timer->InitWithFuncCallback(DummyCallback, nullptr, aDelayMS,
|
2011-07-17 23:09:13 +04:00
|
|
|
nsITimer::TYPE_ONE_SHOT))) {
|
|
|
|
JS_ReportError(aCx, "Failed to start timer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer.swap(timer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class UpdateJSContextOptionsRunnable : public WorkerControlRunnable
|
|
|
|
{
|
2013-05-17 02:49:43 +04:00
|
|
|
uint32_t mContentOptions;
|
|
|
|
uint32_t mChromeOptions;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
UpdateJSContextOptionsRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-05-17 02:49:43 +04:00
|
|
|
uint32_t aContentOptions,
|
|
|
|
uint32_t aChromeOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
2013-05-17 02:49:43 +04:00
|
|
|
mContentOptions(aContentOptions), mChromeOptions(aChromeOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2013-05-17 02:49:43 +04:00
|
|
|
aWorkerPrivate->UpdateJSContextOptionsInternal(aCx, mContentOptions,
|
|
|
|
mChromeOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-11 02:50:40 +04:00
|
|
|
class UpdateJSWorkerMemoryParameterRunnable : public WorkerControlRunnable
|
2012-01-04 23:11:32 +04:00
|
|
|
{
|
2013-01-11 02:50:40 +04:00
|
|
|
uint32_t mValue;
|
|
|
|
JSGCParamKey mKey;
|
2012-01-04 23:11:32 +04:00
|
|
|
|
|
|
|
public:
|
2013-01-11 02:50:40 +04:00
|
|
|
UpdateJSWorkerMemoryParameterRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
JSGCParamKey aKey,
|
|
|
|
uint32_t aValue)
|
2012-01-04 23:11:32 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
2013-01-11 02:50:40 +04:00
|
|
|
mValue(aValue), mKey(aKey)
|
2012-01-04 23:11:32 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2013-02-16 02:12:19 +04:00
|
|
|
aWorkerPrivate->UpdateJSWorkerMemoryParameterInternal(aCx, mKey, mValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
class UpdateGCZealRunnable : public WorkerControlRunnable
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mGCZeal;
|
2013-05-17 02:49:43 +04:00
|
|
|
uint32_t mFrequency;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
UpdateGCZealRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-05-17 02:49:43 +04:00
|
|
|
uint8_t aGCZeal,
|
|
|
|
uint32_t aFrequency)
|
2011-07-17 23:09:13 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
2013-05-17 02:49:43 +04:00
|
|
|
mGCZeal(aGCZeal), mFrequency(aFrequency)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2013-05-17 02:49:43 +04:00
|
|
|
aWorkerPrivate->UpdateGCZealInternal(aCx, mGCZeal, mFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
class UpdateJITHardeningRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
bool mJITHardening;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UpdateJITHardeningRunnable(WorkerPrivate* aWorkerPrivate, bool aJITHardening)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
|
|
|
mJITHardening(aJITHardening)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->UpdateJITHardeningInternal(aCx, mJITHardening);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
class GarbageCollectRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool mShrinking;
|
|
|
|
bool mCollectChildren;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GarbageCollectRunnable(WorkerPrivate* aWorkerPrivate, bool aShrinking,
|
|
|
|
bool aCollectChildren)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThread, UnchangedBusyCount),
|
|
|
|
mShrinking(aShrinking), mCollectChildren(aCollectChildren)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
// Silence bad assertions, this can be dispatched from either the main
|
|
|
|
// thread or the timer thread..
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
// Silence bad assertions, this can be dispatched from either the main
|
|
|
|
// thread or the timer thread..
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->GarbageCollectInternal(aCx, mShrinking, mCollectChildren);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
class WorkerJSRuntimeStats : public JS::RuntimeStats
|
2011-08-07 05:03:46 +04:00
|
|
|
{
|
2012-12-30 22:21:52 +04:00
|
|
|
const nsACString& mRtPath;
|
2011-08-07 05:03:46 +04:00
|
|
|
|
|
|
|
public:
|
2012-12-30 22:21:52 +04:00
|
|
|
WorkerJSRuntimeStats(const nsACString& aRtPath)
|
|
|
|
: JS::RuntimeStats(JsWorkerMallocSizeOf), mRtPath(aRtPath)
|
2011-08-07 05:03:46 +04:00
|
|
|
{ }
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
~WorkerJSRuntimeStats()
|
2011-08-07 05:03:46 +04:00
|
|
|
{
|
2013-03-17 07:36:37 +04:00
|
|
|
for (size_t i = 0; i != zoneStatsVector.length(); i++) {
|
2013-04-04 19:55:56 +04:00
|
|
|
delete static_cast<xpc::ZoneStatsExtras*>(zoneStatsVector[i].extra);
|
2013-03-17 07:36:37 +04:00
|
|
|
}
|
|
|
|
|
2012-07-13 10:51:01 +04:00
|
|
|
for (size_t i = 0; i != compartmentStatsVector.length(); i++) {
|
2013-04-04 19:55:56 +04:00
|
|
|
delete static_cast<xpc::CompartmentStatsExtras*>(compartmentStatsVector[i].extra);
|
2012-07-13 10:51:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-17 07:36:37 +04:00
|
|
|
virtual void
|
|
|
|
initExtraZoneStats(JS::Zone* aZone,
|
|
|
|
JS::ZoneStats* aZoneStats)
|
|
|
|
MOZ_OVERRIDE
|
|
|
|
{
|
2013-04-04 19:55:56 +04:00
|
|
|
MOZ_ASSERT(!aZoneStats->extra);
|
2013-03-17 07:36:37 +04:00
|
|
|
|
|
|
|
// ReportJSRuntimeExplicitTreeStats expects that
|
2013-04-04 19:55:56 +04:00
|
|
|
// aZoneStats->extra is a xpc::ZoneStatsExtras pointer.
|
|
|
|
xpc::ZoneStatsExtras* extras = new xpc::ZoneStatsExtras;
|
|
|
|
extras->pathPrefix = mRtPath;
|
2013-06-13 07:14:52 +04:00
|
|
|
extras->pathPrefix += nsPrintfCString("zone(0x%p)/", (void *)aZone);
|
2013-04-04 19:55:56 +04:00
|
|
|
aZoneStats->extra = extras;
|
2013-03-17 07:36:37 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
virtual void
|
|
|
|
initExtraCompartmentStats(JSCompartment* aCompartment,
|
|
|
|
JS::CompartmentStats* aCompartmentStats)
|
|
|
|
MOZ_OVERRIDE
|
2012-07-13 10:51:01 +04:00
|
|
|
{
|
2013-04-04 19:55:56 +04:00
|
|
|
MOZ_ASSERT(!aCompartmentStats->extra);
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
// ReportJSRuntimeExplicitTreeStats expects that
|
2013-04-04 19:55:56 +04:00
|
|
|
// aCompartmentStats->extra is a xpc::CompartmentStatsExtras pointer.
|
|
|
|
xpc::CompartmentStatsExtras* extras = new xpc::CompartmentStatsExtras;
|
2012-07-13 10:51:01 +04:00
|
|
|
|
2013-04-04 19:55:56 +04:00
|
|
|
// This is the |jsPathPrefix|. Each worker has exactly two compartments:
|
2012-07-13 10:51:01 +04:00
|
|
|
// one for atoms, and one for everything else.
|
2013-04-04 19:55:56 +04:00
|
|
|
extras->jsPathPrefix.Assign(mRtPath);
|
2013-06-13 07:14:52 +04:00
|
|
|
extras->jsPathPrefix += nsPrintfCString("zone(0x%p)/",
|
2013-04-04 19:55:56 +04:00
|
|
|
(void *)js::GetCompartmentZone(aCompartment));
|
|
|
|
extras->jsPathPrefix += js::IsAtomsCompartment(aCompartment)
|
|
|
|
? NS_LITERAL_CSTRING("compartment(web-worker-atoms)/")
|
|
|
|
: NS_LITERAL_CSTRING("compartment(web-worker)/");
|
2012-07-13 10:51:01 +04:00
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
// This should never be used when reporting with workers (hence the "?!").
|
2013-04-04 19:55:56 +04:00
|
|
|
extras->domPathPrefix.AssignLiteral("explicit/workers/?!/");
|
|
|
|
|
|
|
|
aCompartmentStats->extra = extras;
|
2012-07-13 10:51:01 +04:00
|
|
|
}
|
|
|
|
};
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
class MessagePortRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
uint64_t mMessagePortSerial;
|
|
|
|
bool mConnect;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MessagePortRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
bool aConnect)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThread,
|
|
|
|
aConnect ? ModifyBusyCount : UnchangedBusyCount,
|
|
|
|
SkipWhenClearing),
|
|
|
|
mMessagePortSerial(aMessagePortSerial), mConnect(aConnect)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (mConnect) {
|
|
|
|
return aWorkerPrivate->ConnectMessagePort(aCx, mMessagePortSerial);
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->DisconnectMessagePort(mMessagePortSerial);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
} /* anonymous namespace */
|
2012-07-13 10:51:01 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
mozilla::dom::workers::AssertIsOnMainThread()
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerRunnable::WorkerRunnable(WorkerPrivate* aWorkerPrivate, Target aTarget,
|
2012-02-10 08:54:47 +04:00
|
|
|
BusyBehavior aBusyBehavior,
|
|
|
|
ClearingBehavior aClearingBehavior)
|
2011-07-17 23:09:13 +04:00
|
|
|
: mWorkerPrivate(aWorkerPrivate), mTarget(aTarget),
|
2012-02-10 08:54:47 +04:00
|
|
|
mBusyBehavior(aBusyBehavior), mClearingBehavior(aClearingBehavior)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aWorkerPrivate, "Null worker private!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(WorkerRunnable, nsIRunnable)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRunnable::PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mBusyBehavior == ModifyBusyCount) {
|
|
|
|
NS_ASSERTION(mTarget == WorkerThread,
|
|
|
|
"Don't set this option unless targeting the worker thread!");
|
|
|
|
}
|
|
|
|
if (mTarget == ParentThread) {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-12 20:55:44 +04:00
|
|
|
if (mBusyBehavior == ModifyBusyCount && aCx) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRunnable::Dispatch(JSContext* aCx)
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
if (!aCx) {
|
2012-07-30 18:20:58 +04:00
|
|
|
ok = PreDispatch(nullptr, mWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (ok) {
|
|
|
|
ok = DispatchInternal();
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
PostDispatch(nullptr, mWorkerPrivate, ok);
|
2011-07-17 23:09:13 +04:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
2013-07-30 03:45:27 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
if (global) {
|
|
|
|
ac.construct(aCx, global);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ok = PreDispatch(aCx, mWorkerPrivate);
|
|
|
|
|
|
|
|
if (ok && !DispatchInternal()) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PostDispatch(aCx, mWorkerPrivate, ok);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
WorkerRunnable::DispatchToMainThread(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
NS_ASSERTION(mainThread, "This should never fail!");
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(mainThread->Dispatch(aRunnable, NS_DISPATCH_NORMAL));
|
|
|
|
}
|
|
|
|
|
|
|
|
// These DispatchInternal functions look identical but carry important type
|
|
|
|
// informaton so they can't be consolidated...
|
|
|
|
|
|
|
|
#define IMPL_DISPATCH_INTERNAL(_class) \
|
|
|
|
bool \
|
|
|
|
_class ::DispatchInternal() \
|
|
|
|
{ \
|
|
|
|
if (mTarget == WorkerThread) { \
|
|
|
|
return mWorkerPrivate->Dispatch(this); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (mWorkerPrivate->GetParent()) { \
|
|
|
|
return mWorkerPrivate->GetParent()->Dispatch(this); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return DispatchToMainThread(this); \
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPL_DISPATCH_INTERNAL(WorkerRunnable)
|
|
|
|
IMPL_DISPATCH_INTERNAL(WorkerSyncRunnable)
|
|
|
|
IMPL_DISPATCH_INTERNAL(WorkerControlRunnable)
|
|
|
|
|
|
|
|
#undef IMPL_DISPATCH_INTERNAL
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerRunnable::PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mTarget == ParentThread) {
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-12 20:55:44 +04:00
|
|
|
if (!aDispatchResult && aCx) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (mBusyBehavior == ModifyBusyCount) {
|
|
|
|
aWorkerPrivate->ModifyBusyCount(aCx, false);
|
|
|
|
}
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerRunnable::Run()
|
|
|
|
{
|
|
|
|
JSContext* cx;
|
2012-05-14 22:29:41 +04:00
|
|
|
nsRefPtr<WorkerPrivate> kungFuDeathGrip;
|
2013-05-04 11:53:00 +04:00
|
|
|
nsCxPusher pusher;
|
2012-05-14 22:29:41 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (mTarget == WorkerThread) {
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
cx = mWorkerPrivate->GetJSContext();
|
|
|
|
} else {
|
2012-05-14 22:29:41 +04:00
|
|
|
kungFuDeathGrip = mWorkerPrivate;
|
2011-07-17 23:09:13 +04:00
|
|
|
mWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
cx = mWorkerPrivate->ParentJSContext();
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->GetParent()) {
|
|
|
|
AssertIsOnMainThread();
|
2013-04-18 19:36:03 +04:00
|
|
|
pusher.Push(cx);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Rooted<JSObject*> targetCompartmentObject(cx);
|
|
|
|
|
|
|
|
if (mTarget == WorkerThread) {
|
2013-07-30 03:45:27 +04:00
|
|
|
targetCompartmentObject = JS::CurrentGlobalOrNull(cx);
|
2013-05-04 11:53:00 +04:00
|
|
|
} else {
|
2013-10-14 15:58:05 +04:00
|
|
|
targetCompartmentObject = mWorkerPrivate->GetWrapper();
|
2013-05-04 11:53:00 +04:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(cx, "Must have a context!");
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
if (targetCompartmentObject) {
|
|
|
|
ac.construct(cx, targetCompartmentObject);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = WorkerRun(cx, mWorkerPrivate);
|
2013-07-17 22:53:54 +04:00
|
|
|
// In the case of CompileScriptRunnnable, WorkerRun above can cause us to
|
|
|
|
// lazily create a global, in which case we need to be in its compartment
|
|
|
|
// when calling PostRun() below. Maybe<> this time...
|
|
|
|
if (mTarget == WorkerThread && ac.empty() &&
|
2013-07-30 03:45:25 +04:00
|
|
|
js::DefaultObjectForContextOrNull(cx)) {
|
|
|
|
ac.construct(cx, js::DefaultObjectForContextOrNull(cx));
|
2013-07-17 22:53:54 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
PostRun(cx, mWorkerPrivate, result);
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mTarget == ParentThread) {
|
|
|
|
mWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mBusyBehavior == ModifyBusyCount) {
|
|
|
|
if (!aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false)) {
|
|
|
|
aRunResult = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aRunResult) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
template <class Derived>
|
|
|
|
class WorkerPrivateParent<Derived>::SynchronizeAndResumeRunnable
|
|
|
|
: public nsRunnable
|
|
|
|
{
|
|
|
|
friend class WorkerPrivateParent<Derived>;
|
|
|
|
friend class nsRevocableEventPtr<SynchronizeAndResumeRunnable>;
|
|
|
|
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
nsCOMPtr<nsIScriptContext> mScriptContext;
|
|
|
|
|
|
|
|
SynchronizeAndResumeRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
nsIScriptContext* aScriptContext)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mWindow(aWindow),
|
|
|
|
mScriptContext(aScriptContext)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(!aWorkerPrivate->GetParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
AutoPushJSContext cx(mScriptContext ?
|
|
|
|
mScriptContext->GetNativeContext() :
|
|
|
|
nsContentUtils::GetSafeJSContext());
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->Resume(cx, mWindow)) {
|
|
|
|
JS_ReportPendingException(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Revoke()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(mWindow);
|
|
|
|
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
mWindow = nullptr;
|
|
|
|
mScriptContext = nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
struct WorkerPrivate::TimeoutInfo
|
|
|
|
{
|
|
|
|
TimeoutInfo()
|
2012-05-11 19:46:26 +04:00
|
|
|
: mTimeoutVal(JS::UndefinedValue()), mLineNumber(0), mId(0), mIsInterval(false),
|
2011-07-17 23:09:13 +04:00
|
|
|
mCanceled(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::WorkerPrivate::TimeoutInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
~TimeoutInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::WorkerPrivate::TimeoutInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const TimeoutInfo& aOther)
|
|
|
|
{
|
|
|
|
return mTargetTime == aOther.mTargetTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const TimeoutInfo& aOther)
|
|
|
|
{
|
|
|
|
return mTargetTime < aOther.mTargetTime;
|
|
|
|
}
|
|
|
|
|
2013-07-23 13:58:27 +04:00
|
|
|
JS::Heap<JS::Value> mTimeoutVal;
|
|
|
|
nsTArray<JS::Heap<JS::Value> > mExtraArgVals;
|
2011-07-17 23:09:13 +04:00
|
|
|
mozilla::TimeStamp mTargetTime;
|
|
|
|
mozilla::TimeDuration mInterval;
|
|
|
|
nsCString mFilename;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLineNumber;
|
|
|
|
uint32_t mId;
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mIsInterval;
|
|
|
|
bool mCanceled;
|
|
|
|
};
|
|
|
|
|
2013-08-28 03:24:51 +04:00
|
|
|
class WorkerPrivate::MemoryReporter MOZ_FINAL : public nsIMemoryReporter
|
2013-02-08 15:50:00 +04:00
|
|
|
{
|
|
|
|
friend class WorkerPrivate;
|
|
|
|
|
|
|
|
SharedMutex mMutex;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsCString mRtPath;
|
2013-06-15 06:48:28 +04:00
|
|
|
bool mAlreadyMappedToAddon;
|
2013-02-08 15:50:00 +04:00
|
|
|
|
|
|
|
public:
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-02-08 15:50:00 +04:00
|
|
|
|
|
|
|
MemoryReporter(WorkerPrivate* aWorkerPrivate)
|
2013-06-15 06:48:28 +04:00
|
|
|
: mMutex(aWorkerPrivate->mMutex), mWorkerPrivate(aWorkerPrivate),
|
|
|
|
mAlreadyMappedToAddon(false)
|
2013-02-08 15:50:00 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCString escapedDomain(aWorkerPrivate->Domain());
|
|
|
|
escapedDomain.ReplaceChar('/', '\\');
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 escapedURL(aWorkerPrivate->ScriptURL());
|
|
|
|
escapedURL.ReplaceChar('/', '\\');
|
|
|
|
|
|
|
|
nsAutoCString addressString;
|
|
|
|
addressString.AppendPrintf("0x%p", static_cast<void*>(aWorkerPrivate));
|
|
|
|
|
|
|
|
mRtPath = NS_LITERAL_CSTRING("explicit/workers/workers(") +
|
|
|
|
escapedDomain + NS_LITERAL_CSTRING(")/worker(") +
|
|
|
|
escapedURL + NS_LITERAL_CSTRING(", ") + addressString +
|
|
|
|
NS_LITERAL_CSTRING(")/");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
GetName(nsACString& aName)
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("workers");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2013-08-28 03:24:51 +04:00
|
|
|
CollectReports(nsIMemoryReporterCallback* aCallback,
|
2013-02-08 15:50:00 +04:00
|
|
|
nsISupports* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-06-15 06:48:28 +04:00
|
|
|
// Assumes that WorkerJSRuntimeStats will hold a reference to mRtPath,
|
|
|
|
// and not a copy, as TryToMapAddon() may later modify the string again.
|
2013-02-08 15:50:00 +04:00
|
|
|
WorkerJSRuntimeStats rtStats(mRtPath);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-06-15 06:48:28 +04:00
|
|
|
TryToMapAddon();
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
if (!mWorkerPrivate ||
|
2013-04-12 07:52:32 +04:00
|
|
|
!mWorkerPrivate->BlockAndCollectRuntimeStats(&rtStats)) {
|
2013-02-08 15:50:00 +04:00
|
|
|
// Returning NS_OK here will effectively report 0 memory.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return xpc::ReportJSRuntimeExplicitTreeStats(rtStats, mRtPath,
|
|
|
|
aCallback, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~MemoryReporter()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
Disable()
|
|
|
|
{
|
|
|
|
// Called from WorkerPrivate::DisableMemoryReporter.
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
NS_ASSERTION(mWorkerPrivate, "Disabled more than once!");
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
2013-06-15 06:48:28 +04:00
|
|
|
|
|
|
|
// Only call this from the main thread and under mMutex lock.
|
|
|
|
void
|
|
|
|
TryToMapAddon()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (mAlreadyMappedToAddon || !mWorkerPrivate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scriptURI;
|
|
|
|
if (NS_FAILED(NS_NewURI(getter_AddRefs(scriptURI),
|
|
|
|
mWorkerPrivate->ScriptURL()))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAlreadyMappedToAddon = true;
|
|
|
|
|
|
|
|
nsAutoCString addonId;
|
|
|
|
bool ok;
|
|
|
|
nsCOMPtr<amIAddonManager> addonManager =
|
|
|
|
do_GetService("@mozilla.org/addons/integration;1");
|
|
|
|
|
|
|
|
if (!addonManager ||
|
|
|
|
NS_FAILED(addonManager->MapURIToAddonID(scriptURI, addonId, &ok)) ||
|
|
|
|
!ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t explicitLength = strlen("explicit/");
|
|
|
|
addonId.Insert(NS_LITERAL_CSTRING("add-ons/"), 0);
|
|
|
|
addonId += "/";
|
|
|
|
mRtPath.Insert(addonId, explicitLength);
|
|
|
|
}
|
2013-02-08 15:50:00 +04:00
|
|
|
};
|
|
|
|
|
2013-08-28 03:24:51 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(WorkerPrivate::MemoryReporter, nsIMemoryReporter)
|
2013-02-08 15:50:00 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
template <class Derived>
|
|
|
|
WorkerPrivateParent<Derived>::WorkerPrivateParent(
|
2013-10-14 15:58:05 +04:00
|
|
|
JSContext* aCx,
|
|
|
|
WorkerPrivate* aParent,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker,
|
2013-06-05 18:04:23 +04:00
|
|
|
bool aIsSharedWorker,
|
|
|
|
const nsAString& aSharedWorkerName,
|
|
|
|
LoadInfo& aLoadInfo)
|
2013-10-14 15:58:05 +04:00
|
|
|
: mMutex("WorkerPrivateParent Mutex"),
|
2011-07-17 23:09:13 +04:00
|
|
|
mCondVar(mMutex, "WorkerPrivateParent CondVar"),
|
2012-12-30 22:21:52 +04:00
|
|
|
mMemoryReportCondVar(mMutex, "WorkerPrivateParent Memory Report CondVar"),
|
2013-10-14 15:58:05 +04:00
|
|
|
mParent(aParent), mScriptURL(aScriptURL),
|
2013-06-05 18:04:23 +04:00
|
|
|
mSharedWorkerName(aSharedWorkerName), mBusyCount(0), mMessagePortSerial(0),
|
2013-10-14 15:58:05 +04:00
|
|
|
mParentStatus(Pending), mRooted(false), mParentSuspended(false),
|
2013-06-05 18:04:23 +04:00
|
|
|
mIsChromeWorker(aIsChromeWorker), mMainThreadObjectsForgotten(false),
|
|
|
|
mIsSharedWorker(aIsSharedWorker)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-14 15:58:05 +04:00
|
|
|
SetIsDOMBinding();
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(aIsSharedWorker, !aSharedWorkerName.IsVoid() &&
|
|
|
|
NS_IsMainThread());
|
|
|
|
MOZ_ASSERT_IF(!aIsSharedWorker, aSharedWorkerName.IsEmpty());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (aLoadInfo.mWindow) {
|
2013-10-14 15:58:05 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aLoadInfo.mWindow->IsInnerWindow(),
|
|
|
|
"Should have inner window here!");
|
|
|
|
BindToOwner(aLoadInfo.mWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
mLoadInfo.StealFrom(aLoadInfo);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
if (aParent) {
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
aParent->CopyJSSettings(mJSSettings);
|
2012-01-04 23:11:32 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
NS_ASSERTION(JS_GetOptions(aCx) == (aParent->IsChromeWorker() ?
|
|
|
|
mJSSettings.chrome.options :
|
|
|
|
mJSSettings.content.options),
|
|
|
|
"Options mismatch!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
RuntimeService::GetDefaultJSSettings(mJSSettings);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-14 07:10:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-14 08:59:58 +04:00
|
|
|
WorkerPrivateParent<Derived>::~WorkerPrivateParent()
|
2013-10-14 07:10:43 +04:00
|
|
|
{
|
2013-10-14 15:58:05 +04:00
|
|
|
MOZ_ASSERT(!mRooted);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_ADDREF_INHERITED(WorkerPrivateParent<Derived>, nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WorkerPrivateParent<Derived>, nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WorkerPrivateParent<Derived>)
|
|
|
|
// No new interfaces, just cycle collection.
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WorkerPrivateParent<Derived>,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
// Nothing else to traverse
|
|
|
|
// The various strong references in LoadInfo are managed manually and cannot
|
|
|
|
// be cycle collected.
|
|
|
|
tmp->AssertIsOnParentThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WorkerPrivateParent<Derived>,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
tmp->AssertIsOnParentThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WorkerPrivateParent<Derived>,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
tmp->AssertIsOnParentThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
JSObject*
|
|
|
|
WorkerPrivateParent<Derived>::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsSharedWorker,
|
|
|
|
"We should never wrap a WorkerPrivate for a SharedWorker");
|
|
|
|
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
JSObject* obj = WorkerBinding::Wrap(aCx, aScope, ParentAsWorkerPrivate());
|
|
|
|
|
|
|
|
if (mRooted) {
|
|
|
|
PreserveWrapper(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::Start()
|
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
NS_ASSERTION(mParentStatus != Running, "How can this be?!");
|
|
|
|
|
|
|
|
if (mParentStatus == Pending) {
|
|
|
|
mParentStatus = Running;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-19 18:34:58 +04:00
|
|
|
// aCx is null when called from the finalizer
|
2011-07-17 23:09:13 +04:00
|
|
|
template <class Derived>
|
|
|
|
bool
|
2012-03-19 18:34:58 +04:00
|
|
|
WorkerPrivateParent<Derived>::NotifyPrivate(JSContext* aCx, Status aStatus)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
bool pending;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mParentStatus >= aStatus) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pending = mParentStatus == Pending;
|
|
|
|
mParentStatus = aStatus;
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (IsSharedWorker()) {
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
MOZ_ASSERT(runtime);
|
|
|
|
|
|
|
|
runtime->ForgetSharedWorker(ParentAsWorkerPrivate());
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (pending) {
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
// Silence useless assertions in debug builds.
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(currentThread, "This should never be null!");
|
|
|
|
|
|
|
|
self->SetThread(currentThread);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Worker never got a chance to run, go ahead and delete it.
|
|
|
|
self->ScheduleDeletion(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// Only top-level workers should have a synchronize runnable.
|
|
|
|
MOZ_ASSERT_IF(mSynchronizeRunnable.get(), !GetParent());
|
|
|
|
mSynchronizeRunnable.Revoke();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(aStatus != Terminating || mQueuedRunnables.IsEmpty(),
|
|
|
|
"Shouldn't have anything queued!");
|
|
|
|
|
|
|
|
// Anything queued will be discarded.
|
|
|
|
mQueuedRunnables.Clear();
|
|
|
|
|
|
|
|
nsRefPtr<NotifyRunnable> runnable =
|
2012-08-08 01:38:46 +04:00
|
|
|
new NotifyRunnable(ParentAsWorkerPrivate(), aStatus);
|
2012-03-19 18:34:58 +04:00
|
|
|
return runnable->Dispatch(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivateParent<Derived>::Suspend(JSContext* aCx, nsPIDOMWindow* aWindow)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
|
|
|
// Shared workers are only suspended if all of their owning documents are
|
|
|
|
// suspended.
|
|
|
|
if (IsSharedWorker()) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mSharedWorkers.Count());
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
bool mAllSuspended;
|
|
|
|
|
|
|
|
Closure(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow), mAllSuspended(true)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
// aWindow may be null here.
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
Suspend(const uint64_t& aKey,
|
|
|
|
SharedWorker* aSharedWorker,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto closure = static_cast<Closure*>(aClosure);
|
|
|
|
|
|
|
|
if (closure->mWindow && aSharedWorker->GetOwner() == closure->mWindow) {
|
|
|
|
// Calling Suspend() may change the refcount, ensure that the worker
|
|
|
|
// outlives this call.
|
|
|
|
nsRefPtr<SharedWorker> kungFuDeathGrip = aSharedWorker;
|
|
|
|
|
|
|
|
aSharedWorker->Suspend();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_IF(aSharedWorker->GetOwner() && closure->mWindow,
|
|
|
|
!SameCOMIdentity(aSharedWorker->GetOwner(),
|
|
|
|
closure->mWindow));
|
|
|
|
if (!aSharedWorker->IsSuspended()) {
|
|
|
|
closure->mAllSuspended = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Closure closure(aWindow);
|
|
|
|
|
|
|
|
mSharedWorkers.EnumerateRead(Closure::Suspend, &closure);
|
|
|
|
|
|
|
|
if (!closure.mAllSuspended || mParentSuspended) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mParentSuspended, "Suspended more than once!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
mParentSuspended = true;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mParentStatus >= Terminating) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<SuspendRunnable> runnable =
|
|
|
|
new SuspendRunnable(ParentAsWorkerPrivate());
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2013-06-05 18:04:23 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::Resume(JSContext* aCx, nsPIDOMWindow* aWindow)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT_IF(!IsSharedWorker(), mParentSuspended);
|
|
|
|
|
|
|
|
// Shared workers are resumed if any of their owning documents are resumed.
|
|
|
|
if (IsSharedWorker()) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mSharedWorkers.Count());
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
bool mAnyRunning;
|
|
|
|
|
|
|
|
Closure(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow), mAnyRunning(false)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
// aWindow may be null here.
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
Resume(const uint64_t& aKey,
|
|
|
|
SharedWorker* aSharedWorker,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto closure = static_cast<Closure*>(aClosure);
|
|
|
|
|
|
|
|
if (closure->mWindow && aSharedWorker->GetOwner() == closure->mWindow) {
|
|
|
|
// Calling Resume() may change the refcount, ensure that the worker
|
|
|
|
// outlives this call.
|
|
|
|
nsRefPtr<SharedWorker> kungFuDeathGrip = aSharedWorker;
|
|
|
|
|
|
|
|
aSharedWorker->Resume();
|
|
|
|
closure->mAnyRunning = true;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_IF(aSharedWorker->GetOwner() && closure->mWindow,
|
|
|
|
!SameCOMIdentity(aSharedWorker->GetOwner(),
|
|
|
|
closure->mWindow));
|
|
|
|
if (!aSharedWorker->IsSuspended()) {
|
|
|
|
closure->mAnyRunning = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Closure closure(aWindow);
|
|
|
|
|
|
|
|
mSharedWorkers.EnumerateRead(Closure::Resume, &closure);
|
|
|
|
|
|
|
|
if (!closure.mAnyRunning || !mParentSuspended) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mParentSuspended);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
mParentSuspended = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mParentStatus >= Terminating) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// Only top-level workers should have a synchronize runnable.
|
|
|
|
MOZ_ASSERT_IF(mSynchronizeRunnable.get(), !GetParent());
|
|
|
|
mSynchronizeRunnable.Revoke();
|
|
|
|
|
2013-06-06 21:28:47 +04:00
|
|
|
// Execute queued runnables before waking up the worker, otherwise the worker
|
2011-07-17 23:09:13 +04:00
|
|
|
// could post new messages before we run those that have been queued.
|
|
|
|
if (!mQueuedRunnables.IsEmpty()) {
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(!IsSharedWorker());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsTArray<nsRefPtr<WorkerRunnable> > runnables;
|
|
|
|
mQueuedRunnables.SwapElements(runnables);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < runnables.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsRefPtr<WorkerRunnable>& runnable = runnables[index];
|
2013-06-06 21:28:47 +04:00
|
|
|
runnable->Run();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ResumeRunnable> runnable =
|
|
|
|
new ResumeRunnable(ParentAsWorkerPrivate());
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-06-06 21:28:47 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-06 21:28:47 +04:00
|
|
|
template <class Derived>
|
|
|
|
bool
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivateParent<Derived>::SynchronizeAndResume(
|
|
|
|
JSContext* aCx,
|
|
|
|
nsPIDOMWindow* aWindow,
|
|
|
|
nsIScriptContext* aScriptContext)
|
2013-06-06 21:28:47 +04:00
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(!GetParent());
|
|
|
|
MOZ_ASSERT_IF(!IsSharedWorker(), mParentSuspended);
|
2013-06-06 21:28:47 +04:00
|
|
|
|
|
|
|
// NB: There may be pending unqueued messages. If we resume here we will
|
|
|
|
// execute those messages out of order. Instead we post an event to the
|
|
|
|
// end of the event queue, allowing all of the outstanding messages to be
|
|
|
|
// queued up in order on the worker. Then and only then we execute all of
|
|
|
|
// the messages.
|
|
|
|
|
|
|
|
nsRefPtr<SynchronizeAndResumeRunnable> runnable =
|
2013-06-05 18:04:23 +04:00
|
|
|
new SynchronizeAndResumeRunnable(ParentAsWorkerPrivate(), aWindow,
|
|
|
|
aScriptContext);
|
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
|
|
|
|
JS_ReportError(aCx, "Failed to dispatch to current thread!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSynchronizeRunnable = runnable;
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
2012-04-27 00:57:33 +04:00
|
|
|
WorkerPrivateParent<Derived>::_finalize(JSFreeOp* aFop)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
MOZ_ASSERT(!mRooted);
|
|
|
|
|
|
|
|
ClearWrapper();
|
2011-10-07 03:09:43 +04:00
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
// Ensure that we're held alive across the TerminatePrivate call, and then
|
|
|
|
// release the reference our wrapper held to us.
|
|
|
|
nsRefPtr<WorkerPrivateParent<Derived> > kungFuDeathGrip = dont_AddRef(this);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!TerminatePrivate(nullptr)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_WARNING("Failed to terminate!");
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::Close(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mParentStatus < Closing) {
|
|
|
|
mParentStatus = Closing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::ModifyBusyCount(JSContext* aCx, bool aIncrease)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(aIncrease || mBusyCount, "Mismatched busy count mods!");
|
|
|
|
|
|
|
|
if (aIncrease) {
|
2013-10-14 15:58:05 +04:00
|
|
|
if (mBusyCount++ == 0) {
|
|
|
|
Root(true);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
if (--mBusyCount == 0) {
|
|
|
|
Root(false);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
bool shouldCancel;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
shouldCancel = mParentStatus == Terminating;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldCancel && !Cancel(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-14 15:58:05 +04:00
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::Root(bool aRoot)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
if (aRoot == mRooted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRoot) {
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
if (GetWrapperPreserveColor()) {
|
|
|
|
PreserveWrapper(this);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-14 15:58:05 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (GetWrapperPreserveColor()) {
|
|
|
|
ReleaseWrapper(this);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-14 15:58:05 +04:00
|
|
|
NS_RELEASE_THIS();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
mRooted = aRoot;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::ForgetMainThreadObjects(
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aDoomed)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
2012-03-31 08:42:20 +04:00
|
|
|
MOZ_ASSERT(!mMainThreadObjectsForgotten);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
static const uint32_t kDoomedCount = 7;
|
|
|
|
|
|
|
|
aDoomed.SetCapacity(kDoomedCount);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
SwapToISupportsArray(mLoadInfo.mWindow, aDoomed);
|
|
|
|
SwapToISupportsArray(mLoadInfo.mScriptContext, aDoomed);
|
|
|
|
SwapToISupportsArray(mLoadInfo.mBaseURI, aDoomed);
|
|
|
|
SwapToISupportsArray(mLoadInfo.mResolvedScriptURI, aDoomed);
|
|
|
|
SwapToISupportsArray(mLoadInfo.mPrincipal, aDoomed);
|
|
|
|
SwapToISupportsArray(mLoadInfo.mChannel, aDoomed);
|
|
|
|
SwapToISupportsArray(mLoadInfo.mCSP, aDoomed);
|
|
|
|
// Before adding anything here update kDoomedCount above!
|
|
|
|
|
|
|
|
MOZ_ASSERT(aDoomed.Length() == kDoomedCount);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
mMainThreadObjectsForgotten = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-14 15:58:05 +04:00
|
|
|
void
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivateParent<Derived>::PostMessageInternal(
|
|
|
|
JSContext* aCx,
|
2013-10-14 15:58:05 +04:00
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value> >& aTransferable,
|
2013-06-05 18:04:23 +04:00
|
|
|
bool aToMessagePort,
|
2013-10-14 15:58:05 +04:00
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2012-12-14 20:16:11 +04:00
|
|
|
if (mParentStatus > Running) {
|
2013-10-14 15:58:05 +04:00
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JSStructuredCloneCallbacks* callbacks;
|
|
|
|
if (GetParent()) {
|
2011-08-16 07:40:38 +04:00
|
|
|
if (IsChromeWorker()) {
|
|
|
|
callbacks = &gChromeWorkerStructuredCloneCallbacks;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callbacks = &gWorkerStructuredCloneCallbacks;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (IsChromeWorker()) {
|
|
|
|
callbacks = &gMainThreadChromeWorkerStructuredCloneCallbacks;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callbacks = &gMainThreadWorkerStructuredCloneCallbacks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
|
|
|
JSObject* array =
|
|
|
|
JS_NewArrayObject(aCx, realTransferable.Length(),
|
|
|
|
const_cast<JS::Value*>(realTransferable.Elements()));
|
|
|
|
if (!array) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
2013-10-14 15:58:05 +04:00
|
|
|
if (!buffer.write(aCx, aMessage, transferable, callbacks, &clonedObjects)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MessageEventRunnable> runnable =
|
|
|
|
new MessageEventRunnable(ParentAsWorkerPrivate(),
|
2011-08-16 07:40:38 +04:00
|
|
|
WorkerRunnable::WorkerThread, buffer,
|
2013-06-05 18:04:23 +04:00
|
|
|
clonedObjects, aToMessagePort, aMessagePortSerial);
|
2013-10-14 15:58:05 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivateParent<Derived>::PostMessageToMessagePort(
|
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
PostMessageInternal(aCx, aMessage, aTransferable, true, aMessagePortSerial,
|
|
|
|
aRv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-01-04 23:11:32 +04:00
|
|
|
template <class Derived>
|
2013-06-05 18:04:23 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::DispatchMessageEventToMessagePort(
|
|
|
|
JSContext* aCx, uint64_t aMessagePortSerial,
|
|
|
|
JSAutoStructuredCloneBuffer& aBuffer,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects)
|
2012-01-04 23:11:32 +04:00
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
buffer.swap(aBuffer);
|
2013-05-17 02:49:43 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>> clonedObjects;
|
|
|
|
clonedObjects.SwapElements(aClonedObjects);
|
|
|
|
|
|
|
|
SharedWorker* sharedWorker;
|
|
|
|
if (!mSharedWorkers.Get(aMessagePortSerial, &sharedWorker)) {
|
|
|
|
// SharedWorker has already been unregistered?
|
|
|
|
return true;
|
2013-01-11 02:50:40 +04:00
|
|
|
}
|
2012-01-04 23:11:32 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsRefPtr<MessagePort> port = sharedWorker->Port();
|
|
|
|
NS_ASSERTION(port, "SharedWorkers always have a port!");
|
|
|
|
|
|
|
|
if (port->IsClosed()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo;
|
|
|
|
port->GetParentObject(getter_AddRefs(sgo));
|
|
|
|
MOZ_ASSERT(sgo, "Should never happen if IsClosed() returned false!");
|
|
|
|
MOZ_ASSERT(sgo->GetGlobalJSObject());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> scx = sgo->GetContext();
|
|
|
|
MOZ_ASSERT_IF(scx, scx->GetNativeContext());
|
|
|
|
|
|
|
|
AutoPushJSContext cx(scx ? scx->GetNativeContext() : aCx);
|
|
|
|
JSAutoCompartment(cx, sgo->GetGlobalJSObject());
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> data(cx);
|
|
|
|
if (!buffer.read(cx, data.address(), WorkerStructuredCloneCallbacks(true))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
nsRefPtr<nsDOMMessageEvent> event =
|
|
|
|
new nsDOMMessageEvent(port, nullptr, nullptr);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
event->InitMessageEvent(NS_LITERAL_STRING("message"), false, false, data,
|
|
|
|
EmptyString(), EmptyString(), nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(cx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> domEvent;
|
|
|
|
CallQueryInterface(event.get(), getter_AddRefs(domEvent));
|
|
|
|
NS_ASSERTION(domEvent, "This should never fail!");
|
|
|
|
|
|
|
|
bool ignored;
|
|
|
|
rv = port->DispatchEvent(domEvent, &ignored);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(cx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
uint64_t
|
|
|
|
WorkerPrivateParent<Derived>::GetInnerWindowId()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
NS_ASSERTION(!mLoadInfo.mWindow || mLoadInfo.mWindow->IsInnerWindow(),
|
|
|
|
"Outer window?");
|
|
|
|
return mLoadInfo.mWindow ? mLoadInfo.mWindow->WindowID() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UpdateJSContextOptions(JSContext* aCx,
|
|
|
|
uint32_t aContentOptions,
|
|
|
|
uint32_t aChromeOptions)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mJSSettings.content.options = aContentOptions;
|
|
|
|
mJSSettings.chrome.options = aChromeOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<UpdateJSContextOptionsRunnable> runnable =
|
|
|
|
new UpdateJSContextOptionsRunnable(ParentAsWorkerPrivate(), aContentOptions,
|
|
|
|
aChromeOptions);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker context options!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UpdateJSWorkerMemoryParameter(JSContext* aCx,
|
|
|
|
JSGCParamKey aKey,
|
|
|
|
uint32_t aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
found = mJSSettings.ApplyGCSetting(aKey, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
nsRefPtr<UpdateJSWorkerMemoryParameterRunnable> runnable =
|
|
|
|
new UpdateJSWorkerMemoryParameterRunnable(ParentAsWorkerPrivate(), aKey,
|
|
|
|
aValue);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update memory parameter!");
|
|
|
|
JS_ClearPendingException(aCx);
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2012-01-04 23:11:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2013-05-17 02:49:43 +04:00
|
|
|
WorkerPrivateParent<Derived>::UpdateGCZeal(JSContext* aCx, uint8_t aGCZeal,
|
|
|
|
uint32_t aFrequency)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mJSSettings.gcZeal = aGCZeal;
|
|
|
|
mJSSettings.gcZealFrequency = aFrequency;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsRefPtr<UpdateGCZealRunnable> runnable =
|
2013-05-17 02:49:43 +04:00
|
|
|
new UpdateGCZealRunnable(ParentAsWorkerPrivate(), aGCZeal, aFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker gczeal!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UpdateJITHardening(JSContext* aCx,
|
|
|
|
bool aJITHardening)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mJSSettings.jitHardening = aJITHardening;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<UpdateJITHardeningRunnable> runnable =
|
|
|
|
new UpdateJITHardeningRunnable(ParentAsWorkerPrivate(), aJITHardening);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker jit hardening!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::GarbageCollect(JSContext* aCx, bool aShrinking)
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
nsRefPtr<GarbageCollectRunnable> runnable =
|
|
|
|
new GarbageCollectRunnable(ParentAsWorkerPrivate(), aShrinking, true);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker heap size!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::RegisterSharedWorker(JSContext* aCx,
|
|
|
|
SharedWorker* aSharedWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(IsSharedWorker());
|
|
|
|
MOZ_ASSERT(!mSharedWorkers.Get(aSharedWorker->Serial()));
|
|
|
|
|
|
|
|
nsRefPtr<MessagePortRunnable> runnable =
|
|
|
|
new MessagePortRunnable(ParentAsWorkerPrivate(), aSharedWorker->Serial(),
|
|
|
|
true);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedWorkers.Put(aSharedWorker->Serial(), aSharedWorker);
|
|
|
|
|
|
|
|
// If there were other SharedWorker objects attached to this worker then they
|
|
|
|
// may all have been suspended and this worker would need to be resumed.
|
|
|
|
if (mSharedWorkers.Count() > 1 && !Resume(aCx, nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UnregisterSharedWorker(
|
|
|
|
JSContext* aCx,
|
|
|
|
SharedWorker* aSharedWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(IsSharedWorker());
|
|
|
|
MOZ_ASSERT(mSharedWorkers.Get(aSharedWorker->Serial()));
|
|
|
|
|
|
|
|
nsRefPtr<MessagePortRunnable> runnable =
|
|
|
|
new MessagePortRunnable(ParentAsWorkerPrivate(), aSharedWorker->Serial(),
|
|
|
|
false);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedWorkers.Remove(aSharedWorker->Serial());
|
|
|
|
|
|
|
|
// If there are still SharedWorker objects attached to this worker then they
|
|
|
|
// may all be suspended and this worker would need to be suspended. Otherwise,
|
|
|
|
// if that was the last SharedWorker then it's time to cancel this worker.
|
|
|
|
if (mSharedWorkers.Count()) {
|
|
|
|
if (!Suspend(aCx, nullptr)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
} else if (!Cancel(aCx)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::BroadcastErrorToSharedWorkers(
|
|
|
|
JSContext* aCx,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aFilename,
|
|
|
|
const nsAString& aLine,
|
|
|
|
uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsAutoTArray<nsRefPtr<SharedWorker>, 10> sharedWorkers;
|
|
|
|
GetAllSharedWorkers(sharedWorkers);
|
|
|
|
|
|
|
|
if (sharedWorkers.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<WindowAction, 10> windowActions;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// First fire the error event at all SharedWorker objects. This may include
|
|
|
|
// multiple objects in a single window as well as objects in different
|
|
|
|
// windows.
|
|
|
|
for (uint32_t index = 0; index < sharedWorkers.Length(); index++) {
|
|
|
|
nsRefPtr<SharedWorker>& sharedWorker = sharedWorkers[index];
|
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsPIDOMWindow* window = sharedWorker->GetOwner();
|
|
|
|
|
|
|
|
uint32_t actionsIndex = windowActions.LastIndexOf(WindowAction(window));
|
|
|
|
|
|
|
|
// Get the context for this window so that we can report errors correctly.
|
|
|
|
JSContext* cx;
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
if (actionsIndex == windowActions.NoIndex) {
|
|
|
|
nsIScriptContext* scx = sharedWorker->GetContextForEventHandlers(&rv);
|
|
|
|
cx = (NS_SUCCEEDED(rv) && scx) ?
|
|
|
|
scx->GetNativeContext() :
|
|
|
|
nsContentUtils::GetSafeJSContext();
|
|
|
|
} else {
|
|
|
|
cx = windowActions[actionsIndex].mJSContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoCxPusher autoPush(cx);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Throw(cx, rv);
|
|
|
|
JS_ReportPendingException(cx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorEventInit errorInit;
|
|
|
|
errorInit.mBubbles = false;
|
|
|
|
errorInit.mCancelable = true;
|
|
|
|
errorInit.mMessage = aMessage;
|
|
|
|
errorInit.mFilename = aFilename;
|
|
|
|
errorInit.mLineno = aLineNumber;
|
|
|
|
errorInit.mColumn = aColumnNumber;
|
|
|
|
|
|
|
|
nsRefPtr<ErrorEvent> errorEvent =
|
|
|
|
ErrorEvent::Constructor(sharedWorker, NS_LITERAL_STRING("error"),
|
|
|
|
errorInit);
|
|
|
|
if (!errorEvent) {
|
|
|
|
Throw(cx, NS_ERROR_UNEXPECTED);
|
|
|
|
JS_ReportPendingException(cx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
errorEvent->SetTrusted(true);
|
|
|
|
|
|
|
|
bool defaultActionEnabled;
|
|
|
|
rv = sharedWorker->DispatchEvent(errorEvent, &defaultActionEnabled);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Throw(cx, rv);
|
|
|
|
JS_ReportPendingException(cx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultActionEnabled) {
|
|
|
|
// Add the owning window to our list so that we will fire an error event
|
|
|
|
// at it later.
|
|
|
|
if (!windowActions.Contains(window)) {
|
|
|
|
windowActions.AppendElement(WindowAction(window, cx));
|
|
|
|
}
|
|
|
|
} else if (actionsIndex != windowActions.NoIndex) {
|
|
|
|
// Any listener that calls preventDefault() will prevent the window from
|
|
|
|
// receiving the error event.
|
|
|
|
windowActions[actionsIndex].mDefaultAction = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no windows to consider further then we're done.
|
|
|
|
if (windowActions.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldLogErrorToConsole = true;
|
|
|
|
|
|
|
|
// Now fire error events at all the windows remaining.
|
|
|
|
for (uint32_t index = 0; index < windowActions.Length(); index++) {
|
|
|
|
WindowAction& windowAction = windowActions[index];
|
|
|
|
|
|
|
|
// If there is no window or the script already called preventDefault then
|
|
|
|
// skip this window.
|
|
|
|
if (!windowAction.mWindow || !windowAction.mDefaultAction) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = windowAction.mJSContext;
|
|
|
|
|
|
|
|
AutoCxPusher autoPush(cx);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo =
|
|
|
|
do_QueryInterface(windowAction.mWindow);
|
|
|
|
MOZ_ASSERT(sgo);
|
|
|
|
|
|
|
|
InternalScriptErrorEvent event(true, NS_LOAD_ERROR);
|
|
|
|
event.lineNr = aLineNumber;
|
|
|
|
event.errorMsg = aMessage.BeginReading();
|
|
|
|
event.fileName = aFilename.BeginReading();
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
rv = sgo->HandleScriptError(&event, &status);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Throw(cx, rv);
|
|
|
|
JS_ReportPendingException(cx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == nsEventStatus_eConsumeNoDefault) {
|
|
|
|
shouldLogErrorToConsole = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally log a warning in the console if no window tried to prevent it.
|
|
|
|
if (shouldLogErrorToConsole) {
|
|
|
|
LogErrorToConsole(aMessage, aFilename, aLine, aLineNumber, aColumnNumber,
|
|
|
|
aFlags, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::GetAllSharedWorkers(
|
|
|
|
nsTArray<nsRefPtr<SharedWorker>>& aSharedWorkers)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(IsSharedWorker());
|
|
|
|
|
|
|
|
struct Helper
|
|
|
|
{
|
|
|
|
static PLDHashOperator
|
|
|
|
Collect(const uint64_t& aKey,
|
|
|
|
SharedWorker* aSharedWorker,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto array = static_cast<nsTArray<nsRefPtr<SharedWorker>>*>(aClosure);
|
|
|
|
array->AppendElement(aSharedWorker);
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!aSharedWorkers.IsEmpty()) {
|
|
|
|
aSharedWorkers.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedWorkers.EnumerateRead(Helper::Collect, &aSharedWorkers);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::CloseSharedWorkersForWindow(
|
|
|
|
nsPIDOMWindow* aWindow)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(IsSharedWorker());
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
nsAutoTArray<nsRefPtr<SharedWorker>, 10> mSharedWorkers;
|
|
|
|
|
|
|
|
Closure(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
Collect(const uint64_t& aKey,
|
|
|
|
SharedWorker* aSharedWorker,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto closure = static_cast<Closure*>(aClosure);
|
|
|
|
MOZ_ASSERT(closure->mWindow);
|
|
|
|
|
|
|
|
if (aSharedWorker->GetOwner() == closure->mWindow) {
|
|
|
|
closure->mSharedWorkers.AppendElement(aSharedWorker);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!SameCOMIdentity(aSharedWorker->GetOwner(),
|
|
|
|
closure->mWindow));
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Closure closure(aWindow);
|
|
|
|
|
|
|
|
mSharedWorkers.EnumerateRead(Closure::Collect, &closure);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < closure.mSharedWorkers.Length(); index++) {
|
|
|
|
closure.mSharedWorkers[index]->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::WorkerScriptLoaded()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (IsSharedWorker()) {
|
|
|
|
// No longer need to hold references to the window or document we came from.
|
|
|
|
mLoadInfo.mWindow = nullptr;
|
|
|
|
mLoadInfo.mScriptContext = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
template <class Derived>
|
2011-11-04 20:32:17 +04:00
|
|
|
void
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivateParent<Derived>::SetBaseURI(nsIURI* aBaseURI)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!mLoadInfo.mBaseURI) {
|
|
|
|
NS_ASSERTION(GetParent(), "Shouldn't happen without a parent!");
|
|
|
|
mLoadInfo.mResolvedScriptURI = aBaseURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadInfo.mBaseURI = aBaseURI;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_FAILED(aBaseURI->GetSpec(mLocationInfo.mHref))) {
|
|
|
|
mLocationInfo.mHref.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_FAILED(aBaseURI->GetHost(mLocationInfo.mHostname))) {
|
|
|
|
mLocationInfo.mHostname.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_FAILED(aBaseURI->GetPath(mLocationInfo.mPathname))) {
|
|
|
|
mLocationInfo.mPathname.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsCString temp;
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(aBaseURI));
|
|
|
|
if (url && NS_SUCCEEDED(url->GetQuery(temp)) && !temp.IsEmpty()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
mLocationInfo.mSearch.AssignLiteral("?");
|
|
|
|
mLocationInfo.mSearch.Append(temp);
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetRef(temp)) && !temp.IsEmpty()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsCOMPtr<nsITextToSubURI> converter =
|
2011-11-04 20:32:17 +04:00
|
|
|
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID);
|
|
|
|
if (converter) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsCString charset;
|
2011-11-04 20:32:17 +04:00
|
|
|
nsAutoString unicodeRef;
|
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetOriginCharset(charset)) &&
|
|
|
|
NS_SUCCEEDED(converter->UnEscapeURIForUI(charset, temp,
|
|
|
|
unicodeRef))) {
|
|
|
|
mLocationInfo.mHash.AssignLiteral("#");
|
|
|
|
mLocationInfo.mHash.Append(NS_ConvertUTF16toUTF8(unicodeRef));
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (mLocationInfo.mHash.IsEmpty()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
mLocationInfo.mHash.AssignLiteral("#");
|
|
|
|
mLocationInfo.mHash.Append(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetScheme(mLocationInfo.mProtocol))) {
|
|
|
|
mLocationInfo.mProtocol.AppendLiteral(":");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLocationInfo.mProtocol.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port;
|
2011-11-04 20:32:17 +04:00
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetPort(&port)) && port != -1) {
|
2011-07-17 23:09:13 +04:00
|
|
|
mLocationInfo.mPort.AppendInt(port);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString host(mLocationInfo.mHostname);
|
2011-07-17 23:09:13 +04:00
|
|
|
host.AppendLiteral(":");
|
|
|
|
host.Append(mLocationInfo.mPort);
|
|
|
|
|
|
|
|
mLocationInfo.mHost.Assign(host);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLocationInfo.mHost.Assign(mLocationInfo.mHostname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::SetPrincipal(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
mLoadInfo.mPrincipal = aPrincipal;
|
|
|
|
mLoadInfo.mPrincipalIsSystem = nsContentUtils::IsSystemPrincipal(aPrincipal);
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
template <class Derived>
|
|
|
|
JSContext*
|
|
|
|
WorkerPrivateParent<Derived>::ParentJSContext() const
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (mParent) {
|
|
|
|
return mParent->GetJSContext();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
return mLoadInfo.mScriptContext ?
|
|
|
|
mLoadInfo.mScriptContext->GetNativeContext() :
|
|
|
|
nsContentUtils::GetSafeJSContext();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
WorkerPrivate::WorkerPrivate(JSContext* aCx,
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* aParent,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker, bool aIsSharedWorker,
|
|
|
|
const nsAString& aSharedWorkerName,
|
|
|
|
LoadInfo& aLoadInfo)
|
2013-10-14 15:58:05 +04:00
|
|
|
: WorkerPrivateParent<WorkerPrivate>(aCx, aParent, aScriptURL,
|
2013-06-05 18:04:23 +04:00
|
|
|
aIsChromeWorker, aIsSharedWorker,
|
|
|
|
aSharedWorkerName, aLoadInfo),
|
2012-07-30 18:20:58 +04:00
|
|
|
mJSContext(nullptr), mErrorHandlerRecursionCount(0), mNextTimeoutId(1),
|
2011-07-17 23:09:13 +04:00
|
|
|
mStatus(Pending), mSuspended(false), mTimerRunning(false),
|
|
|
|
mRunningExpiredTimeouts(false), mCloseHandlerStarted(false),
|
2013-02-08 15:50:00 +04:00
|
|
|
mCloseHandlerFinished(false), mMemoryReporterRunning(false),
|
2013-06-05 18:04:23 +04:00
|
|
|
mBlockedForMemoryReporter(false)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-14 15:58:05 +04:00
|
|
|
MOZ_ASSERT_IF(aIsSharedWorker, !aSharedWorkerName.IsVoid());
|
|
|
|
MOZ_ASSERT_IF(!aIsSharedWorker, aSharedWorkerName.IsEmpty());
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate::~WorkerPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-03-31 08:42:20 +04:00
|
|
|
already_AddRefed<WorkerPrivate>
|
2013-10-14 15:58:05 +04:00
|
|
|
WorkerPrivate::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
ErrorResult& aRv)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-10-14 15:58:05 +04:00
|
|
|
return WorkerPrivate::Constructor(aGlobal, aScriptURL, false, false,
|
|
|
|
EmptyString(), nullptr, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
WorkerPrivate::WorkerAvailable(JSContext* /* unused */, JSObject* /* unused */)
|
|
|
|
{
|
|
|
|
// If we're already on a worker workers are clearly enabled.
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our caller is chrome, workers are always available.
|
|
|
|
if (nsContentUtils::IsCallerChrome()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else check the pref.
|
|
|
|
return Preferences::GetBool(PREF_WORKERS_ENABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<ChromeWorkerPrivate>
|
|
|
|
ChromeWorkerPrivate::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return WorkerPrivate::Constructor(aGlobal, aScriptURL, true, false,
|
|
|
|
EmptyString(), nullptr, aRv).downcast<ChromeWorkerPrivate>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
|
|
|
ChromeWorkerPrivate::WorkerAvailable(JSContext* /* unused */, JSObject* /* unused */)
|
|
|
|
{
|
|
|
|
// Chrome is always allowed to use workers, and content is never allowed to
|
|
|
|
// use ChromeWorker, so all we have to check is the caller.
|
|
|
|
return nsContentUtils::ThreadsafeIsCallerChrome();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<WorkerPrivate>
|
|
|
|
WorkerPrivate::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker, bool aIsSharedWorker,
|
|
|
|
const nsAString& aSharedWorkerName,
|
|
|
|
LoadInfo* aLoadInfo, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerPrivate* parent = NS_IsMainThread() ?
|
|
|
|
nullptr :
|
|
|
|
GetCurrentThreadWorkerPrivate();
|
|
|
|
if (parent) {
|
|
|
|
parent->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
JSContext* cx = aGlobal.GetContext();
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(aIsSharedWorker, !aSharedWorkerName.IsVoid());
|
|
|
|
MOZ_ASSERT_IF(!aIsSharedWorker, aSharedWorkerName.IsEmpty());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
mozilla::Maybe<LoadInfo> stackLoadInfo;
|
|
|
|
if (!aLoadInfo) {
|
|
|
|
stackLoadInfo.construct();
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
nsresult rv = GetLoadInfo(cx, nullptr, parent, aScriptURL,
|
2013-06-05 18:04:23 +04:00
|
|
|
aIsChromeWorker, stackLoadInfo.addr());
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-14 15:58:05 +04:00
|
|
|
scriptloader::ReportLoadError(cx, aScriptURL, rv, !parent);
|
|
|
|
aRv.Throw(rv);
|
2013-06-05 18:04:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aLoadInfo = stackLoadInfo.addr();
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
// NB: This has to be done before creating the WorkerPrivate, because it will
|
|
|
|
// attempt to use static variables that are initialized in the RuntimeService
|
|
|
|
// constructor.
|
|
|
|
RuntimeService* runtimeService;
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
runtimeService = RuntimeService::GetOrCreateService();
|
|
|
|
if (!runtimeService) {
|
|
|
|
JS_ReportError(cx, "Failed to create runtime service!");
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
runtimeService = RuntimeService::GetService();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runtimeService);
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsRefPtr<WorkerPrivate> worker =
|
2013-10-14 15:58:05 +04:00
|
|
|
new WorkerPrivate(cx, parent, aScriptURL, aIsChromeWorker,
|
2013-06-05 18:04:23 +04:00
|
|
|
aIsSharedWorker, aSharedWorkerName, *aLoadInfo);
|
|
|
|
|
|
|
|
nsRefPtr<CompileScriptRunnable> compiler = new CompileScriptRunnable(worker);
|
2013-10-14 15:58:05 +04:00
|
|
|
if (!compiler->Dispatch(cx)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!runtimeService->RegisterWorker(cx, worker)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2013-06-05 18:04:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
// The worker will be owned by its JSObject (via the reference we return from
|
|
|
|
// this function), but it also needs to be owned by its thread, so AddRef it
|
|
|
|
// again.
|
|
|
|
NS_ADDREF(worker.get());
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
return worker.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
WorkerPrivate::GetLoadInfo(JSContext* aCx, nsPIDOMWindow* aWindow,
|
|
|
|
WorkerPrivate* aParent, const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker, LoadInfo* aLoadInfo)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
using namespace mozilla::dom::workers::scriptloader;
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (aWindow) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
LoadInfo loadInfo;
|
|
|
|
nsresult rv;
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aParent) {
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
|
2013-03-13 00:33:40 +04:00
|
|
|
// If the parent is going away give up now.
|
2013-06-05 18:04:23 +04:00
|
|
|
Status parentStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(aParent->mMutex);
|
2013-06-05 18:04:23 +04:00
|
|
|
parentStatus = aParent->mStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (parentStatus > Running) {
|
|
|
|
NS_WARNING("Cannot create child workers from the close handler!");
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = ChannelFromScriptURLWorkerThread(aCx, aParent, aScriptURL,
|
|
|
|
getter_AddRefs(loadInfo.mChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// Now that we've spun the loop there's no guarantee that our parent is
|
|
|
|
// still alive. We may have received control messages initiating shutdown.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(aParent->mMutex);
|
|
|
|
parentStatus = aParent->mStatus;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
if (parentStatus > Running) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
if (NS_FAILED(NS_GetMainThread(getter_AddRefs(mainThread))) ||
|
|
|
|
NS_FAILED(NS_ProxyRelease(mainThread, loadInfo.mChannel))) {
|
|
|
|
NS_WARNING("Failed to proxy release of channel, leaking instead!");
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadInfo.mDomain = aParent->Domain();
|
|
|
|
} else {
|
2011-07-17 23:09:13 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(ssm);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-10-22 10:29:55 +04:00
|
|
|
bool isChrome = nsContentUtils::IsCallerChrome();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// First check to make sure the caller has permission to make a privileged
|
|
|
|
// worker if they called the ChromeWorker/ChromeSharedWorker constructor.
|
2011-07-17 23:09:13 +04:00
|
|
|
if (aIsChromeWorker && !isChrome) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Chrome callers (whether ChromeWorker of Worker) always get the system
|
|
|
|
// principal here as they're allowed to load anything. The script loader may
|
|
|
|
// change the principal later depending on the script uri.
|
2013-06-05 18:04:23 +04:00
|
|
|
if (isChrome) {
|
|
|
|
rv = ssm->GetSystemPrincipal(getter_AddRefs(loadInfo.mPrincipal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// See if we're being called from a window.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> globalWindow = aWindow;
|
|
|
|
if (!globalWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal =
|
2013-07-30 03:45:27 +04:00
|
|
|
nsJSUtils::GetStaticScriptGlobal(JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
if (scriptGlobal) {
|
2013-06-05 18:04:23 +04:00
|
|
|
globalWindow = do_QueryInterface(scriptGlobal);
|
|
|
|
MOZ_ASSERT(globalWindow);
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
|
|
|
|
if (globalWindow) {
|
2011-07-17 23:09:13 +04:00
|
|
|
// Only use the current inner window, and only use it if the caller can
|
|
|
|
// access it.
|
2013-06-05 18:04:23 +04:00
|
|
|
nsPIDOMWindow* outerWindow = globalWindow->GetOuterWindow();
|
|
|
|
if (outerWindow) {
|
|
|
|
loadInfo.mWindow = outerWindow->GetCurrentInnerWindow();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!loadInfo.mWindow ||
|
|
|
|
(globalWindow != loadInfo.mWindow &&
|
|
|
|
!nsContentUtils::CanCallerAccess(loadInfo.mWindow))) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(loadInfo.mWindow);
|
|
|
|
MOZ_ASSERT(sgo);
|
|
|
|
|
|
|
|
loadInfo.mScriptContext = sgo->GetContext();
|
|
|
|
NS_ENSURE_TRUE(loadInfo.mScriptContext, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// If we're called from a window then we can dig out the principal and URI
|
|
|
|
// from the document.
|
2013-06-05 18:04:23 +04:00
|
|
|
document = loadInfo.mWindow->GetExtantDoc();
|
|
|
|
NS_ENSURE_TRUE(document, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
loadInfo.mBaseURI = document->GetDocBaseURI();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// Use the document's NodePrincipal as our principal if we're not being
|
|
|
|
// called from chrome.
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!loadInfo.mPrincipal) {
|
|
|
|
loadInfo.mPrincipal = document->NodePrincipal();
|
|
|
|
NS_ENSURE_TRUE(loadInfo.mPrincipal, NS_ERROR_FAILURE);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-01-09 01:53:32 +04:00
|
|
|
// We use the document's base domain to limit the number of workers
|
|
|
|
// each domain can create. For sandboxed documents, we use the domain
|
|
|
|
// of their first non-sandboxed document, walking up until we find
|
|
|
|
// one. If we can't find one, we fall back to using the GUID of the
|
|
|
|
// null principal as the base domain.
|
|
|
|
if (document->GetSandboxFlags() & SANDBOXED_ORIGIN) {
|
|
|
|
nsCOMPtr<nsIDocument> tmpDoc = document;
|
|
|
|
do {
|
|
|
|
tmpDoc = tmpDoc->GetParentDocument();
|
|
|
|
} while (tmpDoc && tmpDoc->GetSandboxFlags() & SANDBOXED_ORIGIN);
|
|
|
|
|
|
|
|
if (tmpDoc) {
|
|
|
|
// There was an unsandboxed ancestor, yay!
|
|
|
|
nsCOMPtr<nsIPrincipal> tmpPrincipal = tmpDoc->NodePrincipal();
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = tmpPrincipal->GetBaseDomain(loadInfo.mDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-01-09 01:53:32 +04:00
|
|
|
} else {
|
|
|
|
// No unsandboxed ancestor, use our GUID.
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = loadInfo.mPrincipal->GetBaseDomain(loadInfo.mDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-01-09 01:53:32 +04:00
|
|
|
}
|
2012-08-20 22:34:32 +04:00
|
|
|
} else {
|
2013-01-09 01:53:32 +04:00
|
|
|
// Document creating the worker is not sandboxed.
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = loadInfo.mPrincipal->GetBaseDomain(loadInfo.mDomain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
nsCOMPtr<nsIPermissionManager> permMgr =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
uint32_t perm;
|
|
|
|
rv = permMgr->TestPermissionFromPrincipal(loadInfo.mPrincipal, "systemXHR",
|
|
|
|
&perm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
loadInfo.mXHRParamsAllowed = perm == nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
} else {
|
|
|
|
// Not a window
|
|
|
|
MOZ_ASSERT(isChrome);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// We're being created outside of a window. Need to figure out the script
|
|
|
|
// that is creating us in order for us to use relative URIs later on.
|
2013-09-20 13:22:59 +04:00
|
|
|
JS::RootedScript script(aCx);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (JS_DescribeScriptedCaller(aCx, &script, nullptr)) {
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(loadInfo.mBaseURI),
|
|
|
|
JS_GetScriptFilename(aCx, script));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
loadInfo.mXHRParamsAllowed = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(loadInfo.mPrincipal);
|
|
|
|
MOZ_ASSERT(isChrome || !loadInfo.mDomain.IsEmpty());
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
// XXXbent Use subject principal here instead of the one we already have?
|
|
|
|
nsCOMPtr<nsIPrincipal> subjectPrincipal = ssm->GetCxSubjectPrincipal(aCx);
|
|
|
|
MOZ_ASSERT(subjectPrincipal);
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!nsContentUtils::GetContentSecurityPolicy(aCx,
|
|
|
|
getter_AddRefs(loadInfo.mCSP))) {
|
|
|
|
NS_WARNING("Failed to get CSP!");
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-09-15 22:51:55 +04:00
|
|
|
}
|
2013-03-13 00:33:40 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (loadInfo.mCSP) {
|
|
|
|
rv = loadInfo.mCSP->GetAllowsEval(&loadInfo.mReportCSPViolations,
|
|
|
|
&loadInfo.mEvalAllowed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
loadInfo.mEvalAllowed = true;
|
|
|
|
loadInfo.mReportCSPViolations = false;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = ChannelFromScriptURLMainThread(loadInfo.mPrincipal, loadInfo.mBaseURI,
|
|
|
|
document, aScriptURL,
|
|
|
|
getter_AddRefs(loadInfo.mChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-03-13 00:33:40 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = NS_GetFinalChannelURI(loadInfo.mChannel,
|
|
|
|
getter_AddRefs(loadInfo.mResolvedScriptURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
aLoadInfo->StealFrom(loadInfo);
|
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::DoRunLoop(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mJSContext = aCx;
|
|
|
|
|
|
|
|
NS_ASSERTION(mStatus == Pending, "Huh?!");
|
|
|
|
mStatus = Running;
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
// We need a timer for GC. The basic plan is to run a normal (non-shrinking)
|
|
|
|
// GC periodically (NORMAL_GC_TIMER_DELAY_MS) while the worker is running.
|
|
|
|
// Once the worker goes idle we set a short (IDLE_GC_TIMER_DELAY_MS) timer to
|
|
|
|
// run a shrinking GC. If the worker receives more messages then the short
|
|
|
|
// timer is canceled and the periodic timer resumes.
|
|
|
|
nsCOMPtr<nsITimer> gcTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (!gcTimer) {
|
|
|
|
JS_ReportError(aCx, "Failed to create GC timer!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool normalGCTimerRunning = false;
|
|
|
|
|
|
|
|
// We need to swap event targets below to get different types of GC behavior.
|
|
|
|
nsCOMPtr<nsIEventTarget> normalGCEventTarget;
|
|
|
|
nsCOMPtr<nsIEventTarget> idleGCEventTarget;
|
|
|
|
|
|
|
|
// We also need to track the idle GC event so that we don't confuse it with a
|
|
|
|
// generic event that should re-trigger the idle GC timer.
|
|
|
|
nsCOMPtr<nsIRunnable> idleGCEvent;
|
|
|
|
{
|
|
|
|
nsRefPtr<GarbageCollectRunnable> runnable =
|
|
|
|
new GarbageCollectRunnable(this, false, false);
|
|
|
|
normalGCEventTarget = new WorkerRunnableEventTarget(runnable);
|
|
|
|
|
|
|
|
runnable = new GarbageCollectRunnable(this, true, false);
|
|
|
|
idleGCEventTarget = new WorkerRunnableEventTarget(runnable);
|
|
|
|
|
|
|
|
idleGCEvent = runnable;
|
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
EnableMemoryReporter();
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2013-07-17 22:53:54 +04:00
|
|
|
Maybe<JSAutoCompartment> maybeAC;
|
2011-07-17 23:09:13 +04:00
|
|
|
for (;;) {
|
|
|
|
Status currentStatus;
|
2012-01-18 00:05:25 +04:00
|
|
|
bool scheduleIdleGC;
|
|
|
|
|
2012-02-10 08:54:47 +04:00
|
|
|
WorkerRunnable* event;
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
while (!mControlQueue.Pop(event) && !mQueue.Pop(event)) {
|
2012-12-30 22:21:52 +04:00
|
|
|
WaitForWorkerEvents();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
bool eventIsNotIdleGCEvent;
|
|
|
|
currentStatus = mStatus;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
|
|
|
|
2013-07-17 22:53:54 +04:00
|
|
|
// Workers lazily create a global object in CompileScriptRunnable. In
|
|
|
|
// the old world, creating the global would implicitly set it as the
|
|
|
|
// default compartment object on mJSContext, meaning that subsequent
|
|
|
|
// runnables would be able to operate in that compartment without
|
|
|
|
// explicitly entering it. That no longer works, so we mimic the
|
|
|
|
// "operate in the compartment of the worker global once it exists"
|
|
|
|
// behavior here. This could probably be improved with some refactoring.
|
2013-07-30 03:45:25 +04:00
|
|
|
if (maybeAC.empty() && js::DefaultObjectForContextOrNull(aCx)) {
|
|
|
|
maybeAC.construct(aCx, js::DefaultObjectForContextOrNull(aCx));
|
2013-07-17 22:53:54 +04:00
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
if (!normalGCTimerRunning &&
|
|
|
|
event != idleGCEvent &&
|
|
|
|
currentStatus <= Terminating) {
|
|
|
|
// Must always cancel before changing the timer's target.
|
|
|
|
if (NS_FAILED(gcTimer->Cancel())) {
|
|
|
|
NS_WARNING("Failed to cancel GC timer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(gcTimer->SetTarget(normalGCEventTarget)) &&
|
|
|
|
NS_SUCCEEDED(gcTimer->InitWithFuncCallback(
|
2012-07-30 18:20:58 +04:00
|
|
|
DummyCallback, nullptr,
|
2012-01-18 00:05:25 +04:00
|
|
|
NORMAL_GC_TIMER_DELAY_MS,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK))) {
|
|
|
|
normalGCTimerRunning = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JS_ReportError(aCx, "Failed to start normal GC timer!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of whether or not this is the idle GC event.
|
|
|
|
eventIsNotIdleGCEvent = event != idleGCEvent;
|
|
|
|
|
2012-02-10 08:54:47 +04:00
|
|
|
static_cast<nsIRunnable*>(event)->Run();
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_RELEASE(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
currentStatus = mStatus;
|
2012-01-18 00:05:25 +04:00
|
|
|
scheduleIdleGC = mControlQueue.IsEmpty() &&
|
|
|
|
mQueue.IsEmpty() &&
|
2013-05-17 02:49:43 +04:00
|
|
|
eventIsNotIdleGCEvent &&
|
2013-07-30 03:45:27 +04:00
|
|
|
JS::CurrentGlobalOrNull(aCx);
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Take care of the GC timer. If we're starting the close sequence then we
|
|
|
|
// kill the timer once and for all. Otherwise we schedule the idle timeout
|
|
|
|
// if there are no more events.
|
|
|
|
if (currentStatus > Terminating || scheduleIdleGC) {
|
|
|
|
if (NS_SUCCEEDED(gcTimer->Cancel())) {
|
|
|
|
normalGCTimerRunning = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Failed to cancel GC timer!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scheduleIdleGC) {
|
2013-07-30 03:45:27 +04:00
|
|
|
NS_ASSERTION(JS::CurrentGlobalOrNull(aCx), "Should have global here!");
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
2013-07-30 03:45:27 +04:00
|
|
|
JSAutoCompartment ac(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-05-17 02:49:43 +04:00
|
|
|
JS_MaybeGC(aCx);
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
if (NS_SUCCEEDED(gcTimer->SetTarget(idleGCEventTarget)) &&
|
|
|
|
NS_SUCCEEDED(gcTimer->InitWithFuncCallback(
|
2012-07-30 18:20:58 +04:00
|
|
|
DummyCallback, nullptr,
|
2012-01-18 00:05:25 +04:00
|
|
|
IDLE_GC_TIMER_DELAY_MS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT))) {
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JS_ReportError(aCx, "Failed to start idle GC timer!");
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentStatus != Running && !HasActiveFeatures()) {
|
|
|
|
// If the close handler has finished and all features are done then we can
|
|
|
|
// kill this thread.
|
|
|
|
if (mCloseHandlerFinished && currentStatus != Killing) {
|
|
|
|
if (!NotifyInternal(aCx, Killing)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(currentStatus == Killing, "Should have changed status!");
|
|
|
|
#else
|
|
|
|
currentStatus = Killing;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're supposed to die then we should exit the loop.
|
|
|
|
if (currentStatus == Killing) {
|
2012-01-18 00:05:25 +04:00
|
|
|
// Always make sure the timer is canceled.
|
|
|
|
if (NS_FAILED(gcTimer->Cancel())) {
|
|
|
|
NS_WARNING("Failed to cancel the GC timer!");
|
|
|
|
}
|
|
|
|
|
2011-09-09 04:03:03 +04:00
|
|
|
DisableMemoryReporter();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
StopAcceptingEvents();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Shouldn't get here!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::OperationCallback(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
bool mayContinue = true;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Run all control events now.
|
2011-09-09 04:03:03 +04:00
|
|
|
mayContinue = ProcessAllControlRunnables();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
bool maySuspend = mSuspended;
|
|
|
|
if (maySuspend) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
maySuspend = mStatus <= Running;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mayContinue || !maySuspend) {
|
2011-07-17 23:09:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up before suspending.
|
2012-03-28 14:13:30 +04:00
|
|
|
JS_GC(JS_GetRuntime(aCx));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
while ((mayContinue = MayContinueRunning())) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (!mControlQueue.IsEmpty()) {
|
|
|
|
break;
|
|
|
|
}
|
2011-08-02 08:06:17 +04:00
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
WaitForWorkerEvents(PR_MillisecondsToInterval(RemainingRunTimeMS()));
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mayContinue) {
|
|
|
|
// We want only uncatchable exceptions here.
|
|
|
|
NS_ASSERTION(!JS_IsExceptionPending(aCx),
|
|
|
|
"Should not have an exception set here!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::ScheduleDeletion(bool aWasPending)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(mChildWorkers.IsEmpty(), "Live child workers!");
|
|
|
|
NS_ASSERTION(mSyncQueues.IsEmpty(), "Should have no sync queues here!");
|
|
|
|
|
|
|
|
StopAcceptingEvents();
|
|
|
|
|
|
|
|
nsIThread* currentThread;
|
|
|
|
if (aWasPending) {
|
|
|
|
// Don't want to close down this thread since we never got to run!
|
2012-07-30 18:20:58 +04:00
|
|
|
currentThread = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
currentThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(currentThread, "This should never be null!");
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* parent = GetParent();
|
|
|
|
if (parent) {
|
|
|
|
nsRefPtr<WorkerFinishedRunnable> runnable =
|
|
|
|
new WorkerFinishedRunnable(parent, this, currentThread);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!runnable->Dispatch(nullptr)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to dispatch runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsRefPtr<TopLevelWorkerFinishedRunnable> runnable =
|
|
|
|
new TopLevelWorkerFinishedRunnable(this, currentThread);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-07 05:03:46 +04:00
|
|
|
bool
|
2013-04-12 07:52:32 +04:00
|
|
|
WorkerPrivate::BlockAndCollectRuntimeStats(JS::RuntimeStats* aRtStats)
|
2011-08-07 05:03:46 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-02-08 15:50:00 +04:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2013-04-12 07:52:32 +04:00
|
|
|
NS_ASSERTION(aRtStats, "Null RuntimeStats!");
|
2011-08-07 05:03:46 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
NS_ASSERTION(!mMemoryReporterRunning, "How can we get reentered here?!");
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// This signals the worker that it should block itself as soon as possible.
|
|
|
|
mMemoryReporterRunning = true;
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
NS_ASSERTION(mJSContext, "This must never be null!");
|
|
|
|
JSRuntime* rt = JS_GetRuntime(mJSContext);
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// If the worker is not already blocked (e.g. waiting for a worker event or
|
|
|
|
// currently in a ctypes call) then we need to trigger the operation
|
|
|
|
// callback to trap the worker.
|
|
|
|
if (!mBlockedForMemoryReporter) {
|
|
|
|
JS_TriggerOperationCallback(rt);
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// Wait until the worker actually blocks.
|
|
|
|
while (!mBlockedForMemoryReporter) {
|
|
|
|
mMemoryReportCondVar.Wait();
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
2011-09-09 04:03:03 +04:00
|
|
|
}
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
bool succeeded = false;
|
2011-08-07 05:03:46 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// If mMemoryReporter is still set then we can do the actual report. Otherwise
|
|
|
|
// we're trying to shut down and we don't want to do anything but clean up.
|
|
|
|
if (mMemoryReporter) {
|
|
|
|
// Don't hold the lock while doing the actual report.
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2013-04-12 07:52:32 +04:00
|
|
|
succeeded = JS::CollectRuntimeStats(rt, aRtStats, nullptr);
|
2013-02-08 15:50:00 +04:00
|
|
|
}
|
2012-12-30 22:21:52 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(mMemoryReporterRunning, "This isn't possible!");
|
|
|
|
NS_ASSERTION(mBlockedForMemoryReporter, "Somehow we got unblocked!");
|
|
|
|
|
|
|
|
// Tell the worker that it can now continue its execution.
|
|
|
|
mMemoryReporterRunning = false;
|
|
|
|
|
|
|
|
// The worker may be waiting so we must notify.
|
|
|
|
mMemoryReportCondVar.Notify();
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2011-09-09 06:04:43 +04:00
|
|
|
return succeeded;
|
2011-09-09 04:03:03 +04:00
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::EnableMemoryReporter()
|
2011-09-09 04:03:03 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
// No need to lock here since the main thread can't race until we've
|
|
|
|
// successfully registered the reporter.
|
|
|
|
mMemoryReporter = new MemoryReporter(this);
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2013-08-28 03:24:51 +04:00
|
|
|
if (NS_FAILED(NS_RegisterMemoryReporter(mMemoryReporter))) {
|
2012-12-30 22:21:52 +04:00
|
|
|
NS_WARNING("Failed to register memory reporter!");
|
|
|
|
// No need to lock here since a failed registration means our memory
|
|
|
|
// reporter can't start running. Just clean up.
|
|
|
|
mMemoryReporter = nullptr;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::DisableMemoryReporter()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
nsRefPtr<MemoryReporter> memoryReporter;
|
2011-09-09 04:03:03 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// There is nothing to do here if the memory reporter was never successfully
|
|
|
|
// registered.
|
|
|
|
if (!mMemoryReporter) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-30 06:39:38 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// We don't need this set any longer. Swap it out so that we can unregister
|
|
|
|
// below.
|
|
|
|
mMemoryReporter.swap(memoryReporter);
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// Next disable the memory reporter so that the main thread stops trying to
|
|
|
|
// signal us.
|
|
|
|
memoryReporter->Disable();
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// If the memory reporter is waiting to start then we need to wait for it to
|
|
|
|
// finish.
|
|
|
|
if (mMemoryReporterRunning) {
|
|
|
|
NS_ASSERTION(!mBlockedForMemoryReporter,
|
|
|
|
"Can't be blocked in more than one place at the same time!");
|
|
|
|
mBlockedForMemoryReporter = true;
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// Tell the main thread that we're blocked.
|
|
|
|
mMemoryReportCondVar.Notify();
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
// Wait for it the main thread to finish. Since we swapped out
|
|
|
|
// mMemoryReporter above the main thread should respond quickly.
|
|
|
|
while (mMemoryReporterRunning) {
|
|
|
|
mMemoryReportCondVar.Wait();
|
|
|
|
}
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
NS_ASSERTION(mBlockedForMemoryReporter, "Somehow we got unblocked!");
|
|
|
|
mBlockedForMemoryReporter = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally unregister the memory reporter.
|
2013-08-28 03:24:51 +04:00
|
|
|
if (NS_FAILED(NS_UnregisterMemoryReporter(memoryReporter))) {
|
2013-02-08 15:50:00 +04:00
|
|
|
NS_WARNING("Failed to unregister memory reporter!");
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::WaitForWorkerEvents(PRIntervalTime aInterval)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
NS_ASSERTION(!mBlockedForMemoryReporter,
|
|
|
|
"Can't be blocked in more than one place at the same time!");
|
|
|
|
|
|
|
|
// Let the main thread know that the worker is blocked and that memory
|
|
|
|
// reporting may proceed.
|
|
|
|
mBlockedForMemoryReporter = true;
|
|
|
|
|
|
|
|
// The main thread may be waiting so we must notify.
|
|
|
|
mMemoryReportCondVar.Notify();
|
|
|
|
|
|
|
|
// Now wait for an actual worker event.
|
|
|
|
mCondVar.Wait(aInterval);
|
|
|
|
|
|
|
|
// We've gotten some kind of signal but we can't continue until the memory
|
|
|
|
// reporter has finished. Wait again.
|
|
|
|
while (mMemoryReporterRunning) {
|
|
|
|
mMemoryReportCondVar.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mBlockedForMemoryReporter, "Somehow we got unblocked!");
|
|
|
|
|
|
|
|
// No need to notify here as the main thread isn't watching for this state.
|
|
|
|
mBlockedForMemoryReporter = false;
|
2011-09-09 04:03:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::ProcessAllControlRunnables()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
for (;;) {
|
2012-02-10 08:54:47 +04:00
|
|
|
WorkerRunnable* event;
|
2011-09-09 04:03:03 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2012-12-30 22:21:52 +04:00
|
|
|
|
|
|
|
// Block here if the memory reporter is trying to run.
|
|
|
|
if (mMemoryReporterRunning) {
|
|
|
|
NS_ASSERTION(!mBlockedForMemoryReporter,
|
|
|
|
"Can't be blocked in more than one place at the same "
|
|
|
|
"time!");
|
|
|
|
|
|
|
|
// Let the main thread know that we've received the block request and
|
|
|
|
// that memory reporting may proceed.
|
|
|
|
mBlockedForMemoryReporter = true;
|
|
|
|
|
|
|
|
// The main thread is almost certainly waiting so we must notify here.
|
|
|
|
mMemoryReportCondVar.Notify();
|
|
|
|
|
|
|
|
// Wait for the memory report to finish.
|
|
|
|
while (mMemoryReporterRunning) {
|
|
|
|
mMemoryReportCondVar.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mBlockedForMemoryReporter, "Somehow we got unblocked!");
|
|
|
|
|
|
|
|
// No need to notify here as the main thread isn't watching for this
|
|
|
|
// state.
|
|
|
|
mBlockedForMemoryReporter = false;
|
|
|
|
}
|
|
|
|
|
2011-09-09 04:03:03 +04:00
|
|
|
if (!mControlQueue.Pop(event)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 08:54:47 +04:00
|
|
|
if (NS_FAILED(static_cast<nsIRunnable*>(event)->Run())) {
|
2011-09-09 04:03:03 +04:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(event);
|
|
|
|
}
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2011-09-09 04:03:03 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivate::Dispatch(WorkerRunnable* aEvent, EventQueue* aQueue)
|
|
|
|
{
|
|
|
|
nsRefPtr<WorkerRunnable> event(aEvent);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mStatus == Dead) {
|
|
|
|
// Nothing may be added after we've set Dead.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aQueue == &mQueue) {
|
|
|
|
// Check parent status.
|
|
|
|
Status parentStatus = ParentStatus();
|
|
|
|
if (parentStatus >= Terminating) {
|
|
|
|
// Throw.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check inner status too.
|
|
|
|
if (parentStatus >= Closing || mStatus >= Closing) {
|
|
|
|
// Silently eat this one.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aQueue->Push(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aQueue == &mControlQueue && mJSContext) {
|
2012-02-24 15:03:28 +04:00
|
|
|
JS_TriggerOperationCallback(JS_GetRuntime(mJSContext));
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
event.forget();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::DispatchToSyncQueue(WorkerSyncRunnable* aEvent)
|
|
|
|
{
|
|
|
|
nsRefPtr<WorkerRunnable> event(aEvent);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
NS_ASSERTION(mSyncQueues.Length() > aEvent->mSyncQueueKey, "Bad event!");
|
|
|
|
|
|
|
|
if (!mSyncQueues[aEvent->mSyncQueueKey]->mQueue.Push(event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
event.forget();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::ClearQueue(EventQueue* aQueue)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
2012-02-10 08:54:47 +04:00
|
|
|
WorkerRunnable* event;
|
2011-07-17 23:09:13 +04:00
|
|
|
while (aQueue->Pop(event)) {
|
2012-02-10 08:54:47 +04:00
|
|
|
if (event->WantsToRunDuringClear()) {
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
|
|
|
|
|
|
|
static_cast<nsIRunnable*>(event)->Run();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
event->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate::RemainingRunTimeMS() const
|
|
|
|
{
|
|
|
|
if (mKillTime.IsNull()) {
|
2012-09-28 10:57:33 +04:00
|
|
|
return UINT32_MAX;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
TimeDuration runtime = mKillTime - TimeStamp::Now();
|
|
|
|
double ms = runtime > TimeDuration(0) ? runtime.ToMilliseconds() : 0;
|
2012-09-28 10:57:33 +04:00
|
|
|
return ms > double(UINT32_MAX) ? UINT32_MAX : uint32_t(ms);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::SuspendInternal(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(!mSuspended, "Already suspended!");
|
|
|
|
|
|
|
|
mSuspended = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::ResumeInternal(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(mSuspended, "Not yet suspended!");
|
|
|
|
|
|
|
|
mSuspended = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::TraceInternal(JSTracer* aTrc)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mTimeouts.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
TimeoutInfo* info = mTimeouts[index];
|
2013-07-23 13:58:27 +04:00
|
|
|
JS_CallHeapValueTracer(aTrc, &info->mTimeoutVal,
|
|
|
|
"WorkerPrivate timeout value");
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index2 = 0; index2 < info->mExtraArgVals.Length(); index2++) {
|
2013-07-23 13:58:27 +04:00
|
|
|
JS_CallHeapValueTracer(aTrc, &info->mExtraArgVals[index2],
|
|
|
|
"WorkerPrivate timeout extra argument value");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
// MessagePort objects are basically held alive as long as the global.
|
|
|
|
mWorkerPorts.EnumerateRead(TraceMessagePorts, aTrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
PLDHashOperator
|
|
|
|
WorkerPrivate::TraceMessagePorts(const uint64_t& aKey,
|
|
|
|
WorkerMessagePort* aData,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
JSTracer* trc = static_cast<JSTracer*>(aUserArg);
|
|
|
|
aData->TraceJSObject(trc, "mWorkerPorts");
|
|
|
|
return PL_DHASH_NEXT;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::ModifyBusyCountFromWorker(JSContext* aCx, bool aIncrease)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
// If we're in shutdown then the busy count is no longer being considered so
|
|
|
|
// just return now.
|
|
|
|
if (mStatus >= Killing) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ModifyBusyCountRunnable> runnable =
|
|
|
|
new ModifyBusyCountRunnable(this, aIncrease);
|
|
|
|
return runnable->Dispatch(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::AddChildWorker(JSContext* aCx, ParentType* aChildWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-03-13 00:33:40 +04:00
|
|
|
#ifdef DEBUG
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-03-13 00:33:40 +04:00
|
|
|
Status currentStatus;
|
|
|
|
{
|
2011-07-17 23:09:13 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
2013-03-13 00:33:40 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-03-13 00:33:40 +04:00
|
|
|
MOZ_ASSERT(currentStatus == Running);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-03-13 00:33:40 +04:00
|
|
|
#endif
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!mChildWorkers.Contains(aChildWorker),
|
|
|
|
"Already know about this one!");
|
|
|
|
mChildWorkers.AppendElement(aChildWorker);
|
|
|
|
|
|
|
|
return mChildWorkers.Length() == 1 ?
|
|
|
|
ModifyBusyCountFromWorker(aCx, true) :
|
|
|
|
true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::RemoveChildWorker(JSContext* aCx, ParentType* aChildWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(mChildWorkers.Contains(aChildWorker),
|
|
|
|
"Didn't know about this one!");
|
|
|
|
mChildWorkers.RemoveElement(aChildWorker);
|
|
|
|
|
|
|
|
if (mChildWorkers.IsEmpty() && !ModifyBusyCountFromWorker(aCx, false)) {
|
|
|
|
NS_WARNING("Failed to modify busy count!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::AddFeature(JSContext* aCx, WorkerFeature* aFeature)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mStatus >= Canceling) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mFeatures.Contains(aFeature), "Already know about this one!");
|
|
|
|
mFeatures.AppendElement(aFeature);
|
|
|
|
|
|
|
|
return mFeatures.Length() == 1 ?
|
|
|
|
ModifyBusyCountFromWorker(aCx, true) :
|
|
|
|
true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::RemoveFeature(JSContext* aCx, WorkerFeature* aFeature)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(mFeatures.Contains(aFeature), "Didn't know about this one!");
|
|
|
|
mFeatures.RemoveElement(aFeature);
|
|
|
|
|
|
|
|
if (mFeatures.IsEmpty() && !ModifyBusyCountFromWorker(aCx, false)) {
|
|
|
|
NS_WARNING("Failed to modify busy count!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::NotifyFeatures(JSContext* aCx, Status aStatus)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(aStatus > Running, "Bad status!");
|
|
|
|
|
|
|
|
if (aStatus >= Closing) {
|
|
|
|
CancelAllTimeouts(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<WorkerFeature*, 30> features;
|
|
|
|
features.AppendElements(mFeatures);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < features.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!features[index]->Notify(aCx, aStatus)) {
|
|
|
|
NS_WARNING("Failed to notify feature!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<ParentType*, 10> children;
|
|
|
|
children.AppendElements(mChildWorkers);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < children.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!children[index]->Notify(aCx, aStatus)) {
|
|
|
|
NS_WARNING("Failed to notify child worker!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::CancelAllTimeouts(JSContext* aCx)
|
|
|
|
{
|
2012-03-27 08:05:09 +04:00
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (mTimerRunning) {
|
|
|
|
NS_ASSERTION(mTimer, "Huh?!");
|
|
|
|
NS_ASSERTION(!mTimeouts.IsEmpty(), "Huh?!");
|
|
|
|
|
|
|
|
if (NS_FAILED(mTimer->Cancel())) {
|
|
|
|
NS_WARNING("Failed to cancel timer!");
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mTimeouts.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
mTimeouts[index]->mCanceled = true;
|
|
|
|
}
|
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
if (!RunExpiredTimeouts(aCx)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
mTimerRunning = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-03-27 08:05:09 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
else if (!mRunningExpiredTimeouts) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(mTimeouts.IsEmpty(), "Huh?!");
|
|
|
|
}
|
2012-03-27 08:05:09 +04:00
|
|
|
#endif
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate::CreateNewSyncLoop()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
NS_ASSERTION(mSyncQueues.Length() < UINT32_MAX,
|
2011-07-17 23:09:13 +04:00
|
|
|
"Should have bailed by now!");
|
|
|
|
|
|
|
|
mSyncQueues.AppendElement(new SyncQueue());
|
|
|
|
return mSyncQueues.Length() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
WorkerPrivate::RunSyncLoop(JSContext* aCx, uint32_t aSyncLoopKey)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(!mSyncQueues.IsEmpty() ||
|
|
|
|
(aSyncLoopKey != mSyncQueues.Length() - 1),
|
|
|
|
"Forgot to call CreateNewSyncLoop!");
|
|
|
|
if (aSyncLoopKey != mSyncQueues.Length() - 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncQueue* syncQueue = mSyncQueues[aSyncLoopKey].get();
|
|
|
|
|
|
|
|
for (;;) {
|
2012-02-10 08:54:47 +04:00
|
|
|
WorkerRunnable* event;
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
while (!mControlQueue.Pop(event) && !syncQueue->mQueue.Pop(event)) {
|
2012-12-30 22:21:52 +04:00
|
|
|
WaitForWorkerEvents();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 08:54:47 +04:00
|
|
|
static_cast<nsIRunnable*>(event)->Run();
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_RELEASE(event);
|
|
|
|
|
|
|
|
if (syncQueue->mComplete) {
|
|
|
|
NS_ASSERTION(mSyncQueues.Length() - 1 == aSyncLoopKey,
|
|
|
|
"Mismatched calls!");
|
|
|
|
NS_ASSERTION(syncQueue->mQueue.IsEmpty(), "Unprocessed sync events!");
|
|
|
|
|
|
|
|
bool result = syncQueue->mResult;
|
2012-12-22 00:14:47 +04:00
|
|
|
DestroySyncLoop(aSyncLoopKey);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-07-30 18:20:58 +04:00
|
|
|
syncQueue = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Shouldn't get here!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
WorkerPrivate::StopSyncLoop(uint32_t aSyncLoopKey, bool aSyncResult)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(mSyncQueues.IsEmpty() ||
|
|
|
|
(aSyncLoopKey == mSyncQueues.Length() - 1),
|
|
|
|
"Forgot to call CreateNewSyncLoop!");
|
|
|
|
if (aSyncLoopKey != mSyncQueues.Length() - 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncQueue* syncQueue = mSyncQueues[aSyncLoopKey].get();
|
|
|
|
|
|
|
|
NS_ASSERTION(!syncQueue->mComplete, "Already called StopSyncLoop?!");
|
|
|
|
|
|
|
|
syncQueue->mResult = aSyncResult;
|
|
|
|
syncQueue->mComplete = true;
|
|
|
|
}
|
|
|
|
|
2012-12-22 00:14:47 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::DestroySyncLoop(uint32_t aSyncLoopKey)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
mSyncQueues.RemoveElementAt(aSyncLoopKey);
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivate::PostMessageToParentInternal(JSContext* aCx,
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Handle<JS::Value> aMessage,
|
2013-06-05 18:04:23 +04:00
|
|
|
JS::Handle<JS::Value> aTransferable,
|
|
|
|
bool aToMessagePort,
|
|
|
|
uint64_t aMessagePortSerial)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
JSStructuredCloneCallbacks* callbacks =
|
|
|
|
IsChromeWorker() ?
|
|
|
|
&gChromeWorkerStructuredCloneCallbacks :
|
|
|
|
&gWorkerStructuredCloneCallbacks;
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
2012-10-04 02:19:22 +04:00
|
|
|
if (!buffer.write(aCx, aMessage, aTransferable, callbacks, &clonedObjects)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MessageEventRunnable> runnable =
|
2011-08-16 07:40:38 +04:00
|
|
|
new MessageEventRunnable(this, WorkerRunnable::ParentThread, buffer,
|
2013-06-05 18:04:23 +04:00
|
|
|
clonedObjects, aToMessagePort, aMessagePortSerial);
|
2011-07-17 23:09:13 +04:00
|
|
|
return runnable->Dispatch(aCx);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::PostMessageToParentMessagePort(
|
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
JS::HandleValue aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mWorkerPorts.Get(aMessagePortSerial)) {
|
|
|
|
// This port has been closed from the main thread. There's no point in
|
|
|
|
// sending this message so just bail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
|
|
|
JSObject* array =
|
|
|
|
JS_NewArrayObject(aCx, realTransferable.Length(),
|
|
|
|
const_cast<jsval*>(realTransferable.Elements()));
|
|
|
|
if (!array) {
|
|
|
|
aRv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PostMessageToParentInternal(aCx, aMessage, transferable, true,
|
|
|
|
aMessagePortSerial)) {
|
|
|
|
aRv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivate::NotifyInternal(JSContext* aCx, Status aStatus)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(aStatus > Running && aStatus < Dead, "Bad status!");
|
|
|
|
|
|
|
|
// Save the old status and set the new status.
|
|
|
|
Status previousStatus;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mStatus >= aStatus) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousStatus = mStatus;
|
|
|
|
mStatus = aStatus;
|
|
|
|
}
|
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
// Now that status > Running, no-one can create a new mCrossThreadDispatcher
|
|
|
|
// if we don't already have one.
|
|
|
|
if (mCrossThreadDispatcher) {
|
|
|
|
// Since we'll no longer process events, make sure we no longer allow
|
|
|
|
// anyone to post them.
|
|
|
|
// We have to do this without mMutex held, since our mutex must be
|
|
|
|
// acquired *after* mCrossThreadDispatcher's mutex when they're both held.
|
|
|
|
mCrossThreadDispatcher->Forget();
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(previousStatus != Pending, "How is this possible?!");
|
|
|
|
|
|
|
|
NS_ASSERTION(previousStatus >= Canceling || mKillTime.IsNull(),
|
|
|
|
"Bad kill time set!");
|
|
|
|
|
|
|
|
// Let all our features know the new status.
|
|
|
|
NotifyFeatures(aCx, aStatus);
|
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
// If this is the first time our status has changed then we need to clear the
|
|
|
|
// main event queue.
|
|
|
|
if (previousStatus == Running) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
ClearQueue(&mQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've run the close handler, we don't need to do anything else.
|
|
|
|
if (mCloseHandlerFinished) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
// If the worker script never ran, or failed to compile, we don't need to do
|
|
|
|
// anything else, except pretend that we ran the close handler.
|
2013-07-30 03:45:27 +04:00
|
|
|
if (!JS::CurrentGlobalOrNull(aCx)) {
|
2012-10-03 06:03:28 +04:00
|
|
|
mCloseHandlerStarted = true;
|
|
|
|
mCloseHandlerFinished = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the first time our status has changed we also need to schedule
|
|
|
|
// the close handler unless we're being shut down.
|
|
|
|
if (previousStatus == Running && aStatus != Killing) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(!mCloseHandlerStarted && !mCloseHandlerFinished,
|
|
|
|
"This is impossible!");
|
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
nsRefPtr<CloseEventRunnable> closeRunnable = new CloseEventRunnable(this);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
if (!mQueue.Push(closeRunnable)) {
|
|
|
|
NS_WARNING("Failed to push closeRunnable!");
|
|
|
|
return false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-10-03 06:03:28 +04:00
|
|
|
|
|
|
|
closeRunnable.forget();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aStatus == Closing) {
|
|
|
|
// Notify parent to stop sending us messages and balance our busy count.
|
|
|
|
nsRefPtr<CloseRunnable> runnable = new CloseRunnable(this);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't abort the script.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStatus == Terminating) {
|
|
|
|
// Only abort the script if we're not yet running the close handler.
|
|
|
|
return mCloseHandlerStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStatus == Canceling) {
|
|
|
|
// We need to enforce a timeout on the close handler.
|
|
|
|
NS_ASSERTION(previousStatus == Running || previousStatus == Closing ||
|
|
|
|
previousStatus == Terminating,
|
|
|
|
"Bad previous status!");
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
uint32_t killSeconds = IsChromeWorker() ?
|
|
|
|
RuntimeService::GetChromeCloseHandlerTimeoutSeconds() :
|
|
|
|
RuntimeService::GetContentCloseHandlerTimeoutSeconds();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (killSeconds) {
|
|
|
|
mKillTime = TimeStamp::Now() + TimeDuration::FromSeconds(killSeconds);
|
|
|
|
|
|
|
|
if (!mCloseHandlerFinished && !ScheduleKillCloseEventRunnable(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only abort the script if we're not yet running the close handler.
|
|
|
|
return mCloseHandlerStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStatus == Killing) {
|
|
|
|
mKillTime = TimeStamp::Now();
|
|
|
|
|
|
|
|
if (!mCloseHandlerFinished && !ScheduleKillCloseEventRunnable(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always abort the script.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Should never get here!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::ScheduleKillCloseEventRunnable(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(!mKillTime.IsNull(), "Must have a kill time!");
|
|
|
|
|
|
|
|
nsRefPtr<KillCloseEventRunnable> killCloseEventRunnable =
|
|
|
|
new KillCloseEventRunnable(this);
|
|
|
|
if (!killCloseEventRunnable->SetTimeout(aCx, RemainingRunTimeMS())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mQueue.Push(killCloseEventRunnable)) {
|
|
|
|
NS_WARNING("Failed to push killCloseEventRunnable!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
killCloseEventRunnable.forget();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::ReportError(JSContext* aCx, const char* aMessage,
|
|
|
|
JSErrorReport* aReport)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!MayContinueRunning() || mErrorHandlerRecursionCount == 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mErrorHandlerRecursionCount == 0 ||
|
|
|
|
mErrorHandlerRecursionCount == 1,
|
|
|
|
"Bad recursion logic!");
|
|
|
|
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
|
|
|
|
nsString message, filename, line;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t lineNumber, columnNumber, flags, errorNumber;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
if (aReport) {
|
|
|
|
if (aReport->ucmessage) {
|
|
|
|
message = aReport->ucmessage;
|
|
|
|
}
|
|
|
|
filename = NS_ConvertUTF8toUTF16(aReport->filename);
|
|
|
|
line = aReport->uclinebuf;
|
|
|
|
lineNumber = aReport->lineno;
|
|
|
|
columnNumber = aReport->uctokenptr - aReport->uclinebuf;
|
|
|
|
flags = aReport->flags;
|
|
|
|
errorNumber = aReport->errorNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lineNumber = columnNumber = errorNumber = 0;
|
|
|
|
flags = nsIScriptError::errorFlag | nsIScriptError::exceptionFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.IsEmpty()) {
|
|
|
|
message = NS_ConvertUTF8toUTF16(aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
mErrorHandlerRecursionCount++;
|
|
|
|
|
|
|
|
// Don't want to run the scope's error handler if this is a recursive error or
|
2012-01-04 23:11:32 +04:00
|
|
|
// if there was an error in the close handler or if we ran out of memory.
|
2011-07-17 23:09:13 +04:00
|
|
|
bool fireAtScope = mErrorHandlerRecursionCount == 1 &&
|
2012-01-04 23:11:32 +04:00
|
|
|
!mCloseHandlerStarted &&
|
|
|
|
errorNumber != JSMSG_OUT_OF_MEMORY;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!ReportErrorRunnable::ReportError(aCx, this, fireAtScope, nullptr, message,
|
2011-07-17 23:09:13 +04:00
|
|
|
filename, line, lineNumber,
|
|
|
|
columnNumber, flags, errorNumber, 0)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
mErrorHandlerRecursionCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-02-29 03:11:11 +04:00
|
|
|
WorkerPrivate::SetTimeout(JSContext* aCx, unsigned aArgc, jsval* aVp,
|
2011-07-17 23:09:13 +04:00
|
|
|
bool aIsInterval)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(aArgc, "Huh?!");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t timerId = mNextTimeoutId++;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
Status currentStatus;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
// It's a script bug if setTimeout/setInterval are called from a close handler
|
|
|
|
// so throw an exception.
|
|
|
|
if (currentStatus == Closing) {
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ReportError(aCx, "Cannot schedule timeouts from the close handler!");
|
2012-03-27 08:05:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the worker is trying to call setTimeout/setInterval and the parent
|
|
|
|
// thread has initiated the close process then just silently fail.
|
|
|
|
if (currentStatus >= Closing) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<TimeoutInfo> newInfo(new TimeoutInfo());
|
|
|
|
newInfo->mIsInterval = aIsInterval;
|
|
|
|
newInfo->mId = timerId;
|
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(timerId == UINT32_MAX)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Timeout ids overflowed!");
|
|
|
|
mNextTimeoutId = 1;
|
|
|
|
}
|
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
JS::Value* argv = JS_ARGV(aCx, aVp);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// Take care of the main argument.
|
2012-05-11 19:46:26 +04:00
|
|
|
if (argv[0].isObject()) {
|
|
|
|
if (JS_ObjectIsCallable(aCx, &argv[0].toObject())) {
|
2011-07-17 23:09:13 +04:00
|
|
|
newInfo->mTimeoutVal = argv[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSString* timeoutStr = JS_ValueToString(aCx, argv[0]);
|
|
|
|
if (!timeoutStr) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-11 19:46:26 +04:00
|
|
|
newInfo->mTimeoutVal.setString(timeoutStr);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-11 19:46:26 +04:00
|
|
|
else if (argv[0].isString()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
newInfo->mTimeoutVal = argv[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JS_ReportError(aCx, "Useless %s call (missing quotes around argument?)",
|
|
|
|
aIsInterval ? "setInterval" : "setTimeout");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if any of the optional arguments were passed.
|
|
|
|
if (aArgc > 1) {
|
2012-02-25 02:19:52 +04:00
|
|
|
double intervalMS = 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!JS_ValueToNumber(aCx, argv[1], &intervalMS)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
newInfo->mInterval = TimeDuration::FromMilliseconds(intervalMS);
|
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (aArgc > 2 && newInfo->mTimeoutVal.isObject()) {
|
2013-07-23 13:58:27 +04:00
|
|
|
nsTArray<JS::Heap<JS::Value> > extraArgVals(aArgc - 2);
|
2012-02-29 03:11:11 +04:00
|
|
|
for (unsigned index = 2; index < aArgc; index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
extraArgVals.AppendElement(argv[index]);
|
|
|
|
}
|
|
|
|
newInfo->mExtraArgVals.SwapElements(extraArgVals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newInfo->mTargetTime = TimeStamp::Now() + newInfo->mInterval;
|
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (newInfo->mTimeoutVal.isString()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
const char* filenameChars;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t lineNumber;
|
2011-07-17 23:09:13 +04:00
|
|
|
if (nsJSUtils::GetCallingLocation(aCx, &filenameChars, &lineNumber)) {
|
|
|
|
newInfo->mFilename = filenameChars;
|
|
|
|
newInfo->mLineNumber = lineNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("Failed to get calling location!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimeouts.InsertElementSorted(newInfo.get(), GetAutoPtrComparator(mTimeouts));
|
|
|
|
|
|
|
|
// If the timeout we just made is set to fire next then we need to update the
|
|
|
|
// timer.
|
|
|
|
if (mTimeouts[0] == newInfo) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mTimer) {
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_ReportError(aCx, "Failed to create timer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<TimerRunnable> timerRunnable = new TimerRunnable(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
|
|
|
new WorkerRunnableEventTarget(timerRunnable);
|
|
|
|
rv = mTimer->SetTarget(target);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_ReportError(aCx, "Failed to set timer's target!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTimerRunning) {
|
|
|
|
if (!ModifyBusyCountFromWorker(aCx, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mTimerRunning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RescheduleTimeoutTimer(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, INT_TO_JSVAL(timerId));
|
|
|
|
|
|
|
|
newInfo.forget();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-10-12 03:38:04 +04:00
|
|
|
WorkerPrivate::ClearTimeout(JSContext* aCx, uint32_t aId)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mTimeouts.IsEmpty()) {
|
|
|
|
NS_ASSERTION(mTimerRunning, "Huh?!");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mTimeouts.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsAutoPtr<TimeoutInfo>& info = mTimeouts[index];
|
|
|
|
if (info->mId == aId) {
|
|
|
|
info->mCanceled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::RunExpiredTimeouts(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// We may be called recursively (e.g. close() inside a timeout) or we could
|
|
|
|
// have been canceled while this event was pending, bail out if there is
|
|
|
|
// nothing to do.
|
|
|
|
if (mRunningExpiredTimeouts || !mTimerRunning) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
NS_ASSERTION(mTimer, "Must have a timer!");
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_ASSERTION(!mTimeouts.IsEmpty(), "Should have some work to do!");
|
|
|
|
|
|
|
|
bool retval = true;
|
|
|
|
|
2011-08-26 11:34:10 +04:00
|
|
|
AutoPtrComparator<TimeoutInfo> comparator = GetAutoPtrComparator(mTimeouts);
|
2013-07-30 03:45:27 +04:00
|
|
|
JS::RootedObject global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-08-26 11:34:10 +04:00
|
|
|
JSPrincipals* principal = GetWorkerPrincipal();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// We want to make sure to run *something*, even if the timer fired a little
|
|
|
|
// early. Fudge the value of now to at least include the first timeout.
|
2013-01-15 16:22:03 +04:00
|
|
|
const TimeStamp now = std::max(TimeStamp::Now(), mTimeouts[0]->mTargetTime);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsAutoTArray<TimeoutInfo*, 10> expiredTimeouts;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mTimeouts.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsAutoPtr<TimeoutInfo>& info = mTimeouts[index];
|
2011-08-26 11:34:10 +04:00
|
|
|
if (info->mTargetTime > now) {
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
expiredTimeouts.AppendElement(info);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Guard against recursion.
|
|
|
|
mRunningExpiredTimeouts = true;
|
|
|
|
|
|
|
|
// Run expired timeouts.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < expiredTimeouts.Length(); index++) {
|
2011-07-17 23:09:13 +04:00
|
|
|
TimeoutInfo*& info = expiredTimeouts[index];
|
|
|
|
|
|
|
|
if (info->mCanceled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-08-26 11:34:10 +04:00
|
|
|
// Always call JS_ReportPendingException if something fails, and if
|
|
|
|
// JS_ReportPendingException returns false (i.e. uncatchable exception) then
|
|
|
|
// break out of the loop.
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
if (info->mTimeoutVal.isString()) {
|
|
|
|
JSString* expression = info->mTimeoutVal.toString();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-27 16:04:37 +04:00
|
|
|
JS::CompileOptions options(aCx);
|
|
|
|
options.setPrincipals(principal)
|
|
|
|
.setFileAndLine(info->mFilename.get(), info->mLineNumber);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
size_t stringLength;
|
|
|
|
const jschar* string = JS_GetStringCharsAndLength(aCx, expression,
|
|
|
|
&stringLength);
|
2012-08-27 16:04:37 +04:00
|
|
|
if ((!string || !JS::Evaluate(aCx, global, options, string, stringLength, nullptr)) &&
|
2011-08-26 11:34:10 +04:00
|
|
|
!JS_ReportPendingException(aCx)) {
|
2012-08-27 16:21:27 +04:00
|
|
|
retval = false;
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Rooted<JS::Value> rval(aCx);
|
2013-07-23 13:58:27 +04:00
|
|
|
/*
|
|
|
|
* unsafeGet() is needed below because the argument is a not a const
|
|
|
|
* pointer, even though values are not modified.
|
|
|
|
*/
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!JS_CallFunctionValue(aCx, global, info->mTimeoutVal,
|
|
|
|
info->mExtraArgVals.Length(),
|
2013-07-23 13:58:27 +04:00
|
|
|
info->mExtraArgVals.Elements()->unsafeGet(),
|
|
|
|
rval.address()) &&
|
2011-08-26 11:34:10 +04:00
|
|
|
!JS_ReportPendingException(aCx)) {
|
|
|
|
retval = false;
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
NS_ASSERTION(mRunningExpiredTimeouts, "Someone changed this!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// No longer possible to be called recursively.
|
|
|
|
mRunningExpiredTimeouts = false;
|
|
|
|
|
2011-08-26 11:34:10 +04:00
|
|
|
// Now remove canceled and expired timeouts from the main list.
|
2012-08-10 20:10:22 +04:00
|
|
|
// NB: The timeouts present in expiredTimeouts must have the same order
|
|
|
|
// with respect to each other in mTimeouts. That is, mTimeouts is just
|
|
|
|
// expiredTimeouts with extra elements inserted. There may be unexpired
|
|
|
|
// timeouts that have been inserted between the expired timeouts if the
|
|
|
|
// timeout event handler called setTimeout/setInterval.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0, expiredTimeoutIndex = 0,
|
2012-08-10 20:10:22 +04:00
|
|
|
expiredTimeoutLength = expiredTimeouts.Length();
|
|
|
|
index < mTimeouts.Length(); ) {
|
2011-08-26 11:34:10 +04:00
|
|
|
nsAutoPtr<TimeoutInfo>& info = mTimeouts[index];
|
2012-08-10 20:10:22 +04:00
|
|
|
if ((expiredTimeoutIndex < expiredTimeoutLength &&
|
|
|
|
info == expiredTimeouts[expiredTimeoutIndex] &&
|
|
|
|
++expiredTimeoutIndex) ||
|
|
|
|
info->mCanceled) {
|
|
|
|
if (info->mIsInterval && !info->mCanceled) {
|
|
|
|
// Reschedule intervals.
|
|
|
|
info->mTargetTime = info->mTargetTime + info->mInterval;
|
|
|
|
// Don't resort the list here, we'll do that at the end.
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mTimeouts.RemoveElement(info);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
else {
|
2012-08-10 20:10:22 +04:00
|
|
|
// If info did not match the current entry in expiredTimeouts, it
|
|
|
|
// shouldn't be there at all.
|
|
|
|
NS_ASSERTION(!expiredTimeouts.Contains(info),
|
|
|
|
"Our timeouts are out of order!");
|
|
|
|
++index;
|
2011-08-26 11:34:10 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-08-10 20:10:22 +04:00
|
|
|
mTimeouts.Sort(comparator);
|
|
|
|
|
2012-03-27 08:05:09 +04:00
|
|
|
// Either signal the parent that we're no longer using timeouts or reschedule
|
|
|
|
// the timer.
|
2011-07-17 23:09:13 +04:00
|
|
|
if (mTimeouts.IsEmpty()) {
|
|
|
|
if (!ModifyBusyCountFromWorker(aCx, false)) {
|
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
mTimerRunning = false;
|
|
|
|
}
|
|
|
|
else if (retval && !RescheduleTimeoutTimer(aCx)) {
|
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::RescheduleTimeoutTimer(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
NS_ASSERTION(!mTimeouts.IsEmpty(), "Should have some timeouts!");
|
|
|
|
NS_ASSERTION(mTimer, "Should have a timer!");
|
|
|
|
|
|
|
|
double delta =
|
|
|
|
(mTimeouts[0]->mTargetTime - TimeStamp::Now()).ToMilliseconds();
|
2013-01-15 16:22:03 +04:00
|
|
|
uint32_t delay = delta > 0 ? std::min(delta, double(UINT32_MAX)) : 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult rv = mTimer->InitWithFuncCallback(DummyCallback, nullptr, delay,
|
2011-07-17 23:09:13 +04:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_ReportError(aCx, "Failed to start timer!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-05-17 02:49:43 +04:00
|
|
|
WorkerPrivate::UpdateJSContextOptionsInternal(JSContext* aCx,
|
|
|
|
uint32_t aContentOptions,
|
|
|
|
uint32_t aChromeOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
JS_SetOptions(aCx, IsChromeWorker() ? aChromeOptions : aContentOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
2013-05-17 02:49:43 +04:00
|
|
|
mChildWorkers[index]->UpdateJSContextOptions(aCx, aContentOptions,
|
|
|
|
aChromeOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:11:32 +04:00
|
|
|
void
|
2013-01-11 02:50:40 +04:00
|
|
|
WorkerPrivate::UpdateJSWorkerMemoryParameterInternal(JSContext* aCx,
|
|
|
|
JSGCParamKey aKey,
|
|
|
|
uint32_t aValue)
|
2012-01-04 23:11:32 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
2013-05-17 02:49:43 +04:00
|
|
|
|
|
|
|
// XXX aValue might be 0 here (telling us to unset a previous value for child
|
|
|
|
// workers). Calling JS_SetGCParameter with a value of 0 isn't actually
|
|
|
|
// supported though. We really need some way to revert to a default value
|
|
|
|
// here.
|
|
|
|
if (aValue) {
|
|
|
|
JS_SetGCParameter(JS_GetRuntime(aCx), aKey, aValue);
|
|
|
|
}
|
2012-01-04 23:11:32 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
2013-01-11 02:50:40 +04:00
|
|
|
mChildWorkers[index]->UpdateJSWorkerMemoryParameter(aCx, aKey, aValue);
|
2012-01-04 23:11:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
void
|
2013-05-17 02:49:43 +04:00
|
|
|
WorkerPrivate::UpdateGCZealInternal(JSContext* aCx, uint8_t aGCZeal,
|
|
|
|
uint32_t aFrequency)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
JS_SetGCZeal(aCx, aGCZeal, aFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
2013-05-17 02:49:43 +04:00
|
|
|
mChildWorkers[index]->UpdateGCZeal(aCx, aGCZeal, aFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::UpdateJITHardeningInternal(JSContext* aCx, bool aJITHardening)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
JS_SetJitHardening(JS_GetRuntime(aCx), aJITHardening);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
|
|
|
mChildWorkers[index]->UpdateJITHardening(aCx, aJITHardening);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::GarbageCollectInternal(JSContext* aCx, bool aShrinking,
|
|
|
|
bool aCollectChildren)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-07-25 23:03:27 +04:00
|
|
|
if (aShrinking || aCollectChildren) {
|
2013-05-17 02:49:43 +04:00
|
|
|
JSRuntime* rt = JS_GetRuntime(aCx);
|
|
|
|
JS::PrepareForFullGC(rt);
|
2013-07-25 23:03:27 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
if (aShrinking) {
|
|
|
|
JS::ShrinkingGC(rt, JS::gcreason::DOM_WORKER);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JS::GCForReason(rt, JS::gcreason::DOM_WORKER);
|
|
|
|
}
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-05-17 02:49:43 +04:00
|
|
|
JS_MaybeGC(aCx);
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCollectChildren) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
2012-01-18 00:05:25 +04:00
|
|
|
mChildWorkers[index]->GarbageCollect(aCx, aShrinking);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-23 01:48:00 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::RegisterHostObjectURI(const nsACString& aURI)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mHostObjectURIs.AppendElement(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UnregisterHostObjectURI(const nsACString& aURI)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mHostObjectURIs.RemoveElement(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::StealHostObjectURIs(nsTArray<nsCString>& aArray)
|
|
|
|
{
|
|
|
|
aArray.SwapElements(mHostObjectURIs);
|
|
|
|
}
|
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
WorkerCrossThreadDispatcher*
|
|
|
|
WorkerPrivate::GetCrossThreadDispatcher()
|
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
if (!mCrossThreadDispatcher && mStatus <= Running) {
|
|
|
|
mCrossThreadDispatcher = new WorkerCrossThreadDispatcher(this);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
return mCrossThreadDispatcher;
|
|
|
|
}
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::BeginCTypesCall()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
NS_ASSERTION(!mBlockedForMemoryReporter,
|
|
|
|
"Can't be blocked in more than one place at the same time!");
|
|
|
|
|
|
|
|
// Let the main thread know that the worker is effectively blocked while in
|
|
|
|
// this ctypes call. It isn't technically true (obviously the call could do
|
|
|
|
// non-blocking things), but we're assuming that ctypes can't call back into
|
|
|
|
// JSAPI here and therefore any work the ctypes call does will not alter the
|
|
|
|
// data structures of this JS runtime.
|
|
|
|
mBlockedForMemoryReporter = true;
|
|
|
|
|
|
|
|
// The main thread may be waiting on us so it must be notified.
|
|
|
|
mMemoryReportCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::EndCTypesCall()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
NS_ASSERTION(mBlockedForMemoryReporter, "Somehow we got unblocked!");
|
|
|
|
|
|
|
|
// Don't continue until the memory reporter has finished.
|
|
|
|
while (mMemoryReporterRunning) {
|
|
|
|
mMemoryReportCondVar.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to notify the main thread here as it shouldn't be waiting to see
|
|
|
|
// this state.
|
|
|
|
mBlockedForMemoryReporter = false;
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivate::ConnectMessagePort(JSContext* aCx, uint64_t aMessagePortSerial)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(!mWorkerPorts.Get(aMessagePortSerial),
|
|
|
|
"Already have this port registered!");
|
|
|
|
|
|
|
|
nsRefPtr<WorkerMessagePort> port =
|
|
|
|
new WorkerMessagePort(aCx, aMessagePortSerial);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> portObj(aCx, Wrap(aCx, global, port));
|
|
|
|
if (!portObj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> event(aCx, CreateConnectEvent(aCx, portObj));
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWorkerPorts.Put(aMessagePortSerial, port);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
if (!DispatchEventToTarget(aCx, global, event, &dummy)) {
|
|
|
|
mWorkerPorts.Remove(aMessagePortSerial);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::DisconnectMessagePort(uint64_t aMessagePortSerial)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// The port may have already been removed from this list since either the main
|
|
|
|
// thread or the worker thread can remove it.
|
|
|
|
mWorkerPorts.Remove(aMessagePortSerial);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerMessagePort*
|
|
|
|
WorkerPrivate::GetMessagePort(uint64_t aMessagePortSerial)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
WorkerMessagePort* port;
|
|
|
|
if (mWorkerPorts.Get(aMessagePortSerial, &port)) {
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::AssertIsOnParentThread() const
|
|
|
|
{
|
|
|
|
if (GetParent()) {
|
|
|
|
GetParent()->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::AssertInnerWindowIsCorrect() const
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
// Only care about top level workers from windows.
|
|
|
|
if (mParent || !mLoadInfo.mWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsPIDOMWindow* outer = mLoadInfo.mWindow->GetOuterWindow();
|
|
|
|
NS_ASSERTION(outer && outer->GetCurrentInnerWindow() == mLoadInfo.mWindow,
|
|
|
|
"Inner window no longer correct!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::AssertIsOnWorkerThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mThread,
|
|
|
|
"Trying to assert thread identity after thread has been "
|
|
|
|
"shutdown!");
|
|
|
|
|
|
|
|
bool current;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mThread->IsOnCurrentThread(¤t)));
|
|
|
|
MOZ_ASSERT(current, "Wrong thread!");
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2011-08-05 02:01:45 +04:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2013-10-14 15:58:05 +04:00
|
|
|
WorkerCrossThreadDispatcher*
|
|
|
|
GetWorkerCrossThreadDispatcher(JSContext* aCx, JS::Value aWorker)
|
|
|
|
{
|
|
|
|
if (!aWorker.isObject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* w = nullptr;
|
|
|
|
UNWRAP_OBJECT(Worker, aCx, &aWorker.toObject(), w);
|
|
|
|
MOZ_ASSERT(w);
|
|
|
|
return w->GetCrossThreadDispatcher();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't use NS_IMPL_CYCLE_COLLECTION_CLASS(WorkerPrivateParent) because of the
|
|
|
|
// templates.
|
|
|
|
template <>
|
|
|
|
WorkerPrivateParent<WorkerPrivate>::cycleCollection WorkerPrivateParent<WorkerPrivate>::_cycleCollectorGlobal = WorkerPrivateParent<WorkerPrivate>::cycleCollection();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Force instantiation.
|
|
|
|
template class WorkerPrivateParent<WorkerPrivate>;
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks*
|
2011-08-16 07:40:38 +04:00
|
|
|
WorkerStructuredCloneCallbacks(bool aMainRuntime)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-08-16 07:40:38 +04:00
|
|
|
return aMainRuntime ?
|
|
|
|
&gMainThreadWorkerStructuredCloneCallbacks :
|
|
|
|
&gWorkerStructuredCloneCallbacks;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks*
|
2011-08-16 07:40:38 +04:00
|
|
|
ChromeWorkerStructuredCloneCallbacks(bool aMainRuntime)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-08-16 07:40:38 +04:00
|
|
|
return aMainRuntime ?
|
|
|
|
&gMainThreadChromeWorkerStructuredCloneCallbacks :
|
|
|
|
&gChromeWorkerStructuredCloneCallbacks;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|