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 "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 (base::PlatformFileError)> PlatformGeneralCallback; |
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(base::PLATFORM_FILE_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(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 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 buffer.append(data, bytes_read); | 544 buffer.append(data, bytes_read); |
574 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); | 545 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_read)); |
575 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); | 546 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_ReadReply(buffer)); |
576 state_manager_.SetOperationFinished(); | 547 state_manager_.SetOperationFinished(); |
577 } | 548 } |
578 | 549 |
579 void PepperFileIOHost::ExecutePlatformWriteCallback( | 550 void PepperFileIOHost::ExecutePlatformWriteCallback( |
580 ppapi::host::ReplyMessageContext reply_context, | 551 ppapi::host::ReplyMessageContext reply_context, |
581 base::PlatformFileError error_code, | 552 base::PlatformFileError error_code, |
582 int bytes_written) { | 553 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 | 554 // 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 | 555 // depends on whether is negative or not. It is the result here. We translate |
595 // for the callback. | 556 // for the callback. |
596 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); | 557 int32_t pp_error = ::ppapi::PlatformFileErrorToPepperError(error_code); |
597 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); | 558 reply_context.params.set_result(ErrorOrByteNumber(pp_error, bytes_written)); |
598 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); | 559 host()->SendReply(reply_context, PpapiPluginMsg_FileIO_GeneralReply()); |
599 state_manager_.SetOperationFinished(); | 560 state_manager_.SetOperationFinished(); |
600 } | 561 } |
601 | 562 |
602 } // namespace content | 563 } // namespace content |
OLD | NEW |