2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* David Hyatt <hyatt@netscape.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsXBLProtoImpl.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#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"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-01-27 02:54:58 -08:00
|
|
|
// Checks that the version is not modified in a given scope.
|
|
|
|
class AutoVersionChecker
|
|
|
|
{
|
|
|
|
JSContext * const cx;
|
|
|
|
JSVersion versionBefore;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoVersionChecker(JSContext *cx) : cx(cx) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
versionBefore = JS_GetVersion(cx);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoVersionChecker() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
JSVersion versionAfter = JS_GetVersion(cx);
|
|
|
|
NS_ABORT_IF_FALSE(versionAfter == versionBefore, "version must not change");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsXBLProtoImpl::InstallImplementation(nsXBLPrototypeBinding* aBinding, nsIContent* aBoundElement)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
nsIDocument* document = aBoundElement->GetOwnerDoc();
|
|
|
|
if (!document) return NS_OK;
|
|
|
|
|
2008-11-06 18:06:49 -08:00
|
|
|
nsIScriptGlobalObject *global = 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;
|
|
|
|
void * targetClassObject = nsnull;
|
|
|
|
nsresult rv = InitTargetObjects(aBinding, context, aBoundElement,
|
|
|
|
getter_AddRefs(holder), &targetClassObject);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); // kick out if we were unable to properly intialize our target objects
|
|
|
|
|
|
|
|
JSObject * targetScriptObject;
|
|
|
|
holder->GetJSObject(&targetScriptObject);
|
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
JSContext *cx = context->GetNativeContext();
|
2011-01-27 02:54:58 -08:00
|
|
|
|
|
|
|
AutoVersionChecker avc(cx);
|
2008-02-15 21:13:16 -08:00
|
|
|
|
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())
|
|
|
|
curr->InstallMember(context, aBoundElement, targetScriptObject,
|
|
|
|
targetClassObject, mClassName);
|
2008-02-15 21:13:16 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImpl::InitTargetObjects(nsXBLPrototypeBinding* aBinding,
|
|
|
|
nsIScriptContext* aContext,
|
|
|
|
nsIContent* aBoundElement,
|
|
|
|
nsIXPConnectJSObjectHolder** aScriptObjectHolder,
|
|
|
|
void** aTargetClassObject)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
*aScriptObjectHolder = nsnull;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!mClassObject)
|
|
|
|
return NS_OK; // This can be ok, if all we've got are fields (and no methods/properties).
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument *ownerDoc = aBoundElement->GetOwnerDoc();
|
|
|
|
nsIScriptGlobalObject *sgo;
|
|
|
|
|
2008-11-06 18:06:49 -08:00
|
|
|
if (!ownerDoc || !(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.
|
2011-09-18 02:22:17 -07:00
|
|
|
JSContext* jscontext = aContext->GetNativeContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject* global = sgo->GetGlobalJSObject();
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
2009-08-14 12:00:24 -07:00
|
|
|
jsval v;
|
|
|
|
rv = nsContentUtils::WrapNative(jscontext, global, aBoundElement, &v,
|
|
|
|
getter_AddRefs(wrapper));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// 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.
|
2009-08-14 12:00:24 -07:00
|
|
|
rv = aBinding->InitClass(mClassName, jscontext, global, JSVAL_TO_OBJECT(v),
|
2007-03-22 10:30:00 -07:00
|
|
|
aTargetClassObject);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-05-12 13:20:42 -07:00
|
|
|
nsContentUtils::PreserveWrapper(aBoundElement, 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()));
|
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
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
JSContext *cx = context->GetNativeContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *global = globalObject->GetGlobalJSObject();
|
2008-02-15 21:13:16 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void* classObject;
|
2008-02-15 21:13:16 -08:00
|
|
|
nsresult rv = aBinding->InitClass(mClassName, cx, global, global,
|
2007-03-22 10:30:00 -07:00
|
|
|
&classObject);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mClassObject = (JSObject*) classObject;
|
|
|
|
if (!mClassObject)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-01-27 02:54:58 -08:00
|
|
|
AutoVersionChecker avc(cx);
|
2008-02-15 21:13:16 -08:00
|
|
|
|
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()) {
|
|
|
|
nsresult rv = curr->CompileMember(context, mClassName, mClassObject);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-05-24 07:10:02 -07:00
|
|
|
void
|
2007-10-29 06:45:07 -07:00
|
|
|
nsXBLProtoImpl::Trace(TraceCallback aCallback, void *aClosure) const
|
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()) {
|
2007-10-29 06:45:07 -07:00
|
|
|
member->Trace(aCallback, 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-09-28 06:45:01 -07:00
|
|
|
nsXBLProtoImpl::ResolveAllFields(JSContext *cx, JSObject *obj) const
|
|
|
|
{
|
2011-01-27 02:54:58 -08:00
|
|
|
AutoVersionChecker avc(cx);
|
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());
|
|
|
|
jsval dummy;
|
|
|
|
if (!::JS_LookupUCProperty(cx, obj,
|
|
|
|
reinterpret_cast<const jschar*>(name.get()),
|
|
|
|
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
|
|
|
|
nsXBLProtoImpl::UndefineFields(JSContext *cx, JSObject *obj) const
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
jsval dummy;
|
|
|
|
::JS_DeleteUCProperty2(cx, obj, s, name.Length(), &dummy);
|
|
|
|
}
|
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;
|
|
|
|
mMembers = nsnull;
|
|
|
|
mConstructor = nsnull;
|
|
|
|
mDestructor = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|