Bug 468700 - Having "Ask me Everytime" enabled for Cookies during Private Browsing is unusable; r=dwitte sr=bzbarsky
authorEhsan Akhgari <ehsan.akhgari@gmail.com>
Wed, 21 Jan 2009 11:16:28 +0330
changeset 23990 78a74a156436abe7ec872990ef8e2a4ea9fb5a17
parent 23989 71a49097a2fd50b6f6559fe6b26e75aeaaa477a3
child 23991 2deba79aa0b534e626c73a18211cb02ced4aa4fb
push idunknown
push userunknown
push dateunknown
reviewersdwitte, bzbarsky
bugs468700
milestone1.9.2a1pre
Bug 468700 - Having "Ask me Everytime" enabled for Cookies during Private Browsing is unusable; r=dwitte sr=bzbarsky
extensions/cookie/nsCookiePermission.cpp
extensions/cookie/nsCookiePermission.h
extensions/cookie/test/unit/cookieprompt.js
extensions/cookie/test/unit/test_bug468700.js
--- a/extensions/cookie/nsCookiePermission.cpp
+++ b/extensions/cookie/nsCookiePermission.cpp
@@ -18,16 +18,17 @@
  * The Initial Developer of the Original Code is
  * Michiel van Leeuwen (mvl@exedo.nl).
  * Portions created by the Initial Developer are Copyright (C) 2003
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
  *   Darin Fisher <darin@meer.net>
  *   Daniel Witte <dwitte@stanford.edu>
+ *   Ehsan Akhgari <ehsan.akhgari@gmail.com>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
@@ -54,16 +55,17 @@
 #include "nsIChannel.h"
 #include "nsIDOMWindow.h"
 #include "nsIDOMDocument.h"
 #include "nsIPrincipal.h"
 #include "nsString.h"
 #include "nsCRT.h"
 #include "nsILoadContext.h"
 #include "nsIScriptObjectPrincipal.h"
+#include "nsNetCID.h"
 
 /****************************************************************
  ************************ nsCookiePermission ********************
  ****************************************************************/
 
 // values for mCookiesLifetimePolicy
 // 0 == accept normally
 // 1 == ask before accepting
@@ -296,18 +298,20 @@ nsCookiePermission::CanSetCookie(nsIURI 
     
     // declare this here since it'll be used in all of the remaining cases
     PRInt64 currentTime = PR_Now() / PR_USEC_PER_SEC;
     PRInt64 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, just accept the cookie and return
-      if (*aIsSession && mCookiesAlwaysAcceptSession) {
+      // without asking, or if we are in private browsing mode, just
+      // accept the cookie and return
+      if ((*aIsSession && mCookiesAlwaysAcceptSession) ||
+          InPrivateBrowsing()) {
         *aResult = PR_TRUE;
         return NS_OK;
       }
       
       // default to rejecting, in case the prompting process fails
       *aResult = PR_FALSE;
 
       nsCAutoString hostPort;
@@ -503,8 +507,19 @@ 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;
 }
+
+PRBool
+nsCookiePermission::InPrivateBrowsing()
+{
+  PRBool inPrivateBrowsingMode = PR_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
@@ -38,16 +38,17 @@
 #ifndef nsCookiePermission_h__
 #define nsCookiePermission_h__
 
 #include "nsICookiePermission.h"
 #include "nsIPermissionManager.h"
 #include "nsIObserver.h"
 #include "nsCOMPtr.h"
 #include "prlong.h"
+#include "nsIPrivateBrowsingService.h"
 
 class nsIPrefBranch;
 
 class nsCookiePermission : public nsICookiePermission
                          , public nsIObserver
 {
 public:
   NS_DECL_ISUPPORTS
@@ -63,17 +64,20 @@ public:
 #endif
     {}
   virtual ~nsCookiePermission() {}
 
   nsresult Init();
   void     PrefChanged(nsIPrefBranch *, const char *);
 
 private:
+  PRBool InPrivateBrowsing();
+
   nsCOMPtr<nsIPermissionManager> mPermMgr;
+  nsCOMPtr<nsIPrivateBrowsingService> mPBService;
 
   PRInt64      mCookiesLifetimeSec;            // lifetime limit specified in seconds
   PRUint8      mCookiesLifetimePolicy;         // pref for how long cookies are stored
   PRPackedBool mCookiesAlwaysAcceptSession;    // don't prompt for session cookies
 #ifdef MOZ_MAIL_NEWS
   PRPackedBool mCookiesDisabledForMailNews;
 #endif
 
new file mode 100644
--- /dev/null
+++ b/extensions/cookie/test/unit/cookieprompt.js
@@ -0,0 +1,25 @@
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+function CookiePromptService() {
+}
+
+CookiePromptService.prototype = {
+  classDescription: "Cookie Prompt Test Service",
+  contractID: "@mozilla.org/embedcomp/cookieprompt-service;1",
+  classID: Components.ID("{509b5540-c87c-11dd-ad8b-0800200c9a66}"),
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsICookiePromptService]),
+
+  cookieDialog: function(parent, cookie, hostname,
+                         cookiesFromHost, changingCookie,
+                         rememberDecision) {
+    return 0;
+  }
+};
+
+function NSGetModule(compMgr, fileSpec) {
+  return XPCOMUtils.generateModule([CookiePromptService]);
+}
new file mode 100644
--- /dev/null
+++ b/extensions/cookie/test/unit/test_bug468700.js
@@ -0,0 +1,45 @@
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+
+function run_test() {
+  do_load_module("/extensions/cookie/test/unit/cookieprompt.js");
+
+  var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
+  var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
+  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
+  var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
+  var pb = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
+
+  var spec = "http://foo.bar/baz";
+  var uri = ios.newURI(spec, null, null);
+
+  // accept all cookies
+  prefs.setIntPref("network.cookie.lifetimePolicy", 0);
+  // add a test cookie
+  cs.setCookieString(uri, null, "foo=bar", null);
+  do_check_eq(cs.countCookiesFromHost("foo.bar"), 1);
+  // ask all cookies (will result in rejection because the prompt is not available)
+  prefs.setIntPref("network.cookie.lifetimePolicy", 1);
+  // add a test cookie
+  cs.setCookieString(uri, null, "bar=baz", null);
+  do_check_eq(cs.countCookiesFromHost("foo.bar"), 1);
+  cs.removeAll();
+
+  // if private browsing is available
+  if (pb) {
+    // enter private browsing mode
+    pb.privateBrowsingEnabled = true;
+
+    // accept all cookies
+    prefs.setIntPref("network.cookie.lifetimePolicy", 0);
+    // add a test cookie
+    cs.setCookieString(uri, null, "foobar=bar", null);
+    do_check_eq(cs.countCookiesFromHost("foo.bar"), 1);
+    // ask all cookies (will result in rejection because the prompt is not available)
+    prefs.setIntPref("network.cookie.lifetimePolicy", 1);
+    // add a test cookie
+    cs.setCookieString(uri, null, "foobaz=bar", null);
+    do_check_eq(cs.countCookiesFromHost("foo.bar"), 2);
+  }
+}
+