NetscapePluginStream.cpp [plain text]
#include "config.h"
#include "NetscapePluginStream.h"
#if ENABLE(NETSCAPE_PLUGIN_API)
#include "NetscapePlugin.h"
#include <utility>
#include <wtf/Vector.h>
using namespace WebCore;
namespace WebKit {
NetscapePluginStream::NetscapePluginStream(PassRefPtr<NetscapePlugin> plugin, uint64_t streamID, const String& requestURLString, bool sendNotification, void* notificationData)
: m_plugin(plugin)
, m_streamID(streamID)
, m_requestURLString(requestURLString)
, m_sendNotification(sendNotification)
, m_notificationData(notificationData)
, m_npStream()
, m_transferMode(NP_NORMAL)
, m_offset(0)
, m_fileHandle(invalidPlatformFileHandle)
, m_isStarted(false)
#if !ASSERT_DISABLED
, m_urlNotifyHasBeenCalled(false)
#endif
, m_deliveryDataTimer(RunLoop::main(), this, &NetscapePluginStream::deliverDataToPlugin)
, m_stopStreamWhenDoneDelivering(false)
{
}
NetscapePluginStream::~NetscapePluginStream()
{
ASSERT(!m_isStarted);
ASSERT(!m_sendNotification || m_urlNotifyHasBeenCalled);
ASSERT(m_fileHandle == invalidPlatformFileHandle);
}
void NetscapePluginStream::willSendRequest(const URL& requestURL, const URL& redirectResponseURL, int redirectResponseStatus)
{
Ref<NetscapePluginStream> protect(*this);
if (redirectResponseStatus >= 300 && redirectResponseStatus < 400)
m_plugin->registerRedirect(this, requestURL, redirectResponseStatus, m_notificationData);
}
void NetscapePluginStream::didReceiveResponse(const URL& responseURL, uint32_t streamLength, uint32_t lastModifiedTime, const String& mimeType, const String& headers)
{
Ref<NetscapePluginStream> protect(*this);
start(responseURL, streamLength, lastModifiedTime, mimeType, headers);
}
void NetscapePluginStream::didReceiveData(const char* bytes, int length)
{
Ref<NetscapePluginStream> protect(*this);
deliverData(bytes, length);
}
void NetscapePluginStream::didFinishLoading()
{
Ref<NetscapePluginStream> protect(*this);
stop(NPRES_DONE);
}
void NetscapePluginStream::didFail(bool wasCancelled)
{
Ref<NetscapePluginStream> protect(*this);
stop(wasCancelled ? NPRES_USER_BREAK : NPRES_NETWORK_ERR);
}
void NetscapePluginStream::sendJavaScriptStream(const String& result)
{
Ref<NetscapePluginStream> protect(*this);
CString resultCString = result.utf8();
if (resultCString.isNull()) {
notifyAndDestroyStream(NPRES_NETWORK_ERR);
return;
}
if (!start(m_requestURLString, resultCString.length(), 0, "text/plain", ""))
return;
deliverData(resultCString.data(), resultCString.length());
stop(NPRES_DONE);
}
NPError NetscapePluginStream::destroy(NPReason reason)
{
if (!m_isStarted)
return NPERR_GENERIC_ERROR;
if (reason == NPRES_DONE)
return NPERR_INVALID_PARAM;
cancel();
stop(reason);
return NPERR_NO_ERROR;
}
static bool isSupportedTransferMode(uint16_t transferMode)
{
switch (transferMode) {
case NP_ASFILEONLY:
case NP_ASFILE:
case NP_NORMAL:
return true;
case NP_SEEK:
return false;
}
ASSERT_NOT_REACHED();
return false;
}
bool NetscapePluginStream::start(const String& responseURLString, uint32_t streamLength, uint32_t lastModifiedTime, const String& mimeType, const String& headers)
{
m_responseURL = responseURLString.utf8();
m_mimeType = mimeType.utf8();
m_headers = headers.utf8();
m_npStream.ndata = this;
m_npStream.url = m_responseURL.data();
m_npStream.end = streamLength;
m_npStream.lastmodified = lastModifiedTime;
m_npStream.notifyData = m_notificationData;
m_npStream.headers = m_headers.length() == 0 ? 0 : m_headers.data();
NPError error = m_plugin->NPP_NewStream(const_cast<char*>(m_mimeType.data()), &m_npStream, false, &m_transferMode);
if (error != NPERR_NO_ERROR) {
cancel();
notifyAndDestroyStream(NPRES_NETWORK_ERR);
return false;
}
m_isStarted = true;
if (!isSupportedTransferMode(m_transferMode)) {
cancel();
stop(NPRES_NETWORK_ERR);
return false;
}
return true;
}
void NetscapePluginStream::deliverData(const char* bytes, int length)
{
ASSERT(m_isStarted);
if (m_transferMode != NP_ASFILEONLY) {
if (!m_deliveryData)
m_deliveryData = std::make_unique<Vector<uint8_t>>();
m_deliveryData->reserveCapacity(m_deliveryData->size() + length);
m_deliveryData->append(bytes, length);
deliverDataToPlugin();
}
if (m_transferMode == NP_ASFILE || m_transferMode == NP_ASFILEONLY)
deliverDataToFile(bytes, length);
}
void NetscapePluginStream::deliverDataToPlugin()
{
ASSERT(m_isStarted);
int32_t numBytesToDeliver = m_deliveryData->size();
int32_t numBytesDelivered = 0;
while (numBytesDelivered < numBytesToDeliver) {
int32_t numBytesPluginCanHandle = m_plugin->NPP_WriteReady(&m_npStream);
if (!m_isStarted)
return;
if (numBytesPluginCanHandle <= 0) {
m_deliveryDataTimer.startOneShot(0);
break;
}
int32_t dataLength = std::min(numBytesPluginCanHandle, numBytesToDeliver - numBytesDelivered);
uint8_t* data = m_deliveryData->data() + numBytesDelivered;
int32_t numBytesWritten = m_plugin->NPP_Write(&m_npStream, m_offset, dataLength, data);
if (numBytesWritten < 0) {
cancel();
stop(NPRES_NETWORK_ERR);
return;
}
if (!m_isStarted)
return;
numBytesWritten = std::min(numBytesWritten, dataLength);
m_offset += numBytesWritten;
numBytesDelivered += numBytesWritten;
}
if (!numBytesDelivered)
return;
if (numBytesDelivered < numBytesToDeliver) {
m_deliveryData->remove(0, numBytesDelivered);
} else {
m_deliveryData->clear();
if (m_stopStreamWhenDoneDelivering)
stop(NPRES_DONE);
}
}
void NetscapePluginStream::deliverDataToFile(const char* bytes, int length)
{
if (m_fileHandle == invalidPlatformFileHandle && m_filePath.isNull()) {
m_filePath = openTemporaryFile("WebKitPluginStream", m_fileHandle);
if (m_fileHandle == invalidPlatformFileHandle) {
stop(NPRES_NETWORK_ERR);
return;
}
}
if (!length)
return;
int byteCount = writeToFile(m_fileHandle, bytes, length);
if (byteCount != length) {
closeFile(m_fileHandle);
stop(NPRES_NETWORK_ERR);
}
}
void NetscapePluginStream::stop(NPReason reason)
{
if (!m_isStarted) {
ASSERT(reason != NPRES_DONE);
notifyAndDestroyStream(reason);
return;
}
if (reason == NPRES_DONE && m_deliveryData && !m_deliveryData->isEmpty()) {
ASSERT(m_deliveryDataTimer.isActive());
m_stopStreamWhenDoneDelivering = true;
return;
}
m_deliveryData = nullptr;
m_deliveryDataTimer.stop();
if (m_transferMode == NP_ASFILE || m_transferMode == NP_ASFILEONLY) {
if (reason == NPRES_DONE) {
deliverDataToFile(0, 0);
if (m_fileHandle != invalidPlatformFileHandle)
closeFile(m_fileHandle);
ASSERT(!m_filePath.isNull());
m_plugin->NPP_StreamAsFile(&m_npStream, m_filePath.utf8().data());
} else {
if (m_fileHandle != invalidPlatformFileHandle)
closeFile(m_fileHandle);
}
deleteFile(m_filePath);
m_filePath = String();
if (!m_isStarted)
return;
}
m_isStarted = false;
m_plugin->NPP_DestroyStream(&m_npStream, reason);
notifyAndDestroyStream(reason);
}
void NetscapePluginStream::setURL(const String& newURLString)
{
m_requestURLString = newURLString;
}
void NetscapePluginStream::cancel()
{
m_plugin->cancelStreamLoad(this);
}
void NetscapePluginStream::notifyAndDestroyStream(NPReason reason)
{
ASSERT(!m_isStarted);
ASSERT(!m_deliveryDataTimer.isActive());
ASSERT(!m_urlNotifyHasBeenCalled);
if (m_sendNotification) {
m_plugin->NPP_URLNotify(m_requestURLString.utf8().data(), reason, m_notificationData);
#if !ASSERT_DISABLED
m_urlNotifyHasBeenCalled = true;
#endif
}
m_plugin->removePluginStream(this);
}
}
#endif // ENABLE(NETSCAPE_PLUGIN_API)