Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_plugins_BrowserStreamChild_h |
michael@0 | 7 | #define mozilla_plugins_BrowserStreamChild_h 1 |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/plugins/PBrowserStreamChild.h" |
michael@0 | 10 | #include "mozilla/plugins/AStream.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | namespace plugins { |
michael@0 | 14 | |
michael@0 | 15 | class PluginInstanceChild; |
michael@0 | 16 | class StreamNotifyChild; |
michael@0 | 17 | |
michael@0 | 18 | class BrowserStreamChild : public PBrowserStreamChild, public AStream |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | BrowserStreamChild(PluginInstanceChild* instance, |
michael@0 | 22 | const nsCString& url, |
michael@0 | 23 | const uint32_t& length, |
michael@0 | 24 | const uint32_t& lastmodified, |
michael@0 | 25 | StreamNotifyChild* notifyData, |
michael@0 | 26 | const nsCString& headers, |
michael@0 | 27 | const nsCString& mimeType, |
michael@0 | 28 | const bool& seekable, |
michael@0 | 29 | NPError* rv, |
michael@0 | 30 | uint16_t* stype); |
michael@0 | 31 | virtual ~BrowserStreamChild(); |
michael@0 | 32 | |
michael@0 | 33 | virtual bool IsBrowserStream() MOZ_OVERRIDE { return true; } |
michael@0 | 34 | |
michael@0 | 35 | NPError StreamConstructed( |
michael@0 | 36 | const nsCString& mimeType, |
michael@0 | 37 | const bool& seekable, |
michael@0 | 38 | uint16_t* stype); |
michael@0 | 39 | |
michael@0 | 40 | virtual bool RecvWrite(const int32_t& offset, |
michael@0 | 41 | const Buffer& data, |
michael@0 | 42 | const uint32_t& newsize) MOZ_OVERRIDE; |
michael@0 | 43 | virtual bool RecvNPP_StreamAsFile(const nsCString& fname) MOZ_OVERRIDE; |
michael@0 | 44 | virtual bool RecvNPP_DestroyStream(const NPReason& reason) MOZ_OVERRIDE; |
michael@0 | 45 | virtual bool Recv__delete__() MOZ_OVERRIDE; |
michael@0 | 46 | |
michael@0 | 47 | void EnsureCorrectInstance(PluginInstanceChild* i) |
michael@0 | 48 | { |
michael@0 | 49 | if (i != mInstance) |
michael@0 | 50 | NS_RUNTIMEABORT("Incorrect stream instance"); |
michael@0 | 51 | } |
michael@0 | 52 | void EnsureCorrectStream(NPStream* s) |
michael@0 | 53 | { |
michael@0 | 54 | if (s != &mStream) |
michael@0 | 55 | NS_RUNTIMEABORT("Incorrect stream data"); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | NPError NPN_RequestRead(NPByteRange* aRangeList); |
michael@0 | 59 | void NPN_DestroyStream(NPReason reason); |
michael@0 | 60 | |
michael@0 | 61 | void NotifyPending() { |
michael@0 | 62 | NS_ASSERTION(!mNotifyPending, "Pending twice?"); |
michael@0 | 63 | mNotifyPending = true; |
michael@0 | 64 | EnsureDeliveryPending(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * During instance destruction, artificially cancel all outstanding streams. |
michael@0 | 69 | * |
michael@0 | 70 | * @return false if we are already in the DELETING state. |
michael@0 | 71 | */ |
michael@0 | 72 | bool InstanceDying() { |
michael@0 | 73 | if (DELETING == mState) |
michael@0 | 74 | return false; |
michael@0 | 75 | |
michael@0 | 76 | mInstanceDying = true; |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | void FinishDelivery() { |
michael@0 | 81 | NS_ASSERTION(mInstanceDying, "Should only be called after InstanceDying"); |
michael@0 | 82 | NS_ASSERTION(DELETING != mState, "InstanceDying didn't work?"); |
michael@0 | 83 | mStreamStatus = NPRES_USER_BREAK; |
michael@0 | 84 | Deliver(); |
michael@0 | 85 | NS_ASSERTION(!mStreamNotify, "Didn't deliver NPN_URLNotify?"); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | private: |
michael@0 | 89 | friend class StreamNotifyChild; |
michael@0 | 90 | using PBrowserStreamChild::SendNPN_DestroyStream; |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Post an event to ensure delivery of pending data/destroy/urlnotify events |
michael@0 | 94 | * outside of the current RPC stack. |
michael@0 | 95 | */ |
michael@0 | 96 | void EnsureDeliveryPending(); |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * Deliver data, destruction, notify scheduling |
michael@0 | 100 | * or cancelling the suspended timer as needed. |
michael@0 | 101 | */ |
michael@0 | 102 | void Deliver(); |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Deliver one chunk of pending data. |
michael@0 | 106 | * @return true if the plugin indicated a pause was necessary |
michael@0 | 107 | */ |
michael@0 | 108 | bool DeliverPendingData(); |
michael@0 | 109 | |
michael@0 | 110 | void SetSuspendedTimer(); |
michael@0 | 111 | void ClearSuspendedTimer(); |
michael@0 | 112 | |
michael@0 | 113 | PluginInstanceChild* mInstance; |
michael@0 | 114 | NPStream mStream; |
michael@0 | 115 | |
michael@0 | 116 | static const NPReason kStreamOpen = -1; |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * The plugin's notion of whether a stream has been "closed" (no more |
michael@0 | 120 | * data delivery) differs from the plugin host due to asynchronous delivery |
michael@0 | 121 | * of data and NPN_DestroyStream. While the plugin-visible stream is open, |
michael@0 | 122 | * mStreamStatus should be kStreamOpen (-1). mStreamStatus will be a |
michael@0 | 123 | * failure code if either the parent or child indicates stream failure. |
michael@0 | 124 | */ |
michael@0 | 125 | NPReason mStreamStatus; |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Delivery of NPP_DestroyStream and NPP_URLNotify must be postponed until |
michael@0 | 129 | * all data has been delivered. |
michael@0 | 130 | */ |
michael@0 | 131 | enum { |
michael@0 | 132 | NOT_DESTROYED, // NPP_DestroyStream not yet received |
michael@0 | 133 | DESTROY_PENDING, // NPP_DestroyStream received, not yet delivered |
michael@0 | 134 | DESTROYED // NPP_DestroyStream delivered, NPP_URLNotify may still be pending |
michael@0 | 135 | } mDestroyPending; |
michael@0 | 136 | bool mNotifyPending; |
michael@0 | 137 | bool mStreamAsFilePending; |
michael@0 | 138 | nsCString mStreamAsFileName; |
michael@0 | 139 | |
michael@0 | 140 | // When NPP_Destroy is called for our instance (manager), this flag is set |
michael@0 | 141 | // cancels the stream and avoids sending StreamDestroyed. |
michael@0 | 142 | bool mInstanceDying; |
michael@0 | 143 | |
michael@0 | 144 | enum { |
michael@0 | 145 | CONSTRUCTING, |
michael@0 | 146 | ALIVE, |
michael@0 | 147 | DYING, |
michael@0 | 148 | DELETING |
michael@0 | 149 | } mState; |
michael@0 | 150 | nsCString mURL; |
michael@0 | 151 | nsCString mHeaders; |
michael@0 | 152 | StreamNotifyChild* mStreamNotify; |
michael@0 | 153 | |
michael@0 | 154 | struct PendingData |
michael@0 | 155 | { |
michael@0 | 156 | int32_t offset; |
michael@0 | 157 | Buffer data; |
michael@0 | 158 | int32_t curpos; |
michael@0 | 159 | }; |
michael@0 | 160 | nsTArray<PendingData> mPendingData; |
michael@0 | 161 | |
michael@0 | 162 | /** |
michael@0 | 163 | * Asynchronous RecvWrite messages are never delivered to the plugin |
michael@0 | 164 | * immediately, because that may be in the midst of an unexpected RPC |
michael@0 | 165 | * stack frame. It instead posts a runnable using this tracker to cancel |
michael@0 | 166 | * in case we are destroyed. |
michael@0 | 167 | */ |
michael@0 | 168 | ScopedRunnableMethodFactory<BrowserStreamChild> mDeliveryTracker; |
michael@0 | 169 | base::RepeatingTimer<BrowserStreamChild> mSuspendedTimer; |
michael@0 | 170 | }; |
michael@0 | 171 | |
michael@0 | 172 | } // namespace plugins |
michael@0 | 173 | } // namespace mozilla |
michael@0 | 174 | |
michael@0 | 175 | #endif /* mozilla_plugins_BrowserStreamChild_h */ |