2014-04-03 04:58:00 -07:00
|
|
|
/* -*- 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
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
#include "XrayWrapper.h"
|
|
|
|
#include "AccessCheck.h"
|
|
|
|
#include "WrapperFactory.h"
|
|
|
|
|
2013-03-21 17:05:20 -07:00
|
|
|
#include "nsIContent.h"
|
2014-01-16 15:45:40 -08:00
|
|
|
#include "nsIControllers.h"
|
2014-06-04 11:11:05 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2012-10-05 09:59:23 -07:00
|
|
|
#include "nsContentUtils.h"
|
2011-09-21 18:30:27 -07:00
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
#include "XPCWrapper.h"
|
|
|
|
#include "xpcprivate.h"
|
|
|
|
|
2012-01-15 00:13:11 -08:00
|
|
|
#include "jsapi.h"
|
2013-08-17 15:50:18 -07:00
|
|
|
#include "jsprf.h"
|
2012-08-27 06:15:20 -07:00
|
|
|
#include "nsJSUtils.h"
|
2014-06-05 22:32:38 -07:00
|
|
|
#include "nsPrintfCString.h"
|
2012-01-11 00:23:08 -08:00
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2014-01-29 22:34:25 -08:00
|
|
|
#include "mozilla/dom/WindowBinding.h"
|
2013-01-17 09:30:37 -08:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
using namespace mozilla::dom;
|
2013-03-27 04:33:30 -07:00
|
|
|
using namespace JS;
|
2013-04-23 09:50:17 -07:00
|
|
|
using namespace mozilla;
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
using js::Wrapper;
|
2014-02-13 10:54:07 -08:00
|
|
|
using js::BaseProxyHandler;
|
2013-05-17 10:44:25 -07:00
|
|
|
using js::IsCrossCompartmentWrapper;
|
|
|
|
using js::UncheckedUnwrap;
|
|
|
|
using js::CheckedUnwrap;
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
namespace xpc {
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
using namespace XrayUtils;
|
|
|
|
|
2014-06-17 10:16:08 -07:00
|
|
|
inline bool
|
|
|
|
IsTypedArrayKey(JSProtoKey key)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool isTypedArraySlow = key == JSProto_Int8Array ||
|
|
|
|
key == JSProto_Uint8Array ||
|
|
|
|
key == JSProto_Int16Array ||
|
|
|
|
key == JSProto_Uint16Array ||
|
|
|
|
key == JSProto_Int32Array ||
|
|
|
|
key == JSProto_Uint32Array ||
|
|
|
|
key == JSProto_Float32Array ||
|
|
|
|
key == JSProto_Float64Array ||
|
|
|
|
key == JSProto_Uint8ClampedArray;
|
|
|
|
#endif
|
|
|
|
bool isTypedArray = key >= JSProto_Int8Array &&
|
|
|
|
key <= JSProto_Uint8ClampedArray;
|
|
|
|
MOZ_ASSERT(isTypedArray == isTypedArraySlow, "Somebody reordered jsprototypes.h!");
|
|
|
|
static_assert(JSProto_Uint8ClampedArray - JSProto_Int8Array == 8,
|
|
|
|
"New prototype added in typed array range");
|
|
|
|
return isTypedArray;
|
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:12 -07:00
|
|
|
// Whitelist for the standard ES classes we can Xray to.
|
|
|
|
static bool
|
|
|
|
IsJSXraySupported(JSProtoKey key)
|
|
|
|
{
|
2014-06-17 10:16:08 -07:00
|
|
|
if (IsTypedArrayKey(key))
|
|
|
|
return true;
|
2014-03-23 07:02:14 -07:00
|
|
|
switch (key) {
|
|
|
|
case JSProto_Date:
|
2014-06-05 22:32:39 -07:00
|
|
|
case JSProto_Object:
|
2014-06-11 15:16:07 -07:00
|
|
|
case JSProto_Array:
|
2014-06-19 09:57:06 -07:00
|
|
|
case JSProto_Function:
|
2014-03-23 07:02:14 -07:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-23 07:02:12 -07:00
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
XrayType
|
|
|
|
GetXrayType(JSObject *obj)
|
|
|
|
{
|
2013-04-11 11:50:18 -07:00
|
|
|
obj = js::UncheckedUnwrap(obj, /* stopAtOuter = */ false);
|
2012-10-09 11:50:27 -07:00
|
|
|
if (mozilla::dom::UseDOMXray(obj))
|
2012-10-05 09:59:23 -07:00
|
|
|
return XrayForDOMObject;
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
2013-04-19 12:58:30 -07:00
|
|
|
if (IS_WN_CLASS(clasp) || clasp->ext.innerObject)
|
2012-10-05 09:59:23 -07:00
|
|
|
return XrayForWrappedNative;
|
2013-04-19 12:57:18 -07:00
|
|
|
|
2014-03-23 07:02:12 -07:00
|
|
|
JSProtoKey standardProto = IdentifyStandardInstanceOrPrototype(obj);
|
|
|
|
if (IsJSXraySupported(standardProto))
|
|
|
|
return XrayForJSObject;
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
return NotXray;
|
|
|
|
}
|
|
|
|
|
2014-03-04 10:05:07 -08:00
|
|
|
JSObject *
|
|
|
|
XrayAwareCalleeGlobal(JSObject *fun)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::IsFunctionObject(fun));
|
|
|
|
JSObject *scope = js::GetObjectParent(fun);
|
|
|
|
if (IsXrayWrapper(scope))
|
|
|
|
scope = js::UncheckedUnwrap(scope);
|
|
|
|
return js::GetGlobalForObjectCrossCompartment(scope);
|
|
|
|
}
|
|
|
|
|
2014-02-21 15:55:31 -08:00
|
|
|
const uint32_t JSSLOT_RESOLVING = 0;
|
2013-04-24 06:22:21 -07:00
|
|
|
ResolvingId::ResolvingId(JSContext *cx, HandleObject wrapper, HandleId id)
|
|
|
|
: mId(id),
|
2013-04-24 02:45:41 -07:00
|
|
|
mHolder(cx, getHolderObject(wrapper)),
|
2012-10-05 09:59:23 -07:00
|
|
|
mPrev(getResolvingId(mHolder)),
|
|
|
|
mXrayShadowing(false)
|
|
|
|
{
|
|
|
|
js::SetReservedSlot(mHolder, JSSLOT_RESOLVING, js::PrivateValue(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolvingId::~ResolvingId()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(getResolvingId(mHolder) == this, "unbalanced ResolvingIds");
|
|
|
|
js::SetReservedSlot(mHolder, JSSLOT_RESOLVING, js::PrivateValue(mPrev));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ResolvingId::isXrayShadowing(jsid id)
|
|
|
|
{
|
|
|
|
if (!mXrayShadowing)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return mId == id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ResolvingId::isResolving(jsid id)
|
|
|
|
{
|
|
|
|
for (ResolvingId *cur = this; cur; cur = cur->mPrev) {
|
|
|
|
if (cur->mId == id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolvingId *
|
|
|
|
ResolvingId::getResolvingId(JSObject *holder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(strcmp(JS_GetClass(holder)->name, "NativePropertyHolder") == 0);
|
|
|
|
return (ResolvingId *)js::GetReservedSlot(holder, JSSLOT_RESOLVING).toPrivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
ResolvingId::getHolderObject(JSObject *wrapper)
|
|
|
|
{
|
|
|
|
return &js::GetProxyExtra(wrapper, 0).toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolvingId *
|
|
|
|
ResolvingId::getResolvingIdFromWrapper(JSObject *wrapper)
|
|
|
|
{
|
|
|
|
return getResolvingId(getHolderObject(wrapper));
|
|
|
|
}
|
|
|
|
|
2013-04-24 02:45:41 -07:00
|
|
|
class MOZ_STACK_CLASS ResolvingIdDummy
|
2012-10-05 09:59:23 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-04-24 06:22:21 -07:00
|
|
|
ResolvingIdDummy(JSContext *cx, HandleObject wrapper, HandleId id)
|
2012-10-05 09:59:23 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class XrayTraits
|
|
|
|
{
|
|
|
|
public:
|
2014-06-19 09:57:06 -07:00
|
|
|
XrayTraits() {}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
static JSObject* getTargetObject(JSObject *wrapper) {
|
2013-04-11 11:50:18 -07:00
|
|
|
return js::UncheckedUnwrap(wrapper, /* stopAtOuter = */ false);
|
2012-10-05 09:59:23 -07:00
|
|
|
}
|
|
|
|
|
2013-04-23 09:50:16 -07:00
|
|
|
virtual bool resolveNativeProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) = 0;
|
2013-04-23 09:50:17 -07:00
|
|
|
// NB: resolveOwnProperty may decide whether or not to cache what it finds
|
|
|
|
// on the holder. If the result is not cached, the lookup will happen afresh
|
|
|
|
// for each access, which is the right thing for things like dynamic NodeList
|
|
|
|
// properties.
|
2013-03-27 04:33:30 -07:00
|
|
|
virtual bool resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper,
|
|
|
|
HandleObject wrapper, HandleObject holder,
|
2014-04-25 14:11:02 -07:00
|
|
|
HandleId id, MutableHandle<JSPropertyDescriptor> desc);
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2014-06-05 22:32:39 -07:00
|
|
|
bool delete_(JSContext *cx, HandleObject wrapper, HandleId id, bool *bp) {
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
virtual void preserveWrapper(JSObject *target) = 0;
|
|
|
|
|
2014-04-25 13:07:18 -07:00
|
|
|
static bool set(JSContext *cx, HandleObject wrapper, HandleObject receiver, HandleId id,
|
|
|
|
bool strict, MutableHandleValue vp);
|
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
JSObject* getExpandoObject(JSContext *cx, HandleObject target,
|
|
|
|
HandleObject consumer);
|
|
|
|
JSObject* ensureExpandoObject(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject target);
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
JSObject* getHolder(JSObject *wrapper);
|
2013-03-27 04:33:30 -07:00
|
|
|
JSObject* ensureHolder(JSContext *cx, HandleObject wrapper);
|
2012-10-05 09:59:23 -07:00
|
|
|
virtual JSObject* createHolder(JSContext *cx, JSObject *wrapper) = 0;
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2014-02-21 11:35:48 -08:00
|
|
|
JSObject* getExpandoChain(JSObject *obj) {
|
|
|
|
return GetObjectScope(obj)->GetExpandoChain(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setExpandoChain(JSContext *cx, HandleObject obj, HandleObject chain) {
|
|
|
|
return GetObjectScope(obj)->SetExpandoChain(cx, obj, chain);
|
|
|
|
}
|
2013-03-27 04:33:30 -07:00
|
|
|
bool cloneExpandoChain(JSContext *cx, HandleObject dst, HandleObject src);
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
private:
|
2013-03-27 04:33:30 -07:00
|
|
|
bool expandoObjectMatchesConsumer(JSContext *cx, HandleObject expandoObject,
|
2012-10-05 09:59:23 -07:00
|
|
|
nsIPrincipal *consumerOrigin,
|
2013-03-27 04:33:30 -07:00
|
|
|
HandleObject exclusiveGlobal);
|
|
|
|
JSObject* getExpandoObjectInternal(JSContext *cx, HandleObject target,
|
2012-10-05 09:59:23 -07:00
|
|
|
nsIPrincipal *origin,
|
|
|
|
JSObject *exclusiveGlobal);
|
2013-03-27 04:33:30 -07:00
|
|
|
JSObject* attachExpandoObject(JSContext *cx, HandleObject target,
|
2012-10-05 09:59:23 -07:00
|
|
|
nsIPrincipal *origin,
|
2013-03-27 04:33:30 -07:00
|
|
|
HandleObject exclusiveGlobal);
|
2014-06-19 09:57:06 -07:00
|
|
|
|
|
|
|
XrayTraits(XrayTraits &) MOZ_DELETE;
|
|
|
|
const XrayTraits& operator=(XrayTraits &) MOZ_DELETE;
|
2012-10-05 09:59:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class XPCWrappedNativeXrayTraits : public XrayTraits
|
|
|
|
{
|
|
|
|
public:
|
2014-02-21 15:55:30 -08:00
|
|
|
enum {
|
|
|
|
HasPrototype = 0
|
|
|
|
};
|
|
|
|
|
2013-03-14 22:38:26 -07:00
|
|
|
static const XrayType Type = XrayForWrappedNative;
|
|
|
|
|
2013-04-23 09:50:16 -07:00
|
|
|
virtual bool resolveNativeProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
|
2013-03-27 04:33:30 -07:00
|
|
|
virtual bool resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
|
2014-06-05 22:32:39 -07:00
|
|
|
bool defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
Handle<JSPropertyDescriptor> existingDesc, bool *defined);
|
2014-03-23 07:02:13 -07:00
|
|
|
virtual bool enumerateNames(JSContext *cx, HandleObject wrapper, unsigned flags,
|
|
|
|
AutoIdVector &props);
|
2013-04-25 16:03:05 -07:00
|
|
|
static bool call(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance);
|
|
|
|
static bool construct(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance);
|
2012-10-05 09:59:23 -07:00
|
|
|
|
|
|
|
static bool isResolving(JSContext *cx, JSObject *holder, jsid id);
|
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
static bool resolveDOMCollectionProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc);
|
2012-10-05 09:59:23 -07:00
|
|
|
|
|
|
|
static XPCWrappedNative* getWN(JSObject *wrapper) {
|
2013-06-23 00:15:55 -07:00
|
|
|
return XPCWrappedNative::Get(getTargetObject(wrapper));
|
2012-10-05 09:59:23 -07:00
|
|
|
}
|
|
|
|
|
2014-04-25 14:11:02 -07:00
|
|
|
virtual void preserveWrapper(JSObject *target) MOZ_OVERRIDE;
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
typedef ResolvingId ResolvingIdImpl;
|
|
|
|
|
2014-04-25 14:11:02 -07:00
|
|
|
virtual JSObject* createHolder(JSContext *cx, JSObject *wrapper) MOZ_OVERRIDE;
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2014-02-21 15:55:31 -08:00
|
|
|
static const JSClass HolderClass;
|
2012-10-05 09:59:23 -07:00
|
|
|
static XPCWrappedNativeXrayTraits singleton;
|
|
|
|
};
|
|
|
|
|
2014-02-21 15:55:31 -08:00
|
|
|
const JSClass XPCWrappedNativeXrayTraits::HolderClass = {
|
|
|
|
"NativePropertyHolder", JSCLASS_HAS_RESERVED_SLOTS(2),
|
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, holder_get, holder_set,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub
|
|
|
|
};
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
class DOMXrayTraits : public XrayTraits
|
|
|
|
{
|
|
|
|
public:
|
2014-02-21 15:55:30 -08:00
|
|
|
enum {
|
|
|
|
HasPrototype = 0
|
|
|
|
};
|
|
|
|
|
2013-03-14 22:38:26 -07:00
|
|
|
static const XrayType Type = XrayForDOMObject;
|
|
|
|
|
2013-04-23 09:50:16 -07:00
|
|
|
virtual bool resolveNativeProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
|
2013-03-27 04:33:30 -07:00
|
|
|
virtual bool resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
|
2014-06-05 22:32:39 -07:00
|
|
|
bool defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
Handle<JSPropertyDescriptor> existingDesc, bool *defined);
|
2014-04-25 13:07:18 -07:00
|
|
|
static bool set(JSContext *cx, HandleObject wrapper, HandleObject receiver, HandleId id,
|
|
|
|
bool strict, MutableHandleValue vp);
|
2014-03-23 07:02:13 -07:00
|
|
|
virtual bool enumerateNames(JSContext *cx, HandleObject wrapper, unsigned flags,
|
|
|
|
AutoIdVector &props);
|
2013-04-25 16:03:05 -07:00
|
|
|
static bool call(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance);
|
|
|
|
static bool construct(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance);
|
2012-10-05 09:59:23 -07:00
|
|
|
|
|
|
|
static bool isResolving(JSContext *cx, JSObject *holder, jsid id)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef ResolvingIdDummy ResolvingIdImpl;
|
|
|
|
|
2014-04-25 14:11:02 -07:00
|
|
|
virtual void preserveWrapper(JSObject *target) MOZ_OVERRIDE;
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2014-04-25 14:11:02 -07:00
|
|
|
virtual JSObject* createHolder(JSContext *cx, JSObject *wrapper) MOZ_OVERRIDE;
|
2012-10-05 09:59:23 -07:00
|
|
|
|
|
|
|
static DOMXrayTraits singleton;
|
|
|
|
};
|
|
|
|
|
2014-03-23 07:02:12 -07:00
|
|
|
class JSXrayTraits : public XrayTraits
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
HasPrototype = 1
|
|
|
|
};
|
|
|
|
static const XrayType Type = XrayForJSObject;
|
|
|
|
|
|
|
|
virtual bool resolveNativeProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE
|
2014-03-23 07:02:12 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSUME_UNREACHABLE("resolveNativeProperty hook should never be called with HasPrototype = 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE;
|
2014-03-23 07:02:12 -07:00
|
|
|
|
2014-06-05 22:32:39 -07:00
|
|
|
bool delete_(JSContext *cx, HandleObject wrapper, HandleId id, bool *bp);
|
|
|
|
|
2014-06-05 22:32:39 -07:00
|
|
|
bool defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
Handle<JSPropertyDescriptor> existingDesc, bool *defined);
|
2014-03-23 07:02:12 -07:00
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
virtual bool enumerateNames(JSContext *cx, HandleObject wrapper, unsigned flags,
|
2014-03-23 07:02:13 -07:00
|
|
|
AutoIdVector &props);
|
2014-03-23 07:02:12 -07:00
|
|
|
|
|
|
|
static bool call(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance)
|
|
|
|
{
|
2014-06-19 09:57:06 -07:00
|
|
|
JSXrayTraits &self = JSXrayTraits::singleton;
|
|
|
|
RootedObject holder(cx, self.ensureHolder(cx, wrapper));
|
|
|
|
if (self.getProtoKey(holder) == JSProto_Function)
|
|
|
|
return baseInstance.call(cx, wrapper, args);
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
RootedValue v(cx, ObjectValue(*wrapper));
|
|
|
|
js_ReportIsNotFunction(cx, v);
|
|
|
|
return false;
|
2014-03-23 07:02:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool construct(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance)
|
|
|
|
{
|
2014-06-19 09:57:06 -07:00
|
|
|
JSXrayTraits &self = JSXrayTraits::singleton;
|
|
|
|
RootedObject holder(cx, self.ensureHolder(cx, wrapper));
|
|
|
|
if (self.getProtoKey(holder) == JSProto_Function)
|
|
|
|
return baseInstance.construct(cx, wrapper, args);
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
RootedValue v(cx, ObjectValue(*wrapper));
|
|
|
|
js_ReportIsNotFunction(cx, v);
|
|
|
|
return false;
|
2014-03-23 07:02:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isResolving(JSContext *cx, JSObject *holder, jsid id)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef ResolvingIdDummy ResolvingIdImpl;
|
|
|
|
|
|
|
|
bool getPrototypeOf(JSContext *cx, JS::HandleObject wrapper,
|
|
|
|
JS::HandleObject target,
|
|
|
|
JS::MutableHandleObject protop)
|
|
|
|
{
|
2014-03-23 07:02:13 -07:00
|
|
|
RootedObject holder(cx, ensureHolder(cx, wrapper));
|
2014-06-05 22:32:38 -07:00
|
|
|
JSProtoKey key = getProtoKey(holder);
|
|
|
|
if (isPrototype(holder)) {
|
|
|
|
if (key == JSProto_Object) {
|
|
|
|
protop.set(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
key = JSProto_Object;
|
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
if (!JS_GetClassPrototype(cx, key, protop))
|
2014-06-06 12:31:30 -07:00
|
|
|
return false;
|
2014-03-23 07:02:13 -07:00
|
|
|
}
|
|
|
|
return JS_WrapObject(cx, protop);
|
2014-03-23 07:02:12 -07:00
|
|
|
}
|
|
|
|
|
2014-04-25 14:11:02 -07:00
|
|
|
virtual void preserveWrapper(JSObject *target) MOZ_OVERRIDE {
|
2014-03-23 07:02:13 -07:00
|
|
|
// In the case of pure JS objects, there is no underlying object, and
|
|
|
|
// the target is the canonical representation of state. If it gets
|
|
|
|
// collected, then expandos and such should be collected too. So there's
|
|
|
|
// nothing to do here.
|
2014-03-23 07:02:12 -07:00
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
enum {
|
|
|
|
SLOT_PROTOKEY = 0,
|
|
|
|
SLOT_ISPROTOTYPE,
|
|
|
|
SLOT_COUNT
|
|
|
|
};
|
2014-04-25 14:11:02 -07:00
|
|
|
virtual JSObject* createHolder(JSContext *cx, JSObject *wrapper) MOZ_OVERRIDE;
|
2014-03-23 07:02:13 -07:00
|
|
|
|
|
|
|
static JSProtoKey getProtoKey(JSObject *holder) {
|
|
|
|
int32_t key = js::GetReservedSlot(holder, SLOT_PROTOKEY).toInt32();
|
|
|
|
return static_cast<JSProtoKey>(key);
|
2014-03-23 07:02:12 -07:00
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
static bool isPrototype(JSObject *holder) {
|
|
|
|
return js::GetReservedSlot(holder, SLOT_ISPROTOTYPE).toBoolean();
|
|
|
|
}
|
|
|
|
|
2014-06-05 22:32:38 -07:00
|
|
|
static bool getOwnPropertyFromTargetIfSafe(JSContext *cx,
|
|
|
|
HandleObject target,
|
|
|
|
HandleObject wrapper,
|
|
|
|
HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc);
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
static const JSClass HolderClass;
|
2014-03-23 07:02:12 -07:00
|
|
|
static JSXrayTraits singleton;
|
|
|
|
};
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
const JSClass JSXrayTraits::HolderClass = {
|
|
|
|
"JSXrayHolder", JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
|
|
|
|
JS_PropertyStub, JS_DeletePropertyStub,
|
|
|
|
JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub
|
|
|
|
};
|
|
|
|
|
2014-06-05 22:32:38 -07:00
|
|
|
inline bool
|
|
|
|
SilentFailure(JSContext *cx, HandleId id, const char *reason)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2014-06-10 20:15:56 -07:00
|
|
|
nsDependentJSString name;
|
|
|
|
if (!name.init(cx, id))
|
|
|
|
return false;
|
2014-06-05 22:32:38 -07:00
|
|
|
AutoFilename filename;
|
|
|
|
unsigned line = 0;
|
|
|
|
DescribeScriptedCaller(cx, &filename, &line);
|
|
|
|
NS_WARNING(nsPrintfCString("Denied access to property |%s| on Xrayed Object: %s (@%s:%u)",
|
|
|
|
NS_LossyConvertUTF16toASCII(name).get(), reason,
|
|
|
|
filename.get(), line).get());
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSXrayTraits::getOwnPropertyFromTargetIfSafe(JSContext *cx,
|
|
|
|
HandleObject target,
|
|
|
|
HandleObject wrapper,
|
|
|
|
HandleId idArg,
|
|
|
|
MutableHandle<JSPropertyDescriptor> outDesc)
|
|
|
|
{
|
|
|
|
// Note - This function operates in the target compartment, because it
|
|
|
|
// avoids a bunch of back-and-forth wrapping in enumerateNames.
|
|
|
|
MOZ_ASSERT(getTargetObject(wrapper) == target);
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(target, cx));
|
|
|
|
MOZ_ASSERT(WrapperFactory::IsXrayWrapper(wrapper));
|
|
|
|
MOZ_ASSERT(outDesc.object() == nullptr);
|
|
|
|
|
|
|
|
RootedId id(cx, idArg);
|
|
|
|
if (!JS_WrapId(cx, &id))
|
|
|
|
return false;
|
|
|
|
Rooted<JSPropertyDescriptor> desc(cx);
|
|
|
|
if (!JS_GetOwnPropertyDescriptorById(cx, target, id, &desc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the property doesn't exist at all, we're done.
|
|
|
|
if (!desc.object())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Disallow accessor properties.
|
|
|
|
if (desc.hasGetterOrSetter())
|
|
|
|
return SilentFailure(cx, id, "Property has accessor");
|
|
|
|
|
|
|
|
// Apply extra scrutiny to objects.
|
|
|
|
if (desc.value().isObject()) {
|
|
|
|
RootedObject propObj(cx, js::UncheckedUnwrap(&desc.value().toObject()));
|
|
|
|
JSAutoCompartment ac(cx, propObj);
|
|
|
|
|
|
|
|
// Disallow non-subsumed objects.
|
|
|
|
if (!AccessCheck::subsumes(target, propObj))
|
|
|
|
return SilentFailure(cx, id, "Value not same-origin with target");
|
|
|
|
|
|
|
|
// Disallow non-Xrayable objects.
|
2014-06-17 10:16:08 -07:00
|
|
|
if (GetXrayType(propObj) == NotXray)
|
|
|
|
return SilentFailure(cx, id, "Value not Xrayable");
|
2014-06-05 22:32:38 -07:00
|
|
|
|
|
|
|
// Disallow callables.
|
|
|
|
if (JS_ObjectIsCallable(cx, propObj))
|
|
|
|
return SilentFailure(cx, id, "Value is callable");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disallow any property that shadows something on its (Xrayed)
|
|
|
|
// prototype chain.
|
|
|
|
JSAutoCompartment ac2(cx, wrapper);
|
|
|
|
RootedObject proto(cx);
|
|
|
|
bool foundOnProto = false;
|
|
|
|
if (!JS_GetPrototype(cx, wrapper, &proto) ||
|
|
|
|
(proto && !JS_HasPropertyById(cx, proto, id, &foundOnProto)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (foundOnProto)
|
|
|
|
return SilentFailure(cx, id, "Value shadows a property on the standard prototype");
|
|
|
|
|
|
|
|
// We made it! Assign over the descriptor, and don't forget to wrap.
|
|
|
|
outDesc.assign(desc.get());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
bool
|
|
|
|
JSXrayTraits::resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper,
|
|
|
|
HandleObject wrapper, HandleObject holder,
|
|
|
|
HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2014-03-23 07:02:13 -07:00
|
|
|
{
|
|
|
|
// Call the common code.
|
|
|
|
bool ok = XrayTraits::resolveOwnProperty(cx, jsWrapper, wrapper, holder,
|
2014-04-25 14:11:02 -07:00
|
|
|
id, desc);
|
2014-03-23 07:02:13 -07:00
|
|
|
if (!ok || desc.object())
|
|
|
|
return ok;
|
|
|
|
|
2014-06-05 22:32:38 -07:00
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
|
|
|
if (!isPrototype(holder)) {
|
2014-06-17 10:16:08 -07:00
|
|
|
JSProtoKey key = getProtoKey(holder);
|
2014-06-11 15:16:07 -07:00
|
|
|
// For Object and Array instances, we expose some properties from the
|
|
|
|
// underlying object, but only after filtering them carefully.
|
|
|
|
//
|
|
|
|
// Note that, as far as JS observables go, Arrays are just Objects with
|
|
|
|
// a different prototype and a magic (own, non-configurable) |.length| that
|
|
|
|
// serves as a non-tight upper bound on |own| indexed properties. So while
|
|
|
|
// it's tempting to try to impose some sort of structure on what Arrays
|
|
|
|
// "should" look like over Xrays, the underlying object is squishy enough
|
|
|
|
// that it makes sense to just treat them like Objects for Xray purposes.
|
2014-06-17 10:16:08 -07:00
|
|
|
if (key == JSProto_Object || key == JSProto_Array) {
|
2014-06-05 22:32:38 -07:00
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
if (!getOwnPropertyFromTargetIfSafe(cx, target, wrapper, id, desc))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
2014-06-17 10:16:08 -07:00
|
|
|
} else if (IsTypedArrayKey(key)) {
|
|
|
|
if (IsArrayIndex(GetArrayIndexFromId(cx, id))) {
|
|
|
|
JS_ReportError(cx, "Accessing TypedArray data over Xrays is slow, and forbidden "
|
|
|
|
"in order to encourage performant code. To copy TypedArrays "
|
|
|
|
"across origin boundaries, consider using Components.utils.cloneInto().");
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-05 22:32:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The rest of this function applies only to prototypes.
|
2014-03-23 07:02:13 -07:00
|
|
|
return true;
|
2014-06-05 22:32:38 -07:00
|
|
|
}
|
2014-03-23 07:02:13 -07:00
|
|
|
|
|
|
|
// The non-HasPrototypes semantics implemented by traditional Xrays are kind
|
|
|
|
// of broken with respect to |own|-ness and the holder. The common code
|
|
|
|
// muddles through by only checking the holder for non-|own| lookups, but
|
|
|
|
// that doesn't work for us. So we do an explicit holder check here, and hope
|
|
|
|
// that this mess gets fixed up soon.
|
2014-04-25 14:11:01 -07:00
|
|
|
if (!JS_GetPropertyDescriptorById(cx, holder, id, desc))
|
2014-03-23 07:02:13 -07:00
|
|
|
return false;
|
|
|
|
if (desc.object()) {
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the JSClass. We require all Xrayable classes to have a ClassSpec.
|
|
|
|
const js::Class *clasp = js::GetObjectClass(target);
|
|
|
|
JSProtoKey protoKey = JSCLASS_CACHED_PROTO_KEY(clasp);
|
|
|
|
MOZ_ASSERT(protoKey == getProtoKey(holder));
|
|
|
|
MOZ_ASSERT(clasp->spec.defined());
|
|
|
|
|
|
|
|
// Handle the 'constructor' property.
|
|
|
|
if (id == GetRTIdByIndex(cx, XPCJSRuntime::IDX_CONSTRUCTOR)) {
|
|
|
|
RootedObject constructor(cx);
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
if (!JS_GetClassObject(cx, protoKey, &constructor))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!JS_WrapObject(cx, &constructor))
|
|
|
|
return false;
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setAttributes(0);
|
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
|
|
|
desc.value().setObject(*constructor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the property name we're looking for. We'll handle indexed
|
|
|
|
// properties when we start supporting arrays.
|
|
|
|
if (!JSID_IS_STRING(id))
|
|
|
|
return true;
|
|
|
|
Rooted<JSFlatString*> str(cx, JSID_TO_FLAT_STRING(id));
|
|
|
|
|
2014-05-23 16:53:03 -07:00
|
|
|
// Scan through the functions.
|
|
|
|
const JSFunctionSpec *fsMatch = nullptr;
|
|
|
|
for (const JSFunctionSpec *fs = clasp->spec.prototypeFunctions; fs && fs->name; ++fs) {
|
|
|
|
if (JS_FlatStringEqualsAscii(str, fs->name)) {
|
|
|
|
fsMatch = fs;
|
2014-03-23 07:02:13 -07:00
|
|
|
break;
|
2014-05-23 16:53:03 -07:00
|
|
|
}
|
2014-03-23 07:02:13 -07:00
|
|
|
}
|
2014-05-23 16:53:03 -07:00
|
|
|
if (fsMatch) {
|
|
|
|
// Generate an Xrayed version of the method.
|
2014-06-02 13:55:20 -07:00
|
|
|
RootedFunction fun(cx);
|
|
|
|
if (fsMatch->selfHostedName) {
|
|
|
|
fun = JS::GetSelfHostedFunction(cx, fsMatch->selfHostedName, id, fsMatch->nargs);
|
|
|
|
} else {
|
|
|
|
fun = JS_NewFunctionById(cx, fsMatch->call.op, fsMatch->nargs,
|
|
|
|
0, wrapper, id);
|
|
|
|
}
|
2014-05-23 16:53:03 -07:00
|
|
|
if (!fun)
|
|
|
|
return false;
|
2014-03-23 07:02:13 -07:00
|
|
|
|
2014-05-23 16:53:03 -07:00
|
|
|
// The generic Xray machinery only defines non-own properties on the holder.
|
|
|
|
// This is broken, and will be fixed at some point, but for now we need to
|
|
|
|
// cache the value explicitly. See the corresponding call to
|
|
|
|
// JS_GetPropertyById at the top of this function.
|
|
|
|
RootedObject funObj(cx, JS_GetFunctionObject(fun));
|
|
|
|
return JS_DefinePropertyById(cx, holder, id, funObj, 0) &&
|
|
|
|
JS_GetPropertyDescriptorById(cx, holder, id, desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan through the properties.
|
|
|
|
const JSPropertySpec *psMatch = nullptr;
|
|
|
|
for (const JSPropertySpec *ps = clasp->spec.prototypeProperties; ps && ps->name; ++ps) {
|
|
|
|
if (JS_FlatStringEqualsAscii(str, ps->name)) {
|
|
|
|
psMatch = ps;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (psMatch) {
|
2014-06-02 13:55:20 -07:00
|
|
|
desc.value().setUndefined();
|
|
|
|
// Note that this is also kind of an abuse of JSPROP_NATIVE_ACCESSORS.
|
|
|
|
// See bug 992977.
|
|
|
|
RootedFunction getterObj(cx);
|
|
|
|
RootedFunction setterObj(cx);
|
|
|
|
unsigned flags = psMatch->flags;
|
|
|
|
if (flags & JSPROP_NATIVE_ACCESSORS) {
|
|
|
|
desc.setGetter(psMatch->getter.propertyOp.op);
|
|
|
|
desc.setSetter(psMatch->setter.propertyOp.op);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(flags & JSPROP_GETTER);
|
|
|
|
getterObj = JS::GetSelfHostedFunction(cx, psMatch->getter.selfHosted.funname, id, 0);
|
|
|
|
if (!getterObj)
|
|
|
|
return false;
|
|
|
|
desc.setGetterObject(JS_GetFunctionObject(getterObj));
|
|
|
|
if (psMatch->setter.selfHosted.funname) {
|
|
|
|
MOZ_ASSERT(flags & JSPROP_SETTER);
|
|
|
|
setterObj = JS::GetSelfHostedFunction(cx, psMatch->setter.selfHosted.funname, id, 0);
|
|
|
|
if (!setterObj)
|
|
|
|
return false;
|
|
|
|
desc.setSetterObject(JS_GetFunctionObject(setterObj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
desc.setAttributes(flags);
|
|
|
|
|
2014-05-23 16:53:03 -07:00
|
|
|
// The generic Xray machinery only defines non-own properties on the holder.
|
|
|
|
// This is broken, and will be fixed at some point, but for now we need to
|
|
|
|
// cache the value explicitly. See the corresponding call to
|
|
|
|
// JS_GetPropertyById at the top of this function.
|
|
|
|
//
|
|
|
|
// Note also that the public-facing API here doesn't give us a way to
|
|
|
|
// pass along JITInfo. It's probably ok though, since Xrays are already
|
|
|
|
// pretty slow.
|
|
|
|
return JS_DefinePropertyById(cx, holder, id,
|
2014-06-02 13:55:20 -07:00
|
|
|
UndefinedHandleValue, desc.attributes(),
|
|
|
|
desc.getter(), desc.setter()) &&
|
2014-05-23 16:53:03 -07:00
|
|
|
JS_GetPropertyDescriptorById(cx, holder, id, desc);
|
|
|
|
}
|
2014-03-23 07:02:13 -07:00
|
|
|
|
2014-05-23 16:53:03 -07:00
|
|
|
return true;
|
2014-03-23 07:02:13 -07:00
|
|
|
}
|
|
|
|
|
2014-06-05 22:32:39 -07:00
|
|
|
bool
|
|
|
|
JSXrayTraits::delete_(JSContext *cx, HandleObject wrapper, HandleId id, bool *bp)
|
|
|
|
{
|
|
|
|
RootedObject holder(cx, ensureHolder(cx, wrapper));
|
|
|
|
|
|
|
|
// If we're using Object Xrays, we allow callers to attempt to delete any
|
|
|
|
// property from the underlying object that they are able to resolve. Note
|
|
|
|
// that this deleting may fail if the property is non-configurable.
|
2014-06-11 15:16:07 -07:00
|
|
|
JSProtoKey key = getProtoKey(holder);
|
|
|
|
bool isObjectOrArrayInstance = (key == JSProto_Object || key == JSProto_Array) &&
|
|
|
|
!isPrototype(holder);
|
|
|
|
if (isObjectOrArrayInstance) {
|
2014-06-05 22:32:39 -07:00
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
Rooted<JSPropertyDescriptor> desc(cx);
|
|
|
|
if (!getOwnPropertyFromTargetIfSafe(cx, target, wrapper, id, &desc))
|
|
|
|
return false;
|
|
|
|
if (desc.object())
|
|
|
|
return JS_DeletePropertyById2(cx, target, id, bp);
|
|
|
|
}
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-05 22:32:39 -07:00
|
|
|
bool
|
|
|
|
JSXrayTraits::defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
Handle<JSPropertyDescriptor> existingDesc,
|
|
|
|
bool *defined)
|
|
|
|
{
|
|
|
|
*defined = false;
|
|
|
|
RootedObject holder(cx, ensureHolder(cx, wrapper));
|
|
|
|
if (!holder)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2014-06-11 15:16:07 -07:00
|
|
|
// Object and Array instances are special. For those cases, we forward property
|
2014-06-05 22:32:39 -07:00
|
|
|
// definitions to the underlying object if the following conditions are met:
|
|
|
|
// * The property being defined is a value-prop.
|
|
|
|
// * The property being defined is either a primitive or subsumed by the target.
|
|
|
|
// * As seen from the Xray, any existing property that we would overwrite is an
|
|
|
|
// |own| value-prop.
|
|
|
|
//
|
2014-06-11 15:16:07 -07:00
|
|
|
// To avoid confusion, we disallow expandos on Object and Array instances, and
|
2014-06-05 22:32:39 -07:00
|
|
|
// therefore raise an exception here if the above conditions aren't met.
|
2014-06-11 15:16:07 -07:00
|
|
|
JSProtoKey key = getProtoKey(holder);
|
|
|
|
bool isObjectOrArrayInstance = (key == JSProto_Object || key == JSProto_Array) &&
|
|
|
|
!isPrototype(holder);
|
|
|
|
if (isObjectOrArrayInstance) {
|
2014-06-05 22:32:39 -07:00
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
|
|
|
if (desc.hasGetterOrSetter()) {
|
2014-06-11 15:16:07 -07:00
|
|
|
JS_ReportError(cx, "Not allowed to define accessor property on [Object] or [Array] XrayWrapper");
|
2014-06-05 22:32:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (desc.value().isObject() &&
|
|
|
|
!AccessCheck::subsumes(target, js::UncheckedUnwrap(&desc.value().toObject())))
|
|
|
|
{
|
2014-06-11 15:16:07 -07:00
|
|
|
JS_ReportError(cx, "Not allowed to define cross-origin object as property on [Object] or [Array] XrayWrapper");
|
2014-06-05 22:32:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (existingDesc.hasGetterOrSetter()) {
|
2014-06-11 15:16:07 -07:00
|
|
|
JS_ReportError(cx, "Not allowed to overwrite accessor property on [Object] or [Array] XrayWrapper");
|
2014-06-05 22:32:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (existingDesc.object() && existingDesc.object() != wrapper) {
|
2014-06-11 15:16:07 -07:00
|
|
|
JS_ReportError(cx, "Not allowed to shadow non-own Xray-resolved property on [Object] or [Array] XrayWrapper");
|
2014-06-05 22:32:39 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
if (!JS_WrapPropertyDescriptor(cx, desc) ||
|
|
|
|
!JS_DefinePropertyById(cx, target, id, desc.value(), desc.attributes(),
|
|
|
|
JS_PropertyStub, JS_StrictPropertyStub))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*defined = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
bool
|
|
|
|
JSXrayTraits::enumerateNames(JSContext *cx, HandleObject wrapper, unsigned flags,
|
|
|
|
AutoIdVector &props)
|
|
|
|
{
|
2014-06-05 22:32:38 -07:00
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
2014-03-23 07:02:13 -07:00
|
|
|
RootedObject holder(cx, ensureHolder(cx, wrapper));
|
|
|
|
if (!holder)
|
|
|
|
return false;
|
|
|
|
|
2014-06-05 22:32:38 -07:00
|
|
|
if (!isPrototype(holder)) {
|
2014-06-17 10:16:08 -07:00
|
|
|
JSProtoKey key = getProtoKey(holder);
|
2014-06-11 15:16:07 -07:00
|
|
|
// For Object and Array instances, we expose some properties from the underlying
|
2014-06-05 22:32:38 -07:00
|
|
|
// object, but only after filtering them carefully.
|
2014-06-17 10:16:08 -07:00
|
|
|
if (key == JSProto_Object || key == JSProto_Array) {
|
2014-06-05 22:32:38 -07:00
|
|
|
MOZ_ASSERT(props.empty());
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
AutoIdVector targetProps(cx);
|
|
|
|
if (!js::GetPropertyNames(cx, target, flags | JSITER_OWNONLY, &targetProps))
|
|
|
|
return false;
|
|
|
|
// Loop over the properties, and only pass along the ones that
|
|
|
|
// we determine to be safe.
|
2014-06-17 10:16:08 -07:00
|
|
|
if (!props.reserve(targetProps.length()))
|
|
|
|
return false;
|
2014-06-05 22:32:38 -07:00
|
|
|
for (size_t i = 0; i < targetProps.length(); ++i) {
|
|
|
|
Rooted<JSPropertyDescriptor> desc(cx);
|
|
|
|
RootedId id(cx, targetProps[i]);
|
|
|
|
if (!getOwnPropertyFromTargetIfSafe(cx, target, wrapper, id, &desc))
|
|
|
|
return false;
|
|
|
|
if (desc.object())
|
2014-06-17 10:16:08 -07:00
|
|
|
props.infallibleAppend(id);
|
2014-06-05 22:32:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_WrapAutoIdVector(cx, props);
|
2014-06-17 10:16:08 -07:00
|
|
|
} else if (IsTypedArrayKey(key)) {
|
|
|
|
uint32_t length = JS_GetTypedArrayLength(target);
|
|
|
|
// TypedArrays enumerate every indexed property in range, but
|
|
|
|
// |length| is a getter that lives on the proto, like it should be.
|
|
|
|
if (!props.reserve(length))
|
|
|
|
return false;
|
|
|
|
for (int32_t i = 0; i <= int32_t(length - 1); ++i)
|
|
|
|
props.infallibleAppend(INT_TO_JSID(i));
|
2014-06-05 22:32:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The rest of this function applies only to prototypes.
|
2014-03-23 07:02:13 -07:00
|
|
|
return true;
|
2014-06-05 22:32:38 -07:00
|
|
|
}
|
2014-03-23 07:02:13 -07:00
|
|
|
|
|
|
|
// Grab the JSClass. We require all Xrayable classes to have a ClassSpec.
|
|
|
|
const js::Class *clasp = js::GetObjectClass(target);
|
|
|
|
MOZ_ASSERT(JSCLASS_CACHED_PROTO_KEY(clasp) == getProtoKey(holder));
|
|
|
|
MOZ_ASSERT(clasp->spec.defined());
|
|
|
|
|
|
|
|
// Intern all the strings, and pass theme to the caller.
|
2014-05-23 16:53:03 -07:00
|
|
|
for (const JSFunctionSpec *fs = clasp->spec.prototypeFunctions; fs && fs->name; ++fs) {
|
2014-03-23 07:02:13 -07:00
|
|
|
RootedString str(cx, JS_InternString(cx, fs->name));
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
if (!props.append(INTERNED_STRING_TO_JSID(cx, str)))
|
|
|
|
return false;
|
2014-05-23 16:53:03 -07:00
|
|
|
}
|
|
|
|
for (const JSPropertySpec *ps = clasp->spec.prototypeProperties; ps && ps->name; ++ps) {
|
2014-06-02 13:55:20 -07:00
|
|
|
// We have code to Xray self-hosted accessors. But at present, there don't appear
|
|
|
|
// to be any self-hosted accessors anywhere in SpiderMonkey, let alone in on an
|
|
|
|
// Xrayable class, so we can't test it. Assert against it to make sure that we get
|
|
|
|
// test coverage in test_XrayToJS.xul when the time comes.
|
|
|
|
MOZ_ASSERT(ps->flags & JSPROP_NATIVE_ACCESSORS,
|
|
|
|
"Self-hosted accessor added to Xrayable class - ping the XPConnect "
|
|
|
|
"module owner about adding test coverage");
|
2014-05-23 16:53:03 -07:00
|
|
|
RootedString str(cx, JS_InternString(cx, ps->name));
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
if (!props.append(INTERNED_STRING_TO_JSID(cx, str)))
|
|
|
|
return false;
|
2014-03-23 07:02:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the 'constructor' property.
|
|
|
|
return props.append(GetRTIdByIndex(cx, XPCJSRuntime::IDX_CONSTRUCTOR));
|
|
|
|
}
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
JSObject*
|
|
|
|
JSXrayTraits::createHolder(JSContext *cx, JSObject *wrapper)
|
|
|
|
{
|
|
|
|
RootedObject global(cx, JS_GetGlobalForObject(cx, wrapper));
|
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
|
|
|
RootedObject holder(cx, JS_NewObjectWithGivenProto(cx, &HolderClass,
|
|
|
|
JS::NullPtr(), global));
|
|
|
|
if (!holder)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Compute information about the target.
|
|
|
|
bool isPrototype = false;
|
|
|
|
JSProtoKey key = IdentifyStandardInstance(target);
|
|
|
|
if (key == JSProto_Null) {
|
|
|
|
isPrototype = true;
|
|
|
|
key = IdentifyStandardPrototype(target);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(key != JSProto_Null);
|
|
|
|
|
|
|
|
// Store it on the holder.
|
|
|
|
RootedValue v(cx);
|
|
|
|
v.setNumber(static_cast<uint32_t>(key));
|
|
|
|
js::SetReservedSlot(holder, SLOT_PROTOKEY, v);
|
|
|
|
v.setBoolean(isPrototype);
|
|
|
|
js::SetReservedSlot(holder, SLOT_ISPROTOTYPE, v);
|
|
|
|
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNativeXrayTraits XPCWrappedNativeXrayTraits::singleton;
|
|
|
|
DOMXrayTraits DOMXrayTraits::singleton;
|
2014-03-23 07:02:12 -07:00
|
|
|
JSXrayTraits JSXrayTraits::singleton;
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
XrayTraits*
|
|
|
|
GetXrayTraits(JSObject *obj)
|
|
|
|
{
|
|
|
|
switch (GetXrayType(obj)) {
|
|
|
|
case XrayForDOMObject:
|
|
|
|
return &DOMXrayTraits::singleton;
|
|
|
|
case XrayForWrappedNative:
|
|
|
|
return &XPCWrappedNativeXrayTraits::singleton;
|
2014-03-23 07:02:12 -07:00
|
|
|
case XrayForJSObject:
|
|
|
|
return &JSXrayTraits::singleton;
|
2012-10-05 09:59:23 -07:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-05 10:07:37 -07:00
|
|
|
/*
|
|
|
|
* Xray expando handling.
|
|
|
|
*
|
|
|
|
* We hang expandos for Xray wrappers off a reserved slot on the target object
|
|
|
|
* so that same-origin compartments can share expandos for a given object. We
|
|
|
|
* have a linked list of expando objects, one per origin. The properties on these
|
|
|
|
* objects are generally wrappers pointing back to the compartment that applied
|
|
|
|
* them.
|
|
|
|
*
|
|
|
|
* The expando objects should _never_ be exposed to script. The fact that they
|
|
|
|
* live in the target compartment is a detail of the implementation, and does
|
|
|
|
* not imply that code in the target compartment should be allowed to inspect
|
|
|
|
* them. They are private to the origin that placed them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum ExpandoSlots {
|
|
|
|
JSSLOT_EXPANDO_NEXT = 0,
|
|
|
|
JSSLOT_EXPANDO_ORIGIN,
|
|
|
|
JSSLOT_EXPANDO_EXCLUSIVE_GLOBAL,
|
2013-12-13 12:01:30 -08:00
|
|
|
JSSLOT_EXPANDO_PROTOTYPE,
|
2012-06-05 10:07:37 -07:00
|
|
|
JSSLOT_EXPANDO_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsIPrincipal*
|
|
|
|
ObjectPrincipal(JSObject *obj)
|
|
|
|
{
|
|
|
|
return GetCompartmentPrincipal(js::GetObjectCompartment(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIPrincipal*
|
|
|
|
GetExpandoObjectPrincipal(JSObject *expandoObject)
|
|
|
|
{
|
2013-03-27 04:33:30 -07:00
|
|
|
Value v = JS_GetReservedSlot(expandoObject, JSSLOT_EXPANDO_ORIGIN);
|
2012-06-05 10:07:37 -07:00
|
|
|
return static_cast<nsIPrincipal*>(v.toPrivate());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ExpandoObjectFinalize(JSFreeOp *fop, JSObject *obj)
|
|
|
|
{
|
|
|
|
// Release the principal.
|
|
|
|
nsIPrincipal *principal = GetExpandoObjectPrincipal(obj);
|
|
|
|
NS_RELEASE(principal);
|
|
|
|
}
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass ExpandoObjectClass = {
|
2012-06-05 10:07:37 -07:00
|
|
|
"XrayExpandoObject",
|
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(JSSLOT_EXPANDO_COUNT),
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-06-05 10:07:37 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, ExpandoObjectFinalize
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2012-10-05 09:59:23 -07:00
|
|
|
XrayTraits::expandoObjectMatchesConsumer(JSContext *cx,
|
2013-03-27 04:33:30 -07:00
|
|
|
HandleObject expandoObject,
|
2012-10-05 09:59:23 -07:00
|
|
|
nsIPrincipal *consumerOrigin,
|
2013-03-27 04:33:30 -07:00
|
|
|
HandleObject exclusiveGlobal)
|
2012-06-05 10:07:37 -07:00
|
|
|
{
|
2012-06-18 06:28:11 -07:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(expandoObject, cx));
|
|
|
|
|
2012-06-05 10:07:37 -07:00
|
|
|
// First, compare the principals.
|
|
|
|
nsIPrincipal *o = GetExpandoObjectPrincipal(expandoObject);
|
|
|
|
// Note that it's very important here to ignore document.domain. We
|
|
|
|
// pull the principal for the expando object off of the first consumer
|
|
|
|
// for a given origin, and freely share the expandos amongst multiple
|
|
|
|
// same-origin consumers afterwards. However, this means that we have
|
|
|
|
// no way to know whether _all_ consumers have opted in to collaboration
|
|
|
|
// by explicitly setting document.domain. So we just mandate that expando
|
|
|
|
// sharing is unaffected by it.
|
2014-02-13 18:57:36 -08:00
|
|
|
if (!consumerOrigin->Equals(o))
|
|
|
|
return false;
|
2012-06-05 10:07:37 -07:00
|
|
|
|
|
|
|
// Sandboxes want exclusive expando objects.
|
|
|
|
JSObject *owner = JS_GetReservedSlot(expandoObject,
|
|
|
|
JSSLOT_EXPANDO_EXCLUSIVE_GLOBAL)
|
|
|
|
.toObjectOrNull();
|
|
|
|
if (!owner && !exclusiveGlobal)
|
|
|
|
return true;
|
2012-06-18 06:28:11 -07:00
|
|
|
|
|
|
|
// The exclusive global should always be wrapped in the target's compartment.
|
|
|
|
MOZ_ASSERT(!exclusiveGlobal || js::IsObjectInContextCompartment(exclusiveGlobal, cx));
|
|
|
|
MOZ_ASSERT(!owner || js::IsObjectInContextCompartment(owner, cx));
|
2012-06-05 10:07:37 -07:00
|
|
|
return owner == exclusiveGlobal;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::getExpandoObjectInternal(JSContext *cx, HandleObject target,
|
2012-10-05 09:59:23 -07:00
|
|
|
nsIPrincipal *origin,
|
2013-04-15 07:29:37 -07:00
|
|
|
JSObject *exclusiveGlobalArg)
|
2012-06-05 10:07:37 -07:00
|
|
|
{
|
2012-06-18 06:28:11 -07:00
|
|
|
// The expando object lives in the compartment of the target, so all our
|
|
|
|
// work needs to happen there.
|
2013-04-15 07:29:37 -07:00
|
|
|
RootedObject exclusiveGlobal(cx, exclusiveGlobalArg);
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, target);
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &exclusiveGlobal))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2012-06-18 06:28:11 -07:00
|
|
|
|
2012-06-05 10:07:37 -07:00
|
|
|
// Iterate through the chain, looking for a same-origin object.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject head(cx, getExpandoChain(target));
|
2012-06-05 10:07:37 -07:00
|
|
|
while (head) {
|
2012-10-05 09:59:23 -07:00
|
|
|
if (expandoObjectMatchesConsumer(cx, head, origin, exclusiveGlobal))
|
2012-06-05 10:07:37 -07:00
|
|
|
return head;
|
|
|
|
head = JS_GetReservedSlot(head, JSSLOT_EXPANDO_NEXT).toObjectOrNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found.
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-06-05 10:07:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::getExpandoObject(JSContext *cx, HandleObject target, HandleObject consumer)
|
2012-06-05 10:07:37 -07:00
|
|
|
{
|
|
|
|
JSObject *consumerGlobal = js::GetGlobalForObjectCrossCompartment(consumer);
|
|
|
|
bool isSandbox = !strcmp(js::GetObjectJSClass(consumerGlobal)->name, "Sandbox");
|
2012-10-05 09:59:23 -07:00
|
|
|
return getExpandoObjectInternal(cx, target, ObjectPrincipal(consumer),
|
|
|
|
isSandbox ? consumerGlobal : nullptr);
|
2012-06-05 10:07:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::attachExpandoObject(JSContext *cx, HandleObject target,
|
|
|
|
nsIPrincipal *origin, HandleObject exclusiveGlobal)
|
2012-06-05 10:07:37 -07:00
|
|
|
{
|
2012-06-18 06:28:11 -07:00
|
|
|
// Make sure the compartments are sane.
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(target, cx));
|
|
|
|
MOZ_ASSERT(!exclusiveGlobal || js::IsObjectInContextCompartment(exclusiveGlobal, cx));
|
|
|
|
|
2012-06-05 10:07:37 -07:00
|
|
|
// No duplicates allowed.
|
2012-10-05 09:59:23 -07:00
|
|
|
MOZ_ASSERT(!getExpandoObjectInternal(cx, target, origin, exclusiveGlobal));
|
2012-06-05 10:07:37 -07:00
|
|
|
|
|
|
|
// Create the expando object. We parent it directly to the target object.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject expandoObject(cx, JS_NewObjectWithGivenProto(cx, &ExpandoObjectClass,
|
2014-01-16 09:48:58 -08:00
|
|
|
JS::NullPtr(), target));
|
2012-06-05 10:07:37 -07:00
|
|
|
if (!expandoObject)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-06-05 10:07:37 -07:00
|
|
|
|
|
|
|
// AddRef and store the principal.
|
|
|
|
NS_ADDREF(origin);
|
|
|
|
JS_SetReservedSlot(expandoObject, JSSLOT_EXPANDO_ORIGIN, PRIVATE_TO_JSVAL(origin));
|
|
|
|
|
|
|
|
// Note the exclusive global, if any.
|
|
|
|
JS_SetReservedSlot(expandoObject, JSSLOT_EXPANDO_EXCLUSIVE_GLOBAL,
|
|
|
|
OBJECT_TO_JSVAL(exclusiveGlobal));
|
|
|
|
|
|
|
|
// If this is our first expando object, take the opportunity to preserve
|
|
|
|
// the wrapper. This keeps our expandos alive even if the Xray wrapper gets
|
|
|
|
// collected.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject chain(cx, getExpandoChain(target));
|
2012-10-05 09:59:23 -07:00
|
|
|
if (!chain)
|
|
|
|
preserveWrapper(target);
|
2012-06-05 10:07:37 -07:00
|
|
|
|
|
|
|
// Insert it at the front of the chain.
|
|
|
|
JS_SetReservedSlot(expandoObject, JSSLOT_EXPANDO_NEXT, OBJECT_TO_JSVAL(chain));
|
2014-02-21 11:35:48 -08:00
|
|
|
setExpandoChain(cx, target, expandoObject);
|
2012-06-05 10:07:37 -07:00
|
|
|
|
|
|
|
return expandoObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::ensureExpandoObject(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject target)
|
2012-06-05 10:07:37 -07:00
|
|
|
{
|
2012-06-18 06:28:11 -07:00
|
|
|
// Expando objects live in the target compartment.
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, target);
|
2012-10-05 09:59:23 -07:00
|
|
|
JSObject *expandoObject = getExpandoObject(cx, target, wrapper);
|
2012-06-05 10:07:37 -07:00
|
|
|
if (!expandoObject) {
|
|
|
|
// If the object is a sandbox, we don't want it to share expandos with
|
|
|
|
// anyone else, so we tag it with the sandbox global.
|
2012-06-18 06:28:11 -07:00
|
|
|
//
|
|
|
|
// NB: We first need to check the class, _then_ wrap for the target's
|
|
|
|
// compartment.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject consumerGlobal(cx, js::GetGlobalForObjectCrossCompartment(wrapper));
|
2012-06-05 10:07:37 -07:00
|
|
|
bool isSandbox = !strcmp(js::GetObjectJSClass(consumerGlobal)->name, "Sandbox");
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &consumerGlobal))
|
2013-10-10 13:35:53 -07:00
|
|
|
return nullptr;
|
2012-10-05 09:59:23 -07:00
|
|
|
expandoObject = attachExpandoObject(cx, target, ObjectPrincipal(wrapper),
|
2013-03-27 04:33:30 -07:00
|
|
|
isSandbox ? (HandleObject)consumerGlobal : NullPtr());
|
2012-06-05 10:07:37 -07:00
|
|
|
}
|
|
|
|
return expandoObject;
|
|
|
|
}
|
|
|
|
|
2012-06-05 10:07:37 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::cloneExpandoChain(JSContext *cx, HandleObject dst, HandleObject src)
|
2012-06-05 10:07:37 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(dst, cx));
|
2012-10-05 09:59:23 -07:00
|
|
|
MOZ_ASSERT(getExpandoChain(dst) == nullptr);
|
2012-06-05 10:07:37 -07:00
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject oldHead(cx, getExpandoChain(src));
|
2012-06-05 10:07:37 -07:00
|
|
|
while (oldHead) {
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject exclusive(cx, JS_GetReservedSlot(oldHead,
|
|
|
|
JSSLOT_EXPANDO_EXCLUSIVE_GLOBAL)
|
|
|
|
.toObjectOrNull());
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &exclusive))
|
2012-06-18 06:28:11 -07:00
|
|
|
return false;
|
2014-03-17 09:17:58 -07:00
|
|
|
RootedObject newHead(cx, attachExpandoObject(cx, dst, GetExpandoObjectPrincipal(oldHead),
|
|
|
|
exclusive));
|
2012-06-05 10:07:37 -07:00
|
|
|
if (!JS_CopyPropertiesFrom(cx, newHead, oldHead))
|
|
|
|
return false;
|
|
|
|
oldHead = JS_GetReservedSlot(oldHead, JSSLOT_EXPANDO_NEXT).toObjectOrNull();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-05 09:59:23 -07:00
|
|
|
|
|
|
|
namespace XrayUtils {
|
2013-04-15 07:29:37 -07:00
|
|
|
bool CloneExpandoChain(JSContext *cx, JSObject *dstArg, JSObject *srcArg)
|
2012-10-05 09:59:23 -07:00
|
|
|
{
|
2013-04-15 07:29:37 -07:00
|
|
|
RootedObject dst(cx, dstArg);
|
|
|
|
RootedObject src(cx, srcArg);
|
2012-10-05 09:59:23 -07:00
|
|
|
return GetXrayTraits(src)->cloneExpandoChain(cx, dst, src);
|
|
|
|
}
|
|
|
|
}
|
2012-10-05 09:59:22 -07:00
|
|
|
|
2010-09-23 15:56:28 -07:00
|
|
|
static JSObject *
|
|
|
|
GetHolder(JSObject *obj)
|
|
|
|
{
|
2011-05-23 08:39:25 -07:00
|
|
|
return &js::GetProxyExtra(obj, 0).toObject();
|
2010-09-23 15:56:28 -07:00
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
JSObject*
|
|
|
|
XrayTraits::getHolder(JSObject *wrapper)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(WrapperFactory::IsXrayWrapper(wrapper));
|
|
|
|
js::Value v = js::GetProxyExtra(wrapper, 0);
|
|
|
|
return v.isObject() ? &v.toObject() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::ensureHolder(JSContext *cx, HandleObject wrapper)
|
2012-10-05 09:59:23 -07:00
|
|
|
{
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject holder(cx, getHolder(wrapper));
|
2012-10-05 09:59:23 -07:00
|
|
|
if (holder)
|
|
|
|
return holder;
|
|
|
|
holder = createHolder(cx, wrapper); // virtual trap.
|
|
|
|
if (holder)
|
|
|
|
js::SetProxyExtra(wrapper, 0, ObjectValue(*holder));
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
bool
|
|
|
|
XPCWrappedNativeXrayTraits::isResolving(JSContext *cx, JSObject *holder,
|
|
|
|
jsid id)
|
|
|
|
{
|
2013-03-18 08:58:05 -07:00
|
|
|
ResolvingId *cur = ResolvingId::getResolvingId(holder);
|
|
|
|
if (!cur)
|
|
|
|
return false;
|
|
|
|
return cur->isResolving(id);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
|
|
|
|
2014-02-21 15:55:31 -08:00
|
|
|
namespace XrayUtils {
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsXPCWNHolderClass(const JSClass *clasp)
|
|
|
|
{
|
|
|
|
return clasp == &XPCWrappedNativeXrayTraits::HolderClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
2013-04-08 12:51:24 -07:00
|
|
|
holder_get(JSContext *cx, HandleObject wrapper, HandleId id, MutableHandleValue vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2013-04-08 12:51:24 -07:00
|
|
|
// JSClass::getProperty is wacky enough that it's hard to be sure someone
|
|
|
|
// can't inherit this getter by prototyping a random object to an
|
|
|
|
// XrayWrapper. Be safe.
|
|
|
|
NS_ENSURE_TRUE(WrapperFactory::IsXrayWrapper(wrapper), true);
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2010-09-02 16:02:51 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = XPCWrappedNativeXrayTraits::getWN(wrapper);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantGetProperty)) {
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, holder);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = true;
|
2012-03-27 16:31:37 -07:00
|
|
|
nsresult rv = wn->GetScriptableCallback()->GetProperty(wn, cx, wrapper,
|
2012-07-30 04:19:09 -07:00
|
|
|
id, vp.address(), &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;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool
|
|
|
|
holder_set(JSContext *cx, HandleObject wrapper, HandleId id, bool strict, MutableHandleValue vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2013-04-08 12:51:24 -07:00
|
|
|
// JSClass::setProperty is wacky enough that it's hard to be sure someone
|
|
|
|
// can't inherit this getter by prototyping a random object to an
|
|
|
|
// XrayWrapper. Be safe.
|
|
|
|
NS_ENSURE_TRUE(WrapperFactory::IsXrayWrapper(wrapper), true);
|
2010-09-23 15:56:28 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2012-03-27 16:31:37 -07:00
|
|
|
if (XPCWrappedNativeXrayTraits::isResolving(cx, holder, id)) {
|
2010-10-10 15:39:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = XPCWrappedNativeXrayTraits::getWN(wrapper);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantSetProperty)) {
|
2012-08-21 18:42:53 -07:00
|
|
|
JSAutoCompartment ac(cx, holder);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = true;
|
2012-03-27 16:31:37 -07:00
|
|
|
nsresult rv = wn->GetScriptableCallback()->SetProperty(wn, cx, wrapper,
|
2012-07-30 04:19:09 -07:00
|
|
|
id, vp.address(), &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;
|
|
|
|
}
|
|
|
|
|
2012-08-27 06:06:34 -07:00
|
|
|
class AutoSetWrapperNotShadowing
|
|
|
|
{
|
|
|
|
public:
|
2013-04-07 01:18:58 -07:00
|
|
|
AutoSetWrapperNotShadowing(ResolvingId *resolvingId MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-08-27 06:06:34 -07:00
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2013-04-07 01:18:58 -07:00
|
|
|
MOZ_ASSERT(resolvingId);
|
|
|
|
mResolvingId = resolvingId;
|
2012-08-27 06:06:34 -07:00
|
|
|
mResolvingId->mXrayShadowing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoSetWrapperNotShadowing()
|
|
|
|
{
|
|
|
|
mResolvingId->mXrayShadowing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
ResolvingId *mResolvingId;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is called after the resolveNativeProperty could not find any property
|
|
|
|
// with the given id. At this point we can check for DOM specific collections
|
|
|
|
// like document["formName"] because we already know that it is not shadowing
|
|
|
|
// any native property.
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XPCWrappedNativeXrayTraits::resolveDOMCollectionProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2012-08-27 06:06:34 -07:00
|
|
|
{
|
|
|
|
// If we are not currently resolving this id and resolveNative is called
|
|
|
|
// we don't do anything. (see defineProperty in case of shadowing is forbidden).
|
|
|
|
ResolvingId *rid = ResolvingId::getResolvingId(holder);
|
|
|
|
if (!rid || rid->mId != id)
|
|
|
|
return true;
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = getWN(wrapper);
|
2013-04-07 01:18:58 -07:00
|
|
|
if (!wn) {
|
|
|
|
// This should NEVER happen, but let's be extra careful here
|
2013-04-12 20:04:51 -07:00
|
|
|
// because of the reported crashes (Bug 832091).
|
2013-04-07 01:18:58 -07:00
|
|
|
XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-27 06:06:34 -07:00
|
|
|
if (!NATIVE_HAS_FLAG(wn, WantNewResolve))
|
|
|
|
return true;
|
|
|
|
|
2013-04-07 01:18:58 -07:00
|
|
|
ResolvingId *resolvingId = ResolvingId::getResolvingIdFromWrapper(wrapper);
|
|
|
|
if (!resolvingId) {
|
|
|
|
// This should NEVER happen, but let's be extra careful here
|
|
|
|
// becaue of the reported crashes (Bug 832091).
|
|
|
|
XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-27 06:06:34 -07:00
|
|
|
// Setting the current ResolvingId in non-shadowing mode. So for this id
|
|
|
|
// Xray won't ignore DOM specific collection properties temporarily.
|
2013-04-07 01:18:58 -07:00
|
|
|
AutoSetWrapperNotShadowing asw(resolvingId);
|
2012-08-27 06:06:34 -07:00
|
|
|
|
|
|
|
bool retval = true;
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject pobj(cx);
|
2012-08-27 06:06:34 -07:00
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->NewResolve(wn, cx, wrapper, id,
|
2014-04-25 14:11:02 -07:00
|
|
|
pobj.address(), &retval);
|
2012-08-27 06:06:34 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (retval)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-25 14:11:01 -07:00
|
|
|
if (pobj && !JS_GetPropertyDescriptorById(cx, holder, id, desc))
|
2012-08-27 06:06:34 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:34:25 -08:00
|
|
|
static nsGlobalWindow*
|
|
|
|
AsWindow(JSContext *cx, JSObject *wrapper)
|
2012-08-27 06:15:20 -07:00
|
|
|
{
|
2014-01-29 22:34:25 -08:00
|
|
|
nsGlobalWindow* win;
|
|
|
|
// We want to use our target object here, since we don't want to be
|
|
|
|
// doing a security check while unwrapping.
|
|
|
|
JSObject* target = XrayTraits::getTargetObject(wrapper);
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Window, target, win);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return win;
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> piWin = do_QueryInterface(
|
|
|
|
nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, target));
|
|
|
|
return static_cast<nsGlobalWindow*>(piWin.get());
|
2013-01-17 09:30:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-01-29 22:34:25 -08:00
|
|
|
IsWindow(JSContext *cx, JSObject *wrapper)
|
2013-01-17 09:30:37 -08:00
|
|
|
{
|
2014-01-29 22:34:25 -08:00
|
|
|
return !!AsWindow(cx, wrapper);
|
2012-08-27 06:15:20 -07:00
|
|
|
}
|
|
|
|
|
2012-09-26 07:17:51 -07:00
|
|
|
static nsQueryInterface
|
2013-03-27 04:33:30 -07:00
|
|
|
do_QueryInterfaceNative(JSContext* cx, HandleObject wrapper);
|
2012-09-26 07:17:51 -07:00
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
void
|
|
|
|
XPCWrappedNativeXrayTraits::preserveWrapper(JSObject *target)
|
|
|
|
{
|
2013-06-23 00:15:55 -07:00
|
|
|
XPCWrappedNative *wn = XPCWrappedNative::Get(target);
|
2012-10-05 09:59:23 -07:00
|
|
|
nsRefPtr<nsXPCClassInfo> ci;
|
|
|
|
CallQueryInterface(wn->Native(), getter_AddRefs(ci));
|
|
|
|
if (ci)
|
|
|
|
ci->PreserveWrapper(wn->Native());
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XPCWrappedNativeXrayTraits::resolveNativeProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2012-08-27 06:15:20 -07:00
|
|
|
MOZ_ASSERT(js::GetObjectJSClass(holder) == &HolderClass);
|
|
|
|
|
2013-10-10 13:35:53 -07:00
|
|
|
desc.object().set(nullptr);
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
// This will do verification and the method lookup for us.
|
2013-04-20 01:52:56 -07:00
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
|
|
|
XPCCallContext ccx(JS_CALLER, cx, target, NullPtr(), id);
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2012-08-27 06:06:34 -07:00
|
|
|
// There are no native numeric properties, so we can shortcut here. We will
|
|
|
|
// not find the property. However we want to support non shadowing dom
|
|
|
|
// specific collection properties like window.frames, so we still have to
|
|
|
|
// check for those.
|
2012-01-15 00:13:07 -08:00
|
|
|
if (!JSID_IS_STRING(id)) {
|
2010-07-02 14:09:48 -07:00
|
|
|
/* Not found */
|
2014-04-25 14:11:02 -07:00
|
|
|
return resolveDOMCollectionProperty(cx, wrapper, holder, id, desc);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2014-01-16 15:45:40 -08:00
|
|
|
|
|
|
|
// The |controllers| property is accessible as a [ChromeOnly] property on
|
|
|
|
// Window.WebIDL, and [noscript] in XPIDL. Chrome needs to see this over
|
|
|
|
// Xray, so we need to special-case it until we move |Window| to WebIDL.
|
|
|
|
nsGlobalWindow *win = nullptr;
|
|
|
|
if (id == GetRTIdByIndex(cx, XPCJSRuntime::IDX_CONTROLLERS) &&
|
|
|
|
AccessCheck::isChrome(wrapper) &&
|
2014-01-29 22:34:25 -08:00
|
|
|
(win = AsWindow(cx, wrapper)))
|
2014-01-16 15:45:40 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIControllers> c;
|
|
|
|
nsresult rv = win->GetControllers(getter_AddRefs(c));
|
|
|
|
if (NS_SUCCEEDED(rv) && c) {
|
|
|
|
rv = nsXPConnect::XPConnect()->WrapNativeToJSVal(cx, CurrentGlobalOrNull(cx),
|
|
|
|
c, nullptr, nullptr, true,
|
2014-01-16 16:11:25 -08:00
|
|
|
desc.value());
|
2014-01-16 15:45:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || !c) {
|
|
|
|
JS_ReportError(cx, "Failed to invoke GetControllers via Xrays");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-04 11:11:05 -07:00
|
|
|
// The |realFrameElement| property is accessible as a [ChromeOnly] property
|
|
|
|
// on Window.WebIDL, and [noscript] in XPIDL. Chrome needs to see this over
|
|
|
|
// Xray, so we need to special-case it until we move |Window| to WebIDL.
|
|
|
|
if (id == GetRTIdByIndex(cx, XPCJSRuntime::IDX_REALFRAMEELEMENT) &&
|
|
|
|
AccessCheck::isChrome(wrapper) &&
|
|
|
|
(win = AsWindow(cx, wrapper)))
|
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
Element* f = win->GetRealFrameElement(rv);
|
|
|
|
if (!f) {
|
|
|
|
desc.object().set(nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WrapNewBindingObject(cx, f, desc.value())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
XPCNativeInterface *iface;
|
|
|
|
XPCNativeMember *member;
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = getWN(wrapper);
|
2013-04-07 01:18:58 -07:00
|
|
|
|
|
|
|
if (ccx.GetWrapper() != wn || !wn->IsValid()) {
|
|
|
|
// Something is wrong. If the wrapper is not even valid let's not risk
|
|
|
|
// calling resolveDOMCollectionProperty.
|
|
|
|
return true;
|
|
|
|
} else if (!(iface = ccx.GetInterface()) ||
|
|
|
|
!(member = ccx.GetMember())) {
|
2010-07-02 14:09:48 -07:00
|
|
|
/* Not found */
|
2014-04-25 14:11:02 -07:00
|
|
|
return resolveDOMCollectionProperty(cx, wrapper, holder, id, desc);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.object().set(holder);
|
|
|
|
desc.setAttributes(JSPROP_ENUMERATE);
|
2013-10-10 13:35:53 -07:00
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.value().set(JSVAL_VOID);
|
2010-07-02 14:09:48 -07:00
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedValue fval(cx, JSVAL_VOID);
|
2010-07-02 14:09:48 -07:00
|
|
|
if (member->IsConstant()) {
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!member->GetConstantValue(ccx, iface, desc.value().address())) {
|
2010-07-02 14:09:48 -07:00
|
|
|
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.
|
2013-03-27 04:33:30 -07:00
|
|
|
if (!member->NewFunctionObject(ccx, iface, wrapper, fval.address())) {
|
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
|
|
|
|
2013-04-30 10:29:40 -07:00
|
|
|
unsigned attrs = desc.attributes();
|
|
|
|
attrs |= JSPROP_GETTER;
|
2010-09-20 14:48:01 -07:00
|
|
|
if (member->IsWritableAttribute())
|
2013-04-30 10:29:40 -07:00
|
|
|
attrs |= JSPROP_SETTER;
|
2010-07-02 14:09:48 -07:00
|
|
|
|
|
|
|
// Make the property shared on the holder so no slot is allocated
|
|
|
|
// for it. This avoids keeping garbage alive through that slot.
|
2013-04-30 10:29:40 -07:00
|
|
|
attrs |= JSPROP_SHARED;
|
|
|
|
desc.setAttributes(attrs);
|
2010-07-02 14:09:48 -07:00
|
|
|
} else {
|
|
|
|
// This is a method. Clone a function for it.
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!member->NewFunctionObject(ccx, iface, wrapper, desc.value().address())) {
|
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.
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.setGetter(JS_PropertyStub);
|
|
|
|
desc.setSetter(JS_StrictPropertyStub);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(cx, desc.value()) || !JS_WrapValue(cx, &fval))
|
2010-09-20 14:48:01 -07:00
|
|
|
return false;
|
|
|
|
|
2013-04-30 10:29:40 -07:00
|
|
|
if (desc.hasGetterObject())
|
|
|
|
desc.setGetterObject(&fval.toObject());
|
|
|
|
if (desc.hasSetterObject())
|
|
|
|
desc.setSetterObject(&fval.toObject());
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
// Define the property.
|
2014-04-30 02:10:33 -07:00
|
|
|
return JS_DefinePropertyById(cx, holder, id, desc.value(), desc.attributes(),
|
|
|
|
desc.getter(), desc.setter());
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-03-27 04:33:30 -07:00
|
|
|
wrappedJSObject_getter(JSContext *cx, HandleObject wrapper, HandleId id, MutableHandleValue vp)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2011-10-04 07:06:54 -07:00
|
|
|
if (!IsWrapper(wrapper) || !WrapperFactory::IsXrayWrapper(wrapper)) {
|
2011-09-21 18:30:27 -07:00
|
|
|
JS_ReportError(cx, "Unexpected object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 04:19:09 -07:00
|
|
|
vp.set(OBJECT_TO_JSVAL(wrapper));
|
2010-10-10 15:47:22 -07:00
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
return WrapperFactory::WaiveXrayAndWrap(cx, vp);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayTraits::resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper,
|
|
|
|
HandleObject wrapper, HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-05 09:59:23 -07:00
|
|
|
{
|
2013-10-10 13:35:53 -07:00
|
|
|
desc.object().set(nullptr);
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject target(cx, getTargetObject(wrapper));
|
|
|
|
RootedObject expando(cx, getExpandoObject(cx, target, wrapper));
|
2012-10-05 09:59:23 -07:00
|
|
|
|
|
|
|
// Check for expando properties first. Note that the expando object lives
|
|
|
|
// in the target compartment.
|
2013-11-22 10:55:42 -08:00
|
|
|
bool found = false;
|
2012-10-05 09:59:23 -07:00
|
|
|
if (expando) {
|
|
|
|
JSAutoCompartment ac(cx, expando);
|
2014-04-25 14:11:01 -07:00
|
|
|
if (!JS_GetPropertyDescriptorById(cx, expando, id, desc))
|
2012-10-05 09:59:23 -07:00
|
|
|
return false;
|
2013-11-22 10:55:42 -08:00
|
|
|
found = !!desc.object();
|
2012-10-05 09:59:23 -07:00
|
|
|
}
|
2013-11-22 10:55:42 -08:00
|
|
|
|
2013-11-22 10:55:43 -08:00
|
|
|
// Next, check for ES builtins.
|
2013-11-22 10:55:42 -08:00
|
|
|
if (!found && JS_IsGlobalObject(target)) {
|
|
|
|
JSProtoKey key = JS_IdToProtoKey(cx, id);
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
if (key != JSProto_Null) {
|
|
|
|
MOZ_ASSERT(key < JSProto_LIMIT);
|
|
|
|
RootedObject constructor(cx);
|
2014-01-30 07:45:16 -08:00
|
|
|
if (!JS_GetClassObject(cx, key, &constructor))
|
2013-11-22 10:55:42 -08:00
|
|
|
return false;
|
|
|
|
MOZ_ASSERT(constructor);
|
|
|
|
desc.value().set(ObjectValue(*constructor));
|
|
|
|
found = true;
|
2013-11-22 10:55:43 -08:00
|
|
|
} else if (id == GetRTIdByIndex(cx, XPCJSRuntime::IDX_EVAL)) {
|
|
|
|
RootedObject eval(cx);
|
|
|
|
if (!js::GetOriginalEval(cx, target, &eval))
|
|
|
|
return false;
|
|
|
|
desc.value().set(ObjectValue(*eval));
|
|
|
|
found = true;
|
2013-11-22 10:55:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
2013-08-12 04:09:14 -07:00
|
|
|
if (!JS_WrapPropertyDescriptor(cx, desc))
|
2012-10-05 09:59:23 -07:00
|
|
|
return false;
|
|
|
|
// Pretend the property lives on the wrapper.
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.object().set(wrapper);
|
2012-10-05 09:59:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-21 16:03:08 -08:00
|
|
|
|
|
|
|
// Handle .wrappedJSObject for subsuming callers. This should move once we
|
|
|
|
// sort out own-ness for the holder.
|
|
|
|
if (id == GetRTIdByIndex(cx, XPCJSRuntime::IDX_WRAPPED_JSOBJECT) &&
|
|
|
|
AccessCheck::wrapperSubsumes(wrapper))
|
|
|
|
{
|
|
|
|
if (!JS_AlreadyHasOwnPropertyById(cx, holder, id, &found))
|
|
|
|
return false;
|
2014-04-30 02:10:33 -07:00
|
|
|
if (!found && !JS_DefinePropertyById(cx, holder, id, UndefinedHandleValue,
|
|
|
|
JSPROP_ENUMERATE | JSPROP_SHARED,
|
|
|
|
wrappedJSObject_getter)) {
|
2014-02-21 16:03:08 -08:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-25 14:11:01 -07:00
|
|
|
if (!JS_GetPropertyDescriptorById(cx, holder, id, desc))
|
2014-02-21 16:03:08 -08:00
|
|
|
return false;
|
|
|
|
desc.object().set(wrapper);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:24 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-25 13:07:18 -07:00
|
|
|
bool
|
|
|
|
XrayTraits::set(JSContext *cx, HandleObject wrapper, HandleObject receiver, HandleId id,
|
|
|
|
bool strict, MutableHandleValue vp)
|
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already BaseProxyHandler.
|
|
|
|
js::BaseProxyHandler *handler = js::GetProxyHandler(wrapper);
|
|
|
|
return handler->js::BaseProxyHandler::set(cx, wrapper, receiver, id, strict, vp);
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:24 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XPCWrappedNativeXrayTraits::resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper,
|
|
|
|
HandleObject wrapper, HandleObject holder,
|
2014-04-25 14:11:02 -07:00
|
|
|
HandleId id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2012-10-05 09:59:24 -07:00
|
|
|
{
|
|
|
|
// Call the common code.
|
|
|
|
bool ok = XrayTraits::resolveOwnProperty(cx, jsWrapper, wrapper, holder,
|
2014-04-25 14:11:02 -07:00
|
|
|
id, desc);
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!ok || desc.object())
|
2012-10-05 09:59:24 -07:00
|
|
|
return ok;
|
2012-10-05 09:59:23 -07:00
|
|
|
|
2013-01-17 09:30:37 -08:00
|
|
|
// Check for indexed access on a window.
|
|
|
|
int32_t index = GetArrayIndexFromId(cx, id);
|
|
|
|
if (IsArrayIndex(index)) {
|
2014-01-29 22:34:25 -08:00
|
|
|
nsGlobalWindow* win = AsWindow(cx, wrapper);
|
2013-01-17 09:30:37 -08:00
|
|
|
// Note: As() unwraps outer windows to get to the inner window.
|
|
|
|
if (win) {
|
|
|
|
bool unused;
|
|
|
|
nsCOMPtr<nsIDOMWindow> subframe = win->IndexedGetter(index, unused);
|
|
|
|
if (subframe) {
|
|
|
|
nsGlobalWindow* global = static_cast<nsGlobalWindow*>(subframe.get());
|
|
|
|
global->EnsureInnerWindow();
|
|
|
|
JSObject* obj = global->FastGetGlobalJSObject();
|
|
|
|
if (MOZ_UNLIKELY(!obj)) {
|
|
|
|
// It's gone?
|
|
|
|
return xpc::Throw(cx, NS_ERROR_FAILURE);
|
|
|
|
}
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.value().setObject(*obj);
|
2014-01-29 22:34:25 -08:00
|
|
|
FillPropertyDescriptor(desc, wrapper, true);
|
2013-08-12 04:09:14 -07:00
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
2013-01-17 09:30:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-18 06:47:09 -07:00
|
|
|
// Xray wrappers don't use the regular wrapper hierarchy, so we should be
|
|
|
|
// in the wrapper's compartment here, not the wrappee.
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(wrapper, cx));
|
2010-09-20 14:48:01 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProp;
|
2011-02-15 19:00:55 -08:00
|
|
|
if (!JS_HasPropertyById(cx, holder, id, &hasProp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!hasProp) {
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = getWN(wrapper);
|
2011-02-15 19:00:55 -08:00
|
|
|
|
|
|
|
// Run the resolve hook of the wrapped native.
|
|
|
|
if (!NATIVE_HAS_FLAG(wn, WantNewResolve)) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-10 14:08:44 -08:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = true;
|
2013-04-30 07:27:48 -07:00
|
|
|
RootedObject pobj(cx);
|
|
|
|
nsIXPCScriptable *callback = wn->GetScriptableInfo()->GetCallback();
|
2014-04-25 14:11:02 -07:00
|
|
|
nsresult rv = callback->NewResolve(wn, cx, wrapper, id, pobj.address(),
|
|
|
|
&retval);
|
2010-11-10 14:08:44 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (retval)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-25 08:42:44 -07:00
|
|
|
MOZ_ASSERT(!pobj || (JS_HasPropertyById(cx, holder, id, &hasProp) &&
|
|
|
|
hasProp), "id got defined somewhere else?");
|
2010-11-10 14:08:44 -08:00
|
|
|
}
|
|
|
|
|
2013-04-23 09:50:17 -07:00
|
|
|
// resolveOwnProperty must return a non-empty |desc| if and only if an |own|
|
|
|
|
// property was found on the object. However, given how the NewResolve setup
|
|
|
|
// works, we can't run the resolve hook if the holder already has a property
|
|
|
|
// of the same name. So if there was a pre-existing property on the holder,
|
|
|
|
// we have to use it. But we have no way of knowing if it corresponded to an
|
|
|
|
// |own| or non-|own| property, since both get cached on the holder and the
|
|
|
|
// |own|-ness information is lost.
|
|
|
|
//
|
|
|
|
// So we just over-zealously call things |own| here. This can cause us to
|
|
|
|
// return non-|own| properties from Object.getOwnPropertyDescriptor if
|
|
|
|
// lookups are performed in a certain order, but we can probably live with
|
|
|
|
// that until XPCWN Xrays go away with the new DOM bindings.
|
2014-04-25 14:11:01 -07:00
|
|
|
return JS_GetPropertyDescriptorById(cx, holder, id, desc);
|
2010-10-10 15:49:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 15:23:47 -07:00
|
|
|
XPCWrappedNativeXrayTraits::defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
2013-07-25 22:52:59 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
Handle<JSPropertyDescriptor> existingDesc, bool *defined)
|
2010-10-10 15:49:13 -07:00
|
|
|
{
|
2012-10-05 09:59:24 -07:00
|
|
|
*defined = false;
|
2014-04-30 02:10:33 -07:00
|
|
|
RootedObject holder(cx, singleton.ensureHolder(cx, wrapper));
|
2012-03-27 16:31:37 -07:00
|
|
|
if (isResolving(cx, holder, id)) {
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!desc.hasAttributes(JSPROP_GETTER | JSPROP_SETTER)) {
|
|
|
|
if (!desc.getter())
|
|
|
|
desc.setGetter(holder_get);
|
|
|
|
if (!desc.setter())
|
|
|
|
desc.setSetter(holder_set);
|
2010-10-10 15:41:24 -07:00
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:24 -07:00
|
|
|
*defined = true;
|
2014-04-30 02:10:33 -07:00
|
|
|
return JS_DefinePropertyById(cx, holder, id, desc.value(), desc.attributes(),
|
|
|
|
desc.getter(), desc.setter());
|
2010-10-10 15:41:24 -07:00
|
|
|
}
|
2013-01-17 09:30:37 -08:00
|
|
|
|
|
|
|
// Check for an indexed property on a Window. If that's happening, do
|
|
|
|
// nothing but claim we defined it so it won't get added as an expando.
|
|
|
|
int32_t index = GetArrayIndexFromId(cx, id);
|
2014-01-29 22:34:25 -08:00
|
|
|
if (IsArrayIndex(index) && IsWindow(cx, wrapper)) {
|
2013-01-17 09:30:37 -08:00
|
|
|
*defined = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-10 14:08:44 -08:00
|
|
|
return true;
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XPCWrappedNativeXrayTraits::enumerateNames(JSContext *cx, HandleObject wrapper, unsigned flags,
|
|
|
|
AutoIdVector &props)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2010-11-10 14:08:44 -08:00
|
|
|
// Force all native properties to be materialized onto the wrapped native.
|
2013-03-27 04:33:30 -07:00
|
|
|
AutoIdVector wnProps(cx);
|
2010-11-10 14:08:44 -08:00
|
|
|
{
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject target(cx, singleton.getTargetObject(wrapper));
|
2012-10-05 09:59:23 -07:00
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
if (!js::GetPropertyNames(cx, target, flags, &wnProps))
|
2010-11-10 14:08:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-13 10:54:08 -08:00
|
|
|
if (!JS_WrapAutoIdVector(cx, wnProps))
|
|
|
|
return false;
|
2010-11-10 14:08:44 -08:00
|
|
|
|
|
|
|
// Go through the properties we got and enumerate all native ones.
|
2014-06-17 10:16:08 -07:00
|
|
|
if (!props.reserve(wnProps.length()))
|
|
|
|
return false;
|
2010-11-10 14:08:44 -08:00
|
|
|
for (size_t n = 0; n < wnProps.length(); ++n) {
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedId id(cx, wnProps[n]);
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProp;
|
2010-11-10 14:08:44 -08:00
|
|
|
if (!JS_HasPropertyById(cx, wrapper, id, &hasProp))
|
|
|
|
return false;
|
|
|
|
if (hasProp)
|
2014-06-17 10:16:08 -07:00
|
|
|
props.infallibleAppend(id);
|
2010-11-10 14:08:44 -08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
JSObject *
|
|
|
|
XPCWrappedNativeXrayTraits::createHolder(JSContext *cx, JSObject *wrapper)
|
|
|
|
{
|
2014-01-16 09:48:58 -08:00
|
|
|
RootedObject global(cx, JS_GetGlobalForObject(cx, wrapper));
|
|
|
|
JSObject *holder = JS_NewObjectWithGivenProto(cx, &HolderClass, JS::NullPtr(),
|
2012-10-05 09:59:23 -07:00
|
|
|
global);
|
|
|
|
if (!holder)
|
|
|
|
return nullptr;
|
|
|
|
|
2013-10-10 13:35:53 -07:00
|
|
|
js::SetReservedSlot(holder, JSSLOT_RESOLVING, PrivateValue(nullptr));
|
2012-10-05 09:59:23 -07:00
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:22 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XPCWrappedNativeXrayTraits::call(JSContext *cx, HandleObject wrapper,
|
2013-04-25 16:03:05 -07:00
|
|
|
const JS::CallArgs &args,
|
|
|
|
js::Wrapper& baseInstance)
|
2012-10-05 09:59:22 -07:00
|
|
|
{
|
|
|
|
// Run the resolve hook of the wrapped native.
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = getWN(wrapper);
|
2012-10-05 09:59:22 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantCall)) {
|
2013-04-20 01:52:56 -07:00
|
|
|
XPCCallContext ccx(JS_CALLER, cx, wrapper, NullPtr(), JSID_VOIDHANDLE, args.length(),
|
|
|
|
args.array(), args.rval().address());
|
2012-10-05 09:59:22 -07:00
|
|
|
if (!ccx.IsValid())
|
|
|
|
return false;
|
|
|
|
bool ok = true;
|
2013-04-04 00:02:24 -07:00
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->Call(
|
2013-05-04 10:25:15 -07:00
|
|
|
wn, cx, wrapper, args, &ok);
|
2012-10-05 09:59:22 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (ok)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XPCWrappedNativeXrayTraits::construct(JSContext *cx, HandleObject wrapper,
|
2013-04-25 16:03:05 -07:00
|
|
|
const JS::CallArgs &args,
|
|
|
|
js::Wrapper& baseInstance)
|
2012-10-05 09:59:22 -07:00
|
|
|
{
|
|
|
|
// Run the resolve hook of the wrapped native.
|
2012-10-05 09:59:23 -07:00
|
|
|
XPCWrappedNative *wn = getWN(wrapper);
|
2012-10-05 09:59:22 -07:00
|
|
|
if (NATIVE_HAS_FLAG(wn, WantConstruct)) {
|
2013-04-20 01:52:56 -07:00
|
|
|
XPCCallContext ccx(JS_CALLER, cx, wrapper, NullPtr(), JSID_VOIDHANDLE, args.length(),
|
|
|
|
args.array(), args.rval().address());
|
2012-10-05 09:59:22 -07:00
|
|
|
if (!ccx.IsValid())
|
|
|
|
return false;
|
|
|
|
bool ok = true;
|
2013-04-04 00:02:24 -07:00
|
|
|
nsresult rv = wn->GetScriptableInfo()->GetCallback()->Construct(
|
2013-05-04 10:25:15 -07:00
|
|
|
wn, cx, wrapper, args, &ok);
|
2012-10-05 09:59:22 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (ok)
|
|
|
|
XPCThrower::Throw(rv, cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
DOMXrayTraits::resolveNativeProperty(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-04-24 06:22:21 -07:00
|
|
|
RootedObject obj(cx, getTargetObject(wrapper));
|
2013-08-12 04:09:14 -07:00
|
|
|
if (!XrayResolveNativeProperty(cx, wrapper, obj, id, desc))
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
2012-06-06 12:52:26 -07:00
|
|
|
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(!desc.object() || desc.object() == wrapper, "What did we resolve this on?");
|
2012-03-30 21:42:20 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
DOMXrayTraits::resolveOwnProperty(JSContext *cx, Wrapper &jsWrapper, HandleObject wrapper,
|
|
|
|
HandleObject holder, HandleId id,
|
2014-04-25 14:11:02 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2012-10-05 09:59:23 -07:00
|
|
|
// Call the common code.
|
2014-04-25 14:11:02 -07:00
|
|
|
bool ok = XrayTraits::resolveOwnProperty(cx, jsWrapper, wrapper, holder, id, desc);
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!ok || desc.object())
|
2012-10-05 09:59:23 -07:00
|
|
|
return ok;
|
|
|
|
|
2014-01-29 22:34:25 -08:00
|
|
|
// Check for indexed access on a window.
|
|
|
|
int32_t index = GetArrayIndexFromId(cx, id);
|
|
|
|
if (IsArrayIndex(index)) {
|
|
|
|
nsGlobalWindow* win = AsWindow(cx, wrapper);
|
|
|
|
// Note: As() unwraps outer windows to get to the inner window.
|
|
|
|
if (win) {
|
|
|
|
bool unused;
|
|
|
|
nsCOMPtr<nsIDOMWindow> subframe = win->IndexedGetter(index, unused);
|
|
|
|
if (subframe) {
|
|
|
|
nsGlobalWindow* global = static_cast<nsGlobalWindow*>(subframe.get());
|
|
|
|
global->EnsureInnerWindow();
|
|
|
|
JSObject* obj = global->FastGetGlobalJSObject();
|
|
|
|
if (MOZ_UNLIKELY(!obj)) {
|
|
|
|
// It's gone?
|
|
|
|
return xpc::Throw(cx, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
desc.value().setObject(*obj);
|
|
|
|
FillPropertyDescriptor(desc, wrapper, true);
|
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 06:22:21 -07:00
|
|
|
RootedObject obj(cx, getTargetObject(wrapper));
|
2014-04-25 14:11:02 -07:00
|
|
|
if (!XrayResolveOwnProperty(cx, wrapper, obj, id, desc))
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
2012-06-06 12:52:26 -07:00
|
|
|
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(!desc.object() || desc.object() == wrapper, "What did we resolve this on?");
|
2012-06-06 12:52:26 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-14 02:29:48 -08:00
|
|
|
bool
|
2013-03-21 15:23:47 -07:00
|
|
|
DOMXrayTraits::defineProperty(JSContext *cx, HandleObject wrapper, HandleId id,
|
2013-07-25 22:52:59 -07:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
Handle<JSPropertyDescriptor> existingDesc, bool *defined)
|
2013-01-14 02:29:48 -08:00
|
|
|
{
|
2014-01-29 22:34:25 -08:00
|
|
|
// Check for an indexed property on a Window. If that's happening, do
|
|
|
|
// nothing but claim we defined it so it won't get added as an expando.
|
2014-02-15 13:12:35 -08:00
|
|
|
if (IsWindow(cx, wrapper)) {
|
|
|
|
int32_t index = GetArrayIndexFromId(cx, id);
|
|
|
|
if (IsArrayIndex(index)) {
|
|
|
|
*defined = true;
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-29 22:34:25 -08:00
|
|
|
}
|
|
|
|
|
2014-02-15 13:12:35 -08:00
|
|
|
if (!existingDesc.object())
|
|
|
|
return true;
|
|
|
|
|
2013-05-29 02:38:57 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, getTargetObject(wrapper));
|
|
|
|
return XrayDefineProperty(cx, wrapper, obj, id, desc, defined);
|
2013-01-14 02:29:48 -08:00
|
|
|
}
|
|
|
|
|
2014-04-25 13:07:18 -07:00
|
|
|
bool
|
|
|
|
DOMXrayTraits::set(JSContext *cx, HandleObject wrapper, HandleObject receiver, HandleId id,
|
|
|
|
bool strict, MutableHandleValue vp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(xpc::WrapperFactory::IsXrayWrapper(wrapper));
|
|
|
|
RootedObject obj(cx, getTargetObject(wrapper));
|
|
|
|
if (IsDOMProxy(obj)) {
|
|
|
|
DOMProxyHandler* handler = GetDOMProxyHandler(obj);
|
|
|
|
|
|
|
|
bool done;
|
|
|
|
if (!handler->setCustom(cx, obj, id, vp, &done))
|
|
|
|
return false;
|
|
|
|
if (done)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return XrayTraits::set(cx, wrapper, receiver, id, strict, vp);
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
DOMXrayTraits::enumerateNames(JSContext *cx, HandleObject wrapper, unsigned flags,
|
|
|
|
AutoIdVector &props)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2013-05-03 16:29:08 -07:00
|
|
|
JS::Rooted<JSObject*> obj(cx, getTargetObject(wrapper));
|
|
|
|
return XrayEnumerateProperties(cx, wrapper, obj, flags, props);
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
bool
|
2013-04-25 16:03:05 -07:00
|
|
|
DOMXrayTraits::call(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject obj(cx, getTargetObject(wrapper));
|
2013-09-11 05:49:05 -07:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
2013-04-25 16:03:05 -07:00
|
|
|
// What we have is either a WebIDL interface object, a WebIDL prototype
|
|
|
|
// object, or a WebIDL instance object. WebIDL prototype objects never have
|
|
|
|
// a clasp->call. WebIDL interface objects we want to invoke on the xray
|
|
|
|
// compartment. WebIDL instance objects either don't have a clasp->call or
|
|
|
|
// are using "legacycaller", which basically means plug-ins. We want to
|
|
|
|
// call those on the content compartment.
|
|
|
|
if (clasp->flags & JSCLASS_IS_DOMIFACEANDPROTOJSCLASS) {
|
|
|
|
if (!clasp->call) {
|
2014-01-14 18:46:44 -08:00
|
|
|
RootedValue v(cx, ObjectValue(*wrapper));
|
|
|
|
js_ReportIsNotFunction(cx, v);
|
2012-03-30 21:42:20 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-25 16:03:05 -07:00
|
|
|
// call it on the Xray compartment
|
|
|
|
if (!clasp->call(cx, args.length(), args.base()))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// This is only reached for WebIDL instance objects, and in practice
|
|
|
|
// only for plugins. Just call them on the content compartment.
|
|
|
|
if (!baseInstance.call(cx, wrapper, args))
|
2013-04-04 00:02:24 -07:00
|
|
|
return false;
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
2013-10-26 09:19:05 -07:00
|
|
|
return JS_WrapValue(cx, args.rval());
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
bool
|
2013-04-25 16:03:05 -07:00
|
|
|
DOMXrayTraits::construct(JSContext *cx, HandleObject wrapper,
|
|
|
|
const JS::CallArgs &args, js::Wrapper& baseInstance)
|
2012-10-09 11:50:27 -07:00
|
|
|
{
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject obj(cx, getTargetObject(wrapper));
|
2012-10-09 11:50:27 -07:00
|
|
|
MOZ_ASSERT(mozilla::dom::HasConstructor(obj));
|
2013-09-11 05:49:05 -07:00
|
|
|
const js::Class* clasp = js::GetObjectClass(obj);
|
2013-04-25 16:03:05 -07:00
|
|
|
// See comments in DOMXrayTraits::call() explaining what's going on here.
|
|
|
|
if (clasp->flags & JSCLASS_IS_DOMIFACEANDPROTOJSCLASS) {
|
|
|
|
if (!clasp->construct) {
|
2014-01-14 18:46:44 -08:00
|
|
|
RootedValue v(cx, ObjectValue(*wrapper));
|
|
|
|
js_ReportIsNotFunction(cx, v);
|
2013-04-25 16:03:05 -07:00
|
|
|
return false;
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
2013-04-25 16:03:05 -07:00
|
|
|
if (!clasp->construct(cx, args.length(), args.base()))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!baseInstance.construct(cx, wrapper, args))
|
|
|
|
return false;
|
2012-10-09 11:50:27 -07:00
|
|
|
}
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!args.rval().isObject() || !JS_WrapValue(cx, args.rval()))
|
2012-10-09 11:50:27 -07:00
|
|
|
return false;
|
2012-03-30 21:42:20 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:23 -07:00
|
|
|
void
|
|
|
|
DOMXrayTraits::preserveWrapper(JSObject *target)
|
|
|
|
{
|
2013-05-31 23:56:02 -07:00
|
|
|
nsISupports *identity = mozilla::dom::UnwrapDOMObjectToISupports(target);
|
|
|
|
if (!identity)
|
2012-10-05 09:59:23 -07:00
|
|
|
return;
|
|
|
|
nsWrapperCache* cache = nullptr;
|
|
|
|
CallQueryInterface(identity, &cache);
|
|
|
|
if (cache)
|
2013-06-23 00:15:42 -07:00
|
|
|
cache->PreserveWrapper(identity);
|
2012-10-05 09:59:23 -07:00
|
|
|
}
|
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
JSObject*
|
2012-10-05 09:59:23 -07:00
|
|
|
DOMXrayTraits::createHolder(JSContext *cx, JSObject *wrapper)
|
2012-03-30 21:42:20 -07:00
|
|
|
{
|
2014-01-16 09:48:58 -08:00
|
|
|
RootedObject global(cx, JS_GetGlobalForObject(cx, wrapper));
|
|
|
|
return JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), global);
|
2012-03-30 21:42:20 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
XrayWrapper<Base, Traits>::XrayWrapper(unsigned flags)
|
|
|
|
: Base(flags | WrapperFactory::IS_XRAY_WRAPPER_FLAG)
|
2010-07-02 14:09:48 -07:00
|
|
|
{
|
2014-02-21 15:55:30 -08:00
|
|
|
Base::setHasPrototype(Traits::HasPrototype);
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
XrayWrapper<Base, Traits>::~XrayWrapper()
|
2010-09-20 14:48:01 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
namespace XrayUtils {
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
GetNativePropertiesObject(JSContext *cx, JSObject *wrapper)
|
2011-01-25 15:06:45 -08:00
|
|
|
{
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(js::IsWrapper(wrapper) && WrapperFactory::IsXrayWrapper(wrapper),
|
|
|
|
"bad object passed in");
|
2011-01-25 15:06:45 -08:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
JSObject *holder = GetHolder(wrapper);
|
2013-08-14 00:00:34 -07:00
|
|
|
MOZ_ASSERT(holder, "uninitialized wrapper being used?");
|
2012-03-27 16:31:37 -07:00
|
|
|
return holder;
|
|
|
|
}
|
2011-01-25 15:06:45 -08:00
|
|
|
|
2013-02-25 13:54:17 -08:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
IsXrayResolving(JSContext *cx, HandleObject wrapper, HandleId id)
|
2013-02-25 13:54:17 -08:00
|
|
|
{
|
|
|
|
if (!WrapperFactory::IsXrayWrapper(wrapper) ||
|
|
|
|
GetXrayType(wrapper) != XrayForWrappedNative)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JSObject *holder =
|
|
|
|
XPCWrappedNativeXrayTraits::singleton.ensureHolder(cx, wrapper);
|
|
|
|
return XPCWrappedNativeXrayTraits::isResolving(cx, holder, id);
|
|
|
|
}
|
|
|
|
|
2013-04-23 09:50:17 -07:00
|
|
|
bool
|
|
|
|
HasNativeProperty(JSContext *cx, HandleObject wrapper, HandleId id, bool *hasProp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(WrapperFactory::IsXrayWrapper(wrapper));
|
|
|
|
XrayTraits *traits = GetXrayTraits(wrapper);
|
|
|
|
MOZ_ASSERT(traits);
|
|
|
|
RootedObject holder(cx, traits->ensureHolder(cx, wrapper));
|
|
|
|
NS_ENSURE_TRUE(holder, false);
|
|
|
|
*hasProp = false;
|
2013-07-25 22:52:59 -07:00
|
|
|
Rooted<JSPropertyDescriptor> desc(cx);
|
2013-04-23 09:50:17 -07:00
|
|
|
Wrapper *handler = Wrapper::wrapperHandler(wrapper);
|
|
|
|
|
|
|
|
// Try resolveOwnProperty.
|
|
|
|
Maybe<ResolvingId> resolvingId;
|
|
|
|
if (traits == &XPCWrappedNativeXrayTraits::singleton)
|
2013-04-24 02:45:41 -07:00
|
|
|
resolvingId.construct(cx, wrapper, id);
|
2014-04-25 14:11:02 -07:00
|
|
|
if (!traits->resolveOwnProperty(cx, *handler, wrapper, holder, id, &desc))
|
2013-04-23 09:50:17 -07:00
|
|
|
return false;
|
2013-04-26 10:50:18 -07:00
|
|
|
if (desc.object()) {
|
2013-04-23 09:50:17 -07:00
|
|
|
*hasProp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the holder.
|
2013-08-08 15:53:04 -07:00
|
|
|
bool found = false;
|
2013-04-23 09:50:17 -07:00
|
|
|
if (!JS_AlreadyHasOwnPropertyById(cx, holder, id, &found))
|
|
|
|
return false;
|
|
|
|
if (found) {
|
|
|
|
*hasProp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try resolveNativeProperty.
|
2014-04-25 14:11:02 -07:00
|
|
|
if (!traits->resolveNativeProperty(cx, wrapper, holder, id, &desc))
|
2013-04-23 09:50:17 -07:00
|
|
|
return false;
|
2013-04-26 10:50:18 -07:00
|
|
|
*hasProp = !!desc.object();
|
2013-04-23 09:50:17 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-22 19:43:03 -07:00
|
|
|
} // namespace XrayUtils
|
2011-01-25 15:06:45 -08:00
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-10-26 09:19:05 -07:00
|
|
|
XrayToString(JSContext *cx, unsigned argc, Value *vp)
|
2011-01-25 15:06:45 -08:00
|
|
|
{
|
2013-10-26 09:19:05 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
|
|
|
if (!args.thisv().isObject()) {
|
|
|
|
JS_ReportError(cx, "XrayToString called on an incompatible object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RootedObject wrapper(cx, &args.thisv().toObject());
|
2012-04-27 03:47:00 -07:00
|
|
|
if (!wrapper)
|
|
|
|
return false;
|
2012-10-09 11:50:27 -07:00
|
|
|
if (IsWrapper(wrapper) &&
|
|
|
|
GetProxyHandler(wrapper) == &sandboxCallableProxyHandler) {
|
|
|
|
wrapper = xpc::SandboxCallableProxyHandler::wrappedObject(wrapper);
|
|
|
|
}
|
2012-04-27 03:47:00 -07:00
|
|
|
if (!IsWrapper(wrapper) || !WrapperFactory::IsXrayWrapper(wrapper)) {
|
2012-03-27 16:31:37 -07:00
|
|
|
JS_ReportError(cx, "XrayToString called on an incompatible object");
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-25 15:06:45 -08:00
|
|
|
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject obj(cx, XrayTraits::getTargetObject(wrapper));
|
2012-10-09 11:50:27 -07:00
|
|
|
|
|
|
|
static const char start[] = "[object XrayWrapper ";
|
|
|
|
static const char end[] = "]";
|
|
|
|
if (UseDOMXray(obj))
|
2013-10-26 09:19:05 -07:00
|
|
|
return NativeToString(cx, wrapper, obj, start, end, args.rval());
|
2012-10-09 11:50:27 -07:00
|
|
|
|
|
|
|
nsAutoString result;
|
|
|
|
result.AppendASCII(start);
|
|
|
|
|
|
|
|
XPCCallContext ccx(JS_CALLER, cx, obj);
|
|
|
|
XPCWrappedNative *wn = XPCWrappedNativeXrayTraits::getWN(wrapper);
|
2013-06-04 21:11:18 -07:00
|
|
|
char *wrapperStr = wn->ToString();
|
2012-10-09 11:50:27 -07:00
|
|
|
if (!wrapperStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
2011-01-25 15:06:45 -08:00
|
|
|
}
|
2012-10-09 11:50:27 -07:00
|
|
|
result.AppendASCII(wrapperStr);
|
|
|
|
JS_smprintf_free(wrapperStr);
|
2011-01-25 15:06:45 -08:00
|
|
|
|
2012-10-09 11:50:27 -07:00
|
|
|
result.AppendASCII(end);
|
2011-05-25 08:30:50 -07:00
|
|
|
|
2013-12-05 01:56:22 -08:00
|
|
|
JSString *str = JS_NewUCStringCopyN(cx, result.get(), result.Length());
|
2012-03-27 16:31:37 -07:00
|
|
|
if (!str)
|
|
|
|
return false;
|
2011-05-27 08:06:42 -07:00
|
|
|
|
2013-10-26 09:19:05 -07:00
|
|
|
args.rval().setString(str);
|
2012-03-27 16:31:37 -07:00
|
|
|
return true;
|
2011-05-27 08:06:42 -07:00
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
static void
|
|
|
|
DEBUG_CheckXBLCallable(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2013-07-30 08:43:46 -07:00
|
|
|
// In general, we shouldn't have cross-compartment wrappers here, because
|
|
|
|
// we should be running in an XBL scope, and the content prototype should
|
|
|
|
// contain wrappers to functions defined in the XBL scope. But if the node
|
|
|
|
// has been adopted into another compartment, those prototypes will now point
|
|
|
|
// to a different XBL scope (which is ok).
|
|
|
|
MOZ_ASSERT_IF(js::IsCrossCompartmentWrapper(obj),
|
2014-06-02 18:04:21 -07:00
|
|
|
xpc::IsContentXBLScope(js::GetObjectCompartment(js::UncheckedUnwrap(obj))));
|
2013-02-08 06:24:21 -08:00
|
|
|
MOZ_ASSERT(JS_ObjectIsCallable(cx, obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DEBUG_CheckXBLLookup(JSContext *cx, JSPropertyDescriptor *desc)
|
|
|
|
{
|
|
|
|
if (!desc->obj)
|
|
|
|
return;
|
|
|
|
if (!desc->value.isUndefined()) {
|
|
|
|
MOZ_ASSERT(desc->value.isObject());
|
|
|
|
DEBUG_CheckXBLCallable(cx, &desc->value.toObject());
|
|
|
|
}
|
|
|
|
if (desc->getter) {
|
|
|
|
MOZ_ASSERT(desc->attrs & JSPROP_GETTER);
|
|
|
|
DEBUG_CheckXBLCallable(cx, JS_FUNC_TO_DATA_PTR(JSObject *, desc->getter));
|
|
|
|
}
|
|
|
|
if (desc->setter) {
|
|
|
|
MOZ_ASSERT(desc->attrs & JSPROP_SETTER);
|
|
|
|
DEBUG_CheckXBLCallable(cx, JS_FUNC_TO_DATA_PTR(JSObject *, desc->setter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DEBUG_CheckXBLLookup(a, b) {}
|
|
|
|
#endif
|
|
|
|
|
2013-03-22 19:43:03 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-06-28 14:01:09 -07:00
|
|
|
XrayWrapper<Base, Traits>::isExtensible(JSContext *cx, JS::Handle<JSObject*> wrapper, bool *extensible)
|
2013-03-22 19:43:03 -07:00
|
|
|
{
|
|
|
|
// Xray wrappers are supposed to provide a clean view of the target
|
|
|
|
// reflector, hiding any modifications by script in the target scope. So
|
|
|
|
// even if that script freezes the reflector, we don't want to make that
|
|
|
|
// visible to the caller. DOM reflectors are always extensible by default,
|
|
|
|
// so we can just return true here.
|
2013-06-28 14:01:09 -07:00
|
|
|
*extensible = true;
|
2013-03-22 19:43:03 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-04-24 02:45:41 -07:00
|
|
|
XrayWrapper<Base, Traits>::preventExtensions(JSContext *cx, HandleObject wrapper)
|
2013-03-22 19:43:03 -07:00
|
|
|
{
|
|
|
|
// See above.
|
2013-10-10 13:35:53 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_CANT_CHANGE_EXTENSIBILITY);
|
2013-03-22 19:43:03 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
2011-05-25 08:30:50 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::getPropertyDescriptor(JSContext *cx, HandleObject wrapper, HandleId id,
|
2014-04-25 14:11:01 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2011-05-25 08:30:50 -07:00
|
|
|
{
|
2014-02-13 10:54:07 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, id, BaseProxyHandler::GET | BaseProxyHandler::SET);
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject holder(cx, Traits::singleton.ensureHolder(cx, wrapper));
|
2012-03-27 16:31:37 -07:00
|
|
|
if (Traits::isResolving(cx, holder, id)) {
|
2013-10-10 13:35:53 -07:00
|
|
|
desc.object().set(nullptr);
|
2012-03-27 16:31:37 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-27 08:06:42 -07:00
|
|
|
|
2013-04-24 02:45:41 -07:00
|
|
|
typename Traits::ResolvingIdImpl resolving(cx, wrapper, id);
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
if (!holder)
|
2011-05-27 08:06:42 -07:00
|
|
|
return false;
|
2012-03-27 16:31:37 -07:00
|
|
|
|
2013-04-23 09:50:17 -07:00
|
|
|
// Ordering is important here.
|
|
|
|
//
|
|
|
|
// We first need to call resolveOwnProperty, even before checking the holder,
|
|
|
|
// because there might be a new dynamic |own| property that appears and
|
|
|
|
// shadows a previously-resolved non-own property that we cached on the
|
|
|
|
// holder. This can happen with indexed properties on NodeLists, for example,
|
|
|
|
// which are |own| value props.
|
|
|
|
//
|
|
|
|
// resolveOwnProperty may or may not cache what it finds on the holder,
|
|
|
|
// depending on how ephemeral it decides the property is. XPCWN |own|
|
|
|
|
// properties generally end up on the holder via NewResolve, whereas
|
|
|
|
// NodeList |own| properties don't get defined on the holder, since they're
|
|
|
|
// supposed to be dynamic. This means that we have to first check the result
|
|
|
|
// of resolveOwnProperty, and _then_, if that comes up blank, check the
|
|
|
|
// holder for any cached native properties.
|
|
|
|
//
|
|
|
|
// Finally, we call resolveNativeProperty, which checks non-own properties,
|
|
|
|
// and unconditionally caches what it finds on the holder.
|
|
|
|
|
|
|
|
// Check resolveOwnProperty.
|
2014-04-25 14:11:02 -07:00
|
|
|
if (!Traits::singleton.resolveOwnProperty(cx, *this, wrapper, holder, id, desc))
|
2012-03-27 16:31:37 -07:00
|
|
|
return false;
|
2013-04-23 09:50:17 -07:00
|
|
|
|
|
|
|
// Check the holder.
|
2014-04-25 14:11:01 -07:00
|
|
|
if (!desc.object() && !JS_GetPropertyDescriptorById(cx, holder, id, desc))
|
2013-04-23 09:50:17 -07:00
|
|
|
return false;
|
2013-04-30 10:29:40 -07:00
|
|
|
if (desc.object()) {
|
|
|
|
desc.object().set(wrapper);
|
2011-05-27 08:06:42 -07:00
|
|
|
return true;
|
2013-01-22 21:04:38 -08:00
|
|
|
}
|
2011-05-27 08:06:42 -07:00
|
|
|
|
2013-04-23 09:50:17 -07:00
|
|
|
// Nothing in the cache. Call through, and cache the result.
|
2014-04-25 14:11:02 -07:00
|
|
|
if (!Traits::singleton.resolveNativeProperty(cx, wrapper, holder, id, desc))
|
2013-04-23 09:50:17 -07:00
|
|
|
return false;
|
|
|
|
|
2013-03-14 22:38:26 -07:00
|
|
|
// We need to handle named access on the Window somewhere other than
|
|
|
|
// Traits::resolveOwnProperty, because per spec it happens on the Global
|
|
|
|
// Scope Polluter and thus the resulting properties are non-|own|. However,
|
2013-04-23 09:50:17 -07:00
|
|
|
// we're set up (above) to cache (on the holder) anything that comes out of
|
2013-03-14 22:38:26 -07:00
|
|
|
// resolveNativeProperty, which we don't want for something dynamic like
|
2013-04-23 09:50:17 -07:00
|
|
|
// named access. So we just handle it separately here.
|
2013-10-11 22:02:39 -07:00
|
|
|
nsGlobalWindow *win = nullptr;
|
|
|
|
if (!desc.object() &&
|
|
|
|
JSID_IS_STRING(id) &&
|
2014-01-29 22:34:25 -08:00
|
|
|
(win = AsWindow(cx, wrapper)))
|
2013-03-14 22:38:26 -07:00
|
|
|
{
|
2014-06-10 20:15:56 -07:00
|
|
|
// Note - The infallibleInit() depends on the JSID_IS_STRING check above.
|
|
|
|
nsDependentJSString name;
|
|
|
|
name.infallibleInit(id);
|
2013-05-05 00:03:19 -07:00
|
|
|
nsCOMPtr<nsIDOMWindow> childDOMWin = win->GetChildWindow(name);
|
2013-03-14 22:38:26 -07:00
|
|
|
if (childDOMWin) {
|
|
|
|
nsGlobalWindow *cwin = static_cast<nsGlobalWindow*>(childDOMWin.get());
|
|
|
|
JSObject *childObj = cwin->FastGetGlobalJSObject();
|
|
|
|
if (MOZ_UNLIKELY(!childObj))
|
|
|
|
return xpc::Throw(cx, NS_ERROR_FAILURE);
|
2014-01-29 22:34:25 -08:00
|
|
|
FillPropertyDescriptor(desc, wrapper, ObjectValue(*childObj),
|
|
|
|
/* readOnly = */ true);
|
2013-08-12 04:09:14 -07:00
|
|
|
return JS_WrapPropertyDescriptor(cx, desc);
|
2013-03-14 22:38:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!desc.object() &&
|
2013-02-08 06:24:21 -08:00
|
|
|
id == nsXPConnect::GetRuntimeInstance()->GetStringID(XPCJSRuntime::IDX_TO_STRING))
|
|
|
|
{
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2014-03-04 10:05:07 -08:00
|
|
|
JSFunction *toString = JS_NewFunction(cx, XrayToString, 0, 0, wrapper, "toString");
|
2011-08-20 06:53:32 -07:00
|
|
|
if (!toString)
|
|
|
|
return false;
|
|
|
|
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.object().set(wrapper);
|
|
|
|
desc.setAttributes(0);
|
2013-10-10 13:35:53 -07:00
|
|
|
desc.setGetter(nullptr);
|
|
|
|
desc.setSetter(nullptr);
|
2013-04-30 10:29:40 -07:00
|
|
|
desc.value().setObject(*JS_GetFunctionObject(toString));
|
2011-08-20 06:53:32 -07:00
|
|
|
}
|
2011-05-27 08:06:42 -07:00
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// If we're a special scope for in-content XBL, our script expects to see
|
|
|
|
// the bound XBL methods and attributes when accessing content. However,
|
|
|
|
// these members are implemented in content via custom-spliced prototypes,
|
|
|
|
// and thus aren't visible through Xray wrappers unless we handle them
|
|
|
|
// explicitly. So we check if we're running in such a scope, and if so,
|
|
|
|
// whether the wrappee is a bound element. If it is, we do a lookup via
|
|
|
|
// specialized XBL machinery.
|
|
|
|
//
|
|
|
|
// While we have to do some sketchy walking through content land, we should
|
|
|
|
// be protected by read-only/non-configurable properties, and any functions
|
2013-07-30 08:43:46 -07:00
|
|
|
// we end up with should _always_ be living in an XBL scope (usually ours,
|
|
|
|
// but could be another if the node has been adopted).
|
|
|
|
//
|
|
|
|
// Make sure to assert this.
|
2013-02-08 06:24:21 -08:00
|
|
|
nsCOMPtr<nsIContent> content;
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!desc.object() &&
|
2014-06-02 18:04:21 -07:00
|
|
|
EnsureCompartmentPrivate(wrapper)->scope->IsContentXBLScope() &&
|
2013-02-08 06:24:21 -08:00
|
|
|
(content = do_QueryInterfaceNative(cx, wrapper)))
|
|
|
|
{
|
2013-08-12 04:09:14 -07:00
|
|
|
if (!nsContentUtils::LookupBindingMember(cx, content, id, desc))
|
2013-02-08 06:24:21 -08:00
|
|
|
return false;
|
2013-04-30 10:29:40 -07:00
|
|
|
DEBUG_CheckXBLLookup(cx, desc.address());
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have nothing, we're done.
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!desc.object())
|
2013-07-01 00:14:36 -07:00
|
|
|
return true;
|
2013-02-08 06:24:21 -08:00
|
|
|
|
2014-04-30 02:10:33 -07:00
|
|
|
if (!JS_DefinePropertyById(cx, holder, id, desc.value(), desc.attributes(),
|
|
|
|
desc.getter(), desc.setter()) ||
|
2014-04-25 14:11:01 -07:00
|
|
|
!JS_GetPropertyDescriptorById(cx, holder, id, desc))
|
2013-01-22 21:04:38 -08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-30 10:29:40 -07:00
|
|
|
MOZ_ASSERT(desc.object());
|
|
|
|
desc.object().set(wrapper);
|
2013-01-22 21:04:38 -08:00
|
|
|
return true;
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
2011-05-25 08:30:50 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::getOwnPropertyDescriptor(JSContext *cx, HandleObject wrapper, HandleId id,
|
2014-04-25 14:11:01 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> desc)
|
2011-05-25 08:30:50 -07:00
|
|
|
{
|
2014-02-13 10:54:07 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, id, BaseProxyHandler::GET | BaseProxyHandler::SET);
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject holder(cx, Traits::singleton.ensureHolder(cx, wrapper));
|
2012-03-27 16:31:37 -07:00
|
|
|
if (Traits::isResolving(cx, holder, id)) {
|
2013-10-10 13:35:53 -07:00
|
|
|
desc.object().set(nullptr);
|
2012-03-27 16:31:37 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2013-04-24 02:45:41 -07:00
|
|
|
typename Traits::ResolvingIdImpl resolving(cx, wrapper, id);
|
2012-03-27 16:31:37 -07:00
|
|
|
|
|
|
|
// NB: Nothing we do here acts on the wrapped native itself, so we don't
|
|
|
|
// enter our policy.
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2014-04-25 14:11:02 -07:00
|
|
|
if (!Traits::singleton.resolveOwnProperty(cx, *this, wrapper, holder, id, desc))
|
2011-08-20 06:53:32 -07:00
|
|
|
return false;
|
2013-04-30 10:29:40 -07:00
|
|
|
if (desc.object())
|
|
|
|
desc.object().set(wrapper);
|
2011-12-06 11:05:26 -08:00
|
|
|
return true;
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
|
|
|
|
2013-05-17 10:44:25 -07:00
|
|
|
// Consider what happens when chrome does |xray.expando = xray.wrappedJSObject|.
|
|
|
|
//
|
|
|
|
// Since the expando comes from the target compartment, wrapping it back into
|
|
|
|
// the target compartment to define it on the expando object ends up stripping
|
|
|
|
// off the Xray waiver that gives |xray| and |xray.wrappedJSObject| different
|
|
|
|
// identities. This is generally the right thing to do when wrapping across
|
|
|
|
// compartments, but is incorrect in the special case of the Xray expando
|
|
|
|
// object. Manually re-apply Xrays if necessary.
|
|
|
|
//
|
|
|
|
// NB: In order to satisfy the invariants of WaiveXray, we need to pass
|
|
|
|
// in an object sans security wrapper, which means we need to strip off any
|
|
|
|
// potential same-compartment security wrapper that may have been applied
|
|
|
|
// to the content object. This is ok, because the the expando object is only
|
|
|
|
// ever accessed by code across the compartment boundary.
|
|
|
|
static bool
|
2013-07-25 22:52:59 -07:00
|
|
|
RecreateLostWaivers(JSContext *cx, JSPropertyDescriptor *orig,
|
|
|
|
MutableHandle<JSPropertyDescriptor> wrapped)
|
2013-05-17 10:44:25 -07:00
|
|
|
{
|
|
|
|
// Compute whether the original objects were waived, and implicitly, whether
|
|
|
|
// they were objects at all.
|
|
|
|
bool valueWasWaived =
|
|
|
|
orig->value.isObject() &&
|
|
|
|
WrapperFactory::HasWaiveXrayFlag(&orig->value.toObject());
|
|
|
|
bool getterWasWaived =
|
|
|
|
(orig->attrs & JSPROP_GETTER) &&
|
|
|
|
WrapperFactory::HasWaiveXrayFlag(JS_FUNC_TO_DATA_PTR(JSObject*, orig->getter));
|
|
|
|
bool setterWasWaived =
|
|
|
|
(orig->attrs & JSPROP_SETTER) &&
|
|
|
|
WrapperFactory::HasWaiveXrayFlag(JS_FUNC_TO_DATA_PTR(JSObject*, orig->setter));
|
|
|
|
|
|
|
|
// Recreate waivers. Note that for value, we need an extra UncheckedUnwrap
|
|
|
|
// to handle same-compartment security wrappers (see above). This should
|
|
|
|
// never happen for getters/setters.
|
|
|
|
|
|
|
|
RootedObject rewaived(cx);
|
|
|
|
if (valueWasWaived && !IsCrossCompartmentWrapper(&wrapped.value().toObject())) {
|
|
|
|
rewaived = &wrapped.value().toObject();
|
|
|
|
rewaived = WrapperFactory::WaiveXray(cx, UncheckedUnwrap(rewaived));
|
|
|
|
NS_ENSURE_TRUE(rewaived, false);
|
|
|
|
wrapped.value().set(ObjectValue(*rewaived));
|
|
|
|
}
|
|
|
|
if (getterWasWaived && !IsCrossCompartmentWrapper(wrapped.getterObject())) {
|
|
|
|
MOZ_ASSERT(CheckedUnwrap(wrapped.getterObject()));
|
|
|
|
rewaived = WrapperFactory::WaiveXray(cx, wrapped.getterObject());
|
|
|
|
NS_ENSURE_TRUE(rewaived, false);
|
|
|
|
wrapped.setGetterObject(rewaived);
|
|
|
|
}
|
|
|
|
if (setterWasWaived && !IsCrossCompartmentWrapper(wrapped.setterObject())) {
|
|
|
|
MOZ_ASSERT(CheckedUnwrap(wrapped.setterObject()));
|
|
|
|
rewaived = WrapperFactory::WaiveXray(cx, wrapped.setterObject());
|
|
|
|
NS_ENSURE_TRUE(rewaived, false);
|
|
|
|
wrapped.setSetterObject(rewaived);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
2011-05-25 08:30:50 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::defineProperty(JSContext *cx, HandleObject wrapper,
|
2013-07-25 22:52:59 -07:00
|
|
|
HandleId id, MutableHandle<JSPropertyDescriptor> desc)
|
2011-05-25 08:30:50 -07:00
|
|
|
{
|
2014-02-13 10:54:07 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, id, BaseProxyHandler::SET);
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2013-07-25 22:52:59 -07:00
|
|
|
Rooted<JSPropertyDescriptor> existing_desc(cx);
|
2014-06-05 22:32:38 -07:00
|
|
|
if (!JS_GetPropertyDescriptorById(cx, wrapper, id, &existing_desc))
|
2011-08-20 06:53:32 -07:00
|
|
|
return false;
|
|
|
|
|
2014-06-05 22:32:38 -07:00
|
|
|
// Note that the check here is intended to differentiate between own and
|
|
|
|
// non-own properties, since the above lookup is not limited to own
|
|
|
|
// properties. At present, this may not always do the right thing because
|
|
|
|
// we often lie (sloppily) about where we found properties and set
|
|
|
|
// desc.object() to |wrapper|. Once we fully fix our Xray prototype semantics,
|
|
|
|
// this should work as intended.
|
2014-06-11 15:16:06 -07:00
|
|
|
if (existing_desc.object() == wrapper && existing_desc.isPermanent()) {
|
|
|
|
// We have a non-configurable property. See if the caller is trying to
|
|
|
|
// re-configure it in any way other than making it non-writable.
|
|
|
|
if (existing_desc.hasGetterOrSetterObject() || desc.hasGetterOrSetterObject() ||
|
|
|
|
existing_desc.isEnumerable() != desc.isEnumerable() ||
|
|
|
|
(existing_desc.isReadonly() && !desc.isReadonly()))
|
|
|
|
{
|
|
|
|
// We should technically report non-configurability in strict mode, but
|
|
|
|
// doing that via JSAPI is a lot of trouble.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (existing_desc.isReadonly()) {
|
|
|
|
// Same as the above for non-writability.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-10-05 09:59:24 -07:00
|
|
|
bool defined = false;
|
2014-06-05 22:32:39 -07:00
|
|
|
if (!Traits::singleton.defineProperty(cx, wrapper, id, desc, existing_desc, &defined))
|
2012-10-05 09:59:24 -07:00
|
|
|
return false;
|
|
|
|
if (defined)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// We're placing an expando. The expando objects live in the target
|
|
|
|
// compartment, so we need to enter it.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject target(cx, Traits::singleton.getTargetObject(wrapper));
|
2012-10-05 09:59:24 -07:00
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
|
|
|
|
// Grab the relevant expando object.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject expandoObject(cx, Traits::singleton.ensureExpandoObject(cx, wrapper,
|
|
|
|
target));
|
2012-10-05 09:59:24 -07:00
|
|
|
if (!expandoObject)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Wrap the property descriptor for the target compartment.
|
2013-07-25 22:52:59 -07:00
|
|
|
Rooted<JSPropertyDescriptor> wrappedDesc(cx, desc);
|
2013-08-12 04:09:14 -07:00
|
|
|
if (!JS_WrapPropertyDescriptor(cx, &wrappedDesc))
|
2012-10-05 09:59:24 -07:00
|
|
|
return false;
|
|
|
|
|
2013-05-17 10:44:25 -07:00
|
|
|
// Fix up Xray waivers.
|
2013-04-30 10:29:40 -07:00
|
|
|
if (!RecreateLostWaivers(cx, desc.address(), &wrappedDesc))
|
2013-05-17 10:44:25 -07:00
|
|
|
return false;
|
|
|
|
|
2013-04-24 02:45:41 -07:00
|
|
|
return JS_DefinePropertyById(cx, expandoObject, id, wrappedDesc.value(),
|
2014-04-30 02:10:33 -07:00
|
|
|
wrappedDesc.get().attrs,
|
|
|
|
wrappedDesc.getter(), wrappedDesc.setter());
|
2011-08-20 06:53:32 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::getOwnPropertyNames(JSContext *cx, HandleObject wrapper,
|
|
|
|
AutoIdVector &props)
|
2011-08-20 06:53:32 -07:00
|
|
|
{
|
2014-02-13 10:54:08 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, JSID_VOID, BaseProxyHandler::ENUMERATE);
|
2012-03-27 16:31:37 -07:00
|
|
|
return enumerate(cx, wrapper, JSITER_OWNONLY | JSITER_HIDDEN, props);
|
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::delete_(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleId id, bool *bp)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
2014-02-13 10:54:07 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, id, BaseProxyHandler::SET);
|
2012-03-27 16:31:37 -07:00
|
|
|
|
2012-10-05 09:59:24 -07:00
|
|
|
// Check the expando object.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject target(cx, Traits::getTargetObject(wrapper));
|
|
|
|
RootedObject expando(cx, Traits::singleton.getExpandoObject(cx, target, wrapper));
|
2012-10-05 09:59:24 -07:00
|
|
|
if (expando) {
|
|
|
|
JSAutoCompartment ac(cx, expando);
|
2013-08-05 06:01:53 -07:00
|
|
|
return JS_DeletePropertyById2(cx, expando, id, bp);
|
2012-10-05 09:59:24 -07:00
|
|
|
}
|
2014-06-05 22:32:39 -07:00
|
|
|
|
|
|
|
return Traits::singleton.delete_(cx, wrapper, id, bp);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::enumerate(JSContext *cx, HandleObject wrapper, unsigned flags,
|
|
|
|
AutoIdVector &props)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
2014-02-13 10:54:08 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, JSID_VOID, BaseProxyHandler::ENUMERATE);
|
2012-09-11 01:05:10 -07:00
|
|
|
if (!AccessCheck::wrapperSubsumes(wrapper)) {
|
2011-08-20 06:53:32 -07:00
|
|
|
JS_ReportError(cx, "Not allowed to enumerate cross origin objects");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-05 09:59:24 -07:00
|
|
|
// Enumerate expando properties first. Note that the expando object lives
|
|
|
|
// in the target compartment.
|
2013-03-27 04:33:30 -07:00
|
|
|
RootedObject target(cx, Traits::singleton.getTargetObject(wrapper));
|
|
|
|
RootedObject expando(cx, Traits::singleton.getExpandoObject(cx, target, wrapper));
|
2012-10-05 09:59:24 -07:00
|
|
|
if (expando) {
|
|
|
|
JSAutoCompartment ac(cx, expando);
|
|
|
|
if (!js::GetPropertyNames(cx, expando, flags, &props))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!JS_WrapAutoIdVector(cx, props))
|
|
|
|
return false;
|
|
|
|
|
2014-03-23 07:02:13 -07:00
|
|
|
return Traits::singleton.enumerateNames(cx, wrapper, flags, props);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::enumerate(JSContext *cx, HandleObject wrapper,
|
|
|
|
AutoIdVector &props)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
|
|
|
return enumerate(cx, wrapper, 0, props);
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
2011-05-25 08:30:50 -07:00
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::get(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject receiver, HandleId id,
|
|
|
|
MutableHandleValue vp)
|
2011-05-25 08:30:50 -07:00
|
|
|
{
|
2012-03-27 16:31:37 -07:00
|
|
|
// Skip our Base if it isn't already ProxyHandler.
|
|
|
|
// NB: None of the functions we call are prepared for the receiver not
|
|
|
|
// being the wrapper, so ignore the receiver here.
|
2014-02-21 15:55:31 -08:00
|
|
|
return js::BaseProxyHandler::get(cx, wrapper, Traits::HasPrototype ? receiver : wrapper, id, vp);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::set(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleObject receiver, HandleId id,
|
|
|
|
bool strict, MutableHandleValue vp)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
2014-04-25 13:07:18 -07:00
|
|
|
// Delegate to Traits.
|
2012-03-27 16:31:37 -07:00
|
|
|
// NB: None of the functions we call are prepared for the receiver not
|
|
|
|
// being the wrapper, so ignore the receiver here.
|
2014-04-25 13:07:18 -07:00
|
|
|
return Traits::set(cx, wrapper, Traits::HasPrototype ? receiver : wrapper, id, strict, vp);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::has(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleId id, bool *bp)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already ProxyHandler.
|
2013-03-27 04:33:30 -07:00
|
|
|
return js::BaseProxyHandler::has(cx, wrapper, id, bp);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::hasOwn(JSContext *cx, HandleObject wrapper,
|
|
|
|
HandleId id, bool *bp)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already ProxyHandler.
|
2013-03-27 04:33:30 -07:00
|
|
|
return js::BaseProxyHandler::hasOwn(cx, wrapper, id, bp);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::keys(JSContext *cx, HandleObject wrapper,
|
|
|
|
AutoIdVector &props)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already ProxyHandler.
|
2013-03-27 04:33:30 -07:00
|
|
|
return js::BaseProxyHandler::keys(cx, wrapper, props);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
2011-08-20 06:53:32 -07:00
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-03-27 04:33:30 -07:00
|
|
|
XrayWrapper<Base, Traits>::iterate(JSContext *cx, HandleObject wrapper,
|
|
|
|
unsigned flags, MutableHandleValue vp)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
|
|
|
// Skip our Base if it isn't already ProxyHandler.
|
2013-03-27 04:33:30 -07:00
|
|
|
return js::BaseProxyHandler::iterate(cx, wrapper, flags, vp);
|
2012-03-27 16:31:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
2013-04-04 00:02:24 -07:00
|
|
|
XrayWrapper<Base, Traits>::call(JSContext *cx, HandleObject wrapper, const JS::CallArgs &args)
|
2012-03-27 16:31:37 -07:00
|
|
|
{
|
2014-02-13 10:54:07 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, JSID_VOID, BaseProxyHandler::CALL);
|
2013-04-25 16:03:05 -07:00
|
|
|
return Traits::call(cx, wrapper, args, Base::singleton);
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 16:31:37 -07:00
|
|
|
template <typename Base, typename Traits>
|
2011-05-25 08:30:50 -07:00
|
|
|
bool
|
2013-04-04 00:02:24 -07:00
|
|
|
XrayWrapper<Base, Traits>::construct(JSContext *cx, HandleObject wrapper, const JS::CallArgs &args)
|
2011-05-25 08:30:50 -07:00
|
|
|
{
|
2014-02-13 10:54:07 -08:00
|
|
|
assertEnteredPolicy(cx, wrapper, JSID_VOID, BaseProxyHandler::CALL);
|
2013-04-25 16:03:05 -07:00
|
|
|
return Traits::construct(cx, wrapper, args, Base::singleton);
|
2011-05-25 08:30:50 -07:00
|
|
|
}
|
|
|
|
|
2013-05-31 10:36:01 -07:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Traits>::defaultValue(JSContext *cx, HandleObject wrapper,
|
|
|
|
JSType hint, MutableHandleValue vp)
|
|
|
|
{
|
|
|
|
// Even if this isn't a security wrapper, Xray semantics dictate that we
|
|
|
|
// run the DefaultValue algorithm directly on the Xray wrapper.
|
|
|
|
//
|
|
|
|
// NB: We don't have to worry about things with special [[DefaultValue]]
|
|
|
|
// behavior like Date because we'll never have an XrayWrapper to them.
|
|
|
|
return js::DefaultValue(cx, wrapper, hint, vp);
|
|
|
|
}
|
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Traits>::getPrototypeOf(JSContext *cx, JS::HandleObject wrapper,
|
|
|
|
JS::MutableHandleObject protop)
|
|
|
|
{
|
2014-01-29 13:07:13 -08:00
|
|
|
// We really only want this override for non-SecurityWrapper-inheriting
|
|
|
|
// |Base|. But doing that statically with templates requires partial method
|
|
|
|
// specializations (and therefore a helper class), which is all more trouble
|
|
|
|
// than it's worth. Do a dynamic check.
|
|
|
|
if (Base::hasSecurityPolicy())
|
|
|
|
return Base::getPrototypeOf(cx, wrapper, protop);
|
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
RootedObject target(cx, Traits::getTargetObject(wrapper));
|
|
|
|
RootedObject expando(cx, Traits::singleton.getExpandoObject(cx, target, wrapper));
|
|
|
|
|
2014-02-21 15:55:30 -08:00
|
|
|
// We want to keep the Xray's prototype distinct from that of content, but
|
|
|
|
// only if there's been a set. If there's not an expando, or the expando
|
|
|
|
// slot is |undefined|, hand back the default proto, appropriately wrapped.
|
2013-12-13 12:01:30 -08:00
|
|
|
|
|
|
|
RootedValue v(cx);
|
2014-02-21 15:55:30 -08:00
|
|
|
if (expando) {
|
2013-12-13 12:01:30 -08:00
|
|
|
JSAutoCompartment ac(cx, expando);
|
|
|
|
v = JS_GetReservedSlot(expando, JSSLOT_EXPANDO_PROTOTYPE);
|
|
|
|
}
|
|
|
|
if (v.isUndefined())
|
2014-02-21 15:55:30 -08:00
|
|
|
return getPrototypeOfHelper(cx, wrapper, target, protop);
|
2013-12-13 12:01:30 -08:00
|
|
|
|
|
|
|
protop.set(v.toObjectOrNull());
|
|
|
|
return JS_WrapObject(cx, protop);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename Traits>
|
|
|
|
bool
|
|
|
|
XrayWrapper<Base, Traits>::setPrototypeOf(JSContext *cx, JS::HandleObject wrapper,
|
|
|
|
JS::HandleObject proto, bool *bp)
|
|
|
|
{
|
2014-01-29 13:07:13 -08:00
|
|
|
// Do this only for non-SecurityWrapper-inheriting |Base|. See the comment
|
|
|
|
// in getPrototypeOf().
|
|
|
|
if (Base::hasSecurityPolicy())
|
|
|
|
return Base::setPrototypeOf(cx, wrapper, proto, bp);
|
|
|
|
|
2013-12-13 12:01:30 -08:00
|
|
|
RootedObject target(cx, Traits::getTargetObject(wrapper));
|
|
|
|
RootedObject expando(cx, Traits::singleton.ensureExpandoObject(cx, wrapper, target));
|
|
|
|
|
|
|
|
// The expando lives in the target's compartment, so do our installation there.
|
|
|
|
JSAutoCompartment ac(cx, target);
|
|
|
|
|
|
|
|
RootedValue v(cx, ObjectValue(*proto));
|
|
|
|
if (!JS_WrapValue(cx, &v))
|
|
|
|
return false;
|
|
|
|
JS_SetReservedSlot(expando, JSSLOT_EXPANDO_PROTOTYPE, v);
|
|
|
|
*bp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-31 10:36:01 -07:00
|
|
|
|
2012-11-14 09:56:25 -08:00
|
|
|
/*
|
|
|
|
* The Permissive / Security variants should be used depending on whether the
|
|
|
|
* compartment of the wrapper is guranteed to subsume the compartment of the
|
|
|
|
* wrapped object (i.e. - whether it is safe from a security perspective to
|
|
|
|
* unwrap the wrapper).
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<>
|
|
|
|
PermissiveXrayXPCWN PermissiveXrayXPCWN::singleton(0);
|
|
|
|
template class PermissiveXrayXPCWN;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
SecurityXrayXPCWN SecurityXrayXPCWN::singleton(0);
|
|
|
|
template class SecurityXrayXPCWN;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
PermissiveXrayDOM PermissiveXrayDOM::singleton(0);
|
|
|
|
template class PermissiveXrayDOM;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
SecurityXrayDOM SecurityXrayDOM::singleton(0);
|
|
|
|
template class SecurityXrayDOM;
|
|
|
|
|
2014-03-23 07:02:12 -07:00
|
|
|
template<>
|
|
|
|
PermissiveXrayJS PermissiveXrayJS::singleton(0);
|
|
|
|
template class PermissiveXrayJS;
|
|
|
|
|
2012-11-14 09:56:25 -08:00
|
|
|
template<>
|
|
|
|
SCSecurityXrayXPCWN SCSecurityXrayXPCWN::singleton(0);
|
|
|
|
template class SCSecurityXrayXPCWN;
|
2011-05-25 08:30:50 -07:00
|
|
|
|
2012-09-26 07:17:51 -07:00
|
|
|
static nsQueryInterface
|
2013-03-27 04:33:30 -07:00
|
|
|
do_QueryInterfaceNative(JSContext* cx, HandleObject wrapper)
|
2012-09-26 07:17:51 -07:00
|
|
|
{
|
|
|
|
nsISupports* nativeSupports;
|
|
|
|
if (IsWrapper(wrapper) && WrapperFactory::IsXrayWrapper(wrapper)) {
|
2013-04-24 06:22:21 -07:00
|
|
|
RootedObject target(cx, XrayTraits::getTargetObject(wrapper));
|
2012-09-26 07:17:51 -07:00
|
|
|
if (GetXrayType(target) == XrayForDOMObject) {
|
2013-05-31 23:56:02 -07:00
|
|
|
nativeSupports = UnwrapDOMObjectToISupports(target);
|
2012-09-26 07:17:51 -07:00
|
|
|
} else {
|
2013-06-23 00:15:55 -07:00
|
|
|
XPCWrappedNative *wn = XPCWrappedNative::Get(target);
|
2012-09-26 07:17:51 -07:00
|
|
|
nativeSupports = wn->Native();
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-16 22:49:41 -07:00
|
|
|
nsIXPConnect *xpc = nsXPConnect::XPConnect();
|
2012-09-26 07:17:51 -07:00
|
|
|
nativeSupports = xpc->GetNativeOfWrapper(cx, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsQueryInterface(nativeSupports);
|
|
|
|
}
|
|
|
|
|
2010-07-02 14:09:48 -07:00
|
|
|
}
|