1332245 - Move nsScriptError from js/xpconnect to dom/bindings.

This commit is contained in:
Gaming4JC 2019-06-16 09:48:07 -04:00 committed by Roy Tam
commit 19854690b7
19 changed files with 335 additions and 83 deletions

View file

@ -34,9 +34,11 @@
#include "nsDOMClassInfo.h"
#include "ShimInterfaceInfo.h"
#include "nsIAddonInterposition.h"
#include "nsIScriptError.h"
#include "nsISimpleEnumerator.h"
#include "nsPIDOMWindow.h"
#include "nsGlobalWindow.h"
#include "nsScriptError.h"
using namespace mozilla;
using namespace JS;

View file

@ -11,9 +11,11 @@
#include "xpcprivate.h"
#include "nsIAtom.h"
#include "nsIScriptError.h"
#include "nsWrapperCache.h"
#include "nsJSUtils.h"
#include "nsQueryObject.h"
#include "nsScriptError.h"
#include "WrapperFactory.h"
#include "nsWrapperCacheInlines.h"

View file

@ -23,7 +23,6 @@
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect,
nsXPConnect::GetSingleton)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptError)
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSComponentLoader)
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
@ -31,14 +30,12 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
NS_DEFINE_NAMED_CID(NS_JS_ID_CID);
NS_DEFINE_NAMED_CID(NS_XPCONNECT_CID);
NS_DEFINE_NAMED_CID(NS_XPCEXCEPTION_CID);
NS_DEFINE_NAMED_CID(NS_SCRIPTERROR_CID);
NS_DEFINE_NAMED_CID(MOZJSCOMPONENTLOADER_CID);
NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADER_CID);
#define XPCONNECT_CIDENTRIES \
{ &kNS_JS_ID_CID, false, nullptr, nsJSIDConstructor }, \
{ &kNS_XPCONNECT_CID, false, nullptr, nsIXPConnectConstructor }, \
{ &kNS_SCRIPTERROR_CID, false, nullptr, nsScriptErrorConstructor }, \
{ &kMOZJSCOMPONENTLOADER_CID, false, nullptr, mozJSComponentLoaderConstructor },\
{ &kMOZ_JSSUBSCRIPTLOADER_CID, false, nullptr, mozJSSubScriptLoaderConstructor },
@ -46,7 +43,6 @@ NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADER_CID);
{ XPC_ID_CONTRACTID, &kNS_JS_ID_CID }, \
{ XPC_XPCONNECT_CONTRACTID, &kNS_XPCONNECT_CID }, \
{ XPC_CONTEXT_STACK_CONTRACTID, &kNS_XPCONNECT_CID }, \
{ NS_SCRIPTERROR_CONTRACTID, &kNS_SCRIPTERROR_CID }, \
{ MOZJSCOMPONENTLOADER_CONTRACTID, &kMOZJSCOMPONENTLOADER_CID }, \
{ MOZJSSUBSCRIPTLOADER_CONTRACTID, &kMOZ_JSSUBSCRIPTLOADER_CID },

View file

@ -10,6 +10,7 @@
#include "jsprf.h"
#include "nsArrayEnumerator.h"
#include "nsContentUtils.h"
#include "nsIScriptError.h"
#include "nsWrapperCache.h"
#include "AccessCheck.h"
#include "nsJSUtils.h"

View file

@ -11,6 +11,7 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/XPTInterfaceInfoManager.h"
#include "nsIScriptError.h"
#include "nsPrintfCString.h"
using namespace JS;

View file

