|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "mozilla/net/CookieServiceParent.h" |
|
7 #include "mozilla/dom/PContentParent.h" |
|
8 #include "mozilla/net/NeckoParent.h" |
|
9 |
|
10 #include "mozilla/ipc/URIUtils.h" |
|
11 #include "nsCookieService.h" |
|
12 #include "nsNetUtil.h" |
|
13 #include "nsPrintfCString.h" |
|
14 #include "SerializedLoadContext.h" |
|
15 |
|
16 using namespace mozilla::ipc; |
|
17 using mozilla::dom::PContentParent; |
|
18 using mozilla::net::NeckoParent; |
|
19 |
|
20 namespace mozilla { |
|
21 namespace net { |
|
22 |
|
23 MOZ_WARN_UNUSED_RESULT |
|
24 bool |
|
25 CookieServiceParent::GetAppInfoFromParams(const IPC::SerializedLoadContext &aLoadContext, |
|
26 uint32_t& aAppId, |
|
27 bool& aIsInBrowserElement, |
|
28 bool& aIsPrivate) |
|
29 { |
|
30 aAppId = NECKO_NO_APP_ID; |
|
31 aIsInBrowserElement = false; |
|
32 aIsPrivate = false; |
|
33 |
|
34 const char* error = NeckoParent::GetValidatedAppInfo(aLoadContext, |
|
35 Manager()->Manager(), |
|
36 &aAppId, |
|
37 &aIsInBrowserElement); |
|
38 if (error) { |
|
39 NS_WARNING(nsPrintfCString("CookieServiceParent: GetAppInfoFromParams: " |
|
40 "FATAL error: %s: KILLING CHILD PROCESS\n", |
|
41 error).get()); |
|
42 return false; |
|
43 } |
|
44 |
|
45 if (aLoadContext.IsPrivateBitValid()) { |
|
46 aIsPrivate = aLoadContext.mUsePrivateBrowsing; |
|
47 } |
|
48 return true; |
|
49 } |
|
50 |
|
51 CookieServiceParent::CookieServiceParent() |
|
52 { |
|
53 // Instantiate the cookieservice via the service manager, so it sticks around |
|
54 // until shutdown. |
|
55 nsCOMPtr<nsICookieService> cs = do_GetService(NS_COOKIESERVICE_CONTRACTID); |
|
56 |
|
57 // Get the nsCookieService instance directly, so we can call internal methods. |
|
58 mCookieService = |
|
59 already_AddRefed<nsCookieService>(nsCookieService::GetSingleton()); |
|
60 NS_ASSERTION(mCookieService, "couldn't get nsICookieService"); |
|
61 } |
|
62 |
|
63 CookieServiceParent::~CookieServiceParent() |
|
64 { |
|
65 } |
|
66 |
|
67 bool |
|
68 CookieServiceParent::RecvGetCookieString(const URIParams& aHost, |
|
69 const bool& aIsForeign, |
|
70 const bool& aFromHttp, |
|
71 const IPC::SerializedLoadContext& |
|
72 aLoadContext, |
|
73 nsCString* aResult) |
|
74 { |
|
75 if (!mCookieService) |
|
76 return true; |
|
77 |
|
78 // Deserialize URI. Having a host URI is mandatory and should always be |
|
79 // provided by the child; thus we consider failure fatal. |
|
80 nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost); |
|
81 if (!hostURI) |
|
82 return false; |
|
83 |
|
84 uint32_t appId; |
|
85 bool isInBrowserElement, isPrivate; |
|
86 bool valid = GetAppInfoFromParams(aLoadContext, appId, |
|
87 isInBrowserElement, isPrivate); |
|
88 if (!valid) { |
|
89 return false; |
|
90 } |
|
91 |
|
92 mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aFromHttp, appId, |
|
93 isInBrowserElement, isPrivate, *aResult); |
|
94 return true; |
|
95 } |
|
96 |
|
97 bool |
|
98 CookieServiceParent::RecvSetCookieString(const URIParams& aHost, |
|
99 const bool& aIsForeign, |
|
100 const nsCString& aCookieString, |
|
101 const nsCString& aServerTime, |
|
102 const bool& aFromHttp, |
|
103 const IPC::SerializedLoadContext& |
|
104 aLoadContext) |
|
105 { |
|
106 if (!mCookieService) |
|
107 return true; |
|
108 |
|
109 // Deserialize URI. Having a host URI is mandatory and should always be |
|
110 // provided by the child; thus we consider failure fatal. |
|
111 nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost); |
|
112 if (!hostURI) |
|
113 return false; |
|
114 |
|
115 uint32_t appId; |
|
116 bool isInBrowserElement, isPrivate; |
|
117 bool valid = GetAppInfoFromParams(aLoadContext, appId, |
|
118 isInBrowserElement, isPrivate); |
|
119 if (!valid) { |
|
120 return false; |
|
121 } |
|
122 |
|
123 nsDependentCString cookieString(aCookieString, 0); |
|
124 //TODO: bug 812475, pass a real channel object |
|
125 mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString, |
|
126 aServerTime, aFromHttp, appId, |
|
127 isInBrowserElement, isPrivate, nullptr); |
|
128 return true; |
|
129 } |
|
130 |
|
131 mozilla::ipc::IProtocol* |
|
132 CookieServiceParent::CloneProtocol(Channel* aChannel, |
|
133 mozilla::ipc::ProtocolCloneContext* aCtx) |
|
134 { |
|
135 NeckoParent* manager = aCtx->GetNeckoParent(); |
|
136 nsAutoPtr<PCookieServiceParent> actor(manager->AllocPCookieServiceParent()); |
|
137 if (!actor || !manager->RecvPCookieServiceConstructor(actor)) { |
|
138 return nullptr; |
|
139 } |
|
140 return actor.forget(); |
|
141 } |
|
142 |
|
143 } |
|
144 } |
|
145 |