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: */
|
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"
|
2013-11-05 18:16:24 +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"
|
2015-02-21 18:09:17 +03:00
|
|
|
#include "nsIInterfaceRequestor.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"
|
2015-02-21 18:09:17 +03:00
|
|
|
#include "nsITabChild.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsITextToSubURI.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "nsIThreadInternal.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
2015-02-21 18:09:17 +03:00
|
|
|
#include "nsIWeakReferenceUtils.h"
|
2014-10-27 20:00:05 +03:00
|
|
|
#include "nsIWorkerDebugger.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2014-12-17 09:26:15 +03:00
|
|
|
#include "nsPerformance.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#include <algorithm>
|
2012-01-15 12:13:08 +04:00
|
|
|
#include "jsfriendapi.h"
|
2012-01-11 12:23:08 +04:00
|
|
|
#include "js/MemoryMetrics.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-09-02 02:26:43 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-25 15:21:20 +04:00
|
|
|
#include "mozilla/ContentEvents.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-05-17 02:49:43 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2014-12-12 19:06:00 +03:00
|
|
|
#include "mozilla/LoadContext.h"
|
2013-08-04 03:55:40 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-10-08 20:15:22 +04:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/dom/ErrorEvent.h"
|
2013-11-05 18:16:24 +04:00
|
|
|
#include "mozilla/dom/ErrorEventBinding.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/FunctionBinding.h"
|
2013-08-04 03:55:40 +04:00
|
|
|
#include "mozilla/dom/ImageData.h"
|
|
|
|
#include "mozilla/dom/ImageDataBinding.h"
|
2014-02-27 14:51:14 +04:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
|
|
|
#include "mozilla/dom/MessagePortList.h"
|
2014-10-28 15:08:19 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-04-10 18:27:57 +03:00
|
|
|
#include "mozilla/dom/PromiseDebugging.h"
|
2014-07-01 14:25:27 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-08-02 00:47:47 +04:00
|
|
|
#include "mozilla/dom/StructuredClone.h"
|
2015-01-28 02:16:21 +03:00
|
|
|
#include "mozilla/dom/WebCryptoCommon.h"
|
2013-11-05 18:16:24 +04:00
|
|
|
#include "mozilla/dom/WorkerBinding.h"
|
2015-03-04 02:51:53 +03:00
|
|
|
#include "mozilla/dom/WorkerDebuggerGlobalScopeBinding.h"
|
|
|
|
#include "mozilla/dom/WorkerGlobalScopeBinding.h"
|
2014-12-17 09:26:15 +03:00
|
|
|
#include "mozilla/dom/indexedDB/IDBFactory.h"
|
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/ipc/nsIRemoteBlob.h"
|
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2013-11-05 18:16:24 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-05-12 15:11:03 +03:00
|
|
|
#include "MultipartBlobImpl.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsAlgorithm.h"
|
|
|
|
#include "nsContentUtils.h"
|
2015-03-18 21:36:03 +03:00
|
|
|
#include "nsCycleCollector.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2015-01-28 02:16:21 +03:00
|
|
|
#include "nsFormData.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"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2012-08-20 22:34:32 +04:00
|
|
|
#include "nsSandboxFlags.h"
|
2014-11-17 22:55:37 +03:00
|
|
|
#include "prthread.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-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "MessagePort.h"
|
2013-11-20 03:08:50 +04:00
|
|
|
#include "Navigator.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "Principal.h"
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "ScriptLoader.h"
|
2014-07-03 04:48:35 +04:00
|
|
|
#include "ServiceWorkerManager.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "SharedWorker.h"
|
2014-10-27 20:00:05 +03:00
|
|
|
#include "WorkerDebuggerManager.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "WorkerFeature.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "WorkerScope.h"
|
2014-11-17 22:55:37 +03:00
|
|
|
#include "WorkerThread.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// JS_MaybeGC will run once every second during normal execution.
|
|
|
|
#define PERIODIC_GC_TIMER_DELAY_SEC 1
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// A shrinking GC will run five seconds after the last event is processed.
|
|
|
|
#define IDLE_GC_TIMER_DELAY_SEC 5
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
#define PREF_WORKERS_ENABLED "dom.workers.enabled"
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef WORKER_LOGGING
|
|
|
|
#define LOG(_args) do { printf _args ; fflush(stdout); } while (0)
|
|
|
|
#else
|
|
|
|
#define LOG(_args) do { } while (0)
|
|
|
|
#endif
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
using namespace mozilla;
|
2012-04-24 14:58:08 +04:00
|
|
|
using namespace mozilla::dom;
|
2014-12-17 09:26:15 +03:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-12-08 09:38:32 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(JsWorkerMallocSizeOf)
|
2012-07-13 10:51:01 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnMainThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
namespace {
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
const nsIID kDEBUGWorkerEventTargetIID = {
|
|
|
|
0xccaba3fa, 0x5be2, 0x4de2, { 0xba, 0x87, 0x3b, 0x3b, 0x5b, 0x1d, 0x5, 0xfb }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
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-10-23 17:16:49 +04:00
|
|
|
// This class is used to wrap any runnables that the worker receives via the
|
|
|
|
// nsIEventTarget::Dispatch() method (either from NS_DispatchToCurrentThread or
|
|
|
|
// from the worker's EventTarget).
|
2015-03-21 19:28:04 +03:00
|
|
|
class ExternalRunnableWrapper final : public WorkerRunnable
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsICancelableRunnable> mWrappedRunnable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ExternalRunnableWrapper(WorkerPrivate* aWorkerPrivate,
|
|
|
|
nsICancelableRunnable* aWrappedRunnable)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mWrappedRunnable(aWrappedRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aWrappedRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ExternalRunnableWrapper()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
nsresult rv = mWrappedRunnable->Run();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (!JS_IsExceptionPending(aCx)) {
|
|
|
|
Throw(aCx, rv);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Cancel() override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
nsresult rv = mWrappedRunnable->Cancel();
|
|
|
|
nsresult rv2 = WorkerRunnable::Cancel();
|
|
|
|
return NS_FAILED(rv) ? rv : rv2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
struct WindowAction
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
bool mDefaultAction;
|
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
MOZ_IMPLICIT WindowAction(nsPIDOMWindow* aWindow)
|
2014-07-02 14:26:49 +04:00
|
|
|
: mWindow(aWindow), mDefaultAction(true)
|
2013-06-05 18:04:23 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:15:04 +03:00
|
|
|
// Recursive!
|
2015-05-12 15:11:03 +03:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
EnsureBlobForBackgroundManager(BlobImpl* aBlobImpl,
|
2015-01-14 01:15:04 +03:00
|
|
|
PBackgroundChild* aManager = nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
|
|
|
|
|
|
|
if (!aManager) {
|
|
|
|
aManager = BackgroundChild::GetForCurrentThread();
|
|
|
|
MOZ_ASSERT(aManager);
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsRefPtr<BlobImpl> blobImpl = aBlobImpl;
|
2015-01-14 01:15:04 +03:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
const nsTArray<nsRefPtr<BlobImpl>>* subBlobImpls =
|
2015-01-14 01:15:04 +03:00
|
|
|
aBlobImpl->GetSubBlobImpls();
|
|
|
|
|
|
|
|
if (!subBlobImpls) {
|
|
|
|
if (nsCOMPtr<nsIRemoteBlob> remoteBlob = do_QueryObject(blobImpl)) {
|
|
|
|
// Always make sure we have a blob from an actor we can use on this
|
|
|
|
// thread.
|
|
|
|
BlobChild* blobChild = BlobChild::GetOrCreate(aManager, blobImpl);
|
|
|
|
MOZ_ASSERT(blobChild);
|
|
|
|
|
|
|
|
blobImpl = blobChild->GetBlobImpl();
|
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
DebugOnly<bool> isMutable;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(blobImpl->GetMutable(&isMutable)));
|
|
|
|
MOZ_ASSERT(!isMutable);
|
|
|
|
} else {
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(blobImpl->SetMutable(false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t subBlobCount = subBlobImpls->Length();
|
|
|
|
MOZ_ASSERT(subBlobCount);
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsTArray<nsRefPtr<BlobImpl>> newSubBlobImpls;
|
2015-01-14 01:15:04 +03:00
|
|
|
newSubBlobImpls.SetLength(subBlobCount);
|
|
|
|
|
|
|
|
bool newBlobImplNeeded = false;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < subBlobCount; index++) {
|
2015-05-12 15:11:03 +03:00
|
|
|
const nsRefPtr<BlobImpl>& subBlobImpl = subBlobImpls->ElementAt(index);
|
2015-01-14 01:15:04 +03:00
|
|
|
MOZ_ASSERT(subBlobImpl);
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsRefPtr<BlobImpl>& newSubBlobImpl = newSubBlobImpls[index];
|
2015-01-14 01:15:04 +03:00
|
|
|
|
|
|
|
newSubBlobImpl = EnsureBlobForBackgroundManager(subBlobImpl, aManager);
|
|
|
|
MOZ_ASSERT(newSubBlobImpl);
|
|
|
|
|
|
|
|
if (subBlobImpl != newSubBlobImpl) {
|
|
|
|
newBlobImplNeeded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newBlobImplNeeded) {
|
|
|
|
nsString contentType;
|
|
|
|
blobImpl->GetType(contentType);
|
|
|
|
|
|
|
|
if (blobImpl->IsFile()) {
|
|
|
|
nsString name;
|
|
|
|
blobImpl->GetName(name);
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
blobImpl = new MultipartBlobImpl(newSubBlobImpls, name, contentType);
|
2015-01-14 01:15:04 +03:00
|
|
|
} else {
|
2015-05-12 15:11:03 +03:00
|
|
|
blobImpl = new MultipartBlobImpl(newSubBlobImpls, contentType);
|
2015-01-14 01:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(blobImpl->SetMutable(false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
already_AddRefed<Blob>
|
2015-01-28 02:16:21 +03:00
|
|
|
ReadBlobOrFileNoWrap(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
bool aIsMainThread)
|
2014-12-17 09:26:15 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsRefPtr<BlobImpl> blobImpl;
|
2014-12-17 09:26:15 +03:00
|
|
|
{
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImpl* rawBlobImpl;
|
2014-12-17 09:26:15 +03:00
|
|
|
MOZ_ALWAYS_TRUE(JS_ReadBytes(aReader, &rawBlobImpl, sizeof(rawBlobImpl)));
|
|
|
|
|
|
|
|
MOZ_ASSERT(rawBlobImpl);
|
|
|
|
|
|
|
|
blobImpl = rawBlobImpl;
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:15:04 +03:00
|
|
|
blobImpl = EnsureBlobForBackgroundManager(blobImpl);
|
|
|
|
MOZ_ASSERT(blobImpl);
|
2014-12-17 09:26:15 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> parent;
|
|
|
|
if (aIsMainThread) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal =
|
|
|
|
nsJSUtils::GetStaticScriptGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
parent = do_QueryInterface(scriptGlobal);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
WorkerGlobalScope* globalScope = workerPrivate->GlobalScope();
|
|
|
|
MOZ_ASSERT(globalScope);
|
|
|
|
|
|
|
|
parent = do_QueryObject(globalScope);
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = Blob::Create(parent, blobImpl);
|
2015-01-28 02:16:21 +03:00
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReadBlobOrFile(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
bool aIsMainThread,
|
|
|
|
JS::MutableHandle<JSObject*> aBlobOrFile)
|
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = ReadBlobOrFileNoWrap(aCx, aReader, aIsMainThread);
|
2015-05-14 00:07:34 +03:00
|
|
|
aBlobOrFile.set(blob->WrapObject(aCx, nullptr));
|
2014-12-17 09:26:15 +03:00
|
|
|
}
|
|
|
|
|
2015-01-28 02:16:21 +03:00
|
|
|
// See WriteFormData for serialization format.
|
|
|
|
void
|
|
|
|
ReadFormData(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
bool aIsMainThread,
|
|
|
|
uint32_t aCount,
|
|
|
|
JS::MutableHandle<JSObject*> aFormData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aReader);
|
|
|
|
MOZ_ASSERT(!aFormData);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> parent;
|
|
|
|
if (aIsMainThread) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal =
|
|
|
|
nsJSUtils::GetStaticScriptGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
parent = do_QueryInterface(scriptGlobal);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
WorkerGlobalScope* globalScope = workerPrivate->GlobalScope();
|
|
|
|
MOZ_ASSERT(globalScope);
|
|
|
|
|
|
|
|
parent = do_QueryObject(globalScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsFormData> formData = new nsFormData(parent);
|
|
|
|
MOZ_ASSERT(formData);
|
|
|
|
|
|
|
|
Optional<nsAString> thirdArg;
|
|
|
|
|
|
|
|
uint32_t isFile;
|
|
|
|
uint32_t dummy;
|
|
|
|
for (uint32_t i = 0; i < aCount; ++i) {
|
|
|
|
MOZ_ALWAYS_TRUE(JS_ReadUint32Pair(aReader, &isFile, &dummy));
|
|
|
|
|
|
|
|
nsAutoString name;
|
|
|
|
MOZ_ALWAYS_TRUE(ReadString(aReader, name));
|
|
|
|
|
|
|
|
if (isFile) {
|
|
|
|
// Read out the tag since the blob reader isn't expecting it.
|
|
|
|
MOZ_ALWAYS_TRUE(JS_ReadUint32Pair(aReader, &dummy, &dummy));
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob = ReadBlobOrFileNoWrap(aCx, aReader, aIsMainThread);
|
2015-01-28 02:16:21 +03:00
|
|
|
MOZ_ASSERT(blob);
|
|
|
|
formData->Append(name, *blob, thirdArg);
|
|
|
|
} else {
|
|
|
|
nsAutoString value;
|
|
|
|
MOZ_ALWAYS_TRUE(ReadString(aReader, value));
|
|
|
|
formData->Append(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 00:07:34 +03:00
|
|
|
aFormData.set(formData->WrapObject(aCx, nullptr));
|
2015-01-28 02:16:21 +03:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
bool
|
|
|
|
WriteBlobOrFile(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImpl* aBlobOrBlobImpl,
|
2014-12-17 09:26:15 +03:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aWriter);
|
2015-05-12 15:11:03 +03:00
|
|
|
MOZ_ASSERT(aBlobOrBlobImpl);
|
2014-12-17 09:26:15 +03:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsRefPtr<BlobImpl> blobImpl = EnsureBlobForBackgroundManager(aBlobOrBlobImpl);
|
2015-01-14 01:15:04 +03:00
|
|
|
MOZ_ASSERT(blobImpl);
|
2014-12-17 09:26:15 +03:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
aBlobOrBlobImpl = blobImpl;
|
2014-12-17 09:26:15 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(!JS_WriteUint32Pair(aWriter, DOMWORKER_SCTAG_BLOB, 0)) ||
|
|
|
|
NS_WARN_IF(!JS_WriteBytes(aWriter,
|
2015-05-12 15:11:03 +03:00
|
|
|
&aBlobOrBlobImpl,
|
|
|
|
sizeof(aBlobOrBlobImpl)))) {
|
2014-12-17 09:26:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
aClonedObjects.AppendElement(aBlobOrBlobImpl);
|
2014-12-17 09:26:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-28 02:16:21 +03:00
|
|
|
// A FormData is serialized as:
|
|
|
|
// - A pair of ints (tag identifying it as a FormData, number of elements in
|
|
|
|
// the FormData)
|
|
|
|
// - for each (key, value) pair:
|
|
|
|
// - pair of ints (is value a file?, 0). If not a file, value is a string.
|
|
|
|
// - string name
|
|
|
|
// - if value is a file:
|
|
|
|
// - write the file/blob
|
|
|
|
// - else:
|
|
|
|
// - string value
|
|
|
|
bool
|
|
|
|
WriteFormData(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
nsFormData* aFormData,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aWriter);
|
|
|
|
MOZ_ASSERT(aFormData);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!JS_WriteUint32Pair(aWriter, DOMWORKER_SCTAG_FORMDATA, aFormData->Length()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS Closure {
|
|
|
|
JSContext* mCx;
|
|
|
|
JSStructuredCloneWriter* mWriter;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& mClones;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Closure(JSContext* aCx, JSStructuredCloneWriter* aWriter,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClones)
|
|
|
|
: mCx(aCx), mWriter(aWriter), mClones(aClones)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
static bool
|
|
|
|
Write(const nsString& aName, bool isFile, const nsString& aValue,
|
|
|
|
File* aFile, void* aClosure)
|
|
|
|
{
|
|
|
|
Closure* closure = static_cast<Closure*>(aClosure);
|
|
|
|
if (!JS_WriteUint32Pair(closure->mWriter, /* a file? */ (uint32_t) isFile, 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WriteString(closure->mWriter, aName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFile) {
|
|
|
|
if (!WriteBlobOrFile(closure->mCx, closure->mWriter, aFile->Impl(), closure->mClones)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!WriteString(closure->mWriter, aValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Closure closure(aCx, aWriter, aClonedObjects);
|
|
|
|
return aFormData->ForEach(Closure::Write, &closure);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-06-26 02:35:36 +04:00
|
|
|
JS::Rooted<JSObject*> result(aCx);
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
// See if object is a nsIDOMBlob pointer.
|
2014-10-08 20:15:22 +04:00
|
|
|
if (aTag == DOMWORKER_SCTAG_BLOB) {
|
2014-02-18 10:09:06 +04:00
|
|
|
MOZ_ASSERT(!aData);
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
JS::Rooted<JSObject*> blobOrFile(aCx);
|
|
|
|
ReadBlobOrFile(aCx, aReader, /* aIsMainThread */ false, &blobOrFile);
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
return blobOrFile;
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
2012-04-24 14:58:08 +04:00
|
|
|
// See if the object is an ImageData.
|
|
|
|
else if (aTag == SCTAG_DOM_IMAGEDATA) {
|
2014-02-18 10:09:06 +04:00
|
|
|
MOZ_ASSERT(!aData);
|
2014-08-02 00:47:47 +04:00
|
|
|
return ReadStructuredCloneImageData(aCx, aReader);
|
2012-04-24 14:58:08 +04:00
|
|
|
}
|
2015-01-28 02:16:21 +03:00
|
|
|
// See if the object is a FormData.
|
|
|
|
else if (aTag == DOMWORKER_SCTAG_FORMDATA) {
|
|
|
|
JS::Rooted<JSObject*> formData(aCx);
|
|
|
|
// aData is the entry count.
|
|
|
|
ReadFormData(aCx, aReader, /* aIsMainThread */ false, aData, &formData);
|
|
|
|
return formData;
|
|
|
|
}
|
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.
|
2014-12-17 09:26:15 +03:00
|
|
|
auto* clonedObjects =
|
|
|
|
static_cast<nsTArray<nsCOMPtr<nsISupports>>*>(aClosure);
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
// See if this is a Blob/File object.
|
2011-08-16 07:40:38 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob;
|
2014-10-08 20:15:22 +04:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob))) {
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImpl* blobImpl = blob->Impl();
|
2014-12-17 09:26:15 +03:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
|
|
|
if (WriteBlobOrFile(aCx, aWriter, blobImpl, *clonedObjects)) {
|
2011-08-16 07:40:38 +04:00
|
|
|
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-11-21 16:51:16 +04:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(ImageData, aObj, imageData))) {
|
2014-08-02 00:47:47 +04:00
|
|
|
return WriteStructuredCloneImageData(aCx, aWriter, imageData);
|
2013-08-04 03:55:40 +04:00
|
|
|
}
|
2012-04-24 14:58:08 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 02:16:21 +03:00
|
|
|
// See if this is a FormData object.
|
|
|
|
{
|
|
|
|
nsFormData* formData = nullptr;
|
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(FormData, aObj, formData))) {
|
|
|
|
if (WriteFormData(aCx, aWriter, formData, *clonedObjects)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const JSStructuredCloneCallbacks gWorkerStructuredCloneCallbacks = {
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerStructuredCloneCallbacks::Read,
|
|
|
|
WorkerStructuredCloneCallbacks::Write,
|
2013-11-15 08:42:34 +04:00
|
|
|
WorkerStructuredCloneCallbacks::Error,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
// See if object is a Blob/File pointer.
|
|
|
|
if (aTag == DOMWORKER_SCTAG_BLOB) {
|
2014-02-18 10:09:06 +04:00
|
|
|
MOZ_ASSERT(!aData);
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
JS::Rooted<JSObject*> blobOrFile(aCx);
|
|
|
|
ReadBlobOrFile(aCx, aReader, /* aIsMainThread */ true, &blobOrFile);
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
return blobOrFile;
|
2011-08-16 07:40:38 +04:00
|
|
|
}
|
2015-01-28 02:16:21 +03:00
|
|
|
// See if the object is a FormData.
|
|
|
|
else if (aTag == DOMWORKER_SCTAG_FORMDATA) {
|
|
|
|
JS::Rooted<JSObject*> formData(aCx);
|
|
|
|
// aData is the entry count.
|
|
|
|
ReadFormData(aCx, aReader, /* aIsMainThread */ true, aData, &formData);
|
|
|
|
return formData;
|
|
|
|
}
|
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.
|
2014-12-17 09:26:15 +03:00
|
|
|
auto* clonedObjects =
|
|
|
|
static_cast<nsTArray<nsCOMPtr<nsISupports>>*>(aClosure);
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
// See if this is a Blob/File object.
|
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
nsRefPtr<Blob> blob;
|
2014-10-08 20:15:22 +04:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob))) {
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImpl* blobImpl = blob->Impl();
|
2014-12-17 09:26:15 +03:00
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
2015-01-12 00:35:24 +03:00
|
|
|
if (!blobImpl->MayBeClonedToOtherThreads()) {
|
|
|
|
NS_WARNING("Not all the blob implementations can be sent between threads.");
|
2014-12-17 09:26:15 +03:00
|
|
|
} else if (WriteBlobOrFile(aCx, aWriter, blobImpl, *clonedObjects)) {
|
2014-10-08 20:15:22 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const JSStructuredCloneCallbacks gMainThreadWorkerStructuredCloneCallbacks = {
|
2011-07-17 23:09:13 +04:00
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Read,
|
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Write,
|
2013-11-15 08:42:34 +04:00
|
|
|
MainThreadWorkerStructuredCloneCallbacks::Error,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const JSStructuredCloneCallbacks gChromeWorkerStructuredCloneCallbacks = {
|
2011-07-17 23:09:13 +04:00
|
|
|
ChromeWorkerStructuredCloneCallbacks::Read,
|
|
|
|
ChromeWorkerStructuredCloneCallbacks::Write,
|
2013-11-15 08:42:34 +04:00
|
|
|
ChromeWorkerStructuredCloneCallbacks::Error,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const JSStructuredCloneCallbacks gMainThreadChromeWorkerStructuredCloneCallbacks = {
|
2011-07-17 23:09:13 +04:00
|
|
|
MainThreadChromeWorkerStructuredCloneCallbacks::Read,
|
|
|
|
MainThreadChromeWorkerStructuredCloneCallbacks::Write,
|
2013-11-15 08:42:34 +04:00
|
|
|
MainThreadChromeWorkerStructuredCloneCallbacks::Error,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MainThreadReleaseRunnable final : public nsRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>> mDoomed;
|
2015-02-21 18:09:17 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroupToCancel;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
public:
|
2013-10-23 17:16:49 +04:00
|
|
|
MainThreadReleaseRunnable(nsTArray<nsCOMPtr<nsISupports>>& aDoomed,
|
2015-02-21 18:09:17 +03:00
|
|
|
nsCOMPtr<nsILoadGroup>& aLoadGroupToCancel)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
mDoomed.SwapElements(aDoomed);
|
2015-02-21 18:09:17 +03:00
|
|
|
mLoadGroupToCancel.swap(aLoadGroupToCancel);
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2011-07-26 05:49:16 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
2015-02-21 18:09:17 +03:00
|
|
|
if (mLoadGroupToCancel) {
|
|
|
|
mLoadGroupToCancel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mLoadGroupToCancel = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
mDoomed.Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
~MainThreadReleaseRunnable()
|
|
|
|
{ }
|
2012-03-31 08:42:20 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerFinishedRunnable final : public WorkerControlRunnable
|
2012-03-31 08:42:20 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* mFinishedWorker;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerFinishedRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate* aFinishedWorker)
|
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mFinishedWorker(aFinishedWorker)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2011-07-17 23:09:13 +04:00
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2015-02-21 18:09:17 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroupToCancel;
|
|
|
|
mFinishedWorker->ForgetOverridenLoadGroup(loadGroupToCancel);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>> doomed;
|
2011-07-17 23:09:13 +04:00
|
|
|
mFinishedWorker->ForgetMainThreadObjects(doomed);
|
|
|
|
|
|
|
|
nsRefPtr<MainThreadReleaseRunnable> runnable =
|
2015-05-07 10:06:24 +03:00
|
|
|
new MainThreadReleaseRunnable(doomed, loadGroupToCancel);
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to dispatch, going to leak!");
|
|
|
|
}
|
|
|
|
|
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
NS_ASSERTION(runtime, "This should never be null!");
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
mFinishedWorker->DisableDebugger();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
runtime->UnregisterWorker(aCx, mFinishedWorker);
|
|
|
|
|
2014-02-02 22:08:50 +04:00
|
|
|
mFinishedWorker->ClearSelfRef();
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TopLevelWorkerFinishedRunnable final : public nsRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* mFinishedWorker;
|
|
|
|
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit TopLevelWorkerFinishedRunnable(WorkerPrivate* aFinishedWorker)
|
2013-10-23 17:16:49 +04:00
|
|
|
: mFinishedWorker(aFinishedWorker)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
aFinishedWorker->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
private:
|
2014-07-09 01:23:17 +04:00
|
|
|
~TopLevelWorkerFinishedRunnable() {}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Run() override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-02-02 22:08:50 +04:00
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
MOZ_ASSERT(runtime);
|
|
|
|
|
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
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
mFinishedWorker->DisableDebugger();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
runtime->UnregisterWorker(cx, mFinishedWorker);
|
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroupToCancel;
|
|
|
|
mFinishedWorker->ForgetOverridenLoadGroup(loadGroupToCancel);
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > doomed;
|
|
|
|
mFinishedWorker->ForgetMainThreadObjects(doomed);
|
|
|
|
|
|
|
|
nsRefPtr<MainThreadReleaseRunnable> runnable =
|
2015-05-07 10:06:24 +03:00
|
|
|
new MainThreadReleaseRunnable(doomed, loadGroupToCancel);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch, going to leak!");
|
|
|
|
}
|
|
|
|
|
2014-02-02 22:08:50 +04:00
|
|
|
mFinishedWorker->ClearSelfRef();
|
2011-07-26 05:49:16 +04:00
|
|
|
return NS_OK;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ModifyBusyCountRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
bool mIncrease;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ModifyBusyCountRunnable(WorkerPrivate* aWorkerPrivate, bool aIncrease)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount),
|
2011-07-17 23:09:13 +04:00
|
|
|
mIncrease(aIncrease)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, mIncrease);
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2011-07-17 23:09:13 +04:00
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
2015-03-21 19:28:04 +03:00
|
|
|
override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
if (mIncrease) {
|
|
|
|
WorkerControlRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't do anything here as it's possible that aWorkerPrivate has been
|
|
|
|
// deleted.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CompileScriptRunnable final : public WorkerRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2015-03-17 13:15:19 +03:00
|
|
|
nsString mScriptURL;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
public:
|
2015-03-17 13:15:19 +03:00
|
|
|
explicit CompileScriptRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aScriptURL)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
|
|
|
|
mScriptURL(aScriptURL)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2015-03-04 02:51:53 +03:00
|
|
|
WorkerGlobalScope* globalScope =
|
|
|
|
aWorkerPrivate->GetOrCreateGlobalScope(aCx);
|
|
|
|
if (!globalScope) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to make global!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
JS::Rooted<JSObject*> global(aCx, globalScope->GetWrapper());
|
2015-03-17 13:15:19 +03:00
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
JSAutoCompartment ac(aCx, global);
|
2015-03-17 13:15:19 +03:00
|
|
|
bool result = scriptloader::LoadMainScript(aCx, mScriptURL, WorkerScript);
|
2014-05-29 20:33:04 +04:00
|
|
|
if (result) {
|
|
|
|
aWorkerPrivate->SetWorkerScriptExecutedSuccessfully();
|
|
|
|
}
|
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CompileDebuggerScriptRunnable final : public WorkerDebuggerRunnable
|
2015-03-17 13:15:19 +03:00
|
|
|
{
|
|
|
|
nsString mScriptURL;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CompileDebuggerScriptRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aScriptURL)
|
|
|
|
: WorkerDebuggerRunnable(aWorkerPrivate),
|
|
|
|
mScriptURL(aScriptURL)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2015-03-17 13:15:19 +03:00
|
|
|
{
|
|
|
|
WorkerDebuggerGlobalScope* globalScope =
|
|
|
|
aWorkerPrivate->CreateDebuggerGlobalScope(aCx);
|
|
|
|
if (!globalScope) {
|
|
|
|
NS_WARNING("Failed to make global!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx, globalScope->GetWrapper());
|
|
|
|
|
|
|
|
JSAutoCompartment ac(aCx, global);
|
|
|
|
return scriptloader::LoadMainScript(aCx, mScriptURL, DebuggerScript);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CloseEventRunnable final : public WorkerRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit CloseEventRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Don't call Dispatch() on CloseEventRunnable!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Don't call Dispatch() on CloseEventRunnable!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
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-11-05 18:16:26 +04:00
|
|
|
WorkerGlobalScope* globalScope = aWorkerPrivate->GlobalScope();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
nsresult rv =
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), globalScope, nullptr, nullptr);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Throw(aCx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
rv = event->InitEvent(NS_LITERAL_STRING("close"), false, false);
|
2013-10-23 17:16:49 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Throw(aCx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
event->SetTrusted(true);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
globalScope->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2011-07-17 23:09:13 +04:00
|
|
|
PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, bool aRunResult)
|
2015-03-21 19:28:04 +03:00
|
|
|
override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// Report errors.
|
|
|
|
WorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult);
|
|
|
|
|
|
|
|
// Match the busy count increase from NotifyRunnable.
|
|
|
|
if (!aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->CloseHandlerFinished();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MessageEventRunnable final : public WorkerRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
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
|
|
|
|
2015-03-16 23:23:42 +03:00
|
|
|
// This is only used for messages dispatched to a service worker.
|
|
|
|
nsAutoPtr<ServiceWorkerClientInfo> mEventSource;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
public:
|
2013-10-23 17:16:49 +04:00
|
|
|
MessageEventRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
TargetAndBusyBehavior aBehavior,
|
2014-02-01 06:50:07 +04:00
|
|
|
JSAutoStructuredCloneBuffer&& aData,
|
2013-06-05 18:04:23 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
|
|
|
bool aToMessagePort, uint64_t aMessagePortSerial)
|
2014-02-01 06:50:07 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, aBehavior)
|
|
|
|
, mBuffer(Move(aData))
|
|
|
|
, mMessagePortSerial(aMessagePortSerial)
|
|
|
|
, mToMessagePort(aToMessagePort)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-04-01 21:35:13 +04:00
|
|
|
mClonedObjects.SwapElements(aClonedObjects);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:23:42 +03:00
|
|
|
void
|
|
|
|
SetMessageSource(ServiceWorkerClientInfo* aSource)
|
|
|
|
{
|
|
|
|
mEventSource = aSource;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
bool
|
|
|
|
DispatchDOMEvent(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper* aTarget, bool aIsMainThread)
|
2013-11-05 18:16:26 +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,
|
|
|
|
workers::WorkerStructuredCloneCallbacks(aIsMainThread))) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:51:14 +04:00
|
|
|
nsRefPtr<MessageEvent> event = new MessageEvent(aTarget, nullptr, nullptr);
|
2013-11-05 18:16:26 +04:00
|
|
|
nsresult rv =
|
|
|
|
event->InitMessageEvent(NS_LITERAL_STRING("message"),
|
|
|
|
false /* non-bubbling */,
|
|
|
|
true /* cancelable */,
|
|
|
|
messageData,
|
|
|
|
EmptyString(),
|
|
|
|
EmptyString(),
|
|
|
|
nullptr);
|
2015-03-16 23:23:42 +03:00
|
|
|
if (mEventSource) {
|
|
|
|
nsRefPtr<ServiceWorkerClient> client =
|
|
|
|
new ServiceWorkerWindowClient(aTarget, *mEventSource);
|
|
|
|
event->SetSource(client);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(aCx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> domEvent = do_QueryObject(event);
|
|
|
|
|
|
|
|
nsEventStatus dummy = nsEventStatus_eIgnore;
|
|
|
|
aTarget->DispatchDOMEvent(nullptr, domEvent, nullptr, &dummy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT_IF(mToMessagePort, aWorkerPrivate->IsSharedWorker());
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mBehavior == ParentThreadUnchangedBusyCount) {
|
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,
|
2014-02-01 06:50:07 +04:00
|
|
|
Move(mBuffer),
|
2013-06-05 18:04:23 +04:00
|
|
|
mClonedObjects);
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (aWorkerPrivate->IsFrozen()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
aWorkerPrivate->QueueRunnable(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->AssertInnerWindowIsCorrect();
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return DispatchDOMEvent(aCx, aWorkerPrivate, aWorkerPrivate,
|
|
|
|
!aWorkerPrivate->GetParent());
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aWorkerPrivate == GetWorkerPrivateFromContext(aCx));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
if (mToMessagePort) {
|
2013-11-05 18:16:26 +04:00
|
|
|
nsRefPtr<workers::MessagePort> port =
|
2013-11-05 18:16:24 +04:00
|
|
|
aWorkerPrivate->GetMessagePort(mMessagePortSerial);
|
|
|
|
if (!port) {
|
|
|
|
// Must have been closed already.
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
return DispatchDOMEvent(aCx, aWorkerPrivate, port, false);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return DispatchDOMEvent(aCx, aWorkerPrivate, aWorkerPrivate->GlobalScope(),
|
|
|
|
false);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-20 14:15:59 +03:00
|
|
|
class DebuggerMessageEventRunnable : public WorkerDebuggerRunnable {
|
|
|
|
nsString mMessage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DebuggerMessageEventRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
: WorkerDebuggerRunnable(aWorkerPrivate),
|
|
|
|
mMessage(aMessage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2015-03-20 14:15:59 +03:00
|
|
|
{
|
|
|
|
WorkerDebuggerGlobalScope* globalScope = aWorkerPrivate->DebuggerGlobalScope();
|
|
|
|
MOZ_ASSERT(globalScope);
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> message(aCx, JS_NewUCStringCopyN(aCx, mMessage.get(),
|
|
|
|
mMessage.Length()));
|
|
|
|
if (!message) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JS::Rooted<JS::Value> data(aCx, JS::StringValue(message));
|
|
|
|
|
|
|
|
nsRefPtr<MessageEvent> event = new MessageEvent(globalScope, nullptr,
|
|
|
|
nullptr);
|
|
|
|
nsresult rv =
|
|
|
|
event->InitMessageEvent(NS_LITERAL_STRING("message"),
|
|
|
|
false, // canBubble
|
|
|
|
true, // cancelable
|
|
|
|
data,
|
|
|
|
EmptyString(),
|
|
|
|
EmptyString(),
|
|
|
|
nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
xpc::Throw(aCx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> domEvent = do_QueryObject(event);
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
globalScope->DispatchDOMEvent(nullptr, domEvent, nullptr, &status);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class NotifyRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
Status mStatus;
|
|
|
|
|
|
|
|
public:
|
2012-08-08 01:38:46 +04:00
|
|
|
NotifyRunnable(WorkerPrivate* aWorkerPrivate, Status aStatus)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2012-08-08 01:38:46 +04:00
|
|
|
mStatus(aStatus)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-01-11 04:37:47 +04:00
|
|
|
MOZ_ASSERT(aStatus == Closing || aStatus == Terminating ||
|
|
|
|
aStatus == Canceling || aStatus == Killing);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2013-11-05 18:16:24 +04:00
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2013-11-05 18:16:24 +04:00
|
|
|
{
|
|
|
|
if (!aDispatchResult) {
|
|
|
|
// We couldn't dispatch to the worker, which means it's already dead.
|
|
|
|
// Undo the busy count modification.
|
|
|
|
aWorkerPrivate->ModifyBusyCount(aCx, false);
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
return aWorkerPrivate->NotifyInternal(aCx, mStatus);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CloseRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit CloseRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// This busy count will be matched by the CloseEventRunnable.
|
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, true) &&
|
2015-04-15 05:35:01 +03:00
|
|
|
aWorkerPrivate->Close();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
class FreezeRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-04-01 12:00:19 +03:00
|
|
|
explicit FreezeRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2015-04-01 12:00:19 +03:00
|
|
|
return aWorkerPrivate->FreezeInternal(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
class ThawRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-04-01 12:00:19 +03:00
|
|
|
explicit ThawRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2015-04-01 12:00:19 +03:00
|
|
|
return aWorkerPrivate->ThawInternal(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ReportErrorRunnable final : public WorkerRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
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:
|
2013-11-05 18:16:24 +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-11-05 18:16:24 +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.
|
2014-01-03 05:04:15 +04:00
|
|
|
RootedDictionary<ErrorEventInit> init(aCx);
|
2014-02-23 09:01:12 +04:00
|
|
|
init.mMessage = aMessage;
|
|
|
|
init.mFilename = aFilename;
|
|
|
|
init.mLineno = aLineNumber;
|
|
|
|
init.mCancelable = true;
|
|
|
|
init.mBubbles = true;
|
2013-10-14 07:10:43 +04:00
|
|
|
|
2014-02-23 09:01:12 +04:00
|
|
|
if (aTarget) {
|
2013-11-05 18:16:24 +04:00
|
|
|
nsRefPtr<ErrorEvent> event =
|
|
|
|
ErrorEvent::Constructor(aTarget, NS_LITERAL_STRING("error"), init);
|
2013-11-05 18:16:26 +04:00
|
|
|
event->SetTrusted(true);
|
|
|
|
|
2013-11-05 18:16:24 +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-11-05 18:16:24 +04:00
|
|
|
if (aFireAtScope && (aTarget || aErrorNumber != JSMSG_OVER_RECURSED)) {
|
2015-03-04 02:51:53 +03:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
NS_ASSERTION(global, "This should never be null!");
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2013-12-12 09:13:24 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2012-12-08 04:35:44 +04:00
|
|
|
nsIScriptGlobalObject* sgo;
|
|
|
|
|
2013-12-10 01:59:15 +04:00
|
|
|
if (aWorkerPrivate) {
|
2015-03-04 02:51:53 +03:00
|
|
|
WorkerGlobalScope* globalScope = nullptr;
|
|
|
|
UNWRAP_WORKER_OBJECT(WorkerGlobalScope, global, globalScope);
|
2015-03-26 22:09:45 +03:00
|
|
|
|
|
|
|
if (!globalScope) {
|
2015-03-04 02:51:53 +03:00
|
|
|
WorkerDebuggerGlobalScope* globalScope = nullptr;
|
|
|
|
UNWRAP_OBJECT(WorkerDebuggerGlobalScope, global, globalScope);
|
2015-03-31 13:22:40 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT_IF(globalScope, globalScope->GetWrapperPreserveColor() == global);
|
|
|
|
MOZ_ASSERT_IF(!globalScope, IsDebuggerSandbox(global));
|
2015-03-26 22:09:45 +03:00
|
|
|
|
|
|
|
aWorkerPrivate->ReportErrorToDebugger(aFilename, aLineNumber,
|
|
|
|
aMessage);
|
|
|
|
return true;
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2015-03-31 13:22:40 +03:00
|
|
|
MOZ_ASSERT(globalScope->GetWrapperPreserveColor() == global);
|
2015-03-26 22:09:45 +03:00
|
|
|
nsIDOMEventTarget* target = static_cast<nsIDOMEventTarget*>(globalScope);
|
|
|
|
|
2014-02-23 09:01:12 +04:00
|
|
|
nsRefPtr<ErrorEvent> event =
|
|
|
|
ErrorEvent::Constructor(aTarget, NS_LITERAL_STRING("error"), init);
|
|
|
|
event->SetTrusted(true);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
if (NS_FAILED(EventDispatcher::DispatchDOMEvent(target, nullptr,
|
|
|
|
event, nullptr,
|
|
|
|
&status))) {
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_WARNING("Failed to dispatch worker thread error event!");
|
|
|
|
status = nsEventStatus_eIgnore;
|
2012-12-08 04:35:44 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2015-03-04 02:51:53 +03:00
|
|
|
else if ((sgo = nsJSUtils::GetStaticScriptGlobal(global))) {
|
2013-12-21 04:22:13 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2014-02-23 09:01:12 +04:00
|
|
|
if (NS_FAILED(sgo->HandleScriptError(init, &status))) {
|
2012-12-08 04:35:44 +04:00
|
|
|
NS_WARNING("Failed to dispatch main thread error event!");
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-12-12 09:13:24 +04:00
|
|
|
// Was preventDefault() called?
|
|
|
|
if (status == nsEventStatus_eConsumeNoDefault) {
|
2012-12-08 04:35:44 +04:00
|
|
|
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;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
ReportErrorRunnable(WorkerPrivate* aWorkerPrivate, const nsString& aMessage,
|
|
|
|
const nsString& aFilename, const nsString& aLine,
|
|
|
|
uint32_t aLineNumber, uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags, uint32_t aErrorNumber)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, ParentThreadUnchangedBusyCount),
|
|
|
|
mMessage(aMessage), mFilename(aFilename), mLine(aLine),
|
|
|
|
mLineNumber(aLineNumber), mColumnNumber(aColumnNumber), mFlags(aFlags),
|
|
|
|
mErrorNumber(aErrorNumber)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> target(aCx, aWorkerPrivate->GetWrapper());
|
|
|
|
|
|
|
|
uint64_t innerWindowId;
|
|
|
|
bool fireAtScope = true;
|
|
|
|
|
2014-07-11 22:59:39 +04:00
|
|
|
bool workerIsAcceptingEvents = aWorkerPrivate->IsAcceptingEvents();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate* parent = aWorkerPrivate->GetParent();
|
|
|
|
if (parent) {
|
|
|
|
innerWindowId = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (aWorkerPrivate->IsFrozen()) {
|
2013-10-23 17:16:49 +04:00
|
|
|
aWorkerPrivate->QueueRunnable(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-19 13:48:31 +03:00
|
|
|
if (aWorkerPrivate->IsServiceWorker() || aWorkerPrivate->IsSharedWorker()) {
|
|
|
|
if (aWorkerPrivate->IsServiceWorker()) {
|
|
|
|
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
MOZ_ASSERT(swm);
|
|
|
|
bool handled = swm->HandleError(aCx, aWorkerPrivate->SharedWorkerName(),
|
|
|
|
aWorkerPrivate->ScriptURL(),
|
|
|
|
mMessage,
|
|
|
|
mFilename, mLine, mLineNumber,
|
|
|
|
mColumnNumber, mFlags);
|
|
|
|
if (handled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
aWorkerPrivate->BroadcastErrorToSharedWorkers(aCx, mMessage, mFilename,
|
|
|
|
mLine, mLineNumber,
|
|
|
|
mColumnNumber, mFlags);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:59:39 +04:00
|
|
|
// The innerWindowId is only required if we are going to ReportError
|
|
|
|
// below, which is gated on this condition. The inner window correctness
|
|
|
|
// check is only going to succeed when the worker is accepting events.
|
|
|
|
if (workerIsAcceptingEvents) {
|
|
|
|
aWorkerPrivate->AssertInnerWindowIsCorrect();
|
2014-12-17 09:26:15 +03:00
|
|
|
innerWindowId = aWorkerPrivate->WindowID();
|
2014-07-11 22:59:39 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-07-11 22:59:39 +04:00
|
|
|
// Don't fire this event if the JS object has been disconnected from the
|
|
|
|
// private object.
|
|
|
|
if (!workerIsAcceptingEvents) {
|
|
|
|
return true;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ReportError(aCx, parent, fireAtScope, aWorkerPrivate, mMessage,
|
|
|
|
mFilename, mLine, mLineNumber, mColumnNumber, mFlags,
|
|
|
|
mErrorNumber, innerWindowId);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TimerRunnable final : public WorkerRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit TimerRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2011-07-17 23:09:13 +04:00
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
return aWorkerPrivate->RunExpiredTimeouts(aCx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-30 14:54:38 +03:00
|
|
|
class DebuggerImmediateRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
nsRefPtr<Function> mHandler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DebuggerImmediateRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Function& aHandler)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
|
|
|
mHandler(&aHandler)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-30 20:18:07 +03:00
|
|
|
IsDebuggerRunnable() const override
|
2015-03-30 14:54:38 +03:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override
|
|
|
|
{
|
|
|
|
// Silence bad assertions.
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
JS::Rooted<JS::Value> callable(aCx, JS::ObjectValue(*mHandler->Callable()));
|
|
|
|
JS::HandleValueArray args = JS::HandleValueArray::empty();
|
|
|
|
JS::Rooted<JS::Value> rval(aCx);
|
|
|
|
if (!JS_CallFunctionValue(aCx, global, callable, args, &rval) &&
|
|
|
|
!JS_ReportPendingException(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
|
|
|
DummyCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
// Nothing!
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TimerThreadEventTarget final : public nsIEventTarget
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-06-23 23:56:07 +04:00
|
|
|
~TimerThreadEventTarget() {}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2011-07-17 23:09:13 +04:00
|
|
|
nsRefPtr<WorkerRunnable> mWorkerRunnable;
|
|
|
|
|
|
|
|
public:
|
2013-10-23 17:16:49 +04:00
|
|
|
TimerThreadEventTarget(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerRunnable* aWorkerRunnable)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mWorkerRunnable(aWorkerRunnable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aWorkerRunnable);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-07-19 06:21:20 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
protected:
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_IMETHOD
|
2015-03-21 19:28:04 +03:00
|
|
|
Dispatch(nsIRunnable* aRunnable, uint32_t aFlags) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// This should only happen on the timer thread.
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aFlags == nsIEventTarget::DISPATCH_NORMAL);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsRefPtr<TimerThreadEventTarget> kungFuDeathGrip = this;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
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
|
2015-03-21 19:28:04 +03:00
|
|
|
IsOnCurrentThread(bool* aIsOnCurrentThread) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aIsOnCurrentThread);
|
|
|
|
|
|
|
|
nsresult rv = mWorkerPrivate->IsOnCurrentThread(aIsOnCurrentThread);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class KillCloseEventRunnable final : public WorkerRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class KillScriptRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit KillScriptRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// Silence bad assertions, this is dispatched from the timer thread.
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2011-07-17 23:09:13 +04:00
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// Silence bad assertions, this is dispatched from the timer thread.
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
// Kill running script.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit KillCloseEventRunnable(WorkerPrivate* aWorkerPrivate)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsRefPtr<TimerThreadEventTarget> target =
|
|
|
|
new TimerThreadEventTarget(mWorkerPrivate, runnable);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~KillCloseEventRunnable()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Don't call Dispatch() on KillCloseEventRunnable!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Don't call Dispatch() on KillCloseEventRunnable!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateRuntimeOptionsRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-02-26 13:25:36 +04:00
|
|
|
JS::RuntimeOptions mRuntimeOptions;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
public:
|
2014-07-30 08:00:37 +04:00
|
|
|
UpdateRuntimeOptionsRunnable(
|
2014-02-26 13:25:36 +04:00
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2014-07-30 08:00:37 +04:00
|
|
|
const JS::RuntimeOptions& aRuntimeOptions)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2014-07-30 08:00:37 +04:00
|
|
|
mRuntimeOptions(aRuntimeOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-07-30 08:00:37 +04:00
|
|
|
aWorkerPrivate->UpdateRuntimeOptionsInternal(aCx, mRuntimeOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdatePreferenceRunnable final : public WorkerControlRunnable
|
2013-11-24 23:27:15 +04:00
|
|
|
{
|
|
|
|
WorkerPreference mPref;
|
|
|
|
bool mValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UpdatePreferenceRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
WorkerPreference aPref,
|
|
|
|
bool aValue)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-11-24 23:27:15 +04:00
|
|
|
mPref(aPref),
|
|
|
|
mValue(aValue)
|
2013-10-23 17:16:49 +04:00
|
|
|
{ }
|
2013-11-24 23:27:15 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-11-24 23:27:15 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->UpdatePreferenceInternal(aCx, mPref, mValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateLanguagesRunnable final : public WorkerRunnable
|
2014-09-05 18:26:34 +04:00
|
|
|
{
|
|
|
|
nsTArray<nsString> mLanguages;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UpdateLanguagesRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsTArray<nsString>& aLanguages)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
|
|
|
|
mLanguages(aLanguages)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-09-05 18:26:34 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->UpdateLanguagesInternal(aCx, mLanguages);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateJSWorkerMemoryParameterRunnable final :
|
2013-10-23 17:16:49 +04:00
|
|
|
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)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-01-11 02:50:40 +04:00
|
|
|
mValue(aValue), mKey(aKey)
|
2012-01-04 23:11:32 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2012-01-04 23:11:32 +04:00
|
|
|
{
|
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
|
2015-03-21 19:28:04 +03:00
|
|
|
class UpdateGCZealRunnable final : public WorkerControlRunnable
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
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)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-05-17 02:49:43 +04:00
|
|
|
mGCZeal(aGCZeal), mFrequency(aFrequency)
|
2011-07-17 23:09:13 +04:00
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-05-17 02:49:43 +04:00
|
|
|
aWorkerPrivate->UpdateGCZealInternal(aCx, mGCZeal, mFrequency);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class GarbageCollectRunnable final : public WorkerControlRunnable
|
2012-01-18 00:05:25 +04:00
|
|
|
{
|
|
|
|
bool mShrinking;
|
|
|
|
bool mCollectChildren;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GarbageCollectRunnable(WorkerPrivate* aWorkerPrivate, bool aShrinking,
|
|
|
|
bool aCollectChildren)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2012-01-18 00:05:25 +04:00
|
|
|
mShrinking(aShrinking), mCollectChildren(aCollectChildren)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2012-01-18 00:05:25 +04:00
|
|
|
{
|
|
|
|
// Silence bad assertions, this can be dispatched from either the main
|
|
|
|
// thread or the timer thread..
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual void
|
2012-01-18 00:05:25 +04:00
|
|
|
PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2012-01-18 00:05:25 +04:00
|
|
|
{
|
|
|
|
// Silence bad assertions, this can be dispatched from either the main
|
|
|
|
// thread or the timer thread..
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2012-01-18 00:05:25 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->GarbageCollectInternal(aCx, mShrinking, mCollectChildren);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
class CycleCollectRunnable : public WorkerControlRunnable
|
|
|
|
{
|
|
|
|
bool mCollectChildren;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CycleCollectRunnable(WorkerPrivate* aWorkerPrivate, bool aCollectChildren)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-12-03 08:07:02 +04:00
|
|
|
mCollectChildren(aCollectChildren)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->CycleCollectInternal(aCx, mCollectChildren);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
class OfflineStatusChangeRunnable : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OfflineStatusChangeRunnable(WorkerPrivate* aWorkerPrivate, bool aIsOffline)
|
2014-02-02 03:30:55 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
|
2013-11-20 03:08:50 +04:00
|
|
|
mIsOffline(aIsOffline)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->OfflineStatusChangeEventInternal(aCx, mIsOffline);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mIsOffline;
|
|
|
|
};
|
|
|
|
|
2015-01-23 08:26:21 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
static bool
|
|
|
|
StartsWithExplicit(nsACString& s)
|
|
|
|
{
|
|
|
|
return StringBeginsWith(s, NS_LITERAL_CSTRING("explicit/"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit WorkerJSRuntimeStats(const nsACString& aRtPath)
|
2012-12-30 22:21:52 +04:00
|
|
|
: 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)
|
2015-03-21 19:28:04 +03:00
|
|
|
override
|
2013-03-17 07:36:37 +04:00
|
|
|
{
|
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);
|
2015-01-23 08:26:21 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(StartsWithExplicit(extras->pathPrefix));
|
|
|
|
|
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)
|
2015-03-21 19:28:04 +03:00
|
|
|
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/?!/");
|
|
|
|
|
2015-01-23 08:26:21 +03:00
|
|
|
MOZ_ASSERT(StartsWithExplicit(extras->jsPathPrefix));
|
|
|
|
MOZ_ASSERT(StartsWithExplicit(extras->domPathPrefix));
|
|
|
|
|
2014-05-21 10:06:54 +04:00
|
|
|
extras->location = nullptr;
|
|
|
|
|
2013-04-04 19:55:56 +04:00
|
|
|
aCompartmentStats->extra = extras;
|
2012-07-13 10:51:01 +04:00
|
|
|
}
|
|
|
|
};
|
2012-12-08 04:35:44 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MessagePortRunnable final : public WorkerRunnable
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
uint64_t mMessagePortSerial;
|
|
|
|
bool mConnect;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MessagePortRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
bool aConnect)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, aConnect ?
|
|
|
|
WorkerThreadModifyBusyCount :
|
|
|
|
WorkerThreadUnchangedBusyCount),
|
2013-06-05 18:04:23 +04:00
|
|
|
mMessagePortSerial(aMessagePortSerial), mConnect(aConnect)
|
|
|
|
{ }
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
private:
|
|
|
|
~MessagePortRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
if (mConnect) {
|
|
|
|
return aWorkerPrivate->ConnectMessagePort(aCx, mMessagePortSerial);
|
|
|
|
}
|
|
|
|
|
|
|
|
aWorkerPrivate->DisconnectMessagePort(mMessagePortSerial);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class DummyRunnable final
|
2014-12-17 09:26:15 +03:00
|
|
|
: public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit
|
|
|
|
DummyRunnable(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~DummyRunnable()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-12-17 09:26:15 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Should never call Dispatch on this!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
PostDispatch(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool aDispatchResult) override
|
2014-12-17 09:26:15 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Should never call Dispatch on this!");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
2014-12-17 09:26:15 +03:00
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
PRThread*
|
|
|
|
PRThreadFromThread(nsIThread* aThread)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
PRThread* result;
|
2014-12-11 08:42:07 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(aThread->GetPRThread(&result)));
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(result);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
} /* anonymous namespace */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(MainThreadReleaseRunnable, nsRunnable)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(TopLevelWorkerFinishedRunnable, nsRunnable)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TimerThreadEventTarget, nsIEventTarget)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo::WorkerLoadInfo()
|
2014-12-17 09:26:15 +03:00
|
|
|
: mWindowID(UINT64_MAX)
|
2015-05-14 22:41:42 +03:00
|
|
|
, mServiceWorkerID(0)
|
2014-12-17 09:26:15 +03:00
|
|
|
, mFromWindow(false)
|
|
|
|
, mEvalAllowed(false)
|
|
|
|
, mReportCSPViolations(false)
|
|
|
|
, mXHRParamsAllowed(false)
|
|
|
|
, mPrincipalIsSystem(false)
|
|
|
|
, mIsInPrivilegedApp(false)
|
|
|
|
, mIsInCertifiedApp(false)
|
|
|
|
, mIndexedDBAllowed(false)
|
|
|
|
{
|
2015-02-12 12:50:05 +03:00
|
|
|
MOZ_COUNT_CTOR(WorkerLoadInfo);
|
2014-12-17 09:26:15 +03:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo::~WorkerLoadInfo()
|
2015-02-12 12:50:05 +03:00
|
|
|
{
|
2015-02-12 12:50:05 +03:00
|
|
|
MOZ_COUNT_DTOR(WorkerLoadInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerLoadInfo::StealFrom(WorkerLoadInfo& aOther)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mBaseURI);
|
|
|
|
aOther.mBaseURI.swap(mBaseURI);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mResolvedScriptURI);
|
|
|
|
aOther.mResolvedScriptURI.swap(mResolvedScriptURI);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mPrincipal);
|
|
|
|
aOther.mPrincipal.swap(mPrincipal);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mScriptContext);
|
|
|
|
aOther.mScriptContext.swap(mScriptContext);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mWindow);
|
|
|
|
aOther.mWindow.swap(mWindow);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mCSP);
|
|
|
|
aOther.mCSP.swap(mCSP);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mChannel);
|
|
|
|
aOther.mChannel.swap(mChannel);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mLoadGroup);
|
|
|
|
aOther.mLoadGroup.swap(mLoadGroup);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mInterfaceRequestor);
|
|
|
|
aOther.mInterfaceRequestor.swap(mInterfaceRequestor);
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mPrincipalInfo);
|
|
|
|
mPrincipalInfo = aOther.mPrincipalInfo.forget();
|
|
|
|
|
|
|
|
mDomain = aOther.mDomain;
|
2015-03-19 21:41:42 +03:00
|
|
|
mServiceWorkerCacheName = aOther.mServiceWorkerCacheName;
|
2015-02-12 12:50:05 +03:00
|
|
|
mWindowID = aOther.mWindowID;
|
2015-05-14 22:41:42 +03:00
|
|
|
mServiceWorkerID = aOther.mServiceWorkerID;
|
2015-02-12 12:50:05 +03:00
|
|
|
mFromWindow = aOther.mFromWindow;
|
|
|
|
mEvalAllowed = aOther.mEvalAllowed;
|
|
|
|
mReportCSPViolations = aOther.mReportCSPViolations;
|
|
|
|
mXHRParamsAllowed = aOther.mXHRParamsAllowed;
|
|
|
|
mPrincipalIsSystem = aOther.mPrincipalIsSystem;
|
|
|
|
mIsInPrivilegedApp = aOther.mIsInPrivilegedApp;
|
|
|
|
mIsInCertifiedApp = aOther.mIsInCertifiedApp;
|
|
|
|
mIndexedDBAllowed = aOther.mIndexedDBAllowed;
|
2015-03-03 21:13:23 +03:00
|
|
|
}
|
2015-02-12 12:50:05 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
template <class Derived>
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerPrivateParent<Derived>::EventTarget final
|
2015-02-12 12:50:05 +03:00
|
|
|
: public nsIEventTarget
|
|
|
|
{
|
|
|
|
// This mutex protects mWorkerPrivate and must be acquired *before* the
|
|
|
|
// WorkerPrivate's mutex whenever they must both be held.
|
|
|
|
mozilla::Mutex mMutex;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsIEventTarget* mWeakNestedEventTarget;
|
|
|
|
nsCOMPtr<nsIEventTarget> mNestedEventTarget;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit EventTarget(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mMutex("WorkerPrivateParent::EventTarget::mMutex"),
|
|
|
|
mWorkerPrivate(aWorkerPrivate), mWeakNestedEventTarget(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventTarget(WorkerPrivate* aWorkerPrivate, nsIEventTarget* aNestedEventTarget)
|
|
|
|
: mMutex("WorkerPrivateParent::EventTarget::mMutex"),
|
|
|
|
mWorkerPrivate(aWorkerPrivate), mWeakNestedEventTarget(aNestedEventTarget),
|
|
|
|
mNestedEventTarget(aNestedEventTarget)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aNestedEventTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Disable()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEventTarget> nestedEventTarget;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
mNestedEventTarget.swap(nestedEventTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIEventTarget*
|
|
|
|
GetWeakNestedEventTarget() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWeakNestedEventTarget);
|
|
|
|
return mWeakNestedEventTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
|
|
|
|
private:
|
|
|
|
~EventTarget()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
WorkerLoadInfo::
|
|
|
|
InterfaceRequestor::InterfaceRequestor(nsIPrincipal* aPrincipal,
|
|
|
|
nsILoadGroup* aLoadGroup)
|
2015-02-21 18:09:17 +03:00
|
|
|
{
|
2015-02-12 12:50:05 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
// Look for an existing LoadContext. This is optional and it's ok if
|
|
|
|
// we don't find one.
|
|
|
|
nsCOMPtr<nsILoadContext> baseContext;
|
|
|
|
if (aLoadGroup) {
|
2015-02-21 18:09:17 +03:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
2015-02-12 12:50:05 +03:00
|
|
|
if (callbacks) {
|
|
|
|
callbacks->GetInterface(NS_GET_IID(nsILoadContext),
|
|
|
|
getter_AddRefs(baseContext));
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
2015-03-29 20:43:34 +03:00
|
|
|
mOuterRequestor = callbacks;
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
mLoadContext = new LoadContext(aPrincipal, baseContext);
|
|
|
|
}
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
void
|
|
|
|
WorkerLoadInfo::
|
|
|
|
InterfaceRequestor::MaybeAddTabChild(nsILoadGroup* aLoadGroup)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
if (!aLoadGroup) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
if (!callbacks) {
|
|
|
|
return;
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
nsCOMPtr<nsITabChild> tabChild;
|
|
|
|
callbacks->GetInterface(NS_GET_IID(nsITabChild), getter_AddRefs(tabChild));
|
|
|
|
if (!tabChild) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
// Use weak references to the tab child. Holding a strong reference will
|
|
|
|
// not prevent an ActorDestroy() from being called on the TabChild.
|
|
|
|
// Therefore, we should let the TabChild destroy itself as soon as possible.
|
|
|
|
mTabChildList.AppendElement(do_GetWeakReference(tabChild));
|
|
|
|
}
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerLoadInfo::
|
|
|
|
InterfaceRequestor::GetInterface(const nsIID& aIID, void** aSink)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mLoadContext);
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsILoadContext))) {
|
|
|
|
nsCOMPtr<nsILoadContext> ref = mLoadContext;
|
|
|
|
ref.forget(aSink);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-02-21 18:09:17 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
// If we still have an active nsITabChild, then return it. Its possible,
|
|
|
|
// though, that all of the TabChild objects have been destroyed. In that
|
|
|
|
// case we return NS_NOINTERFACE.
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsITabChild))) {
|
|
|
|
nsCOMPtr<nsITabChild> tabChild = GetAnyLiveTabChild();
|
|
|
|
if (!tabChild) {
|
|
|
|
return NS_NOINTERFACE;
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
2015-02-12 12:50:05 +03:00
|
|
|
tabChild.forget(aSink);
|
|
|
|
return NS_OK;
|
2015-02-21 18:09:17 +03:00
|
|
|
}
|
|
|
|
|
2015-03-29 20:43:34 +03:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsINetworkInterceptController)) &&
|
|
|
|
mOuterRequestor) {
|
|
|
|
// If asked for the network intercept controller, ask the outer requestor,
|
|
|
|
// which could be the docshell.
|
|
|
|
return mOuterRequestor->GetInterface(aIID, aSink);
|
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
2015-03-03 21:13:23 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
already_AddRefed<nsITabChild>
|
|
|
|
WorkerLoadInfo::
|
|
|
|
InterfaceRequestor::GetAnyLiveTabChild()
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
2015-02-12 12:50:05 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
// Search our list of known TabChild objects for one that still exists.
|
|
|
|
while (!mTabChildList.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsITabChild> tabChild =
|
|
|
|
do_QueryReferent(mTabChildList.LastElement());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
// Does this tab child still exist? If so, return it. We are done.
|
|
|
|
if (tabChild) {
|
|
|
|
return tabChild.forget();
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
// Otherwise remove the stale weak reference and check the next one
|
|
|
|
mTabChildList.RemoveElementAt(mTabChildList.Length() - 1);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
NS_IMPL_ADDREF(WorkerLoadInfo::InterfaceRequestor)
|
|
|
|
NS_IMPL_RELEASE(WorkerLoadInfo::InterfaceRequestor)
|
|
|
|
NS_IMPL_QUERY_INTERFACE(WorkerLoadInfo::InterfaceRequestor, nsIInterfaceRequestor)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
struct WorkerPrivate::TimeoutInfo
|
|
|
|
{
|
|
|
|
TimeoutInfo()
|
2013-11-05 18:16:26 +04:00
|
|
|
: mTimeoutCallable(JS::UndefinedValue()), mLineNumber(0), mId(0),
|
|
|
|
mIsInterval(false), mCanceled(false)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
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-11-05 18:16:26 +04:00
|
|
|
JS::Heap<JS::Value> mTimeoutCallable;
|
|
|
|
nsString mTimeoutString;
|
2013-07-23 13:58:27 +04:00
|
|
|
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;
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t mId;
|
2011-07-17 23:09:13 +04:00
|
|
|
bool mIsInterval;
|
|
|
|
bool mCanceled;
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class WorkerPrivate::MemoryReporter final : public nsIMemoryReporter
|
2013-02-08 15:50:00 +04:00
|
|
|
{
|
2013-12-08 09:39:47 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2013-02-08 15:50:00 +04:00
|
|
|
friend class WorkerPrivate;
|
|
|
|
|
|
|
|
SharedMutex mMutex;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
2013-06-15 06:48:28 +04:00
|
|
|
bool mAlreadyMappedToAddon;
|
2013-02-08 15:50:00 +04:00
|
|
|
|
|
|
|
public:
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit MemoryReporter(WorkerPrivate* aWorkerPrivate)
|
2013-12-04 08:01:24 +04:00
|
|
|
: mMutex(aWorkerPrivate->mMutex), mWorkerPrivate(aWorkerPrivate),
|
2013-06-15 06:48:28 +04:00
|
|
|
mAlreadyMappedToAddon(false)
|
2013-02-08 15:50:00 +04:00
|
|
|
{
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2013-08-28 03:24:51 +04:00
|
|
|
CollectReports(nsIMemoryReporterCallback* aCallback,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsISupports* aClosure, bool aAnonymize) override
|
2013-02-08 15:50:00 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-05-21 10:06:54 +04:00
|
|
|
// Assumes that WorkerJSRuntimeStats will hold a reference to |path|, and
|
2015-01-23 08:26:21 +03:00
|
|
|
// not a copy, as TryToMapAddon() may later modify it.
|
2014-05-21 10:06:54 +04:00
|
|
|
nsCString path;
|
|
|
|
WorkerJSRuntimeStats rtStats(path);
|
2013-02-08 15:50:00 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2015-01-23 08:26:21 +03:00
|
|
|
if (!mWorkerPrivate) {
|
2015-01-16 07:12:20 +03:00
|
|
|
// Returning NS_OK here will effectively report 0 memory.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:06:54 +04:00
|
|
|
path.AppendLiteral("explicit/workers/workers(");
|
|
|
|
if (aAnonymize && !mWorkerPrivate->Domain().IsEmpty()) {
|
|
|
|
path.AppendLiteral("<anonymized-domain>)/worker(<anonymized-url>");
|
|
|
|
} else {
|
|
|
|
nsCString escapedDomain(mWorkerPrivate->Domain());
|
|
|
|
if (escapedDomain.IsEmpty()) {
|
|
|
|
escapedDomain += "chrome";
|
|
|
|
} else {
|
|
|
|
escapedDomain.ReplaceChar('/', '\\');
|
|
|
|
}
|
|
|
|
path.Append(escapedDomain);
|
|
|
|
path.AppendLiteral(")/worker(");
|
|
|
|
NS_ConvertUTF16toUTF8 escapedURL(mWorkerPrivate->ScriptURL());
|
|
|
|
escapedURL.ReplaceChar('/', '\\');
|
|
|
|
path.Append(escapedURL);
|
|
|
|
}
|
|
|
|
path.AppendPrintf(", 0x%p)/", static_cast<void*>(mWorkerPrivate));
|
|
|
|
|
|
|
|
TryToMapAddon(path);
|
2015-01-23 08:26:21 +03:00
|
|
|
|
|
|
|
if (!mWorkerPrivate->BlockAndCollectRuntimeStats(&rtStats, aAnonymize)) {
|
|
|
|
// Returning NS_OK here will effectively report 0 memory.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-02-08 15:50:00 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 10:06:54 +04:00
|
|
|
return xpc::ReportJSRuntimeExplicitTreeStats(rtStats, path,
|
|
|
|
aCallback, aClosure,
|
|
|
|
aAnonymize);
|
2013-02-08 15:50:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-05-21 10:06:54 +04:00
|
|
|
TryToMapAddon(nsACString &path)
|
2013-06-15 06:48:28 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (mAlreadyMappedToAddon || !mWorkerPrivate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> scriptURI;
|
|
|
|
if (NS_FAILED(NS_NewURI(getter_AddRefs(scriptURI),
|
|
|
|
mWorkerPrivate->ScriptURL()))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAlreadyMappedToAddon = true;
|
|
|
|
|
2013-10-28 08:53:00 +04:00
|
|
|
if (XRE_GetProcessType() != GeckoProcessType_Default) {
|
|
|
|
// Only try to access the service from the main process.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:48:28 +04:00
|
|
|
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 += "/";
|
2014-05-21 10:06:54 +04:00
|
|
|
path.Insert(addonId, explicitLength);
|
2013-06-15 06:48:28 +04:00
|
|
|
}
|
2013-02-08 15:50:00 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(WorkerPrivate::MemoryReporter, nsIMemoryReporter)
|
2013-12-08 09:39:47 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::SyncLoopInfo::SyncLoopInfo(EventTarget* aEventTarget)
|
|
|
|
: mEventTarget(aEventTarget), mCompleted(false), mResult(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
, mHasRun(false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
struct WorkerPrivate::PreemptingRunnableInfo final
|
2014-12-17 09:26:15 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
uint32_t mRecursionDepth;
|
|
|
|
|
|
|
|
PreemptingRunnableInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(WorkerPrivate::PreemptingRunnableInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PreemptingRunnableInfo()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(WorkerPrivate::PreemptingRunnableInfo);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-07 19:19:19 +03:00
|
|
|
template <class Derived>
|
|
|
|
nsIDocument*
|
|
|
|
WorkerPrivateParent<Derived>::GetDocument() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
if (mLoadInfo.mWindow) {
|
|
|
|
return mLoadInfo.mWindow->GetExtantDoc();
|
|
|
|
}
|
|
|
|
// if we don't have a document, we should query the document
|
|
|
|
// from the parent in case of a nested worker
|
|
|
|
WorkerPrivate* parent = mParent;
|
|
|
|
while (parent) {
|
|
|
|
if (parent->mLoadInfo.mWindow) {
|
|
|
|
return parent->mLoadInfo.mWindow->GetExtantDoc();
|
|
|
|
}
|
|
|
|
parent = parent->GetParent();
|
|
|
|
}
|
|
|
|
// couldn't query a document, give up and return nullptr
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Can't use NS_IMPL_CYCLE_COLLECTION_CLASS(WorkerPrivateParent) because of the
|
|
|
|
// templates.
|
|
|
|
template <class Derived>
|
|
|
|
typename WorkerPrivateParent<Derived>::cycleCollection
|
|
|
|
WorkerPrivateParent<Derived>::_cycleCollectorGlobal =
|
|
|
|
WorkerPrivateParent<Derived>::cycleCollection();
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
template <class Derived>
|
|
|
|
WorkerPrivateParent<Derived>::WorkerPrivateParent(
|
2014-02-06 18:39:10 +04:00
|
|
|
JSContext* aCx,
|
|
|
|
WorkerPrivate* aParent,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker,
|
|
|
|
WorkerType aWorkerType,
|
|
|
|
const nsACString& aSharedWorkerName,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo& aLoadInfo)
|
2013-11-05 18:16:24 +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-11-05 18:16:24 +04:00
|
|
|
mParent(aParent), mScriptURL(aScriptURL),
|
2015-03-19 21:41:42 +03:00
|
|
|
mSharedWorkerName(aSharedWorkerName), mLoadingWorkerScript(false),
|
|
|
|
mBusyCount(0), mMessagePortSerial(0),
|
2015-04-01 12:00:19 +03:00
|
|
|
mParentStatus(Pending), mParentFrozen(false),
|
2013-06-05 18:04:23 +04:00
|
|
|
mIsChromeWorker(aIsChromeWorker), mMainThreadObjectsForgotten(false),
|
2014-05-06 13:50:12 +04:00
|
|
|
mWorkerType(aWorkerType),
|
2015-05-25 19:53:07 +03:00
|
|
|
mCreationTimeStamp(TimeStamp::Now()),
|
|
|
|
mCreationTimeHighRes((double)PR_Now() / PR_USEC_PER_MSEC)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT_IF(!IsDedicatedWorker(),
|
|
|
|
!aSharedWorkerName.IsVoid() && NS_IsMainThread());
|
|
|
|
MOZ_ASSERT_IF(IsDedicatedWorker(), aSharedWorkerName.IsEmpty());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
if (aLoadInfo.mWindow) {
|
2013-11-05 18:16:24 +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);
|
2014-08-30 03:50:06 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(IsDedicatedWorker());
|
|
|
|
mNowBaseTimeStamp = aParent->NowBaseTimeStamp();
|
2015-05-25 19:53:07 +03:00
|
|
|
mNowBaseTimeHighRes = aParent->NowBaseTimeHighRes();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
RuntimeService::GetDefaultJSSettings(mJSSettings);
|
2014-08-30 03:50:06 +04:00
|
|
|
|
2014-08-30 08:56:35 +04:00
|
|
|
if (IsDedicatedWorker() && mLoadInfo.mWindow &&
|
|
|
|
mLoadInfo.mWindow->GetPerformance()) {
|
|
|
|
mNowBaseTimeStamp = mLoadInfo.mWindow->GetPerformance()->GetDOMTiming()->
|
2014-08-30 03:50:06 +04:00
|
|
|
GetNavigationStartTimeStamp();
|
2015-05-25 19:53:07 +03:00
|
|
|
mNowBaseTimeHighRes = mLoadInfo.mWindow->GetPerformance()->GetDOMTiming()->
|
|
|
|
GetNavigationStartHighRes();
|
2014-08-30 03:50:06 +04:00
|
|
|
} else {
|
|
|
|
mNowBaseTimeStamp = CreationTimeStamp();
|
2015-05-25 19:53:07 +03:00
|
|
|
mNowBaseTimeHighRes = CreationTimeHighRes();
|
2014-08-30 03:50:06 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-14 15:58:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-14 22:38:54 +04:00
|
|
|
WorkerPrivateParent<Derived>::~WorkerPrivateParent()
|
2013-10-14 15:58:05 +04:00
|
|
|
{
|
2013-11-05 18:16:24 +04:00
|
|
|
DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-23 17:16:49 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
WorkerPrivateParent<Derived>::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsSharedWorker(),
|
|
|
|
"We should never wrap a WorkerPrivate for a SharedWorker");
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertIsOnParentThread();
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2014-03-06 05:58:03 +04:00
|
|
|
// XXXkhuey this should not need to be rooted, the analysis is dumb.
|
|
|
|
// See bug 980181.
|
|
|
|
JS::Rooted<JSObject*> wrapper(aCx,
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
WorkerBinding::Wrap(aCx, ParentAsWorkerPrivate(), aGivenProto));
|
2014-03-06 05:58:03 +04:00
|
|
|
if (wrapper) {
|
|
|
|
MOZ_ALWAYS_TRUE(TryPreserveWrapper(wrapper));
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-23 17:16:49 +04:00
|
|
|
nsresult
|
|
|
|
WorkerPrivateParent<Derived>::DispatchPrivate(WorkerRunnable* aRunnable,
|
|
|
|
nsIEventTarget* aSyncLoopTarget)
|
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
MOZ_ASSERT_IF(aSyncLoopTarget, self->mThread);
|
|
|
|
|
|
|
|
if (!self->mThread) {
|
|
|
|
if (ParentStatus() == Pending || self->mStatus == Pending) {
|
|
|
|
mPreStartRunnables.AppendElement(aRunnable);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_WARNING("Using a worker event target after the thread has already"
|
|
|
|
"been released!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mStatus == Dead ||
|
|
|
|
(!aSyncLoopTarget && ParentStatus() > Running)) {
|
|
|
|
NS_WARNING("A runnable was posted to a worker that is already shutting "
|
|
|
|
"down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
nsresult rv;
|
2013-10-23 17:16:49 +04:00
|
|
|
if (aSyncLoopTarget) {
|
2014-11-17 22:55:37 +03:00
|
|
|
rv = aSyncLoopTarget->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
rv = self->mThread->Dispatch(WorkerThreadFriendKey(), aRunnable);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::EnableDebugger()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
|
|
|
|
|
|
|
MOZ_ASSERT(!self->mDebugger);
|
|
|
|
self->mDebugger = new WorkerDebugger(self);
|
|
|
|
|
|
|
|
if (NS_FAILED(RegisterWorkerDebugger(self->mDebugger))) {
|
|
|
|
NS_WARNING("Failed to register worker debugger!");
|
|
|
|
self->mDebugger = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::DisableDebugger()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
|
|
|
|
|
|
|
if (!self->mDebugger) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(UnregisterWorkerDebugger(self->mDebugger))) {
|
|
|
|
NS_WARNING("Failed to unregister worker debugger!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
template <class Derived>
|
2013-10-23 17:16:49 +04:00
|
|
|
nsresult
|
|
|
|
WorkerPrivateParent<Derived>::DispatchControlRunnable(
|
|
|
|
WorkerControlRunnable* aWorkerControlRunnable)
|
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
|
|
|
|
MOZ_ASSERT(aWorkerControlRunnable);
|
|
|
|
|
|
|
|
nsRefPtr<WorkerControlRunnable> runnable = aWorkerControlRunnable;
|
|
|
|
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (self->mStatus == Dead) {
|
|
|
|
NS_WARNING("A control runnable was posted to a worker that is already "
|
|
|
|
"shutting down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer ownership to the control queue.
|
2014-03-15 23:00:15 +04:00
|
|
|
self->mControlQueue.Push(runnable.forget().take());
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (JSContext* cx = self->mJSContext) {
|
|
|
|
MOZ_ASSERT(self->mThread);
|
|
|
|
|
|
|
|
JSRuntime* rt = JS_GetRuntime(cx);
|
|
|
|
MOZ_ASSERT(rt);
|
|
|
|
|
2014-03-11 01:28:43 +04:00
|
|
|
JS_RequestInterruptCallback(rt);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
template <class Derived>
|
|
|
|
nsresult
|
|
|
|
WorkerPrivateParent<Derived>::DispatchDebuggerRunnable(
|
|
|
|
WorkerRunnable *aDebuggerRunnable)
|
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
|
|
|
|
MOZ_ASSERT(aDebuggerRunnable);
|
|
|
|
|
|
|
|
nsRefPtr<WorkerRunnable> runnable = aDebuggerRunnable;
|
|
|
|
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (self->mStatus == Dead) {
|
|
|
|
NS_WARNING("A debugger runnable was posted to a worker that is already "
|
|
|
|
"shutting down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer ownership to the debugger queue.
|
|
|
|
self->mDebuggerQueue.Push(runnable.forget().take());
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
template <class Derived>
|
2013-10-23 17:16:49 +04:00
|
|
|
already_AddRefed<WorkerRunnable>
|
|
|
|
WorkerPrivateParent<Derived>::MaybeWrapAsWorkerRunnable(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
// May be called on any thread!
|
|
|
|
|
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
|
|
|
|
nsRefPtr<WorkerRunnable> workerRunnable =
|
|
|
|
WorkerRunnable::FromRunnable(aRunnable);
|
|
|
|
if (workerRunnable) {
|
|
|
|
return workerRunnable.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICancelableRunnable> cancelable = do_QueryInterface(aRunnable);
|
|
|
|
if (!cancelable) {
|
|
|
|
MOZ_CRASH("All runnables destined for a worker thread must be cancelable!");
|
|
|
|
}
|
|
|
|
|
|
|
|
workerRunnable =
|
|
|
|
new ExternalRunnableWrapper(ParentAsWorkerPrivate(), cancelable);
|
|
|
|
return workerRunnable.forget();
|
|
|
|
}
|
2013-11-05 18:16:24 +04:00
|
|
|
|
|
|
|
template <class Derived>
|
2013-10-23 17:16:49 +04:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
WorkerPrivateParent<Derived>::GetEventTarget()
|
2013-11-05 18:16:24 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> target;
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (!mEventTarget &&
|
|
|
|
ParentStatus() <= Running &&
|
|
|
|
self->mStatus <= Running) {
|
|
|
|
mEventTarget = new EventTarget(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
target = mEventTarget;
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_WARN_IF_FALSE(target,
|
|
|
|
"Requested event target for a worker that is already "
|
|
|
|
"shutting down!");
|
|
|
|
|
|
|
|
return target.forget();
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:11:15 +04:00
|
|
|
if (IsSharedWorker() || IsServiceWorker()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
RuntimeService* runtime = RuntimeService::GetService();
|
|
|
|
MOZ_ASSERT(runtime);
|
|
|
|
|
|
|
|
runtime->ForgetSharedWorker(ParentAsWorkerPrivate());
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (pending) {
|
|
|
|
WorkerPrivate* self = ParentAsWorkerPrivate();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// Fake a thread here just so that our assertions don't go off for no
|
|
|
|
// reason.
|
2011-07-17 23:09:13 +04:00
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(currentThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!self->mPRThread);
|
|
|
|
self->mPRThread = PRThreadFromThread(currentThread);
|
|
|
|
MOZ_ASSERT(self->mPRThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Worker never got a chance to run, go ahead and delete it.
|
2014-03-05 03:09:23 +04:00
|
|
|
self->ScheduleDeletion(WorkerPrivate::WorkerNeverRan);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-04-01 12:00:19 +03:00
|
|
|
WorkerPrivateParent<Derived>::Freeze(JSContext* aCx, nsPIDOMWindow* aWindow)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
// Shared workers are only frozen if all of their owning documents are
|
|
|
|
// frozen. It can happen that mSharedWorkers is empty but this thread has
|
2014-09-30 14:43:19 +04:00
|
|
|
// not been unregistered yet.
|
|
|
|
if ((IsSharedWorker() || IsServiceWorker()) && mSharedWorkers.Count()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
2015-04-01 12:00:19 +03:00
|
|
|
bool mAllFrozen;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit Closure(nsPIDOMWindow* aWindow)
|
2015-04-01 12:00:19 +03:00
|
|
|
: mWindow(aWindow), mAllFrozen(true)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
// aWindow may be null here.
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2015-04-01 12:00:19 +03:00
|
|
|
Freeze(const uint64_t& aKey,
|
2013-06-05 18:04:23 +04:00
|
|
|
SharedWorker* aSharedWorker,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto closure = static_cast<Closure*>(aClosure);
|
|
|
|
|
|
|
|
if (closure->mWindow && aSharedWorker->GetOwner() == closure->mWindow) {
|
2015-04-01 12:00:19 +03:00
|
|
|
// Calling Freeze() may change the refcount, ensure that the worker
|
2013-06-05 18:04:23 +04:00
|
|
|
// outlives this call.
|
|
|
|
nsRefPtr<SharedWorker> kungFuDeathGrip = aSharedWorker;
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
aSharedWorker->Freeze();
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_IF(aSharedWorker->GetOwner() && closure->mWindow,
|
|
|
|
!SameCOMIdentity(aSharedWorker->GetOwner(),
|
|
|
|
closure->mWindow));
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!aSharedWorker->IsFrozen()) {
|
|
|
|
closure->mAllFrozen = false;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Closure closure(aWindow);
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mSharedWorkers.EnumerateRead(Closure::Freeze, &closure);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!closure.mAllFrozen || mParentFrozen) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mParentFrozen = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mParentStatus >= Terminating) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
nsRefPtr<FreezeRunnable> runnable =
|
|
|
|
new FreezeRunnable(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
|
2015-04-01 12:00:19 +03:00
|
|
|
WorkerPrivateParent<Derived>::Thaw(JSContext* aCx, nsPIDOMWindow* aWindow)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (IsDedicatedWorker() && !mParentFrozen) {
|
|
|
|
// If we are in here, it means that this worker has been created when the
|
|
|
|
// parent was actually suspended (maybe during a sync XHR), and in this case
|
|
|
|
// we don't need to thaw.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shared workers are resumed if any of their owning documents are thawed.
|
2014-09-30 14:43:19 +04:00
|
|
|
// It can happen that mSharedWorkers is empty but this thread has not been
|
|
|
|
// unregistered yet.
|
|
|
|
if ((IsSharedWorker() || IsServiceWorker()) && mSharedWorkers.Count()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
bool mAnyRunning;
|
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit Closure(nsPIDOMWindow* aWindow)
|
2013-06-05 18:04:23 +04:00
|
|
|
: mWindow(aWindow), mAnyRunning(false)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
// aWindow may be null here.
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2015-04-01 12:00:19 +03:00
|
|
|
Thaw(const uint64_t& aKey,
|
2013-06-05 18:04:23 +04:00
|
|
|
SharedWorker* aSharedWorker,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
|
|
|
|
auto closure = static_cast<Closure*>(aClosure);
|
|
|
|
|
|
|
|
if (closure->mWindow && aSharedWorker->GetOwner() == closure->mWindow) {
|
2015-04-01 12:00:19 +03:00
|
|
|
// Calling Thaw() may change the refcount, ensure that the worker
|
2013-06-05 18:04:23 +04:00
|
|
|
// outlives this call.
|
|
|
|
nsRefPtr<SharedWorker> kungFuDeathGrip = aSharedWorker;
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
aSharedWorker->Thaw();
|
2013-06-05 18:04:23 +04:00
|
|
|
closure->mAnyRunning = true;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_IF(aSharedWorker->GetOwner() && closure->mWindow,
|
|
|
|
!SameCOMIdentity(aSharedWorker->GetOwner(),
|
|
|
|
closure->mWindow));
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!aSharedWorker->IsFrozen()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
closure->mAnyRunning = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Closure closure(aWindow);
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mSharedWorkers.EnumerateRead(Closure::Thaw, &closure);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!closure.mAnyRunning || !mParentFrozen) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
MOZ_ASSERT(mParentFrozen);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mParentFrozen = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
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-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-10-31 03:40:16 +04:00
|
|
|
MOZ_ASSERT(IsDedicatedWorker());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIRunnable>> runnables;
|
2011-07-17 23:09:13 +04:00
|
|
|
mQueuedRunnables.SwapElements(runnables);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index = 0; index < runnables.Length(); index++) {
|
2013-10-23 17:16:49 +04:00
|
|
|
runnables[index]->Run();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
nsRefPtr<ThawRunnable> runnable =
|
|
|
|
new ThawRunnable(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
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
bool
|
2015-04-15 05:35:01 +03:00
|
|
|
WorkerPrivateParent<Derived>::Close()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
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) {
|
2014-02-02 22:08:50 +04:00
|
|
|
mBusyCount++;
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
if (--mBusyCount == 0) {
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
bool shouldCancel;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
shouldCancel = mParentStatus == Terminating;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldCancel && !Cancel(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::ForgetOverridenLoadGroup(
|
|
|
|
nsCOMPtr<nsILoadGroup>& aLoadGroupOut)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
// If we're not overriden, then do nothing here. Let the load group get
|
|
|
|
// handled in ForgetMainThreadObjects().
|
|
|
|
if (!mLoadInfo.mInterfaceRequestor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoadInfo.mLoadGroup.swap(aLoadGroupOut);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
static const uint32_t kDoomedCount = 9;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
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);
|
2014-12-12 19:06:00 +03:00
|
|
|
SwapToISupportsArray(mLoadInfo.mLoadGroup, aDoomed);
|
2015-02-21 18:09:17 +03:00
|
|
|
SwapToISupportsArray(mLoadInfo.mInterfaceRequestor, aDoomed);
|
2013-06-05 18:04:23 +04:00
|
|
|
// 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-11-05 18:16:24 +04:00
|
|
|
void
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivateParent<Derived>::PostMessageInternal(
|
|
|
|
JSContext* aCx,
|
2013-11-05 18:16:24 +04:00
|
|
|
JS::Handle<JS::Value> aMessage,
|
2015-03-16 23:23:42 +03:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
2013-06-05 18:04:23 +04:00
|
|
|
bool aToMessagePort,
|
2013-11-05 18:16:24 +04:00
|
|
|
uint64_t aMessagePortSerial,
|
2015-03-16 23:23:42 +03:00
|
|
|
ServiceWorkerClientInfo* aClientInfo,
|
2013-11-05 18:16:24 +04:00
|
|
|
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-11-05 18:16:24 +04:00
|
|
|
return;
|
2012-03-31 08:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const JSStructuredCloneCallbacks* callbacks;
|
2011-07-17 23:09:13 +04:00
|
|
|
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-11-05 18:16:24 +04:00
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
2014-02-12 14:50:46 +04:00
|
|
|
|
|
|
|
// The input sequence only comes from the generated bindings code, which
|
|
|
|
// ensures it is rooted.
|
|
|
|
JS::HandleValueArray elements =
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(realTransferable.Length(),
|
|
|
|
realTransferable.Elements());
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
JSObject* array =
|
2014-02-12 14:50:46 +04:00
|
|
|
JS_NewArrayObject(aCx, elements);
|
2013-11-05 18:16:24 +04:00
|
|
|
if (!array) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
|
|
|
|
2015-01-28 02:16:21 +03:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>> clonedObjects;
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
2013-11-05 18:16:24 +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(),
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerRunnable::WorkerThreadModifyBusyCount,
|
2014-02-01 06:50:07 +04:00
|
|
|
Move(buffer), clonedObjects, aToMessagePort,
|
2013-10-23 17:16:49 +04:00
|
|
|
aMessagePortSerial);
|
2015-03-16 23:23:42 +03:00
|
|
|
runnable->SetMessageSource(aClientInfo);
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:23:42 +03:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::PostMessageToServiceWorker(
|
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
nsAutoPtr<ServiceWorkerClientInfo>& aClientInfo,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
PostMessageInternal(aCx, aMessage, aTransferable, false, 0,
|
|
|
|
aClientInfo.forget(), aRv);
|
|
|
|
}
|
|
|
|
|
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-11-05 18:16:24 +04:00
|
|
|
PostMessageInternal(aCx, aMessage, aTransferable, true, aMessagePortSerial,
|
2015-03-16 23:23:42 +03:00
|
|
|
nullptr, 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,
|
2014-02-01 06:50:07 +04:00
|
|
|
JSAutoStructuredCloneBuffer&& aBuffer,
|
2013-06-05 18:04:23 +04:00
|
|
|
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
|
|
|
|
2014-02-01 06:50:07 +04:00
|
|
|
JSAutoStructuredCloneBuffer buffer(Move(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;
|
|
|
|
}
|
|
|
|
|
2014-06-30 23:39:15 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(port->GetParentObject()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> data(cx);
|
2013-10-23 04:18:32 +04:00
|
|
|
if (!buffer.read(cx, &data, WorkerStructuredCloneCallbacks(true))) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.clear();
|
|
|
|
|
2014-02-27 14:51:14 +04:00
|
|
|
nsRefPtr<MessageEvent> event = new MessageEvent(port, nullptr, nullptr);
|
2013-06-05 18:04:23 +04:00
|
|
|
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);
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsTArray<nsRefPtr<MessagePortBase>> ports;
|
|
|
|
ports.AppendElement(port);
|
|
|
|
|
|
|
|
nsRefPtr<MessagePortList> portList = new MessagePortList(port, ports);
|
|
|
|
event->SetPorts(portList);
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
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>
|
|
|
|
void
|
2014-07-30 08:00:37 +04:00
|
|
|
WorkerPrivateParent<Derived>::UpdateRuntimeOptions(
|
2014-02-26 13:25:36 +04:00
|
|
|
JSContext* aCx,
|
2014-07-30 08:00:37 +04:00
|
|
|
const JS::RuntimeOptions& aRuntimeOptions)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2014-02-26 13:25:36 +04:00
|
|
|
mJSSettings.runtimeOptions = aRuntimeOptions;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2014-07-30 08:00:37 +04:00
|
|
|
nsRefPtr<UpdateRuntimeOptionsRunnable> runnable =
|
|
|
|
new UpdateRuntimeOptionsRunnable(ParentAsWorkerPrivate(), aRuntimeOptions);
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker context options!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:27:15 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UpdatePreference(JSContext* aCx, WorkerPreference aPref, bool aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
MOZ_ASSERT(aPref >= 0 && aPref < WORKERPREF_COUNT);
|
|
|
|
|
|
|
|
nsRefPtr<UpdatePreferenceRunnable> runnable =
|
|
|
|
new UpdatePreferenceRunnable(ParentAsWorkerPrivate(), aPref, aValue);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker preferences!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UpdateLanguages(JSContext* aCx,
|
|
|
|
const nsTArray<nsString>& aLanguages)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
nsRefPtr<UpdateLanguagesRunnable> runnable =
|
|
|
|
new UpdateLanguagesRunnable(ParentAsWorkerPrivate(), aLanguages);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to update worker languages!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
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
|
|
|
|
|
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 =
|
2013-12-03 08:07:02 +04:00
|
|
|
new GarbageCollectRunnable(ParentAsWorkerPrivate(), aShrinking,
|
|
|
|
/* collectChildren = */ true);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to GC worker!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::CycleCollect(JSContext* aCx, bool aDummy)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
nsRefPtr<CycleCollectRunnable> runnable =
|
|
|
|
new CycleCollectRunnable(ParentAsWorkerPrivate(),
|
|
|
|
/* collectChildren = */ true);
|
2012-01-18 00:05:25 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
2013-12-03 08:07:02 +04:00
|
|
|
NS_WARNING("Failed to CC worker!");
|
2012-01-18 00:05:25 +04:00
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::OfflineStatusChangeEvent(JSContext* aCx, bool aIsOffline)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
nsRefPtr<OfflineStatusChangeRunnable> runnable =
|
|
|
|
new OfflineStatusChangeRunnable(ParentAsWorkerPrivate(), aIsOffline);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
NS_WARNING("Failed to dispatch offline status change event!");
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::OfflineStatusChangeEventInternal(JSContext* aCx, bool aIsOffline)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2014-08-27 06:42:13 +04:00
|
|
|
// The worker is already in this state. No need to dispatch an event.
|
|
|
|
if (mOnLine == !aIsOffline) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); ++index) {
|
|
|
|
mChildWorkers[index]->OfflineStatusChangeEvent(aCx, aIsOffline);
|
|
|
|
}
|
|
|
|
|
|
|
|
mOnLine = !aIsOffline;
|
|
|
|
WorkerGlobalScope* globalScope = GlobalScope();
|
|
|
|
nsRefPtr<WorkerNavigator> nav = globalScope->GetExistingNavigator();
|
|
|
|
if (nav) {
|
|
|
|
nav->SetOnLine(mOnLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString eventType;
|
|
|
|
if (aIsOffline) {
|
|
|
|
eventType.AssignLiteral("offline");
|
|
|
|
} else {
|
|
|
|
eventType.AssignLiteral("online");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
nsresult rv =
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), globalScope, nullptr, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
rv = event->InitEvent(eventType, false, false);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
globalScope->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
template <class Derived>
|
|
|
|
bool
|
|
|
|
WorkerPrivateParent<Derived>::RegisterSharedWorker(JSContext* aCx,
|
|
|
|
SharedWorker* aSharedWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT(IsSharedWorker() || IsServiceWorker());
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(!mSharedWorkers.Get(aSharedWorker->Serial()));
|
|
|
|
|
2014-05-12 20:11:15 +04:00
|
|
|
if (IsSharedWorker()) {
|
|
|
|
nsRefPtr<MessagePortRunnable> runnable =
|
|
|
|
new MessagePortRunnable(ParentAsWorkerPrivate(), aSharedWorker->Serial(),
|
|
|
|
true);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mSharedWorkers.Put(aSharedWorker->Serial(), aSharedWorker);
|
|
|
|
|
|
|
|
// If there were other SharedWorker objects attached to this worker then they
|
2015-04-01 12:00:19 +03:00
|
|
|
// may all have been frozen and this worker would need to be thawed.
|
|
|
|
if (mSharedWorkers.Count() > 1 && !Thaw(aCx, nullptr)) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::UnregisterSharedWorker(
|
|
|
|
JSContext* aCx,
|
|
|
|
SharedWorker* aSharedWorker)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT(IsSharedWorker() || IsServiceWorker());
|
2013-06-05 18:04:23 +04:00
|
|
|
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
|
2015-04-01 12:00:19 +03:00
|
|
|
// may all be frozen and this worker would need to be frozen. Otherwise,
|
2013-06-05 18:04:23 +04:00
|
|
|
// if that was the last SharedWorker then it's time to cancel this worker.
|
|
|
|
if (mSharedWorkers.Count()) {
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!Freeze(aCx, nullptr)) {
|
2013-06-05 18:04:23 +04:00
|
|
|
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.
|
2014-05-09 05:03:35 +04:00
|
|
|
for (size_t index = 0; index < sharedWorkers.Length(); index++) {
|
2013-06-05 18:04:23 +04:00
|
|
|
nsRefPtr<SharedWorker>& sharedWorker = sharedWorkers[index];
|
|
|
|
|
|
|
|
// May be null.
|
|
|
|
nsPIDOMWindow* window = sharedWorker->GetOwner();
|
|
|
|
|
2014-01-03 05:04:15 +04:00
|
|
|
RootedDictionary<ErrorEventInit> errorInit(aCx);
|
2013-06-05 18:04:23 +04:00
|
|
|
errorInit.mBubbles = false;
|
|
|
|
errorInit.mCancelable = true;
|
|
|
|
errorInit.mMessage = aMessage;
|
|
|
|
errorInit.mFilename = aFilename;
|
|
|
|
errorInit.mLineno = aLineNumber;
|
2014-04-23 15:55:58 +04:00
|
|
|
errorInit.mColno = aColumnNumber;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
nsRefPtr<ErrorEvent> errorEvent =
|
|
|
|
ErrorEvent::Constructor(sharedWorker, NS_LITERAL_STRING("error"),
|
|
|
|
errorInit);
|
|
|
|
if (!errorEvent) {
|
2014-07-02 14:26:49 +04:00
|
|
|
ThrowAndReport(window, NS_ERROR_UNEXPECTED);
|
2013-06-05 18:04:23 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
errorEvent->SetTrusted(true);
|
|
|
|
|
|
|
|
bool defaultActionEnabled;
|
2014-06-12 21:02:33 +04:00
|
|
|
nsresult rv = sharedWorker->DispatchEvent(errorEvent, &defaultActionEnabled);
|
2013-06-05 18:04:23 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-07-02 14:26:49 +04:00
|
|
|
ThrowAndReport(window, rv);
|
2013-06-05 18:04:23 +04:00
|
|
|
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)) {
|
2014-07-02 14:26:49 +04:00
|
|
|
windowActions.AppendElement(WindowAction(window));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t actionsIndex = windowActions.LastIndexOf(WindowAction(window));
|
|
|
|
if (actionsIndex != windowActions.NoIndex) {
|
|
|
|
// Any listener that calls preventDefault() will prevent the window from
|
|
|
|
// receiving the error event.
|
|
|
|
windowActions[actionsIndex].mDefaultAction = false;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo =
|
|
|
|
do_QueryInterface(windowAction.mWindow);
|
|
|
|
MOZ_ASSERT(sgo);
|
|
|
|
|
2013-12-21 04:22:13 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-01-03 05:04:15 +04:00
|
|
|
RootedDictionary<ErrorEventInit> init(aCx);
|
2014-02-23 09:01:12 +04:00
|
|
|
init.mLineno = aLineNumber;
|
|
|
|
init.mFilename = aFilename;
|
|
|
|
init.mMessage = aMessage;
|
|
|
|
init.mCancelable = true;
|
|
|
|
init.mBubbles = true;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-02-23 09:01:12 +04:00
|
|
|
rv = sgo->HandleScriptError(init, &status);
|
2013-06-05 18:04:23 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-07-02 14:26:49 +04:00
|
|
|
ThrowAndReport(windowAction.mWindow, rv);
|
2013-06-05 18:04:23 +04:00
|
|
|
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();
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT(IsSharedWorker() || IsServiceWorker());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
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();
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT(IsSharedWorker() || IsServiceWorker());
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
nsPIDOMWindow* mWindow;
|
|
|
|
nsAutoTArray<nsRefPtr<SharedWorker>, 10> mSharedWorkers;
|
|
|
|
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit Closure(nsPIDOMWindow* aWindow)
|
2013-06-05 18:04:23 +04:00
|
|
|
: 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();
|
|
|
|
|
2014-05-12 20:11:15 +04:00
|
|
|
if (IsSharedWorker() || IsServiceWorker()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
// 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
|
|
|
|
2015-04-15 23:32:53 +03:00
|
|
|
mLocationInfo.mHostname.Truncate();
|
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(aBaseURI, mLocationInfo.mHostname);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-15 23:32:53 +03:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(aBaseURI));
|
|
|
|
if (!url || NS_FAILED(url->GetFilePath(mLocationInfo.mPathname))) {
|
2011-11-04 20:32:17 +04:00
|
|
|
mLocationInfo.mPathname.Truncate();
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsCString temp;
|
|
|
|
|
2011-11-04 20:32:17 +04:00
|
|
|
if (url && NS_SUCCEEDED(url->GetQuery(temp)) && !temp.IsEmpty()) {
|
2014-05-26 22:55:08 +04:00
|
|
|
mLocationInfo.mSearch.Assign('?');
|
2011-07-17 23:09:13 +04:00
|
|
|
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);
|
2015-01-18 01:10:38 +03:00
|
|
|
if (converter && nsContentUtils::EncodeDecodeURLHash()) {
|
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))) {
|
2014-05-26 22:55:08 +04:00
|
|
|
mLocationInfo.mHash.Assign('#');
|
2011-11-04 20:32:17 +04:00
|
|
|
mLocationInfo.mHash.Append(NS_ConvertUTF16toUTF8(unicodeRef));
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mLocationInfo.mHash.IsEmpty()) {
|
2014-05-26 22:55:08 +04:00
|
|
|
mLocationInfo.mHash.Assign('#');
|
2013-10-23 17:16:49 +04:00
|
|
|
mLocationInfo.mHash.Append(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetScheme(mLocationInfo.mProtocol))) {
|
2014-05-22 07:48:51 +04:00
|
|
|
mLocationInfo.mProtocol.Append(':');
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLocationInfo.mProtocol.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t port;
|
|
|
|
if (NS_SUCCEEDED(aBaseURI->GetPort(&port)) && port != -1) {
|
|
|
|
mLocationInfo.mPort.AppendInt(port);
|
|
|
|
|
|
|
|
nsAutoCString host(mLocationInfo.mHostname);
|
2014-05-22 07:48:51 +04:00
|
|
|
host.Append(':');
|
2013-10-23 17:16:49 +04:00
|
|
|
host.Append(mLocationInfo.mPort);
|
|
|
|
|
|
|
|
mLocationInfo.mHost.Assign(host);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLocationInfo.mHost.Assign(mLocationInfo.mHostname);
|
|
|
|
}
|
2014-01-31 22:22:52 +04:00
|
|
|
|
2014-08-31 14:40:11 +04:00
|
|
|
nsContentUtils::GetUTFOrigin(aBaseURI, mLocationInfo.mOrigin);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2014-12-12 19:06:00 +03:00
|
|
|
WorkerPrivateParent<Derived>::SetPrincipal(nsIPrincipal* aPrincipal,
|
|
|
|
nsILoadGroup* aLoadGroup)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2014-12-12 19:06:00 +03:00
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(aLoadGroup, aPrincipal));
|
2014-12-17 09:26:15 +03:00
|
|
|
MOZ_ASSERT(!mLoadInfo.mPrincipalInfo);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
mLoadInfo.mPrincipal = aPrincipal;
|
|
|
|
mLoadInfo.mPrincipalIsSystem = nsContentUtils::IsSystemPrincipal(aPrincipal);
|
2014-02-05 22:38:16 +04:00
|
|
|
uint16_t appStatus = aPrincipal->GetAppStatus();
|
|
|
|
mLoadInfo.mIsInPrivilegedApp =
|
|
|
|
(appStatus == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
|
|
|
appStatus == nsIPrincipal::APP_STATUS_PRIVILEGED);
|
|
|
|
mLoadInfo.mIsInCertifiedApp = (appStatus == nsIPrincipal::APP_STATUS_CERTIFIED);
|
2014-12-12 19:06:00 +03:00
|
|
|
|
|
|
|
mLoadInfo.mLoadGroup = aLoadGroup;
|
2014-12-17 09:26:15 +03:00
|
|
|
|
|
|
|
mLoadInfo.mPrincipalInfo = new PrincipalInfo();
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
PrincipalToPrincipalInfo(aPrincipal, mLoadInfo.mPrincipalInfo)));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
JSContext*
|
|
|
|
WorkerPrivateParent<Derived>::ParentJSContext() const
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
if (mParent) {
|
|
|
|
return mParent->GetJSContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
return mLoadInfo.mScriptContext ?
|
|
|
|
mLoadInfo.mScriptContext->GetNativeContext() :
|
|
|
|
nsContentUtils::GetSafeJSContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
void
|
2015-02-21 18:09:17 +03:00
|
|
|
WorkerPrivateParent<Derived>::UpdateOverridenLoadGroup(nsILoadGroup* aBaseLoadGroup)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// The load group should have been overriden at init time.
|
|
|
|
mLoadInfo.mInterfaceRequestor->MaybeAddTabChild(aBaseLoadGroup);
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
template <class Derived>
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(WorkerPrivateParent<Derived>, DOMEventTargetHelper)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
template <class Derived>
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_RELEASE_INHERITED(WorkerPrivateParent<Derived>, DOMEventTargetHelper)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WorkerPrivateParent<Derived>)
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WorkerPrivateParent<Derived>,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2014-02-02 22:08:50 +04:00
|
|
|
tmp->AssertIsOnParentThread();
|
|
|
|
|
|
|
|
// The WorkerPrivate::mSelfRef has a reference to itself, which is really
|
|
|
|
// held by the worker thread. We traverse this reference if and only if our
|
|
|
|
// busy count is zero and we have not released the main thread reference.
|
|
|
|
// We do not unlink it. This allows the CC to break cycles involving the
|
|
|
|
// WorkerPrivate and begin shutting it down (which does happen in unlink) but
|
|
|
|
// ensures that the WorkerPrivate won't be deleted before we're done shutting
|
|
|
|
// down the thread.
|
|
|
|
|
|
|
|
if (!tmp->mBusyCount && !tmp->mMainThreadObjectsForgotten) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelfRef)
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// The various strong references in LoadInfo are managed manually and cannot
|
|
|
|
// be cycle collected.
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WorkerPrivateParent<Derived>,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2014-02-02 22:08:50 +04:00
|
|
|
tmp->Terminate(nullptr);
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WorkerPrivateParent<Derived>,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-10-23 17:16:49 +04:00
|
|
|
tmp->AssertIsOnParentThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
|
|
|
WorkerPrivateParent<Derived>::AssertIsOnParentThread() const
|
|
|
|
{
|
|
|
|
if (GetParent()) {
|
|
|
|
GetParent()->AssertIsOnWorkerThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertIsOnMainThread();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 07:40:38 +04:00
|
|
|
template <class Derived>
|
|
|
|
void
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivateParent<Derived>::AssertInnerWindowIsCorrect() const
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Only care about top level workers from windows.
|
|
|
|
if (mParent || !mLoadInfo.mWindow) {
|
|
|
|
return;
|
|
|
|
}
|
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-23 17:16:49 +04:00
|
|
|
nsPIDOMWindow* outer = mLoadInfo.mWindow->GetOuterWindow();
|
|
|
|
NS_ASSERTION(outer && outer->GetCurrentInnerWindow() == mLoadInfo.mWindow,
|
|
|
|
"Inner window no longer correct!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#endif
|
2014-10-27 20:00:05 +03:00
|
|
|
|
2015-03-26 22:09:45 +03:00
|
|
|
class ReportDebuggerErrorRunnable final : public nsIRunnable
|
|
|
|
{
|
|
|
|
nsRefPtr<WorkerDebugger> mDebugger;
|
|
|
|
nsString mFilename;
|
|
|
|
uint32_t mLineno;
|
|
|
|
nsString mMessage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ReportDebuggerErrorRunnable(WorkerDebugger* aDebugger,
|
|
|
|
const nsAString& aFilename, uint32_t aLineno,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
: mDebugger(aDebugger),
|
|
|
|
mFilename(aFilename),
|
|
|
|
mLineno(aLineno),
|
|
|
|
mMessage(aMessage)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ReportDebuggerErrorRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
mDebugger->ReportErrorToDebuggerOnMainThread(mFilename, mLineno, mMessage);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ReportDebuggerErrorRunnable, nsIRunnable)
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
WorkerDebugger::WorkerDebugger(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mMutex("WorkerDebugger::mMutex"),
|
|
|
|
mCondVar(mMutex, "WorkerDebugger::mCondVar"),
|
|
|
|
mWorkerPrivate(aWorkerPrivate),
|
2015-03-17 13:15:19 +03:00
|
|
|
mIsEnabled(false),
|
2015-04-01 21:32:20 +03:00
|
|
|
mIsInitialized(false),
|
|
|
|
mIsFrozen(false)
|
2014-10-27 20:00:05 +03:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnParentThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebugger::~WorkerDebugger()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
2014-10-29 23:11:33 +03:00
|
|
|
MOZ_ASSERT(!mIsEnabled);
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
if (NS_FAILED(NS_GetMainThread(getter_AddRefs(mainThread)))) {
|
|
|
|
NS_WARNING("Failed to proxy release of listeners, leaking instead!");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < mListeners.Length(); ++index) {
|
|
|
|
nsIWorkerDebuggerListener* listener = nullptr;
|
|
|
|
mListeners[index].forget(&listener);
|
|
|
|
if (NS_FAILED(NS_ProxyRelease(mainThread, listener))) {
|
|
|
|
NS_WARNING("Failed to proxy release of listener, leaking instead!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 20:00:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WorkerDebugger, nsIWorkerDebugger)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetIsClosed(bool* aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
*aResult = !mWorkerPrivate;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-29 23:11:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetIsChrome(bool* aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = mWorkerPrivate->IsChromeWorker();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-01 21:32:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetIsFrozen(bool* aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = mIsFrozen;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-29 23:11:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetParent(nsIWorkerDebugger** aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* parent = mWorkerPrivate->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsDedicatedWorker());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWorkerDebugger> debugger = parent->Debugger();
|
|
|
|
debugger.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetType(uint32_t* aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = mWorkerPrivate->Type();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetUrl(nsAString& aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult = mWorkerPrivate->ScriptURL();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-29 23:11:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::GetWindow(nsIDOMWindow** aResult)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWorkerPrivate->GetParent() || !mWorkerPrivate->IsDedicatedWorker()) {
|
|
|
|
*aResult = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = mWorkerPrivate->GetWindow();
|
|
|
|
window.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::Initialize(const nsAString& aURL, JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate || mIsInitialized) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CompileDebuggerScriptRunnable> runnable =
|
|
|
|
new CompileDebuggerScriptRunnable(mWorkerPrivate, aURL);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIsInitialized = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:15:59 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::PostMessageMoz(const nsAString& aMessage, JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate || !mIsInitialized) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<DebuggerMessageEventRunnable> runnable =
|
|
|
|
new DebuggerMessageEventRunnable(mWorkerPrivate, aMessage);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-29 23:11:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::AddListener(nsIWorkerDebuggerListener* aListener)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mListeners.Contains(aListener)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.AppendElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerDebugger::RemoveListener(nsIWorkerDebuggerListener* aListener)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (!mListeners.Contains(aListener)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListeners.RemoveElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
void
|
|
|
|
WorkerDebugger::WaitIsEnabled(bool aIsEnabled)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
while (mIsEnabled != aIsEnabled) {
|
|
|
|
mCondVar.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::NotifyIsEnabled(bool aIsEnabled)
|
|
|
|
{
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mIsEnabled != aIsEnabled);
|
|
|
|
mIsEnabled = aIsEnabled;
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::Enable()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
|
|
NotifyIsEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::Disable()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
|
2014-10-29 23:11:33 +03:00
|
|
|
{
|
|
|
|
MutexAutoUnlock unlock(mMutex);
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIWorkerDebuggerListener>> listeners(mListeners);
|
|
|
|
for (size_t index = 0; index < listeners.Length(); ++index) {
|
|
|
|
listeners[index]->OnClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
NotifyIsEnabled(false);
|
|
|
|
}
|
|
|
|
|
2015-04-01 21:32:20 +03:00
|
|
|
void
|
|
|
|
WorkerDebugger::Freeze()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &WorkerDebugger::FreezeOnMainThread);
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::FreezeOnMainThread()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
mIsFrozen = true;
|
|
|
|
|
|
|
|
for (size_t index = 0; index < mListeners.Length(); ++index) {
|
|
|
|
mListeners[index]->OnFreeze();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::Thaw()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &WorkerDebugger::ThawOnMainThread);
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::ThawOnMainThread()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
mIsFrozen = false;
|
|
|
|
|
|
|
|
for (size_t index = 0; index < mListeners.Length(); ++index) {
|
|
|
|
mListeners[index]->OnThaw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:15:59 +03:00
|
|
|
void
|
|
|
|
WorkerDebugger::PostMessageToDebugger(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethodWithArg<nsString>(this,
|
|
|
|
&WorkerDebugger::PostMessageToDebuggerOnMainThread, nsString(aMessage));
|
|
|
|
NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::PostMessageToDebuggerOnMainThread(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIWorkerDebuggerListener>> listeners;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
listeners.AppendElements(mListeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < listeners.Length(); ++index) {
|
|
|
|
listeners[index]->OnMessage(aMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:09:45 +03:00
|
|
|
void
|
|
|
|
WorkerDebugger::ReportErrorToDebugger(const nsAString& aFilename,
|
|
|
|
uint32_t aLineno,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new ReportDebuggerErrorRunnable(this, aFilename, aLineno, aMessage);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable, NS_DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to report error to debugger on main thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebugger::ReportErrorToDebuggerOnMainThread(const nsAString& aFilename,
|
|
|
|
uint32_t aLineno,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsIWorkerDebuggerListener>> listeners;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
listeners.AppendElements(mListeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t index = 0; index < listeners.Length(); ++index) {
|
|
|
|
listeners[index]->OnError(aFilename, aLineno, aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogErrorToConsole(aMessage, aFilename, nsString(), aLineno, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +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,
|
2013-10-31 03:40:16 +04:00
|
|
|
bool aIsChromeWorker, WorkerType aWorkerType,
|
2014-02-06 18:39:10 +04:00
|
|
|
const nsACString& aSharedWorkerName,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo& aLoadInfo)
|
2014-11-17 22:55:37 +03:00
|
|
|
: WorkerPrivateParent<WorkerPrivate>(aCx, aParent, aScriptURL,
|
|
|
|
aIsChromeWorker, aWorkerType,
|
|
|
|
aSharedWorkerName, aLoadInfo)
|
|
|
|
, mJSContext(nullptr)
|
2013-10-23 17:16:49 +04:00
|
|
|
, mPRThread(nullptr)
|
2015-03-27 09:17:16 +03:00
|
|
|
, mDebuggerEventLoopLevel(0)
|
2014-11-17 22:55:37 +03:00
|
|
|
, mErrorHandlerRecursionCount(0)
|
|
|
|
, mNextTimeoutId(1)
|
|
|
|
, mStatus(Pending)
|
2015-04-01 12:00:19 +03:00
|
|
|
, mFrozen(false)
|
2014-11-17 22:55:37 +03:00
|
|
|
, mTimerRunning(false)
|
|
|
|
, mRunningExpiredTimeouts(false)
|
|
|
|
, mCloseHandlerStarted(false)
|
|
|
|
, mCloseHandlerFinished(false)
|
|
|
|
, mMemoryReporterRunning(false)
|
|
|
|
, mBlockedForMemoryReporter(false)
|
|
|
|
, mCancelAllPendingRunnables(false)
|
|
|
|
, mPeriodicGCTimerRunning(false)
|
|
|
|
, mIdleGCTimerRunning(false)
|
|
|
|
, mWorkerScriptExecutedSuccessfully(false)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT_IF(!IsDedicatedWorker(), !aSharedWorkerName.IsVoid());
|
|
|
|
MOZ_ASSERT_IF(IsDedicatedWorker(), aSharedWorkerName.IsEmpty());
|
2013-11-24 23:27:15 +04:00
|
|
|
|
|
|
|
if (aParent) {
|
|
|
|
aParent->AssertIsOnWorkerThread();
|
|
|
|
aParent->GetAllPreferences(mPreferences);
|
2013-11-20 03:08:50 +04:00
|
|
|
mOnLine = aParent->OnLine();
|
2013-11-24 23:27:15 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
RuntimeService::GetDefaultPreferences(mPreferences);
|
2014-08-23 07:06:56 +04:00
|
|
|
mOnLine = !NS_IsOffline() && !NS_IsAppOffline(aLoadInfo.mPrincipal);
|
2013-11-24 23:27:15 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate::~WorkerPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-03-31 08:42:20 +04:00
|
|
|
already_AddRefed<WorkerPrivate>
|
2013-11-05 18:16:24 +04:00
|
|
|
WorkerPrivate::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
ErrorResult& aRv)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2014-02-06 18:39:10 +04:00
|
|
|
return WorkerPrivate::Constructor(aGlobal, aScriptURL, false,
|
|
|
|
WorkerTypeDedicated, EmptyCString(),
|
|
|
|
nullptr, aRv);
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2014-02-06 18:39:10 +04:00
|
|
|
return WorkerPrivate::Constructor(aGlobal, aScriptURL, true,
|
|
|
|
WorkerTypeDedicated, EmptyCString(),
|
|
|
|
nullptr, aRv)
|
|
|
|
.downcast<ChromeWorkerPrivate>();
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool
|
2014-08-05 06:20:35 +04:00
|
|
|
ChromeWorkerPrivate::WorkerAvailable(JSContext* aCx, JSObject* /* unused */)
|
2013-11-05 18:16:24 +04:00
|
|
|
{
|
2014-08-05 06:20:35 +04:00
|
|
|
// Chrome is always allowed to use workers, and content is never
|
|
|
|
// allowed to use ChromeWorker, so all we have to check is the
|
|
|
|
// caller. However, chrome workers apparently might not have a
|
|
|
|
// system principal, so we have to check for them manually.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return nsContentUtils::IsCallerChrome();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetWorkerPrivateFromContext(aCx)->IsChromeWorker();
|
2013-11-05 18:16:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<WorkerPrivate>
|
|
|
|
WorkerPrivate::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker, WorkerType aWorkerType,
|
2014-02-06 18:39:10 +04:00
|
|
|
const nsACString& aSharedWorkerName,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo* aLoadInfo, ErrorResult& aRv)
|
2014-05-29 20:19:00 +04:00
|
|
|
{
|
2014-06-16 22:08:00 +04:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2014-05-29 20:19:00 +04:00
|
|
|
return Constructor(cx, aScriptURL, aIsChromeWorker, aWorkerType,
|
|
|
|
aSharedWorkerName, aLoadInfo, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<WorkerPrivate>
|
|
|
|
WorkerPrivate::Constructor(JSContext* aCx,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker, WorkerType aWorkerType,
|
|
|
|
const nsACString& aSharedWorkerName,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo* aLoadInfo, ErrorResult& aRv)
|
2013-11-05 18:16:24 +04:00
|
|
|
{
|
|
|
|
WorkerPrivate* parent = NS_IsMainThread() ?
|
|
|
|
nullptr :
|
|
|
|
GetCurrentThreadWorkerPrivate();
|
|
|
|
if (parent) {
|
|
|
|
parent->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
} else {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT_IF(aWorkerType != WorkerTypeDedicated,
|
2013-11-05 18:16:24 +04:00
|
|
|
!aSharedWorkerName.IsVoid());
|
2014-05-12 20:11:15 +04:00
|
|
|
MOZ_ASSERT_IF(aWorkerType == WorkerTypeDedicated,
|
2013-11-05 18:16:24 +04:00
|
|
|
aSharedWorkerName.IsEmpty());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
Maybe<WorkerLoadInfo> stackLoadInfo;
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!aLoadInfo) {
|
2014-08-14 02:39:41 +04:00
|
|
|
stackLoadInfo.emplace();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-05-29 20:19:00 +04:00
|
|
|
nsresult rv = GetLoadInfo(aCx, nullptr, parent, aScriptURL,
|
2015-02-21 18:09:17 +03:00
|
|
|
aIsChromeWorker, InheritLoadGroup,
|
|
|
|
stackLoadInfo.ptr());
|
2013-06-05 18:04:23 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-29 20:19:00 +04:00
|
|
|
scriptloader::ReportLoadError(aCx, aScriptURL, rv, !parent);
|
2013-11-05 18:16:24 +04:00
|
|
|
aRv.Throw(rv);
|
2013-06-05 18:04:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-14 02:39:41 +04:00
|
|
|
aLoadInfo = stackLoadInfo.ptr();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +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) {
|
2014-05-29 20:19:00 +04:00
|
|
|
JS_ReportError(aCx, "Failed to create runtime service!");
|
2013-11-05 18:16:24 +04:00
|
|
|
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 =
|
2014-05-29 20:19:00 +04:00
|
|
|
new WorkerPrivate(aCx, parent, aScriptURL, aIsChromeWorker,
|
2013-10-31 03:40:16 +04:00
|
|
|
aWorkerType, aSharedWorkerName, *aLoadInfo);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-05-29 20:19:00 +04:00
|
|
|
if (!runtimeService->RegisterWorker(aCx, worker)) {
|
2013-11-05 18:16:24 +04:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:00:05 +03:00
|
|
|
worker->EnableDebugger();
|
|
|
|
|
2015-03-17 13:15:19 +03:00
|
|
|
nsRefPtr<CompileScriptRunnable> compiler =
|
|
|
|
new CompileScriptRunnable(worker, aScriptURL);
|
2014-05-29 20:19:00 +04:00
|
|
|
if (!compiler->Dispatch(aCx)) {
|
2013-11-05 18:16:24 +04:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2013-06-05 18:04:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-02 22:08:50 +04:00
|
|
|
worker->mSelfRef = worker;
|
2013-11-05 18:16:24 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
return worker.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
WorkerPrivate::GetLoadInfo(JSContext* aCx, nsPIDOMWindow* aWindow,
|
|
|
|
WorkerPrivate* aParent, const nsAString& aScriptURL,
|
2015-02-21 18:09:17 +03:00
|
|
|
bool aIsChromeWorker,
|
|
|
|
LoadGroupBehavior aLoadGroupBehavior,
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo* aLoadInfo)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-06-05 18:04:23 +04:00
|
|
|
using namespace mozilla::dom::workers::scriptloader;
|
2014-12-17 09:26:15 +03:00
|
|
|
using mozilla::dom::indexedDB::IDBFactory;
|
2012-09-15 22:51:55 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aCx);
|
2014-05-07 02:43:02 +04:00
|
|
|
MOZ_ASSERT_IF(NS_IsMainThread(), aCx == nsContentUtils::GetCurrentJSContext());
|
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
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerLoadInfo loadInfo;
|
2013-06-05 18:04:23 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-26 04:16:46 +04:00
|
|
|
// StartAssignment() is used instead getter_AddRefs because, getter_AddRefs
|
|
|
|
// does QI in debug build and, if this worker runs in a child process,
|
|
|
|
// HttpChannelChild will crash because it's not thread-safe.
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = ChannelFromScriptURLWorkerThread(aCx, aParent, aScriptURL,
|
2014-01-26 04:16:46 +04:00
|
|
|
loadInfo.mChannel.StartAssignment());
|
2013-06-05 18:04:23 +04:00
|
|
|
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;
|
2013-10-23 17:16:49 +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();
|
2014-12-17 09:26:15 +03:00
|
|
|
loadInfo.mFromWindow = aParent->IsFromWindow();
|
|
|
|
loadInfo.mWindowID = aParent->WindowID();
|
|
|
|
loadInfo.mIndexedDBAllowed = aParent->IsIndexedDBAllowed();
|
2013-06-05 18:04:23 +04:00
|
|
|
} 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) {
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal =
|
|
|
|
nsJSUtils::GetStaticScriptGlobal(JS::CurrentGlobalOrNull(aCx));
|
|
|
|
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();
|
2014-12-12 19:06:00 +03:00
|
|
|
loadInfo.mLoadGroup = document->GetDocumentLoadGroup();
|
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-10-23 17:16:49 +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
|
|
|
}
|
2013-10-23 17:16:49 +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;
|
2014-08-20 21:34:14 +04:00
|
|
|
|
|
|
|
uint16_t appStatus = loadInfo.mPrincipal->GetAppStatus();
|
|
|
|
loadInfo.mIsInPrivilegedApp =
|
|
|
|
(appStatus == nsIPrincipal::APP_STATUS_CERTIFIED ||
|
|
|
|
appStatus == nsIPrincipal::APP_STATUS_PRIVILEGED);
|
|
|
|
loadInfo.mIsInCertifiedApp = (appStatus == nsIPrincipal::APP_STATUS_CERTIFIED);
|
2014-12-17 09:26:15 +03:00
|
|
|
loadInfo.mFromWindow = true;
|
|
|
|
loadInfo.mWindowID = globalWindow->WindowID();
|
|
|
|
loadInfo.mIndexedDBAllowed = IDBFactory::AllowedForWindow(globalWindow);
|
2013-06-05 18:04:23 +04:00
|
|
|
} 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.
|
2014-02-25 19:43:14 +04:00
|
|
|
JS::AutoFilename fileName;
|
|
|
|
if (JS::DescribeScriptedCaller(aCx, &fileName)) {
|
2013-11-06 18:05:17 +04:00
|
|
|
// In most cases, fileName is URI. In a few other cases
|
|
|
|
// (e.g. xpcshell), fileName is a file path. Ideally, we would
|
|
|
|
// prefer testing whether fileName parses as an URI and fallback
|
|
|
|
// to file path in case of error, but Windows file paths have
|
|
|
|
// the interesting property that they can be parsed as bogus
|
|
|
|
// URIs (e.g. C:/Windows/Tmp is interpreted as scheme "C",
|
|
|
|
// hostname "Windows", path "Tmp"), which defeats this algorithm.
|
|
|
|
// Therefore, we adopt the opposite convention.
|
|
|
|
nsCOMPtr<nsIFile> scriptFile =
|
|
|
|
do_CreateInstance("@mozilla.org/file/local;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2012-09-17 04:20:16 +04:00
|
|
|
|
2014-02-25 19:43:14 +04:00
|
|
|
rv = scriptFile->InitWithPath(NS_ConvertUTF8toUTF16(fileName.get()));
|
2013-11-06 18:05:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_NewFileURI(getter_AddRefs(loadInfo.mBaseURI),
|
|
|
|
scriptFile);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// As expected, fileName is not a path, so proceed with
|
|
|
|
// a uri.
|
|
|
|
rv = NS_NewURI(getter_AddRefs(loadInfo.mBaseURI),
|
2014-02-25 19:43:14 +04:00
|
|
|
fileName.get());
|
2013-11-06 18:05:17 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
loadInfo.mXHRParamsAllowed = true;
|
2014-12-17 09:26:15 +03:00
|
|
|
loadInfo.mFromWindow = false;
|
|
|
|
loadInfo.mWindowID = UINT64_MAX;
|
|
|
|
loadInfo.mIndexedDBAllowed = 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
|
|
|
|
2014-07-10 21:25:00 +04:00
|
|
|
if (!nsContentUtils::GetContentSecurityPolicy(getter_AddRefs(loadInfo.mCSP))) {
|
2013-06-05 18:04:23 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
if (!loadInfo.mLoadGroup || aLoadGroupBehavior == OverrideLoadGroup) {
|
|
|
|
OverrideLoadInfoLoadGroup(loadInfo);
|
2014-12-12 19:06:00 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(loadInfo.mLoadGroup,
|
|
|
|
loadInfo.mPrincipal));
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
rv = ChannelFromScriptURLMainThread(loadInfo.mPrincipal, loadInfo.mBaseURI,
|
2014-12-12 19:06:00 +03:00
|
|
|
document, loadInfo.mLoadGroup,
|
|
|
|
aScriptURL,
|
2013-06-05 18:04:23 +04:00
|
|
|
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-10-23 17:16:49 +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
|
|
|
}
|
|
|
|
|
2015-02-21 18:09:17 +03:00
|
|
|
// static
|
|
|
|
void
|
2015-02-12 12:50:05 +03:00
|
|
|
WorkerPrivate::OverrideLoadInfoLoadGroup(WorkerLoadInfo& aLoadInfo)
|
2015-02-21 18:09:17 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aLoadInfo.mInterfaceRequestor);
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
aLoadInfo.mInterfaceRequestor =
|
|
|
|
new WorkerLoadInfo::InterfaceRequestor(aLoadInfo.mPrincipal,
|
|
|
|
aLoadInfo.mLoadGroup);
|
2015-02-21 18:09:17 +03:00
|
|
|
aLoadInfo.mInterfaceRequestor->MaybeAddTabChild(aLoadInfo.mLoadGroup);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup =
|
|
|
|
do_CreateInstance(NS_LOADGROUP_CONTRACTID);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
loadGroup->SetNotificationCallbacks(aLoadInfo.mInterfaceRequestor);
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
aLoadInfo.mLoadGroup = loadGroup.forget();
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::DoRunLoop(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mJSContext = aCx;
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mStatus == Pending);
|
2011-07-17 23:09:13 +04:00
|
|
|
mStatus = Running;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
EnableMemoryReporter();
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
InitializeGCTimers();
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
Maybe<JSAutoCompartment> workerCompartment;
|
2011-09-09 04:03:03 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
for (;;) {
|
|
|
|
Status currentStatus;
|
2015-03-04 17:11:32 +03:00
|
|
|
bool debuggerRunnablesPending = false;
|
2013-10-23 17:16:49 +04:00
|
|
|
bool normalRunnablesPending = false;
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
while (mControlQueue.IsEmpty() &&
|
2015-03-04 17:11:32 +03:00
|
|
|
!(debuggerRunnablesPending = !mDebuggerQueue.IsEmpty()) &&
|
2013-10-23 17:16:49 +04:00
|
|
|
!(normalRunnablesPending = NS_HasPendingEvents(mThread))) {
|
2012-12-30 22:21:52 +04:00
|
|
|
WaitForWorkerEvents();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
ProcessAllControlRunnablesLocked();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// If the close handler has finished and all features are done then we can
|
|
|
|
// kill this thread.
|
2011-07-17 23:09:13 +04:00
|
|
|
if (currentStatus != Running && !HasActiveFeatures()) {
|
|
|
|
if (mCloseHandlerFinished && currentStatus != Killing) {
|
|
|
|
if (!NotifyInternal(aCx, Killing)) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
currentStatus = mStatus;
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(currentStatus == Killing);
|
2011-07-17 23:09:13 +04:00
|
|
|
#else
|
|
|
|
currentStatus = Killing;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're supposed to die then we should exit the loop.
|
|
|
|
if (currentStatus == Killing) {
|
2015-04-10 18:27:57 +03:00
|
|
|
// Flush uncaught rejections immediately, without
|
|
|
|
// waiting for a next tick.
|
|
|
|
PromiseDebugging::FlushUncaughtRejections();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
ShutdownGCTimers();
|
2012-01-18 00:05:25 +04:00
|
|
|
|
2011-09-09 04:03:03 +04:00
|
|
|
DisableMemoryReporter();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
mStatus = Dead;
|
|
|
|
mJSContext = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After mStatus is set to Dead there can be no more
|
|
|
|
// WorkerControlRunnables so no need to lock here.
|
|
|
|
if (!mControlQueue.IsEmpty()) {
|
|
|
|
WorkerControlRunnable* runnable;
|
|
|
|
while (mControlQueue.Pop(runnable)) {
|
|
|
|
runnable->Cancel();
|
|
|
|
runnable->Release();
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
// Clear away our MessagePorts.
|
|
|
|
mWorkerPorts.Clear();
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
// Unroot the globals
|
2013-11-05 18:16:26 +04:00
|
|
|
mScope = nullptr;
|
2015-03-04 02:51:53 +03:00
|
|
|
mDebuggerScope = nullptr;
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
if (debuggerRunnablesPending || normalRunnablesPending) {
|
|
|
|
// Start the periodic GC timer if it is not already running.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
if (debuggerRunnablesPending) {
|
|
|
|
WorkerRunnable* runnable;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
mDebuggerQueue.Pop(runnable);
|
|
|
|
debuggerRunnablesPending = !mDebuggerQueue.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
static_cast<nsIRunnable*>(runnable)->Run();
|
|
|
|
runnable->Release();
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
if (debuggerRunnablesPending) {
|
|
|
|
WorkerDebuggerGlobalScope* globalScope = DebuggerGlobalScope();
|
|
|
|
MOZ_ASSERT(globalScope);
|
2014-10-28 15:08:19 +03:00
|
|
|
|
2015-03-04 17:11:32 +03:00
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
|
|
|
JSAutoCompartment ac(aCx, globalScope->GetGlobalJSObject());
|
|
|
|
JS_MaybeGC(aCx);
|
|
|
|
}
|
|
|
|
} else if (normalRunnablesPending) {
|
|
|
|
MOZ_ASSERT(NS_HasPendingEvents(mThread));
|
|
|
|
|
|
|
|
// Process a single runnable from the main queue.
|
|
|
|
MOZ_ALWAYS_TRUE(NS_ProcessNextEvent(mThread, false));
|
|
|
|
|
|
|
|
// Only perform the Promise microtask checkpoint on the outermost event
|
|
|
|
// loop. Don't run it, for example, during sync XHR or importScripts.
|
|
|
|
(void)Promise::PerformMicroTaskCheckpoint();
|
|
|
|
|
|
|
|
normalRunnablesPending = NS_HasPendingEvents(mThread);
|
|
|
|
if (normalRunnablesPending && GlobalScope()) {
|
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
|
|
|
JSAutoCompartment ac(aCx, GlobalScope()->GetGlobalJSObject());
|
2013-10-23 17:16:49 +04:00
|
|
|
JS_MaybeGC(aCx);
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 17:11:32 +03:00
|
|
|
|
|
|
|
if (!debuggerRunnablesPending && !normalRunnablesPending) {
|
|
|
|
// Both the debugger event queue and the normal event queue has been
|
|
|
|
// exhausted, cancel the periodic GC timer and schedule the idle GC timer.
|
2013-10-23 17:16:49 +04:00
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-20 11:36:40 +04:00
|
|
|
MOZ_CRASH("Shouldn't get here!");
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::OnProcessNextEvent(uint32_t aRecursionDepth)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aRecursionDepth);
|
|
|
|
|
|
|
|
// Normally we process control runnables in DoRunLoop or RunCurrentSyncLoop.
|
|
|
|
// However, it's possible that non-worker C++ could spin its own nested event
|
|
|
|
// loop, and in that case we must ensure that we continue to process control
|
|
|
|
// runnables here.
|
|
|
|
if (aRecursionDepth > 1 &&
|
|
|
|
mSyncLoopStack.Length() < aRecursionDepth - 1) {
|
|
|
|
ProcessAllControlRunnables();
|
|
|
|
}
|
2014-12-17 09:26:15 +03:00
|
|
|
|
|
|
|
// Run any preempting runnables that match this depth.
|
|
|
|
if (!mPreemptingRunnableInfos.IsEmpty()) {
|
|
|
|
nsTArray<PreemptingRunnableInfo> pendingRunnableInfos;
|
|
|
|
|
|
|
|
for (uint32_t index = 0;
|
|
|
|
index < mPreemptingRunnableInfos.Length();
|
|
|
|
index++) {
|
|
|
|
PreemptingRunnableInfo& preemptingRunnableInfo =
|
|
|
|
mPreemptingRunnableInfos[index];
|
|
|
|
|
|
|
|
if (preemptingRunnableInfo.mRecursionDepth == aRecursionDepth) {
|
|
|
|
preemptingRunnableInfo.mRunnable->Run();
|
|
|
|
preemptingRunnableInfo.mRunnable = nullptr;
|
|
|
|
} else {
|
|
|
|
PreemptingRunnableInfo* pending = pendingRunnableInfos.AppendElement();
|
|
|
|
pending->mRunnable.swap(preemptingRunnableInfo.mRunnable);
|
|
|
|
pending->mRecursionDepth = preemptingRunnableInfo.mRecursionDepth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mPreemptingRunnableInfos.SwapElements(pendingRunnableInfos);
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::AfterProcessNextEvent(uint32_t aRecursionDepth)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aRecursionDepth);
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
bool
|
|
|
|
WorkerPrivate::RunBeforeNextEvent(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aRunnable);
|
|
|
|
MOZ_ASSERT_IF(!mPreemptingRunnableInfos.IsEmpty(),
|
|
|
|
NS_HasPendingEvents(mThread));
|
|
|
|
|
|
|
|
const uint32_t recursionDepth =
|
|
|
|
mThread->RecursionDepth(WorkerThreadFriendKey());
|
|
|
|
|
|
|
|
PreemptingRunnableInfo* preemptingRunnableInfo =
|
|
|
|
mPreemptingRunnableInfos.AppendElement();
|
|
|
|
|
|
|
|
preemptingRunnableInfo->mRunnable = aRunnable;
|
|
|
|
|
|
|
|
// Due to the weird way that the thread recursion counter is implemented we
|
|
|
|
// subtract one from the recursion level if we have one.
|
|
|
|
preemptingRunnableInfo->mRecursionDepth =
|
|
|
|
recursionDepth ? recursionDepth - 1 : 0;
|
|
|
|
|
|
|
|
// Ensure that we have a pending event so that the runnable will be guaranteed
|
|
|
|
// to run.
|
|
|
|
if (mPreemptingRunnableInfos.Length() == 1 && !NS_HasPendingEvents(mThread)) {
|
|
|
|
nsRefPtr<DummyRunnable> dummyRunnable = new DummyRunnable(this);
|
|
|
|
if (NS_FAILED(Dispatch(dummyRunnable))) {
|
|
|
|
NS_WARNING("RunBeforeNextEvent called after the thread is shutting "
|
|
|
|
"down!");
|
|
|
|
mPreemptingRunnableInfos.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::InitializeGCTimers()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// We need a timer for GC. The basic plan is to run a non-shrinking GC
|
|
|
|
// periodically (PERIODIC_GC_TIMER_DELAY_SEC) while the worker is running.
|
|
|
|
// Once the worker goes idle we set a short (IDLE_GC_TIMER_DELAY_SEC) timer to
|
|
|
|
// run a shrinking GC. If the worker receives more messages then the short
|
|
|
|
// timer is canceled and the periodic timer resumes.
|
|
|
|
mGCTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
MOZ_ASSERT(mGCTimer);
|
|
|
|
|
|
|
|
nsRefPtr<GarbageCollectRunnable> runnable =
|
|
|
|
new GarbageCollectRunnable(this, false, false);
|
|
|
|
mPeriodicGCTimerTarget = new TimerThreadEventTarget(this, runnable);
|
|
|
|
|
|
|
|
runnable = new GarbageCollectRunnable(this, true, false);
|
|
|
|
mIdleGCTimerTarget = new TimerThreadEventTarget(this, runnable);
|
|
|
|
|
|
|
|
mPeriodicGCTimerRunning = false;
|
2013-12-20 23:03:19 +04:00
|
|
|
mIdleGCTimerRunning = false;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::SetGCTimerMode(GCTimerMode aMode)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mGCTimer);
|
|
|
|
MOZ_ASSERT(mPeriodicGCTimerTarget);
|
|
|
|
MOZ_ASSERT(mIdleGCTimerTarget);
|
|
|
|
|
2013-12-20 23:03:19 +04:00
|
|
|
if ((aMode == PeriodicTimer && mPeriodicGCTimerRunning) ||
|
|
|
|
(aMode == IdleTimer && mIdleGCTimerRunning)) {
|
2013-10-23 17:16:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mGCTimer->Cancel()));
|
|
|
|
|
|
|
|
mPeriodicGCTimerRunning = false;
|
2013-12-20 23:03:19 +04:00
|
|
|
mIdleGCTimerRunning = false;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
LOG(("Worker %p canceled GC timer because %s\n", this,
|
|
|
|
aMode == PeriodicTimer ?
|
|
|
|
"periodic" :
|
|
|
|
aMode == IdleTimer ? "idle" : "none"));
|
|
|
|
|
|
|
|
if (aMode == NoTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aMode == PeriodicTimer || aMode == IdleTimer);
|
|
|
|
|
|
|
|
nsIEventTarget* target;
|
|
|
|
uint32_t delay;
|
|
|
|
int16_t type;
|
|
|
|
|
|
|
|
if (aMode == PeriodicTimer) {
|
|
|
|
target = mPeriodicGCTimerTarget;
|
|
|
|
delay = PERIODIC_GC_TIMER_DELAY_SEC * 1000;
|
|
|
|
type = nsITimer::TYPE_REPEATING_SLACK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
target = mIdleGCTimerTarget;
|
|
|
|
delay = IDLE_GC_TIMER_DELAY_SEC * 1000;
|
|
|
|
type = nsITimer::TYPE_ONE_SHOT;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mGCTimer->SetTarget(target)));
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mGCTimer->InitWithFuncCallback(DummyCallback,
|
|
|
|
nullptr, delay,
|
|
|
|
type)));
|
|
|
|
|
|
|
|
if (aMode == PeriodicTimer) {
|
|
|
|
LOG(("Worker %p scheduled periodic GC timer\n", this));
|
|
|
|
mPeriodicGCTimerRunning = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(("Worker %p scheduled idle GC timer\n", this));
|
2013-12-20 23:03:19 +04:00
|
|
|
mIdleGCTimerRunning = true;
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::ShutdownGCTimers()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(mGCTimer);
|
|
|
|
|
|
|
|
// Always make sure the timer is canceled.
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mGCTimer->Cancel()));
|
|
|
|
|
|
|
|
LOG(("Worker %p killed the GC timer\n", this));
|
|
|
|
|
|
|
|
mGCTimer = nullptr;
|
|
|
|
mPeriodicGCTimerTarget = nullptr;
|
|
|
|
mIdleGCTimerTarget = nullptr;
|
|
|
|
mPeriodicGCTimerRunning = false;
|
2013-12-20 23:03:19 +04:00
|
|
|
mIdleGCTimerRunning = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-03-11 01:28:43 +04:00
|
|
|
WorkerPrivate::InterruptCallback(JSContext* aCx)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
bool mayContinue = true;
|
2013-10-23 17:16:49 +04:00
|
|
|
bool scheduledIdleGC = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Run all control events now.
|
2011-09-09 04:03:03 +04:00
|
|
|
mayContinue = ProcessAllControlRunnables();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
bool mayFreeze = mFrozen;
|
|
|
|
if (mayFreeze) {
|
2013-06-05 18:04:23 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2015-04-01 12:00:19 +03:00
|
|
|
mayFreeze = mStatus <= Running;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!mayContinue || !mayFreeze) {
|
2011-07-17 23:09:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
// Cancel the periodic GC timer here before freezing. The idle GC timer
|
2013-10-23 17:16:49 +04:00
|
|
|
// will clean everything up once it runs.
|
|
|
|
if (!scheduledIdleGC) {
|
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
scheduledIdleGC = true;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Make sure the periodic timer gets turned back on here.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsresult
|
|
|
|
WorkerPrivate::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aIsOnCurrentThread);
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ASSERT(mPRThread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
*aIsOnCurrentThread = PR_GetCurrentThread() == mPRThread;
|
2013-10-23 17:16:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-05 03:09:23 +04:00
|
|
|
WorkerPrivate::ScheduleDeletion(WorkerRanOrNot aRanOrNot)
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mChildWorkers.IsEmpty());
|
|
|
|
MOZ_ASSERT(mSyncLoopStack.IsEmpty());
|
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
ClearMainEventQueue(aRanOrNot);
|
2014-07-29 17:25:29 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (WorkerRan == aRanOrNot) {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
MOZ_ASSERT(!NS_HasPendingEvents(currentThread));
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (WorkerPrivate* parent = GetParent()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsRefPtr<WorkerFinishedRunnable> runnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new WorkerFinishedRunnable(parent, this);
|
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 =
|
2013-10-23 17:16:49 +04:00
|
|
|
new TopLevelWorkerFinishedRunnable(this);
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(runnable))) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to dispatch runnable!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-07 05:03:46 +04:00
|
|
|
bool
|
2014-05-21 10:06:54 +04:00
|
|
|
WorkerPrivate::BlockAndCollectRuntimeStats(JS::RuntimeStats* aRtStats,
|
|
|
|
bool aAnonymize)
|
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
|
2014-03-11 01:28:43 +04:00
|
|
|
// currently in a ctypes call) then we need to trigger the interrupt
|
2013-02-08 15:50:00 +04:00
|
|
|
// callback to trap the worker.
|
|
|
|
if (!mBlockedForMemoryReporter) {
|
2014-03-11 01:28:43 +04:00
|
|
|
JS_RequestInterruptCallback(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);
|
2014-05-21 10:06:54 +04:00
|
|
|
succeeded = JS::CollectRuntimeStats(rt, aRtStats, nullptr, aAnonymize);
|
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();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mMemoryReporter);
|
2011-09-09 04:03:03 +04:00
|
|
|
|
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-11-07 09:35:30 +04:00
|
|
|
if (NS_FAILED(RegisterWeakMemoryReporter(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-07 09:35:30 +04:00
|
|
|
if (NS_FAILED(UnregisterWeakMemoryReporter(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
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::ProcessAllControlRunnablesLocked()
|
2011-09-09 04:03:03 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2011-09-09 04:03:03 +04:00
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
for (;;) {
|
|
|
|
// Block here if the memory reporter is trying to run.
|
|
|
|
if (mMemoryReporterRunning) {
|
|
|
|
MOZ_ASSERT(!mBlockedForMemoryReporter);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Let the main thread know that we've received the block request and
|
|
|
|
// that memory reporting may proceed.
|
|
|
|
mBlockedForMemoryReporter = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// The main thread is almost certainly waiting so we must notify here.
|
|
|
|
mMemoryReportCondVar.Notify();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Wait for the memory report to finish.
|
|
|
|
while (mMemoryReporterRunning) {
|
|
|
|
mMemoryReportCondVar.Wait();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(mBlockedForMemoryReporter);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// No need to notify here as the main thread isn't watching for this
|
|
|
|
// state.
|
|
|
|
mBlockedForMemoryReporter = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerControlRunnable* event;
|
|
|
|
if (!mControlQueue.Pop(event)) {
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoUnlock unlock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(event);
|
|
|
|
if (NS_FAILED(static_cast<nsIRunnable*>(event)->Run())) {
|
|
|
|
result = false;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
event->Release();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-05 03:09:23 +04:00
|
|
|
WorkerPrivate::ClearMainEventQueue(WorkerRanOrNot aRanOrNot)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mCancelAllPendingRunnables);
|
|
|
|
mCancelAllPendingRunnables = true;
|
|
|
|
|
2014-03-05 03:09:23 +04:00
|
|
|
if (WorkerNeverRan == aRanOrNot) {
|
|
|
|
for (uint32_t count = mPreStartRunnables.Length(), index = 0;
|
|
|
|
index < count;
|
|
|
|
index++) {
|
|
|
|
nsRefPtr<WorkerRunnable> runnable = mPreStartRunnables[index].forget();
|
|
|
|
static_cast<nsIRunnable*>(runnable.get())->Run();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsIThread* currentThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(currentThread);
|
|
|
|
|
|
|
|
NS_ProcessPendingEvents(currentThread);
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mCancelAllPendingRunnables);
|
|
|
|
mCancelAllPendingRunnables = false;
|
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::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
|
2015-04-01 12:00:19 +03:00
|
|
|
WorkerPrivate::FreezeInternal(JSContext* aCx)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
NS_ASSERTION(!mFrozen, "Already frozen!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mFrozen = true;
|
2015-04-01 21:32:20 +03:00
|
|
|
mDebugger->Freeze();
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-04-01 12:00:19 +03:00
|
|
|
WorkerPrivate::ThawInternal(JSContext* aCx)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
NS_ASSERTION(mFrozen, "Not yet frozen!");
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mFrozen = false;
|
2015-04-01 21:32:20 +03:00
|
|
|
mDebugger->Thaw();
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-05 18:16:26 +04:00
|
|
|
WorkerPrivate::TraceTimeouts(const TraceCallbacks& aCallbacks,
|
|
|
|
void* aClosure) const
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
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-11-19 10:05:41 +04:00
|
|
|
|
|
|
|
if (info->mTimeoutCallable.isUndefined()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
aCallbacks.Trace(&info->mTimeoutCallable, "mTimeoutCallable", aClosure);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t index2 = 0; index2 < info->mExtraArgVals.Length(); index2++) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aCallbacks.Trace(&info->mExtraArgVals[index2], "mExtraArgVals[i]", aClosure);
|
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;
|
|
|
|
{
|
2013-10-23 17:16:49 +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);
|
|
|
|
|
2015-01-10 01:28:27 +03:00
|
|
|
if (mStatus >= Canceling) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:51:47 +03:00
|
|
|
MOZ_ASSERT(!mFeatures.Contains(aFeature), "Already know about this one!");
|
2011-07-17 23:09:13 +04:00
|
|
|
mFeatures.AppendElement(aFeature);
|
|
|
|
|
|
|
|
return mFeatures.Length() == 1 ?
|
|
|
|
ModifyBusyCountFromWorker(aCx, true) :
|
|
|
|
true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::RemoveFeature(JSContext* aCx, WorkerFeature* aFeature)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2015-03-09 16:51:47 +03:00
|
|
|
MOZ_ASSERT(mFeatures.Contains(aFeature), "Didn't know about this one!");
|
2011-07-17 23:09:13 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-04-07 20:31:51 +03:00
|
|
|
nsTObserverArray<WorkerFeature*>::ForwardIterator iter(mFeatures);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
WorkerFeature* feature = iter.GetNext();
|
|
|
|
if (!feature->Notify(aCx, aStatus)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
already_AddRefed<nsIEventTarget>
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate::CreateNewSyncLoop()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIThreadInternal> thread = do_QueryInterface(NS_GetCurrentThread());
|
|
|
|
MOZ_ASSERT(thread);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> realEventTarget;
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(thread->PushEventQueue(
|
|
|
|
getter_AddRefs(realEventTarget))));
|
|
|
|
|
|
|
|
nsRefPtr<EventTarget> workerEventTarget =
|
|
|
|
new EventTarget(this, realEventTarget);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Modifications must be protected by mMutex in DEBUG builds, see comment
|
|
|
|
// about mSyncLoopStack in WorkerPrivate.h.
|
|
|
|
#ifdef DEBUG
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mSyncLoopStack.AppendElement(new SyncLoopInfo(workerEventTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
return workerEventTarget.forget();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::RunCurrentSyncLoop()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
JSContext* cx = GetJSContext();
|
|
|
|
MOZ_ASSERT(cx);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// This should not change between now and the time we finish running this sync
|
|
|
|
// loop.
|
|
|
|
uint32_t currentLoopIndex = mSyncLoopStack.Length() - 1;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
SyncLoopInfo* loopInfo = mSyncLoopStack[currentLoopIndex];
|
|
|
|
|
|
|
|
MOZ_ASSERT(loopInfo);
|
|
|
|
MOZ_ASSERT(!loopInfo->mHasRun);
|
|
|
|
MOZ_ASSERT(!loopInfo->mCompleted);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
loopInfo->mHasRun = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (!loopInfo->mCompleted) {
|
|
|
|
bool normalRunnablesPending = false;
|
|
|
|
|
|
|
|
// Don't block with the periodic GC timer running.
|
2014-11-17 22:55:37 +03:00
|
|
|
if (!NS_HasPendingEvents(mThread)) {
|
2013-10-23 17:16:49 +04:00
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for something to do.
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
for (;;) {
|
|
|
|
while (mControlQueue.IsEmpty() &&
|
|
|
|
!normalRunnablesPending &&
|
2014-11-17 22:55:37 +03:00
|
|
|
!(normalRunnablesPending = NS_HasPendingEvents(mThread))) {
|
2013-10-23 17:16:49 +04:00
|
|
|
WaitForWorkerEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessAllControlRunnablesLocked();
|
|
|
|
|
2014-01-14 03:22:53 +04:00
|
|
|
// NB: If we processed a NotifyRunnable, we might have run non-control
|
|
|
|
// runnables, one of which may have shut down the sync loop.
|
|
|
|
if (normalRunnablesPending || loopInfo->mCompleted) {
|
2013-10-23 17:16:49 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 03:22:53 +04:00
|
|
|
if (normalRunnablesPending) {
|
|
|
|
// Make sure the periodic timer is running before we continue.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_ProcessNextEvent(mThread, false));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2014-01-14 03:22:53 +04:00
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
|
|
|
JS_MaybeGC(cx);
|
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the stack didn't change underneath us.
|
|
|
|
MOZ_ASSERT(mSyncLoopStack[currentLoopIndex] == loopInfo);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
return DestroySyncLoop(currentLoopIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::DestroySyncLoop(uint32_t aLoopIndex, nsIThreadInternal* aThread)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mSyncLoopStack.IsEmpty());
|
|
|
|
MOZ_ASSERT(mSyncLoopStack.Length() - 1 == aLoopIndex);
|
|
|
|
|
|
|
|
if (!aThread) {
|
2014-11-17 22:55:37 +03:00
|
|
|
aThread = mThread;
|
2014-01-11 04:37:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're about to delete the loop, stash its event target and result.
|
|
|
|
SyncLoopInfo* loopInfo = mSyncLoopStack[aLoopIndex];
|
2013-10-23 17:16:49 +04:00
|
|
|
nsIEventTarget* nestedEventTarget =
|
|
|
|
loopInfo->mEventTarget->GetWeakNestedEventTarget();
|
|
|
|
MOZ_ASSERT(nestedEventTarget);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
bool result = loopInfo->mResult;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
// Modifications must be protected by mMutex in DEBUG builds, see comment
|
|
|
|
// about mSyncLoopStack in WorkerPrivate.h.
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef DEBUG
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 23:09:13 +04:00
|
|
|
#endif
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
// This will delete |loopInfo|!
|
|
|
|
mSyncLoopStack.RemoveElementAt(aLoopIndex);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-01-11 04:37:47 +04:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(aThread->PopEventQueue(nestedEventTarget)));
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
return result;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::StopSyncLoop(nsIEventTarget* aSyncLoopTarget, bool aResult)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
AssertValidSyncLoop(aSyncLoopTarget);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mSyncLoopStack.IsEmpty());
|
|
|
|
|
|
|
|
for (uint32_t index = mSyncLoopStack.Length(); index > 0; index--) {
|
|
|
|
nsAutoPtr<SyncLoopInfo>& loopInfo = mSyncLoopStack[index - 1];
|
|
|
|
MOZ_ASSERT(loopInfo);
|
|
|
|
MOZ_ASSERT(loopInfo->mEventTarget);
|
|
|
|
|
|
|
|
if (loopInfo->mEventTarget == aSyncLoopTarget) {
|
|
|
|
// Can't assert |loop->mHasRun| here because dispatch failures can cause
|
|
|
|
// us to bail out early.
|
|
|
|
MOZ_ASSERT(!loopInfo->mCompleted);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
loopInfo->mResult = aResult;
|
|
|
|
loopInfo->mCompleted = true;
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
loopInfo->mEventTarget->Disable();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!SameCOMIdentity(loopInfo->mEventTarget, aSyncLoopTarget));
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("Unknown sync loop!");
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#ifdef DEBUG
|
2012-12-22 00:14:47 +04:00
|
|
|
void
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::AssertValidSyncLoop(nsIEventTarget* aSyncLoopTarget)
|
2012-12-22 00:14:47 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aSyncLoopTarget);
|
|
|
|
|
|
|
|
EventTarget* workerTarget;
|
|
|
|
nsresult rv =
|
|
|
|
aSyncLoopTarget->QueryInterface(kDEBUGWorkerEventTargetIID,
|
|
|
|
reinterpret_cast<void**>(&workerTarget));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(workerTarget);
|
|
|
|
|
|
|
|
bool valid = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mSyncLoopStack.Length(); index++) {
|
|
|
|
nsAutoPtr<SyncLoopInfo>& loopInfo = mSyncLoopStack[index];
|
|
|
|
MOZ_ASSERT(loopInfo);
|
|
|
|
MOZ_ASSERT(loopInfo->mEventTarget);
|
|
|
|
|
|
|
|
if (loopInfo->mEventTarget == aSyncLoopTarget) {
|
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!SameCOMIdentity(loopInfo->mEventTarget, aSyncLoopTarget));
|
|
|
|
}
|
|
|
|
}
|
2012-12-22 00:14:47 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(valid);
|
2012-12-22 00:14:47 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
#endif
|
2012-12-22 00:14:47 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::PostMessageToParentInternal(
|
|
|
|
JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
bool aToMessagePort,
|
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
2014-02-12 14:50:46 +04:00
|
|
|
|
|
|
|
// The input sequence only comes from the generated bindings code, which
|
|
|
|
// ensures it is rooted.
|
|
|
|
JS::HandleValueArray elements =
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(realTransferable.Length(),
|
|
|
|
realTransferable.Elements());
|
|
|
|
|
|
|
|
JSObject* array = JS_NewArrayObject(aCx, elements);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!array) {
|
|
|
|
aRv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const JSStructuredCloneCallbacks* callbacks =
|
2011-08-16 07:40:38 +04:00
|
|
|
IsChromeWorker() ?
|
|
|
|
&gChromeWorkerStructuredCloneCallbacks :
|
|
|
|
&gWorkerStructuredCloneCallbacks;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>> clonedObjects;
|
2011-08-16 07:40:38 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!buffer.write(aCx, aMessage, transferable, callbacks, &clonedObjects)) {
|
|
|
|
aRv = NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
return;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MessageEventRunnable> runnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new MessageEventRunnable(this,
|
|
|
|
WorkerRunnable::ParentThreadUnchangedBusyCount,
|
2014-02-01 06:50:07 +04:00
|
|
|
Move(buffer), clonedObjects, aToMessagePort,
|
2013-10-23 17:16:49 +04:00
|
|
|
aMessagePortSerial);
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
aRv = NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::PostMessageToParentMessagePort(
|
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t aMessagePortSerial,
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Handle<JS::Value> aMessage,
|
2013-06-05 18:04:23 +04:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!mWorkerPorts.GetWeak(aMessagePortSerial)) {
|
2013-06-05 18:04:23 +04:00
|
|
|
// This port has been closed from the main thread. There's no point in
|
|
|
|
// sending this message so just bail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
PostMessageToParentInternal(aCx, aMessage, aTransferable, true,
|
|
|
|
aMessagePortSerial, aRv);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-03-27 09:17:16 +03:00
|
|
|
void
|
|
|
|
WorkerPrivate::EnterDebuggerEventLoop()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
JSContext* cx = GetJSContext();
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
|
|
|
|
uint32_t currentEventLoopLevel = ++mDebuggerEventLoopLevel;
|
|
|
|
|
|
|
|
while (currentEventLoopLevel <= mDebuggerEventLoopLevel) {
|
|
|
|
bool debuggerRunnablesPending = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
debuggerRunnablesPending = !mDebuggerQueue.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't block with the periodic GC timer running.
|
|
|
|
if (!debuggerRunnablesPending) {
|
|
|
|
SetGCTimerMode(IdleTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for something to do
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
while (mControlQueue.IsEmpty() &&
|
|
|
|
!(debuggerRunnablesPending = !mDebuggerQueue.IsEmpty())) {
|
|
|
|
WaitForWorkerEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessAllControlRunnablesLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debuggerRunnablesPending) {
|
|
|
|
// Start the periodic GC timer if it is not already running.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
|
|
|
|
|
|
|
WorkerRunnable* runnable;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
mDebuggerQueue.Pop(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
static_cast<nsIRunnable*>(runnable)->Run();
|
|
|
|
runnable->Release();
|
|
|
|
|
|
|
|
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
|
|
|
JS_MaybeGC(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::LeaveDebuggerEventLoop()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mDebuggerEventLoopLevel > 0) {
|
|
|
|
--mDebuggerEventLoopLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:15:59 +03:00
|
|
|
void
|
|
|
|
WorkerPrivate::PostMessageToDebugger(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
mDebugger->PostMessageToDebugger(aMessage);
|
|
|
|
}
|
|
|
|
|
2015-03-30 14:54:38 +03:00
|
|
|
void
|
|
|
|
WorkerPrivate::SetDebuggerImmediate(JSContext* aCx, Function& aHandler,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsRefPtr<DebuggerImmediateRunnable> runnable =
|
|
|
|
new DebuggerImmediateRunnable(this, aHandler);
|
|
|
|
if (!runnable->Dispatch(aCx)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:09:45 +03:00
|
|
|
void
|
|
|
|
WorkerPrivate::ReportErrorToDebugger(const nsAString& aFilename,
|
|
|
|
uint32_t aLineno,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
mDebugger->ReportErrorToDebugger(aFilename, aLineno, aMessage);
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivate::NotifyInternal(JSContext* aCx, Status aStatus)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
NS_ASSERTION(aStatus > Running && aStatus < Dead, "Bad status!");
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsRefPtr<EventTarget> eventTarget;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Save the old status and set the new status.
|
|
|
|
Status previousStatus;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mStatus >= aStatus) {
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mEventTarget);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousStatus = mStatus;
|
|
|
|
mStatus = aStatus;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
mEventTarget.swap(eventTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that mStatus > Running, no-one can create a new WorkerEventTarget or
|
|
|
|
// WorkerCrossThreadDispatcher if we don't already have one.
|
|
|
|
if (eventTarget) {
|
|
|
|
// 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* the WorkerEventTarget's mutex when they're both
|
|
|
|
// held.
|
|
|
|
eventTarget->Disable();
|
|
|
|
eventTarget = nullptr;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
if (mCrossThreadDispatcher) {
|
|
|
|
// Since we'll no longer process events, make sure we no longer allow
|
2013-10-23 17:16:49 +04:00
|
|
|
// 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.
|
2011-12-05 11:58:27 +04:00
|
|
|
mCrossThreadDispatcher->Forget();
|
2013-10-23 17:16:49 +04:00
|
|
|
mCrossThreadDispatcher = nullptr;
|
2011-12-05 11:58:27 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(previousStatus != Pending);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(previousStatus >= Canceling || mKillTime.IsNull());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// 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) {
|
2014-03-05 03:09:23 +04:00
|
|
|
ClearMainEventQueue(WorkerRan);
|
2012-10-03 06:03:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mCloseHandlerStarted && !mCloseHandlerFinished);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-10-03 06:03:28 +04:00
|
|
|
nsRefPtr<CloseEventRunnable> closeRunnable = new CloseEventRunnable(this);
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(closeRunnable)));
|
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.
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(previousStatus >= Running && previousStatus <= Terminating);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aStatus == Killing);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
mKillTime = TimeStamp::Now();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mCloseHandlerStarted && !mCloseHandlerFinished) {
|
|
|
|
ScheduleKillCloseEventRunnable(aCx);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Always abort the script.
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerPrivate::ScheduleKillCloseEventRunnable(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mKillTime.IsNull());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
nsRefPtr<KillCloseEventRunnable> killCloseEventRunnable =
|
|
|
|
new KillCloseEventRunnable(this);
|
|
|
|
if (!killCloseEventRunnable->SetTimeout(aCx, RemainingRunTimeMS())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToCurrentThread(
|
|
|
|
killCloseEventRunnable)));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
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) {
|
2014-01-30 21:30:30 +04:00
|
|
|
// ErrorEvent objects don't have a |name| field the way ES |Error| objects
|
|
|
|
// do. Traditionally (and mostly by accident), the |message| field of
|
|
|
|
// ErrorEvent has corresponded to |Name: Message| of the original Error
|
|
|
|
// object. Things have been cleaned up in the JS engine, so now we need to
|
|
|
|
// format this string explicitly.
|
|
|
|
JS::Rooted<JSString*> messageStr(aCx,
|
|
|
|
js::ErrorReportToString(aCx, aReport));
|
|
|
|
if (messageStr) {
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString autoStr;
|
|
|
|
if (autoStr.init(aCx, messageStr)) {
|
|
|
|
message = autoStr;
|
2014-01-30 21:30:30 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t
|
|
|
|
WorkerPrivate::SetTimeout(JSContext* aCx,
|
|
|
|
Function* aHandler,
|
|
|
|
const nsAString& aStringHandler,
|
|
|
|
int32_t aTimeout,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
bool aIsInterval,
|
|
|
|
ErrorResult& aRv)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
const int32_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) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoPtr<TimeoutInfo> newInfo(new TimeoutInfo());
|
|
|
|
newInfo->mIsInterval = aIsInterval;
|
|
|
|
newInfo->mId = timerId;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (MOZ_UNLIKELY(timerId == INT32_MAX)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Timeout ids overflowed!");
|
|
|
|
mNextTimeoutId = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take care of the main argument.
|
2013-11-05 18:16:26 +04:00
|
|
|
if (aHandler) {
|
|
|
|
newInfo->mTimeoutCallable = JS::ObjectValue(*aHandler->Callable());
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
else if (!aStringHandler.IsEmpty()) {
|
|
|
|
newInfo->mTimeoutString = aStringHandler;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
JS_ReportError(aCx, "Useless %s call (missing quotes around argument?)",
|
|
|
|
aIsInterval ? "setInterval" : "setTimeout");
|
2013-11-05 18:16:26 +04:00
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// See if any of the optional arguments were passed.
|
2013-11-05 18:16:26 +04:00
|
|
|
aTimeout = std::max(0, aTimeout);
|
|
|
|
newInfo->mInterval = TimeDuration::FromMilliseconds(aTimeout);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
uint32_t argc = aArguments.Length();
|
|
|
|
if (argc && !newInfo->mTimeoutCallable.isUndefined()) {
|
|
|
|
nsTArray<JS::Heap<JS::Value>> extraArgVals(argc);
|
|
|
|
for (uint32_t index = 0; index < argc; index++) {
|
|
|
|
extraArgVals.AppendElement(aArguments[index]);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
newInfo->mExtraArgVals.SwapElements(extraArgVals);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
newInfo->mTargetTime = TimeStamp::Now() + newInfo->mInterval;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!newInfo->mTimeoutString.IsEmpty()) {
|
2015-01-06 23:50:29 +03:00
|
|
|
if (!nsJSUtils::GetCallingLocation(aCx, newInfo->mFilename, &newInfo->mLineNumber)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
NS_WARNING("Failed to get calling location!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsAutoPtr<TimeoutInfo>* insertedInfo =
|
|
|
|
mTimeouts.InsertElementSorted(newInfo.forget(), GetAutoPtrComparator(mTimeouts));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
// If the timeout we just made is set to fire next then we need to update the
|
|
|
|
// timer.
|
2013-11-05 18:16:26 +04:00
|
|
|
if (insertedInfo == mTimeouts.Elements()) {
|
2011-07-17 23:09:13 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mTimer) {
|
2013-10-23 17:16:49 +04:00
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(rv);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsRefPtr<TimerRunnable> runnable = new TimerRunnable(this);
|
|
|
|
|
|
|
|
nsRefPtr<TimerThreadEventTarget> target =
|
|
|
|
new TimerThreadEventTarget(this, runnable);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
rv = timer->SetTarget(target);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(rv);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
timer.swap(mTimer);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mTimerRunning) {
|
|
|
|
if (!ModifyBusyCountFromWorker(aCx, true)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
mTimerRunning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RescheduleTimeoutTimer(aCx)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return timerId;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::ClearTimeout(int32_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
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
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!info->mTimeoutCallable.isUndefined()) {
|
2013-05-04 11:53:00 +04:00
|
|
|
JS::Rooted<JS::Value> rval(aCx);
|
2014-02-11 14:59:16 +04:00
|
|
|
JS::HandleValueArray args =
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(info->mExtraArgVals.Length(),
|
2014-02-13 19:33:04 +04:00
|
|
|
info->mExtraArgVals.Elements()->address());
|
|
|
|
JS::Rooted<JS::Value> callable(aCx, info->mTimeoutCallable);
|
|
|
|
if (!JS_CallFunctionValue(aCx, global, callable, args, &rval) &&
|
2011-08-26 11:34:10 +04:00
|
|
|
!JS_ReportPendingException(aCx)) {
|
|
|
|
retval = false;
|
|
|
|
break;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
else {
|
|
|
|
nsString expression = info->mTimeoutString;
|
|
|
|
|
|
|
|
JS::CompileOptions options(aCx);
|
2014-07-31 20:55:20 +04:00
|
|
|
options.setFileAndLine(info->mFilename.get(), info->mLineNumber)
|
|
|
|
.setNoScriptRval(true);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2014-11-19 07:27:16 +03:00
|
|
|
JS::Rooted<JS::Value> unused(aCx);
|
2013-11-05 18:16:26 +04:00
|
|
|
if ((expression.IsEmpty() ||
|
2015-03-14 08:36:16 +03:00
|
|
|
!JS::Evaluate(aCx, options,
|
2014-11-19 07:27:16 +03:00
|
|
|
expression.get(), expression.Length(), &unused)) &&
|
2013-11-05 18:16:26 +04:00
|
|
|
!JS_ReportPendingException(aCx)) {
|
|
|
|
retval = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 11:34:10 +04:00
|
|
|
|
2015-05-07 21:49:31 +03:00
|
|
|
// Since we might be processing more timeouts, go ahead and flush
|
|
|
|
// the promise queue now before we do that.
|
|
|
|
Promise::PerformMicroTaskCheckpoint();
|
|
|
|
|
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
|
2014-07-30 08:00:37 +04:00
|
|
|
WorkerPrivate::UpdateRuntimeOptionsInternal(
|
2014-02-26 13:25:36 +04:00
|
|
|
JSContext* aCx,
|
2014-07-30 08:00:37 +04:00
|
|
|
const JS::RuntimeOptions& aRuntimeOptions)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2014-02-26 13:25:36 +04:00
|
|
|
JS::RuntimeOptionsRef(aCx) = aRuntimeOptions;
|
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++) {
|
2014-07-30 08:00:37 +04:00
|
|
|
mChildWorkers[index]->UpdateRuntimeOptions(aCx, aRuntimeOptions);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 18:26:34 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::UpdateLanguagesInternal(JSContext* aCx,
|
|
|
|
const nsTArray<nsString>& aLanguages)
|
|
|
|
{
|
|
|
|
WorkerGlobalScope* globalScope = GlobalScope();
|
|
|
|
if (globalScope) {
|
|
|
|
nsRefPtr<WorkerNavigator> nav = globalScope->GetExistingNavigator();
|
|
|
|
if (nav) {
|
|
|
|
nav->SetLanguages(aLanguages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
|
|
|
mChildWorkers[index]->UpdateLanguages(aCx, aLanguages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:27:15 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::UpdatePreferenceInternal(JSContext* aCx, WorkerPreference aPref, bool aValue)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aPref >= 0 && aPref < WORKERPREF_COUNT);
|
|
|
|
|
|
|
|
mPreferences[aPref] = aValue;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
|
|
|
mChildWorkers[index]->UpdatePreference(aCx, aPref, aValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2012-01-18 00:05:25 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::GarbageCollectInternal(JSContext* aCx, bool aShrinking,
|
|
|
|
bool aCollectChildren)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (!JS::CurrentGlobalOrNull(aCx)) {
|
|
|
|
// We haven't compiled anything yet. Just bail out.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-01-02 20:19:43 +03:00
|
|
|
JS::GCForReason(rt, GC_SHRINK, JS::gcreason::DOM_WORKER);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (!aCollectChildren) {
|
|
|
|
LOG(("Worker %p collected idle garbage\n", this));
|
|
|
|
}
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-01-02 20:19:43 +03:00
|
|
|
JS::GCForReason(rt, GC_NORMAL, JS::gcreason::DOM_WORKER);
|
2013-10-23 17:16:49 +04:00
|
|
|
LOG(("Worker %p collected garbage\n", this));
|
2013-05-17 02:49:43 +04:00
|
|
|
}
|
2012-01-18 00:05:25 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-05-17 02:49:43 +04:00
|
|
|
JS_MaybeGC(aCx);
|
2013-10-23 17:16:49 +04:00
|
|
|
LOG(("Worker %p collected periodic garbage\n", this));
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 08:07:02 +04:00
|
|
|
void
|
|
|
|
WorkerPrivate::CycleCollectInternal(JSContext* aCx, bool aCollectChildren)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsCycleCollector_collect(nullptr);
|
|
|
|
|
|
|
|
if (aCollectChildren) {
|
|
|
|
for (uint32_t index = 0; index < mChildWorkers.Length(); index++) {
|
|
|
|
mChildWorkers[index]->CycleCollect(aCx, /* dummy = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 01:48:00 +04:00
|
|
|
void
|
2014-11-17 22:55:37 +03:00
|
|
|
WorkerPrivate::SetThread(WorkerThread* aThread)
|
2013-06-23 01:48:00 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
if (aThread) {
|
2014-11-17 22:55:37 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
bool isOnCurrentThread;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(aThread->IsOnCurrentThread(&isOnCurrentThread)));
|
|
|
|
MOZ_ASSERT(isOnCurrentThread);
|
|
|
|
}
|
|
|
|
#endif
|
2013-06-23 01:48:00 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(!mPRThread);
|
|
|
|
mPRThread = PRThreadFromThread(aThread);
|
|
|
|
MOZ_ASSERT(mPRThread);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(mPRThread);
|
|
|
|
}
|
2013-06-23 01:48:00 +04:00
|
|
|
|
2014-11-17 22:55:37 +03:00
|
|
|
const WorkerThreadFriendKey friendKey;
|
|
|
|
|
|
|
|
nsRefPtr<WorkerThread> doomedThread;
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
{ // Scope so that |doomedThread| is released without holding the lock.
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (aThread) {
|
|
|
|
MOZ_ASSERT(!mThread);
|
|
|
|
MOZ_ASSERT(mStatus == Pending);
|
|
|
|
|
|
|
|
mThread = aThread;
|
2014-11-17 22:55:37 +03:00
|
|
|
mThread->SetWorker(friendKey, this);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
if (!mPreStartRunnables.IsEmpty()) {
|
|
|
|
for (uint32_t index = 0; index < mPreStartRunnables.Length(); index++) {
|
2014-11-17 22:55:37 +03:00
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
|
|
|
mThread->Dispatch(friendKey, mPreStartRunnables[index])));
|
2013-10-23 17:16:49 +04:00
|
|
|
}
|
|
|
|
mPreStartRunnables.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(mThread);
|
2014-11-17 22:55:37 +03:00
|
|
|
|
|
|
|
mThread->SetWorker(friendKey, nullptr);
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
mThread.swap(doomedThread);
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 01:48:00 +04:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Don't try to GC while we're blocked in a ctypes call.
|
|
|
|
SetGCTimerMode(NoTimer);
|
|
|
|
|
2012-12-30 22:21:52 +04:00
|
|
|
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();
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_ASSERTION(mBlockedForMemoryReporter, "Somehow we got unblocked!");
|
2012-12-30 22:21:52 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// 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;
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Make sure the periodic timer is running before we start running JS again.
|
|
|
|
SetGCTimerMode(PeriodicTimer);
|
2012-12-30 22:21:52 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
bool
|
|
|
|
WorkerPrivate::ConnectMessagePort(JSContext* aCx, uint64_t aMessagePortSerial)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_ASSERTION(!mWorkerPorts.GetWeak(aMessagePortSerial),
|
2013-06-05 18:04:23 +04:00
|
|
|
"Already have this port registered!");
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
WorkerGlobalScope* globalScope = GlobalScope();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JS::Rooted<JSObject*> jsGlobal(aCx, globalScope->GetWrapper());
|
|
|
|
MOZ_ASSERT(jsGlobal);
|
|
|
|
|
|
|
|
nsRefPtr<MessagePort> port = new MessagePort(this, aMessagePortSerial);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
GlobalObject globalObject(aCx, jsGlobal);
|
|
|
|
if (globalObject.Failed()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-22 00:42:50 +04:00
|
|
|
RootedDictionary<MessageEventInit> init(aCx);
|
2013-11-22 00:39:43 +04:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
2013-12-20 12:51:03 +04:00
|
|
|
init.mSource.SetValue().SetAsMessagePort() = port;
|
2013-11-22 00:39:43 +04:00
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
|
2014-02-27 14:51:14 +04:00
|
|
|
nsRefPtr<MessageEvent> event =
|
2014-06-16 20:52:00 +04:00
|
|
|
MessageEvent::Constructor(globalObject,
|
2014-02-27 14:51:14 +04:00
|
|
|
NS_LITERAL_STRING("connect"), init, rv);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<MessagePortBase>> ports;
|
|
|
|
ports.AppendElement(port);
|
|
|
|
|
|
|
|
nsRefPtr<MessagePortList> portList =
|
|
|
|
new MessagePortList(static_cast<nsIDOMEventTarget*>(globalScope), ports);
|
|
|
|
event->SetPorts(portList);
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
mWorkerPorts.Put(aMessagePortSerial, port);
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsCOMPtr<nsIDOMEvent> domEvent = do_QueryObject(event);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsEventStatus dummy = nsEventStatus_eIgnore;
|
|
|
|
globalScope->DispatchDOMEvent(nullptr, domEvent, nullptr, &dummy);
|
2013-06-05 18:04:23 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerPrivate::DisconnectMessagePort(uint64_t aMessagePortSerial)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
mWorkerPorts.Remove(aMessagePortSerial);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
workers::MessagePort*
|
2013-06-05 18:04:23 +04:00
|
|
|
WorkerPrivate::GetMessagePort(uint64_t aMessagePortSerial)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
nsRefPtr<MessagePort> port;
|
|
|
|
if (mWorkerPorts.Get(aMessagePortSerial, getter_AddRefs(port))) {
|
2013-06-05 18:04:23 +04:00
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
WorkerGlobalScope*
|
|
|
|
WorkerPrivate::GetOrCreateGlobalScope(JSContext* aCx)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
if (!mScope) {
|
|
|
|
nsRefPtr<WorkerGlobalScope> globalScope;
|
|
|
|
if (IsSharedWorker()) {
|
|
|
|
globalScope = new SharedWorkerGlobalScope(this, SharedWorkerName());
|
|
|
|
} else if (IsServiceWorker()) {
|
|
|
|
globalScope = new ServiceWorkerGlobalScope(this, SharedWorkerName());
|
|
|
|
} else {
|
|
|
|
globalScope = new DedicatedWorkerGlobalScope(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx);
|
|
|
|
NS_ENSURE_TRUE(globalScope->WrapGlobalObject(aCx, &global), nullptr);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(aCx, global);
|
|
|
|
|
|
|
|
if (!RegisterBindings(aCx, global)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FireOnNewGlobalObject(aCx, global);
|
|
|
|
|
|
|
|
mScope = globalScope.forget();
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
return mScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebuggerGlobalScope*
|
|
|
|
WorkerPrivate::CreateDebuggerGlobalScope(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mDebuggerScope);
|
|
|
|
|
|
|
|
nsRefPtr<WorkerDebuggerGlobalScope> globalScope =
|
|
|
|
new WorkerDebuggerGlobalScope(this);
|
|
|
|
|
2015-01-09 00:56:42 +03:00
|
|
|
JS::Rooted<JSObject*> global(aCx);
|
2015-03-04 02:51:53 +03:00
|
|
|
NS_ENSURE_TRUE(globalScope->WrapGlobalObject(aCx, &global), nullptr);
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
JSAutoCompartment ac(aCx, global);
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
if (!JS_DefineDebuggerObject(aCx, global)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FireOnNewGlobalObject(aCx, global);
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
mDebuggerScope = globalScope.forget();
|
|
|
|
|
|
|
|
return mDebuggerScope;
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
#ifdef DEBUG
|
2013-10-23 17:16:49 +04:00
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
void
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate::AssertIsOnWorkerThread() const
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// This is much more complicated than it needs to be but we can't use mThread
|
|
|
|
// because it must be protected by mMutex and sometimes this method is called
|
|
|
|
// when mMutex is already locked. This method should always work.
|
|
|
|
MOZ_ASSERT(mPRThread,
|
|
|
|
"AssertIsOnWorkerThread() called before a thread was assigned!");
|
|
|
|
|
|
|
|
MOZ_ASSERT(nsThreadManager::get());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv =
|
|
|
|
nsThreadManager::get()->GetThreadFromPRThread(mPRThread,
|
|
|
|
getter_AddRefs(thread));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(thread);
|
|
|
|
|
|
|
|
bool current;
|
|
|
|
rv = thread->IsOnCurrentThread(¤t);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(current, "Wrong thread!");
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(ExternalRunnableWrapper, WorkerRunnable)
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
template <class Derived>
|
2013-10-23 17:16:49 +04:00
|
|
|
NS_IMPL_ADDREF(WorkerPrivateParent<Derived>::EventTarget)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMPL_RELEASE(WorkerPrivateParent<Derived>::EventTarget)
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_INTERFACE_MAP_BEGIN(WorkerPrivateParent<Derived>::EventTarget)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEventTarget)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
#ifdef DEBUG
|
|
|
|
// kDEBUGWorkerEventTargetIID is special in that it does not AddRef its
|
|
|
|
// result.
|
|
|
|
if (aIID.Equals(kDEBUGWorkerEventTargetIID)) {
|
|
|
|
*aInstancePtr = this;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
template <class Derived>
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerPrivateParent<Derived>::
|
|
|
|
EventTarget::Dispatch(nsIRunnable* aRunnable, uint32_t aFlags)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Workers only support asynchronous dispatch for now.
|
|
|
|
if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
nsRefPtr<WorkerRunnable> workerRunnable;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
NS_WARNING("A runnable was posted to a worker that is already shutting "
|
|
|
|
"down!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRunnable) {
|
|
|
|
workerRunnable = mWorkerPrivate->MaybeWrapAsWorkerRunnable(aRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
mWorkerPrivate->DispatchPrivate(workerRunnable, mNestedEventTarget);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
template <class Derived>
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WorkerPrivateParent<Derived>::
|
|
|
|
EventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
// May be called on any thread!
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(aIsOnCurrentThread);
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate) {
|
|
|
|
NS_WARNING("A worker's event target was used after the worker has !");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mWorkerPrivate->IsOnCurrentThread(aIsOnCurrentThread);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2011-08-05 02:01:45 +04:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
WorkerCrossThreadDispatcher*
|
|
|
|
GetWorkerCrossThreadDispatcher(JSContext* aCx, JS::Value aWorker)
|
|
|
|
{
|
|
|
|
if (!aWorker.isObject()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* w = nullptr;
|
2013-11-21 16:51:16 +04:00
|
|
|
UNWRAP_OBJECT(Worker, &aWorker.toObject(), w);
|
2013-11-05 18:16:24 +04:00
|
|
|
MOZ_ASSERT(w);
|
|
|
|
return w->GetCrossThreadDispatcher();
|
|
|
|
}
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const 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
|
|
|
}
|
|
|
|
|
2014-12-17 03:30:39 +03:00
|
|
|
const 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
|
|
|
}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Force instantiation.
|
|
|
|
template class WorkerPrivateParent<WorkerPrivate>;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
END_WORKERS_NAMESPACE
|