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 | |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
11 #include "base/file_path.h" | 9 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
15 #include "base/platform_file.h" | 13 #include "ppapi/shared_impl/ppb_file_io_shared.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" | |
21 #include "webkit/plugins/ppapi/plugin_delegate.h" | 14 #include "webkit/plugins/ppapi/plugin_delegate.h" |
22 | 15 |
23 struct PP_CompletionCallback; | 16 struct PP_CompletionCallback; |
24 struct PPB_FileIO; | 17 struct PPB_FileIO; |
25 | 18 |
26 namespace webkit { | 19 namespace webkit { |
27 namespace ppapi { | 20 namespace ppapi { |
28 | 21 |
29 class QuotaFileIO; | 22 class QuotaFileIO; |
30 | 23 |
31 class PPB_FileIO_Impl : public ::ppapi::Resource, | 24 class PPB_FileIO_Impl : public ::ppapi::PPB_FileIO_Shared { |
32 public ::ppapi::thunk::PPB_FileIO_API { | |
33 public: | 25 public: |
34 explicit PPB_FileIO_Impl(PP_Instance instance); | 26 explicit PPB_FileIO_Impl(PP_Instance instance); |
35 virtual ~PPB_FileIO_Impl(); | 27 virtual ~PPB_FileIO_Impl(); |
36 | 28 |
37 // Resource overrides. | 29 // PPB_FileIO_API implementation (most of the operations are implemented |
38 virtual ::ppapi::thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; | 30 // as the "Validated" versions below). |
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; | |
60 virtual void Close() OVERRIDE; | 31 virtual void Close() OVERRIDE; |
61 virtual int32_t GetOSFileDescriptor() OVERRIDE; | 32 virtual int32_t GetOSFileDescriptor() OVERRIDE; |
62 virtual int32_t WillWrite(int64_t offset, | 33 virtual int32_t WillWrite(int64_t offset, |
63 int32_t bytes_to_write, | 34 int32_t bytes_to_write, |
64 PP_CompletionCallback callback) OVERRIDE; | 35 PP_CompletionCallback callback) OVERRIDE; |
65 virtual int32_t WillSetLength(int64_t length, | 36 virtual int32_t WillSetLength(int64_t length, |
66 PP_CompletionCallback callback) OVERRIDE; | 37 PP_CompletionCallback callback) OVERRIDE; |
67 | 38 |
68 private: | 39 private: |
69 struct CallbackEntry { | 40 // FileIOImpl overrides. |
70 CallbackEntry(); | 41 virtual int32_t OpenValidated(PP_Resource file_ref_resource, |
71 CallbackEntry(const CallbackEntry& entry); | 42 ::ppapi::thunk::PPB_FileRef_API* file_ref_api, |
72 ~CallbackEntry(); | 43 int32_t open_flags, |
| 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; |
73 | 61 |
74 scoped_refptr<TrackedCompletionCallback> callback; | 62 // Returns the plugin delegate for this resource if it exists, or NULL if it |
| 63 // doesn't. Calling code should always check for NULL. |
| 64 PluginDelegate* GetPluginDelegate(); |
75 | 65 |
76 // Pointer back to the caller's read buffer; only used by |Read()|. | 66 // Callback handlers. These mostly convert the PlatformFileError to the |
77 // Not owned. | 67 // PP_Error code and call the shared (non-"Platform") version. |
78 char* read_buffer; | 68 void ExecutePlatformGeneralCallback(base::PlatformFileError error_code); |
79 }; | 69 void ExecutePlatformOpenFileCallback(base::PlatformFileError error_code, |
80 | 70 base::PassPlatformFile file); |
81 enum OperationType { | 71 void ExecutePlatformQueryCallback(base::PlatformFileError error_code, |
82 // If there are pending reads, any other kind of async operation is not | 72 const base::PlatformFileInfo& file_info); |
83 // allowed. | 73 void ExecutePlatformReadCallback(base::PlatformFileError error_code, |
84 OPERATION_READ, | 74 const char* data, int bytes_read); |
85 // If there are pending writes, any other kind of async operation is not | 75 void ExecutePlatformWriteCallback(base::PlatformFileError error_code, |
86 // allowed. | 76 int bytes_written); |
87 OPERATION_WRITE, | 77 void ExecutePlatformWillWriteCallback(base::PlatformFileError error_code, |
88 // If there is a pending operation that is neither read nor write, no | 78 int bytes_written); |
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); | |
123 | 79 |
124 base::PlatformFile file_; | 80 base::PlatformFile file_; |
125 PP_FileSystemType file_system_type_; | |
126 | 81 |
127 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. | 82 // Valid only for PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}. |
128 GURL file_system_url_; | 83 GURL file_system_url_; |
129 | 84 |
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 | |
137 // Pointer to a QuotaFileIO instance, which is valid only while a file | 85 // Pointer to a QuotaFileIO instance, which is valid only while a file |
138 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. | 86 // of type PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY} is opened. |
139 scoped_ptr<QuotaFileIO> quota_file_io_; | 87 scoped_ptr<QuotaFileIO> quota_file_io_; |
140 | 88 |
141 base::WeakPtrFactory<PPB_FileIO_Impl> weak_factory_; | 89 base::WeakPtrFactory<PPB_FileIO_Impl> weak_factory_; |
142 | 90 |
143 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); | 91 DISALLOW_COPY_AND_ASSIGN(PPB_FileIO_Impl); |
144 }; | 92 }; |
145 | 93 |
146 } // namespace ppapi | 94 } // namespace ppapi |
147 } // namespace webkit | 95 } // namespace webkit |
148 | 96 |
149 #endif // WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ | 97 #endif // WEBKIT_PLUGINS_PPAPI_PPB_FILE_IO_IMPL_H_ |
OLD | NEW |