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"
|
|
|
|
#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"
|
2013-08-23 19:42:40 -07:00
|
|
|
#include "nsIURI.h"
|
2014-06-23 16:33:37 -07:00
|
|
|
#include "mozilla/AddonPathService.h"
|
2014-08-06 08:30:25 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-08-28 15:48:42 -07:00
|
|
|
#include "mozilla/dom/XULElementBinding.h"
|
2013-08-23 19:42:40 -07:00
|
|
|
#include "xpcpublic.h"
|
2013-11-01 07:31:58 -07:00
|
|
|
#include "js/CharacterEncoding.h"
|
2012-10-25 18:48:22 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-11-01 07:31:57 -07:00
|
|
|
using js::GetGlobalForObjectCrossCompartment;
|
|
|
|
using js::AssertSameCompartment;
|
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
|
|
|
|
2014-08-06 08:30:25 -07:00
|
|
|
// This sometimes gets called when we have no outer window and if we don't
|
|
|
|
// catch this, we get leaks during crashtests and reftests.
|
|
|
|
if (NS_WARN_IF(!document->GetWindow())) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-08-06 08:30:25 -07:00
|
|
|
// |propertyHolder| (below) can be an existing object, so in theory we might
|
|
|
|
// hit something that could end up running script. We never want that to
|
|
|
|
// happen here, so we use an AutoJSAPI instead of an AutoEntryScript.
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(document->GetScopeObject()))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2007-03-22 10:30:00 -07: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 09:25:13 -07:00
|
|
|
JS::Rooted<JSObject*> targetClassObject(cx, nullptr);
|
2013-02-08 06:24:20 -08:00
|
|
|
bool targetObjectIsNew = false;
|
2013-08-09 09:25:13 -07:00
|
|
|
nsresult rv = InitTargetObjects(aPrototypeBinding,
|
2013-02-08 06:24:20 -08:00
|
|
|
aBinding->GetBoundElement(),
|
2014-01-06 11:54:42 -08:00
|
|
|
&targetClassObject,
|
2013-02-08 06:24:20 -08:00
|
|
|
&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);
|
|
|
|
|
|
|
|
// 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-11-01 07:31:57 -07: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 06:24:21 -08:00
|
|
|
|
2013-11-01 07:31:57 -07:00
|
|
|
// First, start by entering the compartment of the XBL scope. This may or may
|
|
|
|
// not be the same compartment as globalObject.
|
2014-06-23 16:33:37 -07:00
|
|
|
JSAddonId* addonId = MapURIToAddonID(aPrototypeBinding->BindingURI());
|
2013-05-02 02:12:45 -07:00
|
|
|
JS::Rooted<JSObject*> globalObject(cx,
|
2013-11-01 07:31:57 -07:00
|
|
|
GetGlobalForObjectCrossCompartment(targetClassObject));
|
2014-06-23 16:33:37 -07:00
|
|
|
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetScopeForXBLExecution(cx, globalObject, addonId));
|
2013-04-05 12:04:09 -07:00
|
|
|
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
|
2013-11-01 07:31:57 -07:00
|
|
|
JSAutoCompartment ac(cx, scopeObject);
|
|
|
|
|
2014-05-22 18:44:03 -07:00
|
|
|
// Determine the appropriate property holder.
|
|
|
|
//
|
|
|
|
// Note: If |targetIsNew| is false, we'll early-return above. However, that only
|
|
|
|
// tells us if the content-side object is new, which may be the case even if
|
|
|
|
// we've already set up the binding on the XBL side. For example, if we apply
|
|
|
|
// a binding #foo to a <span> when we've already applied it to a <div>, we'll
|
|
|
|
// end up with a different content prototype, but we'll already have a property
|
|
|
|
// holder called |foo| in the XBL scope. Check for that to avoid wasteful and
|
|
|
|
// weird property holder duplication.
|
|
|
|
const char* className = aPrototypeBinding->ClassName().get();
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(cx);
|
2014-05-22 18:44:03 -07:00
|
|
|
JS::Rooted<JSPropertyDescriptor> existingHolder(cx);
|
|
|
|
if (scopeObject != globalObject &&
|
|
|
|
!JS_GetOwnPropertyDescriptor(cx, scopeObject, className, &existingHolder)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
bool propertyHolderIsNew = !existingHolder.object() || !existingHolder.value().isObject();
|
|
|
|
|
|
|
|
if (!propertyHolderIsNew) {
|
|
|
|
propertyHolder = &existingHolder.value().toObject();
|
|
|
|
} else if (scopeObject != globalObject) {
|
2013-02-13 10:16:19 -08:00
|
|
|
|
2013-11-01 07:31:57 -07:00
|
|
|
// This is just a property holder, so it doesn't need any special JSClass.
|
2014-01-16 09:48:58 -08:00
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), scopeObject);
|
2013-11-01 07:31:57 -07:00
|
|
|
NS_ENSURE_TRUE(propertyHolder, NS_ERROR_OUT_OF_MEMORY);
|
2013-02-13 10:16:19 -08:00
|
|
|
|
|
|
|
// Define it as a property on the scopeObject, using the same name used on
|
|
|
|
// the content side.
|
2014-05-22 18:44:03 -07:00
|
|
|
bool ok = JS_DefineProperty(cx, scopeObject, className, propertyHolder,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY,
|
2014-01-21 18:11:17 -08:00
|
|
|
JS_PropertyStub, JS_StrictPropertyStub);
|
2013-02-13 10:16:19 -08:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED);
|
2013-11-01 07:31:57 -07:00
|
|
|
} else {
|
|
|
|
propertyHolder = targetClassObject;
|
|
|
|
}
|
2013-02-13 10:16:19 -08:00
|
|
|
|
2013-11-01 07:31:57 -07:00
|
|
|
// Walk our member list and install each one in turn on the XBL scope object.
|
2014-05-22 18:44:03 -07:00
|
|
|
if (propertyHolderIsNew) {
|
|
|
|
for (nsXBLProtoImplMember* curr = mMembers;
|
|
|
|
curr;
|
|
|
|
curr = curr->GetNext())
|
|
|
|
curr->InstallMember(cx, propertyHolder);
|
|
|
|
}
|
2013-11-01 07:31:57 -07:00
|
|
|
|
|
|
|
// Now, if we're using a separate XBL scope, enter the compartment of the
|
2013-11-01 07:31:58 -07: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 07:31:57 -07:00
|
|
|
if (propertyHolder != targetClassObject) {
|
|
|
|
AssertSameCompartment(propertyHolder, scopeObject);
|
|
|
|
AssertSameCompartment(targetClassObject, globalObject);
|
2014-06-23 16:33:37 -07:00
|
|
|
bool inContentXBLScope = xpc::IsInContentXBLScope(scopeObject);
|
2013-11-01 07:31:58 -07:00
|
|
|
for (nsXBLProtoImplMember* curr = mMembers; curr; curr = curr->GetNext()) {
|
2014-06-23 16:33:37 -07:00
|
|
|
if (!inContentXBLScope || curr->ShouldExposeToUntrustedContent()) {
|
2013-11-01 07:31:58 -07:00
|
|
|
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);
|
2014-06-23 16:33:37 -07:00
|
|
|
|
|
|
|
bool found;
|
|
|
|
ok = JS_HasPropertyById(cx, propertyHolder, id, &found);
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED);
|
|
|
|
if (!found) {
|
|
|
|
// Some members don't install anything in InstallMember (e.g.,
|
|
|
|
// nsXBLProtoImplAnonymousMethod). We need to skip copying in
|
|
|
|
// those cases.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = JS_CopyPropertyFrom(cx, id, targetClassObject, propertyHolder);
|
2013-11-01 07:31:58 -07:00
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
}
|
2013-02-13 10:16:19 -08:00
|
|
|
}
|
|
|
|
|
2013-11-25 15:35:22 -08:00
|
|
|
// From here on out, work in the scope of the bound element.
|
|
|
|
JSAutoCompartment ac2(cx, targetClassObject);
|
|
|
|
|
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,
|
|
|
|
nsIContent* aBoundElement,
|
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;
|
2014-01-06 11:54:42 -08:00
|
|
|
|
2014-04-11 07:59:47 -07:00
|
|
|
if (!mPrecompiledMemberHolder) {
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
|
2014-04-11 07:59:47 -07:00
|
|
|
MOZ_ASSERT(mPrecompiledMemberHolder);
|
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-08-09 09:25:13 -07:00
|
|
|
AutoJSContext cx;
|
2013-04-05 06:21:01 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, sgo->GetGlobalJSObject());
|
2013-05-13 10:43:53 -07:00
|
|
|
JS::Rooted<JS::Value> v(cx);
|
2013-08-28 15:48:42 -07:00
|
|
|
|
2014-01-24 15:01:48 -08: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-28 15:48:42 -07:00
|
|
|
|
2014-04-09 21:58:41 -07:00
|
|
|
rv = nsContentUtils::WrapNative(cx, aBoundElement, &v,
|
2014-02-12 15:19:32 -08:00
|
|
|
/* aAllowWrapping = */ false);
|
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());
|
2014-04-11 07:59:47 -07:00
|
|
|
JSAutoCompartment ac2(cx, value);
|
2013-04-05 06:21:01 -07:00
|
|
|
|
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.
|
2014-03-19 09:35:45 -07:00
|
|
|
rv = aBinding->InitClass(mClassName, cx, 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
|
|
|
|
|
|
|
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.
|
2013-08-09 09:25:14 -07:00
|
|
|
AutoSafeJSContext cx;
|
2014-08-18 10:57:30 -07:00
|
|
|
JS::Rooted<JSObject*> compilationGlobal(cx, xpc::CompilationScope());
|
2013-08-09 09:25:14 -07:00
|
|
|
JSAutoCompartment ac(cx, compilationGlobal);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-11 07:59:47 -07:00
|
|
|
mPrecompiledMemberHolder = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), compilationGlobal);
|
|
|
|
if (!mPrecompiledMemberHolder)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
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.
|
2014-04-11 07:59:47 -07:00
|
|
|
JS::Rooted<JSObject*> rootedHolder(cx, mPrecompiledMemberHolder);
|
2007-03-22 10:30:00 -07:00
|
|
|
for (nsXBLProtoImplMember* curr = mMembers;
|
|
|
|
curr;
|
|
|
|
curr = curr->GetNext()) {
|
2014-04-11 07:59:47 -07:00
|
|
|
nsresult rv = curr->CompileMember(mClassName, rootedHolder);
|
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,
|
2013-11-11 00:04:41 -08:00
|
|
|
JS::Handle<jsid> aNameAsId,
|
2013-08-12 04:09:14 -07:00
|
|
|
JS::MutableHandle<JSPropertyDescriptor> aDesc,
|
2014-03-17 09:17:58 -07:00
|
|
|
JS::Handle<JSObject*> aClassObject)
|
2013-02-08 06:24:21 -08:00
|
|
|
{
|
|
|
|
for (nsXBLProtoImplMember* m = mMembers; m; m = m->GetNext()) {
|
|
|
|
if (aName.Equals(m->GetName())) {
|
2014-04-25 14:11:01 -07:00
|
|
|
return JS_GetPropertyDescriptorById(aCx, aClassObject, aNameAsId, aDesc);
|
2013-02-08 06:24:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
2014-04-11 07:59:47 -07:00
|
|
|
if (!mPrecompiledMemberHolder) {
|
2007-05-24 11:39:49 -07:00
|
|
|
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
|
|
|
{
|
2014-04-11 07:59:47 -07:00
|
|
|
if (mPrecompiledMemberHolder) {
|
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
|
2014-01-04 07:02:17 -08:00
|
|
|
// char16_t* for the property name. Let's just use the public API and
|
2007-09-28 06:45:01 -07:00
|
|
|
// all.
|
|
|
|
nsDependentString name(f->GetName());
|
2013-04-05 06:21:03 -07:00
|
|
|
JS::Rooted<JS::Value> dummy(cx);
|
2013-12-03 07:09:50 -08:00
|
|
|
if (!::JS_LookupUCProperty(cx, obj, 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
|
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
|
|
|
|
2013-12-03 07:09:50 -08:00
|
|
|
const jschar* s = name.get();
|
2013-08-08 15:53:04 -07:00
|
|
|
bool hasProp;
|
2007-11-15 09:09:14 -08:00
|
|
|
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
|
|
|
{
|
2014-04-11 07:59:47 -07:00
|
|
|
MOZ_ASSERT(mPrecompiledMemberHolder);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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
|
2013-08-09 09:25:13 -07:00
|
|
|
nsXBLProtoImpl::Read(nsIObjectInputStream* aStream,
|
2013-08-09 09:25:14 -07:00
|
|
|
nsXBLPrototypeBinding* aBinding)
|
2011-11-03 13:39:08 -07:00
|
|
|
{
|
2013-08-09 09:25:13 -07:00
|
|
|
AssertInCompilationScope();
|
|
|
|
AutoJSContext cx;
|
2011-11-03 13:39:08 -07:00
|
|
|
// Set up a class object first so that deserialization is possible
|
2013-08-09 09:25:14 -07:00
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
2014-04-11 07:59:47 -07:00
|
|
|
mPrecompiledMemberHolder = JS_NewObjectWithGivenProto(cx, nullptr, JS::NullPtr(), global);
|
|
|
|
if (!mPrecompiledMemberHolder)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
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;
|
2014-04-11 07:59:47 -07:00
|
|
|
nsresult rv = aStream->Read8(&type);
|
2011-11-03 13:39:08 -07:00
|
|
|
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 09:25:13 -07:00
|
|
|
rv = field->Read(aStream);
|
2011-11-03 13:39:08 -07: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 09:25:13 -07:00
|
|
|
rv = prop->Read(aStream, type & XBLBinding_Serialize_Mask);
|
2011-11-03 13:39:08 -07: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 09:25:13 -07:00
|
|
|
rv = method->Read(aStream);
|
2011-11-03 13:39:08 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete method;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousMember = AddMember(method, previousMember);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XBLBinding_Serialize_Constructor:
|
|
|
|
{
|
2013-08-21 16:45:52 -07:00
|
|
|
nsAutoString name;
|
|
|
|
rv = aStream->ReadString(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mConstructor = new nsXBLProtoImplAnonymousMethod(name.get());
|
2013-08-09 09:25:13 -07:00
|
|
|
rv = mConstructor->Read(aStream);
|
2011-11-03 13:39:08 -07:00
|
|
|
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:
|
|
|
|
{
|
2013-08-21 16:45:52 -07:00
|
|
|
nsAutoString name;
|
|
|
|
rv = aStream->ReadString(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mDestructor = new nsXBLProtoImplAnonymousMethod(name.get());
|
2013-08-09 09:25:13 -07:00
|
|
|
rv = mDestructor->Read(aStream);
|
2011-11-03 13:39:08 -07:00
|
|
|
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
|
2013-08-09 09:25:13 -07:00
|
|
|
nsXBLProtoImpl::Write(nsIObjectOutputStream* aStream,
|
2011-11-03 13:39:08 -07:00
|
|
|
nsXBLPrototypeBinding* aBinding)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-04-11 07:59:47 -07:00
|
|
|
if (!mPrecompiledMemberHolder) {
|
2011-11-03 13:39:08 -07:00
|
|
|
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 09:25:13 -07:00
|
|
|
rv = curr->Write(aStream);
|
2011-11-03 13:39:08 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
for (nsXBLProtoImplMember* curr = mMembers; curr; curr = curr->GetNext()) {
|
|
|
|
if (curr == mConstructor) {
|
2013-08-09 09:25:13 -07:00
|
|
|
rv = mConstructor->Write(aStream, XBLBinding_Serialize_Constructor);
|
2011-11-03 13:39:08 -07:00
|
|
|
}
|
|
|
|
else if (curr == mDestructor) {
|
2013-08-09 09:25:13 -07:00
|
|
|
rv = mDestructor->Write(aStream, XBLBinding_Serialize_Destructor);
|
2011-11-03 13:39:08 -07:00
|
|
|
}
|
|
|
|
else {
|
2013-08-09 09:25:13 -07:00
|
|
|
rv = curr->Write(aStream);
|
2011-11-03 13:39:08 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aStream->Write8(XBLBinding_Serialize_NoMoreItems);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
NS_NewXBLProtoImpl(nsXBLPrototypeBinding* aBinding,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aClassName,
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|