2015-05-03 19:32:37 +00: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 11:12:37 +00: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 19:09:13 +00:00
|
|
|
|
|
|
|
#ifndef mozilla_dom_workers_workerprivate_h__
|
|
|
|
#define mozilla_dom_workers_workerprivate_h__
|
|
|
|
|
2018-01-31 07:24:59 +00:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "mozilla/CondVar.h"
|
2014-04-01 06:13:50 +00:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2018-01-30 09:14:00 +00:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "nsIEventTarget.h"
|
2015-04-07 17:31:51 +00:00
|
|
|
#include "nsTObserverArray.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2018-02-08 07:26:05 +00:00
|
|
|
#include "mozilla/dom/Worker.h"
|
2018-01-31 07:24:08 +00:00
|
|
|
#include "mozilla/dom/WorkerHolder.h"
|
|
|
|
#include "mozilla/dom/WorkerLoadInfo.h"
|
2018-01-31 07:24:59 +00:00
|
|
|
#include "mozilla/dom/workerinternals/JSSettings.h"
|
|
|
|
#include "mozilla/dom/workerinternals/Queue.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-05-23 06:56:46 +00:00
|
|
|
class nsIConsoleReportCollector;
|
2014-01-11 00:37:47 +00:00
|
|
|
class nsIThreadInternal;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2018-01-30 09:14:00 +00:00
|
|
|
|
2018-02-02 16:35:28 +00:00
|
|
|
// If you change this, the corresponding list in nsIWorkerDebugger.idl needs
|
|
|
|
// to be updated too.
|
|
|
|
enum WorkerType
|
|
|
|
{
|
|
|
|
WorkerTypeDedicated,
|
|
|
|
WorkerTypeShared,
|
|
|
|
WorkerTypeService
|
|
|
|
};
|
|
|
|
|
2017-11-21 20:13:04 +00:00
|
|
|
class ClientInfo;
|
|
|
|
class ClientSource;
|
2013-11-05 14:16:26 +00:00
|
|
|
class Function;
|
2015-09-16 03:27:56 +00:00
|
|
|
class MessagePort;
|
|
|
|
class MessagePortIdentifier;
|
2018-01-24 16:17:31 +00:00
|
|
|
class PerformanceStorage;
|
2018-01-31 07:19:10 +00:00
|
|
|
class SharedWorker;
|
2018-01-31 07:22:56 +00:00
|
|
|
class WorkerControlRunnable;
|
2018-01-31 07:21:06 +00:00
|
|
|
class WorkerDebugger;
|
2016-06-28 17:47:23 +00:00
|
|
|
class WorkerDebuggerGlobalScope;
|
2018-01-30 09:12:50 +00:00
|
|
|
class WorkerErrorReport;
|
2018-01-31 07:20:25 +00:00
|
|
|
class WorkerEventTarget;
|
2016-06-28 17:47:23 +00:00
|
|
|
class WorkerGlobalScope;
|
2018-01-31 07:22:56 +00:00
|
|
|
class WorkerRunnable;
|
2018-01-31 07:20:07 +00:00
|
|
|
class WorkerThread;
|
2013-11-05 14:16:26 +00:00
|
|
|
|
2013-02-08 11:50:00 +00:00
|
|
|
// SharedMutex is a small wrapper around an (internal) reference-counted Mutex
|
|
|
|
// object. It exists to avoid changing a lot of code to use Mutex* instead of
|
|
|
|
// Mutex&.
|
|
|
|
class SharedMutex
|
|
|
|
{
|
|
|
|
typedef mozilla::Mutex Mutex;
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class RefCountedMutex final : public Mutex
|
2013-02-08 11:50:00 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit RefCountedMutex(const char* aName)
|
2013-02-08 11:50:00 +00:00
|
|
|
: Mutex(aName)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefCountedMutex)
|
|
|
|
|
|
|
|
private:
|
|
|
|
~RefCountedMutex()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<RefCountedMutex> mMutex;
|
2013-02-08 11:50:00 +00:00
|
|
|
|
|
|
|
public:
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit SharedMutex(const char* aName)
|
2013-02-08 11:50:00 +00:00
|
|
|
: mMutex(new RefCountedMutex(aName))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
SharedMutex(SharedMutex& aOther)
|
|
|
|
: mMutex(aOther.mMutex)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
operator Mutex&()
|
|
|
|
{
|
|
|
|
return *mMutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const Mutex&() const
|
|
|
|
{
|
|
|
|
return *mMutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertCurrentThreadOwns() const
|
|
|
|
{
|
|
|
|
mMutex->AssertCurrentThreadOwns();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
class WorkerPrivate
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct LocationInfo
|
|
|
|
{
|
|
|
|
nsCString mHref;
|
|
|
|
nsCString mProtocol;
|
|
|
|
nsCString mHost;
|
|
|
|
nsCString mHostname;
|
|
|
|
nsCString mPort;
|
|
|
|
nsCString mPathname;
|
|
|
|
nsCString mSearch;
|
|
|
|
nsCString mHash;
|
2014-01-31 18:22:52 +00:00
|
|
|
nsString mOrigin;
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(WorkerPrivate)
|
|
|
|
|
2014-05-29 16:19:00 +00:00
|
|
|
static already_AddRefed<WorkerPrivate>
|
|
|
|
Constructor(JSContext* aCx, const nsAString& aScriptURL, bool aIsChromeWorker,
|
2017-05-17 14:49:34 +00:00
|
|
|
WorkerType aWorkerType, const nsAString& aWorkerName,
|
|
|
|
const nsACString& aServiceWorkerScope,
|
2015-02-12 09:50:05 +00:00
|
|
|
WorkerLoadInfo* aLoadInfo, ErrorResult& aRv);
|
2014-05-29 16:19:00 +00:00
|
|
|
|
2015-02-21 15:09:17 +00:00
|
|
|
enum LoadGroupBehavior
|
|
|
|
{
|
|
|
|
InheritLoadGroup,
|
|
|
|
OverrideLoadGroup
|
|
|
|
};
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
static nsresult
|
2016-01-30 17:05:36 +00:00
|
|
|
GetLoadInfo(JSContext* aCx, nsPIDOMWindowInner* aWindow,
|
|
|
|
WorkerPrivate* aParent,
|
2013-06-05 14:04:23 +00:00
|
|
|
const nsAString& aScriptURL, bool aIsChromeWorker,
|
2015-06-17 01:21:08 +00:00
|
|
|
LoadGroupBehavior aLoadGroupBehavior, WorkerType aWorkerType,
|
|
|
|
WorkerLoadInfo* aLoadInfo);
|
2015-02-21 15:09:17 +00:00
|
|
|
|
2018-02-08 08:33:32 +00:00
|
|
|
void
|
|
|
|
Traverse(nsCycleCollectionTraversalCallback& aCb);
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearSelfAndParentEventTargetRef()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
MOZ_ASSERT(mSelfRef);
|
|
|
|
mParentEventTargetRef = nullptr;
|
|
|
|
mSelfRef = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
// May be called on any thread...
|
|
|
|
bool
|
|
|
|
Start();
|
|
|
|
|
|
|
|
// Called on the parent thread.
|
|
|
|
bool
|
|
|
|
Notify(WorkerStatus aStatus)
|
|
|
|
{
|
|
|
|
return NotifyPrivate(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Cancel()
|
|
|
|
{
|
|
|
|
return Notify(Canceling);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Kill()
|
|
|
|
{
|
|
|
|
return Notify(Killing);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Terminate()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
return TerminatePrivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Close();
|
|
|
|
|
2017-12-07 11:29:05 +00:00
|
|
|
// The passed principal must be the Worker principal in case of a
|
|
|
|
// ServiceWorker and the loading principal for any other type.
|
2015-02-21 15:09:17 +00:00
|
|
|
static void
|
2017-12-07 11:29:05 +00:00
|
|
|
OverrideLoadInfoLoadGroup(WorkerLoadInfo& aLoadInfo,
|
|
|
|
nsIPrincipal* aPrincipal);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-01-07 12:35:31 +00:00
|
|
|
bool
|
|
|
|
IsDebuggerRegistered()
|
|
|
|
{
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2016-01-07 12:35:31 +00:00
|
|
|
|
|
|
|
// No need to lock here since this is only ever modified by the same thread.
|
|
|
|
return mDebuggerRegistered;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetIsDebuggerRegistered(bool aDebuggerRegistered)
|
|
|
|
{
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2016-01-07 12:35:31 +00:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mDebuggerRegistered != aDebuggerRegistered);
|
|
|
|
mDebuggerRegistered = aDebuggerRegistered;
|
|
|
|
|
|
|
|
mCondVar.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WaitForIsDebuggerRegistered(bool aDebuggerRegistered)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
while (mDebuggerRegistered != aDebuggerRegistered) {
|
|
|
|
mCondVar.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:11:33 +00:00
|
|
|
WorkerDebugger*
|
|
|
|
Debugger() const
|
|
|
|
{
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2016-01-07 12:35:31 +00:00
|
|
|
|
2014-10-29 20:11:33 +00:00
|
|
|
MOZ_ASSERT(mDebugger);
|
|
|
|
return mDebugger;
|
|
|
|
}
|
|
|
|
|
2016-01-07 12:35:31 +00:00
|
|
|
void
|
|
|
|
SetDebugger(WorkerDebugger* aDebugger)
|
|
|
|
{
|
2018-02-05 18:55:07 +00:00
|
|
|
AssertIsOnMainThread();
|
2016-01-07 12:35:31 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(mDebugger != aDebugger);
|
|
|
|
mDebugger = aDebugger;
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:33:39 +00:00
|
|
|
JS::UniqueChars
|
|
|
|
AdoptDefaultLocale()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDefaultLocale,
|
|
|
|
"the default locale must have been successfully set for anyone "
|
|
|
|
"to be trying to adopt it");
|
|
|
|
return Move(mDefaultLocale);
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
|
|
|
DoRunLoop(JSContext* aCx);
|
|
|
|
|
|
|
|
bool
|
2014-03-10 21:28:43 +00:00
|
|
|
InterruptCallback(JSContext* aCx);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2017-05-22 18:26:39 +00:00
|
|
|
bool
|
|
|
|
IsOnCurrentThread();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
CloseInternal(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return NotifyInternal(aCx, Closing);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-02-29 19:52:43 +00:00
|
|
|
FreezeInternal();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
2016-02-29 19:52:43 +00:00
|
|
|
ThawInternal();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
void
|
2016-08-16 06:10:30 +00:00
|
|
|
TraverseTimeouts(nsCycleCollectionTraversalCallback& aCallback);
|
|
|
|
|
|
|
|
void
|
|
|
|
UnlinkTimeouts();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
2016-02-26 20:23:12 +00:00
|
|
|
ModifyBusyCountFromWorker(bool aIncrease);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
2018-02-08 08:33:33 +00:00
|
|
|
AddChildWorker(WorkerPrivate* aChildWorker);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
void
|
2018-02-08 08:33:33 +00:00
|
|
|
RemoveChildWorker(WorkerPrivate* aChildWorker);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
void
|
2013-06-05 14:04:23 +00:00
|
|
|
PostMessageToParent(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
2017-02-03 10:00:38 +00:00
|
|
|
const Sequence<JSObject*>& aTransferable,
|
2013-11-05 14:16:26 +00:00
|
|
|
ErrorResult& aRv)
|
2013-06-05 14:04:23 +00:00
|
|
|
{
|
2015-09-16 03:27:56 +00:00
|
|
|
PostMessageToParentInternal(aCx, aMessage, aTransferable, aRv);
|
2013-06-05 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-02-03 10:00:38 +00:00
|
|
|
PostMessageToParentMessagePort(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
const Sequence<JSObject*>& aTransferable,
|
|
|
|
ErrorResult& aRv);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2015-03-27 06:17:16 +00:00
|
|
|
void
|
|
|
|
EnterDebuggerEventLoop();
|
|
|
|
|
|
|
|
void
|
|
|
|
LeaveDebuggerEventLoop();
|
|
|
|
|
2015-03-20 11:15:59 +00:00
|
|
|
void
|
|
|
|
PostMessageToDebugger(const nsAString& aMessage);
|
|
|
|
|
2015-03-30 11:54:38 +00:00
|
|
|
void
|
2016-02-26 20:23:12 +00:00
|
|
|
SetDebuggerImmediate(Function& aHandler, ErrorResult& aRv);
|
2015-03-30 11:54:38 +00:00
|
|
|
|
2015-03-26 19:09:45 +00:00
|
|
|
void
|
|
|
|
ReportErrorToDebugger(const nsAString& aFilename, uint32_t aLineno,
|
|
|
|
const nsAString& aMessage);
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
2018-01-31 07:23:44 +00:00
|
|
|
NotifyInternal(JSContext* aCx, WorkerStatus aStatus);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
void
|
2016-08-14 11:39:31 +00:00
|
|
|
ReportError(JSContext* aCx, JS::ConstUTF8CharsZ aToStringResult,
|
|
|
|
JSErrorReport* aReport);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-04-11 23:41:00 +00:00
|
|
|
static void
|
|
|
|
ReportErrorToConsole(const char* aMessage);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
int32_t
|
2016-08-16 06:10:30 +00:00
|
|
|
SetTimeout(JSContext* aCx, nsIScriptTimeoutHandler* aHandler,
|
|
|
|
int32_t aTimeout, bool aIsInterval,
|
2013-11-05 14:16:26 +00:00
|
|
|
ErrorResult& aRv);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
void
|
|
|
|
ClearTimeout(int32_t aId);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
RunExpiredTimeouts(JSContext* aCx);
|
|
|
|
|
|
|
|
bool
|
|
|
|
RescheduleTimeoutTimer(JSContext* aCx);
|
|
|
|
|
|
|
|
void
|
2016-07-07 06:15:15 +00:00
|
|
|
UpdateContextOptionsInternal(JSContext* aCx, const JS::ContextOptions& aContextOptions);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2014-09-05 14:26:34 +00:00
|
|
|
void
|
2016-02-26 20:23:12 +00:00
|
|
|
UpdateLanguagesInternal(const nsTArray<nsString>& aLanguages);
|
2014-09-05 14:26:34 +00:00
|
|
|
|
2012-01-04 19:11:32 +00:00
|
|
|
void
|
2013-01-10 22:50:40 +00:00
|
|
|
UpdateJSWorkerMemoryParameterInternal(JSContext* aCx, JSGCParamKey key, uint32_t aValue);
|
2012-01-04 19:11:32 +00:00
|
|
|
|
2014-03-04 23:09:23 +00:00
|
|
|
enum WorkerRanOrNot {
|
|
|
|
WorkerNeverRan = 0,
|
|
|
|
WorkerRan
|
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2014-03-04 23:09:23 +00:00
|
|
|
ScheduleDeletion(WorkerRanOrNot aRanOrNot);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2011-08-07 01:03:46 +00:00
|
|
|
bool
|
2016-10-28 09:50:16 +00:00
|
|
|
CollectRuntimeStats(JS::RuntimeStats* aRtStats, bool aAnonymize);
|
2011-09-09 00:03:03 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
void
|
2013-05-16 22:49:43 +00:00
|
|
|
UpdateGCZealInternal(JSContext* aCx, uint8_t aGCZeal, uint32_t aFrequency);
|
2011-07-17 19:09:13 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-17 20:05:25 +00:00
|
|
|
void
|
|
|
|
GarbageCollectInternal(JSContext* aCx, bool aShrinking,
|
|
|
|
bool aCollectChildren);
|
|
|
|
|
2013-12-03 04:07:02 +00:00
|
|
|
void
|
2016-02-26 20:23:12 +00:00
|
|
|
CycleCollectInternal(bool aCollectChildren);
|
2013-12-03 04:07:02 +00:00
|
|
|
|
2013-11-19 23:08:50 +00:00
|
|
|
void
|
2016-02-26 20:23:12 +00:00
|
|
|
OfflineStatusChangeEventInternal(bool aIsOffline);
|
2013-11-19 23:08:50 +00:00
|
|
|
|
2016-03-23 21:55:07 +00:00
|
|
|
void
|
|
|
|
MemoryPressureInternal();
|
|
|
|
|
2016-12-19 02:38:53 +00:00
|
|
|
void
|
|
|
|
SetFetchHandlerWasAdded()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsServiceWorker());
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
mFetchHandlerWasAdded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FetchHandlerWasAdded() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsServiceWorker());
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mFetchHandlerWasAdded;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
JSContext*
|
|
|
|
GetJSContext() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mJSContext;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
WorkerGlobalScope*
|
|
|
|
GlobalScope() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mScope;
|
|
|
|
}
|
|
|
|
|
2015-03-03 23:51:53 +00:00
|
|
|
WorkerDebuggerGlobalScope*
|
|
|
|
DebuggerGlobalScope() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mDebuggerScope;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2014-11-17 19:55:37 +00:00
|
|
|
SetThread(WorkerThread* aThread);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnWorkerThread() const
|
2013-10-23 13:16:49 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
;
|
|
|
|
#else
|
2011-07-17 19:09:13 +00:00
|
|
|
{ }
|
|
|
|
#endif
|
|
|
|
|
2012-12-30 18:21:52 +00:00
|
|
|
// This may block!
|
|
|
|
void
|
|
|
|
BeginCTypesCall();
|
|
|
|
|
|
|
|
// This may block!
|
|
|
|
void
|
|
|
|
EndCTypesCall();
|
|
|
|
|
2013-01-08 12:57:44 +00:00
|
|
|
void
|
|
|
|
BeginCTypesCallback()
|
|
|
|
{
|
|
|
|
// If a callback is beginning then we need to do the exact same thing as
|
|
|
|
// when a ctypes call ends.
|
|
|
|
EndCTypesCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EndCTypesCallback()
|
|
|
|
{
|
|
|
|
// If a callback is ending then we need to do the exact same thing as
|
|
|
|
// when a ctypes call begins.
|
|
|
|
BeginCTypesCall();
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
bool
|
2015-09-16 03:27:56 +00:00
|
|
|
ConnectMessagePort(JSContext* aCx, MessagePortIdentifier& aIdentifier);
|
2013-06-05 14:04:23 +00:00
|
|
|
|
2015-03-03 23:51:53 +00:00
|
|
|
WorkerGlobalScope*
|
|
|
|
GetOrCreateGlobalScope(JSContext* aCx);
|
|
|
|
|
|
|
|
WorkerDebuggerGlobalScope*
|
|
|
|
CreateDebuggerGlobalScope(JSContext* aCx);
|
2013-11-05 14:16:26 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
RegisterBindings(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
|
|
|
|
|
2016-02-22 09:41:09 +00:00
|
|
|
bool
|
|
|
|
RegisterDebuggerBindings(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
|
|
|
|
|
2015-07-27 21:57:34 +00:00
|
|
|
bool
|
2013-11-19 23:08:50 +00:00
|
|
|
OnLine() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mOnLine;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
void
|
|
|
|
StopSyncLoop(nsIEventTarget* aSyncLoopTarget, bool aResult);
|
|
|
|
|
|
|
|
bool
|
|
|
|
AllPendingRunnablesShouldBeCanceled() const
|
|
|
|
{
|
|
|
|
return mCancelAllPendingRunnables;
|
|
|
|
}
|
|
|
|
|
2015-04-08 22:20:59 +00:00
|
|
|
void
|
|
|
|
ClearMainEventQueue(WorkerRanOrNot aRanOrNot);
|
|
|
|
|
2016-02-26 16:32:28 +00:00
|
|
|
void
|
|
|
|
ClearDebuggerEventQueue();
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
void
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 13:10:46 +00:00
|
|
|
OnProcessNextEvent();
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
void
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 13:10:46 +00:00
|
|
|
AfterProcessNextEvent();
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
AssertValidSyncLoop(nsIEventTarget* aSyncLoopTarget)
|
|
|
|
#ifdef DEBUG
|
|
|
|
;
|
|
|
|
#else
|
|
|
|
{ }
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 16:33:04 +00:00
|
|
|
void
|
|
|
|
SetWorkerScriptExecutedSuccessfully()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
// Should only be called once!
|
|
|
|
MOZ_ASSERT(!mWorkerScriptExecutedSuccessfully);
|
|
|
|
mWorkerScriptExecutedSuccessfully = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only valid after CompileScriptRunnable has finished running!
|
|
|
|
bool
|
|
|
|
WorkerScriptExecutedSuccessfully() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mWorkerScriptExecutedSuccessfully;
|
|
|
|
}
|
|
|
|
|
2015-07-15 19:21:40 +00:00
|
|
|
void
|
|
|
|
MaybeDispatchLoadFailedRunnable();
|
|
|
|
|
2016-09-14 03:14:02 +00:00
|
|
|
// Get the event target to use when dispatching to the main thread
|
|
|
|
// from this Worker thread. This may be the main thread itself or
|
2016-11-07 20:30:17 +00:00
|
|
|
// a ThrottledEventQueue to the main thread.
|
2016-09-14 03:14:02 +00:00
|
|
|
nsIEventTarget*
|
|
|
|
MainThreadEventTarget();
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchToMainThread(nsIRunnable* aRunnable,
|
|
|
|
uint32_t aFlags = NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchToMainThread(already_AddRefed<nsIRunnable> aRunnable,
|
|
|
|
uint32_t aFlags = NS_DISPATCH_NORMAL);
|
|
|
|
|
2017-01-26 16:01:33 +00:00
|
|
|
// Get an event target that will dispatch runnables as control runnables on
|
|
|
|
// the worker thread. Implement nsICancelableRunnable if you wish to take
|
|
|
|
// action on cancelation.
|
2017-07-21 15:16:23 +00:00
|
|
|
nsISerialEventTarget*
|
2017-01-26 16:01:33 +00:00
|
|
|
ControlEventTarget();
|
|
|
|
|
2017-07-21 15:16:24 +00:00
|
|
|
// Get an event target that will attempt to dispatch a normal WorkerRunnable,
|
|
|
|
// but if that fails will then fall back to a control runnable.
|
|
|
|
nsISerialEventTarget*
|
|
|
|
HybridEventTarget();
|
|
|
|
|
2017-11-15 06:58:38 +00:00
|
|
|
void
|
|
|
|
DumpCrashInformation(nsACString& aString);
|
|
|
|
|
2017-11-27 19:48:17 +00:00
|
|
|
bool
|
2017-11-21 20:13:04 +00:00
|
|
|
EnsureClientSource();
|
|
|
|
|
|
|
|
const ClientInfo&
|
|
|
|
GetClientInfo() const;
|
|
|
|
|
2018-01-12 01:46:08 +00:00
|
|
|
const ClientState
|
|
|
|
GetClientState() const;
|
|
|
|
|
|
|
|
const Maybe<ServiceWorkerDescriptor>
|
|
|
|
GetController() const;
|
|
|
|
|
2017-12-06 01:45:22 +00:00
|
|
|
void
|
|
|
|
Control(const ServiceWorkerDescriptor& aServiceWorker);
|
|
|
|
|
2017-11-21 20:13:04 +00:00
|
|
|
void
|
|
|
|
ExecutionReady();
|
|
|
|
|
2018-01-24 16:17:31 +00:00
|
|
|
PerformanceStorage*
|
|
|
|
GetPerformanceStorage();
|
|
|
|
|
2018-02-08 08:33:32 +00:00
|
|
|
bool
|
|
|
|
IsAcceptingEvents()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mParentStatus < Terminating;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerStatus
|
|
|
|
ParentStatusProtected()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mParentStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerStatus
|
|
|
|
ParentStatus() const
|
|
|
|
{
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
return mParentStatus;
|
|
|
|
}
|
|
|
|
|
2018-02-08 07:26:05 +00:00
|
|
|
Worker*
|
|
|
|
ParentEventTargetRef() const
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mParentEventTargetRef);
|
|
|
|
return mParentEventTargetRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetParentEventTargetRef(Worker* aParentEventTargetRef)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aParentEventTargetRef);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mParentEventTargetRef);
|
|
|
|
mParentEventTargetRef = aParentEventTargetRef;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:32 +00:00
|
|
|
bool
|
|
|
|
ModifyBusyCount(bool aIncrease);
|
|
|
|
|
|
|
|
// This method is used by RuntimeService to know what is going wrong the
|
|
|
|
// shutting down.
|
|
|
|
uint32_t
|
|
|
|
BusyCount()
|
|
|
|
{
|
|
|
|
return mBusyCount;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
// Check whether this worker is a secure context. For use from the parent
|
|
|
|
// thread only; the canonical "is secure context" boolean is stored on the
|
|
|
|
// compartment of the worker global. The only reason we don't
|
|
|
|
// AssertIsOnParentThread() here is so we can assert that this value matches
|
|
|
|
// the one on the compartment, which has to be done from the worker thread.
|
|
|
|
bool IsSecureContext() const
|
|
|
|
{
|
|
|
|
return mIsSecureContext;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:32 +00:00
|
|
|
TimeStamp CreationTimeStamp() const
|
|
|
|
{
|
|
|
|
return mCreationTimeStamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMHighResTimeStamp CreationTime() const
|
|
|
|
{
|
|
|
|
return mCreationTimeHighRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMHighResTimeStamp TimeStampToDOMHighRes(const TimeStamp& aTimeStamp) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aTimeStamp.IsNull());
|
|
|
|
TimeDuration duration = aTimeStamp - mCreationTimeStamp;
|
|
|
|
return duration.ToMilliseconds();
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
LocationInfo&
|
|
|
|
GetLocationInfo()
|
|
|
|
{
|
|
|
|
return mLocationInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyJSSettings(workerinternals::JSSettings& aSettings)
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mMutex);
|
|
|
|
aSettings = mJSSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CopyJSCompartmentOptions(JS::CompartmentOptions& aOptions)
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mMutex);
|
|
|
|
aOptions = IsChromeWorker() ? mJSSettings.chrome.compartmentOptions
|
|
|
|
: mJSSettings.content.compartmentOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The ability to be a chrome worker is orthogonal to the type of
|
|
|
|
// worker [Dedicated|Shared|Service].
|
|
|
|
bool
|
|
|
|
IsChromeWorker() const
|
|
|
|
{
|
|
|
|
return mIsChromeWorker;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
WorkerPrivate*
|
|
|
|
GetParent() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
bool
|
|
|
|
IsFrozen() const
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
return mParentFrozen;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
bool
|
|
|
|
IsParentWindowPaused() const
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
return mParentWindowPausedDepth > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we debug a worker, we want to disconnect the window and the worker
|
|
|
|
// communication. This happens calling this method.
|
|
|
|
// Note: this method doesn't suspend the worker! Use Freeze/Thaw instead.
|
|
|
|
void
|
|
|
|
ParentWindowPaused();
|
|
|
|
|
|
|
|
void
|
|
|
|
ParentWindowResumed();
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
const nsString&
|
|
|
|
ScriptURL() const
|
|
|
|
{
|
|
|
|
return mScriptURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsString&
|
|
|
|
WorkerName() const
|
|
|
|
{
|
|
|
|
return mWorkerName;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
WorkerType
|
|
|
|
Type() const
|
|
|
|
{
|
|
|
|
return mWorkerType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsDedicatedWorker() const
|
|
|
|
{
|
|
|
|
return mWorkerType == WorkerTypeDedicated;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsSharedWorker() const
|
|
|
|
{
|
|
|
|
return mWorkerType == WorkerTypeShared;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsServiceWorker() const
|
|
|
|
{
|
|
|
|
return mWorkerType == WorkerTypeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentPolicyType
|
|
|
|
ContentPolicyType() const
|
|
|
|
{
|
|
|
|
return ContentPolicyType(mWorkerType);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsContentPolicyType
|
|
|
|
ContentPolicyType(WorkerType aWorkerType)
|
|
|
|
{
|
|
|
|
switch (aWorkerType) {
|
|
|
|
case WorkerTypeDedicated:
|
|
|
|
return nsIContentPolicy::TYPE_INTERNAL_WORKER;
|
|
|
|
case WorkerTypeShared:
|
|
|
|
return nsIContentPolicy::TYPE_INTERNAL_SHARED_WORKER;
|
|
|
|
case WorkerTypeService:
|
|
|
|
return nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Invalid worker type");
|
|
|
|
return nsIContentPolicy::TYPE_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
nsIScriptContext*
|
|
|
|
GetScriptContext() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mScriptContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString&
|
|
|
|
Domain() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mDomain;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsFromWindow() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mFromWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLoadFlags
|
|
|
|
GetLoadFlags() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mLoadFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
WindowID() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mWindowID;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
ServiceWorkerID() const
|
|
|
|
{
|
|
|
|
return GetServiceWorkerDescriptor().Id();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString&
|
|
|
|
ServiceWorkerScope() const
|
|
|
|
{
|
|
|
|
return GetServiceWorkerDescriptor().Scope();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIURI*
|
|
|
|
GetBaseURI() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mBaseURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetBaseURI(nsIURI* aBaseURI);
|
|
|
|
|
|
|
|
nsIURI*
|
|
|
|
GetResolvedScriptURI() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mResolvedScriptURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsString&
|
|
|
|
ServiceWorkerCacheName() const
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsServiceWorker());
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mServiceWorkerCacheName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ServiceWorkerDescriptor&
|
|
|
|
GetServiceWorkerDescriptor() const
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsServiceWorker());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mLoadInfo.mServiceWorkerDescriptor.isSome());
|
|
|
|
return mLoadInfo.mServiceWorkerDescriptor.ref();
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:23:15 +00:00
|
|
|
const ServiceWorkerRegistrationDescriptor&
|
|
|
|
GetServiceWorkerRegistrationDescriptor() const
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsServiceWorker());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mLoadInfo.mServiceWorkerRegistrationDescriptor.isSome());
|
|
|
|
return mLoadInfo.mServiceWorkerRegistrationDescriptor.ref();
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
void
|
|
|
|
UpdateServiceWorkerState(ServiceWorkerState aState)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsServiceWorker());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mLoadInfo.mServiceWorkerDescriptor.isSome());
|
|
|
|
return mLoadInfo.mServiceWorkerDescriptor.ref().SetState(aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Maybe<ServiceWorkerDescriptor>&
|
|
|
|
GetParentController() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mParentController;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChannelInfo&
|
|
|
|
GetChannelInfo() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mChannelInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetChannelInfo(const ChannelInfo& aChannelInfo)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(!mLoadInfo.mChannelInfo.IsInitialized());
|
|
|
|
MOZ_ASSERT(aChannelInfo.IsInitialized());
|
|
|
|
mLoadInfo.mChannelInfo = aChannelInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InitChannelInfo(nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
mLoadInfo.mChannelInfo.InitFromChannel(aChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InitChannelInfo(const ChannelInfo& aChannelInfo)
|
|
|
|
{
|
|
|
|
mLoadInfo.mChannelInfo = aChannelInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
GetPrincipal() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal*
|
|
|
|
GetLoadingPrincipal() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mLoadingPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAString& Origin() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mOrigin;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsILoadGroup*
|
|
|
|
GetLoadGroup() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mLoadGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method allows the principal to be retrieved off the main thread.
|
|
|
|
// Principals are main-thread objects so the caller must ensure that all
|
|
|
|
// access occurs on the main thread.
|
|
|
|
nsIPrincipal*
|
|
|
|
GetPrincipalDontAssertMainThread() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UsesSystemPrincipal() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mPrincipalIsSystem;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
const mozilla::ipc::PrincipalInfo&
|
2018-02-08 08:33:33 +00:00
|
|
|
GetPrincipalInfo() const
|
|
|
|
{
|
|
|
|
return *mLoadInfo.mPrincipalInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIChannel>
|
|
|
|
ForgetWorkerChannel()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mChannel.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindowInner*
|
|
|
|
GetWindow()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContentSecurityPolicy*
|
|
|
|
GetCSP() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mLoadInfo.mCSP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetCSP(nsIContentSecurityPolicy* aCSP);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SetCSPFromHeaderValues(const nsACString& aCSPHeaderValue,
|
|
|
|
const nsACString& aCSPReportOnlyHeaderValue);
|
|
|
|
|
|
|
|
void
|
|
|
|
SetReferrerPolicyFromHeaderValue(const nsACString& aReferrerPolicyHeaderValue);
|
|
|
|
|
|
|
|
net::ReferrerPolicy
|
|
|
|
GetReferrerPolicy() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mReferrerPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetReferrerPolicy(net::ReferrerPolicy aReferrerPolicy)
|
|
|
|
{
|
|
|
|
mLoadInfo.mReferrerPolicy = aReferrerPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsEvalAllowed() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mEvalAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetEvalAllowed(bool aEvalAllowed)
|
|
|
|
{
|
|
|
|
mLoadInfo.mEvalAllowed = aEvalAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetReportCSPViolations() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mReportCSPViolations;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetReportCSPViolations(bool aReport)
|
|
|
|
{
|
|
|
|
mLoadInfo.mReportCSPViolations = aReport;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XHRParamsAllowed() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mXHRParamsAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetXHRParamsAllowed(bool aAllowed)
|
|
|
|
{
|
|
|
|
mLoadInfo.mXHRParamsAllowed = aAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsStorageAllowed() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mStorageAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OriginAttributes&
|
|
|
|
GetOriginAttributes() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mOriginAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the SW testing per-window flag is set by devtools
|
|
|
|
bool
|
|
|
|
ServiceWorkersTestingInWindow() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mServiceWorkersTestingInWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIRunnable>
|
|
|
|
StealLoadFailedAsyncRunnable()
|
|
|
|
{
|
|
|
|
return mLoadInfo.mLoadFailedAsyncRunnable.forget();
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
// This is used to handle importScripts(). When the worker is first loaded
|
|
|
|
// and executed, it happens in a sync loop. At this point it sets
|
|
|
|
// mLoadingWorkerScript to true. importScripts() calls that occur during the
|
|
|
|
// execution run in nested sync loops and so this continues to return true,
|
|
|
|
// leading to these scripts being cached offline.
|
|
|
|
// mLoadingWorkerScript is set to false when the top level loop ends.
|
|
|
|
// importScripts() in function calls or event handlers are always fetched
|
|
|
|
// from the network.
|
|
|
|
bool
|
|
|
|
LoadScriptAsPartOfLoadingServiceWorkerScript()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsServiceWorker());
|
|
|
|
return mLoadingWorkerScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetLoadingWorkerScript(bool aLoadingWorkerScript)
|
|
|
|
{
|
|
|
|
// any thread
|
|
|
|
MOZ_ASSERT(IsServiceWorker());
|
|
|
|
mLoadingWorkerScript = aLoadingWorkerScript;
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
void
|
|
|
|
QueueRunnable(nsIRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
mQueuedRunnables.AppendElement(aRunnable);
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
bool
|
|
|
|
RegisterSharedWorker(SharedWorker* aSharedWorker, MessagePort* aPort);
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastErrorToSharedWorkers(JSContext* aCx,
|
|
|
|
const WorkerErrorReport* aReport,
|
|
|
|
bool aIsErrorEvent);
|
|
|
|
|
|
|
|
void
|
|
|
|
GetAllSharedWorkers(nsTArray<RefPtr<SharedWorker>>& aSharedWorkers);
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseSharedWorkersForWindow(nsPIDOMWindowInner* aWindow);
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseAllSharedWorkers();
|
|
|
|
|
|
|
|
void
|
|
|
|
FlushReportsToSharedWorkers(nsIConsoleReportCollector* aReporter);
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
// We can assume that an nsPIDOMWindow will be available for Freeze, Thaw
|
|
|
|
// as these are only used for globals going in and out of the bfcache.
|
|
|
|
//
|
|
|
|
// XXXbz: This is a bald-faced lie given the uses in RegisterSharedWorker and
|
|
|
|
// CloseSharedWorkersForWindow, which pass null for aWindow to Thaw and Freeze
|
|
|
|
// respectively. See bug 1251722.
|
|
|
|
bool
|
|
|
|
Freeze(nsPIDOMWindowInner* aWindow);
|
|
|
|
|
|
|
|
bool
|
|
|
|
Thaw(nsPIDOMWindowInner* aWindow);
|
|
|
|
|
2018-02-08 08:33:33 +00:00
|
|
|
void
|
|
|
|
EnableDebugger();
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableDebugger();
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
already_AddRefed<WorkerRunnable>
|
|
|
|
MaybeWrapAsWorkerRunnable(already_AddRefed<nsIRunnable> aRunnable);
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProxyReleaseMainThreadObjects();
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
void
|
|
|
|
GarbageCollect(bool aShrinking);
|
|
|
|
|
|
|
|
void
|
|
|
|
CycleCollect(bool aDummy);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SetPrincipalOnMainThread(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SetPrincipalFromChannel(nsIChannel* aChannel);
|
|
|
|
|
|
|
|
bool
|
|
|
|
FinalChannelPrincipalIsValid(nsIChannel* aChannel);
|
|
|
|
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
bool
|
|
|
|
PrincipalURIMatchesScriptURL();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateOverridenLoadGroup(nsILoadGroup* aBaseLoadGroup);
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerScriptLoaded();
|
|
|
|
|
|
|
|
nsIDocument* GetDocument() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
MemoryPressure(bool aDummy);
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateContextOptions(const JS::ContextOptions& aContextOptions);
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateLanguages(const nsTArray<nsString>& aLanguages);
|
|
|
|
|
|
|
|
void
|
|
|
|
UpdateJSWorkerMemoryParameter(JSGCParamKey key, uint32_t value);
|
|
|
|
|
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
void
|
|
|
|
UpdateGCZeal(uint8_t aGCZeal, uint32_t aFrequency);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
OfflineStatusChangeEvent(bool aIsOffline);
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
nsresult
|
|
|
|
Dispatch(already_AddRefed<WorkerRunnable> aRunnable)
|
|
|
|
{
|
|
|
|
return DispatchPrivate(Move(aRunnable), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchControlRunnable(already_AddRefed<WorkerControlRunnable> aWorkerControlRunnable);
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchDebuggerRunnable(already_AddRefed<WorkerRunnable> aDebuggerRunnable);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertIsOnParentThread() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertInnerWindowIsCorrect() const;
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
AssertIsOnParentThread() const
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertInnerWindowIsCorrect() const
|
|
|
|
{ }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
bool
|
|
|
|
PrincipalIsValid() const;
|
|
|
|
#endif
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
private:
|
2016-03-11 21:43:30 +00:00
|
|
|
WorkerPrivate(WorkerPrivate* aParent,
|
2013-11-05 14:16:24 +00:00
|
|
|
const nsAString& aScriptURL, bool aIsChromeWorker,
|
2017-05-17 14:49:34 +00:00
|
|
|
WorkerType aWorkerType, const nsAString& aWorkerName,
|
|
|
|
const nsACString& aServiceWorkerScope,
|
2015-02-12 09:50:05 +00:00
|
|
|
WorkerLoadInfo& aLoadInfo);
|
2012-09-15 18:51:55 +00:00
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
~WorkerPrivate();
|
|
|
|
|
2018-02-08 08:33:32 +00:00
|
|
|
nsresult
|
|
|
|
DispatchPrivate(already_AddRefed<WorkerRunnable> aRunnable,
|
|
|
|
nsIEventTarget* aSyncLoopTarget);
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
bool
|
|
|
|
NotifyPrivate(WorkerStatus aStatus);
|
|
|
|
|
|
|
|
bool
|
|
|
|
TerminatePrivate()
|
|
|
|
{
|
|
|
|
return NotifyPrivate(Terminating);
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
|
|
|
MayContinueRunning()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
2018-01-31 07:23:44 +00:00
|
|
|
WorkerStatus status;
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 19:09:13 +00:00
|
|
|
status = mStatus;
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:33:05 +00:00
|
|
|
if (status < Terminating) {
|
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 04:33:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
void
|
2016-02-27 02:15:56 +00:00
|
|
|
CancelAllTimeouts();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2016-05-11 18:45:58 +00:00
|
|
|
enum class ProcessAllControlRunnablesResult
|
|
|
|
{
|
|
|
|
// We did not process anything.
|
|
|
|
Nothing,
|
|
|
|
// We did process something, states may have changed, but we can keep
|
|
|
|
// executing script.
|
|
|
|
MayContinue,
|
|
|
|
// We did process something, and should not continue executing script.
|
|
|
|
Abort
|
|
|
|
};
|
|
|
|
|
|
|
|
ProcessAllControlRunnablesResult
|
2013-10-23 13:16:49 +00:00
|
|
|
ProcessAllControlRunnables()
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return ProcessAllControlRunnablesLocked();
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
2011-09-09 00:03:03 +00:00
|
|
|
|
2016-05-11 18:45:58 +00:00
|
|
|
ProcessAllControlRunnablesResult
|
2013-10-23 13:16:49 +00:00
|
|
|
ProcessAllControlRunnablesLocked();
|
2012-09-17 00:20:16 +00:00
|
|
|
|
2012-12-30 18:21:52 +00:00
|
|
|
void
|
|
|
|
EnableMemoryReporter();
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableMemoryReporter();
|
|
|
|
|
|
|
|
void
|
|
|
|
WaitForWorkerEvents(PRIntervalTime interval = PR_INTERVAL_NO_TIMEOUT);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
void
|
2013-06-05 14:04:23 +00:00
|
|
|
PostMessageToParentInternal(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
2017-02-03 10:00:38 +00:00
|
|
|
const Sequence<JSObject*>& aTransferable,
|
2013-11-05 14:16:26 +00:00
|
|
|
ErrorResult& aRv);
|
2013-11-24 19:27:15 +00:00
|
|
|
|
2017-01-05 09:05:32 +00:00
|
|
|
// If the worker shutdown status is equal or greater then aFailStatus, this
|
|
|
|
// operation will fail and nullptr will be returned. See WorkerHolder.h for
|
|
|
|
// more information about the correct value to use.
|
2013-10-23 13:16:49 +00:00
|
|
|
already_AddRefed<nsIEventTarget>
|
2018-01-31 07:23:44 +00:00
|
|
|
CreateNewSyncLoop(WorkerStatus aFailStatus);
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
RunCurrentSyncLoop();
|
|
|
|
|
2014-01-11 00:37:47 +00:00
|
|
|
bool
|
|
|
|
DestroySyncLoop(uint32_t aLoopIndex, nsIThreadInternal* aThread = nullptr);
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
void
|
|
|
|
InitializeGCTimers();
|
|
|
|
|
2018-02-08 08:33:34 +00:00
|
|
|
enum GCTimerMode
|
|
|
|
{
|
|
|
|
PeriodicTimer = 0,
|
|
|
|
IdleTimer,
|
|
|
|
NoTimer
|
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
void
|
|
|
|
SetGCTimerMode(GCTimerMode aMode);
|
|
|
|
|
|
|
|
void
|
|
|
|
ShutdownGCTimers();
|
2016-06-23 08:53:14 +00:00
|
|
|
|
|
|
|
bool
|
2018-01-31 07:23:44 +00:00
|
|
|
AddHolder(WorkerHolder* aHolder, WorkerStatus aFailStatus);
|
2016-06-23 08:53:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
RemoveHolder(WorkerHolder* aHolder);
|
|
|
|
|
|
|
|
void
|
2018-01-31 07:23:44 +00:00
|
|
|
NotifyHolders(JSContext* aCx, WorkerStatus aStatus);
|
2016-06-23 08:53:14 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
HasActiveHolders()
|
|
|
|
{
|
|
|
|
return !(mChildWorkers.IsEmpty() && mTimeouts.IsEmpty() &&
|
|
|
|
mHolders.IsEmpty());
|
|
|
|
}
|
2018-02-08 08:33:34 +00:00
|
|
|
|
|
|
|
class EventTarget;
|
|
|
|
friend class EventTarget;
|
|
|
|
friend class mozilla::dom::WorkerHolder;
|
|
|
|
friend class AutoSyncLoopHolder;
|
|
|
|
|
|
|
|
struct TimeoutInfo;
|
|
|
|
|
|
|
|
class MemoryReporter;
|
|
|
|
friend class MemoryReporter;
|
|
|
|
|
|
|
|
friend class mozilla::dom::WorkerThread;
|
|
|
|
|
|
|
|
SharedMutex mMutex;
|
|
|
|
mozilla::CondVar mCondVar;
|
|
|
|
|
|
|
|
WorkerPrivate* mParent;
|
|
|
|
|
|
|
|
nsString mScriptURL;
|
|
|
|
|
|
|
|
// This is the worker name for shared workers and dedicated workers.
|
|
|
|
nsString mWorkerName;
|
|
|
|
|
|
|
|
WorkerType mWorkerType;
|
|
|
|
|
|
|
|
// The worker is owned by its thread, which is represented here. This is set
|
|
|
|
// in Constructor() and emptied by WorkerFinishedRunnable, and conditionally
|
|
|
|
// traversed by the cycle collector if the busy count is zero.
|
|
|
|
//
|
|
|
|
// There are 4 ways a worker can be terminated:
|
|
|
|
// 1. GC/CC - When the worker is in idle state (busycount == 0), it allows to
|
|
|
|
// traverse the 'hidden' mParentEventTargetRef pointer. This is the exposed
|
|
|
|
// Worker webidl object. Doing this, CC will be able to detect a cycle and
|
|
|
|
// Unlink is called. In Unlink, Worker calls Terminate().
|
|
|
|
// 2. Worker::Terminate() is called - the shutdown procedure starts
|
|
|
|
// immediately.
|
|
|
|
// 3. WorkerScope::Close() is called - Similar to point 2.
|
|
|
|
// 4. xpcom-shutdown notification - We call Kill().
|
|
|
|
RefPtr<Worker> mParentEventTargetRef;
|
|
|
|
RefPtr<WorkerPrivate> mSelfRef;
|
|
|
|
|
|
|
|
// The lifetime of these objects within LoadInfo is managed explicitly;
|
|
|
|
// they do not need to be cycle collected.
|
|
|
|
WorkerLoadInfo mLoadInfo;
|
|
|
|
LocationInfo mLocationInfo;
|
|
|
|
|
|
|
|
// Protected by mMutex.
|
|
|
|
workerinternals::JSSettings mJSSettings;
|
|
|
|
|
|
|
|
WorkerDebugger* mDebugger;
|
|
|
|
|
|
|
|
workerinternals::Queue<WorkerControlRunnable*, 4> mControlQueue;
|
|
|
|
workerinternals::Queue<WorkerRunnable*, 4> mDebuggerQueue;
|
|
|
|
|
|
|
|
// Touched on multiple threads, protected with mMutex.
|
|
|
|
JSContext* mJSContext;
|
|
|
|
RefPtr<WorkerThread> mThread;
|
|
|
|
PRThread* mPRThread;
|
|
|
|
|
|
|
|
// Things touched on worker thread only.
|
|
|
|
RefPtr<WorkerGlobalScope> mScope;
|
|
|
|
RefPtr<WorkerDebuggerGlobalScope> mDebuggerScope;
|
|
|
|
nsTArray<WorkerPrivate*> mChildWorkers;
|
|
|
|
nsTObserverArray<WorkerHolder*> mHolders;
|
|
|
|
nsTArray<nsAutoPtr<TimeoutInfo>> mTimeouts;
|
|
|
|
RefPtr<ThrottledEventQueue> mMainThreadThrottledEventQueue;
|
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
|
|
|
RefPtr<WorkerEventTarget> mWorkerControlEventTarget;
|
|
|
|
RefPtr<WorkerEventTarget> mWorkerHybridEventTarget;
|
|
|
|
|
|
|
|
struct SyncLoopInfo
|
|
|
|
{
|
|
|
|
explicit SyncLoopInfo(EventTarget* aEventTarget);
|
|
|
|
|
|
|
|
RefPtr<EventTarget> mEventTarget;
|
|
|
|
bool mCompleted;
|
|
|
|
bool mResult;
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool mHasRun;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is only modified on the worker thread, but in DEBUG builds
|
|
|
|
// AssertValidSyncLoop function iterates it on other threads. Therefore
|
|
|
|
// modifications are done with mMutex held *only* in DEBUG builds.
|
|
|
|
nsTArray<nsAutoPtr<SyncLoopInfo>> mSyncLoopStack;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsITimerCallback> mTimerRunnable;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mGCTimer;
|
|
|
|
|
|
|
|
RefPtr<MemoryReporter> mMemoryReporter;
|
|
|
|
|
|
|
|
// fired on the main thread if the worker script fails to load
|
|
|
|
nsCOMPtr<nsIRunnable> mLoadFailedRunnable;
|
|
|
|
|
|
|
|
RefPtr<PerformanceStorage> mPerformanceStorage;
|
|
|
|
|
|
|
|
// Only used for top level workers.
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable>> mQueuedRunnables;
|
|
|
|
|
|
|
|
// Protected by mMutex.
|
|
|
|
nsTArray<RefPtr<WorkerRunnable>> mPreStartRunnables;
|
|
|
|
|
|
|
|
// Only touched on the parent thread (currently this is always the main
|
|
|
|
// thread as SharedWorkers are always top-level).
|
|
|
|
nsTArray<RefPtr<SharedWorker>> mSharedWorkers;
|
|
|
|
|
|
|
|
JS::UniqueChars mDefaultLocale; // nulled during worker JSContext init
|
|
|
|
TimeStamp mKillTime;
|
|
|
|
WorkerStatus mParentStatus;
|
|
|
|
WorkerStatus mStatus;
|
|
|
|
UniquePtr<ClientSource> mClientSource;
|
|
|
|
|
|
|
|
// This is touched on parent thread only, but it can be read on a different
|
|
|
|
// thread before crashing because hanging.
|
|
|
|
Atomic<uint64_t> mBusyCount;
|
|
|
|
|
|
|
|
Atomic<bool> mLoadingWorkerScript;
|
|
|
|
|
|
|
|
TimeStamp mCreationTimeStamp;
|
|
|
|
DOMHighResTimeStamp mCreationTimeHighRes;
|
|
|
|
|
|
|
|
// Things touched on worker thread only.
|
|
|
|
uint32_t mNumHoldersPreventingShutdownStart;
|
|
|
|
uint32_t mDebuggerEventLoopLevel;
|
|
|
|
|
|
|
|
uint32_t mErrorHandlerRecursionCount;
|
|
|
|
uint32_t mNextTimeoutId;
|
|
|
|
|
|
|
|
// SharedWorkers may have multiple windows paused, so this must be
|
|
|
|
// a count instead of just a boolean.
|
|
|
|
uint32_t mParentWindowPausedDepth;
|
|
|
|
|
|
|
|
bool mFrozen;
|
|
|
|
bool mTimerRunning;
|
|
|
|
bool mRunningExpiredTimeouts;
|
|
|
|
bool mPendingEventQueueClearing;
|
|
|
|
bool mCancelAllPendingRunnables;
|
|
|
|
bool mPeriodicGCTimerRunning;
|
|
|
|
bool mIdleGCTimerRunning;
|
|
|
|
bool mWorkerScriptExecutedSuccessfully;
|
|
|
|
bool mFetchHandlerWasAdded;
|
|
|
|
bool mOnLine;
|
|
|
|
bool mMainThreadObjectsForgotten;
|
|
|
|
bool mIsChromeWorker;
|
|
|
|
bool mParentFrozen;
|
|
|
|
|
|
|
|
// mIsSecureContext is set once in our constructor; after that it can be read
|
|
|
|
// from various threads. We could make this const if we were OK with setting
|
|
|
|
// it in the initializer list via calling some function that takes all sorts
|
|
|
|
// of state (loadinfo, worker type, parent).
|
|
|
|
//
|
|
|
|
// It's a bit unfortunate that we have to have an out-of-band boolean for
|
|
|
|
// this, but we need access to this state from the parent thread, and we can't
|
|
|
|
// use our global object's secure state there.
|
|
|
|
bool mIsSecureContext;
|
|
|
|
|
|
|
|
bool mDebuggerRegistered;
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2012-12-21 20:14:47 +00:00
|
|
|
class AutoSyncLoopHolder
|
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
2014-01-11 00:37:47 +00:00
|
|
|
uint32_t mIndex;
|
2013-10-23 13:16:49 +00:00
|
|
|
|
2012-12-21 20:14:47 +00:00
|
|
|
public:
|
2017-01-05 09:05:32 +00:00
|
|
|
// See CreateNewSyncLoop() for more information about the correct value to use
|
|
|
|
// for aFailStatus.
|
2018-01-31 07:23:44 +00:00
|
|
|
AutoSyncLoopHolder(WorkerPrivate* aWorkerPrivate, WorkerStatus aFailStatus)
|
2014-01-11 00:37:47 +00:00
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
2017-01-05 09:05:32 +00:00
|
|
|
, mTarget(aWorkerPrivate->CreateNewSyncLoop(aFailStatus))
|
2014-01-11 00:37:47 +00:00
|
|
|
, mIndex(aWorkerPrivate->mSyncLoopStack.Length() - 1)
|
2012-12-21 20:14:47 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
2012-12-21 20:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoSyncLoopHolder()
|
|
|
|
{
|
2017-01-05 09:05:32 +00:00
|
|
|
if (mWorkerPrivate && mTarget) {
|
2013-10-23 13:16:49 +00:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mWorkerPrivate->StopSyncLoop(mTarget, false);
|
2014-01-11 00:37:47 +00:00
|
|
|
mWorkerPrivate->DestroySyncLoop(mIndex);
|
2012-12-21 20:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-10-23 13:16:49 +00:00
|
|
|
Run()
|
2012-12-21 20:14:47 +00:00
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = mWorkerPrivate;
|
|
|
|
mWorkerPrivate = nullptr;
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
return workerPrivate->RunCurrentSyncLoop();
|
2012-12-21 20:14:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
nsIEventTarget*
|
2017-01-05 09:05:32 +00:00
|
|
|
GetEventTarget() const
|
2012-12-21 20:14:47 +00:00
|
|
|
{
|
2017-01-05 09:05:32 +00:00
|
|
|
// This can be null if CreateNewSyncLoop() fails.
|
2013-10-23 13:16:49 +00:00
|
|
|
return mTarget;
|
2012-12-21 20:14:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-31 07:24:08 +00:00
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_workerprivate_h__ */
|