1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cookie/CookieServiceChild.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,214 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/net/CookieServiceChild.h" 1.10 +#include "mozilla/ipc/URIUtils.h" 1.11 +#include "mozilla/net/NeckoChild.h" 1.12 +#include "nsIURI.h" 1.13 +#include "nsIPrefService.h" 1.14 +#include "nsIPrefBranch.h" 1.15 +#include "nsNetUtil.h" 1.16 +#include "SerializedLoadContext.h" 1.17 + 1.18 +using namespace mozilla::ipc; 1.19 + 1.20 +namespace mozilla { 1.21 +namespace net { 1.22 + 1.23 +// Behavior pref constants 1.24 +static const int32_t BEHAVIOR_ACCEPT = 0; 1.25 +static const int32_t BEHAVIOR_REJECTFOREIGN = 1; 1.26 +// static const int32_t BEHAVIOR_REJECT = 2; 1.27 +static const int32_t BEHAVIOR_LIMITFOREIGN = 3; 1.28 + 1.29 +// Pref string constants 1.30 +static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior"; 1.31 +static const char kPrefThirdPartySession[] = 1.32 + "network.cookie.thirdparty.sessionOnly"; 1.33 + 1.34 +static CookieServiceChild *gCookieService; 1.35 + 1.36 +CookieServiceChild* 1.37 +CookieServiceChild::GetSingleton() 1.38 +{ 1.39 + if (!gCookieService) 1.40 + gCookieService = new CookieServiceChild(); 1.41 + 1.42 + NS_ADDREF(gCookieService); 1.43 + return gCookieService; 1.44 +} 1.45 + 1.46 +NS_IMPL_ISUPPORTS(CookieServiceChild, 1.47 + nsICookieService, 1.48 + nsIObserver, 1.49 + nsISupportsWeakReference) 1.50 + 1.51 +CookieServiceChild::CookieServiceChild() 1.52 + : mCookieBehavior(BEHAVIOR_ACCEPT) 1.53 + , mThirdPartySession(false) 1.54 +{ 1.55 + NS_ASSERTION(IsNeckoChild(), "not a child process"); 1.56 + 1.57 + // This corresponds to Release() in DeallocPCookieService. 1.58 + NS_ADDREF_THIS(); 1.59 + 1.60 + // Create a child PCookieService actor. 1.61 + NeckoChild::InitNeckoChild(); 1.62 + gNeckoChild->SendPCookieServiceConstructor(this); 1.63 + 1.64 + // Init our prefs and observer. 1.65 + nsCOMPtr<nsIPrefBranch> prefBranch = 1.66 + do_GetService(NS_PREFSERVICE_CONTRACTID); 1.67 + NS_WARN_IF_FALSE(prefBranch, "no prefservice"); 1.68 + if (prefBranch) { 1.69 + prefBranch->AddObserver(kPrefCookieBehavior, this, true); 1.70 + prefBranch->AddObserver(kPrefThirdPartySession, this, true); 1.71 + PrefChanged(prefBranch); 1.72 + } 1.73 +} 1.74 + 1.75 +CookieServiceChild::~CookieServiceChild() 1.76 +{ 1.77 + gCookieService = nullptr; 1.78 +} 1.79 + 1.80 +void 1.81 +CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch) 1.82 +{ 1.83 + int32_t val; 1.84 + if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val))) 1.85 + mCookieBehavior = 1.86 + val >= BEHAVIOR_ACCEPT && val <= BEHAVIOR_LIMITFOREIGN ? val : BEHAVIOR_ACCEPT; 1.87 + 1.88 + bool boolval; 1.89 + if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval))) 1.90 + mThirdPartySession = !!boolval; 1.91 + 1.92 + if (!mThirdPartyUtil && RequireThirdPartyCheck()) { 1.93 + mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID); 1.94 + NS_ASSERTION(mThirdPartyUtil, "require ThirdPartyUtil service"); 1.95 + } 1.96 +} 1.97 + 1.98 +bool 1.99 +CookieServiceChild::RequireThirdPartyCheck() 1.100 +{ 1.101 + return mCookieBehavior == BEHAVIOR_REJECTFOREIGN || mCookieBehavior == BEHAVIOR_LIMITFOREIGN || mThirdPartySession; 1.102 +} 1.103 + 1.104 +nsresult 1.105 +CookieServiceChild::GetCookieStringInternal(nsIURI *aHostURI, 1.106 + nsIChannel *aChannel, 1.107 + char **aCookieString, 1.108 + bool aFromHttp) 1.109 +{ 1.110 + NS_ENSURE_ARG(aHostURI); 1.111 + NS_ENSURE_ARG_POINTER(aCookieString); 1.112 + 1.113 + *aCookieString = nullptr; 1.114 + 1.115 + // Determine whether the request is foreign. Failure is acceptable. 1.116 + bool isForeign = true; 1.117 + if (RequireThirdPartyCheck()) 1.118 + mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign); 1.119 + 1.120 + URIParams uriParams; 1.121 + SerializeURI(aHostURI, uriParams); 1.122 + 1.123 + // Synchronously call the parent. 1.124 + nsAutoCString result; 1.125 + SendGetCookieString(uriParams, !!isForeign, aFromHttp, 1.126 + IPC::SerializedLoadContext(aChannel), &result); 1.127 + if (!result.IsEmpty()) 1.128 + *aCookieString = ToNewCString(result); 1.129 + 1.130 + return NS_OK; 1.131 +} 1.132 + 1.133 +nsresult 1.134 +CookieServiceChild::SetCookieStringInternal(nsIURI *aHostURI, 1.135 + nsIChannel *aChannel, 1.136 + const char *aCookieString, 1.137 + const char *aServerTime, 1.138 + bool aFromHttp) 1.139 +{ 1.140 + NS_ENSURE_ARG(aHostURI); 1.141 + NS_ENSURE_ARG_POINTER(aCookieString); 1.142 + 1.143 + // Determine whether the request is foreign. Failure is acceptable. 1.144 + bool isForeign = true; 1.145 + if (RequireThirdPartyCheck()) 1.146 + mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign); 1.147 + 1.148 + nsDependentCString cookieString(aCookieString); 1.149 + nsDependentCString serverTime; 1.150 + if (aServerTime) 1.151 + serverTime.Rebind(aServerTime); 1.152 + 1.153 + URIParams uriParams; 1.154 + SerializeURI(aHostURI, uriParams); 1.155 + 1.156 + // Synchronously call the parent. 1.157 + SendSetCookieString(uriParams, !!isForeign, cookieString, serverTime, 1.158 + aFromHttp, IPC::SerializedLoadContext(aChannel)); 1.159 + return NS_OK; 1.160 +} 1.161 + 1.162 +NS_IMETHODIMP 1.163 +CookieServiceChild::Observe(nsISupports *aSubject, 1.164 + const char *aTopic, 1.165 + const char16_t *aData) 1.166 +{ 1.167 + NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0, 1.168 + "not a pref change topic!"); 1.169 + 1.170 + nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject); 1.171 + if (prefBranch) 1.172 + PrefChanged(prefBranch); 1.173 + return NS_OK; 1.174 +} 1.175 + 1.176 +NS_IMETHODIMP 1.177 +CookieServiceChild::GetCookieString(nsIURI *aHostURI, 1.178 + nsIChannel *aChannel, 1.179 + char **aCookieString) 1.180 +{ 1.181 + return GetCookieStringInternal(aHostURI, aChannel, aCookieString, false); 1.182 +} 1.183 + 1.184 +NS_IMETHODIMP 1.185 +CookieServiceChild::GetCookieStringFromHttp(nsIURI *aHostURI, 1.186 + nsIURI *aFirstURI, 1.187 + nsIChannel *aChannel, 1.188 + char **aCookieString) 1.189 +{ 1.190 + return GetCookieStringInternal(aHostURI, aChannel, aCookieString, true); 1.191 +} 1.192 + 1.193 +NS_IMETHODIMP 1.194 +CookieServiceChild::SetCookieString(nsIURI *aHostURI, 1.195 + nsIPrompt *aPrompt, 1.196 + const char *aCookieString, 1.197 + nsIChannel *aChannel) 1.198 +{ 1.199 + return SetCookieStringInternal(aHostURI, aChannel, aCookieString, 1.200 + nullptr, false); 1.201 +} 1.202 + 1.203 +NS_IMETHODIMP 1.204 +CookieServiceChild::SetCookieStringFromHttp(nsIURI *aHostURI, 1.205 + nsIURI *aFirstURI, 1.206 + nsIPrompt *aPrompt, 1.207 + const char *aCookieString, 1.208 + const char *aServerTime, 1.209 + nsIChannel *aChannel) 1.210 +{ 1.211 + return SetCookieStringInternal(aHostURI, aChannel, aCookieString, 1.212 + aServerTime, true); 1.213 +} 1.214 + 1.215 +} 1.216 +} 1.217 +