OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/file_io_resource.h" | 5 #include "ppapi/proxy/file_io_resource.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "ipc/ipc_message.h" | 8 #include "ipc/ipc_message.h" |
9 #include "ppapi/c/pp_errors.h" | 9 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/proxy/ppapi_messages.h" | 10 #include "ppapi/proxy/ppapi_messages.h" |
11 #include "ppapi/shared_impl/array_writer.h" | 11 #include "ppapi/shared_impl/array_writer.h" |
12 #include "ppapi/shared_impl/file_type_conversion.h" | 12 #include "ppapi/shared_impl/file_type_conversion.h" |
13 #include "ppapi/shared_impl/ppapi_globals.h" | 13 #include "ppapi/shared_impl/ppapi_globals.h" |
14 #include "ppapi/shared_impl/proxy_lock.h" | 14 #include "ppapi/shared_impl/proxy_lock.h" |
15 #include "ppapi/shared_impl/resource_tracker.h" | 15 #include "ppapi/shared_impl/resource_tracker.h" |
16 #include "ppapi/thunk/enter.h" | 16 #include "ppapi/thunk/enter.h" |
17 #include "ppapi/thunk/ppb_file_ref_api.h" | 17 #include "ppapi/thunk/ppb_file_ref_api.h" |
18 | 18 |
19 using ppapi::thunk::EnterResourceNoLock; | 19 using ppapi::thunk::EnterResourceNoLock; |
20 using ppapi::thunk::PPB_FileIO_API; | 20 using ppapi::thunk::PPB_FileIO_API; |
21 using ppapi::thunk::PPB_FileRef_API; | 21 using ppapi::thunk::PPB_FileRef_API; |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // We must allocate a buffer sized according to the request of the plugin. To | |
26 // reduce the chance of out-of-memory errors, we cap the read size to 32MB. | |
27 // This is OK since the API specifies that it may perform a partial read. | |
28 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB | |
29 | |
25 // An adapter to let Read() share the same implementation with ReadToArray(). | 30 // An adapter to let Read() share the same implementation with ReadToArray(). |
26 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { | 31 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
27 return user_data; | 32 return user_data; |
28 } | 33 } |
29 | 34 |
30 // File thread task to close the file handle. | 35 // File thread task to close the file handle. |
31 void DoClose(base::PlatformFile file) { | 36 void DoClose(base::PlatformFile file) { |
32 base::ClosePlatformFile(file); | 37 base::ClosePlatformFile(file); |
33 } | 38 } |
34 | 39 |
35 } // namespace | 40 } // namespace |
36 | 41 |
37 namespace ppapi { | 42 namespace ppapi { |
38 namespace proxy { | 43 namespace proxy { |
39 | 44 |
45 FileIOResource::ReadData::ReadData(int32_t bytes_to_read) | |
46 : buffer_(new char[bytes_to_read]), | |
47 bytes_read_(0) { | |
48 } | |
49 | |
50 FileIOResource::ReadData::~ReadData() { | |
51 } | |
52 | |
40 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) | 53 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
41 : PluginResource(connection, instance), | 54 : PluginResource(connection, instance), |
42 file_handle_(base::kInvalidPlatformFileValue), | 55 file_handle_(base::kInvalidPlatformFileValue), |
43 file_system_type_(PP_FILESYSTEMTYPE_INVALID) { | 56 file_system_type_(PP_FILESYSTEMTYPE_INVALID), |
57 query_result_(0) { | |
44 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); | 58 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); |
45 } | 59 } |
46 | 60 |
47 FileIOResource::~FileIOResource() { | 61 FileIOResource::~FileIOResource() { |
48 CloseFileHandle(); | 62 CloseFileHandle(); |
49 } | 63 } |
50 | 64 |
51 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { | 65 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { |
52 return this; | 66 return this; |
53 } | 67 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 99 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
86 return PP_OK_COMPLETIONPENDING; | 100 return PP_OK_COMPLETIONPENDING; |
87 } | 101 } |
88 | 102 |
89 int32_t FileIOResource::Query(PP_FileInfo* info, | 103 int32_t FileIOResource::Query(PP_FileInfo* info, |
90 scoped_refptr<TrackedCallback> callback) { | 104 scoped_refptr<TrackedCallback> callback) { |
91 int32_t rv = state_manager_.CheckOperationState( | 105 int32_t rv = state_manager_.CheckOperationState( |
92 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 106 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
93 if (rv != PP_OK) | 107 if (rv != PP_OK) |
94 return rv; | 108 return rv; |
109 if (!info) | |
110 return PP_ERROR_BADARGUMENT; | |
111 if (file_handle_ == base::kInvalidPlatformFileValue) | |
112 return PP_ERROR_FAILED; | |
95 | 113 |
96 if (callback->is_blocking() && | 114 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
97 file_handle_ != base::kInvalidPlatformFileValue) { | 115 |
98 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 116 // If the callback is blocking, perform the task on the calling thread. |
99 int32_t result = PP_ERROR_FAILED; | 117 if (callback->is_blocking()) { |
100 base::PlatformFileInfo file_info; | |
101 // Release the proxy lock while making a potentially blocking file call. | |
102 { | 118 { |
119 // Release the proxy lock while making a potentially slow file call. | |
103 ProxyAutoUnlock unlock; | 120 ProxyAutoUnlock unlock; |
104 result = base::GetPlatformFileInfo(file_handle_, &file_info) ? | 121 DoQuery(); |
105 PP_OK : PP_ERROR_FAILED; | |
106 } | 122 } |
107 if ((result == PP_OK) && TrackedCallback::IsPending(callback)) { | 123 return OnQueryComplete(info, PP_OK); |
dmichael (off chromium)
2013/08/09 17:37:42
I think you could pass the result of the query to
bbudge
2013/08/09 21:43:24
Done.
| |
108 ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type_, | |
109 info); | |
110 } | |
111 | |
112 state_manager_.SetOperationFinished(); | |
113 return result; | |
114 } | 124 } |
115 | 125 |
116 Call<PpapiPluginMsg_FileIO_QueryReply>(RENDERER, | 126 // For the non-blocking case, post a task to the file thread. |
117 PpapiHostMsg_FileIO_Query(), | 127 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance())->PostTaskAndReply( |
118 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, | 128 FROM_HERE, |
119 callback, info)); | 129 Bind(&FileIOResource::DoQuery, this), |
130 RunWhileLocked( | |
131 Bind(&FileIOResource::OnFileTaskComplete, this, callback))); | |
132 callback->set_completion_task( | |
133 Bind(&FileIOResource::OnQueryComplete, this, info)); | |
120 | 134 |
121 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
122 return PP_OK_COMPLETIONPENDING; | 135 return PP_OK_COMPLETIONPENDING; |
123 } | 136 } |
124 | 137 |
125 int32_t FileIOResource::Touch(PP_Time last_access_time, | 138 int32_t FileIOResource::Touch(PP_Time last_access_time, |
126 PP_Time last_modified_time, | 139 PP_Time last_modified_time, |
127 scoped_refptr<TrackedCallback> callback) { | 140 scoped_refptr<TrackedCallback> callback) { |
128 int32_t rv = state_manager_.CheckOperationState( | 141 int32_t rv = state_manager_.CheckOperationState( |
129 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 142 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
130 if (rv != PP_OK) | 143 if (rv != PP_OK) |
131 return rv; | 144 return rv; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, callback)); | 280 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, callback)); |
268 | 281 |
269 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 282 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
270 return PP_OK_COMPLETIONPENDING; | 283 return PP_OK_COMPLETIONPENDING; |
271 } | 284 } |
272 | 285 |
273 int32_t FileIOResource::ReadValidated(int64_t offset, | 286 int32_t FileIOResource::ReadValidated(int64_t offset, |
274 int32_t bytes_to_read, | 287 int32_t bytes_to_read, |
275 const PP_ArrayOutput& array_output, | 288 const PP_ArrayOutput& array_output, |
276 scoped_refptr<TrackedCallback> callback) { | 289 scoped_refptr<TrackedCallback> callback) { |
290 if (bytes_to_read < 0) | |
291 return PP_ERROR_FAILED; | |
292 if (file_handle_ == base::kInvalidPlatformFileValue) | |
293 return PP_ERROR_FAILED; | |
294 | |
277 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | 295 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); |
278 | 296 |
279 if (callback->is_blocking() && | 297 bytes_to_read = std::min(bytes_to_read, kMaxReadSize); |
280 file_handle_ != base::kInvalidPlatformFileValue) { | 298 scoped_refptr<ReadData> read_data(new ReadData(bytes_to_read)); |
dmichael (off chromium)
2013/08/09 17:37:42
It would be better to create an empty ReadData her
bbudge
2013/08/09 21:43:24
It would be messy, as the allocation has to happen
bbudge
2013/08/09 21:58:14
Turns out I was needlessly worried about allocatin
| |
281 int32_t result = PP_ERROR_FAILED; | 299 if (callback->is_blocking()) { |
282 if (bytes_to_read >= 0) { | 300 // Release the proxy lock while making a potentially slow file call. |
283 // We need a buffer (and therefore we must copy) since we don't know until | 301 { |
284 // reacquire the lock whether to write data to the plugin's buffer. | 302 ProxyAutoUnlock unlock; |
285 scoped_ptr<char[]> buffer; | 303 DoRead(offset, bytes_to_read, read_data); |
dmichael (off chromium)
2013/08/09 17:37:42
I'm thinking maybe DoRead can return the result (i
bbudge
2013/08/09 21:43:24
Done.
| |
286 // Release the proxy lock while making a potentially blocking file call. | |
287 { | |
288 ProxyAutoUnlock unlock; | |
289 buffer.reset(new char[bytes_to_read]); | |
290 int32_t bytes_read = base::ReadPlatformFile( | |
291 file_handle_, offset, buffer.get(), bytes_to_read); | |
292 result = (bytes_read < 0) ? PP_ERROR_FAILED : bytes_read; | |
293 } | |
294 if ((result >= 0) && TrackedCallback::IsPending(callback)) { | |
295 ArrayWriter output; | |
296 output.set_pp_array_output(array_output); | |
297 if (output.is_valid()) | |
298 output.StoreArray(buffer.get(), result); | |
299 else | |
300 result = PP_ERROR_FAILED; | |
301 } | |
302 } | 304 } |
303 | 305 return OnReadComplete(read_data, array_output, PP_OK); |
dmichael (off chromium)
2013/08/09 17:37:42
would it work to pass read_data->bytes_read() inst
bbudge
2013/08/09 21:43:24
Done.
| |
304 state_manager_.SetOperationFinished(); | |
305 return result; | |
306 } | 306 } |
307 | 307 |
308 Call<PpapiPluginMsg_FileIO_ReadReply>(RENDERER, | 308 // For the non-blocking case, post a task to the file thread. |
309 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), | 309 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance())->PostTaskAndReply( |
310 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, | 310 FROM_HERE, |
311 callback, array_output)); | 311 Bind(&FileIOResource::DoRead, this, offset, bytes_to_read, read_data), |
312 RunWhileLocked( | |
313 Bind(&FileIOResource::OnFileTaskComplete, this, callback))); | |
dmichael (off chromium)
2013/08/09 17:37:42
If you make OnFileTaskComplete take a result param
bbudge
2013/08/09 21:43:24
Done.
| |
314 callback->set_completion_task( | |
315 Bind(&FileIOResource::OnReadComplete, this, read_data, array_output)); | |
dmichael (off chromium)
2013/08/09 17:37:42
If you are able to remove the "result" part of Rea
bbudge
2013/08/09 21:43:24
I couldn't get this to work, since in the abort ca
| |
316 | |
312 return PP_OK_COMPLETIONPENDING; | 317 return PP_OK_COMPLETIONPENDING; |
313 } | 318 } |
314 | 319 |
315 void FileIOResource::CloseFileHandle() { | 320 void FileIOResource::CloseFileHandle() { |
316 if (file_handle_ != base::kInvalidPlatformFileValue) { | 321 if (file_handle_ != base::kInvalidPlatformFileValue) { |
317 // Close our local fd on the file thread. | 322 // Close our local fd on the file thread. |
318 base::TaskRunner* file_task_runner = | 323 base::TaskRunner* file_task_runner = |
319 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()); | 324 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()); |
320 file_task_runner->PostTask(FROM_HERE, | 325 file_task_runner->PostTask(FROM_HERE, |
321 base::Bind(&DoClose, file_handle_)); | 326 base::Bind(&DoClose, file_handle_)); |
322 | 327 |
323 file_handle_ = base::kInvalidPlatformFileValue; | 328 file_handle_ = base::kInvalidPlatformFileValue; |
324 } | 329 } |
325 } | 330 } |
326 | 331 |
332 void FileIOResource::DoQuery() { | |
333 query_result_ = base::GetPlatformFileInfo(file_handle_, &file_info_) ? | |
334 PP_OK : PP_ERROR_FAILED; | |
335 } | |
336 | |
337 void FileIOResource::DoRead(int64_t offset, | |
338 int32_t bytes_to_read, | |
339 scoped_refptr<ReadData> read_data) { | |
340 read_data->set_bytes_read(base::ReadPlatformFile( | |
341 file_handle_, offset, read_data->buffer(), bytes_to_read)); | |
342 } | |
343 | |
344 void FileIOResource::OnFileTaskComplete( | |
345 scoped_refptr<TrackedCallback> callback) { | |
346 callback->Run(PP_OK); | |
dmichael (off chromium)
2013/08/09 17:37:42
I think OnFileTaskComplete should take a result pa
bbudge
2013/08/09 21:43:24
Done.
| |
347 } | |
348 | |
349 int32_t FileIOResource::OnQueryComplete(PP_FileInfo* info, int32_t result) { | |
350 DCHECK(state_manager_.get_pending_operation() == | |
351 FileIOStateManager::OPERATION_EXCLUSIVE); | |
352 | |
353 if (result == PP_OK && query_result_ == PP_OK) { | |
354 ppapi::PlatformFileInfoToPepperFileInfo(file_info_, file_system_type_, | |
355 info); | |
356 } | |
357 state_manager_.SetOperationFinished(); | |
358 return result; | |
dmichael (off chromium)
2013/08/09 17:37:42
Given the way this is all structured currently, I
bbudge
2013/08/09 21:43:24
Done.
| |
359 } | |
360 | |
361 int32_t FileIOResource::OnReadComplete(scoped_refptr<ReadData> read_data, | |
362 PP_ArrayOutput array_output, | |
363 int32_t result) { | |
364 DCHECK(state_manager_.get_pending_operation() == | |
365 FileIOStateManager::OPERATION_READ); | |
366 if (result == PP_OK) { | |
367 result = read_data->bytes_read(); | |
368 if (result >= 0) { | |
369 ArrayWriter output; | |
370 output.set_pp_array_output(array_output); | |
371 if (output.is_valid()) | |
372 output.StoreArray(read_data->buffer(), result); | |
373 else | |
374 result = PP_ERROR_FAILED; | |
375 } else { | |
376 // The read operation failed. | |
377 result = PP_ERROR_FAILED; | |
378 } | |
379 } | |
380 state_manager_.SetOperationFinished(); | |
381 return result; | |
382 } | |
383 | |
327 void FileIOResource::OnPluginMsgGeneralComplete( | 384 void FileIOResource::OnPluginMsgGeneralComplete( |
328 scoped_refptr<TrackedCallback> callback, | 385 scoped_refptr<TrackedCallback> callback, |
329 const ResourceMessageReplyParams& params) { | 386 const ResourceMessageReplyParams& params) { |
330 DCHECK(state_manager_.get_pending_operation() == | 387 DCHECK(state_manager_.get_pending_operation() == |
331 FileIOStateManager::OPERATION_EXCLUSIVE || | 388 FileIOStateManager::OPERATION_EXCLUSIVE || |
332 state_manager_.get_pending_operation() == | 389 state_manager_.get_pending_operation() == |
333 FileIOStateManager::OPERATION_WRITE); | 390 FileIOStateManager::OPERATION_WRITE); |
334 // End this operation now, so the user's callback can execute another FileIO | 391 // End this operation now, so the user's callback can execute another FileIO |
335 // operation, assuming there are no other pending operations. | 392 // operation, assuming there are no other pending operations. |
336 state_manager_.SetOperationFinished(); | 393 state_manager_.SetOperationFinished(); |
(...skipping 11 matching lines...) Expand all Loading... | |
348 int32_t result = params.result(); | 405 int32_t result = params.result(); |
349 IPC::PlatformFileForTransit transit_file; | 406 IPC::PlatformFileForTransit transit_file; |
350 if ((result == PP_OK) && params.TakeFileHandleAtIndex(0, &transit_file)) | 407 if ((result == PP_OK) && params.TakeFileHandleAtIndex(0, &transit_file)) |
351 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 408 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
352 // End this operation now, so the user's callback can execute another FileIO | 409 // End this operation now, so the user's callback can execute another FileIO |
353 // operation, assuming there are no other pending operations. | 410 // operation, assuming there are no other pending operations. |
354 state_manager_.SetOperationFinished(); | 411 state_manager_.SetOperationFinished(); |
355 callback->Run(result); | 412 callback->Run(result); |
356 } | 413 } |
357 | 414 |
358 void FileIOResource::OnPluginMsgQueryComplete( | |
359 scoped_refptr<TrackedCallback> callback, | |
360 PP_FileInfo* output_info, | |
361 const ResourceMessageReplyParams& params, | |
362 const PP_FileInfo& info) { | |
363 DCHECK(state_manager_.get_pending_operation() == | |
364 FileIOStateManager::OPERATION_EXCLUSIVE); | |
365 *output_info = info; | |
366 // End this operation now, so the user's callback can execute another FileIO | |
367 // operation, assuming there are no other pending operations. | |
368 state_manager_.SetOperationFinished(); | |
369 callback->Run(params.result()); | |
370 } | |
371 | |
372 void FileIOResource::OnPluginMsgReadComplete( | |
373 scoped_refptr<TrackedCallback> callback, | |
374 PP_ArrayOutput array_output, | |
375 const ResourceMessageReplyParams& params, | |
376 const std::string& data) { | |
377 DCHECK(state_manager_.get_pending_operation() == | |
378 FileIOStateManager::OPERATION_READ); | |
379 | |
380 // The result code should contain the data size if it's positive. | |
381 int32_t result = params.result(); | |
382 DCHECK((result < 0 && data.size() == 0) || | |
383 result == static_cast<int32_t>(data.size())); | |
384 | |
385 ArrayWriter output; | |
386 output.set_pp_array_output(array_output); | |
387 if (output.is_valid()) | |
388 output.StoreArray(data.data(), std::max(0, result)); | |
389 else | |
390 result = PP_ERROR_FAILED; | |
391 | |
392 // End this operation now, so the user's callback can execute another FileIO | |
393 // operation, assuming there are no other pending operations. | |
394 state_manager_.SetOperationFinished(); | |
395 callback->Run(result); | |
396 } | |
397 | |
398 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( | 415 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( |
399 scoped_refptr<TrackedCallback> callback, | 416 scoped_refptr<TrackedCallback> callback, |
400 PP_FileHandle* output_handle, | 417 PP_FileHandle* output_handle, |
401 const ResourceMessageReplyParams& params) { | 418 const ResourceMessageReplyParams& params) { |
402 DCHECK(state_manager_.get_pending_operation() == | 419 DCHECK(state_manager_.get_pending_operation() == |
403 FileIOStateManager::OPERATION_EXCLUSIVE); | 420 FileIOStateManager::OPERATION_EXCLUSIVE); |
404 | 421 |
405 if (!TrackedCallback::IsPending(callback)) { | 422 if (!TrackedCallback::IsPending(callback)) { |
406 state_manager_.SetOperationFinished(); | 423 state_manager_.SetOperationFinished(); |
407 return; | 424 return; |
408 } | 425 } |
409 | 426 |
410 int32_t result = params.result(); | 427 int32_t result = params.result(); |
411 IPC::PlatformFileForTransit transit_file; | 428 IPC::PlatformFileForTransit transit_file; |
412 if (!params.TakeFileHandleAtIndex(0, &transit_file)) | 429 if (!params.TakeFileHandleAtIndex(0, &transit_file)) |
413 result = PP_ERROR_FAILED; | 430 result = PP_ERROR_FAILED; |
414 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 431 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
415 | 432 |
416 // End this operation now, so the user's callback can execute another FileIO | 433 // End this operation now, so the user's callback can execute another FileIO |
417 // operation, assuming there are no other pending operations. | 434 // operation, assuming there are no other pending operations. |
418 state_manager_.SetOperationFinished(); | 435 state_manager_.SetOperationFinished(); |
419 callback->Run(result); | 436 callback->Run(result); |
420 } | 437 } |
421 | 438 |
422 } // namespace proxy | 439 } // namespace proxy |
423 } // namespace ppapi | 440 } // namespace ppapi |
OLD | NEW |