netwerk/cookie/CookieServiceChild.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/net/CookieServiceChild.h"
michael@0 7 #include "mozilla/ipc/URIUtils.h"
michael@0 8 #include "mozilla/net/NeckoChild.h"
michael@0 9 #include "nsIURI.h"
michael@0 10 #include "nsIPrefService.h"
michael@0 11 #include "nsIPrefBranch.h"
michael@0 12 #include "nsNetUtil.h"
michael@0 13 #include "SerializedLoadContext.h"
michael@0 14
michael@0 15 using namespace mozilla::ipc;
michael@0 16
michael@0 17 namespace mozilla {
michael@0 18 namespace net {
michael@0 19
michael@0 20 // Behavior pref constants
michael@0 21 static const int32_t BEHAVIOR_ACCEPT = 0;
michael@0 22 static const int32_t BEHAVIOR_REJECTFOREIGN = 1;
michael@0 23 // static const int32_t BEHAVIOR_REJECT = 2;
michael@0 24 static const int32_t BEHAVIOR_LIMITFOREIGN = 3;
michael@0 25
michael@0 26 // Pref string constants
michael@0 27 static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
michael@0 28 static const char kPrefThirdPartySession[] =
michael@0 29 "network.cookie.thirdparty.sessionOnly";
michael@0 30
michael@0 31 static CookieServiceChild *gCookieService;
michael@0 32
michael@0 33 CookieServiceChild*
michael@0 34 CookieServiceChild::GetSingleton()
michael@0 35 {
michael@0 36 if (!gCookieService)
michael@0 37 gCookieService = new CookieServiceChild();
michael@0 38
michael@0 39 NS_ADDREF(gCookieService);
michael@0 40 return gCookieService;
michael@0 41 }
michael@0 42
michael@0 43 NS_IMPL_ISUPPORTS(CookieServiceChild,
michael@0 44 nsICookieService,
michael@0 45 nsIObserver,
michael@0 46 nsISupportsWeakReference)
michael@0 47
michael@0 48 CookieServiceChild::CookieServiceChild()
michael@0 49 : mCookieBehavior(BEHAVIOR_ACCEPT)
michael@0 50 , mThirdPartySession(false)
michael@0 51 {
michael@0 52 NS_ASSERTION(IsNeckoChild(), "not a child process");
michael@0 53
michael@0 54 // This corresponds to Release() in DeallocPCookieService.
michael@0 55 NS_ADDREF_THIS();
michael@0 56
michael@0 57 // Create a child PCookieService actor.
michael@0 58 NeckoChild::InitNeckoChild();
michael@0 59 gNeckoChild->SendPCookieServiceConstructor(this);
michael@0 60
michael@0 61 // Init our prefs and observer.
michael@0 62 nsCOMPtr<nsIPrefBranch> prefBranch =
michael@0 63 do_GetService(NS_PREFSERVICE_CONTRACTID);
michael@0 64 NS_WARN_IF_FALSE(prefBranch, "no prefservice");
michael@0 65 if (prefBranch) {
michael@0 66 prefBranch->AddObserver(kPrefCookieBehavior, this, true);
michael@0 67 prefBranch->AddObserver(kPrefThirdPartySession, this, true);
michael@0 68 PrefChanged(prefBranch);
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 CookieServiceChild::~CookieServiceChild()
michael@0 73 {
michael@0 74 gCookieService = nullptr;
michael@0 75 }
michael@0 76
michael@0 77 void
michael@0 78 CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
michael@0 79 {
michael@0 80 int32_t val;
michael@0 81 if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
michael@0 82 mCookieBehavior =
michael@0 83 val >= BEHAVIOR_ACCEPT && val <= BEHAVIOR_LIMITFOREIGN ? val : BEHAVIOR_ACCEPT;
michael@0 84
michael@0 85 bool boolval;
michael@0 86 if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval)))
michael@0 87 mThirdPartySession = !!boolval;
michael@0 88
michael@0 89 if (!mThirdPartyUtil && RequireThirdPartyCheck()) {
michael@0 90 mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
michael@0 91 NS_ASSERTION(mThirdPartyUtil, "require ThirdPartyUtil service");
michael@0 92 }
michael@0 93 }
michael@0 94
michael@0 95 bool
michael@0 96 CookieServiceChild::RequireThirdPartyCheck()
michael@0 97 {
michael@0 98 return mCookieBehavior == BEHAVIOR_REJECTFOREIGN || mCookieBehavior == BEHAVIOR_LIMITFOREIGN || mThirdPartySession;
michael@0 99 }
michael@0 100
michael@0 101 nsresult
michael@0 102 CookieServiceChild::GetCookieStringInternal(nsIURI *aHostURI,
michael@0 103 nsIChannel *aChannel,
michael@0 104 char **aCookieString,
michael@0 105 bool aFromHttp)
michael@0 106 {
michael@0 107 NS_ENSURE_ARG(aHostURI);
michael@0 108 NS_ENSURE_ARG_POINTER(aCookieString);
michael@0 109
michael@0 110 *aCookieString = nullptr;
michael@0 111
michael@0 112 // Determine whether the request is foreign. Failure is acceptable.
michael@0 113 bool isForeign = true;
michael@0 114 if (RequireThirdPartyCheck())
michael@0 115 mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
michael@0 116
michael@0 117 URIParams uriParams;
michael@0 118 SerializeURI(aHostURI, uriParams);
michael@0 119
michael@0 120 // Synchronously call the parent.
michael@0 121 nsAutoCString result;
michael@0 122 SendGetCookieString(uriParams, !!isForeign, aFromHttp,
michael@0 123 IPC::SerializedLoadContext(aChannel), &result);
michael@0 124 if (!result.IsEmpty())
michael@0 125 *aCookieString = ToNewCString(result);
michael@0 126
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130 nsresult
michael@0 131 CookieServiceChild::SetCookieStringInternal(nsIURI *aHostURI,
michael@0 132 nsIChannel *aChannel,
michael@0 133 const char *aCookieString,
michael@0 134 const char *aServerTime,
michael@0 135 bool aFromHttp)
michael@0 136 {
michael@0 137 NS_ENSURE_ARG(aHostURI);
michael@0 138 NS_ENSURE_ARG_POINTER(aCookieString);
michael@0 139
michael@0 140 // Determine whether the request is foreign. Failure is acceptable.
michael@0 141 bool isForeign = true;
michael@0 142 if (RequireThirdPartyCheck())
michael@0 143 mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
michael@0 144
michael@0 145 nsDependentCString cookieString(aCookieString);
michael@0 146 nsDependentCString serverTime;
michael@0 147 if (aServerTime)
michael@0 148 serverTime.Rebind(aServerTime);
michael@0 149
michael@0 150 URIParams uriParams;
michael@0 151 SerializeURI(aHostURI, uriParams);
michael@0 152
michael@0 153 // Synchronously call the parent.
michael@0 154 SendSetCookieString(uriParams, !!isForeign, cookieString, serverTime,
michael@0 155 aFromHttp, IPC::SerializedLoadContext(aChannel));
michael@0 156 return NS_OK;
michael@0 157 }
michael@0 158
michael@0 159 NS_IMETHODIMP
michael@0 160 CookieServiceChild::Observe(nsISupports *aSubject,
michael@0 161 const char *aTopic,
michael@0 162 const char16_t *aData)
michael@0 163 {
michael@0 164 NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
michael@0 165 "not a pref change topic!");
michael@0 166
michael@0 167 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
michael@0 168 if (prefBranch)
michael@0 169 PrefChanged(prefBranch);
michael@0 170 return NS_OK;
michael@0 171 }
michael@0 172
michael@0 173 NS_IMETHODIMP
michael@0 174 CookieServiceChild::GetCookieString(nsIURI *aHostURI,
michael@0 175 nsIChannel *aChannel,
michael@0 176 char **aCookieString)
michael@0 177 {
michael@0 178 return GetCookieStringInternal(aHostURI, aChannel, aCookieString, false);
michael@0 179 }
michael@0 180
michael@0 181 NS_IMETHODIMP
michael@0 182 CookieServiceChild::GetCookieStringFromHttp(nsIURI *aHostURI,
michael@0 183 nsIURI *aFirstURI,
michael@0 184 nsIChannel *aChannel,
michael@0 185 char **aCookieString)
michael@0 186 {
michael@0 187 return GetCookieStringInternal(aHostURI, aChannel, aCookieString, true);
michael@0 188 }
michael@0 189
michael@0 190 NS_IMETHODIMP
michael@0 191 CookieServiceChild::SetCookieString(nsIURI *aHostURI,
michael@0 192 nsIPrompt *aPrompt,
michael@0 193 const char *aCookieString,
michael@0 194 nsIChannel *aChannel)
michael@0 195 {
michael@0 196 return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
michael@0 197 nullptr, false);
michael@0 198 }
michael@0 199
michael@0 200 NS_IMETHODIMP
michael@0 201 CookieServiceChild::SetCookieStringFromHttp(nsIURI *aHostURI,
michael@0 202 nsIURI *aFirstURI,
michael@0 203 nsIPrompt *aPrompt,
michael@0 204 const char *aCookieString,
michael@0 205 const char *aServerTime,
michael@0 206 nsIChannel *aChannel)
michael@0 207 {
michael@0 208 return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
michael@0 209 aServerTime, true);
michael@0 210 }
michael@0 211
michael@0 212 }
michael@0 213 }
michael@0 214

mercurial