Bug 468700 - Having "Ask me Everytime" enabled for Cookies during Private Browsing is unusable; r=dwitte sr=bzbarsky

This commit is contained in:
Ehsan Akhgari 2009-01-21 11:16:28 +03:30
Родитель fd81b1000a
Коммит 1d1ff107b0
4 изменённых файлов: 91 добавлений и 2 удалений

Просмотреть файл

@ -23,6 +23,7 @@
* 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
@ -59,6 +60,7 @@
#include "nsCRT.h"
#include "nsILoadContext.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsNetCID.h"
/****************************************************************
************************ nsCookiePermission ********************
@ -301,8 +303,10 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
// 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;
}
@ -508,3 +512,14 @@ nsCookiePermission::Observe(nsISupports *aSubject,
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;
}

Просмотреть файл

@ -43,6 +43,7 @@
#include "nsIObserver.h"
#include "nsCOMPtr.h"
#include "prlong.h"
#include "nsIPrivateBrowsingService.h"
class nsIPrefBranch;
@ -68,7 +69,10 @@ public:
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

Просмотреть файл

@ -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]);
}

Просмотреть файл

@ -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);
}
}