OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 5 #ifndef WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
6 #define WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 6 #define WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
7 | 7 |
| 8 #include <queue> |
| 9 |
8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
9 #include "base/compiler_specific.h" | 11 #include "base/file_path.h" |
10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
13 #include "ppapi/shared_impl/ppb_file_io_shared.h" | 15 #include "base/platform_file.h" |
| 16 #include "ppapi/c/pp_file_info.h" |
| 17 #include "ppapi/c/pp_time.h" |
| 18 #include "ppapi/shared_impl/resource.h" |
| 19 #include "ppapi/thunk/ppb_file_io_api.h" |
| 20 #include "webkit/plugins/ppapi/callbacks.h" |
14 #include "webkit/plugins/ppapi/plugin_delegate.h" | 21 #include "webkit/plugins/ppapi/plugin_delegate.h" |
15 | 22 |
16 struct PP_CompletionCallback; | 23 struct PP_CompletionCallback; |
17 struct PPB_FileIO; | 24 struct PPB_FileIO; |
18 | 25 |
19 namespace webkit { | 26 namespace webkit { |
20 namespace ppapi { | 27 namespace ppapi { |
21 | 28 |
22 class QuotaFileIO; | 29 class QuotaFileIO; |
23 | 30 |
24 class PPB_FileIO_Impl : public ::ppapi::PPB_FileIO_Shared { | 31 class PPB_FileIO_Impl : public ::ppapi::Resource, |
| 32 public ::ppapi::thunk::PPB_FileIO_API { |
25 public: | 33 public: |
26 explicit PPB_FileIO_Impl(PP_Instance instance); | 34 explicit PPB_FileIO_Impl(PP_Instance instance); |
27 virtual ~PPB_FileIO_Impl(); | 35 virtual ~PPB_FileIO_Impl(); |
28 | 36 |
29 // PPB_FileIO_API implementation (most of the operations are implemented | 37 // Resource overrides. |
30 // as the "Validated" versions below). | 38 virtual ::ppapi::thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; |
| 39 |
| 40 // PPB_FileIO_API implementation. |
| 41 virtual int32_t Open(PP_Resource file_ref, |
| 42 int32_t open_flags, |
| 43 PP_CompletionCallback callback) OVERRIDE; |
| 44 virtual int32_t Query(PP_FileInfo* info, |
| 45 PP_CompletionCallback callback) OVERRIDE; |
| 46 virtual int32_t Touch(PP_Time last_access_time, |
| 47 PP_Time last_modified_time, |
| 48 PP_CompletionCallback callback) OVERRIDE; |
| 49 virtual int32_t Read(int64_t offset, |
| 50 char* buffer, |
| 51 int32_t bytes_to_read, |
| 52 PP_CompletionCallback callback) OVERRIDE; |
| 53 virtual int32_t Write(int64_t offset, |
| 54 const char* buffer, |
| 55 int32_t bytes_to_write, |
| 56 PP_CompletionCallback callback) OVERRIDE; |
| 57 virtual int32_t SetLength(int64_t length, |
| 58 PP_CompletionCallback callback) OVERRIDE; |
| 59 virtual int32_t Flush(PP_CompletionCallback callback) OVERRIDE; |
31 virtual void Close() OVERRIDE; | 60 virtual void Close() OVERRIDE; |
32 virtual int32_t GetOSFileDescriptor() OVERRIDE; | 61 virtual int32_t GetOSFileDescriptor() OVERRIDE; |
33 virtual int32_t WillWrite(int64_t offset, | 62 virtual int32_t WillWrite(int64_t offset, |
34 int32_t bytes_to_write, | 63 int32_t bytes_to_write, |
35 PP_CompletionCallback callback) OVERRIDE; | 64 PP_CompletionCallback callback) OVERRIDE; |
36 virtual int32_t WillSetLength(int64_t length, | 65 virtual int32_t WillSetLength(int64_t length, |
37 PP_CompletionCallback callback) OVERRIDE; | 66 PP_CompletionCallback callback) OVERRIDE; |
38 | 67 |
39 private: | 68 private: |
40 // FileIOImpl overrides. | 69 struct CallbackEntry { |
41 virtual int32_t OpenValidated(PP_Resource file_ref_resource, | 70 CallbackEntry(); |
42 ::ppapi::thunk::PPB_FileRef_API* file_ref_api, | 71 CallbackEntry(const CallbackEntry& entry); |
43 int32_t open_flags, | 72 ~CallbackEntry(); |
44 PP_CompletionCallback callback) OVERRIDE; | |
45 virtual int32_t QueryValidated(PP_FileInfo* info, | |
46 PP_CompletionCallback callback) OVERRIDE; | |
47 virtual int32_t TouchValidated(PP_Time last_access_time, | |
48 PP_Time last_modified_time, | |
49 PP_CompletionCallback callback) OVERRIDE; | |
50 virtual int32_t ReadValidated(int64_t offset, | |
51 char* buffer, | |
52 int32_t bytes_to_read, | |
53 PP_CompletionCallback callback) OVERRIDE; | |
54 virtual int32_t WriteValidated(int64_t offset, | |
55 const char* buffer, | |
56 int32_t bytes_to_write, | |
57 PP_CompletionCallback callback) OVERRIDE; | |
58 virtual int32_t SetLengthValidated(int64_t length, | |
59 PP_CompletionCallback callback) OVERRIDE; | |
60 virtual int32_t FlushValidated(PP_CompletionCallback callback) OVERRIDE; | |
61 | 73 |
62 // Returns the plugin delegate for this resource if it exists, or NULL if it | 74 scoped_refptr<TrackedCompletionCallback> callback; |
63 // doesn't. Calling code should always check for NULL. | |
64 PluginDelegate* GetPluginDelegate(); | |
65 | 75 |
66 // Callback handlers. These mostly convert the PlatformFileError to the | 76 // Pointer back to the caller's read buffer; only used by |Read()|. |
67 // PP_Error code and call the shared (non-"Platform") version. | 77 // Not owned. |
68 void ExecutePlatformGeneralCallback(base::PlatformFileError error_code); | 78 char* read_buffer; |
69 void ExecutePlatformOpenFileCallback(base::PlatformFileError error_code, | 79 }; |
70 base::PassPlatformFile file); | 80 |
71 void ExecutePlatformQueryCallback(base::PlatformFileError error_code, | 81 enum OperationType { |
72 const base::PlatformFileInfo& file_info); | 82 // If there are pending reads, any other kind of async operation is not |
73 void ExecutePlatformReadCallback(base::PlatformFileError error_code, | 83 // allowed. |
74 const char* data, int bytes_read); | 84 OPERATION_READ, |
75 void ExecutePlatformWriteCallback(base::PlatformFileError error_code, | 85 // If there are pending writes, any other kind of async operation is not |
76 int bytes_written); | 86 // allowed. |
77 void ExecutePlatformWillWriteCallback(base::PlatformFileError error_code, | 87 OPERATION_WRITE, |
78 int bytes_written); | 88 // If there is a pending operation that is neither read nor write, no |
| 89 // further async operation is allowed. |
| 90 OPERATION_EXCLUSIVE, |
| 91 // There is no pending operation right now. |
| 92 OPERATION_NONE, |
| 93 }; |
| 94 |
| 95 // Verifies: |
| 96 // - that |callback| is valid (only nonblocking operation supported); |
| 97 // - that the file is already open or not, depending on |should_be_open|; and |
| 98 // - that no callback is already pending, or it is a read(write) request |
| 99 // and currently the pending operations are reads(writes). |
| 100 // Returns |PP_OK| to indicate that everything is valid or |PP_ERROR_...| if |
| 101 // the call should be aborted and that code returned to the plugin. |
| 102 int32_t CommonCallValidation(bool should_be_open, |
| 103 OperationType new_op, |
| 104 PP_CompletionCallback callback); |
| 105 |
| 106 // Sets up a pending callback. This should only be called once it is certain |
| 107 // that |PP_OK_COMPLETIONPENDING| will be returned. |
| 108 // |read_buffer| is only used by read operations. |
| 109 void RegisterCallback(OperationType op, |
| 110 PP_CompletionCallback callback, |
| 111 char* read_buffer); |
| 112 void RunAndRemoveFirstPendingCallback(int32_t result); |
| 113 |
| 114 void StatusCallback(base::PlatformFileError error_code); |
| 115 void AsyncOpenFileCallback(base::PlatformFileError error_code, |
| 116 base::PassPlatformFile file); |
| 117 void QueryInfoCallback(base::PlatformFileError error_code, |
| 118 const base::PlatformFileInfo& file_info); |
| 119 void ReadCallback(base::PlatformFileError error_code, |
| 120 const char* data, int bytes_read); |
| 121 void WriteCallback(base::PlatformFileError error_code, int bytes_written); |
| 122 void WillWriteCallback(base::PlatformFileError error_code, int bytes_written); |
79 | 123 |
80 base::PlatformFile file_; | 124 base::PlatformFile file_; |
| 125 PP_FileSystemType file_system_type_; |
81 | 126 |
82 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. | 127 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. |
83 GURL file_system_url_; | 128 GURL file_system_url_; |
84 | 129 |
| 130 std::queue<CallbackEntry> callbacks_; |
| 131 OperationType pending_op_; |
| 132 |
| 133 // Output buffer pointer for |Query()|; only non-null when a callback is |
| 134 // pending for it. |
| 135 PP_FileInfo* info_; |
| 136 |
85 // Pointer to a QuotaFileIO instance, which is valid only while a file | 137 // Pointer to a QuotaFileIO instance, which is valid only while a file |
86 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. | 138 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. |
87 scoped_ptr<QuotaFileIO> quota_file_io_; | 139 scoped_ptr<QuotaFileIO> quota_file_io_; |
88 | 140 |
89 base::WeakPtrFactory<PPB_FileIO_Impl> weak_factory_; | 141 base::WeakPtrFactory<PPB_FileIO_Impl> weak_factory_; |
90 | 142 |
91 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); | 143 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); |
92 }; | 144 }; |
93 | 145 |
94 } // namespace ppapi | 146 } // namespace ppapi |
95 } // namespace webkit | 147 } // namespace webkit |
96 | 148 |
97 #endif // WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 149 #endif // WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
OLD | NEW |