Bug 722850 - Part 3: Check the private browsing status of channels when checking cookie permissions. r=mconnor
authorJosh Matthews <josh@joshmatthews.net>
Wed, 01 Feb 2012 05:20:01 -0500
changeset 113490 2a9fbd15cad4fe21dee53ea0e7858c700d07de5b
parent 113489 320c1567e431f64c4be5bb5acfec2d7e68a543c3
child 113491 7a0fe388a24b7df6fb1d6efeab09ed3f1d1a102b
push id23872
push useremorley@mozilla.com
push dateFri, 16 Nov 2012 17:06:27 +0000
treeherdermozilla-central@a7ed19f7d21a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconnor
bugs722850
milestone19.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 722850 - Part 3: Check the private browsing status of channels when checking cookie permissions. r=mconnor
extensions/cookie/nsCookiePermission.cpp
extensions/cookie/nsCookiePermission.h
netwerk/cookie/CookieServiceParent.cpp
netwerk/cookie/nsCookieService.cpp
netwerk/cookie/nsCookieService.h
--- a/extensions/cookie/nsCookiePermission.cpp
+++ b/extensions/cookie/nsCookiePermission.cpp
@@ -233,17 +233,17 @@ nsCookiePermission::CanSetCookie(nsIURI 
     int64_t delta = *aExpiry - currentTime;
 
     // check whether the user wants to be prompted
     if (mCookiesLifetimePolicy == ASK_BEFORE_ACCEPT) {
       // if it's a session cookie and the user wants to accept these 
       // without asking, or if we are in private browsing mode, just
       // accept the cookie and return
       if ((*aIsSession && mCookiesAlwaysAcceptSession) ||
-          InPrivateBrowsing()) {
+          (aChannel && NS_UsePrivateBrowsing(aChannel))) {
         *aResult = true;
         return NS_OK;
       }
 
       // default to rejecting, in case the prompting process fails
       *aResult = false;
 
       nsAutoCString hostPort;
@@ -267,26 +267,16 @@ nsCookiePermission::CanSetCookie(nsIURI 
 
       // we don't cache the cookiePromptService - it's not used often, so not
       // worth the memory.
       nsresult rv;
       nsCOMPtr<nsICookiePromptService> cookiePromptService =
           do_GetService(NS_COOKIEPROMPTSERVICE_CONTRACTID, &rv);
       if (NS_FAILED(rv)) return rv;
 
-      // try to get a nsIDOMWindow from the channel...
-      nsCOMPtr<nsIDOMWindow> parent;
-      if (aChannel) {
-        nsCOMPtr<nsILoadContext> ctx;
-        NS_QueryNotificationCallbacks(aChannel, ctx);
-        if (ctx) {
-          ctx->GetAssociatedWindow(getter_AddRefs(parent));
-        }
-      }
-
       // get some useful information to present to the user:
       // whether a previous cookie already exists, and how many cookies this host
       // has set
       bool foundCookie = false;
       uint32_t countFromHost;
       nsCOMPtr<nsICookieManager2> cookieManager = do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv);
       if (NS_SUCCEEDED(rv)) {
         nsAutoCString rawHost;
@@ -305,17 +295,17 @@ nsCookiePermission::CanSetCookie(nsIURI 
         // the cookie has already expired. accept it, and let the backend figure
         // out it's expired, so that we get correct logging & notifications.
         *aResult = true;
         return rv;
       }
 
       bool rememberDecision = false;
       int32_t dialogRes = nsICookiePromptService::DENY_COOKIE;
-      rv = cookiePromptService->CookieDialog(parent, aCookie, hostPort, 
+      rv = cookiePromptService->CookieDialog(nullptr, aCookie, hostPort, 
                                              countFromHost, foundCookie,
                                              &rememberDecision, &dialogRes);
       if (NS_FAILED(rv)) return rv;
 
       *aResult = !!dialogRes;
       if (dialogRes == nsICookiePromptService::ACCEPT_SESSION_COOKIE)
         *aIsSession = true;
 
@@ -363,19 +353,8 @@ nsCookiePermission::Observe(nsISupports 
   nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
   NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic),
                "unexpected topic - we only deal with pref changes!");
 
   if (prefBranch)
     PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get());
   return NS_OK;
 }
-
-bool
-nsCookiePermission::InPrivateBrowsing()
-{
-  bool inPrivateBrowsingMode = false;
-  if (!mPBService)
-    mPBService = do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
-  if (mPBService)
-    mPBService->GetPrivateBrowsingEnabled(&inPrivateBrowsingMode);
-  return inPrivateBrowsingMode;
-}
--- a/extensions/cookie/nsCookiePermission.h
+++ b/extensions/cookie/nsCookiePermission.h
@@ -5,17 +5,16 @@
 #ifndef nsCookiePermission_h__
 #define nsCookiePermission_h__
 
 #include "nsICookiePermission.h"
 #include "nsIPermissionManager.h"
 #include "nsIObserver.h"
 #include "nsCOMPtr.h"
 #include "prlong.h"
-#include "nsIPrivateBrowsingService.h"
 #include "mozIThirdPartyUtil.h"
 
 class nsIPrefBranch;
 
 class nsCookiePermission : public nsICookiePermission
                          , public nsIObserver
 {
 public:
@@ -30,20 +29,18 @@ public:
     {}
   virtual ~nsCookiePermission() {}
 
   bool Init();
   void PrefChanged(nsIPrefBranch *, const char *);
 
 private:
    bool EnsureInitialized() { return (mPermMgr != NULL && mThirdPartyUtil != NULL) || Init(); };
-   bool InPrivateBrowsing();
 
   nsCOMPtr<nsIPermissionManager> mPermMgr;
-  nsCOMPtr<nsIPrivateBrowsingService> mPBService;
   nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
 
   int64_t      mCookiesLifetimeSec;            // lifetime limit specified in seconds
   uint8_t      mCookiesLifetimePolicy;         // pref for how long cookies are stored
   bool mCookiesAlwaysAcceptSession;    // don't prompt for session cookies
 };
 
 // {EF565D0A-AB9A-4A13-9160-0644CDFD859A}
--- a/netwerk/cookie/CookieServiceParent.cpp
+++ b/netwerk/cookie/CookieServiceParent.cpp
@@ -95,17 +95,18 @@ CookieServiceParent::RecvSetCookieString
   if (!hostURI)
     return false;
 
   uint32_t appId;
   bool isInBrowserElement, isPrivate;
   GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement, isPrivate);
 
   nsDependentCString cookieString(aCookieString, 0);
