gecko/content/xbl/src/nsXBLProtoImplField.cpp

178 lines
4.9 KiB
C++
Raw Normal View History

/* -*- 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/. */
#include "nsIAtom.h"
#include "nsString.h"
#include "jsapi.h"
#include "nsUnicharUtils.h"
#include "nsReadableUtils.h"
#include "nsXBLProtoImplField.h"
#include "nsIScriptContext.h"
#include "nsIURI.h"
#include "nsXBLSerialize.h"
#include "nsXBLPrototypeBinding.h"
nsXBLProtoImplField::nsXBLProtoImplField(const PRUnichar* aName, const PRUnichar* aReadOnly)
: mNext(nullptr),
mFieldText(nullptr),
mFieldTextLength(0),
mLineNumber(0)
{
MOZ_COUNT_CTOR(nsXBLProtoImplField);
mName = NS_strdup(aName); // XXXbz make more sense to use a stringbuffer?
mJSAttributes = JSPROP_ENUMERATE;
if (aReadOnly) {
nsAutoString readOnly; readOnly.Assign(aReadOnly);
if (readOnly.LowerCaseEqualsLiteral("true"))
mJSAttributes |= JSPROP_READONLY;
}
}
nsXBLProtoImplField::nsXBLProtoImplField(const bool aIsReadOnly)
: mNext(nullptr),
mFieldText(nullptr),
mFieldTextLength(0),
mLineNumber(0)
{
MOZ_COUNT_CTOR(nsXBLProtoImplField);
mJSAttributes = JSPROP_ENUMERATE;
if (aIsReadOnly)
mJSAttributes |= JSPROP_READONLY;
}
nsXBLProtoImplField::~nsXBLProtoImplField()
{
MOZ_COUNT_DTOR(nsXBLProtoImplField);
if (mFieldText)
nsMemory::Free(mFieldText);
NS_Free(mName);
NS_CONTENT_DELETE_LIST_MEMBER(nsXBLProtoImplField, this, mNext);
}
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
nsXBLProtoImplField::InstallField(nsIScriptContext* aContext,
JSObject* aBoundNode,
nsIURI* aBindingDocURI,
bool* aDidInstall) const
{
NS_PRECONDITION(aBoundNode,
"uh-oh, bound node should NOT be null or bad things will "
"happen");
*aDidInstall = false;
// Empty fields are treated as not actually present.
if (IsEmpty()) {
return NS_OK;
}
nsAutoMicroTask mt;
// EvaluateString and JS_DefineUCProperty can both trigger GC, so
// protect |result| here.
nsresult rv;
nsAutoCString uriSpec;
aBindingDocURI->GetSpec(uriSpec);
JSContext* cx = aContext->GetNativeContext();
NS_ASSERTION(!::JS_IsExceptionPending(cx),
"Shouldn't get here when an exception is pending!");
// compile the literal string
nsCOMPtr<nsIScriptContext> context = aContext;
JSAutoRequest ar(cx);
jsval result = JSVAL_NULL;
JS::CompileOptions options(cx);
options.setFileAndLine(uriSpec.get(), mLineNumber)
.setVersion(JSVERSION_LATEST)
.setUserBit(true); // Flag us as XBL
rv = context->EvaluateString(nsDependentString(mFieldText,
mFieldTextLength),
*aBoundNode, options,
/* aCoerceToString = */ false,
&result);
if (NS_FAILED(rv)) {
return rv;
}
2007-09-26 07:39:31 -07:00
// Define the evaluated result as a JS property
nsDependentString name(mName);
if (!::JS_DefineUCProperty(cx, aBoundNode,
reinterpret_cast<const jschar*>(mName),
name.Length(), result, nullptr, nullptr,
mJSAttributes)) {
return NS_ERROR_OUT_OF_MEMORY;
}
*aDidInstall = true;
return NS_OK;
2007-09-26 07:39:31 -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());
}