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