Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: content/renderer/pepper/pepper_file_io_host.cc

Issue 13726024: Refactor FileSystem (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_helpers.h" 9 #include "base/callback_helpers.h"
9 #include "base/command_line.h" 10 #include "base/command_line.h"
10 #include "base/files/file_util_proxy.h" 11 #include "base/files/file_util_proxy.h"
11 #include "content/public/common/content_client.h" 12 #include "content/public/common/content_client.h"
12 #include "content/public/renderer/content_renderer_client.h" 13 #include "content/public/renderer/content_renderer_client.h"
14 #include "content/renderer/pepper/null_file_system_callback_dispatcher.h"
13 #include "ppapi/c/pp_errors.h" 15 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/host/dispatch_host_message.h" 16 #include "ppapi/host/dispatch_host_message.h"
15 #include "ppapi/host/ppapi_host.h" 17 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h" 18 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/shared_impl/file_type_conversion.h" 19 #include "ppapi/shared_impl/file_type_conversion.h"
18 #include "ppapi/shared_impl/time_conversion.h" 20 #include "ppapi/shared_impl/time_conversion.h"
19 #include "ppapi/thunk/enter.h" 21 #include "ppapi/thunk/enter.h"
20 #include "webkit/fileapi/file_system_callback_dispatcher.h"
21 #include "webkit/plugins/ppapi/file_callbacks.h" 22 #include "webkit/plugins/ppapi/file_callbacks.h"
22 #include "webkit/plugins/ppapi/host_globals.h" 23 #include "webkit/plugins/ppapi/host_globals.h"
23 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 24 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
24 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" 25 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
25 #include "webkit/plugins/ppapi/quota_file_io.h" 26 #include "webkit/plugins/ppapi/quota_file_io.h"
26 27
27 namespace content { 28 namespace content {
28 29
29 using ppapi::FileIOStateManager; 30 using ppapi::FileIOStateManager;
30 using ppapi::PPTimeToTime; 31 using ppapi::PPTimeToTime;
31 using ppapi::TimeToPPTime; 32 using ppapi::TimeToPPTime;
32 using ppapi::host::ReplyMessageContext; 33 using ppapi::host::ReplyMessageContext;
33 using ppapi::thunk::EnterResourceNoLock; 34 using ppapi::thunk::EnterResourceNoLock;
34 using ppapi::thunk::PPB_FileRef_API; 35 using ppapi::thunk::PPB_FileRef_API;
35 using webkit::ppapi::PPB_FileRef_Impl; 36 using webkit::ppapi::PPB_FileRef_Impl;
36 using webkit::ppapi::PluginDelegate; 37 using webkit::ppapi::PluginDelegate;
37 38
38 namespace { 39 namespace {
39 40
40 // The maximum size we'll support reading in one chunk. The renderer process 41 // The maximum size we'll support reading in one chunk. The renderer process
41 // must allocate a buffer sized according to the request of the plugin. To 42 // must allocate a buffer sized according to the request of the plugin. To
42 // keep things from getting out of control, we cap the read size to this value. 43 // keep things from getting out of control, we cap the read size to this value.
43 // This should generally be OK since the API specifies that it may perform a 44 // This should generally be OK since the API specifies that it may perform a
44 // partial read. 45 // partial read.
45 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB 46 static const int32_t kMaxReadSize = 32 * 1024 * 1024; // 32MB
46 47
47 typedef base::Callback<void (base::PlatformFileError)> PlatformGeneralCallback; 48 typedef base::Callback<void (int32_t)> PlatformGeneralCallback;
yzshen1 2013/04/11 17:04:57 I think it is better to use PlatformFileError inst
victorhsieh 2013/04/11 18:24:38 Actually it's a pp_error.
48 49
49 class PlatformGeneralCallbackTranslator 50 class PlatformGeneralCallbackTranslator
50 : public fileapi::FileSystemCallbackDispatcher { 51 : public NullFileSystemCallbackDispatcher {
51 public: 52 public:
52 explicit PlatformGeneralCallbackTranslator( 53 explicit PlatformGeneralCallbackTranslator(
53 const PlatformGeneralCallback& callback) 54 const PlatformGeneralCallback& callback)
54 : callback_(callback) {} 55 : callback_(callback) {}
55 56
56 virtual ~PlatformGeneralCallbackTranslator() {} 57 virtual ~PlatformGeneralCallbackTranslator() {}
57 58
58 virtual void DidSucceed() OVERRIDE { 59 virtual void DidSucceed() OVERRIDE {
59 callback_.Run(base::PLATFORM_FILE_OK); 60 callback_.Run(PP_OK);
60 } 61 }
61 62
62 virtual void DidReadMetadata(const base::PlatformFileInfo& file_info, 63 virtual void DidFail(base::PlatformFileError platform_error) OVERRIDE {
63 const base::FilePath& platform_path) OVERRIDE { 64 callback_.Run(ppapi::PlatformFileErrorToPepperError(platform_error));
64 NOTREACHED();
65 }
66
67 virtual void DidCreateSnapshotFile(
68 const base::PlatformFileInfo& file_info,
69 const base::FilePath& platform_path) OVERRIDE {
70 NOTREACHED();
71 }
72
73 virtual void DidReadDirectory(
74 const std::vector<base::FileUtilProxy::Entry>& entries,
75 bool has_more) OVERRIDE {
76 NOTREACHED();
77 }
78
79 virtual void DidOpenFileSystem(const std::string& name,
80 const GURL& root) OVERRIDE {
81 NOTREACHED();
82 }
83
84 virtual void DidFail(base::PlatformFileError error_code) OVERRIDE {
85 callback_.Run(error_code);
86 }
87
88 virtual void DidWrite(int64 bytes, bool complete) OVERRIDE {
89 NOTREACHED();
90 }
91
92 virtual void DidOpenFile(base::PlatformFile file) OVERRIDE {
93 NOTREACHED();
94 } 65 }
95 66
96 private: 67 private:
97 PlatformGeneralCallback callback_; 68 PlatformGeneralCallback callback_;
98 }; 69 };
99 70
100 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) { 71 int32_t ErrorOrByteNumber(int32_t pp_error, int32_t byte_number) {
101 // On the plugin side, some callbacks expect a parameter that means different 72 // On the plugin side, some callbacks expect a parameter that means different
102 // things depending on whether is negative or not. We translate for those 73 // things depending on whether is negative or not. We translate for those
103 // callbacks here. 74 // callbacks here.
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 212
242 if (!plugin_delegate_) 213 if (!plugin_delegate_)
243 return PP_ERROR_FAILED; 214 return PP_ERROR_FAILED;
244 215
245 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { 216 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) {
246 if (!plugin_delegate_->Touch( 217 if (!plugin_delegate_->Touch(
247 file_system_url_, 218 file_system_url_,
248 PPTimeToTime(last_access_time), 219 PPTimeToTime(last_access_time),
249 PPTimeToTime(last_modified_time), 220 PPTimeToTime(last_modified_time),
250 new PlatformGeneralCallbackTranslator( 221 new PlatformGeneralCallbackTranslator(
251 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, 222 base::Bind(&PepperFileIOHost::ExecuteGeneralCallback,
252 weak_factory_.GetWeakPtr(), 223 weak_factory_.GetWeakPtr(),
253 context->MakeReplyMessageContext())))) 224 context->MakeReplyMessageContext()))))
254 return PP_ERROR_FAILED; 225 return PP_ERROR_FAILED;
255 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 226 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
256 return PP_OK_COMPLETIONPENDING; 227 return PP_OK_COMPLETIONPENDING;
257 } 228 }
258 229
259 // TODO(nhiroki): fix a failure of FileIO.Touch for an external filesystem on 230 // TODO(nhiroki): fix a failure of FileIO.Touch for an external filesystem on
260 // Mac and Linux due to sandbox restrictions (http://crbug.com/101128). 231 // Mac and Linux due to sandbox restrictions (http://crbug.com/101128).
261 if (!base::FileUtilProxy::Touch( 232 if (!base::FileUtilProxy::Touch(
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 if (rv != PP_OK) 317 if (rv != PP_OK)
347 return rv; 318 return rv;
348 319
349 if (!plugin_delegate_) 320 if (!plugin_delegate_)
350 return PP_ERROR_FAILED; 321 return PP_ERROR_FAILED;
351 322
352 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) { 323 if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL) {
353 if (!plugin_delegate_->SetLength( 324 if (!plugin_delegate_->SetLength(
354 file_system_url_, length, 325 file_system_url_, length,
355 new PlatformGeneralCallbackTranslator( 326 new PlatformGeneralCallbackTranslator(
356 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, 327 base::Bind(&PepperFileIOHost::ExecuteGeneralCallback,
357 weak_factory_.GetWeakPtr(), 328 weak_factory_.GetWeakPtr(),
358 context->MakeReplyMessageContext())))) 329 context->MakeReplyMessageContext()))))
359 return PP_ERROR_FAILED; 330 return PP_ERROR_FAILED;
360 } else { 331 } else {
361 // TODO(nhiroki): fix a failure of FileIO.SetLength for an external 332 // TODO(nhiroki): fix a failure of FileIO.SetLength for an external
362 // filesystem on Mac due to sandbox restrictions (http://crbug.com/156077). 333 // filesystem on Mac due to sandbox restrictions (http://crbug.com/156077).
363 if (!base::FileUtilProxy::Truncate( 334 if (!base::FileUtilProxy::Truncate(
364 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, length, 335 plugin_delegate_->GetFileThreadMessageLoopProxy(), file_, length,
365 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback, 336 base::Bind(&PepperFileIOHost::ExecutePlatformGeneralCallback,
366 weak_factory_.GetWeakPtr(), 337 weak_factory_.GetWeakPtr(),
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 int32_t rv = state_manager_.CheckOperationState( 384 int32_t rv = state_manager_.CheckOperationState(
414 FileIOStateManager::OPERATION_EXCLUSIVE, true); 385 FileIOStateManager::OPERATION_EXCLUSIVE, true);
415 if (rv != PP_OK) 386 if (rv != PP_OK)
416 return rv; 387 return rv;
417 388
418 if (!quota_file_io_.get()) 389 if (!quota_file_io_.get())
419 return PP_OK; 390 return PP_OK;
420 391
421 if (!quota_file_io_->WillWrite( 392 if (!quota_file_io_->WillWrite(
422 offset, bytes_to_write, 393 offset, bytes_to_write,
423 base::Bind(&PepperFileIOHost::ExecutePlatformWillWriteCallback, 394 base::Bind(&PepperFileIOHost::ExecutePlatformWriteCallback,
424 weak_factory_.GetWeakPtr(), 395 weak_factory_.GetWeakPtr(),
425 context->MakeReplyMessageContext()))) 396 context->MakeReplyMessageContext())))
426 return PP_ERROR_FAILED; 397 return PP_ERROR_FAILED;
427 398
428 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 399 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
429 return PP_OK_COMPLETIONPENDING; 400 return PP_OK_COMPLETIONPENDING;
430 } 401 }
431 402
432 int32_t PepperFileIOHost::OnHostMsgWillSetLength( 403 int32_t PepperFileIOHost::OnHostMsgWillSetLength(
433 ppapi::host::HostMessageContext* context, 404 ppapi::host::HostMessageContext* context,
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 469
499 void PepperFileIOHost::ExecutePlatformGeneralCallback( 470 void PepperFileIOHost::ExecutePlatformGeneralCallback(
500 ppapi::host::ReplyMessageContext reply_context, 471 ppapi::host::ReplyMessageContext reply_context,
501 base::PlatformFileError error_code) { 472 base::PlatformFileError error_code) {
502 reply_context.params.set_result( 473 reply_context.params.set_result(
503 ::ppapi::PlatformFileErrorToPepperError(error_code)); 474 ::ppapi::PlatformFileErrorToPepperError(error_code));
504 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); 475 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
505 state_manager_.SetOperationFinished(); 476 state_manager_.SetOperationFinished();
506 } 477 }
507 478
479 void PepperFileIOHost::ExecuteGeneralCallback(
480 ppapi::host::ReplyMessageContext reply_context,
481 int32_t pp_error) {
482 reply_context.params.set_result(pp_error);
483 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
484 state_manager_.SetOperationFinished();
485 }
486
508 void PepperFileIOHost::ExecutePlatformOpenFileCallback( 487 void PepperFileIOHost::ExecutePlatformOpenFileCallback(
509 ppapi::host::ReplyMessageContext reply_context, 488 ppapi::host::ReplyMessageContext reply_context,
510 base::PlatformFileError error_code, 489 base::PlatformFileError error_code,
511 base::PassPlatformFile file) { 490 base::PassPlatformFile file) {
512 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); 491 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
513 if (pp_error == PP_OK) 492 if (pp_error == PP_OK)
514 state_manager_.SetOpenSucceed(); 493 state_manager_.SetOpenSucceed();
515 494
516 DCHECK(file_ == base::kInvalidPlatformFileValue); 495 DCHECK(file_ == base::kInvalidPlatformFileValue);
517 file_ = file.ReleaseValue(); 496 file_ = file.ReleaseValue();
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 buffer.append(data, bytes_read); 552 buffer.append(data, bytes_read);
574 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); 553 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read));
575 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); 554 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer));
576 state_manager_.SetOperationFinished(); 555 state_manager_.SetOperationFinished();
577 } 556 }
578 557
579 void PepperFileIOHost::ExecutePlatformWriteCallback( 558 void PepperFileIOHost::ExecutePlatformWriteCallback(
580 ppapi::host::ReplyMessageContext reply_context, 559 ppapi::host::ReplyMessageContext reply_context,
581 base::PlatformFileError error_code, 560 base::PlatformFileError error_code,
582 int bytes_written) { 561 int bytes_written) {
583 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
584 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written));
585 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
586 state_manager_.SetOperationFinished();
587 }
588
589 void PepperFileIOHost::ExecutePlatformWillWriteCallback(
590 ppapi::host::ReplyMessageContext reply_context,
591 base::PlatformFileError error_code,
592 int bytes_written) {
593 // On the plugin side, the callback expects a parameter with different meaning 562 // On the plugin side, the callback expects a parameter with different meaning
594 // depends on whether is negative or not. It is the result here. We translate 563 // depends on whether is negative or not. It is the result here. We translate
595 // for the callback. 564 // for the callback.
596 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); 565 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code);
597 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); 566 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written));
598 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); 567 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply());
599 state_manager_.SetOperationFinished(); 568 state_manager_.SetOperationFinished();
600 } 569 }
601 570
602 } // namespace content 571 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698