Skip to content

Commit

Permalink
Bug 1184971 - Expose cookieBehavior and cookieLifetimePolicy preferen…
Browse files Browse the repository at this point in the history
…ce variants from nsICookieService. r=ehsan
  • Loading branch information
mystor committed Jul 17, 2015
1 parent 2ef7950 commit 4f798b2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
9 changes: 3 additions & 6 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "IHistory.h"
#include "nsViewSourceHandler.h"
#include "nsWhitespaceTokenizer.h"
#include "nsICookieService.h"

// we want to explore making the document own the load group
// so we can associate the document URI with the load group.
Expand Down Expand Up @@ -207,10 +208,6 @@
#endif

#include "mozIThirdPartyUtil.h"
// Values for the network.cookie.cookieBehavior pref are documented in
// nsCookieService.cpp
#define COOKIE_BEHAVIOR_ACCEPT 0 // Allow all cookies.
#define COOKIE_BEHAVIOR_REJECT_FOREIGN 1 // Reject all third-party cookies.

static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);

Expand Down Expand Up @@ -14087,8 +14084,8 @@ nsDocShell::ShouldPrepareForIntercept(nsIURI* aURI, bool aIsNavigate,
NS_ENSURE_SUCCESS(result, result);
if (isThirdPartyURI &&
(Preferences::GetInt("network.cookie.cookieBehavior",
COOKIE_BEHAVIOR_ACCEPT) ==
COOKIE_BEHAVIOR_REJECT_FOREIGN)) {
nsICookieService::BEHAVIOR_ACCEPT) ==
nsICookieService::BEHAVIOR_REJECT_FOREIGN)) {
return NS_OK;
}
}
Expand Down
10 changes: 4 additions & 6 deletions dom/storage/DOMStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "nsIPermissionManager.h"
#include "nsIPrincipal.h"
#include "nsICookiePermission.h"
#include "nsICookieService.h"

#include "mozilla/dom/StorageBinding.h"
#include "mozilla/dom/StorageEvent.h"
Expand Down Expand Up @@ -228,10 +229,6 @@ DOMStorage::BroadcastChangeNotification(const nsSubstring& aKey,
NS_DispatchToMainThread(r);
}

static const uint32_t ASK_BEFORE_ACCEPT = 1;
static const uint32_t ACCEPT_SESSION = 2;
static const uint32_t BEHAVIOR_REJECT = 2;

static const char kPermissionType[] = "cookie";
static const char kStorageEnabled[] = "dom.storage.enabled";
static const char kCookiesBehavior[] = "network.cookie.cookieBehavior";
Expand Down Expand Up @@ -282,11 +279,12 @@ DOMStorage::CanUseStorage(DOMStorage* aStorage)
uint32_t lifetimePolicy = Preferences::GetUint(kCookiesLifetimePolicy);

