2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
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"
|
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"
|
|
|
|
|
2013-06-06 10:28:47 -07:00
|
|
|
class nsIScriptContext;
|
2011-07-17 12:09:13 -07:00
|
|
|
class nsPIDOMWindow;
|
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::ContextOptions contextOptions;
|
|
|
|
JS::CompartmentOptions compartmentOptions;
|
2013-05-16 15:49:43 -07:00
|
|
|
int32_t maxScriptRuntime;
|
|
|
|
|
|
|
|
JSContentChromeSettings()
|
2013-12-10 03:43:16 -08:00
|
|
|
: contextOptions(), 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
|
|
|
|
{
|
|
|
|
WORKERPREF_DUMP = 0, // browser.dom.window.dump.enabled
|
|
|
|
WORKERPREF_COUNT
|
|
|
|
};
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
// All of these are implemented in RuntimeService.cpp
|
2014-07-04 12:41:27 -07:00
|
|
|
|
|
|
|
// Resolves all of the worker classes onto |aObjp| if one of them matches |aId|
|
|
|
|
// or if |aId| is JSID_VOID.
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2013-06-21 06:12:46 -07:00
|
|
|
ResolveWorkerClasses(JSContext* aCx, JS::Handle<JSObject*> aObj, JS::Handle<jsid> aId,
|
2014-04-25 14:11:02 -07:00
|
|
|
JS::MutableHandle<JSObject*> aObjp);
|
2011-07-12 09:32:14 -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
|
2013-06-05 07:04:23 -07:00
|
|
|
SuspendWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
void
|
2013-06-05 07:04:23 -07:00
|
|
|
ResumeWorkersForWindow(nsPIDOMWindow* aWindow);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
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.
|
|
|
|
WorkerCrossThreadDispatcher(WorkerPrivate* aWorkerPrivate);
|
|
|
|
|
|
|
|
// 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*
|
|
|
|
GetWorkerCrossThreadDispatcher(JSContext* aCx, jsval aWorker);
|
|
|
|
|
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
|
|
|
|
|
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__
|