Bumping manifests a=b2g-bump
[gecko.git] / dom / xbl / nsXBLProtoImplField.cpp
blob07ae96bda438c0f7e06f5bc1a30eafeea59ead16
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/. */
6 #include "nsIAtom.h"
7 #include "nsIContent.h"
8 #include "nsString.h"
9 #include "nsJSUtils.h"
10 #include "jsapi.h"
11 #include "js/CharacterEncoding.h"
12 #include "nsUnicharUtils.h"
13 #include "nsReadableUtils.h"
14 #include "nsXBLProtoImplField.h"
15 #include "nsIScriptContext.h"
16 #include "nsIURI.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)
31 : mNext(nullptr),
32 mFieldText(nullptr),
33 mFieldTextLength(0),
34 mLineNumber(0)
36 MOZ_COUNT_CTOR(nsXBLProtoImplField);
37 mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer?
39 mJSAttributes = JSPROP_ENUMERATE;
40 if (aReadOnly) {
41 nsAutoString readOnly; readOnly.Assign(aReadOnly);
42 if (readOnly.LowerCaseEqualsLiteral("true"))
43 mJSAttributes |= JSPROP_READONLY;
48 nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly)
49 : mNext(nullptr),
50 mFieldText(nullptr),
51 mFieldTextLength(0),
52 mLineNumber(0)
54 MOZ_COUNT_CTOR(nsXBLProtoImplField);
56 mJSAttributes = JSPROP_ENUMERATE;
57 if (aIsReadOnly)
58 mJSAttributes |= JSPROP_READONLY;
61 nsXBLProtoImplField::~nsXBLProtoImplField()
63 MOZ_COUNT_DTOR(nsXBLProtoImplField);
64 if (mFieldText)
65 nsMemory::Free(mFieldText);
66 NS_Free(mName);
67 NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext);
70 void
71 nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
73 if (mFieldText) {
74 nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
75 nsAutoString newFieldText = fieldTextStr + aText;
76 char16_t* temp = mFieldText;
77 mFieldText = ToNewUnicode(newFieldText);
78 mFieldTextLength = newFieldText.Length();
79 nsMemory::Free(temp);
81 else {
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
91 // element.
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;
110 bool
111 ValueHasISupportsPrivate(JS::Handle<JS::Value> v)
113 if (!v.isObject()) {
114 return false;
117 const DOMJSClass* domClass = GetDOMClass(&v.toObject());
118 if (domClass) {
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;
128 #ifdef DEBUG
129 static bool
130 ValueHasISupportsPrivate(JSContext* cx, const JS::Value& aVal)
132 JS::Rooted<JS::Value> v(cx, aVal);
133 return ValueHasISupportsPrivate(v);
135 #endif
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
140 // defined.
141 static bool
142 InstallXBLField(JSContext* cx,
143 JS::Handle<JSObject*> callee, JS::Handle<JSObject*> thisObj,
144 JS::MutableHandle<jsid> idp, bool* installed)
146 *installed = false;
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
156 // duties.
157 nsISupports* native =
158 nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, thisObj);
159 if (!native) {
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
163 // here.
165 // We could make this stricter by checking the class maybe, but whatever.
166 return true;
169 nsCOMPtr<nsIContent> xblNode = do_QueryInterface(native);
170 if (!xblNode) {
171 xpc::Throw(cx, NS_ERROR_UNEXPECTED);
172 return false;
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())) {
194 return false;
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);
209 MOZ_ASSERT(field);
211 nsresult rv = field->InstallField(thisObj, protoBinding->DocURI(), installed);
212 if (NS_SUCCEEDED(rv)) {
213 return true;
216 if (!::JS_IsExceptionPending(cx)) {
217 xpc::Throw(cx, rv);
219 return false;
222 bool
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)) {
239 return false;
242 if (!installed) {
243 args.rval().setUndefined();
244 return true;
247 JS::Rooted<JS::Value> v(cx);
248 if (!JS_GetPropertyById(cx, thisObj, id, &v)) {
249 return false;
251 args.rval().set(v);
252 return true;
255 static bool
256 FieldGetter(JSContext *cx, unsigned argc, JS::Value *vp)
258 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
259 return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldGetterImpl>
260 (cx, args);
263 bool
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)) {
280 return false;
283 if (installed) {
284 if (!::JS_SetPropertyById(cx, thisObj, id, args.get(0))) {
285 return false;
288 args.rval().setUndefined();
289 return true;
292 static bool
293 FieldSetter(JSContext *cx, unsigned argc, JS::Value *vp)
295 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
296 return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldSetterImpl>
297 (cx, args);
300 nsresult
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.
311 if (IsEmpty()) {
312 return NS_OK;
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
326 // defined.
327 bool found = false;
328 if (!JS_AlreadyHasOwnPropertyById(aCx, aTargetClassObject, id, &found))
329 return NS_ERROR_FAILURE;
330 if (found)
331 return NS_OK;
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)));
345 if (!get) {
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)));
355 if (!set) {
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
363 // them there.
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;
376 return NS_OK;
379 nsresult
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 "
386 "happen");
388 *aDidInstall = false;
390 // Empty fields are treated as not actually present.
391 if (IsEmpty()) {
392 return NS_OK;
395 nsAutoMicroTask mt;
397 nsAutoCString uriSpec;
398 aBindingDocURI->GetSpec(uriSpec);
400 nsIGlobalObject* globalObject = xpc::WindowGlobalOrNull(aBoundNode);
401 if (!globalObject) {
402 return NS_OK;
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))) {
418 return 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,
437 mFieldTextLength),
438 scopeObject, options, evalOptions, &result);
439 if (NS_FAILED(rv)) {
440 return rv;
443 // Now, enter the node's compartment, wrap the eval result, and define it on
444 // the bound node.
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;
454 *aDidInstall = true;
455 return NS_OK;
458 nsresult
459 nsXBLProtoImplField::Read(nsIObjectInputStream* aStream)
461 nsAutoString name;
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);
476 return NS_OK;
479 nsresult
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(""));