| 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/files/file_util_proxy.h" |
| 8 #include "ipc/ipc_message.h" | 9 #include "ipc/ipc_message.h" |
| 9 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/proxy/plugin_globals.h" |
| 10 #include "ppapi/proxy/ppapi_messages.h" | 12 #include "ppapi/proxy/ppapi_messages.h" |
| 11 #include "ppapi/shared_impl/array_writer.h" | 13 #include "ppapi/shared_impl/array_writer.h" |
| 14 #include "ppapi/shared_impl/file_type_conversion.h" |
| 12 #include "ppapi/shared_impl/ppapi_globals.h" | 15 #include "ppapi/shared_impl/ppapi_globals.h" |
| 16 #include "ppapi/shared_impl/proxy_lock.h" |
| 13 #include "ppapi/shared_impl/resource_tracker.h" | 17 #include "ppapi/shared_impl/resource_tracker.h" |
| 14 #include "ppapi/thunk/enter.h" | 18 #include "ppapi/thunk/enter.h" |
| 15 #include "ppapi/thunk/ppb_file_ref_api.h" | 19 #include "ppapi/thunk/ppb_file_ref_api.h" |
| 16 | 20 |
| 17 using ppapi::thunk::EnterResourceNoLock; | 21 using ppapi::thunk::EnterResourceNoLock; |
| 18 using ppapi::thunk::PPB_FileIO_API; | 22 using ppapi::thunk::PPB_FileIO_API; |
| 19 using ppapi::thunk::PPB_FileRef_API; | 23 using ppapi::thunk::PPB_FileRef_API; |
| 20 | 24 |
| 25 namespace ppapi { |
| 26 namespace proxy { |
| 27 |
| 21 namespace { | 28 namespace { |
| 22 | 29 |
| 23 // An adapter to let Read() share the same implementation with ReadToArray(). | 30 // An adapter to let Read() share the same implementation with ReadToArray(). |
| 24 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { | 31 void* DummyGetDataBuffer(void* user_data, uint32_t count, uint32_t size) { |
| 25 return user_data; | 32 return user_data; |
| 26 } | 33 } |
| 27 | 34 |
| 35 // Dummy close callback allows us to call CloseFileHandle in the destructor. |
| 36 void DummyCloseCallback(base::PlatformFileError error_code) { |
| 37 } |
| 38 |
| 39 // We can't directly pass a reference to the resource to our callbacks because |
| 40 // when these hold the last reference, the resource destructor will run when we |
| 41 // are not holding the proxy lock. |
| 42 // Instead, pass a scoped_ptr to a scoped_refptr. This allows us to destroy |
| 43 // the resource while we hold the proxy lock. |
| 44 typedef scoped_ptr<scoped_refptr<FileIOResource> > PassedRef; |
| 45 |
| 46 void QueryCallback(PassedRef file_io_ref, |
| 47 scoped_refptr<TrackedCallback> callback, |
| 48 PP_FileInfo* output_info, |
| 49 base::PlatformFileError error_code, |
| 50 const base::PlatformFileInfo& file_info) { |
| 51 ProxyAutoLock lock; |
| 52 scoped_refptr<FileIOResource> file_io; |
| 53 file_io.swap(*file_io_ref.get()); |
| 54 file_io->OnQueryComplete(callback, output_info, error_code, file_info); |
| 55 } |
| 56 |
| 57 void ReadCallback(PassedRef file_io_ref, |
| 58 scoped_refptr<TrackedCallback> callback, |
| 59 PP_ArrayOutput array_output, |
| 60 base::PlatformFileError error_code, |
| 61 const char* data, int bytes_read) { |
| 62 ProxyAutoLock lock; |
| 63 scoped_refptr<FileIOResource> file_io; |
| 64 file_io.swap(*file_io_ref.get()); |
| 65 file_io->OnReadComplete(callback, array_output, error_code, data, bytes_read); |
| 66 } |
| 67 |
| 28 } // namespace | 68 } // namespace |
| 29 | 69 |
| 30 namespace ppapi { | |
| 31 namespace proxy { | |
| 32 | |
| 33 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) | 70 FileIOResource::FileIOResource(Connection connection, PP_Instance instance) |
| 34 : PluginResource(connection, instance) { | 71 : PluginResource(connection, instance), |
| 72 file_handle_(PP_kInvalidFileHandle), |
| 73 file_system_type_(PP_FILESYSTEMTYPE_INVALID) { |
| 35 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); | 74 SendCreate(RENDERER, PpapiHostMsg_FileIO_Create()); |
| 36 } | 75 } |
| 37 | 76 |
| 38 FileIOResource::~FileIOResource() { | 77 FileIOResource::~FileIOResource() { |
| 78 CloseFileHandle(); |
| 39 } | 79 } |
| 40 | 80 |
| 41 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { | 81 PPB_FileIO_API* FileIOResource::AsPPB_FileIO_API() { |
| 42 return this; | 82 return this; |
| 43 } | 83 } |
| 44 | 84 |
| 45 int32_t FileIOResource::Open(PP_Resource file_ref, | 85 int32_t FileIOResource::Open(PP_Resource file_ref, |
| 46 int32_t open_flags, | 86 int32_t open_flags, |
| 47 scoped_refptr<TrackedCallback> callback) { | 87 scoped_refptr<TrackedCallback> callback) { |
| 48 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); | 88 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref, true); |
| 49 if (enter.failed()) | 89 if (enter.failed()) |
| 50 return PP_ERROR_BADRESOURCE; | 90 return PP_ERROR_BADRESOURCE; |
| 51 | 91 |
| 92 PPB_FileRef_API* file_ref_api = enter.object(); |
| 93 PP_FileSystemType type = file_ref_api->GetFileSystemType(); |
| 94 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 95 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && |
| 96 type != PP_FILESYSTEMTYPE_EXTERNAL && |
| 97 type != PP_FILESYSTEMTYPE_ISOLATED) |
| 98 return PP_ERROR_FAILED; |
| 99 file_system_type_ = type; |
| 100 |
| 52 int32_t rv = state_manager_.CheckOperationState( | 101 int32_t rv = state_manager_.CheckOperationState( |
| 53 FileIOStateManager::OPERATION_EXCLUSIVE, false); | 102 FileIOStateManager::OPERATION_EXCLUSIVE, false); |
| 54 if (rv != PP_OK) | 103 if (rv != PP_OK) |
| 55 return rv; | 104 return rv; |
| 56 | 105 |
| 57 Call<PpapiPluginMsg_FileIO_OpenReply>(RENDERER, | 106 Call<PpapiPluginMsg_FileIO_OpenReply>(RENDERER, |
| 58 PpapiHostMsg_FileIO_Open( | 107 PpapiHostMsg_FileIO_Open( |
| 59 enter.resource()->host_resource().host_resource(), | 108 enter.resource()->host_resource().host_resource(), |
| 60 open_flags), | 109 open_flags), |
| 61 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, | 110 base::Bind(&FileIOResource::OnPluginMsgOpenFileComplete, this, |
| 62 callback)); | 111 callback)); |
| 63 | 112 |
| 64 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 113 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 65 return PP_OK_COMPLETIONPENDING; | 114 return PP_OK_COMPLETIONPENDING; |
| 66 } | 115 } |
| 67 | 116 |
| 68 int32_t FileIOResource::Query(PP_FileInfo* info, | 117 int32_t FileIOResource::Query(PP_FileInfo* info, |
| 69 scoped_refptr<TrackedCallback> callback) { | 118 scoped_refptr<TrackedCallback> callback) { |
| 70 int32_t rv = state_manager_.CheckOperationState( | 119 int32_t rv = state_manager_.CheckOperationState( |
| 71 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 120 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 72 if (rv != PP_OK) | 121 if (rv != PP_OK) |
| 73 return rv; | 122 return rv; |
| 74 | 123 |
| 75 Call<PpapiPluginMsg_FileIO_QueryReply>(RENDERER, | 124 if (file_handle_ == base::kInvalidPlatformFileValue) |
| 76 PpapiHostMsg_FileIO_Query(), | 125 return PP_ERROR_FAILED; |
| 77 base::Bind(&FileIOResource::OnPluginMsgQueryComplete, this, | |
| 78 callback, info)); | |
| 79 | 126 |
| 127 PassedRef passed_ref(new scoped_refptr<FileIOResource>(this)); |
| 128 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( |
| 129 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()), |
| 130 file_handle_, |
| 131 base::Bind(&QueryCallback, base::Passed(&passed_ref), |
| 132 callback, info))) { |
| 133 return PP_ERROR_FAILED; |
| 134 } |
| 80 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 135 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 81 return PP_OK_COMPLETIONPENDING; | 136 return PP_OK_COMPLETIONPENDING; |
| 82 } | 137 } |
| 83 | 138 |
| 84 int32_t FileIOResource::Touch(PP_Time last_access_time, | 139 int32_t FileIOResource::Touch(PP_Time last_access_time, |
| 85 PP_Time last_modified_time, | 140 PP_Time last_modified_time, |
| 86 scoped_refptr<TrackedCallback> callback) { | 141 scoped_refptr<TrackedCallback> callback) { |
| 87 int32_t rv = state_manager_.CheckOperationState( | 142 int32_t rv = state_manager_.CheckOperationState( |
| 88 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 143 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 89 if (rv != PP_OK) | 144 if (rv != PP_OK) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 103 int32_t bytes_to_read, | 158 int32_t bytes_to_read, |
| 104 scoped_refptr<TrackedCallback> callback) { | 159 scoped_refptr<TrackedCallback> callback) { |
| 105 int32_t rv = state_manager_.CheckOperationState( | 160 int32_t rv = state_manager_.CheckOperationState( |
| 106 FileIOStateManager::OPERATION_READ, true); | 161 FileIOStateManager::OPERATION_READ, true); |
| 107 if (rv != PP_OK) | 162 if (rv != PP_OK) |
| 108 return rv; | 163 return rv; |
| 109 | 164 |
| 110 PP_ArrayOutput output_adapter; | 165 PP_ArrayOutput output_adapter; |
| 111 output_adapter.GetDataBuffer = &DummyGetDataBuffer; | 166 output_adapter.GetDataBuffer = &DummyGetDataBuffer; |
| 112 output_adapter.user_data = buffer; | 167 output_adapter.user_data = buffer; |
| 113 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | |
| 114 return ReadValidated(offset, bytes_to_read, output_adapter, callback); | 168 return ReadValidated(offset, bytes_to_read, output_adapter, callback); |
| 115 } | 169 } |
| 116 | 170 |
| 117 int32_t FileIOResource::ReadToArray(int64_t offset, | 171 int32_t FileIOResource::ReadToArray(int64_t offset, |
| 118 int32_t max_read_length, | 172 int32_t max_read_length, |
| 119 PP_ArrayOutput* array_output, | 173 PP_ArrayOutput* array_output, |
| 120 scoped_refptr<TrackedCallback> callback) { | 174 scoped_refptr<TrackedCallback> callback) { |
| 121 DCHECK(array_output); | 175 DCHECK(array_output); |
| 122 int32_t rv = state_manager_.CheckOperationState( | 176 int32_t rv = state_manager_.CheckOperationState( |
| 123 FileIOStateManager::OPERATION_READ, true); | 177 FileIOStateManager::OPERATION_READ, true); |
| 124 if (rv != PP_OK) | 178 if (rv != PP_OK) |
| 125 return rv; | 179 return rv; |
| 126 | 180 |
| 127 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); | |
| 128 return ReadValidated(offset, max_read_length, *array_output, callback); | 181 return ReadValidated(offset, max_read_length, *array_output, callback); |
| 129 } | 182 } |
| 130 | 183 |
| 131 int32_t FileIOResource::Write(int64_t offset, | 184 int32_t FileIOResource::Write(int64_t offset, |
| 132 const char* buffer, | 185 const char* buffer, |
| 133 int32_t bytes_to_write, | 186 int32_t bytes_to_write, |
| 134 scoped_refptr<TrackedCallback> callback) { | 187 scoped_refptr<TrackedCallback> callback) { |
| 135 int32_t rv = state_manager_.CheckOperationState( | 188 int32_t rv = state_manager_.CheckOperationState( |
| 136 FileIOStateManager::OPERATION_WRITE, true); | 189 FileIOStateManager::OPERATION_WRITE, true); |
| 137 if (rv != PP_OK) | 190 if (rv != PP_OK) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, | 227 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, |
| 175 PpapiHostMsg_FileIO_Flush(), | 228 PpapiHostMsg_FileIO_Flush(), |
| 176 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 229 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 177 callback)); | 230 callback)); |
| 178 | 231 |
| 179 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 232 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 180 return PP_OK_COMPLETIONPENDING; | 233 return PP_OK_COMPLETIONPENDING; |
| 181 } | 234 } |
| 182 | 235 |
| 183 void FileIOResource::Close() { | 236 void FileIOResource::Close() { |
| 237 CloseFileHandle(); |
| 184 Post(RENDERER, PpapiHostMsg_FileIO_Close()); | 238 Post(RENDERER, PpapiHostMsg_FileIO_Close()); |
| 185 } | 239 } |
| 186 | 240 |
| 187 int32_t FileIOResource::GetOSFileDescriptor() { | 241 int32_t FileIOResource::GetOSFileDescriptor() { |
| 188 int32_t file_descriptor; | 242 int32_t file_descriptor; |
| 189 // Only available when running in process. | 243 // Only available when running in process. |
| 190 SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>( | 244 SyncCall<PpapiPluginMsg_FileIO_GetOSFileDescriptorReply>( |
| 191 RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor); | 245 RENDERER, PpapiHostMsg_FileIO_GetOSFileDescriptor(), &file_descriptor); |
| 192 return file_descriptor; | 246 return file_descriptor; |
| 193 } | 247 } |
| 194 | 248 |
| 249 int32_t FileIOResource::RequestOSFileHandle( |
| 250 PP_FileHandle* handle, |
| 251 scoped_refptr<TrackedCallback> callback) { |
| 252 int32_t rv = state_manager_.CheckOperationState( |
| 253 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 254 if (rv != PP_OK) |
| 255 return rv; |
| 256 |
| 257 Call<PpapiPluginMsg_FileIO_RequestOSFileHandleReply>(RENDERER, |
| 258 PpapiHostMsg_FileIO_RequestOSFileHandle(), |
| 259 base::Bind(&FileIOResource::OnPluginMsgRequestOSFileHandleComplete, this, |
| 260 callback, handle)); |
| 261 |
| 262 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 263 return PP_OK_COMPLETIONPENDING; |
| 264 } |
| 265 |
| 195 int32_t FileIOResource::WillWrite(int64_t offset, | 266 int32_t FileIOResource::WillWrite(int64_t offset, |
| 196 int32_t bytes_to_write, | 267 int32_t bytes_to_write, |
| 197 scoped_refptr<TrackedCallback> callback) { | 268 scoped_refptr<TrackedCallback> callback) { |
| 198 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, | 269 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, |
| 199 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), | 270 PpapiHostMsg_FileIO_WillWrite(offset, bytes_to_write), |
| 200 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 271 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 201 callback)); | 272 callback)); |
| 202 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 273 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 203 return PP_OK_COMPLETIONPENDING; | 274 return PP_OK_COMPLETIONPENDING; |
| 204 } | 275 } |
| 205 | 276 |
| 206 int32_t FileIOResource::WillSetLength(int64_t length, | 277 int32_t FileIOResource::WillSetLength(int64_t length, |
| 207 scoped_refptr<TrackedCallback> callback) { | 278 scoped_refptr<TrackedCallback> callback) { |
| 208 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, | 279 Call<PpapiPluginMsg_FileIO_GeneralReply>(RENDERER, |
| 209 PpapiHostMsg_FileIO_WillSetLength(length), | 280 PpapiHostMsg_FileIO_WillSetLength(length), |
| 210 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, | 281 base::Bind(&FileIOResource::OnPluginMsgGeneralComplete, this, |
| 211 callback)); | 282 callback)); |
| 212 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 283 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 213 return PP_OK_COMPLETIONPENDING; | 284 return PP_OK_COMPLETIONPENDING; |
| 214 } | 285 } |
| 215 | 286 |
| 216 int32_t FileIOResource::ReadValidated(int64_t offset, | 287 int32_t FileIOResource::ReadValidated(int64_t offset, |
| 217 int32_t bytes_to_read, | 288 int32_t bytes_to_read, |
| 218 const PP_ArrayOutput& array_output, | 289 const PP_ArrayOutput& array_output, |
| 219 scoped_refptr<TrackedCallback> callback) { | 290 scoped_refptr<TrackedCallback> callback) { |
| 220 Call<PpapiPluginMsg_FileIO_ReadReply>(RENDERER, | 291 if (file_handle_ == base::kInvalidPlatformFileValue) |
| 221 PpapiHostMsg_FileIO_Read(offset, bytes_to_read), | 292 return PP_ERROR_FAILED; |
| 222 base::Bind(&FileIOResource::OnPluginMsgReadComplete, this, | 293 |
| 223 callback, array_output)); | 294 PassedRef passed_ref(new scoped_refptr<FileIOResource>(this)); |
| 295 if (!base::FileUtilProxy::Read( |
| 296 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()), |
| 297 file_handle_, |
| 298 offset, |
| 299 bytes_to_read, |
| 300 base::Bind(&ReadCallback, base::Passed(&passed_ref), |
| 301 callback, array_output))) { |
| 302 return PP_ERROR_FAILED; |
| 303 } |
| 304 |
| 305 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); |
| 224 return PP_OK_COMPLETIONPENDING; | 306 return PP_OK_COMPLETIONPENDING; |
| 225 } | 307 } |
| 226 | 308 |
| 227 int32_t FileIOResource::RequestOSFileHandle( | 309 void FileIOResource::CloseFileHandle() { |
| 228 PP_FileHandle* handle, | 310 if (file_handle_ != base::kInvalidPlatformFileValue) { |
| 229 scoped_refptr<TrackedCallback> callback) { | 311 base::FileUtilProxy::Close( |
| 230 int32_t rv = state_manager_.CheckOperationState( | 312 PpapiGlobals::Get()->GetFileTaskRunner(pp_instance()), |
| 231 FileIOStateManager::OPERATION_EXCLUSIVE, true); | 313 file_handle_, |
| 232 if (rv != PP_OK) | 314 base::Bind(&DummyCloseCallback)); |
| 233 return rv; | 315 file_handle_ = base::kInvalidPlatformFileValue; |
| 316 } |
| 317 } |
| 234 | 318 |
| 235 Call<PpapiPluginMsg_FileIO_RequestOSFileHandleReply>(RENDERER, | 319 void FileIOResource::OnQueryComplete( |
| 236 PpapiHostMsg_FileIO_RequestOSFileHandle(), | 320 scoped_refptr<TrackedCallback> callback, |
| 237 base::Bind(&FileIOResource::OnPluginMsgRequestOSFileHandleComplete, this, | 321 PP_FileInfo* output_info, |
| 238 callback, handle)); | 322 base::PlatformFileError error_code, |
| 323 const base::PlatformFileInfo& file_info) { |
| 324 DCHECK(state_manager_.get_pending_operation() == |
| 325 FileIOStateManager::OPERATION_EXCLUSIVE); |
| 239 | 326 |
| 240 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | 327 if (!TrackedCallback::IsPending(callback)) { |
| 241 return PP_OK_COMPLETIONPENDING; | 328 state_manager_.SetOperationFinished(); |
| 329 return; |
| 330 } |
| 331 |
| 332 int32_t result = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 333 if (result == PP_OK) { |
| 334 ppapi::PlatformFileInfoToPepperFileInfo(file_info, file_system_type_, |
| 335 output_info); |
| 336 } |
| 337 |
| 338 // End the operation now. The callback may perform another file operation. |
| 339 state_manager_.SetOperationFinished(); |
| 340 callback->Run(result); |
| 341 } |
| 342 |
| 343 void FileIOResource::OnReadComplete( |
| 344 scoped_refptr<TrackedCallback> callback, |
| 345 PP_ArrayOutput array_output, |
| 346 base::PlatformFileError error_code, |
| 347 const char* data, int bytes_read) { |
| 348 DCHECK(state_manager_.get_pending_operation() == |
| 349 FileIOStateManager::OPERATION_READ); |
| 350 |
| 351 if (!TrackedCallback::IsPending(callback)) { |
| 352 state_manager_.SetOperationFinished(); |
| 353 return; |
| 354 } |
| 355 |
| 356 int32_t result = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 357 if (result == PP_OK) { |
| 358 result = std::max(0, bytes_read); |
| 359 ArrayWriter output; |
| 360 output.set_pp_array_output(array_output); |
| 361 if (output.is_valid()) |
| 362 output.StoreArray(data, result); |
| 363 else |
| 364 result = PP_ERROR_FAILED; |
| 365 } |
| 366 |
| 367 // End the operation now. The callback may perform another file operation. |
| 368 state_manager_.SetOperationFinished(); |
| 369 callback->Run(result); |
| 242 } | 370 } |
| 243 | 371 |
| 244 void FileIOResource::OnPluginMsgGeneralComplete( | 372 void FileIOResource::OnPluginMsgGeneralComplete( |
| 245 scoped_refptr<TrackedCallback> callback, | 373 scoped_refptr<TrackedCallback> callback, |
| 246 const ResourceMessageReplyParams& params) { | 374 const ResourceMessageReplyParams& params) { |
| 247 DCHECK(state_manager_.get_pending_operation() == | 375 DCHECK(state_manager_.get_pending_operation() == |
| 248 FileIOStateManager::OPERATION_EXCLUSIVE || | 376 FileIOStateManager::OPERATION_EXCLUSIVE || |
| 249 state_manager_.get_pending_operation() == | 377 state_manager_.get_pending_operation() == |
| 250 FileIOStateManager::OPERATION_WRITE); | 378 FileIOStateManager::OPERATION_WRITE); |
| 251 // End the operation now. The callback may perform another file operation. | 379 // End the operation now. The callback may perform another file operation. |
| 252 state_manager_.SetOperationFinished(); | 380 state_manager_.SetOperationFinished(); |
| 253 callback->Run(params.result()); | 381 callback->Run(params.result()); |
| 254 } | 382 } |
| 255 | 383 |
| 256 void FileIOResource::OnPluginMsgOpenFileComplete( | 384 void FileIOResource::OnPluginMsgOpenFileComplete( |
| 257 scoped_refptr<TrackedCallback> callback, | 385 scoped_refptr<TrackedCallback> callback, |
| 258 const ResourceMessageReplyParams& params) { | 386 const ResourceMessageReplyParams& params) { |
| 259 DCHECK(state_manager_.get_pending_operation() == | 387 DCHECK(state_manager_.get_pending_operation() == |
| 260 FileIOStateManager::OPERATION_EXCLUSIVE); | 388 FileIOStateManager::OPERATION_EXCLUSIVE); |
| 261 if (params.result() == PP_OK) | 389 if (params.result() == PP_OK) |
| 262 state_manager_.SetOpenSucceed(); | 390 state_manager_.SetOpenSucceed(); |
| 391 |
| 392 int32_t result = params.result(); |
| 393 IPC::PlatformFileForTransit transit_file; |
| 394 if (result == PP_OK && !params.TakeFileHandleAtIndex(0, &transit_file)) |
| 395 result = PP_ERROR_FAILED; |
| 396 file_handle_ = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
| 397 |
| 263 // End the operation now. The callback may perform another file operation. | 398 // End the operation now. The callback may perform another file operation. |
| 264 state_manager_.SetOperationFinished(); | 399 state_manager_.SetOperationFinished(); |
| 265 callback->Run(params.result()); | 400 callback->Run(params.result()); |
| 266 } | 401 } |
| 267 | 402 |
| 268 void FileIOResource::OnPluginMsgQueryComplete( | |
| 269 scoped_refptr<TrackedCallback> callback, | |
| 270 PP_FileInfo* output_info, | |
| 271 const ResourceMessageReplyParams& params, | |
| 272 const PP_FileInfo& info) { | |
| 273 DCHECK(state_manager_.get_pending_operation() == | |
| 274 FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 275 *output_info = info; | |
| 276 // End the operation now. The callback may perform another file operation. | |
| 277 state_manager_.SetOperationFinished(); | |
| 278 callback->Run(params.result()); | |
| 279 } | |
| 280 | |
| 281 void FileIOResource::OnPluginMsgReadComplete( | |
| 282 scoped_refptr<TrackedCallback> callback, | |
| 283 PP_ArrayOutput array_output, | |
| 284 const ResourceMessageReplyParams& params, | |
| 285 const std::string& data) { | |
| 286 DCHECK(state_manager_.get_pending_operation() == | |
| 287 FileIOStateManager::OPERATION_READ); | |
| 288 | |
| 289 // The result code should contain the data size if it's positive. | |
| 290 int32_t result = params.result(); | |
| 291 DCHECK((result < 0 && data.size() == 0) || | |
| 292 result == static_cast<int32_t>(data.size())); | |
| 293 | |
| 294 ArrayWriter output; | |
| 295 output.set_pp_array_output(array_output); | |
| 296 if (output.is_valid()) | |
| 297 output.StoreArray(data.data(), std::max(0, result)); | |
| 298 else | |
| 299 result = PP_ERROR_FAILED; | |
| 300 | |
| 301 // End the operation now. The callback may perform another file operation. | |
| 302 state_manager_.SetOperationFinished(); | |
| 303 callback->Run(result); | |
| 304 } | |
| 305 | |
| 306 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( | 403 void FileIOResource::OnPluginMsgRequestOSFileHandleComplete( |
| 307 scoped_refptr<TrackedCallback> callback, | 404 scoped_refptr<TrackedCallback> callback, |
| 308 PP_FileHandle* output_handle, | 405 PP_FileHandle* output_handle, |
| 309 const ResourceMessageReplyParams& params) { | 406 const ResourceMessageReplyParams& params) { |
| 310 DCHECK(state_manager_.get_pending_operation() == | 407 DCHECK(state_manager_.get_pending_operation() == |
| 311 FileIOStateManager::OPERATION_EXCLUSIVE); | 408 FileIOStateManager::OPERATION_EXCLUSIVE); |
| 312 | 409 |
| 313 if (!TrackedCallback::IsPending(callback)) { | 410 if (!TrackedCallback::IsPending(callback)) { |
| 314 state_manager_.SetOperationFinished(); | 411 state_manager_.SetOperationFinished(); |
| 315 return; | 412 return; |
| 316 } | 413 } |
| 317 | 414 |
| 318 int32_t result = params.result(); | 415 int32_t result = params.result(); |
| 319 IPC::PlatformFileForTransit transit_file; | 416 IPC::PlatformFileForTransit transit_file; |
| 320 if (!params.TakeFileHandleAtIndex(0, &transit_file)) | 417 if (!params.TakeFileHandleAtIndex(0, &transit_file)) |
| 321 result = PP_ERROR_FAILED; | 418 result = PP_ERROR_FAILED; |
| 322 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 419 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
| 323 | 420 |
| 324 // End the operation now. The callback may perform another file operation. | 421 // End the operation now. The callback may perform another file operation. |
| 325 state_manager_.SetOperationFinished(); | 422 state_manager_.SetOperationFinished(); |
| 326 callback->Run(result); | 423 callback->Run(result); |
| 327 } | 424 } |
| 328 | 425 |
| 329 } // namespace proxy | 426 } // namespace proxy |
| 330 } // namespace ppapi | 427 } // namespace ppapi |
| OLD | NEW |