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 "base/task_runner_util.h" |
9 #include "ipc/ipc_message.h" | 9 #include "ipc/ipc_message.h" |
10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
11 #include "ppapi/proxy/ppapi_messages.h" | 11 #include "ppapi/proxy/ppapi_messages.h" |
12 #include "ppapi/shared_impl/array_writer.h" | 12 #include "ppapi/shared_impl/array_writer.h" |
13 #include "ppapi/shared_impl/file_ref_create_info.h" | 13 #include "ppapi/shared_impl/file_ref_create_info.h" |
14 #include "ppapi/shared_impl/file_system_util.h" | 14 #include "ppapi/shared_impl/file_system_util.h" |
15 #include "ppapi/shared_impl/file_type_conversion.h" | 15 #include "ppapi/shared_impl/file_type_conversion.h" |
16 #include "ppapi/shared_impl/ppapi_globals.h" | 16 #include "ppapi/shared_impl/ppapi_globals.h" |
17 #include "ppapi/shared_impl/proxy_lock.h" | 17 #include "ppapi/shared_impl/proxy_lock.h" |
18 #include "ppapi/shared_impl/resource_tracker.h" | 18 #include "ppapi/shared_impl/resource_tracker.h" |
19 #include "ppapi/thunk/enter.h" | 19 #include "ppapi/thunk/enter.h" |
20 #include "ppapi/thunk/ppb_file_ref_api.h" | 20 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 21 #include "ppapi/thunk/ppb_file_system_api.h" |
21 | 22 |
22 using ppapi::thunk::EnterResourceNoLock; | 23 using ppapi::thunk::EnterResourceNoLock; |
23 using ppapi::thunk::PPB_FileIO_API; | 24 using ppapi::thunk::PPB_FileIO_API; |
24 using ppapi::thunk::PPB_FileRef_API; | 25 using ppapi::thunk::PPB_FileRef_API; |
| 26 using ppapi::thunk::PPB_FileSystem_API; |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 // We must allocate a buffer sized according to the request of the plugin. To | 30 // We must allocate a buffer sized according to the request of the plugin. To |
29 // reduce the chance of out-of-memory errors, we cap the read and write size to | 31 // reduce the chance of out-of-memory errors, we cap the read and write size to |
30 // 32MB. This is OK since the API specifies that it may perform a partial read | 32 // 32MB. This is OK since the API specifies that it may perform a partial read |
31 // or write. | 33 // or write. |
32 static const int32_t kMaxReadWriteSize = 32 * 1024 * 1024; // 32MB | 34 static const int32_t kMaxReadWriteSize = 32 * 1024 * 1024; // 32MB |
33 | 35 |
34 // An adapter to let Read() share the same implementation with ReadToArray(). | 36 // An adapter to let Read() share the same implementation with ReadToArray(). |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 75 |
74 int32_t FileIOResource::ReadOp::DoWork() { | 76 int32_t FileIOResource::ReadOp::DoWork() { |
75 DCHECK(!buffer_.get()); | 77 DCHECK(!buffer_.get()); |
76 buffer_.reset(new char[bytes_to_read_]); | 78 buffer_.reset(new char[bytes_to_read_]); |
77 return base::ReadPlatformFile( | 79 return base::ReadPlatformFile( |
78 file_handle_->raw_handle(), offset_, buffer_.get(), bytes_to_read_); | 80 file_handle_->raw_handle(), offset_, buffer_.get(), bytes_to_read_); |
79 } | 81 } |
80 | 82 |
81 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) | 83 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
82 : PluginResource(connection, instance), | 84 : PluginResource(connection, instance), |
83 file_system_type_(PP_FILESYSTEMTYPE_INVALID) { | 85 file_system_type_(PP_FILESYSTEMTYPE_INVALID), |
| 86 called_close_(false) { |
84 SendCreate(BROWSER, PpapiHostMsg_FileIO_Create()); | 87 SendCreate(BROWSER, PpapiHostMsg_FileIO_Create()); |
85 } | 88 } |
86 | 89 |
87 FileIOResource::~FileIOResource() { | 90 FileIOResource::~FileIOResource() { |
| 91 Close(); |
88 } | 92 } |
89 | 93 |
90 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { | 94 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { |
91 return this; | 95 return this; |
92 } | 96 } |
93 | 97 |
94 int32_t FileIOResource::Open(PP_Resource file_ref, | 98 int32_t FileIOResource::Open(PP_Resource file_ref, |
95 int32_t open_flags, | 99 int32_t open_flags, |
96 scoped_refptr<TrackedCallback> callback) { | 100 scoped_refptr<TrackedCallback> callback) { |
97 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); | 101 EnterResourceNoLock<PPB_FileRef_API> enter_file_ref(file_ref, true); |
98 if (enter.failed()) | 102 if (enter_file_ref.failed()) |
99 return PP_ERROR_BADRESOURCE; | 103 return PP_ERROR_BADRESOURCE; |
100 | 104 |
101 PPB_FileRef_API* file_ref_api = enter.object(); | 105 PPB_FileRef_API* file_ref_api = enter_file_ref.object(); |
102 const FileRefCreateInfo& create_info = file_ref_api->GetCreateInfo(); | 106 const FileRefCreateInfo& create_info = file_ref_api->GetCreateInfo(); |
103 if (!FileSystemTypeIsValid(create_info.file_system_type)) { | 107 if (!FileSystemTypeIsValid(create_info.file_system_type)) { |
104 NOTREACHED(); | 108 NOTREACHED(); |
105 return PP_ERROR_FAILED; | 109 return PP_ERROR_FAILED; |
106 } | 110 } |
107 | |
108 int32_t rv = state_manager_.CheckOperationState( | 111 int32_t rv = state_manager_.CheckOperationState( |
109 FileIOStateManager::OPERATION_EXCLUSIVE, false); | 112 FileIOStateManager::OPERATION_EXCLUSIVE, false); |
110 if (rv != PP_OK) | 113 if (rv != PP_OK) |
111 return rv; | 114 return rv; |
112 | 115 |
113 file_system_type_ = create_info.file_system_type; | 116 file_system_type_ = create_info.file_system_type; |
114 // Keep the FileSystem host alive by taking a reference to its resource. The | 117 |
115 // FileIO host uses the FileSystem host for running tasks. | 118 if (create_info.file_system_plugin_resource) { |
116 file_system_resource_ = create_info.file_system_plugin_resource; | 119 EnterResourceNoLock<PPB_FileSystem_API> enter_file_system( |
| 120 create_info.file_system_plugin_resource, true); |
| 121 if (enter_file_system.failed()) |
| 122 return PP_ERROR_FAILED; |
| 123 // Take a reference on the FileSystem resource. The FileIO host uses the |
| 124 // FileSystem host for running tasks and checking quota. |
| 125 file_system_resource_ = enter_file_system.resource(); |
| 126 } |
117 | 127 |
118 // Take a reference on the FileRef resource while we're opening the file; we | 128 // Take a reference on the FileRef resource while we're opening the file; we |
119 // don't want the plugin destroying it during the Open operation. | 129 // don't want the plugin destroying it during the Open operation. |
120 file_ref_ = enter.resource(); | 130 file_ref_ = enter_file_ref.resource(); |
121 | 131 |
122 Call<PpapiPluginMsg_FileIO_OpenReply>(BROWSER, | 132 Call<PpapiPluginMsg_FileIO_OpenReply>(BROWSER, |
123 PpapiHostMsg_FileIO_Open( | 133 PpapiHostMsg_FileIO_Open( |
124 file_ref, | 134 file_ref, |
125 open_flags), | 135 open_flags), |
126 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, | 136 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, |
127 callback)); | 137 callback)); |
128 | 138 |
129 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 139 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
130 return PP_OK_COMPLETIONPENDING; | 140 return PP_OK_COMPLETIONPENDING; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 Call<PpapiPluginMsg_FileIO_GeneralReply>(BROWSER, | 282 Call<PpapiPluginMsg_FileIO_GeneralReply>(BROWSER, |
273 PpapiHostMsg_FileIO_Flush(), | 283 PpapiHostMsg_FileIO_Flush(), |
274 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 284 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
275 callback)); | 285 callback)); |
276 | 286 |
277 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 287 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
278 return PP_OK_COMPLETIONPENDING; | 288 return PP_OK_COMPLETIONPENDING; |
279 } | 289 } |
280 | 290 |
281 void FileIOResource::Close() { | 291 void FileIOResource::Close() { |
282 if (file_handle_) { | 292 if (called_close_) |
| 293 return; |
| 294 |
| 295 called_close_ = true; |
| 296 if (file_handle_) |
283 file_handle_ = NULL; | 297 file_handle_ = NULL; |
284 } | 298 |
285 Post(BROWSER, PpapiHostMsg_FileIO_Close()); | 299 Post(BROWSER, PpapiHostMsg_FileIO_Close()); |
286 } | 300 } |
287 | 301 |
288 int32_t FileIOResource::RequestOSFileHandle( | 302 int32_t FileIOResource::RequestOSFileHandle( |
289 PP_FileHandle* handle, | 303 PP_FileHandle* handle, |
290 scoped_refptr<TrackedCallback> callback) { | 304 scoped_refptr<TrackedCallback> callback) { |
291 int32_t rv = state_manager_.CheckOperationState( | 305 int32_t rv = state_manager_.CheckOperationState( |
292 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 306 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
293 if (rv != PP_OK) | 307 if (rv != PP_OK) |
294 return rv; | 308 return rv; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 471 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
458 | 472 |
459 // End this operation now, so the user's callback can execute another FileIO | 473 // End this operation now, so the user's callback can execute another FileIO |
460 // operation, assuming there are no other pending operations. | 474 // operation, assuming there are no other pending operations. |
461 state_manager_.SetOperationFinished(); | 475 state_manager_.SetOperationFinished(); |
462 callback->Run(result); | 476 callback->Run(result); |
463 } | 477 } |
464 | 478 |
465 } // namespace proxy | 479 } // namespace proxy |
466 } // namespace ppapi | 480 } // namespace ppapi |
OLD | NEW |