1 : /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Plugin App.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Benjamin Smedberg <benjamin@smedbergs.us>
19 : * Portions created by the Initial Developer are Copyright (C) 2009
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef mozilla_plugins_BrowserStreamChild_h
39 : #define mozilla_plugins_BrowserStreamChild_h 1
40 :
41 : #include "mozilla/plugins/PBrowserStreamChild.h"
42 : #include "mozilla/plugins/AStream.h"
43 :
44 : namespace mozilla {
45 : namespace plugins {
46 :
47 : class PluginInstanceChild;
48 : class StreamNotifyChild;
49 :
50 : class BrowserStreamChild : public PBrowserStreamChild, public AStream
51 : {
52 : public:
53 : BrowserStreamChild(PluginInstanceChild* instance,
54 : const nsCString& url,
55 : const uint32_t& length,
56 : const uint32_t& lastmodified,
57 : StreamNotifyChild* notifyData,
58 : const nsCString& headers,
59 : const nsCString& mimeType,
60 : const bool& seekable,
61 : NPError* rv,
62 : uint16_t* stype);
63 : virtual ~BrowserStreamChild();
64 :
65 : NS_OVERRIDE virtual bool IsBrowserStream() { return true; }
66 :
67 : NPError StreamConstructed(
68 : const nsCString& mimeType,
69 : const bool& seekable,
70 : uint16_t* stype);
71 :
72 : virtual bool RecvWrite(const int32_t& offset,
73 : const Buffer& data,
74 : const uint32_t& newsize);
75 : virtual bool RecvNPP_StreamAsFile(const nsCString& fname);
76 : virtual bool RecvNPP_DestroyStream(const NPReason& reason);
77 : virtual bool Recv__delete__();
78 :
79 0 : void EnsureCorrectInstance(PluginInstanceChild* i)
80 : {
81 0 : if (i != mInstance)
82 0 : NS_RUNTIMEABORT("Incorrect stream instance");
83 0 : }
84 0 : void EnsureCorrectStream(NPStream* s)
85 : {
86 0 : if (s != &mStream)
87 0 : NS_RUNTIMEABORT("Incorrect stream data");
88 0 : }
89 :
90 : NPError NPN_RequestRead(NPByteRange* aRangeList);
91 : void NPN_DestroyStream(NPReason reason);
92 :
93 : void NotifyPending() {
94 : NS_ASSERTION(!mNotifyPending, "Pending twice?");
95 : mNotifyPending = true;
96 : EnsureDeliveryPending();
97 : }
98 :
99 : /**
100 : * During instance destruction, artificially cancel all outstanding streams.
101 : *
102 : * @return false if we are already in the DELETING state.
103 : */
104 : bool InstanceDying() {
105 : if (DELETING == mState)
106 : return false;
107 :
108 : mInstanceDying = true;
109 : return true;
110 : }
111 :
112 : void FinishDelivery() {
113 : NS_ASSERTION(mInstanceDying, "Should only be called after InstanceDying");
114 : NS_ASSERTION(DELETING != mState, "InstanceDying didn't work?");
115 : mStreamStatus = NPRES_USER_BREAK;
116 : Deliver();
117 : NS_ASSERTION(!mStreamNotify, "Didn't deliver NPN_URLNotify?");
118 : }
119 :
120 : private:
121 : friend class StreamNotifyChild;
122 : using PBrowserStreamChild::SendNPN_DestroyStream;
123 :
124 : /**
125 : * Post an event to ensure delivery of pending data/destroy/urlnotify events
126 : * outside of the current RPC stack.
127 : */
128 : void EnsureDeliveryPending();
129 :
130 : /**
131 : * Deliver data, destruction, notify scheduling
132 : * or cancelling the suspended timer as needed.
133 : */
134 : void Deliver();
135 :
136 : /**
137 : * Deliver one chunk of pending data.
138 : * @return true if the plugin indicated a pause was necessary
139 : */
140 : bool DeliverPendingData();
141 :
142 : void SetSuspendedTimer();
143 : void ClearSuspendedTimer();
144 :
145 : PluginInstanceChild* mInstance;
146 : NPStream mStream;
147 :
148 : static const NPReason kStreamOpen = -1;
149 :
150 : /**
151 : * The plugin's notion of whether a stream has been "closed" (no more
152 : * data delivery) differs from the plugin host due to asynchronous delivery
153 : * of data and NPN_DestroyStream. While the plugin-visible stream is open,
154 : * mStreamStatus should be kStreamOpen (-1). mStreamStatus will be a
155 : * failure code if either the parent or child indicates stream failure.
156 : */
157 : NPReason mStreamStatus;
158 :
159 : /**
160 : * Delivery of NPP_DestroyStream and NPP_URLNotify must be postponed until
161 : * all data has been delivered.
162 : */
163 : enum {
164 : NOT_DESTROYED, // NPP_DestroyStream not yet received
165 : DESTROY_PENDING, // NPP_DestroyStream received, not yet delivered
166 : DESTROYED // NPP_DestroyStream delivered, NPP_URLNotify may still be pending
167 : } mDestroyPending;
168 : bool mNotifyPending;
169 : bool mStreamAsFilePending;
170 : nsCString mStreamAsFileName;
171 :
172 : // When NPP_Destroy is called for our instance (manager), this flag is set
173 : // cancels the stream and avoids sending StreamDestroyed.
174 : bool mInstanceDying;
175 :
176 : enum {
177 : CONSTRUCTING,
178 : ALIVE,
179 : DYING,
180 : DELETING
181 : } mState;
182 : nsCString mURL;
183 : nsCString mHeaders;
184 : StreamNotifyChild* mStreamNotify;
185 :
186 : struct PendingData
187 : {
188 : int32_t offset;
189 : Buffer data;
190 : int32_t curpos;
191 : };
192 : nsTArray<PendingData> mPendingData;
193 :
194 : /**
195 : * Asynchronous RecvWrite messages are never delivered to the plugin
196 : * immediately, because that may be in the midst of an unexpected RPC
197 : * stack frame. It instead posts a runnable using this tracker to cancel
198 : * in case we are destroyed.
199 : */
200 : ScopedRunnableMethodFactory<BrowserStreamChild> mDeliveryTracker;
201 : base::RepeatingTimer<BrowserStreamChild> mSuspendedTimer;
202 : };
203 :
204 : } // namespace plugins
205 : } // namespace mozilla
206 :
207 : #endif /* mozilla_plugins_BrowserStreamChild_h */
|