Line data Source code
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/BasePrincipal.h"
11 : #include "mozilla/ipc/URIUtils.h"
12 : #include "nsCookieService.h"
13 : #include "nsIChannel.h"
14 : #include "nsIScriptSecurityManager.h"
15 : #include "nsIPrivateBrowsingChannel.h"
16 : #include "nsNetCID.h"
17 : #include "nsPrintfCString.h"
18 :
19 : using namespace mozilla::ipc;
20 : using mozilla::BasePrincipal;
21 : using mozilla::OriginAttributes;
22 : using mozilla::dom::PContentParent;
23 : using mozilla::net::NeckoParent;
24 :
25 : namespace {
26 :
27 : // Ignore failures from this function, as they only affect whether we do or
28 : // don't show a dialog box in private browsing mode if the user sets a pref.
29 : void
30 0 : CreateDummyChannel(nsIURI* aHostURI, OriginAttributes& aAttrs, nsIChannel** aChannel)
31 : {
32 : nsCOMPtr<nsIPrincipal> principal =
33 0 : BasePrincipal::CreateCodebasePrincipal(aHostURI, aAttrs);
34 0 : if (!principal) {
35 0 : return;
36 : }
37 :
38 0 : nsCOMPtr<nsIURI> dummyURI;
39 0 : nsresult rv = NS_NewURI(getter_AddRefs(dummyURI), "about:blank");
40 0 : if (NS_FAILED(rv)) {
41 0 : return;
42 : }
43 :
44 : // The following channel is never openend, so it does not matter what
45 : // securityFlags we pass; let's follow the principle of least privilege.
46 0 : nsCOMPtr<nsIChannel> dummyChannel;
47 0 : NS_NewChannel(getter_AddRefs(dummyChannel), dummyURI, principal,
48 : nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
49 0 : nsIContentPolicy::TYPE_INVALID);
50 0 : nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(dummyChannel);
51 0 : if (!pbChannel) {
52 0 : return;
53 : }
54 :
55 0 : pbChannel->SetPrivate(aAttrs.mPrivateBrowsingId > 0);
56 0 : dummyChannel.forget(aChannel);
57 0 : return;
58 : }
59 :
60 : }
61 :
62 : namespace mozilla {
63 : namespace net {
64 :
65 0 : CookieServiceParent::CookieServiceParent()
66 : {
67 : // Instantiate the cookieservice via the service manager, so it sticks around
68 : // until shutdown.
69 0 : nsCOMPtr<nsICookieService> cs = do_GetService(NS_COOKIESERVICE_CONTRACTID);
70 :
71 : // Get the nsCookieService instance directly, so we can call internal methods.
72 : mCookieService =
73 0 : already_AddRefed<nsCookieService>(nsCookieService::GetSingleton());
74 0 : NS_ASSERTION(mCookieService, "couldn't get nsICookieService");
75 0 : }
76 :
77 0 : CookieServiceParent::~CookieServiceParent()
78 : {
79 0 : }
80 :
81 : void
82 0 : CookieServiceParent::ActorDestroy(ActorDestroyReason aWhy)
83 : {
84 : // Nothing needed here. Called right before destructor since this is a
85 : // non-refcounted class.
86 0 : }
87 :
88 : mozilla::ipc::IPCResult
89 0 : CookieServiceParent::RecvGetCookieString(const URIParams& aHost,
90 : const bool& aIsForeign,
91 : const OriginAttributes& aAttrs,
92 : nsCString* aResult)
93 : {
94 0 : if (!mCookieService)
95 0 : return IPC_OK();
96 :
97 : // Deserialize URI. Having a host URI is mandatory and should always be
98 : // provided by the child; thus we consider failure fatal.
99 0 : nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
100 0 : if (!hostURI)
101 0 : return IPC_FAIL_NO_REASON(this);
102 :
103 0 : mCookieService->GetCookieStringInternal(hostURI, aIsForeign, false, aAttrs, *aResult);
104 0 : return IPC_OK();
105 : }
106 :
107 : mozilla::ipc::IPCResult
108 0 : CookieServiceParent::RecvSetCookieString(const URIParams& aHost,
109 : const bool& aIsForeign,
110 : const nsCString& aCookieString,
111 : const nsCString& aServerTime,
112 : const OriginAttributes& aAttrs,
113 : const bool& aFromHttp)
114 : {
115 0 : if (!mCookieService)
116 0 : return IPC_OK();
117 :
118 : // Deserialize URI. Having a host URI is mandatory and should always be
119 : // provided by the child; thus we consider failure fatal.
120 0 : nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
121 0 : if (!hostURI)
122 0 : return IPC_FAIL_NO_REASON(this);
123 :
124 : // This is a gross hack. We've already computed everything we need to know
125 : // for whether to set this cookie or not, but we need to communicate all of
126 : // this information through to nsICookiePermission, which indirectly
127 : // computes the information from the channel. We only care about the
128 : // aIsPrivate argument as nsCookieService::SetCookieStringInternal deals
129 : // with aIsForeign before we have to worry about nsCookiePermission trying
130 : // to use the channel to inspect it.
131 0 : nsCOMPtr<nsIChannel> dummyChannel;
132 0 : CreateDummyChannel(hostURI, const_cast<OriginAttributes&>(aAttrs),
133 0 : getter_AddRefs(dummyChannel));
134 :
135 : // NB: dummyChannel could be null if something failed in CreateDummyChannel.
136 0 : nsDependentCString cookieString(aCookieString, 0);
137 0 : mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString,
138 0 : aServerTime, aFromHttp, aAttrs,
139 0 : dummyChannel);
140 0 : return IPC_OK();
141 : }
142 :
143 : } // namespace net
144 : } // namespace mozilla
145 :
|