Bug 1547114 - Part 2: Read the cookie lifetime policy from nsContentUtils; r=baku

Differential Revision: https://phabricator.services.mozilla.com/D28915

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Ehsan Akhgari 2019-04-26 14:30:27 +00:00
Родитель 9421931e25
Коммит 29825d4324
3 изменённых файлов: 14 добавлений и 59 удалений

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

@ -3360,6 +3360,11 @@ class nsContentUtils {
static bool HighPriorityEventPendingForTopLevelDocumentBeforeContentfulPaint(
Document* aDocument);
/**
* Gets the global cookie lifetime policy.
*/
static uint32_t GetCookieLifetimePolicy() { return sCookiesLifetimePolicy; }
private:
static bool InitializeEventTable();

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

@ -10,12 +10,11 @@
#include "nsICookie2.h"
#include "nsIServiceManager.h"
#include "nsICookieManager.h"
#include "nsICookieService.h"
#include "nsNetUtil.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIProtocolHandler.h"
#include "nsIURI.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIChannel.h"
#include "nsIHttpChannelInternal.h"
#include "nsIDOMWindow.h"
@ -27,20 +26,13 @@
#include "nsNetCID.h"
#include "prtime.h"
#include "mozilla/StaticPtr.h"
#include "nsContentUtils.h"
/****************************************************************
************************ nsCookiePermission ********************
****************************************************************/
// values for mCookiesLifetimePolicy
// 0 == accept normally
// 1 == ask before accepting, no more supported, treated like ACCEPT_NORMALLY
// (Bug 606655). 2 == downgrade to session 3 == limit lifetime to N days
static const uint32_t ACCEPT_NORMALLY = 0;
static const uint32_t ACCEPT_SESSION = 2;
static const bool kDefaultPolicy = true;
static const char kCookiesLifetimePolicy[] = "network.cookie.lifetimePolicy";
static const nsLiteralCString kPermissionType(NS_LITERAL_CSTRING("cookie"));
@ -48,7 +40,7 @@ namespace {
mozilla::StaticRefPtr<nsCookiePermission> gSingleton;
}
NS_IMPL_ISUPPORTS(nsCookiePermission, nsICookiePermission, nsIObserver)
NS_IMPL_ISUPPORTS(nsCookiePermission, nsICookiePermission)
// static
already_AddRefed<nsICookiePermission> nsCookiePermission::GetOrCreate() {
@ -71,31 +63,9 @@ bool nsCookiePermission::Init() {
mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID, &rv);
if (NS_FAILED(rv)) return false;
// failure to access the pref service is non-fatal...
nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (prefBranch) {
prefBranch->AddObserver(kCookiesLifetimePolicy, this, false);
PrefChanged(prefBranch, nullptr);
}
return true;
}
void nsCookiePermission::PrefChanged(nsIPrefBranch *aPrefBranch,
const char *aPref) {
int32_t val;
#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P))
if (PREF_CHANGED(kCookiesLifetimePolicy) &&
NS_SUCCEEDED(aPrefBranch->GetIntPref(kCookiesLifetimePolicy, &val))) {
if (val != static_cast<int32_t>(ACCEPT_SESSION)) {
val = ACCEPT_NORMALLY;
}
mCookiesLifetimePolicy = val;
}
}
NS_IMETHODIMP
nsCookiePermission::SetAccess(nsIURI *aURI, nsCookieAccess aAccess) {
// Lazily initialize ourselves
@ -159,7 +129,8 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI, nsIChannel *aChannel,
// now we need to figure out what type of accept policy we're dealing with
// if we accept cookies normally, just bail and return
if (mCookiesLifetimePolicy == ACCEPT_NORMALLY) {
if (nsContentUtils::GetCookieLifetimePolicy() ==
nsICookieService::ACCEPT_NORMALLY) {
*aResult = true;
return NS_OK;
}
@ -171,7 +142,8 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI, nsIChannel *aChannel,
// We are accepting the cookie, but,
// if it's not a session cookie, we may have to limit its lifetime.
if (!*aIsSession && delta > 0) {
if (mCookiesLifetimePolicy == ACCEPT_SESSION) {
if (nsContentUtils::GetCookieLifetimePolicy() ==
nsICookieService::ACCEPT_SESSION) {
// limit lifetime to session
*aIsSession = true;
}
@ -180,15 +152,3 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI, nsIChannel *aChannel,
return NS_OK;
}
NS_IMETHODIMP
nsCookiePermission::Observe(nsISupports *aSubject, const char *aTopic,
const char16_t *aData) {
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;
}

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

@ -7,31 +7,23 @@
#include "nsICookiePermission.h"
#include "nsIPermissionManager.h"
#include "nsIObserver.h"
#include "nsCOMPtr.h"
#include "mozIThirdPartyUtil.h"
class nsIPrefBranch;
class nsCookiePermission final : public nsICookiePermission,
public nsIObserver {
class nsCookiePermission final : public nsICookiePermission {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSICOOKIEPERMISSION
NS_DECL_NSIOBSERVER
// Singleton accessor
static already_AddRefed<nsICookiePermission> GetOrCreate();
static void Shutdown();
bool Init();
void PrefChanged(nsIPrefBranch *, const char *);
private:
nsCookiePermission()
: mCookiesLifetimePolicy(0) // ACCEPT_NORMALLY
{}
virtual ~nsCookiePermission() {}
~nsCookiePermission() = default;
bool EnsureInitialized() {
return (mPermMgr != nullptr && mThirdPartyUtil != nullptr) || Init();
@ -39,8 +31,6 @@ class nsCookiePermission final : public nsICookiePermission,
nsCOMPtr<nsIPermissionManager> mPermMgr;
nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
uint8_t mCookiesLifetimePolicy; // pref for how long cookies are stored
};
#endif