Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: ppapi/native_client/tests/ppapi_simple_tests/file.cc

Issue 7740013: Cloning a bunch of stuff from the native_client repository at r6528 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Native Client 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 <stdint.h>
6 #include <stdlib.h>
7
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include <queue>
12
13 #include <nacl/nacl_check.h>
14
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/ppb_file_io.h"
17
18 #include "ppapi/cpp/completion_callback.h"
19 #include "ppapi/cpp/instance.h"
20 #include "ppapi/cpp/file_ref.h"
21 #include "ppapi/cpp/file_io.h"
22 #include "ppapi/cpp/module.h"
23 #include "ppapi/cpp/url_response_info.h"
24 #include "ppapi/cpp/url_loader.h"
25 #include "ppapi/cpp/url_request_info.h"
26 #include "ppapi/cpp/var.h"
27
28 using std::string;
29 using std::ostringstream;
30
31 const int kDefaultChunkSize = 1024;
32
33
34 class MyInstance : public pp::Instance {
35 private:
36 string url_;
37 bool stream_to_file_;
38 uint32_t chunk_size_;
39 bool debug_;
40 bool pdebug_;
41
42 void ParseArgs(uint32_t argc, const char* argn[], const char* argv[]) {
43 for (uint32_t i = 0; i < argc; ++i) {
44 const std::string tag = argn[i];
45 if (tag == "chunk_size") chunk_size_ = strtol(argv[i], 0, 0);
46 if (tag == "url") url_ = argv[i];
47 if (tag == "to_file") stream_to_file_ = strtol(argv[i], 0, 0);
48 if (tag == "debug") debug_ = strtol(argv[i], 0, 0);
49 if (tag == "pdebug") pdebug_ = strtol(argv[i], 0, 0);
50 // ignore other tags
51 }
52 }
53
54 public:
55 void Message(const string& s) {
56 ostringstream stream;
57 stream << pp_instance() << ": " << s;
58 pp::Var message(stream.str());
59 PostMessage(message);
60 }
61
62
63 void Debug(const string& s) {
64 if (debug_) {
65 std::cout << "DEBUG: " << s;
66 }
67 if (pdebug_) {
68 Message("DEBUG: " + s);
69 }
70 }
71
72 explicit MyInstance(PP_Instance instance)
73 : pp::Instance(instance),
74 url_("no_url_given.html"),
75 stream_to_file_(true),
76 chunk_size_(kDefaultChunkSize),
77 debug_(false),
78 pdebug_(false) {
79 }
80
81 virtual ~MyInstance() {}
82
83 // Defined below. This function does the real work by delegating it to
84 // either ReaderStreamAsFile or ReaderResponseBody
85 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
86 };
87
88
89
90 class ReaderStreamAsFile {
91 private:
92 uint32_t current_offset_;
93 uint32_t chunk_size_;
94 char* buffer_;
95 pp::URLResponseInfo* response_info_;
96 pp::FileRef* file_ref_;
97 pp::FileIO* file_io_;
98 MyInstance* instance_;
99 pp::URLLoader loader_;
100
101 // forward to instance
102 void Message(const string& s) {
103 instance_->Message(s);
104 }
105
106 void Debug(const string& s) {
107 instance_->Debug(s);
108 }
109
110 static void ReadCompleteCallback(void* thiz, int32_t result) {
111 ReaderStreamAsFile* reader = static_cast<ReaderStreamAsFile*>(thiz);
112 ostringstream stream;
113 stream << "ReadCompleteCallback Bytes Read: " << result;
114 reader->Debug(stream.str());
115 if (result <= 0) {
116 reader->Message("COMPLETE");
117 return;
118 }
119
120 reader->current_offset_ += result;
121 reader->ReadMore();
122 }
123
124 void ReadMore() {
125 pp::CompletionCallback cc(ReadCompleteCallback, this);
126 cc.set_flags(PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
127 int32_t rv = file_io_->Read(current_offset_, buffer_, chunk_size_, cc);
128 if (rv == PP_OK) {
129 cc.Run(rv);
130 } else if (rv != PP_OK_COMPLETIONPENDING) {
131 Message("Error: ReadMore unexpected rv");
132 }
133 }
134
135 static void OpenFileCompleteCallback(void* thiz, int32_t result) {
136 ReaderStreamAsFile* reader = static_cast<ReaderStreamAsFile*>(thiz);
137
138 if (result != PP_OK) {
139 reader->Message("Error: FileOpenCompleteCallback unexpected result");
140 return;
141 }
142
143 reader->ReadMore();
144 }
145
146 void OpenFile() {
147 file_ref_ = new pp::FileRef(response_info_->GetBodyAsFileRef());
148 CHECK(!file_ref_->is_null());
149
150 file_io_ = new pp::FileIO(instance_);
151 CHECK(!file_io_->is_null());
152
153 pp::CompletionCallback cc(OpenFileCompleteCallback, this);
154 int32_t rv = file_io_->Open(*file_ref_, PP_FILEOPENFLAG_READ, cc);
155 if (rv != PP_OK_COMPLETIONPENDING) {
156 Message("Error: OpenFile unexpected rv");
157 }
158 }
159
160 static void FinishCompleteCallback(void* thiz, int32_t result) {
161 ReaderStreamAsFile* reader = static_cast<ReaderStreamAsFile*>(thiz);
162 if (result != PP_OK) {
163 reader->Message("Error: FinishCompleteCallback unexpected result");
164 return;
165 }
166
167 reader->OpenFile();
168 }
169
170 void Finish() {
171 pp::CompletionCallback cc(FinishCompleteCallback, this);
172 cc.set_flags(PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
173 int32_t rv = loader_.FinishStreamingToFile(cc);
174 if (rv == PP_OK) {
175 cc.Run(rv);
176 } else if (rv != PP_OK_COMPLETIONPENDING) {
177 Message("Error: Finish unexpected rv");
178 }
179 }
180
181 // this callback handles both regular and "stream-to-file" mode
182 // But control flow diverges afterwards
183 static void OpenURLCompleteCallback(void* thiz, int32_t result) {
184 ReaderStreamAsFile* reader = static_cast<ReaderStreamAsFile*>(thiz);
185
186 reader->response_info_ =
187 new pp::URLResponseInfo(reader->loader_.GetResponseInfo());
188 CHECK(!reader->response_info_->is_null());
189 int32_t status_code = reader->response_info_->GetStatusCode();
190 if (status_code != 200) {
191 reader->Message("Error: OpenURLCompleteCallback unexpected status code");
192 return;
193 }
194
195 reader->Finish();
196 }
197
198 void OpenURL(const string& url) {
199 pp::URLRequestInfo request(instance_);
200 request.SetURL(url);
201 request.SetStreamToFile(true);
202
203 pp::CompletionCallback cc(OpenURLCompleteCallback, this);
204 int32_t rv = loader_.Open(request, cc);
205 if (rv != PP_OK_COMPLETIONPENDING) {
206 Message("Error: OpenURL unexpected rv");
207 }
208 }
209
210 public:
211 ReaderStreamAsFile(MyInstance* instance,
212 uint32_t chunk_size,
213 const string& url) :
214 current_offset_(0),
215 chunk_size_(chunk_size),
216 buffer_(new char[chunk_size]),
217 response_info_(0),
218 file_ref_(0),
219 file_io_(0),
220 instance_(instance),
221 loader_(instance) {
222 OpenURL(url);
223 }
224 };
225
226
227 class ReaderResponseBody {
228 private:
229 uint32_t chunk_size_;
230 char* buffer_;
231 MyInstance* instance_;
232 pp::URLLoader loader_;
233
234 // forward to instance
235 void Message(const string& s) {
236 instance_->Message(s);
237 }
238
239 void Debug(const string& s) {
240 instance_->Debug(s);
241 }
242
243 static void ReadCompleteCallback(void* thiz, int32_t result) {
244 ReaderResponseBody* reader = static_cast<ReaderResponseBody*>(thiz);
245 ostringstream stream;
246 stream << "ReadCompleteCallback Bytes Read: " << result;
247 reader->Debug(stream.str());
248 if (result <= 0) {
249 reader->Message("COMPLETE");
250 return;
251 }
252 reader->ReadMore();
253 }
254
255 void ReadMore() {
256 pp::CompletionCallback cc(ReadCompleteCallback, this);
257 cc.set_flags(PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
258 int rv = loader_.ReadResponseBody(buffer_, chunk_size_, cc);
259 if (rv == PP_OK) {
260 cc.Run(rv);
261 } else if (rv != PP_OK_COMPLETIONPENDING) {
262 Message("Error: ReadMore unexpected rv");
263 }
264 }
265
266 static void LoadCompleteCallback(void* thiz, int32_t result) {
267 ReaderResponseBody* reader = static_cast<ReaderResponseBody*>(thiz);
268 ostringstream stream;
269 stream << "LoadCompleteCallback: " << result;
270 reader->Debug(stream.str());
271 pp::URLResponseInfo response_info(reader->loader_.GetResponseInfo());
272 CHECK(!response_info.is_null());
273 int32_t status_code = response_info.GetStatusCode();
274 if (status_code != 200) {
275 reader->Message("Error: LoadCompleteCallback unexpected status code");
276 return;
277 }
278
279 reader->ReadMore();
280 }
281
282 public:
283 ReaderResponseBody(MyInstance* instance,
284 uint32_t chunk_size,
285 const string& url) :
286 chunk_size_(chunk_size),
287 buffer_(new char[chunk_size]),
288 instance_(instance),
289 loader_(instance) {
290 pp::URLRequestInfo request(instance_);
291 request.SetURL(url);
292 request.SetStreamToFile(false);
293
294 pp::CompletionCallback cc(LoadCompleteCallback, this);
295 int32_t rv = loader_.Open(request, cc);
296 if (rv != PP_OK_COMPLETIONPENDING) {
297 Message("Error: ReaderResponseBody: unexpected rv");
298 }
299 }
300 };
301
302 // Defined here because of circular class visibility issues
303 bool MyInstance::Init(uint32_t argc, const char* argn[], const char* argv[]) {
304 ParseArgs(argc, argn, argv);
305 if (stream_to_file_) {
306 new ReaderStreamAsFile(this, chunk_size_, url_);
307 } else {
308 new ReaderResponseBody(this, chunk_size_, url_);
309 }
310 return true;
311 }
312
313 // standard boilerplate code below
314 class MyModule : public pp::Module {
315 public:
316 virtual pp::Instance* CreateInstance(PP_Instance instance) {
317 return new MyInstance(instance);
318 }
319 };
320
321 namespace pp {
322 Module* CreateModule() {
323 return new MyModule();
324 }
325 }
OLDNEW
« no previous file with comments | « ppapi/native_client/tests/ppapi_simple_tests/event.stdout ('k') | ppapi/native_client/tests/ppapi_simple_tests/file1.stdin » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698