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 nsBaseChannel_h__
7
#define nsBaseChannel_h__
8
9
#include "mozilla/net/NeckoTargetHolder.h"
10
#include "mozilla/MozPromise.h"
11
#include "nsString.h"
12
#include "nsAutoPtr.h"
13
#include "nsCOMPtr.h"
14
#include "nsHashPropertyBag.h"
15
#include "nsInputStreamPump.h"
16
17
#include "nsIChannel.h"
18
#include "nsIURI.h"
19
#include "nsILoadGroup.h"
20
#include "nsILoadInfo.h"
21
#include "nsIStreamListener.h"
22
#include "nsIInterfaceRequestor.h"
23
#include "nsIProgressEventSink.h"
24
#include "nsITransport.h"
25
#include "nsIAsyncVerifyRedirectCallback.h"
26
#include "nsIThreadRetargetableRequest.h"
27
#include "nsIThreadRetargetableStreamListener.h"
28
#include "mozilla/net/PrivateBrowsingChannel.h"
29
#include "nsThreadUtils.h"
30
31
class nsIInputStream;
32
33
//-----------------------------------------------------------------------------
34
// nsBaseChannel is designed to be subclassed. The subclass is responsible for
35
// implementing the OpenContentStream method, which will be called by the
36
// nsIChannel::AsyncOpen and nsIChannel::Open implementations.
37
//
38
// nsBaseChannel implements nsIInterfaceRequestor to provide a convenient way
39
// for subclasses to query both the nsIChannel::notificationCallbacks and
40
// nsILoadGroup::notificationCallbacks for supported interfaces.
41
//
42
// nsBaseChannel implements nsITransportEventSink to support progress & status
43
// notifications generated by the transport layer.
44
45
class nsBaseChannel
46
: public nsHashPropertyBag,
47
public nsIChannel,
48
public nsIThreadRetargetableRequest,
49
public nsIInterfaceRequestor,
50
public nsITransportEventSink,
51
public nsIAsyncVerifyRedirectCallback,
52
public mozilla::net::PrivateBrowsingChannel<nsBaseChannel>,
53
public mozilla::net::NeckoTargetHolder,
54
protected nsIStreamListener,
55
protected nsIThreadRetargetableStreamListener {
56
public:
57
NS_DECL_ISUPPORTS_INHERITED
58
NS_DECL_NSIREQUEST
59
NS_DECL_NSICHANNEL
60
NS_DECL_NSIINTERFACEREQUESTOR
61
NS_DECL_NSITRANSPORTEVENTSINK
62
NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
63
NS_DECL_NSITHREADRETARGETABLEREQUEST
64
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
65
66
nsBaseChannel();
67
68
// This method must be called to initialize the basechannel instance.
69
nsresult Init() { return NS_OK; }
70
71
protected:
72
// -----------------------------------------------
73
// Methods to be implemented by the derived class:
74
75
virtual ~nsBaseChannel();
76
77
using BlockingPromise = mozilla::MozPromise<nsresult, nsresult, true>;
78
79
private:
80
// Implemented by subclass to supply data stream. The parameter, async, is
81
// true when called from nsIChannel::AsyncOpen and false otherwise. When
82
// async is true, the resulting stream will be used with a nsIInputStreamPump
83
// instance. This means that if it is a non-blocking stream that supports
84
// nsIAsyncInputStream that it will be read entirely on the main application
85
// thread, and its AsyncWait method will be called whenever ReadSegments
86
// returns NS_BASE_STREAM_WOULD_BLOCK. Otherwise, if the stream is blocking,
87
// then it will be read on one of the background I/O threads, and it does not
88
// need to implement ReadSegments. If async is false, this method may return
89
// NS_ERROR_NOT_IMPLEMENTED to cause the basechannel to implement Open in
90
// terms of AsyncOpen (see NS_ImplementChannelOpen).
91
// A callee is allowed to return an nsIChannel instead of an nsIInputStream.
92
// That case will be treated as a redirect to the new channel. By default
93
// *channel will be set to null by the caller, so callees who don't want to
94
// return one an just not touch it.
95
virtual nsresult OpenContentStream(bool async, nsIInputStream** stream,
96
nsIChannel** channel) = 0;
97
98
// Implemented by subclass to begin pumping data for an async channel, in
99
// lieu of returning a stream. If implemented, OpenContentStream will never
100
// be called for async channels. If not implemented, AsyncOpen will fall
101
// back to OpenContentStream.
102
//
103
// On success, the callee must begin pumping data to the stream listener,
104
// and at some point call OnStartRequest followed by OnStopRequest.
105
// Additionally, it may provide a request object which may be used to
106
// suspend, resume, and cancel the underlying request.
107
virtual nsresult BeginAsyncRead(nsIStreamListener* listener,
108
nsIRequest** request) {
109
return NS_ERROR_NOT_IMPLEMENTED;
110
}
111
112
// This method may return a promise that will keep the input stream pump
113
// suspended until the promise is resolved or rejected. On resolution the
114
// pump is resumed. On rejection the channel is canceled with the resulting
115
// error and then the pump is also resumed to propagate the error to the
116
// channel listener. Use it to do any asynchronous/background tasks you need
117
// to finish prior calling OnStartRequest of the listener. This method is
118
// called right after OpenContentStream() with async == true, after the input
119
// stream pump has already been called asyncRead().
120
virtual nsresult ListenerBlockingPromise(BlockingPromise** aPromise) {
121
NS_ENSURE_ARG(aPromise);
122
*aPromise = nullptr;
123
return NS_OK;
124
}
125
126
// The basechannel calls this method from its OnTransportStatus method to
127
// determine whether to call nsIProgressEventSink::OnStatus in addition to
128
// nsIProgressEventSink::OnProgress. This method may be overriden by the
129
// subclass to enable nsIProgressEventSink::OnStatus events. If this method
130
// returns true, then the statusArg out param specifies the "statusArg" value
131
// to pass to the OnStatus method. By default, OnStatus messages are
132
// suppressed. The status parameter passed to this method is the status value
133
// from the OnTransportStatus method.
134
virtual bool GetStatusArg(nsresult status, nsString& statusArg) {
135
return false;
136
}
137
138
// Called when the callbacks available to this channel may have changed.
139
virtual void OnCallbacksChanged() {}
140
141
// Called when our channel is done, to allow subclasses to drop resources.
142
virtual void OnChannelDone() {}
143
144
public:
145
// ----------------------------------------------
146
// Methods provided for use by the derived class:
147
148
// Redirect to another channel. This method takes care of notifying
149
// observers of this redirect as well as of opening the new channel, if asked
150
// to do so. It also cancels |this| with the status code
151
// NS_BINDING_REDIRECTED. A failure return from this method means that the
152
// redirect could not be performed (no channel was opened; this channel
153
// wasn't canceled.) The redirectFlags parameter consists of the flag values
154
// defined on nsIChannelEventSink.
155
nsresult Redirect(nsIChannel* newChannel, uint32_t redirectFlags,
156
bool openNewChannel);
157
158
// Tests whether a type hint was set. Subclasses can use this to decide
159
// whether to call SetContentType.
160
// NOTE: This is only reliable if the subclass didn't itself call
161
// SetContentType, and should also not be called after OpenContentStream.
162
bool HasContentTypeHint() const;
163
164
// The URI member should be initialized before the channel is used, and then
165
// it should never be changed again until the channel is destroyed.
166
nsIURI* URI() { return mURI; }
167
void SetURI(nsIURI* uri) {
168
NS_ASSERTION(uri, "must specify a non-null URI");
169
NS_ASSERTION(!mURI, "must not modify URI");
170
NS_ASSERTION(!mOriginalURI, "how did that get set so early?");
171
mURI = uri;
172
mOriginalURI = uri;
173
}
174
nsIURI* OriginalURI() { return mOriginalURI; }
175
176
// The security info is a property of the transport-layer, which should be
177
// assigned by the subclass.
178
nsISupports* SecurityInfo() { return mSecurityInfo; }
179
void SetSecurityInfo(nsISupports* info) { mSecurityInfo = info; }
180
181
// Test the load flags
182
bool HasLoadFlag(uint32_t flag) { return (mLoadFlags & flag) != 0; }
183
184
// This is a short-cut to calling nsIRequest::IsPending()
185
virtual bool Pending() const {
186
return mPumpingData || mWaitingOnAsyncRedirect;
187
}
188
189
// Helper function for querying the channel's notification callbacks.
190
template <class T>
191
void GetCallback(nsCOMPtr<T>& result) {
192
GetInterface(NS_GET_TEMPLATE_IID(T), getter_AddRefs(result));
193
}
194
195
// If a subclass does not want to feed transport-layer progress events to the
196
// base channel via nsITransportEventSink, then it may set this flag to cause
197
// the base channel to synthesize progress events when it receives data from
198
// the content stream. By default, progress events are not synthesized.
199
void EnableSynthesizedProgressEvents(bool enable) {
200
mSynthProgressEvents = enable;
201
}
202
203
// Some subclasses may wish to manually insert a stream listener between this
204
// and the channel's listener. The following methods make that possible.
205
void SetStreamListener(nsIStreamListener* listener) { mListener = listener; }
206
nsIStreamListener* StreamListener() { return mListener; }
207
208
// Pushes a new stream converter in front of the channel's stream listener.
209
// The fromType and toType values are passed to nsIStreamConverterService's
210
// AsyncConvertData method. If invalidatesContentLength is true, then the
211
// channel's content-length property will be assigned a value of -1. This is
212
// necessary when the converter changes the length of the resulting data
213
// stream, which is almost always the case for a "stream converter" ;-)
214
// This function optionally returns a reference to the new converter.
215
nsresult PushStreamConverter(const char* fromType, const char* toType,
216
bool invalidatesContentLength = true,
217
nsIStreamListener** converter = nullptr);
218
219
protected:
220
void DisallowThreadRetargeting() { mAllowThreadRetargeting = false; }
221
222
virtual void SetupNeckoTarget();
223
224
private:
225
NS_DECL_NSISTREAMLISTENER
226
NS_DECL_NSIREQUESTOBSERVER
227
228
// Called to setup mPump and call AsyncRead on it.
229
nsresult BeginPumpingData();
230
231
// Called when the callbacks available to this channel may have changed.
232
void CallbacksChanged() {
233
mProgressSink = nullptr;
234
mQueriedProgressSink = false;
235
OnCallbacksChanged();
236
}
237
238
// Called when our channel is done. This should drop no-longer-needed
239
// pointers.
240
void ChannelDone() {
241
mListener = nullptr;
242
OnChannelDone();
243
}
244
245
// Handle an async redirect callback. This will only be called if we
246
// returned success from AsyncOpen while posting a redirect runnable.
247
void HandleAsyncRedirect(nsIChannel* newChannel);
248
void ContinueHandleAsyncRedirect(nsresult result);
249
nsresult ContinueRedirect();
250
251
// start URI classifier if requested
252
void ClassifyURI();
253
254
class RedirectRunnable : public mozilla::Runnable {
255
public:
256
RedirectRunnable(nsBaseChannel* chan, nsIChannel* newChannel)
257
: mozilla::Runnable("nsBaseChannel::RedirectRunnable"),
258
mChannel(chan),
259
mNewChannel(newChannel) {
260
MOZ_ASSERT(newChannel, "Must have channel to redirect to");
261
}
262
263
NS_IMETHOD Run() override {
264
mChannel->HandleAsyncRedirect(mNewChannel);
265
return NS_OK;
266
}
267
268
private:
269
RefPtr<nsBaseChannel> mChannel;
270
nsCOMPtr<nsIChannel> mNewChannel;
271
};
272
friend class RedirectRunnable;
273
274
RefPtr<nsInputStreamPump> mPump;
275
RefPtr<nsIRequest> mRequest;
276
bool mPumpingData;
277
nsCOMPtr<nsIProgressEventSink> mProgressSink;
278
nsCOMPtr<nsIURI> mOriginalURI;
279
nsCOMPtr<nsISupports> mOwner;
280
nsCOMPtr<nsISupports> mSecurityInfo;
281
nsCOMPtr<nsIChannel> mRedirectChannel;
282
nsCString mContentType;
283
nsCString mContentCharset;
284
uint32_t mLoadFlags;
285
bool mQueriedProgressSink;
286
bool mSynthProgressEvents;
287
bool mAllowThreadRetargeting;
288
bool mWaitingOnAsyncRedirect;
289
bool mOpenRedirectChannel;
290
uint32_t mRedirectFlags;
291
292
protected:
293
nsCOMPtr<nsIURI> mURI;
294
nsCOMPtr<nsILoadGroup> mLoadGroup;
295
nsCOMPtr<nsILoadInfo> mLoadInfo;
296
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
297
nsCOMPtr<nsIStreamListener> mListener;
298
nsresult mStatus;
299
uint32_t mContentDispositionHint;
300
nsAutoPtr<nsString> mContentDispositionFilename;
301
int64_t mContentLength;
302
bool mWasOpened;
303
304
friend class mozilla::net::PrivateBrowsingChannel<nsBaseChannel>;
305
};
306
307
#endif // !nsBaseChannel_h__