Bug 722850 - Part 2: Query the private browsing status of channels used to manipulate cookies. r=mconnor

This commit is contained in:
Josh Matthews 2012-02-08 13:37:07 -05:00
parent 3e6d64c9c2
commit a7cb2b3434
10 changed files with 232 additions and 255 deletions

View File

@ -23,7 +23,7 @@ MOCHITEST_BROWSER_FILES = \
browser_privatebrowsing_popupmode.js \
browser_privatebrowsing_searchbar.js \
browser_privatebrowsing_sslsite_transition.js \
browser_privatebrowsing_transition.js \
$(warning browser_privatebrowsing_transition.js disabled since it no longer makes sense) \
browser_privatebrowsing_urlbarundo.js \
browser_privatebrowsing_viewsource.js \
staller.sjs \

View File

@ -1,6 +1,18 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function make_channel(url) {
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
return chan;
}
function make_uri(url) {
var ios = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
return ios.newURI(url, null, null);
}
function run_test() {
do_load_manifest("cookieprompt.manifest");
@ -8,10 +20,6 @@ function run_test() {
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 = null;
try {
pb = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
} catch (e) {}
var spec = "http://foo.bar/baz";
var uri = ios.newURI(spec, null, null);
@ -28,25 +36,22 @@ function run_test() {
do_check_eq(cs.countCookiesFromHost("foo.bar"), 1);
cs.removeAll();
// if private browsing is available
if (pb) {
prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true);
// enter private browsing mode
pb.privateBrowsingEnabled = true;
// Simulate private browsing with a private channel for context
var chan = make_channel(uri.spec);
chan.QueryInterface(Ci.nsIPrivateBrowsingChannel);
chan.setPrivate(true);
// accept all cookies
prefs.setIntPref("network.cookie.lifetimePolicy", 0);
// 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);
prefs.clearUserPref("browser.privatebrowsing.keep_current_session");
}
// add a test cookie
cs.setCookieString(uri, null, "foobar=bar", chan);
do_check_eq(cs.getCookieString(uri, chan), "foobar=bar");
// 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", chan);
do_check_eq(cs.getCookieString(uri, chan), "foobar=bar; foobaz=bar");
}

View File

@ -17,6 +17,12 @@ function finish_test() {
});
}
function make_channel(url) {
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
return chan;
}
function do_run_test() {
// Set up a profile.
let profile = do_get_profile();
@ -45,36 +51,28 @@ function do_run_test() {
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
// Enter private browsing mode, set a cookie for host 2, and check the counts.
Services.pb.privateBrowsingEnabled = true;
Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", null);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 1);
var chan1 = make_channel(uri1.spec);
chan1.QueryInterface(Ci.nsIPrivateBrowsingChannel);
chan1.setPrivate(true);
var chan2 = make_channel(uri2.spec);
chan2.QueryInterface(Ci.nsIPrivateBrowsingChannel);
chan2.setPrivate(true);
Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
// Remove cookies and check counts.
Services.cookies.removeAll();
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
Services.obs.notifyObservers(null, "last-pb-context-exited", null);
do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
// Check that attempting to import cookies while in private browsing throws.
do_check_throws(function() {
Services.cookiemgr.importCookies(null);
}, Cr.NS_ERROR_NOT_AVAILABLE);
Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", null);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 1);
Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
// Leave private browsing mode and check counts.
Services.pb.privateBrowsingEnabled = false;
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
// Enter private browsing mode and check counts.
Services.pb.privateBrowsingEnabled = true;
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
// Leave private browsing mode and check counts.
Services.pb.privateBrowsingEnabled = false;
Services.obs.notifyObservers(null, "last-pb-context-exited", null);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
@ -88,11 +86,10 @@ function do_run_test() {
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
// Enter private browsing mode, set a cookie for host 2, and check the counts.
Services.pb.privateBrowsingEnabled = true;
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", null);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 1);
do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
Services.cookies.setCookieString(uri2, null, "oh=hai; max-age=1000", chan2);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), "oh=hai");
// Fake a profile change.
do_close_profile(test_generator);
@ -101,16 +98,15 @@ function do_run_test() {
// We're still in private browsing mode, but should have a new session.
// Check counts.
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
// Leave private browsing mode and check counts.
Services.pb.privateBrowsingEnabled = false;
Services.obs.notifyObservers(null, "last-pb-context-exited", null);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
// Enter private browsing mode.
Services.pb.privateBrowsingEnabled = true;
// Fake a profile change, but wait for async read completion.
do_close_profile(test_generator);
@ -120,11 +116,11 @@ function do_run_test() {
// We're still in private browsing mode, but should have a new session.
// Check counts.
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 0);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);
do_check_eq(Services.cookiemgr.getCookieString(uri1, chan1), null);
do_check_eq(Services.cookiemgr.getCookieString(uri2, chan2), null);
// Leave private browsing mode and check counts.
Services.pb.privateBrowsingEnabled = false;
Services.obs.notifyObservers(null, "last-pb-context-exited", null);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri1.host), 1);
do_check_eq(Services.cookiemgr.countCookiesFromHost(uri2.host), 0);

