2011-07-17 23:09:13 +04:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-16 07:40:38 +04:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +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/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-12-28 12:13:38 +04:00
|
|
|
#include "jsdbgapi.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "mozilla/Util.h"
|
2012-05-03 08:35:38 +04:00
|
|
|
#include "mozilla/dom/DOMJSClass.h"
|
|
|
|
#include "mozilla/dom/EventTargetBinding.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/XMLHttpRequestBinding.h"
|
|
|
|
#include "mozilla/dom/XMLHttpRequestUploadBinding.h"
|
2012-05-09 14:00:48 +04:00
|
|
|
#include "mozilla/OSFileConstants.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "nsTraceRefcnt.h"
|
2011-12-28 12:13:38 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "ChromeWorkerScope.h"
|
|
|
|
#include "Events.h"
|
2012-03-31 08:42:20 +04:00
|
|
|
#include "EventListenerManager.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "EventTarget.h"
|
|
|
|
#include "Exceptions.h"
|
2011-08-16 07:40:38 +04:00
|
|
|
#include "File.h"
|
|
|
|
#include "FileReaderSync.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "Location.h"
|
2012-04-24 14:58:08 +04:00
|
|
|
#include "ImageData.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
#include "Navigator.h"
|
|
|
|
#include "Principal.h"
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
#include "Worker.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "XMLHttpRequest.h"
|
|
|
|
|
|
|
|
#include "WorkerInlines.h"
|
|
|
|
|
|
|
|
#define PROPERTY_FLAGS \
|
2012-02-13 12:02:21 +04:00
|
|
|
(JSPROP_ENUMERATE | JSPROP_SHARED)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#define FUNCTION_FLAGS \
|
|
|
|
JSPROP_ENUMERATE
|
|
|
|
|
2011-10-21 14:16:47 +04:00
|
|
|
using namespace mozilla;
|
2011-07-17 23:09:13 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
class WorkerGlobalScope : public EventTarget
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
static JSClass sClass;
|
|
|
|
static JSPropertySpec sProperties[];
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SLOT_wrappedScope = 0,
|
|
|
|
SLOT_wrappedFunction
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SLOT_location = 0,
|
|
|
|
SLOT_navigator,
|
|
|
|
|
|
|
|
SLOT_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
// Must be traced!
|
|
|
|
jsval mSlots[SLOT_COUNT];
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
STRING_onerror = 0,
|
|
|
|
STRING_onclose,
|
|
|
|
|
|
|
|
STRING_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* const sEventStrings[STRING_COUNT];
|
|
|
|
|
|
|
|
protected:
|
|
|
|
WorkerPrivate* mWorker;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static JSClass*
|
|
|
|
Class()
|
|
|
|
{
|
|
|
|
return &sClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto)
|
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, Class(), Construct, 0,
|
2011-07-17 23:09:13 +04:00
|
|
|
sProperties, sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-27 00:57:33 +04:00
|
|
|
using EventTarget::GetEventListener;
|
|
|
|
using EventTarget::SetEventListener;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
protected:
|
2012-03-31 08:42:20 +04:00
|
|
|
WorkerGlobalScope(JSContext* aCx, WorkerPrivate* aWorker)
|
|
|
|
: EventTarget(aCx), mWorker(aWorker)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::WorkerGlobalScope);
|
|
|
|
for (int32 i = 0; i < SLOT_COUNT; i++) {
|
|
|
|
mSlots[i] = JSVAL_VOID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerGlobalScope()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::WorkerGlobalScope);
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
virtual void
|
2012-04-27 00:57:33 +04:00
|
|
|
_trace(JSTracer* aTrc) MOZ_OVERRIDE
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
for (int32 i = 0; i < SLOT_COUNT; i++) {
|
|
|
|
JS_CALL_VALUE_TRACER(aTrc, mSlots[i], "WorkerGlobalScope instance slot");
|
|
|
|
}
|
|
|
|
mWorker->TraceInternal(aTrc);
|
2012-04-27 00:57:33 +04:00
|
|
|
EventTarget::_trace(aTrc);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
virtual void
|
2012-04-27 00:57:33 +04:00
|
|
|
_finalize(JSFreeOp* aFop) MOZ_OVERRIDE
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-04-27 00:57:33 +04:00
|
|
|
EventTarget::_finalize(aFop);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static JSBool
|
2012-04-27 00:57:33 +04:00
|
|
|
GetEventListener(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < STRING_COUNT);
|
|
|
|
|
|
|
|
const char* name = sEventStrings[JSID_TO_INT(aIdval)];
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
JSObject* listener =
|
|
|
|
scope->GetEventListener(NS_ConvertASCIItoUTF16(name + 2), rv);
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
if (rv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to get event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVp = listener ? OBJECT_TO_JSVAL(listener) : JSVAL_NULL;
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-04-27 00:57:33 +04:00
|
|
|
SetEventListener(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSBool aStrict,
|
2011-07-17 23:09:13 +04:00
|
|
|
jsval* aVp)
|
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < STRING_COUNT);
|
|
|
|
|
|
|
|
const char* name = sEventStrings[JSID_TO_INT(aIdval)];
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(*aVp)) {
|
|
|
|
JS_ReportError(aCx, "Not an event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
scope->SetEventListener(NS_ConvertASCIItoUTF16(name + 2),
|
|
|
|
JSVAL_TO_OBJECT(*aVp), rv);
|
2012-05-06 05:15:11 +04:00
|
|
|
if (rv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to set event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static WorkerGlobalScope*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName);
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-05-20 02:03:45 +04:00
|
|
|
GetSelf(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
if (!GetInstancePrivate(aCx, aObj, "self")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVp = OBJECT_TO_JSVAL(aObj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-05-20 02:03:45 +04:00
|
|
|
GetLocation(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
WorkerGlobalScope* scope =
|
|
|
|
GetInstancePrivate(aCx, aObj, sProperties[SLOT_location].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JSVAL_IS_VOID(scope->mSlots[SLOT_location])) {
|
|
|
|
JSString* href, *protocol, *host, *hostname;
|
|
|
|
JSString* port, *pathname, *search, *hash;
|
|
|
|
|
|
|
|
WorkerPrivate::LocationInfo& info = scope->mWorker->GetLocationInfo();
|
|
|
|
|
|
|
|
#define COPY_STRING(_jsstr, _cstr) \
|
|
|
|
if (info. _cstr .IsEmpty()) { \
|
|
|
|
_jsstr = NULL; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
if (!(_jsstr = JS_NewStringCopyN(aCx, info. _cstr .get(), \
|
|
|
|
info. _cstr .Length()))) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
info. _cstr .Truncate(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
COPY_STRING(href, mHref);
|
|
|
|
COPY_STRING(protocol, mProtocol);
|
|
|
|
COPY_STRING(host, mHost);
|
|
|
|
COPY_STRING(hostname, mHostname);
|
|
|
|
COPY_STRING(port, mPort);
|
|
|
|
COPY_STRING(pathname, mPathname);
|
|
|
|
COPY_STRING(search, mSearch);
|
|
|
|
COPY_STRING(hash, mHash);
|
|
|
|
|
|
|
|
#undef COPY_STRING
|
|
|
|
|
|
|
|
JSObject* location = location::Create(aCx, href, protocol, host, hostname,
|
|
|
|
port, pathname, search, hash);
|
|
|
|
if (!location) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope->mSlots[SLOT_location] = OBJECT_TO_JSVAL(location);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVp = scope->mSlots[SLOT_location];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
UnwrapErrorEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JS_ASSERT(aArgc == 1);
|
2012-05-11 19:46:26 +04:00
|
|
|
JS_ASSERT((JS_ARGV(aCx, aVp)[0]).isObject());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
JSObject* wrapper = &JS_CALLEE(aCx, aVp).toObject();
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ASSERT(JS_ObjectIsFunction(aCx, wrapper));
|
|
|
|
|
2011-10-30 05:26:21 +04:00
|
|
|
jsval scope = js::GetFunctionNativeReserved(wrapper, SLOT_wrappedScope);
|
|
|
|
jsval listener = js::GetFunctionNativeReserved(wrapper, SLOT_wrappedFunction);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
JS_ASSERT(scope.isObject());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-05-11 19:46:26 +04:00
|
|
|
JSObject* event = &JS_ARGV(aCx, aVp)[0].toObject();
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
jsval argv[3] = { JSVAL_VOID, JSVAL_VOID, JSVAL_VOID };
|
|
|
|
if (!JS_GetProperty(aCx, event, "message", &argv[0]) ||
|
|
|
|
!JS_GetProperty(aCx, event, "filename", &argv[1]) ||
|
|
|
|
!JS_GetProperty(aCx, event, "lineno", &argv[2])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval rval = JSVAL_VOID;
|
|
|
|
if (!JS_CallFunctionValue(aCx, JSVAL_TO_OBJECT(scope), listener,
|
2011-10-21 14:16:47 +04:00
|
|
|
ArrayLength(argv), argv, &rval)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JSVAL_IS_BOOLEAN(rval) && JSVAL_TO_BOOLEAN(rval) &&
|
|
|
|
!JS_CallFunctionName(aCx, event, "preventDefault", 0, NULL, &rval)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-05-20 02:03:45 +04:00
|
|
|
GetOnErrorListener(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
const char* name = sEventStrings[STRING_onerror];
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
JSObject* adaptor =
|
|
|
|
scope->GetEventListener(NS_ConvertASCIItoUTF16(name + 2), rv);
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
if (rv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to get event listener!");
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (!adaptor) {
|
2012-01-18 00:06:10 +04:00
|
|
|
*aVp = JSVAL_NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
*aVp = js::GetFunctionNativeReserved(adaptor, SLOT_wrappedFunction);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ASSERT(!JSVAL_IS_PRIMITIVE(*aVp));
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-05-20 02:03:45 +04:00
|
|
|
SetOnErrorListener(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval,
|
2011-07-17 23:09:13 +04:00
|
|
|
JSBool aStrict, jsval* aVp)
|
|
|
|
{
|
|
|
|
const char* name = sEventStrings[STRING_onerror];
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(*aVp)) {
|
|
|
|
JS_ReportError(aCx, "Not an event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction* adaptor =
|
|
|
|
js::NewFunctionWithReserved(aCx, UnwrapErrorEvent, 1, 0,
|
|
|
|
JS_GetGlobalObject(aCx), "unwrap");
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!adaptor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* listener = JS_GetFunctionObject(adaptor);
|
|
|
|
if (!listener) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-30 05:26:21 +04:00
|
|
|
js::SetFunctionNativeReserved(listener, SLOT_wrappedScope,
|
|
|
|
OBJECT_TO_JSVAL(aObj));
|
|
|
|
js::SetFunctionNativeReserved(listener, SLOT_wrappedFunction, *aVp);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
scope->SetEventListener(NS_ConvertASCIItoUTF16(name + 2), listener, rv);
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
if (rv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to set event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-05-20 02:03:45 +04:00
|
|
|
GetNavigator(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
WorkerGlobalScope* scope =
|
|
|
|
GetInstancePrivate(aCx, aObj, sProperties[SLOT_navigator].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JSVAL_IS_VOID(scope->mSlots[SLOT_navigator])) {
|
|
|
|
JSObject* navigator = navigator::Create(aCx);
|
|
|
|
if (!navigator) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope->mSlots[SLOT_navigator] = OBJECT_TO_JSVAL(navigator);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVp = scope->mSlots[SLOT_navigator];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Close(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope->mWorker->CloseInternal(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
ImportScripts(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, sFunctions[1].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aArgc && !scriptloader::Load(aCx, aArgc, JS_ARGV(aCx, aVp))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
SetTimeout(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, sFunctions[2].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval dummy;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "v", &dummy)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope->mWorker->SetTimeout(aCx, aArgc, aVp, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
ClearTimeout(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, sFunctions[3].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
uint32_t id;
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "u", &id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope->mWorker->ClearTimeout(aCx, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
SetInterval(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, sFunctions[4].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval dummy;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "v", &dummy)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope->mWorker->SetTimeout(aCx, aArgc, aVp, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
ClearInterval(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
WorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, sFunctions[5].name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 07:54:10 +04:00
|
|
|
uint32_t id;
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "u", &id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope->mWorker->ClearTimeout(aCx, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Dump(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-12-02 01:30:28 +04:00
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetInstancePrivate(aCx, obj, sFunctions[6].name)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aArgc) {
|
|
|
|
JSString* str = JS_ValueToString(aCx, JS_ARGV(aCx, aVp)[0]);
|
|
|
|
if (!str) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoByteString buffer(aCx, str);
|
|
|
|
if (!buffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-07 14:29:34 +04:00
|
|
|
#ifdef ANDROID
|
2012-06-28 18:14:11 +04:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", buffer.ptr());
|
2011-12-07 14:29:34 +04:00
|
|
|
#endif
|
2012-03-06 08:44:37 +04:00
|
|
|
fputs(buffer.ptr(), stdout);
|
|
|
|
fflush(stdout);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
AtoB(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-12-02 01:30:28 +04:00
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetInstancePrivate(aCx, obj, sFunctions[7].name)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval string;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "v", &string)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval result;
|
2011-12-28 12:13:38 +04:00
|
|
|
if (!xpc::Base64Decode(aCx, string, &result)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
BtoA(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2011-12-02 01:30:28 +04:00
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetInstancePrivate(aCx, obj, sFunctions[8].name)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval binary;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "v", &binary)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval result;
|
2011-12-28 12:13:38 +04:00
|
|
|
if (!xpc::Base64Encode(aCx, binary, &result)) {
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSClass WorkerGlobalScope::sClass = {
|
|
|
|
"WorkerGlobalScope",
|
|
|
|
0,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 18:27:58 +04:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec WorkerGlobalScope::sProperties[] = {
|
|
|
|
{ "location", SLOT_location, PROPERTY_FLAGS, GetLocation,
|
|
|
|
js_GetterOnlyPropertyStub },
|
|
|
|
{ sEventStrings[STRING_onerror], STRING_onerror, PROPERTY_FLAGS,
|
|
|
|
GetOnErrorListener, SetOnErrorListener },
|
|
|
|
{ sEventStrings[STRING_onclose], STRING_onclose, PROPERTY_FLAGS,
|
2012-04-27 00:57:33 +04:00
|
|
|
GetEventListener, SetEventListener },
|
2011-07-17 23:09:13 +04:00
|
|
|
{ "navigator", SLOT_navigator, PROPERTY_FLAGS, GetNavigator,
|
|
|
|
js_GetterOnlyPropertyStub },
|
|
|
|
{ "self", 0, PROPERTY_FLAGS, GetSelf, js_GetterOnlyPropertyStub },
|
|
|
|
{ 0, 0, 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec WorkerGlobalScope::sFunctions[] = {
|
|
|
|
JS_FN("close", Close, 0, FUNCTION_FLAGS),
|
|
|
|
JS_FN("importScripts", ImportScripts, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("setTimeout", SetTimeout, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("clearTimeout", ClearTimeout, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("setInterval", SetInterval, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("clearInterval", ClearTimeout, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("dump", Dump, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("atob", AtoB, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FN("btoa", BtoA, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* const WorkerGlobalScope::sEventStrings[STRING_COUNT] = {
|
|
|
|
"onerror",
|
|
|
|
"onclose"
|
|
|
|
};
|
|
|
|
|
|
|
|
class DedicatedWorkerGlobalScope : public WorkerGlobalScope
|
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
static DOMJSClass sClass;
|
2011-07-17 23:09:13 +04:00
|
|
|
static JSPropertySpec sProperties[];
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
STRING_onmessage = 0,
|
|
|
|
|
|
|
|
STRING_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* const sEventStrings[STRING_COUNT];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static JSClass*
|
|
|
|
Class()
|
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
return sClass.ToJSClass();
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto)
|
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, Class(), Construct, 0,
|
2011-07-17 23:09:13 +04:00
|
|
|
sProperties, sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
InitPrivate(JSContext* aCx, JSObject* aObj, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == Class());
|
|
|
|
|
2012-05-03 08:35:38 +04:00
|
|
|
dom::AllocateProtoOrIfaceCache(aObj);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
nsRefPtr<DedicatedWorkerGlobalScope> scope =
|
|
|
|
new DedicatedWorkerGlobalScope(aCx, aWorkerPrivate);
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2012-06-06 23:59:46 +04:00
|
|
|
js::SetReservedSlot(aObj, DOM_OBJECT_SLOT, PRIVATE_TO_JSVAL(scope));
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
scope->SetIsDOMBinding();
|
|
|
|
scope->SetWrapper(aObj);
|
|
|
|
|
|
|
|
scope.forget();
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-03-31 08:42:20 +04:00
|
|
|
DedicatedWorkerGlobalScope(JSContext* aCx, WorkerPrivate* aWorker)
|
|
|
|
: WorkerGlobalScope(aCx, aWorker)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::DedicatedWorkerGlobalScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DedicatedWorkerGlobalScope()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::DedicatedWorkerGlobalScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-04-27 00:57:33 +04:00
|
|
|
using EventTarget::GetEventListener;
|
|
|
|
using EventTarget::SetEventListener;
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
static JSBool
|
2012-04-27 00:57:33 +04:00
|
|
|
GetEventListener(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < STRING_COUNT);
|
|
|
|
|
|
|
|
const char* name = sEventStrings[JSID_TO_INT(aIdval)];
|
|
|
|
DedicatedWorkerGlobalScope* scope = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
JSObject* listener =
|
|
|
|
scope->GetEventListener(NS_ConvertASCIItoUTF16(name + 2), rv);
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
if (rv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to get event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aVp = listener ? OBJECT_TO_JSVAL(listener) : JSVAL_NULL;
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-04-27 00:57:33 +04:00
|
|
|
SetEventListener(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSBool aStrict,
|
|
|
|
jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < STRING_COUNT);
|
|
|
|
|
|
|
|
const char* name = sEventStrings[JSID_TO_INT(aIdval)];
|
|
|
|
DedicatedWorkerGlobalScope* scope = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(*aVp)) {
|
|
|
|
JS_ReportError(aCx, "Not an event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
ErrorResult rv;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
scope->SetEventListener(NS_ConvertASCIItoUTF16(name + 2),
|
|
|
|
JSVAL_TO_OBJECT(*aVp), rv);
|
|
|
|
|
2012-05-06 05:15:11 +04:00
|
|
|
if (rv.Failed()) {
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ReportError(aCx, "Failed to set event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static DedicatedWorkerGlobalScope*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-04 04:54:57 +04:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2012-03-31 08:42:20 +04:00
|
|
|
if (classPtr == Class()) {
|
2012-06-06 23:59:46 +04:00
|
|
|
return UnwrapDOMObject<DedicatedWorkerGlobalScope>(aObj);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
2012-03-31 08:42:20 +04:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, Class()->name, aFunctionName,
|
2012-02-03 22:00:08 +04:00
|
|
|
classPtr->name);
|
2011-07-17 23:09:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
2012-03-31 08:42:20 +04:00
|
|
|
Class()->name);
|
2011-07-17 23:09:13 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-05-20 02:03:45 +04:00
|
|
|
Resolve(JSContext* aCx, JSHandleObject aObj, JSHandleId aId, unsigned aFlags,
|
2012-07-04 22:12:16 +04:00
|
|
|
JSMutableHandleObject aObjp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSBool resolved;
|
|
|
|
if (!JS_ResolveStandardClass(aCx, aObj, aId, &resolved)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-05 00:34:42 +04:00
|
|
|
aObjp.set(resolved ? aObj.get() : NULL);
|
2011-07-17 23:09:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 18:34:55 +04:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == Class());
|
2011-07-17 23:09:13 +04:00
|
|
|
DedicatedWorkerGlobalScope* scope =
|
2012-06-06 23:59:46 +04:00
|
|
|
UnwrapDOMObject<DedicatedWorkerGlobalScope>(aObj);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (scope) {
|
2012-03-31 08:42:20 +04:00
|
|
|
DestroyProtoOrIfaceCache(aObj);
|
2012-04-27 00:57:33 +04:00
|
|
|
scope->_finalize(aFop);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Trace(JSTracer* aTrc, JSObject* aObj)
|
|
|
|
{
|
2012-03-31 08:42:20 +04:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == Class());
|
2011-07-17 23:09:13 +04:00
|
|
|
DedicatedWorkerGlobalScope* scope =
|
2012-06-06 23:59:46 +04:00
|
|
|
UnwrapDOMObject<DedicatedWorkerGlobalScope>(aObj);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (scope) {
|
2012-05-02 16:49:43 +04:00
|
|
|
mozilla::dom::TraceProtoOrIfaceCache(aTrc, aObj);
|
2012-04-27 00:57:33 +04:00
|
|
|
scope->_trace(aTrc);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
PostMessage(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-02 01:30:28 +04:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
const char*& name = sFunctions[0].name;
|
|
|
|
DedicatedWorkerGlobalScope* scope = GetInstancePrivate(aCx, obj, name);
|
|
|
|
if (!scope) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval message;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "v", &message)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope->mWorker->PostMessageToParent(aCx, message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
MOZ_STATIC_ASSERT(prototypes::MaxProtoChainLength == 3,
|
|
|
|
"The MaxProtoChainLength must match our manual DOMJSClasses");
|
|
|
|
|
|
|
|
DOMJSClass DedicatedWorkerGlobalScope::sClass = {
|
|
|
|
{
|
|
|
|
"DedicatedWorkerGlobalScope",
|
|
|
|
JSCLASS_DOM_GLOBAL | JSCLASS_IS_DOMJSCLASS | JSCLASS_IMPLEMENTS_BARRIERS |
|
|
|
|
JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(3) | JSCLASS_NEW_RESOLVE,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, reinterpret_cast<JSResolveOp>(Resolve), JS_ConvertStub,
|
|
|
|
Finalize, NULL, NULL, NULL, NULL, Trace
|
|
|
|
},
|
|
|
|
{ prototypes::id::EventTarget_workers, prototypes::id::_ID_Count,
|
|
|
|
prototypes::id::_ID_Count },
|
2012-06-06 23:59:46 +04:00
|
|
|
-1, false, NULL
|
2011-07-17 23:09:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec DedicatedWorkerGlobalScope::sProperties[] = {
|
|
|
|
{ sEventStrings[STRING_onmessage], STRING_onmessage, PROPERTY_FLAGS,
|
2012-04-27 00:57:33 +04:00
|
|
|
GetEventListener, SetEventListener },
|
2011-07-17 23:09:13 +04:00
|
|
|
{ 0, 0, 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec DedicatedWorkerGlobalScope::sFunctions[] = {
|
|
|
|
JS_FN("postMessage", PostMessage, 1, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* const DedicatedWorkerGlobalScope::sEventStrings[STRING_COUNT] = {
|
|
|
|
"onmessage",
|
|
|
|
};
|
|
|
|
|
|
|
|
WorkerGlobalScope*
|
|
|
|
WorkerGlobalScope::GetInstancePrivate(JSContext* aCx, JSObject* aObj,
|
|
|
|
const char* aFunctionName)
|
|
|
|
{
|
2012-02-04 04:54:57 +04:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2012-03-31 08:42:20 +04:00
|
|
|
|
|
|
|
// We can only make DedicatedWorkerGlobalScope, not WorkerGlobalScope, so this
|
|
|
|
// should never happen.
|
|
|
|
JS_ASSERT(classPtr != Class());
|
|
|
|
|
|
|
|
if (classPtr == DedicatedWorkerGlobalScope::Class()) {
|
2012-06-06 23:59:46 +04:00
|
|
|
return UnwrapDOMObject<DedicatedWorkerGlobalScope>(aObj);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_INCOMPATIBLE_PROTO,
|
2012-02-03 22:00:08 +04:00
|
|
|
sClass.name, aFunctionName, classPtr->name);
|
2011-07-17 23:09:13 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateDedicatedWorkerGlobalScope(JSContext* aCx)
|
|
|
|
{
|
2012-05-03 08:35:38 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-03-31 08:42:20 +04:00
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
|
|
|
|
JS_ASSERT(worker);
|
|
|
|
|
|
|
|
JSObject* global =
|
2012-06-20 19:57:29 +04:00
|
|
|
JS_NewGlobalObject(aCx, DedicatedWorkerGlobalScope::Class(),
|
|
|
|
GetWorkerPrincipal());
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!global) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-11 00:04:24 +04:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(aCx, global)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
// Make the private slots now so that all our instance checks succeed.
|
|
|
|
if (!DedicatedWorkerGlobalScope::InitPrivate(aCx, global, worker)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proto chain should be:
|
|
|
|
// global -> DedicatedWorkerGlobalScope
|
|
|
|
// -> WorkerGlobalScope
|
|
|
|
// -> EventTarget
|
|
|
|
// -> Object
|
|
|
|
|
2012-04-23 18:10:56 +04:00
|
|
|
JSObject* eventTargetProto =
|
2012-05-03 08:35:38 +04:00
|
|
|
EventTargetBinding_workers::GetProtoObject(aCx, global, global);
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!eventTargetProto) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* scopeProto =
|
|
|
|
WorkerGlobalScope::InitClass(aCx, global, eventTargetProto);
|
|
|
|
if (!scopeProto) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* dedicatedScopeProto =
|
|
|
|
DedicatedWorkerGlobalScope::InitClass(aCx, global, scopeProto);
|
|
|
|
if (!dedicatedScopeProto) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetPrototype(aCx, global, dedicatedScopeProto)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* workerProto = worker::InitClass(aCx, global, eventTargetProto,
|
|
|
|
false);
|
|
|
|
if (!workerProto) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
if (worker->IsChromeWorker() &&
|
|
|
|
(!chromeworker::InitClass(aCx, global, workerProto, false) ||
|
2012-05-09 14:00:48 +04:00
|
|
|
!DefineChromeWorkerFunctions(aCx, global)) ||
|
|
|
|
!DefineOSFileConstants(aCx, global)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return NULL;
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init other classes we care about.
|
|
|
|
if (!events::InitClasses(aCx, global, false) ||
|
2011-08-16 07:40:38 +04:00
|
|
|
!file::InitClasses(aCx, global) ||
|
|
|
|
!filereadersync::InitClass(aCx, global) ||
|
2011-07-17 23:09:13 +04:00
|
|
|
!exceptions::InitClasses(aCx, global) ||
|
|
|
|
!location::InitClass(aCx, global) ||
|
2012-04-24 14:58:08 +04:00
|
|
|
!imagedata::InitClass(aCx, global) ||
|
2011-07-17 23:09:13 +04:00
|
|
|
!navigator::InitClass(aCx, global)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-31 08:42:20 +04:00
|
|
|
// Init other paris-bindings.
|
2012-05-03 08:35:38 +04:00
|
|
|
if (!XMLHttpRequestBinding_workers::CreateInterfaceObjects(aCx, global,
|
|
|
|
global) ||
|
|
|
|
!XMLHttpRequestUploadBinding_workers::CreateInterfaceObjects(aCx, global,
|
|
|
|
global)) {
|
2012-03-31 08:42:20 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
if (!JS_DefineProfilingFunctions(aCx, global)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2012-01-18 22:05:38 +04:00
|
|
|
bool
|
|
|
|
ClassIsWorkerGlobalScope(JSClass* aClass)
|
|
|
|
{
|
|
|
|
return WorkerGlobalScope::Class() == aClass ||
|
|
|
|
DedicatedWorkerGlobalScope::Class() == aClass;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
END_WORKERS_NAMESPACE
|