| 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_helpers.h" | 9 #include "base/callback_helpers.h" |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/files/file_util_proxy.h" | 11 #include "base/files/file_util_proxy.h" |
| 11 #include "content/public/common/content_client.h" | 12 #include "content/public/common/content_client.h" |
| 12 #include "content/public/renderer/content_renderer_client.h" | 13 #include "content/public/renderer/content_renderer_client.h" |
| 14 #include "content/renderer/pepper/null_file_system_callback_dispatcher.h" |
| 13 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 14 #include "ppapi/host/dispatch_host_message.h" | 16 #include "ppapi/host/dispatch_host_message.h" |
| 15 #include "ppapi/host/ppapi_host.h" | 17 #include "ppapi/host/ppapi_host.h" |
| 16 #include "ppapi/proxy/ppapi_messages.h" | 18 #include "ppapi/proxy/ppapi_messages.h" |
| 17 #include "ppapi/shared_impl/file_type_conversion.h" | 19 #include "ppapi/shared_impl/file_type_conversion.h" |
| 18 #include "ppapi/shared_impl/time_conversion.h" | 20 #include "ppapi/shared_impl/time_conversion.h" |
| 19 #include "ppapi/thunk/enter.h" | 21 #include "ppapi/thunk/enter.h" |
| 20 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
| 21 #include "webkit/plugins/ppapi/file_callbacks.h" | 22 #include "webkit/plugins/ppapi/file_callbacks.h" |
| 22 #include "webkit/plugins/ppapi/host_globals.h" | 23 #include "webkit/plugins/ppapi/host_globals.h" |
| 23 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 24 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 25 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| 25 #include "webkit/plugins/ppapi/quota_file_io.h" | 26 #include "webkit/plugins/ppapi/quota_file_io.h" |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 | 29 |
| 29 using ppapi::FileIOStateManager; | 30 using ppapi::FileIOStateManager; |
| 30 using ppapi::PPTimeToTime; | 31 using ppapi::PPTimeToTime; |
| 31 using ppapi::TimeToPPTime; | 32 using ppapi::TimeToPPTime; |
| 32 using ppapi::host::ReplyMessageContext; | 33 using ppapi::host::ReplyMessageContext; |
| 33 using ppapi::thunk::EnterResourceNoLock; | 34 using ppapi::thunk::EnterResourceNoLock; |
| 34 using ppapi::thunk::PPB_FileRef_API; | 35 using ppapi::thunk::PPB_FileRef_API; |
| 35 using webkit::ppapi::PPB_FileRef_Impl; | 36 using webkit::ppapi::PPB_FileRef_Impl; |
| 36 using webkit::ppapi::PluginDelegate; | 37 using webkit::ppapi::PluginDelegate; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 // The maximum size we'll support reading in one chunk. The renderer process | 41 // The maximum size we'll support reading in one chunk. The renderer process |
| 41 // must allocate a buffer sized according to the request of the plugin. To | 42 // must allocate a buffer sized according to the request of the plugin. To |
| 42 // keep things from getting out of control, we cap the read size to this value. | 43 // keep things from getting out of control, we cap the read size to this value. |
| 43 // This should generally be OK since the API specifies that it may perform a | 44 // This should generally be OK since the API specifies that it may perform a |
| 44 // partial read. | 45 // partial read. |
| 45 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB | 46 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB |
| 46 | 47 |
| 47 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; | 48 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; |
| 48 | 49 |
| 49 class PlatformGeneralCallbackTranslator | 50 class PlatformGeneralCallbackTranslator |
| 50 : public fileapi::FileSystemCallbackDispatcher { | 51 : public NullFileSystemCallbackDispatcher { |
| 51 public: | 52 public: |
| 52 explicit PlatformGeneralCallbackTranslator( | 53 explicit PlatformGeneralCallbackTranslator( |
| 53 const PlatformGeneralCallback& callback) | 54 const PlatformGeneralCallback& callback) |
| 54 : callback_(callback) {} | 55 : callback_(callback) {} |
| 55 | 56 |
| 56 virtual ~PlatformGeneralCallbackTranslator() {} | 57 virtual ~PlatformGeneralCallbackTranslator() {} |
| 57 | 58 |
| 58 virtual void DidSucceed() OVERRIDE { | 59 virtual void DidSucceed() OVERRIDE { |
| 59 callback_.Run(base::PLATFORM_FILE_OK); | 60 callback_.Run(base::PLATFORM_FILE_OK); |
| 60 } | 61 } |
| 61 | 62 |
| 62 virtual void DidReadMetadata(const base::PlatformFileInfo& file_info, | 63 virtual void DidFail(base::PlatformFileError platform_error) OVERRIDE { |
| 63 const base::FilePath& platform_path) OVERRIDE { | 64 callback_.Run(platform_error); |
| 64 NOTREACHED(); | |
| 65 } | |
| 66 | |
| 67 virtual void DidCreateSnapshotFile( | |
| 68 const base::PlatformFileInfo& file_info, | |
| 69 const base::FilePath& platform_path) OVERRIDE { | |
| 70 NOTREACHED(); | |
| 71 } | |
| 72 | |
| 73 virtual void DidReadDirectory( | |
| 74 const std::vector<base::FileUtilProxy::Entry>& entries, | |
| 75 bool has_more) OVERRIDE { | |
| 76 NOTREACHED(); | |
| 77 } | |
| 78 | |
| 79 virtual void DidOpenFileSystem(const std::string& name, | |
| 80 const GURL& root) OVERRIDE { | |
| 81 NOTREACHED(); | |
| 82 } | |
| 83 | |
| 84 virtual void DidFail(base::PlatformFileError error_code) OVERRIDE { | |
| 85 callback_.Run(error_code); | |
| 86 } | |
| 87 | |
| 88 virtual void DidWrite(int64 bytes, bool complete) OVERRIDE { | |
| 89 NOTREACHED(); | |
| 90 } | |
| 91 | |
| 92 virtual void DidOpenFile(base::PlatformFile file, | |
| 93 quota::QuotaLimitType quota_policy) OVERRIDE { | |
| 94 NOTREACHED(); | |
| 95 } | 65 } |
| 96 | 66 |
| 97 private: | 67 private: |
| 98 PlatformGeneralCallback callback_; | 68 PlatformGeneralCallback callback_; |
| 99 }; | 69 }; |
| 100 | 70 |
| 101 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { | 71 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { |
| 102 // On the plugin side, some callbacks expect a parameter that means different | 72 // On the plugin side, some callbacks expect a parameter that means different |
| 103 // things depending on whether is negative or not. We translate for those | 73 // things depending on whether is negative or not. We translate for those |
| 104 // callbacks here. | 74 // callbacks here. |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 int32_t rv = state_manager_.CheckOperationState( | 385 int32_t rv = state_manager_.CheckOperationState( |
| 416 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 386 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 417 if (rv != PP_OK) | 387 if (rv != PP_OK) |
| 418 return rv; | 388 return rv; |
| 419 | 389 |
| 420 if (!quota_file_io_.get()) | 390 if (!quota_file_io_.get()) |
| 421 return PP_OK; | 391 return PP_OK; |
| 422 | 392 |
| 423 if (!quota_file_io_->WillWrite( | 393 if (!quota_file_io_->WillWrite( |
| 424 offset, bytes_to_write, | 394 offset, bytes_to_write, |
| 425 base::Bind(&PepperFileIOHost::ExecutePlatformWillWriteCallback, | 395 base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback, |
| 426 weak_factory_.GetWeakPtr(), | 396 weak_factory_.GetWeakPtr(), |
| 427 context->MakeReplyMessageContext()))) | 397 context->MakeReplyMessageContext()))) |
| 428 return PP_ERROR_FAILED; | 398 return PP_ERROR_FAILED; |
| 429 | 399 |
| 430 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 400 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 431 return PP_OK_COMPLETIONPENDING; | 401 return PP_OK_COMPLETIONPENDING; |
| 432 } | 402 } |
| 433 | 403 |
| 434 int32_t PepperFileIOHost::OnHostMsgWillSetLength( | 404 int32_t PepperFileIOHost::OnHostMsgWillSetLength( |
| 435 ppapi::host::HostMessageContext* context, | 405 ppapi::host::HostMessageContext* context, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 buffer.append(data, bytes_read); | 547 buffer.append(data, bytes_read); |
| 578 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); | 548 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); |
| 579 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); | 549 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); |
| 580 state_manager_.SetOperationFinished(); | 550 state_manager_.SetOperationFinished(); |
| 581 } | 551 } |
| 582 | 552 |
| 583 void PepperFileIOHost::ExecutePlatformWriteCallback( | 553 void PepperFileIOHost::ExecutePlatformWriteCallback( |
| 584 ppapi::host::ReplyMessageContext reply_context, | 554 ppapi::host::ReplyMessageContext reply_context, |
| 585 base::PlatformFileError error_code, | 555 base::PlatformFileError error_code, |
| 586 int bytes_written) { | 556 int bytes_written) { |
| 587 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); | |
| 588 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | |
| 589 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | |
| 590 state_manager_.SetOperationFinished(); | |
| 591 } | |
| 592 | |
| 593 void PepperFileIOHost::ExecutePlatformWillWriteCallback( | |
| 594 ppapi::host::ReplyMessageContext reply_context, | |
| 595 base::PlatformFileError error_code, | |
| 596 int bytes_written) { | |
| 597 // On the plugin side, the callback expects a parameter with different meaning | 557 // On the plugin side, the callback expects a parameter with different meaning |
| 598 // depends on whether is negative or not. It is the result here. We translate | 558 // depends on whether is negative or not. It is the result here. We translate |
| 599 // for the callback. | 559 // for the callback. |
| 600 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); | 560 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 601 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | 561 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
| 602 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | 562 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 603 state_manager_.SetOperationFinished(); | 563 state_manager_.SetOperationFinished(); |
| 604 } | 564 } |
| 605 | 565 |
| 606 } // namespace content | 566 } // namespace content |
| OLD | NEW |