Files
palemoon27/dom/xbl/nsXBLProtoImplMethod.cpp
T
roytam1 5c3e1ed7ef import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1255818. Switch from JS_ClearPendingException to AutoJSAPI::ClearException for cases when we have an AutoJSAPI around. r=bholley (e1e0413493)
- Bug 1253591. HTMLAllCollection should be rooted at the document, not the root element, so it can find the root element. r=peterv (c5f2f253a9)
- Bug 1257270 - Use std::nullptr_t instead of nullptr_t in WMFUtils.h. r=cpearce (539e705876)
- Bug 1251881 - use UniquePtr instead of ScopedDeletePtr in mozglue/linker/; r=glandium (2fe329e32c)
- Bug 1219068 - Compute size correctly when a character consists entirely of glue. r=karlt (80f2e23268)
- Bug 1246132 - Improve register allocation speed on large functions, r=sunfish. (6e023c252b)
- Bug 1150354: Make nsPluginDirServiceProvider build with MSVC 2015 CTP 6, r=jmathies (0113760730)
- Bug 1197311 - Remove PR_snprintf calls in dom/ r=froydnj (6268400ef0)
- Bug 1255099 - XHR CTOR doesn't need to use implicitJSContext, r=bz (72f08fbc36)
- Bug 1255597 - Part 1: Remove redundent trailing spaces. r=khuey (7f2240cb05)
- Bug 1255597 - Part 2: Follow spec to modify the content-type check conditions which determine parsing XHR body or not. r=khuey (ba6f7fc536)
- Bug 1255597 - Part 3: Synchronize content-type hint of HttpChannelChild to HttpChannelParent. r=jduell (bb97478b01)
- Bug 1255597 - Part 4: Remove 'expected fail' settings and expect test cases in responsexml-media-type.htm should be passed. r=khuey (772884b4fb)
- Bug 1201170 - During message diversion must be possible to suspend them. r=jduell (abb2361b7f)
- Bug 1201174 - For FTP - in case of divertToParent, it myst be possible to delay delivering of OnDataAv./OnStopR. r=jduell (9299ef81be)
- Bug 1247393 - use arrays of UniquePtr in ChannelEventQueue; r=mcmanus (7621205b53)
- Bug 1254730 - ChannelEventQueue must be thread-safe, r=michal (b8e01204e9)
- Bug 1254859 part 2. Switch the AutoEntryScript in nsXULTemplateBuilder::InitHTMLTemplateRoot to take ownership of error reporting. r=bholley (f183ec692c)
- Bug 1254859 part 1. Switch the AutoEntryScript in nsGlobalWindow::FireOnNewGlobalObject to take ownership of error reporting. r=bholley (b0cd891917)
- Bug 1254860. Switch the AutoEntryScript TestShellCommandParent::RunCallback to taking ownership of error reporting. r=bholley (0c9697e60f)
- Bug 1254857. Switch the AutoEntryScript in xpc::EvalInSandbox to take ownership of error reporting. r=bholley (93b6bfc87c)
- Bug 1254847 part 3. Make AutoEntryScript always take ownership of error reporting. r=bholley (3c2929462f)
- Bug 1256688 - Continue using getPropertyDescriptor for has in Sandbox창. r=peterv (be7e50b715)
- Bug 1254730 - patch 2 - Better comments and a better management of lifetime of ChannelEvents, r=michal (8348911e35)
- Bug 1163198 - Remove instances of #ifdef PR_LOGGING in dom/plugins. r=froydnj (ba13039dfa)
- Bug 1253216 - clean up the atomic ops ifdef nest. r=jorendorff (6ca747d3cf)
- Bug 1257055 - Use jit/arm64/Architecture-arm64.h on non-JIT aarch64. r=lth (4f3949cd19)
- Bug 1253379 - Cache timings not send to HttpChannelChild r=mayhemer (395172278f)
- align SetAllowStaleCacheContent to FF52, possible misspatch (9d29d27011)
2024-03-19 22:11:27 +08:00