View File

@ -11,6 +11,27 @@
using namespace mozilla::ipc;
static void
GetAppInfoFromLoadContext(const IPC::SerializedLoadContext &aLoadContext,
uint32_t& aAppId,
bool& aIsInBrowserElement,
bool& aIsPrivate)
{
// TODO: bug 782542: what to do when we get null loadContext? For now assume
// NECKO_NO_APP_ID.
aAppId = NECKO_NO_APP_ID;
aIsInBrowserElement = false;
aIsPrivate = false;
if (aLoadContext.IsNotNull()) {
aAppId = aLoadContext.mAppId;
aIsInBrowserElement = aLoadContext.mIsInBrowserElement;
}
if (aLoadContext.IsPrivateBitValid())
aIsPrivate = aLoadContext.mUsePrivateBrowsing;
}
namespace mozilla {
namespace net {
@ -48,11 +69,11 @@ CookieServiceParent::RecvGetCookieString(const URIParams& aHost,
return false;
uint32_t appId;
bool isInBrowserElement;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement);
bool isInBrowserElement, isPrivate;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement, isPrivate);
mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aFromHttp, appId,
isInBrowserElement, *aResult);
isInBrowserElement, isPrivate, *aResult);
return true;
}
@ -75,33 +96,16 @@ CookieServiceParent::RecvSetCookieString(const URIParams& aHost,
return false;
uint32_t appId;
bool isInBrowserElement;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement);
bool isInBrowserElement, isPrivate;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement, isPrivate);
nsDependentCString cookieString(aCookieString, 0);
mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString,
aServerTime, aFromHttp, appId,
isInBrowserElement);
isInBrowserElement, isPrivate);
return true;
}
void
CookieServiceParent::GetAppInfoFromLoadContext(
const IPC::SerializedLoadContext &aLoadContext,
uint32_t& aAppId,
bool& aIsInBrowserElement)
{
// TODO: bug 782542: what to do when we get null loadContext? For now assume
// NECKO_NO_APP_ID.
aAppId = NECKO_NO_APP_ID;
aIsInBrowserElement = false;
if (aLoadContext.IsNotNull()) {
aAppId = aLoadContext.mAppId;
aIsInBrowserElement = aLoadContext.mIsInBrowserElement;
}
}
}
}

View File

@ -37,9 +37,6 @@ protected:
const IPC::SerializedLoadContext&
loadContext);
void GetAppInfoFromLoadContext(const IPC::SerializedLoadContext& aLoadContext,
uint32_t& aAppId, bool& aIsInBrowserElement);
nsRefPtr<nsCookieService> mCookieService;
};

View File

