| 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/child/child_thread.h" | 12 #include "content/child/child_thread.h" |
| 13 #include "content/child/fileapi/file_system_dispatcher.h" | 13 #include "content/child/fileapi/file_system_dispatcher.h" |
| 14 #include "content/child/quota_dispatcher.h" | 14 #include "content/child/quota_dispatcher.h" |
| 15 #include "content/common/fileapi/file_system_messages.h" | 15 #include "content/common/fileapi/file_system_messages.h" |
| 16 #include "content/common/view_messages.h" | 16 #include "content/common/view_messages.h" |
| 17 #include "content/public/common/content_client.h" | 17 #include "content/public/common/content_client.h" |
| 18 #include "content/public/renderer/content_renderer_client.h" | 18 #include "content/public/renderer/content_renderer_client.h" |
| 19 #include "content/renderer/pepper/host_globals.h" | 19 #include "content/renderer/pepper/host_globals.h" |
| 20 #include "content/renderer/pepper/pepper_browser_connection.h" |
| 20 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 21 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 21 #include "content/renderer/pepper/ppb_file_ref_impl.h" | 22 #include "content/renderer/pepper/plugin_module.h" |
| 22 #include "content/renderer/pepper/quota_file_io.h" | 23 #include "content/renderer/pepper/quota_file_io.h" |
| 24 #include "content/renderer/pepper/renderer_ppapi_host_impl.h" |
| 23 #include "content/renderer/render_thread_impl.h" | 25 #include "content/renderer/render_thread_impl.h" |
| 24 #include "ppapi/c/pp_errors.h" | 26 #include "ppapi/c/pp_errors.h" |
| 25 #include "ppapi/c/ppb_file_io.h" | 27 #include "ppapi/c/ppb_file_io.h" |
| 26 #include "ppapi/host/dispatch_host_message.h" | 28 #include "ppapi/host/dispatch_host_message.h" |
| 27 #include "ppapi/host/ppapi_host.h" | 29 #include "ppapi/host/ppapi_host.h" |
| 28 #include "ppapi/proxy/ppapi_messages.h" | 30 #include "ppapi/proxy/ppapi_messages.h" |
| 29 #include "ppapi/shared_impl/file_type_conversion.h" | 31 #include "ppapi/shared_impl/file_type_conversion.h" |
| 30 #include "ppapi/shared_impl/time_conversion.h" | 32 #include "ppapi/shared_impl/time_conversion.h" |
| 31 #include "ppapi/thunk/enter.h" | 33 #include "ppapi/thunk/enter.h" |
| 32 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 34 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
| 33 | 35 |
| 34 namespace content { | 36 namespace content { |
| 35 | 37 |
| 36 using ppapi::FileIOStateManager; | 38 using ppapi::FileIOStateManager; |
| 37 using ppapi::PPTimeToTime; | 39 using ppapi::PPTimeToTime; |
| 38 using ppapi::host::ReplyMessageContext; | 40 using ppapi::host::ReplyMessageContext; |
| 39 using ppapi::thunk::EnterResourceNoLock; | 41 using ppapi::thunk::EnterResourceNoLock; |
| 40 using ppapi::thunk::PPB_FileRef_API; | |
| 41 | 42 |
| 42 namespace { | 43 namespace { |
| 43 | 44 |
| 44 // The maximum size we'll support reading in one chunk. The renderer process | 45 // The maximum size we'll support reading in one chunk. The renderer process |
| 45 // must allocate a buffer sized according to the request of the plugin. To | 46 // must allocate a buffer sized according to the request of the plugin. To |
| 46 // keep things from getting out of control, we cap the read size to this value. | 47 // keep things from getting out of control, we cap the read size to this value. |
| 47 // This should generally be OK since the API specifies that it may perform a | 48 // This should generally be OK since the API specifies that it may perform a |
| 48 // partial read. | 49 // partial read. |
| 49 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB | 50 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB |
| 50 | 51 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 PepperFileIOHost::NotifyCloseFileCallback()); | 138 PepperFileIOHost::NotifyCloseFileCallback()); |
| 138 } | 139 } |
| 139 | 140 |
| 140 } // namespace | 141 } // namespace |
| 141 | 142 |
| 142 PepperFileIOHost::PepperFileIOHost(RendererPpapiHost* host, | 143 PepperFileIOHost::PepperFileIOHost(RendererPpapiHost* host, |
| 143 PP_Instance instance, | 144 PP_Instance instance, |
| 144 PP_Resource resource) | 145 PP_Resource resource) |
| 145 : ResourceHost(host->GetPpapiHost(), instance, resource), | 146 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 146 renderer_ppapi_host_(host), | 147 renderer_ppapi_host_(host), |
| 148 plugin_instance_(HostGlobals::Get()->GetInstance(instance)), |
| 147 file_(base::kInvalidPlatformFileValue), | 149 file_(base::kInvalidPlatformFileValue), |
| 148 file_system_type_(PP_FILESYSTEMTYPE_INVALID), | 150 file_system_type_(PP_FILESYSTEMTYPE_INVALID), |
| 149 quota_policy_(quota::kQuotaLimitTypeUnknown), | 151 quota_policy_(quota::kQuotaLimitTypeUnknown), |
| 150 is_running_in_process_(host->IsRunningInProcess()), | 152 is_running_in_process_(host->IsRunningInProcess()), |
| 151 open_flags_(0), | 153 open_flags_(0), |
| 152 weak_factory_(this), | 154 weak_factory_(this), |
| 153 routing_id_(RenderThreadImpl::current()->GenerateRoutingID()) { | 155 routing_id_(RenderThreadImpl::current()->GenerateRoutingID()) { |
| 154 ChildThread::current()->AddRoute(routing_id_, this); | 156 ChildThread::current()->AddRoute(routing_id_, this); |
| 155 } | 157 } |
| 156 | 158 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 235 |
| 234 // TODO(tommycli): Eventually just pass the Pepper flags straight to the | 236 // TODO(tommycli): Eventually just pass the Pepper flags straight to the |
| 235 // FileSystemDispatcher so it can handle doing the security check. | 237 // FileSystemDispatcher so it can handle doing the security check. |
| 236 int platform_file_flags = 0; | 238 int platform_file_flags = 0; |
| 237 open_flags_ = open_flags; | 239 open_flags_ = open_flags; |
| 238 if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, | 240 if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, |
| 239 &platform_file_flags)) { | 241 &platform_file_flags)) { |
| 240 return PP_ERROR_BADARGUMENT; | 242 return PP_ERROR_BADARGUMENT; |
| 241 } | 243 } |
| 242 | 244 |
| 243 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref_resource, true); | 245 RenderView* render_view = |
| 244 if (enter.failed()) | 246 renderer_ppapi_host_->GetRenderViewForInstance(pp_instance()); |
| 245 return PP_ERROR_BADRESOURCE; | 247 PepperBrowserConnection* browser_connection = |
| 248 PepperBrowserConnection::Get(render_view); |
| 249 int child_process_id = plugin_instance_->module()->GetPluginChildId(); |
| 246 | 250 |
| 247 PPB_FileRef_API* file_ref_api = enter.object(); | 251 // Explicitly set child_process_id to 0 if we're running in process. This is |
| 248 PP_FileSystemType type = file_ref_api->GetFileSystemType(); | 252 // necessary in the case where we're a host for a resource in "the trusted |
| 249 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 253 // NaCl plugin". In that case, we may have already done work for the |
| 250 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && | 254 // untrusted plugin, and the call above will give a non-zero |
| 251 type != PP_FILESYSTEMTYPE_EXTERNAL && | 255 // child_process_id. |
| 252 type != PP_FILESYSTEMTYPE_ISOLATED) | 256 if (renderer_ppapi_host_->IsRunningInProcess()) |
| 253 return PP_ERROR_FAILED; | 257 child_process_id = 0; |
| 254 file_system_type_ = type; | |
| 255 | 258 |
| 256 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api); | 259 std::vector<PP_Resource> resources; |
| 257 if (file_ref->HasValidFileSystem()) { | 260 resources.push_back(file_ref_resource); |
| 258 file_system_url_ = file_ref->GetFileSystemURL(); | 261 browser_connection->SendBrowserFileRefGetInfo( |
| 262 child_process_id, |
| 263 resources, |
| 264 base::Bind(&PepperFileIOHost::DidGetFileRefInfo, |
| 265 weak_factory_.GetWeakPtr(), |
| 266 context->MakeReplyMessageContext(), |
| 267 platform_file_flags)); |
| 268 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 269 return PP_OK_COMPLETIONPENDING; |
| 270 } |
| 259 | 271 |
| 272 void PepperFileIOHost::DidGetFileRefInfo( |
| 273 ppapi::host::ReplyMessageContext reply_context, |
| 274 int platform_file_flags, |
| 275 const std::vector<ppapi::FileRefDetailedInfo>& infos) { |
| 276 if (infos.size() != 1) { |
| 277 reply_context.params.set_result(PP_ERROR_FAILED); |
| 278 host()->SendReply(reply_context, |
| 279 PpapiPluginMsg_FileIO_OpenReply()); |
| 280 return; |
| 281 } |
| 282 |
| 283 file_system_type_ = infos[0].file_system_type; |
| 284 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 285 file_system_url_ = GURL(infos[0].file_system_url_spec); |
| 260 FileSystemDispatcher* file_system_dispatcher = | 286 FileSystemDispatcher* file_system_dispatcher = |
| 261 ChildThread::current()->file_system_dispatcher(); | 287 ChildThread::current()->file_system_dispatcher(); |
| 288 |
| 262 AsyncOpenFileSystemURLCallback callback = base::Bind( | 289 AsyncOpenFileSystemURLCallback callback = base::Bind( |
| 263 &PepperFileIOHost::ExecutePlatformOpenFileSystemURLCallback, | 290 &PepperFileIOHost::ExecutePlatformOpenFileSystemURLCallback, |
| 264 weak_factory_.GetWeakPtr(), | 291 weak_factory_.GetWeakPtr(), |
| 265 context->MakeReplyMessageContext()); | 292 reply_context); |
| 266 file_system_dispatcher->OpenFile( | 293 file_system_dispatcher->OpenFile( |
| 267 file_system_url_, platform_file_flags, | 294 file_system_url_, platform_file_flags, |
| 268 base::Bind(&DidOpenFileSystemURL, callback), | 295 base::Bind(&DidOpenFileSystemURL, callback), |
| 269 base::Bind(&DidFailOpenFileSystemURL, callback)); | 296 base::Bind(&DidFailOpenFileSystemURL, callback)); |
| 270 } else { | 297 } else { |
| 271 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) | |
| 272 return PP_ERROR_FAILED; | |
| 273 int message_id = pending_async_open_files_.Add(new AsyncOpenFileCallback( | 298 int message_id = pending_async_open_files_.Add(new AsyncOpenFileCallback( |
| 274 base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback, | 299 base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback, |
| 275 weak_factory_.GetWeakPtr(), | 300 weak_factory_.GetWeakPtr(), |
| 276 context->MakeReplyMessageContext()))); | 301 reply_context))); |
| 277 RenderThreadImpl::current()->Send(new ViewHostMsg_AsyncOpenPepperFile( | 302 RenderThreadImpl::current()->Send(new ViewHostMsg_AsyncOpenPepperFile( |
| 278 routing_id_, file_ref->GetSystemPath(), open_flags, message_id)); | 303 routing_id_, infos[0].external_path, open_flags_, message_id)); |
| 279 } | 304 } |
| 280 | |
| 281 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 282 return PP_OK_COMPLETIONPENDING; | |
| 283 } | 305 } |
| 284 | 306 |
| 285 int32_t PepperFileIOHost::OnHostMsgQuery( | 307 int32_t PepperFileIOHost::OnHostMsgQuery( |
| 286 ppapi::host::HostMessageContext* context) { | 308 ppapi::host::HostMessageContext* context) { |
| 287 int32_t rv = state_manager_.CheckOperationState( | 309 int32_t rv = state_manager_.CheckOperationState( |
| 288 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 310 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 289 if (rv != PP_OK) | 311 if (rv != PP_OK) |
| 290 return rv; | 312 return rv; |
| 291 | 313 |
| 292 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( | 314 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 // On the plugin side, the callback expects a parameter with different meaning | 679 // On the plugin side, the callback expects a parameter with different meaning |
| 658 // depends on whether is negative or not. It is the result here. We translate | 680 // depends on whether is negative or not. It is the result here. We translate |
| 659 // for the callback. | 681 // for the callback. |
| 660 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); | 682 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 661 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | 683 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
| 662 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | 684 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 663 state_manager_.SetOperationFinished(); | 685 state_manager_.SetOperationFinished(); |
| 664 } | 686 } |
| 665 | 687 |
| 666 } // namespace content | 688 } // namespace content |
| OLD | NEW |