2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-03-31 08:42:20 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-11-27 09:16:40 +03:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifndef mozilla_dom_workers_workers_h__
|
|
|
|
#define mozilla_dom_workers_workers_h__
|
2008-11-27 09:16:40 +03:00
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
#include "jsapi.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-12-05 11:58:27 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsDebug.h"
|
2013-09-23 21:25:00 +04:00
|
|
|
#include "nsString.h"
|
2015-02-12 12:50:05 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIWeakReferenceUtils.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2015-05-25 21:21:05 +03:00
|
|
|
#include "mozilla/dom/ChannelInfo.h"
|
2008-11-27 09:16:40 +03:00
|
|
|
|
2011-07-17 23:09:13 +04: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 20:32:14 +04:00
|
|
|
|
2011-12-29 22:28:09 +04:00
|
|
|
#define WORKERS_SHUTDOWN_TOPIC "web-workers-shutdown"
|
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
class nsIContentSecurityPolicy;
|
2013-06-06 21:28:47 +04:00
|
|
|
class nsIScriptContext;
|
2015-02-12 12:50:05 +03:00
|
|
|
class nsIGlobalObject;
|
2011-07-17 23:09:13 +04:00
|
|
|
class nsPIDOMWindow;
|
2015-02-12 12:50:05 +03:00
|
|
|
class nsIPrincipal;
|
|
|
|
class nsILoadGroup;
|
|
|
|
class nsITabChild;
|
|
|
|
class nsIChannel;
|
2015-07-15 22:21:40 +03:00
|
|
|
class nsIRunnable;
|
2015-02-12 12:50:05 +03:00
|
|
|
class nsIURI;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
class PrincipalInfo;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace ipc
|
2015-03-04 16:54:16 +03: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 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2011-07-12 20:32:14 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
2008-11-27 09:16:40 +03:00
|
|
|
|
2011-12-05 11:58:27 +04:00
|
|
|
class WorkerPrivate;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
struct PrivatizableBase
|
|
|
|
{ };
|
2011-07-12 20:32:14 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
#else
|
|
|
|
inline void
|
|
|
|
AssertIsOnMainThread()
|
|
|
|
{ }
|
|
|
|
#endif
|
2011-07-12 20:32:14 +04:00
|
|
|
|
2013-05-17 02:49:43 +04: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 15:43:16 +04:00
|
|
|
JS::CompartmentOptions compartmentOptions;
|
2013-05-17 02:49:43 +04:00
|
|
|
int32_t maxScriptRuntime;
|
|
|
|
|
|
|
|
JSContentChromeSettings()
|
2014-07-30 08:00:37 +04:00
|
|
|
: compartmentOptions(), maxScriptRuntime(0)
|
2013-05-17 02:49:43 +04:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
JSContentChromeSettings chrome;
|
|
|
|
JSContentChromeSettings content;
|
|
|
|
JSGCSettingsArray gcSettings;
|
2014-02-26 13:25:36 +04:00
|
|
|
JS::RuntimeOptions runtimeOptions;
|
2013-12-25 12:21:59 +04:00
|
|
|
|
2013-05-17 02:49:43 +04:00
|
|
|
#ifdef JS_GC_ZEAL
|
|
|
|
uint8_t gcZeal;
|
|
|
|
uint32_t gcZealFrequency;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JSSettings()
|
|
|
|
#ifdef JS_GC_ZEAL
|
2013-12-25 12:21:59 +04:00
|
|
|
: gcZeal(0), gcZealFrequency(0)
|
2013-05-17 02:49:43 +04: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 23:27:15 +04:00
|
|
|
enum WorkerPreference
|
|
|
|
{
|
|
|
|
WORKERPREF_DUMP = 0, // browser.dom.window.dump.enabled
|
2015-03-02 16:12:00 +03:00
|
|
|
WORKERPREF_DOM_CACHES, // dom.caches.enabled
|
2015-04-04 08:46:07 +03:00
|
|
|
WORKERPREF_SERVICEWORKERS, // dom.serviceWorkers.enabled
|
2015-06-13 02:28:13 +03:00
|
|
|
WORKERPREF_INTERCEPTION_ENABLED, // dom.serviceWorkers.interception.enabled
|
2015-06-26 04:53:02 +03:00
|
|
|
WORKERPREF_DOM_WORKERNOTIFICATION, // dom.webnotifications.workers.enabled
|
2015-06-28 06:19:23 +03:00
|
|
|
WORKERPREF_DOM_CACHES_TESTING, // dom.caches.testing.enabled
|
2015-06-28 06:19:24 +03:00
|
|
|
WORKERPREF_SERVICEWORKERS_TESTING, // dom.serviceWorkers.testing.enabled
|
2015-07-03 06:09:20 +03:00
|
|
|
WORKERPREF_INTERCEPTION_OPAQUE_ENABLED, // dom.serviceWorkers.interception.opaque.enabled
|
2015-07-28 19:33:37 +03:00
|
|
|
WORKERPREF_PERFORMANCE_LOGGING_ENABLED, // dom.performance.enable_user_timing_logging
|
2015-07-27 22:35:28 +03:00
|
|
|
WORKERPREF_PUSH, // dom.push.enabled
|
2015-07-28 00:57:34 +03:00
|
|
|
WORKERPREF_REQUESTCONTEXT, // dom.requestcontext.enabled
|
2013-11-24 23:27:15 +04:00
|
|
|
WORKERPREF_COUNT
|
|
|
|
};
|
|
|
|
|
2015-02-12 12:50:05 +03: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 22:21:40 +03: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 19:28:04 +03:00
|
|
|
class InterfaceRequestor final : public nsIInterfaceRequestor
|
2015-02-12 12:50:05 +03:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
public:
|
|
|
|
InterfaceRequestor(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup);
|
|
|
|
void MaybeAddTabChild(nsILoadGroup* aLoadGroup);
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD GetInterface(const nsIID& aIID, void** aSink) override;
|
2015-02-12 12:50:05 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
~InterfaceRequestor() { }
|
|
|
|
|
|
|
|
already_AddRefed<nsITabChild> GetAnyLiveTabChild();
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadContext> mLoadContext;
|
2015-03-29 20:43:34 +03:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mOuterRequestor;
|
2015-02-12 12:50:05 +03: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
|
|
|
|
nsRefPtr<InterfaceRequestor> mInterfaceRequestor;
|
|
|
|
|
|
|
|
nsAutoPtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
|
|
|
|
nsCString mDomain;
|
|
|
|
|
2015-03-19 21:41:42 +03:00
|
|
|
nsString mServiceWorkerCacheName;
|
|
|
|
|
2015-05-25 21:21:05 +03:00
|
|
|
ChannelInfo mChannelInfo;
|
2015-04-24 01:59:52 +03:00
|
|
|
|
2015-02-12 12:50:05 +03:00
|
|
|
uint64_t mWindowID;
|
2015-05-14 22:41:42 +03:00
|
|
|
uint64_t mServiceWorkerID;
|
2015-02-12 12:50:05 +03:00
|
|
|
|
|
|
|
bool mFromWindow;
|
|
|
|
bool mEvalAllowed;
|
|
|
|
bool mReportCSPViolations;
|
|
|
|
bool mXHRParamsAllowed;
|
|
|
|
bool mPrincipalIsSystem;
|
|
|
|
bool mIsInPrivilegedApp;
|
|
|
|
bool mIsInCertifiedApp;
|
|
|
|
bool mIndexedDBAllowed;
|
2015-06-26 21:18:18 +03:00
|
|
|
bool mPrivateBrowsing;
|
2015-06-28 06:19:24 +03:00
|
|
|
bool mServiceWorkersTestingInWindow;
|
2015-02-12 12:50:05 +03:00
|
|
|
|
|
|
|
WorkerLoadInfo();
|
|
|
|
~WorkerLoadInfo();
|
|
|
|
|
|
|
|
void StealFrom(WorkerLoadInfo& aOther);
|
|
|
|
};
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// All of these are implemented in RuntimeService.cpp
|
2014-07-04 23:41:27 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
void
|
2013-06-05 18:04:23 +04:00
|
|
|
CancelWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
void
|
2015-04-01 12:00:19 +03:00
|
|
|
FreezeWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
void
|
2015-04-01 12:00:19 +03:00
|
|
|
ThawWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class WorkerTask
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
WorkerTask()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual ~WorkerTask()
|
|
|
|
{ }
|
2011-12-05 11:58:27 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkerTask)
|
2011-12-05 11:58:27 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
virtual bool
|
|
|
|
RunTask(JSContext* aCx) = 0;
|
2011-12-05 11:58:27 +04:00
|
|
|
};
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
class WorkerCrossThreadDispatcher
|
|
|
|
{
|
|
|
|
friend class WorkerPrivate;
|
2011-12-05 11:58:27 +04:00
|
|
|
|
2013-10-23 17:16:49 +04: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 21:17:51 +04:00
|
|
|
explicit WorkerCrossThreadDispatcher(WorkerPrivate* aWorkerPrivate);
|
2013-10-23 17:16:49 +04:00
|
|
|
|
|
|
|
// Only called by WorkerPrivate.
|
|
|
|
void
|
|
|
|
Forget()
|
2011-12-05 11:58:27 +04:00
|
|
|
{
|
2013-10-23 17:16:49 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mWorkerPrivate = nullptr;
|
2011-12-05 11:58:27 +04:00
|
|
|
}
|
|
|
|
|
2014-06-19 06:28:59 +04:00
|
|
|
~WorkerCrossThreadDispatcher() {}
|
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkerCrossThreadDispatcher)
|
2011-12-05 11:58:27 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
// Generically useful function for running a bit of C++ code on the worker
|
|
|
|
// thread.
|
|
|
|
bool
|
|
|
|
PostTask(WorkerTask* aTask);
|
2011-12-05 11:58:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
WorkerCrossThreadDispatcher*
|
2015-07-18 22:45:35 +03:00
|
|
|
GetWorkerCrossThreadDispatcher(JSContext* aCx, JS::Value aWorker);
|
2011-12-05 11:58:27 +04:00
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
// Random unique constant to facilitate JSPrincipal debugging
|
|
|
|
const uint32_t kJSPrincipalsDebugToken = 0x7e2df9d2;
|
|
|
|
|
2012-04-25 03:50:00 +04:00
|
|
|
namespace exceptions {
|
|
|
|
|
|
|
|
// Implemented in Exceptions.cpp
|
|
|
|
void
|
|
|
|
ThrowDOMExceptionForNSResult(JSContext* aCx, nsresult aNSResult);
|
|
|
|
|
|
|
|
} // namespace exceptions
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
nsIGlobalObject*
|
|
|
|
GetGlobalObjectForGlobal(JSObject* global);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsWorkerGlobal(JSObject* global);
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsDebuggerGlobal(JSObject* global);
|
|
|
|
|
2015-03-31 13:22:40 +03:00
|
|
|
bool
|
|
|
|
IsDebuggerSandbox(JSObject* object);
|
|
|
|
|
2013-08-09 02:55:23 +04: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 23:09:13 +04:00
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
#endif // mozilla_dom_workers_workers_h__
|