Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsIAtom.h" |
michael@0 | 7 | #include "nsIContent.h" |
michael@0 | 8 | #include "nsString.h" |
michael@0 | 9 | #include "nsJSUtils.h" |
michael@0 | 10 | #include "jsapi.h" |
michael@0 | 11 | #include "js/CharacterEncoding.h" |
michael@0 | 12 | #include "nsUnicharUtils.h" |
michael@0 | 13 | #include "nsReadableUtils.h" |
michael@0 | 14 | #include "nsXBLProtoImplField.h" |
michael@0 | 15 | #include "nsIScriptContext.h" |
michael@0 | 16 | #include "nsIURI.h" |
michael@0 | 17 | #include "nsXBLSerialize.h" |
michael@0 | 18 | #include "nsXBLPrototypeBinding.h" |
michael@0 | 19 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 20 | #include "mozilla/dom/ScriptSettings.h" |
michael@0 | 21 | #include "nsGlobalWindow.h" |
michael@0 | 22 | #include "xpcpublic.h" |
michael@0 | 23 | #include "WrapperFactory.h" |
michael@0 | 24 | |
michael@0 | 25 | using namespace mozilla; |
michael@0 | 26 | using namespace mozilla::dom; |
michael@0 | 27 | |
michael@0 | 28 | nsXBLProtoImplField::nsXBLProtoImplField(const char16_t* aName, const char16_t* aReadOnly) |
michael@0 | 29 | : mNext(nullptr), |
michael@0 | 30 | mFieldText(nullptr), |
michael@0 | 31 | mFieldTextLength(0), |
michael@0 | 32 | mLineNumber(0) |
michael@0 | 33 | { |
michael@0 | 34 | MOZ_COUNT_CTOR(nsXBLProtoImplField); |
michael@0 | 35 | mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer? |
michael@0 | 36 | |
michael@0 | 37 | mJSAttributes = JSPROP_ENUMERATE; |
michael@0 | 38 | if (aReadOnly) { |
michael@0 | 39 | nsAutoString readOnly; readOnly.Assign(aReadOnly); |
michael@0 | 40 | if (readOnly.LowerCaseEqualsLiteral("true")) |
michael@0 | 41 | mJSAttributes |= JSPROP_READONLY; |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | |
michael@0 | 46 | nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly) |
michael@0 | 47 | : mNext(nullptr), |
michael@0 | 48 | mFieldText(nullptr), |
michael@0 | 49 | mFieldTextLength(0), |
michael@0 | 50 | mLineNumber(0) |
michael@0 | 51 | { |
michael@0 | 52 | MOZ_COUNT_CTOR(nsXBLProtoImplField); |
michael@0 | 53 | |
michael@0 | 54 | mJSAttributes = JSPROP_ENUMERATE; |
michael@0 | 55 | if (aIsReadOnly) |
michael@0 | 56 | mJSAttributes |= JSPROP_READONLY; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | nsXBLProtoImplField::~nsXBLProtoImplField() |
michael@0 | 60 | { |
michael@0 | 61 | MOZ_COUNT_DTOR(nsXBLProtoImplField); |
michael@0 | 62 | if (mFieldText) |
michael@0 | 63 | nsMemory::Free(mFieldText); |
michael@0 | 64 | NS_Free(mName); |
michael@0 | 65 | NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | void |
michael@0 | 69 | nsXBLProtoImplField::AppendFieldText(const nsAString& aText) |
michael@0 | 70 | { |
michael@0 | 71 | if (mFieldText) { |
michael@0 | 72 | nsDependentString fieldTextStr(mFieldText, mFieldTextLength); |
michael@0 | 73 | nsAutoString newFieldText = fieldTextStr + aText; |
michael@0 | 74 | char16_t* temp = mFieldText; |
michael@0 | 75 | mFieldText = ToNewUnicode(newFieldText); |
michael@0 | 76 | mFieldTextLength = newFieldText.Length(); |
michael@0 | 77 | nsMemory::Free(temp); |
michael@0 | 78 | } |
michael@0 | 79 | else { |
michael@0 | 80 | mFieldText = ToNewUnicode(aText); |
michael@0 | 81 | mFieldTextLength = aText.Length(); |
michael@0 | 82 | } |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // XBL fields are represented on elements inheriting that field a bit trickily. |
michael@0 | 86 | // When setting up the XBL prototype object, we install accessors for the fields |
michael@0 | 87 | // on the prototype object. Those accessors, when used, will then (via |
michael@0 | 88 | // InstallXBLField below) reify a property for the field onto the actual XBL-backed |
michael@0 | 89 | // element. |
michael@0 | 90 | // |
michael@0 | 91 | // The accessor property is a plain old property backed by a getter function and |
michael@0 | 92 | // a setter function. These properties are backed by the FieldGetter and |
michael@0 | 93 | // FieldSetter natives; they're created by InstallAccessors. The precise field to be |
michael@0 | 94 | // reified is identified using two extra slots on the getter/setter functions. |
michael@0 | 95 | // XBLPROTO_SLOT stores the XBL prototype object that provides the field. |
michael@0 | 96 | // FIELD_SLOT stores the name of the field, i.e. its JavaScript property name. |
michael@0 | 97 | // |
michael@0 | 98 | // This two-step field installation process -- creating an accessor on the |
michael@0 | 99 | // prototype, then have that reify an own property on the actual element -- is |
michael@0 | 100 | // admittedly convoluted. Better would be for XBL-backed elements to be proxies |
michael@0 | 101 | // that could resolve fields onto themselves. But given that XBL bindings are |
michael@0 | 102 | // associated with elements mutably -- you can add/remove/change -moz-binding |
michael@0 | 103 | // whenever you want, alas -- doing so would require all elements to be proxies, |
michael@0 | 104 | // which isn't performant now. So we do this two-step instead. |
michael@0 | 105 | static const uint32_t XBLPROTO_SLOT = 0; |
michael@0 | 106 | static const uint32_t FIELD_SLOT = 1; |
michael@0 | 107 | |
michael@0 | 108 | bool |
michael@0 | 109 | ValueHasISupportsPrivate(JS::Handle<JS::Value> v) |
michael@0 | 110 | { |
michael@0 | 111 | if (!v.isObject()) { |
michael@0 | 112 | return false; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | const DOMClass* domClass = GetDOMClass(&v.toObject()); |
michael@0 | 116 | if (domClass) { |
michael@0 | 117 | return domClass->mDOMObjectIsISupports; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | const JSClass* clasp = ::JS_GetClass(&v.toObject()); |
michael@0 | 121 | const uint32_t HAS_PRIVATE_NSISUPPORTS = |
michael@0 | 122 | JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS; |
michael@0 | 123 | return (clasp->flags & HAS_PRIVATE_NSISUPPORTS) == HAS_PRIVATE_NSISUPPORTS; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | #ifdef DEBUG |
michael@0 | 127 | static bool |
michael@0 | 128 | ValueHasISupportsPrivate(JSContext* cx, const JS::Value& aVal) |
michael@0 | 129 | { |
michael@0 | 130 | JS::Rooted<JS::Value> v(cx, aVal); |
michael@0 | 131 | return ValueHasISupportsPrivate(v); |
michael@0 | 132 | } |
michael@0 | 133 | #endif |
michael@0 | 134 | |
michael@0 | 135 | // Define a shadowing property on |this| for the XBL field defined by the |
michael@0 | 136 | // contents of the callee's reserved slots. If the property was defined, |
michael@0 | 137 | // *installed will be true, and idp will be set to the property name that was |
michael@0 | 138 | // defined. |
michael@0 | 139 | static bool |
michael@0 | 140 | InstallXBLField(JSContext* cx, |
michael@0 | 141 | JS::Handle<JSObject*> callee, JS::Handle<JSObject*> thisObj, |
michael@0 | 142 | JS::MutableHandle<jsid> idp, bool* installed) |
michael@0 | 143 | { |
michael@0 | 144 | *installed = false; |
michael@0 | 145 | |
michael@0 | 146 | // First ensure |this| is a reasonable XBL bound node. |
michael@0 | 147 | // |
michael@0 | 148 | // FieldAccessorGuard already determined whether |thisObj| was acceptable as |
michael@0 | 149 | // |this| in terms of not throwing a TypeError. Assert this for good measure. |
michael@0 | 150 | MOZ_ASSERT(ValueHasISupportsPrivate(cx, JS::ObjectValue(*thisObj))); |
michael@0 | 151 | |
michael@0 | 152 | // But there are some cases where we must accept |thisObj| but not install a |
michael@0 | 153 | // property on it, or otherwise touch it. Hence this split of |this|-vetting |
michael@0 | 154 | // duties. |
michael@0 | 155 | nsISupports* native = |
michael@0 | 156 | nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, thisObj); |
michael@0 | 157 | if (!native) { |
michael@0 | 158 | // Looks like whatever |thisObj| is it's not our nsIContent. It might well |
michael@0 | 159 | // be the proto our binding installed, however, where the private is the |
michael@0 | 160 | // nsXBLDocumentInfo, so just baul out quietly. Do NOT throw an exception |
michael@0 | 161 | // here. |
michael@0 | 162 | // |
michael@0 | 163 | // We could make this stricter by checking the class maybe, but whatever. |
michael@0 | 164 | return true; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | nsCOMPtr<nsIContent> xblNode = do_QueryInterface(native); |
michael@0 | 168 | if (!xblNode) { |
michael@0 | 169 | xpc::Throw(cx, NS_ERROR_UNEXPECTED); |
michael@0 | 170 | return false; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | // Now that |this| is okay, actually install the field. |
michael@0 | 174 | |
michael@0 | 175 | // Because of the possibility (due to XBL binding inheritance, because each |
michael@0 | 176 | // XBL binding lives in its own global object) that |this| might be in a |
michael@0 | 177 | // different compartment from the callee (not to mention that this method can |
michael@0 | 178 | // be called with an arbitrary |this| regardless of how insane XBL is), and |
michael@0 | 179 | // because in this method we've entered |this|'s compartment (see in |
michael@0 | 180 | // Field[GS]etter where we attempt a cross-compartment call), we must enter |
michael@0 | 181 | // the callee's compartment to access its reserved slots. |
michael@0 | 182 | nsXBLPrototypeBinding* protoBinding; |
michael@0 | 183 | nsDependentJSString fieldName; |
michael@0 | 184 | { |
michael@0 | 185 | JSAutoCompartment ac(cx, callee); |
michael@0 | 186 | |
michael@0 | 187 | JS::Rooted<JSObject*> xblProto(cx); |
michael@0 | 188 | xblProto = &js::GetFunctionNativeReserved(callee, XBLPROTO_SLOT).toObject(); |
michael@0 | 189 | |
michael@0 | 190 | JS::Rooted<JS::Value> name(cx, js::GetFunctionNativeReserved(callee, FIELD_SLOT)); |
michael@0 | 191 | JSFlatString* fieldStr = JS_ASSERT_STRING_IS_FLAT(name.toString()); |
michael@0 | 192 | fieldName.init(fieldStr); |
michael@0 | 193 | |
michael@0 | 194 | MOZ_ALWAYS_TRUE(JS_ValueToId(cx, name, idp)); |
michael@0 | 195 | |
michael@0 | 196 | // If a separate XBL scope is being used, the callee is not same-compartment |
michael@0 | 197 | // with the xbl prototype, and the object is a cross-compartment wrapper. |
michael@0 | 198 | xblProto = js::UncheckedUnwrap(xblProto); |
michael@0 | 199 | JSAutoCompartment ac2(cx, xblProto); |
michael@0 | 200 | JS::Value slotVal = ::JS_GetReservedSlot(xblProto, 0); |
michael@0 | 201 | protoBinding = static_cast<nsXBLPrototypeBinding*>(slotVal.toPrivate()); |
michael@0 | 202 | MOZ_ASSERT(protoBinding); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | nsXBLProtoImplField* field = protoBinding->FindField(fieldName); |
michael@0 | 206 | MOZ_ASSERT(field); |
michael@0 | 207 | |
michael@0 | 208 | nsresult rv = field->InstallField(thisObj, protoBinding->DocURI(), installed); |
michael@0 | 209 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 210 | return true; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | if (!::JS_IsExceptionPending(cx)) { |
michael@0 | 214 | xpc::Throw(cx, rv); |
michael@0 | 215 | } |
michael@0 | 216 | return false; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | bool |
michael@0 | 220 | FieldGetterImpl(JSContext *cx, JS::CallArgs args) |
michael@0 | 221 | { |
michael@0 | 222 | JS::Handle<JS::Value> thisv = args.thisv(); |
michael@0 | 223 | MOZ_ASSERT(ValueHasISupportsPrivate(thisv)); |
michael@0 | 224 | |
michael@0 | 225 | JS::Rooted<JSObject*> thisObj(cx, &thisv.toObject()); |
michael@0 | 226 | |
michael@0 | 227 | // We should be in the compartment of |this|. If we got here via nativeCall, |
michael@0 | 228 | // |this| is not same-compartment with |callee|, and it's possible via |
michael@0 | 229 | // asymmetric security semantics that |args.calleev()| is actually a security |
michael@0 | 230 | // wrapper. In this case, we know we want to do an unsafe unwrap, and |
michael@0 | 231 | // InstallXBLField knows how to handle cross-compartment pointers. |
michael@0 | 232 | bool installed = false; |
michael@0 | 233 | JS::Rooted<JSObject*> callee(cx, js::UncheckedUnwrap(&args.calleev().toObject())); |
michael@0 | 234 | JS::Rooted<jsid> id(cx); |
michael@0 | 235 | if (!InstallXBLField(cx, callee, thisObj, &id, &installed)) { |
michael@0 | 236 | return false; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | if (!installed) { |
michael@0 | 240 | args.rval().setUndefined(); |
michael@0 | 241 | return true; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | JS::Rooted<JS::Value> v(cx); |
michael@0 | 245 | if (!JS_GetPropertyById(cx, thisObj, id, &v)) { |
michael@0 | 246 | return false; |
michael@0 | 247 | } |
michael@0 | 248 | args.rval().set(v); |
michael@0 | 249 | return true; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | static bool |
michael@0 | 253 | FieldGetter(JSContext *cx, unsigned argc, JS::Value *vp) |
michael@0 | 254 | { |
michael@0 | 255 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
michael@0 | 256 | return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldGetterImpl> |
michael@0 | 257 | (cx, args); |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | bool |
michael@0 | 261 | FieldSetterImpl(JSContext *cx, JS::CallArgs args) |
michael@0 | 262 | { |
michael@0 | 263 | JS::Handle<JS::Value> thisv = args.thisv(); |
michael@0 | 264 | MOZ_ASSERT(ValueHasISupportsPrivate(thisv)); |
michael@0 | 265 | |
michael@0 | 266 | JS::Rooted<JSObject*> thisObj(cx, &thisv.toObject()); |
michael@0 | 267 | |
michael@0 | 268 | // We should be in the compartment of |this|. If we got here via nativeCall, |
michael@0 | 269 | // |this| is not same-compartment with |callee|, and it's possible via |
michael@0 | 270 | // asymmetric security semantics that |args.calleev()| is actually a security |
michael@0 | 271 | // wrapper. In this case, we know we want to do an unsafe unwrap, and |
michael@0 | 272 | // InstallXBLField knows how to handle cross-compartment pointers. |
michael@0 | 273 | bool installed = false; |
michael@0 | 274 | JS::Rooted<JSObject*> callee(cx, js::UncheckedUnwrap(&args.calleev().toObject())); |
michael@0 | 275 | JS::Rooted<jsid> id(cx); |
michael@0 | 276 | if (!InstallXBLField(cx, callee, thisObj, &id, &installed)) { |
michael@0 | 277 | return false; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | if (installed) { |
michael@0 | 281 | if (!::JS_SetPropertyById(cx, thisObj, id, args.get(0))) { |
michael@0 | 282 | return false; |
michael@0 | 283 | } |
michael@0 | 284 | } |
michael@0 | 285 | args.rval().setUndefined(); |
michael@0 | 286 | return true; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | static bool |
michael@0 | 290 | FieldSetter(JSContext *cx, unsigned argc, JS::Value *vp) |
michael@0 | 291 | { |
michael@0 | 292 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
michael@0 | 293 | return JS::CallNonGenericMethod<ValueHasISupportsPrivate, FieldSetterImpl> |
michael@0 | 294 | (cx, args); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | nsresult |
michael@0 | 298 | nsXBLProtoImplField::InstallAccessors(JSContext* aCx, |
michael@0 | 299 | JS::Handle<JSObject*> aTargetClassObject) |
michael@0 | 300 | { |
michael@0 | 301 | MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx)); |
michael@0 | 302 | JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject)); |
michael@0 | 303 | JS::Rooted<JSObject*> scopeObject(aCx, xpc::GetXBLScopeOrGlobal(aCx, globalObject)); |
michael@0 | 304 | NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 305 | |
michael@0 | 306 | // Don't install it if the field is empty; see also InstallField which also must |
michael@0 | 307 | // implement the not-empty requirement. |
michael@0 | 308 | if (IsEmpty()) { |
michael@0 | 309 | return NS_OK; |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | // Install a getter/setter pair which will resolve the field onto the actual |
michael@0 | 313 | // object, when invoked. |
michael@0 | 314 | |
michael@0 | 315 | // Get the field name as an id. |
michael@0 | 316 | JS::Rooted<jsid> id(aCx); |
michael@0 | 317 | JS::TwoByteChars chars(mName, NS_strlen(mName)); |
michael@0 | 318 | if (!JS_CharsToId(aCx, chars, &id)) |
michael@0 | 319 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 320 | |
michael@0 | 321 | // Properties/Methods have historically taken precendence over fields. We |
michael@0 | 322 | // install members first, so just bounce here if the property is already |
michael@0 | 323 | // defined. |
michael@0 | 324 | bool found = false; |
michael@0 | 325 | if (!JS_AlreadyHasOwnPropertyById(aCx, aTargetClassObject, id, &found)) |
michael@0 | 326 | return NS_ERROR_FAILURE; |
michael@0 | 327 | if (found) |
michael@0 | 328 | return NS_OK; |
michael@0 | 329 | |
michael@0 | 330 | // FieldGetter and FieldSetter need to run in the XBL scope so that they can |
michael@0 | 331 | // see through any SOWs on their targets. |
michael@0 | 332 | |
michael@0 | 333 | // First, enter the XBL scope, and compile the functions there. |
michael@0 | 334 | JSAutoCompartment ac(aCx, scopeObject); |
michael@0 | 335 | JS::Rooted<JS::Value> wrappedClassObj(aCx, JS::ObjectValue(*aTargetClassObject)); |
michael@0 | 336 | if (!JS_WrapValue(aCx, &wrappedClassObj) || !JS_WrapId(aCx, &id)) |
michael@0 | 337 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 338 | |
michael@0 | 339 | JS::Rooted<JSObject*> get(aCx, |
michael@0 | 340 | JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx, FieldGetter, |
michael@0 | 341 | 0, 0, scopeObject, id))); |
michael@0 | 342 | if (!get) { |
michael@0 | 343 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 344 | } |
michael@0 | 345 | js::SetFunctionNativeReserved(get, XBLPROTO_SLOT, wrappedClassObj); |
michael@0 | 346 | js::SetFunctionNativeReserved(get, FIELD_SLOT, |
michael@0 | 347 | JS::StringValue(JSID_TO_STRING(id))); |
michael@0 | 348 | |
michael@0 | 349 | JS::Rooted<JSObject*> set(aCx, |
michael@0 | 350 | JS_GetFunctionObject(js::NewFunctionByIdWithReserved(aCx, FieldSetter, |
michael@0 | 351 | 1, 0, scopeObject, id))); |
michael@0 | 352 | if (!set) { |
michael@0 | 353 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 354 | } |
michael@0 | 355 | js::SetFunctionNativeReserved(set, XBLPROTO_SLOT, wrappedClassObj); |
michael@0 | 356 | js::SetFunctionNativeReserved(set, FIELD_SLOT, |
michael@0 | 357 | JS::StringValue(JSID_TO_STRING(id))); |
michael@0 | 358 | |
michael@0 | 359 | // Now, re-enter the class object's scope, wrap the getters/setters, and define |
michael@0 | 360 | // them there. |
michael@0 | 361 | JSAutoCompartment ac2(aCx, aTargetClassObject); |
michael@0 | 362 | if (!JS_WrapObject(aCx, &get) || !JS_WrapObject(aCx, &set) || |
michael@0 | 363 | !JS_WrapId(aCx, &id)) |
michael@0 | 364 | { |
michael@0 | 365 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | if (!::JS_DefinePropertyById(aCx, aTargetClassObject, id, JS::UndefinedValue(), |
michael@0 | 369 | JS_DATA_TO_FUNC_PTR(JSPropertyOp, get.get()), |
michael@0 | 370 | JS_DATA_TO_FUNC_PTR(JSStrictPropertyOp, set.get()), |
michael@0 | 371 | AccessorAttributes())) { |
michael@0 | 372 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | return NS_OK; |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | nsresult |
michael@0 | 379 | nsXBLProtoImplField::InstallField(JS::Handle<JSObject*> aBoundNode, |
michael@0 | 380 | nsIURI* aBindingDocURI, |
michael@0 | 381 | bool* aDidInstall) const |
michael@0 | 382 | { |
michael@0 | 383 | NS_PRECONDITION(aBoundNode, |
michael@0 | 384 | "uh-oh, bound node should NOT be null or bad things will " |
michael@0 | 385 | "happen"); |
michael@0 | 386 | |
michael@0 | 387 | *aDidInstall = false; |
michael@0 | 388 | |
michael@0 | 389 | // Empty fields are treated as not actually present. |
michael@0 | 390 | if (IsEmpty()) { |
michael@0 | 391 | return NS_OK; |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | nsAutoMicroTask mt; |
michael@0 | 395 | |
michael@0 | 396 | // EvaluateString and JS_DefineUCProperty can both trigger GC, so |
michael@0 | 397 | // protect |result| here. |
michael@0 | 398 | nsresult rv; |
michael@0 | 399 | |
michael@0 | 400 | nsAutoCString uriSpec; |
michael@0 | 401 | aBindingDocURI->GetSpec(uriSpec); |
michael@0 | 402 | |
michael@0 | 403 | nsIGlobalObject* globalObject = xpc::WindowGlobalOrNull(aBoundNode); |
michael@0 | 404 | if (!globalObject) { |
michael@0 | 405 | return NS_OK; |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | // We are going to run script via EvaluateString, so we need a script entry |
michael@0 | 409 | // point, but as this is XBL related it does not appear in the HTML spec. |
michael@0 | 410 | AutoEntryScript entryScript(globalObject, true); |
michael@0 | 411 | JSContext* cx = entryScript.cx(); |
michael@0 | 412 | |
michael@0 | 413 | NS_ASSERTION(!::JS_IsExceptionPending(cx), |
michael@0 | 414 | "Shouldn't get here when an exception is pending!"); |
michael@0 | 415 | |
michael@0 | 416 | // First, enter the xbl scope, wrap the node, and use that as the scope for |
michael@0 | 417 | // the evaluation. |
michael@0 | 418 | JS::Rooted<JSObject*> scopeObject(cx, xpc::GetXBLScopeOrGlobal(cx, aBoundNode)); |
michael@0 | 419 | NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 420 | JSAutoCompartment ac(cx, scopeObject); |
michael@0 | 421 | |
michael@0 | 422 | JS::Rooted<JSObject*> wrappedNode(cx, aBoundNode); |
michael@0 | 423 | if (!JS_WrapObject(cx, &wrappedNode)) |
michael@0 | 424 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 425 | |
michael@0 | 426 | JS::Rooted<JS::Value> result(cx); |
michael@0 | 427 | JS::CompileOptions options(cx); |
michael@0 | 428 | options.setFileAndLine(uriSpec.get(), mLineNumber) |
michael@0 | 429 | .setVersion(JSVERSION_LATEST); |
michael@0 | 430 | nsJSUtils::EvaluateOptions evalOptions; |
michael@0 | 431 | rv = nsJSUtils::EvaluateString(cx, nsDependentString(mFieldText, |
michael@0 | 432 | mFieldTextLength), |
michael@0 | 433 | wrappedNode, options, evalOptions, |
michael@0 | 434 | &result); |
michael@0 | 435 | if (NS_FAILED(rv)) { |
michael@0 | 436 | return rv; |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | |
michael@0 | 440 | // Now, enter the node's compartment, wrap the eval result, and define it on |
michael@0 | 441 | // the bound node. |
michael@0 | 442 | JSAutoCompartment ac2(cx, aBoundNode); |
michael@0 | 443 | nsDependentString name(mName); |
michael@0 | 444 | if (!JS_WrapValue(cx, &result) || |
michael@0 | 445 | !::JS_DefineUCProperty(cx, aBoundNode, |
michael@0 | 446 | reinterpret_cast<const jschar*>(mName), |
michael@0 | 447 | name.Length(), result, nullptr, nullptr, |
michael@0 | 448 | mJSAttributes)) { |
michael@0 | 449 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | *aDidInstall = true; |
michael@0 | 453 | return NS_OK; |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | nsresult |
michael@0 | 457 | nsXBLProtoImplField::Read(nsIObjectInputStream* aStream) |
michael@0 | 458 | { |
michael@0 | 459 | nsAutoString name; |
michael@0 | 460 | nsresult rv = aStream->ReadString(name); |
michael@0 | 461 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 462 | mName = ToNewUnicode(name); |
michael@0 | 463 | |
michael@0 | 464 | rv = aStream->Read32(&mLineNumber); |
michael@0 | 465 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 466 | |
michael@0 | 467 | nsAutoString fieldText; |
michael@0 | 468 | rv = aStream->ReadString(fieldText); |
michael@0 | 469 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 470 | mFieldTextLength = fieldText.Length(); |
michael@0 | 471 | if (mFieldTextLength) |
michael@0 | 472 | mFieldText = ToNewUnicode(fieldText); |
michael@0 | 473 | |
michael@0 | 474 | return NS_OK; |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | nsresult |
michael@0 | 478 | nsXBLProtoImplField::Write(nsIObjectOutputStream* aStream) |
michael@0 | 479 | { |
michael@0 | 480 | XBLBindingSerializeDetails type = XBLBinding_Serialize_Field; |
michael@0 | 481 | |
michael@0 | 482 | if (mJSAttributes & JSPROP_READONLY) { |
michael@0 | 483 | type |= XBLBinding_Serialize_ReadOnly; |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | nsresult rv = aStream->Write8(type); |
michael@0 | 487 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 488 | rv = aStream->WriteWStringZ(mName); |
michael@0 | 489 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 490 | rv = aStream->Write32(mLineNumber); |
michael@0 | 491 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 492 | |
michael@0 | 493 | return aStream->WriteWStringZ(mFieldText ? mFieldText : MOZ_UTF16("")); |
michael@0 | 494 | } |