Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 29825d4

Browse files
author
Ehsan Akhgari
committed
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
1 parent 9421931 commit 29825d4

3 files changed

Lines changed: 14 additions & 59 deletions

File tree

dom/base/nsContentUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,6 +3360,11 @@ class nsContentUtils {
33603360
static bool HighPriorityEventPendingForTopLevelDocumentBeforeContentfulPaint(
33613361
Document* aDocument);
33623362

3363+
/**
3364+
* Gets the global cookie lifetime policy.
3365+
*/
3366+
static uint32_t GetCookieLifetimePolicy() { return sCookiesLifetimePolicy; }
3367+
33633368
private:
33643369
static bool InitializeEventTable();
33653370

extensions/cookie/nsCookiePermission.cpp

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
#include "nsICookie2.h"
1111
#include "nsIServiceManager.h"
1212
#include "nsICookieManager.h"
13+
#include "nsICookieService.h"
1314
#include "nsNetUtil.h"
1415
#include "nsIInterfaceRequestorUtils.h"
1516
#include "nsIProtocolHandler.h"
1617
#include "nsIURI.h"
17-
#include "nsIPrefService.h"
18-
#include "nsIPrefBranch.h"
1918
#include "nsIChannel.h"
2019
#include "nsIHttpChannelInternal.h"
2120
#include "nsIDOMWindow.h"
@@ -27,28 +26,21 @@
2726
#include "nsNetCID.h"
2827
#include "prtime.h"
2928
#include "mozilla/StaticPtr.h"
29+
#include "nsContentUtils.h"
3030

3131
/****************************************************************
3232
************************ nsCookiePermission ********************
3333
****************************************************************/
3434

35-
// values for mCookiesLifetimePolicy
36-
// 0 == accept normally
37-
// 1 == ask before accepting, no more supported, treated like ACCEPT_NORMALLY
38-
// (Bug 606655). 2 == downgrade to session 3 == limit lifetime to N days
39-
static const uint32_t ACCEPT_NORMALLY = 0;
40-
static const uint32_t ACCEPT_SESSION = 2;
41-
4235
static const bool kDefaultPolicy = true;
43-
static const char kCookiesLifetimePolicy[] = "network.cookie.lifetimePolicy";
4436

4537
static const nsLiteralCString kPermissionType(NS_LITERAL_CSTRING("cookie"));
4638

4739
namespace {
4840
mozilla::StaticRefPtr<nsCookiePermission> gSingleton;
4941
}
5042

51-
NS_IMPL_ISUPPORTS(nsCookiePermission, nsICookiePermission, nsIObserver)
43+
NS_IMPL_ISUPPORTS(nsCookiePermission, nsICookiePermission)
5244

5345
// static
5446
already_AddRefed<nsICookiePermission> nsCookiePermission::GetOrCreate() {
@@ -71,31 +63,9 @@ bool nsCookiePermission::Init() {
7163
mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID, &rv);
7264
if (NS_FAILED(rv)) return false;
7365

74-
// failure to access the pref service is non-fatal...
75-
nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
76-
if (prefBranch) {
77-
prefBranch->AddObserver(kCookiesLifetimePolicy, this, false);
78-
PrefChanged(prefBranch, nullptr);
79-
}
80-
8166
return true;
8267
}
8368

84-
void nsCookiePermission::PrefChanged(nsIPrefBranch *aPrefBranch,
85-
const char *aPref) {
86-
int32_t val;
87-
88-
#define PREF_CHANGED(_P) (!aPref || !strcmp(aPref, _P))
89-
90-
if (PREF_CHANGED(kCookiesLifetimePolicy) &&
91-
NS_SUCCEEDED(aPrefBranch->GetIntPref(kCookiesLifetimePolicy, &val))) {
92-
if (val != static_cast<int32_t>(ACCEPT_SESSION)) {
93-
val = ACCEPT_NORMALLY;
94-
}
95-
mCookiesLifetimePolicy = val;
96-
}
97-
}
98-
9969
NS_IMETHODIMP
10070
nsCookiePermission::SetAccess(nsIURI *aURI, nsCookieAccess aAccess) {
10171
// Lazily initialize ourselves
@@ -159,7 +129,8 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI, nsIChannel *aChannel,
159129

160130
// now we need to figure out what type of accept policy we're dealing with
161131
// if we accept cookies normally, just bail and return
162-
if (mCookiesLifetimePolicy == ACCEPT_NORMALLY) {
132+
if (nsContentUtils::GetCookieLifetimePolicy() ==
133+
nsICookieService::ACCEPT_NORMALLY) {
163134
*aResult = true;
164135
return NS_OK;
165136
}
@@ -171,7 +142,8 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI, nsIChannel *aChannel,
171142
// We are accepting the cookie, but,
172143
// if it's not a session cookie, we may have to limit its lifetime.
173144
if (!*aIsSession && delta > 0) {
174-
if (mCookiesLifetimePolicy == ACCEPT_SESSION) {
145+
if (nsContentUtils::GetCookieLifetimePolicy() ==
146+
nsICookieService::ACCEPT_SESSION) {
175147
// limit lifetime to session
176148
*aIsSession = true;
177149
}
@@ -180,15 +152,3 @@ nsCookiePermission::CanSetCookie(nsIURI *aURI, nsIChannel *aChannel,
180152

181153
return NS_OK;
182154
}
183-
184-
NS_IMETHODIMP
185-
nsCookiePermission::Observe(nsISupports *aSubject, const char *aTopic,
186-
const char16_t *aData) {
187-
nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
188-
NS_ASSERTION(!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic),
189-
"unexpected topic - we only deal with pref changes!");
190-
191-
if (prefBranch)
192-
PrefChanged(prefBranch, NS_LossyConvertUTF16toASCII(aData).get());
193-
return NS_OK;
194-
}

extensions/cookie/nsCookiePermission.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,30 @@
77

88
#include "nsICookiePermission.h"
99
#include "nsIPermissionManager.h"
10-
#include "nsIObserver.h"
1110
#include "nsCOMPtr.h"
1211
#include "mozIThirdPartyUtil.h"
1312

14-
class nsIPrefBranch;
1513

16-
class nsCookiePermission final : public nsICookiePermission,
17-
public nsIObserver {
14+
class nsCookiePermission final : public nsICookiePermission {
1815
public:
1916
NS_DECL_ISUPPORTS
2017
NS_DECL_NSICOOKIEPERMISSION
21-
NS_DECL_NSIOBSERVER
2218

2319
// Singleton accessor
2420
static already_AddRefed<nsICookiePermission> GetOrCreate();
2521
static void Shutdown();
2622

2723
bool Init();
28-
void PrefChanged(nsIPrefBranch *, const char *);
2924

3025
private:
31-
nsCookiePermission()
32-
: mCookiesLifetimePolicy(0) // ACCEPT_NORMALLY
33-
{}
34-
virtual ~nsCookiePermission() {}
26+
~nsCookiePermission() = default;
3527

3628
bool EnsureInitialized() {
3729
return (mPermMgr != nullptr && mThirdPartyUtil != nullptr) || Init();
3830
};
3931

4032
nsCOMPtr<nsIPermissionManager> mPermMgr;
4133
nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
42-
43-
uint8_t mCookiesLifetimePolicy; // pref for how long cookies are stored
4434
};
4535

4636
#endif

0 commit comments

Comments
 (0)