Bug 1482117 - Part 2: cache the result of IsThirdPartyChannel; r=mayhemer
authorLiang-Heng Chen <xeonchen@gmail.com>
Fri, 17 Aug 2018 12:43:51 +0000
changeset 487218 32d94a3cc7afd8a09bf182d10932a0ee84407191
parent 487217 804d903755f6290f253e4526b1341ee4f5e2cab8
child 487219 92bb9503d1c3b8f5af908953908eac85be1473da
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1482117
milestone63.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 1482117 - Part 2: cache the result of IsThirdPartyChannel; r=mayhemer cache the result to speed up Differential Revision: https://phabricator.services.mozilla.com/D3423
netwerk/protocol/http/nsHttpChannel.cpp
netwerk/protocol/http/nsHttpChannel.h
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -625,27 +625,18 @@ nsHttpChannel::Connect()
     if (ShouldIntercept()) {
         return RedirectToInterceptedChannel();
     }
 
     bool isTrackingResource = mIsTrackingResource; // is atomic
     LOG(("nsHttpChannel %p tracking resource=%d, cos=%u",
           this, isTrackingResource, mClassOfService));
 
-    if (isTrackingResource) {
-        nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
-          services::GetThirdPartyUtil();
-        bool result = false;
-        if (thirdPartyUtil &&
-            NS_SUCCEEDED(thirdPartyUtil->IsThirdPartyChannel(this, nullptr,
-                                                             &result)) &&
-            result) {
-
-            AddClassFlags(nsIClassOfService::Tail);
-        }
+    if (isTrackingResource && IsThirdPartyChannel()) {
+        AddClassFlags(nsIClassOfService::Tail);
     }
 
     if (WaitingForTailUnblock()) {
         MOZ_DIAGNOSTIC_ASSERT(!mOnTailUnblock);
         mOnTailUnblock = &nsHttpChannel::ConnectOnTailUnblock;
         return NS_OK;
     }
 
@@ -689,21 +680,17 @@ nsHttpChannel::CheckFastBlocked()
 
     if (!sFastBlockInited) {
         sFastBlockInited = true;
         Preferences::AddBoolVarCache(&sIsContentBlockingEnabled, "browser.contentblocking.enabled");
         Preferences::AddBoolVarCache(&sIsFastBlockEnabled, "browser.fastblock.enabled");
         Preferences::AddUintVarCache(&sFastBlockTimeout, "browser.fastblock.timeout");
     }
 
-    nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil = services::GetThirdPartyUtil();
-    bool result = false;
-    if (!thirdPartyUtil ||
-        NS_FAILED(thirdPartyUtil->IsThirdPartyChannel(this, nullptr, &result)) ||
-        !result) {
+    if (!IsThirdPartyChannel()) {
         return false;
     }
 
     TimeStamp timestamp;
     if (NS_FAILED(GetNavigationStartTimeStamp(&timestamp))) {
         return false;
     }
 
@@ -717,16 +704,39 @@ nsHttpChannel::CheckFastBlocked()
     if (duration.ToMilliseconds() < sFastBlockTimeout) {
         return false;
     }
 
     LOG(("FastBlock timeout (%lf) [this=%p]\n", duration.ToMilliseconds(), this));
     return true;
 }
 
+bool
+nsHttpChannel::IsThirdPartyChannel()
+{
+    if (mIsThirdPartyChannel) {
+        return *mIsThirdPartyChannel;
+    }
+
+    nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil = services::GetThirdPartyUtil();
+    if (!thirdPartyUtil) {
+        return false;
+    }
+
+    bool isThirdPartyChannel;
+    if (NS_FAILED(thirdPartyUtil->IsThirdPartyChannel(this,
+                                                      nullptr,
+                                                      &isThirdPartyChannel))) {
+        return false;
+    }
+
+    mIsThirdPartyChannel.emplace(isThirdPartyChannel);
+    return *mIsThirdPartyChannel;
+}
+
 nsresult
 nsHttpChannel::ConnectOnTailUnblock()
 {
     nsresult rv;
 
     LOG(("nsHttpChannel::ConnectOnTailUnblock [this=%p]\n", this));
 
     bool isTrackingResource = mIsTrackingResource; // is atomic
@@ -2320,23 +2330,18 @@ nsHttpChannel::ProcessResponse()
     nsCOMPtr<nsIURI> referrer = GetReferringPage();
     if (!referrer) {
         referrer = mReferrer;
     }
 
     // We consider top-level tracking resource as non-tracking if not in 3rd
     // party context.
     bool isThirdPartyAndTrackingResource = false;
-    if(mIsTrackingResource) {
-        nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
-          services::GetThirdPartyUtil();
-        if (thirdPartyUtil) {
-            thirdPartyUtil->IsThirdPartyChannel(this, nullptr,
-                                                &isThirdPartyAndTrackingResource);
-        }
+    if (mIsTrackingResource) {
+        isThirdPartyAndTrackingResource = IsThirdPartyChannel();
     }
 
     if (referrer) {
         nsCOMPtr<nsILoadContextInfo> lci = GetLoadContextInfo(this);
         mozilla::net::Predictor::UpdateCacheability(referrer, mURI, httpStatus,
                                                     mRequestHead, mResponseHead,
                                                     lci,
                                                     isThirdPartyAndTrackingResource);
--- a/netwerk/protocol/http/nsHttpChannel.h
+++ b/netwerk/protocol/http/nsHttpChannel.h
@@ -27,16 +27,17 @@
 #include "AutoClose.h"
 #include "nsIStreamListener.h"
 #include "nsISupportsPrimitives.h"
 #include "nsICorsPreflightCallback.h"
 #include "AlternateServices.h"
 #include "nsIRaceCacheWithNetwork.h"
 #include "mozilla/extensions/PStreamFilterParent.h"
 #include "mozilla/Mutex.h"
+#include "mozilla/Maybe.h"
 
 class nsDNSPrefetch;
 class nsICancelable;
 class nsIHttpChannelAuthProvider;
 class nsInputStreamPump;
 class nsISSLStatus;
 
 namespace mozilla { namespace net {
@@ -655,16 +656,20 @@ private:
     // Called on untail when tailed during AsyncOpen execution.
     nsresult AsyncOpenOnTailUnblock();
     // Called on untail when tailed because of being a tracking resource.
     nsresult ConnectOnTailUnblock();
 
     // Check if current channel should be canceled by FastBlock rules.
     bool CheckFastBlocked();
 
+    // This caches the result of mozIThirdPartyUtil::IsThirdPartyChannel.
+    bool IsThirdPartyChannel();
+    Maybe<bool> mIsThirdPartyChannel;
+
     nsCString mUsername;
 
     // If non-null, warnings should be reported to this object.
     RefPtr<HttpChannelSecurityWarningReporter> mWarningReporter;
 
     RefPtr<ADivertableParentChannel> mParentChannel;
 
     // True if the channel is reading from cache.