gecko/js/ipc/ObjectWrapperChild.cpp
Bobby Holley 1b2f22fe61 Bug 868130 - Remove the lion's share of JSAutoRequests in gecko. r=gabor
There are still a handful that either are used with other runtimes, or that
happen very early/late in cx the lifetime of various things where it doesn't
necessarily make sense to have a cx on the stack. This should definitely ensure
that we're not doing double-duty with the nsCxPusher change, though.
2013-05-22 10:05:28 -06:00

622 lines
18 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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/. */
#include "mozilla/GuardObjects.h"
#include "base/basictypes.h"
#include "mozilla/jsipc/ContextWrapperChild.h"
#include "mozilla/jsipc/ObjectWrapperChild.h"
#include "mozilla/jsipc/CPOWTypes.h"
#include "jsapi.h"
#include "nsAutoPtr.h"
#include "nsTArray.h"
#include "nsContentUtils.h"
#include "nsCxPusher.h"
#include "nsJSUtils.h"
using namespace mozilla::jsipc;
using namespace js;
namespace {
class MOZ_STACK_CLASS AutoContextPusher {
nsCxPusher mStack;
JSContext* const mContext;
const uint32_t mSavedOptions;
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
public:
AutoContextPusher(JSContext* cx
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: mContext(cx)
, mSavedOptions(JS_SetOptions(cx, (JS_GetOptions(cx) |
JSOPTION_DONT_REPORT_UNCAUGHT)))
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
mStack.Push(cx);
}
~AutoContextPusher() {
mStack.Pop();
JS_SetOptions(mContext, mSavedOptions);
}
};
class StatusPtrOwner
{
OperationStatus* mStatusPtr;
public:
StatusPtrOwner() : mStatusPtr(NULL) {}
void SetStatusPtr(OperationStatus* statusPtr) {
mStatusPtr = statusPtr;
// By default, initialize mStatusPtr to failure without an
// exception. Doing so only when the union is uninitialized
// allows AutoCheckOperation classes to be nested on the
// stack, just in case AnswerConstruct, for example, calls
// AnswerCall (as it once did, before there were unrelated
// problems with that approach).
if (mStatusPtr->type() == OperationStatus::T__None)
*mStatusPtr = JS_FALSE;
}
OperationStatus* StatusPtr() {
NS_ASSERTION(mStatusPtr, "Should have called SetStatusPtr by now.");
return mStatusPtr;
}
};
typedef AutoCheckOperationBase<StatusPtrOwner> ACOBase;
class AutoCheckOperation : public ACOBase
{
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
public:
AutoCheckOperation(ObjectWrapperChild* owc,
OperationStatus* statusPtr
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
: ACOBase(NULL, owc)
{
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
SetStatusPtr(statusPtr);
}
};
}
void
ObjectWrapperChild::CheckOperation(JSContext*,
OperationStatus* status)
{
NS_PRECONDITION(status->type() != OperationStatus::T__None,
"Checking an uninitialized operation.");
JSContext* cx = Manager()->GetContext();
jsval thrown;
if (JS_GetPendingException(cx, &thrown)) {
NS_ASSERTION(!(status->type() == OperationStatus::TJSBool &&
status->get_JSBool()),
"Operation succeeded but exception was thrown?");
JSVariant exception;
if (!jsval_to_JSVariant(cx, thrown, &exception))
exception = void_t(); // XXX Useful?
*status = exception;
JS_ClearPendingException(cx);
}
}
ObjectWrapperChild::ObjectWrapperChild(JSContext* cx, JSObject* obj)
: mObj(obj)
{
AutoContextPusher acp(cx);
#ifdef DEBUG
bool added =
#endif
JS_AddObjectRoot(cx, &mObj);
NS_ASSERTION(added, "ObjectWrapperChild constructor failed to root JSObject*");
}
void
ObjectWrapperChild::ActorDestroy(ActorDestroyReason why)
{
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
JS_RemoveObjectRoot(cx, &mObj);
}
bool
ObjectWrapperChild::JSObject_to_JSVariant(JSContext* cx, JSObject* from,
JSVariant* to)
{
*to = Manager()->GetOrCreateWrapper(from);
return true;
}
bool
ObjectWrapperChild::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:
// fall through
case JSTYPE_OBJECT:
return JSObject_to_JSVariant(cx, JSVAL_TO_OBJECT(from), to);
case JSTYPE_STRING:
{
nsDependentJSString depStr;
if (!depStr.init(cx, from))
return false;
*to = depStr;
}
return true;
case JSTYPE_NUMBER:
if (JSVAL_IS_INT(from))
*to = JSVAL_TO_INT(from);
else if (JSVAL_IS_DOUBLE(from))
*to = JSVAL_TO_DOUBLE(from);
else return false;
return true;
case JSTYPE_BOOLEAN:
*to = !!JSVAL_TO_BOOLEAN(from);
return true;
default:
return false;
}
}
/*static*/ bool
ObjectWrapperChild::
JSObject_from_PObjectWrapperChild(JSContext*,
const PObjectWrapperChild* from,
JSObject** to)
{
const ObjectWrapperChild* owc =
static_cast<const ObjectWrapperChild*>(from);
*to = owc ? owc->mObj : NULL;
return true;
}
/*static*/ bool
ObjectWrapperChild::JSObject_from_JSVariant(JSContext* cx,
const JSVariant& from,
JSObject** to)
{
if (from.type() != JSVariant::TPObjectWrapperChild)
return false;
return JSObject_from_PObjectWrapperChild(cx,
from.get_PObjectWrapperChild(),
to);
}
/*static*/ bool
ObjectWrapperChild::jsval_from_JSVariant(JSContext* cx, const JSVariant& from,
jsval* to)
{
switch (from.type()) {
case JSVariant::Tvoid_t:
*to = JSVAL_VOID;
return true;
case JSVariant::TPObjectWrapperChild:
{
JSObject* obj;
if (!JSObject_from_JSVariant(cx, from, &obj))
return false;
*to = OBJECT_TO_JSVAL(obj);
return true;
}
case JSVariant::TnsString:
{
const nsString& str = from.get_nsString();
JSString* s = JS_NewUCStringCopyN(cx,
str.BeginReading(),
str.Length());
if (!s)
return false;
*to = STRING_TO_JSVAL(s);
}
return true;
case JSVariant::Tint:
*to = INT_TO_JSVAL(from.get_int());
return true;
case JSVariant::Tdouble:
*to = JS_NumberValue(from.get_double());
return true;
case JSVariant::Tbool:
*to = BOOLEAN_TO_JSVAL(from.get_bool());
return true;
default:
return false;
}
}
ContextWrapperChild*
ObjectWrapperChild::Manager()
{
PContextWrapperChild* pcwc = PObjectWrapperChild::Manager();
return static_cast<ContextWrapperChild*>(pcwc);
}
static bool
jsid_to_nsString(JSContext* cx, jsid from, nsString* to)
{
if (JSID_IS_STRING(from)) {
size_t length;
const jschar* chars = JS_GetInternedStringCharsAndLength(JSID_TO_STRING(from), &length);
*to = nsDependentString(chars, length);
return true;
}
return false;
}
static bool
jsid_from_nsString(JSContext* cx, const nsString& from, jsid* to)
{
JSString* str = JS_NewUCStringCopyN(cx, from.BeginReading(), from.Length());
if (!str)
return false;
return JS_ValueToId(cx, STRING_TO_JSVAL(str), to);
}
#if 0
// The general schema for ObjectWrapperChild::Answer* methods:
bool
ObjectWrapperChild::AnswerSomething(/* in-parameters */
/* out-parameters */)
{
// initialize out-parameters for failure
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
// validate in-parameters, else return false
// successfully perform local JS operations, else return true
// perform out-parameter conversions, else return false
return true;
}
// There's an important subtlety here: though a local JS operation may
// fail, leaving out-parameters uninitialized, we must initialize all
// out-parameters when reporting success (returning true) to the IPC
// messaging system. See AnswerGetProperty for illustration.
#endif
bool
ObjectWrapperChild::AnswerAddProperty(const nsString& id,
OperationStatus* status)
{
JSContext* cx = Manager()->GetContext();
JS::RootedId interned_id(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!jsid_from_nsString(cx, id, interned_id.address()))
return false;
*status = JS_DefinePropertyById(cx, mObj, interned_id, JSVAL_VOID,
NULL, NULL, 0);
return true;
}
bool
ObjectWrapperChild::AnswerGetProperty(const nsString& id,
OperationStatus* status, JSVariant* vp)
{
JSContext* cx = Manager()->GetContext();
JS::RootedId interned_id(cx);
JS::RootedValue val(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!jsid_from_nsString(cx, id, interned_id.address()))
return false;
*status = JS_GetPropertyById(cx, mObj, interned_id, val.address());
// Since we fully expect this call to jsval_to_JSVariant to return
// true, we can't just leave vp uninitialized when JS_GetPropertyById
// returns JS_FALSE. This pitfall could be avoided in general if IPDL
// ensured that outparams were pre-initialized to some default value
// (XXXfixme cjones?).
return jsval_to_JSVariant(cx, aco.Ok() ? val : JSVAL_VOID, vp);
}
bool
ObjectWrapperChild::AnswerSetProperty(const nsString& id, const JSVariant& v,
OperationStatus* status, JSVariant* vp)
{
*vp = v;
JSContext* cx = Manager()->GetContext();
JS::RootedId interned_id(cx);
JS::RootedValue val(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!jsid_from_nsString(cx, id, interned_id.address()) ||
!jsval_from_JSVariant(cx, v, val.address())) {
return false;
}
*status = JS_SetPropertyById(cx, mObj, interned_id, val.address());
return jsval_to_JSVariant(cx, aco.Ok() ? val : JSVAL_VOID, vp);
}
bool
ObjectWrapperChild::AnswerDelProperty(const nsString& id,
OperationStatus* status, JSVariant* vp)
{
JSContext* cx = Manager()->GetContext();
JS::RootedId interned_id(cx);
JS::RootedValue val(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!jsid_from_nsString(cx, id, interned_id.address()))
return false;
*status = JS_DeletePropertyById2(cx, mObj, interned_id, val.address());
return jsval_to_JSVariant(cx, aco.Ok() ? val : JSVAL_VOID, vp);
}
static const uint32_t sNextIdIndexSlot = 0;
static const uint32_t sNumNewEnumerateStateSlots = 1;
static void
CPOW_NewEnumerateState_FreeIds(JSObject* state)
{
nsTArray<nsString>* strIds =
static_cast<nsTArray<nsString>*>(JS_GetPrivate(state));
if (strIds) {
delete strIds;
JS_SetPrivate(state, NULL);
}
}
static void
CPOW_NewEnumerateState_Finalize(JSFreeOp* fop, JSObject* state)
{
CPOW_NewEnumerateState_FreeIds(state);
}
// Similar to IteratorClass in XPCWrapper.cpp
static const JSClass sCPOW_NewEnumerateState_JSClass = {
"CPOW NewEnumerate State",
JSCLASS_HAS_PRIVATE |
JSCLASS_HAS_RESERVED_SLOTS(sNumNewEnumerateStateSlots),
JS_PropertyStub, JS_DeletePropertyStub,
JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub,
JS_ConvertStub, CPOW_NewEnumerateState_Finalize
};
bool
ObjectWrapperChild::AnswerNewEnumerateInit(/* no in-parameters */
OperationStatus* status, JSVariant* statep, int* idp)
{
*idp = 0;
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
JSClass* clasp = const_cast<JSClass*>(&sCPOW_NewEnumerateState_JSClass);
JS::Rooted<JSObject*> state(cx, JS_NewObjectWithGivenProto(cx, clasp, NULL, NULL));
if (!state)
return false;
for (JS::RootedObject proto(cx, mObj); proto; ) {
AutoIdArray ids(cx, JS_Enumerate(cx, proto));
for (size_t i = 0; i < ids.length(); ++i)
JS_DefinePropertyById(cx, state, ids[i], JSVAL_VOID,
NULL, NULL, JSPROP_ENUMERATE | JSPROP_SHARED);
if (!JS_GetPrototype(cx, proto, proto.address()))
return false;
}
InfallibleTArray<nsString>* strIds;
{
AutoIdArray ids(cx, JS_Enumerate(cx, state));
if (!ids)
return false;
strIds = new InfallibleTArray<nsString>(ids.length());
for (size_t i = 0; i < ids.length(); ++i)
if (!jsid_to_nsString(cx, ids[i], strIds->AppendElement())) {
delete strIds;
return false;
}
}
*idp = strIds->Length();
JS_SetPrivate(state, strIds);
JS_SetReservedSlot(state, sNextIdIndexSlot, JSVAL_ZERO);
*status = JSObject_to_JSVariant(cx, state, statep);
return true;
}
bool
ObjectWrapperChild::AnswerNewEnumerateNext(const JSVariant& in_state,
OperationStatus* status, JSVariant* statep, nsString* idp)
{
JSObject* state;
*statep = in_state;
idp->Truncate();
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!JSObject_from_JSVariant(cx, in_state, &state))
return false;
InfallibleTArray<nsString>* strIds =
static_cast<InfallibleTArray<nsString>*>(JS_GetPrivate(state));
if (!strIds)
return false;
jsval v = JS_GetReservedSlot(state, sNextIdIndexSlot);
int32_t i = JSVAL_TO_INT(v);
NS_ASSERTION(i >= 0, "Index of next jsid negative?");
NS_ASSERTION(size_t(i) <= strIds->Length(), "Index of next jsid too large?");
if (size_t(i) == strIds->Length()) {
*status = JS_TRUE;
return JSObject_to_JSVariant(cx, NULL, statep);
}
*idp = strIds->ElementAt(i);
JS_SetReservedSlot(state, sNextIdIndexSlot, INT_TO_JSVAL(i + 1));
*status = JS_TRUE;
return true;
}
bool
ObjectWrapperChild::RecvNewEnumerateDestroy(const JSVariant& in_state)
{
JSObject* state;
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
if (!JSObject_from_JSVariant(cx, in_state, &state))
return false;
CPOW_NewEnumerateState_FreeIds(state);
return true;
}
bool
ObjectWrapperChild::AnswerNewResolve(const nsString& id, const int& flags,
OperationStatus* status, PObjectWrapperChild** obj2)
{
*obj2 = NULL;
JSContext* cx = Manager()->GetContext();
JS::RootedId interned_id(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!jsid_from_nsString(cx, id, interned_id.address()))
return false;
CPOW_LOG(("new-resolving \"%s\"...",
NS_ConvertUTF16toUTF8(id).get()));
JS::Rooted<JSPropertyDescriptor> desc(cx);
if (!JS_GetPropertyDescriptorById(cx, mObj, interned_id, flags, desc.address()))
return true;
*status = JS_TRUE;
if (desc.get().obj)
*obj2 = Manager()->GetOrCreateWrapper(desc.get().obj);
return true;
}
bool
ObjectWrapperChild::AnswerConvert(const JSType& type,
OperationStatus* status, JSVariant* vp)
{
JSContext* cx = Manager()->GetContext();
JS::RootedValue v(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
*status = JS_ConvertValue(cx, OBJECT_TO_JSVAL(mObj), type, v.address());
return jsval_to_JSVariant(cx, aco.Ok() ? v : JSVAL_VOID, vp);
}
namespace {
// Should be an overestimate of typical JS function arity.
typedef nsAutoTArray<jsval, 5> AutoJSArgs;
}
bool
ObjectWrapperChild::AnswerCall(PObjectWrapperChild* receiver, const InfallibleTArray<JSVariant>& argv,
OperationStatus* status, JSVariant* rval)
{
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
JS::RootedObject obj(cx);
if (!JSObject_from_PObjectWrapperChild(cx, receiver, obj.address()))
return false;
AutoJSArgs args;
uint32_t argc = argv.Length();
jsval *jsargs = args.AppendElements(argc);
if (!jsargs)
return false;
AutoArrayRooter tvr(cx, argc, jsargs);
for (uint32_t i = 0; i < argc; ++i)
if (!jsval_from_JSVariant(cx, argv.ElementAt(i), jsargs + i))
return false;
JS::RootedValue rv(cx);
*status = JS_CallFunctionValue(cx, obj, OBJECT_TO_JSVAL(mObj),
argv.Length(), jsargs, rv.address());
return jsval_to_JSVariant(cx, aco.Ok() ? rv : JSVAL_VOID, rval);
}
bool
ObjectWrapperChild::AnswerConstruct(const InfallibleTArray<JSVariant>& argv,
OperationStatus* status, PObjectWrapperChild** rval)
{
JSContext* cx = Manager()->GetContext();
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
AutoJSArgs args;
uint32_t argc = argv.Length();
jsval* jsargs = args.AppendElements(argc);
if (!jsargs)
return false;
AutoArrayRooter tvr(cx, argc, jsargs);
for (uint32_t i = 0; i < argc; ++i)
if (!jsval_from_JSVariant(cx, argv.ElementAt(i), jsargs + i))
return false;
JSObject* obj = JS_New(cx, mObj, argc, jsargs);
*status = !!obj;
*rval = Manager()->GetOrCreateWrapper(obj);
return true;
}
bool
ObjectWrapperChild::AnswerHasInstance(const JSVariant& v,
OperationStatus* status, JSBool* bp)
{
JSContext* cx = Manager()->GetContext();
JS::RootedValue candidate(cx);
AutoContextPusher acp(cx);
AutoCheckOperation aco(this, status);
if (!jsval_from_JSVariant(cx, v, candidate.address()))
return false;
*status = JS_HasInstance(cx, mObj, candidate, bp);
return true;
}