Bug 1519205 - Reduce the amount of UTF encodings we do when checking content blocking rules; r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 14 Jan 2019 08:24:41 +0000
changeset 453723 8c6e216e6788b2e81b979e3447b47b5134bc8024
parent 453722 d51aeac358e707997c7f63277ee7ee0b06713d1c
child 453724 b1cfe0ca416e9a1a8208ccaae1787754172b53ea
push id35372
push usercbrindusan@mozilla.com
push dateMon, 14 Jan 2019 21:49:33 +0000
treeherdermozilla-central@50b3268954b1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1519205
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 1519205 - Reduce the amount of UTF encodings we do when checking content blocking rules; r=baku Differential Revision: https://phabricator.services.mozilla.com/D16233
toolkit/components/antitracking/AntiTrackingCommon.cpp
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -489,25 +489,25 @@ AntiTrackingCommon::AddFirstPartyStorage
 
   nsCOMPtr<nsIURI> uri;
   aPrincipal->GetURI(getter_AddRefs(uri));
   if (NS_WARN_IF(!uri)) {
     LOG(("Can't get the URI from the principal"));
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
-  nsAutoString origin;
-  nsresult rv = nsContentUtils::GetUTFOrigin(uri, origin);
+  nsAutoCString origin;
+  nsresult rv = nsContentUtils::GetASCIIOrigin(uri, origin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG(("Can't get the origin from the URI"));
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
   LOG(("Adding a first-party storage exception for %s...",
-       NS_ConvertUTF16toUTF8(origin).get()));
+       PromiseFlatCString(origin).get()));
 
   if (StaticPrefs::network_cookie_cookieBehavior() !=
       nsICookieService::BEHAVIOR_REJECT_TRACKER) {
     LOG(
         ("Disabled by network.cookie.cookieBehavior pref (%d), bailing out "
          "early",
          StaticPrefs::network_cookie_cookieBehavior()));
     return StorageAccessGrantPromise::CreateAndResolve(true, __func__);
@@ -531,17 +531,17 @@ AntiTrackingCommon::AddFirstPartyStorage
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
   LOG(("The current resource is %s-party",
        outerParentWindow->IsTopLevelWindow() ? "first" : "third"));
 
   // We are a first party resource.
   if (outerParentWindow->IsTopLevelWindow()) {
-    CopyUTF16toUTF8(origin, trackingOrigin);
+    trackingOrigin = origin;
     trackingPrincipal = aPrincipal;
     rv = trackingPrincipal->GetURI(getter_AddRefs(trackingURI));
     if (NS_WARN_IF(NS_FAILED(rv))) {
       LOG(("Couldn't get the tracking principal URI"));
       return StorageAccessGrantPromise::CreateAndReject(false, __func__);
     }
     topLevelStoragePrincipal = parentWindow->GetPrincipal();
     if (NS_WARN_IF(!topLevelStoragePrincipal)) {
@@ -606,42 +606,40 @@ AntiTrackingCommon::AddFirstPartyStorage
     LOG(("Couldn't get the top window"));
     return StorageAccessGrantPromise::CreateAndReject(false, __func__);
   }
 
   auto storePermission =
       [pwin, parentWindow, origin, trackingOrigin, trackingPrincipal,
        trackingURI, topInnerWindow, topLevelStoragePrincipal,
        aReason](int aAllowMode) -> RefPtr<StorageAccessGrantPromise> {
-    NS_ConvertUTF16toUTF8 grantedOrigin(origin);
-
     nsAutoCString permissionKey;
-    CreatePermissionKey(trackingOrigin, grantedOrigin, permissionKey);
+    CreatePermissionKey(trackingOrigin, origin, permissionKey);
 
     // Let's store the permission in the current parent window.
     topInnerWindow->SaveStorageAccessGranted(permissionKey);
 
     // Let's inform the parent window.
     parentWindow->StorageAccessGranted();
 
     nsIChannel* channel =
         pwin->GetCurrentInnerWindow()->GetExtantDoc()->GetChannel();
 
     pwin->NotifyContentBlockingState(blockReason, channel, false, trackingURI);
 
     ReportUnblockingConsole(parentWindow, NS_ConvertUTF8toUTF16(trackingOrigin),
-                            origin, aReason);
+                            NS_ConvertUTF8toUTF16(origin), aReason);
 
     if (XRE_IsParentProcess()) {
       LOG(("Saving the permission: trackingOrigin=%s, grantedOrigin=%s",
-           trackingOrigin.get(), grantedOrigin.get()));
+           trackingOrigin.get(), origin.get()));
 
       return SaveFirstPartyStorageAccessGrantedForOriginOnParentProcess(
                  topLevelStoragePrincipal, trackingPrincipal, trackingOrigin,
-                 grantedOrigin, aAllowMode)
+                 origin, aAllowMode)
           ->Then(GetCurrentThreadSerialEventTarget(), __func__,
                  [](FirstPartyStorageAccessGrantPromise::ResolveOrRejectValue&&
                         aValue) {
                    if (aValue.IsResolve()) {
                      return StorageAccessGrantPromise::CreateAndResolve(
                          NS_SUCCEEDED(aValue.ResolveValue()) ? eAllowOnAnySite
                                                              : eAllow,
                          __func__);
@@ -652,24 +650,24 @@ AntiTrackingCommon::AddFirstPartyStorage
     }
 
     ContentChild* cc = ContentChild::GetSingleton();
     MOZ_ASSERT(cc);
 
     LOG(
         ("Asking the parent process to save the permission for us: "
          "trackingOrigin=%s, grantedOrigin=%s",
-         trackingOrigin.get(), grantedOrigin.get()));
+         trackingOrigin.get(), origin.get()));
 
     // This is not really secure, because here we have the content process
     // sending the request of storing a permission.
     return cc
         ->SendFirstPartyStorageAccessGrantedForOrigin(
             IPC::Principal(topLevelStoragePrincipal),
-            IPC::Principal(trackingPrincipal), trackingOrigin, grantedOrigin,
+            IPC::Principal(trackingPrincipal), trackingOrigin, origin,
             aAllowMode)
         ->Then(GetCurrentThreadSerialEventTarget(), __func__,
                [](const ContentChild::
                       FirstPartyStorageAccessGrantedForOriginPromise::
                           ResolveOrRejectValue& aValue) {
                  if (aValue.IsResolve()) {
                    return StorageAccessGrantPromise::CreateAndResolve(
                        aValue.ResolveValue(), __func__);
@@ -949,25 +947,23 @@ bool AntiTrackingCommon::IsFirstPartySto
           nsGlobalWindowInner::Cast(aWindow), getter_AddRefs(parentPrincipal),
           trackingOrigin, getter_AddRefs(trackingURI), nullptr)) {
     LOG(("Failed to obtain the parent principal and the tracking origin"));
     *aRejectedReason = nsIWebProgressListener::STATE_COOKIES_BLOCKED_TRACKER;
     return false;
   }
   Unused << parentPrincipal->GetURI(getter_AddRefs(parentPrincipalURI));
 
-  nsAutoString origin;
-  nsresult rv = nsContentUtils::GetUTFOrigin(aURI, origin);
+  nsAutoCString grantedOrigin;
+  nsresult rv = nsContentUtils::GetASCIIOrigin(aURI, grantedOrigin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG_SPEC(("Failed to compute the origin from %s", _spec), aURI);
     return false;
   }
 
-  NS_ConvertUTF16toUTF8 grantedOrigin(origin);
-
   nsGlobalWindowOuter* outerWindow =
       nsGlobalWindowOuter::Cast(aWindow->GetOuterWindow());
   if (NS_WARN_IF(!outerWindow)) {
     LOG(("No outer window."));
     return false;
   }
 
   nsCOMPtr<nsPIDOMWindowOuter> topOuterWindow = outerWindow->GetTop();
@@ -1216,33 +1212,32 @@ bool AntiTrackingCommon::IsFirstPartySto
 
   nsCOMPtr<nsIURI> trackingURI;
   rv = aChannel->GetURI(getter_AddRefs(trackingURI));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG(("Failed to get the channel URI"));
     return true;
   }
 
