Chromium Code Reviews| 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/browser/renderer_host/pepper/pepper_file_io_host.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/callback_helpers.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/files/file_util_proxy.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "content/browser/renderer_host/pepper/pepper_file_ref_host.h" | |
| 14 #include "content/browser/renderer_host/pepper/pepper_security_helper.h" | |
| 15 #include "content/browser/renderer_host/pepper/quota_file_io.h" | |
| 16 #include "content/common/fileapi/file_system_messages.h" | |
| 17 #include "content/common/sandbox_util.h" | |
| 18 #include "content/common/view_messages.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/content_browser_client.h" | |
| 21 #include "content/public/browser/render_process_host.h" | |
| 22 #include "content/public/browser/storage_partition.h" | |
| 23 #include "content/public/common/content_client.h" | |
| 24 #include "ppapi/c/pp_errors.h" | |
| 25 #include "ppapi/c/ppb_file_io.h" | |
| 26 #include "ppapi/host/dispatch_host_message.h" | |
| 27 #include "ppapi/host/ppapi_host.h" | |
| 28 #include "ppapi/proxy/ppapi_messages.h" | |
| 29 #include "ppapi/shared_impl/file_type_conversion.h" | |
| 30 #include "ppapi/shared_impl/time_conversion.h" | |
| 31 #include "webkit/browser/fileapi/file_observers.h" | |
| 32 #include "webkit/browser/fileapi/file_system_context.h" | |
| 33 #include "webkit/browser/fileapi/task_runner_bound_observer_list.h" | |
| 34 #include "webkit/browser/quota/quota_manager.h" | |
| 35 #include "webkit/common/fileapi/file_system_util.h" | |
| 36 | |
| 37 namespace content { | |
| 38 | |
| 39 using ppapi::FileIOStateManager; | |
| 40 using ppapi::PPTimeToTime; | |
| 41 | |
| 42 namespace { | |
| 43 | |
| 44 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { | |
| 45 // On the plugin side, some callbacks expect a parameter that means different | |
| 46 // things depending on whether is negative or not. We translate for those | |
|
bbudge
2013/10/22 01:59:39
s/is/it is
teravest
2013/10/22 15:45:05
Done.
| |
| 47 // callbacks here. | |
| 48 return pp_error == PP_OK ? byte_number : pp_error; | |
| 49 } | |
| 50 | |
| 51 class QuotaFileIODelegate : public QuotaFileIO::Delegate { | |
| 52 public: | |
| 53 QuotaFileIODelegate(scoped_refptr<fileapi::FileSystemContext> context, | |
| 54 int render_process_id) | |
| 55 : context_(context), | |
| 56 weak_factory_(this) { } | |
| 57 virtual ~QuotaFileIODelegate() {} | |
| 58 | |
| 59 virtual void QueryAvailableSpace( | |
| 60 const GURL& origin, | |
| 61 quota::StorageType type, | |
| 62 const AvailableSpaceCallback& callback) OVERRIDE { | |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 64 quota::QuotaManagerProxy* quota_manager_proxy = | |
| 65 context_->quota_manager_proxy(); | |
| 66 DCHECK(quota_manager_proxy); | |
| 67 if (!quota_manager_proxy) { | |
| 68 callback.Run(0); | |
| 69 return; | |
| 70 } | |
| 71 quota::QuotaManager* qm = quota_manager_proxy->quota_manager(); | |
| 72 DCHECK(qm); | |
| 73 if (!qm) { | |
| 74 callback.Run(0); | |
| 75 return; | |
| 76 } | |
| 77 qm->GetUsageAndQuotaForWebApps( | |
| 78 origin, | |
| 79 type, | |
| 80 base::Bind(&QuotaFileIODelegate::GotUsageAndQuotaForWebApps, | |
| 81 weak_factory_.GetWeakPtr(), callback)); | |
| 82 } | |
| 83 | |
| 84 void GotUsageAndQuotaForWebApps(const AvailableSpaceCallback& callback, | |
| 85 quota::QuotaStatusCode code, | |
| 86 int64 usage, | |
| 87 int64 quota) { | |
| 88 if (code == quota::kQuotaStatusOk) | |
| 89 callback.Run(std::max(static_cast<int64>(0), quota - usage)); | |
| 90 else | |
| 91 callback.Run(0); | |
| 92 } | |
| 93 | |
| 94 virtual void WillUpdateFile(const GURL& file_path) OVERRIDE { | |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 96 fileapi::FileSystemURL url(context_->CrackURL(file_path)); | |
| 97 if (!url.is_valid()) | |
| 98 return; | |
| 99 const fileapi::UpdateObserverList* observers = | |
| 100 context_->GetUpdateObservers(url.type()); | |
| 101 if (!observers) | |
| 102 return; | |
| 103 observers->Notify(&fileapi::FileUpdateObserver::OnStartUpdate, | |
| 104 MakeTuple(url)); | |
| 105 } | |
| 106 virtual void DidUpdateFile(const GURL& file_path, int64_t delta) OVERRIDE { | |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 108 fileapi::FileSystemURL url(context_->CrackURL(file_path)); | |
| 109 if (!url.is_valid()) | |
| 110 return; | |
| 111 const fileapi::UpdateObserverList* observers = | |
| 112 context_->GetUpdateObservers(url.type()); | |
| 113 if (!observers) | |
| 114 return; | |
| 115 observers->Notify(&fileapi::FileUpdateObserver::OnUpdate, | |
| 116 MakeTuple(url, delta)); | |
| 117 observers->Notify(&fileapi::FileUpdateObserver::OnEndUpdate, | |
| 118 MakeTuple(url)); | |
| 119 } | |
| 120 virtual scoped_refptr<base::MessageLoopProxy> | |
| 121 GetFileThreadMessageLoopProxy() OVERRIDE { | |
| 122 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); | |
| 123 } | |
| 124 private: | |
| 125 scoped_refptr<fileapi::FileSystemContext> context_; | |
| 126 base::WeakPtrFactory<QuotaFileIODelegate> weak_factory_; | |
| 127 }; | |
| 128 | |
| 129 // TODO(teravest): Don't duplicate code with PepperFileSystemBrowserHost. | |
| 130 scoped_refptr<fileapi::FileSystemContext> | |
| 131 GetFileSystemContextFromRenderId(int render_process_id) { | |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 133 RenderProcessHost* render_process_host = | |
| 134 RenderProcessHost::FromID(render_process_id); | |
| 135 if (!render_process_host) | |
| 136 return NULL; | |
| 137 StoragePartition* storage_partition = | |
| 138 render_process_host->GetStoragePartition(); | |
| 139 if (!storage_partition) | |
| 140 return NULL; | |
| 141 return storage_partition->GetFileSystemContext(); | |
| 142 } | |
| 143 | |
| 144 } // namespace | |
| 145 | |
| 146 PepperFileIOHost::PepperFileIOHost(BrowserPpapiHostImpl* host, | |
| 147 PP_Instance instance, | |
| 148 PP_Resource resource) | |
| 149 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
| 150 browser_ppapi_host_(host), | |
| 151 render_process_host_(NULL), | |
| 152 file_(base::kInvalidPlatformFileValue), | |
| 153 file_system_type_(PP_FILESYSTEMTYPE_INVALID), | |
| 154 quota_policy_(quota::kQuotaLimitTypeUnknown), | |
| 155 open_flags_(0), | |
| 156 weak_factory_(this) { | |
| 157 int unused; | |
| 158 if (!host->GetRenderViewIDsForInstance(instance, | |
| 159 &render_process_id_, | |
| 160 &unused)) { | |
| 161 render_process_id_ = -1; | |
| 162 } | |
| 163 file_message_loop_ = BrowserThread::GetMessageLoopProxyForThread( | |
| 164 BrowserThread::FILE); | |
| 165 } | |
| 166 | |
| 167 PepperFileIOHost::~PepperFileIOHost() { | |
| 168 OnHostMsgClose(NULL); | |
| 169 } | |
| 170 | |
| 171 int32_t PepperFileIOHost::OnResourceMessageReceived( | |
| 172 const IPC::Message& msg, | |
| 173 ppapi::host::HostMessageContext* context) { | |
| 174 IPC_BEGIN_MESSAGE_MAP(PepperFileIOHost, msg) | |
| 175 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Open, | |
| 176 OnHostMsgOpen) | |
| 177 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Touch, | |
| 178 OnHostMsgTouch) | |
| 179 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_Write, | |
| 180 OnHostMsgWrite) | |
| 181 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FileIO_SetLength, | |
| 182 OnHostMsgSetLength) | |
| 183 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Flush, | |
| 184 OnHostMsgFlush) | |
| 185 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_Close, | |
| 186 OnHostMsgClose) | |
| 187 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_FileIO_RequestOSFileHandle, | |
| 188 OnHostMsgRequestOSFileHandle) | |
| 189 IPC_END_MESSAGE_MAP() | |
| 190 return PP_ERROR_FAILED; | |
| 191 } | |
| 192 | |
| 193 int32_t PepperFileIOHost::OnHostMsgOpen( | |
| 194 ppapi::host::HostMessageContext* context, | |
| 195 PP_Resource file_ref_resource, | |
| 196 int32_t open_flags) { | |
| 197 int32_t rv = state_manager_.CheckOperationState( | |
| 198 FileIOStateManager::OPERATION_EXCLUSIVE, false); | |
| 199 if (rv != PP_OK) | |
| 200 return rv; | |
| 201 | |
| 202 int platform_file_flags = 0; | |
| 203 if (!ppapi::PepperFileOpenFlagsToPlatformFileFlags(open_flags, | |
| 204 &platform_file_flags)) | |
| 205 return PP_ERROR_BADARGUMENT; | |
| 206 | |
| 207 ppapi::host::ResourceHost* resource_host = | |
| 208 host()->GetResourceHost(file_ref_resource); | |
| 209 if (!resource_host || !resource_host->IsFileRefHost()) | |
| 210 return PP_ERROR_BADRESOURCE; | |
| 211 PepperFileRefHost* file_ref_host = | |
| 212 static_cast<PepperFileRefHost*>(resource_host); | |
| 213 if (file_ref_host->GetFileSystemType() == PP_FILESYSTEMTYPE_INVALID) | |
| 214 return PP_ERROR_FAILED; | |
| 215 | |
| 216 open_flags_ = open_flags; | |
| 217 file_system_type_ = file_ref_host->GetFileSystemType(); | |
| 218 file_system_url_ = file_ref_host->GetFileSystemURL(); | |
| 219 | |
| 220 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { | |
| 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 222 fileapi::FileSystemURL url(file_ref_host->GetFileSystemURL()); | |
| 223 if (!url.is_valid()) | |
| 224 return PP_ERROR_BADARGUMENT; | |
| 225 if (!CanOpenFileSystemURLWithPepperFlags(open_flags, | |
| 226 render_process_id_, | |
| 227 url)) | |
| 228 return PP_ERROR_NOACCESS; | |
| 229 | |
| 230 BrowserThread::PostTaskAndReplyWithResult( | |
| 231 BrowserThread::UI, | |
| 232 FROM_HERE, | |
| 233 base::Bind(&GetFileSystemContextFromRenderId, render_process_id_), | |
| 234 base::Bind(&PepperFileIOHost::GotFileSystemContext, | |
| 235 weak_factory_.GetWeakPtr(), | |
| 236 context->MakeReplyMessageContext(), | |
| 237 platform_file_flags)); | |
| 238 } else { | |
| 239 base::FilePath path = file_ref_host->GetExternalFilePath(); | |
| 240 if (!CanOpenWithPepperFlags(open_flags, render_process_id_, path)) | |
| 241 return PP_ERROR_NOACCESS; | |
| 242 base::FileUtilProxy::CreateOrOpen( | |
| 243 file_message_loop_, | |
| 244 path, | |
| 245 platform_file_flags, | |
| 246 base::Bind(&PepperFileIOHost::ExecutePlatformOpenFileCallback, | |
| 247 weak_factory_.GetWeakPtr(), | |
| 248 context->MakeReplyMessageContext())); | |
| 249 } | |
| 250 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 251 return PP_OK_COMPLETIONPENDING; | |
| 252 } | |
| 253 | |
| 254 void PepperFileIOHost::GotFileSystemContext( | |
| 255 ppapi::host::ReplyMessageContext reply_context, | |
| 256 int platform_file_flags, | |
| 257 scoped_refptr<fileapi::FileSystemContext> file_system_context) { | |
| 258 if (!file_system_context.get()) { | |
| 259 reply_context.params.set_result(PP_ERROR_FAILED); | |
| 260 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_OpenReply()); | |
| 261 return; | |
| 262 } | |
| 263 file_system_context_ = file_system_context; | |
| 264 | |
| 265 if (file_system_context_->GetFileSystemBackend( | |
| 266 file_system_url_.type()) == NULL) { | |
|
bbudge
2013/10/22 01:59:39
nit: !expr seems more consistent with the rest of
teravest
2013/10/22 15:45:05
Done.
| |
| 267 reply_context.params.set_result(PP_ERROR_FAILED); | |
| 268 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_OpenReply()); | |
| 269 return; | |
| 270 } | |
| 271 quota_policy_ = quota::kQuotaLimitTypeUnknown; | |
| 272 quota::QuotaManagerProxy* quota_manager_proxy = | |
| 273 file_system_context_->quota_manager_proxy(); | |
| 274 CHECK(quota_manager_proxy); | |
| 275 CHECK(quota_manager_proxy->quota_manager()); | |
| 276 if (quota_manager_proxy->quota_manager()->IsStorageUnlimited( | |
| 277 file_system_url_.origin(), | |
| 278 fileapi::FileSystemTypeToQuotaStorageType(file_system_url_.type()))) | |
| 279 quota_policy_ = quota::kQuotaLimitTypeUnlimited; | |
| 280 else | |
| 281 quota_policy_ = quota::kQuotaLimitTypeLimited; | |
| 282 file_system_operation_runner_ = | |
| 283 file_system_context_->CreateFileSystemOperationRunner(); | |
| 284 file_system_operation_runner_->OpenFile( | |
| 285 file_system_url_, | |
| 286 platform_file_flags, | |
| 287 browser_ppapi_host_->GetPluginProcessHandle(), | |
| 288 base::Bind(&PepperFileIOHost::DidOpenFile, | |
| 289 weak_factory_.GetWeakPtr(), | |
|
bbudge
2013/10/22 01:59:39
nit: Indent is different from other base::Bind inv
teravest
2013/10/22 15:45:05
Done.
| |
| 290 reply_context)); | |
| 291 } | |
| 292 | |
| 293 void PepperFileIOHost::DidOpenFile( | |
| 294 ppapi::host::ReplyMessageContext reply_context, | |
| 295 base::PlatformFileError result, | |
| 296 base::PlatformFile file, | |
| 297 const base::Closure& on_close_callback, | |
| 298 base::ProcessHandle peer_handle) { | |
| 299 if (result == base::PLATFORM_FILE_OK) | |
| 300 on_close_callback_ = on_close_callback; | |
| 301 ExecutePlatformOpenFileCallback( | |
| 302 reply_context, result, base::PassPlatformFile(&file), true); | |
| 303 } | |
| 304 | |
| 305 int32_t PepperFileIOHost::OnHostMsgTouch( | |
| 306 ppapi::host::HostMessageContext* context, | |
| 307 PP_Time last_access_time, | |
| 308 PP_Time last_modified_time) { | |
| 309 int32_t rv = state_manager_.CheckOperationState( | |
| 310 FileIOStateManager::OPERATION_EXCLUSIVE, true); | |
| 311 if (rv != PP_OK) | |
| 312 return rv; | |
| 313 | |
| 314 if (!base::FileUtilProxy::Touch( | |
| 315 file_message_loop_, | |
| 316 file_, | |
| 317 PPTimeToTime(last_access_time), | |
| 318 PPTimeToTime(last_modified_time), | |
| 319 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | |
| 320 weak_factory_.GetWeakPtr(), | |
| 321 context->MakeReplyMessageContext()))) | |
| 322 return PP_ERROR_FAILED; | |
| 323 | |
| 324 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 325 return PP_OK_COMPLETIONPENDING; | |
| 326 } | |
| 327 | |
| 328 int32_t PepperFileIOHost::OnHostMsgWrite( | |
| 329 ppapi::host::HostMessageContext* context, | |
| 330 int64_t offset, | |
| 331 const std::string& buffer) { | |
| 332 int32_t rv = state_manager_.CheckOperationState( | |
| 333 FileIOStateManager::OPERATION_WRITE, true); | |
| 334 if (rv != PP_OK) | |
| 335 return rv; | |
| 336 | |
| 337 if (quota_file_io_) { | |
| 338 if (!quota_file_io_->Write( | |
| 339 offset, buffer.c_str(), buffer.size(), | |
| 340 base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback, | |
| 341 weak_factory_.GetWeakPtr(), | |
| 342 context->MakeReplyMessageContext()))) | |
| 343 return PP_ERROR_FAILED; | |
| 344 } else { | |
| 345 if (!base::FileUtilProxy::Write( | |
| 346 file_message_loop_, | |
| 347 file_, | |
| 348 offset, | |
| 349 buffer.c_str(), | |
| 350 buffer.size(), | |
| 351 base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback, | |
| 352 weak_factory_.GetWeakPtr(), | |
| 353 context->MakeReplyMessageContext()))) | |
| 354 return PP_ERROR_FAILED; | |
| 355 } | |
| 356 | |
| 357 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_WRITE); | |
| 358 return PP_OK_COMPLETIONPENDING; | |
| 359 } | |
| 360 | |
| 361 int32_t PepperFileIOHost::OnHostMsgSetLength( | |
| 362 ppapi::host::HostMessageContext* context, | |
| 363 int64_t length) { | |
| 364 int32_t rv = state_manager_.CheckOperationState( | |
| 365 FileIOStateManager::OPERATION_EXCLUSIVE, true); | |
| 366 if (rv != PP_OK) | |
| 367 return rv; | |
| 368 | |
| 369 QuotaFileIO::StatusCallback cb = | |
| 370 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | |
| 371 weak_factory_.GetWeakPtr(), | |
| 372 context->MakeReplyMessageContext()); | |
| 373 if (quota_file_io_) { | |
| 374 if (!quota_file_io_->SetLength(length, cb)) | |
| 375 return PP_ERROR_FAILED; | |
| 376 } else { | |
| 377 if (!base::FileUtilProxy::Truncate(file_message_loop_, file_, length, cb)) | |
| 378 return PP_ERROR_FAILED; | |
| 379 } | |
| 380 | |
| 381 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 382 return PP_OK_COMPLETIONPENDING; | |
| 383 } | |
| 384 | |
| 385 int32_t PepperFileIOHost::OnHostMsgFlush( | |
| 386 ppapi::host::HostMessageContext* context) { | |
| 387 int32_t rv = state_manager_.CheckOperationState( | |
| 388 FileIOStateManager::OPERATION_EXCLUSIVE, true); | |
| 389 if (rv != PP_OK) | |
| 390 return rv; | |
| 391 | |
| 392 if (!base::FileUtilProxy::Flush( | |
| 393 file_message_loop_, | |
| 394 file_, | |
| 395 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, | |
| 396 weak_factory_.GetWeakPtr(), | |
| 397 context->MakeReplyMessageContext()))) | |
| 398 return PP_ERROR_FAILED; | |
| 399 | |
| 400 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); | |
| 401 return PP_OK_COMPLETIONPENDING; | |
| 402 } | |
| 403 | |
| 404 int32_t PepperFileIOHost::OnHostMsgClose( | |
| 405 ppapi::host::HostMessageContext* context) { | |
| 406 if (file_ != base::kInvalidPlatformFileValue) { | |
| 407 base::FileUtilProxy::Close( | |
| 408 file_message_loop_, | |
| 409 file_, | |
| 410 base::ResetAndReturn(¬ify_close_file_callback_)); | |
| 411 file_ = base::kInvalidPlatformFileValue; | |
| 412 quota_file_io_.reset(); | |
| 413 } | |
| 414 return PP_OK; | |
| 415 } | |
| 416 | |
| 417 int32_t PepperFileIOHost::OnHostMsgRequestOSFileHandle( | |
| 418 ppapi::host::HostMessageContext* context) { | |
| 419 if (open_flags_ != PP_FILEOPENFLAG_READ && | |
| 420 quota_policy_ != quota::kQuotaLimitTypeUnlimited) | |
| 421 return PP_ERROR_FAILED; | |
| 422 GetRenderProcessHost( | |
| 423 base::Bind(&PepperFileIOHost::FinishRequestOSFileHandle, | |
| 424 weak_factory_.GetWeakPtr(), | |
| 425 context->MakeReplyMessageContext())); | |
| 426 return PP_OK_COMPLETIONPENDING; | |
| 427 } | |
| 428 | |
| 429 void PepperFileIOHost::FinishRequestOSFileHandle( | |
| 430 ppapi::host::ReplyMessageContext reply_context, | |
| 431 RenderProcessHost* render_process_host) { | |
| 432 // Whitelist to make it privately accessible. | |
| 433 if (!browser_ppapi_host_->external_plugin() || | |
| 434 host()->permissions().HasPermission(ppapi::PERMISSION_PRIVATE) || | |
| 435 GetContentClient()->browser()->IsPluginAllowedToCallRequestOSFileHandle( | |
| 436 render_process_host->GetBrowserContext(), | |
| 437 browser_ppapi_host_->GetDocumentURLForInstance(pp_instance()))) { | |
| 438 if (!AddFileToReplyContext(open_flags_, &reply_context)) | |
| 439 reply_context.params.set_result(PP_ERROR_FAILED); | |
| 440 } else { | |
| 441 reply_context.params.set_result(PP_ERROR_NOACCESS); | |
| 442 } | |
| 443 host()->SendReply(reply_context, | |
| 444 PpapiPluginMsg_FileIO_RequestOSFileHandleReply()); | |
| 445 } | |
| 446 | |
| 447 void PepperFileIOHost::ExecutePlatformGeneralCallback( | |
| 448 ppapi::host::ReplyMessageContext reply_context, | |
| 449 base::PlatformFileError error_code) { | |
| 450 reply_context.params.set_result( | |
| 451 ppapi::PlatformFileErrorToPepperError(error_code)); | |
| 452 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | |
| 453 state_manager_.SetOperationFinished(); | |
| 454 } | |
| 455 | |
| 456 void PepperFileIOHost::ExecutePlatformOpenFileCallback( | |
| 457 ppapi::host::ReplyMessageContext reply_context, | |
| 458 base::PlatformFileError error_code, | |
| 459 base::PassPlatformFile file, | |
| 460 bool unused_created) { | |
| 461 int32_t pp_error = ppapi::PlatformFileErrorToPepperError(error_code); | |
| 462 if (pp_error == PP_OK) | |
| 463 state_manager_.SetOpenSucceed(); | |
| 464 | |
| 465 DCHECK(file_ == base::kInvalidPlatformFileValue); | |
| 466 file_ = file.ReleaseValue(); | |
| 467 | |
| 468 DCHECK(!quota_file_io_.get()); | |
| 469 if (file_ != base::kInvalidPlatformFileValue) { | |
| 470 if (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY || | |
| 471 file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT) { | |
| 472 quota_file_io_.reset(new QuotaFileIO( | |
| 473 new QuotaFileIODelegate(file_system_context_, render_process_id_), | |
| 474 file_, file_system_url_.ToGURL(), file_system_type_)); | |
| 475 } | |
| 476 int32_t flags_to_send = open_flags_; | |
| 477 if (!host()->permissions().HasPermission(ppapi::PERMISSION_DEV)) { | |
| 478 // IMPORTANT: Clear PP_FILEOPENFLAG_WRITE and PP_FILEOPENFLAG_APPEND so | |
| 479 // the plugin can't write and so bypass our quota checks. | |
| 480 flags_to_send = | |
| 481 open_flags_ & ~(PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_APPEND); | |
| 482 } | |
| 483 if (!AddFileToReplyContext(flags_to_send, &reply_context)) | |
| 484 pp_error = PP_ERROR_FAILED; | |
| 485 } | |
| 486 reply_context.params.set_result(pp_error); | |
| 487 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_OpenReply()); | |
| 488 state_manager_.SetOperationFinished(); | |
| 489 } | |
| 490 | |
| 491 void PepperFileIOHost::ExecutePlatformWriteCallback( | |
| 492 ppapi::host::ReplyMessageContext reply_context, | |
| 493 base::PlatformFileError error_code, | |
| 494 int bytes_written) { | |
| 495 // On the plugin side, the callback expects a parameter with different meaning | |
| 496 // depends on whether is negative or not. It is the result here. We translate | |
| 497 // for the callback. | |
| 498 int32_t pp_error = ppapi::PlatformFileErrorToPepperError(error_code); | |
| 499 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | |
| 500 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | |
| 501 state_manager_.SetOperationFinished(); | |
| 502 } | |
| 503 | |
| 504 void PepperFileIOHost::GetRenderProcessHost( | |
| 505 base::Callback<void(RenderProcessHost*)> callback) { | |
| 506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 507 if (render_process_host_) { | |
| 508 callback.Run(render_process_host_); | |
| 509 } else { | |
| 510 BrowserThread::PostTaskAndReplyWithResult( | |
| 511 BrowserThread::UI, | |
| 512 FROM_HERE, | |
| 513 base::Bind(&RenderProcessHost::FromID, render_process_id_), | |
| 514 base::Bind(&PepperFileIOHost::GotRenderProcessHost, | |
| 515 weak_factory_.GetWeakPtr(), | |
| 516 callback)); | |
| 517 } | |
| 518 } | |
| 519 | |
| 520 void PepperFileIOHost::GotRenderProcessHost( | |
| 521 base::Callback<void(RenderProcessHost*)> callback, | |
| 522 RenderProcessHost* host) { | |
| 523 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 524 render_process_host_ = host; | |
| 525 callback.Run(render_process_host_); | |
| 526 } | |
| 527 | |
| 528 bool PepperFileIOHost::AddFileToReplyContext( | |
| 529 int32_t open_flags, | |
| 530 ppapi::host::ReplyMessageContext* reply_context) const { | |
| 531 base::ProcessId plugin_process_id = | |
| 532 base::GetProcId(browser_ppapi_host_->GetPluginProcessHandle()); | |
| 533 if (!browser_ppapi_host_->external_plugin()) | |
| 534 plugin_process_id = render_process_id_; | |
| 535 IPC::PlatformFileForTransit transit_file = BrokerGetFileHandleForProcess( | |
| 536 file_, plugin_process_id, false); | |
| 537 if (transit_file == IPC::InvalidPlatformFileForTransit()) | |
| 538 return false; | |
| 539 ppapi::proxy::SerializedHandle file_handle; | |
| 540 file_handle.set_file_handle(transit_file, open_flags); | |
| 541 reply_context->params.AppendHandle(file_handle); | |
| 542 return true; | |
| 543 } | |
| 544 | |
| 545 } // namespace content | |
| OLD | NEW |