| 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 "content/renderer/pepper/pepper_file_io_host.h" | 5 #include "content/renderer/pepper/pepper_file_io_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 namespace content { | 36 namespace content { |
| 37 | 37 |
| 38 using ppapi::FileIOStateManager; | 38 using ppapi::FileIOStateManager; |
| 39 using ppapi::PPTimeToTime; | 39 using ppapi::PPTimeToTime; |
| 40 using ppapi::host::ReplyMessageContext; | 40 using ppapi::host::ReplyMessageContext; |
| 41 using ppapi::thunk::EnterResourceNoLock; | 41 using ppapi::thunk::EnterResourceNoLock; |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 // The maximum size we'll support reading in one chunk. The renderer process | |
| 46 // must allocate a buffer sized according to the request of the plugin. To | |
| 47 // keep things from getting out of control, we cap the read size to this value. | |
| 48 // This should generally be OK since the API specifies that it may perform a | |
| 49 // partial read. | |
| 50 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB | |
| 51 | |
| 52 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; | 45 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; |
| 53 | 46 |
| 54 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { | 47 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { |
| 55 // On the plugin side, some callbacks expect a parameter that means different | 48 // On the plugin side, some callbacks expect a parameter that means different |
| 56 // things depending on whether is negative or not. We translate for those | 49 // things depending on whether is negative or not. We translate for those |
| 57 // callbacks here. | 50 // callbacks here. |
| 58 return pp_error == PP_OK ? byte_number : pp_error; | 51 return pp_error == PP_OK ? byte_number : pp_error; |
| 59 } | 52 } |
| 60 | 53 |
| 61 class QuotaCallbackTranslator : public QuotaDispatcher::Callback { | 54 class QuotaCallbackTranslator : public QuotaDispatcher::Callback { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 OnHostMsgClose(NULL); | 153 OnHostMsgClose(NULL); |
| 161 ChildThread::current()->RemoveRoute(routing_id_); | 154 ChildThread::current()->RemoveRoute(routing_id_); |
| 162 } | 155 } |
| 163 | 156 |
| 164 int32_t PepperFileIOHost::OnResourceMessageReceived( | 157 int32_t PepperFileIOHost::OnResourceMessageReceived( |
| 165 const IPC::Message& msg, | 158 const IPC::Message& msg, |
| 166 ppapi::host::HostMessageContext* context) { | 159 ppapi::host::HostMessageContext* context) { |
| 167 IPC_BEGIN_MESSAGE_MAP(PepperFileIOHost, msg) | 160 IPC_BEGIN_MESSAGE_MAP(PepperFileIOHost, msg) |
| 168 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open, | 161 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open, |
| 169 OnHostMsgOpen) | 162 OnHostMsgOpen) |
| 170 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Query, | |
| 171 OnHostMsgQuery) | |
| 172 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch, | 163 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch, |
| 173 OnHostMsgTouch) | 164 OnHostMsgTouch) |
| 174 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Read, | |
| 175 OnHostMsgRead) | |
| 176 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Write, | 165 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Write, |
| 177 OnHostMsgWrite) | 166 OnHostMsgWrite) |
| 178 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength, | 167 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength, |
| 179 OnHostMsgSetLength) | 168 OnHostMsgSetLength) |
| 180 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush, | 169 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush, |
| 181 OnHostMsgFlush) | 170 OnHostMsgFlush) |
| 182 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Close, | 171 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Close, |
| 183 OnHostMsgClose) | 172 OnHostMsgClose) |
| 184 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_WillWrite, | 173 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_WillWrite, |
| 185 OnHostMsgWillWrite) | 174 OnHostMsgWillWrite) |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } else { | 286 } else { |
| 298 int message_id = pending_async_open_files_.Add(new AsyncOpenFileCallback( | 287 int message_id = pending_async_open_files_.Add(new AsyncOpenFileCallback( |
| 299 base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback, | 288 base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback, |
| 300 weak_factory_.GetWeakPtr(), | 289 weak_factory_.GetWeakPtr(), |
| 301 reply_context))); | 290 reply_context))); |
| 302 RenderThreadImpl::current()->Send(new ViewHostMsg_AsyncOpenPepperFile( | 291 RenderThreadImpl::current()->Send(new ViewHostMsg_AsyncOpenPepperFile( |
| 303 routing_id_, infos[0].external_path, open_flags_, message_id)); | 292 routing_id_, infos[0].external_path, open_flags_, message_id)); |
| 304 } | 293 } |
| 305 } | 294 } |
| 306 | 295 |
| 307 int32_t PepperFileIOHost::OnHostMsgQuery( | |
| 308 ppapi::host::HostMessageContext* context) { | |
| 309 int32_t rv = state_manager_.CheckOperationState( | |
| 310 FileIOStateManager::OPERATION_EXCLUSIVE, true); | |
| 311 if (rv != PP_OK) | |
| 312 return rv; | |
| 313 | |
| 314 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( | |
| 315 RenderThreadImpl::current()->GetFileThreadMessageLoopProxy().get(), | |
| 316 file_, | |
| 317 base::Bind(&PepperFileIOHost::ExecutePlatformQueryCallback, | |
| 318 weak_factory_.GetWeakPtr(), | |
| 319 context->MakeReplyMessageContext()))) | |
| 320 return PP_ERROR_FAILED; | |
| 321 | |
| 322 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 323 return PP_OK_COMPLETIONPENDING; | |
| 324 } | |
| 325 | |
| 326 int32_t PepperFileIOHost::OnHostMsgTouch( | 296 int32_t PepperFileIOHost::OnHostMsgTouch( |
| 327 ppapi::host::HostMessageContext* context, | 297 ppapi::host::HostMessageContext* context, |
| 328 PP_Time last_access_time, | 298 PP_Time last_access_time, |
| 329 PP_Time last_modified_time) { | 299 PP_Time last_modified_time) { |
| 330 int32_t rv = state_manager_.CheckOperationState( | 300 int32_t rv = state_manager_.CheckOperationState( |
| 331 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 301 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 332 if (rv != PP_OK) | 302 if (rv != PP_OK) |
| 333 return rv; | 303 return rv; |
| 334 | 304 |
| 335 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { | 305 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 355 PPTimeToTime(last_modified_time), | 325 PPTimeToTime(last_modified_time), |
| 356 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | 326 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 357 weak_factory_.GetWeakPtr(), | 327 weak_factory_.GetWeakPtr(), |
| 358 context->MakeReplyMessageContext()))) | 328 context->MakeReplyMessageContext()))) |
| 359 return PP_ERROR_FAILED; | 329 return PP_ERROR_FAILED; |
| 360 | 330 |
| 361 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 331 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 362 return PP_OK_COMPLETIONPENDING; | 332 return PP_OK_COMPLETIONPENDING; |
| 363 } | 333 } |
| 364 | 334 |
| 365 int32_t PepperFileIOHost::OnHostMsgRead( | |
| 366 ppapi::host::HostMessageContext* context, | |
| 367 int64_t offset, | |
| 368 int32_t max_read_length) { | |
| 369 int32_t rv = state_manager_.CheckOperationState( | |
| 370 FileIOStateManager::OPERATION_READ, true); | |
| 371 if (rv != PP_OK) | |
| 372 return rv; | |
| 373 | |
| 374 // Validate max_read_length before allocating below. This value is coming from | |
| 375 // the untrusted plugin. | |
| 376 if (max_read_length < 0) { | |
| 377 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); | |
| 378 reply_context.params.set_result(PP_ERROR_FAILED); | |
| 379 host()->SendReply(reply_context, | |
| 380 PpapiPluginMsg_FileIO_ReadReply(std::string())); | |
| 381 return PP_OK_COMPLETIONPENDING; | |
| 382 } | |
| 383 | |
| 384 if (!base::FileUtilProxy::Read( | |
| 385 RenderThreadImpl::current()->GetFileThreadMessageLoopProxy().get(), | |
| 386 file_, | |
| 387 offset, | |
| 388 max_read_length, | |
| 389 base::Bind(&PepperFileIOHost::ExecutePlatformReadCallback, | |
| 390 weak_factory_.GetWeakPtr(), | |
| 391 context->MakeReplyMessageContext()))) | |
| 392 return PP_ERROR_FAILED; | |
| 393 | |
| 394 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | |
| 395 return PP_OK_COMPLETIONPENDING; | |
| 396 } | |
| 397 | |
| 398 int32_t PepperFileIOHost::OnHostMsgWrite( | 335 int32_t PepperFileIOHost::OnHostMsgWrite( |
| 399 ppapi::host::HostMessageContext* context, | 336 ppapi::host::HostMessageContext* context, |
| 400 int64_t offset, | 337 int64_t offset, |
| 401 const std::string& buffer) { | 338 const std::string& buffer) { |
| 402 int32_t rv = state_manager_.CheckOperationState( | 339 int32_t rv = state_manager_.CheckOperationState( |
| 403 FileIOStateManager::OPERATION_WRITE, true); | 340 FileIOStateManager::OPERATION_WRITE, true); |
| 404 if (rv != PP_OK) | 341 if (rv != PP_OK) |
| 405 return rv; | 342 return rv; |
| 406 | 343 |
| 407 if (quota_file_io_) { | 344 if (quota_file_io_) { |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 base::PlatformFileError error_code, | 572 base::PlatformFileError error_code, |
| 636 base::PassPlatformFile file, | 573 base::PassPlatformFile file, |
| 637 quota::QuotaLimitType quota_policy, | 574 quota::QuotaLimitType quota_policy, |
| 638 const PepperFileIOHost::NotifyCloseFileCallback& callback) { | 575 const PepperFileIOHost::NotifyCloseFileCallback& callback) { |
| 639 if (error_code == base::PLATFORM_FILE_OK) | 576 if (error_code == base::PLATFORM_FILE_OK) |
| 640 notify_close_file_callback_ = callback; | 577 notify_close_file_callback_ = callback; |
| 641 quota_policy_ = quota_policy; | 578 quota_policy_ = quota_policy; |
| 642 ExecutePlatformOpenFileCallback(reply_context, error_code, file); | 579 ExecutePlatformOpenFileCallback(reply_context, error_code, file); |
| 643 } | 580 } |
| 644 | 581 |
| 645 void PepperFileIOHost::ExecutePlatformQueryCallback( | |
| 646 ppapi::host::ReplyMessageContext reply_context, | |
| 647 base::PlatformFileError error_code, | |
| 648 const base::PlatformFileInfo& file_info) { | |
| 649 PP_FileInfo pp_info; | |
| 650 ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type_, | |
| 651 &pp_info); | |
| 652 | |
| 653 int32_t pp_error = ppapi::PlatformFileErrorToPepperError(error_code); | |
| 654 reply_context.params.set_result(pp_error); | |
| 655 host()->SendReply(reply_context, | |
| 656 PpapiPluginMsg_FileIO_QueryReply(pp_info)); | |
| 657 state_manager_.SetOperationFinished(); | |
| 658 } | |
| 659 | |
| 660 void PepperFileIOHost::ExecutePlatformReadCallback( | |
| 661 ppapi::host::ReplyMessageContext reply_context, | |
| 662 base::PlatformFileError error_code, | |
| 663 const char* data, int bytes_read) { | |
| 664 int32_t pp_error = ppapi::PlatformFileErrorToPepperError(error_code); | |
| 665 | |
| 666 // Only send the amount of data in the string that was actually read. | |
| 667 std::string buffer; | |
| 668 if (pp_error == PP_OK) | |
| 669 buffer.append(data, bytes_read); | |
| 670 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); | |
| 671 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); | |
| 672 state_manager_.SetOperationFinished(); | |
| 673 } | |
| 674 | |
| 675 void PepperFileIOHost::ExecutePlatformWriteCallback( | 582 void PepperFileIOHost::ExecutePlatformWriteCallback( |
| 676 ppapi::host::ReplyMessageContext reply_context, | 583 ppapi::host::ReplyMessageContext reply_context, |
| 677 base::PlatformFileError error_code, | 584 base::PlatformFileError error_code, |
| 678 int bytes_written) { | 585 int bytes_written) { |
| 679 // On the plugin side, the callback expects a parameter with different meaning | 586 // On the plugin side, the callback expects a parameter with different meaning |
| 680 // depends on whether is negative or not. It is the result here. We translate | 587 // depends on whether is negative or not. It is the result here. We translate |
| 681 // for the callback. | 588 // for the callback. |
| 682 int32_t pp_error = ppapi::PlatformFileErrorToPepperError(error_code); | 589 int32_t pp_error = ppapi::PlatformFileErrorToPepperError(error_code); |
| 683 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | 590 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
| 684 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | 591 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 685 state_manager_.SetOperationFinished(); | 592 state_manager_.SetOperationFinished(); |
| 686 } | 593 } |
| 687 | 594 |
| 688 } // namespace content | 595 } // namespace content |
| OLD | NEW |