Bug 1409852 - nsContentUtils::ExtractErrorValues with nsAString;r?bz
nsContentUtils::ExtractErrorValues converts `aSourceSpecOut` from
UTF-16 to UTF-8, which is generally not what we want. This patch
introduces a new version of ExtractErrorValues that doesn't perform
the conversion. To keep the patch short and avoid rewriting existing
clients, the existing version of the function is left in place.
MozReview-Commit-ID: J2NQb5ZCfht
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -10987,28 +10987,26 @@ nsContentUtils::IsOverridingWindowName(c
#define EXTRACT_EXN_VALUES(T, ...) \
ExtractExceptionValues<mozilla::dom::prototypes::id::T, \
T##Binding::NativeType, T>(__VA_ARGS__).isOk()
template <prototypes::ID PrototypeID, class NativeType, typename T>
static Result<Ok, nsresult>
ExtractExceptionValues(JSContext* aCx,
JS::HandleObject aObj,
- nsACString& aSourceSpecOut,
+ nsAString& aSourceSpecOut,
uint32_t* aLineOut,
uint32_t* aColumnOut,
nsString& aMessageOut)
{
RefPtr<T> exn;
MOZ_TRY((UnwrapObject<PrototypeID, NativeType>(aObj, exn)));
- nsAutoString filename;
- exn->GetFilename(aCx, filename);
- if (!filename.IsEmpty()) {
- CopyUTF16toUTF8(filename, aSourceSpecOut);
+ exn->GetFilename(aCx, aSourceSpecOut);
+ if (!aSourceSpecOut.IsEmpty()) {
*aLineOut = exn->LineNumber(aCx);
*aColumnOut = exn->ColumnNumber();
}
exn->GetName(aMessageOut);
aMessageOut.AppendLiteral(": ");
nsAutoString message;
@@ -11020,16 +11018,29 @@ ExtractExceptionValues(JSContext* aCx,
/* static */ void
nsContentUtils::ExtractErrorValues(JSContext* aCx,
JS::Handle<JS::Value> aValue,
nsACString& aSourceSpecOut,
uint32_t* aLineOut,
uint32_t* aColumnOut,
nsString& aMessageOut)
{
+ nsAutoString sourceSpec;
+ ExtractErrorValues(aCx, aValue, sourceSpec, aLineOut, aColumnOut, aMessageOut);
+ CopyUTF16toUTF8(sourceSpec, aSourceSpecOut);
+}
+
+/* static */ void
+nsContentUtils::ExtractErrorValues(JSContext* aCx,
+ JS::Handle<JS::Value> aValue,
+ nsAString& aSourceSpecOut,
+ uint32_t* aLineOut,
+ uint32_t* aColumnOut,
+ nsString& aMessageOut)
+{
MOZ_ASSERT(aLineOut);
MOZ_ASSERT(aColumnOut);
if (aValue.isObject()) {
JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
// Try to process as an Error object. Use the file/line/column values
// from the Error as they will be more specific to the root cause of
@@ -11040,17 +11051,17 @@ nsContentUtils::ExtractErrorValues(JSCon
// this report anywhere.
RefPtr<xpc::ErrorReport> report = new xpc::ErrorReport();
report->Init(err,
"<unknown>", // toString result
false, // chrome
0); // window ID
if (!report->mFileName.IsEmpty()) {
- CopyUTF16toUTF8(report->mFileName, aSourceSpecOut);
+ aSourceSpecOut = report->mFileName;
*aLineOut = report->mLineNumber;
*aColumnOut = report->mColumn;
}
aMessageOut.Assign(report->mErrorMsg);
}
// Next, try to unwrap the rejection value as a DOMException.
else if (EXTRACT_EXN_VALUES(DOMException, aCx, obj, aSourceSpecOut,
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -1131,16 +1131,24 @@ public:
* Helper function that generates a UUID.
*/
static nsresult GenerateUUIDInPlace(nsID& aUUID);
static bool PrefetchPreloadEnabled(nsIDocShell* aDocShell);
static void
ExtractErrorValues(JSContext* aCx, JS::Handle<JS::Value> aValue,
+ nsAString& aSourceSpecOut, uint32_t *aLineOut,
+ uint32_t *aColumnOut, nsString& aMessageOut);
+
+ // Variant on `ExtractErrorValues` with a `nsACString`. This
+ // method is provided for backwards compatibility. Prefer the
+ // faster method above for your code.
+ static void
+ ExtractErrorValues(JSContext* aCx, JS::Handle<JS::Value> aValue,
nsACString& aSourceSpecOut, uint32_t *aLineOut,
uint32_t *aColumnOut, nsString& aMessageOut);
/**
* Helper function to tell if user ever enabled DevTools explicitely.
* Allows making DevTools related API no-op until user do so.
*/
static bool DevToolsEnabled(JSContext* aCx);