Bug 1409156 - Do not prefetch tracking resources. draft
authorNicholas Hurley <hurley@mozilla.com>
Mon, 07 Aug 2017 12:45:49 -0700
changeset 681063 d072e8172601df48fc979a3177c8549d0f2a38e7
parent 680782 c6a2643362a67cdf7a87ac165454fce4b383debb
child 736084 a08cd9d744ceaa0762ec32012b1bad7ec207d10c
push id84754
push userbmo:hurley@mozilla.com
push dateMon, 16 Oct 2017 23:18:26 +0000
bugs1409156
milestone58.0a1
Bug 1409156 - Do not prefetch tracking resources. MozReview-Commit-ID: IKKHOLd2b4Y
netwerk/base/Predictor.cpp
netwerk/base/Predictor.h
netwerk/protocol/http/nsHttpChannel.cpp
--- a/netwerk/base/Predictor.cpp
+++ b/netwerk/base/Predictor.cpp
@@ -2581,17 +2581,17 @@ Predictor::PrefetchListener::OnDataAvail
 
 // Miscellaneous Predictor
 
 void
 Predictor::UpdateCacheability(nsIURI *sourceURI, nsIURI *targetURI,
                               uint32_t httpStatus,
                               nsHttpRequestHead &requestHead,
                               nsHttpResponseHead *responseHead,
-                              nsILoadContextInfo *lci)
+                              nsILoadContextInfo *lci, bool isTracking)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (lci && lci->IsPrivate()) {
     PREDICTOR_LOG(("Predictor::UpdateCacheability in PB mode - ignoring"));
     return;
   }
 
