2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-10-23 17:16:49 +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/. */
|
|
|
|
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
|
2014-08-08 19:06:33 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2014-08-08 19:06:33 +04:00
|
|
|
#include "nsIGlobalObject.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "nsIRunnable.h"
|
2014-02-17 10:54:36 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-11-24 08:04:20 +03:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-08-08 19:06:33 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
#include "js/RootingAPI.h"
|
|
|
|
#include "js/Value.h"
|
|
|
|
|
|
|
|
#include "WorkerPrivate.h"
|
2014-08-08 19:06:33 +04:00
|
|
|
#include "WorkerScope.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const nsIID kWorkerRunnableIID = {
|
|
|
|
0x320cc0b5, 0xef12, 0x4084, { 0x88, 0x6e, 0xca, 0x6a, 0x81, 0xe4, 0x1d, 0x68 }
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
WorkerRunnable::WorkerRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
TargetAndBusyBehavior aBehavior)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mBehavior(aBehavior), mCanceled(0),
|
|
|
|
mCallingCancelWithinRun(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
bool
|
|
|
|
WorkerRunnable::IsDebuggerRunnable() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject*
|
|
|
|
WorkerRunnable::DefaultGlobalObject() const
|
|
|
|
{
|
|
|
|
if (IsDebuggerRunnable()) {
|
|
|
|
return mWorkerPrivate->DebuggerGlobalScope();
|
|
|
|
} else {
|
|
|
|
return mWorkerPrivate->GlobalScope();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
bool
|
2016-02-26 00:05:39 +03:00
|
|
|
WorkerRunnable::PreDispatch(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
|
|
switch (mBehavior) {
|
|
|
|
case ParentThreadUnchangedBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WorkerThreadModifyBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WorkerThreadUnchangedBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown behavior!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount) {
|
2016-02-26 00:05:39 +03:00
|
|
|
return aWorkerPrivate->ModifyBusyCount(true);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRunnable::Dispatch(JSContext* aCx)
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
if (!aCx) {
|
2016-02-26 00:05:39 +03:00
|
|
|
ok = PreDispatch(mWorkerPrivate);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (ok) {
|
|
|
|
ok = DispatchInternal();
|
|
|
|
}
|
|
|
|
PostDispatch(nullptr, mWorkerPrivate, ok);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
if (global) {
|
2014-08-14 02:39:41 +04:00
|
|
|
ac.emplace(aCx, global);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-26 00:05:39 +03:00
|
|
|
ok = PreDispatch(mWorkerPrivate);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (ok && !DispatchInternal()) {
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PostDispatch(aCx, mWorkerPrivate, ok);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRunnable::DispatchInternal()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerRunnable> runnable(this);
|
2015-07-10 06:21:46 +03:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount ||
|
|
|
|
mBehavior == WorkerThreadUnchangedBusyCount) {
|
2015-03-04 17:11:32 +03:00
|
|
|
if (IsDebuggerRunnable()) {
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mWorkerPrivate->DispatchDebuggerRunnable(runnable.forget()));
|
2015-03-04 17:11:32 +03:00
|
|
|
} else {
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mWorkerPrivate->Dispatch(runnable.forget()));
|
2015-03-04 17:11:32 +03:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mBehavior == ParentThreadUnchangedBusyCount);
|
|
|
|
|
|
|
|
if (WorkerPrivate* parent = mWorkerPrivate->GetParent()) {
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(parent->Dispatch(runnable.forget()));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
MOZ_ASSERT(mainThread);
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerRunnable::PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
switch (mBehavior) {
|
|
|
|
case ParentThreadUnchangedBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WorkerThreadModifyBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WorkerThreadUnchangedBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown behavior!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!aDispatchResult) {
|
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount) {
|
2016-02-26 00:05:39 +03:00
|
|
|
aWorkerPrivate->ModifyBusyCount(false);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
if (aCx) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aRunResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
switch (mBehavior) {
|
|
|
|
case ParentThreadUnchangedBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WorkerThreadModifyBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WorkerThreadUnchangedBusyCount:
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown behavior!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount) {
|
2016-02-19 02:02:51 +03:00
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aRunResult) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
WorkerRunnable*
|
|
|
|
WorkerRunnable::FromRunnable(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
|
|
|
|
WorkerRunnable* runnable;
|
|
|
|
nsresult rv = aRunnable->QueryInterface(kWorkerRunnableIID,
|
|
|
|
reinterpret_cast<void**>(&runnable));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
return runnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(WorkerRunnable)
|
|
|
|
NS_IMPL_RELEASE(WorkerRunnable)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(WorkerRunnable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICancelableRunnable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
// kWorkerRunnableIID is special in that it does not AddRef its result.
|
|
|
|
if (aIID.Equals(kWorkerRunnableIID)) {
|
|
|
|
*aInstancePtr = this;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerRunnable::Run()
|
|
|
|
{
|
|
|
|
bool targetIsWorkerThread = mBehavior == WorkerThreadModifyBusyCount ||
|
|
|
|
mBehavior == WorkerThreadUnchangedBusyCount;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT_IF(mCallingCancelWithinRun, targetIsWorkerThread);
|
|
|
|
if (targetIsWorkerThread) {
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(mBehavior == ParentThreadUnchangedBusyCount);
|
|
|
|
mWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (IsCanceled() && !mCallingCancelWithinRun) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
if (targetIsWorkerThread &&
|
|
|
|
mWorkerPrivate->AllPendingRunnablesShouldBeCanceled() &&
|
|
|
|
!IsCanceled() && !mCallingCancelWithinRun) {
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
// Prevent recursion.
|
|
|
|
mCallingCancelWithinRun = true;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
Cancel();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
MOZ_ASSERT(mCallingCancelWithinRun);
|
|
|
|
mCallingCancelWithinRun = false;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
MOZ_ASSERT(IsCanceled(), "Subclass Cancel() didn't set IsCanceled()!");
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
return NS_OK;
|
2015-04-09 04:23:48 +03:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-08-12 23:29:50 +04:00
|
|
|
// Track down the appropriate global to use for the AutoJSAPI/AutoEntryScript.
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject;
|
|
|
|
bool isMainThread = !targetIsWorkerThread && !mWorkerPrivate->GetParent();
|
|
|
|
MOZ_ASSERT(isMainThread == NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerPrivate> kungFuDeathGrip;
|
2014-08-12 23:29:50 +04:00
|
|
|
if (targetIsWorkerThread) {
|
2015-04-22 10:03:52 +03:00
|
|
|
JSContext* cx = GetCurrentThreadJSContext();
|
|
|
|
if (NS_WARN_IF(!cx)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* global = JS::CurrentGlobalOrNull(cx);
|
2015-03-04 17:11:32 +03:00
|
|
|
if (global) {
|
|
|
|
globalObject = GetGlobalObjectForGlobal(global);
|
|
|
|
} else {
|
|
|
|
globalObject = DefaultGlobalObject();
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-23 17:16:49 +04:00
|
|
|
kungFuDeathGrip = mWorkerPrivate;
|
2014-08-08 19:06:33 +04:00
|
|
|
if (isMainThread) {
|
2016-01-30 20:05:36 +03:00
|
|
|
globalObject = nsGlobalWindow::Cast(mWorkerPrivate->GetWindow());
|
2014-08-08 19:06:33 +04:00
|
|
|
} else {
|
|
|
|
globalObject = mWorkerPrivate->GetParent()->GlobalScope();
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 19:06:33 +04:00
|
|
|
// We might run script as part of WorkerRun, so we need an AutoEntryScript.
|
|
|
|
// This is part of the HTML spec for workers at:
|
|
|
|
// http://www.whatwg.org/specs/web-apps/current-work/#run-a-worker
|
|
|
|
// If we don't have a globalObject we have to use an AutoJSAPI instead, but
|
|
|
|
// this is OK as we won't be running script in these circumstances.
|
|
|
|
// It's important that aes is declared after jsapi, because if WorkerRun
|
|
|
|
// creates a global then we construct aes before PostRun and we need them to
|
|
|
|
// be destroyed in the correct order.
|
|
|
|
mozilla::dom::AutoJSAPI jsapi;
|
|
|
|
Maybe<mozilla::dom::AutoEntryScript> aes;
|
|
|
|
JSContext* cx;
|
|
|
|
if (globalObject) {
|
2015-04-09 04:23:48 +03:00
|
|
|
aes.emplace(globalObject, "Worker runnable",
|
|
|
|
isMainThread,
|
|
|
|
isMainThread ? nullptr : GetCurrentThreadJSContext());
|
2014-08-14 02:39:41 +04:00
|
|
|
cx = aes->cx();
|
2013-10-23 17:16:49 +04:00
|
|
|
} else {
|
2014-08-08 19:06:33 +04:00
|
|
|
jsapi.Init();
|
|
|
|
cx = jsapi.cx();
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2016-02-19 02:02:51 +03:00
|
|
|
// Note that we can't assert anything about mWorkerPrivate->GetWrapper()
|
|
|
|
// existing, since it may in fact have been GCed (and we may be one of the
|
|
|
|
// runnables cleaning up the worker as a result).
|
|
|
|
|
|
|
|
// If we are on the parent thread and that thread is not the main thread,
|
|
|
|
// then we must be a dedicated worker (because there are no
|
|
|
|
// Shared/ServiceWorkers whose parent is itself a worker) and then we
|
|
|
|
// definitely have a globalObject. If it _is_ the main thread, globalObject
|
|
|
|
// can be null for workers started from JSMs or other non-window contexts,
|
|
|
|
// sadly.
|
|
|
|
MOZ_ASSERT_IF(!targetIsWorkerThread && !isMainThread,
|
|
|
|
mWorkerPrivate->IsDedicatedWorker() && globalObject);
|
|
|
|
|
|
|
|
// If we're on the parent thread we might be in a null compartment in the
|
|
|
|
// situation described above when globalObject is null. Make sure to enter
|
|
|
|
// the compartment of the worker's reflector if there is one. There might
|
|
|
|
// not be one if we're just starting to compile the script for this worker.
|
2013-10-23 17:16:49 +04:00
|
|
|
Maybe<JSAutoCompartment> ac;
|
2014-08-08 19:06:33 +04:00
|
|
|
if (!targetIsWorkerThread && mWorkerPrivate->GetWrapper()) {
|
2016-02-19 02:02:51 +03:00
|
|
|
// If we're on the parent thread and have a reflector and a globalObject,
|
|
|
|
// then the compartments of cx, globalObject, and the worker's reflector
|
|
|
|
// should all match.
|
|
|
|
MOZ_ASSERT_IF(globalObject,
|
|
|
|
js::GetObjectCompartment(mWorkerPrivate->GetWrapper()) ==
|
|
|
|
js::GetContextCompartment(cx));
|
|
|
|
MOZ_ASSERT_IF(globalObject,
|
|
|
|
js::GetObjectCompartment(mWorkerPrivate->GetWrapper()) ==
|
|
|
|
js::GetObjectCompartment(globalObject->GetGlobalJSObject()));
|
|
|
|
|
|
|
|
// If we're on the parent thread and have a reflector, then our
|
|
|
|
// JSContext had better be either in the null compartment (and hence
|
|
|
|
// have no globalObject) or in the compartment of our reflector.
|
|
|
|
MOZ_ASSERT(!js::GetContextCompartment(cx) ||
|
|
|
|
js::GetObjectCompartment(mWorkerPrivate->GetWrapper()) ==
|
|
|
|
js::GetContextCompartment(cx),
|
|
|
|
"Must either be in the null compartment or in our reflector "
|
|
|
|
"compartment");
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
ac.emplace(cx, mWorkerPrivate->GetWrapper());
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = WorkerRun(cx, mWorkerPrivate);
|
|
|
|
|
|
|
|
// In the case of CompileScriptRunnnable, WorkerRun above can cause us to
|
2014-08-08 19:06:33 +04:00
|
|
|
// lazily create a global, so we construct aes here before calling PostRun.
|
2015-03-04 17:11:32 +03:00
|
|
|
if (targetIsWorkerThread && !aes && DefaultGlobalObject()) {
|
2015-04-09 04:23:48 +03:00
|
|
|
aes.emplace(DefaultGlobalObject(), "worker runnable",
|
|
|
|
false, GetCurrentThreadJSContext());
|
2014-08-14 02:39:41 +04:00
|
|
|
cx = aes->cx();
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PostRun(cx, mWorkerPrivate, result);
|
|
|
|
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerRunnable::Cancel()
|
|
|
|
{
|
|
|
|
uint32_t canceledCount = ++mCanceled;
|
|
|
|
|
|
|
|
MOZ_ASSERT(canceledCount, "Cancel() overflow!");
|
|
|
|
|
|
|
|
// The docs say that Cancel() should not be called more than once and that we
|
|
|
|
// should throw NS_ERROR_UNEXPECTED if it is.
|
|
|
|
return (canceledCount == 1) ? NS_OK : NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerRunnable::PostDispatch(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
2016-02-23 23:02:43 +03:00
|
|
|
// The only way aDispatchResult can be false here is if either PreDispatch or
|
|
|
|
// DispatchInternal returned false.
|
|
|
|
//
|
2016-02-26 00:05:39 +03:00
|
|
|
// PreDispatch can never throw on a JSContext. We inherit DispatchInternal
|
|
|
|
// from WorkerRunnable and don't allow overriding it in our subclasses.
|
|
|
|
// WorkerRunnable::DispatchInternal only fails if one of its runnable
|
|
|
|
// dispatching functions fails, and none of those cases can throw a JS
|
|
|
|
// exception. So we can never have a JS exception here.
|
2016-02-23 23:02:43 +03:00
|
|
|
MOZ_ASSERT_IF(aCx, !JS_IsExceptionPending(aCx));
|
2015-03-04 17:11:32 +03:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerSyncRunnable::WorkerSyncRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
nsIEventTarget* aSyncLoopTarget)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mSyncLoopTarget(aSyncLoopTarget)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mSyncLoopTarget) {
|
|
|
|
mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerSyncRunnable::WorkerSyncRunnable(
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2014-03-15 23:00:17 +04:00
|
|
|
already_AddRefed<nsIEventTarget>&& aSyncLoopTarget)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mSyncLoopTarget(aSyncLoopTarget)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mSyncLoopTarget) {
|
|
|
|
mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerSyncRunnable::~WorkerSyncRunnable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerSyncRunnable::DispatchInternal()
|
|
|
|
{
|
|
|
|
if (mSyncLoopTarget) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerSyncRunnable> runnable(this);
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mSyncLoopTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return WorkerRunnable::DispatchInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainThreadWorkerSyncRunnable::PostDispatch(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
2016-02-23 23:02:42 +03:00
|
|
|
// The only way aDispatchResult can be false here is if either PreDispatch or
|
|
|
|
// DispatchInternal returned false.
|
|
|
|
//
|
2016-02-26 00:05:39 +03:00
|
|
|
// PreDispatch can never throw on a JSContext. We inherit DispatchInternal
|
|
|
|
// from WorkerSyncRunnable and don't allow overriding it in our subclasses.
|
|
|
|
// WorkerSyncRunnable::DispatchInternal only returns false if if dispatch to
|
|
|
|
// the syncloop target fails or if calling up to
|
2016-02-23 23:02:42 +03:00
|
|
|
// WorkerRunnable::DispatchInternal fails. WorkerRunnable::DispatchInternal
|
|
|
|
// only fails if one of its runnable dispatching functions fails, and none of
|
|
|
|
// those cases can throw a JS exception. So we can never have a JS exception
|
|
|
|
// here.
|
|
|
|
MOZ_ASSERT_IF(aCx, !JS_IsExceptionPending(aCx));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
StopSyncLoopRunnable::StopSyncLoopRunnable(
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2014-03-15 23:00:17 +04:00
|
|
|
already_AddRefed<nsIEventTarget>&& aSyncLoopTarget,
|
2013-10-23 17:16:49 +04:00
|
|
|
bool aResult)
|
2014-03-15 23:00:17 +04:00
|
|
|
: WorkerSyncRunnable(aWorkerPrivate, Move(aSyncLoopTarget)), mResult(aResult)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StopSyncLoopRunnable::Cancel()
|
|
|
|
{
|
|
|
|
nsresult rv = Run();
|
2014-05-01 00:44:03 +04:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Run() failed");
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-05-01 00:44:03 +04:00
|
|
|
nsresult rv2 = WorkerSyncRunnable::Cancel();
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv2), "Cancel() failed");
|
|
|
|
|
|
|
|
return NS_FAILED(rv) ? rv : rv2;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StopSyncLoopRunnable::WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mSyncLoopTarget);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget;
|
|
|
|
mSyncLoopTarget.swap(syncLoopTarget);
|
|
|
|
|
|
|
|
if (!mResult) {
|
|
|
|
MaybeSetException(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->StopSyncLoop(syncLoopTarget, mResult);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
StopSyncLoopRunnable::DispatchInternal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSyncLoopTarget);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StopSyncLoopRunnable> runnable(this);
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mSyncLoopTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainThreadStopSyncLoopRunnable::PostDispatch(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
2016-02-23 23:02:41 +03:00
|
|
|
// The only way aDispatchResult can be false here is if either PreDispatch or
|
|
|
|
// DispatchInternal returned false.
|
|
|
|
//
|
2016-02-26 00:05:39 +03:00
|
|
|
// PreDispatch can never throw on a JSContext. We inherit DispatchInternal
|
|
|
|
// from StopSyncLoopRunnable, and that itself is final and only returns false
|
|
|
|
// if dispatch to the syncloop target fails. So we can never have a JS
|
|
|
|
// exception here.
|
2016-02-23 23:02:41 +03:00
|
|
|
MOZ_ASSERT_IF(aCx, !JS_IsExceptionPending(aCx));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
WorkerControlRunnable::WorkerControlRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
TargetAndBusyBehavior aBehavior)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, aBehavior)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aBehavior == ParentThreadUnchangedBusyCount ||
|
|
|
|
aBehavior == WorkerThreadUnchangedBusyCount,
|
|
|
|
"WorkerControlRunnables should not modify the busy count");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-22 10:03:52 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerControlRunnable::Cancel()
|
|
|
|
{
|
|
|
|
if (NS_FAILED(Run())) {
|
|
|
|
NS_WARNING("WorkerControlRunnable::Run() failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return WorkerRunnable::Cancel();
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
bool
|
|
|
|
WorkerControlRunnable::DispatchInternal()
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerControlRunnable> runnable(this);
|
2015-07-10 06:21:46 +03:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mBehavior == WorkerThreadUnchangedBusyCount) {
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mWorkerPrivate->DispatchControlRunnable(runnable.forget()));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (WorkerPrivate* parent = mWorkerPrivate->GetParent()) {
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(parent->DispatchControlRunnable(runnable.forget()));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
|
|
|
|
MOZ_ASSERT(mainThread);
|
|
|
|
|
2015-07-10 06:21:46 +03:00
|
|
|
return NS_SUCCEEDED(mainThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MainThreadWorkerControlRunnable::PostDispatch(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (aCx && !aDispatchResult) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable)
|
2014-02-17 10:54:36 +04:00
|
|
|
|
2014-02-24 17:57:03 +04:00
|
|
|
WorkerMainThreadRunnable::WorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2015-11-24 08:04:20 +03:00
|
|
|
void
|
|
|
|
WorkerMainThreadRunnable::Dispatch(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
AutoSyncLoopHolder syncLoop(mWorkerPrivate);
|
|
|
|
|
|
|
|
mSyncLoopTarget = syncLoop.EventTarget();
|
|
|
|
RefPtr<WorkerMainThreadRunnable> runnable(this);
|
|
|
|
|
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
NS_DispatchToMainThread(runnable.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv),
|
|
|
|
"Should only fail after xpcom-shutdown-threads and we're gone by then");
|
|
|
|
|
|
|
|
if (!syncLoop.Run()) {
|
|
|
|
aRv.ThrowUncatchableException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:57:03 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerMainThreadRunnable::Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
bool runResult = MainThreadRun();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MainThreadStopSyncLoopRunnable> response =
|
2014-02-24 17:57:03 +04:00
|
|
|
new MainThreadStopSyncLoopRunnable(mWorkerPrivate,
|
|
|
|
mSyncLoopTarget.forget(),
|
|
|
|
runResult);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(response->Dispatch(nullptr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-24 08:04:20 +03:00
|
|
|
bool
|
|
|
|
WorkerCheckAPIExposureOnMainThreadRunnable::Dispatch()
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
WorkerMainThreadRunnable::Dispatch(rv);
|
|
|
|
bool ok = !rv.Failed();
|
|
|
|
rv.SuppressException();
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2014-02-17 10:54:36 +04:00
|
|
|
bool
|
2016-02-26 00:05:39 +03:00
|
|
|
WorkerSameThreadRunnable::PreDispatch(WorkerPrivate* aWorkerPrivate)
|
2014-02-17 10:54:36 +04:00
|
|
|
{
|
2016-02-19 02:02:51 +03:00
|
|
|
// We don't call WorkerRunnable::PreDispatch, because we're using
|
|
|
|
// WorkerThreadModifyBusyCount for mBehavior, and WorkerRunnable will assert
|
|
|
|
// that PreDispatch is on the parent thread in that case.
|
2014-02-17 10:54:36 +04:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerSameThreadRunnable::PostDispatch(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
2016-02-19 02:02:51 +03:00
|
|
|
// We don't call WorkerRunnable::PostDispatch, because we're using
|
|
|
|
// WorkerThreadModifyBusyCount for mBehavior, and WorkerRunnable will assert
|
|
|
|
// that PostDispatch is on the parent thread in that case.
|
2014-02-17 10:54:36 +04:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
if (aDispatchResult) {
|
|
|
|
DebugOnly<bool> willIncrement = aWorkerPrivate->ModifyBusyCountFromWorker(aCx, true);
|
|
|
|
// Should never fail since if this thread is still running, so should the
|
|
|
|
// parent and it should be able to process a control runnable.
|
|
|
|
MOZ_ASSERT(willIncrement);
|
|
|
|
}
|
|
|
|
}
|