2015-05-03 12:32:37 -07: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-03-30 21:42:20 -07: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/. */
|
2008-11-26 22:16:40 -08:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
#ifndef mozilla_dom_workers_workers_h__
|
|
|
|
#define mozilla_dom_workers_workers_h__
|
2008-11-26 22:16:40 -08:00
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
#include "jsapi.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-12-04 23:58:27 -08:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-07-30 07:25:31 -07:00
|
|
|
#include <stdint.h>
|
2012-03-30 21:42:20 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
2013-09-23 10:25:00 -07:00
|
|
|
#include "nsString.h"
|
2015-02-12 01:50:05 -08:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIWeakReferenceUtils.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2015-05-25 11:21:05 -07:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2008-11-26 22:16:40 -08:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
#define BEGIN_WORKERS_NAMESPACE \
|
|
|
|
namespace mozilla { namespace dom { namespace workers {
|
|
|
|
#define END_WORKERS_NAMESPACE \
|
|
|
|
} /* namespace workers */ } /* namespace dom */ } /* namespace mozilla */
|
|
|
|
#define USING_WORKERS_NAMESPACE \
|
|
|
|
using namespace mozilla::dom::workers;
|
2011-07-12 09:32:14 -07:00
|
|
|
|
2011-12-29 10:28:09 -08:00
|
|
|
#define WORKERS_SHUTDOWN_TOPIC "web-workers-shutdown"
|
|
|
|
|
2015-02-12 01:50:05 -08:00
|
|
|
class nsIContentSecurityPolicy;
|
2013-06-06 10:28:47 -07:00
|
|
|
class nsIScriptContext;
|
2015-02-12 01:50:05 -08:00
|
|
|
class nsIGlobalObject;
|
2011-07-17 12:09:13 -07:00
|
|
|
class nsPIDOMWindow;
|
2015-02-12 01:50:05 -08:00
|
|
|
class nsIPrincipal;
|
|
|
|
class nsILoadGroup;
|
|
|
|
class nsITabChild;
|
|
|
|
class nsIChannel;
|
2015-07-15 12:21:40 -07:00
|
|
|
class nsIRunnable;
|
2015-02-12 01:50:05 -08:00
|
|
|
class nsIURI;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
class PrincipalInfo;
|
2015-07-13 08:25:42 -07:00
|
|
|
} // namespace ipc
|
2015-03-04 05:54:16 -08:00
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
// If you change this, the corresponding list in nsIWorkerDebugger.idl needs to
|
|
|
|
// be updated too.
|
|
|
|
enum WorkerType
|
|
|
|
{
|
|
|
|
WorkerTypeDedicated,
|
|
|
|
WorkerTypeShared,
|
|
|
|
WorkerTypeService
|
|
|
|
};
|
2015-07-13 08:25:42 -07:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2011-07-12 09:32:14 -07:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
2008-11-26 22:16:40 -08:00
|
|
|
|
2011-12-04 23:58:27 -08:00
|
|
|
class WorkerPrivate;
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
struct PrivatizableBase
|
|
|
|
{ };
|
2011-07-12 09:32:14 -07:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
#else
|
|
|
|
inline void
|
|
|
|
AssertIsOnMainThread()
|
|
|
|
{ }
|
|
|
|
#endif
|
2011-07-12 09:32:14 -07:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
struct JSSettings
|
|
|
|
{
|
|
|
|
enum {
|
|
|
|
// All the GC parameters that we support.
|
|
|
|
JSSettings_JSGC_MAX_BYTES = 0,
|
|
|
|
JSSettings_JSGC_MAX_MALLOC_BYTES,
|
|
|
|
JSSettings_JSGC_HIGH_FREQUENCY_TIME_LIMIT,
|
|
|
|
JSSettings_JSGC_LOW_FREQUENCY_HEAP_GROWTH,
|
|
|
|
JSSettings_JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MIN,
|
|
|
|
JSSettings_JSGC_HIGH_FREQUENCY_HEAP_GROWTH_MAX,
|
|
|
|
JSSettings_JSGC_HIGH_FREQUENCY_LOW_LIMIT,
|
|
|
|
JSSettings_JSGC_HIGH_FREQUENCY_HIGH_LIMIT,
|
|
|
|
JSSettings_JSGC_ALLOCATION_THRESHOLD,
|
|
|
|
JSSettings_JSGC_SLICE_TIME_BUDGET,
|
|
|
|
JSSettings_JSGC_DYNAMIC_HEAP_GROWTH,
|
|
|
|
JSSettings_JSGC_DYNAMIC_MARK_SLICE,
|
|
|
|
// JSGC_MODE not supported
|
|
|
|
|
|
|
|
// This must be last so that we get an accurate count.
|
|
|
|
kGCSettingsArraySize
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JSGCSetting
|
|
|
|
{
|
|
|
|
JSGCParamKey key;
|
|
|
|
uint32_t value;
|
|
|
|
|
|
|
|
JSGCSetting()
|
|
|
|
: key(static_cast<JSGCParamKey>(-1)), value(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsSet() const
|
|
|
|
{
|
|
|
|
return key != static_cast<JSGCParamKey>(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Unset()
|
|
|
|
{
|
|
|
|
key = static_cast<JSGCParamKey>(-1);
|
|
|
|
value = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// There are several settings that we know we need so it makes sense to
|
|
|
|
// preallocate here.
|
|
|
|
typedef JSGCSetting JSGCSettingsArray[kGCSettingsArraySize];
|
|
|
|
|
|
|
|
// Settings that change based on chrome/content context.
|
|
|
|
struct JSContentChromeSettings
|
|
|
|
{
|
2013-12-10 03:43:16 -08:00
|
|
|
JS::CompartmentOptions compartmentOptions;
|
2013-05-16 15:49:43 -07:00
|
|
|
int32_t maxScriptRuntime;
|
|
|
|
|
|
|
|
JSContentChromeSettings()
|
2014-07-29 21:00:37 -07:00
|
|
|
: compartmentOptions(), maxScriptRuntime(0)
|
2013-05-16 15:49:43 -07:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
JSContentChromeSettings chrome;
|
|
|
|
JSContentChromeSettings content;
|
|
|
|
JSGCSettingsArray gcSettings;
|
2014-02-26 01:25:36 -08:00
|
|
|
JS::RuntimeOptions runtimeOptions;
|
2013-12-25 00:21:59 -08:00
|
|
|
|
2013-05-16 15:49:43 -07:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
uint8_t gcZeal;
|
|
|
|
uint32_t gcZealFrequency;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JSSettings()
|
|
|
|
#ifdef JS_GC_ZEAL
|
2013-12-25 00:21:59 -08:00
|
|
|
: gcZeal(0), gcZealFrequency(0)
|
2013-05-16 15:49:43 -07:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for (uint32_t index = 0; index < ArrayLength(gcSettings); index++) {
|
|
|
|
new (gcSettings + index) JSGCSetting();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ApplyGCSetting(JSGCParamKey aKey, uint32_t aValue)
|
|
|
|
{
|
|
|
|
JSSettings::JSGCSetting* firstEmptySetting = nullptr;
|
|
|
|
JSSettings::JSGCSetting* foundSetting = nullptr;
|
|
|
|
|
|
|
|
for (uint32_t index = 0; index < ArrayLength(gcSettings); index++) {
|
|
|
|
JSSettings::JSGCSetting& setting = gcSettings[index];
|
|
|
|
if (setting.key == aKey) {
|
|
|
|
foundSetting = &setting;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!firstEmptySetting && !setting.IsSet()) {
|
|
|
|
firstEmptySetting = &setting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aValue) {
|
|
|
|
if (!foundSetting) {
|
|
|
|
foundSetting = firstEmptySetting;
|
|
|
|
if (!foundSetting) {
|
|
|
|
NS_ERROR("Not enough space for this value!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foundSetting->key = aKey;
|
|
|
|
foundSetting->value = aValue;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundSetting) {
|
|
|
|
foundSetting->Unset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-24 11:27:15 -08:00
|
|
|
enum WorkerPreference
|
|
|
|
{
|
2015-10-24 09:06:57 -07:00
|
|
|
#define WORKER_SIMPLE_PREF(name, getter, NAME) WORKERPREF_ ## NAME,
|
|
|
|
#define WORKER_PREF(name, callback)
|
|
|
|
#include "mozilla/dom/WorkerPrefs.h"
|
|
|
|
#undef WORKER_SIMPLE_PREF
|
|
|
|
#undef WORKER_PREF
|
2013-11-24 11:27:15 -08:00
|
|
|
WORKERPREF_COUNT
|
|
|
|
};
|
|
|
|
|
2015-02-12 01:50:05 -08:00
|
|
|
// Implemented in WorkerPrivate.cpp
|
|
|
|
|
|
|
|
struct WorkerLoadInfo
|
|
|
|
{
|
|
|
|
// All of these should be released in WorkerPrivateParent::ForgetMainThreadObjects.
|
|
|
|
nsCOMPtr<nsIURI> mBaseURI;
|
|
|
|
nsCOMPtr<nsIURI> mResolvedScriptURI;
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsCOMPtr<nsIScriptContext> mScriptContext;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> mWindow;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> mCSP;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
|
|
|
|
2015-07-15 12:21:40 -07:00
|
|
|
// mLoadFailedAsyncRunnable will execute on main thread if script loading
|
|
|
|
// fails during script loading. If script loading is never started due to
|
|
|
|
// a synchronous error, then the runnable is never executed. The runnable
|
|
|
|
// is guaranteed to be released on the main thread.
|
|
|
|
nsCOMPtr<nsIRunnable> mLoadFailedAsyncRunnable;
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class InterfaceRequestor final : public nsIInterfaceRequestor
|
2015-02-12 01:50:05 -08:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
public:
|
|
|
|
InterfaceRequestor(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup);
|
|
|
|
void MaybeAddTabChild(nsILoadGroup* aLoadGroup);
|
2015-03-21 09:28:04 -07:00
|
|
|
NS_IMETHOD GetInterface(const nsIID& aIID, void** aSink) override;
|
2015-02-12 01:50:05 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
~InterfaceRequestor() { }
|
|
|
|
|
|
|
|
already_AddRefed<nsITabChild> GetAnyLiveTabChild();
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadContext> mLoadContext;
|
2015-03-29 10:43:34 -07:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mOuterRequestor;
|
2015-02-12 01:50:05 -08:00
|
|
|
|
|
|
|
// Array of weak references to nsITabChild. We do not want to keep TabChild
|
|
|
|
// actors alive for long after their ActorDestroy() methods are called.
|
|
|
|
nsTArray<nsWeakPtr> mTabChildList;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only set if we have a custom overriden load group
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<InterfaceRequestor> mInterfaceRequestor;
|
2015-02-12 01:50:05 -08:00
|
|
|
|
|
|
|
nsAutoPtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
|
|
|
|
nsCString mDomain;
|
|
|
|
|
2015-03-19 11:41:42 -07:00
|
|
|
nsString mServiceWorkerCacheName;
|
|
|
|
|
2015-05-25 11:21:05 -07:00
|
|
|
ChannelInfo mChannelInfo;
|
2015-04-23 15:59:52 -07:00
|
|
|
|
2015-02-12 01:50:05 -08:00
|
|
|
uint64_t mWindowID;
|
2015-05-14 12:41:42 -07:00
|
|
|
uint64_t mServiceWorkerID;
|
2015-02-12 01:50:05 -08:00
|
|
|
|
|
|
|
bool mFromWindow;
|
|
|
|
bool mEvalAllowed;
|
|
|
|
bool mReportCSPViolations;
|
|
|
|
bool mXHRParamsAllowed;
|
|
|
|
bool mPrincipalIsSystem;
|
|
|
|
bool mIsInPrivilegedApp;
|
|
|
|
bool mIsInCertifiedApp;
|
2015-07-15 14:01:02 -07:00
|
|
|
bool mStorageAllowed;
|
2015-06-26 11:18:18 -07:00
|
|
|
bool mPrivateBrowsing;
|
2015-06-27 20:19:24 -07:00
|
|
|
bool mServiceWorkersTestingInWindow;
|
2015-02-12 01:50:05 -08:00
|
|
|
|
|
|
|
WorkerLoadInfo();
|
|
|
|
~WorkerLoadInfo();
|
|
|
|
|
|
|
|
void StealFrom(WorkerLoadInfo& aOther);
|
|
|
|
};
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
// All of these are implemented in RuntimeService.cpp
|
2014-07-04 12:41:27 -07:00
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
CancelWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
void
|
2015-04-01 02:00:19 -07:00
|
|
|
FreezeWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
void
|
2015-04-01 02:00:19 -07:00
|
|
|
ThawWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2015-10-07 03:20:59 -07:00
|
|
|
void
|
|
|
|
SuspendWorkersForWindow(nsPIDOMWindow* aWindow);
|
|
|
|
|
|
|
|
void
|
|
|
|
ResumeWorkersForWindow(nsPIDOMWindow* aWindow);
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
class WorkerTask
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
WorkerTask()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual ~WorkerTask()
|
|
|
|
{ }
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkerTask)
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
virtual bool
|
|
|
|
RunTask(JSContext* aCx) = 0;
|
2011-12-04 23:58:27 -08:00
|
|
|
};
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
class WorkerCrossThreadDispatcher
|
|
|
|
{
|
|
|
|
friend class WorkerPrivate;
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
// Must be acquired *before* the WorkerPrivate's mutex, when they're both
|
|
|
|
// held.
|
|
|
|
Mutex mMutex;
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Only created by WorkerPrivate.
|
2014-07-28 10:17:51 -07:00
|
|
|
explicit WorkerCrossThreadDispatcher(WorkerPrivate* aWorkerPrivate);
|
2013-10-23 06:16:49 -07:00
|
|
|
|
|
|
|
// Only called by WorkerPrivate.
|
|
|
|
void
|
|
|
|
Forget()
|
2011-12-04 23:58:27 -08:00
|
|
|
{
|
2013-10-23 06:16:49 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mWorkerPrivate = nullptr;
|
2011-12-04 23:58:27 -08:00
|
|
|
}
|
|
|
|
|
2014-06-18 19:28:59 -07:00
|
|
|
~WorkerCrossThreadDispatcher() {}
|
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkerCrossThreadDispatcher)
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
// Generically useful function for running a bit of C++ code on the worker
|
|
|
|
// thread.
|
|
|
|
bool
|
|
|
|
PostTask(WorkerTask* aTask);
|
2011-12-04 23:58:27 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
WorkerCrossThreadDispatcher*
|
2015-07-18 12:45:35 -07:00
|
|
|
GetWorkerCrossThreadDispatcher(JSContext* aCx, JS::Value aWorker);
|
2011-12-04 23:58:27 -08:00
|
|
|
|
2012-03-09 01:48:50 -08:00
|
|
|
// Random unique constant to facilitate JSPrincipal debugging
|
|
|
|
const uint32_t kJSPrincipalsDebugToken = 0x7e2df9d2;
|
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
namespace exceptions {
|
|
|
|
|
|
|
|
// Implemented in Exceptions.cpp
|
|
|
|
void
|
|
|
|
ThrowDOMExceptionForNSResult(JSContext* aCx, nsresult aNSResult);
|
|
|
|
|
|
|
|
} // namespace exceptions
|
|
|
|
|
2015-03-03 15:51:53 -08:00
|
|
|
nsIGlobalObject*
|
|
|
|
GetGlobalObjectForGlobal(JSObject* global);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsWorkerGlobal(JSObject* global);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsDebuggerGlobal(JSObject* global);
|
|
|
|
|
2015-03-31 03:22:40 -07:00
|
|
|
bool
|
|
|
|
IsDebuggerSandbox(JSObject* object);
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
// Throws the JSMSG_GETTER_ONLY exception. This shouldn't be used going
|
|
|
|
// forward -- getter-only properties should just use JS_PSG for the setter
|
|
|
|
// (implying no setter at all), which will not throw when set in non-strict
|
|
|
|
// code but will in strict code. Old code should use this only for temporary
|
|
|
|
// compatibility reasons.
|
|
|
|
extern bool
|
|
|
|
GetterOnlyJSNative(JSContext* aCx, unsigned aArgc, JS::Value* aVp);
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
#endif // mozilla_dom_workers_workers_h__
|