FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
NpapiStream.cpp
1 /**********************************************************\
2 Original Author: Matthias (nitrogenycs)
3 
4 Created: Feb 28, 2010
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2010 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #include "NpapiPlugin.h"
16 #include "NpapiStream.h"
17 #include "precompiled_headers.h" // On windows, everything above this line in PCH
18 #undef min
19 
20 using namespace FB::Npapi;
21 
22 NpapiStream::NpapiStream( const std::string& url, bool cache, bool seekable, size_t internalBufferSize, const NpapiBrowserHostConstPtr& Host )
23  : FB::BrowserStream( url, cache, seekable, internalBufferSize )
24  /*, internalBuffer( internalBufferSize ) */
25  , stream(0), host(Host), m_streamDestroyed(false), m_streamNotified(false)
26 {
27 }
28 
29 NpapiStream::~NpapiStream()
30 {
31  close();
32 }
33 
34 bool NpapiStream::readRanges( const std::vector<Range>& ranges )
35 {
36  if ( !getStream() || !isSeekable() || !isOpen() ) return false;
37  if ( !ranges.size() ) return true;
38 
39  std::vector<NPByteRange> vecranges( ranges.size() );
40  for ( size_t i = 0; i < ranges.size(); i++ )
41  {
42  NPByteRange& range = vecranges[i];
43  range.offset = ranges[0].start;
44  range.length = ranges[0].end - ranges[0].start;
45  range.next = ( ( i + 1 ) < ranges.size() ) ? &vecranges[i+1] : 0;
46  }
47 
48  return getHost()->RequestRead( getStream(), &vecranges[0] ) == NPERR_NO_ERROR;
49 }
50 
51 bool NpapiStream::write(const char* data, size_t dataLength, size_t& written)
52 {
53  if ( !getStream() || !isOpen() ) return false;
54  written = getHost()->Write( getStream(), dataLength, const_cast<char*>(data) );
55  return written == dataLength;
56 }
57 
58 bool NpapiStream::close()
59 {
60  if ( !getStream() ) return false;
61  if ( isOpen() )
62  {
63  StreamCompletedEvent ev(this, true);
64  SendEvent( &ev );
65  }
66  setOpen( false );
67  bool result = getHost()->DestroyStream( getStream(), NPRES_USER_BREAK ) == NPERR_NO_ERROR;
68  setStream( 0 );
69  return result;
70 }
71 
72 int32_t NpapiStream::signalDataArrived(void* buffer, int32_t len, int32_t offset)
73 {
74  size_t effectiveLen = std::min( getInternalBufferSize(), static_cast<size_t>(len) );
75  if ( effectiveLen )
76  {
77  //memcpy( &internalBuffer[0], buffer, effectiveLen );
78 
79  float progress = 0;
80  if ( getLength() )
81  {
82  progress = float( offset + len ) / float( getLength() ) * 100.f;
83  }
84  if ( isOpen() )
85  {
86  StreamDataArrivedEvent ev(this, buffer, effectiveLen, offset, progress);
87  SendEvent( &ev );
88  }
89  }
90  return effectiveLen;
91 }
92 
93 void NpapiStream::signalOpened()
94 {
95  setOpen( true );
96  StreamOpenedEvent ev(this);
97  SendEvent( &ev );
98 }
99 
100 void NpapiStream::signalFailedOpen()
101 {
102  StreamFailedOpenEvent ev(this);
103  SendEvent( &ev );
104 }
105 
106 void NpapiStream::signalCompleted(bool success)
107 {
108  if ( isSeekable() && success ) return; // If seekable, then complete is immediately sent after the open call worked.
109  // But we don't want to signal completenes right then.
110 
111  if ( isCompleted() ) return; // If already completed, don't send event again
112 
113  setCompleted( true );
114 
115  /*if ( !isOpen() && !success )
116  {
117  signalFailedOpen();
118  }*/
119 
120  close();
121 
122  StreamCompletedEvent ev(this, success);
123  SendEvent( &ev );
124 }
125 
126 void NpapiStream::signalCacheFilename(const std::wstring& CacheFilename)
127 {
128  setCacheFilename( CacheFilename );
129 }
130 
131 void NpapiStream::setStream(NPStream* Stream)
132 {
133  stream = Stream;
134 }
135 
136 NPStream* NpapiStream::getStream() const
137 {
138  return stream;
139 }
140 
141 NpapiBrowserHostConstPtr NpapiStream::getHost() const
142 {
143  return host.lock();
144 }
145 
146 void FB::Npapi::NpapiStream::setCreated()
147 {
148  // We hold the reference here until NPP_DestroyStream is called so that the
149  // object can't be deleted out from under the browser
150  m_selfReference = FB::ptr_cast<NpapiStream>(shared_from_this());
151 }
152 
153 void FB::Npapi::NpapiStream::setDestroyed()
154 {
155  m_streamDestroyed = true;
156  if (m_streamNotified)
157  m_selfReference.reset();
158 }
159 
160 void FB::Npapi::NpapiStream::setNotified()
161 {
162  m_streamNotified = true;
163  if (m_streamDestroyed)
164  m_selfReference.reset();
165 }
166 
boost::shared_ptr< T > ptr_cast(boost::shared_ptr< U > const &r)
Convenience function for doing a dynamic cast of one boost::shared_ptr to another.
Definition: APITypes.h:339