@ -47,6 +47,7 @@
#include "mozilla/storage.h"
#include "mozilla/Util.h" // for DebugOnly
#include "mozilla/Attributes.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/Likely.h"
#include "nsIAppsService.h"
#include "mozIApplication.h"
@ -684,7 +685,7 @@ nsCookieService::Init()
NS_ENSURE_STATE(mObserverService);
mObserverService->AddObserver(this, "profile-before-change", true);
mObserverService->AddObserver(this, "profile-do-change", true);
mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, true);
mObserverService->AddObserver(this, "last-pb-context-exited", true);
mPermissionService = do_GetService(NS_COOKIEPERMISSION_CONTRACTID);
if (!mPermissionService) {
@ -706,17 +707,7 @@ nsCookieService::InitDBStates()
mDefaultDBState = new DBState();
mDBState = mDefaultDBState;
// If we're in private browsing mode, create a private DBState.
nsCOMPtr<nsIPrivateBrowsingService> pbs =
do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
if (pbs) {
bool inPrivateBrowsing = false;
pbs->GetPrivateBrowsingEnabled(&inPrivateBrowsing);
if (inPrivateBrowsing) {
mPrivateDBState = new DBState();
mDBState = mPrivateDBState;
}
}
mPrivateDBState = new DBState();
// Get our cookie file.
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
@ -1477,29 +1468,12 @@ nsCookieService::Observe(nsISupports *aSubject,
if (prefBranch)
PrefChanged(prefBranch);
} else if (!strcmp(aTopic, NS_PRIVATE_BROWSING_SWITCH_TOPIC)) {
if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_ENTER).Equals(aData)) {
NS_ASSERTION(mDefaultDBState, "don't have a default state");
NS_ASSERTION(mDBState == mDefaultDBState, "not in default state");
NS_ASSERTION(!mPrivateDBState, "already have a private state");
// Create a new DBState, and swap it in.
mPrivateDBState = new DBState();
mDBState = mPrivateDBState;
} else if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData)) {
NS_ASSERTION(mDefaultDBState, "don't have a default state");
NS_ASSERTION(mDBState == mPrivateDBState, "not in private state");
NS_ASSERTION(!mPrivateDBState->dbConn, "private DB connection not null");
// Clear the private DBState, and restore the default one.
mPrivateDBState = NULL;
mDBState = mDefaultDBState;
}
NotifyChanged(nullptr, NS_LITERAL_STRING("reload").get());
} else if (!strcmp(aTopic, "last-pb-context-exited")) {
// Flush all the cookies stored by private browsing contexts
mPrivateDBState = new DBState();
}
return NS_OK;
}
@ -1540,9 +1514,11 @@ nsCookieService::GetCookieStringCommon(nsIURI *aHostURI,
NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
}
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
nsAutoCString result;
GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId,
inBrowserElement, result);
inBrowserElement, isPrivate, result);
*aCookie = result.IsEmpty() ? nullptr : ToNewCString(result);
return NS_OK;
}
@ -1589,10 +1565,13 @@ nsCookieService::SetCookieStringCommon(nsIURI *aHostURI,
NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
}
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
nsDependentCString cookieString(aCookieHeader);
nsDependentCString serverTime(aServerTime ? aServerTime : "");
SetCookieStringInternal(aHostURI, isForeign, cookieString,
serverTime, aFromHttp, appId, inBrowserElement);
serverTime, aFromHttp, appId, inBrowserElement,
isPrivate);
return NS_OK;
}
@ -1603,7 +1582,8 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
const nsCString &aServerTime,
bool aFromHttp,
uint32_t aAppId,
bool aInBrowserElement)
bool aInBrowserElement,
bool aIsPrivate)
{
NS_ASSERTION(aHostURI, "null host!");
@ -1612,6 +1592,9 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
return;
}
AutoRestore<DBState*> savePrevDBState(mDBState);
mDBState = aIsPrivate ? mPrivateDBState : mDefaultDBState;
// get the base domain for the host URI.
// e.g. for "www.bbc.co.uk", this would be "bbc.co.uk".
// file:// URI's (i.e. with an empty host) are allowed, but any other
@ -2467,6 +2450,7 @@ nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
bool aHttpBound,
uint32_t aAppId,
bool aInBrowserElement,
bool aIsPrivate,
nsCString &aCookieString)
{
NS_ASSERTION(aHostURI, "null host!");
@ -2476,6 +2460,9 @@ nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
return;
}
AutoRestore<DBState*> savePrevDBState(mDBState);
mDBState = aIsPrivate ? mPrivateDBState : mDefaultDBState;
// get the base domain, host, and path from the URI.
// e.g. for "www.bbc.co.uk", the base domain would be "bbc.co.uk".
// file:// URI's (i.e. with an empty host) are allowed, but any other

View File

