Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / xbl / nsXBLProtoImplField.cpp
blobc5f9c69401ce513cc2fe890ed7bd738329a4b718
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/ScriptSettings.h"
22 #include "nsGlobalWindow.h"
23 #include "xpcpublic.h"
24 #include "WrapperFactory.h"
26 using namespace mozilla;
27 using namespace mozilla::dom;
29 nsXBLProtoImplField::nsXBLProtoImplField(const char16_t* aName, const char16_t* aReadOnly)
30 : mNext(nullptr),
31 mFieldText(nullptr),
32 mFieldTextLength(0),
33 mLineNumber(0)
35 MOZ_COUNT_CTOR(nsXBLProtoImplField);
36 mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer?
38 mJSAttributes = JSPROP_ENUMERATE;
39 if (aReadOnly) {
40 nsAutoString readOnly; readOnly.Assign(aReadOnly);
41 if (readOnly.LowerCaseEqualsLiteral("true"))
42 mJSAttributes |= JSPROP_READONLY;
47 nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly)
48 : mNext(nullptr),
49 mFieldText(nullptr),
50 mFieldTextLength(0),
51 mLineNumber(0)
53 MOZ_COUNT_CTOR(nsXBLProtoImplField);
55 mJSAttributes = JSPROP_ENUMERATE;
56 if (aIsReadOnly)
57 mJSAttributes |= JSPROP_READONLY;
60 nsXBLProtoImplField::~nsXBLProtoImplField()
62 MOZ_COUNT_DTOR(nsXBLProtoImplField);
63 if (mFieldText)
64 nsMemory::Free(mFieldText);
65 NS_Free(mName);
66 NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext);
69 void
70 nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
72 if (mFieldText) {
73 nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
74 nsAutoString newFieldText = fieldTextStr + aText;
75 char16_t* temp = mFieldText;
76 mFieldText = ToNewUnicode(newFieldText);
77 mFieldTextLength = newFieldText.Length();
78 nsMemory::Free(temp);
80 else {
81 mFieldText = ToNewUnicode(aText);
82 mFieldTextLength = aText.Length();
86 // XBL fields are represented on elements inheriting that field a bit trickily.
87 // When setting up the XBL prototype object, we install accessors for the fields
88 // on the prototype object. Those accessors, when used, will then (via
89 // InstallXBLField below) reify a property for the field onto the actual XBL-backed
90 // element.
92 // The accessor property is a plain old property backed by a getter function and
93 // a setter function. These properties are backed by the FieldGetter and
94 // FieldSetter natives; they're created by InstallAccessors. The precise field to be
95 // reified is identified using two extra slots on the getter/setter functions.
96 // XBLPROTO_SLOT stores the XBL prototype object that provides the field.
97 // FIELD_SLOT stores the name of the field, i.e. its JavaScript property name.
99 // This two-step field installation process -- creating an accessor on the
100 // prototype, then have that reify an own property on the actual element -- is
101 // admittedly convoluted. Better would be for XBL-backed elements to be proxies
102 // that could resolve fields onto themselves. But given that XBL bindings are
103 // associated with elements mutably -- you can add/remove/change -moz-binding
104 // whenever you want, alas -- doing so would require all elements to be proxies,
105 // which isn't performant now. So we do this two-step instead.
106 static const uint32_t XBLPROTO_SLOT = 0;
107 static const uint32_t FIELD_SLOT = 1;
109 bool
110 ValueHasISupportsPrivate(JS::Handle<JS::Value> v)
112 if (!v.isObject()) {
113 return false;
116 const DOMJSClass* domClass = GetDOMClass(&v.toObject());
117 if (domClass) {
118 return domClass->mDOMObjectIsISupports;
121 const JSClass* clasp = ::JS_GetClass(&v.toObject());
122 const uint32_t HAS_PRIVATE_NSISUPPORTS =
123 JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS;
124 return (clasp->flags & HAS_PRIVATE_NSISUPPORTS) == HAS_PRIVATE_NSISUPPORTS;
127 #ifdef DEBUG
128 static bool
129 ValueHasISupportsPrivate(JSContext* cx, const JS::Value& aVal)
131 JS::Rooted<JS::Value> v(cx, aVal);
132 return ValueHasISupportsPrivate(v);
134 #endif
136 // Define a shadowing property on |this| for the XBL field defined by the
137 // contents of the callee's reserved slots. If the property was defined,
138 // *installed will be true, and idp will be set to the property name that was
139 // defined.
140 static bool
141 InstallXBLField(JSContext* cx,
142 JS::Handle<JSObject*> callee, JS::Handle<JSObject*> thisObj,
143 JS::MutableHandle<jsid> idp, bool* installed)
145 *installed = false;
147 // First ensure |this| is a reasonable XBL bound node.
149 // FieldAccessorGuard already determined whether |thisObj| was acceptable as
150 // |this| in terms of not throwing a TypeError. Assert this for good measure.
151 MOZ_ASSERT(ValueHasISupportsPrivate(cx, JS::ObjectValue(*thisObj)));
153 // But there are some cases where we must accept |thisObj| but not install a
154 // property on it, or otherwise touch it. Hence this split of |this|-vetting
155 // duties.
156 nsISupports* native =
157 nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, thisObj);
158 if (!native) {
159 // Looks like whatever |thisObj| is it's not our nsIContent. It might well
160 // be the proto our binding installed, however, where the private is the
161 // nsXBLDocumentInfo, so just baul out quietly. Do NOT throw an exception
162 // here.
164 // We could make this stricter by checking the class maybe, but whatever.
165 return true;
168 nsCOMPtr<nsIContent> xblNode = do_QueryInterface(native);
169 if (!xblNode) {
170 xpc::Throw(cx, NS_ERROR_UNEXPECTED);
171 return false;
174 // Now that |this| is okay, actually install the field.
176 // Because of the possibility (due to XBL binding inheritance, because each
177 // XBL binding lives in its own global object) that |this| might be in a
178 // different compartment from the callee (not to mention that this method can
179 // be called with an arbitrary |this| regardless of how insane XBL is), and
180 // because in this method we've entered |this|'s compartment (see in
181 // Field[GS]etter where we attempt a cross-compartment call), we must enter
182 // the callee's compartment to access its reserved slots.
183 nsXBLPrototypeBinding* protoBinding;
184 nsAutoJSString fieldName;
186 JSAutoCompartment ac(cx, callee);
188 JS::Rooted<JSObject*> xblProto(cx);
189 xblProto = &js::GetFunctionNativeReserved(callee, XBLPROTO_SLOT).toObject();
191 JS::Rooted<JS::Value> name(cx, js::GetFunctionNativeReserved(callee, FIELD_SLOT));
192 if (!fieldName.init(cx, name.toString())) {
193 return false;
196 MOZ_ALWAYS_TRUE(JS_ValueToId(cx, name, idp));
198 // If a separate XBL scope is being used, the callee is not same-compartment
199 // with the xbl prototype, and the object is a cross-compartment wrapper.
200 xblProto = js::UncheckedUnwrap(xblProto);
201 JSAutoCompartment ac2(cx, xblProto);
202 JS::Value slotVal = ::JS_GetReservedSlot(xblProto, 0);
203 protoBinding = static_cast<nsXBLPrototypeBinding*>(slotVal.toPrivate());
204 MOZ_ASSERT(protoBinding);
207 nsXBLProtoImplField* field = protoBinding->FindField(fieldName);
208 MOZ_ASSERT(field);
210 nsresult rv = field->InstallField(thisObj, protoBinding->DocURI(), installed);
211 if (NS_SUCCEEDED(rv)) {
212 return true;
215 if (!::JS_IsExceptionPending(cx)) {
216 xpc::Throw(cx, rv);
218 return false;
221 bool
222 FieldGetterImpl(JSContext *cx, JS::CallArgs args)
224 JS::Handle<JS::Value> thisv = args.thisv();
225 MOZ_ASSERT(ValueHasISupportsPrivate(thisv));
227 JS::Rooted<JSObject*> thisObj(cx, &thisv.toObject());
229 // We should be in the compartment of |this|. If we got here via nativeCall,
230 // |this| is not same-compartment with |callee|, and it's possible via
231 // asymmetric security semantics that |args.calleev()| is actually a security
232 // wrapper. In this case, we know we want to do an unsafe unwrap, and
233 // InstallXBLField knows how to handle cross-compartment pointers.
234 bool installed = false;
235 JS::Rooted<JSObject*> callee(cx, js::UncheckedUnwrap(&args.calleev().toObject()));
236 JS::Rooted<jsid> id(cx);
237 if (!InstallXBLField(cx, callee, thisObj, &id, &installed)) {
238 return false;
241 if (!installed) {
242 args.rval().setUndefined();
243 return true;
246 JS::Rooted<JS::Value> v(cx);
247 if (!JS_GetPropertyById(cx, thisObj, id, &v)) {
248 return false;
250 args.rval().set(v);
251 return true;
254 static bool
255 FieldGetter(JSContext *cx, unsigned argc, JS::Value *vp)
257 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
258 return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldGetterImpl>
259 (cx, args);
262 bool
263 FieldSetterImpl(JSContext *cx, JS::CallArgs args)
265 JS::Handle<JS::Value> thisv = args.thisv();
266 MOZ_ASSERT(ValueHasISupportsPrivate(thisv));
268 JS::Rooted<JSObject*> thisObj(cx, &thisv.toObject());
270 // We should be in the compartment of |this|. If we got here via nativeCall,
271 // |this| is not same-compartment with |callee|, and it's possible via
272 // asymmetric security semantics that |args.calleev()| is actually a security
273 // wrapper. In this case, we know we want to do an unsafe unwrap, and
274 // InstallXBLField knows how to handle cross-compartment pointers.
275 bool installed = false;
276 JS::Rooted<JSObject*> callee(cx, js::UncheckedUnwrap(&args.calleev().toObject()));
277 JS::Rooted<jsid> id(cx);
278 if (!InstallXBLField(cx, callee, thisObj, &id, &installed)) {
279 return false;
282 if (installed) {
283 if (!::JS_SetPropertyById(cx, thisObj, id, args.get(0))) {
284 return false;
287 args.rval().setUndefined();
288 return true;
291 static bool
292 FieldSetter(JSContext *cx, unsigned argc, JS::Value *vp)
294 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
295 return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldSetterImpl>
296 (cx, args);
299 nsresult
300 nsXBLProtoImplField::InstallAccessors(JSContext* aCx,
301 JS::Handle<JSObject*> aTargetClassObject)
303 MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
304 JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
305 JS::Rooted<JSObject*> scopeObject(aCx, xpc::GetXBLScopeOrGlobal(aCx, globalObject));
306 NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
308 // Don't install it if the field is empty; see also InstallField which also must
309 // implement the not-empty requirement.
310 if (IsEmpty()) {
311 return NS_OK;
314 // Install a getter/setter pair which will resolve the field onto the actual
315 // object, when invoked.
317 // Get the field name as an id.
318 JS::Rooted<jsid> id(aCx);
319 JS::TwoByteChars chars(mName, NS_strlen(mName));
320 if (!JS_CharsToId(aCx, chars, &id))
321 return NS_ERROR_OUT_OF_MEMORY;
323 // Properties/Methods have historically taken precendence over fields. We
324 // install members first, so just bounce here if the property is already
325 // defined.
326 bool found = false;
327 if (!JS_AlreadyHasOwnPropertyById(aCx, aTargetClassObject, id, &found))
328 return NS_ERROR_FAILURE;
329 if (found)
330 return NS_OK;
332 // FieldGetter and FieldSetter need to run in the XBL scope so that they can
333 // see through any SOWs on their targets.
335 // First, enter the XBL scope, and compile the functions there.
336 JSAutoCompartment ac(aCx, scopeObject);
337 JS::Rooted<JS::Value> wrappedClassObj(aCx, JS::ObjectValue(*aTargetClassObject));
338 if (!JS_WrapValue(aCx, &wrappedClassObj))
339 return NS_ERROR_OUT_OF_MEMORY;
341 JS::Rooted<JSObject*> get(aCx,
342 JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx, FieldGetter,
343 0, 0, scopeObject, id)));
344 if (!get) {
345 return NS_ERROR_OUT_OF_MEMORY;
347 js::SetFunctionNativeReserved(get, XBLPROTO_SLOT, wrappedClassObj);
348 js::SetFunctionNativeReserved(get, FIELD_SLOT,
349 JS::StringValue(JSID_TO_STRING(id)));
351 JS::Rooted<JSObject*> set(aCx,
352 JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx, FieldSetter,
353 1, 0, scopeObject, id)));
354 if (!set) {
355 return NS_ERROR_OUT_OF_MEMORY;
357 js::SetFunctionNativeReserved(set, XBLPROTO_SLOT, wrappedClassObj);
358 js::SetFunctionNativeReserved(set, FIELD_SLOT,
359 JS::StringValue(JSID_TO_STRING(id)));
361 // Now, re-enter the class object's scope, wrap the getters/setters, and define
362 // them there.
363 JSAutoCompartment ac2(aCx, aTargetClassObject);
364 if (!JS_WrapObject(aCx, &get) || !JS_WrapObject(aCx, &set)) {
365 return NS_ERROR_OUT_OF_MEMORY;
368 if (!::JS_DefinePropertyById(aCx, aTargetClassObject, id, JS::UndefinedHandleValue,
369 AccessorAttributes(),
370 JS_DATA_TO_FUNC_PTR(JSPropertyOp, get.get()),
371 JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, set.get()))) {
372 return NS_ERROR_OUT_OF_MEMORY;
375 return NS_OK;
378 nsresult
379 nsXBLProtoImplField::InstallField(JS::Handle<JSObject*> aBoundNode,
380 nsIURI* aBindingDocURI,
381 bool* aDidInstall) const
383 NS_PRECONDITION(aBoundNode,
384 "uh-oh, bound node should NOT be null or bad things will "
385 "happen");
387 *aDidInstall = false;
389 // Empty fields are treated as not actually present.
390 if (IsEmpty()) {
391 return NS_OK;
394 nsAutoMicroTask mt;
396 // EvaluateString and JS_DefineUCProperty can both trigger GC, so
397 // protect |result| here.
398 nsresult rv;
400 nsAutoCString uriSpec;
401 aBindingDocURI->GetSpec(uriSpec);
403 nsIGlobalObject* globalObject = xpc::WindowGlobalOrNull(aBoundNode);
404 if (!globalObject) {
405 return NS_OK;
408 // We are going to run script via EvaluateString, so we need a script entry
409 // point, but as this is XBL related it does not appear in the HTML spec.
410 AutoEntryScript entryScript(globalObject, true);
411 JSContext* cx = entryScript.cx();
413 NS_ASSERTION(!::JS_IsExceptionPending(cx),
414 "Shouldn't get here when an exception is pending!");
416 JSAddonId* addonId = MapURIToAddonID(aBindingDocURI);
418 // First, enter the xbl scope, wrap the node, and use that as the scope for
419 // the evaluation.
420 JS::Rooted<JSObject*> scopeObject(cx, xpc::GetScopeForXBLExecution(cx, aBoundNode, addonId));
421 NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
422 JSAutoCompartment ac(cx, scopeObject);
424 JS::Rooted<JSObject*> wrappedNode(cx, aBoundNode);
425 if (!JS_WrapObject(cx, &wrappedNode))
426 return NS_ERROR_OUT_OF_MEMORY;
428 JS::Rooted<JS::Value> result(cx);
429 JS::CompileOptions options(cx);
430 options.setFileAndLine(uriSpec.get(), mLineNumber)
431 .setVersion(JSVERSION_LATEST);
432 nsJSUtils::EvaluateOptions evalOptions;
433 rv = nsJSUtils::EvaluateString(cx, nsDependentString(mFieldText,
434 mFieldTextLength),
435 wrappedNode, options, evalOptions,
436 &result);
437 if (NS_FAILED(rv)) {
438 return rv;
442 // Now, enter the node's compartment, wrap the eval result, and define it on
443 // the bound node.
444 JSAutoCompartment ac2(cx, aBoundNode);
445 nsDependentString name(mName);
446 if (!JS_WrapValue(cx, &result) ||
447 !::JS_DefineUCProperty(cx, aBoundNode,
448 reinterpret_cast<const jschar*>(mName),
449 name.Length(), result, mJSAttributes)) {
450 return NS_ERROR_OUT_OF_MEMORY;
453 *aDidInstall = true;
454 return NS_OK;
457 nsresult
458 nsXBLProtoImplField::Read(nsIObjectInputStream* aStream)
460 nsAutoString name;
461 nsresult rv = aStream->ReadString(name);
462 NS_ENSURE_SUCCESS(rv, rv);
463 mName = ToNewUnicode(name);
465 rv = aStream->Read32(&mLineNumber);
466 NS_ENSURE_SUCCESS(rv, rv);
468 nsAutoString fieldText;
469 rv = aStream->ReadString(fieldText);
470 NS_ENSURE_SUCCESS(rv, rv);
471 mFieldTextLength = fieldText.Length();
472 if (mFieldTextLength)
473 mFieldText = ToNewUnicode(fieldText);
475 return NS_OK;
478 nsresult
479 nsXBLProtoImplField::Write(nsIObjectOutputStream* aStream)
481 XBLBindingSerializeDetails type = XBLBinding_Serialize_Field;
483 if (mJSAttributes & JSPROP_READONLY) {
484 type |= XBLBinding_Serialize_ReadOnly;
487 nsresult rv = aStream->Write8(type);
488 NS_ENSURE_SUCCESS(rv, rv);
489 rv = aStream->WriteWStringZ(mName);
490 NS_ENSURE_SUCCESS(rv, rv);
491 rv = aStream->Write32(mLineNumber);
492 NS_ENSURE_SUCCESS(rv, rv);
494 return aStream->WriteWStringZ(mFieldText ? mFieldText : MOZ_UTF16(""));