forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdm_file_io_impl.cc
547 lines (452 loc) · 16.5 KB
/
cdm_file_io_impl.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2013 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 "media/cdm/ppapi/cdm_file_io_impl.h"
#include <algorithm>
#include <sstream>
#include "media/cdm/ppapi/cdm_logging.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/dev/url_util_dev.h"
namespace media {
// Arbitrary choice based on the following heuristic ideas:
// - not too big to avoid unnecessarily large memory allocation;
// - not too small to avoid breaking most reads into multiple read operations.
const int kReadSize = 8 * 1024;
// Call func_call and check the result. If the result is not
// PP_OK_COMPLETIONPENDING, print out logs, call OnError() and return.
#define CHECK_PP_OK_COMPLETIONPENDING(func_call, error_type) \
do { \
int32_t result = func_call; \
PP_DCHECK(result != PP_OK); \
if (result != PP_OK_COMPLETIONPENDING) { \
CDM_DLOG() << #func_call << " failed with result: " << result; \
state_ = STATE_ERROR; \
OnError(error_type); \
return; \
} \
} while (0)
#if !defined(NDEBUG)
// PPAPI calls should only be made on the main thread. In this file, main thread
// checking is only performed in public APIs and the completion callbacks. This
// ensures all functions are running on the main thread since internal methods
// are called either by the public APIs or by the completion callbacks.
static bool IsMainThread() {
return pp::Module::Get()->core()->IsMainThread();
}
#endif // !defined(NDEBUG)
// Posts a task to run |cb| on the main thread. The task is posted even if the
// current thread is the main thread.
static void PostOnMain(pp::CompletionCallback cb) {
pp::Module::Get()->core()->CallOnMainThread(0, cb, PP_OK);
}
CdmFileIOImpl::FileLockMap* CdmFileIOImpl::file_lock_map_ = NULL;
CdmFileIOImpl::ResourceTracker::ResourceTracker() {
// Do nothing here since we lazy-initialize CdmFileIOImpl::file_lock_map_
// in CdmFileIOImpl::AcquireFileLock().
}
CdmFileIOImpl::ResourceTracker::~ResourceTracker() {
delete CdmFileIOImpl::file_lock_map_;
}
CdmFileIOImpl::CdmFileIOImpl(
cdm::FileIOClient* client,
PP_Instance pp_instance,
const pp::CompletionCallback& first_file_read_cb)
: state_(STATE_UNOPENED),
client_(client),
pp_instance_handle_(pp_instance),
io_offset_(0),
first_file_read_reported_(false),
first_file_read_cb_(first_file_read_cb),
callback_factory_(this) {
PP_DCHECK(IsMainThread());
PP_DCHECK(pp_instance); // 0 indicates a "NULL handle".
}
CdmFileIOImpl::~CdmFileIOImpl() {
// The destructor is private. |this| can only be destructed through Close().
PP_DCHECK(state_ == STATE_CLOSED);
}
// Call sequence: Open() -> OpenFileSystem() -> STATE_FILE_SYSTEM_OPENED.
// Note: This only stores file name and opens the file system. The real file
// open is deferred to when Read() or Write() is called.
void CdmFileIOImpl::Open(const char* file_name, uint32_t file_name_size) {
CDM_DLOG() << __FUNCTION__;
PP_DCHECK(IsMainThread());
if (state_ != STATE_UNOPENED) {
CDM_DLOG() << "Open() called in an invalid state.";
OnError(OPEN_ERROR);
return;
}
// File name should not (1) be empty, (2) start with '_', or (3) contain any
// path separators.
std::string file_name_str(file_name, file_name_size);
if (file_name_str.empty() ||
file_name_str[0] == '_' ||
file_name_str.find('/') != std::string::npos ||
file_name_str.find('\\') != std::string::npos) {
CDM_DLOG() << "Invalid file name.";
state_ = STATE_ERROR;
OnError(OPEN_ERROR);
return;
}
// pp::FileRef only accepts path that begins with a '/' character.
file_name_ = '/' + file_name_str;
if (!AcquireFileLock()) {
CDM_DLOG() << "File is in use by other cdm::FileIO objects.";
OnError(OPEN_WHILE_IN_USE);
return;
}
state_ = STATE_OPENING_FILE_SYSTEM;
OpenFileSystem();
}
// Call sequence:
// Read() -> OpenFileForRead() -> ReadFile() -> Done.
void CdmFileIOImpl::Read() {
CDM_DLOG() << __FUNCTION__;
PP_DCHECK(IsMainThread());
if (state_ == STATE_READING || state_ == STATE_WRITING) {
CDM_DLOG() << "Read() called during pending read/write.";
OnError(READ_WHILE_IN_USE);
return;
}
if (state_ != STATE_FILE_SYSTEM_OPENED) {
CDM_DLOG() << "Read() called in an invalid state.";
OnError(READ_ERROR);
return;
}
PP_DCHECK(io_offset_ == 0);
PP_DCHECK(io_buffer_.empty());
PP_DCHECK(cumulative_read_buffer_.empty());
io_buffer_.resize(kReadSize);
io_offset_ = 0;
state_ = STATE_READING;
OpenFileForRead();
}
// Call sequence:
// Write() -> OpenTempFileForWrite() -> WriteTempFile() -> RenameTempFile().
// The file name of the temporary file is /_<requested_file_name>.
void CdmFileIOImpl::Write(const uint8_t* data, uint32_t data_size) {
CDM_DLOG() << __FUNCTION__;
PP_DCHECK(IsMainThread());
if (state_ == STATE_READING || state_ == STATE_WRITING) {
CDM_DLOG() << "Write() called during pending read/write.";
OnError(WRITE_WHILE_IN_USE);
return;
}
if (state_ != STATE_FILE_SYSTEM_OPENED) {
CDM_DLOG() << "Write() called in an invalid state.";
OnError(WRITE_ERROR);
return;
}
PP_DCHECK(io_offset_ == 0);
PP_DCHECK(io_buffer_.empty());
if (data_size > 0)
io_buffer_.assign(data, data + data_size);
else
PP_DCHECK(!data);
state_ = STATE_WRITING;
OpenTempFileForWrite();
}
void CdmFileIOImpl::Close() {
CDM_DLOG() << __FUNCTION__;
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ != STATE_CLOSED);
Reset();
state_ = STATE_CLOSED;
ReleaseFileLock();
// All pending callbacks are canceled since |callback_factory_| is destroyed.
delete this;
}
bool CdmFileIOImpl::SetFileID() {
PP_DCHECK(file_id_.empty());
PP_DCHECK(!file_name_.empty() && file_name_[0] == '/');
// Not taking ownership of |url_util_dev| (which is a singleton).
const pp::URLUtil_Dev* url_util_dev = pp::URLUtil_Dev::Get();
PP_URLComponents_Dev components;
pp::Var url_var =
url_util_dev->GetDocumentURL(pp_instance_handle_, &components);
if (!url_var.is_string())
return false;
std::string url = url_var.AsString();
file_id_.append(url, components.scheme.begin, components.scheme.len);
file_id_ += ':';
file_id_.append(url, components.host.begin, components.host.len);
file_id_ += ':';
file_id_.append(url, components.port.begin, components.port.len);
file_id_ += file_name_;
return true;
}
bool CdmFileIOImpl::AcquireFileLock() {
PP_DCHECK(IsMainThread());
if (file_id_.empty() && !SetFileID())
return false;
if (!file_lock_map_) {
file_lock_map_ = new FileLockMap();
} else {
FileLockMap::iterator found = file_lock_map_->find(file_id_);
if (found != file_lock_map_->end() && found->second)
return false;
}
(*file_lock_map_)[file_id_] = true;
return true;
}
void CdmFileIOImpl::ReleaseFileLock() {
PP_DCHECK(IsMainThread());
if (!file_lock_map_)
return;
FileLockMap::iterator found = file_lock_map_->find(file_id_);
if (found != file_lock_map_->end() && found->second)
found->second = false;
}
void CdmFileIOImpl::OpenFileSystem() {
PP_DCHECK(state_ == STATE_OPENING_FILE_SYSTEM);
pp::CompletionCallbackWithOutput<pp::FileSystem> cb =
callback_factory_.NewCallbackWithOutput(
&CdmFileIOImpl::OnFileSystemOpened);
isolated_file_system_ = pp::IsolatedFileSystemPrivate(
pp_instance_handle_, PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_PLUGINPRIVATE);
CHECK_PP_OK_COMPLETIONPENDING(isolated_file_system_.Open(cb), OPEN_ERROR);
}
void CdmFileIOImpl::OnFileSystemOpened(int32_t result,
pp::FileSystem file_system) {
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ == STATE_OPENING_FILE_SYSTEM);
if (result != PP_OK) {
CDM_DLOG() << "File system open failed asynchronously.";
ReleaseFileLock();
state_ = STATE_ERROR;
OnError(OPEN_ERROR);
return;
}
file_system_ = file_system;
state_ = STATE_FILE_SYSTEM_OPENED;
client_->OnOpenComplete(cdm::FileIOClient::kSuccess);
}
void CdmFileIOImpl::OpenFileForRead() {
PP_DCHECK(state_ == STATE_READING);
PP_DCHECK(file_io_.is_null());
PP_DCHECK(file_ref_.is_null());
file_io_ = pp::FileIO(pp_instance_handle_);
file_ref_ = pp::FileRef(file_system_, file_name_.c_str());
// Open file for read. If file doesn't exist, PP_ERROR_FILENOTFOUND will be
// returned.
int32_t file_open_flag = PP_FILEOPENFLAG_READ;
pp::CompletionCallback cb =
callback_factory_.NewCallback(&CdmFileIOImpl::OnFileOpenedForRead);
CHECK_PP_OK_COMPLETIONPENDING(file_io_.Open(file_ref_, file_open_flag, cb),
READ_ERROR);
}
void CdmFileIOImpl::OnFileOpenedForRead(int32_t result) {
CDM_DLOG() << __FUNCTION__ << ": " << result;
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ == STATE_READING);
if (result != PP_OK && result != PP_ERROR_FILENOTFOUND) {
CDM_DLOG() << "File open failed.";
state_ = STATE_ERROR;
OnError(OPEN_ERROR);
return;
}
// File doesn't exist.
if (result == PP_ERROR_FILENOTFOUND) {
Reset();
state_ = STATE_FILE_SYSTEM_OPENED;
client_->OnReadComplete(cdm::FileIOClient::kSuccess, NULL, 0);
return;
}
ReadFile();
}
// Call sequence:
// fully read
// ReadFile() ---> OnFileRead() ------------> Done.
// ^ |
// | partially read |
// |----------------|
void CdmFileIOImpl::ReadFile() {
PP_DCHECK(state_ == STATE_READING);
PP_DCHECK(!io_buffer_.empty());
pp::CompletionCallback cb =
callback_factory_.NewCallback(&CdmFileIOImpl::OnFileRead);
CHECK_PP_OK_COMPLETIONPENDING(
file_io_.Read(io_offset_, &io_buffer_[0], io_buffer_.size(), cb),
READ_ERROR);
}
void CdmFileIOImpl::OnFileRead(int32_t bytes_read) {
CDM_DLOG() << __FUNCTION__ << ": " << bytes_read;
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ == STATE_READING);
// 0 |bytes_read| indicates end-of-file reached.
if (bytes_read < PP_OK) {
CDM_DLOG() << "Read file failed.";
state_ = STATE_ERROR;
OnError(READ_ERROR);
return;
}
PP_DCHECK(static_cast<size_t>(bytes_read) <= io_buffer_.size());
// Append |bytes_read| in |io_buffer_| to |cumulative_read_buffer_|.
cumulative_read_buffer_.insert(cumulative_read_buffer_.end(),
io_buffer_.begin(),
io_buffer_.begin() + bytes_read);
io_offset_ += bytes_read;
// Not received end-of-file yet. Keep reading.
if (bytes_read > 0) {
ReadFile();
return;
}
// We hit end-of-file. Return read data to the client.
// Clear |cumulative_read_buffer_| in case OnReadComplete() calls Read() or
// Write().
std::vector<char> local_buffer;
std::swap(cumulative_read_buffer_, local_buffer);
const uint8_t* data = local_buffer.empty() ?
NULL : reinterpret_cast<const uint8_t*>(&local_buffer[0]);
// Call this before OnReadComplete() so that we always have the latest file
// size before CDM fires errors.
if (!first_file_read_reported_) {
first_file_read_cb_.Run(local_buffer.size());
first_file_read_reported_ = true;
}
Reset();
state_ = STATE_FILE_SYSTEM_OPENED;
client_->OnReadComplete(
cdm::FileIOClient::kSuccess, data, local_buffer.size());
}
void CdmFileIOImpl::OpenTempFileForWrite() {
PP_DCHECK(state_ == STATE_WRITING);
PP_DCHECK(file_name_.size() > 1 && file_name_[0] == '/');
// Temporary file name format: /_<requested_file_name>
std::string temp_file_name = "/_" + file_name_.substr(1);
PP_DCHECK(file_io_.is_null());
PP_DCHECK(file_ref_.is_null());
file_io_ = pp::FileIO(pp_instance_handle_);
file_ref_ = pp::FileRef(file_system_, temp_file_name.c_str());
// Create the file if it doesn't exist. Truncate the file to length 0 if it
// exists.
// TODO(xhwang): Find a good way to report to UMA cases where the temporary
// file already exists (due to previous interruption or failure).
int32_t file_open_flag = PP_FILEOPENFLAG_WRITE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_CREATE;
pp::CompletionCallback cb =
callback_factory_.NewCallback(&CdmFileIOImpl::OnTempFileOpenedForWrite);
CHECK_PP_OK_COMPLETIONPENDING(
file_io_.Open(file_ref_, file_open_flag, cb), WRITE_ERROR);
}
void CdmFileIOImpl::OnTempFileOpenedForWrite(int32_t result) {
CDM_DLOG() << __FUNCTION__ << ": " << result;
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ == STATE_WRITING);
if (result != PP_OK) {
CDM_DLOG() << "Open temporary file failed.";
state_ = STATE_ERROR;
OnError(WRITE_ERROR);
return;
}
// We were told to write 0 bytes (to clear the file). In this case, there's
// no need to write anything.
if (io_buffer_.empty()) {
RenameTempFile();
return;
}
PP_DCHECK(io_offset_ == 0);
io_offset_ = 0;
WriteTempFile();
}
// Call sequence:
// fully written
// WriteTempFile() -> OnTempFileWritten() ---------------> RenameTempFile().
// ^ |
// | partially written |
// |---------------------|
void CdmFileIOImpl::WriteTempFile() {
PP_DCHECK(state_ == STATE_WRITING);
PP_DCHECK(io_offset_ < io_buffer_.size());
pp::CompletionCallback cb =
callback_factory_.NewCallback(&CdmFileIOImpl::OnTempFileWritten);
CHECK_PP_OK_COMPLETIONPENDING(file_io_.Write(io_offset_,
&io_buffer_[io_offset_],
io_buffer_.size() - io_offset_,
cb),
WRITE_ERROR);
}
void CdmFileIOImpl::OnTempFileWritten(int32_t bytes_written) {
CDM_DLOG() << __FUNCTION__ << ": " << bytes_written;
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ == STATE_WRITING);
if (bytes_written <= PP_OK) {
CDM_DLOG() << "Write temporary file failed.";
state_ = STATE_ERROR;
OnError(WRITE_ERROR);
return;
}
io_offset_ += bytes_written;
PP_DCHECK(io_offset_ <= io_buffer_.size());
if (io_offset_ < io_buffer_.size()) {
WriteTempFile();
return;
}
// All data written. Now rename the temporary file to the real file.
RenameTempFile();
}
void CdmFileIOImpl::RenameTempFile() {
PP_DCHECK(state_ == STATE_WRITING);
pp::CompletionCallback cb =
callback_factory_.NewCallback(&CdmFileIOImpl::OnTempFileRenamed);
CHECK_PP_OK_COMPLETIONPENDING(
file_ref_.Rename(pp::FileRef(file_system_, file_name_.c_str()), cb),
WRITE_ERROR);
}
void CdmFileIOImpl::OnTempFileRenamed(int32_t result) {
CDM_DLOG() << __FUNCTION__ << ": " << result;
PP_DCHECK(IsMainThread());
PP_DCHECK(state_ == STATE_WRITING);
if (result != PP_OK) {
CDM_DLOG() << "Rename temporary file failed.";
state_ = STATE_ERROR;
OnError(WRITE_ERROR);
return;
}
Reset();
state_ = STATE_FILE_SYSTEM_OPENED;
client_->OnWriteComplete(cdm::FileIOClient::kSuccess);
}
void CdmFileIOImpl::Reset() {
PP_DCHECK(IsMainThread());
io_buffer_.clear();
io_offset_ = 0;
cumulative_read_buffer_.clear();
file_io_.Close();
file_io_ = pp::FileIO();
file_ref_ = pp::FileRef();
}
void CdmFileIOImpl::OnError(ErrorType error_type) {
// For *_WHILE_IN_USE errors, do not reset these values. Otherwise, the
// existing read/write operation will fail.
if (error_type == READ_ERROR || error_type == WRITE_ERROR)
Reset();
PostOnMain(callback_factory_.NewCallback(&CdmFileIOImpl::NotifyClientOfError,
error_type));
}
void CdmFileIOImpl::NotifyClientOfError(int32_t result,
ErrorType error_type) {
PP_DCHECK(result == PP_OK);
switch (error_type) {
case OPEN_ERROR:
client_->OnOpenComplete(cdm::FileIOClient::kError);
break;
case READ_ERROR:
client_->OnReadComplete(cdm::FileIOClient::kError, NULL, 0);
break;
case WRITE_ERROR:
client_->OnWriteComplete(cdm::FileIOClient::kError);
break;
case OPEN_WHILE_IN_USE:
client_->OnOpenComplete(cdm::FileIOClient::kInUse);
break;
case READ_WHILE_IN_USE:
client_->OnReadComplete(cdm::FileIOClient::kInUse, NULL, 0);
break;
case WRITE_WHILE_IN_USE:
client_->OnWriteComplete(cdm::FileIOClient::kInUse);
break;
}
}
} // namespace media