2010-07-02 14:09:48 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=4 sw=4 et tw=99 ft=cpp:
|
|
|
|
*
|
|
|
|
* ***** 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, released
|
|
|
|
* June 24, 2010.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Andreas Gal <gal@mozilla.com>
|
|
|
|
*
|
|
|
|
* 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 "XrayWrapper.h"
|
|
|
|
#include "AccessCheck.h"
|
|
|
|
#include "FilteringWrapper.h"
|
|
|
|
#include "CrossOriginWrapper.h"
|
|
|
|
#include "WrapperFactory.h"
|
|
|
|
|
|
|
|
#include "jscntxt.h"
|
2010-10-10 15:37:22 -07:00
|
|
|
#include "jsiter.h"
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
#include "XPCWrapper.h"
|
|
|
|
#include "xpcprivate.h"
|
|
|
|
|
|
|
|
namespace xpc {
|
|
|
|
|
|
|
|
using namespace js;
|
|
|
|
|
2011-04-29 14:51:49 -07:00
|
|
|
static const uint32 JSSLOT_WN = 0;
|
2010-10-13 11:49:22 -07:00
|
|
|
static const uint32 JSSLOT_RESOLVING = 1;
|
|
|
|
static const uint32 JSSLOT_EXPANDO = 2;
|
2010-09-23 15:56:28 -07:00
|
|
|
|
|
|
|
class ResolvingId
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResolvingId(JSObject *holder, jsid id)
|
|
|
|
: mId(id),
|
|
|
|
mPrev(getResolvingId(holder)),
|
|
|
|
mHolder(holder)
|
|
|
|
{
|
|
|
|
holder->setSlot(JSSLOT_RESOLVING, PrivateValue(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
~ResolvingId() {
|
|
|
|
NS_ASSERTION(getResolvingId(mHolder) == this, "unbalanced ResolvingIds");
|
|
|
|
mHolder->setSlot(JSSLOT_RESOLVING, PrivateValue(mPrev));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ResolvingId *getResolvingId(JSObject *holder) {
|
|
|
|
return (ResolvingId *)holder->getSlot(JSSLOT_RESOLVING).toPrivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
jsid mId;
|
|
|
|
ResolvingId *mPrev;
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSObject *mHolder;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsResolving(JSObject *holder, jsid id)
|
|
|
|
{
|
|
|
|
for (ResolvingId *cur = ResolvingId::getResolvingId(holder); cur; cur = cur->mPrev) {
|
|
|
|
if (cur->mId == id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
static JSBool
|
|
|
|
holder_get(JSContext *cx, JSObject *holder, jsid id, jsval *vp);
|
|
|
|
|
|
|
|
static JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
holder_set(JSContext *cx, JSObject *holder, jsid id, JSBool strict, jsval *vp);
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
namespace XrayUtils {
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
JSClass HolderClass = {
|
|
|
|
"NativePropertyHolder",
|
2010-10-10 15:49:13 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(3),
|
2010-07-02 14:09:48 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, holder_get, holder_set,
|
2010-10-10 15:49:13 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
|
2010-07-02 14:09:48 -07:00
|
|
|
NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace XrayUtils;
|
|
|
|
|
|
|
|
static JSObject *
|
|
|
|
GetHolder(JSObject *obj)
|
|
|
|
{
|
|
|
|
return &obj->getProxyExtra().toObject();
|
|
|
|
}
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
static XPCWrappedNative *
|
|
|
|
GetWrappedNative(JSObject *obj)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IS_WN_WRAPPER_OBJECT(obj), "expected a wrapped native here");
|
|
|
|
return static_cast<XPCWrappedNative *>(obj->getPrivate());
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:51:49 -07:00
|
|
|
static XPCWrappedNative *
|
|
|
|
GetWrappedNativeFromHolder(JSObject *holder)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(holder->getJSClass() == &HolderClass, "expected a native property holder object");
|
|
|
|
return static_cast<XPCWrappedNative *>(holder->getSlot(JSSLOT_WN).toPrivate());
|
|
|
|
}
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
static JSObject *
|
2011-04-29 14:51:49 -07:00
|
|
|
GetWrappedNativeObjectFromHolder(JSObject *holder)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
NS_ASSERTION(holder->getJSClass() == &HolderClass, "expected a native property holder object");
|
2011-04-29 14:51:49 -07:00
|
|
|
return GetWrappedNativeFromHolder(holder)->GetFlatJSObject();
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2010-10-10 15:49:13 -07:00
|
|
|
static JSObject *
|
2011-01-18 16:47:22 -08:00
|
|
|
GetExpandoObject(JSObject *holder)
|
2010-10-10 15:49:13 -07:00
|
|
|
{
|
2011-01-18 16:47:22 -08:00
|
|
|
NS_ASSERTION(holder->getJSClass() == &HolderClass, "expected a native property holder object");
|
|
|
|
return holder->getSlot(JSSLOT_EXPANDO).toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject *
|
|
|
|
EnsureExpandoObject(JSContext *cx, JSObject *holder)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(holder->getJSClass() == &HolderClass, "expected a native property holder object");
|
|
|
|
JSObject *expando = GetExpandoObject(holder);
|
|
|
|
if (expando)
|
|
|
|
return expando;
|
|
|
|
CompartmentPrivate *priv =
|
|
|
|
(CompartmentPrivate *)JS_GetCompartmentPrivate(cx, holder->compartment());
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
2011-01-18 16:47:22 -08:00
|
|
|
expando = priv->LookupExpandoObject(wn);
|
2010-10-10 15:49:13 -07:00
|
|
|
if (!expando) {
|
2011-01-18 16:47:22 -08:00
|
|
|
expando = JS_NewObjectWithGivenProto(cx, nsnull, nsnull, holder->getParent());
|
2011-01-07 09:19:54 -08:00
|
|
|
if (!expando)
|
|
|
|
return NULL;
|
2011-01-18 16:47:22 -08:00
|
|
|
// Add the expando object to the expando map to keep it alive.
|
|
|
|
if (!priv->RegisterExpandoObject(wn, expando)) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Make sure the wn stays alive so it keeps the expando object alive.
|
|
|
|
nsRefPtr<nsXPCClassInfo> ci;
|
|
|
|
CallQueryInterface(wn->Native(), getter_AddRefs(ci));
|
|
|
|
if (ci)
|
|
|
|
ci->PreserveWrapper(wn->Native());
|
2010-10-10 15:49:13 -07:00
|
|
|
}
|
2011-01-18 16:47:22 -08:00
|
|
|
holder->setSlot(JSSLOT_EXPANDO, ObjectValue(*expando));
|
2010-10-10 15:49:13 -07:00
|
|
|
return expando;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:04:47 -07:00
|
|
|
static inline JSObject *
|
|
|
|
FindWrapper(JSObject *wrapper)
|
|
|
|
{
|
|
|
|
while (!wrapper->isWrapper() ||
|
|
|
|
!(JSWrapper::wrapperHandler(wrapper)->flags() & WrapperFactory::IS_XRAY_WRAPPER_FLAG)) {
|
|
|
|
wrapper = wrapper->getProto();
|
|
|
|
// NB: we must eventually hit our wrapper.
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
// Some DOM objects have shared properties that don't have an explicit
|
|
|
|
// getter/setter and rely on the class getter/setter. We install a
|
|
|
|
// class getter/setter on the holder object to trigger them.
|
|
|
|
static JSBool
|
2010-09-23 15:56:28 -07:00
|
|
|
holder_get(JSContext *cx, JSObject *wrapper, jsid id, jsval *vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2011-04-11 16:04:47 -07:00
|
|
|
wrapper = FindWrapper(wrapper);
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-09-02 16:02:51 -07:00
|
|
|
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantGetProperty)) {
|
2010-10-10 15:38:39 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, holder))
|
|
|
|
return false;
|
2011-08-03 18:59:10 -07:00
|
|
|
PRBool retval = true;
|
2010-09-23 15:56:28 -07:00
|
|
|
nsresult rv = wn->GetScriptableCallback()->GetProperty(wn, cx, wrapper, id, vp, &retval);
|
2011-02-15 19:00:55 -08:00
|
|
|
if (NS_FAILED(rv) || !retval) {
|
2010-09-23 15:56:28 -07:00
|
|
|
if (retval)
|
2010-07-02 14:09:48 -07:00
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2011-02-09 11:31:40 -08:00
|
|
|
holder_set(JSContext *cx, JSObject *wrapper, jsid id, JSBool strict, jsval *vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2011-04-11 16:04:47 -07:00
|
|
|
wrapper = FindWrapper(wrapper);
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-10-10 15:39:23 -07:00
|
|
|
if (IsResolving(holder, id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantSetProperty)) {
|
2010-10-10 15:38:39 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, holder))
|
|
|
|
return false;
|
2011-08-03 18:59:10 -07:00
|
|
|
PRBool retval = true;
|
2010-09-23 15:56:28 -07:00
|
|
|
nsresult rv = wn->GetScriptableCallback()->SetProperty(wn, cx, wrapper, id, vp, &retval);
|
2011-02-15 19:00:55 -08:00
|
|
|
if (NS_FAILED(rv) || !retval) {
|
2010-09-23 15:56:28 -07:00
|
|
|
if (retval)
|
2010-07-02 14:09:48 -07:00
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2010-09-23 15:56:28 -07:00
|
|
|
ResolveNativeProperty(JSContext *cx, JSObject *wrapper, JSObject *holder, jsid id, bool set,
|
2010-09-20 14:48:01 -07:00
|
|
|
JSPropertyDescriptor *desc)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
|
|
|
desc->obj = NULL;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
NS_ASSERTION(holder->getJSClass() == &HolderClass, "expected a native property holder object");
|
2011-04-29 14:51:49 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(holder);
|
2010-07-02 14:09:48 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNative(wnObject);
|
|
|
|
|
|
|
|
// This will do verification and the method lookup for us.
|
2010-09-02 16:02:51 -07:00
|
|
|
XPCCallContext ccx(JS_CALLER, cx, wnObject, nsnull, id);
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
// There are no native numeric properties, so we can shortcut here. We will not
|
|
|
|
// find the property.
|
|
|
|
if (!JSID_IS_ATOM(id)) {
|
|
|
|
/* Not found */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCNativeInterface *iface;
|
|
|
|
XPCNativeMember *member;
|
|
|
|
if (ccx.GetWrapper() != wn ||
|
2010-09-02 16:02:51 -07:00
|
|
|
!wn->IsValid() ||
|
|
|
|
!(iface = ccx.GetInterface()) ||
|
|
|
|
!(member = ccx.GetMember())) {
|
2010-07-02 14:09:48 -07:00
|
|
|
/* Not found */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc->obj = holder;
|
|
|
|
desc->attrs = JSPROP_ENUMERATE;
|
|
|
|
desc->getter = NULL;
|
|
|
|
desc->setter = NULL;
|
2010-11-17 12:39:37 -08:00
|
|
|
desc->shortid = 0;
|
2010-07-02 14:09:48 -07:00
|
|
|
desc->value = JSVAL_VOID;
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
jsval fval = JSVAL_VOID;
|
2010-07-02 14:09:48 -07:00
|
|
|
if (member->IsConstant()) {
|
|
|
|
if (!member->GetConstantValue(ccx, iface, &desc->value)) {
|
|
|
|
JS_ReportError(cx, "Failed to convert constant native property to JS value");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (member->IsAttribute()) {
|
|
|
|
// This is a getter/setter. Clone a function for it.
|
2011-01-25 15:06:45 -08:00
|
|
|
if (!member->NewFunctionObject(ccx, iface, wrapper, &fval)) {
|
2010-07-02 14:09:48 -07:00
|
|
|
JS_ReportError(cx, "Failed to clone function object for native getter/setter");
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
desc->attrs |= JSPROP_GETTER;
|
2010-09-20 14:48:01 -07:00
|
|
|
if (member->IsWritableAttribute())
|
2010-07-02 14:09:48 -07:00
|
|
|
desc->attrs |= JSPROP_SETTER;
|
|
|
|
|
|
|
|
// Make the property shared on the holder so no slot is allocated
|
|
|
|
// for it. This avoids keeping garbage alive through that slot.
|
|
|
|
desc->attrs |= JSPROP_SHARED;
|
|
|
|
} else {
|
|
|
|
// This is a method. Clone a function for it.
|
2011-01-25 15:06:45 -08:00
|
|
|
if (!member->NewFunctionObject(ccx, iface, wrapper, &desc->value)) {
|
2010-07-02 14:09:48 -07:00
|
|
|
JS_ReportError(cx, "Failed to clone function object for native function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Without a wrapper the function would live on the prototype. Since we
|
|
|
|
// don't have one, we have to avoid calling the scriptable helper's
|
|
|
|
// GetProperty method for this property, so stub out the getter and
|
|
|
|
// setter here explicitly.
|
2011-02-09 11:31:40 -08:00
|
|
|
desc->getter = JS_PropertyStub;
|
|
|
|
desc->setter = JS_StrictPropertyStub;
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
if (!JS_WrapValue(cx, &desc->value) || !JS_WrapValue(cx, &fval))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (desc->attrs & JSPROP_GETTER)
|
|
|
|
desc->getter = CastAsJSPropertyOp(JSVAL_TO_OBJECT(fval));
|
|
|
|
if (desc->attrs & JSPROP_SETTER)
|
2011-02-09 11:31:40 -08:00
|
|
|
desc->setter = CastAsJSStrictPropertyOp(JSVAL_TO_OBJECT(fval));
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
// Define the property.
|
|
|
|
return JS_DefinePropertyById(cx, holder, id, desc->value,
|
|
|
|
desc->getter, desc->setter, desc->attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-10-10 15:48:29 -07:00
|
|
|
wrappedJSObject_getter(JSContext *cx, JSObject *wrapper, jsid id, jsval *vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-10-10 15:48:29 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(wrapper);
|
2010-10-10 15:47:22 -07:00
|
|
|
|
2010-10-10 15:48:29 -07:00
|
|
|
return WrapperFactory::WaiveXrayAndWrap(cx, vp);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2010-10-10 15:36:32 -07:00
|
|
|
static JSBool
|
|
|
|
XrayToString(JSContext *cx, uintN argc, jsval *vp)
|
|
|
|
{
|
|
|
|
JSObject *wrapper = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!wrapper->isWrapper() || !WrapperFactory::IsXrayWrapper(wrapper)) {
|
|
|
|
JS_ReportError(cx, "XrayToString called on an incompatible object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
|
|
|
JSObject *wrappednative = wn->GetFlatJSObject();
|
2010-10-10 15:36:32 -07:00
|
|
|
|
|
|
|
XPCCallContext ccx(JS_CALLER, cx, wrappednative);
|
|
|
|
char *wrapperStr = wn->ToString(ccx);
|
|
|
|
if (!wrapperStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString result(NS_LITERAL_STRING("[object XrayWrapper "));
|
|
|
|
result.AppendASCII(wrapperStr);
|
|
|
|
JS_smprintf_free(wrapperStr);
|
|
|
|
result.Append(']');
|
|
|
|
|
|
|
|
JSString *str = JS_NewUCStringCopyN(cx, reinterpret_cast<const jschar *>(result.get()),
|
|
|
|
result.Length());
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*vp = STRING_TO_JSVAL(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
|
|
|
XrayWrapper<Base>::XrayWrapper(uintN flags)
|
2010-10-10 15:36:04 -07:00
|
|
|
: Base(flags | WrapperFactory::IS_XRAY_WRAPPER_FLAG)
|
2010-09-02 16:02:51 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
|
|
|
XrayWrapper<Base>::~XrayWrapper()
|
2010-09-02 16:02:51 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-29 10:00:52 -07:00
|
|
|
class AutoLeaveHelper
|
|
|
|
{
|
|
|
|
public:
|
2011-01-25 15:06:45 -08:00
|
|
|
AutoLeaveHelper(XrayWrapper<Base> &xray, JSContext *cx, JSObject *wrapper)
|
2010-09-29 10:00:52 -07:00
|
|
|
: xray(xray), cx(cx), wrapper(wrapper)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~AutoLeaveHelper()
|
|
|
|
{
|
|
|
|
xray.leave(cx, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base> &xray;
|
2010-09-29 10:00:52 -07:00
|
|
|
JSContext *cx;
|
|
|
|
JSObject *wrapper;
|
|
|
|
};
|
|
|
|
|
2010-10-10 15:41:24 -07:00
|
|
|
static bool
|
2010-10-10 15:42:21 -07:00
|
|
|
Transparent(JSContext *cx, JSObject *wrapper)
|
2010-10-10 15:41:24 -07:00
|
|
|
{
|
2010-10-10 15:47:22 -07:00
|
|
|
if (WrapperFactory::HasWaiveXrayFlag(wrapper))
|
|
|
|
return true;
|
|
|
|
|
2010-10-10 15:42:21 -07:00
|
|
|
if (!WrapperFactory::IsPartiallyTransparent(wrapper))
|
|
|
|
return false;
|
|
|
|
|
2010-10-10 15:41:24 -07:00
|
|
|
// Redirect access straight to the wrapper if UniversalXPConnect is enabled.
|
|
|
|
nsIScriptSecurityManager *ssm = XPCWrapper::GetSecurityManager();
|
|
|
|
if (ssm) {
|
|
|
|
PRBool privileged;
|
|
|
|
if (NS_SUCCEEDED(ssm->IsCapabilityEnabled("UniversalXPConnect", &privileged)) && privileged)
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-10 15:42:21 -07:00
|
|
|
|
2010-10-16 15:26:14 -07:00
|
|
|
return AccessCheck::documentDomainMakesSameOrigin(cx, wrapper->unwrap());
|
2010-10-10 15:41:24 -07:00
|
|
|
}
|
|
|
|
|
2010-10-10 15:49:30 -07:00
|
|
|
namespace XrayUtils {
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsTransparent(JSContext *cx, JSObject *wrapper)
|
|
|
|
{
|
|
|
|
return Transparent(cx, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::resolveOwnProperty(JSContext *cx, JSObject *wrapper, jsid id, bool set,
|
|
|
|
PropertyDescriptor *desc_in)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSPropertyDescriptor *desc = Jsvalify(desc_in);
|
|
|
|
|
2011-05-10 14:41:25 -07:00
|
|
|
// Partially transparent wrappers (which used to be known as XOWs) don't
|
|
|
|
// have a .wrappedJSObject property.
|
|
|
|
if (!WrapperFactory::IsPartiallyTransparent(wrapper) &&
|
|
|
|
id == nsXPConnect::GetRuntimeInstance()->GetStringID(XPCJSRuntime::IDX_WRAPPED_JSOBJECT)) {
|
2011-01-29 18:48:30 -08:00
|
|
|
bool status;
|
|
|
|
JSWrapper::Action action = set ? JSWrapper::SET : JSWrapper::GET;
|
|
|
|
desc->obj = NULL; // default value
|
|
|
|
if (!this->enter(cx, wrapper, id, action, &status))
|
|
|
|
return status;
|
2010-10-10 15:39:23 -07:00
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
AutoLeaveHelper<Base> helper(*this, cx, wrapper);
|
2010-10-10 15:39:23 -07:00
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
desc->obj = wrapper;
|
|
|
|
desc->attrs = JSPROP_ENUMERATE|JSPROP_SHARED;
|
|
|
|
desc->getter = wrappedJSObject_getter;
|
|
|
|
desc->setter = NULL;
|
2010-10-10 15:36:32 -07:00
|
|
|
desc->shortid = 0;
|
2010-07-02 14:09:48 -07:00
|
|
|
desc->value = JSVAL_VOID;
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2011-02-15 19:00:55 -08:00
|
|
|
desc->obj = NULL;
|
|
|
|
|
|
|
|
uintN flags = (set ? JSRESOLVE_ASSIGNING : 0) | JSRESOLVE_QUALIFIED;
|
2010-11-10 14:08:44 -08:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2011-01-18 16:47:22 -08:00
|
|
|
JSObject *expando = GetExpandoObject(holder);
|
2011-02-14 18:26:20 -08:00
|
|
|
|
2011-02-15 19:00:55 -08:00
|
|
|
// Check for expando properties first.
|
|
|
|
if (expando && !JS_GetPropertyDescriptorById(cx, expando, id, flags, desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc->obj) {
|
|
|
|
// Pretend the property lives on the wrapper.
|
|
|
|
desc->obj = wrapper;
|
|
|
|
return true;
|
2010-11-10 14:08:44 -08:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:00:55 -08:00
|
|
|
JSBool hasProp;
|
|
|
|
if (!JS_HasPropertyById(cx, holder, id, &hasProp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!hasProp) {
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
2011-02-15 19:00:55 -08:00
|
|
|
|
|
|
|
// Run the resolve hook of the wrapped native.
|
|
|
|
if (!NATIVE_HAS_FLAG(wn, WantNewResolve)) {
|
|
|
|
desc->obj = nsnull;
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-10 14:08:44 -08:00
|
|
|
|
2011-08-03 18:59:10 -07:00
|
|
|
PRBool retval = true;
|
2010-11-10 14:08:44 -08:00
|
|
|
JSObject *pobj = NULL;
|
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->NewResolve(wn, cx, wrapper, id,
|
|
|
|
flags, &pobj, &retval);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (retval)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-15 19:00:55 -08:00
|
|
|
if (!pobj) {
|
|
|
|
desc->obj = nsnull;
|
2010-11-10 14:08:44 -08:00
|
|
|
return true;
|
|
|
|
}
|
2011-02-15 19:00:55 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(JS_HasPropertyById(cx, holder, id, &hasProp) &&
|
|
|
|
hasProp, "id got defined somewhere else?");
|
|
|
|
#endif
|
2010-11-10 14:08:44 -08:00
|
|
|
}
|
|
|
|
|
2011-02-15 19:00:55 -08:00
|
|
|
if (!JS_GetPropertyDescriptorById(cx, holder, id, flags, desc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Pretend we found the property on the wrapper, not the holder.
|
|
|
|
desc->obj = wrapper;
|
|
|
|
|
2010-10-10 15:49:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-10-10 15:49:13 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::getPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id,
|
|
|
|
bool set, PropertyDescriptor *desc_in)
|
2010-10-10 15:49:13 -07:00
|
|
|
{
|
|
|
|
JSPropertyDescriptor *desc = Jsvalify(desc_in);
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
if (IsResolving(holder, id)) {
|
|
|
|
desc->obj = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-29 18:48:30 -08:00
|
|
|
bool status;
|
|
|
|
JSWrapper::Action action = set ? JSWrapper::SET : JSWrapper::GET;
|
|
|
|
desc->obj = NULL; // default value
|
|
|
|
if (!this->enter(cx, wrapper, id, action, &status))
|
|
|
|
return status;
|
2010-10-10 15:39:23 -07:00
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
AutoLeaveHelper<Base> helper(*this, cx, wrapper);
|
2010-10-10 15:39:23 -07:00
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
ResolvingId resolving(holder, id);
|
|
|
|
|
2010-10-10 15:47:22 -07:00
|
|
|
// Redirect access straight to the wrapper if we should be transparent.
|
2010-10-10 15:42:21 -07:00
|
|
|
if (Transparent(cx, wrapper)) {
|
2011-04-29 14:51:49 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(holder);
|
2010-10-10 15:41:24 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wnObject))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!JS_GetPropertyDescriptorById(cx, wnObject, id,
|
|
|
|
(set ? JSRESOLVE_ASSIGNING : 0) | JSRESOLVE_QUALIFIED,
|
|
|
|
desc))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-24 10:36:33 -08:00
|
|
|
if (desc->obj)
|
|
|
|
desc->obj = wrapper;
|
2011-09-03 03:22:10 -07:00
|
|
|
return cx->compartment->wrap(cx, desc_in);
|
2010-10-10 15:41:24 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
if (!this->resolveOwnProperty(cx, wrapper, id, set, desc_in))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (desc->obj)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bool ok = ResolveNativeProperty(cx, wrapper, holder, id, set, desc);
|
2010-10-10 15:46:26 -07:00
|
|
|
if (!ok || desc->obj)
|
|
|
|
return ok;
|
|
|
|
|
|
|
|
if (id == nsXPConnect::GetRuntimeInstance()->GetStringID(XPCJSRuntime::IDX_TO_STRING)) {
|
|
|
|
desc->obj = wrapper;
|
|
|
|
desc->attrs = 0;
|
|
|
|
desc->getter = NULL;
|
|
|
|
desc->setter = NULL;
|
|
|
|
desc->shortid = 0;
|
|
|
|
|
|
|
|
JSObject *toString = JS_NewFunction(cx, XrayToString, 0, 0, holder, "toString");
|
|
|
|
if (!toString)
|
|
|
|
return false;
|
|
|
|
desc->value = OBJECT_TO_JSVAL(toString);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
return true;
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::getOwnPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id,
|
|
|
|
bool set, PropertyDescriptor *desc_in)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-10-10 15:49:13 -07:00
|
|
|
JSPropertyDescriptor *desc = Jsvalify(desc_in);
|
2010-11-10 14:08:44 -08:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
if (IsResolving(holder, id)) {
|
|
|
|
desc->obj = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-10 15:49:13 -07:00
|
|
|
|
2011-01-29 18:48:30 -08:00
|
|
|
bool status;
|
|
|
|
JSWrapper::Action action = set ? JSWrapper::SET : JSWrapper::GET;
|
|
|
|
desc->obj = NULL; // default value
|
|
|
|
if (!this->enter(cx, wrapper, id, action, &status))
|
|
|
|
return status;
|
2010-10-10 15:49:13 -07:00
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
AutoLeaveHelper<Base> helper(*this, cx, wrapper);
|
2010-10-10 15:49:13 -07:00
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
ResolvingId resolving(holder, id);
|
|
|
|
|
|
|
|
// NB: Nothing we do here acts on the wrapped native itself, so we don't
|
|
|
|
// enter our policy.
|
|
|
|
// Redirect access straight to the wrapper if we should be transparent.
|
|
|
|
if (Transparent(cx, wrapper)) {
|
2011-04-29 14:51:49 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(holder);
|
2010-10-10 15:49:13 -07:00
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
{
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wnObject))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!JS_GetPropertyDescriptorById(cx, wnObject, id,
|
|
|
|
(set ? JSRESOLVE_ASSIGNING : 0) | JSRESOLVE_QUALIFIED,
|
|
|
|
desc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
desc->obj = (desc->obj == wnObject) ? wrapper : nsnull;
|
2011-09-03 03:22:10 -07:00
|
|
|
return cx->compartment->wrap(cx, desc_in);
|
2010-11-10 14:08:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return this->resolveOwnProperty(cx, wrapper, id, set, desc_in);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::defineProperty(JSContext *cx, JSObject *wrapper, jsid id,
|
|
|
|
js::PropertyDescriptor *desc)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-10-10 15:41:24 -07:00
|
|
|
JSPropertyDescriptor *jsdesc = Jsvalify(desc);
|
|
|
|
|
2010-10-10 15:47:22 -07:00
|
|
|
// Redirect access straight to the wrapper if we should be transparent.
|
2010-10-10 15:42:21 -07:00
|
|
|
if (Transparent(cx, wrapper)) {
|
2011-04-29 14:51:49 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(holder);
|
2010-10-10 15:41:24 -07:00
|
|
|
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wnObject))
|
|
|
|
return false;
|
|
|
|
|
2011-09-03 03:22:10 -07:00
|
|
|
if (!cx->compartment->wrap(cx, desc))
|
2010-10-10 15:41:24 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return JS_DefinePropertyById(cx, wnObject, id, jsdesc->value, jsdesc->getter, jsdesc->setter,
|
|
|
|
jsdesc->attrs);
|
|
|
|
}
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
PropertyDescriptor existing_desc;
|
|
|
|
if (!getOwnPropertyDescriptor(cx, wrapper, id, true, &existing_desc))
|
|
|
|
return false;
|
|
|
|
|
2010-09-24 15:30:00 -07:00
|
|
|
if (existing_desc.obj && (existing_desc.attrs & JSPROP_PERMANENT))
|
2010-10-10 15:37:22 -07:00
|
|
|
return true; // silently ignore attempt to overwrite native property
|
2010-09-23 15:56:28 -07:00
|
|
|
|
2010-10-10 15:49:13 -07:00
|
|
|
if (IsResolving(holder, id)) {
|
|
|
|
if (!(jsdesc->attrs & (JSPROP_GETTER | JSPROP_SETTER))) {
|
|
|
|
if (!desc->getter)
|
|
|
|
jsdesc->getter = holder_get;
|
|
|
|
if (!desc->setter)
|
|
|
|
jsdesc->setter = holder_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_DefinePropertyById(cx, holder, id, jsdesc->value, jsdesc->getter, jsdesc->setter,
|
|
|
|
jsdesc->attrs);
|
2010-09-23 15:56:28 -07:00
|
|
|
}
|
|
|
|
|
2011-01-18 16:47:22 -08:00
|
|
|
JSObject *expando = EnsureExpandoObject(cx, holder);
|
2010-10-10 15:49:13 -07:00
|
|
|
if (!expando)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return JS_DefinePropertyById(cx, expando, id, jsdesc->value, jsdesc->getter, jsdesc->setter,
|
2010-09-23 15:56:28 -07:00
|
|
|
jsdesc->attrs);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
static bool
|
|
|
|
EnumerateNames(JSContext *cx, JSObject *wrapper, uintN flags, js::AutoIdVector &props)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
2010-10-10 15:37:22 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-10-10 15:41:24 -07:00
|
|
|
|
2011-04-29 14:51:49 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(holder);
|
2010-11-10 14:08:44 -08:00
|
|
|
|
2010-10-10 15:47:22 -07:00
|
|
|
// Redirect access straight to the wrapper if we should be transparent.
|
2010-10-10 15:42:21 -07:00
|
|
|
if (Transparent(cx, wrapper)) {
|
2010-10-10 15:41:24 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wnObject))
|
|
|
|
return false;
|
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
return js::GetPropertyNames(cx, wnObject, flags, &props);
|
2010-10-10 15:41:24 -07:00
|
|
|
}
|
|
|
|
|
2010-12-14 16:38:51 -08:00
|
|
|
if (WrapperFactory::IsPartiallyTransparent(wrapper)) {
|
|
|
|
JS_ReportError(cx, "Not allowed to enumerate cross origin objects");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
// Enumerate expando properties first.
|
2011-01-18 16:47:22 -08:00
|
|
|
JSObject *expando = GetExpandoObject(holder);
|
|
|
|
if (expando && !js::GetPropertyNames(cx, expando, flags, &props))
|
2010-11-10 14:08:44 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Force all native properties to be materialized onto the wrapped native.
|
|
|
|
js::AutoIdVector wnProps(cx);
|
|
|
|
{
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wnObject))
|
|
|
|
return false;
|
|
|
|
if (!js::GetPropertyNames(cx, wnObject, flags, &wnProps))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the properties we got and enumerate all native ones.
|
|
|
|
for (size_t n = 0; n < wnProps.length(); ++n) {
|
|
|
|
jsid id = wnProps[n];
|
|
|
|
JSBool hasProp;
|
|
|
|
if (!JS_HasPropertyById(cx, wrapper, id, &hasProp))
|
|
|
|
return false;
|
|
|
|
if (hasProp)
|
|
|
|
props.append(id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-11-10 14:08:44 -08:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::getOwnPropertyNames(JSContext *cx, JSObject *wrapper,
|
|
|
|
js::AutoIdVector &props)
|
2010-11-10 14:08:44 -08:00
|
|
|
{
|
|
|
|
return EnumerateNames(cx, wrapper, JSITER_OWNONLY | JSITER_HIDDEN, props);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::delete_(JSContext *cx, JSObject *wrapper, jsid id, bool *bp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-10-10 15:37:22 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
jsval v;
|
|
|
|
JSBool b;
|
2010-10-10 15:41:24 -07:00
|
|
|
|
2010-10-10 15:42:21 -07:00
|
|
|
// Redirect access straight to the wrapper if we should be transparent.
|
|
|
|
if (Transparent(cx, wrapper)) {
|
2011-04-29 14:51:49 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(holder);
|
2010-10-10 15:41:24 -07:00
|
|
|
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wnObject))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!JS_DeletePropertyById2(cx, wnObject, id, &v) || !JS_ValueToBoolean(cx, v, &b))
|
|
|
|
return false;
|
|
|
|
*bp = !!b;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-18 16:47:22 -08:00
|
|
|
JSObject *expando = GetExpandoObject(holder);
|
|
|
|
b = true;
|
|
|
|
if (expando &&
|
|
|
|
(!JS_DeletePropertyById2(cx, expando, id, &v) ||
|
|
|
|
!JS_ValueToBoolean(cx, v, &b))) {
|
2010-10-10 15:49:13 -07:00
|
|
|
return false;
|
2011-01-18 16:47:22 -08:00
|
|
|
}
|
2010-10-10 15:49:13 -07:00
|
|
|
|
2010-10-10 15:37:22 -07:00
|
|
|
*bp = !!b;
|
2010-09-20 14:48:01 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::enumerate(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
2010-11-10 14:08:44 -08:00
|
|
|
return EnumerateNames(cx, wrapper, 0, props);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::fix(JSContext *cx, JSObject *proxy, js::Value *vp)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
vp->setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::get(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id,
|
|
|
|
js::Value *vp)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
2010-11-02 11:26:43 -07:00
|
|
|
// NB: None of the functions we call are prepared for the receiver not
|
|
|
|
// being the wrapper, so ignore the receiver here.
|
|
|
|
return JSProxyHandler::get(cx, wrapper, wrapper, id, vp);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id,
|
2011-02-09 11:31:40 -08:00
|
|
|
bool strict, js::Value *vp)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
2010-11-02 11:26:43 -07:00
|
|
|
// NB: None of the functions we call are prepared for the receiver not
|
|
|
|
// being the wrapper, so ignore the receiver here.
|
2011-02-09 11:31:40 -08:00
|
|
|
return JSProxyHandler::set(cx, wrapper, wrapper, id, strict, vp);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::has(JSContext *cx, JSObject *wrapper, jsid id, bool *bp)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
2010-07-02 14:09:48 -07:00
|
|
|
return JSProxyHandler::has(cx, wrapper, id, bp);
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::hasOwn(JSContext *cx, JSObject *wrapper, jsid id, bool *bp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-09-20 14:48:01 -07:00
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
2010-07-02 14:09:48 -07:00
|
|
|
return JSProxyHandler::hasOwn(cx, wrapper, id, bp);
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::keys(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
2011-01-10 11:42:11 -08:00
|
|
|
return JSProxyHandler::keys(cx, wrapper, props);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::iterate(JSContext *cx, JSObject *wrapper, uintN flags, js::Value *vp)
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
|
|
|
return JSProxyHandler::iterate(cx, wrapper, flags, vp);
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
template <typename Base>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base>::call(JSContext *cx, JSObject *wrapper, uintN argc, js::Value *vp)
|
|
|
|
{
|
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
2011-01-25 15:06:45 -08:00
|
|
|
|
|
|
|
// Run the resolve hook of the wrapped native.
|
|
|
|
if (NATIVE_HAS_FLAG(wn, WantCall)) {
|
|
|
|
XPCCallContext ccx(JS_CALLER, cx, wrapper, nsnull, JSID_VOID, argc,
|
|
|
|
Jsvalify(vp + 2), Jsvalify(vp));
|
|
|
|
if (!ccx.IsValid())
|
|
|
|
return false;
|
|
|
|
PRBool ok = PR_TRUE;
|
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->Call(wn, cx, wrapper,
|
|
|
|
argc, Jsvalify(vp + 2),
|
|
|
|
Jsvalify(vp), &ok);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (ok)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base>::construct(JSContext *cx, JSObject *wrapper, uintN argc,
|
|
|
|
js::Value *argv, js::Value *rval)
|
|
|
|
{
|
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2011-04-29 14:51:49 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNativeFromHolder(holder);
|
2011-01-25 15:06:45 -08:00
|
|
|
|
|
|
|
// Run the resolve hook of the wrapped native.
|
|
|
|
if (NATIVE_HAS_FLAG(wn, WantConstruct)) {
|
|
|
|
XPCCallContext ccx(JS_CALLER, cx, wrapper, nsnull, JSID_VOID, argc,
|
|
|
|
Jsvalify(argv), Jsvalify(rval));
|
|
|
|
if (!ccx.IsValid())
|
|
|
|
return false;
|
|
|
|
PRBool ok = PR_TRUE;
|
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->Construct(wn, cx, wrapper,
|
|
|
|
argc,
|
|
|
|
Jsvalify(argv),
|
|
|
|
Jsvalify(rval),
|
|
|
|
&ok);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (ok)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base>
|
2010-09-02 16:02:51 -07:00
|
|
|
JSObject *
|
2011-01-25 15:06:45 -08:00
|
|
|
XrayWrapper<Base>::createHolder(JSContext *cx, JSObject *wrappedNative, JSObject *parent)
|
2010-09-02 16:02:51 -07:00
|
|
|
{
|
|
|
|
JSObject *holder = JS_NewObjectWithGivenProto(cx, &HolderClass, nsnull, parent);
|
|
|
|
if (!holder)
|
|
|
|
return nsnull;
|
|
|
|
|
2011-01-18 16:47:22 -08:00
|
|
|
CompartmentPrivate *priv =
|
|
|
|
(CompartmentPrivate *)JS_GetCompartmentPrivate(cx, holder->compartment());
|
|
|
|
JSObject *inner = wrappedNative;
|
|
|
|
OBJ_TO_INNER_OBJECT(cx, inner);
|
|
|
|
XPCWrappedNative *wn = GetWrappedNative(inner);
|
|
|
|
Value expando = ObjectOrNullValue(priv->LookupExpandoObject(wn));
|
|
|
|
|
2011-04-29 14:51:49 -07:00
|
|
|
// A note about ownership: the holder has a direct pointer to the wrapped
|
|
|
|
// native that we're wrapping. Normally, we'd have to AddRef the pointer
|
|
|
|
// so that it doesn't have to be collected, but then we'd have to tell the
|
|
|
|
// cycle collector. Fortunately for us, we know that the Xray wrapper
|
|
|
|
// itself has a reference to the flat JS object which will hold the
|
|
|
|
// wrapped native alive. Furthermore, the reachability of that object and
|
|
|
|
// the associated holder are exactly the same, so we can use that for our
|
|
|
|
// strong reference.
|
2010-10-10 15:46:23 -07:00
|
|
|
JS_ASSERT(IS_WN_WRAPPER(wrappedNative) ||
|
|
|
|
wrappedNative->getClass()->ext.innerObject);
|
2011-04-29 14:51:49 -07:00
|
|
|
holder->setSlot(JSSLOT_WN, PrivateValue(wn));
|
2010-09-23 15:56:28 -07:00
|
|
|
holder->setSlot(JSSLOT_RESOLVING, PrivateValue(NULL));
|
2011-01-18 16:47:22 -08:00
|
|
|
holder->setSlot(JSSLOT_EXPANDO, expando);
|
2010-09-02 16:02:51 -07:00
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
2011-01-25 15:06:45 -08:00
|
|
|
#define XPCNW XrayWrapper<JSCrossCompartmentWrapper>
|
|
|
|
#define SCNW XrayWrapper<JSWrapper>
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <> XPCNW XPCNW::singleton(0);
|
|
|
|
template <> SCNW SCNW::singleton(0);
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template class XPCNW;
|
|
|
|
template class SCNW;
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
}
|