OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/pepper/pepper_file_io_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/file_util_proxy.h" |
| 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/host/dispatch_host_message.h" |
| 12 #include "ppapi/host/ppapi_host.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" |
| 14 #include "ppapi/shared_impl/file_type_conversion.h" |
| 15 #include "ppapi/shared_impl/time_conversion.h" |
| 16 #include "ppapi/thunk/enter.h" |
| 17 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| 18 #include "webkit/plugins/ppapi/file_callbacks.h" |
| 19 #include "webkit/plugins/ppapi/host_globals.h" |
| 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 21 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| 22 #include "webkit/plugins/ppapi/quota_file_io.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 using ppapi::FileIOStateManager; |
| 27 using ppapi::PPTimeToTime; |
| 28 using ppapi::TimeToPPTime; |
| 29 using ppapi::host::ReplyMessageContext; |
| 30 using ppapi::thunk::EnterResourceNoLock; |
| 31 using ppapi::thunk::PPB_FileRef_API; |
| 32 using webkit::ppapi::PPB_FileRef_Impl; |
| 33 using webkit::ppapi::PluginDelegate; |
| 34 |
| 35 namespace { |
| 36 |
| 37 // The maximum size we'll support reading in one chunk. The renderer process |
| 38 // must allocate a buffer sized according to the request of the plugin. To |
| 39 // keep things from getting out of control, we cap the read size to this value. |
| 40 // This should generally be OK since the API specifies that it may perform a |
| 41 // partial read. |
| 42 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB |
| 43 |
| 44 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; |
| 45 |
| 46 class PlatformGeneralCallbackTranslator |
| 47 : public fileapi::FileSystemCallbackDispatcher { |
| 48 public: |
| 49 PlatformGeneralCallbackTranslator(const PlatformGeneralCallback& callback) |
| 50 : callback_(callback) {} |
| 51 |
| 52 virtual ~PlatformGeneralCallbackTranslator() {} |
| 53 |
| 54 virtual void DidSucceed() OVERRIDE { |
| 55 callback_.Run(base::PLATFORM_FILE_OK); |
| 56 } |
| 57 |
| 58 virtual void DidReadMetadata(const base::PlatformFileInfo& file_info, |
| 59 const FilePath& platform_path) OVERRIDE { |
| 60 NOTREACHED(); |
| 61 } |
| 62 |
| 63 virtual void DidReadDirectory( |
| 64 const std::vector<base::FileUtilProxy::Entry>& entries, |
| 65 bool has_more) OVERRIDE { |
| 66 NOTREACHED(); |
| 67 } |
| 68 |
| 69 virtual void DidOpenFileSystem(const std::string& name, |
| 70 const GURL& root) OVERRIDE { |
| 71 NOTREACHED(); |
| 72 } |
| 73 |
| 74 virtual void DidFail(base::PlatformFileError error_code) OVERRIDE { |
| 75 callback_.Run(error_code); |
| 76 } |
| 77 |
| 78 virtual void DidWrite(int64 bytes, bool complete) OVERRIDE { |
| 79 NOTREACHED(); |
| 80 } |
| 81 |
| 82 virtual void DidOpenFile(base::PlatformFile file) OVERRIDE { |
| 83 NOTREACHED(); |
| 84 } |
| 85 |
| 86 private: |
| 87 PlatformGeneralCallback callback_; |
| 88 }; |
| 89 |
| 90 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { |
| 91 // On the plugin side, some callbacks expect a parameter that means different |
| 92 // things depending on whether is negative or not. We translate for those |
| 93 // callbacks here. |
| 94 return pp_error == PP_OK ? byte_number : pp_error; |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 |
| 99 PepperFileIOHost::PepperFileIOHost(RendererPpapiHost* host, |
| 100 PP_Instance instance, |
| 101 PP_Resource resource) |
| 102 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 103 file_(base::kInvalidPlatformFileValue), |
| 104 file_system_type_(PP_FILESYSTEMTYPE_INVALID), |
| 105 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 106 // TODO(victorhsieh): eliminate plugin_delegate_ as it's no longer needed. |
| 107 webkit::ppapi::PluginInstance* plugin_instance = |
| 108 webkit::ppapi::HostGlobals::Get()->GetInstance(instance); |
| 109 plugin_delegate_ = plugin_instance ? plugin_instance->delegate() : NULL; |
| 110 } |
| 111 |
| 112 PepperFileIOHost::~PepperFileIOHost() { |
| 113 OnHostMsgClose(NULL); |
| 114 } |
| 115 |
| 116 int32_t PepperFileIOHost::OnResourceMessageReceived( |
| 117 const IPC::Message& msg, |
| 118 ppapi::host::HostMessageContext* context) { |
| 119 IPC_BEGIN_MESSAGE_MAP(PepperFileIOHost, msg) |
| 120 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open, |
| 121 OnHostMsgOpen) |
| 122 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Query, |
| 123 OnHostMsgQuery) |
| 124 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch, |
| 125 OnHostMsgTouch) |
| 126 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Read, |
| 127 OnHostMsgRead) |
| 128 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Write, |
| 129 OnHostMsgWrite) |
| 130 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength, |
| 131 OnHostMsgSetLength) |
| 132 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush, |
| 133 OnHostMsgFlush) |
| 134 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Close, |
| 135 OnHostMsgClose) |
| 136 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_WillWrite, |
| 137 OnHostMsgWillWrite) |
| 138 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_WillSetLength, |
| 139 OnHostMsgWillSetLength) |
| 140 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_GetOSFileDescriptor, |
| 141 OnHostMsgGetOSFileDescriptor) |
| 142 IPC_END_MESSAGE_MAP() |
| 143 return PP_ERROR_FAILED; |
| 144 } |
| 145 |
| 146 int32_t PepperFileIOHost::OnHostMsgOpen( |
| 147 ppapi::host::HostMessageContext* context, |
| 148 PP_Resource file_ref_resource, |
| 149 int32_t open_flags) { |
| 150 int32_t rv = state_manager_.CheckOperationState( |
| 151 FileIOStateManager::OPERATION_EXCLUSIVE, false); |
| 152 if (rv != PP_OK) |
| 153 return rv; |
| 154 |
| 155 int flags = 0; |
| 156 if (!::ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) |
| 157 return PP_ERROR_BADARGUMENT; |
| 158 |
| 159 EnterResourceNoLock<PPB_FileRef_API> enter(file_ref_resource, true); |
| 160 if (enter.failed()) |
| 161 return PP_ERROR_BADRESOURCE; |
| 162 |
| 163 PPB_FileRef_API* file_ref_api = enter.object(); |
| 164 PP_FileSystemType type = file_ref_api->GetFileSystemType(); |
| 165 if (type != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 166 type != PP_FILESYSTEMTYPE_LOCALTEMPORARY && |
| 167 type != PP_FILESYSTEMTYPE_EXTERNAL) |
| 168 return PP_ERROR_FAILED; |
| 169 file_system_type_ = type; |
| 170 |
| 171 if (!plugin_delegate_) |
| 172 return PP_ERROR_FAILED; |
| 173 |
| 174 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api); |
| 175 if (file_ref->HasValidFileSystem()) { |
| 176 file_system_url_ = file_ref->GetFileSystemURL(); |
| 177 if (!plugin_delegate_->AsyncOpenFileSystemURL( |
| 178 file_system_url_, flags, |
| 179 base::Bind( |
| 180 &PepperFileIOHost::ExecutePlatformOpenFileSystemURLCallback, |
| 181 weak_factory_.GetWeakPtr(), |
| 182 context->MakeReplyMessageContext()))) |
| 183 return PP_ERROR_FAILED; |
| 184 } else { |
| 185 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) |
| 186 return PP_ERROR_FAILED; |
| 187 if (!plugin_delegate_->AsyncOpenFile( |
| 188 file_ref->GetSystemPath(), flags, |
| 189 base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback, |
| 190 weak_factory_.GetWeakPtr(), |
| 191 context->MakeReplyMessageContext()))) |
| 192 return PP_ERROR_FAILED; |
| 193 } |
| 194 |
| 195 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 196 return PP_OK_COMPLETIONPENDING; |
| 197 } |
| 198 |
| 199 int32_t PepperFileIOHost::OnHostMsgQuery( |
| 200 ppapi::host::HostMessageContext* context) { |
| 201 int32_t rv = state_manager_.CheckOperationState( |
| 202 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 203 if (rv != PP_OK) |
| 204 return rv; |
| 205 |
| 206 if (!plugin_delegate_) |
| 207 return PP_ERROR_FAILED; |
| 208 |
| 209 if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( |
| 210 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, |
| 211 base::Bind(&PepperFileIOHost::ExecutePlatformQueryCallback, |
| 212 weak_factory_.GetWeakPtr(), |
| 213 context->MakeReplyMessageContext()))) |
| 214 return PP_ERROR_FAILED; |
| 215 |
| 216 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 217 return PP_OK_COMPLETIONPENDING; |
| 218 } |
| 219 |
| 220 int32_t PepperFileIOHost::OnHostMsgTouch( |
| 221 ppapi::host::HostMessageContext* context, |
| 222 PP_Time last_access_time, |
| 223 PP_Time last_modified_time) { |
| 224 int32_t rv = state_manager_.CheckOperationState( |
| 225 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 226 if (rv != PP_OK) |
| 227 return rv; |
| 228 |
| 229 if (!plugin_delegate_) |
| 230 return PP_ERROR_FAILED; |
| 231 |
| 232 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 233 if (!plugin_delegate_->Touch( |
| 234 file_system_url_, |
| 235 PPTimeToTime(last_access_time), |
| 236 PPTimeToTime(last_modified_time), |
| 237 new PlatformGeneralCallbackTranslator( |
| 238 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 239 weak_factory_.GetWeakPtr(), |
| 240 context->MakeReplyMessageContext())))) |
| 241 return PP_ERROR_FAILED; |
| 242 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 243 return PP_OK_COMPLETIONPENDING; |
| 244 } |
| 245 |
| 246 // TODO(nhiroki): fix a failure of FileIO.Touch for an external filesystem on |
| 247 // Mac and Linux due to sandbox restrictions (http://crbug.com/101128). |
| 248 if (!base::FileUtilProxy::Touch( |
| 249 plugin_delegate_->GetFileThreadMessageLoopProxy(), |
| 250 file_, PPTimeToTime(last_access_time), |
| 251 PPTimeToTime(last_modified_time), |
| 252 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 253 weak_factory_.GetWeakPtr(), |
| 254 context->MakeReplyMessageContext()))) |
| 255 return PP_ERROR_FAILED; |
| 256 |
| 257 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 258 return PP_OK_COMPLETIONPENDING; |
| 259 } |
| 260 |
| 261 int32_t PepperFileIOHost::OnHostMsgRead( |
| 262 ppapi::host::HostMessageContext* context, |
| 263 int64_t offset, |
| 264 int32_t max_read_length) { |
| 265 int32_t rv = state_manager_.CheckOperationState( |
| 266 FileIOStateManager::OPERATION_READ, true); |
| 267 if (rv != PP_OK) |
| 268 return rv; |
| 269 |
| 270 // Validate max_read_length before allocating below. This value is coming from |
| 271 // the untrusted plugin. |
| 272 if (max_read_length < 0) { |
| 273 ReplyMessageContext reply_context = context->MakeReplyMessageContext(); |
| 274 reply_context.params.set_result(PP_ERROR_FAILED); |
| 275 host()->SendReply(reply_context, |
| 276 PpapiPluginMsg_FileIO_ReadReply(std::string())); |
| 277 return PP_OK_COMPLETIONPENDING; |
| 278 } |
| 279 |
| 280 if (!plugin_delegate_) |
| 281 return PP_ERROR_FAILED; |
| 282 |
| 283 if (!base::FileUtilProxy::Read( |
| 284 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, offset, |
| 285 max_read_length, |
| 286 base::Bind(&PepperFileIOHost::ExecutePlatformReadCallback, |
| 287 weak_factory_.GetWeakPtr(), |
| 288 context->MakeReplyMessageContext()))) |
| 289 return PP_ERROR_FAILED; |
| 290 |
| 291 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); |
| 292 return PP_OK_COMPLETIONPENDING; |
| 293 } |
| 294 |
| 295 int32_t PepperFileIOHost::OnHostMsgWrite( |
| 296 ppapi::host::HostMessageContext* context, |
| 297 int64_t offset, |
| 298 const std::string& buffer) { |
| 299 int32_t rv = state_manager_.CheckOperationState( |
| 300 FileIOStateManager::OPERATION_WRITE, true); |
| 301 if (rv != PP_OK) |
| 302 return rv; |
| 303 |
| 304 if (quota_file_io_.get()) { |
| 305 if (!quota_file_io_->Write( |
| 306 offset, buffer.c_str(), buffer.size(), |
| 307 base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback, |
| 308 weak_factory_.GetWeakPtr(), |
| 309 context->MakeReplyMessageContext()))) |
| 310 return PP_ERROR_FAILED; |
| 311 } else { |
| 312 if (!plugin_delegate_) |
| 313 return PP_ERROR_FAILED; |
| 314 |
| 315 if (!base::FileUtilProxy::Write( |
| 316 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, offset, |
| 317 buffer.c_str(), buffer.size(), |
| 318 base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback, |
| 319 weak_factory_.GetWeakPtr(), |
| 320 context->MakeReplyMessageContext()))) |
| 321 return PP_ERROR_FAILED; |
| 322 } |
| 323 |
| 324 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_WRITE); |
| 325 return PP_OK_COMPLETIONPENDING; |
| 326 } |
| 327 |
| 328 int32_t PepperFileIOHost::OnHostMsgSetLength( |
| 329 ppapi::host::HostMessageContext* context, |
| 330 int64_t length) { |
| 331 int32_t rv = state_manager_.CheckOperationState( |
| 332 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 333 if (rv != PP_OK) |
| 334 return rv; |
| 335 |
| 336 if (!plugin_delegate_) |
| 337 return PP_ERROR_FAILED; |
| 338 |
| 339 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 340 if (!plugin_delegate_->SetLength( |
| 341 file_system_url_, length, |
| 342 new PlatformGeneralCallbackTranslator( |
| 343 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 344 weak_factory_.GetWeakPtr(), |
| 345 context->MakeReplyMessageContext())))) |
| 346 return PP_ERROR_FAILED; |
| 347 } else { |
| 348 // TODO(nhiroki): fix a failure of FileIO.SetLength for an external |
| 349 // filesystem on Mac due to sandbox restrictions (http://crbug.com/156077). |
| 350 if (!base::FileUtilProxy::Truncate( |
| 351 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, length, |
| 352 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 353 weak_factory_.GetWeakPtr(), |
| 354 context->MakeReplyMessageContext()))) |
| 355 return PP_ERROR_FAILED; |
| 356 } |
| 357 |
| 358 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 359 return PP_OK_COMPLETIONPENDING; |
| 360 } |
| 361 |
| 362 int32_t PepperFileIOHost::OnHostMsgFlush( |
| 363 ppapi::host::HostMessageContext* context) { |
| 364 int32_t rv = state_manager_.CheckOperationState( |
| 365 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 366 if (rv != PP_OK) |
| 367 return rv; |
| 368 |
| 369 if (!plugin_delegate_) |
| 370 return PP_ERROR_FAILED; |
| 371 |
| 372 if (!base::FileUtilProxy::Flush( |
| 373 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, |
| 374 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 375 weak_factory_.GetWeakPtr(), |
| 376 context->MakeReplyMessageContext()))) |
| 377 return PP_ERROR_FAILED; |
| 378 |
| 379 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 380 return PP_OK_COMPLETIONPENDING; |
| 381 } |
| 382 |
| 383 int32_t PepperFileIOHost::OnHostMsgClose( |
| 384 ppapi::host::HostMessageContext* context) { |
| 385 if (file_ != base::kInvalidPlatformFileValue && plugin_delegate_) { |
| 386 base::FileUtilProxy::Close( |
| 387 plugin_delegate_->GetFileThreadMessageLoopProxy(), |
| 388 file_, |
| 389 base::ResetAndReturn(¬ify_close_file_callback_)); |
| 390 file_ = base::kInvalidPlatformFileValue; |
| 391 quota_file_io_.reset(); |
| 392 } |
| 393 return PP_OK; |
| 394 } |
| 395 |
| 396 int32_t PepperFileIOHost::OnHostMsgWillWrite( |
| 397 ppapi::host::HostMessageContext* context, |
| 398 int64_t offset, |
| 399 int32_t bytes_to_write) { |
| 400 int32_t rv = state_manager_.CheckOperationState( |
| 401 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 402 if (rv != PP_OK) |
| 403 return rv; |
| 404 |
| 405 if (!quota_file_io_.get()) |
| 406 return PP_OK; |
| 407 |
| 408 if (!quota_file_io_->WillWrite( |
| 409 offset, bytes_to_write, |
| 410 base::Bind(&PepperFileIOHost::ExecutePlatformWillWriteCallback, |
| 411 weak_factory_.GetWeakPtr(), |
| 412 context->MakeReplyMessageContext()))) |
| 413 return PP_ERROR_FAILED; |
| 414 |
| 415 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 416 return PP_OK_COMPLETIONPENDING; |
| 417 } |
| 418 |
| 419 int32_t PepperFileIOHost::OnHostMsgWillSetLength( |
| 420 ppapi::host::HostMessageContext* context, |
| 421 int64_t length) { |
| 422 int32_t rv = state_manager_.CheckOperationState( |
| 423 FileIOStateManager::OPERATION_EXCLUSIVE, true); |
| 424 if (rv != PP_OK) |
| 425 return rv; |
| 426 |
| 427 if (!quota_file_io_.get()) |
| 428 return PP_OK; |
| 429 |
| 430 if (!quota_file_io_->WillSetLength( |
| 431 length, |
| 432 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, |
| 433 weak_factory_.GetWeakPtr(), |
| 434 context->MakeReplyMessageContext()))) |
| 435 return PP_ERROR_FAILED; |
| 436 |
| 437 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); |
| 438 return PP_OK_COMPLETIONPENDING; |
| 439 } |
| 440 |
| 441 int32_t PepperFileIOHost::OnHostMsgGetOSFileDescriptor( |
| 442 ppapi::host::HostMessageContext* context) { |
| 443 //if (!host()->IsRunningInProcess()) |
| 444 // return PP_ERROR_FAILED; |
| 445 int32_t fd = |
| 446 #if defined(OS_POSIX) |
| 447 file_; |
| 448 #elif defined(OS_WIN) |
| 449 reinterpret_cast<uintptr_t>(file_); |
| 450 #else |
| 451 -1; // Platform not supported. |
| 452 #endif |
| 453 // TODO(victorhsieh): Pass the file handle in the reply params once this works |
| 454 // in-process. |
| 455 host()->SendReply(context->MakeReplyMessageContext(), |
| 456 PpapiPluginMsg_FileIO_GetOSFileDescriptorReply(fd)); |
| 457 return PP_OK_COMPLETIONPENDING; |
| 458 } |
| 459 |
| 460 void PepperFileIOHost::ExecutePlatformGeneralCallback( |
| 461 ppapi::host::ReplyMessageContext reply_context, |
| 462 base::PlatformFileError error_code) { |
| 463 reply_context.params.set_result( |
| 464 ::ppapi::PlatformFileErrorToPepperError(error_code)); |
| 465 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 466 state_manager_.OperationFinished(); |
| 467 } |
| 468 |
| 469 void PepperFileIOHost::ExecutePlatformOpenFileCallback( |
| 470 ppapi::host::ReplyMessageContext reply_context, |
| 471 base::PlatformFileError error_code, |
| 472 base::PassPlatformFile file) { |
| 473 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 474 if (pp_error == PP_OK) |
| 475 state_manager_.SetOpenSucceed(); |
| 476 |
| 477 DCHECK(file_ == base::kInvalidPlatformFileValue); |
| 478 file_ = file.ReleaseValue(); |
| 479 |
| 480 DCHECK(!quota_file_io_.get()); |
| 481 if (file_ != base::kInvalidPlatformFileValue && |
| 482 (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || |
| 483 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) { |
| 484 quota_file_io_.reset(new webkit::ppapi::QuotaFileIO( |
| 485 pp_instance(), file_, file_system_url_, file_system_type_)); |
| 486 } |
| 487 |
| 488 reply_context.params.set_result(pp_error); |
| 489 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_OpenReply()); |
| 490 state_manager_.OperationFinished(); |
| 491 } |
| 492 |
| 493 void PepperFileIOHost::ExecutePlatformOpenFileSystemURLCallback( |
| 494 ppapi::host::ReplyMessageContext reply_context, |
| 495 base::PlatformFileError error_code, |
| 496 base::PassPlatformFile file, |
| 497 const PluginDelegate::NotifyCloseFileCallback& callback) { |
| 498 if (error_code == base::PLATFORM_FILE_OK) |
| 499 notify_close_file_callback_ = callback; |
| 500 ExecutePlatformOpenFileCallback(reply_context, error_code, file); |
| 501 } |
| 502 |
| 503 void PepperFileIOHost::ExecutePlatformQueryCallback( |
| 504 ppapi::host::ReplyMessageContext reply_context, |
| 505 base::PlatformFileError error_code, |
| 506 const base::PlatformFileInfo& file_info) { |
| 507 PP_FileInfo pp_info; |
| 508 pp_info.size = file_info.size; |
| 509 pp_info.creation_time = TimeToPPTime(file_info.creation_time); |
| 510 pp_info.last_access_time = TimeToPPTime(file_info.last_accessed); |
| 511 pp_info.last_modified_time = TimeToPPTime(file_info.last_modified); |
| 512 pp_info.system_type = file_system_type_; |
| 513 if (file_info.is_directory) |
| 514 pp_info.type = PP_FILETYPE_DIRECTORY; |
| 515 else |
| 516 pp_info.type = PP_FILETYPE_REGULAR; |
| 517 |
| 518 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 519 reply_context.params.set_result(pp_error); |
| 520 host()->SendReply(reply_context, |
| 521 PpapiPluginMsg_FileIO_QueryReply(pp_info)); |
| 522 state_manager_.OperationFinished(); |
| 523 } |
| 524 |
| 525 void PepperFileIOHost::ExecutePlatformReadCallback( |
| 526 ppapi::host::ReplyMessageContext reply_context, |
| 527 base::PlatformFileError error_code, |
| 528 const char* data, int bytes_read) { |
| 529 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 530 |
| 531 // Only send the amount of data in the string that was actually read. |
| 532 std::string buffer; |
| 533 if (pp_error == PP_OK) |
| 534 buffer.append(data, bytes_read); |
| 535 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); |
| 536 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); |
| 537 state_manager_.OperationFinished(); |
| 538 } |
| 539 |
| 540 void PepperFileIOHost::ExecutePlatformWriteCallback( |
| 541 ppapi::host::ReplyMessageContext reply_context, |
| 542 base::PlatformFileError error_code, |
| 543 int bytes_written) { |
| 544 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 545 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
| 546 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 547 state_manager_.OperationFinished(); |
| 548 } |
| 549 |
| 550 void PepperFileIOHost::ExecutePlatformWillWriteCallback( |
| 551 ppapi::host::ReplyMessageContext reply_context, |
| 552 base::PlatformFileError error_code, |
| 553 int bytes_written) { |
| 554 // On the plugin side, the callback expects a parameter with different meaning |
| 555 // depends on whether is negative or not. It is the result here. We |
| 556 // translate for the callback. |
| 557 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
| 558 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
| 559 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
| 560 state_manager_.OperationFinished(); |
| 561 } |
| 562 |
| 563 } // namespace content |
OLD | NEW |