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

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

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

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

@ -44,9 +44,6 @@ function runTests() {
is(state1, false, "Browser returned incorrect private mode state."); is(state1, false, "Browser returned incorrect private mode state.");
is(state2, 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 // change private mode pref
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var keepCurrentSession; var keepCurrentSession;
@ -79,18 +76,10 @@ function runTests() {
is(officialState1, state1, "Private mode reported and queried is inconsistent."); is(officialState1, state1, "Private mode reported and queried is inconsistent.");
is(officialState2, state2, "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 // reset preference states
privateBrowsing.privateBrowsingEnabled = false; privateBrowsing.privateBrowsingEnabled = false;
prefs.setBoolPref("browser.privatebrowsing.keep_current_session", keepCurrentSession); prefs.setBoolPref("browser.privatebrowsing.keep_current_session", keepCurrentSession);
is(pluginElement1.getCookie(), "foo", "Private cookie was not retrieved in public mode.");
SimpleTest.finish(); SimpleTest.finish();
} }
]]> ]]>

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

@ -238,7 +238,7 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
// without asking, or if we are in private browsing mode, just // without asking, or if we are in private browsing mode, just
// accept the cookie and return // accept the cookie and return
if ((*aIsSession && mCookiesAlwaysAcceptSession) || if ((*aIsSession && mCookiesAlwaysAcceptSession) ||
(aChannel && NS_UsePrivateBrowsing(aChannel))) { InPrivateBrowsing()) {
*aResult = true; *aResult = true;
return NS_OK; return NS_OK;
} }
@ -272,6 +272,16 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
do_GetService(NS_COOKIEPROMPTSERVICE_CONTRACTID, &rv); do_GetService(NS_COOKIEPROMPTSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return 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: // get some useful information to present to the user:
// whether a previous cookie already exists, and how many cookies this host // whether a previous cookie already exists, and how many cookies this host
// has set // has set
@ -300,7 +310,7 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI,
bool rememberDecision = false; bool rememberDecision = false;
int32_t dialogRes = nsICookiePromptService::DENY_COOKIE; int32_t dialogRes = nsICookiePromptService::DENY_COOKIE;
rv = cookiePromptService->CookieDialog(nullptr, aCookie, hostPort, rv = cookiePromptService->CookieDialog(parent, aCookie, hostPort,
countFromHost, foundCookie, countFromHost, foundCookie,
&rememberDecision, &dialogRes); &rememberDecision, &dialogRes);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
@ -358,3 +368,14 @@ nsCookiePermission::Observe(nsISupports *aSubject,
PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get()); PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get());
return NS_OK; 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 "nsIObserver.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "prlong.h" #include "prlong.h"
#include "nsIPrivateBrowsingService.h"
#include "mozIThirdPartyUtil.h" #include "mozIThirdPartyUtil.h"
class nsIPrefBranch; class nsIPrefBranch;
@ -34,8 +35,10 @@ public:
private: private:
bool EnsureInitialized() { return (mPermMgr != NULL && mThirdPartyUtil != NULL) || Init(); }; bool EnsureInitialized() { return (mPermMgr != NULL && mThirdPartyUtil != NULL) || Init(); };
bool InPrivateBrowsing();
nsCOMPtr<nsIPermissionManager> mPermMgr; nsCOMPtr<nsIPermissionManager> mPermMgr;
nsCOMPtr<nsIPrivateBrowsingService> mPBService;
nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil; nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
int64_t mCookiesLifetimeSec; // lifetime limit specified in seconds int64_t mCookiesLifetimeSec; // lifetime limit specified in seconds

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

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

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

