Source code

Revision control

Other Tools

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 "SimpleChannel.h"
7
8
#include "nsBaseChannel.h"
9
#include "nsIChannel.h"
10
#include "nsIChildChannel.h"
11
#include "nsIInputStream.h"
12
#include "nsIRequest.h"
13
#include "nsISupportsImpl.h"
14
#include "nsNetUtil.h"
15
16
#include "mozilla/Unused.h"
17
#include "mozilla/dom/ContentChild.h"
18
#include "mozilla/net/NeckoChild.h"
19
#include "mozilla/net/PSimpleChannelChild.h"
20
21
namespace mozilla {
22
namespace net {
23
24
SimpleChannel::SimpleChannel(UniquePtr<SimpleChannelCallbacks>&& aCallbacks)
25
: mCallbacks(std::move(aCallbacks)) {
26
EnableSynthesizedProgressEvents(true);
27
}
28
29
nsresult SimpleChannel::OpenContentStream(bool async,
30
nsIInputStream** streamOut,
31
nsIChannel** channel) {
32
NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
33
34
nsCOMPtr<nsIInputStream> stream;
35
MOZ_TRY_VAR(stream, mCallbacks->OpenContentStream(async, this));
36
MOZ_ASSERT(stream);
37
38
mCallbacks = nullptr;
39
40
*channel = nullptr;
41
stream.forget(streamOut);
42
return NS_OK;
43
}
44
45
nsresult SimpleChannel::BeginAsyncRead(nsIStreamListener* listener,
46
nsIRequest** request) {
47
NS_ENSURE_TRUE(mCallbacks, NS_ERROR_UNEXPECTED);
48
49
nsCOMPtr<nsIRequest> req;
50
MOZ_TRY_VAR(req, mCallbacks->StartAsyncRead(listener, this));
51
52
mCallbacks = nullptr;
53
54
req.forget(request);
55
return NS_OK;
56
}
57
58
NS_IMPL_ISUPPORTS_INHERITED(SimpleChannelChild, SimpleChannel, nsIChildChannel)
59
60
SimpleChannelChild::SimpleChannelChild(
61
UniquePtr<SimpleChannelCallbacks>&& aCallbacks)
62
: SimpleChannel(std::move(aCallbacks)) {}
63
64
NS_IMETHODIMP
65
SimpleChannelChild::ConnectParent(uint32_t aId) {
66
MOZ_ASSERT(NS_IsMainThread());
67
mozilla::dom::ContentChild* cc =
68
static_cast<mozilla::dom::ContentChild*>(gNeckoChild->Manager());
69
if (cc->IsShuttingDown()) {
70
return NS_ERROR_FAILURE;
71
}
72
73
// Reference freed in DeallocPSimpleChannelChild.
74
if (!gNeckoChild->SendPSimpleChannelConstructor(do_AddRef(this).take(),
75
aId)) {
76
return NS_ERROR_FAILURE;
77
}
78
79
return NS_OK;
80
}
81
82
NS_IMETHODIMP
83
SimpleChannelChild::CompleteRedirectSetup(nsIStreamListener* aListener,
84
nsISupports* aContext) {
85
if (CanSend()) {
86
MOZ_ASSERT(NS_IsMainThread());
87
}
88
89
nsresult rv;
90
MOZ_ASSERT(!aContext, "aContext should be null!");
91
rv = AsyncOpen(aListener);
92
if (NS_WARN_IF(NS_FAILED(rv))) {
93
return rv;
94
}
95
96
if (CanSend()) {
97
Unused << Send__delete__(this);
98
}
99
return NS_OK;
100
}
101
102
already_AddRefed<nsIChannel> NS_NewSimpleChannelInternal(
103
nsIURI* aURI, nsILoadInfo* aLoadInfo,
104
UniquePtr<SimpleChannelCallbacks>&& aCallbacks) {
105
RefPtr<SimpleChannel> chan;
106
if (IsNeckoChild()) {
107
chan = new SimpleChannelChild(std::move(aCallbacks));
108
} else {
109
chan = new SimpleChannel(std::move(aCallbacks));
110
}
111
112
chan->SetURI(aURI);
113
114
MOZ_ALWAYS_SUCCEEDS(chan->SetLoadInfo(aLoadInfo));
115
116
return chan.forget();
117
}
118
119
} // namespace net
120
} // namespace mozilla