Fix regexp match pair end-index == -1 assumption. (r=dmandelin, a=blocker b=605754)
[mozilla-central.git] / dom / plugins / BrowserStreamChild.h
blob9903acffd0ad1aa184c6733867960ec5e6c61337
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
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/
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.
15 * The Original Code is Mozilla Plugin App.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef mozilla_plugins_BrowserStreamChild_h
39 #define mozilla_plugins_BrowserStreamChild_h 1
41 #include "mozilla/plugins/PBrowserStreamChild.h"
42 #include "mozilla/plugins/AStream.h"
44 namespace mozilla {
45 namespace plugins {
47 class PluginInstanceChild;
48 class StreamNotifyChild;
50 class BrowserStreamChild : public PBrowserStreamChild, public AStream
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();
65 NS_OVERRIDE virtual bool IsBrowserStream() { return true; }
67 NPError StreamConstructed(
68 const nsCString& mimeType,
69 const bool& seekable,
70 uint16_t* stype);
72 virtual bool RecvWrite(const int32_t& offset,
73 const Buffer& data,
74 const uint32_t& newsize);
75 virtual bool AnswerNPP_StreamAsFile(const nsCString& fname);
76 virtual bool RecvNPP_DestroyStream(const NPReason& reason);
77 virtual bool Recv__delete__();
79 void EnsureCorrectInstance(PluginInstanceChild* i)
81 if (i != mInstance)
82 NS_RUNTIMEABORT("Incorrect stream instance");
84 void EnsureCorrectStream(NPStream* s)
86 if (s != &mStream)
87 NS_RUNTIMEABORT("Incorrect stream data");
90 NPError NPN_RequestRead(NPByteRange* aRangeList);
91 void NPN_DestroyStream(NPReason reason);
93 void NotifyPending() {
94 NS_ASSERTION(!mNotifyPending, "Pending twice?");
95 mNotifyPending = true;
96 EnsureDeliveryPending();
99 /**
100 * During instance destruction, artificially cancel all outstanding streams.
102 * @return false if we are already in the DELETING state.
104 bool InstanceDying() {
105 if (DELETING == mState)
106 return false;
108 mInstanceDying = true;
109 return true;
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?");
120 private:
121 friend class StreamNotifyChild;
122 using PBrowserStreamChild::SendNPN_DestroyStream;
125 * Post an event to ensure delivery of pending data/destroy/urlnotify events
126 * outside of the current RPC stack.
128 void EnsureDeliveryPending();
131 * Deliver data, destruction, notify scheduling
132 * or cancelling the suspended timer as needed.
134 void Deliver();
137 * Deliver one chunk of pending data.
138 * @return true if the plugin indicated a pause was necessary
140 bool DeliverPendingData();
142 void SetSuspendedTimer();
143 void ClearSuspendedTimer();
145 PluginInstanceChild* mInstance;
146 NPStream mStream;
148 static const NPReason kStreamOpen = -1;
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.
157 NPReason mStreamStatus;
160 * Delivery of NPP_DestroyStream and NPP_URLNotify must be postponed until
161 * all data has been delivered.
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;
170 // When NPP_Destroy is called for our instance (manager), this flag is set
171 // cancels the stream and avoids sending StreamDestroyed.
172 bool mInstanceDying;
174 enum {
175 CONSTRUCTING,
176 ALIVE,
177 DYING,
178 DELETING
179 } mState;
180 nsCString mURL;
181 nsCString mHeaders;
182 StreamNotifyChild* mStreamNotify;
184 struct PendingData
186 int32_t offset;
187 Buffer data;
188 int32_t curpos;
190 nsTArray<PendingData> mPendingData;
193 * Asynchronous RecvWrite messages are never delivered to the plugin
194 * immediately, because that may be in the midst of an unexpected RPC
195 * stack frame. It instead posts a runnable using this tracker to cancel
196 * in case we are destroyed.
198 ScopedRunnableMethodFactory<BrowserStreamChild> mDeliveryTracker;
199 base::RepeatingTimer<BrowserStreamChild> mSuspendedTimer;
202 } // namespace plugins
203 } // namespace mozilla
205 #endif /* mozilla_plugins_BrowserStreamChild_h */