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
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2010-04-30 12:40:59 -07:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsXBLProtoImplField.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2007-09-28 06:45:01 -07:00
|
|
|
#include "nsIURI.h"
|
2011-11-03 13:39:08 -07:00
|
|
|
#include "nsXBLSerialize.h"
|
|
|
|
#include "nsXBLPrototypeBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const PRUnichar* aName, const PRUnichar* aReadOnly)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mNext(nullptr),
|
|
|
|
mFieldText(nullptr),
|
2007-03-22 10:30:00 -07:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
2007-09-28 06:45:01 -07:00
|
|
|
mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer?
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aReadOnly) {
|
2010-02-03 13:17:55 -08:00
|
|
|
nsAutoString readOnly; readOnly.Assign(aReadOnly);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (readOnly.LowerCaseEqualsLiteral("true"))
|
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 13:39:08 -07:00
|
|
|
|
2011-11-20 19:09:16 -08:00
|
|
|
nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mNext(nullptr),
|
|
|
|
mFieldText(nullptr),
|
2011-11-03 13:39:08 -07:00
|
|
|
mFieldTextLength(0),
|
|
|
|
mLineNumber(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXBLProtoImplField);
|
|
|
|
|
|
|
|
mJSAttributes = JSPROP_ENUMERATE;
|
|
|
|
if (aIsReadOnly)
|
|
|
|
mJSAttributes |= JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXBLProtoImplField::~nsXBLProtoImplField()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXBLProtoImplField);
|
|
|
|
if (mFieldText)
|
|
|
|
nsMemory::Free(mFieldText);
|
2007-09-28 06:45:01 -07:00
|
|
|
NS_Free(mName);
|
2008-12-15 03:33:56 -08:00
|
|
|
NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXBLProtoImplField::AppendFieldText(const nsAString& aText)
|
|
|
|
{
|
|
|
|
if (mFieldText) {
|
|
|
|
nsDependentString fieldTextStr(mFieldText, mFieldTextLength);
|
|
|
|
nsAutoString newFieldText = fieldTextStr + aText;
|
|
|
|
PRUnichar* temp = mFieldText;
|
|
|
|
mFieldText = ToNewUnicode(newFieldText);
|
|
|
|
mFieldTextLength = newFieldText.Length();
|
|
|
|
nsMemory::Free(temp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mFieldText = ToNewUnicode(aText);
|
|
|
|
mFieldTextLength = aText.Length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-09-28 06:45:01 -07:00
|
|
|
nsXBLProtoImplField::InstallField(nsIScriptContext* aContext,
|
|
|
|
JSObject* aBoundNode,
|
2008-11-28 18:16:17 -08:00
|
|
|
nsIPrincipal* aPrincipal,
|
2007-10-02 07:38:35 -07:00
|
|
|
nsIURI* aBindingDocURI,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aDidInstall) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-30 12:40:59 -07:00
|
|
|
NS_TIME_FUNCTION_MIN(5);
|
2007-09-28 06:45:01 -07:00
|
|
|
NS_PRECONDITION(aBoundNode,
|
|
|
|
"uh-oh, bound node should NOT be null or bad things will "
|
|
|
|
"happen");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidInstall = false;
|
2007-10-02 07:38:35 -07:00
|
|
|
|
2012-05-29 12:01:30 -07:00
|
|
|
// Empty fields are treated as not actually present.
|
|
|
|
if (IsEmpty()) {
|
2007-10-02 07:38:35 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
nsAutoMicroTask mt;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// EvaluateStringWithValue and JS_DefineUCProperty can both trigger GC, so
|
|
|
|
// protect |result| here.
|
|
|
|
nsresult rv;
|
|
|
|
|
2007-10-02 07:38:35 -07:00
|
|
|
nsCAutoString uriSpec;
|
|
|
|
aBindingDocURI->GetSpec(uriSpec);
|
2007-09-26 06:55:06 -07:00
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
JSContext* cx = aContext->GetNativeContext();
|
2008-03-05 12:41:27 -08:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"Shouldn't get here when an exception is pending!");
|
|
|
|
|
2007-10-02 07:38:35 -07:00
|
|
|
// compile the literal string
|
2011-09-28 23:19:26 -07:00
|
|
|
bool undefined;
|
2007-10-02 07:38:35 -07:00
|
|
|
nsCOMPtr<nsIScriptContext> context = aContext;
|
2010-11-26 06:11:14 -08:00
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
jsval result = JSVAL_NULL;
|
2007-10-02 07:38:35 -07:00
|
|
|
rv = context->EvaluateStringWithValue(nsDependentString(mFieldText,
|
|
|
|
mFieldTextLength),
|
|
|
|
aBoundNode,
|
2008-11-28 18:16:17 -08:00
|
|
|
aPrincipal, uriSpec.get(),
|
2008-02-15 21:13:16 -08:00
|
|
|
mLineNumber, JSVERSION_LATEST,
|
2011-11-15 23:50:20 -08:00
|
|
|
&result, &undefined);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-10-02 07:38:35 -07:00
|
|
|
return rv;
|
2011-11-15 23:50:20 -08:00
|
|
|
}
|
2007-09-26 07:39:31 -07:00
|
|
|
|
2007-10-02 07:38:35 -07:00
|
|
|
if (undefined) {
|
|
|
|
result = JSVAL_VOID;
|
2007-09-28 06:45:01 -07:00
|
|
|
}
|
2007-09-26 07:39:31 -07:00
|
|
|
|
2007-09-28 06:45:01 -07:00
|
|
|
// Define the evaluated result as a JS property
|
|
|
|
nsDependentString name(mName);
|
|
|
|
if (!::JS_DefineUCProperty(cx, aBoundNode,
|
|
|
|
reinterpret_cast<const jschar*>(mName),
|
2012-07-30 07:20:58 -07:00
|
|
|
name.Length(), result, nullptr, nullptr,
|
2007-09-28 06:45:01 -07:00
|
|
|
mJSAttributes)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-10-02 07:38:35 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aDidInstall = true;
|
2007-09-28 06:45:01 -07:00
|
|
|
return NS_OK;
|
2007-09-26 07:39:31 -07:00
|
|
|
}
|
2011-11-03 13:39:08 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplField::Read(nsIScriptContext* aContext,
|
|
|
|
nsIObjectInputStream* aStream)
|
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
nsresult rv = aStream->ReadString(name);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mName = ToNewUnicode(name);
|
|
|
|
|
|
|
|
rv = aStream->Read32(&mLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString fieldText;
|
|
|
|
rv = aStream->ReadString(fieldText);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mFieldTextLength = fieldText.Length();
|
|
|
|
if (mFieldTextLength)
|
|
|
|
mFieldText = ToNewUnicode(fieldText);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXBLProtoImplField::Write(nsIScriptContext* aContext,
|
|
|
|
nsIObjectOutputStream* aStream)
|
|
|
|
{
|
|
|
|
XBLBindingSerializeDetails type = XBLBinding_Serialize_Field;
|
|
|
|
|
|
|
|
if (mJSAttributes & JSPROP_READONLY) {
|
|
|
|
type |= XBLBinding_Serialize_ReadOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = aStream->Write8(type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aStream->WriteWStringZ(mName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aStream->Write32(mLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return aStream->WriteWStringZ(mFieldText ? mFieldText : EmptyString().get());
|
|
|
|
}
|