2014-07-10 10:56:36 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
#ifndef nsBaseChannel_h__
|
|
|
|
#define nsBaseChannel_h__
|
|
|
|
|
2017-07-05 01:47:00 +03:00
|
|
|
#include "mozilla/net/NeckoTargetHolder.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
#include "nsString.h"
|
2006-02-17 19:54:27 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsHashPropertyBag.h"
|
2006-02-17 19:54:27 +03:00
|
|
|
#include "nsInputStreamPump.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsILoadGroup.h"
|
2014-07-10 10:56:36 +04:00
|
|
|
#include "nsILoadInfo.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
|
|
|
#include "nsITransport.h"
|
2010-07-28 22:33:06 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2014-06-17 20:39:26 +04:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
|
|
|
#include "nsIThreadRetargetableStreamListener.h"
|
2012-10-05 23:53:07 +04:00
|
|
|
#include "PrivateBrowsingChannel.h"
|
2008-09-30 01:02:44 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2013-09-22 07:04:57 +04:00
|
|
|
class nsIInputStream;
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseChannel is designed to be subclassed. The subclass is responsible for
|
|
|
|
// implementing the OpenContentStream method, which will be called by the
|
|
|
|
// nsIChannel::AsyncOpen and nsIChannel::Open implementations.
|
|
|
|
//
|
|
|
|
// nsBaseChannel implements nsIInterfaceRequestor to provide a convenient way
|
|
|
|
// for subclasses to query both the nsIChannel::notificationCallbacks and
|
|
|
|
// nsILoadGroup::notificationCallbacks for supported interfaces.
|
|
|
|
//
|
|
|
|
// nsBaseChannel implements nsITransportEventSink to support progress & status
|
|
|
|
// notifications generated by the transport layer.
|
|
|
|
|
|
|
|
class nsBaseChannel : public nsHashPropertyBag
|
|
|
|
, public nsIChannel
|
2014-06-17 20:39:26 +04:00
|
|
|
, public nsIThreadRetargetableRequest
|
2005-11-12 21:17:19 +03:00
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsITransportEventSink
|
2010-07-28 22:33:06 +04:00
|
|
|
, public nsIAsyncVerifyRedirectCallback
|
2012-10-05 23:53:07 +04:00
|
|
|
, public mozilla::net::PrivateBrowsingChannel<nsBaseChannel>
|
2017-07-05 01:47:00 +03:00
|
|
|
, public mozilla::net::NeckoTargetHolder
|
2014-02-27 13:18:34 +04:00
|
|
|
, protected nsIStreamListener
|
2014-06-17 20:39:26 +04:00
|
|
|
, protected nsIThreadRetargetableStreamListener
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIREQUEST
|
|
|
|
NS_DECL_NSICHANNEL
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSITRANSPORTEVENTSINK
|
2010-07-28 22:33:06 +04:00
|
|
|
NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
|
2014-06-17 20:39:26 +04:00
|
|
|
NS_DECL_NSITHREADRETARGETABLEREQUEST
|
|
|
|
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
nsBaseChannel();
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
// This method must be called to initialize the basechannel instance.
|
|
|
|
nsresult Init() {
|
2013-09-02 12:41:57 +04:00
|
|
|
return NS_OK;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
protected:
|
2005-11-12 21:17:19 +03:00
|
|
|
// -----------------------------------------------
|
|
|
|
// Methods to be implemented by the derived class:
|
|
|
|
|
2014-09-29 19:48:11 +04:00
|
|
|
virtual ~nsBaseChannel();
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
private:
|
2005-11-12 21:17:19 +03:00
|
|
|
// Implemented by subclass to supply data stream. The parameter, async, is
|
|
|
|
// true when called from nsIChannel::AsyncOpen and false otherwise. When
|
|
|
|
// async is true, the resulting stream will be used with a nsIInputStreamPump
|
|
|
|
// instance. This means that if it is a non-blocking stream that supports
|
|
|
|
// nsIAsyncInputStream that it will be read entirely on the main application
|
|
|
|
// thread, and its AsyncWait method will be called whenever ReadSegments
|
|
|
|
// returns NS_BASE_STREAM_WOULD_BLOCK. Otherwise, if the stream is blocking,
|
|
|
|
// then it will be read on one of the background I/O threads, and it does not
|
|
|
|
// need to implement ReadSegments. If async is false, this method may return
|
|
|
|
// NS_ERROR_NOT_IMPLEMENTED to cause the basechannel to implement Open in
|
|
|
|
// terms of AsyncOpen (see NS_ImplementChannelOpen).
|
2008-09-30 01:02:44 +04:00
|
|
|
// A callee is allowed to return an nsIChannel instead of an nsIInputStream.
|
|
|
|
// That case will be treated as a redirect to the new channel. By default
|
|
|
|
// *channel will be set to null by the caller, so callees who don't want to
|
|
|
|
// return one an just not touch it.
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual nsresult OpenContentStream(bool async, nsIInputStream **stream,
|
2008-09-30 01:02:44 +04:00
|
|
|
nsIChannel** channel) = 0;
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2017-04-08 00:25:33 +03:00
|
|
|
// Implemented by subclass to begin pumping data for an async channel, in
|
|
|
|
// lieu of returning a stream. If implemented, OpenContentStream will never
|
|
|
|
// be called for async channels. If not implemented, AsyncOpen2 will fall
|
|
|
|
// back to OpenContentStream.
|
|
|
|
//
|
|
|
|
// On success, the callee must begin pumping data to the stream listener,
|
|
|
|
// and at some point call OnStartRequest followed by OnStopRequest.
|
|
|
|
// Additionally, it may provide a request object which may be used to
|
|
|
|
// suspend, resume, and cancel the underlying request.
|
|
|
|
virtual nsresult BeginAsyncRead(nsIStreamListener* listener, nsIRequest** request) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// The basechannel calls this method from its OnTransportStatus method to
|
|
|
|
// determine whether to call nsIProgressEventSink::OnStatus in addition to
|
|
|
|
// nsIProgressEventSink::OnProgress. This method may be overriden by the
|
|
|
|
// subclass to enable nsIProgressEventSink::OnStatus events. If this method
|
|
|
|
// returns true, then the statusArg out param specifies the "statusArg" value
|
|
|
|
// to pass to the OnStatus method. By default, OnStatus messages are
|
|
|
|
// suppressed. The status parameter passed to this method is the status value
|
|
|
|
// from the OnTransportStatus method.
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool GetStatusArg(nsresult status, nsString &statusArg) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
// Called when the callbacks available to this channel may have changed.
|
|
|
|
virtual void OnCallbacksChanged() {
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:02:00 +04:00
|
|
|
// Called when our channel is done, to allow subclasses to drop resources.
|
|
|
|
virtual void OnChannelDone() {
|
|
|
|
}
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
public:
|
2005-11-12 21:17:19 +03:00
|
|
|
// ----------------------------------------------
|
|
|
|
// Methods provided for use by the derived class:
|
|
|
|
|
|
|
|
// Redirect to another channel. This method takes care of notifying
|
2008-09-30 01:02:44 +04:00
|
|
|
// observers of this redirect as well as of opening the new channel, if asked
|
|
|
|
// to do so. It also cancels |this| with the status code
|
|
|
|
// NS_BINDING_REDIRECTED. A failure return from this method means that the
|
|
|
|
// redirect could not be performed (no channel was opened; this channel
|
|
|
|
// wasn't canceled.) The redirectFlags parameter consists of the flag values
|
|
|
|
// defined on nsIChannelEventSink.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Redirect(nsIChannel *newChannel, uint32_t redirectFlags,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool openNewChannel);
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2006-02-14 21:46:12 +03:00
|
|
|
// Tests whether a type hint was set. Subclasses can use this to decide
|
|
|
|
// whether to call SetContentType.
|
|
|
|
// NOTE: This is only reliable if the subclass didn't itself call
|
|
|
|
// SetContentType, and should also not be called after OpenContentStream.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasContentTypeHint() const;
|
2006-02-14 21:46:12 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// The URI member should be initialized before the channel is used, and then
|
|
|
|
// it should never be changed again until the channel is destroyed.
|
|
|
|
nsIURI *URI() {
|
|
|
|
return mURI;
|
|
|
|
}
|
|
|
|
void SetURI(nsIURI *uri) {
|
|
|
|
NS_ASSERTION(uri, "must specify a non-null URI");
|
|
|
|
NS_ASSERTION(!mURI, "must not modify URI");
|
2008-10-16 00:05:23 +04:00
|
|
|
NS_ASSERTION(!mOriginalURI, "how did that get set so early?");
|
2005-11-12 21:17:19 +03:00
|
|
|
mURI = uri;
|
2008-10-16 00:05:23 +04:00
|
|
|
mOriginalURI = uri;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
nsIURI *OriginalURI() {
|
2008-10-16 00:05:23 +04:00
|
|
|
return mOriginalURI;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The security info is a property of the transport-layer, which should be
|
|
|
|
// assigned by the subclass.
|
|
|
|
nsISupports *SecurityInfo() {
|
2017-07-06 15:00:35 +03:00
|
|
|
return mSecurityInfo;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
void SetSecurityInfo(nsISupports *info) {
|
|
|
|
mSecurityInfo = info;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the load flags
|
2012-08-22 19:56:38 +04:00
|
|
|
bool HasLoadFlag(uint32_t flag) {
|
2005-11-12 21:17:19 +03:00
|
|
|
return (mLoadFlags & flag) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a short-cut to calling nsIRequest::IsPending()
|
2014-03-11 02:04:28 +04:00
|
|
|
virtual bool Pending() const {
|
2017-04-08 00:25:33 +03:00
|
|
|
return mPumpingData || mWaitingOnAsyncRedirect;
|
2014-03-11 02:04:28 +04:00
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
// Helper function for querying the channel's notification callbacks.
|
|
|
|
template <class T> void GetCallback(nsCOMPtr<T> &result) {
|
2005-11-14 18:27:46 +03:00
|
|
|
GetInterface(NS_GET_TEMPLATE_IID(T), getter_AddRefs(result));
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a subclass does not want to feed transport-layer progress events to the
|
|
|
|
// base channel via nsITransportEventSink, then it may set this flag to cause
|
|
|
|
// the base channel to synthesize progress events when it receives data from
|
|
|
|
// the content stream. By default, progress events are not synthesized.
|
2011-09-29 10:19:26 +04:00
|
|
|
void EnableSynthesizedProgressEvents(bool enable) {
|
2005-11-12 21:17:19 +03:00
|
|
|
mSynthProgressEvents = enable;
|
|
|
|
}
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
// Some subclasses may wish to manually insert a stream listener between this
|
|
|
|
// and the channel's listener. The following methods make that possible.
|
|
|
|
void SetStreamListener(nsIStreamListener *listener) {
|
|
|
|
mListener = listener;
|
|
|
|
}
|
|
|
|
nsIStreamListener *StreamListener() {
|
|
|
|
return mListener;
|
|
|
|
}
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Pushes a new stream converter in front of the channel's stream listener.
|
|
|
|
// The fromType and toType values are passed to nsIStreamConverterService's
|
|
|
|
// AsyncConvertData method. If invalidatesContentLength is true, then the
|
|
|
|
// channel's content-length property will be assigned a value of -1. This is
|
|
|
|
// necessary when the converter changes the length of the resulting data
|
|
|
|
// stream, which is almost always the case for a "stream converter" ;-)
|
|
|
|
// This function optionally returns a reference to the new converter.
|
|
|
|
nsresult PushStreamConverter(const char *fromType, const char *toType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool invalidatesContentLength = true,
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIStreamListener **converter = nullptr);
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2014-06-17 20:39:26 +04:00
|
|
|
protected:
|
|
|
|
void DisallowThreadRetargeting() {
|
|
|
|
mAllowThreadRetargeting = false;
|
|
|
|
}
|
|
|
|
|
2017-07-05 01:47:00 +03:00
|
|
|
virtual void SetupNeckoTarget();
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
private:
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
// Called to setup mPump and call AsyncRead on it.
|
|
|
|
nsresult BeginPumpingData();
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// Called when the callbacks available to this channel may have changed.
|
|
|
|
void CallbacksChanged() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mProgressSink = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mQueriedProgressSink = false;
|
2006-03-03 19:27:35 +03:00
|
|
|
OnCallbacksChanged();
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:02:00 +04:00
|
|
|
// Called when our channel is done. This should drop no-longer-needed pointers.
|
|
|
|
void ChannelDone() {
|
|
|
|
mListener = nullptr;
|
|
|
|
mListenerContext = nullptr;
|
|
|
|
OnChannelDone();
|
|
|
|
}
|
|
|
|
|
2008-09-30 01:02:44 +04:00
|
|
|
// Handle an async redirect callback. This will only be called if we
|
|
|
|
// returned success from AsyncOpen while posting a redirect runnable.
|
|
|
|
void HandleAsyncRedirect(nsIChannel* newChannel);
|
2010-07-28 22:33:06 +04:00
|
|
|
void ContinueHandleAsyncRedirect(nsresult result);
|
|
|
|
nsresult ContinueRedirect();
|
2008-09-30 01:02:44 +04:00
|
|
|
|
2010-05-22 01:03:02 +04:00
|
|
|
// start URI classifier if requested
|
|
|
|
void ClassifyURI();
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class RedirectRunnable : public mozilla::Runnable
|
2008-09-30 01:02:44 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
RedirectRunnable(nsBaseChannel* chan, nsIChannel* newChannel)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("nsBaseChannel::RedirectRunnable")
|
|
|
|
, mChannel(chan)
|
|
|
|
, mNewChannel(newChannel)
|
2008-09-30 01:02:44 +04:00
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(newChannel, "Must have channel to redirect to");
|
2008-09-30 01:02:44 +04:00
|
|
|
}
|
2016-08-08 05:18:10 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
2008-09-30 01:02:44 +04:00
|
|
|
{
|
|
|
|
mChannel->HandleAsyncRedirect(mNewChannel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsBaseChannel> mChannel;
|
2008-09-30 01:02:44 +04:00
|
|
|
nsCOMPtr<nsIChannel> mNewChannel;
|
|
|
|
};
|
|
|
|
friend class RedirectRunnable;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsInputStreamPump> mPump;
|
2017-04-08 00:25:33 +03:00
|
|
|
RefPtr<nsIRequest> mRequest;
|
|
|
|
bool mPumpingData;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIProgressEventSink> mProgressSink;
|
|
|
|
nsCOMPtr<nsIURI> mOriginalURI;
|
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
nsCOMPtr<nsISupports> mSecurityInfo;
|
2010-07-28 22:33:06 +04:00
|
|
|
nsCOMPtr<nsIChannel> mRedirectChannel;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCString mContentType;
|
|
|
|
nsCString mContentCharset;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLoadFlags;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mQueriedProgressSink;
|
|
|
|
bool mSynthProgressEvents;
|
2014-06-17 20:39:26 +04:00
|
|
|
bool mAllowThreadRetargeting;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWaitingOnAsyncRedirect;
|
|
|
|
bool mOpenRedirectChannel;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mRedirectFlags;
|
2010-08-10 22:47:00 +04:00
|
|
|
|
|
|
|
protected:
|
2012-07-23 22:12:03 +04:00
|
|
|
nsCOMPtr<nsIURI> mURI;
|
2010-08-10 22:47:00 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> mLoadGroup;
|
2015-08-13 07:36:33 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> mLoadInfo;
|
2012-07-23 02:35:33 +04:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
|
2010-08-10 22:47:00 +04:00
|
|
|
nsCOMPtr<nsIStreamListener> mListener;
|
|
|
|
nsCOMPtr<nsISupports> mListenerContext;
|
|
|
|
nsresult mStatus;
|
2012-10-02 20:43:00 +04:00
|
|
|
uint32_t mContentDispositionHint;
|
|
|
|
nsAutoPtr<nsString> mContentDispositionFilename;
|
2012-10-22 21:51:07 +04:00
|
|
|
int64_t mContentLength;
|
2015-04-25 16:58:53 +03:00
|
|
|
bool mWasOpened;
|
2012-10-05 23:53:07 +04:00
|
|
|
|
|
|
|
friend class mozilla::net::PrivateBrowsingChannel<nsBaseChannel>;
|
2005-11-12 21:17:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // !nsBaseChannel_h__
|