Backout 7a0fe388a24b, 2a9fbd15cad4, 320c1567e431 & d907ac7bf669 (bug 722850) for xpcshell & browser-chrome failures

This commit is contained in:
Ed Morley 2012-11-16 14:18:41 +00:00
Родитель f4938f9319
Коммит fd965f5f0b
12 изменённых файлов: 228 добавлений и 214 удалений

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

@ -67,7 +67,6 @@ struct ParamTraits<SerializedLoadContext>
{
WriteParam(aMsg, aParam.mIsNotNull);
WriteParam(aMsg, aParam.mIsContent);
WriteParam(aMsg, aParam.mIsPrivateBitValid);
WriteParam(aMsg, aParam.mUsePrivateBrowsing);
WriteParam(aMsg, aParam.mAppId);
WriteParam(aMsg, aParam.mIsInBrowserElement);
@ -77,7 +76,6 @@ struct ParamTraits<SerializedLoadContext>
{
if (!ReadParam(aMsg, aIter, &aResult->mIsNotNull) ||
!ReadParam(aMsg, aIter, &aResult->mIsContent) ||
!ReadParam(aMsg, aIter, &aResult->mIsPrivateBitValid) ||
!ReadParam(aMsg, aIter, &aResult->mUsePrivateBrowsing) ||
!ReadParam(aMsg, aIter, &aResult->mAppId) ||
!ReadParam(aMsg, aIter, &aResult->mIsInBrowserElement)) {

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

@ -669,23 +669,6 @@ GetPrivacyFromNPP(NPP npp, bool* aPrivate)
return NS_OK;
}
static already_AddRefed<nsIChannel>
GetChannelFromNPP(NPP npp)
{
nsCOMPtr<nsIDocument> doc = GetDocumentFromNPP(npp);
if (!doc)
return nullptr;
nsCOMPtr<nsPIDOMWindow> domwindow = doc->GetWindow();
nsCOMPtr<nsIChannel> channel;
if (domwindow) {
nsCOMPtr<nsIDocShell> docShell = domwindow->GetDocShell();
if (docShell) {
docShell->GetCurrentDocumentChannel(getter_AddRefs(channel));
}
}
return channel.forget();
}
static NPIdentifier
doGetIdentifier(JSContext *cx, const NPUTF8* name)
{
@ -2657,9 +2640,7 @@ _getvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
return NPERR_GENERIC_ERROR;
}
nsCOMPtr<nsIChannel> channel = GetChannelFromNPP(instance);
if (NS_FAILED(cookieService->GetCookieString(uri, channel, value)) ||
if (NS_FAILED(cookieService->GetCookieString(uri, nullptr, value)) ||
!*value) {
return NPERR_GENERIC_ERROR;
}
@ -2712,12 +2693,10 @@ _setvalueforurl(NPP instance, NPNURLVariable variable, const char *url,
nsCOMPtr<nsIPrompt> prompt;
nsPluginHost::GetPrompt(nullptr, getter_AddRefs(prompt));
nsCOMPtr<nsIChannel> channel = GetChannelFromNPP(instance);
char *cookie = (char*)value;
char c = cookie[len];
cookie[len] = '\0';
rv = cookieService->SetCookieString(uriIn, prompt, cookie, channel);
rv = cookieService->SetCookieString(uriIn, prompt, cookie, nullptr);
cookie[len] = c;
if (NS_SUCCEEDED(rv))
return NPERR_NO_ERROR;

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

@ -44,9 +44,6 @@ function runTests() {
is(state1, false, "Browser returned incorrect private mode state.");
is(state2, false, "Browser returned incorrect private mode state.");
pluginElement1.setCookie("foo");
is(pluginElement1.getCookie(), "foo", "Cookie was set and retrieved correctly in public mode.");
// change private mode pref
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var keepCurrentSession;
@ -79,18 +76,10 @@ function runTests() {
is(officialState1, state1, "Private mode reported and queried is inconsistent.");
is(officialState2, state2, "Private mode reported and queried is inconsistent.");
// It would be nice to assert that we don't see the public cookie in private mode,
// but the NPAPI complains when the resulting string is empty.
// is(pluginElement1.getCookie(), "", "Public cookie was not retrieved in private mode.");
pluginElement1.setCookie("bar");
is(pluginElement1.getCookie(), "bar", "Cookie was set and retrieved correctly in private mode.");
// reset preference states
privateBrowsing.privateBrowsingEnabled = false;
prefs.setBoolPref("browser.privatebrowsing.keep_current_session", keepCurrentSession);
is(pluginElement1.getCookie(), "foo", "Private cookie was not retrieved in public mode.");
SimpleTest.finish();
}
]]>

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

@ -238,7 +238,7 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
// without asking, or if we are in private browsing mode, just
// accept the cookie and return
if ((*aIsSession && mCookiesAlwaysAcceptSession) ||
(aChannel && NS_UsePrivateBrowsing(aChannel))) {
InPrivateBrowsing()) {
*aResult = true;
return NS_OK;
}
@ -272,6 +272,16 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
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
@ -300,7 +310,7 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
bool rememberDecision = false;
int32_t dialogRes = nsICookiePromptService::DENY_COOKIE;
rv = cookiePromptService->CookieDialog(nullptr, aCookie, hostPort,
rv = cookiePromptService->CookieDialog(parent, aCookie, hostPort,
countFromHost, foundCookie,
&rememberDecision, &dialogRes);
if (NS_FAILED(rv)) return rv;
@ -358,3 +368,14 @@ nsCookiePermission::Observe(nsISupports *aSubject,
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;
}

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

@ -10,6 +10,7 @@
#include "nsIObserver.h"
#include "nsCOMPtr.h"
#include "prlong.h"
#include "nsIPrivateBrowsingService.h"
#include "mozIThirdPartyUtil.h"
class nsIPrefBranch;
@ -34,8 +35,10 @@ public:
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

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

@ -11,27 +11,6 @@
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 {
@ -69,11 +48,11 @@ CookieServiceParent::RecvGetCookieString(const URIParams& aHost,
return false;
uint32_t appId;
bool isInBrowserElement, isPrivate;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement, isPrivate);
bool isInBrowserElement;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement);
mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aFromHttp, appId,
isInBrowserElement, isPrivate, *aResult);
isInBrowserElement, *aResult);
return true;
}
@ -96,17 +75,33 @@ CookieServiceParent::RecvSetCookieString(const URIParams& aHost,
return false;
uint32_t appId;
bool isInBrowserElement, isPrivate;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement, isPrivate);
bool isInBrowserElement;
GetAppInfoFromLoadContext(aLoadContext, appId, isInBrowserElement);
nsDependentCString cookieString(aCookieString, 0);
//TODO: bug 812475, pass a real channel object
mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString,
aServerTime, aFromHttp, appId,
isInBrowserElement, isPrivate, nullptr);
isInBrowserElement);
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;
}
}
}
}

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

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

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

