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