-  nsAutoString trackingOrigin;
-  rv = nsContentUtils::GetUTFOrigin(trackingURI, trackingOrigin);
+  nsAutoCString trackingOrigin;
+  rv = nsContentUtils::GetASCIIOrigin(trackingURI, trackingOrigin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG_SPEC(("Failed to compute the origin from %s", _spec), trackingURI);
     return false;
   }
 
-  nsAutoString origin;
-  rv = nsContentUtils::GetUTFOrigin(aURI, origin);
+  nsAutoCString origin;
+  rv = nsContentUtils::GetASCIIOrigin(aURI, origin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG_SPEC(("Failed to compute the origin from %s", _spec), aURI);
     return false;
   }
 
   nsAutoCString type;
-  CreatePermissionKey(NS_ConvertUTF16toUTF8(trackingOrigin),
-                      NS_ConvertUTF16toUTF8(origin), type);
+  CreatePermissionKey(trackingOrigin, origin, type);
 
   nsCOMPtr<nsIPermissionManager> pm = services::GetPermissionManager();
   if (NS_WARN_IF(!pm)) {
     LOG(("Failed to obtain the permission manager"));
     return false;
   }
 
   uint32_t result = 0;
@@ -1319,27 +1314,25 @@ bool AntiTrackingCommon::IsFirstPartySto
     LOG(
         ("CheckCookiePermissionForPrincipal() returned a non-default access "
          "code (%d), returning %s",
          int(access),
          access != nsICookiePermission::ACCESS_DENY ? "success" : "failure"));
     return access != nsICookiePermission::ACCESS_DENY;
   }
 
-  nsAutoString origin;
-  nsresult rv = nsContentUtils::GetUTFOrigin(aURI, origin);
+  nsAutoCString origin;
+  nsresult rv = nsContentUtils::GetASCIIOrigin(aURI, origin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG_SPEC(("Failed to compute the origin from %s", _spec), aURI);
     return false;
   }
 
-  NS_ConvertUTF16toUTF8 utf8Origin(origin);
-
   nsAutoCString type;
-  CreatePermissionKey(utf8Origin, utf8Origin, type);
+  CreatePermissionKey(origin, origin, type);
 
   nsCOMPtr<nsIPermissionManager> pm = services::GetPermissionManager();
   if (NS_WARN_IF(!pm)) {
     LOG(("Failed to obtain the permission manager"));
     return false;
   }
 
   uint32_t result = 0;