357 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "nsIContent.h"
#include "nsIDocument.h"
#include "nsIGlobalObject.h"
#include "nsUnicharUtils.h"
#include "nsReadableUtils.h"
#include "nsXBLProtoImplMethod.h"
#include "nsJSUtils.h"
#include "nsContentUtils.h"
#include "nsIScriptSecurityManager.h"
#include "nsIXPConnect.h"
#include "xpcpublic.h"
#include "nsXBLPrototypeBinding.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ScriptSettings.h"
using namespace mozilla;
using namespace mozilla::dom;
nsXBLProtoImplMethod::nsXBLProtoImplMethod(const char16_t* aName) :
nsXBLProtoImplMember(aName),
mMethod()
{
MOZ_COUNT_CTOR(nsXBLProtoImplMethod);
}
nsXBLProtoImplMethod::~nsXBLProtoImplMethod()
{
MOZ_COUNT_DTOR(nsXBLProtoImplMethod);
if (!IsCompiled()) {
delete GetUncompiledMethod();
}
}
void
nsXBLProtoImplMethod::AppendBodyText(const nsAString& aText)
{
NS_PRECONDITION(!IsCompiled(),
"Must not be compiled when accessing uncompiled method");
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
if (!uncompiledMethod) {
uncompiledMethod = new nsXBLUncompiledMethod();
SetUncompiledMethod(uncompiledMethod);
}
uncompiledMethod->AppendBodyText(aText);
}
void
nsXBLProtoImplMethod::AddParameter(const nsAString& aText)
{
NS_PRECONDITION(!IsCompiled(),
"Must not be compiled when accessing uncompiled method");
if (aText.IsEmpty()) {
NS_WARNING("Empty name attribute in xbl:parameter!");
return;
}
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
if (!uncompiledMethod) {
uncompiledMethod = new nsXBLUncompiledMethod();
SetUncompiledMethod(uncompiledMethod);
}
uncompiledMethod->AddParameter(aText);
}
void
nsXBLProtoImplMethod::SetLineNumber(uint32_t aLineNumber)
{
NS_PRECONDITION(!IsCompiled(),
"Must not be compiled when accessing uncompiled method");
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
if (!uncompiledMethod) {
uncompiledMethod = new nsXBLUncompiledMethod();
SetUncompiledMethod(uncompiledMethod);
}
uncompiledMethod->SetLineNumber(aLineNumber);
}
nsresult
nsXBLProtoImplMethod::InstallMember(JSContext* aCx,
JS::Handle<JSObject*> aTargetClassObject)
{
NS_PRECONDITION(IsCompiled(),
"Should not be installing an uncompiled method");
MOZ_ASSERT(js::IsObjectInContextCompartment(aTargetClassObject, aCx));
#ifdef DEBUG
{
JS::Rooted<JSObject*> globalObject(aCx, JS_GetGlobalForObject(aCx, aTargetClassObject));
MOZ_ASSERT(xpc::IsInContentXBLScope(globalObject) ||
xpc::IsInAddonScope(globalObject) ||
globalObject == xpc::GetXBLScope(aCx, globalObject));
MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx) == globalObject);
}
#endif
JS::Rooted<JSObject*> jsMethodObject(aCx, GetCompiledMethod());
if (jsMethodObject) {
nsDependentString name(mName);
JS::Rooted<JSObject*> method(aCx, JS::CloneFunctionObject(aCx, jsMethodObject));
NS_ENSURE_TRUE(method, NS_ERROR_OUT_OF_MEMORY);
if (!::JS_DefineUCProperty(aCx, aTargetClassObject,
static_cast<const char16_t*>(mName),
name.Length(), method,
JSPROP_ENUMERATE)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}
nsresult
nsXBLProtoImplMethod::CompileMember(AutoJSAPI& jsapi, const nsString& aClassStr,
JS::Handle<JSObject*> aClassObject)
{
AssertInCompilationScope();
NS_PRECONDITION(!IsCompiled(),
"Trying to compile an already-compiled method");
NS_PRECONDITION(aClassObject,
"Must have class object to compile");
nsXBLUncompiledMethod* uncompiledMethod = GetUncompiledMethod();
// No parameters or body was supplied, so don't install method.
if (!uncompiledMethod) {
// Early return after which we consider ourselves compiled.
SetCompiledMethod(nullptr);
return NS_OK;
}
// Don't install method if no name was supplied.
if (!mName) {
delete uncompiledMethod;
// Early return after which we consider ourselves compiled.
SetCompiledMethod(nullptr);
return NS_OK;
}
// We have a method.
// Allocate an array for our arguments.
int32_t paramCount = uncompiledMethod->GetParameterCount();
char** args = nullptr;
if (paramCount > 0) {
args = new char*[paramCount];
// Add our parameters to our args array.
int32_t argPos = 0;
for (nsXBLParameter* curr = uncompiledMethod->mParameters;
curr;
curr = curr->mNext) {
args[argPos] = curr->mName;
argPos++;
}
}
// Get the body
nsDependentString body;
char16_t *bodyText = uncompiledMethod->mBodyText.GetText();
if (bodyText)
body.Rebind(bodyText);
// Now that we have a body and args, compile the function
// and then define it.
NS_ConvertUTF16toUTF8 cname(mName);
NS_ConvertUTF16toUTF8 functionUri(aClassStr);
int32_t hash = functionUri.RFindChar('#');
if (hash != kNotFound) {
functionUri.Truncate(hash);
}
JSContext *cx = jsapi.cx();
JSAutoCompartment ac(cx, aClassObject);
JS::CompileOptions options(cx);
options.setFileAndLine(functionUri.get(),
uncompiledMethod->mBodyText.GetLineNumber())
.setVersion(JSVERSION_LATEST);
JS::Rooted<JSObject*> methodObject(cx);
JS::AutoObjectVector emptyVector(cx);
nsresult rv = nsJSUtils::CompileFunction(jsapi, emptyVector, options, cname,
paramCount,
const_cast<const char**>(args),
body, methodObject.address());
// Destroy our uncompiled method and delete our arg list.
delete uncompiledMethod;
delete [] args;
if (NS_FAILED(rv)) {
SetUncompiledMethod(nullptr);
return rv;
}
SetCompiledMethod(methodObject);
return NS_OK;
}
void
nsXBLProtoImplMethod::Trace(const TraceCallbacks& aCallbacks, void *aClosure)
{
if (IsCompiled() && GetCompiledMethodPreserveColor()) {
aCallbacks.Trace(&mMethod.AsHeapObject(), "mMethod", aClosure);
}
}
nsresult
nsXBLProtoImplMethod::Read(nsIObjectInputStream* aStream)
{
AssertInCompilationScope();
MOZ_ASSERT(!IsCompiled() && !GetUncompiledMethod());
AutoJSContext cx;
JS::Rooted<JSObject*> methodObject(cx);
nsresult rv = XBL_DeserializeFunction(aStream, &methodObject);
if (NS_FAILED(rv)) {
SetUncompiledMethod(nullptr);
return rv;
}
SetCompiledMethod(methodObject);
return NS_OK;
}
nsresult
nsXBLProtoImplMethod::Write(nsIObjectOutputStream* aStream)
{
AssertInCompilationScope();
MOZ_ASSERT(IsCompiled());
if (GetCompiledMethodPreserveColor()) {
nsresult rv = aStream->Write8(XBLBinding_Serialize_Method);
NS_ENSURE_SUCCESS(rv, rv);
rv = aStream->WriteWStringZ(mName);
NS_ENSURE_SUCCESS(rv, rv);
// Calling fromMarkedLocation() is safe because mMethod is traced by the
// Trace() method above, and because its value is never changed after it has
// been set to a compiled method.
JS::Handle<JSObject*> method =
JS::Handle<JSObject*>::fromMarkedLocation(mMethod.AsHeapObject().address());
return XBL_SerializeFunction(aStream, method);
}
return NS_OK;
}
nsresult
nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement, JSAddonId* aAddonId)
{
MOZ_ASSERT(aBoundElement->IsElement());
NS_PRECONDITION(IsCompiled(), "Can't execute uncompiled method");
if (!GetCompiledMethod()) {
// Nothing to do here
return NS_OK;
}
// Get the script context the same way
// nsXBLProtoImpl::InstallImplementation does.
nsIDocument* document = aBoundElement->OwnerDoc();
nsCOMPtr<nsIGlobalObject> global =
do_QueryInterface(document->GetInnerWindow());
if (!global) {
return NS_OK;
}
nsAutoMicroTask mt;
// We are going to run script via JS::Call, so we need a script entry point,
// but as this is XBL related it does not appear in the HTML spec.
dom::AutoEntryScript aes(global, "XBL <constructor>/<destructor> invocation");
JSContext* cx = aes.cx();
JS::Rooted<JSObject*> globalObject(cx, global->GetGlobalJSObject());
JS::Rooted<JSObject*> scopeObject(cx, xpc::GetScopeForXBLExecution(cx, globalObject, aAddonId));
NS_ENSURE_TRUE(scopeObject, NS_ERROR_OUT_OF_MEMORY);
JSAutoCompartment ac(cx, scopeObject);
JS::AutoObjectVector scopeChain(cx);
if (!nsJSUtils::GetScopeChainForElement(cx, aBoundElement->AsElement(),
scopeChain)) {
return NS_ERROR_OUT_OF_MEMORY;
}
MOZ_ASSERT(scopeChain.length() != 0);
// Clone the function object, using our scope chain (for backwards
// compat to the days when this was an event handler).
JS::Rooted<JSObject*> jsMethodObject(cx, GetCompiledMethod());
JS::Rooted<JSObject*> method(cx, JS::CloneFunctionObject(cx, jsMethodObject,
scopeChain));
if (!method)
return NS_ERROR_OUT_OF_MEMORY;
// Now call the method
// Check whether script is enabled.
bool scriptAllowed = nsContentUtils::GetSecurityManager()->
ScriptAllowed(js::GetGlobalForObjectCrossCompartment(method));
if (scriptAllowed) {
JS::Rooted<JS::Value> retval(cx);
JS::Rooted<JS::Value> methodVal(cx, JS::ObjectValue(*method));
// No need to check the return here as AutoEntryScript has taken ownership
// of error reporting.
::JS::Call(cx, scopeChain[0], methodVal, JS::HandleValueArray::empty(), &retval);
}
return NS_OK;
}
nsresult
nsXBLProtoImplAnonymousMethod::Write(nsIObjectOutputStream* aStream,
XBLBindingSerializeDetails aType)
{
AssertInCompilationScope();
MOZ_ASSERT(IsCompiled());
if (GetCompiledMethodPreserveColor()) {
nsresult rv = aStream->Write8(aType);
NS_ENSURE_SUCCESS(rv, rv);
rv = aStream->WriteWStringZ(mName);
NS_ENSURE_SUCCESS(rv, rv);
// Calling fromMarkedLocation() is safe because mMethod is traced by the
// Trace() method above, and because its value is never changed after it has
// been set to a compiled method.
JS::Handle<JSObject*> method =
JS::Handle<JSObject*>::fromMarkedLocation(mMethod.AsHeapObject().address());
rv = XBL_SerializeFunction(aStream, method);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}