| 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 "base/task_runner_util.h" |
| 8 #include "ipc/ipc_message.h" | 9 #include "ipc/ipc_message.h" |
| 9 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 10 #include "ppapi/proxy/ppapi_messages.h" | 11 #include "ppapi/proxy/ppapi_messages.h" |
| 11 #include "ppapi/shared_impl/array_writer.h" | 12 #include "ppapi/shared_impl/array_writer.h" |
| 12 #include "ppapi/shared_impl/file_type_conversion.h" | 13 #include "ppapi/shared_impl/file_type_conversion.h" |
| 13 #include "ppapi/shared_impl/ppapi_globals.h" | 14 #include "ppapi/shared_impl/ppapi_globals.h" |
| 14 #include "ppapi/shared_impl/proxy_lock.h" | 15 #include "ppapi/shared_impl/proxy_lock.h" |
| 15 #include "ppapi/shared_impl/resource_tracker.h" | 16 #include "ppapi/shared_impl/resource_tracker.h" |
| 16 #include "ppapi/thunk/enter.h" | 17 #include "ppapi/thunk/enter.h" |
| 17 #include "ppapi/thunk/ppb_file_ref_api.h" | 18 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 18 | 19 |
| 19 using ppapi::thunk::EnterResourceNoLock; | 20 using ppapi::thunk::EnterResourceNoLock; |
| 20 using ppapi::thunk::PPB_FileIO_API; | 21 using ppapi::thunk::PPB_FileIO_API; |
| 21 using ppapi::thunk::PPB_FileRef_API; | 22 using ppapi::thunk::PPB_FileRef_API; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 26 // We must allocate a buffer sized according to the request of the plugin. To |
| 27 // reduce the chance of out-of-memory errors, we cap the read size to 32MB. |
| 28 // This is OK since the API specifies that it may perform a partial read. |
| 29 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB |
| 30 |
| 25 // An adapter to let Read() share the same implementation with ReadToArray(). | 31 // An adapter to let Read() share the same implementation with ReadToArray(). |
| 26 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { | 32 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
| 27 return user_data; | 33 return user_data; |
| 28 } | 34 } |
| 29 | 35 |
| 30 // File thread task to close the file handle. | 36 // File thread task to close the file handle. |
| 31 void DoClose(base::PlatformFile file) { | 37 void DoClose(base::PlatformFile file) { |
| 32 base::ClosePlatformFile(file); | 38 base::ClosePlatformFile(file); |
| 33 } | 39 } |
| 34 | 40 |
| 35 } // namespace | 41 } // namespace |
| 36 | 42 |
| 37 namespace ppapi { | 43 namespace ppapi { |
| 38 namespace proxy { | 44 namespace proxy { |
| 39 | 45 |
| 46 FileIOResource::QueryOp::QueryOp(PP_FileHandle file_handle) |
| 47 : file_handle_(file_handle) { |
| 48 } |
| 49 |
| 50 FileIOResource::QueryOp::~QueryOp() { |
| 51 } |
| 52 |
| 53 int32_t FileIOResource::QueryOp::DoWork() { |
| 54 return base::GetPlatformFileInfo(file_handle_, &file_info_) ? |
| 55 PP_OK : PP_ERROR_FAILED; |
| 56 } |
| 57 |
| 58 FileIOResource::ReadOp::ReadOp(PP_FileHandle file_handle, |
| 59 int64_t offset, |
| 60 int32_t bytes_to_read) |
| 61 : file_handle_(file_handle), |
| 62 offset_(offset), |
| 63 bytes_to_read_(bytes_to_read) { |
| 64 } |
| 65 |
| 66 FileIOResource::ReadOp::~ReadOp() { |
| 67 } |
| 68 |
| 69 int32_t FileIOResource::ReadOp::DoWork() { |
| 70 DCHECK(!buffer_.get()); |
| 71 buffer_.reset(new char[bytes_to_read_]); |
| 72 return base::ReadPlatformFile( |
| 73 file_handle_, offset_, buffer_.get(), bytes_to_read_); |
| 74 } |
| 75 |
| 40 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) | 76 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
| 41 : PluginResource(connection, instance), | 77 : PluginResource(connection, instance), |
| 42 file_handle_(base::kInvalidPlatformFileValue), | 78 file_handle_(base::kInvalidPlatformFileValue), |
| 43 file_system_type_(PP_FILESYSTEMTYPE_INVALID) { | 79 file_system_type_(PP_FILESYSTEMTYPE_INVALID) { |
| 44 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); | 80 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); |
| 45 } | 81 } |
| 46 | 82 |
| 47 FileIOResource::~FileIOResource() { | 83 FileIOResource::~FileIOResource() { |
| 48 CloseFileHandle(); | 84 CloseFileHandle(); |
| 49 } | 85 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 121 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 86 return PP_OK_COMPLETIONPENDING; | 122 return PP_OK_COMPLETIONPENDING; |
| 87 } | 123 } |
| 88 | 124 |
| 89 int32_t FileIOResource::Query(PP_FileInfo* info, | 125 int32_t FileIOResource::Query(PP_FileInfo* info, |
| 90 scoped_refptr<TrackedCallback> callback) { | 126 scoped_refptr<TrackedCallback> callback) { |
| 91 int32_t rv = state_manager_.CheckOperationState( | 127 int32_t rv = state_manager_.CheckOperationState( |
| 92 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 128 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 93 if (rv != PP_OK) | 129 if (rv != PP_OK) |
| 94 return rv; | 130 return rv; |
| 131 if (!info) |
| 132 return PP_ERROR_BADARGUMENT; |
| 133 if (file_handle_ == base::kInvalidPlatformFileValue) |
| 134 return PP_ERROR_FAILED; |
| 95 | 135 |
| 96 if (callback->is_blocking() && | 136 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 97 file_handle_ != base::kInvalidPlatformFileValue) { | 137 scoped_refptr<QueryOp> query_op(new QueryOp(file_handle_)); |
| 98 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 138 |
| 99 int32_t result = PP_ERROR_FAILED; | 139 // If the callback is blocking, perform the task on the calling thread. |
| 100 base::PlatformFileInfo file_info; | 140 if (callback->is_blocking()) { |
| 101 // Release the proxy lock while making a potentially blocking file call. | 141 int32_t result; |
| 102 { | 142 { |
| 143 // Release the proxy lock while making a potentially slow file call. |
| 103 ProxyAutoUnlock unlock; | 144 ProxyAutoUnlock unlock; |
| 104 result = base::GetPlatformFileInfo(file_handle_, &file_info) ? | 145 result = query_op->DoWork(); |
| 105 PP_OK : PP_ERROR_FAILED; | |
| 106 } | 146 } |
| 107 if ((result == PP_OK) && TrackedCallback::IsPending(callback)) { | 147 return OnQueryComplete(query_op, info, result); |
| 108 ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type_, | |
| 109 info); | |
| 110 } | |
| 111 | |
| 112 state_manager_.SetOperationFinished(); | |
| 113 return result; | |
| 114 } | 148 } |
| 115 | 149 |
| 116 Call<PpapiPluginMsg_FileIO_QueryReply>(RENDERER, | 150 // For the non-blocking case, post a task to the file thread and add a |
| 117 PpapiHostMsg_FileIO_Query(), | 151 // completion task to write the result. |
| 118 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, | 152 base::PostTaskAndReplyWithResult( |
| 119 callback, info)); | 153 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()), |
| 154 FROM_HERE, |
| 155 Bind(&FileIOResource::QueryOp::DoWork, query_op), |
| 156 RunWhileLocked(Bind(&TrackedCallback::Run, callback))); |
| 157 callback->set_completion_task( |
| 158 Bind(&FileIOResource::OnQueryComplete, this, query_op, info)); |
| 120 | 159 |
| 121 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 122 return PP_OK_COMPLETIONPENDING; | 160 return PP_OK_COMPLETIONPENDING; |
| 123 } | 161 } |
| 124 | 162 |
| 125 int32_t FileIOResource::Touch(PP_Time last_access_time, | 163 int32_t FileIOResource::Touch(PP_Time last_access_time, |
| 126 PP_Time last_modified_time, | 164 PP_Time last_modified_time, |
| 127 scoped_refptr<TrackedCallback> callback) { | 165 scoped_refptr<TrackedCallback> callback) { |
| 128 int32_t rv = state_manager_.CheckOperationState( | 166 int32_t rv = state_manager_.CheckOperationState( |
| 129 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 167 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 130 if (rv != PP_OK) | 168 if (rv != PP_OK) |
| 131 return rv; | 169 return rv; |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, callback)); | 305 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, callback)); |
| 268 | 306 |
| 269 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 307 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 270 return PP_OK_COMPLETIONPENDING; | 308 return PP_OK_COMPLETIONPENDING; |
| 271 } | 309 } |
| 272 | 310 |
| 273 int32_t FileIOResource::ReadValidated(int64_t offset, | 311 int32_t FileIOResource::ReadValidated(int64_t offset, |
| 274 int32_t bytes_to_read, | 312 int32_t bytes_to_read, |
| 275 const PP_ArrayOutput& array_output, | 313 const PP_ArrayOutput& array_output, |
| 276 scoped_refptr<TrackedCallback> callback) { | 314 scoped_refptr<TrackedCallback> callback) { |
| 315 if (bytes_to_read < 0) |
| 316 return PP_ERROR_FAILED; |
| 317 if (file_handle_ == base::kInvalidPlatformFileValue) |
| 318 return PP_ERROR_FAILED; |
| 319 |
| 277 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | 320 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); |
| 278 | 321 |
| 279 if (callback->is_blocking() && | 322 bytes_to_read = std::min(bytes_to_read, kMaxReadSize); |
| 280 file_handle_ != base::kInvalidPlatformFileValue) { | 323 scoped_refptr<ReadOp> read_op( |
| 281 int32_t result = PP_ERROR_FAILED; | 324 new ReadOp(file_handle_, offset, bytes_to_read)); |
| 282 if (bytes_to_read >= 0) { | 325 if (callback->is_blocking()) { |
| 283 // We need a buffer (and therefore we must copy) since we don't know until | 326 int32_t result; |
| 284 // reacquire the lock whether to write data to the plugin's buffer. | 327 { |
| 285 scoped_ptr<char[]> buffer; | 328 // Release the proxy lock while making a potentially slow file call. |
| 286 // Release the proxy lock while making a potentially blocking file call. | 329 ProxyAutoUnlock unlock; |
| 287 { | 330 result = read_op->DoWork(); |
| 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 } | 331 } |
| 303 | 332 return OnReadComplete(read_op, array_output, result); |
| 304 state_manager_.SetOperationFinished(); | |
| 305 return result; | |
| 306 } | 333 } |
| 307 | 334 |
| 308 Call<PpapiPluginMsg_FileIO_ReadReply>(RENDERER, | 335 // For the non-blocking case, post a task to the file thread. |
| 309 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), | 336 base::PostTaskAndReplyWithResult( |
| 310 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, | 337 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()), |
| 311 callback, array_output)); | 338 FROM_HERE, |
| 339 Bind(&FileIOResource::ReadOp::DoWork, read_op), |
| 340 RunWhileLocked(Bind(&TrackedCallback::Run, callback))); |
| 341 callback->set_completion_task( |
| 342 Bind(&FileIOResource::OnReadComplete, this, read_op, array_output)); |
| 343 |
| 312 return PP_OK_COMPLETIONPENDING; | 344 return PP_OK_COMPLETIONPENDING; |
| 313 } | 345 } |
| 314 | 346 |
| 315 void FileIOResource::CloseFileHandle() { | 347 void FileIOResource::CloseFileHandle() { |
| 316 if (file_handle_ != base::kInvalidPlatformFileValue) { | 348 if (file_handle_ != base::kInvalidPlatformFileValue) { |
| 317 // Close our local fd on the file thread. | 349 // Close our local fd on the file thread. |
| 318 base::TaskRunner* file_task_runner = | 350 base::TaskRunner* file_task_runner = |
| 319 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()); | 351 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()); |
| 320 file_task_runner->PostTask(FROM_HERE, | 352 file_task_runner->PostTask(FROM_HERE, |
| 321 base::Bind(&DoClose, file_handle_)); | 353 base::Bind(&DoClose, file_handle_)); |
| 322 | 354 |
| 323 file_handle_ = base::kInvalidPlatformFileValue; | 355 file_handle_ = base::kInvalidPlatformFileValue; |
| 324 } | 356 } |
| 325 } | 357 } |
| 326 | 358 |
| 359 int32_t FileIOResource::OnQueryComplete(scoped_refptr<QueryOp> query_op, |
| 360 PP_FileInfo* info, |
| 361 int32_t result) { |
| 362 DCHECK(state_manager_.get_pending_operation() == |
| 363 FileIOStateManager::OPERATION_EXCLUSIVE); |
| 364 |
| 365 if (result == PP_OK) { |
| 366 // This writes the file info into the plugin's PP_FileInfo struct. |
| 367 ppapi::PlatformFileInfoToPepperFileInfo(query_op->file_info(), |
| 368 file_system_type_, |
| 369 info); |
| 370 } |
| 371 state_manager_.SetOperationFinished(); |
| 372 return result; |
| 373 } |
| 374 |
| 375 int32_t FileIOResource::OnReadComplete(scoped_refptr<ReadOp> read_op, |
| 376 PP_ArrayOutput array_output, |
| 377 int32_t result) { |
| 378 DCHECK(state_manager_.get_pending_operation() == |
| 379 FileIOStateManager::OPERATION_READ); |
| 380 if (result >= 0) { |
| 381 ArrayWriter output; |
| 382 output.set_pp_array_output(array_output); |
| 383 if (output.is_valid()) |
| 384 output.StoreArray(read_op->buffer(), result); |
| 385 else |
| 386 result = PP_ERROR_FAILED; |
| 387 } else { |
| 388 // The read operation failed. |
| 389 result = PP_ERROR_FAILED; |
| 390 } |
| 391 state_manager_.SetOperationFinished(); |
| 392 return result; |
| 393 } |
| 394 |
| 327 void FileIOResource::OnPluginMsgGeneralComplete( | 395 void FileIOResource::OnPluginMsgGeneralComplete( |
| 328 scoped_refptr<TrackedCallback> callback, | 396 scoped_refptr<TrackedCallback> callback, |
| 329 const ResourceMessageReplyParams& params) { | 397 const ResourceMessageReplyParams& params) { |
| 330 DCHECK(state_manager_.get_pending_operation() == | 398 DCHECK(state_manager_.get_pending_operation() == |
| 331 FileIOStateManager::OPERATION_EXCLUSIVE || | 399 FileIOStateManager::OPERATION_EXCLUSIVE || |
| 332 state_manager_.get_pending_operation() == | 400 state_manager_.get_pending_operation() == |
| 333 FileIOStateManager::OPERATION_WRITE); | 401 FileIOStateManager::OPERATION_WRITE); |
| 334 // End this operation now, so the user's callback can execute another FileIO | 402 // End this operation now, so the user's callback can execute another FileIO |
| 335 // operation, assuming there are no other pending operations. | 403 // operation, assuming there are no other pending operations. |
| 336 state_manager_.SetOperationFinished(); | 404 state_manager_.SetOperationFinished(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 348 int32_t result = params.result(); | 416 int32_t result = params.result(); |
| 349 IPC::PlatformFileForTransit transit_file; | 417 IPC::PlatformFileForTransit transit_file; |
| 350 if ((result == PP_OK) && params.TakeFileHandleAtIndex(0, &transit_file)) | 418 if ((result == PP_OK) && params.TakeFileHandleAtIndex(0, &transit_file)) |
| 351 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 419 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
| 352 // End this operation now, so the user's callback can execute another FileIO | 420 // End this operation now, so the user's callback can execute another FileIO |
| 353 // operation, assuming there are no other pending operations. | 421 // operation, assuming there are no other pending operations. |
| 354 state_manager_.SetOperationFinished(); | 422 state_manager_.SetOperationFinished(); |
| 355 callback->Run(result); | 423 callback->Run(result); |
| 356 } | 424 } |
| 357 | 425 |
| 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( | 426 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( |
| 399 scoped_refptr<TrackedCallback> callback, | 427 scoped_refptr<TrackedCallback> callback, |
| 400 PP_FileHandle* output_handle, | 428 PP_FileHandle* output_handle, |
| 401 const ResourceMessageReplyParams& params) { | 429 const ResourceMessageReplyParams& params) { |
| 402 DCHECK(state_manager_.get_pending_operation() == | 430 DCHECK(state_manager_.get_pending_operation() == |
| 403 FileIOStateManager::OPERATION_EXCLUSIVE); | 431 FileIOStateManager::OPERATION_EXCLUSIVE); |
| 404 | 432 |
| 405 if (!TrackedCallback::IsPending(callback)) { | 433 if (!TrackedCallback::IsPending(callback)) { |
| 406 state_manager_.SetOperationFinished(); | 434 state_manager_.SetOperationFinished(); |
| 407 return; | 435 return; |
| 408 } | 436 } |
| 409 | 437 |
| 410 int32_t result = params.result(); | 438 int32_t result = params.result(); |
| 411 IPC::PlatformFileForTransit transit_file; | 439 IPC::PlatformFileForTransit transit_file; |
| 412 if (!params.TakeFileHandleAtIndex(0, &transit_file)) | 440 if (!params.TakeFileHandleAtIndex(0, &transit_file)) |
| 413 result = PP_ERROR_FAILED; | 441 result = PP_ERROR_FAILED; |
| 414 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 442 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
| 415 | 443 |
| 416 // End this operation now, so the user's callback can execute another FileIO | 444 // End this operation now, so the user's callback can execute another FileIO |
| 417 // operation, assuming there are no other pending operations. | 445 // operation, assuming there are no other pending operations. |
| 418 state_manager_.SetOperationFinished(); | 446 state_manager_.SetOperationFinished(); |
| 419 callback->Run(result); | 447 callback->Run(result); |
| 420 } | 448 } |
| 421 | 449 |
| 422 } // namespace proxy | 450 } // namespace proxy |
| 423 } // namespace ppapi | 451 } // namespace ppapi |
| OLD | NEW |