// Treat "ask every time" as "reject always".
if ((cookieBehavior == BEHAVIOR_REJECT || lifetimePolicy == ASK_BEFORE_ACCEPT)) {
if (cookieBehavior == nsICookieService::BEHAVIOR_REJECT ||
lifetimePolicy == nsICookieService::ASK_BEFORE_ACCEPT) {
return false;
}

if (lifetimePolicy == ACCEPT_SESSION && aStorage) {
if (lifetimePolicy == nsICookieService::ACCEPT_SESSION && aStorage) {
aStorage->mIsSessionOnly = true;
}
}
Expand Down
16 changes: 7 additions & 9 deletions netwerk/cookie/CookieServiceChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ using namespace mozilla::ipc;
namespace mozilla {
namespace net {

// Behavior pref constants
static const int32_t BEHAVIOR_ACCEPT = 0;
static const int32_t BEHAVIOR_REJECTFOREIGN = 1;
// static const int32_t BEHAVIOR_REJECT = 2;
static const int32_t BEHAVIOR_LIMITFOREIGN = 3;

// Pref string constants
static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
static const char kPrefThirdPartySession[] =
Expand All @@ -46,7 +40,7 @@ NS_IMPL_ISUPPORTS(CookieServiceChild,
nsISupportsWeakReference)

CookieServiceChild::CookieServiceChild()
: mCookieBehavior(BEHAVIOR_ACCEPT)
: mCookieBehavior(nsICookieService::BEHAVIOR_ACCEPT)
, mThirdPartySession(false)
{
NS_ASSERTION(IsNeckoChild(), "not a child process");
Expand Down Expand Up @@ -80,7 +74,9 @@ CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
int32_t val;
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
mCookieBehavior =
val >= BEHAVIOR_ACCEPT && val <= BEHAVIOR_LIMITFOREIGN ? val : BEHAVIOR_ACCEPT;
val >= nsICookieService::BEHAVIOR_ACCEPT &&
val <= nsICookieService::BEHAVIOR_LIMIT_FOREIGN
? val : nsICookieService::BEHAVIOR_ACCEPT;

bool boolval;
if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval)))
Expand All @@ -95,7 +91,9 @@ CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
bool
CookieServiceChild::RequireThirdPartyCheck()
{
return mCookieBehavior == BEHAVIOR_REJECTFOREIGN || mCookieBehavior == BEHAVIOR_LIMITFOREIGN || mThirdPartySession;
return mCookieBehavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN ||
mCookieBehavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN ||
mThirdPartySession;
}

nsresult
Expand Down
17 changes: 5 additions & 12 deletions netwerk/cookie/nsCookieService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ static const uint32_t kMaxCookiesPerHost = 150;
static const uint32_t kMaxBytesPerCookie = 4096;
static const uint32_t kMaxBytesPerPath = 1024;

// behavior pref constants
static const uint32_t BEHAVIOR_ACCEPT = 0; // allow all cookies
static const uint32_t BEHAVIOR_REJECTFOREIGN = 1; // reject all third-party cookies
static const uint32_t BEHAVIOR_REJECT = 2; // reject all cookies
static const uint32_t BEHAVIOR_LIMITFOREIGN = 3; // reject third-party cookies unless the
// eTLD already has at least one cookie

// pref string constants
static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
static const char kPrefMaxNumberOfCookies[] = "network.cookie.maxNumber";
Expand Down Expand Up @@ -711,7 +704,7 @@ NS_IMPL_ISUPPORTS(nsCookieService,

nsCookieService::nsCookieService()
: mDBState(nullptr)
, mCookieBehavior(BEHAVIOR_ACCEPT)
, mCookieBehavior(nsICookieService::BEHAVIOR_ACCEPT)
, mThirdPartySession(false)
, mMaxNumberOfCookies(kMaxNumberOfCookies)
, mMaxCookiesPerHost(kMaxCookiesPerHost)
Expand Down Expand Up @@ -3490,22 +3483,22 @@ nsCookieService::CheckPrefs(nsIURI *aHostURI,
}

// check default prefs
if (mCookieBehavior == BEHAVIOR_REJECT) {
if (mCookieBehavior == nsICookieService::BEHAVIOR_REJECT) {
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "cookies are disabled");
return STATUS_REJECTED;
}

// check if cookie is foreign
if (aIsForeign) {
if (mCookieBehavior == BEHAVIOR_ACCEPT && mThirdPartySession)
if (mCookieBehavior == nsICookieService::BEHAVIOR_ACCEPT && mThirdPartySession)
return STATUS_ACCEPT_SESSION;

if (mCookieBehavior == BEHAVIOR_REJECTFOREIGN) {
if (mCookieBehavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN) {
COOKIE_LOGFAILURE(aCookieHeader ? SET_COOKIE : GET_COOKIE, aHostURI, aCookieHeader, "context is third party");
return STATUS_REJECTED;
}

if (mCookieBehavior == BEHAVIOR_LIMITFOREIGN) {
if (mCookieBehavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN) {
uint32_t priorCookieCount = 0;
nsAutoCString hostFromURI;
aHostURI->GetHost(hostFromURI);
Expand Down
19 changes: 18 additions & 1 deletion netwerk/cookie/nsICookieService.idl
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,26 @@ interface nsIChannel;
* to set the cookie.
* data : the referrer, or "?" if unknown
*/
[scriptable, uuid(2aaa897a-293c-4d2b-a657-8c9b7136996d)]
[scriptable, uuid(f5807c53-de48-461a-8117-bd156bc2dcf0)]
interface nsICookieService : nsISupports
{
/*
* Possible values for the "network.cookie.cookieBehavior" preference.
*/
const uint32_t BEHAVIOR_ACCEPT = 0; // allow all cookies
const uint32_t BEHAVIOR_REJECT_FOREIGN = 1; // reject all third-party cookies
const uint32_t BEHAVIOR_REJECT = 2; // reject all cookies
const uint32_t BEHAVIOR_LIMIT_FOREIGN = 3; // reject third-party cookies unless the
// eTLD already has at least one cookie

/*
* Possible values for the "network.cookie.lifetimePolicy" preference.
*/
const uint32_t ACCEPT_NORMALLY = 0; // accept normally
const uint32_t ASK_BEFORE_ACCEPT = 1; // ask before accepting
const uint32_t ACCEPT_SESSION = 2; // downgrade to session
const uint32_t ACCEPT_FOR_N_DAYS = 3; // limit lifetime to N days

/*
* Get the complete cookie string associated with the URI.
*
Expand Down

0 comments on commit 4f798b2

Please sign in to comment.