michael@0: /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */ 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 mozilla_plugins_BrowserStreamChild_h michael@0: #define mozilla_plugins_BrowserStreamChild_h 1 michael@0: michael@0: #include "mozilla/plugins/PBrowserStreamChild.h" michael@0: #include "mozilla/plugins/AStream.h" michael@0: michael@0: namespace mozilla { michael@0: namespace plugins { michael@0: michael@0: class PluginInstanceChild; michael@0: class StreamNotifyChild; michael@0: michael@0: class BrowserStreamChild : public PBrowserStreamChild, public AStream michael@0: { michael@0: public: michael@0: BrowserStreamChild(PluginInstanceChild* instance, michael@0: const nsCString& url, michael@0: const uint32_t& length, michael@0: const uint32_t& lastmodified, michael@0: StreamNotifyChild* notifyData, michael@0: const nsCString& headers, michael@0: const nsCString& mimeType, michael@0: const bool& seekable, michael@0: NPError* rv, michael@0: uint16_t* stype); michael@0: virtual ~BrowserStreamChild(); michael@0: michael@0: virtual bool IsBrowserStream() MOZ_OVERRIDE { return true; } michael@0: michael@0: NPError StreamConstructed( michael@0: const nsCString& mimeType, michael@0: const bool& seekable, michael@0: uint16_t* stype); michael@0: michael@0: virtual bool RecvWrite(const int32_t& offset, michael@0: const Buffer& data, michael@0: const uint32_t& newsize) MOZ_OVERRIDE; michael@0: virtual bool RecvNPP_StreamAsFile(const nsCString& fname) MOZ_OVERRIDE; michael@0: virtual bool RecvNPP_DestroyStream(const NPReason& reason) MOZ_OVERRIDE; michael@0: virtual bool Recv__delete__() MOZ_OVERRIDE; michael@0: michael@0: void EnsureCorrectInstance(PluginInstanceChild* i) michael@0: { michael@0: if (i != mInstance) michael@0: NS_RUNTIMEABORT("Incorrect stream instance"); michael@0: } michael@0: void EnsureCorrectStream(NPStream* s) michael@0: { michael@0: if (s != &mStream) michael@0: NS_RUNTIMEABORT("Incorrect stream data"); michael@0: } michael@0: michael@0: NPError NPN_RequestRead(NPByteRange* aRangeList); michael@0: void NPN_DestroyStream(NPReason reason); michael@0: michael@0: void NotifyPending() { michael@0: NS_ASSERTION(!mNotifyPending, "Pending twice?"); michael@0: mNotifyPending = true; michael@0: EnsureDeliveryPending(); michael@0: } michael@0: michael@0: /** michael@0: * During instance destruction, artificially cancel all outstanding streams. michael@0: * michael@0: * @return false if we are already in the DELETING state. michael@0: */ michael@0: bool InstanceDying() { michael@0: if (DELETING == mState) michael@0: return false; michael@0: michael@0: mInstanceDying = true; michael@0: return true; michael@0: } michael@0: michael@0: void FinishDelivery() { michael@0: NS_ASSERTION(mInstanceDying, "Should only be called after InstanceDying"); michael@0: NS_ASSERTION(DELETING != mState, "InstanceDying didn't work?"); michael@0: mStreamStatus = NPRES_USER_BREAK; michael@0: Deliver(); michael@0: NS_ASSERTION(!mStreamNotify, "Didn't deliver NPN_URLNotify?"); michael@0: } michael@0: michael@0: private: michael@0: friend class StreamNotifyChild; michael@0: using PBrowserStreamChild::SendNPN_DestroyStream; michael@0: michael@0: /** michael@0: * Post an event to ensure delivery of pending data/destroy/urlnotify events michael@0: * outside of the current RPC stack. michael@0: */ michael@0: void EnsureDeliveryPending(); michael@0: michael@0: /** michael@0: * Deliver data, destruction, notify scheduling michael@0: * or cancelling the suspended timer as needed. michael@0: */ michael@0: void Deliver(); michael@0: michael@0: /** michael@0: * Deliver one chunk of pending data. michael@0: * @return true if the plugin indicated a pause was necessary michael@0: */ michael@0: bool DeliverPendingData(); michael@0: michael@0: void SetSuspendedTimer(); michael@0: void ClearSuspendedTimer(); michael@0: michael@0: PluginInstanceChild* mInstance; michael@0: NPStream mStream; michael@0: michael@0: static const NPReason kStreamOpen = -1; michael@0: michael@0: /** michael@0: * The plugin's notion of whether a stream has been "closed" (no more michael@0: * data delivery) differs from the plugin host due to asynchronous delivery michael@0: * of data and NPN_DestroyStream. While the plugin-visible stream is open, michael@0: * mStreamStatus should be kStreamOpen (-1). mStreamStatus will be a michael@0: * failure code if either the parent or child indicates stream failure. michael@0: */ michael@0: NPReason mStreamStatus; michael@0: michael@0: /** michael@0: * Delivery of NPP_DestroyStream and NPP_URLNotify must be postponed until michael@0: * all data has been delivered. michael@0: */ michael@0: enum { michael@0: NOT_DESTROYED, // NPP_DestroyStream not yet received michael@0: DESTROY_PENDING, // NPP_DestroyStream received, not yet delivered michael@0: DESTROYED // NPP_DestroyStream delivered, NPP_URLNotify may still be pending michael@0: } mDestroyPending; michael@0: bool mNotifyPending; michael@0: bool mStreamAsFilePending; michael@0: nsCString mStreamAsFileName; michael@0: michael@0: // When NPP_Destroy is called for our instance (manager), this flag is set michael@0: // cancels the stream and avoids sending StreamDestroyed. michael@0: bool mInstanceDying; michael@0: michael@0: enum { michael@0: CONSTRUCTING, michael@0: ALIVE, michael@0: DYING, michael@0: DELETING michael@0: } mState; michael@0: nsCString mURL; michael@0: nsCString mHeaders; michael@0: StreamNotifyChild* mStreamNotify; michael@0: michael@0: struct PendingData michael@0: { michael@0: int32_t offset; michael@0: Buffer data; michael@0: int32_t curpos; michael@0: }; michael@0: nsTArray mPendingData; michael@0: michael@0: /** michael@0: * Asynchronous RecvWrite messages are never delivered to the plugin michael@0: * immediately, because that may be in the midst of an unexpected RPC michael@0: * stack frame. It instead posts a runnable using this tracker to cancel michael@0: * in case we are destroyed. michael@0: */ michael@0: ScopedRunnableMethodFactory mDeliveryTracker; michael@0: base::RepeatingTimer mSuspendedTimer; michael@0: }; michael@0: michael@0: } // namespace plugins michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_plugins_BrowserStreamChild_h */