Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / base / SimpleChannel.cpp
blob6297331135e1b71ceeba66d33e5784ed5986d245
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"
23 namespace mozilla {
24 namespace net {
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));
38 MOZ_ASSERT(stream);
40 mCallbacks = nullptr;
42 *channel = nullptr;
43 stream.forget(streamOut);
44 return NS_OK;
47 nsresult SimpleChannel::BeginAsyncRead(nsIStreamListener* listener,
48 nsIRequest** request,
49 nsICancelable** cancelableRequest) {
50 NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
52 RequestOrReason res = mCallbacks->StartAsyncRead(listener, this);
54 if (res.isErr()) {
55 return res.propagateErr();
58 mCallbacks = nullptr;
60 RequestOrCancelable value = res.unwrap();
62 if (value.is<NotNullRequest>()) {
63 nsCOMPtr<nsIRequest> req = value.as<NotNullRequest>().get();
64 req.forget(request);
65 } else if (value.is<NotNullCancelable>()) {
66 nsCOMPtr<nsICancelable> cancelable = value.as<NotNullCancelable>().get();
67 cancelable.forget(cancelableRequest);
68 } else {
69 MOZ_ASSERT_UNREACHABLE(
70 "StartAsyncRead didn't return a NotNull nsIRequest or nsICancelable.");
71 return NS_ERROR_UNEXPECTED;
74 return NS_OK;
77 NS_IMPL_ISUPPORTS_INHERITED(SimpleChannelChild, SimpleChannel, nsIChildChannel)
79 SimpleChannelChild::SimpleChannelChild(
80 UniquePtr<SimpleChannelCallbacks>&& aCallbacks)
81 : SimpleChannel(std::move(aCallbacks)) {}
83 NS_IMETHODIMP
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(),
94 aId)) {
95 return NS_ERROR_FAILURE;
98 return NS_OK;
101 NS_IMETHODIMP
102 SimpleChannelChild::CompleteRedirectSetup(nsIStreamListener* aListener) {
103 if (CanSend()) {
104 MOZ_ASSERT(NS_IsMainThread());
107 nsresult rv;
108 rv = AsyncOpen(aListener);
109 if (NS_WARN_IF(NS_FAILED(rv))) {
110 return rv;
113 if (CanSend()) {
114 Unused << Send__delete__(this);
116 return NS_OK;
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));
125 } else {
126 chan = new SimpleChannel(std::move(aCallbacks));
129 chan->SetURI(aURI);
131 MOZ_ALWAYS_SUCCEEDS(chan->SetLoadInfo(aLoadInfo));
133 return chan.forget();
136 } // namespace net
137 } // namespace mozilla