michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: michael@0: #include "nsXBLProtoImpl.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCxPusher.h" michael@0: #include "nsIScriptGlobalObject.h" michael@0: #include "nsIScriptContext.h" michael@0: #include "nsIXPConnect.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIDOMNode.h" michael@0: #include "nsXBLPrototypeBinding.h" michael@0: #include "nsXBLProtoImplProperty.h" michael@0: #include "nsIURI.h" michael@0: #include "mozilla/dom/XULElementBinding.h" michael@0: #include "xpcpublic.h" michael@0: #include "js/CharacterEncoding.h" michael@0: michael@0: using namespace mozilla; michael@0: using js::GetGlobalForObjectCrossCompartment; michael@0: using js::AssertSameCompartment; michael@0: michael@0: nsresult michael@0: nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aPrototypeBinding, michael@0: nsXBLBinding* aBinding) michael@0: { michael@0: // This function is called to install a concrete implementation on a bound element using michael@0: // this prototype implementation as a guide. The prototype implementation is compiled lazily, michael@0: // so for the first bound element that needs a concrete implementation, we also build the michael@0: // prototype implementation. michael@0: if (!mMembers && !mFields) // Constructor and destructor also live in mMembers michael@0: return NS_OK; // Nothing to do, so let's not waste time. michael@0: michael@0: // If the way this gets the script context changes, fix michael@0: // nsXBLProtoImplAnonymousMethod::Execute michael@0: nsIDocument* document = aBinding->GetBoundElement()->OwnerDoc(); michael@0: michael@0: nsCOMPtr global = do_QueryInterface(document->GetScopeObject()); michael@0: if (!global) return NS_OK; michael@0: michael@0: nsCOMPtr context = global->GetContext(); michael@0: if (!context) return NS_OK; michael@0: JSContext* cx = context->GetNativeContext(); michael@0: AutoCxPusher pusher(cx); michael@0: michael@0: // InitTarget objects gives us back the JS object that represents the bound element and the michael@0: // class object in the bound document that represents the concrete version of this implementation. michael@0: // This function also has the side effect of building up the prototype implementation if it has michael@0: // not been built already. michael@0: JS::Rooted targetClassObject(cx, nullptr); michael@0: bool targetObjectIsNew = false; michael@0: nsresult rv = InitTargetObjects(aPrototypeBinding, michael@0: aBinding->GetBoundElement(), michael@0: &targetClassObject, michael@0: &targetObjectIsNew); michael@0: NS_ENSURE_SUCCESS(rv, rv); // kick out if we were unable to properly intialize our target objects michael@0: MOZ_ASSERT(targetClassObject); michael@0: michael@0: // If the prototype already existed, we don't need to install anything. return early. michael@0: if (!targetObjectIsNew) michael@0: return NS_OK; michael@0: michael@0: // We want to define the canonical set of members in a safe place. If we're michael@0: // using a separate XBL scope, we want to define them there first (so that michael@0: // they'll be available for Xray lookups, among other things), and then copy michael@0: // the properties to the content-side prototype as needed. We don't need to michael@0: // bother about the field accessors here, since we don't use/support those michael@0: // for in-content bindings. michael@0: michael@0: // First, start by entering the compartment of the XBL scope. This may or may michael@0: // not be the same compartment as globalObject. michael@0: JS::Rooted globalObject(cx, michael@0: GetGlobalForObjectCrossCompartment(targetClassObject)); michael@0: JS::Rooted scopeObject(cx, xpc::GetXBLScopeOrGlobal(cx, globalObject)); michael@0: NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY); michael@0: JSAutoCompartment ac(cx, scopeObject); michael@0: michael@0: // If they're different, create our safe holder object in the XBL scope. michael@0: JS::Rooted propertyHolder(cx); michael@0: if (scopeObject != globalObject) { michael@0: michael@0: // This is just a property holder, so it doesn't need any special JSClass. michael@0: propertyHolder = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), scopeObject); michael@0: NS_ENSURE_TRUE(propertyHolder, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: // Define it as a property on the scopeObject, using the same name used on michael@0: // the content side. michael@0: bool ok = JS_DefineProperty(cx, scopeObject, aPrototypeBinding->ClassName().get(), michael@0: propertyHolder, JSPROP_PERMANENT | JSPROP_READONLY, michael@0: JS_PropertyStub, JS_StrictPropertyStub); michael@0: NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED); michael@0: } else { michael@0: propertyHolder = targetClassObject; michael@0: } michael@0: michael@0: // Walk our member list and install each one in turn on the XBL scope object. michael@0: for (nsXBLProtoImplMember* curr = mMembers; michael@0: curr; michael@0: curr = curr->GetNext()) michael@0: curr->InstallMember(cx, propertyHolder); michael@0: michael@0: // Now, if we're using a separate XBL scope, enter the compartment of the michael@0: // bound node and copy exposable properties to the prototype there. This michael@0: // rewraps them appropriately, which should result in cross-compartment michael@0: // function wrappers. michael@0: if (propertyHolder != targetClassObject) { michael@0: AssertSameCompartment(propertyHolder, scopeObject); michael@0: AssertSameCompartment(targetClassObject, globalObject); michael@0: for (nsXBLProtoImplMember* curr = mMembers; curr; curr = curr->GetNext()) { michael@0: if (curr->ShouldExposeToUntrustedContent()) { michael@0: JS::Rooted id(cx); michael@0: JS::TwoByteChars chars(curr->GetName(), NS_strlen(curr->GetName())); michael@0: bool ok = JS_CharsToId(cx, chars, &id); michael@0: NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED); michael@0: JS_CopyPropertyFrom(cx, id, targetClassObject, propertyHolder); michael@0: NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // From here on out, work in the scope of the bound element. michael@0: JSAutoCompartment ac2(cx, targetClassObject); michael@0: michael@0: // Install all of our field accessors. michael@0: for (nsXBLProtoImplField* curr = mFields; michael@0: curr; michael@0: curr = curr->GetNext()) michael@0: curr->InstallAccessors(cx, targetClassObject); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXBLProtoImpl::InitTargetObjects(nsXBLPrototypeBinding* aBinding, michael@0: nsIContent* aBoundElement, michael@0: JS::MutableHandle aTargetClassObject, michael@0: bool* aTargetIsNew) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: michael@0: if (!mPrecompiledMemberHolder) { michael@0: rv = CompilePrototypeMembers(aBinding); // This is the first time we've ever installed this binding on an element. michael@0: // We need to go ahead and compile all methods and properties on a class michael@0: // in our prototype binding. michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: MOZ_ASSERT(mPrecompiledMemberHolder); michael@0: } michael@0: michael@0: nsIDocument *ownerDoc = aBoundElement->OwnerDoc(); michael@0: nsIGlobalObject *sgo; michael@0: michael@0: if (!(sgo = ownerDoc->GetScopeObject())) { michael@0: return NS_ERROR_UNEXPECTED; michael@0: } michael@0: michael@0: // Because our prototype implementation has a class, we need to build up a corresponding michael@0: // class for the concrete implementation in the bound document. michael@0: AutoJSContext cx; michael@0: JS::Rooted global(cx, sgo->GetGlobalJSObject()); michael@0: JS::Rooted v(cx); michael@0: michael@0: JSAutoCompartment ac(cx, global); michael@0: // Make sure the interface object is created before the prototype object michael@0: // so that XULElement is hidden from content. See bug 909340. michael@0: bool defineOnGlobal = dom::XULElementBinding::ConstructorEnabled(cx, global); michael@0: dom::XULElementBinding::GetConstructorObject(cx, global, defineOnGlobal); michael@0: michael@0: rv = nsContentUtils::WrapNative(cx, aBoundElement, &v, michael@0: /* aAllowWrapping = */ false); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: JS::Rooted value(cx, &v.toObject()); michael@0: JSAutoCompartment ac2(cx, value); michael@0: michael@0: // All of the above code was just obtaining the bound element's script object and its immediate michael@0: // concrete base class. We need to alter the object so that our concrete class is interposed michael@0: // between the object and its base class. We become the new base class of the object, and the michael@0: // object's old base class becomes the new class' base class. michael@0: rv = aBinding->InitClass(mClassName, cx, value, aTargetClassObject, aTargetIsNew); michael@0: if (NS_FAILED(rv)) { michael@0: return rv; michael@0: } michael@0: michael@0: aBoundElement->PreserveWrapper(aBoundElement); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsXBLProtoImpl::CompilePrototypeMembers(nsXBLPrototypeBinding* aBinding) michael@0: { michael@0: // We want to pre-compile our implementation's members against a "prototype context". Then when we actually michael@0: // bind the prototype to a real xbl instance, we'll clone the pre-compiled JS into the real instance's michael@0: // context. michael@0: AutoSafeJSContext cx; michael@0: JS::Rooted compilationGlobal(cx, xpc::GetCompilationScope()); michael@0: NS_ENSURE_TRUE(compilationGlobal, NS_ERROR_UNEXPECTED); michael@0: JSAutoCompartment ac(cx, compilationGlobal); michael@0: michael@0: mPrecompiledMemberHolder = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), compilationGlobal); michael@0: if (!mPrecompiledMemberHolder) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: // Now that we have a class object installed, we walk our member list and compile each of our michael@0: // properties and methods in turn. michael@0: JS::Rooted rootedHolder(cx, mPrecompiledMemberHolder); michael@0: for (nsXBLProtoImplMember* curr = mMembers; michael@0: curr; michael@0: curr = curr->GetNext()) { michael@0: nsresult rv = curr->CompileMember(mClassName, rootedHolder); michael@0: if (NS_FAILED(rv)) { michael@0: DestroyMembers(); michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: nsXBLProtoImpl::LookupMember(JSContext* aCx, nsString& aName, michael@0: JS::Handle aNameAsId, michael@0: JS::MutableHandle aDesc, michael@0: JS::Handle aClassObject) michael@0: { michael@0: for (nsXBLProtoImplMember* m = mMembers; m; m = m->GetNext()) { michael@0: if (aName.Equals(m->GetName())) { michael@0: return JS_GetPropertyDescriptorById(aCx, aClassObject, aNameAsId, aDesc); michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: nsXBLProtoImpl::Trace(const TraceCallbacks& aCallbacks, void *aClosure) michael@0: { michael@0: // If we don't have a class object then we either didn't compile members michael@0: // or we only have fields, in both cases there are no cycles through our michael@0: // members. michael@0: if (!mPrecompiledMemberHolder) { michael@0: return; michael@0: } michael@0: michael@0: nsXBLProtoImplMember *member; michael@0: for (member = mMembers; member; member = member->GetNext()) { michael@0: member->Trace(aCallbacks, aClosure); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsXBLProtoImpl::UnlinkJSObjects() michael@0: { michael@0: if (mPrecompiledMemberHolder) { michael@0: DestroyMembers(); michael@0: } michael@0: } michael@0: michael@0: nsXBLProtoImplField* michael@0: nsXBLProtoImpl::FindField(const nsString& aFieldName) const michael@0: { michael@0: for (nsXBLProtoImplField* f = mFields; f; f = f->GetNext()) { michael@0: if (aFieldName.Equals(f->GetName())) { michael@0: return f; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: bool michael@0: nsXBLProtoImpl::ResolveAllFields(JSContext *cx, JS::Handle obj) const michael@0: { michael@0: for (nsXBLProtoImplField* f = mFields; f; f = f->GetNext()) { michael@0: // Using OBJ_LOOKUP_PROPERTY is a pain, since what we have is a michael@0: // char16_t* for the property name. Let's just use the public API and michael@0: // all. michael@0: nsDependentString name(f->GetName()); michael@0: JS::Rooted dummy(cx); michael@0: if (!::JS_LookupUCProperty(cx, obj, name.get(), name.Length(), &dummy)) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: nsXBLProtoImpl::UndefineFields(JSContext *cx, JS::Handle obj) const michael@0: { michael@0: JSAutoRequest ar(cx); michael@0: for (nsXBLProtoImplField* f = mFields; f; f = f->GetNext()) { michael@0: nsDependentString name(f->GetName()); michael@0: michael@0: const jschar* s = name.get(); michael@0: bool hasProp; michael@0: if (::JS_AlreadyHasOwnUCProperty(cx, obj, s, name.Length(), &hasProp) && michael@0: hasProp) { michael@0: bool dummy; michael@0: ::JS_DeleteUCProperty2(cx, obj, s, name.Length(), &dummy); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsXBLProtoImpl::DestroyMembers() michael@0: { michael@0: MOZ_ASSERT(mPrecompiledMemberHolder); michael@0: michael@0: delete mMembers; michael@0: mMembers = nullptr; michael@0: mConstructor = nullptr; michael@0: mDestructor = nullptr; michael@0: } michael@0: michael@0: nsresult michael@0: nsXBLProtoImpl::Read(nsIObjectInputStream* aStream, michael@0: nsXBLPrototypeBinding* aBinding) michael@0: { michael@0: AssertInCompilationScope(); michael@0: AutoJSContext cx; michael@0: // Set up a class object first so that deserialization is possible michael@0: JS::Rooted global(cx, JS::CurrentGlobalOrNull(cx)); michael@0: mPrecompiledMemberHolder = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), global); michael@0: if (!mPrecompiledMemberHolder) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsXBLProtoImplField* previousField = nullptr; michael@0: nsXBLProtoImplMember* previousMember = nullptr; michael@0: michael@0: do { michael@0: XBLBindingSerializeDetails type; michael@0: nsresult rv = aStream->Read8(&type); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (type == XBLBinding_Serialize_NoMoreItems) michael@0: break; michael@0: michael@0: switch (type & XBLBinding_Serialize_Mask) { michael@0: case XBLBinding_Serialize_Field: michael@0: { michael@0: nsXBLProtoImplField* field = michael@0: new nsXBLProtoImplField(type & XBLBinding_Serialize_ReadOnly); michael@0: rv = field->Read(aStream); michael@0: if (NS_FAILED(rv)) { michael@0: delete field; michael@0: return rv; michael@0: } michael@0: michael@0: if (previousField) { michael@0: previousField->SetNext(field); michael@0: } michael@0: else { michael@0: mFields = field; michael@0: } michael@0: previousField = field; michael@0: michael@0: break; michael@0: } michael@0: case XBLBinding_Serialize_GetterProperty: michael@0: case XBLBinding_Serialize_SetterProperty: michael@0: case XBLBinding_Serialize_GetterSetterProperty: michael@0: { michael@0: nsAutoString name; michael@0: nsresult rv = aStream->ReadString(name); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsXBLProtoImplProperty* prop = michael@0: new nsXBLProtoImplProperty(name.get(), type & XBLBinding_Serialize_ReadOnly); michael@0: rv = prop->Read(aStream, type & XBLBinding_Serialize_Mask); michael@0: if (NS_FAILED(rv)) { michael@0: delete prop; michael@0: return rv; michael@0: } michael@0: michael@0: previousMember = AddMember(prop, previousMember); michael@0: break; michael@0: } michael@0: case XBLBinding_Serialize_Method: michael@0: { michael@0: nsAutoString name; michael@0: rv = aStream->ReadString(name); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsXBLProtoImplMethod* method = new nsXBLProtoImplMethod(name.get()); michael@0: rv = method->Read(aStream); michael@0: if (NS_FAILED(rv)) { michael@0: delete method; michael@0: return rv; michael@0: } michael@0: michael@0: previousMember = AddMember(method, previousMember); michael@0: break; michael@0: } michael@0: case XBLBinding_Serialize_Constructor: michael@0: { michael@0: nsAutoString name; michael@0: rv = aStream->ReadString(name); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mConstructor = new nsXBLProtoImplAnonymousMethod(name.get()); michael@0: rv = mConstructor->Read(aStream); michael@0: if (NS_FAILED(rv)) { michael@0: delete mConstructor; michael@0: mConstructor = nullptr; michael@0: return rv; michael@0: } michael@0: michael@0: previousMember = AddMember(mConstructor, previousMember); michael@0: break; michael@0: } michael@0: case XBLBinding_Serialize_Destructor: michael@0: { michael@0: nsAutoString name; michael@0: rv = aStream->ReadString(name); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mDestructor = new nsXBLProtoImplAnonymousMethod(name.get()); michael@0: rv = mDestructor->Read(aStream); michael@0: if (NS_FAILED(rv)) { michael@0: delete mDestructor; michael@0: mDestructor = nullptr; michael@0: return rv; michael@0: } michael@0: michael@0: previousMember = AddMember(mDestructor, previousMember); michael@0: break; michael@0: } michael@0: default: michael@0: NS_ERROR("Unexpected binding member type"); michael@0: break; michael@0: } michael@0: } while (1); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsXBLProtoImpl::Write(nsIObjectOutputStream* aStream, michael@0: nsXBLPrototypeBinding* aBinding) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (!mPrecompiledMemberHolder) { michael@0: rv = CompilePrototypeMembers(aBinding); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: rv = aStream->WriteStringZ(mClassName.get()); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: for (nsXBLProtoImplField* curr = mFields; curr; curr = curr->GetNext()) { michael@0: rv = curr->Write(aStream); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: for (nsXBLProtoImplMember* curr = mMembers; curr; curr = curr->GetNext()) { michael@0: if (curr == mConstructor) { michael@0: rv = mConstructor->Write(aStream, XBLBinding_Serialize_Constructor); michael@0: } michael@0: else if (curr == mDestructor) { michael@0: rv = mDestructor->Write(aStream, XBLBinding_Serialize_Destructor); michael@0: } michael@0: else { michael@0: rv = curr->Write(aStream); michael@0: } michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return aStream->Write8(XBLBinding_Serialize_NoMoreItems); michael@0: } michael@0: michael@0: nsresult michael@0: NS_NewXBLProtoImpl(nsXBLPrototypeBinding* aBinding, michael@0: const char16_t* aClassName, michael@0: nsXBLProtoImpl** aResult) michael@0: { michael@0: nsXBLProtoImpl* impl = new nsXBLProtoImpl(); michael@0: if (!impl) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: if (aClassName) michael@0: impl->mClassName.AssignWithConversion(aClassName); michael@0: else michael@0: aBinding->BindingURI()->GetSpec(impl->mClassName); michael@0: aBinding->SetImplementation(impl); michael@0: *aResult = impl; michael@0: michael@0: return NS_OK; michael@0: } michael@0: