This source file includes following definitions.
- seekable_stream_
- Open
- Write
- WriteToFile
- WriteToPlugin
- OnDelayDelivery
- TryWriteToPlugin
- Close
- AsResourceClient
- Notify
- RequestedPluginModeIsAsFile
#include "content/child/npapi/plugin_stream.h"
#include <algorithm>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/child/npapi/plugin_instance.h"
#include "net/base/mime_util.h"
#include "url/gurl.h"
namespace content {
PluginStream::PluginStream(
PluginInstance* instance,
const char* url,
bool need_notify,
void* notify_data)
: instance_(instance),
notify_needed_(need_notify),
notify_data_(notify_data),
close_on_write_data_(false),
requested_plugin_mode_(NP_NORMAL),
opened_(false),
data_offset_(0),
seekable_stream_(false) {
memset(&stream_, 0, sizeof(stream_));
stream_.url = base::strdup(url);
ResetTempFileHandle();
ResetTempFileName();
}
PluginStream::~PluginStream() {
CloseTempFile();
free(const_cast<char*>(stream_.url));
}
bool PluginStream::Open(const std::string& mime_type,
const std::string& headers,
uint32 length,
uint32 last_modified,
bool request_is_seekable) {
headers_ = headers;
NPP id = instance_->npp();
stream_.end = length;
stream_.lastmodified = last_modified;
stream_.pdata = 0;
stream_.ndata = id->ndata;
stream_.notifyData = notify_data_;
if (!headers_.empty())
stream_.headers = headers_.c_str();
bool seekable_stream = false;
if (request_is_seekable) {
std::string headers_lc = StringToLowerASCII(headers);
if (headers_lc.find("accept-ranges: bytes") != std::string::npos) {
seekable_stream = true;
}
}
const char* char_mime_type = "application/x-unknown-content-type";
std::string temp_mime_type;
if (!mime_type.empty()) {
char_mime_type = mime_type.c_str();
} else {
GURL gurl(stream_.url);
base::FilePath path = base::FilePath::FromUTF8Unsafe(gurl.path());
if (net::GetMimeTypeFromFile(path, &temp_mime_type))
char_mime_type = temp_mime_type.c_str();
}
DCHECK_NE(0U, strlen(char_mime_type));
NPError err = instance_->NPP_NewStream((NPMIMEType)char_mime_type,
&stream_, seekable_stream,
&requested_plugin_mode_);
if (err != NPERR_NO_ERROR) {
Notify(err);
return false;
}
opened_ = true;
if (requested_plugin_mode_ == NP_SEEK) {
seekable_stream_ = true;
}
if (RequestedPluginModeIsAsFile()) {
if (OpenTempFile() == false) {
return false;
}
}
mime_type_ = char_mime_type;
return true;
}
int PluginStream::Write(const char* buffer, const int length,
int data_offset) {
DCHECK(opened_);
if (WriteToFile(buffer, length) &&
WriteToPlugin(buffer, length, data_offset)) {
return length;
}
return -1;
}
bool PluginStream::WriteToFile(const char* buf, size_t length) {
if (TempFileIsValid() && RequestedPluginModeIsAsFile()) {
size_t totalBytesWritten = 0, bytes;
do {
bytes = WriteBytes(buf, length);
totalBytesWritten += bytes;
} while (bytes > 0U && totalBytesWritten < length);
if (totalBytesWritten != length) {
return false;
}
}
return true;
}
bool PluginStream::WriteToPlugin(const char* buf, const int length,
const int data_offset) {
if (requested_plugin_mode_ != NP_NORMAL &&
requested_plugin_mode_ != NP_ASFILE &&
requested_plugin_mode_ != NP_SEEK) {
return true;
}
int written = TryWriteToPlugin(buf, length, data_offset);
if (written == -1)
return false;
if (written < length) {
size_t remaining = length - written;
size_t previous_size = delivery_data_.size();
delivery_data_.resize(previous_size + remaining);
data_offset_ = data_offset;
memcpy(&delivery_data_[previous_size], buf + written, remaining);
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&PluginStream::OnDelayDelivery, this));
}
return true;
}
void PluginStream::OnDelayDelivery() {
if (!opened_)
return;
int size = static_cast<int>(delivery_data_.size());
int written = TryWriteToPlugin(&delivery_data_.front(), size, data_offset_);
if (written > 0) {
delivery_data_.erase(delivery_data_.begin(),
delivery_data_.begin() + written);
}
}
int PluginStream::TryWriteToPlugin(const char* buf, const int length,
const int data_offset) {
int byte_offset = 0;
if (data_offset > 0)
data_offset_ = data_offset;
while (byte_offset < length) {
int bytes_remaining = length - byte_offset;
int bytes_to_write = instance_->NPP_WriteReady(&stream_);
if (bytes_to_write > bytes_remaining)
bytes_to_write = bytes_remaining;
if (bytes_to_write == 0)
return byte_offset;
int bytes_consumed = instance_->NPP_Write(
&stream_, data_offset_, bytes_to_write,
const_cast<char*>(buf + byte_offset));
if (bytes_consumed < 0) {
Close(NPRES_NETWORK_ERR);
return -1;
}
if (bytes_consumed == 0) {
return byte_offset;
}
bytes_consumed = std::min(bytes_consumed, bytes_to_write);
data_offset_ += bytes_consumed;
byte_offset += bytes_consumed;
}
if (close_on_write_data_)
Close(NPRES_DONE);
return length;
}
bool PluginStream::Close(NPReason reason) {
if (opened_ == true) {
opened_ = false;
if (delivery_data_.size()) {
if (reason == NPRES_DONE) {
close_on_write_data_ = true;
return true;
} else {
delivery_data_.resize(0);
}
}
if (TempFileIsValid()) {
CloseTempFile();
if (reason == NPRES_DONE)
WriteAsFile();
}
if (stream_.ndata != NULL) {
NPError err = instance_->NPP_DestroyStream(&stream_, reason);
DCHECK(err == NPERR_NO_ERROR);
}
}
Notify(reason);
return true;
}
WebPluginResourceClient* PluginStream::AsResourceClient() {
return NULL;
}
void PluginStream::Notify(NPReason reason) {
if (notify_needed_) {
instance_->NPP_URLNotify(stream_.url, reason, notify_data_);
notify_needed_ = false;
}
}
bool PluginStream::RequestedPluginModeIsAsFile() const {
return (requested_plugin_mode_ == NP_ASFILE ||
requested_plugin_mode_ == NP_ASFILEONLY);
}
}