@@ -2605,25 +2605,27 @@ Predictor::UpdateCacheability(nsIURI *so
     return;
   }
 
   RefPtr<Predictor> self = sSelf;
   if (self) {
     nsAutoCString method;
     requestHead.Method(method);
     self->UpdateCacheabilityInternal(sourceURI, targetURI, httpStatus,
-                                     method, *lci->OriginAttributesPtr());
+                                     method, *lci->OriginAttributesPtr(),
+                                     isTracking);
   }
 }
 
 void
 Predictor::UpdateCacheabilityInternal(nsIURI *sourceURI, nsIURI *targetURI,
                                       uint32_t httpStatus,
                                       const nsCString &method,
-                                      const OriginAttributes& originAttributes)
+                                      const OriginAttributes& originAttributes,
+                                      bool isTracking)
 {
   PREDICTOR_LOG(("Predictor::UpdateCacheability httpStatus=%u", httpStatus));
 
   nsresult rv;
 
   if (!mInitialized) {
     PREDICTOR_LOG(("    not initialized"));
     return;
@@ -2650,17 +2652,18 @@ Predictor::UpdateCacheabilityInternal(ns
     PREDICTOR_LOG(("    cannot get disk cache storage"));
     return;
   }
 
   uint32_t openFlags = nsICacheStorage::OPEN_READONLY |
                        nsICacheStorage::OPEN_SECRETLY |
                        nsICacheStorage::CHECK_MULTITHREADED;
   RefPtr<Predictor::CacheabilityAction> action =
-    new Predictor::CacheabilityAction(targetURI, httpStatus, method, this);
+    new Predictor::CacheabilityAction(targetURI, httpStatus, method, isTracking,
+                                      this);
   nsAutoCString uri;
   targetURI->GetAsciiSpec(uri);
   PREDICTOR_LOG(("    uri=%s action=%p", uri.get(), action.get()));
   cacheDiskStorage->AsyncOpenURI(sourceURI, EmptyCString(), openFlags, action);
 }
 
 NS_IMPL_ISUPPORTS(Predictor::CacheabilityAction,
                   nsICacheEntryOpenCallback,
@@ -2725,17 +2728,19 @@ Predictor::CacheabilityAction::OnCacheEn
 
     if (!mPredictor->ParseMetaDataEntry(key, value, uri, hitCount, lastHit,
                                         flags)) {
       PREDICTOR_LOG(("    failed to parse key=%s value=%s", key, value));
       continue;
     }
 
     if (strTargetURI.Equals(uri)) {
-      if (mHttpStatus == 200 && mMethod.EqualsLiteral("GET") && !hasQueryString) {
+      if (mHttpStatus == 200 && mMethod.EqualsLiteral("GET") &&
+          !hasQueryString &&
+          !mIsTracking) {
         PREDICTOR_LOG(("    marking %s cacheable", key));
         flags |= FLAG_PREFETCHABLE;
       } else {
         PREDICTOR_LOG(("    marking %s uncacheable", key));
         flags &= ~FLAG_PREFETCHABLE;
       }
       nsCString newValue;
       MakeMetadataEntry(hitCount, lastHit, flags, newValue);
--- a/netwerk/base/Predictor.h
+++ b/netwerk/base/Predictor.h
@@ -61,17 +61,17 @@ public:
 
   // Used to update whether a particular URI was cacheable or not.
   // sourceURI and targetURI are the same as the arguments to Learn
   // and httpStatus is the status code we got while loading targetURI.
   static void UpdateCacheability(nsIURI *sourceURI, nsIURI *targetURI,
                                  uint32_t httpStatus,
                                  nsHttpRequestHead &requestHead,
                                  nsHttpResponseHead *reqponseHead,
-                                 nsILoadContextInfo *lci);
+                                 nsILoadContextInfo *lci, bool isTracking);
 
 private:
   virtual ~Predictor();
 
   // Stores callbacks for a child process predictor (for test purposes)
   nsCOMPtr<nsINetworkPredictorVerifier> mChildVerifier;
 
   union Reason {
@@ -134,29 +134,32 @@ private:
                            , public nsICacheEntryMetaDataVisitor
   {
   public:
     NS_DECL_THREADSAFE_ISUPPORTS
     NS_DECL_NSICACHEENTRYOPENCALLBACK
     NS_DECL_NSICACHEENTRYMETADATAVISITOR
 
     CacheabilityAction(nsIURI *targetURI, uint32_t httpStatus,
-                       const nsCString &method, Predictor *predictor)
+                       const nsCString &method, bool isTracking,
+                       Predictor *predictor)
       :mTargetURI(targetURI)
       ,mHttpStatus(httpStatus)
       ,mMethod(method)
+      ,mIsTracking(isTracking)
       ,mPredictor(predictor)
     { }
 
   private:
     virtual ~CacheabilityAction() { }
 
     nsCOMPtr<nsIURI> mTargetURI;
     uint32_t mHttpStatus;
     nsCString mMethod;
+    bool mIsTracking;
     RefPtr<Predictor> mPredictor;
     nsTArray<nsCString> mKeysToCheck;
     nsTArray<nsCString> mValuesToCheck;
   };
 
   class Resetter : public nsICacheEntryOpenCallback,
                    public nsICacheEntryMetaDataVisitor,
                    public nsICacheStorageVisitor
@@ -417,17 +420,18 @@ private:
                           uint32_t &hitCount, uint32_t &lastHit,
                           uint32_t &flags);
 
   // Used to update whether a particular URI was cacheable or not.
   // sourceURI and targetURI are the same as the arguments to Learn
   // and httpStatus is the status code we got while loading targetURI.
   void UpdateCacheabilityInternal(nsIURI *sourceURI, nsIURI *targetURI,
                                   uint32_t httpStatus, const nsCString &method,
-                                  const OriginAttributes& originAttributes);
+                                  const OriginAttributes& originAttributes,
+                                  bool isTracking);
 
   // Make sure our prefs are in their expected range of values
   void SanitizePrefs();
 
   // Our state
   bool mInitialized;
 
   bool mEnabled;
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -2349,17 +2349,17 @@ nsHttpChannel::ProcessResponse()
     nsCOMPtr<nsIURI> referrer = GetReferringPage();
     if (!referrer) {
         referrer = mReferrer;
     }
     if (referrer) {
         nsCOMPtr<nsILoadContextInfo> lci = GetLoadContextInfo(this);
         mozilla::net::Predictor::UpdateCacheability(referrer, mURI, httpStatus,
                                                     mRequestHead, mResponseHead,
-                                                    lci);
+                                                    lci, mIsTrackingResource);
     }
 
     if (mTransaction && mTransaction->ProxyConnectFailed()) {
         // Only allow 407 (authentication required) to continue
         if (httpStatus != 407) {
             return ProcessFailedProxyConnect(httpStatus);
         }
         // If proxy CONNECT response needs to complete, wait to process connection