Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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/. */
6 #include "mozilla/net/CookieServiceParent.h"
7 #include "mozilla/dom/PContentParent.h"
8 #include "mozilla/net/NeckoParent.h"
10 #include "mozilla/ipc/URIUtils.h"
11 #include "nsCookieService.h"
12 #include "nsNetUtil.h"
13 #include "nsPrintfCString.h"
14 #include "SerializedLoadContext.h"
16 using namespace mozilla::ipc;
17 using mozilla::dom::PContentParent;
18 using mozilla::net::NeckoParent;
20 namespace mozilla {
21 namespace net {
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;
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 }
45 if (aLoadContext.IsPrivateBitValid()) {
46 aIsPrivate = aLoadContext.mUsePrivateBrowsing;
47 }
48 return true;
49 }
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);
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 }
63 CookieServiceParent::~CookieServiceParent()
64 {
65 }
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;
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;
84 uint32_t appId;
85 bool isInBrowserElement, isPrivate;
86 bool valid = GetAppInfoFromParams(aLoadContext, appId,
87 isInBrowserElement, isPrivate);
88 if (!valid) {
89 return false;
90 }
92 // Method is called nowhere
93 nsAutoCString origDomain;
94 mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aFromHttp, appId,
95 isInBrowserElement, isPrivate,
96 origDomain, *aResult);
97 return true;
98 }
100 bool
101 CookieServiceParent::RecvSetCookieString(const URIParams& aHost,
102 const bool& aIsForeign,
103 const nsCString& aCookieString,
104 const nsCString& aServerTime,
105 const bool& aFromHttp,
106 const IPC::SerializedLoadContext&
107 aLoadContext)
108 {
109 if (!mCookieService)
110 return true;
112 // Deserialize URI. Having a host URI is mandatory and should always be
113 // provided by the child; thus we consider failure fatal.
114 nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
115 if (!hostURI)
116 return false;
118 uint32_t appId;
119 bool isInBrowserElement, isPrivate;
120 bool valid = GetAppInfoFromParams(aLoadContext, appId,
121 isInBrowserElement, isPrivate);
122 if (!valid) {
123 return false;
124 }
126 nsDependentCString cookieString(aCookieString, 0);
127 //TODO: bug 812475, pass a real channel object
128 mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString,
129 aServerTime, aFromHttp, appId,
130 isInBrowserElement, isPrivate, nullptr);
131 return true;
132 }
134 mozilla::ipc::IProtocol*
135 CookieServiceParent::CloneProtocol(Channel* aChannel,
136 mozilla::ipc::ProtocolCloneContext* aCtx)
137 {
138 NeckoParent* manager = aCtx->GetNeckoParent();
139 nsAutoPtr<PCookieServiceParent> actor(manager->AllocPCookieServiceParent());
140 if (!actor || !manager->RecvPCookieServiceConstructor(actor)) {
141 return nullptr;
142 }
143 return actor.forget();
144 }
146 }
147 }