/src/PluginCore/BrowserStream.h

https://github.com/GordonSmith/FireBreath · C Header · 276 lines · 65 code · 30 blank · 181 comment · 0 complexity · c29e77b0b8170f29fafd30218187e112 MD5 · raw file

  1. /**********************************************************\
  2. Original Author: Matthias (nitrogenycs)
  3. Created: Feb 28, 2010
  4. License: Dual license model; choose one of two:
  5. New BSD License
  6. http://www.opensource.org/licenses/bsd-license.php
  7. - or -
  8. GNU Lesser General Public License, version 2.1
  9. http://www.gnu.org/licenses/lgpl-2.1.html
  10. Copyright 2010 PacketPass, Inc and the Firebreath development team
  11. \**********************************************************/
  12. #pragma once
  13. #ifndef H_FB_BROWSERSTREAM
  14. #define H_FB_BROWSERSTREAM
  15. #include <string>
  16. #include "APITypes.h"
  17. #include "PluginEventSource.h"
  18. #include "PluginEventSink.h"
  19. #include "PluginEvents/StreamEvents.h"
  20. namespace FB {
  21. FB_FORWARD_PTR(BrowserStream);
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////
  23. /// @class BrowserStream
  24. ///
  25. /// @brief This is the abstract base class (interface class) for a browser stream.
  26. ///
  27. /// A BrowserStream is used to download and upload data via the browser host.
  28. /// The different hosts (NPAPI, ActiveX) derive from this class and provide an implementation.
  29. /// Instances are created by the factory method BrowserHost::createStream( ... parameters ... ).
  30. /// BrowserStreams send various events when the stream is created, destroyed or data in response to
  31. /// a read request arrived.
  32. ///
  33. /// @author Matthias
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////
  35. class BrowserStream : public PluginEventSource
  36. {
  37. public:
  38. ////////////////////////////////////////////////////////////////////////////////////////////////////
  39. /// @class Range
  40. ///
  41. /// @brief Specifies the range for a read range request (start to end) in bytes.
  42. ///
  43. /// @author Matthias
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////
  45. struct Range
  46. {
  47. Range( size_t start, size_t end );
  48. size_t start, end;
  49. };
  50. public:
  51. ////////////////////////////////////////////////////////////////////////////////////////////////////
  52. /// @fn BrowserStream::BrowserStream( const std::string& url, bool cache, bool seekable, size_t internalBufferSize)
  53. ///
  54. /// @brief Default constructor. Dont't use directly, use BrowserHost::createStream instead.
  55. ///
  56. /// @author Matthias
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////
  58. BrowserStream( const std::string& url, bool cache, bool requestSeekable, size_t internalBufferSize);
  59. ////////////////////////////////////////////////////////////////////////////////////////////////////
  60. /// @fn virtual BrowserStream::~BrowserStream()
  61. ///
  62. /// @brief Destructor.
  63. ///
  64. /// @author Matthias
  65. ////////////////////////////////////////////////////////////////////////////////////////////////////
  66. virtual ~BrowserStream();
  67. virtual BrowserStreamPtr shared_from_this() { return FB::ptr_cast<BrowserStream>(PluginEventSource::shared_from_this()); }
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////
  69. /// @fn virtual bool BrowserStream::readRange( size_t start, size_t end )
  70. ///
  71. /// @brief Performs a read range request, returns the bytes in the range [start, end).
  72. ///
  73. /// Only works if stream is seekable. Asynchronous, returns immediately. Listen for
  74. /// StreamFailedOpenEvent, StreamOpenedEvent, StreamDataArrivedEvent to get any results.
  75. ///
  76. /// @author Matthias
  77. ///
  78. /// @return false on error, else true
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. virtual bool readRange( size_t start, size_t end );
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////
  82. /// @fn virtual bool BrowserStream::readRanges( const std::vector<Range>& ranges ) = 0;
  83. ///
  84. /// @brief Same functionality as BrowserStream::readRange( size_t start, size_t end ), except
  85. /// multiple ranges can be requested at once.
  86. ///
  87. /// @author Matthias
  88. ///
  89. /// @return false on error, else true
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////
  91. virtual bool readRanges( const std::vector<Range>& ranges ) = 0;
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////
  93. /// @fn virtual bool BrowserStream::write(const char* data, size_t dataLength, size_t& written) = 0;
  94. ///
  95. /// @brief Writes data to the stream.
  96. ///
  97. /// @param[in] data pointer to raw data to send
  98. /// @param[in] dataLength number of bytes provided by data
  99. /// @param[out] written the number of bytes written
  100. ///
  101. /// @author Matthias
  102. ///
  103. /// @return false on error, else true
  104. ////////////////////////////////////////////////////////////////////////////////////////////////////
  105. virtual bool write(const char* data, size_t dataLength, size_t& written) = 0;
  106. ////////////////////////////////////////////////////////////////////////////////////////////////////
  107. /// @fn virtual bool BrowserStream::close() = 0;
  108. ///
  109. /// @brief Closes and destroys the stream.
  110. ///
  111. /// @author Matthias
  112. ///
  113. /// @return false on error, else true
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////
  115. virtual bool close() = 0;
  116. public:
  117. // property getters
  118. ////////////////////////////////////////////////////////////////////////////////////////////////////
  119. /// @fn virtual std::string BrowserStream::getUrl() const;
  120. ///
  121. /// @brief Returns the url associated with this stream. This is the url supplied to
  122. // BrowserHost::createStream.
  123. ///
  124. /// @author Matthias
  125. ////////////////////////////////////////////////////////////////////////////////////////////////////
  126. virtual std::string getUrl() const;
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////
  128. /// @fn virtual bool BrowserStream::isSeekableByServer() const;
  129. ///
  130. /// @brief Returns true if the stream is actually seekable (server says so).
  131. ///
  132. /// @author Matthias
  133. ////////////////////////////////////////////////////////////////////////////////////////////////////
  134. virtual bool isSeekableByServer() const;
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////
  136. /// @fn virtual bool BrowserStream::isSeekableRequested() const;
  137. ///
  138. /// @brief Returns true if a stream was requested to be seekable. Not all servers support this.
  139. ///
  140. /// @author Matthias
  141. ////////////////////////////////////////////////////////////////////////////////////////////////////
  142. virtual bool isSeekableRequested() const;
  143. ////////////////////////////////////////////////////////////////////////////////////////////////////
  144. /// @fn virtual bool BrowserStream::isSeekable() const;
  145. ///
  146. /// @brief Returns true if a stream was requested to be seekable and the server supports it.
  147. ///
  148. /// @author Matthias
  149. ////////////////////////////////////////////////////////////////////////////////////////////////////
  150. virtual bool isSeekable() const;
  151. ////////////////////////////////////////////////////////////////////////////////////////////////////
  152. /// @fn virtual bool BrowserStream::isCached() const;
  153. ///
  154. /// @brief Returns true if the content of this stream should be cached (i.e. a physical file must
  155. /// be created).
  156. ///
  157. /// @author Matthias
  158. ////////////////////////////////////////////////////////////////////////////////////////////////////
  159. virtual bool isCached() const;
  160. ////////////////////////////////////////////////////////////////////////////////////////////////////
  161. /// @fn virtual bool BrowserStream::isCompleted() const;
  162. ///
  163. /// @brief Returns true if download of this stream is completed (e.g. due to error or download finished).
  164. ///
  165. /// @author Matthias
  166. ////////////////////////////////////////////////////////////////////////////////////////////////////
  167. virtual bool isCompleted() const;
  168. ////////////////////////////////////////////////////////////////////////////////////////////////////
  169. /// @fn virtual bool BrowserStream::isOpen() const;
  170. ///
  171. /// @brief Returns true if the stream is open.
  172. ///
  173. /// @author Matthias
  174. ////////////////////////////////////////////////////////////////////////////////////////////////////
  175. virtual bool isOpen() const;
  176. ////////////////////////////////////////////////////////////////////////////////////////////////////
  177. /// @fn virtual std::string BrowserStream::getMimeType() const;
  178. ///
  179. /// @brief Returns the mime type of this stream, e.g. "text/plain".
  180. ///
  181. /// @author Matthias
  182. ////////////////////////////////////////////////////////////////////////////////////////////////////
  183. virtual std::string getMimeType() const;
  184. ////////////////////////////////////////////////////////////////////////////////////////////////////
  185. /// @fn virtual std::wstring BrowserStream::getCacheFilename() const;
  186. ///
  187. /// @brief If this is a cached stream, returns the physical filename of the file in the browser's
  188. /// cache.
  189. ///
  190. /// @author Matthias
  191. ////////////////////////////////////////////////////////////////////////////////////////////////////
  192. virtual std::wstring getCacheFilename() const;
  193. ////////////////////////////////////////////////////////////////////////////////////////////////////
  194. /// @fn virtual std::string BrowserStream::getHeaders() const;
  195. ///
  196. /// @brief Returns the http headers.
  197. ///
  198. /// @author Matthias
  199. ////////////////////////////////////////////////////////////////////////////////////////////////////
  200. virtual std::string getHeaders() const;
  201. ////////////////////////////////////////////////////////////////////////////////////////////////////
  202. /// @fn virtual size_t BrowserStream::getInternalBufferSize() const;
  203. ///
  204. /// @brief Returns the size of the internal buffer used by this stream.
  205. ///
  206. /// @author Matthias
  207. ////////////////////////////////////////////////////////////////////////////////////////////////////
  208. virtual size_t getInternalBufferSize() const;
  209. ////////////////////////////////////////////////////////////////////////////////////////////////////
  210. /// @fn virtual size_t BrowserStream::getLength() const;
  211. ///
  212. /// @brief Returns the length of the stream in bytes (if server gives info like content-length).
  213. ///
  214. /// @author Matthias
  215. ////////////////////////////////////////////////////////////////////////////////////////////////////
  216. virtual size_t getLength() const;
  217. protected:
  218. // property setters
  219. virtual void setUrl(const std::string& url);
  220. virtual void setSeekableByServer(bool seekable);
  221. virtual void setCached(bool cached);
  222. virtual void setCompleted(bool completed);
  223. virtual void setOpen(bool open);
  224. virtual void setMimeType(const std::string& mimeType);
  225. virtual void setCacheFilename(const std::wstring& cacheFilename);
  226. virtual void setHeaders(const std::string& headers);
  227. virtual void setInternalBufferSize(size_t internalBufferSize);
  228. virtual void setLength(size_t length);
  229. private:
  230. // properties
  231. std::string url;
  232. bool seekableRequested;
  233. bool seekableByServer;
  234. bool cached;
  235. size_t internalBufferSize;
  236. std::wstring cacheFilename;
  237. size_t length;
  238. std::string mimeType;
  239. bool completed;
  240. bool opened;
  241. std::string headers;
  242. };
  243. };
  244. #endif