michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsBaseChannel_h__ michael@0: #define nsBaseChannel_h__ michael@0: michael@0: #include "nsString.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsHashPropertyBag.h" michael@0: #include "nsInputStreamPump.h" michael@0: michael@0: #include "nsIChannel.h" michael@0: #include "nsIURI.h" michael@0: #include "nsILoadGroup.h" michael@0: #include "nsIStreamListener.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsIProgressEventSink.h" michael@0: #include "nsITransport.h" michael@0: #include "nsIAsyncVerifyRedirectCallback.h" michael@0: #include "PrivateBrowsingChannel.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: class nsIInputStream; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsBaseChannel is designed to be subclassed. The subclass is responsible for michael@0: // implementing the OpenContentStream method, which will be called by the michael@0: // nsIChannel::AsyncOpen and nsIChannel::Open implementations. michael@0: // michael@0: // nsBaseChannel implements nsIInterfaceRequestor to provide a convenient way michael@0: // for subclasses to query both the nsIChannel::notificationCallbacks and michael@0: // nsILoadGroup::notificationCallbacks for supported interfaces. michael@0: // michael@0: // nsBaseChannel implements nsITransportEventSink to support progress & status michael@0: // notifications generated by the transport layer. michael@0: michael@0: class nsBaseChannel : public nsHashPropertyBag michael@0: , public nsIChannel michael@0: , public nsIInterfaceRequestor michael@0: , public nsITransportEventSink michael@0: , public nsIAsyncVerifyRedirectCallback michael@0: , public mozilla::net::PrivateBrowsingChannel michael@0: , protected nsIStreamListener michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_NSIREQUEST michael@0: NS_DECL_NSICHANNEL michael@0: NS_DECL_NSIINTERFACEREQUESTOR michael@0: NS_DECL_NSITRANSPORTEVENTSINK michael@0: NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK michael@0: michael@0: nsBaseChannel(); michael@0: michael@0: // This method must be called to initialize the basechannel instance. michael@0: nsresult Init() { michael@0: return NS_OK; michael@0: } michael@0: michael@0: protected: michael@0: // ----------------------------------------------- michael@0: // Methods to be implemented by the derived class: michael@0: michael@0: virtual ~nsBaseChannel() {} michael@0: michael@0: private: michael@0: // Implemented by subclass to supply data stream. The parameter, async, is michael@0: // true when called from nsIChannel::AsyncOpen and false otherwise. When michael@0: // async is true, the resulting stream will be used with a nsIInputStreamPump michael@0: // instance. This means that if it is a non-blocking stream that supports michael@0: // nsIAsyncInputStream that it will be read entirely on the main application michael@0: // thread, and its AsyncWait method will be called whenever ReadSegments michael@0: // returns NS_BASE_STREAM_WOULD_BLOCK. Otherwise, if the stream is blocking, michael@0: // then it will be read on one of the background I/O threads, and it does not michael@0: // need to implement ReadSegments. If async is false, this method may return michael@0: // NS_ERROR_NOT_IMPLEMENTED to cause the basechannel to implement Open in michael@0: // terms of AsyncOpen (see NS_ImplementChannelOpen). michael@0: // A callee is allowed to return an nsIChannel instead of an nsIInputStream. michael@0: // That case will be treated as a redirect to the new channel. By default michael@0: // *channel will be set to null by the caller, so callees who don't want to michael@0: // return one an just not touch it. michael@0: virtual nsresult OpenContentStream(bool async, nsIInputStream **stream, michael@0: nsIChannel** channel) = 0; michael@0: michael@0: // The basechannel calls this method from its OnTransportStatus method to michael@0: // determine whether to call nsIProgressEventSink::OnStatus in addition to michael@0: // nsIProgressEventSink::OnProgress. This method may be overriden by the michael@0: // subclass to enable nsIProgressEventSink::OnStatus events. If this method michael@0: // returns true, then the statusArg out param specifies the "statusArg" value michael@0: // to pass to the OnStatus method. By default, OnStatus messages are michael@0: // suppressed. The status parameter passed to this method is the status value michael@0: // from the OnTransportStatus method. michael@0: virtual bool GetStatusArg(nsresult status, nsString &statusArg) { michael@0: return false; michael@0: } michael@0: michael@0: // Called when the callbacks available to this channel may have changed. michael@0: virtual void OnCallbacksChanged() { michael@0: } michael@0: michael@0: // Called when our channel is done, to allow subclasses to drop resources. michael@0: virtual void OnChannelDone() { michael@0: } michael@0: michael@0: public: michael@0: // ---------------------------------------------- michael@0: // Methods provided for use by the derived class: michael@0: michael@0: // Redirect to another channel. This method takes care of notifying michael@0: // observers of this redirect as well as of opening the new channel, if asked michael@0: // to do so. It also cancels |this| with the status code michael@0: // NS_BINDING_REDIRECTED. A failure return from this method means that the michael@0: // redirect could not be performed (no channel was opened; this channel michael@0: // wasn't canceled.) The redirectFlags parameter consists of the flag values michael@0: // defined on nsIChannelEventSink. michael@0: nsresult Redirect(nsIChannel *newChannel, uint32_t redirectFlags, michael@0: bool openNewChannel); michael@0: michael@0: // Tests whether a type hint was set. Subclasses can use this to decide michael@0: // whether to call SetContentType. michael@0: // NOTE: This is only reliable if the subclass didn't itself call michael@0: // SetContentType, and should also not be called after OpenContentStream. michael@0: bool HasContentTypeHint() const; michael@0: michael@0: // The URI member should be initialized before the channel is used, and then michael@0: // it should never be changed again until the channel is destroyed. michael@0: nsIURI *URI() { michael@0: return mURI; michael@0: } michael@0: void SetURI(nsIURI *uri) { michael@0: NS_ASSERTION(uri, "must specify a non-null URI"); michael@0: NS_ASSERTION(!mURI, "must not modify URI"); michael@0: NS_ASSERTION(!mOriginalURI, "how did that get set so early?"); michael@0: mURI = uri; michael@0: mOriginalURI = uri; michael@0: } michael@0: nsIURI *OriginalURI() { michael@0: return mOriginalURI; michael@0: } michael@0: michael@0: // The security info is a property of the transport-layer, which should be michael@0: // assigned by the subclass. michael@0: nsISupports *SecurityInfo() { michael@0: return mSecurityInfo; michael@0: } michael@0: void SetSecurityInfo(nsISupports *info) { michael@0: mSecurityInfo = info; michael@0: } michael@0: michael@0: // Test the load flags michael@0: bool HasLoadFlag(uint32_t flag) { michael@0: return (mLoadFlags & flag) != 0; michael@0: } michael@0: michael@0: // This is a short-cut to calling nsIRequest::IsPending() michael@0: virtual bool Pending() const { michael@0: return mPump || mWaitingOnAsyncRedirect; michael@0: } michael@0: michael@0: // Helper function for querying the channel's notification callbacks. michael@0: template void GetCallback(nsCOMPtr &result) { michael@0: GetInterface(NS_GET_TEMPLATE_IID(T), getter_AddRefs(result)); michael@0: } michael@0: michael@0: // Helper function for calling QueryInterface on this. michael@0: nsQueryInterface do_QueryInterface() { michael@0: return nsQueryInterface(static_cast(this)); michael@0: } michael@0: // MSVC needs this: michael@0: nsQueryInterface do_QueryInterface(nsISupports *obj) { michael@0: return nsQueryInterface(obj); michael@0: } michael@0: michael@0: // If a subclass does not want to feed transport-layer progress events to the michael@0: // base channel via nsITransportEventSink, then it may set this flag to cause michael@0: // the base channel to synthesize progress events when it receives data from michael@0: // the content stream. By default, progress events are not synthesized. michael@0: void EnableSynthesizedProgressEvents(bool enable) { michael@0: mSynthProgressEvents = enable; michael@0: } michael@0: michael@0: // Some subclasses may wish to manually insert a stream listener between this michael@0: // and the channel's listener. The following methods make that possible. michael@0: void SetStreamListener(nsIStreamListener *listener) { michael@0: mListener = listener; michael@0: } michael@0: nsIStreamListener *StreamListener() { michael@0: return mListener; michael@0: } michael@0: michael@0: // Pushes a new stream converter in front of the channel's stream listener. michael@0: // The fromType and toType values are passed to nsIStreamConverterService's michael@0: // AsyncConvertData method. If invalidatesContentLength is true, then the michael@0: // channel's content-length property will be assigned a value of -1. This is michael@0: // necessary when the converter changes the length of the resulting data michael@0: // stream, which is almost always the case for a "stream converter" ;-) michael@0: // This function optionally returns a reference to the new converter. michael@0: nsresult PushStreamConverter(const char *fromType, const char *toType, michael@0: bool invalidatesContentLength = true, michael@0: nsIStreamListener **converter = nullptr); michael@0: michael@0: private: michael@0: NS_DECL_NSISTREAMLISTENER michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: michael@0: // Called to setup mPump and call AsyncRead on it. michael@0: nsresult BeginPumpingData(); michael@0: michael@0: // Called when the callbacks available to this channel may have changed. michael@0: void CallbacksChanged() { michael@0: mProgressSink = nullptr; michael@0: mQueriedProgressSink = false; michael@0: OnCallbacksChanged(); michael@0: } michael@0: michael@0: // Called when our channel is done. This should drop no-longer-needed pointers. michael@0: void ChannelDone() { michael@0: mListener = nullptr; michael@0: mListenerContext = nullptr; michael@0: OnChannelDone(); michael@0: } michael@0: michael@0: // Handle an async redirect callback. This will only be called if we michael@0: // returned success from AsyncOpen while posting a redirect runnable. michael@0: void HandleAsyncRedirect(nsIChannel* newChannel); michael@0: void ContinueHandleAsyncRedirect(nsresult result); michael@0: nsresult ContinueRedirect(); michael@0: michael@0: // start URI classifier if requested michael@0: void ClassifyURI(); michael@0: michael@0: class RedirectRunnable : public nsRunnable michael@0: { michael@0: public: michael@0: RedirectRunnable(nsBaseChannel* chan, nsIChannel* newChannel) michael@0: : mChannel(chan), mNewChannel(newChannel) michael@0: { michael@0: NS_PRECONDITION(newChannel, "Must have channel to redirect to"); michael@0: } michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: mChannel->HandleAsyncRedirect(mNewChannel); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsRefPtr mChannel; michael@0: nsCOMPtr mNewChannel; michael@0: }; michael@0: friend class RedirectRunnable; michael@0: michael@0: nsRefPtr mPump; michael@0: nsCOMPtr mProgressSink; michael@0: nsCOMPtr mOriginalURI; michael@0: nsCOMPtr mOwner; michael@0: nsCOMPtr mSecurityInfo; michael@0: nsCOMPtr mRedirectChannel; michael@0: nsCString mContentType; michael@0: nsCString mContentCharset; michael@0: uint32_t mLoadFlags; michael@0: bool mQueriedProgressSink; michael@0: bool mSynthProgressEvents; michael@0: bool mWasOpened; michael@0: bool mWaitingOnAsyncRedirect; michael@0: bool mOpenRedirectChannel; michael@0: uint32_t mRedirectFlags; michael@0: michael@0: protected: michael@0: nsCOMPtr mURI; michael@0: nsCOMPtr mLoadGroup; michael@0: nsCOMPtr mCallbacks; michael@0: nsCOMPtr mListener; michael@0: nsCOMPtr mListenerContext; michael@0: nsresult mStatus; michael@0: uint32_t mContentDispositionHint; michael@0: nsAutoPtr mContentDispositionFilename; michael@0: int64_t mContentLength; michael@0: michael@0: friend class mozilla::net::PrivateBrowsingChannel; michael@0: }; michael@0: michael@0: #endif // !nsBaseChannel_h__