Bug 1518491 - Reduce the amount of UTF encodings we do when reporting content blocking events; r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 10 Jan 2019 13:36:52 -0500
changeset 453711 a34a6c26b080
parent 453710 1957ef91d750
child 453712 500d91e30e9d
push id111134
push usereakhgari@mozilla.com
push dateMon, 14 Jan 2019 14:36:33 +0000
treeherdermozilla-inbound@a34a6c26b080 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1518491
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1518491 - Reduce the amount of UTF encodings we do when reporting content blocking events; r=baku Differential Revision: https://phabricator.services.mozilla.com/D16217
docshell/base/nsDocShell.cpp
dom/base/ContentBlockingLog.h
dom/base/Document.h
dom/base/nsGlobalWindowOuter.cpp
dom/ipc/PBrowser.ipdl
dom/ipc/TabChild.cpp
dom/ipc/TabParent.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -3662,17 +3662,18 @@ nsDocShell::GetContentBlockingLog(Promis
   }
 
   Document* doc = mContentViewer->GetDocument();
   ErrorResult rv;
   RefPtr<Promise> promise = Promise::Create(doc->GetOwnerGlobal(), rv);
   if (NS_WARN_IF(rv.Failed())) {
     return rv.StealNSResult();
   }
-  promise->MaybeResolve(doc->GetContentBlockingLog()->Stringify());
+  promise->MaybeResolve(
+      NS_ConvertUTF8toUTF16(doc->GetContentBlockingLog()->Stringify()));
   promise.forget(aPromise);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDocShell::SetDeviceSizeIsPageSize(bool aValue) {
   if (mDeviceSizeIsPageSize != aValue) {
     mDeviceSizeIsPageSize = aValue;
--- a/dom/base/ContentBlockingLog.h
+++ b/dom/base/ContentBlockingLog.h
@@ -25,46 +25,44 @@ class ContentBlockingLog final {
     uint32_t mRepeatCount;
     bool mBlocked;
   };
 
   // Each element is a tuple of (type, blocked, repeatCount). The type values
   // come from the blocking types defined in nsIWebProgressListener.
   typedef nsTArray<LogEntry> OriginLog;
   typedef Tuple<bool, Maybe<bool>, OriginLog> OriginData;
-  typedef nsTArray<Tuple<nsString, UniquePtr<OriginData>>> OriginDataTable;
+  typedef nsTArray<Tuple<nsCString, UniquePtr<OriginData>>> OriginDataTable;
 
   struct StringWriteFunc : public JSONWriteFunc {
-    nsAString&
+    nsACString&
         mBuffer;  // The lifetime of the struct must be bound to the buffer
-    explicit StringWriteFunc(nsAString& aBuffer) : mBuffer(aBuffer) {}
+    explicit StringWriteFunc(nsACString& aBuffer) : mBuffer(aBuffer) {}
 
-    void Write(const char* aStr) override {
-      mBuffer.Append(NS_ConvertUTF8toUTF16(aStr));
-    }
+    void Write(const char* aStr) override { mBuffer.Append(aStr); }
   };
 
   struct Comparator {
    public:
     bool Equals(const OriginDataTable::elem_type& aLeft,
                 const OriginDataTable::elem_type& aRight) const {
       return Get<0>(aLeft).Equals(Get<0>(aRight));
     }
 
     bool Equals(const OriginDataTable::elem_type& aLeft,
-                const nsAString& aRight) const {
+                const nsACString& aRight) const {
       return Get<0>(aLeft).Equals(aRight);
     }
   };
 
  public:
   ContentBlockingLog() = default;
   ~ContentBlockingLog() = default;
 
-  void RecordLog(const nsAString& aOrigin, uint32_t aType, bool aBlocked) {
+  void RecordLog(const nsACString& aOrigin, uint32_t aType, bool aBlocked) {
     if (aOrigin.IsVoid()) {
       return;
     }
     auto index = mLog.IndexOf(aOrigin, 0, Comparator());
     if (index != OriginDataTable::NoIndex) {
       auto& data = Get<1>(mLog[index]);
       if (aType == nsIWebProgressListener::STATE_LOADED_TRACKING_CONTENT) {
         Get<0>(*data) = aBlocked;
@@ -102,39 +100,37 @@ class ContentBlockingLog final {
         if (Get<1>(*data).isSome()) {
           Get<1>(*data).ref() = aBlocked;
         } else {
           Get<1>(*data).emplace(aBlocked);
         }
       } else {
         Get<2>(*data).AppendElement(LogEntry{aType, 1u, aBlocked});
       }
-      nsAutoString origin(aOrigin);
-      mLog.AppendElement(Tuple<nsString, UniquePtr<OriginData>>(
+      nsAutoCString origin(aOrigin);
+      mLog.AppendElement(Tuple<nsCString, UniquePtr<OriginData>>(
           std::move(origin), std::move(data)));
     }
   }
 
-  nsAutoString Stringify() {
-    nsAutoString buffer;
+  nsAutoCString Stringify() {
+    nsAutoCString buffer;
 
     JSONWriter w(MakeUnique<StringWriteFunc>(buffer));
     w.Start();
 
     const auto end = mLog.end();
     for (auto iter = mLog.begin(); iter != end; ++iter) {
       if (!Get<1>(*iter)) {
-        w.StartArrayProperty(NS_ConvertUTF16toUTF8(Get<0>(*iter)).get(),
-                             w.SingleLineStyle);
+        w.StartArrayProperty(Get<0>(*iter).get(), w.SingleLineStyle);
         w.EndArray();
         continue;
       }
 
-      w.StartArrayProperty(NS_ConvertUTF16toUTF8(Get<0>(*iter)).get(),
-                           w.SingleLineStyle);
+      w.StartArrayProperty(Get<0>(*iter).get(), w.SingleLineStyle);
       auto& data = Get<1>(*iter);
       if (Get<0>(*data)) {
         w.StartArrayElement(w.SingleLineStyle);
         {
           w.IntElement(nsIWebProgressListener::STATE_LOADED_TRACKING_CONTENT);
           w.BoolElement(true);  // blocked
           w.IntElement(1);      // repeat count
         }
--- a/dom/base/Document.h
+++ b/dom/base/Document.h
@@ -1014,68 +1014,68 @@ class Document : public nsINode,
     return mContentBlockingLog.HasBlockedAnyOfType(
         nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION);
   }
 
   /**
    * Set the tracking content blocked flag for this document.
    */
   void SetHasTrackingContentBlocked(bool aHasTrackingContentBlocked,
-                                    const nsAString& aOriginBlocked) {
+                                    const nsACString& aOriginBlocked) {
     RecordContentBlockingLog(
         aOriginBlocked, nsIWebProgressListener::STATE_BLOCKED_TRACKING_CONTENT,
         aHasTrackingContentBlocked);
   }
 
   /**
    * Set the all cookies blocked flag for this document.
    */
   void SetHasAllCookiesBlocked(bool aHasAllCookiesBlocked,
-                               const nsAString& aOriginBlocked) {
+                               const nsACString& aOriginBlocked) {
     RecordContentBlockingLog(aOriginBlocked,
                              nsIWebProgressListener::STATE_COOKIES_BLOCKED_ALL,
                              aHasAllCookiesBlocked);
   }
 
   /**
    * Set the tracking cookies blocked flag for this document.
    */
   void SetHasTrackingCookiesBlocked(bool aHasTrackingCookiesBlocked,
-                                    const nsAString& aOriginBlocked) {
+                                    const nsACString& aOriginBlocked) {
     RecordContentBlockingLog(
         aOriginBlocked, nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER,
         aHasTrackingCookiesBlocked);
   }
 
   /**
    * Set the third-party cookies blocked flag for this document.
    */
   void SetHasForeignCookiesBlocked(bool aHasForeignCookiesBlocked,
-                                   const nsAString& aOriginBlocked) {
+                                   const nsACString& aOriginBlocked) {
     RecordContentBlockingLog(
         aOriginBlocked, nsIWebProgressListener::STATE_COOKIES_BLOCKED_FOREIGN,
         aHasForeignCookiesBlocked);
   }
 
   /**
    * Set the cookies blocked by site permission flag for this document.
    */
   void SetHasCookiesBlockedByPermission(bool aHasCookiesBlockedByPermission,
-                                        const nsAString& aOriginBlocked) {
+                                        const nsACString& aOriginBlocked) {
     RecordContentBlockingLog(
         aOriginBlocked,
         nsIWebProgressListener::STATE_COOKIES_BLOCKED_BY_PERMISSION,
         aHasCookiesBlockedByPermission);
   }
 
   /**
    * Set the cookies loaded flag for this document.
    */
   void SetHasCookiesLoaded(bool aHasCookiesLoaded,
-                           const nsAString& aOriginLoaded) {
+                           const nsACString& aOriginLoaded) {
     RecordContentBlockingLog(aOriginLoaded,
                              nsIWebProgressListener::STATE_COOKIES_LOADED,
                              aHasCookiesLoaded);
   }
 
   /**
    * Get cookies loaded flag for this document.
    */
@@ -1091,17 +1091,17 @@ class Document : public nsINode,
     return mContentBlockingLog.HasBlockedAnyOfType(
         nsIWebProgressListener::STATE_LOADED_TRACKING_CONTENT);
   }
 
   /**
    * Set the tracking content loaded flag for this document.
    */
   void SetHasTrackingContentLoaded(bool aHasTrackingContentLoaded,
-                                   const nsAString& aOriginBlocked) {
+                                   const nsACString& aOriginBlocked) {
     RecordContentBlockingLog(
         aOriginBlocked, nsIWebProgressListener::STATE_LOADED_TRACKING_CONTENT,
         aHasTrackingContentLoaded);
   }
 
   /**
    * Get the sandbox flags for this document.
    * @see nsSandboxFlags.h for the possible flags
@@ -3576,17 +3576,17 @@ class Document : public nsINode,
   void NotifyStyleSheetApplicableStateChanged();
   // Just like EnableStyleSheetsForSet, but doesn't check whether
   // aSheetSet is null and allows the caller to control whether to set
   // aSheetSet as the preferred set in the CSSLoader.
   void EnableStyleSheetsForSetInternal(const nsAString& aSheetSet,
                                        bool aUpdateCSSLoader);
 
  private:
-  void RecordContentBlockingLog(const nsAString& aOrigin, uint32_t aType,
+  void RecordContentBlockingLog(const nsACString& aOrigin, uint32_t aType,
                                 bool aBlocked) {
     mContentBlockingLog.RecordLog(aOrigin, aType, aBlocked);
   }
 
   mutable std::bitset<eDeprecatedOperationCount> mDeprecationWarnedAbout;
   mutable std::bitset<eDocumentWarningCount> mDocWarningWarnedAbout;
 
   // Lazy-initialization to have mDocGroup initialized in prior to the
--- a/dom/base/nsGlobalWindowOuter.cpp
+++ b/dom/base/nsGlobalWindowOuter.cpp
@@ -5032,18 +5032,18 @@ void nsGlobalWindowOuter::NotifyContentB
   NS_ENSURE_SUCCESS_VOID(rv);
   uint32_t state = 0;
   nsCOMPtr<nsISecureBrowserUI> securityUI;
   docShell->GetSecurityUI(getter_AddRefs(securityUI));
   if (!securityUI) {
     return;
   }
   securityUI->GetState(&state);
-  nsAutoString origin;
-  nsContentUtils::GetUTFOrigin(aURIHint, origin);
+  nsAutoCString origin;
+  nsContentUtils::GetASCIIOrigin(aURIHint, origin);
 
   bool blockedValue = aBlocked;
   bool unblocked = false;
   if (aState == nsIWebProgressListener::STATE_BLOCKED_TRACKING_CONTENT) {
     doc->SetHasTrackingContentBlocked(aBlocked, origin);
     if (!aBlocked) {
       unblocked = !doc->GetHasTrackingContentBlocked();
     }
--- a/dom/ipc/PBrowser.ipdl
+++ b/dom/ipc/PBrowser.ipdl
@@ -898,17 +898,17 @@ child:
      * Pass the current handle for the current native widget to the content
      * process, so it can be used by PuppetWidget.
      */
     async SetWidgetNativeData(WindowsHandle aHandle);
 
     /**
      * Requests the content blocking log, which is sent back in response.
      */
-    async GetContentBlockingLog() returns(nsString log, bool success);
+    async GetContentBlockingLog() returns(nsCString log, bool success);
 
 parent:
     /** Records a history visit. */
     async VisitURI(URIParams aURI, OptionalURIParams aLastVisitedURI,
                    uint32_t aFlags);
 
     /** Fetches the visited status for an array of URIs (Android-only). */
     async QueryVisitedState(URIParams[] aURIs);
--- a/dom/ipc/TabChild.cpp
+++ b/dom/ipc/TabChild.cpp
@@ -3077,24 +3077,24 @@ mozilla::ipc::IPCResult TabChild::RecvSe
     const WindowsHandle& aWidgetNativeData) {
   mWidgetNativeData = aWidgetNativeData;
   return IPC_OK();
 }
 
 mozilla::ipc::IPCResult TabChild::RecvGetContentBlockingLog(
     GetContentBlockingLogResolver&& aResolve) {
   bool success = false;
-  nsAutoString result;
+  nsAutoCString result;
 
   if (nsCOMPtr<Document> doc = GetDocument()) {
     result = doc->GetContentBlockingLog()->Stringify();
     success = true;
   }
 
-  aResolve(Tuple<const nsString&, const bool&>(result, success));
+  aResolve(Tuple<const nsCString&, const bool&>(result, success));
   return IPC_OK();
 }
 
 mozilla::plugins::PPluginWidgetChild* TabChild::AllocPPluginWidgetChild() {
 #ifdef XP_WIN
   return new mozilla::plugins::PluginWidgetChild();
 #else
   MOZ_ASSERT_UNREACHABLE("AllocPPluginWidgetChild only supports Windows");
--- a/dom/ipc/TabParent.cpp
+++ b/dom/ipc/TabParent.cpp
@@ -2711,19 +2711,20 @@ TabParent::GetContentBlockingLog(Promise
     return NS_ERROR_FAILURE;
   }
 
   RefPtr<Promise> copy(jsPromise);
   copy.forget(aPromise);
 
   auto cblPromise = SendGetContentBlockingLog();
   cblPromise->Then(GetMainThreadSerialEventTarget(), __func__,
-                   [jsPromise](Tuple<nsString, bool>&& aResult) {
+                   [jsPromise](Tuple<nsCString, bool>&& aResult) {
                      if (Get<1>(aResult)) {
-                       jsPromise->MaybeResolve(std::move(Get<0>(aResult)));
+                       NS_ConvertUTF8toUTF16 utf16(Get<0>(aResult));
+                       jsPromise->MaybeResolve(std::move(utf16));
                      } else {
                        jsPromise->MaybeRejectWithUndefined();
                      }
                    },
                    [jsPromise](ResponseRejectReason&& aReason) {
                      jsPromise->MaybeRejectWithUndefined();
                    });