@ -47,7 +47,6 @@
#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"
@ -685,7 +684,7 @@ nsCookieService::Init()
NS_ENSURE_STATE(mObserverService);
mObserverService->AddObserver(this, "profile-before-change", true);
mObserverService->AddObserver(this, "profile-do-change", true);
mObserverService->AddObserver(this, "last-pb-context-exited", true);
mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, true);
mPermissionService = do_GetService(NS_COOKIEPERMISSION_CONTRACTID);
if (!mPermissionService) {
@ -707,7 +706,17 @@ nsCookieService::InitDBStates()
mDefaultDBState = new DBState();
mDBState = mDefaultDBState;
mPrivateDBState = new DBState();
// 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;
}
}
// Get our cookie file.
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
@ -1468,11 +1477,28 @@ nsCookieService::Observe(nsISupports *aSubject,
if (prefBranch)
PrefChanged(prefBranch);
} else if (!strcmp(aTopic, "last-pb-context-exited")) {
// Flush all the cookies stored by private browsing contexts
mPrivateDBState = new DBState();
}
} 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());
}
return NS_OK;
}
@ -1514,11 +1540,9 @@ nsCookieService::GetCookieStringCommon(nsIURI *aHostURI,
NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
}
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
nsAutoCString result;
GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId,
inBrowserElement, isPrivate, result);
inBrowserElement, result);
*aCookie = result.IsEmpty() ? nullptr : ToNewCString(result);
return NS_OK;
}
@ -1565,13 +1589,10 @@ 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,
isPrivate, aChannel);
serverTime, aFromHttp, appId, inBrowserElement);
return NS_OK;
}
@ -1582,9 +1603,7 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
const nsCString &aServerTime,
bool aFromHttp,
uint32_t aAppId,
bool aInBrowserElement,
bool aIsPrivate,
nsIChannel *aChannel)
bool aInBrowserElement)
{
NS_ASSERTION(aHostURI, "null host!");
@ -1593,9 +1612,6 @@ 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
@ -1643,7 +1659,7 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
// process each cookie in the header
while (SetCookieInternal(aHostURI, key, requireHostMatch, cookieStatus,
aCookieHeader, serverTime, aFromHttp, aChannel)) {
aCookieHeader, serverTime, aFromHttp)) {
// document.cookie can only set one cookie at a time
if (!aFromHttp)
break;
@ -2451,7 +2467,6 @@ nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
bool aHttpBound,
uint32_t aAppId,
bool aInBrowserElement,
bool aIsPrivate,
nsCString &aCookieString)
{
NS_ASSERTION(aHostURI, "null host!");
@ -2461,9 +2476,6 @@ 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
@ -2645,8 +2657,7 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI,
CookieStatus aStatus,
nsDependentCString &aCookieHeader,
int64_t aServerTime,
bool aFromHttp,
nsIChannel *aChannel)
bool aFromHttp)
{
NS_ASSERTION(aHostURI, "null host!");
@ -2716,8 +2727,11 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI,
// 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,
aChannel,
nullptr,
static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)),
&cookieAttributes.isSession,
&cookieAttributes.expiryTime,

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

