| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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_ASYNC_FILE_UTIL_H_ | |
| 6 #define WEBKIT_FILEAPI_ASYNC_FILE_UTIL_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/callback_forward.h" | |
| 10 #include "base/files/file_util_proxy.h" | |
| 11 #include "base/platform_file.h" | |
| 12 #include "webkit/storage/webkit_storage_export.h" | |
| 13 | |
| 14 namespace base { | |
| 15 class Time; | |
| 16 } | |
| 17 | |
| 18 namespace webkit_blob { | |
| 19 class ShareableFileReference; | |
| 20 } | |
| 21 | |
| 22 namespace fileapi { | |
| 23 | |
| 24 class FileSystemOperationContext; | |
| 25 class FileSystemURL; | |
| 26 | |
| 27 // An interface which provides filesystem-specific file operations for | |
| 28 // LocalFileSystemOperation. | |
| 29 // | |
| 30 // Each filesystem which needs to be dispatched from LocalFileSystemOperation | |
| 31 // must implement this interface or a synchronous version of interface: | |
| 32 // FileSystemFileUtil. | |
| 33 // | |
| 34 class WEBKIT_STORAGE_EXPORT AsyncFileUtil { | |
| 35 public: | |
| 36 typedef base::Callback< | |
| 37 void(base::PlatformFileError result)> StatusCallback; | |
| 38 | |
| 39 typedef base::FileUtilProxy::CreateOrOpenCallback CreateOrOpenCallback; | |
| 40 | |
| 41 typedef base::Callback< | |
| 42 void(base::PlatformFileError result, | |
| 43 bool created)> EnsureFileExistsCallback; | |
| 44 | |
| 45 typedef base::Callback< | |
| 46 void(base::PlatformFileError result, | |
| 47 const base::PlatformFileInfo& file_info, | |
| 48 const base::FilePath& platform_path)> GetFileInfoCallback; | |
| 49 | |
| 50 typedef base::FileUtilProxy::Entry Entry; | |
| 51 typedef std::vector<base::FileUtilProxy::Entry> EntryList; | |
| 52 typedef base::Callback< | |
| 53 void(base::PlatformFileError result, | |
| 54 const EntryList& file_list, | |
| 55 bool has_more)> ReadDirectoryCallback; | |
| 56 | |
| 57 typedef base::Callback< | |
| 58 void(base::PlatformFileError result, | |
| 59 const base::PlatformFileInfo& file_info, | |
| 60 const base::FilePath& platform_path, | |
| 61 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref | |
| 62 )> CreateSnapshotFileCallback; | |
| 63 | |
| 64 AsyncFileUtil() {} | |
| 65 virtual ~AsyncFileUtil() {} | |
| 66 | |
| 67 // Creates or opens a file with the given flags. | |
| 68 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create | |
| 69 // a new file at the given |url| and calls back with | |
| 70 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |url| already exists. | |
| 71 // | |
| 72 // LocalFileSystemOperation::OpenFile calls this. | |
| 73 // This is used only by Pepper/NaCL File API. | |
| 74 // | |
| 75 // This returns false if it fails to post an async task. | |
| 76 // | |
| 77 virtual bool CreateOrOpen( | |
| 78 FileSystemOperationContext* context, | |
| 79 const FileSystemURL& url, | |
| 80 int file_flags, | |
| 81 const CreateOrOpenCallback& callback) = 0; | |
| 82 | |
| 83 // Ensures that the given |url| exist. This creates a empty new file | |
| 84 // at |url| if the |url| does not exist. | |
| 85 // | |
| 86 // LocalFileSystemOperation::CreateFile calls this. | |
| 87 // | |
| 88 // This returns false if it fails to post an async task. | |
| 89 // | |
| 90 // This reports following error code via |callback|: | |
| 91 // - PLATFORM_FILE_OK and created==true if a file has not existed and | |
| 92 // is created at |url|. | |
| 93 // - PLATFORM_FILE_OK and created==false if the file already exists. | |
| 94 // - Other error code (with created=false) if a file hasn't existed yet | |
| 95 // and there was an error while creating a new file. | |
| 96 // | |
| 97 virtual bool EnsureFileExists( | |
| 98 FileSystemOperationContext* context, | |
| 99 const FileSystemURL& url, | |
| 100 const EnsureFileExistsCallback& callback) = 0; | |
| 101 | |
| 102 // Creates directory at given url. | |
| 103 // | |
| 104 // LocalFileSystemOperation::CreateDirectory calls this. | |
| 105 // | |
| 106 // This returns false if it fails to post an async task. | |
| 107 // | |
| 108 // This reports following error code via |callback|: | |
| 109 // - PLATFORM_FILE_ERROR_NOT_FOUND if the |url|'s parent directory | |
| 110 // does not exist and |recursive| is false. | |
| 111 // - PLATFORM_FILE_ERROR_EXISTS if a directory already exists at |url| | |
| 112 // and |exclusive| is true. | |
| 113 // - PLATFORM_FILE_ERROR_EXISTS if a file already exists at |url| | |
| 114 // (regardless of |exclusive| value). | |
| 115 // - Other error code if it failed to create a directory. | |
| 116 // | |
| 117 virtual bool CreateDirectory( | |
| 118 FileSystemOperationContext* context, | |
| 119 const FileSystemURL& url, | |
| 120 bool exclusive, | |
| 121 bool recursive, | |
| 122 const StatusCallback& callback) = 0; | |
| 123 | |
| 124 // Retrieves the information about a file. | |
| 125 // | |
| 126 // LocalFileSystemOperation::GetMetadata calls this. | |
| 127 // | |
| 128 // This returns false if it fails to post an async task. | |
| 129 // | |
| 130 // This reports following error code via |callback|: | |
| 131 // - PLATFORM_FILE_ERROR_NOT_FOUND if the file doesn't exist. | |
| 132 // - Other error code if there was an error while retrieving the file info. | |
| 133 // | |
| 134 virtual bool GetFileInfo( | |
| 135 FileSystemOperationContext* context, | |
| 136 const FileSystemURL& url, | |
| 137 const GetFileInfoCallback& callback) = 0; | |
| 138 | |
| 139 // Reads contents of a directory at |path|. | |
| 140 // | |
| 141 // LocalFileSystemOperation::ReadDirectory calls this. | |
| 142 // | |
| 143 // Note that the |name| field of each entry in |file_list| | |
| 144 // returned by |callback| should have a base file name | |
| 145 // of the entry relative to the directory, but not an absolute path. | |
| 146 // | |
| 147 // (E.g. if ReadDirectory is called for a directory | |
| 148 // 'path/to/dir' and the directory has entries 'a' and 'b', | |
| 149 // the returned |file_list| should include entries whose names | |
| 150 // are 'a' and 'b', but not '/path/to/dir/a' and '/path/to/dir/b'.) | |
| 151 // | |
| 152 // This returns false if it fails to post an async task. | |
| 153 // | |
| 154 // This reports following error code via |callback|: | |
| 155 // - PLATFORM_FILE_ERROR_NOT_FOUND if the target directory doesn't exist. | |
| 156 // - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY if an entry exists at |url| but | |
| 157 // is a file (not a directory). | |
| 158 // | |
| 159 virtual bool ReadDirectory( | |
| 160 FileSystemOperationContext* context, | |
| 161 const FileSystemURL& url, | |
| 162 const ReadDirectoryCallback& callback) = 0; | |
| 163 | |
| 164 // Modifies timestamps of a file or directory at |url| with | |
| 165 // |last_access_time| and |last_modified_time|. The function DOES NOT | |
| 166 // create a file unlike 'touch' command on Linux. | |
| 167 // | |
| 168 // LocalFileSystemOperation::TouchFile calls this. | |
| 169 // This is used only by Pepper/NaCL File API. | |
| 170 // | |
| 171 // This returns false if it fails to post an async task. | |
| 172 virtual bool Touch( | |
| 173 FileSystemOperationContext* context, | |
| 174 const FileSystemURL& url, | |
| 175 const base::Time& last_access_time, | |
| 176 const base::Time& last_modified_time, | |
| 177 const StatusCallback& callback) = 0; | |
| 178 | |
| 179 // Truncates a file at |path| to |length|. If |length| is larger than | |
| 180 // the original file size, the file will be extended, and the extended | |
| 181 // part is filled with null bytes. | |
| 182 // | |
| 183 // LocalFileSystemOperation::Truncate calls this. | |
| 184 // | |
| 185 // This returns false if it fails to post an async task. | |
| 186 // | |
| 187 // This reports following error code via |callback|: | |
| 188 // - PLATFORM_FILE_ERROR_NOT_FOUND if the file doesn't exist. | |
| 189 // | |
| 190 virtual bool Truncate( | |
| 191 FileSystemOperationContext* context, | |
| 192 const FileSystemURL& url, | |
| 193 int64 length, | |
| 194 const StatusCallback& callback) = 0; | |
| 195 | |
| 196 // Copies a file from |src_url| to |dest_url|. | |
| 197 // This must be called for files that belong to the same filesystem | |
| 198 // (i.e. type() and origin() of the |src_url| and |dest_url| must match). | |
| 199 // | |
| 200 // LocalFileSystemOperation::Copy calls this for same-filesystem copy case. | |
| 201 // | |
| 202 // This returns false if it fails to post an async task. | |
| 203 // | |
| 204 // This reports following error code via |callback|: | |
| 205 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url| | |
| 206 // or the parent directory of |dest_url| does not exist. | |
| 207 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file. | |
| 208 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and | |
| 209 // is not a file. | |
| 210 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and | |
| 211 // its parent path is a file. | |
| 212 // | |
| 213 virtual bool CopyFileLocal( | |
| 214 FileSystemOperationContext* context, | |
| 215 const FileSystemURL& src_url, | |
| 216 const FileSystemURL& dest_url, | |
| 217 const StatusCallback& callback) = 0; | |
| 218 | |
| 219 // Moves a local file from |src_url| to |dest_url|. | |
| 220 // This must be called for files that belong to the same filesystem | |
| 221 // (i.e. type() and origin() of the |src_url| and |dest_url| must match). | |
| 222 // | |
| 223 // LocalFileSystemOperation::Move calls this for same-filesystem move case. | |
| 224 // | |
| 225 // This returns false if it fails to post an async task. | |
| 226 // | |
| 227 // This reports following error code via |callback|: | |
| 228 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url| | |
| 229 // or the parent directory of |dest_url| does not exist. | |
| 230 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file. | |
| 231 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and | |
| 232 // is not a file. | |
| 233 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and | |
| 234 // its parent path is a file. | |
| 235 // | |
| 236 virtual bool MoveFileLocal( | |
| 237 FileSystemOperationContext* context, | |
| 238 const FileSystemURL& src_url, | |
| 239 const FileSystemURL& dest_url, | |
| 240 const StatusCallback& callback) = 0; | |
| 241 | |
| 242 // Copies in a single file from a different filesystem. | |
| 243 // | |
| 244 // LocalFileSystemOperation::Copy or Move calls this for cross-filesystem | |
| 245 // cases. | |
| 246 // | |
| 247 // This returns false if it fails to post an async task. | |
| 248 // | |
| 249 // This reports following error code via |callback|: | |
| 250 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_file_path| | |
| 251 // or the parent directory of |dest_url| does not exist. | |
| 252 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and | |
| 253 // is not a file. | |
| 254 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and | |
| 255 // its parent path is a file. | |
| 256 // | |
| 257 virtual bool CopyInForeignFile( | |
| 258 FileSystemOperationContext* context, | |
| 259 const base::FilePath& src_file_path, | |
| 260 const FileSystemURL& dest_url, | |
| 261 const StatusCallback& callback) = 0; | |
| 262 | |
| 263 // Deletes a single file. | |
| 264 // | |
| 265 // LocalFileSystemOperation::RemoveFile calls this. | |
| 266 // | |
| 267 // This returns false if it fails to post an async task. | |
| 268 // | |
| 269 // This reports following error code via |callback|: | |
| 270 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist. | |
| 271 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |url| is not a file. | |
| 272 // | |
| 273 virtual bool DeleteFile( | |
| 274 FileSystemOperationContext* context, | |
| 275 const FileSystemURL& url, | |
| 276 const StatusCallback& callback) = 0; | |
| 277 | |
| 278 // Removes a single empty directory. | |
| 279 // | |
| 280 // LocalFileSystemOperation::RemoveDirectory calls this. | |
| 281 // | |
| 282 // This returns false if it fails to post an async task. | |
| 283 // | |
| 284 // This reports following error code via |callback|: | |
| 285 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist. | |
| 286 // - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory. | |
| 287 // - PLATFORM_FILE_ERROR_NOT_EMPTY if |url| is not empty. | |
| 288 // | |
| 289 virtual bool DeleteDirectory( | |
| 290 FileSystemOperationContext* context, | |
| 291 const FileSystemURL& url, | |
| 292 const StatusCallback& callback) = 0; | |
| 293 | |
| 294 // Creates a local snapshot file for a given |url| and returns the | |
| 295 // metadata and platform path of the snapshot file via |callback|. | |
| 296 // In regular filesystem cases the implementation may simply return | |
| 297 // the metadata of the file itself (as well as GetMetadata does), | |
| 298 // while in non-regular filesystem case the backend may create a | |
| 299 // temporary snapshot file which holds the file data and return | |
| 300 // the metadata of the temporary file. | |
| 301 // | |
| 302 // In the callback, it returns: | |
| 303 // |file_info| is the metadata of the snapshot file created. | |
| 304 // |platform_path| is the full absolute platform path to the snapshot | |
| 305 // file created. If a file is not backed by a real local file in | |
| 306 // the implementor's FileSystem, the implementor must create a | |
| 307 // local snapshot file and return the path of the created file. | |
| 308 // | |
| 309 // If implementors creates a temporary file for snapshotting and wants | |
| 310 // FileAPI backend to take care of the lifetime of the file (so that | |
| 311 // it won't get deleted while JS layer has any references to the created | |
| 312 // File/Blob object), it should return non-empty |file_ref|. | |
| 313 // Via the |file_ref| implementors can schedule a file deletion | |
| 314 // or arbitrary callbacks when the last reference of File/Blob is dropped. | |
| 315 // | |
| 316 // LocalFileSystemOperation::CreateSnapshotFile calls this. | |
| 317 // | |
| 318 // This returns false if it fails to post an async task. | |
| 319 // | |
| 320 // This reports following error code via |callback|: | |
| 321 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist. | |
| 322 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |url| exists but is a directory. | |
| 323 // | |
| 324 // The field values of |file_info| are undefined (implementation | |
| 325 // dependent) in error cases, and the caller should always | |
| 326 // check the return code. | |
| 327 virtual bool CreateSnapshotFile( | |
| 328 FileSystemOperationContext* context, | |
| 329 const FileSystemURL& url, | |
| 330 const CreateSnapshotFileCallback& callback) = 0; | |
| 331 | |
| 332 private: | |
| 333 DISALLOW_COPY_AND_ASSIGN(AsyncFileUtil); | |
| 334 }; | |
| 335 | |
| 336 } // namespace fileapi | |
| 337 | |
| 338 #endif // WEBKIT_FILEAPI_ASYNC_FILE_UTIL_H_ | |
| OLD | NEW |