/* The browser calls the NPP_Write function to deliver the data specified in a previous NPP_WriteReady call to the plug-in. A plug-in must consume at least as many bytes as indicated in the NPP_WriteReady call. After a stream is created by a call to NPP_NewStream, the browser calls NPP_Write either: * If the plug-in requested a normal-mode stream, the data in the stream is delivered to the plug-in instance in a series of calls to NPP_WriteReady and NPP_Write. * If the plug-in requested a seekable stream, the NPN_RequestRead function requests reads of a specified byte range that results in a series of calls to NPP_WriteReady and NPP_Write. The plug-in can use the offset parameter to track the bytes that are written. This gives you different information depending in the type of stream. In a normal-mode stream., the parameter value increases as the each buffer is written. The buf parameter is not persistent, so the plug-in must process data immediately or allocate memory and save a copy of it. In a seekable stream with byte range requests, you can use this parameter to track NPN_RequestRead requests. */ int32_t NpapiPlugin::Write(NPStream* stream, int32_t offset, int32_t len, void* buffer) { NpapiStream* s = static_cast<NpapiStream*>( stream->pdata ); // check for streams we did not request or create if ( !s ) return -1; return s->signalDataArrived( buffer, len, offset ); }
/* The browser calls NPP_WriteReady before each call to NPP_Write to determine whether a plug-in can receive data and how many bytes it can receive. This function allows the browser to send only as much data to the instance as it can handle at one time, making resource use more efficient for both the browser and plug-in. The NPP_Write function may pass a larger buffer, but the plug-in is required to consume only the amount of data returned by NPP_WriteReady. The browser can write a smaller amount of data if desired or necessary; for example, if only 8K of data is available in a network buffer. If the plug-in is allocating memory for the entire stream at once (an AS_FILE stream), it can return a very large number. Because it is not processing streaming data, the browser can pass as much data to the instance as necessary in a single NPP_Write. If the plug-in receives a value of zero, the data flow temporarily stops. The browser checks to see if the plug-in can receive data again by resending the data at regular intervals. */ int32_t NpapiPlugin::WriteReady(NPStream* stream) { NpapiStream* s = static_cast<NpapiStream*>( stream->pdata ); // check for streams we did not request or create if ( !s ) return -1; return s->getInternalBufferSize(); }
/* The browser calls NPP_URLNotify after the completion of a NPN_GetURLNotify or NPN_PostURLNotify request to inform the plug-in that the request was completed and supply a reason code for the completion. The most common reason code is NPRES_DONE, indicating simply that the request completed normally. Other possible reason codes are NPRES_USER_BREAK, indicating that the request was halted due to a user action (for example, clicking the Stop button), and NPRES_NETWORK_ERR, indicating that the request could not be completed, perhaps because the URL could not be found. The parameter notifyData is the plug-in-private value passed as an argument by a previous NPN_GetURLNotify or NPN_PostURLNotify call, and can be used as an identifier for the request. */ void NpapiPlugin::URLNotify(const char* url, NPReason reason, void* notifyData) { NpapiStream* s = static_cast<NpapiStream*>( notifyData ); // check for streams we did not request or create if ( !s ) return; s->signalCompleted( reason == NPRES_DONE ); s->setNotified(); }
/* When the stream is complete, the browser calls NPP_StreamAsFile to provide the instance with a full path name for a local file for the stream. NPP_StreamAsFile is called for streams whose mode is set to NP_ASFILEONLY or NP_ASFILE only in a previous call to NPP_NewStream. If an error occurs while retrieving the data or writing the file, the file name (fname) is null. */ void NpapiPlugin::StreamAsFile(NPStream* stream, const char* fname) { NpapiStream* s = static_cast<NpapiStream*>( stream->pdata ); // check for streams we did not request or create if ( !s ) return; std::string cacheFilename( fname ); s->signalCacheFilename( std::wstring( cacheFilename.begin(), cacheFilename.end() ) ); }
/* The browser calls the NPP_DestroyStream function when a data stream sent to the plug-in is finished, either because it has completed successfully or terminated abnormally. After this, the browser deletes the NPStream object. You should delete any private data allocated in stream->pdata at this time, and should not make any further references to the stream object. */ NPError NpapiPlugin::DestroyStream(NPStream* stream, NPReason reason) { NpapiStream* s = static_cast<NpapiStream*>( stream->notifyData ); // check for streams we did not request or create if ( !s || !s->getStream() ) return NPERR_NO_ERROR; s->setStream( 0 ); stream->notifyData = 0; if ( !s->isCompleted() ) s->signalCompleted( reason == NPRES_DONE ); s->setDestroyed(); return NPERR_NO_ERROR; }
void NpapiPlugin::signalStreamOpened(FB::BrowserStream* stream) { NpapiStream* s = dynamic_cast<NpapiStream*>(stream); if ( s != NULL && !s->isCompleted() ) s->signalOpened(); }
/* see https://developer.mozilla.org/en/NPP_NewStream NPP_NewStream notifies the plug-in when a new stream is created. The NPStream* pointer is valid until the stream is destroyed. The plug-in can store plug-in-private data associated with the stream in stream->pdata. The MIME type of the stream is provided by the type parameter. The data in the stream can be the file specified in the SRC attribute of the EMBED tag, for an embedded instance, or the file itself, for a full-page instance. A plug-in can also request a stream with the function NPN_GetURL. The browser calls NPP_DestroyStream when the stream completes (either successfully or abnormally). The plug-in can terminate the stream itself by calling NPN_DestroyStream. */ NPError NpapiPlugin::NewStream(NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype) { if (stream->notifyData && !stream->pdata) stream->pdata = stream->notifyData; NpapiStream* s = static_cast<NpapiStream*>( stream->pdata ); // check for streams we did not request or create if ( !s ) { // ask the plugin if it would like to create a new stream FB::BrowserStreamPtr newstream; if ((newstream = pluginMain->handleUnsolicitedStream(type, seekable, stream->url, stream->end, stream->lastmodified, stream->headers))) { // continue function using the newly created stream object s = dynamic_cast<NpapiStream*> ( newstream.get() ); stream->pdata = static_cast<void*>( s ); } } if ( !s ) return NPERR_NO_ERROR; s->setMimeType( type ); s->setStream( stream ); s->setLength( stream->end ); s->setUrl( stream->url ); if( stream->headers ) s->setHeaders( stream->headers ); s->setSeekableByServer( seekable ? true : false ); if ( s->isSeekableRequested() && !s->isSeekableByServer() ) // requested seekable stream, but stream was not seekable { // stream can only be made seekable by downloading the entire file // which we don't want to happen automatically. s->signalFailedOpen(); // If unsuccessful, the function should return one of the NPError Error Codes. // This will cause the browser to destroy the stream without calling NPP_DestroyStream. s->setStream( 0 ); return NPERR_STREAM_NOT_SEEKABLE; } if ( s->isSeekable() ) *stype = NP_SEEK; else if ( s->isCached() ) *stype = NP_ASFILE; else *stype = NP_NORMAL; // first npp_newstream has to finish before the user may perform a RequestRead in the opened handler if ( s->isSeekable() ) signalStreamOpened( s ); //m_npHost->ScheduleAsyncCall( signalStreamOpened, s ); else signalStreamOpened( s ); return NPERR_NO_ERROR; }