2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsXBLProtoImpl.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptGlobalObjectOwner.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIDOMNode.h"
|
2007-09-28 06:45:01 -07:00
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2011-11-03 13:39:08 -07:00
|
|
|
#include "nsXBLProtoImplProperty.h"
|
2012-10-25 18:48:22 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
2013-02-08 06:24:20 -08:00
|
|
|
nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aPrototypeBinding,
|
|
|
|
nsXBLBinding* aBinding)
|
2007-03-22 10:30:00 -07: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 06:45:01 -07:00
|
|
|
if (!mMembers && !mFields) // Constructor and destructor also live in mMembers
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK; // Nothing to do, so let's not waste time.
|
|
|
|
|
|
|
|
// If the way this gets the script context changes, fix
|
|
|
|
// nsXBLProtoImplAnonymousMethod::Execute
|
2013-02-08 06:24:20 -08:00
|
|
|
nsIDocument* document = aBinding->GetBoundElement()->OwnerDoc();
|
2013-04-05 06:21:02 -07:00
|
|
|
|
2013-04-04 02:27:41 -07:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global = do_QueryInterface(document->GetScopeObject());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!global) return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> context = global->GetContext();
|
|
|
|
if (!context) return NS_OK;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2013-06-20 11:05:34 -07:00
|
|
|
JSAutoRequest ar(context->GetNativeContext());
|
2013-04-05 06:21:02 -07:00
|
|
|
JS::Rooted<JSObject*> targetClassObject(context->GetNativeContext(), nullptr);
|
2013-02-08 06:24:20 -08:00
|
|
|
bool targetObjectIsNew = false;
|
2013-02-08 06:24:20 -08:00
|
|
|
nsresult rv = InitTargetObjects(aPrototypeBinding, context,
|
|
|
|
aBinding->GetBoundElement(),
|
2013-02-08 06:24:20 -08:00
|
|
|
getter_AddRefs(holder), &targetClassObject,
|
|
|
|
&targetObjectIsNew);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv); // kick out if we were unable to properly intialize our target objects
|
2013-02-08 06:24:20 -08:00
|
|
|
MOZ_ASSERT(targetClassObject);
|
|
|
|
|
2013-02-08 06:24:20 -08:00
|
|
|
// Stash a strong reference to the JSClass in the binding.
|
|
|
|
aBinding->SetJSClass(static_cast<nsXBLJSClass*>(JS_GetClass(targetClassObject)));
|
|
|
|
|
2013-02-08 06:24:20 -08:00
|
|
|
// If the prototype already existed, we don't need to install anything. return early.
|
|
|
|
if (!targetObjectIsNew)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-20 05:44:18 -07:00
|
|
|
JS::Rooted<JSObject*> targetScriptObject(context->GetNativeContext(),
|
|
|
|
holder->GetJSObject());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(context->GetNativeContext());
|
2013-02-08 06:24:21 -08:00
|
|
|
JSAutoCompartment ac(cx, targetClassObject);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Walk our member list and install each one in turn.
|
|
|
|
for (nsXBLProtoImplMember* curr = mMembers;
|
|
|
|
curr;
|
|
|
|
curr = curr->GetNext())
|
2013-02-08 06:24:21 -08:00
|
|
|
curr->InstallMember(cx, targetClassObject);
|
2008-02-15 21:13:16 -08:00
|
|
|
|
2013-02-13 10:16:19 -08:00
|
|
|
// If we're using a separate XBL scope, make a safe copy of the target class
|
|
|
|
// object in the XBL scope that we can use for Xray lookups. We don't need
|
|
|
|
// the field accessors, so do this before installing them.
|
2013-05-02 02:12:45 -07:00
|
|
|
JS::Rooted<JSObject*> globalObject(cx,
|
|
|
|
JS_GetGlobalForObject(cx, targetClassObject));
|
|
|
|
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetXBLScope(cx, globalObject));
|
2013-04-05 12:04:09 -07:00
|
|
|
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
|
2013-02-13 10:16:19 -08:00
|
|
|
if (scopeObject != globalObject) {
|
|
|
|
JSAutoCompartment ac2(cx, scopeObject);
|
|
|
|
|
|
|
|
// Create the object. This is just a property holder, so it doesn't need
|
|
|
|
// any special JSClass.
|
2013-05-02 02:12:45 -07:00
|
|
|
JS::Rooted<JSObject*> shadowProto(cx,
|
|
|
|
JS_NewObjectWithGivenProto(cx, nullptr, nullptr, scopeObject));
|
2013-02-13 10:16:19 -08:00
|
|
|
NS_ENSURE_TRUE(shadowProto, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
JS::ObjectValue(*shadowProto), JS_PropertyStub,
|
|
|
|
JS_StrictPropertyStub,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// Copy all the properties from the content-visible prototype to the shadow
|
|
|
|
// object. This rewraps them appropriately, which should result in vanilla
|
|
|
|
// functions, since the properties on the content prototype were cross-
|
|
|
|
// compartment wrappers.
|
|
|
|
ok = JS_CopyPropertiesFrom(cx, shadowProto, targetClassObject);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// Content shouldn't have any way to touch this object, but freeze it just
|
|
|
|
// to be safe.
|
|
|
|
ok = JS_FreezeObject(cx, shadowProto);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
// Install all of our field accessors.
|
|
|
|
for (nsXBLProtoImplField* curr = mFields;
|
|
|
|
curr;
|
|
|
|
curr = curr->GetNext())
|
|
|
|
curr->InstallAccessors(cx, targetClassObject);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImpl::InitTargetObjects(nsXBLPrototypeBinding* aBinding,
|
|
|
|
nsIScriptContext* aContext,
|
|
|
|
nsIContent* aBoundElement,
|
|
|
|
nsIXPConnectJSObjectHolder** aScriptObjectHolder,
|
2013-04-05 06:21:03 -07:00
|
|
|
JS::MutableHandle<JSObject*> aTargetClassObject,
|
2013-02-08 06:24:20 -08:00
|
|
|
bool* aTargetIsNew)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aScriptObjectHolder = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mClassObject) {
|
|
|
|
rv = CompilePrototypeMembers(aBinding); // This is the first time we've ever installed this binding on an element.
|
|
|
|
// We need to go ahead and compile all methods and properties on a class
|
|
|
|
// in our prototype binding.
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2013-02-08 06:24:20 -08:00
|
|
|
MOZ_ASSERT(mClassObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument *ownerDoc = aBoundElement->OwnerDoc();
|
2013-04-04 02:27:41 -07:00
|
|
|
nsIGlobalObject *sgo;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-18 04:19:44 -07:00
|
|
|
if (!(sgo = ownerDoc->GetScopeObject())) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because our prototype implementation has a class, we need to build up a corresponding
|
|
|
|
// class for the concrete implementation in the bound document.
|
2013-04-05 06:21:01 -07:00
|
|
|
AutoPushJSContext cx(aContext->GetNativeContext());
|
|
|
|
JS::Rooted<JSObject*> global(cx, sgo->GetGlobalJSObject());
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
2013-05-13 10:43:53 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
|
|
|
rv = nsContentUtils::WrapNative(cx, global, aBoundElement, v.address(),
|
2009-08-14 12:00:24 -07:00
|
|
|
getter_AddRefs(wrapper));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-04-05 06:21:01 -07:00
|
|
|
JS::Rooted<JSObject*> value(cx, &v.toObject());
|
|
|
|
|
2007-03-22 10:30:00 -07: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 06:21:03 -07:00
|
|
|
rv = aBinding->InitClass(mClassName, cx, global, value, aTargetClassObject, aTargetIsNew);
|
2011-11-15 23:50:20 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
2011-11-15 23:50:20 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-23 00:15:42 -07:00
|
|
|
aBoundElement->PreserveWrapper(aBoundElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
wrapper.swap(*aScriptObjectHolder);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImpl::CompilePrototypeMembers(nsXBLPrototypeBinding* aBinding)
|
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
nsCOMPtr<nsIScriptGlobalObjectOwner> globalOwner(
|
2010-07-14 18:53:11 -07:00
|
|
|
do_QueryObject(aBinding->XBLDocumentInfo()));
|
2013-04-05 06:21:01 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScriptGlobalObject* globalObject = globalOwner->GetScriptGlobalObject();
|
|
|
|
NS_ENSURE_TRUE(globalObject, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsIScriptContext *context = globalObject->GetContext();
|
|
|
|
NS_ENSURE_TRUE(context, NS_ERROR_OUT_OF_MEMORY);
|
2008-02-15 21:13:16 -08:00
|
|
|
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(context->GetNativeContext());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-05 06:21:01 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, globalObject->GetGlobalJSObject());
|
|
|
|
JS::Rooted<JSObject*> classObject(cx);
|
2013-02-08 06:24:20 -08:00
|
|
|
bool classObjectIsNew = false;
|
2008-02-15 21:13:16 -08:00
|
|
|
nsresult rv = aBinding->InitClass(mClassName, cx, global, global,
|
2013-02-08 06:24:20 -08:00
|
|
|
&classObject, &classObjectIsNew);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2013-02-08 06:24:20 -08:00
|
|
|
MOZ_ASSERT(classObjectIsNew);
|
|
|
|
MOZ_ASSERT(classObject);
|
2011-11-15 23:50:20 -08:00
|
|
|
mClassObject = classObject;
|
2007-03-22 10:30:00 -07: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-04-05 06:21:02 -07:00
|
|
|
nsresult rv = curr->CompileMember(context, mClassName, classObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2008-02-12 08:02:41 -08:00
|
|
|
DestroyMembers();
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2008-02-15 21:13:16 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-08 06:24:21 -08:00
|
|
|
bool
|
|
|
|
nsXBLProtoImpl::LookupMember(JSContext* aCx, nsString& aName,
|
|
|
|
JS::HandleId aNameAsId,
|
|
|
|
JSPropertyDescriptor* aDesc,
|
|
|
|
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 07:10:02 -07:00
|
|
|
void
|
2013-05-27 04:50:49 -07:00
|
|
|
nsXBLProtoImpl::Trace(const TraceCallbacks& aCallbacks, void *aClosure)
|
2007-05-24 07:10:02 -07:00
|
|
|
{
|
2007-05-24 11:39:49 -07: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 07:10:02 -07:00
|
|
|
nsXBLProtoImplMember *member;
|
|
|
|
for (member = mMembers; member; member = member->GetNext()) {
|
2013-05-27 04:50:49 -07:00
|
|
|
member->Trace(aCallbacks, aClosure);
|
2007-05-24 07:10:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 06:45:01 -07:00
|
|
|
void
|
2008-01-29 18:05:43 -08:00
|
|
|
nsXBLProtoImpl::UnlinkJSObjects()
|
2007-09-28 06:45:01 -07:00
|
|
|
{
|
|
|
|
if (mClassObject) {
|
2008-02-12 08:02:41 -08:00
|
|
|
DestroyMembers();
|
2007-09-28 06:45:01 -07: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 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-09-28 06:45:01 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-04-05 06:21:03 -07:00
|
|
|
nsXBLProtoImpl::ResolveAllFields(JSContext *cx, JS::Handle<JSObject*> obj) const
|
2007-09-28 06:45:01 -07:00
|
|
|
{
|
|
|
|
for (nsXBLProtoImplField* f = mFields; f; f = f->GetNext()) {
|
|
|
|
// Using OBJ_LOOKUP_PROPERTY is a pain, since what we have is a
|
|
|
|
// PRUnichar* for the property name. Let's just use the public API and
|
|
|
|
// all.
|
|
|
|
nsDependentString name(f->GetName());
|
2013-04-05 06:21:03 -07:00
|
|
|
JS::Rooted<JS::Value> dummy(cx);
|
2007-09-28 06:45:01 -07:00
|
|
|
if (!::JS_LookupUCProperty(cx, obj,
|
|
|
|
reinterpret_cast<const jschar*>(name.get()),
|
2013-08-02 05:15:39 -07:00
|
|
|
name.Length(), &dummy)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-09-28 06:45:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-09-28 06:45:01 -07:00
|
|
|
}
|
|
|
|
|
2007-10-19 21:22:43 -07:00
|
|
|
void
|
2013-04-05 06:21:03 -07:00
|
|
|
nsXBLProtoImpl::UndefineFields(JSContext *cx, JS::Handle<JSObject*> obj) const
|
2007-10-19 21:22:43 -07:00
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
for (nsXBLProtoImplField* f = mFields; f; f = f->GetNext()) {
|
|
|
|
nsDependentString name(f->GetName());
|
2007-11-15 09:09:14 -08:00
|
|
|
|
|
|
|
const jschar* s = reinterpret_cast<const jschar*>(name.get());
|
|
|
|
JSBool hasProp;
|
|
|
|
if (::JS_AlreadyHasOwnUCProperty(cx, obj, s, name.Length(), &hasProp) &&
|
|
|
|
hasProp) {
|
2013-08-05 06:01:53 -07:00
|
|
|
bool dummy;
|
2013-08-02 05:15:39 -07:00
|
|
|
::JS_DeleteUCProperty2(cx, obj, s, name.Length(), &dummy);
|
2007-11-15 09:09:14 -08:00
|
|
|
}
|
2007-10-19 21:22:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2008-02-12 08:02:41 -08:00
|
|
|
nsXBLProtoImpl::DestroyMembers()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mClassObject, "This should never be called when there is no class object");
|
|
|
|
|
|
|
|
delete mMembers;
|
2012-07-30 07:20:58 -07:00
|
|
|
mMembers = nullptr;
|
|
|
|
mConstructor = nullptr;
|
|
|
|
mDestructor = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-03 13:39:08 -07:00
|
|
|
nsresult
|
|
|
|
nsXBLProtoImpl::Read(nsIScriptContext* aContext,
|
|
|
|
nsIObjectInputStream* aStream,
|
|
|
|
nsXBLPrototypeBinding* aBinding,
|
|
|
|
nsIScriptGlobalObject* aGlobal)
|
|
|
|
{
|
|
|
|
// Set up a class object first so that deserialization is possible
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(aContext->GetNativeContext());
|
2013-04-05 06:21:01 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, aGlobal->GetGlobalJSObject());
|
2011-11-03 13:39:08 -07:00
|
|
|
|
2013-04-05 06:21:01 -07:00
|
|
|
JS::Rooted<JSObject*> classObject(cx);
|
2013-02-08 06:24:20 -08:00
|
|
|
bool classObjectIsNew = false;
|
|
|
|
nsresult rv = aBinding->InitClass(mClassName, cx, global, global, &classObject,
|
|
|
|
&classObjectIsNew);
|
2011-11-03 13:39:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-02-08 06:24:20 -08:00
|
|
|
MOZ_ASSERT(classObject);
|
|
|
|
MOZ_ASSERT(classObjectIsNew);
|
2011-11-03 13:39:08 -07:00
|
|
|
|
2011-11-15 23:50:20 -08:00
|
|
|
mClassObject = classObject;
|
2011-11-03 13:39:08 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsXBLProtoImplField* previousField = nullptr;
|
|
|
|
nsXBLProtoImplMember* previousMember = nullptr;
|
2011-11-03 13:39:08 -07: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);
|
|
|
|
rv = field->Read(aContext, aStream);
|
|
|
|
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);
|
|
|
|
rv = prop->Read(aContext, aStream, type & XBLBinding_Serialize_Mask);
|
|
|
|
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());
|
|
|
|
rv = method->Read(aContext, aStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete method;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousMember = AddMember(method, previousMember);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XBLBinding_Serialize_Constructor:
|
|
|
|
{
|
|
|
|
mConstructor = new nsXBLProtoImplAnonymousMethod();
|
|
|
|
rv = mConstructor->Read(aContext, aStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete mConstructor;
|
2012-07-30 07:20:58 -07:00
|
|
|
mConstructor = nullptr;
|
2011-11-03 13:39:08 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousMember = AddMember(mConstructor, previousMember);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XBLBinding_Serialize_Destructor:
|
|
|
|
{
|
|
|
|
mDestructor = new nsXBLProtoImplAnonymousMethod();
|
|
|
|
rv = mDestructor->Read(aContext, aStream);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete mDestructor;
|
2012-07-30 07:20:58 -07:00
|
|
|
mDestructor = nullptr;
|
2011-11-03 13:39:08 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousMember = AddMember(mDestructor, previousMember);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unexpected binding member type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImpl::Write(nsIScriptContext* aContext,
|
|
|
|
nsIObjectOutputStream* aStream,
|
|
|
|
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()) {
|
|
|
|
rv = curr->Write(aContext, aStream);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
for (nsXBLProtoImplMember* curr = mMembers; curr; curr = curr->GetNext()) {
|
|
|
|
if (curr == mConstructor) {
|
|
|
|
rv = mConstructor->Write(aContext, aStream, XBLBinding_Serialize_Constructor);
|
|
|
|
}
|
|
|
|
else if (curr == mDestructor) {
|
|
|
|
rv = mDestructor->Write(aContext, aStream, XBLBinding_Serialize_Destructor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = curr->Write(aContext, aStream);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aStream->Write8(XBLBinding_Serialize_NoMoreItems);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
NS_NewXBLProtoImpl(nsXBLPrototypeBinding* aBinding,
|
|
|
|
const PRUnichar* aClassName,
|
|
|
|
nsXBLProtoImpl** aResult)
|
|
|
|
{
|
|
|
|
nsXBLProtoImpl* impl = new nsXBLProtoImpl();
|
|
|
|
if (!impl)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
if (aClassName)
|
|
|
|
impl->mClassName.AssignWithConversion(aClassName);
|
|
|
|
else
|
|
|
|
aBinding->BindingURI()->GetSpec(impl->mClassName);
|
|
|
|
aBinding->SetImplementation(impl);
|
|
|
|
*aResult = impl;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|