1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation <http://www.mozilla.org/>.
19 : * Portions created by the Initial Developer are Copyright (C) 2010
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Daniel Witte <dwitte@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "mozilla/net/CookieServiceChild.h"
40 : #include "mozilla/net/NeckoChild.h"
41 : #include "nsIURI.h"
42 : #include "nsIPrefService.h"
43 : #include "nsIPrefBranch.h"
44 :
45 : namespace mozilla {
46 : namespace net {
47 :
48 : // Behavior pref constants
49 : static const PRInt32 BEHAVIOR_ACCEPT = 0;
50 : static const PRInt32 BEHAVIOR_REJECTFOREIGN = 1;
51 : static const PRInt32 BEHAVIOR_REJECT = 2;
52 :
53 : // Pref string constants
54 : static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
55 : static const char kPrefThirdPartySession[] =
56 : "network.cookie.thirdparty.sessionOnly";
57 :
58 : static CookieServiceChild *gCookieService;
59 :
60 : CookieServiceChild*
61 0 : CookieServiceChild::GetSingleton()
62 : {
63 0 : if (!gCookieService)
64 0 : gCookieService = new CookieServiceChild();
65 :
66 0 : NS_ADDREF(gCookieService);
67 0 : return gCookieService;
68 : }
69 :
70 0 : NS_IMPL_ISUPPORTS3(CookieServiceChild,
71 : nsICookieService,
72 : nsIObserver,
73 : nsISupportsWeakReference)
74 :
75 0 : CookieServiceChild::CookieServiceChild()
76 : : mCookieBehavior(BEHAVIOR_ACCEPT)
77 0 : , mThirdPartySession(false)
78 : {
79 0 : NS_ASSERTION(IsNeckoChild(), "not a child process");
80 :
81 : // This corresponds to Release() in DeallocPCookieService.
82 0 : NS_ADDREF_THIS();
83 :
84 : // Create a child PCookieService actor.
85 0 : NeckoChild::InitNeckoChild();
86 0 : gNeckoChild->SendPCookieServiceConstructor(this);
87 :
88 : // Init our prefs and observer.
89 : nsCOMPtr<nsIPrefBranch> prefBranch =
90 0 : do_GetService(NS_PREFSERVICE_CONTRACTID);
91 0 : NS_WARN_IF_FALSE(prefBranch, "no prefservice");
92 0 : if (prefBranch) {
93 0 : prefBranch->AddObserver(kPrefCookieBehavior, this, true);
94 0 : prefBranch->AddObserver(kPrefThirdPartySession, this, true);
95 0 : PrefChanged(prefBranch);
96 : }
97 0 : }
98 :
99 0 : CookieServiceChild::~CookieServiceChild()
100 : {
101 0 : gCookieService = nsnull;
102 0 : }
103 :
104 : void
105 0 : CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
106 : {
107 : PRInt32 val;
108 0 : if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
109 : mCookieBehavior =
110 0 : val >= BEHAVIOR_ACCEPT && val <= BEHAVIOR_REJECT ? val : BEHAVIOR_ACCEPT;
111 :
112 : bool boolval;
113 0 : if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval)))
114 0 : mThirdPartySession = !!boolval;
115 :
116 0 : if (!mThirdPartyUtil && RequireThirdPartyCheck()) {
117 0 : mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
118 0 : NS_ASSERTION(mThirdPartyUtil, "require ThirdPartyUtil service");
119 : }
120 0 : }
121 :
122 : bool
123 0 : CookieServiceChild::RequireThirdPartyCheck()
124 : {
125 0 : return mCookieBehavior == BEHAVIOR_REJECTFOREIGN || mThirdPartySession;
126 : }
127 :
128 : nsresult
129 0 : CookieServiceChild::GetCookieStringInternal(nsIURI *aHostURI,
130 : nsIChannel *aChannel,
131 : char **aCookieString,
132 : bool aFromHttp)
133 : {
134 0 : NS_ENSURE_ARG(aHostURI);
135 0 : NS_ENSURE_ARG_POINTER(aCookieString);
136 :
137 0 : *aCookieString = NULL;
138 :
139 : // Determine whether the request is foreign. Failure is acceptable.
140 0 : bool isForeign = true;
141 0 : if (RequireThirdPartyCheck())
142 0 : mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
143 :
144 : // Synchronously call the parent.
145 0 : nsCAutoString result;
146 0 : SendGetCookieString(IPC::URI(aHostURI), !!isForeign, aFromHttp, &result);
147 0 : if (!result.IsEmpty())
148 0 : *aCookieString = ToNewCString(result);
149 :
150 0 : return NS_OK;
151 : }
152 :
153 : nsresult
154 0 : CookieServiceChild::SetCookieStringInternal(nsIURI *aHostURI,
155 : nsIChannel *aChannel,
156 : const char *aCookieString,
157 : const char *aServerTime,
158 : bool aFromHttp)
159 : {
160 0 : NS_ENSURE_ARG(aHostURI);
161 0 : NS_ENSURE_ARG_POINTER(aCookieString);
162 :
163 : // Determine whether the request is foreign. Failure is acceptable.
164 0 : bool isForeign = true;
165 0 : if (RequireThirdPartyCheck())
166 0 : mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
167 :
168 0 : nsDependentCString cookieString(aCookieString);
169 0 : nsDependentCString serverTime;
170 0 : if (aServerTime)
171 0 : serverTime.Rebind(aServerTime);
172 :
173 : // Synchronously call the parent.
174 0 : SendSetCookieString(IPC::URI(aHostURI), !!isForeign,
175 0 : cookieString, serverTime, aFromHttp);
176 0 : return NS_OK;
177 : }
178 :
179 : NS_IMETHODIMP
180 0 : CookieServiceChild::Observe(nsISupports *aSubject,
181 : const char *aTopic,
182 : const PRUnichar *aData)
183 : {
184 0 : NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
185 : "not a pref change topic!");
186 :
187 0 : nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
188 0 : if (prefBranch)
189 0 : PrefChanged(prefBranch);
190 0 : return NS_OK;
191 : }
192 :
193 : NS_IMETHODIMP
194 0 : CookieServiceChild::GetCookieString(nsIURI *aHostURI,
195 : nsIChannel *aChannel,
196 : char **aCookieString)
197 : {
198 0 : return GetCookieStringInternal(aHostURI, aChannel, aCookieString, false);
199 : }
200 :
201 : NS_IMETHODIMP
202 0 : CookieServiceChild::GetCookieStringFromHttp(nsIURI *aHostURI,
203 : nsIURI *aFirstURI,
204 : nsIChannel *aChannel,
205 : char **aCookieString)
206 : {
207 0 : return GetCookieStringInternal(aHostURI, aChannel, aCookieString, true);
208 : }
209 :
210 : NS_IMETHODIMP
211 0 : CookieServiceChild::SetCookieString(nsIURI *aHostURI,
212 : nsIPrompt *aPrompt,
213 : const char *aCookieString,
214 : nsIChannel *aChannel)
215 : {
216 : return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
217 0 : nsnull, false);
218 : }
219 :
220 : NS_IMETHODIMP
221 0 : CookieServiceChild::SetCookieStringFromHttp(nsIURI *aHostURI,
222 : nsIURI *aFirstURI,
223 : nsIPrompt *aPrompt,
224 : const char *aCookieString,
225 : const char *aServerTime,
226 : nsIChannel *aChannel)
227 : {
228 : return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
229 0 : aServerTime, true);
230 : }
231 :
232 : }
233 : }
234 :
|