2010-03-25 16:02:28 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2010-03-25 16:02:28 -07:00
|
|
|
|
|
|
|
#include "mozilla/net/CookieServiceChild.h"
|
2012-08-23 12:33:46 -07:00
|
|
|
|
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2010-03-25 16:02:28 -07:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
|
|
|
#include "nsIURI.h"
|
2010-10-19 09:37:03 -07:00
|
|
|
#include "nsIPrefService.h"
|
2012-01-16 17:48:29 -08:00
|
|
|
#include "nsIPrefBranch.h"
|
2010-03-25 16:02:28 -07:00
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
// Behavior pref constants
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t BEHAVIOR_ACCEPT = 0;
|
|
|
|
static const int32_t BEHAVIOR_REJECTFOREIGN = 1;
|
|
|
|
static const int32_t BEHAVIOR_REJECT = 2;
|
2010-10-19 09:37:03 -07:00
|
|
|
|
|
|
|
// Pref string constants
|
|
|
|
static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
|
|
|
|
static const char kPrefThirdPartySession[] =
|
|
|
|
"network.cookie.thirdparty.sessionOnly";
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
static CookieServiceChild *gCookieService;
|
|
|
|
|
|
|
|
CookieServiceChild*
|
|
|
|
CookieServiceChild::GetSingleton()
|
|
|
|
{
|
|
|
|
if (!gCookieService)
|
|
|
|
gCookieService = new CookieServiceChild();
|
|
|
|
|
|
|
|
NS_ADDREF(gCookieService);
|
|
|
|
return gCookieService;
|
|
|
|
}
|
|
|
|
|
2010-11-30 18:47:08 -08:00
|
|
|
NS_IMPL_ISUPPORTS3(CookieServiceChild,
|
|
|
|
nsICookieService,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
2010-03-25 16:02:28 -07:00
|
|
|
|
|
|
|
CookieServiceChild::CookieServiceChild()
|
2010-10-19 09:37:03 -07:00
|
|
|
: mCookieBehavior(BEHAVIOR_ACCEPT)
|
|
|
|
, mThirdPartySession(false)
|
2010-03-25 16:02:28 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsNeckoChild(), "not a child process");
|
|
|
|
|
|
|
|
// This corresponds to Release() in DeallocPCookieService.
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
|
|
|
|
// Create a child PCookieService actor.
|
|
|
|
NeckoChild::InitNeckoChild();
|
|
|
|
gNeckoChild->SendPCookieServiceConstructor(this);
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
// Init our prefs and observer.
|
2012-01-16 17:48:29 -08:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch =
|
2010-10-19 09:37:03 -07:00
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(prefBranch, "no prefservice");
|
|
|
|
if (prefBranch) {
|
2011-10-17 07:59:28 -07:00
|
|
|
prefBranch->AddObserver(kPrefCookieBehavior, this, true);
|
|
|
|
prefBranch->AddObserver(kPrefThirdPartySession, this, true);
|
2010-10-19 09:37:03 -07:00
|
|
|
PrefChanged(prefBranch);
|
|
|
|
}
|
2010-03-25 16:02:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CookieServiceChild::~CookieServiceChild()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
gCookieService = nullptr;
|
2010-03-25 16:02:28 -07:00
|
|
|
}
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
void
|
|
|
|
CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t val;
|
2010-10-19 09:37:03 -07:00
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
|
|
|
|
mCookieBehavior =
|
|
|
|
val >= BEHAVIOR_ACCEPT && val <= BEHAVIOR_REJECT ? val : BEHAVIOR_ACCEPT;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool boolval;
|
2010-10-19 09:37:03 -07:00
|
|
|
if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval)))
|
|
|
|
mThirdPartySession = !!boolval;
|
|
|
|
|
|
|
|
if (!mThirdPartyUtil && RequireThirdPartyCheck()) {
|
|
|
|
mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
|
|
|
|
NS_ASSERTION(mThirdPartyUtil, "require ThirdPartyUtil service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CookieServiceChild::RequireThirdPartyCheck()
|
|
|
|
{
|
|
|
|
return mCookieBehavior == BEHAVIOR_REJECTFOREIGN || mThirdPartySession;
|
|
|
|
}
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
nsresult
|
|
|
|
CookieServiceChild::GetCookieStringInternal(nsIURI *aHostURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
char **aCookieString,
|
|
|
|
bool aFromHttp)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aHostURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCookieString);
|
|
|
|
|
|
|
|
*aCookieString = NULL;
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
// Determine whether the request is foreign. Failure is acceptable.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isForeign = true;
|
2010-10-19 09:37:03 -07:00
|
|
|
if (RequireThirdPartyCheck())
|
|
|
|
mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
|
2010-03-25 16:02:28 -07:00
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
URIParams uriParams;
|
|
|
|
SerializeURI(aHostURI, uriParams);
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
// Synchronously call the parent.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString result;
|
2012-09-18 09:04:04 -07:00
|
|
|
SendGetCookieString(uriParams, !!isForeign, aFromHttp,
|
|
|
|
IPC::SerializedLoadContext(aChannel), &result);
|
2010-03-25 16:02:28 -07:00
|
|
|
if (!result.IsEmpty())
|
|
|
|
*aCookieString = ToNewCString(result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CookieServiceChild::SetCookieStringInternal(nsIURI *aHostURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
const char *aCookieString,
|
|
|
|
const char *aServerTime,
|
|
|
|
bool aFromHttp)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aHostURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aCookieString);
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
// Determine whether the request is foreign. Failure is acceptable.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isForeign = true;
|
2010-10-19 09:37:03 -07:00
|
|
|
if (RequireThirdPartyCheck())
|
|
|
|
mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
|
2010-03-25 16:02:28 -07:00
|
|
|
|
|
|
|
nsDependentCString cookieString(aCookieString);
|
|
|
|
nsDependentCString serverTime;
|
|
|
|
if (aServerTime)
|
|
|
|
serverTime.Rebind(aServerTime);
|
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
URIParams uriParams;
|
|
|
|
SerializeURI(aHostURI, uriParams);
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
// Synchronously call the parent.
|
2012-08-23 12:33:46 -07:00
|
|
|
SendSetCookieString(uriParams, !!isForeign, cookieString, serverTime,
|
2012-09-18 09:04:04 -07:00
|
|
|
aFromHttp, IPC::SerializedLoadContext(aChannel));
|
2010-03-25 16:02:28 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-19 09:37:03 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieServiceChild::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
|
|
|
|
"not a pref change topic!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
|
|
|
|
if (prefBranch)
|
|
|
|
PrefChanged(prefBranch);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-25 16:02:28 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieServiceChild::GetCookieString(nsIURI *aHostURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
char **aCookieString)
|
|
|
|
{
|
|
|
|
return GetCookieStringInternal(aHostURI, aChannel, aCookieString, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieServiceChild::GetCookieStringFromHttp(nsIURI *aHostURI,
|
|
|
|
nsIURI *aFirstURI,
|
|
|
|
nsIChannel *aChannel,
|
|
|
|
char **aCookieString)
|
|
|
|
{
|
|
|
|
return GetCookieStringInternal(aHostURI, aChannel, aCookieString, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieServiceChild::SetCookieString(nsIURI *aHostURI,
|
|
|
|
nsIPrompt *aPrompt,
|
|
|
|
const char *aCookieString,
|
|
|
|
nsIChannel *aChannel)
|
|
|
|
{
|
|
|
|
return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, false);
|
2010-03-25 16:02:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CookieServiceChild::SetCookieStringFromHttp(nsIURI *aHostURI,
|
|
|
|
nsIURI *aFirstURI,
|
|
|
|
nsIPrompt *aPrompt,
|
|
|
|
const char *aCookieString,
|
|
|
|
const char *aServerTime,
|
|
|
|
nsIChannel *aChannel)
|
|
|
|
{
|
|
|
|
return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
|
|
|
|
aServerTime, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|