2012-05-22 13:46:20 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: set ts=2 sw=2 et tw=99 ft=cpp: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-08-24 02:42:39 +00:00
|
|
|
#include "mozilla/dom/DOMJSProxyHandler.h"
|
2012-05-22 13:46:20 +00:00
|
|
|
#include "xpcpublic.h"
|
|
|
|
#include "xpcprivate.h"
|
|
|
|
#include "XPCQuickStubs.h"
|
|
|
|
#include "XPCWrapper.h"
|
|
|
|
#include "WrapperFactory.h"
|
|
|
|
#include "nsDOMClassInfo.h"
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
|
|
|
using namespace JS;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-12-17 02:27:43 +00:00
|
|
|
jsid s_length_id = JSID_VOID;
|
2012-05-22 13:46:20 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
DefineStaticJSVals(JSContext* cx)
|
|
|
|
{
|
|
|
|
return InternJSString(cx, s_length_id, "length");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-24 15:21:22 +00:00
|
|
|
const char HandlerFamily = 0;
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2013-06-06 18:32:26 +00:00
|
|
|
js::DOMProxyShadowsResult
|
2013-06-21 13:12:46 +00:00
|
|
|
DOMProxyShadows(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id)
|
2013-04-29 13:17:59 +00:00
|
|
|
{
|
|
|
|
JS::Value v = js::GetProxyExtra(proxy, JSPROXYSLOT_EXPANDO);
|
|
|
|
if (v.isObject()) {
|
2013-08-08 22:53:04 +00:00
|
|
|
bool hasOwn;
|
2014-01-22 11:28:06 +00:00
|
|
|
Rooted<JSObject*> object(cx, &v.toObject());
|
|
|
|
if (!JS_AlreadyHasOwnPropertyById(cx, object, id, &hasOwn))
|
2013-04-29 13:17:59 +00:00
|
|
|
return js::ShadowCheckFailed;
|
|
|
|
|
|
|
|
return hasOwn ? js::Shadows : js::DoesntShadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v.isUndefined()) {
|
|
|
|
return js::DoesntShadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasOwn;
|
|
|
|
if (!GetProxyHandler(proxy)->hasOwn(cx, proxy, id, &hasOwn))
|
|
|
|
return js::ShadowCheckFailed;
|
|
|
|
|
|
|
|
return hasOwn ? js::Shadows : js::DoesntShadowUnique;
|
|
|
|
}
|
|
|
|
|
2012-08-24 16:32:26 +00:00
|
|
|
// Store the information for the specialized ICs.
|
2013-06-06 18:32:26 +00:00
|
|
|
struct SetDOMProxyInformation
|
2012-08-24 16:32:26 +00:00
|
|
|
{
|
2013-06-06 18:32:26 +00:00
|
|
|
SetDOMProxyInformation() {
|
2013-09-24 15:21:22 +00:00
|
|
|
js::SetDOMProxyInformation((const void*) &HandlerFamily,
|
2013-06-21 05:39:22 +00:00
|
|
|
js::PROXY_EXTRA_SLOT + JSPROXYSLOT_EXPANDO, DOMProxyShadows);
|
2012-08-24 16:32:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-06 18:32:26 +00:00
|
|
|
SetDOMProxyInformation gSetDOMProxyInformation;
|
2012-08-24 16:32:26 +00:00
|
|
|
|
2013-04-16 17:02:57 +00:00
|
|
|
// static
|
|
|
|
JSObject*
|
|
|
|
DOMProxyHandler::GetAndClearExpandoObject(JSObject* obj)
|
|
|
|
{
|
2013-04-20 16:04:09 +00:00
|
|
|
MOZ_ASSERT(IsDOMProxy(obj), "expected a DOM proxy object");
|
|
|
|
JS::Value v = js::GetProxyExtra(obj, JSPROXYSLOT_EXPANDO);
|
|
|
|
if (v.isUndefined()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v.isObject()) {
|
|
|
|
js::SetProxyExtra(obj, JSPROXYSLOT_EXPANDO, UndefinedValue());
|
2013-11-20 15:37:30 +00:00
|
|
|
XPCWrappedNativeScope* scope = xpc::MaybeGetObjectScope(obj);
|
|
|
|
if (scope) {
|
|
|
|
scope->RemoveDOMExpandoObject(obj);
|
|
|
|
}
|
2012-12-12 02:45:36 +00:00
|
|
|
} else {
|
|
|
|
js::ExpandoAndGeneration* expandoAndGeneration =
|
|
|
|
static_cast<js::ExpandoAndGeneration*>(v.toPrivate());
|
|
|
|
v = expandoAndGeneration->expando;
|
|
|
|
if (v.isUndefined()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
expandoAndGeneration->expando = UndefinedValue();
|
2013-04-20 16:04:09 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 02:45:36 +00:00
|
|
|
|
2013-04-20 16:04:09 +00:00
|
|
|
return &v.toObject();
|
2013-04-16 17:02:57 +00:00
|
|
|
}
|
2012-08-24 16:32:26 +00:00
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
// static
|
|
|
|
JSObject*
|
2013-05-03 23:29:09 +00:00
|
|
|
DOMProxyHandler::EnsureExpandoObject(JSContext* cx, JS::Handle<JSObject*> obj)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsDOMProxy(obj), "expected a DOM proxy object");
|
2013-04-20 16:04:09 +00:00
|
|
|
JS::Value v = js::GetProxyExtra(obj, JSPROXYSLOT_EXPANDO);
|
|
|
|
if (v.isObject()) {
|
|
|
|
return &v.toObject();
|
|
|
|
}
|
2012-05-22 13:46:20 +00:00
|
|
|
|
2013-04-20 16:04:09 +00:00
|
|
|
js::ExpandoAndGeneration* expandoAndGeneration;
|
|
|
|
if (!v.isUndefined()) {
|
|
|
|
expandoAndGeneration = static_cast<js::ExpandoAndGeneration*>(v.toPrivate());
|
|
|
|
if (expandoAndGeneration->expando.isObject()) {
|
|
|
|
return &expandoAndGeneration->expando.toObject();
|
2013-05-04 23:27:20 +00:00
|
|
|
}
|
2013-04-20 16:04:09 +00:00
|
|
|
} else {
|
|
|
|
expandoAndGeneration = nullptr;
|
|
|
|
}
|
2013-04-20 16:04:09 +00:00
|
|
|
|
2014-01-16 17:48:58 +00:00
|
|
|
JS::Rooted<JSObject*> parent(cx, js::GetObjectParent(obj));
|
2013-05-08 02:34:56 +00:00
|
|
|
JS::Rooted<JSObject*> expando(cx,
|
2014-01-16 17:48:58 +00:00
|
|
|
JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), parent));
|
2013-04-20 16:04:09 +00:00
|
|
|
if (!expando) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-04-20 16:04:09 +00:00
|
|
|
|
2013-05-17 15:04:08 +00:00
|
|
|
nsISupports* native = UnwrapDOMObject<nsISupports>(obj);
|
|
|
|
nsWrapperCache* cache;
|
|
|
|
CallQueryInterface(native, &cache);
|
|
|
|
if (expandoAndGeneration) {
|
2013-06-23 07:15:42 +00:00
|
|
|
cache->PreserveWrapper(native);
|
2013-05-17 15:04:08 +00:00
|
|
|
expandoAndGeneration->expando.setObject(*expando);
|
|
|
|
|
|
|
|
return expando;
|
|
|
|
}
|
|
|
|
|
2013-04-20 16:04:09 +00:00
|
|
|
XPCWrappedNativeScope* scope = xpc::GetObjectScope(obj);
|
|
|
|
if (!scope->RegisterDOMExpandoObject(obj)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->SetPreservingWrapper(true);
|
2013-05-17 15:04:08 +00:00
|
|
|
js::SetProxyExtra(obj, JSPROXYSLOT_EXPANDO, ObjectValue(*expando));
|
2013-04-20 16:04:09 +00:00
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
return expando;
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:43:03 +00:00
|
|
|
bool
|
2013-06-28 21:01:09 +00:00
|
|
|
DOMProxyHandler::isExtensible(JSContext *cx, JS::Handle<JSObject*> proxy, bool *extensible)
|
2013-03-23 02:43:03 +00:00
|
|
|
{
|
2013-06-28 21:01:09 +00:00
|
|
|
// always extensible per WebIDL
|
|
|
|
*extensible = true;
|
|
|
|
return true;
|
2013-03-23 02:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DOMProxyHandler::preventExtensions(JSContext *cx, JS::Handle<JSObject*> proxy)
|
|
|
|
{
|
|
|
|
// Throw a TypeError, per WebIDL.
|
2013-10-28 14:04:12 +00:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr,
|
|
|
|
JSMSG_CANT_CHANGE_EXTENSIBILITY);
|
2013-03-23 02:43:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
bool
|
2013-07-09 14:45:13 +00:00
|
|
|
BaseDOMProxyHandler::getPropertyDescriptor(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> proxy,
|
|
|
|
JS::Handle<jsid> id,
|
|
|
|
MutableHandle<JSPropertyDescriptor> desc,
|
|
|
|
unsigned flags)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-01-03 21:31:36 +00:00
|
|
|
if (!getOwnPropertyDescriptor(cx, proxy, id, desc, flags)) {
|
2012-05-22 13:46:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-30 17:29:40 +00:00
|
|
|
if (desc.object()) {
|
2012-05-22 13:46:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-05-05 07:03:14 +00:00
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
2012-09-03 23:42:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 13:46:20 +00:00
|
|
|
if (!proto) {
|
2013-04-30 17:29:40 +00:00
|
|
|
desc.object().set(nullptr);
|
2012-05-22 13:46:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-12 11:09:14 +00:00
|
|
|
return JS_GetPropertyDescriptorById(cx, proto, id, 0, desc);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 22:23:48 +00:00
|
|
|
DOMProxyHandler::defineProperty(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
2013-04-30 17:29:40 +00:00
|
|
|
MutableHandle<JSPropertyDescriptor> desc, bool* defined)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-04-30 17:29:40 +00:00
|
|
|
if (desc.hasGetterObject() && desc.setter() == JS_StrictPropertyStub) {
|
2012-05-22 13:46:20 +00:00
|
|
|
return JS_ReportErrorFlagsAndNumber(cx,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT |
|
|
|
|
JSREPORT_STRICT_MODE_ERROR,
|
2013-10-28 14:04:12 +00:00
|
|
|
js_GetErrorMessage, nullptr,
|
2012-05-22 13:46:20 +00:00
|
|
|
JSMSG_GETTER_ONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xpc::WrapperFactory::IsXrayWrapper(proxy)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* expando = EnsureExpandoObject(cx, proxy);
|
|
|
|
if (!expando) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:53:04 +00:00
|
|
|
bool dummy;
|
2013-04-30 17:29:40 +00:00
|
|
|
return js_DefineOwnProperty(cx, expando, id, desc, &dummy);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-03-21 22:23:48 +00:00
|
|
|
DOMProxyHandler::delete_(JSContext* cx, JS::Handle<JSObject*> proxy,
|
2013-03-21 22:23:48 +00:00
|
|
|
JS::Handle<jsid> id, bool* bp)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JSObject*> expando(cx);
|
2012-05-22 13:46:20 +00:00
|
|
|
if (!xpc::WrapperFactory::IsXrayWrapper(proxy) && (expando = GetExpandoObject(proxy))) {
|
2013-08-05 13:01:53 +00:00
|
|
|
return JS_DeletePropertyById2(cx, expando, id, bp);
|
2012-05-22 13:46:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 13:01:53 +00:00
|
|
|
*bp = true;
|
2012-05-22 13:46:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-07-09 14:45:13 +00:00
|
|
|
BaseDOMProxyHandler::enumerate(JSContext* cx, JS::Handle<JSObject*> proxy,
|
|
|
|
AutoIdVector& props)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-07-31 16:20:33 +00:00
|
|
|
if (!JS_GetPrototype(cx, proxy, &proto)) {
|
2012-09-03 23:42:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 13:46:20 +00:00
|
|
|
return getOwnPropertyNames(cx, proxy, props) &&
|
|
|
|
(!proto || js::GetPropertyNames(cx, proto, 0, &props));
|
|
|
|
}
|
|
|
|
|
2013-10-29 23:39:09 +00:00
|
|
|
bool
|
|
|
|
BaseDOMProxyHandler::watch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
|
|
|
|
JS::Handle<JSObject*> callable)
|
|
|
|
{
|
|
|
|
return js::WatchGuts(cx, proxy, id, callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BaseDOMProxyHandler::unwatch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id)
|
|
|
|
{
|
|
|
|
return js::UnwatchGuts(cx, proxy, id);
|
|
|
|
}
|
|
|
|
|
2012-05-22 13:46:20 +00:00
|
|
|
bool
|
2013-03-21 22:23:48 +00:00
|
|
|
DOMProxyHandler::has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
|
|
|
if (!hasOwn(cx, proxy, id, bp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*bp) {
|
|
|
|
// We have the property ourselves; no need to worry about our prototype
|
|
|
|
// chain.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OK, now we have to look at the proto
|
2013-05-03 23:29:09 +00:00
|
|
|
JS::Rooted<JSObject*> proto(cx);
|
2013-05-05 07:03:14 +00:00
|
|
|
if (!js::GetObjectProto(cx, proxy, &proto)) {
|
2012-09-03 23:42:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-05-22 13:46:20 +00:00
|
|
|
if (!proto) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-08 22:53:04 +00:00
|
|
|
bool protoHasProp;
|
2012-05-22 13:46:20 +00:00
|
|
|
bool ok = JS_HasPropertyById(cx, proto, id, &protoHasProp);
|
|
|
|
if (ok) {
|
|
|
|
*bp = protoHasProp;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2013-05-03 23:29:09 +00:00
|
|
|
IdToInt32(JSContext* cx, JS::Handle<jsid> id)
|
2012-05-22 13:46:20 +00:00
|
|
|
{
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Rooted<JS::Value> idval(cx);
|
2012-05-22 13:46:20 +00:00
|
|
|
double array_index;
|
|
|
|
int32_t i;
|
2014-01-17 18:08:51 +00:00
|
|
|
if (!::JS_IdToValue(cx, id, &idval) ||
|
2013-10-19 16:39:52 +00:00
|
|
|
!JS::ToNumber(cx, idval, &array_index) ||
|
2012-05-22 13:46:20 +00:00
|
|
|
!::JS_DoubleIsInt32(array_index, &i)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|