2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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"
|
2001-11-13 07:09:56 +03:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIServiceManager.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"
|
2014-08-06 19:30:25 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.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;
|
2014-09-29 17:34:21 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-11-01 18:31:57 +04:00
|
|
|
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
|
|
|
|
2014-08-06 19:30:25 +04: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;
|
|
|
|
}
|
2001-11-13 07:09:56 +03:00
|
|
|
|
2014-08-06 19:30:25 +04: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();
|
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);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
// First, start by entering the realm of the XBL scope. This may or may
|
|
|
|
// not be the same realm as globalObject.
|
2013-05-02 13:12:45 +04:00
|
|
|
JS::Rooted<JSObject*> globalObject(cx,
|
2018-07-26 11:53:00 +03:00
|
|
|
JS::GetNonCCWObjectGlobal(targetClassObject));
|
2018-03-14 05:43:30 +03:00
|
|
|
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetXBLScopeOrGlobal(cx, globalObject));
|
2013-04-05 23:04:09 +04:00
|
|
|
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
|
2018-07-06 19:16:23 +03:00
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(scopeObject));
|
2018-07-31 09:37:07 +03:00
|
|
|
JSAutoRealm ar(cx, scopeObject);
|
2013-11-01 18:31:57 +04:00
|
|
|
|
2014-05-23 05:44:03 +04: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.
|
2018-09-25 12:53:39 +03:00
|
|
|
const nsString& className = aPrototypeBinding->ClassName();
|
|
|
|
const char16_t* classNameChars = className.get();
|
|
|
|
const size_t classNameLen = className.Length();
|
|
|
|
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JSObject*> propertyHolder(cx);
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::Rooted<JS::PropertyDescriptor> existingHolder(cx);
|
2014-05-23 05:44:03 +04:00
|
|
|
if (scopeObject != globalObject &&
|
2018-09-25 12:53:39 +03:00
|
|
|
!JS_GetOwnUCPropertyDescriptor(cx, scopeObject, classNameChars,
|
|
|
|
classNameLen, &existingHolder)) {
|
2014-05-23 05:44:03 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
bool propertyHolderIsNew = !existingHolder.object() || !existingHolder.value().isObject();
|
|
|
|
|
|
|
|
if (!propertyHolderIsNew) {
|
|
|
|
propertyHolder = &existingHolder.value().toObject();
|
|
|
|
} else if (scopeObject != globalObject) {
|
2013-02-13 22:16:19 +04:00
|
|
|
|
2013-11-01 18:31:57 +04:00
|
|
|
// This is just a property holder, so it doesn't need any special JSClass.
|
2015-05-14 00:07:34 +03:00
|
|
|
propertyHolder = JS_NewObjectWithGivenProto(cx, nullptr, nullptr);
|
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.
|
2018-09-25 12:53:39 +03:00
|
|
|
bool ok =
|
|
|
|
JS_DefineUCProperty(cx, scopeObject, classNameChars, classNameLen,
|
|
|
|
propertyHolder, JSPROP_PERMANENT | JSPROP_READONLY);
|
2013-02-13 22:16:19 +04:00
|
|
|
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.
|
2014-05-23 05:44:03 +04:00
|
|
|
if (propertyHolderIsNew) {
|
|
|
|
for (nsXBLProtoImplMember* curr = mMembers;
|
|
|
|
curr;
|
|
|
|
curr = curr->GetNext())
|
|
|
|
curr->InstallMember(cx, propertyHolder);
|
|
|
|
}
|
2013-11-01 18:31:57 +04:00
|
|
|
|
|
|
|
// 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);
|
2014-06-24 03:33:37 +04:00
|
|
|
bool inContentXBLScope = xpc::IsInContentXBLScope(scopeObject);
|
2013-11-01 18:31:58 +04:00
|
|
|
for (nsXBLProtoImplMember* curr = mMembers; curr; curr = curr->GetNext()) {
|
2014-06-24 03:33:37 +04:00
|
|
|
if (!inContentXBLScope || curr->ShouldExposeToUntrustedContent()) {
|
2013-11-01 18:31:58 +04: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-24 03:33:37 +04: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 18:31:58 +04:00
|
|
|
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.
|
2018-07-31 09:37:07 +03:00
|
|
|
JSAutoRealm ar2(cx, targetClassObject);
|
2013-11-26 03:35:22 +04:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-10 06:45:11 +03:00
|
|
|
nsresult
|
2003-03-07 02:59:18 +03:00
|
|
|
nsXBLProtoImpl::InitTargetObjects(nsXBLPrototypeBinding* aBinding,
|
2017-05-10 06:45:11 +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
|
|
|
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!mPrecompiledMemberHolder) {
|
2003-09-11 08:10:20 +04:00
|
|
|
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;
|
|
|
|
|
2014-04-11 18:59:47 +04:00
|
|
|
MOZ_ASSERT(mPrecompiledMemberHolder);
|
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
|
|
|
|
2018-07-31 09:37:07 +03:00
|
|
|
JSAutoRealm ar(cx, global);
|
2014-01-25 03:01:48 +04:00
|
|
|
// Make sure the interface object is created before the prototype object
|
|
|
|
// so that XULElement is hidden from content. See bug 909340.
|
2018-06-26 00:20:54 +03:00
|
|
|
bool defineOnGlobal = dom::XULElement_Binding::ConstructorEnabled(cx, global);
|
|
|
|
dom::XULElement_Binding::GetConstructorObjectHandle(cx, defineOnGlobal);
|
2013-08-29 02:48:42 +04:00
|
|
|
|
2014-04-10 08:58:41 +04:00
|
|
|
rv = nsContentUtils::WrapNative(cx, aBoundElement, &v,
|
2014-02-13 03:19:32 +04:00
|
|
|
/* aAllowWrapping = */ false);
|
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());
|
|
|
|
|
2018-07-26 11:52:41 +03:00
|
|
|
// We passed aAllowWrapping = false to nsContentUtils::WrapNative so we
|
|
|
|
// should not have a wrapper.
|
|
|
|
MOZ_ASSERT(!js::IsWrapper(value));
|
|
|
|
|
2018-07-31 09:37:07 +03:00
|
|
|
JSAutoRealm ar2(cx, value);
|
|
|
|
|
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.
|
2014-03-19 20:35:45 +04:00
|
|
|
rv = aBinding->InitClass(mClassName, cx, 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
|
|
|
{
|
2017-05-10 06:45:11 +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
|
2001-11-13 07:09:56 +03:00
|
|
|
// context.
|
2014-09-29 17:34:21 +04:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(xpc::CompilationScope())))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
JSContext* cx = jsapi.cx();
|
2001-11-13 07:09:56 +03:00
|
|
|
|
2015-05-14 00:07:34 +03:00
|
|
|
mPrecompiledMemberHolder = JS_NewObjectWithGivenProto(cx, nullptr, nullptr);
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!mPrecompiledMemberHolder)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
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.
|
2014-04-11 18:59:47 +04:00
|
|
|
JS::Rooted<JSObject*> rootedHolder(cx, mPrecompiledMemberHolder);
|
2001-11-13 07:09:56 +03:00
|
|
|
for (nsXBLProtoImplMember* curr = mMembers;
|
|
|
|
curr;
|
|
|
|
curr = curr->GetNext()) {
|
2014-09-29 17:34:21 +04:00
|
|
|
nsresult rv = curr->CompileMember(jsapi, mClassName, rootedHolder);
|
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,
|
2016-01-28 13:28:04 +03:00
|
|
|
JS::MutableHandle<JS::PropertyDescriptor> aDesc,
|
2014-03-17 20:17:58 +04:00
|
|
|
JS::Handle<JSObject*> aClassObject)
|
2013-02-08 18:24:21 +04:00
|
|
|
{
|
|
|
|
for (nsXBLProtoImplMember* m = mMembers; m; m = m->GetNext()) {
|
|
|
|
if (aName.Equals(m->GetName())) {
|
2014-04-26 01:11:01 +04:00
|
|
|
return JS_GetPropertyDescriptorById(aCx, aClassObject, aNameAsId, aDesc);
|
2013-02-08 18:24:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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.
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!mPrecompiledMemberHolder) {
|
2007-05-24 22:39:49 +04:00
|
|
|
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
|
|
|
{
|
2014-04-11 18:59:47 +04:00
|
|
|
if (mPrecompiledMemberHolder) {
|
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()) {
|
|
|
|
nsDependentString name(f->GetName());
|
2014-12-17 02:28:39 +03:00
|
|
|
bool dummy;
|
|
|
|
if (!::JS_HasUCProperty(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
|
|
|
{
|
|
|
|
for (nsXBLProtoImplField* f = mFields; f; f = f->GetNext()) {
|
|
|
|
nsDependentString name(f->GetName());
|
2007-11-15 20:09:14 +03:00
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
const char16_t* 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) {
|
2015-02-04 19:20:04 +03:00
|
|
|
JS::ObjectOpResult ignored;
|
|
|
|
::JS_DeleteUCProperty(cx, obj, s, name.Length(), ignored);
|
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
|
|
|
{
|
2014-04-11 18:59:47 +04:00
|
|
|
MOZ_ASSERT(mPrecompiledMemberHolder);
|
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
|
2015-05-14 00:07:34 +03:00
|
|
|
mPrecompiledMemberHolder = JS_NewObjectWithGivenProto(cx, nullptr, nullptr);
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!mPrecompiledMemberHolder)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
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;
|
2014-04-11 18:59:47 +04:00
|
|
|
nsresult rv = aStream->Read8(&type);
|
2011-11-04 00:39:08 +04: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 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;
|
|
|
|
|
2014-04-11 18:59:47 +04:00
|
|
|
if (!mPrecompiledMemberHolder) {
|
2011-11-04 00:39:08 +04:00
|
|
|
rv = CompilePrototypeMembers(aBinding);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2015-03-13 22:05:57 +03:00
|
|
|
rv = aStream->WriteUtf8Z(mClassName.get());
|
2011-11-04 00:39:08 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-09-08 07:19:01 +03:00
|
|
|
void
|
2017-05-10 06:45:11 +03:00
|
|
|
NS_NewXBLProtoImpl(nsXBLPrototypeBinding* aBinding,
|
|
|
|
const char16_t* aClassName,
|
2003-09-27 08:18:26 +04:00
|
|
|
nsXBLProtoImpl** aResult)
|
|
|
|
{
|
|
|
|
nsXBLProtoImpl* impl = new nsXBLProtoImpl();
|
2015-03-13 22:05:57 +03:00
|
|
|
if (aClassName) {
|
|
|
|
impl->mClassName = aClassName;
|
|
|
|
} else {
|
|
|
|
nsCString spec;
|
2016-09-08 07:19:01 +03:00
|
|
|
nsresult rv = aBinding->BindingURI()->GetSpec(spec);
|
|
|
|
// XXX: should handle this better
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
2015-03-13 22:05:57 +03:00
|
|
|
impl->mClassName = NS_ConvertUTF8toUTF16(spec);
|
|
|
|
}
|
|
|
|
|
2003-09-27 08:18:26 +04:00
|
|
|
aBinding->SetImplementation(impl);
|
|
|
|
*aResult = impl;
|
|
|
|
}
|
|
|
|
|