| 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" |
| 11 #include "base/files/file_util_proxy.h" | 11 #include "base/files/file_util_proxy.h" |
| 12 #include "content/public/common/content_client.h" | 12 #include "content/public/common/content_client.h" |
| 13 #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" | |
| 15 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
| 16 #include "ppapi/host/dispatch_host_message.h" | 15 #include "ppapi/host/dispatch_host_message.h" |
| 17 #include "ppapi/host/ppapi_host.h" | 16 #include "ppapi/host/ppapi_host.h" |
| 18 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
| 19 #include "ppapi/shared_impl/file_type_conversion.h" | 18 #include "ppapi/shared_impl/file_type_conversion.h" |
| 20 #include "ppapi/shared_impl/time_conversion.h" | 19 #include "ppapi/shared_impl/time_conversion.h" |
| 21 #include "ppapi/thunk/enter.h" | 20 #include "ppapi/thunk/enter.h" |
| 22 #include "webkit/plugins/ppapi/file_callbacks.h" | |
| 23 #include "webkit/plugins/ppapi/host_globals.h" | 21 #include "webkit/plugins/ppapi/host_globals.h" |
| 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 22 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 25 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 23 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| 26 #include "webkit/plugins/ppapi/quota_file_io.h" | 24 #include "webkit/plugins/ppapi/quota_file_io.h" |
| 27 | 25 |
| 28 namespace content { | 26 namespace content { |
| 29 | 27 |
| 30 using ppapi::FileIOStateManager; | 28 using ppapi::FileIOStateManager; |
| 31 using ppapi::PPTimeToTime; | 29 using ppapi::PPTimeToTime; |
| 32 using ppapi::TimeToPPTime; | 30 using ppapi::TimeToPPTime; |
| 33 using ppapi::host::ReplyMessageContext; | 31 using ppapi::host::ReplyMessageContext; |
| 34 using ppapi::thunk::EnterResourceNoLock; | 32 using ppapi::thunk::EnterResourceNoLock; |
| 35 using ppapi::thunk::PPB_FileRef_API; | 33 using ppapi::thunk::PPB_FileRef_API; |
| 36 using webkit::ppapi::PPB_FileRef_Impl; | 34 using webkit::ppapi::PPB_FileRef_Impl; |
| 37 using webkit::ppapi::PluginDelegate; | 35 using webkit::ppapi::PluginDelegate; |
| 38 | 36 |
| 39 namespace { | 37 namespace { |
| 40 | 38 |
| 41 // The maximum size we'll support reading in one chunk. The renderer process | 39 // The maximum size we'll support reading in one chunk. The renderer process |
| 42 // must allocate a buffer sized according to the request of the plugin. To | 40 // must allocate a buffer sized according to the request of the plugin. To |
| 43 // keep things from getting out of control, we cap the read size to this value. | 41 // keep things from getting out of control, we cap the read size to this value. |
| 44 // This should generally be OK since the API specifies that it may perform a | 42 // This should generally be OK since the API specifies that it may perform a |
| 45 // partial read. | 43 // partial read. |
| 46 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB | 44 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB |
| 47 | 45 |
| 48 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; | 46 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; |
| 49 | 47 |
| 50 class PlatformGeneralCallbackTranslator | |
| 51 : public NullFileSystemCallbackDispatcher { | |
| 52 public: | |
| 53 explicit PlatformGeneralCallbackTranslator( | |
| 54 const PlatformGeneralCallback& callback) | |
| 55 : callback_(callback) {} | |
| 56 | |
| 57 virtual ~PlatformGeneralCallbackTranslator() {} | |
| 58 | |
| 59 virtual void DidSucceed() OVERRIDE { | |
| 60 callback_.Run(base::PLATFORM_FILE_OK); | |
| 61 } | |
| 62 | |
| 63 virtual void DidFail(base::PlatformFileError platform_error) OVERRIDE { | |
| 64 callback_.Run(platform_error); | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 PlatformGeneralCallback callback_; | |
| 69 }; | |
| 70 | |
| 71 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { | 48 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { |
| 72 // On the plugin side, some callbacks expect a parameter that means different | 49 // On the plugin side, some callbacks expect a parameter that means different |
| 73 // things depending on whether is negative or not. We translate for those | 50 // things depending on whether is negative or not. We translate for those |
| 74 // callbacks here. | 51 // callbacks here. |
| 75 return pp_error == PP_OK ? byte_number : pp_error; | 52 return pp_error == PP_OK ? byte_number : pp_error; |
| 76 } | 53 } |
| 77 | 54 |
| 78 } // namespace | 55 } // namespace |
| 79 | 56 |
| 80 PepperFileIOHost::PepperFileIOHost(RendererPpapiHost* host, | 57 PepperFileIOHost::PepperFileIOHost(RendererPpapiHost* host, |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 return rv; | 192 return rv; |
| 216 | 193 |
| 217 if (!plugin_delegate_) | 194 if (!plugin_delegate_) |
| 218 return PP_ERROR_FAILED; | 195 return PP_ERROR_FAILED; |
| 219 | 196 |
| 220 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { | 197 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 221 if (!plugin_delegate_->Touch( | 198 if (!plugin_delegate_->Touch( |
| 222 file_system_url_, | 199 file_system_url_, |
| 223 PPTimeToTime(last_access_time), | 200 PPTimeToTime(last_access_time), |
| 224 PPTimeToTime(last_modified_time), | 201 PPTimeToTime(last_modified_time), |
| 225 new PlatformGeneralCallbackTranslator( | 202 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 226 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | 203 weak_factory_.GetWeakPtr(), |
| 227 weak_factory_.GetWeakPtr(), | 204 context->MakeReplyMessageContext()))) |
| 228 context->MakeReplyMessageContext())))) | |
| 229 return PP_ERROR_FAILED; | 205 return PP_ERROR_FAILED; |
| 230 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 206 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 231 return PP_OK_COMPLETIONPENDING; | 207 return PP_OK_COMPLETIONPENDING; |
| 232 } | 208 } |
| 233 | 209 |
| 234 // TODO(nhiroki): fix a failure of FileIO.Touch for an external filesystem on | 210 // TODO(nhiroki): fix a failure of FileIO.Touch for an external filesystem on |
| 235 // Mac and Linux due to sandbox restrictions (http://crbug.com/101128). | 211 // Mac and Linux due to sandbox restrictions (http://crbug.com/101128). |
| 236 if (!base::FileUtilProxy::Touch( | 212 if (!base::FileUtilProxy::Touch( |
| 237 plugin_delegate_->GetFileThreadMessageLoopProxy(), | 213 plugin_delegate_->GetFileThreadMessageLoopProxy(), |
| 238 file_, PPTimeToTime(last_access_time), | 214 file_, PPTimeToTime(last_access_time), |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 296 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 321 if (rv != PP_OK) | 297 if (rv != PP_OK) |
| 322 return rv; | 298 return rv; |
| 323 | 299 |
| 324 if (!plugin_delegate_) | 300 if (!plugin_delegate_) |
| 325 return PP_ERROR_FAILED; | 301 return PP_ERROR_FAILED; |
| 326 | 302 |
| 327 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { | 303 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 328 if (!plugin_delegate_->SetLength( | 304 if (!plugin_delegate_->SetLength( |
| 329 file_system_url_, length, | 305 file_system_url_, length, |
| 330 new PlatformGeneralCallbackTranslator( | 306 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 331 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | 307 weak_factory_.GetWeakPtr(), |
| 332 weak_factory_.GetWeakPtr(), | 308 context->MakeReplyMessageContext()))) |
| 333 context->MakeReplyMessageContext())))) | |
| 334 return PP_ERROR_FAILED; | 309 return PP_ERROR_FAILED; |
| 335 } else { | 310 } else { |
| 336 // TODO(nhiroki): fix a failure of FileIO.SetLength for an external | 311 // TODO(nhiroki): fix a failure of FileIO.SetLength for an external |
| 337 // filesystem on Mac due to sandbox restrictions (http://crbug.com/156077). | 312 // filesystem on Mac due to sandbox restrictions (http://crbug.com/156077). |
| 338 if (!base::FileUtilProxy::Truncate( | 313 if (!base::FileUtilProxy::Truncate( |
| 339 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, length, | 314 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, length, |
| 340 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | 315 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 341 weak_factory_.GetWeakPtr(), | 316 weak_factory_.GetWeakPtr(), |
| 342 context->MakeReplyMessageContext()))) | 317 context->MakeReplyMessageContext()))) |
| 343 return PP_ERROR_FAILED; | 318 return PP_ERROR_FAILED; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 // On the plugin side, the callback expects a parameter with different meaning | 532 // On the plugin side, the callback expects a parameter with different meaning |
| 558 // depends on whether is negative or not. It is the result here. We translate | 533 // depends on whether is negative or not. It is the result here. We translate |
| 559 // for the callback. | 534 // for the callback. |
| 560 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); | 535 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 561 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | 536 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
| 562 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | 537 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 563 state_manager_.SetOperationFinished(); | 538 state_manager_.SetOperationFinished(); |
| 564 } | 539 } |
| 565 | 540 |
| 566 } // namespace content | 541 } // namespace content |
| OLD | NEW |