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