@ -47,7 +47,6 @@
#include "mozilla/storage.h" #include "mozilla/storage.h"
#include "mozilla/Util.h" // for DebugOnly #include "mozilla/Util.h" // for DebugOnly
#include "mozilla/Attributes.h" #include "mozilla/Attributes.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/Likely.h" #include "mozilla/Likely.h"
#include "nsIAppsService.h" #include "nsIAppsService.h"
#include "mozIApplication.h" #include "mozIApplication.h"
@ -685,7 +684,7 @@ nsCookieService::Init()
NS_ENSURE_STATE(mObserverService); NS_ENSURE_STATE(mObserverService);
mObserverService->AddObserver(this, "profile-before-change", true); mObserverService->AddObserver(this, "profile-before-change", true);
mObserverService->AddObserver(this, "profile-do-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); mPermissionService = do_GetService(NS_COOKIEPERMISSION_CONTRACTID);
if (!mPermissionService) { if (!mPermissionService) {
@ -707,7 +706,17 @@ nsCookieService::InitDBStates()
mDefaultDBState = new DBState(); mDefaultDBState = new DBState();
mDBState = mDefaultDBState; 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. // Get our cookie file.
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
@ -1468,11 +1477,28 @@ nsCookieService::Observe(nsISupports *aSubject,
if (prefBranch) if (prefBranch)
PrefChanged(prefBranch); PrefChanged(prefBranch);
} else if (!strcmp(aTopic, "last-pb-context-exited")) { } else if (!strcmp(aTopic, NS_PRIVATE_BROWSING_SWITCH_TOPIC)) {
// Flush all the cookies stored by private browsing contexts if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_ENTER).Equals(aData)) {
mPrivateDBState = new DBState(); 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; return NS_OK;
} }
@ -1514,11 +1540,9 @@ nsCookieService::GetCookieStringCommon(nsIURI *aHostURI,
NS_GetAppInfo(aChannel, &appId, &inBrowserElement); NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
} }
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
nsAutoCString result; nsAutoCString result;
GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId, GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId,
inBrowserElement, isPrivate, result); inBrowserElement, result);
*aCookie = result.IsEmpty() ? nullptr : ToNewCString(result); *aCookie = result.IsEmpty() ? nullptr : ToNewCString(result);
return NS_OK; return NS_OK;
} }
@ -1565,13 +1589,10 @@ nsCookieService::SetCookieStringCommon(nsIURI *aHostURI,
NS_GetAppInfo(aChannel, &appId, &inBrowserElement); NS_GetAppInfo(aChannel, &appId, &inBrowserElement);
} }
bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel);
nsDependentCString cookieString(aCookieHeader); nsDependentCString cookieString(aCookieHeader);
nsDependentCString serverTime(aServerTime ? aServerTime : ""); nsDependentCString serverTime(aServerTime ? aServerTime : "");
SetCookieStringInternal(aHostURI, isForeign, cookieString, SetCookieStringInternal(aHostURI, isForeign, cookieString,
serverTime, aFromHttp, appId, inBrowserElement, serverTime, aFromHttp, appId, inBrowserElement);
isPrivate, aChannel);
return NS_OK; return NS_OK;
} }
@ -1582,9 +1603,7 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
const nsCString &aServerTime, const nsCString &aServerTime,
bool aFromHttp, bool aFromHttp,
uint32_t aAppId, uint32_t aAppId,
bool aInBrowserElement, bool aInBrowserElement)
bool aIsPrivate,
nsIChannel *aChannel)
{ {
NS_ASSERTION(aHostURI, "null host!"); NS_ASSERTION(aHostURI, "null host!");
@ -1593,9 +1612,6 @@ nsCookieService::SetCookieStringInternal(nsIURI *aHostURI,
return; return;
} }
AutoRestore<DBState*> savePrevDBState(mDBState);
mDBState = aIsPrivate ? mPrivateDBState : mDefaultDBState;
// get the base domain for the host URI. // get the base domain for the host URI.
// e.g. for "www.bbc.co.uk", this would be "bbc.co.uk". // 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 // 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 // process each cookie in the header
while (SetCookieInternal(aHostURI, key, requireHostMatch, cookieStatus, while (SetCookieInternal(aHostURI, key, requireHostMatch, cookieStatus,
aCookieHeader, serverTime, aFromHttp, aChannel)) { aCookieHeader, serverTime, aFromHttp)) {
// document.cookie can only set one cookie at a time // document.cookie can only set one cookie at a time
if (!aFromHttp) if (!aFromHttp)
break; break;
@ -2451,7 +2467,6 @@ nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
bool aHttpBound, bool aHttpBound,
uint32_t aAppId, uint32_t aAppId,
bool aInBrowserElement, bool aInBrowserElement,
bool aIsPrivate,
nsCString &aCookieString) nsCString &aCookieString)
{ {
NS_ASSERTION(aHostURI, "null host!"); NS_ASSERTION(aHostURI, "null host!");
@ -2461,9 +2476,6 @@ nsCookieService::GetCookieStringInternal(nsIURI *aHostURI,
return; return;
} }
AutoRestore<DBState*> savePrevDBState(mDBState);
mDBState = aIsPrivate ? mPrivateDBState : mDefaultDBState;
// get the base domain, host, and path from the URI. // 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". // 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 // file:// URI's (i.e. with an empty host) are allowed, but any other
@ -2645,8 +2657,7 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI,
CookieStatus aStatus, CookieStatus aStatus,
nsDependentCString &aCookieHeader, nsDependentCString &aCookieHeader,
int64_t aServerTime, int64_t aServerTime,
bool aFromHttp, bool aFromHttp)
nsIChannel *aChannel)
{ {
NS_ASSERTION(aHostURI, "null host!"); NS_ASSERTION(aHostURI, "null host!");
@ -2716,8 +2727,11 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI,
// to determine if we can set the cookie // to determine if we can set the cookie
if (mPermissionService) { if (mPermissionService) {
bool permission; 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, mPermissionService->CanSetCookie(aHostURI,
aChannel, nullptr,
static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)), static_cast<nsICookie2*>(static_cast<nsCookie*>(cookie)),
&cookieAttributes.isSession, &cookieAttributes.isSession,
&cookieAttributes.expiryTime, &cookieAttributes.expiryTime,

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

@ -267,10 +267,10 @@ class nsCookieService : public nsICookieService
nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, bool &aRequireHostMatch); nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, bool &aRequireHostMatch);
nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain); nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain);
nsresult GetCookieStringCommon(nsIURI *aHostURI, nsIChannel *aChannel, bool aHttpBound, char** aCookie); 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); 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); 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, nsIChannel* aChannel); 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 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 RemoveCookieFromList(const nsListIter &aIter, mozIStorageBindingParamsArray *aParamsArray = NULL);
void AddCookieToList(const nsCookieKey& aKey, nsCookie *aCookie, DBState *aDBState, mozIStorageBindingParamsArray *aParamsArray, bool aWriteToDB = true); 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; nsCOMPtr<mozIStorageService> mStorageService;
// we have two separate DB states: one for normal browsing and one for // we have two separate DB states: one for normal browsing and one for
// private browsing, switching between them on a per-cookie-request basis. // private browsing, switching between them as appropriate. this state
// this state encapsulates both the in-memory table and the on-disk DB. // encapsulates both the in-memory table and the on-disk DB.
// note that the private states' dbConn should always be null - we never // 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. // want to be dealing with the on-disk DB when in private browsing.
DBState *mDBState; DBState *mDBState;

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