+  //TODO: bug 812475, pass a real channel object
   mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString,
                                           aServerTime, aFromHttp, appId,
-                                          isInBrowserElement, isPrivate);
+                                          isInBrowserElement, isPrivate, nullptr);
   return true;
 }
 
 }
 }
 
--- a/netwerk/cookie/nsCookieService.cpp
+++ b/netwerk/cookie/nsCookieService.cpp
@@ -1566,29 +1566,30 @@ nsCookieService::SetCookieStringCommon(n
   }
 
   bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
 
   nsDependentCString cookieString(aCookieHeader);
   nsDependentCString serverTime(aServerTime ? aServerTime : "");
   SetCookieStringInternal(aHostURI, isForeign, cookieString,
                           serverTime, aFromHttp, appId, inBrowserElement,
-                          isPrivate);
+                          isPrivate, aChannel);
   return NS_OK;
 }
 
 void
 nsCookieService::SetCookieStringInternal(nsIURI             *aHostURI,
                                          bool                aIsForeign,
                                          nsDependentCString &aCookieHeader,
                                          const nsCString    &aServerTime,
                                          bool                aFromHttp,
                                          uint32_t            aAppId,
                                          bool                aInBrowserElement,
-                                         bool                aIsPrivate)
+                                         bool                aIsPrivate,
+                                         nsIChannel         *aChannel)
 {
   NS_ASSERTION(aHostURI, "null host!");
 
   if (!mDBState) {
     NS_WARNING("No DBState! Profile already closed?");
     return;
   }
 
@@ -1637,17 +1638,17 @@ nsCookieService::SetCookieStringInternal
   if (result == PR_SUCCESS) {
     serverTime = tempServerTime / int64_t(PR_USEC_PER_SEC);
   } else {
     serverTime = PR_Now() / PR_USEC_PER_SEC;
   }
 
   // process each cookie in the header
   while (SetCookieInternal(aHostURI, key, requireHostMatch, cookieStatus,
-                           aCookieHeader, serverTime, aFromHttp)) {
+                           aCookieHeader, serverTime, aFromHttp, aChannel)) {
     // 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
@@ -2639,17 +2640,18 @@ nsCookieService::GetCookieStringInternal
 // to be processed
 bool
 nsCookieService::SetCookieInternal(nsIURI                        *aHostURI,
                                    const nsCookieKey             &aKey,
                                    bool                           aRequireHostMatch,
                                    CookieStatus                   aStatus,
                                    nsDependentCString            &aCookieHeader,
                                    int64_t                        aServerTime,
-                                   bool                           aFromHttp)
+                                   bool                           aFromHttp,
+                                   nsIChannel                    *aChannel)
 {
   NS_ASSERTION(aHostURI, "null host!");
 
   // create a stack-based nsCookieAttributes, to store all the
   // attributes parsed from the cookie
   nsCookieAttributes cookieAttributes;
 
   // init expiryTime such that session cookies won't prematurely expire
@@ -2709,21 +2711,18 @@ nsCookieService::SetCookieInternal(nsIUR
                      cookieAttributes.isHttpOnly);
   if (!cookie)
     return newCookie;
 
   // check permissions from site permission list, or ask the user,
   // to determine if we can set the cookie
   if (mPermissionService) {
     bool permission;
-    // Not passing an nsIChannel here means CanSetCookie will use the currently
-    // active window to display the prompt. This isn't exactly ideal, but this
-    // code is going away. See bug 546746.
     mPermissionService->CanSetCookie(aHostURI,
-                                     nullptr,
+                                     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);
       return newCookie;
--- a/netwerk/cookie/nsCookieService.h
+++ b/netwerk/cookie/nsCookieService.h
@@ -264,18 +264,18 @@ class nsCookieService : public nsICookie
     void                          EnsureReadDomain(const nsCookieKey &aKey);
     void                          EnsureReadComplete();
     nsresult                      NormalizeHost(nsCString &aHost);
     nsresult                      GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, bool &aRequireHostMatch);
     nsresult                      GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain);
     nsresult                      GetCookieStringCommon(nsIURI *aHostURI, nsIChannel *aChannel, bool aHttpBound, char** aCookie);
   void                            GetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, bool aHttpBound, uint32_t aAppId, bool aInBrowserElement, bool aIsPrivate, nsCString &aCookie);
     nsresult                      SetCookieStringCommon(nsIURI *aHostURI, const char *aCookieHeader, const char *aServerTime, nsIChannel *aChannel, bool aFromHttp);
