Bug 1487093 - Call AntiTrackingCommon::NotifyRejection when cookies are rejected because of cookie policies, r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 30 Aug 2018 17:44:38 +0200
changeset 482383 f3472f48b9fec654ac16fdcecef468314d04075a
parent 482382 93112d1b45cba75b29cbc123b710760af1bd13dc
child 482384 08c242ce352facd4d887fb3874fb8e26dfe32e56
push id232
push userfmarier@mozilla.com
push dateWed, 05 Sep 2018 20:45:54 +0000
reviewersehsan
bugs1487093
milestone63.0a1
Bug 1487093 - Call AntiTrackingCommon::NotifyRejection when cookies are rejected because of cookie policies, r=ehsan
netwerk/cookie/nsCookieService.cpp
netwerk/cookie/nsCookieService.h
toolkit/components/antitracking/AntiTrackingCommon.cpp
toolkit/components/antitracking/AntiTrackingCommon.h
--- a/netwerk/cookie/nsCookieService.cpp
+++ b/netwerk/cookie/nsCookieService.cpp
@@ -2229,17 +2229,17 @@ nsCookieService::SetCookieStringInternal
                                          aFirstPartyStorageAccessGranted,
                                          aCookieHeader.get(), priorCookieCount,
                                          aOriginAttrs);
 
   // fire a notification if third party or if cookie was rejected
   // (but not if there was an error)
   switch (cookieStatus) {
   case STATUS_REJECTED:
-    NotifyRejected(aHostURI);
+    NotifyRejected(aHostURI, aChannel);
     if (aIsForeign) {
       NotifyThirdParty(aHostURI, false, aChannel);
     }
     return; // Stop here
   case STATUS_REJECTED_WITH_ERROR:
     return;
   case STATUS_ACCEPTED: // Fallthrough
   case STATUS_ACCEPT_SESSION:
@@ -2259,22 +2259,24 @@ nsCookieService::SetCookieStringInternal
     // document.cookie can only set one cookie at a time
     if (!aFromHttp)
       break;
   }
 }
 
 // notify observers that a cookie was rejected due to the users' prefs.
 void
-nsCookieService::NotifyRejected(nsIURI *aHostURI)
+nsCookieService::NotifyRejected(nsIURI *aHostURI, nsIChannel* aChannel)
 {
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
   if (os) {
     os->NotifyObservers(aHostURI, "cookie-rejected", nullptr);
   }
+
+  AntiTrackingCommon::NotifyRejection(aChannel);
 }
 
 // notify observers that a third-party cookie was accepted/rejected
 // if the cookie issuer is unknown, it defaults to "?"
 void
 nsCookieService::NotifyThirdParty(nsIURI *aHostURI, bool aIsAccepted, nsIChannel *aChannel)
 {
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
@@ -3603,17 +3605,17 @@ nsCookieService::SetCookieInternal(nsIUR
     mPermissionService->CanSetCookie(aHostURI,
                                      aChannel,
                                      static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)),
                                      &cookieAttributes.isSession,
                                      &cookieAttributes.expiryTime,
                                      &permission);
     if (!permission) {
       COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "cookie rejected by permission manager");
-      NotifyRejected(aHostURI);
+      NotifyRejected(aHostURI, aChannel);
       return newCookie;
     }
 
     // update isSession and expiry attributes, in case they changed
     cookie->SetIsSession(cookieAttributes.isSession);
     cookie->SetExpiry(cookieAttributes.expiryTime);
   }
 
--- a/netwerk/cookie/nsCookieService.h
+++ b/netwerk/cookie/nsCookieService.h
@@ -314,17 +314,17 @@ class nsCookieService final : public nsI
     static bool                   CheckPrefixes(nsCookieAttributes &aCookie, bool aSecureRequest);
     static bool                   GetExpiry(nsCookieAttributes &aCookie, int64_t aServerTime, int64_t aCurrentTime);
     void                          RemoveAllFromMemory();
     already_AddRefed<nsIArray>    PurgeCookies(int64_t aCurrentTimeInUsec);
     bool                          FindCookie(const nsCookieKey& aKey, const nsCString& aHost, const nsCString& aName, const nsCString& aPath, nsListIter &aIter);
     bool                          FindSecureCookie(const nsCookieKey& aKey, nsCookie* aCookie);
     int64_t                       FindStaleCookie(nsCookieEntry *aEntry, int64_t aCurrentTime, nsIURI* aSource, const mozilla::Maybe<bool> &aIsSecure, nsListIter &aIter);
     void                          TelemetryForEvictingStaleCookie(nsCookie* aEvicted, int64_t oldestCookieTime);
-    void                          NotifyRejected(nsIURI *aHostURI);
+    void                          NotifyRejected(nsIURI *aHostURI, nsIChannel* aChannel);
     void                          NotifyThirdParty(nsIURI *aHostURI, bool aAccepted, nsIChannel *aChannel);
     void                          NotifyChanged(nsISupports *aSubject, const char16_t *aData, bool aOldCookieIsSession = false, bool aFromHttp = false);
     void                          NotifyPurged(nsICookie2* aCookie);
     already_AddRefed<nsIArray>    CreatePurgeList(nsICookie2* aCookie);
     void                          UpdateCookieOldestTime(DBState* aDBState, nsCookie* aCookie);
 
     nsresult                      GetCookiesWithOriginAttributes(const mozilla::OriginAttributesPattern& aPattern, const nsCString& aBaseDomain, nsISimpleEnumerator **aEnumerator);
     nsresult                      RemoveCookiesWithOriginAttributes(const mozilla::OriginAttributesPattern& aPattern, const nsCString& aBaseDomain);
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -828,8 +828,19 @@ AntiTrackingCommon::IsOnContentBlockingA
   }
 
   if (!aIsAllowListed) {
     LOG(("No user override found"));
   }
 
   return NS_OK;
 }
+
+/* static */ void
+AntiTrackingCommon::NotifyRejection(nsIChannel* aChannel)
+{
+  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
+  if (!httpChannel) {
+    return;
+  }
+
+  // TODO: use aState to inform the rest of the world.
+}
--- a/toolkit/components/antitracking/AntiTrackingCommon.h
+++ b/toolkit/components/antitracking/AntiTrackingCommon.h
@@ -6,16 +6,17 @@
 
 #ifndef mozilla_antitrackingservice_h
 #define mozilla_antitrackingservice_h
 
 #include "nsString.h"
 #include "mozilla/MozPromise.h"
 #include "mozilla/RefPtr.h"
 
+class nsIChannel;
 class nsIHttpChannel;
 class nsIPrincipal;
 class nsIURI;
 class nsPIDOMWindowInner;
 
 namespace mozilla {
 
 class AntiTrackingCommon final
@@ -83,13 +84,18 @@ public:
                                                              const nsCString& aGrantedOrigin,
                                                              FirstPartyStorageAccessGrantedForOriginResolver&& aResolver);
 
 
   // Check whether a top window URI is on the content blocking allow list.
   static nsresult
   IsOnContentBlockingAllowList(nsIURI* aTopWinURI, bool& aIsAllowListed);
 
+  // This method can be called on the parent process or on the content process.
+  // The notification is propagated to the child channel if aChannel is a parent
+  // channel proxy.
+  static void
+  NotifyRejection(nsIChannel* aChannel);
 };
 
 } // namespace mozilla
 
 #endif // mozilla_antitrackingservice_h