@ -267,10 +267,10 @@ 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, bool aIsPrivate, nsCString &aCookie);
void GetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, bool aHttpBound, uint32_t aAppId, bool aInBrowserElement, 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, nsIChannel* aChannel);
bool SetCookieInternal(nsIURI *aHostURI, const nsCookieKey& aKey, bool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, int64_t aServerTime, bool aFromHttp, nsIChannel* aChannel);
void SetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, nsDependentCString &aCookieHeader, const nsCString &aServerTime, bool aFromHttp, uint32_t aAppId, bool aInBrowserElement);
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);
void AddCookieToList(const nsCookieKey& aKey, nsCookie *aCookie, DBState *aDBState, mozIStorageBindingParamsArray *aParamsArray, bool aWriteToDB = true);
@ -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 on a per-cookie-request basis.
// this state encapsulates both the in-memory table and the on-disk DB.
// private browsing, switching between them as appropriate. 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;

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

@ -2,133 +2,149 @@
* 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/. */
const Cu = Components.utils;
const Ci = Components.interfaces;
const Cc = Components.classes;
var _PBSvc = null;
function get_PBSvc() {
if (_PBSvc)
return _PBSvc;
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;
try {
_PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"].
getService(Components.interfaces.nsIPrivateBrowsingService);
return _PBSvc;
} catch (e) {}
return null;
}
function setup_chan(path, isPrivate, callback) {
var chan = makeChan(path);
chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate);
chan.asyncOpen(new ChannelListener(callback), null);
}
var _CMSvc = null;
function get_CookieManager() {
if (_CMSvc)
return _CMSvc;
function set_cookie(value, callback) {
return setup_chan('set?cookie=' + value, false, callback);
return _CMSvc = Components.classes["@mozilla.org/cookiemanager;1"].
getService(Components.interfaces.nsICookieManager2);
}
function set_private_cookie(value, callback) {
return setup_chan('set?cookie=' + value, true, 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;
}
return false;
}
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) {
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;
}
}
response.setStatusLine("1.0", present ? 200 : 404, "");
};
var cm = get_CookieManager();
return cm.cookieExists(cookie);
}
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]);
}
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 run_test() {
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);
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);
try {
do_check_true(req.getResponseHeader("Set-Cookie") != "", "expected a Set-Cookie header");
} catch (x) {
do_throw("missing Set-Cookie header");
// 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);
}
runNextTest();
prefBranch.clearUserPref("browser.privatebrowsing.keep_current_session");
}
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();
}

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

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

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

@ -2,7 +2,6 @@
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]