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 nsInputStreamPump_h__ michael@0: #define nsInputStreamPump_h__ michael@0: michael@0: #include "nsIInputStreamPump.h" michael@0: #include "nsIAsyncInputStream.h" michael@0: #include "nsIThreadRetargetableRequest.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/ReentrantMonitor.h" michael@0: michael@0: class nsIInputStream; michael@0: class nsILoadGroup; michael@0: class nsIStreamListener; michael@0: michael@0: class nsInputStreamPump MOZ_FINAL : public nsIInputStreamPump michael@0: , public nsIInputStreamCallback michael@0: , public nsIThreadRetargetableRequest michael@0: { michael@0: public: michael@0: typedef mozilla::ReentrantMonitorAutoEnter ReentrantMonitorAutoEnter; michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIREQUEST michael@0: NS_DECL_NSIINPUTSTREAMPUMP michael@0: NS_DECL_NSIINPUTSTREAMCALLBACK michael@0: NS_DECL_NSITHREADRETARGETABLEREQUEST michael@0: michael@0: nsInputStreamPump(); michael@0: ~nsInputStreamPump(); michael@0: michael@0: static NS_HIDDEN_(nsresult) michael@0: Create(nsInputStreamPump **result, michael@0: nsIInputStream *stream, michael@0: int64_t streamPos = -1, michael@0: int64_t streamLen = -1, michael@0: uint32_t segsize = 0, michael@0: uint32_t segcount = 0, michael@0: bool closeWhenDone = false); michael@0: michael@0: typedef void (*PeekSegmentFun)(void *closure, const uint8_t *buf, michael@0: uint32_t bufLen); michael@0: /** michael@0: * Peek into the first chunk of data that's in the stream. Note that this michael@0: * method will not call the callback when there is no data in the stream. michael@0: * The callback will be called at most once. michael@0: * michael@0: * The data from the stream will not be consumed, i.e. the pump's listener michael@0: * can still read all the data. michael@0: * michael@0: * Do not call before asyncRead. Do not call after onStopRequest. michael@0: */ michael@0: NS_HIDDEN_(nsresult) PeekStream(PeekSegmentFun callback, void *closure); michael@0: michael@0: /** michael@0: * Dispatched (to the main thread) by OnStateStop if it's called off main michael@0: * thread. Updates mState based on return value of OnStateStop. michael@0: */ michael@0: nsresult CallOnStateStop(); michael@0: michael@0: protected: michael@0: michael@0: enum { michael@0: STATE_IDLE, michael@0: STATE_START, michael@0: STATE_TRANSFER, michael@0: STATE_STOP michael@0: }; michael@0: michael@0: nsresult EnsureWaiting(); michael@0: uint32_t OnStateStart(); michael@0: uint32_t OnStateTransfer(); michael@0: uint32_t OnStateStop(); michael@0: michael@0: uint32_t mState; michael@0: nsCOMPtr mLoadGroup; michael@0: nsCOMPtr mListener; michael@0: nsCOMPtr mListenerContext; michael@0: nsCOMPtr mTargetThread; michael@0: nsCOMPtr mStream; michael@0: nsCOMPtr mAsyncStream; michael@0: uint64_t mStreamOffset; michael@0: uint64_t mStreamLength; michael@0: uint32_t mSegSize; michael@0: uint32_t mSegCount; michael@0: nsresult mStatus; michael@0: uint32_t mSuspendCount; michael@0: uint32_t mLoadFlags; michael@0: bool mIsPending; michael@0: // True while in OnInputStreamReady, calling OnStateStart, OnStateTransfer michael@0: // and OnStateStop. Used to prevent calls to AsyncWait during callbacks. michael@0: bool mProcessingCallbacks; michael@0: // True if waiting on the "input stream ready" callback. michael@0: bool mWaitingForInputStreamReady; michael@0: bool mCloseWhenDone; michael@0: bool mRetargeting; michael@0: // Protects state/member var accesses across multiple threads. michael@0: mozilla::ReentrantMonitor mMonitor; michael@0: }; michael@0: michael@0: #endif // !nsInputStreamChannel_h__