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"
|
2016-05-03 10:09:47 +03:00
|
|
|
#include "mozilla/Telemetry.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 }
|
|
|
|
};
|
|
|
|
|
2016-05-11 09:05:38 +03:00
|
|
|
// This runnable is used to stop a sync loop and it's meant to be used on the
|
|
|
|
// main-thread only. As sync loops keep the busy count incremented as long as
|
|
|
|
// they run this runnable does not modify the busy count
|
|
|
|
// in any way.
|
|
|
|
class MainThreadStopSyncLoopRunnable : public WorkerSyncRunnable
|
|
|
|
{
|
|
|
|
bool mResult;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Passing null for aSyncLoopTarget is not allowed.
|
|
|
|
MainThreadStopSyncLoopRunnable(
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
already_AddRefed<nsIEventTarget>&& aSyncLoopTarget,
|
|
|
|
bool aResult);
|
|
|
|
|
|
|
|
// By default StopSyncLoopRunnables cannot be canceled since they could leave
|
|
|
|
// a sync loop spinning forever.
|
|
|
|
nsresult
|
|
|
|
Cancel() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~MainThreadStopSyncLoopRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool
|
|
|
|
PreDispatch(WorkerPrivate* aWorkerPrivate) override final
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
PostDispatch(WorkerPrivate* aWorkerPrivate, bool aDispatchResult) override;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
DispatchInternal() override final;
|
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
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
|
2016-02-26 23:23:12 +03:00
|
|
|
WorkerRunnable::Dispatch()
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2016-02-26 23:23:12 +03:00
|
|
|
bool ok = PreDispatch(mWorkerPrivate);
|
|
|
|
if (ok) {
|
|
|
|
ok = DispatchInternal();
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2016-02-26 23:23:12 +03:00
|
|
|
PostDispatch(mWorkerPrivate, ok);
|
2013-10-23 17:16:49 +04:00
|
|
|
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
|
2016-02-26 23:23:12 +03:00
|
|
|
WorkerRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
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 (!aDispatchResult) {
|
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount) {
|
2016-02-26 00:05:39 +03:00
|
|
|
aWorkerPrivate->ModifyBusyCount(false);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
bool
|
|
|
|
WorkerRunnable::PreRun(WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
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-26 23:23:12 +03:00
|
|
|
aWorkerPrivate->ModifyBusyCountFromWorker(false);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIRunnable)
|
2013-10-23 17:16:49 +04:00
|
|
|
// 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
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
bool result = PreRun(mWorkerPrivate);
|
|
|
|
if (!result) {
|
|
|
|
MOZ_ASSERT(targetIsWorkerThread,
|
|
|
|
"The only PreRun implementation that can fail is "
|
|
|
|
"ScriptExecutorRunnable");
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(!JS_IsExceptionPending(mWorkerPrivate->GetJSContext()));
|
|
|
|
// We can't enter a useful compartment on the JSContext here; just pass it
|
|
|
|
// in as-is.
|
|
|
|
PostRun(mWorkerPrivate->GetJSContext(), mWorkerPrivate, false);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track down the appropriate global, if any, to use for the AutoEntryScript.
|
2014-08-12 23:29:50 +04:00
|
|
|
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) {
|
2016-04-08 04:58:21 +03:00
|
|
|
globalObject = xpc::NativeGlobal(global);
|
2015-03-04 17:11:32 +03:00
|
|
|
} else {
|
|
|
|
globalObject = DefaultGlobalObject();
|
|
|
|
}
|
2016-03-02 00:52:26 +03:00
|
|
|
|
|
|
|
// We may still not have a globalObject here: in the case of
|
|
|
|
// CompileScriptRunnable, we don't actually create the global object until
|
|
|
|
// we have the script data, which happens in a syncloop under
|
|
|
|
// CompileScriptRunnable::WorkerRun, so we can't assert that it got created
|
|
|
|
// in the PreRun call above.
|
2015-03-04 17:11:32 +03:00
|
|
|
} 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.
|
2016-03-02 00:52:27 +03:00
|
|
|
Maybe<mozilla::dom::AutoJSAPI> maybeJSAPI;
|
2014-08-08 19:06:33 +04:00
|
|
|
Maybe<mozilla::dom::AutoEntryScript> aes;
|
|
|
|
JSContext* cx;
|
2016-03-02 00:52:27 +03:00
|
|
|
AutoJSAPI* jsapi;
|
2014-08-08 19:06:33 +04:00
|
|
|
if (globalObject) {
|
2015-04-09 04:23:48 +03:00
|
|
|
aes.emplace(globalObject, "Worker runnable",
|
|
|
|
isMainThread,
|
|
|
|
isMainThread ? nullptr : GetCurrentThreadJSContext());
|
2016-03-02 00:52:27 +03:00
|
|
|
jsapi = aes.ptr();
|
2014-08-14 02:39:41 +04:00
|
|
|
cx = aes->cx();
|
2013-10-23 17:16:49 +04:00
|
|
|
} else {
|
2016-03-02 00:52:27 +03:00
|
|
|
maybeJSAPI.emplace();
|
|
|
|
maybeJSAPI->Init();
|
|
|
|
jsapi = maybeJSAPI.ptr();
|
2016-03-02 00:52:26 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-02 00:52:27 +03:00
|
|
|
MOZ_ASSERT(!jsapi->HasException());
|
2016-03-02 00:52:26 +03:00
|
|
|
result = WorkerRun(cx, mWorkerPrivate);
|
2016-03-02 00:52:27 +03:00
|
|
|
MOZ_ASSERT_IF(result, !jsapi->HasException());
|
|
|
|
jsapi->ReportException();
|
2016-03-02 02:41:24 +03:00
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
// We can't even assert that this didn't create our global, since in the case
|
|
|
|
// of CompileScriptRunnable it _does_.
|
|
|
|
|
2016-03-02 00:52:26 +03:00
|
|
|
// It would be nice to avoid passing a JSContext to PostRun, but in the case
|
|
|
|
// of ScriptExecutorRunnable we need to know the current compartment on the
|
|
|
|
// JSContext (the one we set up based on the global returned from PreRun) so
|
|
|
|
// that we can sanely do exception reporting. In particular, we want to make
|
|
|
|
// sure that we do our JS_SetPendingException while still in that compartment,
|
|
|
|
// because otherwise we might end up trying to create a cross-compartment
|
|
|
|
// wrapper when we try to move the JS exception from our runnable's
|
|
|
|
// ErrorResult to the JSContext, and that's not desirable in this case.
|
|
|
|
//
|
|
|
|
// We _could_ skip passing a JSContext here and then in
|
|
|
|
// ScriptExecutorRunnable::PostRun end up grabbing it from the WorkerPrivate
|
|
|
|
// and looking at its current compartment. But that seems like slightly weird
|
|
|
|
// action-at-a-distance...
|
|
|
|
//
|
|
|
|
// In any case, we do NOT try to change the compartment on the JSContext at
|
|
|
|
// this point; in the one case in which we could do that
|
|
|
|
// (CompileScriptRunnable) it actually doesn't matter which compartment we're
|
|
|
|
// in for PostRun.
|
2013-10-23 17:16:49 +04:00
|
|
|
PostRun(cx, mWorkerPrivate, result);
|
2016-03-02 00:52:27 +03:00
|
|
|
MOZ_ASSERT(!jsapi->HasException());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
return result ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult
|
2013-10-23 17:16:49 +04:00
|
|
|
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
|
2016-02-26 23:23:12 +03:00
|
|
|
WorkerDebuggerRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
|
2015-03-04 17:11:32 +03:00
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
2016-02-26 23:23:12 +03:00
|
|
|
MainThreadWorkerSyncRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-03 11:21:59 +03:00
|
|
|
MainThreadStopSyncLoopRunnable::MainThreadStopSyncLoopRunnable(
|
2013-10-23 17:16:49 +04:00
|
|
|
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
|
|
|
{
|
2016-05-03 11:21:59 +03:00
|
|
|
AssertIsOnMainThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult
|
2016-05-03 11:21:59 +03:00
|
|
|
MainThreadStopSyncLoopRunnable::Cancel()
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
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
|
2016-05-03 11:21:59 +03:00
|
|
|
MainThreadStopSyncLoopRunnable::WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mSyncLoopTarget);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> syncLoopTarget;
|
|
|
|
mSyncLoopTarget.swap(syncLoopTarget);
|
|
|
|
|
|
|
|
aWorkerPrivate->StopSyncLoop(syncLoopTarget, mResult);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-05-03 11:21:59 +03:00
|
|
|
MainThreadStopSyncLoopRunnable::DispatchInternal()
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSyncLoopTarget);
|
|
|
|
|
2016-05-03 11:21:59 +03:00
|
|
|
RefPtr<MainThreadStopSyncLoopRunnable> 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
|
2016-02-26 23:23:12 +03:00
|
|
|
MainThreadStopSyncLoopRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult
|
2015-04-22 10:03:52 +03:00
|
|
|
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
|
2016-02-26 23:23:12 +03:00
|
|
|
MainThreadWorkerControlRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable)
|
2014-02-17 10:54:36 +04:00
|
|
|
|
2016-05-03 10:09:47 +03:00
|
|
|
WorkerMainThreadRunnable::WorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aTelemetryKey)
|
2014-02-24 17:57:03 +04:00
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
2016-05-03 10:09:47 +03:00
|
|
|
, mTelemetryKey(aTelemetryKey)
|
2014-02-24 17:57:03 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2015-11-24 08:04:20 +03:00
|
|
|
void
|
|
|
|
WorkerMainThreadRunnable::Dispatch(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2016-05-03 10:09:47 +03:00
|
|
|
TimeStamp startTime = TimeStamp::NowLoRes();
|
|
|
|
|
2015-11-24 08:04:20 +03:00
|
|
|
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();
|
|
|
|
}
|
2016-05-03 10:09:47 +03:00
|
|
|
|
2016-05-11 21:37:59 +03:00
|
|
|
// Telemetry is apparently not threadsafe
|
|
|
|
// Telemetry::Accumulate(Telemetry::SYNC_WORKER_OPERATION, mTelemetryKey,
|
|
|
|
// static_cast<uint32_t>((TimeStamp::NowLoRes() - startTime)
|
|
|
|
// .ToMilliseconds()));
|
2015-11-24 08:04:20 +03:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
MOZ_ALWAYS_TRUE(response->Dispatch());
|
2014-02-24 17:57:03 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-03 10:09:47 +03:00
|
|
|
WorkerCheckAPIExposureOnMainThreadRunnable::WorkerCheckAPIExposureOnMainThreadRunnable(WorkerPrivate* aWorkerPrivate):
|
|
|
|
WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("WorkerCheckAPIExposureOnMainThread"))
|
|
|
|
{}
|
|
|
|
|
|
|
|
WorkerCheckAPIExposureOnMainThreadRunnable::~WorkerCheckAPIExposureOnMainThreadRunnable()
|
|
|
|
{}
|
|
|
|
|
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
|
2016-02-26 23:23:12 +03:00
|
|
|
WorkerSameThreadRunnable::PostDispatch(WorkerPrivate* aWorkerPrivate,
|
2014-02-17 10:54:36 +04:00
|
|
|
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) {
|
2016-02-26 23:23:12 +03:00
|
|
|
DebugOnly<bool> willIncrement = aWorkerPrivate->ModifyBusyCountFromWorker(true);
|
2014-02-17 10:54:36 +04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|