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;
|
|
|
|
|
|
|
|
static const uint32 JSSLOT_WN_OBJ = JSSLOT_PRIVATE;
|
2010-09-23 15:56:28 -07:00
|
|
|
static const uint32 JSSLOT_RESOLVING = JSSLOT_PRIVATE + 1;
|
|
|
|
|
|
|
|
namespace XrayUtils {
|
|
|
|
|
|
|
|
const uint32 JSSLOT_PROXY_OBJ = JSSLOT_PRIVATE + 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
holder_set(JSContext *cx, JSObject *holder, jsid id, jsval *vp);
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
holder_enumerate(JSContext *cx, JSObject *holder);
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
namespace XrayUtils {
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
JSClass HolderClass = {
|
|
|
|
"NativePropertyHolder",
|
2010-09-23 15:56:28 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(3),
|
2010-07-02 14:09:48 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, holder_get, holder_set,
|
|
|
|
holder_enumerate, JS_ResolveStub, JS_ConvertStub, NULL,
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject *
|
2010-09-02 16:02:51 -07:00
|
|
|
GetWrappedNativeObjectFromHolder(JSContext *cx, 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");
|
2010-09-02 16:02:51 -07:00
|
|
|
JSObject *wrappedObj = &holder->getSlot(JSSLOT_WN_OBJ).toObject();
|
|
|
|
OBJ_TO_INNER_OBJECT(cx, wrappedObj);
|
|
|
|
return wrappedObj;
|
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
|
|
|
{
|
2010-09-23 15:56:28 -07:00
|
|
|
NS_ASSERTION(wrapper->isProxy(), "bad this object in get");
|
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-09-02 16:02:51 -07:00
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(cx, holder);
|
2010-07-02 14:09:48 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNative(wnObject);
|
|
|
|
if (NATIVE_HAS_FLAG(wn, WantGetProperty)) {
|
2010-10-10 15:38:39 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, holder))
|
|
|
|
return false;
|
2010-07-02 14:09:48 -07:00
|
|
|
JSBool retval = true;
|
2010-09-23 15:56:28 -07:00
|
|
|
nsresult rv = wn->GetScriptableCallback()->GetProperty(wn, cx, wrapper, id, vp, &retval);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
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
|
2010-09-23 15:56:28 -07:00
|
|
|
holder_set(JSContext *cx, JSObject *wrapper, jsid id, jsval *vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-09-23 15:56:28 -07:00
|
|
|
NS_ASSERTION(wrapper->isProxy(), "bad this object in set");
|
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-10-10 15:39:23 -07:00
|
|
|
if (IsResolving(holder, id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(cx, holder);
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
XPCWrappedNative *wn = GetWrappedNative(wnObject);
|
|
|
|
if (NATIVE_HAS_FLAG(wn, WantSetProperty)) {
|
2010-10-10 15:38:39 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, holder))
|
|
|
|
return false;
|
2010-07-02 14:09:48 -07:00
|
|
|
JSBool retval = true;
|
2010-09-23 15:56:28 -07:00
|
|
|
nsresult rv = wn->GetScriptableCallback()->SetProperty(wn, cx, wrapper, id, vp, &retval);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
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");
|
2010-09-02 16:02:51 -07:00
|
|
|
JSObject *wnObject = GetWrappedNativeObjectFromHolder(cx, 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
|
|
|
|
|
|
|
// Run the resolve hook of the wrapped native.
|
2010-09-02 16:02:51 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantNewResolve)) {
|
2010-09-22 17:34:20 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, holder))
|
|
|
|
return false;
|
|
|
|
|
2010-09-02 16:02:51 -07:00
|
|
|
JSBool retval = true;
|
|
|
|
JSObject *pobj = NULL;
|
2010-09-24 15:30:00 -07:00
|
|
|
uintN flags = (set ? JSRESOLVE_ASSIGNING : 0) | JSRESOLVE_QUALIFIED;
|
2010-09-23 15:56:28 -07:00
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->NewResolve(wn, cx, wrapper, id,
|
|
|
|
flags, &pobj, &retval);
|
2010-09-02 16:02:51 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (retval) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
}
|
|
|
|
return false;
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
if (pobj)
|
2010-09-24 15:30:00 -07:00
|
|
|
return JS_GetPropertyDescriptorById(cx, pobj, id, flags, desc);
|
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;
|
|
|
|
desc->shortid = NULL;
|
|
|
|
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.
|
|
|
|
if (!member->NewFunctionObject(ccx, iface, wnObject, &fval)) {
|
|
|
|
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.
|
|
|
|
if (!member->NewFunctionObject(ccx, iface, wnObject, &desc->value)) {
|
|
|
|
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.
|
|
|
|
desc->getter = desc->setter = JS_PropertyStub;
|
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, holder))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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)
|
|
|
|
desc->setter = desc->getter;
|
|
|
|
|
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
|
|
|
|
holder_enumerate(JSContext *cx, JSObject *holder)
|
|
|
|
{
|
|
|
|
// Ask the native wrapper for all its ids
|
2010-10-10 15:38:39 -07:00
|
|
|
JSIdArray *ida;
|
|
|
|
{
|
|
|
|
JSObject *wrappednative = GetWrappedNativeObjectFromHolder(cx, holder);
|
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(cx, wrappednative))
|
|
|
|
return false;
|
|
|
|
ida = JS_Enumerate(cx, wrappednative);
|
|
|
|
}
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
if (!ida)
|
|
|
|
return false;
|
2010-09-02 16:02:51 -07:00
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *wrapper = &holder->getSlot(JSSLOT_PROXY_OBJ).toObject();
|
|
|
|
|
2010-09-02 16:02:51 -07:00
|
|
|
// Resolve the underlying native properties onto the holder object
|
2010-07-02 14:09:48 -07:00
|
|
|
jsid *idp = ida->vector;
|
|
|
|
size_t length = ida->length;
|
|
|
|
while (length-- > 0) {
|
|
|
|
JSPropertyDescriptor dummy;
|
2010-09-23 15:56:28 -07:00
|
|
|
if (!ResolveNativeProperty(cx, wrapper, holder, *idp++, false, &dummy))
|
2010-07-02 14:09:48 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:35:48 -07:00
|
|
|
extern CrossOriginWrapper XrayWrapperWaivedWrapper;
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
static JSBool
|
|
|
|
wrappedJSObject_getter(JSContext *cx, JSObject *holder, jsid id, jsval *vp)
|
|
|
|
{
|
2010-09-20 14:48:01 -07:00
|
|
|
if (holder->isWrapper())
|
2010-09-23 15:56:28 -07:00
|
|
|
holder = GetHolder(holder);
|
2010-09-03 19:24:23 -07:00
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
// If the caller intentionally waives the X-ray wrapper we usually
|
|
|
|
// apply for wrapped natives, use a special wrapper to make sure the
|
|
|
|
// membrane will not automatically apply an X-ray wrapper.
|
2010-09-02 16:02:51 -07:00
|
|
|
JSObject *wn = GetWrappedNativeObjectFromHolder(cx, holder);
|
2010-09-03 19:24:23 -07:00
|
|
|
|
|
|
|
// We have to make sure that if we're wrapping an outer window, that
|
|
|
|
// the .wrappedJSObject also wraps the outer window.
|
|
|
|
OBJ_TO_OUTER_OBJECT(cx, wn);
|
|
|
|
if (!wn)
|
|
|
|
return false;
|
|
|
|
JSObject *obj = JSWrapper::New(cx, wn, NULL, holder->getParent(), &XrayWrapperWaivedWrapper);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
*vp = OBJECT_TO_JSVAL(obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
JSObject *wrappednative = GetWrappedNativeObjectFromHolder(cx, holder);
|
|
|
|
XPCWrappedNative *wn = GetWrappedNative(wrappednative);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
2010-10-10 15:36:04 -07:00
|
|
|
XrayWrapper<Base, Policy>::XrayWrapper(uintN flags)
|
|
|
|
: Base(flags | WrapperFactory::IS_XRAY_WRAPPER_FLAG)
|
2010-09-02 16:02:51 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
|
|
|
XrayWrapper<Base, Policy>::~XrayWrapper()
|
2010-09-02 16:02:51 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-29 10:00:52 -07:00
|
|
|
template <typename Base, typename Policy>
|
|
|
|
class AutoLeaveHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoLeaveHelper(XrayWrapper<Base, Policy> &xray, JSContext *cx, JSObject *wrapper)
|
|
|
|
: xray(xray), cx(cx), wrapper(wrapper)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~AutoLeaveHelper()
|
|
|
|
{
|
|
|
|
xray.leave(cx, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
XrayWrapper<Base, Policy> &xray;
|
|
|
|
JSContext *cx;
|
|
|
|
JSObject *wrapper;
|
|
|
|
};
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2010-09-20 14:48:01 -07:00
|
|
|
XrayWrapper<Base, Policy>::getPropertyDescriptor(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);
|
|
|
|
|
|
|
|
if (id == nsXPConnect::GetRuntimeInstance()->GetStringID(XPCJSRuntime::IDX_WRAPPED_JSOBJECT)) {
|
2010-10-10 15:39:23 -07:00
|
|
|
if (!this->enter(cx, wrapper, id, set ? JSWrapper::SET : JSWrapper::GET))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AutoLeaveHelper<Base, Policy> helper(*this, cx, wrapper);
|
|
|
|
|
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
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
if (IsResolving(holder, id)) {
|
|
|
|
desc->obj = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-10 15:39:23 -07:00
|
|
|
if (!this->enter(cx, wrapper, id, set ? JSWrapper::SET : JSWrapper::GET))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AutoLeaveHelper<Base, Policy> helper(*this, cx, wrapper);
|
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
ResolvingId resolving(holder, id);
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
void *priv;
|
|
|
|
if (!Policy::enter(cx, wrapper, &id, set ? JSWrapper::SET : JSWrapper::GET, &priv))
|
2010-07-02 14:09:48 -07:00
|
|
|
return false;
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
bool ok = ResolveNativeProperty(cx, wrapper, holder, id, false, desc);
|
2010-09-20 14:48:01 -07:00
|
|
|
Policy::leave(cx, wrapper, priv);
|
2010-09-23 15:56:28 -07:00
|
|
|
if (!ok || desc->obj)
|
|
|
|
return ok;
|
|
|
|
|
2010-10-10 15:36:32 -07:00
|
|
|
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-09-23 15:56:28 -07:00
|
|
|
return JS_GetPropertyDescriptorById(cx, holder, id,
|
|
|
|
(set ? JSRESOLVE_ASSIGNING : 0) | JSRESOLVE_QUALIFIED,
|
|
|
|
desc);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2010-09-20 14:48:01 -07:00
|
|
|
XrayWrapper<Base, Policy>::getOwnPropertyDescriptor(JSContext *cx, JSObject *wrapper, jsid id,
|
|
|
|
bool set, PropertyDescriptor *desc)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-09-17 14:54:41 -07:00
|
|
|
return getPropertyDescriptor(cx, wrapper, id, set, desc);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::defineProperty(JSContext *cx, JSObject *wrapper, jsid id,
|
|
|
|
js::PropertyDescriptor *desc)
|
|
|
|
{
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
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
|
|
|
|
|
|
|
JSPropertyDescriptor *jsdesc = Jsvalify(desc);
|
|
|
|
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-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::getOwnPropertyNames(JSContext *cx, JSObject *wrapper,
|
|
|
|
js::AutoIdVector &props)
|
|
|
|
{
|
2010-10-10 15:37:22 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
return js::GetPropertyNames(cx, holder, JSITER_OWNONLY | JSITER_HIDDEN, &props);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2010-09-20 14:48:01 -07:00
|
|
|
XrayWrapper<Base, Policy>::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;
|
|
|
|
if (!JS_DeletePropertyById2(cx, holder, id, &v) || !JS_ValueToBoolean(cx, v, &b))
|
|
|
|
return false;
|
|
|
|
*bp = !!b;
|
2010-09-20 14:48:01 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::enumerate(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props)
|
|
|
|
{
|
2010-10-10 15:37:22 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
|
|
|
return js::GetPropertyNames(cx, holder, 0, &props);
|
2010-09-20 14:48:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::fix(JSContext *cx, JSObject *proxy, js::Value *vp)
|
|
|
|
{
|
|
|
|
vp->setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::get(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id,
|
|
|
|
js::Value *vp)
|
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
|
|
|
return JSProxyHandler::get(cx, wrapper, receiver, id, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::set(JSContext *cx, JSObject *wrapper, JSObject *receiver, jsid id,
|
|
|
|
js::Value *vp)
|
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
|
|
|
return JSProxyHandler::set(cx, wrapper, receiver, id, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::has(JSContext *cx, JSObject *wrapper, jsid id, bool *bp)
|
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
2010-07-02 14:09:48 -07:00
|
|
|
return JSProxyHandler::has(cx, wrapper, id, bp);
|
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
2010-07-02 14:09:48 -07:00
|
|
|
bool
|
2010-09-20 14:48:01 -07:00
|
|
|
XrayWrapper<Base, Policy>::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);
|
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::enumerateOwn(JSContext *cx, JSObject *wrapper, js::AutoIdVector &props)
|
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
|
|
|
return JSProxyHandler::enumerateOwn(cx, wrapper, props);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Policy>::iterate(JSContext *cx, JSObject *wrapper, uintN flags, js::Value *vp)
|
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already JSProxyHandler.
|
|
|
|
return JSProxyHandler::iterate(cx, wrapper, flags, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Policy>
|
2010-09-02 16:02:51 -07:00
|
|
|
JSObject *
|
2010-09-20 14:48:01 -07:00
|
|
|
XrayWrapper<Base, Policy>::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;
|
|
|
|
|
|
|
|
holder->setSlot(JSSLOT_WN_OBJ, ObjectValue(*wrappedNative));
|
2010-09-23 15:56:28 -07:00
|
|
|
holder->setSlot(JSSLOT_RESOLVING, PrivateValue(NULL));
|
2010-09-02 16:02:51 -07:00
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
bool
|
|
|
|
CrossCompartmentXray::enter(JSContext *cx, JSObject *wrapper, jsid *idp,
|
|
|
|
JSWrapper::Action act, void **priv)
|
|
|
|
{
|
|
|
|
JSObject *target = wrapper->unwrap();
|
|
|
|
JSCrossCompartmentCall *call = JS_EnterCrossCompartmentCall(cx, target);
|
|
|
|
if (!call)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*priv = call;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CrossCompartmentXray::leave(JSContext *cx, JSObject *wrapper, void *priv)
|
2010-09-02 16:02:51 -07:00
|
|
|
{
|
2010-09-20 14:48:01 -07:00
|
|
|
JS_LeaveCrossCompartmentCall(static_cast<JSCrossCompartmentCall *>(priv));
|
2010-09-02 16:02:51 -07:00
|
|
|
}
|
|
|
|
|
2010-09-20 14:48:01 -07:00
|
|
|
#define XPCNW XrayWrapper<JSCrossCompartmentWrapper, CrossCompartmentXray>
|
2010-09-29 10:00:52 -07:00
|
|
|
#define SCNW XrayWrapper<JSWrapper, SameCompartmentXray>
|
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
|
|
|
|
|
|
|
}
|