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
#ifndef SimpleChannel_h
7
#define SimpleChannel_h
8
9
#include "mozilla/ResultExtensions.h"
10
#include "mozilla/UniquePtr.h"
11
#include "nsBaseChannel.h"
12
#include "nsIChildChannel.h"
13
#include "mozilla/net/PSimpleChannelChild.h"
14
#include "nsCOMPtr.h"
15
16
class nsIChannel;
17
class nsIInputStream;
18
class nsILoadInfo;
19
class nsIRequest;
20
class nsIStreamListener;
21
class nsIURI;
22
23
//-----------------------------------------------------------------------------
24
25
namespace mozilla {
26
27
using InputStreamOrReason = Result<nsCOMPtr<nsIInputStream>, nsresult>;
28
using RequestOrReason = Result<nsCOMPtr<nsIRequest>, nsresult>;
29
30
namespace net {
31
32
class SimpleChannelCallbacks {
33
public:
34
virtual InputStreamOrReason OpenContentStream(bool async,
35
nsIChannel* channel) = 0;
36
37
virtual RequestOrReason StartAsyncRead(nsIStreamListener* stream,
38
nsIChannel* channel) = 0;
39
40
virtual ~SimpleChannelCallbacks() = default;
41
};
42
43
template <typename F1, typename F2, typename T>
44
class SimpleChannelCallbacksImpl final : public SimpleChannelCallbacks {
45
public:
46
SimpleChannelCallbacksImpl(F1&& aStartAsyncRead, F2&& aOpenContentStream,
47
T* context)
48
: mStartAsyncRead(aStartAsyncRead),
49
mOpenContentStream(aOpenContentStream),
50
mContext(context) {}
51
52
virtual ~SimpleChannelCallbacksImpl() = default;
53
54
virtual InputStreamOrReason OpenContentStream(bool async,
55
nsIChannel* channel) override {
56
return mOpenContentStream(async, channel, mContext);
57
}
58
59
virtual RequestOrReason StartAsyncRead(nsIStreamListener* listener,
60
nsIChannel* channel) override {
61
return mStartAsyncRead(listener, channel, mContext);
62
}
63
64
private:
65
F1 mStartAsyncRead;
66
F2 mOpenContentStream;
67
RefPtr<T> mContext;
68
};
69
70
class SimpleChannel : public nsBaseChannel {
71
public:
72
explicit SimpleChannel(UniquePtr<SimpleChannelCallbacks>&& aCallbacks);
73
74
protected:
75
virtual ~SimpleChannel() = default;
76
77
virtual nsresult OpenContentStream(bool async, nsIInputStream** streamOut,
78
nsIChannel** channel) override;
79
80
virtual nsresult BeginAsyncRead(nsIStreamListener* listener,
81
nsIRequest** request) override;
82
83
private:
84
UniquePtr<SimpleChannelCallbacks> mCallbacks;
85
};
86
87
class SimpleChannelChild final : public SimpleChannel,
88
public nsIChildChannel,
89
public PSimpleChannelChild {
90
public:
91
explicit SimpleChannelChild(UniquePtr<SimpleChannelCallbacks>&& aCallbacks);
92
93
NS_DECL_ISUPPORTS_INHERITED
94
NS_DECL_NSICHILDCHANNEL
95
96
private:
97
virtual ~SimpleChannelChild() = default;
98
};
99
100
already_AddRefed<nsIChannel> NS_NewSimpleChannelInternal(
101
nsIURI* aURI, nsILoadInfo* aLoadInfo,
102
UniquePtr<SimpleChannelCallbacks>&& aCallbacks);
103
104
} // namespace net
105
} // namespace mozilla
106
107
/**
108
* Creates a simple channel which wraps an input stream created by the given
109
* callbacks. The callbacks are not called until the underlying AsyncOpen or
110
* Open methods are called, and correspond to the nsBaseChannel::StartAsyncRead
111
* and nsBaseChannel::OpenContentStream methods of the same names.
112
*
113
* The last two arguments of each callback are the created channel instance,
114
* and the ref-counted context object passed to NS_NewSimpleChannel. A strong
115
* reference to that object is guaranteed to be kept alive until after a
116
* callback successfully completes.
117
*/
118
template <typename T, typename F1, typename F2>
119
inline already_AddRefed<nsIChannel> NS_NewSimpleChannel(
120
nsIURI* aURI, nsILoadInfo* aLoadInfo, T* context, F1&& aStartAsyncRead,
121
F2&& aOpenContentStream) {
122
using namespace mozilla;
123
124
auto callbacks = MakeUnique<net::SimpleChannelCallbacksImpl<F1, F2, T>>(
125
std::move(aStartAsyncRead), std::move(aOpenContentStream), context);
126
127
return net::NS_NewSimpleChannelInternal(aURI, aLoadInfo,
128
std::move(callbacks));
129
}
130
131
template <typename T, typename F1>
132
inline already_AddRefed<nsIChannel> NS_NewSimpleChannel(nsIURI* aURI,
133
nsILoadInfo* aLoadInfo,
134
T* context,
135
F1&& aStartAsyncRead) {
136
using namespace mozilla;
137
138
auto openContentStream = [](bool async, nsIChannel* channel, T* context) {
139
return Err(NS_ERROR_NOT_IMPLEMENTED);
140
};
141
142
return NS_NewSimpleChannel(aURI, aLoadInfo, context,
143
std::move(aStartAsyncRead),
144
std::move(openContentStream));
145
}
146
147
#endif // SimpleChannel_h