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 "SimpleChannel.h"
8 #include "nsBaseChannel.h"
9 #include "nsIChannel.h"
10 #include "nsIChildChannel.h"
11 #include "nsICancelable.h"
12 #include "nsIInputStream.h"
13 #include "nsIRequest.h"
14 #include "nsISupportsImpl.h"
15 #include "nsNetUtil.h"
17 #include "mozilla/Try.h"
18 #include "mozilla/Unused.h"
19 #include "mozilla/dom/ContentChild.h"
20 #include "mozilla/net/NeckoChild.h"
21 #include "mozilla/net/PSimpleChannelChild.h"
26 SimpleChannel::SimpleChannel(UniquePtr
<SimpleChannelCallbacks
>&& aCallbacks
)
27 : mCallbacks(std::move(aCallbacks
)) {
28 EnableSynthesizedProgressEvents(true);
31 nsresult
SimpleChannel::OpenContentStream(bool async
,
32 nsIInputStream
** streamOut
,
33 nsIChannel
** channel
) {
34 NS_ENSURE_TRUE(mCallbacks
, NS_ERROR_UNEXPECTED
);
36 nsCOMPtr
<nsIInputStream
> stream
;
37 MOZ_TRY_VAR(stream
, mCallbacks
->OpenContentStream(async
, this));
43 stream
.forget(streamOut
);
47 nsresult
SimpleChannel::BeginAsyncRead(nsIStreamListener
* listener
,
49 nsICancelable
** cancelableRequest
) {
50 NS_ENSURE_TRUE(mCallbacks
, NS_ERROR_UNEXPECTED
);
52 RequestOrReason res
= mCallbacks
->StartAsyncRead(listener
, this);
55 return res
.propagateErr();
60 RequestOrCancelable value
= res
.unwrap();
62 if (value
.is
<NotNullRequest
>()) {
63 nsCOMPtr
<nsIRequest
> req
= value
.as
<NotNullRequest
>().get();
65 } else if (value
.is
<NotNullCancelable
>()) {
66 nsCOMPtr
<nsICancelable
> cancelable
= value
.as
<NotNullCancelable
>().get();
67 cancelable
.forget(cancelableRequest
);
69 MOZ_ASSERT_UNREACHABLE(
70 "StartAsyncRead didn't return a NotNull nsIRequest or nsICancelable.");
71 return NS_ERROR_UNEXPECTED
;
77 NS_IMPL_ISUPPORTS_INHERITED(SimpleChannelChild
, SimpleChannel
, nsIChildChannel
)
79 SimpleChannelChild::SimpleChannelChild(
80 UniquePtr
<SimpleChannelCallbacks
>&& aCallbacks
)
81 : SimpleChannel(std::move(aCallbacks
)) {}
84 SimpleChannelChild::ConnectParent(uint32_t aId
) {
85 MOZ_ASSERT(NS_IsMainThread());
86 mozilla::dom::ContentChild
* cc
=
87 static_cast<mozilla::dom::ContentChild
*>(gNeckoChild
->Manager());
88 if (cc
->IsShuttingDown()) {
89 return NS_ERROR_FAILURE
;
92 // Reference freed in DeallocPSimpleChannelChild.
93 if (!gNeckoChild
->SendPSimpleChannelConstructor(do_AddRef(this).take(),
95 return NS_ERROR_FAILURE
;
102 SimpleChannelChild::CompleteRedirectSetup(nsIStreamListener
* aListener
) {
104 MOZ_ASSERT(NS_IsMainThread());
108 rv
= AsyncOpen(aListener
);
109 if (NS_WARN_IF(NS_FAILED(rv
))) {
114 Unused
<< Send__delete__(this);
119 already_AddRefed
<nsIChannel
> NS_NewSimpleChannelInternal(
120 nsIURI
* aURI
, nsILoadInfo
* aLoadInfo
,
121 UniquePtr
<SimpleChannelCallbacks
>&& aCallbacks
) {
122 RefPtr
<SimpleChannel
> chan
;
123 if (IsNeckoChild()) {
124 chan
= new SimpleChannelChild(std::move(aCallbacks
));
126 chan
= new SimpleChannel(std::move(aCallbacks
));
131 MOZ_ALWAYS_SUCCEEDS(chan
->SetLoadInfo(aLoadInfo
));
133 return chan
.forget();
137 } // namespace mozilla