This source file includes following definitions.
- read_requested_
- New
- SetWindow
- NewStream
- DestroyStream
- WriteReady
- Write
#include "content/test/plugin/plugin_request_read_test.h"
#include "base/basictypes.h"
namespace NPAPIClient {
PluginRequestReadTest::PluginRequestReadTest(NPP id,
NPNetscapeFuncs* host_functions)
: PluginTest(id, host_functions),
tests_started_(false),
read_requested_(false) {
}
PluginRequestReadTest::~PluginRequestReadTest() {
}
NPError PluginRequestReadTest::New(uint16 mode, int16 argc, const char* argn[],
const char* argv[], NPSavedData* saved) {
url_to_request_ = GetArgValue("url_to_request", argc, argn, argv);
return PluginTest::New(mode, argc, argn, argv, saved);
}
NPError PluginRequestReadTest::SetWindow(NPWindow* window) {
if (!tests_started_) {
tests_started_ = true;
NPError result = HostFunctions()->geturl(id(),
url_to_request_.c_str(),
NULL);
if (result != NPERR_NO_ERROR)
SetError("Failed request anURL.");
}
return PluginTest::SetWindow(window);
}
NPError PluginRequestReadTest::NewStream(NPMIMEType type, NPStream* stream,
NPBool seekable, uint16* stream_type) {
*stream_type = NP_SEEK;
if (!read_requested_) {
requested_ranges_.resize(1);
requested_ranges_[0].offset = 4;
requested_ranges_[0].length = 6;
requested_ranges_[0].next = NULL;
NPError result = HostFunctions()->requestread(stream,
&requested_ranges_[0]);
if (result != NPERR_NO_ERROR)
SetError("Failed request read from stream.");
read_requested_ = true;
}
return PluginTest::NewStream(type, stream, seekable, stream_type);
}
NPError PluginRequestReadTest::DestroyStream(NPStream *stream, NPError reason) {
if (!requested_ranges_.empty())
SetError("Some requested ranges are not received!");
SignalTestCompleted();
return PluginTest::DestroyStream(stream, reason);
}
int32 PluginRequestReadTest::WriteReady(NPStream* stream) {
int32 result = 0;
for (size_t i = 0; i < requested_ranges_.size(); ++i)
result += requested_ranges_[i].length;
return result;
}
int32 PluginRequestReadTest::Write(NPStream* stream, int32 offset, int32 len,
void* buffer) {
std::vector<NPByteRange>::iterator it;
for (it = requested_ranges_.begin(); it != requested_ranges_.end(); ++it) {
if (it->offset == offset)
break;
}
if (it == requested_ranges_.end()) {
return len;
}
it->offset += len;
if (static_cast<int32>(it->length) < len)
it->length = 0;
else
it->length -= len;
if (it->length == 0)
requested_ranges_.erase(it);
const char* data = static_cast<const char*>(buffer);
for (int32 i = 0; i < len; ++i) {
int cur_offset = offset + i;
char expected = '0' + cur_offset;
if (data[i] != expected) {
SetError("Content mismatch between data and source!");
break;
}
}
if (requested_ranges_.empty())
SignalTestCompleted();
return len;
}
}