@ -14,8 +14,6 @@ EXPORTS += [
UNIFIED_SOURCES += [
'ExportHelpers.cpp',
'nsScriptError.cpp',
'nsScriptErrorWithStack.cpp',
'nsXPConnect.cpp',
'Sandbox.cpp',
'XPCCallContext.cpp',
@ -58,6 +56,7 @@ LOCAL_INCLUDES += [
'../wrappers',
'/caps',
'/dom/base',
'/dom/bindings',
'/dom/html',
'/dom/svg',
'/dom/workers',

View file

@ -1,345 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
/* 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/. */
/*
* nsIScriptError implementation. Defined here, lacking a JS-specific
* place to put XPCOM things.
*/
#include "xpcprivate.h"
#include "jsprf.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "nsGlobalWindow.h"
#include "nsPIDOMWindow.h"
#include "nsILoadContext.h"
#include "nsIDocShell.h"
#include "nsIScriptError.h"
#include "nsISensitiveInfoHiddenURI.h"
static_assert(nsIScriptError::errorFlag == JSREPORT_ERROR &&
nsIScriptError::warningFlag == JSREPORT_WARNING &&
nsIScriptError::exceptionFlag == JSREPORT_EXCEPTION &&
nsIScriptError::strictFlag == JSREPORT_STRICT &&
nsIScriptError::infoFlag == JSREPORT_USER_1,
"flags should be consistent");
nsScriptErrorBase::nsScriptErrorBase()
: mMessage(),
mMessageName(),
mSourceName(),
mLineNumber(0),
mSourceLine(),
mColumnNumber(0),
mFlags(0),
mCategory(),
mOuterWindowID(0),
mInnerWindowID(0),
mTimeStamp(0),
mInitializedOnMainThread(false),
mIsFromPrivateWindow(false)
{
}
nsScriptErrorBase::~nsScriptErrorBase() {}
void
nsScriptErrorBase::InitializeOnMainThread()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mInitializedOnMainThread);
if (mInnerWindowID) {
nsGlobalWindow* window =
nsGlobalWindow::GetInnerWindowWithId(mInnerWindowID);
if (window) {
nsPIDOMWindowOuter* outer = window->GetOuterWindow();
if (outer)
mOuterWindowID = outer->WindowID();
nsIDocShell* docShell = window->GetDocShell();
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(docShell);
if (loadContext) {
// Never mark exceptions from chrome windows as having come from
// private windows, since we always want them to be reported.
nsIPrincipal* winPrincipal = window->GetPrincipal();
mIsFromPrivateWindow = loadContext->UsePrivateBrowsing() &&
!nsContentUtils::IsSystemPrincipal(winPrincipal);
}
}
}
mInitializedOnMainThread = true;
}
// nsIConsoleMessage methods
NS_IMETHODIMP
nsScriptErrorBase::GetMessageMoz(char16_t** result) {
nsresult rv;
nsAutoCString message;
rv = ToString(message);
if (NS_FAILED(rv))
return rv;
*result = UTF8ToNewUnicode(message);
if (!*result)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetLogLevel(uint32_t* aLogLevel)
{
if (mFlags & (uint32_t)nsIScriptError::infoFlag) {
*aLogLevel = nsIConsoleMessage::info;
} else if (mFlags & (uint32_t)nsIScriptError::warningFlag) {
*aLogLevel = nsIConsoleMessage::warn;
} else {
*aLogLevel = nsIConsoleMessage::error;
}
return NS_OK;
}
// nsIScriptError methods
NS_IMETHODIMP
nsScriptErrorBase::GetErrorMessage(nsAString& aResult) {
aResult.Assign(mMessage);
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetSourceName(nsAString& aResult) {
aResult.Assign(mSourceName);
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetSourceLine(nsAString& aResult) {
aResult.Assign(mSourceLine);
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetLineNumber(uint32_t* result) {
*result = mLineNumber;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetColumnNumber(uint32_t* result) {
*result = mColumnNumber;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetFlags(uint32_t* result) {
*result = mFlags;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetCategory(char** result) {
*result = ToNewCString(mCategory);
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetStack(JS::MutableHandleValue aStack) {
aStack.setUndefined();
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::SetStack(JS::HandleValue aStack) {
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetErrorMessageName(nsAString& aErrorMessageName) {
aErrorMessageName = mMessageName;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::SetErrorMessageName(const nsAString& aErrorMessageName) {
mMessageName = aErrorMessageName;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::Init(const nsAString& message,
const nsAString& sourceName,
const nsAString& sourceLine,
uint32_t lineNumber,
uint32_t columnNumber,
uint32_t flags,
const char* category)
{
return InitWithWindowID(message, sourceName, sourceLine, lineNumber,
columnNumber, flags,
category ? nsDependentCString(category)
: EmptyCString(),
0);
}
NS_IMETHODIMP
nsScriptErrorBase::InitWithWindowID(const nsAString& message,
const nsAString& sourceName,
const nsAString& sourceLine,
uint32_t lineNumber,
uint32_t columnNumber,
uint32_t flags,
const nsACString& category,
uint64_t aInnerWindowID)
{
mMessage.Assign(message);
if (!sourceName.IsEmpty()) {
mSourceName.Assign(sourceName);
nsCOMPtr<nsIURI> uri;
nsAutoCString pass;
if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), sourceName)) &&
NS_SUCCEEDED(uri->GetPassword(pass)) &&
!pass.IsEmpty()) {
nsCOMPtr<nsISensitiveInfoHiddenURI> safeUri =
do_QueryInterface(uri);
nsAutoCString loc;
if (safeUri &&
NS_SUCCEEDED(safeUri->GetSensitiveInfoHiddenSpec(loc))) {
mSourceName.Assign(NS_ConvertUTF8toUTF16(loc));
}
}
}
mLineNumber = lineNumber;
mSourceLine.Assign(sourceLine);
mColumnNumber = columnNumber;
mFlags = flags;
mCategory = category;
mTimeStamp = JS_Now() / 1000;
mInnerWindowID = aInnerWindowID;
if (aInnerWindowID && NS_IsMainThread()) {
InitializeOnMainThread();
}
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::ToString(nsACString& /*UTF8*/ aResult)
{
static const char format0[] =
"[%s: \"%s\" {file: \"%s\" line: %d column: %d source: \"%s\"}]";
static const char format1[] =
"[%s: \"%s\" {file: \"%s\" line: %d}]";
static const char format2[] =
"[%s: \"%s\"]";
static const char error[] = "JavaScript Error";
static const char warning[] = "JavaScript Warning";
const char* severity = !(mFlags & JSREPORT_WARNING) ? error : warning;
char* temp;
char* tempMessage = nullptr;
char* tempSourceName = nullptr;
char* tempSourceLine = nullptr;
if (!mMessage.IsEmpty())
tempMessage = ToNewUTF8String(mMessage);
if (!mSourceName.IsEmpty())
// Use at most 512 characters from mSourceName.
tempSourceName = ToNewUTF8String(StringHead(mSourceName, 512));
if (!mSourceLine.IsEmpty())
// Use at most 512 characters from mSourceLine.
tempSourceLine = ToNewUTF8String(StringHead(mSourceLine, 512));
if (nullptr != tempSourceName && nullptr != tempSourceLine)
temp = JS_smprintf(format0,
severity,
tempMessage,
tempSourceName,
mLineNumber,
mColumnNumber,
tempSourceLine);
else if (!mSourceName.IsEmpty())
temp = JS_smprintf(format1,
severity,
tempMessage,
tempSourceName,
mLineNumber);
else
temp = JS_smprintf(format2,
severity,
tempMessage);
if (nullptr != tempMessage)
free(tempMessage);
if (nullptr != tempSourceName)
free(tempSourceName);
if (nullptr != tempSourceLine)
free(tempSourceLine);
if (!temp)
return NS_ERROR_OUT_OF_MEMORY;
aResult.Assign(temp);
JS_smprintf_free(temp);
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetOuterWindowID(uint64_t* aOuterWindowID)
{
NS_WARNING_ASSERTION(NS_IsMainThread() || mInitializedOnMainThread,
"This can't be safely determined off the main thread, "
"returning an inaccurate value!");
if (!mInitializedOnMainThread && NS_IsMainThread()) {
InitializeOnMainThread();
}
*aOuterWindowID = mOuterWindowID;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetInnerWindowID(uint64_t* aInnerWindowID)
{
*aInnerWindowID = mInnerWindowID;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetTimeStamp(int64_t* aTimeStamp)
{
*aTimeStamp = mTimeStamp;
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorBase::GetIsFromPrivateWindow(bool* aIsFromPrivateWindow)
{
NS_WARNING_ASSERTION(NS_IsMainThread() || mInitializedOnMainThread,
"This can't be safely determined off the main thread, "
"returning an inaccurate value!");
if (!mInitializedOnMainThread && NS_IsMainThread()) {
InitializeOnMainThread();
}
*aIsFromPrivateWindow = mIsFromPrivateWindow;
return NS_OK;
}
NS_IMPL_ISUPPORTS(nsScriptError, nsIConsoleMessage, nsIScriptError)

View file

@ -1,118 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
/* 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/. */
/*
* nsScriptErrorWithStack implementation.
* a main-thread-only, cycle-collected subclass of nsScriptErrorBase
* that can store a SavedFrame stack trace object.
*/
#include "xpcprivate.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "nsGlobalWindow.h"
#include "nsCycleCollectionParticipant.h"
namespace {
static nsCString
FormatStackString(JSContext* cx, HandleObject aStack) {
JS::RootedString formattedStack(cx);
if (!JS::BuildStackString(cx, aStack, &formattedStack)) {
return nsCString();
}
nsAutoJSString stackJSString;
if (!stackJSString.init(cx, formattedStack)) {
return nsCString();
}
return NS_ConvertUTF16toUTF8(stackJSString.get());
}
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsScriptErrorWithStack)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsScriptErrorWithStack)
tmp->mStack = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsScriptErrorWithStack)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsScriptErrorWithStack)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsScriptErrorWithStack)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsScriptErrorWithStack)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsScriptErrorWithStack)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_ENTRY(nsIConsoleMessage)
NS_INTERFACE_MAP_ENTRY(nsIScriptError)
NS_INTERFACE_MAP_END
nsScriptErrorWithStack::nsScriptErrorWithStack(JS::HandleObject aStack)
: mStack(aStack)
{
MOZ_ASSERT(NS_IsMainThread(), "You can't use this class on workers.");
mozilla::HoldJSObjects(this);
}
nsScriptErrorWithStack::~nsScriptErrorWithStack() {
mozilla::DropJSObjects(this);
}
NS_IMETHODIMP
nsScriptErrorWithStack::Init(const nsAString& message,
const nsAString& sourceName,
const nsAString& sourceLine,
uint32_t lineNumber,
uint32_t columnNumber,
uint32_t flags,
const char* category)
{
MOZ_CRASH("nsScriptErrorWithStack requires to be initialized with a document, by using InitWithWindowID");
}
NS_IMETHODIMP
nsScriptErrorWithStack::GetStack(JS::MutableHandleValue aStack) {
aStack.setObjectOrNull(mStack);
return NS_OK;
}
NS_IMETHODIMP
nsScriptErrorWithStack::ToString(nsACString& /*UTF8*/ aResult)
{
MOZ_ASSERT(NS_IsMainThread());
nsCString message;
nsresult rv = nsScriptErrorBase::ToString(message);
NS_ENSURE_SUCCESS(rv, rv);
if (!mStack) {
aResult.Assign(message);
return NS_OK;
}
AutoJSAPI jsapi;
if (!jsapi.Init(mStack)) {
return NS_ERROR_FAILURE;
}
JSContext* cx = jsapi.cx();
RootedObject stack(cx, mStack);
nsCString stackString = FormatStackString(cx, stack);
nsCString combined = message + NS_LITERAL_CSTRING("\n") + stackString;
aResult.Assign(combined);
return NS_OK;
}

View file

@ -33,7 +33,9 @@
#include "nsIObjectOutputStream.h"
#include "nsScriptSecurityManager.h"
#include "nsIPermissionManager.h"
#include "nsIScriptError.h"
#include "nsContentUtils.h"
#include "nsScriptError.h"
#include "jsfriendapi.h"
using namespace mozilla;

View file

@ -128,7 +128,6 @@
#include "MainThreadUtils.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIException.h"
#include "nsVariant.h"
@ -2552,77 +2551,6 @@ extern char*
xpc_PrintJSStack(JSContext* cx, bool showArgs, bool showLocals,
bool showThisProps);
/***************************************************************************/
// Definition of nsScriptError, defined here because we lack a place to put
// XPCOM objects associated with the JavaScript engine.
class nsScriptErrorBase : public nsIScriptError {
public:
nsScriptErrorBase();
// TODO - do something reasonable on getting null from these babies.
NS_DECL_NSICONSOLEMESSAGE
NS_DECL_NSISCRIPTERROR
protected:
virtual ~nsScriptErrorBase();
void
InitializeOnMainThread();
nsString mMessage;
nsString mMessageName;
nsString mSourceName;
uint32_t mLineNumber;
nsString mSourceLine;
uint32_t mColumnNumber;
uint32_t mFlags;
nsCString mCategory;
// mOuterWindowID is set on the main thread from InitializeOnMainThread().
uint64_t mOuterWindowID;
uint64_t mInnerWindowID;
int64_t mTimeStamp;
// mInitializedOnMainThread and mIsFromPrivateWindow are set on the main
// thread from InitializeOnMainThread().
mozilla::Atomic<bool> mInitializedOnMainThread;
bool mIsFromPrivateWindow;
};
class nsScriptError final : public nsScriptErrorBase {
public:
nsScriptError() {}
NS_DECL_THREADSAFE_ISUPPORTS
private:
virtual ~nsScriptError() {}
};
class nsScriptErrorWithStack : public nsScriptErrorBase {
public:
explicit nsScriptErrorWithStack(JS::HandleObject);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsScriptErrorWithStack)
NS_IMETHOD Init(const nsAString& message,
const nsAString& sourceName,
const nsAString& sourceLine,
uint32_t lineNumber,
uint32_t columnNumber,
uint32_t flags,
const char* category) override;
NS_IMETHOD GetStack(JS::MutableHandleValue) override;
NS_IMETHOD ToString(nsACString& aResult) override;
private:
virtual ~nsScriptErrorWithStack();
// Complete stackframe where the error happened.
// Must be SavedFrame object.
JS::Heap<JSObject*> mStack;
};
/******************************************************************************
* Handles pre/post script processing.
*/