Bug 1409852 - nsContentUtils::ExtractErrorValues with nsAString;r?bz draft
authorDavid Teller <dteller@mozilla.com>
Tue, 12 Dec 2017 18:20:06 -0600
changeset 713999 67ab99a089fc65c520f96d5b4d154e5d504be92c
parent 713998 5b1fdaa14d35ddf1a638c9422786ede707cacf1f
child 714000 2ab7c46b2dd79b477372ea4d303604c61b8e95fd
push id93804
push userdteller@mozilla.com
push dateThu, 21 Dec 2017 12:36:34 +0000
reviewersbz
bugs1409852
milestone59.0a1
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
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
--- 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);