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 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_ | |
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/platform_file.h" | |
13 #include "base/process.h" | |
14 #include "webkit/fileapi/directory_entry.h" | |
15 | |
16 namespace base { | |
17 class Time; | |
18 } // namespace base | |
19 | |
20 namespace net { | |
21 class URLRequestContext; | |
22 } // namespace net | |
23 | |
24 namespace webkit_blob { | |
25 class ShareableFileReference; | |
26 } | |
27 | |
28 class GURL; | |
29 | |
30 namespace fileapi { | |
31 | |
32 class FileSystemURL; | |
33 class LocalFileSystemOperation; | |
34 | |
35 // The interface class for FileSystemOperation implementations. | |
36 // | |
37 // This interface defines file system operations required to implement | |
38 // "File API: Directories and System" | |
39 // http://www.w3.org/TR/file-system-api/ | |
40 // | |
41 // DESIGN NOTES | |
42 // | |
43 // This class is designed to | |
44 // | |
45 // 1) Serve one-time file system operation per instance. Only one | |
46 // method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists, | |
47 // GetMetadata, ReadDirectory and Remove) may be called during the | |
48 // lifetime of this object and it should be called no more than once. | |
49 // | |
50 // 2) Be self-destructed, or get deleted via base::Owned() after the | |
51 // operation finishes and completion callback is called. | |
52 // | |
53 // 3) Deliver the results of operations to the client via the callback function | |
54 // passed as the last parameter of the method. | |
55 // | |
56 class FileSystemOperation { | |
57 public: | |
58 virtual ~FileSystemOperation() {} | |
59 | |
60 // Used for CreateFile(), etc. |result| is the return code of the operation. | |
61 typedef base::Callback<void(base::PlatformFileError result)> StatusCallback; | |
62 | |
63 // Used for GetMetadata(). |result| is the return code of the operation, | |
64 // |file_info| is the obtained file info, and |platform_path| is the path | |
65 // of the file. | |
66 typedef base::Callback< | |
67 void(base::PlatformFileError result, | |
68 const base::PlatformFileInfo& file_info, | |
69 const base::FilePath& platform_path)> GetMetadataCallback; | |
70 | |
71 // Used for OpenFile(). |result| is the return code of the operation. | |
72 // |on_close_callback| will be called after the file is closed in the child | |
73 // process. | |
74 typedef base::Callback< | |
75 void(base::PlatformFileError result, | |
76 base::PlatformFile file, | |
77 const base::Closure& on_close_callback, | |
78 base::ProcessHandle peer_handle)> OpenFileCallback; | |
79 | |
80 // Used for ReadDirectoryCallback. | |
81 typedef std::vector<DirectoryEntry> FileEntryList; | |
82 | |
83 // Used for ReadDirectory(). |result| is the return code of the operation, | |
84 // |file_list| is the list of files read, and |has_more| is true if some files | |
85 // are yet to be read. | |
86 typedef base::Callback< | |
87 void(base::PlatformFileError result, | |
88 const FileEntryList& file_list, | |
89 bool has_more)> ReadDirectoryCallback; | |
90 | |
91 // Used for CreateSnapshotFile(). (Please see the comment at | |
92 // CreateSnapshotFile() below for how the method is called) | |
93 // |result| is the return code of the operation. | |
94 // |file_info| is the metadata of the snapshot file created. | |
95 // |platform_path| is the path to the snapshot file created. | |
96 // | |
97 // The snapshot file could simply be of the local file pointed by the given | |
98 // filesystem URL in local filesystem cases; remote filesystems | |
99 // may want to download the file into a temporary snapshot file and then | |
100 // return the metadata of the temporary file. | |
101 // | |
102 // |file_ref| is used to manage the lifetime of the returned | |
103 // snapshot file. It can be set to let the chromium backend take | |
104 // care of the life time of the snapshot file. Otherwise (if the returned | |
105 // file does not require any handling) the implementation can just | |
106 // return NULL. In a more complex case, the implementaiton can manage | |
107 // the lifetime of the snapshot file on its own (e.g. by its cache system) | |
108 // but also can be notified via the reference when the file becomes no | |
109 // longer necessary in the javascript world. | |
110 // Please see the comment for ShareableFileReference for details. | |
111 // | |
112 typedef base::Callback< | |
113 void(base::PlatformFileError result, | |
114 const base::PlatformFileInfo& file_info, | |
115 const base::FilePath& platform_path, | |
116 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref)> | |
117 SnapshotFileCallback; | |
118 | |
119 // Used for Write(). | |
120 typedef base::Callback<void(base::PlatformFileError result, | |
121 int64 bytes, | |
122 bool complete)> WriteCallback; | |
123 | |
124 // Creates a file at |path|. If |exclusive| is true, an error is raised | |
125 // in case a file is already present at the URL. | |
126 virtual void CreateFile(const FileSystemURL& path, | |
127 bool exclusive, | |
128 const StatusCallback& callback) = 0; | |
129 | |
130 // Creates a directory at |path|. If |exclusive| is true, an error is | |
131 // raised in case a directory is already present at the URL. If | |
132 // |recursive| is true, create parent directories as needed just like | |
133 // mkdir -p does. | |
134 virtual void CreateDirectory(const FileSystemURL& path, | |
135 bool exclusive, | |
136 bool recursive, | |
137 const StatusCallback& callback) = 0; | |
138 | |
139 // Copies a file or directory from |src_path| to |dest_path|. If | |
140 // |src_path| is a directory, the contents of |src_path| are copied to | |
141 // |dest_path| recursively. A new file or directory is created at | |
142 // |dest_path| as needed. | |
143 virtual void Copy(const FileSystemURL& src_path, | |
144 const FileSystemURL& dest_path, | |
145 const StatusCallback& callback) = 0; | |
146 | |
147 // Moves a file or directory from |src_path| to |dest_path|. A new file | |
148 // or directory is created at |dest_path| as needed. | |
149 virtual void Move(const FileSystemURL& src_path, | |
150 const FileSystemURL& dest_path, | |
151 const StatusCallback& callback) = 0; | |
152 | |
153 // Checks if a directory is present at |path|. | |
154 virtual void DirectoryExists(const FileSystemURL& path, | |
155 const StatusCallback& callback) = 0; | |
156 | |
157 // Checks if a file is present at |path|. | |
158 virtual void FileExists(const FileSystemURL& path, | |
159 const StatusCallback& callback) = 0; | |
160 | |
161 // Gets the metadata of a file or directory at |path|. | |
162 virtual void GetMetadata(const FileSystemURL& path, | |
163 const GetMetadataCallback& callback) = 0; | |
164 | |
165 // Reads contents of a directory at |path|. | |
166 virtual void ReadDirectory(const FileSystemURL& path, | |
167 const ReadDirectoryCallback& callback) = 0; | |
168 | |
169 // Removes a file or directory at |path|. If |recursive| is true, remove | |
170 // all files and directories under the directory at |path| recursively. | |
171 virtual void Remove(const FileSystemURL& path, bool recursive, | |
172 const StatusCallback& callback) = 0; | |
173 | |
174 // Writes contents of |blob_url| to |path| at |offset|. | |
175 // |url_request_context| is used to read contents in |blob_url|. | |
176 virtual void Write(const net::URLRequestContext* url_request_context, | |
177 const FileSystemURL& path, | |
178 const GURL& blob_url, | |
179 int64 offset, | |
180 const WriteCallback& callback) = 0; | |
181 | |
182 // Truncates a file at |path| to |length|. If |length| is larger than | |
183 // the original file size, the file will be extended, and the extended | |
184 // part is filled with null bytes. | |
185 virtual void Truncate(const FileSystemURL& path, int64 length, | |
186 const StatusCallback& callback) = 0; | |
187 | |
188 // Tries to cancel the current operation [we support cancelling write or | |
189 // truncate only]. Reports failure for the current operation, then reports | |
190 // success for the cancel operation itself via the |cancel_dispatcher|. | |
191 // | |
192 // E.g. a typical cancel implementation would look like: | |
193 // | |
194 // virtual void SomeOperationImpl::Cancel( | |
195 // const StatusCallback& cancel_callback) { | |
196 // // Abort the current inflight operation first. | |
197 // ... | |
198 // | |
199 // // Dispatch ABORT error for the current operation by invoking | |
200 // // the callback function for the ongoing operation, | |
201 // operation_callback.Run(base::PLATFORM_FILE_ERROR_ABORT, ...); | |
202 // | |
203 // // Dispatch 'success' for the cancel (or dispatch appropriate | |
204 // // error code with DidFail() if the cancel has somehow failed). | |
205 // cancel_callback.Run(base::PLATFORM_FILE_OK); | |
206 // } | |
207 // | |
208 // Note that, for reporting failure, the callback function passed to a | |
209 // cancellable operations are kept around with the operation instance | |
210 // (as |operation_callback_| in the code example). | |
211 virtual void Cancel(const StatusCallback& cancel_callback) = 0; | |
212 | |
213 // Modifies timestamps of a file or directory at |path| with | |
214 // |last_access_time| and |last_modified_time|. The function DOES NOT | |
215 // create a file unlike 'touch' command on Linux. | |
216 // | |
217 // This function is used only by Pepper as of writing. | |
218 virtual void TouchFile(const FileSystemURL& path, | |
219 const base::Time& last_access_time, | |
220 const base::Time& last_modified_time, | |
221 const StatusCallback& callback) = 0; | |
222 | |
223 // Opens a file at |path| with |file_flags|, where flags are OR'ed | |
224 // values of base::PlatformFileFlags. | |
225 // | |
226 // |peer_handle| is the process handle of a pepper plugin process, which | |
227 // is necessary for underlying IPC calls with Pepper plugins. | |
228 // | |
229 // This function is used only by Pepper as of writing. | |
230 virtual void OpenFile(const FileSystemURL& path, | |
231 int file_flags, | |
232 base::ProcessHandle peer_handle, | |
233 const OpenFileCallback& callback) = 0; | |
234 | |
235 // For downcasting to FileSystemOperation. | |
236 // TODO(kinuko): this hack should go away once appropriate upload-stream | |
237 // handling based on element types is supported. | |
238 virtual LocalFileSystemOperation* AsLocalFileSystemOperation() = 0; | |
239 | |
240 // Creates a local snapshot file for a given |path| and returns the | |
241 // metadata and platform path of the snapshot file via |callback|. | |
242 // In local filesystem cases the implementation may simply return | |
243 // the metadata of the file itself (as well as GetMetadata does), | |
244 // while in remote filesystem case the backend may want to download the file | |
245 // into a temporary snapshot file and return the metadata of the | |
246 // temporary file. Or if the implementaiton already has the local cache | |
247 // data for |path| it can simply return the path to the cache. | |
248 virtual void CreateSnapshotFile(const FileSystemURL& path, | |
249 const SnapshotFileCallback& callback) = 0; | |
250 | |
251 protected: | |
252 // Used only for internal assertions. | |
253 enum OperationType { | |
254 kOperationNone, | |
255 kOperationCreateFile, | |
256 kOperationCreateDirectory, | |
257 kOperationCreateSnapshotFile, | |
258 kOperationCopy, | |
259 kOperationCopyInForeignFile, | |
260 kOperationMove, | |
261 kOperationDirectoryExists, | |
262 kOperationFileExists, | |
263 kOperationGetMetadata, | |
264 kOperationReadDirectory, | |
265 kOperationRemove, | |
266 kOperationWrite, | |
267 kOperationTruncate, | |
268 kOperationTouchFile, | |
269 kOperationOpenFile, | |
270 kOperationCloseFile, | |
271 kOperationGetLocalPath, | |
272 kOperationCancel, | |
273 }; | |
274 }; | |
275 | |
276 } // namespace fileapi | |
277 | |
278 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_ | |
OLD | NEW |