1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/net/WyciwygChannelParent.h"
8 #include "nsWyciwygChannel.h"
10 #include "nsCharsetSource.h"
11 #include "nsISerializable.h"
12 #include "nsSerializationHelper.h"
13 #include "mozilla/ipc/URIUtils.h"
14 #include "mozilla/net/NeckoParent.h"
15 #include "SerializedLoadContext.h"
17 using namespace mozilla::ipc
;
22 WyciwygChannelParent::WyciwygChannelParent()
24 , mReceivedAppData(false)
26 #if defined(PR_LOGGING)
28 gWyciwygLog
= PR_NewLogModule("nsWyciwygChannel");
32 WyciwygChannelParent::~WyciwygChannelParent()
37 WyciwygChannelParent::ActorDestroy(ActorDestroyReason why
)
39 // We may still have refcount>0 if the channel hasn't called OnStopRequest
40 // yet, but we must not send any more msgs to child.
43 // We need to force the cycle to break here
44 mChannel
->SetNotificationCallbacks(nullptr);
47 //-----------------------------------------------------------------------------
48 // WyciwygChannelParent::nsISupports
49 //-----------------------------------------------------------------------------
51 NS_IMPL_ISUPPORTS(WyciwygChannelParent
,
53 nsIInterfaceRequestor
,
56 //-----------------------------------------------------------------------------
57 // WyciwygChannelParent::PWyciwygChannelParent
58 //-----------------------------------------------------------------------------
61 WyciwygChannelParent::RecvInit(const URIParams
& aURI
)
65 nsCOMPtr
<nsIURI
> uri
= DeserializeURI(aURI
);
70 uri
->GetSpec(uriSpec
);
71 LOG(("WyciwygChannelParent RecvInit [this=%p uri=%s]\n",
72 this, uriSpec
.get()));
74 nsCOMPtr
<nsIIOService
> ios(do_GetIOService(&rv
));
76 return SendCancelEarly(rv
);
78 nsCOMPtr
<nsIChannel
> chan
;
79 rv
= NS_NewChannel(getter_AddRefs(chan
), uri
, ios
);
81 return SendCancelEarly(rv
);
83 mChannel
= do_QueryInterface(chan
, &rv
);
85 return SendCancelEarly(rv
);
91 WyciwygChannelParent::RecvAppData(const IPC::SerializedLoadContext
& loadContext
,
92 PBrowserParent
* parent
)
94 LOG(("WyciwygChannelParent RecvAppData [this=%p]\n", this));
96 if (!SetupAppData(loadContext
, parent
))
99 mChannel
->SetNotificationCallbacks(this);
104 WyciwygChannelParent::SetupAppData(const IPC::SerializedLoadContext
& loadContext
,
105 PBrowserParent
* aParent
)
110 const char* error
= NeckoParent::CreateChannelLoadContext(aParent
,
111 Manager()->Manager(),
115 printf_stderr("WyciwygChannelParent::SetupAppData: FATAL ERROR: %s\n",
120 if (!mLoadContext
&& loadContext
.IsPrivateBitValid()) {
121 nsCOMPtr
<nsIPrivateBrowsingChannel
> pbChannel
= do_QueryInterface(mChannel
);
123 pbChannel
->SetPrivate(loadContext
.mUsePrivateBrowsing
);
126 mReceivedAppData
= true;
131 WyciwygChannelParent::RecvAsyncOpen(const URIParams
& aOriginal
,
132 const uint32_t& aLoadFlags
,
133 const IPC::SerializedLoadContext
& loadContext
,
134 PBrowserParent
* aParent
)
136 nsCOMPtr
<nsIURI
> original
= DeserializeURI(aOriginal
);
140 LOG(("WyciwygChannelParent RecvAsyncOpen [this=%p]\n", this));
147 rv
= mChannel
->SetOriginalURI(original
);
149 return SendCancelEarly(rv
);
151 rv
= mChannel
->SetLoadFlags(aLoadFlags
);
153 return SendCancelEarly(rv
);
155 if (!mReceivedAppData
&& !SetupAppData(loadContext
, aParent
)) {
159 rv
= mChannel
->SetNotificationCallbacks(this);
161 return SendCancelEarly(rv
);
163 rv
= mChannel
->AsyncOpen(this, nullptr);
165 return SendCancelEarly(rv
);
171 WyciwygChannelParent::RecvWriteToCacheEntry(const nsString
& data
)
173 if (!mReceivedAppData
) {
174 printf_stderr("WyciwygChannelParent::RecvWriteToCacheEntry: FATAL ERROR: didn't receive app data\n");
179 mChannel
->WriteToCacheEntry(data
);
185 WyciwygChannelParent::RecvCloseCacheEntry(const nsresult
& reason
)
188 mChannel
->CloseCacheEntry(reason
);
195 WyciwygChannelParent::RecvSetCharsetAndSource(const int32_t& aCharsetSource
,
196 const nsCString
& aCharset
)
199 mChannel
->SetCharsetAndSource(aCharsetSource
, aCharset
);
205 WyciwygChannelParent::RecvSetSecurityInfo(const nsCString
& aSecurityInfo
)
208 nsCOMPtr
<nsISupports
> securityInfo
;
209 NS_DeserializeObject(aSecurityInfo
, getter_AddRefs(securityInfo
));
210 mChannel
->SetSecurityInfo(securityInfo
);
217 WyciwygChannelParent::RecvCancel(const nsresult
& aStatusCode
)
220 mChannel
->Cancel(aStatusCode
);
224 //-----------------------------------------------------------------------------
225 // WyciwygChannelParent::nsIRequestObserver
226 //-----------------------------------------------------------------------------
229 WyciwygChannelParent::OnStartRequest(nsIRequest
*aRequest
, nsISupports
*aContext
)
231 LOG(("WyciwygChannelParent::OnStartRequest [this=%p]\n", this));
235 nsCOMPtr
<nsIWyciwygChannel
> chan
= do_QueryInterface(aRequest
, &rv
);
236 NS_ENSURE_SUCCESS(rv
, rv
);
239 chan
->GetStatus(&status
);
241 int64_t contentLength
= -1;
242 chan
->GetContentLength(&contentLength
);
244 int32_t charsetSource
= kCharsetUninitialized
;
245 nsAutoCString charset
;
246 chan
->GetCharsetAndSource(&charsetSource
, charset
);
248 nsCOMPtr
<nsISupports
> securityInfo
;
249 chan
->GetSecurityInfo(getter_AddRefs(securityInfo
));
250 nsCString secInfoStr
;
252 nsCOMPtr
<nsISerializable
> serializable
= do_QueryInterface(securityInfo
);
254 NS_SerializeToString(serializable
, secInfoStr
);
256 NS_ERROR("Can't serialize security info");
257 return NS_ERROR_UNEXPECTED
;
262 !SendOnStartRequest(status
, contentLength
, charsetSource
, charset
, secInfoStr
)) {
263 return NS_ERROR_UNEXPECTED
;
270 WyciwygChannelParent::OnStopRequest(nsIRequest
*aRequest
,
271 nsISupports
*aContext
,
272 nsresult aStatusCode
)
274 LOG(("WyciwygChannelParent::OnStopRequest: [this=%p status=%ul]\n",
277 if (mIPCClosed
|| !SendOnStopRequest(aStatusCode
)) {
278 return NS_ERROR_UNEXPECTED
;
284 //-----------------------------------------------------------------------------
285 // WyciwygChannelParent::nsIStreamListener
286 //-----------------------------------------------------------------------------
289 WyciwygChannelParent::OnDataAvailable(nsIRequest
*aRequest
,
290 nsISupports
*aContext
,
291 nsIInputStream
*aInputStream
,
295 LOG(("WyciwygChannelParent::OnDataAvailable [this=%p]\n", this));
298 nsresult rv
= NS_ReadInputStreamToString(aInputStream
, data
, aCount
);
302 if (mIPCClosed
|| !SendOnDataAvailable(data
, aOffset
)) {
303 return NS_ERROR_UNEXPECTED
;
309 //-----------------------------------------------------------------------------
310 // WyciwygChannelParent::nsIInterfaceRequestor
311 //-----------------------------------------------------------------------------
314 WyciwygChannelParent::GetInterface(const nsIID
& uuid
, void** result
)
316 // Only support nsILoadContext if child channel's callbacks did too
317 if (uuid
.Equals(NS_GET_IID(nsILoadContext
)) && mLoadContext
) {
318 NS_ADDREF(mLoadContext
);
319 *result
= static_cast<nsILoadContext
*>(mLoadContext
);
323 return QueryInterface(uuid
, result
);