2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
2013-03-21 17:05:20 -07:00
|
|
|
#include "nsIContent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
2013-02-08 06:24:21 -08:00
|
|
|
#include "nsJSUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
2013-08-13 18:34:24 -07:00
|
|
|
#include "js/CharacterEncoding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXBLProtoImplField.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2007-09-28 06:45:01 -07:00
|
|
|
#include "nsIURI.h"
|
2011-11-03 13:39:08 -07:00
|
|
|
#include "nsXBLSerialize.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2013-02-08 06:24:21 -08:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-02-08 06:24:22 -08:00
|
|
|
#include "xpcpublic.h"
|
2013-04-03 11:41:23 -07:00
|
|
|
#include "WrapperFactory.h"
|
2013-02-08 06:24:21 -08:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const char16_t* aName, const char16_t* aReadOnly)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mNext(nullptr),
|
|
|
|
mFieldText(nullptr),
|
2007-03-22 10:30:00 -07:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
2007-09-28 06:45:01 -07:00
|
|
|
mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer?
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aReadOnly) {
|
2010-02-03 13:17:55 -08:00
|
|
|
nsAutoString readOnly; readOnly.Assign(aReadOnly);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (readOnly.LowerCaseEqualsLiteral("true"))
|
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 13:39:08 -07:00
|
|
|
|
2011-11-20 19:09:16 -08:00
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mNext(nullptr),
|
|
|
|
mFieldText(nullptr),
|
2011-11-03 13:39:08 -07:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
|
|
|
|
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aIsReadOnly)
|
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXBLProtoImplField::~nsXBLProtoImplField()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXBLProtoImplField);
|
|
|
|
if (mFieldText)
|
|
|
|
nsMemory::Free(mFieldText);
|
2007-09-28 06:45:01 -07:00
|
|
|
NS_Free(mName);
|
2008-12-15 03:33:56 -08:00
|
|
|
NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
|
|
|
|
{
|
|
|
|
if (mFieldText) {
|
|
|
|
nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
|
|
|
|
nsAutoString newFieldText = fieldTextStr + aText;
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* temp = mFieldText;
|
2007-03-22 10:30:00 -07:00
|
|
|
mFieldText = ToNewUnicode(newFieldText);
|
|
|
|
mFieldTextLength = newFieldText.Length();
|
|
|
|
nsMemory::Free(temp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFieldText = ToNewUnicode(aText);
|
|
|
|
mFieldTextLength = aText.Length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// XBL fields are represented on elements inheriting that field a bit trickily.
|
2013-02-08 06:24:21 -08:00
|
|
|
// When setting up the XBL prototype object, we install accessors for the fields
|
|
|
|
// on the prototype object. Those accessors, when used, will then (via
|
|
|
|
// InstallXBLField below) reify a property for the field onto the actual XBL-backed
|
|
|
|
// element.
|
2013-02-08 06:24:21 -08:00
|
|
|
//
|
|
|
|
// The accessor property is a plain old property backed by a getter function and
|
|
|
|
// a setter function. These properties are backed by the FieldGetter and
|
2013-02-08 06:24:21 -08:00
|
|
|
// FieldSetter natives; they're created by InstallAccessors. The precise field to be
|
2013-02-08 06:24:21 -08:00
|
|
|
// reified is identified using two extra slots on the getter/setter functions.
|
|
|
|
// XBLPROTO_SLOT stores the XBL prototype object that provides the field.
|
|
|
|
// FIELD_SLOT stores the name of the field, i.e. its JavaScript property name.
|
|
|
|
//
|
2013-02-08 06:24:21 -08:00
|
|
|
// This two-step field installation process -- creating an accessor on the
|
2013-02-08 06:24:21 -08:00
|
|
|
// prototype, then have that reify an own property on the actual element -- is
|
|
|
|
// admittedly convoluted. Better would be for XBL-backed elements to be proxies
|
|
|
|
// that could resolve fields onto themselves. But given that XBL bindings are
|
|
|
|
// associated with elements mutably -- you can add/remove/change -moz-binding
|
|
|
|
// whenever you want, alas -- doing so would require all elements to be proxies,
|
|
|
|
// which isn't performant now. So we do this two-step instead.
|
|
|
|
static const uint32_t XBLPROTO_SLOT = 0;
|
|
|
|
static const uint32_t FIELD_SLOT = 1;
|
|
|
|
|
|
|
|
bool
|
2013-05-28 11:23:04 -07:00
|
|
|
ValueHasISupportsPrivate(JS::Handle<JS::Value> v)
|
2013-02-08 06:24:21 -08:00
|
|
|
{
|
|
|
|
if (!v.isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DOMClass* domClass = GetDOMClass(&v.toObject());
|
|
|
|
if (domClass) {
|
|
|
|
return domClass->mDOMObjectIsISupports;
|
|
|
|
}
|
|
|
|
|
2013-09-11 05:49:05 -07:00
|
|
|
const JSClass* clasp = ::JS_GetClass(&v.toObject());
|
2013-02-08 06:24:21 -08:00
|
|
|
const uint32_t HAS_PRIVATE_NSISUPPORTS =
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS;
|
|
|
|
return (clasp->flags & HAS_PRIVATE_NSISUPPORTS) == HAS_PRIVATE_NSISUPPORTS;
|
|
|
|
}
|
|
|
|
|
2013-05-28 11:23:04 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
static bool
|
|
|
|
ValueHasISupportsPrivate(JSContext* cx, const JS::Value& aVal)
|
|
|
|
{
|
|
|
|
JS::Rooted<JS::Value> v(cx, aVal);
|
|
|
|
return ValueHasISupportsPrivate(v);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// Define a shadowing property on |this| for the XBL field defined by the
|
|
|
|
// contents of the callee's reserved slots. If the property was defined,
|
|
|
|
// *installed will be true, and idp will be set to the property name that was
|
|
|
|
// defined.
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-02-08 06:24:21 -08:00
|
|
|
InstallXBLField(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> callee, JS::Handle<JSObject*> thisObj,
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::MutableHandle<jsid> idp, bool* installed)
|
2013-02-08 06:24:21 -08:00
|
|
|
{
|
|
|
|
*installed = false;
|
|
|
|
|
|
|
|
// First ensure |this| is a reasonable XBL bound node.
|
|
|
|
//
|
|
|
|
// FieldAccessorGuard already determined whether |thisObj| was acceptable as
|
|
|
|
// |this| in terms of not throwing a TypeError. Assert this for good measure.
|
2013-05-28 11:23:04 -07:00
|
|
|
MOZ_ASSERT(ValueHasISupportsPrivate(cx, JS::ObjectValue(*thisObj)));
|
2013-02-08 06:24:21 -08:00
|
|
|
|
|
|
|
// But there are some cases where we must accept |thisObj| but not install a
|
|
|
|
// property on it, or otherwise touch it. Hence this split of |this|-vetting
|
|
|
|
// duties.
|
|
|
|
nsISupports* native =
|
|
|
|
nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, thisObj);
|
|
|
|
if (!native) {
|
|
|
|
// Looks like whatever |thisObj| is it's not our nsIContent. It might well
|
|
|
|
// be the proto our binding installed, however, where the private is the
|
|
|
|
// nsXBLDocumentInfo, so just baul out quietly. Do NOT throw an exception
|
|
|
|
// here.
|
|
|
|
//
|
|
|
|
// We could make this stricter by checking the class maybe, but whatever.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> xblNode = do_QueryInterface(native);
|
|
|
|
if (!xblNode) {
|
|
|
|
xpc::Throw(cx, NS_ERROR_UNEXPECTED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// Now that |this| is okay, actually install the field.
|
2013-02-08 06:24:21 -08:00
|
|
|
|
|
|
|
// Because of the possibility (due to XBL binding inheritance, because each
|
|
|
|
// XBL binding lives in its own global object) that |this| might be in a
|
|
|
|
// different compartment from the callee (not to mention that this method can
|
|
|
|
// be called with an arbitrary |this| regardless of how insane XBL is), and
|
|
|
|
// because in this method we've entered |this|'s compartment (see in
|
|
|
|
// Field[GS]etter where we attempt a cross-compartment call), we must enter
|
|
|
|
// the callee's compartment to access its reserved slots.
|
|
|
|
nsXBLPrototypeBinding* protoBinding;
|
|
|
|
nsDependentJSString fieldName;
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(cx, callee);
|
|
|
|
|
2013-03-06 08:41:43 -08:00
|
|
|
JS::Rooted<JSObject*> xblProto(cx);
|
2013-02-08 06:24:21 -08:00
|
|
|
xblProto = &js::GetFunctionNativeReserved(callee, XBLPROTO_SLOT).toObject();
|
|
|
|
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JS::Value> name(cx, js::GetFunctionNativeReserved(callee, FIELD_SLOT));
|
2013-02-08 06:24:21 -08:00
|
|
|
JSFlatString* fieldStr = JS_ASSERT_STRING_IS_FLAT(name.toString());
|
|
|
|
fieldName.init(fieldStr);
|
|
|
|
|
2014-01-17 10:08:51 -08:00
|
|
|
MOZ_ALWAYS_TRUE(JS_ValueToId(cx, name, idp));
|
2013-02-08 06:24:21 -08:00
|
|
|
|
2013-02-08 06:24:22 -08:00
|
|
|
// If a separate XBL scope is being used, the callee is not same-compartment
|
|
|
|
// with the xbl prototype, and the object is a cross-compartment wrapper.
|
2013-04-11 11:50:18 -07:00
|
|
|
xblProto = js::UncheckedUnwrap(xblProto);
|
2013-02-08 06:24:22 -08:00
|
|
|
JSAutoCompartment ac2(cx, xblProto);
|
2013-02-08 06:24:21 -08:00
|
|
|
JS::Value slotVal = ::JS_GetReservedSlot(xblProto, 0);
|
|
|
|
protoBinding = static_cast<nsXBLPrototypeBinding*>(slotVal.toPrivate());
|
|
|
|
MOZ_ASSERT(protoBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXBLProtoImplField* field = protoBinding->FindField(fieldName);
|
|
|
|
MOZ_ASSERT(field);
|
|
|
|
|
|
|
|
// This mirrors code in nsXBLProtoImpl::InstallImplementation
|
2013-06-21 07:25:20 -07:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global =
|
|
|
|
do_QueryInterface(xblNode->OwnerDoc()->GetWindow());
|
2013-02-08 06:24:21 -08:00
|
|
|
if (!global) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> context = global->GetContext();
|
|
|
|
if (!context) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = field->InstallField(context, thisObj, protoBinding->DocURI(),
|
|
|
|
installed);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!::JS_IsExceptionPending(cx)) {
|
|
|
|
xpc::Throw(cx, rv);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FieldGetterImpl(JSContext *cx, JS::CallArgs args)
|
|
|
|
{
|
2013-05-28 11:23:04 -07:00
|
|
|
JS::Handle<JS::Value> thisv = args.thisv();
|
2013-02-08 06:24:21 -08:00
|
|
|
MOZ_ASSERT(ValueHasISupportsPrivate(thisv));
|
|
|
|
|
2013-03-06 08:41:43 -08:00
|
|
|
JS::Rooted<JSObject*> thisObj(cx, &thisv.toObject());
|
2013-02-08 06:24:21 -08:00
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// We should be in the compartment of |this|. If we got here via nativeCall,
|
|
|
|
// |this| is not same-compartment with |callee|, and it's possible via
|
|
|
|
// asymmetric security semantics that |args.calleev()| is actually a security
|
|
|
|
// wrapper. In this case, we know we want to do an unsafe unwrap, and
|
|
|
|
// InstallXBLField knows how to handle cross-compartment pointers.
|
2013-02-08 06:24:21 -08:00
|
|
|
bool installed = false;
|
2013-04-11 11:50:18 -07:00
|
|
|
JS::Rooted<JSObject*> callee(cx, js::UncheckedUnwrap(&args.calleev().toObject()));
|
2013-03-06 08:41:43 -08:00
|
|
|
JS::Rooted<jsid> id(cx);
|
2013-04-05 06:21:02 -07:00
|
|
|
if (!InstallXBLField(cx, callee, thisObj, &id, &installed)) {
|
2013-02-08 06:24:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!installed) {
|
|
|
|
args.rval().setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-06 08:41:43 -08:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-07-26 02:00:38 -07:00
|
|
|
if (!JS_GetPropertyById(cx, thisObj, id, &v)) {
|
2013-02-08 06:24:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
args.rval().set(v);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-02-08 06:24:21 -08:00
|
|
|
FieldGetter(JSContext *cx, unsigned argc, JS::Value *vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-05-09 09:16:01 -07:00
|
|
|
return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldGetterImpl>
|
2013-02-08 06:24:21 -08:00
|
|
|
(cx, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FieldSetterImpl(JSContext *cx, JS::CallArgs args)
|
|
|
|
{
|
2013-05-28 11:23:04 -07:00
|
|
|
JS::Handle<JS::Value> thisv = args.thisv();
|
2013-02-08 06:24:21 -08:00
|
|
|
MOZ_ASSERT(ValueHasISupportsPrivate(thisv));
|
|
|
|
|
2013-03-06 08:41:43 -08:00
|
|
|
JS::Rooted<JSObject*> thisObj(cx, &thisv.toObject());
|
2013-02-08 06:24:21 -08:00
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// We should be in the compartment of |this|. If we got here via nativeCall,
|
|
|
|
// |this| is not same-compartment with |callee|, and it's possible via
|
|
|
|
// asymmetric security semantics that |args.calleev()| is actually a security
|
|
|
|
// wrapper. In this case, we know we want to do an unsafe unwrap, and
|
|
|
|
// InstallXBLField knows how to handle cross-compartment pointers.
|
2013-02-08 06:24:21 -08:00
|
|
|
bool installed = false;
|
2013-04-11 11:50:18 -07:00
|
|
|
JS::Rooted<JSObject*> callee(cx, js::UncheckedUnwrap(&args.calleev().toObject()));
|
2013-03-06 08:41:43 -08:00
|
|
|
JS::Rooted<jsid> id(cx);
|
2013-04-05 06:21:02 -07:00
|
|
|
if (!InstallXBLField(cx, callee, thisObj, &id, &installed)) {
|
2013-02-08 06:24:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (installed) {
|
2013-07-26 02:00:38 -07:00
|
|
|
if (!::JS_SetPropertyById(cx, thisObj, id, args.get(0))) {
|
2013-02-08 06:24:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.rval().setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2013-02-08 06:24:21 -08:00
|
|
|
FieldSetter(JSContext *cx, unsigned argc, JS::Value *vp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2013-05-09 09:16:01 -07:00
|
|
|
return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldSetterImpl>
|
2013-02-08 06:24:21 -08:00
|
|
|
(cx, args);
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplField::InstallAccessors(JSContext* aCx,
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Handle<JSObject*> aTargetClassObject)
|
2013-02-08 06:24:21 -08:00
|
|
|
{
|
2013-02-08 06:24:21 -08:00
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
|
2014-03-19 09:35:45 -07:00
|
|
|
JS::Rooted<JSObject*> scopeObject(aCx, xpc::GetXBLScopeOrGlobal(aCx, globalObject));
|
2013-04-05 12:04:09 -07:00
|
|
|
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
|
2013-02-08 06:24:21 -08:00
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// Don't install it if the field is empty; see also InstallField which also must
|
2013-02-08 06:24:21 -08:00
|
|
|
// implement the not-empty requirement.
|
2013-02-08 06:24:21 -08:00
|
|
|
if (IsEmpty()) {
|
|
|
|
return NS_OK;
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// Install a getter/setter pair which will resolve the field onto the actual
|
|
|
|
// object, when invoked.
|
|
|
|
|
|
|
|
// Get the field name as an id.
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<jsid> id(aCx);
|
2013-02-08 06:24:21 -08:00
|
|
|
JS::TwoByteChars chars(mName, NS_strlen(mName));
|
2013-09-19 00:54:02 -07:00
|
|
|
if (!JS_CharsToId(aCx, chars, &id))
|
2013-02-08 06:24:21 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Properties/Methods have historically taken precendence over fields. We
|
|
|
|
// install members first, so just bounce here if the property is already
|
|
|
|
// defined.
|
2013-08-08 15:53:04 -07:00
|
|
|
bool found = false;
|
2013-02-08 06:24:21 -08:00
|
|
|
if (!JS_AlreadyHasOwnPropertyById(aCx, aTargetClassObject, id, &found))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
if (found)
|
|
|
|
return NS_OK;
|
2013-02-08 06:24:21 -08:00
|
|
|
|
2013-02-08 06:24:22 -08:00
|
|
|
// FieldGetter and FieldSetter need to run in the XBL scope so that they can
|
|
|
|
// see through any SOWs on their targets.
|
|
|
|
|
|
|
|
// First, enter the XBL scope, and compile the functions there.
|
|
|
|
JSAutoCompartment ac(aCx, scopeObject);
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JS::Value> wrappedClassObj(aCx, JS::ObjectValue(*aTargetClassObject));
|
2014-03-20 02:32:37 -07:00
|
|
|
if (!JS_WrapValue(aCx, &wrappedClassObj) || !JS_WrapId(aCx, &id))
|
2013-02-08 06:24:22 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JSObject*> get(aCx,
|
2013-02-08 06:24:21 -08:00
|
|
|
JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx, FieldGetter,
|
2013-04-05 06:21:02 -07:00
|
|
|
0, 0, scopeObject, id)));
|
2013-02-08 06:24:21 -08:00
|
|
|
if (!get) {
|
2013-02-08 06:24:21 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
2013-02-08 06:24:22 -08:00
|
|
|
js::SetFunctionNativeReserved(get, XBLPROTO_SLOT, wrappedClassObj);
|
2013-02-08 06:24:21 -08:00
|
|
|
js::SetFunctionNativeReserved(get, FIELD_SLOT,
|
|
|
|
JS::StringValue(JSID_TO_STRING(id)));
|
|
|
|
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JSObject*> set(aCx,
|
2013-02-08 06:24:21 -08:00
|
|
|
JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx, FieldSetter,
|
2013-04-05 06:21:02 -07:00
|
|
|
1, 0, scopeObject, id)));
|
2013-02-08 06:24:21 -08:00
|
|
|
if (!set) {
|
2013-02-08 06:24:21 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
2013-02-08 06:24:22 -08:00
|
|
|
js::SetFunctionNativeReserved(set, XBLPROTO_SLOT, wrappedClassObj);
|
2013-02-08 06:24:21 -08:00
|
|
|
js::SetFunctionNativeReserved(set, FIELD_SLOT,
|
|
|
|
JS::StringValue(JSID_TO_STRING(id)));
|
|
|
|
|
2013-02-08 06:24:22 -08:00
|
|
|
// Now, re-enter the class object's scope, wrap the getters/setters, and define
|
|
|
|
// them there.
|
|
|
|
JSAutoCompartment ac2(aCx, aTargetClassObject);
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(aCx, &get) || !JS_WrapObject(aCx, &set) ||
|
2014-03-20 02:32:37 -07:00
|
|
|
!JS_WrapId(aCx, &id))
|
2013-02-08 06:24:22 -08:00
|
|
|
{
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
if (!::JS_DefinePropertyById(aCx, aTargetClassObject, id, JS::UndefinedValue(),
|
2013-04-05 06:21:02 -07:00
|
|
|
JS_DATA_TO_FUNC_PTR(JSPropertyOp, get.get()),
|
|
|
|
JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, set.get()),
|
2013-02-08 06:24:21 -08:00
|
|
|
AccessorAttributes())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:22 -08:00
|
|
|
return NS_OK;
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2007-09-28 06:45:01 -07:00
|
|
|
nsXBLProtoImplField::InstallField(nsIScriptContext* aContext,
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Handle<JSObject*> aBoundNode,
|
2007-10-02 07:38:35 -07:00
|
|
|
nsIURI* aBindingDocURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aDidInstall) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-09-28 06:45:01 -07:00
|
|
|
NS_PRECONDITION(aBoundNode,
|
|
|
|
"uh-oh, bound node should NOT be null or bad things will "
|
|
|
|
"happen");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidInstall = false;
|
2007-10-02 07:38:35 -07:00
|
|
|
|
2012-05-29 12:01:30 -07:00
|
|
|
// Empty fields are treated as not actually present.
|
|
|
|
if (IsEmpty()) {
|
2007-10-02 07:38:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
nsAutoMicroTask mt;
|
|
|
|
|
2013-01-16 18:50:26 -08:00
|
|
|
// EvaluateString and JS_DefineUCProperty can both trigger GC, so
|
2007-03-22 10:30:00 -07:00
|
|
|
// protect |result| here.
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString uriSpec;
|
2007-10-02 07:38:35 -07:00
|
|
|
aBindingDocURI->GetSpec(uriSpec);
|
2013-04-05 06:21:02 -07:00
|
|
|
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(aContext->GetNativeContext());
|
2008-03-05 12:41:27 -08:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"Shouldn't get here when an exception is pending!");
|
2013-04-05 06:21:02 -07:00
|
|
|
|
2013-02-08 06:24:22 -08:00
|
|
|
// First, enter the xbl scope, wrap the node, and use that as the scope for
|
|
|
|
// the evaluation.
|
2014-03-19 09:35:45 -07:00
|
|
|
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetXBLScopeOrGlobal(cx, aBoundNode));
|
2013-04-05 12:04:09 -07:00
|
|
|
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
|
2013-02-08 06:24:22 -08:00
|
|
|
JSAutoCompartment ac(cx, scopeObject);
|
2013-01-16 18:50:26 -08:00
|
|
|
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JSObject*> wrappedNode(cx, aBoundNode);
|
2013-10-15 17:02:23 -07:00
|
|
|
if (!JS_WrapObject(cx, &wrappedNode))
|
2013-02-08 06:24:22 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JS::Value> result(cx);
|
2013-01-16 18:50:26 -08:00
|
|
|
JS::CompileOptions options(cx);
|
|
|
|
options.setFileAndLine(uriSpec.get(), mLineNumber)
|
2013-05-20 21:34:17 -07:00
|
|
|
.setVersion(JSVERSION_LATEST);
|
2013-08-09 09:25:13 -07:00
|
|
|
rv = aContext->EvaluateString(nsDependentString(mFieldText,
|
|
|
|
mFieldTextLength),
|
|
|
|
wrappedNode, options,
|
|
|
|
/* aCoerceToString = */ false,
|
|
|
|
result.address());
|
2011-11-15 23:50:20 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-10-02 07:38:35 -07:00
|
|
|
return rv;
|
2011-11-15 23:50:20 -08:00
|
|
|
}
|
2007-09-26 07:39:31 -07:00
|
|
|
|
|
|
|
|
2013-02-08 06:24:22 -08:00
|
|
|
// Now, enter the node's compartment, wrap the eval result, and define it on
|
|
|
|
// the bound node.
|
|
|
|
JSAutoCompartment ac2(cx, aBoundNode);
|
2007-09-28 06:45:01 -07:00
|
|
|
nsDependentString name(mName);
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(cx, &result) ||
|
2013-02-08 06:24:22 -08:00
|
|
|
!::JS_DefineUCProperty(cx, aBoundNode,
|
2013-10-26 09:19:05 -07:00
|
|
|
reinterpret_cast<const jschar*>(mName),
|
2012-07-30 07:20:58 -07:00
|
|
|
name.Length(), result, nullptr, nullptr,
|
2007-09-28 06:45:01 -07:00
|
|
|
mJSAttributes)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-10-02 07:38:35 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidInstall = true;
|
2007-09-28 06:45:01 -07:00
|
|
|
return NS_OK;
|
2007-09-26 07:39:31 -07:00
|
|
|
}
|
2011-11-03 13:39:08 -07:00
|
|
|
|
|
|
|
nsresult
|
2013-08-09 09:25:13 -07:00
|
|
|
nsXBLProtoImplField::Read(nsIObjectInputStream* aStream)
|
2011-11-03 13:39:08 -07:00
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
nsresult rv = aStream->ReadString(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mName = ToNewUnicode(name);
|
|
|
|
|
|
|
|
rv = aStream->Read32(&mLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString fieldText;
|
|
|
|
rv = aStream->ReadString(fieldText);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mFieldTextLength = fieldText.Length();
|
|
|
|
if (mFieldTextLength)
|
|
|
|
mFieldText = ToNewUnicode(fieldText);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-09 09:25:13 -07:00
|
|
|
nsXBLProtoImplField::Write(nsIObjectOutputStream* aStream)
|
2011-11-03 13:39:08 -07:00
|
|
|
{
|
|
|
|
XBLBindingSerializeDetails type = XBLBinding_Serialize_Field;
|
|
|
|
|
|
|
|
if (mJSAttributes & JSPROP_READONLY) {
|
|
|
|
type |= XBLBinding_Serialize_ReadOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = aStream->Write8(type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aStream->WriteWStringZ(mName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aStream->Write32(mLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-12-03 07:09:50 -08:00
|
|
|
return aStream->WriteWStringZ(mFieldText ? mFieldText : MOZ_UTF16(""));
|
2011-11-03 13:39:08 -07:00
|
|
|
}
|