1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsIContent.h"
11 #include "js/CharacterEncoding.h"
12 #include "nsUnicharUtils.h"
13 #include "nsReadableUtils.h"
14 #include "nsXBLProtoImplField.h"
15 #include "nsIScriptContext.h"
17 #include "nsXBLSerialize.h"
18 #include "nsXBLPrototypeBinding.h"
19 #include "mozilla/AddonPathService.h"
20 #include "mozilla/dom/BindingUtils.h"
21 #include "mozilla/dom/ElementBinding.h"
22 #include "mozilla/dom/ScriptSettings.h"
23 #include "nsGlobalWindow.h"
24 #include "xpcpublic.h"
25 #include "WrapperFactory.h"
27 using namespace mozilla
;
28 using namespace mozilla::dom
;
30 nsXBLProtoImplField::nsXBLProtoImplField(const char16_t
* aName
, const char16_t
* aReadOnly
)
36 MOZ_COUNT_CTOR(nsXBLProtoImplField
);
37 mName
= NS_strdup(aName
); // XXXbz make more sense to use a stringbuffer?
39 mJSAttributes
= JSPROP_ENUMERATE
;
41 nsAutoString readOnly
; readOnly
.Assign(aReadOnly
);
42 if (readOnly
.LowerCaseEqualsLiteral("true"))
43 mJSAttributes
|= JSPROP_READONLY
;
48 nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly
)
54 MOZ_COUNT_CTOR(nsXBLProtoImplField
);
56 mJSAttributes
= JSPROP_ENUMERATE
;
58 mJSAttributes
|= JSPROP_READONLY
;
61 nsXBLProtoImplField::~nsXBLProtoImplField()
63 MOZ_COUNT_DTOR(nsXBLProtoImplField
);
65 nsMemory::Free(mFieldText
);
67 NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField
, this, mNext
);
71 nsXBLProtoImplField::AppendFieldText(const nsAString
& aText
)
74 nsDependentString
fieldTextStr(mFieldText
, mFieldTextLength
);
75 nsAutoString newFieldText
= fieldTextStr
+ aText
;
76 char16_t
* temp
= mFieldText
;
77 mFieldText
= ToNewUnicode(newFieldText
);
78 mFieldTextLength
= newFieldText
.Length();
82 mFieldText
= ToNewUnicode(aText
);
83 mFieldTextLength
= aText
.Length();
87 // XBL fields are represented on elements inheriting that field a bit trickily.
88 // When setting up the XBL prototype object, we install accessors for the fields
89 // on the prototype object. Those accessors, when used, will then (via
90 // InstallXBLField below) reify a property for the field onto the actual XBL-backed
93 // The accessor property is a plain old property backed by a getter function and
94 // a setter function. These properties are backed by the FieldGetter and
95 // FieldSetter natives; they're created by InstallAccessors. The precise field to be
96 // reified is identified using two extra slots on the getter/setter functions.
97 // XBLPROTO_SLOT stores the XBL prototype object that provides the field.
98 // FIELD_SLOT stores the name of the field, i.e. its JavaScript property name.
100 // This two-step field installation process -- creating an accessor on the
101 // prototype, then have that reify an own property on the actual element -- is
102 // admittedly convoluted. Better would be for XBL-backed elements to be proxies
103 // that could resolve fields onto themselves. But given that XBL bindings are
104 // associated with elements mutably -- you can add/remove/change -moz-binding
105 // whenever you want, alas -- doing so would require all elements to be proxies,
106 // which isn't performant now. So we do this two-step instead.
107 static const uint32_t XBLPROTO_SLOT
= 0;
108 static const uint32_t FIELD_SLOT
= 1;
111 ValueHasISupportsPrivate(JS::Handle
<JS::Value
> v
)
117 const DOMJSClass
* domClass
= GetDOMClass(&v
.toObject());
119 return domClass
->mDOMObjectIsISupports
;
122 const JSClass
* clasp
= ::JS_GetClass(&v
.toObject());
123 const uint32_t HAS_PRIVATE_NSISUPPORTS
=
124 JSCLASS_HAS_PRIVATE
| JSCLASS_PRIVATE_IS_NSISUPPORTS
;
125 return (clasp
->flags
& HAS_PRIVATE_NSISUPPORTS
) == HAS_PRIVATE_NSISUPPORTS
;
130 ValueHasISupportsPrivate(JSContext
* cx
, const JS::Value
& aVal
)
132 JS::Rooted
<JS::Value
> v(cx
, aVal
);
133 return ValueHasISupportsPrivate(v
);
137 // Define a shadowing property on |this| for the XBL field defined by the
138 // contents of the callee's reserved slots. If the property was defined,
139 // *installed will be true, and idp will be set to the property name that was
142 InstallXBLField(JSContext
* cx
,
143 JS::Handle
<JSObject
*> callee
, JS::Handle
<JSObject
*> thisObj
,
144 JS::MutableHandle
<jsid
> idp
, bool* installed
)
148 // First ensure |this| is a reasonable XBL bound node.
150 // FieldAccessorGuard already determined whether |thisObj| was acceptable as
151 // |this| in terms of not throwing a TypeError. Assert this for good measure.
152 MOZ_ASSERT(ValueHasISupportsPrivate(cx
, JS::ObjectValue(*thisObj
)));
154 // But there are some cases where we must accept |thisObj| but not install a
155 // property on it, or otherwise touch it. Hence this split of |this|-vetting
157 nsISupports
* native
=
158 nsContentUtils::XPConnect()->GetNativeOfWrapper(cx
, thisObj
);
160 // Looks like whatever |thisObj| is it's not our nsIContent. It might well
161 // be the proto our binding installed, however, where the private is the
162 // nsXBLDocumentInfo, so just baul out quietly. Do NOT throw an exception
165 // We could make this stricter by checking the class maybe, but whatever.
169 nsCOMPtr
<nsIContent
> xblNode
= do_QueryInterface(native
);
171 xpc::Throw(cx
, NS_ERROR_UNEXPECTED
);
175 // Now that |this| is okay, actually install the field.
177 // Because of the possibility (due to XBL binding inheritance, because each
178 // XBL binding lives in its own global object) that |this| might be in a
179 // different compartment from the callee (not to mention that this method can
180 // be called with an arbitrary |this| regardless of how insane XBL is), and
181 // because in this method we've entered |this|'s compartment (see in
182 // Field[GS]etter where we attempt a cross-compartment call), we must enter
183 // the callee's compartment to access its reserved slots.
184 nsXBLPrototypeBinding
* protoBinding
;
185 nsAutoJSString fieldName
;
187 JSAutoCompartment
ac(cx
, callee
);
189 JS::Rooted
<JSObject
*> xblProto(cx
);
190 xblProto
= &js::GetFunctionNativeReserved(callee
, XBLPROTO_SLOT
).toObject();
192 JS::Rooted
<JS::Value
> name(cx
, js::GetFunctionNativeReserved(callee
, FIELD_SLOT
));
193 if (!fieldName
.init(cx
, name
.toString())) {
197 MOZ_ALWAYS_TRUE(JS_ValueToId(cx
, name
, idp
));
199 // If a separate XBL scope is being used, the callee is not same-compartment
200 // with the xbl prototype, and the object is a cross-compartment wrapper.
201 xblProto
= js::UncheckedUnwrap(xblProto
);
202 JSAutoCompartment
ac2(cx
, xblProto
);
203 JS::Value slotVal
= ::JS_GetReservedSlot(xblProto
, 0);
204 protoBinding
= static_cast<nsXBLPrototypeBinding
*>(slotVal
.toPrivate());
205 MOZ_ASSERT(protoBinding
);
208 nsXBLProtoImplField
* field
= protoBinding
->FindField(fieldName
);
211 nsresult rv
= field
->InstallField(thisObj
, protoBinding
->DocURI(), installed
);
212 if (NS_SUCCEEDED(rv
)) {
216 if (!::JS_IsExceptionPending(cx
)) {
223 FieldGetterImpl(JSContext
*cx
, JS::CallArgs args
)
225 JS::Handle
<JS::Value
> thisv
= args
.thisv();
226 MOZ_ASSERT(ValueHasISupportsPrivate(thisv
));
228 JS::Rooted
<JSObject
*> thisObj(cx
, &thisv
.toObject());
230 // We should be in the compartment of |this|. If we got here via nativeCall,
231 // |this| is not same-compartment with |callee|, and it's possible via
232 // asymmetric security semantics that |args.calleev()| is actually a security
233 // wrapper. In this case, we know we want to do an unsafe unwrap, and
234 // InstallXBLField knows how to handle cross-compartment pointers.
235 bool installed
= false;
236 JS::Rooted
<JSObject
*> callee(cx
, js::UncheckedUnwrap(&args
.calleev().toObject()));
237 JS::Rooted
<jsid
> id(cx
);
238 if (!InstallXBLField(cx
, callee
, thisObj
, &id
, &installed
)) {
243 args
.rval().setUndefined();
247 JS::Rooted
<JS::Value
> v(cx
);
248 if (!JS_GetPropertyById(cx
, thisObj
, id
, &v
)) {
256 FieldGetter(JSContext
*cx
, unsigned argc
, JS::Value
*vp
)
258 JS::CallArgs args
= JS::CallArgsFromVp(argc
, vp
);
259 return JS::CallNonGenericMethod
<ValueHasISupportsPrivate
, FieldGetterImpl
>
264 FieldSetterImpl(JSContext
*cx
, JS::CallArgs args
)
266 JS::Handle
<JS::Value
> thisv
= args
.thisv();
267 MOZ_ASSERT(ValueHasISupportsPrivate(thisv
));
269 JS::Rooted
<JSObject
*> thisObj(cx
, &thisv
.toObject());
271 // We should be in the compartment of |this|. If we got here via nativeCall,
272 // |this| is not same-compartment with |callee|, and it's possible via
273 // asymmetric security semantics that |args.calleev()| is actually a security
274 // wrapper. In this case, we know we want to do an unsafe unwrap, and
275 // InstallXBLField knows how to handle cross-compartment pointers.
276 bool installed
= false;
277 JS::Rooted
<JSObject
*> callee(cx
, js::UncheckedUnwrap(&args
.calleev().toObject()));
278 JS::Rooted
<jsid
> id(cx
);
279 if (!InstallXBLField(cx
, callee
, thisObj
, &id
, &installed
)) {
284 if (!::JS_SetPropertyById(cx
, thisObj
, id
, args
.get(0))) {
288 args
.rval().setUndefined();
293 FieldSetter(JSContext
*cx
, unsigned argc
, JS::Value
*vp
)
295 JS::CallArgs args
= JS::CallArgsFromVp(argc
, vp
);
296 return JS::CallNonGenericMethod
<ValueHasISupportsPrivate
, FieldSetterImpl
>
301 nsXBLProtoImplField::InstallAccessors(JSContext
* aCx
,
302 JS::Handle
<JSObject
*> aTargetClassObject
)
304 MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject
, aCx
));
305 JS::Rooted
<JSObject
*> globalObject(aCx
, JS_GetGlobalForObject(aCx
, aTargetClassObject
));
306 JS::Rooted
<JSObject
*> scopeObject(aCx
, xpc::GetXBLScopeOrGlobal(aCx
, globalObject
));
307 NS_ENSURE_TRUE(scopeObject
, NS_ERROR_OUT_OF_MEMORY
);
309 // Don't install it if the field is empty; see also InstallField which also must
310 // implement the not-empty requirement.
315 // Install a getter/setter pair which will resolve the field onto the actual
316 // object, when invoked.
318 // Get the field name as an id.
319 JS::Rooted
<jsid
> id(aCx
);
320 JS::TwoByteChars
chars(mName
, NS_strlen(mName
));
321 if (!JS_CharsToId(aCx
, chars
, &id
))
322 return NS_ERROR_OUT_OF_MEMORY
;
324 // Properties/Methods have historically taken precendence over fields. We
325 // install members first, so just bounce here if the property is already
328 if (!JS_AlreadyHasOwnPropertyById(aCx
, aTargetClassObject
, id
, &found
))
329 return NS_ERROR_FAILURE
;
333 // FieldGetter and FieldSetter need to run in the XBL scope so that they can
334 // see through any SOWs on their targets.
336 // First, enter the XBL scope, and compile the functions there.
337 JSAutoCompartment
ac(aCx
, scopeObject
);
338 JS::Rooted
<JS::Value
> wrappedClassObj(aCx
, JS::ObjectValue(*aTargetClassObject
));
339 if (!JS_WrapValue(aCx
, &wrappedClassObj
))
340 return NS_ERROR_OUT_OF_MEMORY
;
342 JS::Rooted
<JSObject
*> get(aCx
,
343 JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx
, FieldGetter
,
344 0, 0, scopeObject
, id
)));
346 return NS_ERROR_OUT_OF_MEMORY
;
348 js::SetFunctionNativeReserved(get
, XBLPROTO_SLOT
, wrappedClassObj
);
349 js::SetFunctionNativeReserved(get
, FIELD_SLOT
,
350 JS::StringValue(JSID_TO_STRING(id
)));
352 JS::Rooted
<JSObject
*> set(aCx
,
353 JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx
, FieldSetter
,
354 1, 0, scopeObject
, id
)));
356 return NS_ERROR_OUT_OF_MEMORY
;
358 js::SetFunctionNativeReserved(set
, XBLPROTO_SLOT
, wrappedClassObj
);
359 js::SetFunctionNativeReserved(set
, FIELD_SLOT
,
360 JS::StringValue(JSID_TO_STRING(id
)));
362 // Now, re-enter the class object's scope, wrap the getters/setters, and define
364 JSAutoCompartment
ac2(aCx
, aTargetClassObject
);
365 if (!JS_WrapObject(aCx
, &get
) || !JS_WrapObject(aCx
, &set
)) {
366 return NS_ERROR_OUT_OF_MEMORY
;
369 if (!::JS_DefinePropertyById(aCx
, aTargetClassObject
, id
, JS::UndefinedHandleValue
,
370 AccessorAttributes(),
371 JS_DATA_TO_FUNC_PTR(JSNative
, get
.get()),
372 JS_DATA_TO_FUNC_PTR(JSNative
, set
.get()))) {
373 return NS_ERROR_OUT_OF_MEMORY
;
380 nsXBLProtoImplField::InstallField(JS::Handle
<JSObject
*> aBoundNode
,
381 nsIURI
* aBindingDocURI
,
382 bool* aDidInstall
) const
384 NS_PRECONDITION(aBoundNode
,
385 "uh-oh, bound node should NOT be null or bad things will "
388 *aDidInstall
= false;
390 // Empty fields are treated as not actually present.
397 nsAutoCString uriSpec
;
398 aBindingDocURI
->GetSpec(uriSpec
);
400 nsIGlobalObject
* globalObject
= xpc::WindowGlobalOrNull(aBoundNode
);
405 // We are going to run script via EvaluateString, so we need a script entry
406 // point, but as this is XBL related it does not appear in the HTML spec.
407 AutoEntryScript
entryScript(globalObject
, true);
408 JSContext
* cx
= entryScript
.cx();
410 NS_ASSERTION(!::JS_IsExceptionPending(cx
),
411 "Shouldn't get here when an exception is pending!");
413 JSAddonId
* addonId
= MapURIToAddonID(aBindingDocURI
);
415 Element
* boundElement
= nullptr;
416 nsresult rv
= UNWRAP_OBJECT(Element
, aBoundNode
, boundElement
);
417 if (NS_WARN_IF(NS_FAILED(rv
))) {
421 // First, enter the xbl scope, build the element's scope chain, and use
422 // that as the scope chain for the evaluation.
423 JS::Rooted
<JSObject
*> scopeObject(cx
, xpc::GetScopeForXBLExecution(cx
, aBoundNode
, addonId
));
424 NS_ENSURE_TRUE(scopeObject
, NS_ERROR_OUT_OF_MEMORY
);
425 JSAutoCompartment
ac(cx
, scopeObject
);
427 JS::Rooted
<JS::Value
> result(cx
);
428 JS::CompileOptions
options(cx
);
429 options
.setFileAndLine(uriSpec
.get(), mLineNumber
)
430 .setVersion(JSVERSION_LATEST
);
431 nsJSUtils::EvaluateOptions
evalOptions(cx
);
432 if (!nsJSUtils::GetScopeChainForElement(cx
, boundElement
,
433 evalOptions
.scopeChain
)) {
434 return NS_ERROR_OUT_OF_MEMORY
;
436 rv
= nsJSUtils::EvaluateString(cx
, nsDependentString(mFieldText
,
438 scopeObject
, options
, evalOptions
, &result
);
443 // Now, enter the node's compartment, wrap the eval result, and define it on
445 JSAutoCompartment
ac2(cx
, aBoundNode
);
446 nsDependentString
name(mName
);
447 if (!JS_WrapValue(cx
, &result
) ||
448 !::JS_DefineUCProperty(cx
, aBoundNode
,
449 reinterpret_cast<const char16_t
*>(mName
),
450 name
.Length(), result
, mJSAttributes
)) {
451 return NS_ERROR_OUT_OF_MEMORY
;
459 nsXBLProtoImplField::Read(nsIObjectInputStream
* aStream
)
462 nsresult rv
= aStream
->ReadString(name
);
463 NS_ENSURE_SUCCESS(rv
, rv
);
464 mName
= ToNewUnicode(name
);
466 rv
= aStream
->Read32(&mLineNumber
);
467 NS_ENSURE_SUCCESS(rv
, rv
);
469 nsAutoString fieldText
;
470 rv
= aStream
->ReadString(fieldText
);
471 NS_ENSURE_SUCCESS(rv
, rv
);
472 mFieldTextLength
= fieldText
.Length();
473 if (mFieldTextLength
)
474 mFieldText
= ToNewUnicode(fieldText
);
480 nsXBLProtoImplField::Write(nsIObjectOutputStream
* aStream
)
482 XBLBindingSerializeDetails type
= XBLBinding_Serialize_Field
;
484 if (mJSAttributes
& JSPROP_READONLY
) {
485 type
|= XBLBinding_Serialize_ReadOnly
;
488 nsresult rv
= aStream
->Write8(type
);
489 NS_ENSURE_SUCCESS(rv
, rv
);
490 rv
= aStream
->WriteWStringZ(mName
);
491 NS_ENSURE_SUCCESS(rv
, rv
);
492 rv
= aStream
->Write32(mLineNumber
);
493 NS_ENSURE_SUCCESS(rv
, rv
);
495 return aStream
->WriteWStringZ(mFieldText
? mFieldText
: MOZ_UTF16(""));