-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
module.cc
500 lines (411 loc) · 17.6 KB
/
module.cc
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright 2014 The Chromium OS 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 <clocale>
#include <sstream>
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var_dictionary.h"
#include "ppapi/utility/threading/lock.h"
#include "ppapi/utility/threading/simple_thread.h"
#include "compressor.h"
#include "request.h"
#include "volume.h"
namespace {
typedef std::map<std::string, Volume*>::const_iterator volume_iterator;
typedef std::map<int, Compressor*>::const_iterator compressor_iterator;
// An internal implementation of JavaScriptMessageSenderInterface. This class
// handles all communication from the module to the JavaScript code. Thread
// safety is ensured only for PNaCl, not NaCl. See crbug.com/412692 and
// crbug.com/413513.
class JavaScriptMessageSender : public JavaScriptMessageSenderInterface {
public:
// JavaScriptMessageSender does not own the instance pointer.
explicit JavaScriptMessageSender(pp::Instance* instance)
: instance_(instance) {}
virtual void SendFileSystemError(const std::string& file_system_id,
const std::string& request_id,
const std::string& message) {
JavaScriptPostMessage(
request::CreateFileSystemError(file_system_id, request_id, message));
}
virtual void SendCompressorError(int compressor_id,
const std::string& message) {
JavaScriptPostMessage(
request::CreateCompressorError(compressor_id, message));
}
virtual void SendFileChunkRequest(const std::string& file_system_id,
const std::string& request_id,
int64_t offset,
int64_t bytes_to_read) {
PP_DCHECK(offset >= 0);
PP_DCHECK(bytes_to_read > 0);
JavaScriptPostMessage(request::CreateReadChunkRequest(
file_system_id, request_id, offset, bytes_to_read));
}
virtual void SendPassphraseRequest(const std::string& file_system_id,
const std::string& request_id) {
JavaScriptPostMessage(request::CreateReadPassphraseRequest(
file_system_id, request_id));
}
virtual void SendReadMetadataDone(const std::string& file_system_id,
const std::string& request_id,
const pp::VarDictionary& metadata) {
JavaScriptPostMessage(request::CreateReadMetadataDoneResponse(
file_system_id, request_id, metadata));
}
virtual void SendOpenFileDone(const std::string& file_system_id,
const std::string& request_id) {
JavaScriptPostMessage(
request::CreateOpenFileDoneResponse(file_system_id, request_id));
}
virtual void SendCloseFileDone(const std::string& file_system_id,
const std::string& request_id,
const std::string& open_request_id) {
JavaScriptPostMessage(request::CreateCloseFileDoneResponse(
file_system_id, request_id, open_request_id));
}
virtual void SendReadFileDone(const std::string& file_system_id,
const std::string& request_id,
const pp::VarArrayBuffer& array_buffer,
bool has_more_data) {
JavaScriptPostMessage(request::CreateReadFileDoneResponse(
file_system_id, request_id, array_buffer, has_more_data));
}
virtual void SendConsoleLog(const std::string& file_system_id,
const std::string& request_id,
const std::string& src_file,
int src_line,
const std::string& src_func,
const std::string& message) {
JavaScriptPostMessage(request::CreateConsoleLog(
file_system_id, request_id, src_file, src_line, src_func, message));
}
virtual void SendCreateArchiveDone(int compressor_id) {
JavaScriptPostMessage(request::CreateCreateArchiveDoneResponse(
compressor_id));
}
virtual void SendReadFileChunk(int compressor_id, int64_t length) {
JavaScriptPostMessage(request::CreateReadFileChunkRequest(
compressor_id, length));
}
virtual void SendWriteChunk(int compressor_id,
const pp::VarArrayBuffer& array_buffer,
int64_t length) {
JavaScriptPostMessage(request::CreateWriteChunkRequest(
compressor_id, array_buffer, length));
}
virtual void SendAddToArchiveDone(int compressor_id) {
JavaScriptPostMessage(request::CreateAddToArchiveDoneResponse(
compressor_id));
}
virtual void SendCloseArchiveDone(int compressor_id) {
JavaScriptPostMessage(request::CreateCloseArchiveDoneResponse(
compressor_id));
}
private:
// Posts a message to JavaScript. This is prone to races in case of using
// NaCl instead of PNaCl. See crbug.com/413513.
void JavaScriptPostMessage(const pp::VarDictionary& message) {
instance_->PostMessage(message);
}
pp::Instance* instance_;
};
} // namespace
// An instance for every "embed" in the web page. For this extension only one
// "embed" is necessary.
class NaclArchiveInstance : public pp::Instance {
public:
explicit NaclArchiveInstance(PP_Instance instance)
: pp::Instance(instance),
instance_handle_(instance),
message_sender_(this) {}
virtual ~NaclArchiveInstance() {
for (volume_iterator iterator = volumes_.begin();
iterator != volumes_.end();
++iterator) {
delete iterator->second;
}
}
// Handler for messages coming in from JS via postMessage().
virtual void HandleMessage(const pp::Var& var_message) {
PP_DCHECK(var_message.is_dictionary());
pp::VarDictionary var_dict(var_message);
PP_DCHECK(var_dict.Get(request::key::kOperation).is_int());
int operation = var_dict.Get(request::key::kOperation).AsInt();
if (request::IsPackRequest(operation))
HandlePackMessage(var_dict, operation);
else
HandleUnpackMessage(var_dict, operation);
}
private:
// Processes unpack messages.
void HandleUnpackMessage(const pp::VarDictionary& var_dict,
const int operation) {
PP_DCHECK(var_dict.Get(request::key::kFileSystemId).is_string());
std::string file_system_id =
var_dict.Get(request::key::kFileSystemId).AsString();
PP_DCHECK(var_dict.Get(request::key::kRequestId).is_string());
std::string request_id = var_dict.Get(request::key::kRequestId).AsString();
// Processes operation.
switch (operation) {
case request::READ_METADATA: {
ReadMetadata(var_dict, file_system_id, request_id);
break;
}
case request::READ_CHUNK_DONE:
ReadChunkDone(var_dict, file_system_id, request_id);
break;
case request::READ_CHUNK_ERROR:
ReadChunkError(file_system_id, request_id);
break;
case request::READ_PASSPHRASE_DONE:
ReadPassphraseDone(var_dict, file_system_id, request_id);
break;
case request::READ_PASSPHRASE_ERROR:
ReadPassphraseError(file_system_id, request_id);
break;
case request::OPEN_FILE:
OpenFile(var_dict, file_system_id, request_id);
break;
case request::CLOSE_FILE:
CloseFile(var_dict, file_system_id, request_id);
break;
case request::READ_FILE:
ReadFile(var_dict, file_system_id, request_id);
break;
case request::CLOSE_VOLUME: {
volume_iterator iterator = volumes_.find(file_system_id);
PP_DCHECK(iterator != volumes_.end());
delete iterator->second;
volumes_.erase(file_system_id);
break;
}
default:
PP_NOTREACHED();
}
}
// Processes pack messages.
void HandlePackMessage(const pp::VarDictionary& var_dict,
const int operation) {
PP_DCHECK(var_dict.Get(request::key::kCompressorId).is_int());
int compressor_id =
var_dict.Get(request::key::kCompressorId).AsInt();
switch (operation) {
case request::CREATE_ARCHIVE: {
CreateArchive(compressor_id);
break;
}
case request::ADD_TO_ARCHIVE: {
AddToArchive(var_dict, compressor_id);
break;
}
case request::READ_FILE_CHUNK_DONE: {
ReadFileChunkDone(var_dict, compressor_id);
break;
}
case request::WRITE_CHUNK_DONE: {
WriteChunkDone(var_dict, compressor_id);
break;
}
case request::CLOSE_ARCHIVE: {
CloseArchive(var_dict, compressor_id);
break;
}
default:
PP_NOTREACHED();
}
}
// Reads the metadata for the corresponding volume for file_system_id. This
// should be called only once and before any other operation like OpenFile,
// ReadFile, etc.
// Reading metadata or opening a file could work even if the Volume exists
// or not, but as the JavaScript code doesn't use this feature there is no
// reason to allow it. If the logic on JavaScript changes then this can be
// updated. But in current design if we read metadata for an existing Volume,
// then there is a programmer error on JavaScript side.
void ReadMetadata(const pp::VarDictionary& var_dict,
const std::string& file_system_id,
const std::string& request_id) {
// Should not call ReadMetadata for a Volume already present in NaCl.
PP_DCHECK(volumes_.find(file_system_id) == volumes_.end());
Volume* volume =
new Volume(instance_handle_, file_system_id, &message_sender_);
if (!volume->Init()) {
message_sender_.SendFileSystemError(
file_system_id,
request_id,
"Could not create a volume for: " + file_system_id + ".");
delete volume;
return;
}
volumes_[file_system_id] = volume;
PP_DCHECK(var_dict.Get(request::key::kEncoding).is_string());
PP_DCHECK(var_dict.Get(request::key::kArchiveSize).is_string());
volume->ReadMetadata(
request_id,
var_dict.Get(request::key::kEncoding).AsString(),
request::GetInt64FromString(var_dict, request::key::kArchiveSize));
}
void ReadChunkDone(const pp::VarDictionary& var_dict,
const std::string& file_system_id,
const std::string& request_id) {
PP_DCHECK(var_dict.Get(request::key::kChunkBuffer).is_array_buffer());
pp::VarArrayBuffer array_buffer(var_dict.Get(request::key::kChunkBuffer));
PP_DCHECK(var_dict.Get(request::key::kOffset).is_string());
int64_t read_offset =
request::GetInt64FromString(var_dict, request::key::kOffset);
volume_iterator iterator = volumes_.find(file_system_id);
// Volume was unmounted so ignore the read chunk operation.
// Possible scenario for read ahead.
if (iterator == volumes_.end())
return;
iterator->second->ReadChunkDone(request_id, array_buffer, read_offset);
}
void ReadChunkError(const std::string& file_system_id,
const std::string& request_id) {
volume_iterator iterator = volumes_.find(file_system_id);
// Volume was unmounted so ignore the read chunk operation.
// Possible scenario for read ahead.
if (iterator == volumes_.end())
return;
iterator->second->ReadChunkError(request_id);
}
void ReadPassphraseDone(const pp::VarDictionary& var_dict,
const std::string& file_system_id,
const std::string& request_id) {
PP_DCHECK(var_dict.Get(request::key::kPassphrase).is_string());
std::string passphrase(var_dict.Get(request::key::kPassphrase).AsString());
volume_iterator iterator = volumes_.find(file_system_id);
// Volume was unmounted so ignore the read passphrase operation.
if (iterator == volumes_.end())
return;
iterator->second->ReadPassphraseDone(request_id, passphrase);
}
void ReadPassphraseError(const std::string& file_system_id,
const std::string& request_id) {
volume_iterator iterator = volumes_.find(file_system_id);
// Volume was unmounted so ignore the read chunk operation.
if (iterator == volumes_.end())
return;
iterator->second->ReadPassphraseError(request_id);
}
void OpenFile(const pp::VarDictionary& var_dict,
const std::string& file_system_id,
const std::string& request_id) {
PP_DCHECK(var_dict.Get(request::key::kIndex).is_string());
int64_t index =
request::GetInt64FromString(var_dict, request::key::kIndex);
PP_DCHECK(var_dict.Get(request::key::kEncoding).is_string());
std::string encoding(var_dict.Get(request::key::kEncoding).AsString());
PP_DCHECK(var_dict.Get(request::key::kArchiveSize).is_string());
int64_t archive_size =
request::GetInt64FromString(var_dict, request::key::kArchiveSize);
volume_iterator iterator = volumes_.find(file_system_id);
PP_DCHECK(iterator != volumes_.end()); // Should call OpenFile after
// ReadMetadata.
iterator->second->OpenFile(request_id, index, encoding, archive_size);
}
void CloseFile(const pp::VarDictionary& var_dict,
const std::string& file_system_id,
const std::string& request_id) {
PP_DCHECK(var_dict.Get(request::key::kOpenRequestId).is_string());
std::string open_request_id(
var_dict.Get(request::key::kOpenRequestId).AsString());
volume_iterator iterator = volumes_.find(file_system_id);
PP_DCHECK(iterator !=
volumes_.end()); // Should call CloseFile after OpenFile.
iterator->second->CloseFile(request_id, open_request_id);
}
void ReadFile(const pp::VarDictionary& var_dict,
const std::string& file_system_id,
const std::string& request_id) {
PP_DCHECK(var_dict.Get(request::key::kOpenRequestId).is_string());
PP_DCHECK(var_dict.Get(request::key::kOffset).is_string());
PP_DCHECK(var_dict.Get(request::key::kLength).is_string());
volume_iterator iterator = volumes_.find(file_system_id);
PP_DCHECK(iterator !=
volumes_.end()); // Should call ReadFile after OpenFile.
// Passing the entire dictionary because pp::CompletionCallbackFactory
// cannot create callbacks with more than 3 parameters. Here we need 4:
// request_id, open_request_id, offset and length.
iterator->second->ReadFile(request_id, var_dict);
}
// Requests libarchive to create an archive object for the given compressor_id.
void CreateArchive(int compressor_id) {
Compressor* compressor =
new Compressor(instance_handle_, compressor_id, &message_sender_);
if (!compressor->Init()) {
std::stringstream ss;
ss << compressor_id;
message_sender_.SendCompressorError(
compressor_id,
"Could not create a compressor for compressor id: " + ss.str() + ".");
delete compressor;
return;
}
compressors_[compressor_id] = compressor;
compressor->CreateArchive();
}
void AddToArchive(const pp::VarDictionary& var_dict,
int compressor_id) {
compressor_iterator iterator = compressors_.find(compressor_id);
PP_DCHECK(iterator != compressors_.end());
iterator->second->AddToArchive(var_dict);
}
void ReadFileChunkDone(const pp::VarDictionary& var_dict,
const int compressor_id) {
compressor_iterator iterator = compressors_.find(compressor_id);
PP_DCHECK(iterator != compressors_.end());
iterator->second->ReadFileChunkDone(var_dict);
}
void WriteChunkDone(const pp::VarDictionary& var_dict,
int compressor_id) {
compressor_iterator iterator = compressors_.find(compressor_id);
PP_DCHECK(iterator != compressors_.end());
iterator->second->WriteChunkDone(var_dict);
}
void CloseArchive(const pp::VarDictionary& var_dict,
int compressor_id) {
compressor_iterator iterator = compressors_.find(compressor_id);
if (iterator != compressors_.end())
iterator->second->CloseArchive(var_dict);
}
// A map that holds for every opened archive its instance. The key is the file
// system id of the archive.
std::map<std::string, Volume*> volumes_;
// A map from compressor ids to compressors.
std::map<int, Compressor*> compressors_;
// An pp::InstanceHandle used to create pp::SimpleThread in Volume.
pp::InstanceHandle instance_handle_;
// An object used to send messages to JavaScript.
JavaScriptMessageSender message_sender_;
};
// The Module class. The browser calls the CreateInstance() method to create
// an instance of your NaCl module on the web page. The browser creates a new
// instance for each <embed> tag with type="application/x-pnacl" or
// type="application/x-nacl".
class NaclArchiveModule : public pp::Module {
public:
NaclArchiveModule() : pp::Module() {}
virtual ~NaclArchiveModule() {}
// Create and return a NaclArchiveInstance object.
// @param[in] instance The browser-side instance.
// @return the plugin-side instance.
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new NaclArchiveInstance(instance);
}
};
namespace pp {
// Factory function called by the browser when the module is first loaded.
// The browser keeps a singleton of this module. It calls the
// CreateInstance() method on the object you return to make instances. There
// is one instance per <embed> tag on the page. This is the main binding
// point for your NaCl module with the browser.
Module* CreateModule() {
std::setlocale(LC_ALL, "en_US.UTF-8");
return new NaclArchiveModule();
}
} // namespace pp