2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-15 20:40:38 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -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/. */
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-10-21 03:16:47 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "Events.h"
|
2013-08-08 15:55:23 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-10-04 07:06:54 -07:00
|
|
|
#include "jsfriendapi.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#include "nsTraceRefcnt.h"
|
|
|
|
|
|
|
|
#include "WorkerInlines.h"
|
2011-08-15 20:40:38 -07:00
|
|
|
#include "WorkerPrivate.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#define FUNCTION_FLAGS \
|
|
|
|
JSPROP_ENUMERATE
|
|
|
|
|
2011-10-21 03:16:47 -07:00
|
|
|
using namespace mozilla;
|
2011-07-17 12:09:13 -07:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Event : public PrivatizableBase
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass sClass;
|
|
|
|
static const JSClass sMainRuntimeClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
static const JSPropertySpec sProperties[];
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec sFunctions[];
|
2013-08-08 15:55:23 -07:00
|
|
|
static const dom::ConstantSpec sStaticConstants[];
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mStopPropagationCalled;
|
2011-11-05 14:41:16 -07:00
|
|
|
bool mStopImmediatePropagationCalled;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
static bool
|
2013-09-11 05:49:05 -07:00
|
|
|
IsThisClass(const JSClass* aClass)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
return aClass == &sClass || aClass == &sMainRuntimeClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2013-05-04 00:53:00 -07:00
|
|
|
InitClass(JSContext* aCx, JS::Handle<JSObject*> aObj, bool aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> parentProto(aCx);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
if (aMainRuntime) {
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JS::Value> windowPropVal(aCx);
|
2013-07-26 02:00:38 -07:00
|
|
|
if (!JS_GetProperty(aCx, aObj, sClass.name, &windowPropVal)) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(windowPropVal)) {
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JS::Value> protoVal(aCx);
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!JS_GetProperty(aCx, JSVAL_TO_OBJECT(windowPropVal), "prototype",
|
2013-07-26 02:00:38 -07:00
|
|
|
&protoVal)) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(protoVal)) {
|
|
|
|
parentProto = JSVAL_TO_OBJECT(protoVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = parentProto ? &sMainRuntimeClass : &sClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> proto(aCx, JS_InitClass(aCx, aObj, parentProto, clasp, Construct, 0,
|
2013-08-08 15:55:23 -07:00
|
|
|
sProperties, sFunctions, nullptr, nullptr));
|
|
|
|
if (!proto) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2013-08-08 15:55:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> ctor(aCx, JS_GetConstructor(aCx, proto));
|
|
|
|
if (!ctor) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2013-08-08 15:55:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dom::DefineConstants(aCx, ctor, sStaticConstants) ||
|
|
|
|
!dom::DefineConstants(aCx, proto, sStaticConstants)) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2013-05-09 00:27:40 -07:00
|
|
|
Create(JSContext* aCx, JS::Handle<JSObject*> aParent, JS::Handle<JSString*> aType,
|
|
|
|
bool aBubbles, bool aCancelable, bool aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JSObject* obj = JS_NewObject(aCx, clasp, nullptr, aParent);
|
2011-07-17 12:09:13 -07:00
|
|
|
if (obj) {
|
|
|
|
Event* priv = new Event();
|
2012-02-05 12:07:23 -08:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitEventCommon(obj, priv, aType, aBubbles, aCancelable, true);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-02-05 12:07:23 -08:00
|
|
|
IsSupportedClass(JSObject* aEvent)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
return !!GetPrivate(aEvent);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
static void
|
|
|
|
SetTarget(JSObject* aEvent, JSObject* aTarget)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
JS_ASSERT(IsSupportedClass(aEvent));
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
jsval target = OBJECT_TO_JSVAL(aTarget);
|
2012-02-05 12:07:23 -08:00
|
|
|
JS_SetReservedSlot(aEvent, SLOT_target, target);
|
|
|
|
JS_SetReservedSlot(aEvent, SLOT_currentTarget, target);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-02-05 12:07:23 -08:00
|
|
|
WasCanceled(JSObject* aEvent)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
JS_ASSERT(IsSupportedClass(aEvent));
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
jsval canceled = JS_GetReservedSlot(aEvent, SLOT_defaultPrevented);
|
2011-07-17 12:09:13 -07:00
|
|
|
return JSVAL_TO_BOOLEAN(canceled);
|
|
|
|
}
|
|
|
|
|
2011-11-05 14:41:16 -07:00
|
|
|
static bool
|
2012-02-05 12:07:23 -08:00
|
|
|
ImmediatePropagationStopped(JSObject* aEvent)
|
2011-11-05 14:41:16 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
Event* event = GetPrivate(aEvent);
|
2011-11-05 14:41:16 -07:00
|
|
|
return event ? event->mStopImmediatePropagationCalled : false;
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
protected:
|
|
|
|
Event()
|
2011-11-05 14:41:16 -07:00
|
|
|
: mStopPropagationCalled(false), mStopImmediatePropagationCalled(false)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::Event);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Event()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::Event);
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
enum EventPhase {
|
2011-07-17 12:09:13 -07:00
|
|
|
CAPTURING_PHASE = 1,
|
|
|
|
AT_TARGET = 2,
|
|
|
|
BUBBLING_PHASE = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_type = 0,
|
|
|
|
SLOT_target,
|
|
|
|
SLOT_currentTarget,
|
|
|
|
SLOT_eventPhase,
|
|
|
|
SLOT_bubbles,
|
|
|
|
SLOT_cancelable,
|
|
|
|
SLOT_timeStamp,
|
|
|
|
SLOT_defaultPrevented,
|
|
|
|
SLOT_isTrusted,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_type
|
|
|
|
};
|
|
|
|
|
|
|
|
static Event*
|
2012-02-05 12:07:23 -08:00
|
|
|
GetPrivate(JSObject* aEvent);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
static Event*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
Event* priv = GetPrivate(aObj);
|
2012-02-03 10:00:08 -08:00
|
|
|
if (priv) {
|
|
|
|
return priv;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
2011-07-17 12:09:13 -07:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_GetClass(aObj)->name);
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
static void
|
|
|
|
InitEventCommon(JSObject* aObj, Event* aEvent, JSString* aType,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool aBubbles, bool aCancelable, bool aIsTrusted)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
aEvent->mStopPropagationCalled = false;
|
2011-11-05 14:41:16 -07:00
|
|
|
aEvent->mStopImmediatePropagationCalled = false;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
JS_SetReservedSlot(aObj, SLOT_type, STRING_TO_JSVAL(aType));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_target, JSVAL_NULL);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_currentTarget, JSVAL_NULL);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_eventPhase, INT_TO_JSVAL(CAPTURING_PHASE));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_bubbles,
|
|
|
|
aBubbles ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_cancelable,
|
|
|
|
aCancelable ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_timeStamp, JS::NumberValue(double(JS_Now())));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_defaultPrevented, JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_isTrusted,
|
|
|
|
aIsTrusted ? JSVAL_TRUE : JSVAL_FALSE);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr, JSMSG_WRONG_CONSTRUCTOR,
|
2011-07-17 12:09:13 -07:00
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_ASSERT(IsThisClass(JS_GetClass(aObj)));
|
2012-02-05 12:07:23 -08:00
|
|
|
delete GetJSPrivateSafeish<Event>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-08-08 15:55:23 -07:00
|
|
|
IsEvent(JS::Handle<JS::Value> v)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-08-08 15:55:23 -07:00
|
|
|
return v.isObject() && GetPrivate(&v.toObject()) != nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
template<SLOT Slot>
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-08-08 15:55:23 -07:00
|
|
|
GetPropertyImpl(JSContext* aCx, JS::CallArgs aArgs)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-08-08 15:55:23 -07:00
|
|
|
aArgs.rval().set(JS_GetReservedSlot(&aArgs.thisv().toObject(), Slot));
|
2011-07-17 12:09:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
// This struct (versus just templating the method directly) is needed only for
|
|
|
|
// gcc 4.4 (and maybe 4.5 -- 4.6 is okay) being too braindead to allow
|
|
|
|
// GetProperty<Slot> and friends in the JSPropertySpec[] below.
|
|
|
|
template<SLOT Slot>
|
|
|
|
struct Property
|
|
|
|
{
|
|
|
|
static bool
|
|
|
|
Get(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
static_assert(SLOT_FIRST <= Slot && Slot < SLOT_COUNT, "bad slot");
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsEvent, GetPropertyImpl<Slot> >(aCx, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
StopPropagation(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->mStopPropagationCalled = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
StopImmediatePropagation(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-11-05 14:41:16 -07:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-05 14:41:16 -07:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[3].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->mStopImmediatePropagationCalled = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-02 00:41:57 -07:00
|
|
|
|
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
PreventDefault(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_THIS_OBJECT(aCx, aVp));
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[1].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
jsval cancelableVal = JS_GetReservedSlot(obj, SLOT_cancelable);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
if (JSVAL_TO_BOOLEAN(cancelableVal))
|
|
|
|
JS_SetReservedSlot(obj, SLOT_defaultPrevented, cancelableVal);
|
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
InitEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_THIS_OBJECT(aCx, aVp));
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[2].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSString*> type(aCx);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool bubbles, cancelable;
|
2013-05-04 00:53:00 -07:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "Sbb", type.address(),
|
2011-07-17 12:09:13 -07:00
|
|
|
&bubbles, &cancelable)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
InitEventCommon(obj, event, type, bubbles, cancelable, false);
|
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_EVENT_CLASS(_varname, _name) \
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass _varname = { \
|
2011-07-17 12:09:13 -07:00
|
|
|
_name, \
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT), \
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize \
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-07-08 09:55:14 -07:00
|
|
|
DECL_EVENT_CLASS(Event::sClass, "WorkerEvent")
|
2011-07-17 12:09:13 -07:00
|
|
|
DECL_EVENT_CLASS(Event::sMainRuntimeClass, "WorkerEvent")
|
|
|
|
|
|
|
|
#undef DECL_EVENT_CLASS
|
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
const JSPropertySpec Event::sProperties[] = {
|
2013-08-08 15:55:23 -07:00
|
|
|
JS_PSGS("type", Property<SLOT_type>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("target", Property<SLOT_target>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("currentTarget", Property<SLOT_currentTarget>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("eventPhase", Property<SLOT_eventPhase>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("bubbles", Property<SLOT_bubbles>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("cancelable", Property<SLOT_cancelable>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("timeStamp", Property<SLOT_timeStamp>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("defaultPrevented", Property<SLOT_defaultPrevented>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("isTrusted", Property<SLOT_isTrusted>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PS_END
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
const JSFunctionSpec Event::sFunctions[] = {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_FN("stopPropagation", StopPropagation, 0, FUNCTION_FLAGS),
|
|
|
|
JS_FN("preventDefault", PreventDefault, 0, FUNCTION_FLAGS),
|
|
|
|
JS_FN("initEvent", InitEvent, 3, FUNCTION_FLAGS),
|
2011-11-05 14:41:16 -07:00
|
|
|
JS_FN("stopImmediatePropagation", StopImmediatePropagation, 0, FUNCTION_FLAGS),
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
const dom::ConstantSpec Event::sStaticConstants[] = {
|
|
|
|
{ "CAPTURING_PHASE", JS::Int32Value(CAPTURING_PHASE) },
|
|
|
|
{ "AT_TARGET", JS::Int32Value(AT_TARGET) },
|
|
|
|
{ "BUBBLING_PHASE", JS::Int32Value(BUBBLING_PHASE) },
|
|
|
|
{ nullptr, JS::UndefinedValue() }
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class MessageEvent : public Event
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass sClass;
|
|
|
|
static const JSClass sMainRuntimeClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
static const JSPropertySpec sProperties[];
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec sFunctions[];
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
protected:
|
2013-06-11 14:45:15 -07:00
|
|
|
JSAutoStructuredCloneBuffer mBuffer;
|
2011-08-15 20:40:38 -07:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
|
|
|
bool mMainRuntime;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
static bool
|
2013-09-11 05:49:05 -07:00
|
|
|
IsThisClass(const JSClass* aClass)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
return aClass == &sClass || aClass == &sMainRuntimeClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto,
|
|
|
|
bool aMainRuntime)
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, clasp, Construct, 0,
|
2013-10-28 07:04:47 -07:00
|
|
|
sProperties, sFunctions, nullptr, nullptr);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2013-06-05 07:04:23 -07:00
|
|
|
Create(JSContext* aCx, JS::Handle<JSObject*> aParent,
|
|
|
|
JSAutoStructuredCloneBuffer& aData,
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
|
|
|
bool aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSString*> type(aCx, JS_InternString(aCx, "message"));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!type) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_NewObject(aCx, clasp, nullptr, aParent));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!obj) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
JS::Rooted<JSObject*> ports(aCx, JS_NewArrayObject(aCx, 0, nullptr));
|
|
|
|
if (!ports) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2011-08-15 20:40:38 -07:00
|
|
|
MessageEvent* priv = new MessageEvent(aMainRuntime);
|
2012-02-05 12:07:23 -08:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
InitMessageEventCommon(aCx, obj, priv, type, false, false, nullptr,
|
|
|
|
nullptr, nullptr, ports, true);
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-06-11 14:45:15 -07:00
|
|
|
priv->mBuffer.swap(aData);
|
2011-08-15 20:40:38 -07:00
|
|
|
priv->mClonedObjects.SwapElements(aClonedObjects);
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, JS::Handle<JSObject*> aParent,
|
|
|
|
JS::Handle<JSString*> aType, bool aBubbles, bool aCancelable,
|
|
|
|
JS::Handle<JSString*> aData, JS::Handle<JSString*> aOrigin,
|
|
|
|
JS::Handle<JSObject*> aSource, JS::Handle<JSObject*> aMessagePort,
|
|
|
|
bool aIsTrusted)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx,
|
|
|
|
JS_NewObject(aCx, &sClass, nullptr, aParent));
|
|
|
|
if (!obj) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> ports(aCx);
|
|
|
|
if (aMessagePort) {
|
|
|
|
JS::Value port = OBJECT_TO_JSVAL(aMessagePort);
|
|
|
|
ports = JS_NewArrayObject(aCx, 1, &port);
|
|
|
|
} else {
|
|
|
|
ports = JS_NewArrayObject(aCx, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ports) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageEvent* priv = new MessageEvent(false);
|
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
|
|
|
|
InitMessageEventCommon(aCx, obj, priv, aType, aBubbles, aCancelable, aData,
|
|
|
|
aOrigin, aSource, ports, aIsTrusted);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
protected:
|
2011-08-15 20:40:38 -07:00
|
|
|
MessageEvent(bool aMainRuntime)
|
2013-06-11 14:45:15 -07:00
|
|
|
: mMainRuntime(aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::MessageEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MessageEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::MessageEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_data = Event::SLOT_COUNT,
|
|
|
|
SLOT_origin,
|
|
|
|
SLOT_source,
|
2013-06-05 07:04:23 -07:00
|
|
|
SLOT_ports,
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_data
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static MessageEvent*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* classPtr = JS_GetClass(aObj);
|
2012-02-03 10:00:08 -08:00
|
|
|
if (IsThisClass(classPtr)) {
|
2012-02-05 12:07:23 -08:00
|
|
|
return GetJSPrivateSafeish<MessageEvent>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
2011-07-17 12:09:13 -07:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 10:00:08 -08:00
|
|
|
classPtr->name);
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
static void
|
2011-07-17 12:09:13 -07:00
|
|
|
InitMessageEventCommon(JSContext* aCx, JSObject* aObj, Event* aEvent,
|
2013-08-08 15:53:04 -07:00
|
|
|
JSString* aType, bool aBubbles, bool aCancelable,
|
2011-07-17 12:09:13 -07:00
|
|
|
JSString* aData, JSString* aOrigin, JSObject* aSource,
|
2013-06-05 07:04:23 -07:00
|
|
|
JS::Handle<JSObject*> aMessagePorts, bool aIsTrusted)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
jsval emptyString = JS_GetEmptyStringValue(aCx);
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
Event::InitEventCommon(aObj, aEvent, aType, aBubbles, aCancelable,
|
|
|
|
aIsTrusted);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_data,
|
|
|
|
aData ? STRING_TO_JSVAL(aData) : emptyString);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_origin,
|
|
|
|
aOrigin ? STRING_TO_JSVAL(aOrigin) : emptyString);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_source, OBJECT_TO_JSVAL(aSource));
|
2013-06-05 07:04:23 -07:00
|
|
|
JS_SetReservedSlot(aObj, SLOT_ports, OBJECT_TO_JSVAL(aMessagePorts));
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_WRONG_CONSTRUCTOR, sClass.name);
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_ASSERT(IsThisClass(JS_GetClass(aObj)));
|
2012-02-05 12:07:23 -08:00
|
|
|
MessageEvent* priv = GetJSPrivateSafeish<MessageEvent>(aObj);
|
2013-06-11 14:45:15 -07:00
|
|
|
delete priv;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-08-08 15:55:23 -07:00
|
|
|
IsMessageEvent(JS::Handle<JS::Value> v)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-08-08 15:55:23 -07:00
|
|
|
if (!v.isObject())
|
|
|
|
return false;
|
|
|
|
JSObject* obj = &v.toObject();
|
|
|
|
return IsThisClass(JS_GetClass(obj)) &&
|
|
|
|
GetJSPrivateSafeish<MessageEvent>(obj) != nullptr;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
template<SLOT Slot>
|
|
|
|
static bool
|
|
|
|
GetPropertyImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aArgs.thisv().toObject());
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
const char* name = sProperties[Slot - SLOT_FIRST].name;
|
|
|
|
MessageEvent* event = GetInstancePrivate(aCx, obj, name);
|
|
|
|
MOZ_ASSERT(event);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
// Deserialize and save the data value if we can.
|
2013-08-08 15:55:23 -07:00
|
|
|
if (Slot == SLOT_data && event->mBuffer.data()) {
|
2011-07-17 12:09:13 -07:00
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
2013-06-11 14:45:15 -07:00
|
|
|
buffer.swap(event->mBuffer);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-08-15 20:40:38 -07:00
|
|
|
// Release reference to objects that were AddRef'd for
|
|
|
|
// cloning into worker when array goes out of scope.
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
clonedObjects.SwapElements(event->mClonedObjects);
|
|
|
|
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JS::Value> data(aCx);
|
2013-10-22 17:18:32 -07:00
|
|
|
if (!buffer.read(aCx, &data,
|
2012-02-05 12:07:23 -08:00
|
|
|
WorkerStructuredCloneCallbacks(event->mMainRuntime))) {
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-08 15:55:23 -07:00
|
|
|
JS_SetReservedSlot(obj, Slot, data);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
aArgs.rval().set(data);
|
2011-07-17 12:09:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
aArgs.rval().set(JS_GetReservedSlot(obj, Slot));
|
2012-02-05 12:07:23 -08:00
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
// This struct (versus just templating the method directly) is needed only for
|
|
|
|
// gcc 4.4 (and maybe 4.5 -- 4.6 is okay) being too braindead to allow
|
|
|
|
// GetProperty<Slot> and friends in the JSPropertySpec[] below.
|
|
|
|
template<SLOT Slot>
|
|
|
|
struct Property
|
|
|
|
{
|
|
|
|
static bool
|
|
|
|
Get(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
static_assert(SLOT_FIRST <= Slot && Slot < SLOT_COUNT, "bad slot");
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsMessageEvent, GetPropertyImpl<Slot> >(aCx, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
InitMessageEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_THIS_OBJECT(aCx, aVp));
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
MessageEvent* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSString*> type(aCx), data(aCx), origin(aCx);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool bubbles, cancelable;
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> source(aCx);
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "SbbSSo", type.address(),
|
|
|
|
&bubbles, &cancelable, data.address(),
|
|
|
|
origin.address(), source.address())) {
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
InitMessageEventCommon(aCx, obj, event, type, bubbles, cancelable,
|
2013-06-05 07:04:23 -07:00
|
|
|
data, origin, source, JS::NullPtr(), false);
|
2012-02-05 12:07:23 -08:00
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_MESSAGEEVENT_CLASS(_varname, _name) \
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass _varname = { \
|
2011-07-17 12:09:13 -07:00
|
|
|
_name, \
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT), \
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize \
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-07-08 09:55:14 -07:00
|
|
|
DECL_MESSAGEEVENT_CLASS(MessageEvent::sClass, "WorkerMessageEvent")
|
2011-07-17 12:09:13 -07:00
|
|
|
DECL_MESSAGEEVENT_CLASS(MessageEvent::sMainRuntimeClass, "WorkerMessageEvent")
|
|
|
|
|
|
|
|
#undef DECL_MESSAGEEVENT_CLASS
|
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
const JSPropertySpec MessageEvent::sProperties[] = {
|
2013-08-08 15:55:23 -07:00
|
|
|
JS_PSGS("data", Property<SLOT_data>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("origin", Property<SLOT_origin>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("source", Property<SLOT_source>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
2013-06-05 07:04:23 -07:00
|
|
|
JS_PSGS("ports", Property<SLOT_ports>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
2013-08-08 15:55:23 -07:00
|
|
|
JS_PS_END
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
const JSFunctionSpec MessageEvent::sFunctions[] = {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_FN("initMessageEvent", InitMessageEvent, 6, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
class ErrorEvent : public Event
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass sClass;
|
|
|
|
static const JSClass sMainRuntimeClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
static const JSPropertySpec sProperties[];
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec sFunctions[];
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
static bool
|
2013-09-11 05:49:05 -07:00
|
|
|
IsThisClass(const JSClass* aClass)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
return aClass == &sClass || aClass == &sMainRuntimeClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto,
|
|
|
|
bool aMainRuntime)
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, clasp, Construct, 0,
|
2013-10-28 07:04:47 -07:00
|
|
|
sProperties, sFunctions, nullptr, nullptr);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2013-05-09 00:27:40 -07:00
|
|
|
Create(JSContext* aCx, JS::Handle<JSObject*> aParent, JS::Handle<JSString*> aMessage,
|
|
|
|
JS::Handle<JSString*> aFilename, uint32_t aLineNumber, bool aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSString*> type(aCx, JS_InternString(aCx, "error"));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!type) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_NewObject(aCx, clasp, nullptr, aParent));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!obj) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorEvent* priv = new ErrorEvent();
|
2012-02-05 12:07:23 -08:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitErrorEventCommon(obj, priv, type, false, true, aMessage, aFilename,
|
|
|
|
aLineNumber, true);
|
2011-07-17 12:09:13 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ErrorEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::ErrorEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ErrorEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::ErrorEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_message = Event::SLOT_COUNT,
|
|
|
|
SLOT_filename,
|
|
|
|
SLOT_lineno,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_message
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static ErrorEvent*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* classPtr = JS_GetClass(aObj);
|
2012-02-03 10:00:08 -08:00
|
|
|
if (IsThisClass(classPtr)) {
|
2012-02-05 12:07:23 -08:00
|
|
|
return GetJSPrivateSafeish<ErrorEvent>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
2011-07-17 12:09:13 -07:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 10:00:08 -08:00
|
|
|
classPtr->name);
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
static void
|
|
|
|
InitErrorEventCommon(JSObject* aObj, Event* aEvent, JSString* aType,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool aBubbles, bool aCancelable,
|
2011-07-17 12:09:13 -07:00
|
|
|
JSString* aMessage, JSString* aFilename,
|
2012-10-11 16:38:04 -07:00
|
|
|
uint32_t aLineNumber, bool aIsTrusted)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
Event::InitEventCommon(aObj, aEvent, aType, aBubbles, aCancelable,
|
|
|
|
aIsTrusted);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_message, STRING_TO_JSVAL(aMessage));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_filename, STRING_TO_JSVAL(aFilename));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_lineno, INT_TO_JSVAL(aLineNumber));
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_WRONG_CONSTRUCTOR, sClass.name);
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_ASSERT(IsThisClass(JS_GetClass(aObj)));
|
2012-02-05 12:07:23 -08:00
|
|
|
delete GetJSPrivateSafeish<ErrorEvent>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-08-08 15:55:23 -07:00
|
|
|
IsErrorEvent(JS::Handle<JS::Value> v)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-08-08 15:55:23 -07:00
|
|
|
if (!v.isObject())
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
2013-08-08 15:55:23 -07:00
|
|
|
JSObject* obj = &v.toObject();
|
|
|
|
return IsThisClass(JS_GetClass(obj)) &&
|
|
|
|
GetJSPrivateSafeish<ErrorEvent>(obj) != nullptr;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
template<SLOT Slot>
|
|
|
|
static bool
|
|
|
|
GetPropertyImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
aArgs.rval().set(JS_GetReservedSlot(&aArgs.thisv().toObject(), Slot));
|
2012-02-05 12:07:23 -08:00
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
// This struct (versus just templating the method directly) is needed only for
|
|
|
|
// gcc 4.4 (and maybe 4.5 -- 4.6 is okay) being too braindead to allow
|
|
|
|
// GetProperty<Slot> and friends in the JSPropertySpec[] below.
|
|
|
|
template<SLOT Slot>
|
|
|
|
struct Property
|
|
|
|
{
|
|
|
|
static bool
|
|
|
|
Get(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
static_assert(SLOT_FIRST <= Slot && Slot < SLOT_COUNT, "bad slot");
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsErrorEvent, GetPropertyImpl<Slot> >(aCx, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
InitErrorEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_THIS_OBJECT(aCx, aVp));
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
ErrorEvent* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSString*> type(aCx), message(aCx), filename(aCx);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool bubbles, cancelable;
|
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-08 19:54:10 -08:00
|
|
|
uint32_t lineNumber;
|
2013-05-04 00:53:00 -07:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "SbbSSu", type.address(),
|
|
|
|
&bubbles, &cancelable, message.address(),
|
|
|
|
filename.address(), &lineNumber)) {
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
InitErrorEventCommon(obj, event, type, bubbles, cancelable, message,
|
|
|
|
filename, lineNumber, false);
|
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_ERROREVENT_CLASS(_varname, _name) \
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass _varname = { \
|
2011-07-17 12:09:13 -07:00
|
|
|
_name, \
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT), \
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize \
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-07-08 09:55:14 -07:00
|
|
|
DECL_ERROREVENT_CLASS(ErrorEvent::sClass, "WorkerErrorEvent")
|
2011-07-17 12:09:13 -07:00
|
|
|
DECL_ERROREVENT_CLASS(ErrorEvent::sMainRuntimeClass, "WorkerErrorEvent")
|
|
|
|
|
|
|
|
#undef DECL_ERROREVENT_CLASS
|
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
const JSPropertySpec ErrorEvent::sProperties[] = {
|
2013-08-08 15:55:23 -07:00
|
|
|
JS_PSGS("message", Property<SLOT_message>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("filename", Property<SLOT_filename>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("lineno", Property<SLOT_lineno>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PS_END
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
const JSFunctionSpec ErrorEvent::sFunctions[] = {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_FN("initErrorEvent", InitErrorEvent, 6, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProgressEvent : public Event
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass sClass;
|
2013-04-22 14:15:36 -07:00
|
|
|
static const JSPropertySpec sProperties[];
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
public:
|
2013-09-11 05:49:05 -07:00
|
|
|
static const JSClass*
|
2011-07-17 12:09:13 -07:00
|
|
|
Class()
|
|
|
|
{
|
|
|
|
return &sClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto)
|
|
|
|
{
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, &sClass, Construct, 0,
|
2013-10-28 07:04:47 -07:00
|
|
|
sProperties, nullptr, nullptr, nullptr);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2013-09-16 18:33:40 -07:00
|
|
|
Create(JSContext* aCx, JS::Handle<JSObject*> aParent, JS::Handle<JSString*> aType,
|
2012-02-24 14:19:52 -08:00
|
|
|
bool aLengthComputable, double aLoaded, double aTotal)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSString*> type(aCx, JS_InternJSString(aCx, aType));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!type) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx,
|
|
|
|
JS_NewObject(aCx, &sClass, nullptr, aParent));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!obj) {
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgressEvent* priv = new ProgressEvent();
|
2012-02-05 12:07:23 -08:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitProgressEventCommon(obj, priv, type, false, false, aLengthComputable,
|
|
|
|
aLoaded, aTotal, true);
|
2011-07-17 12:09:13 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ProgressEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::ProgressEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProgressEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::ProgressEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_lengthComputable = Event::SLOT_COUNT,
|
|
|
|
SLOT_loaded,
|
|
|
|
SLOT_total,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_lengthComputable
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static ProgressEvent*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* classPtr = JS_GetClass(aObj);
|
2012-02-03 10:00:08 -08:00
|
|
|
if (classPtr == &sClass) {
|
2012-02-05 12:07:23 -08:00
|
|
|
return GetJSPrivateSafeish<ProgressEvent>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
2011-07-17 12:09:13 -07:00
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 10:00:08 -08:00
|
|
|
classPtr->name);
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
static void
|
|
|
|
InitProgressEventCommon(JSObject* aObj, Event* aEvent, JSString* aType,
|
2013-08-08 15:53:04 -07:00
|
|
|
bool aBubbles, bool aCancelable,
|
|
|
|
bool aLengthComputable, double aLoaded,
|
2012-02-24 14:19:52 -08:00
|
|
|
double aTotal, bool aIsTrusted)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
Event::InitEventCommon(aObj, aEvent, aType, aBubbles, aCancelable,
|
|
|
|
aIsTrusted);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_lengthComputable,
|
|
|
|
aLengthComputable ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_loaded, DOUBLE_TO_JSVAL(aLoaded));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_total, DOUBLE_TO_JSVAL(aTotal));
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-10-28 07:04:47 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_WRONG_CONSTRUCTOR, sClass.name);
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == &sClass);
|
2012-02-05 12:07:23 -08:00
|
|
|
delete GetJSPrivateSafeish<ProgressEvent>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-08-08 15:55:23 -07:00
|
|
|
IsProgressEvent(JS::Handle<JS::Value> v)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-08-08 15:55:23 -07:00
|
|
|
if (!v.isObject())
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
2013-08-08 15:55:23 -07:00
|
|
|
JSObject* obj = &v.toObject();
|
|
|
|
return JS_GetClass(obj) == &sClass &&
|
|
|
|
GetJSPrivateSafeish<ProgressEvent>(obj) != nullptr;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-08 15:55:23 -07:00
|
|
|
template<SLOT Slot>
|
|
|
|
static bool
|
|
|
|
GetPropertyImpl(JSContext* aCx, JS::CallArgs aArgs)
|
|
|
|
{
|
|
|
|
aArgs.rval().set(JS_GetReservedSlot(&aArgs.thisv().toObject(), Slot));
|
2012-02-05 12:07:23 -08:00
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
2013-08-08 15:55:23 -07:00
|
|
|
|
|
|
|
// This struct (versus just templating the method directly) is needed only for
|
|
|
|
// gcc 4.4 (and maybe 4.5 -- 4.6 is okay) being too braindead to allow
|
|
|
|
// GetProperty<Slot> and friends in the JSPropertySpec[] below.
|
|
|
|
template<SLOT Slot>
|
|
|
|
struct Property
|
|
|
|
{
|
|
|
|
static bool
|
|
|
|
Get(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
static_assert(SLOT_FIRST <= Slot && Slot < SLOT_COUNT, "bad slot");
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(aArgc, aVp);
|
|
|
|
return JS::CallNonGenericMethod<IsProgressEvent, GetPropertyImpl<Slot> >(aCx, args);
|
|
|
|
}
|
|
|
|
};
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass ProgressEvent::sClass = {
|
2013-07-08 09:55:14 -07:00
|
|
|
"WorkerProgressEvent",
|
2011-07-17 12:09:13 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
const JSPropertySpec ProgressEvent::sProperties[] = {
|
2013-08-08 15:55:23 -07:00
|
|
|
JS_PSGS("lengthComputable", Property<SLOT_lengthComputable>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("loaded", Property<SLOT_loaded>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PSGS("total", Property<SLOT_total>::Get, GetterOnlyJSNative,
|
|
|
|
JSPROP_ENUMERATE),
|
|
|
|
JS_PS_END
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Event*
|
2012-02-05 12:07:23 -08:00
|
|
|
Event::GetPrivate(JSObject* aObj)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
if (aObj) {
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* classPtr = JS_GetClass(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
if (IsThisClass(classPtr) ||
|
|
|
|
MessageEvent::IsThisClass(classPtr) ||
|
|
|
|
ErrorEvent::IsThisClass(classPtr) ||
|
|
|
|
classPtr == ProgressEvent::Class()) {
|
2012-02-05 12:07:23 -08:00
|
|
|
return GetJSPrivateSafeish<Event>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-28 07:04:47 -07:00
|
|
|
return nullptr;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace events {
|
|
|
|
|
|
|
|
bool
|
2013-05-04 00:53:00 -07:00
|
|
|
InitClasses(JSContext* aCx, JS::Handle<JSObject*> aGlobal, bool aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JSObject*> eventProto(aCx, Event::InitClass(aCx, aGlobal, aMainRuntime));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!eventProto) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MessageEvent::InitClass(aCx, aGlobal, eventProto, aMainRuntime) &&
|
|
|
|
ErrorEvent::InitClass(aCx, aGlobal, eventProto, aMainRuntime) &&
|
|
|
|
ProgressEvent::InitClass(aCx, aGlobal, eventProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-05-09 00:27:40 -07:00
|
|
|
CreateGenericEvent(JSContext* aCx, JS::Handle<JSString*> aType, bool aBubbles,
|
2011-07-17 12:09:13 -07:00
|
|
|
bool aCancelable, bool aMainRuntime)
|
|
|
|
{
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 12:09:13 -07:00
|
|
|
return Event::Create(aCx, global, aType, aBubbles, aCancelable, aMainRuntime);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateMessageEvent(JSContext* aCx, JSAutoStructuredCloneBuffer& aData,
|
2011-08-15 20:40:38 -07:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
2011-07-17 12:09:13 -07:00
|
|
|
bool aMainRuntime)
|
|
|
|
{
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-08-15 20:40:38 -07:00
|
|
|
return MessageEvent::Create(aCx, global, aData, aClonedObjects, aMainRuntime);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-05-09 00:27:40 -07:00
|
|
|
CreateErrorEvent(JSContext* aCx, JS::Handle<JSString*> aMessage,
|
|
|
|
JS::Handle<JSString*> aFilename, uint32_t aLineNumber,
|
|
|
|
bool aMainRuntime)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 12:09:13 -07:00
|
|
|
return ErrorEvent::Create(aCx, global, aMessage, aFilename, aLineNumber,
|
|
|
|
aMainRuntime);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-09-16 18:33:40 -07:00
|
|
|
CreateProgressEvent(JSContext* aCx, JS::Handle<JSString*> aType, bool aLengthComputable,
|
2012-02-24 14:19:52 -08:00
|
|
|
double aLoaded, double aTotal)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-07-29 16:45:27 -07:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
2011-07-17 12:09:13 -07:00
|
|
|
return ProgressEvent::Create(aCx, global, aType, aLengthComputable, aLoaded,
|
|
|
|
aTotal);
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
JSObject*
|
|
|
|
CreateConnectEvent(JSContext* aCx, JS::Handle<JSObject*> aMessagePort)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> type(aCx, JS_InternString(aCx, "connect"));
|
|
|
|
if (!type) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> emptyStr(aCx, JS_GetEmptyString(JS_GetRuntime(aCx)));
|
|
|
|
if (!emptyStr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MessageEvent::Create(aCx, global, type, false, false, emptyStr,
|
|
|
|
emptyStr, JS::NullPtr(), aMessagePort, true);
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
bool
|
2012-02-05 12:07:23 -08:00
|
|
|
IsSupportedEventClass(JSObject* aEvent)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
return Event::IsSupportedClass(aEvent);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
void
|
|
|
|
SetEventTarget(JSObject* aEvent, JSObject* aTarget)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
Event::SetTarget(aEvent, aTarget);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-02-05 12:07:23 -08:00
|
|
|
EventWasCanceled(JSObject* aEvent)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
return Event::WasCanceled(aEvent);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2011-11-05 14:41:16 -07:00
|
|
|
bool
|
2012-02-05 12:07:23 -08:00
|
|
|
EventImmediatePropagationStopped(JSObject* aEvent)
|
2011-11-05 14:41:16 -07:00
|
|
|
{
|
2012-02-05 12:07:23 -08:00
|
|
|
return Event::ImmediatePropagationStopped(aEvent);
|
2011-11-05 14:41:16 -07:00
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
bool
|
2013-05-09 00:27:40 -07:00
|
|
|
DispatchEventToTarget(JSContext* aCx, JS::Handle<JSObject*> aTarget,
|
|
|
|
JS::Handle<JSObject*> aEvent, bool* aPreventDefaultCalled)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2011-10-14 08:58:37 -07:00
|
|
|
static const char kFunctionName[] = "dispatchEvent";
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProperty;
|
2011-10-14 08:58:37 -07:00
|
|
|
if (!JS_HasProperty(aCx, aTarget, kFunctionName, &hasProperty)) {
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool preventDefaultCalled = false;
|
2011-10-14 08:58:37 -07:00
|
|
|
if (hasProperty) {
|
|
|
|
jsval argv[] = { OBJECT_TO_JSVAL(aEvent) };
|
2013-05-04 00:53:00 -07:00
|
|
|
JS::Rooted<JS::Value> rval(aCx, JS::UndefinedValue());
|
2011-10-21 03:16:47 -07:00
|
|
|
if (!JS_CallFunctionName(aCx, aTarget, kFunctionName, ArrayLength(argv),
|
2013-05-04 00:53:00 -07:00
|
|
|
argv, rval.address()) ||
|
2011-10-14 08:58:37 -07:00
|
|
|
!JS_ValueToBoolean(aCx, rval, &preventDefaultCalled)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*aPreventDefaultCalled = !!preventDefaultCalled;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace events
|
|
|
|
|
2013-10-22 17:18:32 -07:00
|
|
|
END_WORKERS_NAMESPACE
|