Skip to content

Commit

Permalink
Bug 1722448 - Implement self.reportError(). r=emilio,smaug
Browse files Browse the repository at this point in the history
This is mostly just copying and adjusting code from `AutoJSAPI::ReportException`.

Differential Revision: https://phabricator.services.mozilla.com/D121070
  • Loading branch information
evilpie committed Aug 9, 2021
1 parent b015315 commit 4e3fc1e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 24 deletions.
25 changes: 25 additions & 0 deletions dom/base/nsGlobalWindowInner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4134,6 +4134,31 @@ void nsGlobalWindowInner::GetOrigin(nsAString& aOrigin) {
nsContentUtils::GetUTFOrigin(GetPrincipal(), aOrigin);
}

// See also AutoJSAPI::ReportException
void nsGlobalWindowInner::ReportError(JSContext* aCx,
JS::Handle<JS::Value> aError,
CallerType aCallerType,
ErrorResult& aRv) {
if (MOZ_UNLIKELY(!HasActiveDocument())) {
return aRv.Throw(NS_ERROR_XPC_SECURITY_MANAGER_VETO);
}

JS::ErrorReportBuilder jsReport(aCx);
JS::ExceptionStack exnStack(aCx, aError, nullptr);
if (!jsReport.init(aCx, exnStack, JS::ErrorReportBuilder::WithSideEffects)) {
return aRv.NoteJSContextException(aCx);
}

RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
bool isChrome = aCallerType == CallerType::System;
xpcReport->Init(jsReport.report(), jsReport.toStringResult().c_str(),
isChrome, WindowID());

JS::RootingContext* rcx = JS::RootingContext::get(aCx);
DispatchScriptErrorEvent(this, rcx, xpcReport, exnStack.exception(),
exnStack.stack());
}

void nsGlobalWindowInner::Atob(const nsAString& aAsciiBase64String,
nsAString& aBinaryData, ErrorResult& aError) {
aError = nsContentUtils::Atob(aAsciiBase64String, aBinaryData);
Expand Down
6 changes: 6 additions & 0 deletions dom/base/nsGlobalWindowInner.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,12 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget,
MOZ_CAN_RUN_SCRIPT
void ClearInterval(int32_t aHandle);
void GetOrigin(nsAString& aOrigin);

MOZ_CAN_RUN_SCRIPT
void ReportError(JSContext* aCx, JS::Handle<JS::Value> aError,
mozilla::dom::CallerType aCallerType,
mozilla::ErrorResult& aRv);

void Atob(const nsAString& aAsciiBase64String, nsAString& aBinaryData,
mozilla::ErrorResult& aError);
void Btoa(const nsAString& aBinaryData, nsAString& aAsciiBase64String,
Expand Down
3 changes: 3 additions & 0 deletions dom/webidl/WindowOrWorkerGlobalScope.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ interface mixin WindowOrWorkerGlobalScope {
[Replaceable] readonly attribute USVString origin;
readonly attribute boolean crossOriginIsolated;

[Throws, NeedsCallerType]
void reportError(any e);

// base64 utility methods
[Throws]
DOMString btoa(DOMString btoa);
Expand Down
1 change: 1 addition & 0 deletions dom/workers/WorkerError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ void WorkerErrorReport::ReportError(
init.mMessage = aReport->mMessage;
init.mFilename = aReport->mFilename;
init.mLineno = aReport->mLineNumber;
init.mColno = aReport->mColumnNumber;
init.mError = aException;
}

Expand Down
20 changes: 20 additions & 0 deletions dom/workers/WorkerScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ nsISerialEventTarget* WorkerGlobalScopeBase::EventTargetFor(
return mSerialEventTarget;
}

// See also AutoJSAPI::ReportException
void WorkerGlobalScopeBase::ReportError(JSContext* aCx,
JS::Handle<JS::Value> aError,
CallerType, ErrorResult& aRv) {
JS::ErrorReportBuilder jsReport(aCx);
JS::ExceptionStack exnStack(aCx, aError, nullptr);
if (!jsReport.init(aCx, exnStack, JS::ErrorReportBuilder::WithSideEffects)) {
return aRv.NoteJSContextException(aCx);
}

// Before invoking ReportError, put the exception back on the context,
// because it may want to put it in its error events and has no other way
// to get hold of it. After we invoke ReportError, clear the exception on
// cx(), just in case ReportError didn't.
JS::SetPendingExceptionStack(aCx, exnStack);
mWorkerPrivate->ReportError(aCx, jsReport.toStringResult(),
jsReport.report());
JS_ClearPendingException(aCx);
}

void WorkerGlobalScopeBase::Atob(const nsAString& aAtob, nsAString& aOut,
ErrorResult& aRv) const {
mWorkerPrivate->AssertIsOnWorkerThread();
Expand Down
4 changes: 4 additions & 0 deletions dom/workers/WorkerScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class WorkerGlobalScopeBase : public DOMEventTargetHelper,
MOZ_CRASH("AbstractMainThreadFor not supported for workers.");
}

MOZ_CAN_RUN_SCRIPT
void ReportError(JSContext* aCx, JS::Handle<JS::Value> aError,
CallerType aCallerType, ErrorResult& aRv);

// atob, btoa, and dump are declared (separately) by both WorkerGlobalScope
// and WorkerDebuggerGlobalScope WebIDL interfaces
void Atob(const nsAString& aAtob, nsAString& aOut, ErrorResult& aRv) const;
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 4e3fc1e

Please sign in to comment.