-  void                            SetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, nsDependentCString &aCookieHeader, const nsCString &aServerTime, bool aFromHttp, uint32_t aAppId, bool aInBrowserElement, bool aIsPrivate);
-    bool                          SetCookieInternal(nsIURI *aHostURI, const nsCookieKey& aKey, bool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, int64_t aServerTime, bool aFromHttp);
+  void                            SetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, nsDependentCString &aCookieHeader, const nsCString &aServerTime, bool aFromHttp, uint32_t aAppId, bool aInBrowserElement, bool aIsPrivate, nsIChannel* aChannel);
+    bool                          SetCookieInternal(nsIURI *aHostURI, const nsCookieKey& aKey, bool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, int64_t aServerTime, bool aFromHttp, nsIChannel* aChannel);
     void                          AddInternal(const nsCookieKey& aKey, nsCookie *aCookie, int64_t aCurrentTimeInUsec, nsIURI *aHostURI, const char *aCookieHeader, bool aFromHttp);
     void                          RemoveCookieFromList(const nsListIter &aIter, mozIStorageBindingParamsArray *aParamsArray = NULL);
     void                          AddCookieToList(const nsCookieKey& aKey, nsCookie *aCookie, DBState *aDBState, mozIStorageBindingParamsArray *aParamsArray, bool aWriteToDB = true);
     void                          UpdateCookieInList(nsCookie *aCookie, int64_t aLastAccessed, mozIStorageBindingParamsArray *aParamsArray);
     static bool                   GetTokenValue(nsASingleFragmentCString::const_char_iterator &aIter, nsASingleFragmentCString::const_char_iterator &aEndIter, nsDependentCSubstring &aTokenString, nsDependentCSubstring &aTokenValue, bool &aEqualsFound);
     static bool                   ParseAttributes(nsDependentCString &aCookieHeader, nsCookieAttributes &aCookie);
     bool                          RequireThirdPartyCheck();
     CookieStatus                  CheckPrefs(nsIURI *aHostURI, bool aIsForeign, bool aRequireHostMatch, const char *aCookieHeader);