• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "content/test/plugin/plugin_request_read_test.h"
6 
7 #include "base/basictypes.h"
8 
9 namespace NPAPIClient {
10 
PluginRequestReadTest(NPP id,NPNetscapeFuncs * host_functions)11 PluginRequestReadTest::PluginRequestReadTest(NPP id,
12                                              NPNetscapeFuncs* host_functions)
13     : PluginTest(id, host_functions),
14       tests_started_(false),
15       read_requested_(false) {
16 }
17 
~PluginRequestReadTest()18 PluginRequestReadTest::~PluginRequestReadTest() {
19 }
20 
New(uint16 mode,int16 argc,const char * argn[],const char * argv[],NPSavedData * saved)21 NPError PluginRequestReadTest::New(uint16 mode, int16 argc, const char* argn[],
22                              const char* argv[], NPSavedData* saved) {
23   url_to_request_ = GetArgValue("url_to_request", argc, argn, argv);
24   return PluginTest::New(mode, argc, argn, argv, saved);
25 }
26 
SetWindow(NPWindow * window)27 NPError PluginRequestReadTest::SetWindow(NPWindow* window) {
28   if (!tests_started_) {
29     tests_started_ = true;
30     NPError result = HostFunctions()->geturl(id(),
31                                              url_to_request_.c_str(),
32                                              NULL);
33     if (result != NPERR_NO_ERROR)
34       SetError("Failed request anURL.");
35   }
36   return PluginTest::SetWindow(window);
37 }
38 
NewStream(NPMIMEType type,NPStream * stream,NPBool seekable,uint16 * stream_type)39 NPError PluginRequestReadTest::NewStream(NPMIMEType type, NPStream* stream,
40                                          NPBool seekable, uint16* stream_type) {
41   *stream_type = NP_SEEK;
42   if (!read_requested_) {
43     requested_ranges_.resize(1);
44     requested_ranges_[0].offset = 4;
45     requested_ranges_[0].length = 6;
46     requested_ranges_[0].next = NULL;
47     NPError result = HostFunctions()->requestread(stream,
48                                                   &requested_ranges_[0]);
49     if (result != NPERR_NO_ERROR)
50       SetError("Failed request read from stream.");
51     read_requested_ = true;
52   }
53   return PluginTest::NewStream(type, stream, seekable, stream_type);
54 }
55 
DestroyStream(NPStream * stream,NPError reason)56 NPError PluginRequestReadTest::DestroyStream(NPStream *stream, NPError reason) {
57   if (!requested_ranges_.empty())
58     SetError("Some requested ranges are not received!");
59   SignalTestCompleted();
60   return PluginTest::DestroyStream(stream, reason);
61 }
62 
WriteReady(NPStream * stream)63 int32 PluginRequestReadTest::WriteReady(NPStream* stream) {
64   int32 result = 0;
65   for (size_t i = 0; i < requested_ranges_.size(); ++i)
66     result += requested_ranges_[i].length;
67   return result;
68 }
69 
Write(NPStream * stream,int32 offset,int32 len,void * buffer)70 int32 PluginRequestReadTest::Write(NPStream* stream, int32 offset, int32 len,
71                                    void* buffer) {
72   std::vector<NPByteRange>::iterator it;
73   // Remove received range (or sub-range) from requested_ranges_, and
74   // verify that we have received proper data.
75 
76   for (it = requested_ranges_.begin(); it != requested_ranges_.end(); ++it) {
77     if (it->offset == offset)
78       break;
79   }
80   if (it == requested_ranges_.end()) {
81     // It is Ok for browser to write some data from start of the stream before
82     // we've issued any read requests.
83     return len;
84   }
85   // Shrink range to mark area we have just received.
86   it->offset += len;
87   if (static_cast<int32>(it->length) < len)
88     it->length = 0;
89   else
90     it->length -= len;
91   if (it->length == 0)
92     requested_ranges_.erase(it);
93 
94   // Verify that data, which we got, is right.
95   const char* data = static_cast<const char*>(buffer);
96   for (int32 i = 0; i < len; ++i) {
97     int cur_offset = offset + i;
98     char expected = '0' + cur_offset;
99     if (data[i] != expected) {
100       SetError("Content mismatch between data and source!");
101       break;
102     }
103   }
104   if (requested_ranges_.empty())
105     SignalTestCompleted();
106 
107   return len;
108 }
109 
110 }  // namespace NPAPIClient
111 
112