@ -2,133 +2,149 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Cu = Components.utils; var _PBSvc = null;
const Ci = Components.interfaces; function get_PBSvc() {
const Cc = Components.classes; if (_PBSvc)
return _PBSvc;
Cu.import("resource://testing-common/httpd.js"); try {
Cu.import("resource://gre/modules/Services.jsm"); _PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"].
getService(Components.interfaces.nsIPrivateBrowsingService);
var httpserver; return _PBSvc;
} catch (e) {}
function inChildProcess() { return null;
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;
} }
function setup_chan(path, isPrivate, callback) { var _CMSvc = null;
var chan = makeChan(path); function get_CookieManager() {
chan.QueryInterface(Ci.nsIPrivateBrowsingChannel).setPrivate(isPrivate); if (_CMSvc)
chan.asyncOpen(new ChannelListener(callback), null); return _CMSvc;
}
function set_cookie(value, callback) { return _CMSvc = Components.classes["@mozilla.org/cookiemanager;1"].
return setup_chan('set?cookie=' + value, false, callback); getService(Components.interfaces.nsICookieManager2);
} }
function set_private_cookie(value, callback) { function is_cookie_available1(domain, path, name, value,
return setup_chan('set?cookie=' + value, true, callback); 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) { function is_cookie_available2(domain, path, name, value,
var chan = setup_chan('present?cookie=' + value.replace('=','|'), isPrivate, function(req) { secure, httponly, session, expires) {
req.QueryInterface(Ci.nsIHttpChannel); var cookie = {
do_check_eq(req.responseStatus, expected ? 200 : 404); name: name,
callback(req); value: value,
}); isDomain: true,
} host: domain,
path: path,
function presentHandler(metadata, response) { isSecure: secure,
var present = false; expires: expires,
var match = /cookie=([^&]*)/.exec(metadata.queryString); status: 0,
if (match) { policy: 0,
try { isSession: session,
present = metadata.getHeader("Cookie").indexOf(match[1].replace("|","=")) != -1; expiry: expires,
} catch (x) { 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) { var cc_observer = null;
response.setStatusLine("1.0", 200, "Cookie set"); function setup_cookie_changed_observer() {
var match = /cookie=([^&]*)/.exec(metadata.queryString); cc_observer = {
if (match) { gotReloaded: false,
response.setHeader("Set-Cookie", match[1]); 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() { function run_test() {
httpserver = new HttpServer(); var pb = get_PBSvc();
httpserver.registerPathHandler("/set", setHandler); if (pb) { // Private Browsing might not be available
httpserver.registerPathHandler("/present", presentHandler); var prefBranch = Components.classes["@mozilla.org/preferences-service;1"].
httpserver.start(4444); getService(Components.interfaces.nsIPrefBranch);
prefBranch.setBoolPref("browser.privatebrowsing.keep_current_session", true);
do_test_pending();
var cm = get_CookieManager();
function check_cookie(req) { do_check_neq(cm, null);
req.QueryInterface(Ci.nsIHttpChannel);
do_check_eq(req.responseStatus, 200); setup_cookie_changed_observer();
do_check_neq(cc_observer, null);
try { try {
do_check_true(req.getResponseHeader("Set-Cookie") != "", "expected a Set-Cookie header"); // create Cookie-A
} catch (x) { const time = (new Date("Jan 1, 2030")).getTime() / 1000;
do_throw("missing Set-Cookie header"); 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 head = head_channels_clone.js
tail = tail =
[test_bug248970_cookie_wrap.js]
[test_cacheflags_wrap.js] [test_cacheflags_wrap.js]
[test_channel_close_wrap.js] [test_channel_close_wrap.js]
[test_cookie_header_wrap.js] [test_cookie_header_wrap.js]