2009-11-06 23:43:39 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sw=4 et tw=80:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Ben Newman <b{enjam,newma}n@mozilla.com> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "mozilla/jsipc/ObjectWrapperParent.h"
|
|
|
|
#include "mozilla/jsipc/ContextWrapperParent.h"
|
|
|
|
#include "mozilla/jsipc/CPOWTypes.h"
|
2010-07-19 22:33:33 +04:00
|
|
|
#include "mozilla/unused.h"
|
2010-12-03 11:24:17 +03:00
|
|
|
#include "nsJSUtils.h"
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsutil.h"
|
|
|
|
|
|
|
|
using namespace mozilla::jsipc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Only need one reserved slot because the ObjectWrapperParent* is
|
|
|
|
// stored in the private slot.
|
|
|
|
static const uintN sFlagsSlot = 0;
|
|
|
|
static const uintN sNumSlots = 1;
|
|
|
|
static const uintN CPOW_FLAG_RESOLVING = 1 << 0;
|
|
|
|
|
|
|
|
class AutoResolveFlag
|
|
|
|
{
|
|
|
|
JSContext* mContext;
|
|
|
|
JSObject* mObj;
|
|
|
|
uintN mOldFlags;
|
2010-11-24 10:33:03 +03:00
|
|
|
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
static uintN GetFlags(JSContext* cx, JSObject* obj) {
|
|
|
|
jsval v;
|
|
|
|
#ifdef DEBUG
|
|
|
|
JSBool ok =
|
|
|
|
#endif
|
|
|
|
JS_GetReservedSlot(cx, obj, sFlagsSlot, &v);
|
|
|
|
NS_ASSERTION(ok, "Failed to get CPOW flags");
|
|
|
|
return JSVAL_TO_INT(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uintN SetFlags(JSContext* cx, JSObject* obj, uintN flags) {
|
|
|
|
uintN oldFlags = GetFlags(cx, obj);
|
|
|
|
if (oldFlags != flags)
|
|
|
|
JS_SetReservedSlot(cx, obj, sFlagsSlot, INT_TO_JSVAL(flags));
|
|
|
|
return oldFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
AutoResolveFlag(JSContext* cx,
|
|
|
|
JSObject* obj
|
|
|
|
JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: mContext(cx)
|
|
|
|
, mObj(obj)
|
|
|
|
, mOldFlags(SetFlags(cx, obj,
|
|
|
|
GetFlags(cx, obj) | CPOW_FLAG_RESOLVING))
|
|
|
|
{
|
|
|
|
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoResolveFlag() {
|
|
|
|
SetFlags(mContext, mObj, mOldFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool IsSet(JSContext* cx, JSObject* obj) {
|
|
|
|
return GetFlags(cx, obj) & CPOW_FLAG_RESOLVING;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-01-22 23:52:57 +03:00
|
|
|
class StatusMemberOwner
|
|
|
|
{
|
|
|
|
OperationStatus mStatus;
|
|
|
|
public:
|
|
|
|
StatusMemberOwner() : mStatus(JS_FALSE) {}
|
|
|
|
OperationStatus* StatusPtr() {
|
|
|
|
return &mStatus;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoCheckOperationBase<StatusMemberOwner> ACOBase;
|
|
|
|
|
|
|
|
class AutoCheckOperation : public ACOBase
|
|
|
|
{
|
2010-11-24 10:33:03 +03:00
|
|
|
JS_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2010-01-22 23:52:57 +03:00
|
|
|
public:
|
|
|
|
AutoCheckOperation(JSContext* cx,
|
|
|
|
ObjectWrapperParent* owp
|
|
|
|
JS_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
: ACOBase(cx, owp)
|
|
|
|
{
|
|
|
|
JS_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectWrapperParent::CheckOperation(JSContext* cx,
|
|
|
|
OperationStatus* status)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(status->type() != OperationStatus::T__None,
|
|
|
|
"Checking an uninitialized operation.");
|
|
|
|
|
|
|
|
switch (status->type()) {
|
|
|
|
case OperationStatus::TJSVariant:
|
|
|
|
{
|
|
|
|
jsval thrown;
|
|
|
|
if (jsval_from_JSVariant(cx, status->get_JSVariant(), &thrown))
|
|
|
|
JS_SetPendingException(cx, thrown);
|
|
|
|
*status = JS_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OperationStatus::TJSBool:
|
|
|
|
if (!status->get_JSBool() && !JS_IsExceptionPending(cx)) {
|
|
|
|
NS_WARNING("CPOW operation failed without setting an exception.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Invalid or uninitialized OperationStatus type.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename RType>
|
|
|
|
static RType
|
|
|
|
with_error(JSContext* cx,
|
|
|
|
RType rval,
|
|
|
|
const char* error = NULL)
|
|
|
|
{
|
|
|
|
if (!JS_IsExceptionPending(cx))
|
|
|
|
JS_ReportError(cx, error ? error : "Unspecified CPOW error");
|
|
|
|
return rval;
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
2010-06-12 20:29:04 +04:00
|
|
|
const js::Class ObjectWrapperParent::sCPOW_JSClass = {
|
|
|
|
"CrossProcessObjectWrapper",
|
|
|
|
JSCLASS_NEW_RESOLVE | JSCLASS_NEW_ENUMERATE |
|
2009-11-06 23:43:39 +03:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(sNumSlots),
|
2010-10-02 04:53:08 +04:00
|
|
|
JS_VALUEIFY(js::PropertyOp, ObjectWrapperParent::CPOW_AddProperty),
|
|
|
|
JS_VALUEIFY(js::PropertyOp, ObjectWrapperParent::CPOW_DelProperty),
|
|
|
|
JS_VALUEIFY(js::PropertyOp, ObjectWrapperParent::CPOW_GetProperty),
|
2011-02-09 22:31:40 +03:00
|
|
|
JS_VALUEIFY(js::StrictPropertyOp, ObjectWrapperParent::CPOW_SetProperty),
|
2009-11-06 23:43:39 +03:00
|
|
|
(JSEnumerateOp) ObjectWrapperParent::CPOW_NewEnumerate,
|
2010-06-12 20:29:04 +04:00
|
|
|
(JSResolveOp) ObjectWrapperParent::CPOW_NewResolve,
|
2010-10-02 04:53:08 +04:00
|
|
|
JS_VALUEIFY(js::ConvertOp, ObjectWrapperParent::CPOW_Convert),
|
2009-11-06 23:43:39 +03:00
|
|
|
ObjectWrapperParent::CPOW_Finalize,
|
2010-06-12 20:29:04 +04:00
|
|
|
nsnull, // reserved1
|
2009-11-06 23:43:39 +03:00
|
|
|
nsnull, // checkAccess
|
2010-10-02 04:53:08 +04:00
|
|
|
JS_VALUEIFY(js::CallOp, ObjectWrapperParent::CPOW_Call),
|
|
|
|
JS_VALUEIFY(js::CallOp, ObjectWrapperParent::CPOW_Construct),
|
2009-11-06 23:43:39 +03:00
|
|
|
nsnull, // xdrObject
|
2010-10-02 04:53:08 +04:00
|
|
|
JS_VALUEIFY(js::HasInstanceOp, ObjectWrapperParent::CPOW_HasInstance),
|
2009-11-06 23:43:39 +03:00
|
|
|
nsnull, // mark
|
2010-06-12 20:29:04 +04:00
|
|
|
{
|
2010-10-02 04:53:08 +04:00
|
|
|
JS_VALUEIFY(js::EqualityOp, ObjectWrapperParent::CPOW_Equality),
|
2010-06-12 20:29:04 +04:00
|
|
|
nsnull, // outerObject
|
|
|
|
nsnull, // innerObject
|
|
|
|
nsnull, // iteratorObject
|
|
|
|
nsnull, // wrappedObject
|
|
|
|
}
|
2009-11-06 23:43:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectWrapperParent::ActorDestroy(ActorDestroyReason)
|
|
|
|
{
|
2010-03-31 08:40:58 +04:00
|
|
|
if (mObj) {
|
2009-11-06 23:43:39 +03:00
|
|
|
mObj->setPrivate(NULL);
|
2010-03-31 08:40:58 +04:00
|
|
|
mObj = NULL;
|
|
|
|
}
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
2010-01-21 05:24:14 +03:00
|
|
|
|
|
|
|
ContextWrapperParent*
|
|
|
|
ObjectWrapperParent::Manager()
|
|
|
|
{
|
|
|
|
PContextWrapperParent* pcwp = PObjectWrapperParent::Manager();
|
|
|
|
return static_cast<ContextWrapperParent*>(pcwp);
|
|
|
|
}
|
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
JSObject*
|
|
|
|
ObjectWrapperParent::GetJSObject(JSContext* cx) const
|
|
|
|
{
|
2010-06-12 20:29:04 +04:00
|
|
|
js::Class *clasp = const_cast<js::Class *>(&ObjectWrapperParent::sCPOW_JSClass);
|
|
|
|
if (!mObj && (mObj = JS_NewObject(cx, js::Jsvalify(clasp), NULL, NULL))) {
|
2009-11-06 23:43:39 +03:00
|
|
|
JS_SetPrivate(cx, mObj, (void*)this);
|
|
|
|
JS_SetReservedSlot(cx, mObj, sFlagsSlot, JSVAL_ZERO);
|
|
|
|
}
|
|
|
|
return mObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ObjectWrapperParent*
|
|
|
|
Unwrap(JSContext* cx, JSObject* obj)
|
|
|
|
{
|
2010-06-12 20:29:04 +04:00
|
|
|
while (obj->getClass() != &ObjectWrapperParent::sCPOW_JSClass)
|
2010-04-07 14:59:04 +04:00
|
|
|
if (!(obj = obj->getProto()))
|
2009-11-06 23:43:39 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ObjectWrapperParent* self =
|
|
|
|
static_cast<ObjectWrapperParent*>(JS_GetPrivate(cx, obj));
|
|
|
|
|
|
|
|
NS_ASSERTION(!self || self->GetJSObject(cx) == obj,
|
|
|
|
"Wrapper and wrapped object disagree?");
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool
|
|
|
|
ObjectWrapperParent::jsval_to_JSVariant(JSContext* cx, jsval from,
|
|
|
|
JSVariant* to)
|
|
|
|
{
|
|
|
|
switch (JS_TypeOfValue(cx, from)) {
|
|
|
|
case JSTYPE_VOID:
|
|
|
|
*to = void_t();
|
|
|
|
return true;
|
|
|
|
case JSTYPE_NULL:
|
|
|
|
if (from != JSVAL_NULL)
|
|
|
|
return false;
|
|
|
|
// fall through
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
// CPOWs can fool JS_TypeOfValue into returning JSTYPE_FUNCTION
|
|
|
|
// because they have a call hook, but CPOWs are really objects, so
|
|
|
|
// fall through to the JSTYPE_OBJECT case:
|
|
|
|
case JSTYPE_OBJECT:
|
|
|
|
{
|
|
|
|
PObjectWrapperParent* powp;
|
|
|
|
if (!JSObject_to_PObjectWrapperParent(cx, JSVAL_TO_OBJECT(from), &powp))
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, false, "Cannot pass parent-created object to child");
|
2009-11-06 23:43:39 +03:00
|
|
|
*to = powp;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case JSTYPE_STRING:
|
2010-12-03 11:24:17 +03:00
|
|
|
{
|
|
|
|
nsDependentJSString depStr;
|
|
|
|
if (!depStr.init(cx, from))
|
|
|
|
return false;
|
|
|
|
*to = depStr;
|
|
|
|
}
|
2009-11-06 23:43:39 +03:00
|
|
|
return true;
|
|
|
|
case JSTYPE_NUMBER:
|
|
|
|
if (JSVAL_IS_INT(from))
|
|
|
|
*to = JSVAL_TO_INT(from);
|
|
|
|
else if (JSVAL_IS_DOUBLE(from))
|
2010-07-15 10:19:36 +04:00
|
|
|
*to = JSVAL_TO_DOUBLE(from);
|
2009-11-06 23:43:39 +03:00
|
|
|
else return false;
|
|
|
|
return true;
|
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
*to = !!JSVAL_TO_BOOLEAN(from);
|
|
|
|
return true;
|
|
|
|
case JSTYPE_XML:
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, false, "CPOWs currently cannot handle JSTYPE_XML");
|
2009-11-06 23:43:39 +03:00
|
|
|
default:
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, false, "Bad jsval type");
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool
|
|
|
|
ObjectWrapperParent::jsval_from_JSVariant(JSContext* cx, const JSVariant& from,
|
|
|
|
jsval* to)
|
|
|
|
{
|
|
|
|
switch (from.type()) {
|
|
|
|
case JSVariant::Tvoid_t:
|
|
|
|
*to = JSVAL_VOID;
|
|
|
|
return true;
|
|
|
|
case JSVariant::TPObjectWrapperParent:
|
|
|
|
return jsval_from_PObjectWrapperParent(cx, from.get_PObjectWrapperParent(), to);
|
|
|
|
case JSVariant::TnsString:
|
|
|
|
{
|
|
|
|
JSString* str = JS_NewUCStringCopyZ(cx, from.get_nsString().BeginReading());
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
*to = STRING_TO_JSVAL(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case JSVariant::Tint:
|
|
|
|
*to = INT_TO_JSVAL(from.get_int());
|
|
|
|
return true;
|
|
|
|
case JSVariant::Tdouble:
|
2010-07-15 10:19:36 +04:00
|
|
|
return !!JS_NewNumberValue(cx, from.get_double(), to);
|
2009-11-06 23:43:39 +03:00
|
|
|
case JSVariant::Tbool:
|
|
|
|
*to = BOOLEAN_TO_JSVAL(from.get_bool());
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool
|
|
|
|
ObjectWrapperParent::
|
|
|
|
JSObject_to_PObjectWrapperParent(JSContext* cx, JSObject* from,
|
|
|
|
PObjectWrapperParent** to)
|
|
|
|
{
|
|
|
|
if (!from) {
|
|
|
|
*to = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ObjectWrapperParent* owp = Unwrap(cx, from);
|
|
|
|
if (!owp)
|
|
|
|
return false;
|
|
|
|
*to = owp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool
|
|
|
|
ObjectWrapperParent::
|
|
|
|
JSObject_from_PObjectWrapperParent(JSContext* cx,
|
|
|
|
const PObjectWrapperParent* from,
|
|
|
|
JSObject** to)
|
|
|
|
{
|
|
|
|
const ObjectWrapperParent* owp =
|
|
|
|
static_cast<const ObjectWrapperParent*>(from);
|
|
|
|
*to = owp
|
|
|
|
? owp->GetJSObject(cx)
|
|
|
|
: JSVAL_TO_OBJECT(JSVAL_NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ bool
|
|
|
|
ObjectWrapperParent::
|
|
|
|
jsval_from_PObjectWrapperParent(JSContext* cx,
|
|
|
|
const PObjectWrapperParent* from,
|
|
|
|
jsval* to)
|
|
|
|
{
|
|
|
|
JSObject* obj;
|
|
|
|
if (!JSObject_from_PObjectWrapperParent(cx, from, &obj))
|
|
|
|
return false;
|
|
|
|
*to = OBJECT_TO_JSVAL(obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
jsid_from_int(JSContext* cx, int from, jsid* to)
|
|
|
|
{
|
|
|
|
jsval v = INT_TO_JSVAL(from);
|
|
|
|
return JS_ValueToId(cx, v, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
jsid_from_nsString(JSContext* cx, const nsString& from, jsid* to)
|
|
|
|
{
|
|
|
|
JSString* str = JS_NewUCStringCopyZ(cx, from.BeginReading());
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
return JS_ValueToId(cx, STRING_TO_JSVAL(str), to);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2010-07-15 10:19:36 +04:00
|
|
|
jsval_to_nsString(JSContext* cx, jsid from, nsString* to)
|
2009-11-06 23:43:39 +03:00
|
|
|
{
|
|
|
|
JSString* str;
|
2010-12-03 11:24:17 +03:00
|
|
|
const jschar* chars;
|
2010-07-15 10:19:36 +04:00
|
|
|
jsval idval;
|
|
|
|
if (JS_IdToValue(cx, from, &idval) &&
|
2010-12-03 11:24:17 +03:00
|
|
|
(str = JS_ValueToString(cx, idval)) &&
|
|
|
|
(chars = JS_GetStringCharsZ(cx, str))) {
|
|
|
|
*to = chars;
|
2009-11-06 23:43:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent::CPOW_AddProperty(JSContext *cx, JSObject *obj, jsid id,
|
2009-11-06 23:43:39 +03:00
|
|
|
jsval *vp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_AddProperty (%s)...",
|
|
|
|
JSVAL_TO_CSTR(cx, id)));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_AddProperty");
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
if (AutoResolveFlag::IsSet(cx, obj))
|
|
|
|
return JS_TRUE;
|
2010-01-22 23:52:57 +03:00
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, self);
|
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
nsString in_id;
|
|
|
|
|
|
|
|
if (!jsval_to_nsString(cx, id, &in_id))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (self->Manager()->RequestRunToCompletion() &&
|
|
|
|
self->CallAddProperty(in_id,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr()) &&
|
|
|
|
aco.Ok());
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent::CPOW_GetProperty(JSContext *cx, JSObject *obj, jsid id,
|
2009-11-06 23:43:39 +03:00
|
|
|
jsval *vp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_GetProperty (%s)...",
|
|
|
|
JSVAL_TO_CSTR(cx, id)));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_GetProperty");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, self);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
nsString in_id;
|
|
|
|
|
|
|
|
if (!jsval_to_nsString(cx, id, &in_id))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSVariant out_v;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (self->Manager()->RequestRunToCompletion() &&
|
|
|
|
self->CallGetProperty(in_id,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), &out_v) &&
|
|
|
|
aco.Ok() &&
|
2009-11-06 23:43:39 +03:00
|
|
|
self->jsval_from_JSVariant(cx, out_v, vp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2011-02-09 22:31:40 +03:00
|
|
|
ObjectWrapperParent::CPOW_SetProperty(JSContext *cx, JSObject *obj, jsid id,
|
|
|
|
JSBool strict, jsval *vp)
|
2009-11-06 23:43:39 +03:00
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_SetProperty (%s)...",
|
|
|
|
JSVAL_TO_CSTR(cx, id)));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_SetProperty");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, self);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
nsString in_id;
|
|
|
|
JSVariant in_v;
|
|
|
|
|
|
|
|
if (!jsval_to_nsString(cx, id, &in_id) ||
|
|
|
|
!self->jsval_to_JSVariant(cx, *vp, &in_v))
|
|
|
|
return JS_FALSE;
|
2010-01-22 23:52:57 +03:00
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
JSVariant out_v;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (self->Manager()->RequestRunToCompletion() &&
|
|
|
|
self->CallSetProperty(in_id, in_v,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), &out_v) &&
|
|
|
|
aco.Ok() &&
|
2009-11-06 23:43:39 +03:00
|
|
|
self->jsval_from_JSVariant(cx, out_v, vp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent::CPOW_DelProperty(JSContext *cx, JSObject *obj, jsid id,
|
2009-11-06 23:43:39 +03:00
|
|
|
jsval *vp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_DelProperty (%s)...",
|
|
|
|
JSVAL_TO_CSTR(cx, id)));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_DelProperty");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, self);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
nsString in_id;
|
|
|
|
|
|
|
|
if (!jsval_to_nsString(cx, id, &in_id))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSVariant out_v;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (self->Manager()->RequestRunToCompletion() &&
|
|
|
|
self->CallDelProperty(in_id,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), &out_v) &&
|
|
|
|
aco.Ok() &&
|
2009-11-06 23:43:39 +03:00
|
|
|
jsval_from_JSVariant(cx, out_v, vp));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
ObjectWrapperParent::NewEnumerateInit(JSContext* cx, jsval* statep, jsid* idp)
|
|
|
|
{
|
2010-01-22 23:52:57 +03:00
|
|
|
AutoCheckOperation aco(cx, this);
|
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
JSVariant out_state;
|
|
|
|
int out_id;
|
|
|
|
|
2010-01-22 23:52:57 +03:00
|
|
|
return (CallNewEnumerateInit(aco.StatusPtr(), &out_state, &out_id) &&
|
|
|
|
aco.Ok() &&
|
2009-11-06 23:43:39 +03:00
|
|
|
jsval_from_JSVariant(cx, out_state, statep) &&
|
|
|
|
(!idp || jsid_from_int(cx, out_id, idp)));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
ObjectWrapperParent::NewEnumerateNext(JSContext* cx, jsval* statep, jsid* idp)
|
|
|
|
{
|
2010-01-22 23:52:57 +03:00
|
|
|
AutoCheckOperation aco(cx, this);
|
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
JSVariant in_state;
|
2010-01-22 23:52:57 +03:00
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
if (!jsval_to_JSVariant(cx, *statep, &in_state))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
JSVariant out_state;
|
|
|
|
nsString out_id;
|
|
|
|
|
|
|
|
if (CallNewEnumerateNext(in_state,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), &out_state, &out_id) &&
|
|
|
|
aco.Ok() &&
|
2009-11-06 23:43:39 +03:00
|
|
|
jsval_from_JSVariant(cx, out_state, statep) &&
|
|
|
|
jsid_from_nsString(cx, out_id, idp))
|
|
|
|
{
|
|
|
|
JSObject* obj = GetJSObject(cx);
|
|
|
|
AutoResolveFlag arf(cx, obj);
|
|
|
|
return JS_DefinePropertyById(cx, obj, *idp, JSVAL_VOID, NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE);
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
ObjectWrapperParent::NewEnumerateDestroy(JSContext* cx, jsval state)
|
|
|
|
{
|
2010-01-22 23:52:57 +03:00
|
|
|
AutoCheckOperation aco(cx, this);
|
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
JSVariant in_state;
|
2010-01-22 23:52:57 +03:00
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
if (!jsval_to_JSVariant(cx, state, &in_state))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
return SendNewEnumerateDestroy(in_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
|
|
|
ObjectWrapperParent::CPOW_NewEnumerate(JSContext *cx, JSObject *obj,
|
|
|
|
JSIterateOp enum_op, jsval *statep,
|
|
|
|
jsid *idp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_NewEnumerate..."));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_NewEnumerate");
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
switch (enum_op) {
|
|
|
|
case JSENUMERATE_INIT:
|
2010-07-08 02:11:17 +04:00
|
|
|
case JSENUMERATE_INIT_ALL:
|
2010-01-21 05:24:14 +03:00
|
|
|
self->Manager()->RequestRunToCompletion();
|
2009-11-06 23:43:39 +03:00
|
|
|
return self->NewEnumerateInit(cx, statep, idp);
|
|
|
|
case JSENUMERATE_NEXT:
|
|
|
|
return self->NewEnumerateNext(cx, statep, idp);
|
|
|
|
case JSENUMERATE_DESTROY:
|
|
|
|
return self->NewEnumerateDestroy(cx, *statep);
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:52:57 +03:00
|
|
|
NS_NOTREACHED("Unknown enum_op value in CPOW_NewEnumerate");
|
2009-11-06 23:43:39 +03:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent::CPOW_NewResolve(JSContext *cx, JSObject *obj, jsid id,
|
2009-11-06 23:43:39 +03:00
|
|
|
uintN flags, JSObject **objp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_NewResolve (%s)...",
|
|
|
|
JSVAL_TO_CSTR(cx, id)));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_NewResolve");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, self);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
nsString in_id;
|
|
|
|
|
|
|
|
if (!jsval_to_nsString(cx, id, &in_id))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
PObjectWrapperParent* out_pobj;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
if (!self->Manager()->RequestRunToCompletion() ||
|
|
|
|
!self->CallNewResolve(in_id, flags,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), &out_pobj) ||
|
|
|
|
!aco.Ok() ||
|
2009-11-06 23:43:39 +03:00
|
|
|
!JSObject_from_PObjectWrapperParent(cx, out_pobj, objp))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
if (*objp) {
|
2009-11-06 23:43:39 +03:00
|
|
|
AutoResolveFlag arf(cx, *objp);
|
2010-07-15 10:19:36 +04:00
|
|
|
JS_DefinePropertyById(cx, *objp, id, JSVAL_VOID, NULL, NULL,
|
2009-11-06 23:43:39 +03:00
|
|
|
JSPROP_ENUMERATE);
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
|
|
|
ObjectWrapperParent::CPOW_Convert(JSContext *cx, JSObject *obj, JSType type,
|
|
|
|
jsval *vp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_Convert (to %s)...",
|
|
|
|
JS_GetTypeName(cx, type)));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_Convert");
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ void
|
|
|
|
ObjectWrapperParent::CPOW_Finalize(JSContext* cx, JSObject* obj)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_Finalize..."));
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
2010-03-31 08:40:58 +04:00
|
|
|
if (self) {
|
|
|
|
self->mObj = NULL;
|
2010-07-19 22:33:33 +04:00
|
|
|
unused << ObjectWrapperParent::Send__delete__(self);
|
2010-03-31 08:40:58 +04:00
|
|
|
}
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-08-16 23:35:04 +04:00
|
|
|
ObjectWrapperParent::CPOW_Call(JSContext* cx, uintN argc, jsval* vp)
|
2009-11-06 23:43:39 +03:00
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_Call..."));
|
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
JSObject* thisobj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!thisobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
ObjectWrapperParent* function =
|
2010-08-16 23:35:04 +04:00
|
|
|
Unwrap(cx, JSVAL_TO_OBJECT(JS_CALLEE(cx, vp)));
|
2009-11-06 23:43:39 +03:00
|
|
|
if (!function)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Could not unwrap CPOW function");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, function);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
ObjectWrapperParent* receiver = Unwrap(cx, thisobj);
|
2009-11-06 23:43:39 +03:00
|
|
|
if (!receiver) {
|
|
|
|
// Substitute child global for parent global object.
|
|
|
|
// TODO First make sure we're really replacing the global object?
|
|
|
|
ContextWrapperParent* manager =
|
|
|
|
static_cast<ContextWrapperParent*>(function->Manager());
|
|
|
|
receiver = manager->GetGlobalObjectWrapper();
|
|
|
|
}
|
|
|
|
|
2010-11-09 05:49:00 +03:00
|
|
|
InfallibleTArray<JSVariant> in_argv(argc);
|
2010-08-16 23:35:04 +04:00
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
2009-11-06 23:43:39 +03:00
|
|
|
for (uintN i = 0; i < argc; i++)
|
|
|
|
if (!jsval_to_JSVariant(cx, argv[i], in_argv.AppendElement()))
|
|
|
|
return JS_FALSE;
|
2010-01-22 23:52:57 +03:00
|
|
|
|
2009-11-06 23:43:39 +03:00
|
|
|
JSVariant out_rval;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (function->Manager()->RequestRunToCompletion() &&
|
|
|
|
function->CallCall(receiver, in_argv,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), &out_rval) &&
|
|
|
|
aco.Ok() &&
|
2010-08-16 23:35:04 +04:00
|
|
|
jsval_from_JSVariant(cx, out_rval, vp));
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-08-16 23:35:04 +04:00
|
|
|
ObjectWrapperParent::CPOW_Construct(JSContext* cx, uintN argc, jsval* vp)
|
2009-11-06 23:43:39 +03:00
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_Construct..."));
|
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
ObjectWrapperParent* constructor = Unwrap(cx, JSVAL_TO_OBJECT(JS_CALLEE(cx, vp)));
|
2009-11-06 23:43:39 +03:00
|
|
|
if (!constructor)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Could not unwrap CPOW constructor function");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, constructor);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
2010-11-09 05:49:00 +03:00
|
|
|
InfallibleTArray<JSVariant> in_argv(argc);
|
2010-08-16 23:35:04 +04:00
|
|
|
jsval* argv = JS_ARGV(cx, vp);
|
2009-11-06 23:43:39 +03:00
|
|
|
for (uintN i = 0; i < argc; i++)
|
|
|
|
if (!jsval_to_JSVariant(cx, argv[i], in_argv.AppendElement()))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
PObjectWrapperParent* out_powp;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (constructor->Manager()->RequestRunToCompletion() &&
|
2010-08-16 23:35:04 +04:00
|
|
|
constructor->CallConstruct(in_argv, aco.StatusPtr(), &out_powp) &&
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.Ok() &&
|
2010-08-16 23:35:04 +04:00
|
|
|
jsval_from_PObjectWrapperParent(cx, out_powp, vp));
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent::CPOW_HasInstance(JSContext *cx, JSObject *obj, const jsval *v,
|
2009-11-06 23:43:39 +03:00
|
|
|
JSBool *bp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_HasInstance..."));
|
|
|
|
|
|
|
|
*bp = JS_FALSE;
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_HasInstance");
|
|
|
|
|
|
|
|
AutoCheckOperation aco(cx, self);
|
2009-11-06 23:43:39 +03:00
|
|
|
|
|
|
|
JSVariant in_v;
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
if (!jsval_to_JSVariant(cx, *v, &in_v))
|
2009-11-06 23:43:39 +03:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-01-21 05:24:14 +03:00
|
|
|
return (self->Manager()->RequestRunToCompletion() &&
|
|
|
|
self->CallHasInstance(in_v,
|
2010-01-22 23:52:57 +03:00
|
|
|
aco.StatusPtr(), bp) &&
|
|
|
|
aco.Ok());
|
2009-11-06 23:43:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ JSBool
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent::CPOW_Equality(JSContext *cx, JSObject *obj, const jsval *v,
|
2009-11-06 23:43:39 +03:00
|
|
|
JSBool *bp)
|
|
|
|
{
|
|
|
|
CPOW_LOG(("Calling CPOW_Equality..."));
|
|
|
|
|
|
|
|
*bp = JS_FALSE;
|
|
|
|
|
|
|
|
ObjectWrapperParent* self = Unwrap(cx, obj);
|
|
|
|
if (!self)
|
2010-01-22 23:52:57 +03:00
|
|
|
return with_error(cx, JS_FALSE, "Unwrapping failed in CPOW_Equality");
|
2009-11-06 23:43:39 +03:00
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
if (JSVAL_IS_PRIMITIVE(*v))
|
2009-11-06 23:43:39 +03:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2010-07-15 10:19:36 +04:00
|
|
|
ObjectWrapperParent* other = Unwrap(cx, JSVAL_TO_OBJECT(*v));
|
2009-11-06 23:43:39 +03:00
|
|
|
if (!other)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
*bp = (self == other);
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|