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