2011-07-17 19:09:13 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
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__
|
|
|
|
|
|
|
|
#include "Workers.h"
|
|
|
|
|
2012-09-15 18:51:55 +00:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2012-09-05 17:47:57 +00:00
|
|
|
#include "nsPIDOMWindow.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"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-06-05 14:04:23 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2013-11-05 14:16:26 +00:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "nsTArray.h"
|
2013-06-05 14:04:23 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-04-24 10:58:07 +00:00
|
|
|
#include "StructuredCloneTags.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#include "Queue.h"
|
|
|
|
#include "WorkerFeature.h"
|
|
|
|
|
|
|
|
class JSAutoStructuredCloneBuffer;
|
2013-03-07 22:27:21 +00:00
|
|
|
class nsIChannel;
|
2011-07-17 19:09:13 +00:00
|
|
|
class nsIDocument;
|
2013-10-23 13:16:49 +00:00
|
|
|
class nsIEventTarget;
|
2011-07-17 19:09:13 +00:00
|
|
|
class nsIPrincipal;
|
|
|
|
class nsIScriptContext;
|
2013-10-23 13:16:49 +00:00
|
|
|
class nsIThread;
|
2014-01-11 00:37:47 +00:00
|
|
|
class nsIThreadInternal;
|
2011-07-17 19:09:13 +00:00
|
|
|
class nsITimer;
|
2013-06-05 14:04:23 +00:00
|
|
|
class nsIURI;
|
2013-05-16 22:49:43 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
class RuntimeStats;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class Function;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
struct PRThread;
|
|
|
|
#endif
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class AutoSyncLoopHolder;
|
2013-06-05 14:04:23 +00:00
|
|
|
class MessagePort;
|
|
|
|
class SharedWorker;
|
2013-10-23 13:16:49 +00:00
|
|
|
class WorkerControlRunnable;
|
2013-11-05 14:16:26 +00:00
|
|
|
class WorkerGlobalScope;
|
2011-07-17 19:09:13 +00:00
|
|
|
class WorkerPrivate;
|
2013-10-23 13:16:49 +00:00
|
|
|
class WorkerRunnable;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2014-05-12 16:11:15 +00:00
|
|
|
enum WorkerType
|
|
|
|
{
|
|
|
|
WorkerTypeDedicated,
|
|
|
|
WorkerTypeShared,
|
|
|
|
WorkerTypeService
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class RefCountedMutex MOZ_FINAL : public Mutex
|
2013-02-08 11:50:00 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefCountedMutex(const char* aName)
|
|
|
|
: Mutex(aName)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefCountedMutex)
|
|
|
|
|
|
|
|
private:
|
|
|
|
~RefCountedMutex()
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
nsRefPtr<RefCountedMutex> mMutex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SharedMutex(const char* aName)
|
|
|
|
: 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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
template <class Derived>
|
2014-04-01 06:13:50 +00:00
|
|
|
class WorkerPrivateParent : public DOMEventTargetHelper
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
class SynchronizeAndResumeRunnable;
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
protected:
|
|
|
|
class EventTarget;
|
|
|
|
friend class EventTarget;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
struct LoadInfo
|
|
|
|
{
|
|
|
|
// All of these should be released in ForgetMainThreadObjects.
|
|
|
|
nsCOMPtr<nsIURI> mBaseURI;
|
|
|
|
nsCOMPtr<nsIURI> mResolvedScriptURI;
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsCOMPtr<nsIScriptContext> mScriptContext;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> mCSP;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
|
|
|
|
nsCString mDomain;
|
|
|
|
|
|
|
|
bool mEvalAllowed;
|
|
|
|
bool mReportCSPViolations;
|
|
|
|
bool mXHRParamsAllowed;
|
|
|
|
bool mPrincipalIsSystem;
|
2014-02-05 18:38:16 +00:00
|
|
|
bool mIsInPrivilegedApp;
|
|
|
|
bool mIsInCertifiedApp;
|
2013-06-05 14:04:23 +00:00
|
|
|
|
|
|
|
LoadInfo()
|
|
|
|
: mEvalAllowed(false), mReportCSPViolations(false),
|
2014-02-05 18:38:16 +00:00
|
|
|
mXHRParamsAllowed(false), mPrincipalIsSystem(false),
|
|
|
|
mIsInPrivilegedApp(false), mIsInCertifiedApp(false)
|
2013-06-05 14:04:23 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
StealFrom(LoadInfo& aOther)
|
|
|
|
{
|
2014-01-23 20:14:35 +00:00
|
|
|
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);
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
mDomain = aOther.mDomain;
|
|
|
|
mEvalAllowed = aOther.mEvalAllowed;
|
|
|
|
mReportCSPViolations = aOther.mReportCSPViolations;
|
|
|
|
mXHRParamsAllowed = aOther.mXHRParamsAllowed;
|
|
|
|
mPrincipalIsSystem = aOther.mPrincipalIsSystem;
|
2014-02-05 18:38:16 +00:00
|
|
|
mIsInPrivilegedApp = aOther.mIsInPrivilegedApp;
|
|
|
|
mIsInCertifiedApp = aOther.mIsInCertifiedApp;
|
2013-06-05 14:04:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
protected:
|
2013-06-05 14:04:23 +00:00
|
|
|
typedef mozilla::ErrorResult ErrorResult;
|
|
|
|
|
2013-02-08 11:50:00 +00:00
|
|
|
SharedMutex mMutex;
|
2011-07-17 19:09:13 +00:00
|
|
|
mozilla::CondVar mCondVar;
|
2012-12-30 18:21:52 +00:00
|
|
|
mozilla::CondVar mMemoryReportCondVar;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
// Protected by mMutex.
|
|
|
|
nsRefPtr<EventTarget> mEventTarget;
|
|
|
|
nsTArray<nsRefPtr<WorkerRunnable>> mPreStartRunnables;
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
private:
|
|
|
|
WorkerPrivate* mParent;
|
|
|
|
nsString mScriptURL;
|
2014-02-06 14:39:10 +00:00
|
|
|
nsCString mSharedWorkerName;
|
2011-07-17 19:09:13 +00:00
|
|
|
LocationInfo mLocationInfo;
|
2013-11-05 14:16:24 +00:00
|
|
|
// The lifetime of these objects within LoadInfo is managed explicitly;
|
|
|
|
// they do not need to be cycle collected.
|
2013-06-05 14:04:23 +00:00
|
|
|
LoadInfo mLoadInfo;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// Only used for top level workers.
|
2013-10-23 13:16:49 +00:00
|
|
|
nsTArray<nsCOMPtr<nsIRunnable>> mQueuedRunnables;
|
2013-06-05 14:04:23 +00:00
|
|
|
nsRevocableEventPtr<SynchronizeAndResumeRunnable> mSynchronizeRunnable;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-06-22 21:48:00 +00:00
|
|
|
// Only for ChromeWorkers without window and only touched on the main thread.
|
|
|
|
nsTArray<nsCString> mHostObjectURIs;
|
|
|
|
|
2013-05-16 22:49:43 +00:00
|
|
|
// Protected by mMutex.
|
|
|
|
JSSettings mJSSettings;
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
// Only touched on the parent thread (currently this is always the main
|
|
|
|
// thread as SharedWorkers are always top-level).
|
|
|
|
nsDataHashtable<nsUint64HashKey, SharedWorker*> mSharedWorkers;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t mBusyCount;
|
2013-06-05 14:04:23 +00:00
|
|
|
uint64_t mMessagePortSerial;
|
2011-07-17 19:09:13 +00:00
|
|
|
Status mParentStatus;
|
|
|
|
bool mParentSuspended;
|
|
|
|
bool mIsChromeWorker;
|
2012-03-31 04:42:20 +00:00
|
|
|
bool mMainThreadObjectsForgotten;
|
2013-10-30 23:40:16 +00:00
|
|
|
WorkerType mWorkerType;
|
2014-05-06 09:50:12 +00:00
|
|
|
TimeStamp mCreationTimeStamp;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
protected:
|
2014-02-02 18:08:50 +00:00
|
|
|
// The worker is owned by its thread, which is represented here. This is set
|
|
|
|
// in Construct() and emptied by WorkerFinishedRunnable, and conditionally
|
|
|
|
// traversed by the cycle collector if the busy count is zero.
|
|
|
|
nsRefPtr<WorkerPrivate> mSelfRef;
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
WorkerPrivateParent(JSContext* aCx, WorkerPrivate* aParent,
|
|
|
|
const nsAString& aScriptURL, bool aIsChromeWorker,
|
|
|
|
WorkerType aWorkerType,
|
2014-02-06 14:39:10 +00:00
|
|
|
const nsACString& aSharedWorkerName,
|
|
|
|
LoadInfo& aLoadInfo);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
~WorkerPrivateParent();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Derived*
|
|
|
|
ParentAsWorkerPrivate() const
|
|
|
|
{
|
|
|
|
return static_cast<Derived*>(const_cast<WorkerPrivateParent*>(this));
|
|
|
|
}
|
|
|
|
|
2012-03-19 14:34:58 +00:00
|
|
|
// aCx is null when called from the finalizer
|
2011-12-29 18:28:13 +00:00
|
|
|
bool
|
2012-03-19 14:34:58 +00:00
|
|
|
NotifyPrivate(JSContext* aCx, Status aStatus);
|
2011-12-29 18:28:13 +00:00
|
|
|
|
2012-03-19 14:34:58 +00:00
|
|
|
// aCx is null when called from the finalizer
|
2011-12-29 18:28:13 +00:00
|
|
|
bool
|
2012-03-19 14:34:58 +00:00
|
|
|
TerminatePrivate(JSContext* aCx)
|
2011-12-29 18:28:13 +00:00
|
|
|
{
|
2012-03-19 14:34:58 +00:00
|
|
|
return NotifyPrivate(aCx, Terminating);
|
2011-12-29 18:28:13 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
void
|
2013-06-05 14:04:23 +00:00
|
|
|
PostMessageInternal(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2013-11-05 14:16:24 +00:00
|
|
|
const Optional<Sequence<JS::Value> >& aTransferable,
|
|
|
|
bool aToMessagePort, uint64_t aMessagePortSerial,
|
|
|
|
ErrorResult& aRv);
|
2013-06-05 14:04:23 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
nsresult
|
|
|
|
DispatchPrivate(WorkerRunnable* aRunnable, nsIEventTarget* aSyncLoopTarget);
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
public:
|
2013-11-05 14:16:24 +00:00
|
|
|
virtual JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2013-11-05 14:16:24 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WorkerPrivateParent,
|
2014-04-01 06:13:50 +00:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 14:16:24 +00:00
|
|
|
|
2014-02-02 18:08:50 +00:00
|
|
|
void
|
|
|
|
ClearSelfRef()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
MOZ_ASSERT(mSelfRef);
|
|
|
|
mSelfRef = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
nsresult
|
|
|
|
Dispatch(WorkerRunnable* aRunnable)
|
|
|
|
{
|
|
|
|
return DispatchPrivate(aRunnable, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatchControlRunnable(WorkerControlRunnable* aWorkerControlRunnable);
|
|
|
|
|
|
|
|
already_AddRefed<WorkerRunnable>
|
|
|
|
MaybeWrapAsWorkerRunnable(nsIRunnable* aRunnable);
|
|
|
|
|
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
GetEventTarget();
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
// May be called on any thread...
|
|
|
|
bool
|
|
|
|
Start();
|
|
|
|
|
|
|
|
// Called on the parent thread.
|
|
|
|
bool
|
2011-12-29 18:28:13 +00:00
|
|
|
Notify(JSContext* aCx, Status aStatus)
|
|
|
|
{
|
2012-03-19 14:34:58 +00:00
|
|
|
return NotifyPrivate(aCx, aStatus);
|
2011-12-29 18:28:13 +00:00
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Cancel(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return Notify(aCx, Canceling);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Kill(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return Notify(aCx, Killing);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-06-05 14:04:23 +00:00
|
|
|
Suspend(JSContext* aCx, nsPIDOMWindow* aWindow);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
bool
|
|
|
|
Resume(JSContext* aCx, nsPIDOMWindow* aWindow);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-06-06 17:28:47 +00:00
|
|
|
bool
|
2013-06-05 14:04:23 +00:00
|
|
|
SynchronizeAndResume(JSContext* aCx, nsPIDOMWindow* aWindow,
|
|
|
|
nsIScriptContext* aScriptContext);
|
2013-06-06 17:28:47 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
|
|
|
Terminate(JSContext* aCx)
|
|
|
|
{
|
2012-03-31 04:42:20 +00:00
|
|
|
AssertIsOnParentThread();
|
2012-03-19 14:34:58 +00:00
|
|
|
return TerminatePrivate(aCx);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Close(JSContext* aCx);
|
|
|
|
|
|
|
|
bool
|
|
|
|
ModifyBusyCount(JSContext* aCx, bool aIncrease);
|
|
|
|
|
|
|
|
void
|
|
|
|
ForgetMainThreadObjects(nsTArray<nsCOMPtr<nsISupports> >& aDoomed);
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
void
|
2013-05-04 07:53:00 +00:00
|
|
|
PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2013-11-05 14:16:24 +00:00
|
|
|
const Optional<Sequence<JS::Value> >& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2013-06-05 14:04:23 +00:00
|
|
|
{
|
2013-11-05 14:16:24 +00:00
|
|
|
PostMessageInternal(aCx, aMessage, aTransferable, false, 0, aRv);
|
2013-06-05 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostMessageToMessagePort(JSContext* aCx,
|
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
2013-11-05 14:16:24 +00:00
|
|
|
const Optional<Sequence<JS::Value> >& aTransferable,
|
2013-06-05 14:04:23 +00:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool
|
|
|
|
DispatchMessageEventToMessagePort(
|
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t aMessagePortSerial,
|
2014-02-01 02:50:07 +00:00
|
|
|
JSAutoStructuredCloneBuffer&& aBuffer,
|
2013-06-05 14:04:23 +00:00
|
|
|
nsTArray<nsCOMPtr<nsISupports>>& aClonedObjects);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t
|
2011-08-24 20:44:35 +00:00
|
|
|
GetInnerWindowId();
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
void
|
2014-02-26 09:25:36 +00:00
|
|
|
UpdateRuntimeAndContextOptions(JSContext* aCx,
|
|
|
|
const JS::RuntimeOptions& aRuntimeOptions,
|
|
|
|
const JS::ContextOptions& aContentCxOptions,
|
|
|
|
const JS::ContextOptions& aChromeCxOptions);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-24 19:27:15 +00:00
|
|
|
void
|
|
|
|
UpdatePreference(JSContext* aCx, WorkerPreference aPref, bool aValue);
|
|
|
|
|
2012-01-04 19:11:32 +00:00
|
|
|
void
|
2013-05-16 22:49:43 +00:00
|
|
|
UpdateJSWorkerMemoryParameter(JSContext* aCx, JSGCParamKey key,
|
|
|
|
uint32_t value);
|
2012-01-04 19:11:32 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
void
|
2013-05-16 22:49:43 +00:00
|
|
|
UpdateGCZeal(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
|
|
|
|
GarbageCollect(JSContext* aCx, bool aShrinking);
|
|
|
|
|
2013-12-03 04:07:02 +00:00
|
|
|
void
|
|
|
|
CycleCollect(JSContext* aCx, bool aDummy);
|
|
|
|
|
2013-11-19 23:08:50 +00:00
|
|
|
void
|
|
|
|
OfflineStatusChangeEvent(JSContext* aCx, bool aIsOffline);
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
bool
|
|
|
|
RegisterSharedWorker(JSContext* aCx, SharedWorker* aSharedWorker);
|
|
|
|
|
|
|
|
void
|
|
|
|
UnregisterSharedWorker(JSContext* aCx, SharedWorker* aSharedWorker);
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastErrorToSharedWorkers(JSContext* aCx,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aFilename,
|
|
|
|
const nsAString& aLine,
|
|
|
|
uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags);
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerScriptLoaded();
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2013-10-23 13:16:49 +00:00
|
|
|
QueueRunnable(nsIRunnable* aRunnable)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mQueuedRunnables.AppendElement(aRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate*
|
|
|
|
GetParent() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsSuspended() const
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
|
|
|
return mParentSuspended;
|
|
|
|
}
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
bool
|
|
|
|
IsAcceptingEvents()
|
|
|
|
{
|
|
|
|
AssertIsOnParentThread();
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mParentStatus < Terminating;
|
2013-02-08 11:50:00 +00:00
|
|
|
}
|
2012-03-31 04:42:20 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
Status
|
|
|
|
ParentStatus() const
|
|
|
|
{
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
return mParentStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext*
|
|
|
|
ParentJSContext() const;
|
|
|
|
|
|
|
|
nsIScriptContext*
|
|
|
|
GetScriptContext() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mScriptContext;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsString&
|
|
|
|
ScriptURL() const
|
|
|
|
{
|
|
|
|
return mScriptURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString&
|
|
|
|
Domain() const
|
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mDomain;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIURI*
|
|
|
|
GetBaseURI() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mBaseURI;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 16:32:17 +00:00
|
|
|
void
|
2011-07-17 19:09:13 +00:00
|
|
|
SetBaseURI(nsIURI* aBaseURI);
|
|
|
|
|
|
|
|
nsIURI*
|
2013-06-05 14:04:23 +00:00
|
|
|
GetResolvedScriptURI() const
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mResolvedScriptURI;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 09:50:12 +00:00
|
|
|
TimeStamp CreationTimeStamp() const
|
|
|
|
{
|
|
|
|
return mCreationTimeStamp;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
nsIPrincipal*
|
|
|
|
GetPrincipal() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mPrincipal;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-03 19:31:30 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2011-08-16 03:40:38 +00:00
|
|
|
SetPrincipal(nsIPrincipal* aPrincipal);
|
|
|
|
|
|
|
|
bool
|
|
|
|
UsesSystemPrincipal() const
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mPrincipalIsSystem;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:38:16 +00:00
|
|
|
bool
|
|
|
|
IsInPrivilegedApp() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mIsInPrivilegedApp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsInCertifiedApp() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mIsInCertifiedApp;
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
already_AddRefed<nsIChannel>
|
|
|
|
ForgetWorkerChannel()
|
2013-03-07 22:27:21 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mChannel.forget();
|
2013-03-07 22:27:21 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
nsIDocument*
|
|
|
|
GetDocument() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mWindow ? mLoadInfo.mWindow->GetExtantDoc() : nullptr;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow*
|
|
|
|
GetWindow()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mWindow;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-15 18:51:55 +00:00
|
|
|
nsIContentSecurityPolicy*
|
|
|
|
GetCSP() const
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mCSP;
|
2012-09-15 18:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetCSP(nsIContentSecurityPolicy* aCSP)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2013-06-05 14:04:23 +00:00
|
|
|
mLoadInfo.mCSP = aCSP;
|
2012-09-15 18:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsEvalAllowed() const
|
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mEvalAllowed;
|
2012-09-15 18:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetEvalAllowed(bool aEvalAllowed)
|
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
mLoadInfo.mEvalAllowed = aEvalAllowed;
|
2012-09-15 18:51:55 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 20:54:58 +00:00
|
|
|
bool
|
|
|
|
GetReportCSPViolations() const
|
|
|
|
{
|
2013-06-05 14:04:23 +00:00
|
|
|
return mLoadInfo.mReportCSPViolations;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XHRParamsAllowed() const
|
|
|
|
{
|
|
|
|
return mLoadInfo.mXHRParamsAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetXHRParamsAllowed(bool aAllowed)
|
|
|
|
{
|
|
|
|
mLoadInfo.mXHRParamsAllowed = aAllowed;
|
2012-10-15 20:54:58 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
LocationInfo&
|
|
|
|
GetLocationInfo()
|
|
|
|
{
|
|
|
|
return mLocationInfo;
|
|
|
|
}
|
|
|
|
|
2013-05-16 22:49:43 +00:00
|
|
|
void
|
|
|
|
CopyJSSettings(JSSettings& aSettings)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-05-16 22:49:43 +00:00
|
|
|
mozilla::MutexAutoLock lock(mMutex);
|
|
|
|
aSettings = mJSSettings;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 11:43:16 +00:00
|
|
|
void
|
|
|
|
CopyJSCompartmentOptions(JS::CompartmentOptions& aOptions)
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mMutex);
|
|
|
|
aOptions = IsChromeWorker() ? mJSSettings.chrome.compartmentOptions
|
|
|
|
: mJSSettings.content.compartmentOptions;
|
|
|
|
}
|
|
|
|
|
2013-10-30 23:40:16 +00:00
|
|
|
// The ability to be a chrome worker is orthogonal to the type of
|
2014-05-12 16:11:15 +00:00
|
|
|
// worker [Dedicated|Shared|Service].
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
|
|
|
IsChromeWorker() const
|
|
|
|
{
|
|
|
|
return mIsChromeWorker;
|
|
|
|
}
|
|
|
|
|
2013-10-30 23:40:16 +00:00
|
|
|
bool
|
|
|
|
IsDedicatedWorker() const
|
|
|
|
{
|
|
|
|
return mWorkerType == WorkerTypeDedicated;
|
|
|
|
}
|
|
|
|
|
2013-06-05 14:04:23 +00:00
|
|
|
bool
|
|
|
|
IsSharedWorker() const
|
|
|
|
{
|
2013-10-30 23:40:16 +00:00
|
|
|
return mWorkerType == WorkerTypeShared;
|
2013-06-05 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 16:11:15 +00:00
|
|
|
bool
|
|
|
|
IsServiceWorker() const
|
|
|
|
{
|
|
|
|
return mWorkerType == WorkerTypeService;
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:39:10 +00:00
|
|
|
const nsCString&
|
2013-06-05 14:04:23 +00:00
|
|
|
SharedWorkerName() const
|
|
|
|
{
|
|
|
|
return mSharedWorkerName;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
NextMessagePortSerial()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
return mMessagePortSerial++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetAllSharedWorkers(nsTArray<nsRefPtr<SharedWorker>>& aSharedWorkers);
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseSharedWorkersForWindow(nsPIDOMWindow* aWindow);
|
|
|
|
|
|
|
|
void
|
|
|
|
RegisterHostObjectURI(const nsACString& aURI);
|
|
|
|
|
|
|
|
void
|
|
|
|
UnregisterHostObjectURI(const nsACString& aURI);
|
|
|
|
|
|
|
|
void
|
|
|
|
StealHostObjectURIs(nsTArray<nsCString>& aArray);
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
IMPL_EVENT_HANDLER(message)
|
|
|
|
IMPL_EVENT_HANDLER(error)
|
2013-06-05 14:04:23 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertIsOnParentThread() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertInnerWindowIsCorrect() const;
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
AssertIsOnParentThread() const
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertInnerWindowIsCorrect() const
|
|
|
|
{ }
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
class WorkerPrivate : public WorkerPrivateParent<WorkerPrivate>
|
|
|
|
{
|
|
|
|
friend class WorkerPrivateParent<WorkerPrivate>;
|
|
|
|
typedef WorkerPrivateParent<WorkerPrivate> ParentType;
|
2013-10-23 13:16:49 +00:00
|
|
|
friend class AutoSyncLoopHolder;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
struct TimeoutInfo;
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
class MemoryReporter;
|
|
|
|
friend class MemoryReporter;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
enum GCTimerMode
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
PeriodicTimer = 0,
|
|
|
|
IdleTimer,
|
|
|
|
NoTimer
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
Queue<WorkerControlRunnable*, 4> mControlQueue;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// Touched on multiple threads, protected with mMutex.
|
|
|
|
JSContext* mJSContext;
|
2011-12-05 07:58:27 +00:00
|
|
|
nsRefPtr<WorkerCrossThreadDispatcher> mCrossThreadDispatcher;
|
2013-10-23 13:16:49 +00:00
|
|
|
nsTArray<nsCOMPtr<nsIRunnable>> mUndispatchedRunnablesForSyncLoop;
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
// Things touched on worker thread only.
|
2013-11-05 14:16:26 +00:00
|
|
|
nsRefPtr<WorkerGlobalScope> mScope;
|
2011-07-17 19:09:13 +00:00
|
|
|
nsTArray<ParentType*> mChildWorkers;
|
|
|
|
nsTArray<WorkerFeature*> mFeatures;
|
2013-10-23 13:16:49 +00:00
|
|
|
nsTArray<nsAutoPtr<TimeoutInfo>> mTimeouts;
|
|
|
|
|
|
|
|
struct SyncLoopInfo
|
|
|
|
{
|
|
|
|
SyncLoopInfo(EventTarget* aEventTarget);
|
|
|
|
|
|
|
|
nsRefPtr<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;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mGCTimer;
|
|
|
|
nsCOMPtr<nsIEventTarget> mPeriodicGCTimerTarget;
|
|
|
|
nsCOMPtr<nsIEventTarget> mIdleGCTimerTarget;
|
|
|
|
|
2013-02-08 11:50:00 +00:00
|
|
|
nsRefPtr<MemoryReporter> mMemoryReporter;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
nsRefPtrHashtable<nsUint64HashKey, MessagePort> mWorkerPorts;
|
2013-06-05 14:04:23 +00:00
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
TimeStamp mKillTime;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mErrorHandlerRecursionCount;
|
|
|
|
uint32_t mNextTimeoutId;
|
2011-07-17 19:09:13 +00:00
|
|
|
Status mStatus;
|
|
|
|
bool mSuspended;
|
|
|
|
bool mTimerRunning;
|
|
|
|
bool mRunningExpiredTimeouts;
|
|
|
|
bool mCloseHandlerStarted;
|
|
|
|
bool mCloseHandlerFinished;
|
2011-09-09 00:03:03 +00:00
|
|
|
bool mMemoryReporterRunning;
|
2012-12-30 18:21:52 +00:00
|
|
|
bool mBlockedForMemoryReporter;
|
2013-10-23 13:16:49 +00:00
|
|
|
bool mCancelAllPendingRunnables;
|
|
|
|
bool mPeriodicGCTimerRunning;
|
2013-12-20 19:03:19 +00:00
|
|
|
bool mIdleGCTimerRunning;
|
2014-05-29 16:33:04 +00:00
|
|
|
bool mWorkerScriptExecutedSuccessfully;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-10-23 13:16:49 +00:00
|
|
|
PRThread* mPRThread;
|
2011-07-17 19:09:13 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-24 19:27:15 +00:00
|
|
|
bool mPreferences[WORKERPREF_COUNT];
|
2013-11-19 23:08:50 +00:00
|
|
|
bool mOnLine;
|
2013-11-24 19:27:15 +00:00
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
protected:
|
2013-10-14 18:38:54 +00:00
|
|
|
~WorkerPrivate();
|
2013-10-14 11:58:05 +00:00
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
public:
|
|
|
|
static already_AddRefed<WorkerPrivate>
|
|
|
|
Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2012-03-31 04:42:20 +00:00
|
|
|
static already_AddRefed<WorkerPrivate>
|
2013-11-05 14:16:24 +00:00
|
|
|
Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL,
|
|
|
|
bool aIsChromeWorker, WorkerType aWorkerType,
|
2014-02-06 14:39:10 +00:00
|
|
|
const nsACString& aSharedWorkerName,
|
2013-11-05 14:16:24 +00:00
|
|
|
LoadInfo* aLoadInfo, ErrorResult& aRv);
|
|
|
|
|
2014-05-29 16:19:00 +00:00
|
|
|
static already_AddRefed<WorkerPrivate>
|
|
|
|
Constructor(JSContext* aCx, const nsAString& aScriptURL, bool aIsChromeWorker,
|
|
|
|
WorkerType aWorkerType, const nsACString& aSharedWorkerName,
|
|
|
|
LoadInfo* aLoadInfo, ErrorResult& aRv);
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
static bool
|
|
|
|
WorkerAvailable(JSContext* /* unused */, JSObject* /* unused */);
|
2013-06-05 14:04:23 +00:00
|
|
|
|
|
|
|
static nsresult
|
|
|
|
GetLoadInfo(JSContext* aCx, nsPIDOMWindow* aWindow, WorkerPrivate* aParent,
|
|
|
|
const nsAString& aScriptURL, bool aIsChromeWorker,
|
|
|
|
LoadInfo* aLoadInfo);
|
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
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
nsresult
|
|
|
|
IsOnCurrentThread(bool* aIsOnCurrentThread);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
CloseInternal(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return NotifyInternal(aCx, Closing);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SuspendInternal(JSContext* aCx);
|
|
|
|
|
|
|
|
bool
|
|
|
|
ResumeInternal(JSContext* aCx);
|
|
|
|
|
|
|
|
void
|
2013-11-05 14:16:26 +00:00
|
|
|
TraceTimeouts(const TraceCallbacks& aCallbacks, void* aClosure) const;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
ModifyBusyCountFromWorker(JSContext* aCx, bool aIncrease);
|
|
|
|
|
|
|
|
bool
|
|
|
|
AddChildWorker(JSContext* aCx, ParentType* aChildWorker);
|
|
|
|
|
|
|
|
void
|
|
|
|
RemoveChildWorker(JSContext* aCx, ParentType* aChildWorker);
|
|
|
|
|
|
|
|
bool
|
|
|
|
AddFeature(JSContext* aCx, WorkerFeature* aFeature);
|
|
|
|
|
|
|
|
void
|
|
|
|
RemoveFeature(JSContext* aCx, WorkerFeature* aFeature);
|
|
|
|
|
|
|
|
void
|
|
|
|
NotifyFeatures(JSContext* aCx, Status aStatus);
|
|
|
|
|
|
|
|
bool
|
|
|
|
HasActiveFeatures()
|
|
|
|
{
|
|
|
|
return !(mChildWorkers.IsEmpty() && mTimeouts.IsEmpty() &&
|
|
|
|
mFeatures.IsEmpty());
|
|
|
|
}
|
|
|
|
|
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,
|
2013-11-05 14:16:26 +00:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2013-06-05 14:04:23 +00:00
|
|
|
{
|
2013-11-05 14:16:26 +00:00
|
|
|
PostMessageToParentInternal(aCx, aMessage, aTransferable, false, 0, aRv);
|
2013-06-05 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PostMessageToParentMessagePort(
|
|
|
|
JSContext* aCx,
|
|
|
|
uint64_t aMessagePortSerial,
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Handle<JS::Value> aMessage,
|
2013-06-05 14:04:23 +00:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
NotifyInternal(JSContext* aCx, Status aStatus);
|
|
|
|
|
|
|
|
void
|
|
|
|
ReportError(JSContext* aCx, const char* aMessage, JSErrorReport* aReport);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
int32_t
|
|
|
|
SetTimeout(JSContext* aCx,
|
|
|
|
Function* aHandler,
|
|
|
|
const nsAString& aStringHandler,
|
|
|
|
int32_t aTimeout,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
bool aIsInterval,
|
|
|
|
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
|
|
|
|
CloseHandlerStarted()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
mCloseHandlerStarted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CloseHandlerFinished()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
mCloseHandlerFinished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-26 09:25:36 +00:00
|
|
|
UpdateRuntimeAndContextOptionsInternal(
|
|
|
|
JSContext* aCx,
|
|
|
|
const JS::RuntimeOptions& aRuntimeOptions,
|
|
|
|
const JS::ContextOptions& aContentCxOptions,
|
|
|
|
const JS::ContextOptions& aChromeCxOptions);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2013-11-24 19:27:15 +00:00
|
|
|
void
|
|
|
|
UpdatePreferenceInternal(JSContext* aCx, WorkerPreference aPref, bool aValue);
|
|
|
|
|
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
|
2013-04-12 03:52:32 +00:00
|
|
|
BlockAndCollectRuntimeStats(JS::RuntimeStats* aRtStats);
|
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
|
|
|
|
CycleCollectInternal(JSContext* aCx, bool aCollectChildren);
|
|
|
|
|
2013-11-19 23:08:50 +00:00
|
|
|
void
|
|
|
|
OfflineStatusChangeEventInternal(JSContext* aCx, bool aIsOffline);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2013-10-23 13:16:49 +00:00
|
|
|
SetThread(nsIThread* 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
|
|
|
|
|
2011-12-05 07:58:27 +00:00
|
|
|
WorkerCrossThreadDispatcher*
|
|
|
|
GetCrossThreadDispatcher();
|
|
|
|
|
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
|
|
|
|
ConnectMessagePort(JSContext* aCx, uint64_t aMessagePortSerial);
|
|
|
|
|
|
|
|
void
|
|
|
|
DisconnectMessagePort(uint64_t aMessagePortSerial);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
MessagePort*
|
2013-06-05 14:04:23 +00:00
|
|
|
GetMessagePort(uint64_t aMessagePortSerial);
|
|
|
|
|
2013-11-05 14:16:26 +00:00
|
|
|
JSObject*
|
|
|
|
CreateGlobalScope(JSContext* aCx);
|
|
|
|
|
|
|
|
bool
|
|
|
|
RegisterBindings(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
|
|
|
|
|
2013-11-24 19:27:15 +00:00
|
|
|
bool
|
|
|
|
DumpEnabled() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mPreferences[WORKERPREF_DUMP];
|
|
|
|
}
|
|
|
|
|
2013-11-19 23:08:50 +00:00
|
|
|
bool
|
|
|
|
OnLine() const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
return mOnLine;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
void
|
|
|
|
StopSyncLoop(nsIEventTarget* aSyncLoopTarget, bool aResult);
|
|
|
|
|
|
|
|
bool
|
|
|
|
AllPendingRunnablesShouldBeCanceled() const
|
|
|
|
{
|
|
|
|
return mCancelAllPendingRunnables;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OnProcessNextEvent(uint32_t aRecursionDepth);
|
|
|
|
|
|
|
|
void
|
|
|
|
AfterProcessNextEvent(uint32_t aRecursionDepth);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
private:
|
2013-11-05 14:16:24 +00:00
|
|
|
WorkerPrivate(JSContext* aCx, WorkerPrivate* aParent,
|
|
|
|
const nsAString& aScriptURL, bool aIsChromeWorker,
|
2014-02-06 14:39:10 +00:00
|
|
|
WorkerType aWorkerType, const nsACString& aSharedWorkerName,
|
2013-11-05 14:16:24 +00:00
|
|
|
LoadInfo& aLoadInfo);
|
2012-09-15 18:51:55 +00:00
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
void
|
2014-03-04 23:09:23 +00:00
|
|
|
ClearMainEventQueue(WorkerRanOrNot aRanOrNot);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
MayContinueRunning()
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
Status status;
|
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-07-17 19:09:13 +00:00
|
|
|
status = mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status >= Killing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (status >= Running) {
|
|
|
|
return mKillTime.IsNull() || RemainingRunTimeMS() > 0;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2011-07-17 19:09:13 +00:00
|
|
|
RemainingRunTimeMS() const;
|
|
|
|
|
|
|
|
void
|
|
|
|
CancelAllTimeouts(JSContext* aCx);
|
|
|
|
|
|
|
|
bool
|
|
|
|
ScheduleKillCloseEventRunnable(JSContext* aCx);
|
|
|
|
|
2013-10-23 13:16:49 +00:00
|
|
|
bool
|
|
|
|
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
|
|
|
|
|
|
|
bool
|
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,
|
2013-11-05 14:16:26 +00:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
2013-06-05 14:04:23 +00:00
|
|
|
bool aToMessagePort,
|
2013-11-05 14:16:26 +00:00
|
|
|
uint64_t aMessagePortSerial,
|
|
|
|
ErrorResult& aRv);
|
2013-11-24 19:27:15 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
GetAllPreferences(bool aPreferences[WORKERPREF_COUNT]) const
|
|
|
|
{
|
|
|
|
AssertIsOnWorkerThread();
|
|
|
|
memcpy(aPreferences, mPreferences, WORKERPREF_COUNT * sizeof(bool));
|
|
|
|
}
|
2013-10-23 13:16:49 +00:00
|
|
|
|
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
CreateNewSyncLoop();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
void
|
|
|
|
SetGCTimerMode(GCTimerMode aMode);
|
|
|
|
|
|
|
|
void
|
|
|
|
ShutdownGCTimers();
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
// This class is only used to trick the DOM bindings. We never create
|
|
|
|
// instances of it, and static_casting to it is fine since it doesn't add
|
|
|
|
// anything to WorkerPrivate.
|
|
|
|
class ChromeWorkerPrivate : public WorkerPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static already_AddRefed<ChromeWorkerPrivate>
|
|
|
|
Constructor(const GlobalObject& aGlobal, const nsAString& aScriptURL,
|
|
|
|
ErrorResult& rv);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
WorkerAvailable(JSContext* /* unused */, JSObject* /* unused */);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ChromeWorkerPrivate() MOZ_DELETE;
|
|
|
|
ChromeWorkerPrivate(const ChromeWorkerPrivate& aRHS) MOZ_DELETE;
|
|
|
|
ChromeWorkerPrivate& operator =(const ChromeWorkerPrivate& aRHS) MOZ_DELETE;
|
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
WorkerPrivate*
|
|
|
|
GetWorkerPrivateFromContext(JSContext* aCx);
|
|
|
|
|
2013-11-05 14:16:24 +00:00
|
|
|
WorkerPrivate*
|
|
|
|
GetCurrentThreadWorkerPrivate();
|
|
|
|
|
2013-08-30 09:47:19 +00:00
|
|
|
bool
|
|
|
|
IsCurrentThreadRunningChromeWorker();
|
|
|
|
|
2013-09-09 03:29:21 +00:00
|
|
|
JSContext*
|
|
|
|
GetCurrentThreadJSContext();
|
|
|
|
|
2011-08-16 03:40:38 +00:00
|
|
|
enum WorkerStructuredDataType
|
|
|
|
{
|
2012-04-24 10:58:07 +00:00
|
|
|
DOMWORKER_SCTAG_FILE = SCTAG_DOM_MAX,
|
2011-08-16 03:40:38 +00:00
|
|
|
DOMWORKER_SCTAG_BLOB,
|
|
|
|
|
|
|
|
DOMWORKER_SCTAG_END
|
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
JSStructuredCloneCallbacks*
|
2011-08-16 03:40:38 +00:00
|
|
|
WorkerStructuredCloneCallbacks(bool aMainRuntime);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
JSStructuredCloneCallbacks*
|
2011-08-16 03:40:38 +00:00
|
|
|
ChromeWorkerStructuredCloneCallbacks(bool aMainRuntime);
|
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:
|
|
|
|
AutoSyncLoopHolder(WorkerPrivate* aWorkerPrivate)
|
2014-01-11 00:37:47 +00:00
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
, mTarget(aWorkerPrivate->CreateNewSyncLoop())
|
|
|
|
, 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()
|
|
|
|
{
|
|
|
|
if (mWorkerPrivate) {
|
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*
|
|
|
|
EventTarget() const
|
2012-12-21 20:14:47 +00:00
|
|
|
{
|
2013-10-23 13:16:49 +00:00
|
|
|
return mTarget;
|
2012-12-21 20:14:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
#endif /* mozilla_dom_workers_workerprivate_h__ */
|