@ -267,9 +267,9 @@ class nsCookieService : public nsICookieService
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, nsCString &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);
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 AddInternal(const nsCookieKey& aKey, nsCookie *aCookie, int64_t aCurrentTimeInUsec, nsIURI *aHostURI, const char *aCookieHeader, bool aFromHttp);
void RemoveCookieFromList(const nsListIter &aIter, mozIStorageBindingParamsArray *aParamsArray = NULL);
@ -316,8 +316,8 @@ class nsCookieService : public nsICookieService
nsCOMPtr<mozIStorageService> mStorageService;
// we have two separate DB states: one for normal browsing and one for
// private browsing, switching between them as appropriate. this state
// encapsulates both the in-memory table and the on-disk DB.
// private browsing, switching between them on a per-cookie-request basis.
// this state encapsulates both the in-memory table and the on-disk DB.
// note that the private states' dbConn should always be null - we never
// want to be dealing with the on-disk DB when in private browsing.
DBState *mDBState;

View File

@ -2,149 +2,133 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var _PBSvc = null;
function get_PBSvc() {
if (_PBSvc)
return _PBSvc;
const Cu = Components.utils;
const Ci = Components.interfaces;
const Cc = Components.classes;
try {
_PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"].
getService(Components.interfaces.nsIPrivateBrowsingService);
return _PBSvc;
} catch (e) {}
return null;
Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/Services.jsm");
var httpserver;
function inChildProcess() {
return Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime)
.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
}
function makeChan(path) {
var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var chan = ios.newChannel("http://localhost:4444/" + path, null, null)
.QueryInterface(Ci.nsIHttpChannel);
return chan;
}
var _CMSvc = null;
function get_CookieManager() {
if (_CMSvc)
return _CMSvc;
function setup_chan(path, isPrivate, callback) {
var chan = makeChan(path);
chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
chan.asyncOpen(new ChannelListener(callback), null);
}
return _CMSvc = Components.classes["@mozilla.org/cookiemanager;1"].
getService(Components.interfaces.nsICookieManager2);
function set_cookie(value, callback) {
return setup_chan('set?cookie=' + value, false, callback);
}
function is_cookie_available1(domain, path, name, value,
secure, httponly, session, expires) {
var cm = get_CookieManager();
var enumerator = cm.enumerator;
while (enumerator.hasMoreElements()) {
var cookie = enumerator.getNext().QueryInterface(Components.interfaces.nsICookie);
if (cookie.host == domain &&
cookie.path == path &&
cookie.name == name &&
cookie.value == value &&
cookie.isSecure == secure &&
cookie.expires == expires)
return true;
function set_private_cookie(value, callback) {
return setup_chan('set?cookie=' + value, true, callback);
}
function check_cookie_presence(value, isPrivate, expected, callback) {
var chan = setup_chan('present?cookie=' + value.replace('=','|'), isPrivate, function(req) {
req.QueryInterface(Ci.nsIHttpChannel);
do_check_eq(req.responseStatus, expected ? 200 : 404);
callback(req);
});
}
function presentHandler(metadata, response) {
var present = false;
var match = /cookie=([^&]*)/.exec(metadata.queryString);
if (match) {
try {
present = metadata.getHeader("Cookie").indexOf(match[1].replace("|","=")) != -1;
} catch (x) {
}
}
return false;
response.setStatusLine("1.0", present ? 200 : 404, "");
}
function is_cookie_available2(domain, path, name, value,
secure, httponly, session, expires) {
var cookie = {
name: name,
value: value,
isDomain: true,
host: domain,
path: path,
isSecure: secure,
expires: expires,
status: 0,
policy: 0,
isSession: session,
expiry: expires,
isHttpOnly: httponly,
QueryInterface: function(iid) {
var validIIDs = [Components.interfaces.nsISupports,
Components.interfaces.nsICookie,
Components.interfaces.nsICookie2];
for (var i = 0; i < validIIDs.length; ++i) {
if (iid == validIIDs[i])
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
}
};
var cm = get_CookieManager();
return cm.cookieExists(cookie);
}
var cc_observer = null;
function setup_cookie_changed_observer() {
cc_observer = {
gotReloaded: false,
QueryInterface: function (iid) {
const interfaces = [Components.interfaces.nsIObserver,
Components.interfaces.nsISupports];
if (!interfaces.some(function(v) iid.equals(v)))
throw Components.results.NS_ERROR_NO_INTERFACE;
return this;
},
observe: function (subject, topic, data) {
if (topic == "cookie-changed") {
if (!subject) {
if (data == "reload")
this.gotReloaded = true;
}
}
}
};
var os = Components.classes["@mozilla.org/observer-service;1"].
getService(Components.interfaces.nsIObserverService);
os.addObserver(cc_observer, "cookie-changed", false);
function setHandler(metadata, response) {
response.setStatusLine("1.0", 200, "Cookie set");
var match = /cookie=([^&]*)/.exec(metadata.queryString);
if (match) {
response.setHeader("Set-Cookie", match[1]);
}
}
function run_test() {
var pb = get_PBSvc();
if (pb) { // Private Browsing might not be available
var prefBranch = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch);
prefBranch.setBoolPref("browser.privatebrowsing.keep_current_session", true);
var cm = get_CookieManager();
do_check_neq(cm, null);
setup_cookie_changed_observer();
do_check_neq(cc_observer, null);
httpserver = new HttpServer();
httpserver.registerPathHandler("/set", setHandler);
httpserver.registerPathHandler("/present", presentHandler);
httpserver.start(4444);
do_test_pending();
function check_cookie(req) {
req.QueryInterface(Ci.nsIHttpChannel);
do_check_eq(req.responseStatus, 200);
try {
// create Cookie-A
const time = (new Date("Jan 1, 2030")).getTime() / 1000;
cm.add("pbtest.example.com", "/", "C1", "V1", false, true, false, time);
// make sure Cookie-A is retrievable
do_check_true(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
do_check_true(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
// enter private browsing mode
pb.privateBrowsingEnabled = true;
// make sure the "cleared" notification was fired
do_check_true(cc_observer.gotReloaded);
cc_observer.gotReloaded = false;
// make sure Cookie-A is not retrievable
do_check_false(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
do_check_false(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
// create Cookie-B
const time2 = (new Date("Jan 2, 2030")).getTime() / 1000;
cm.add("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2);
// make sure Cookie-B is retrievable
do_check_true(is_cookie_available1("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
do_check_true(is_cookie_available2("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
// exit private browsing mode
pb.privateBrowsingEnabled = false;
// make sure the "reload" notification was fired
do_check_true(cc_observer.gotReloaded);
// make sure Cookie-B is not retrievable
do_check_false(is_cookie_available1("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
do_check_false(is_cookie_available2("pbtest2.example.com", "/", "C2", "V2", false, true, false, time2));
// make sure Cookie-A is retrievable
do_check_true(is_cookie_available1("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
do_check_true(is_cookie_available2("pbtest.example.com", "/", "C1", "V1", false, true, false, time));
} catch (e) {
do_throw("Unexpected exception while testing cookies: " + e);
do_check_true(req.getResponseHeader("Set-Cookie") != "", "expected a Set-Cookie header");
} catch (x) {
do_throw("missing Set-Cookie header");
}
prefBranch.clearUserPref("browser.privatebrowsing.keep_current_session");
runNextTest();
}
let tests = [];
function runNextTest() {
do_execute_soon(tests.shift());
}
tests.push(function() {
set_cookie("C1=V1", check_cookie);
});
tests.push(function() {
set_private_cookie("C2=V2", check_cookie);
});
tests.push(function() {
// Check that the first cookie is present in a non-private request
check_cookie_presence("C1=V1", false, true, runNextTest);
});
tests.push(function() {
// Check that the second cookie is present in a private request
check_cookie_presence("C2=V2", true, true, runNextTest);
});
tests.push(function() {
// Check that the first cookie is not present in a private request
check_cookie_presence("C1=V1", true, false, runNextTest);
});
tests.push(function() {
// Check that the second cookie is not present in a non-private request
check_cookie_presence("C2=V2", false, false, runNextTest);
});
// The following test only works in a non-e10s situation at the moment,
// since the notification needs to run in the parent process but there is
// no existing mechanism to make that happen.
if (!inChildProcess()) {
tests.push(function() {
// Simulate all private browsing instances being closed
var obsvc = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
obsvc.notifyObservers(null, "last-pb-context-exited", null);
// Check that all private cookies are now unavailable in new private requests
check_cookie_presence("C2=V2", true, false, runNextTest);
});
}
tests.push(function() { httpserver.stop(do_test_finished); });
runNextTest();
}

View File

@ -0,0 +1,3 @@
function run_test() {
run_test_in_child("../unit/test_bug248970_cookie.js");
}

View File

@ -2,6 +2,7 @@
head = head_channels_clone.js
tail =
[test_bug248970_cookie_wrap.js]
[test_cacheflags_wrap.js]
[test_channel_close_wrap.js]
[test_cookie_header_wrap.js]