| 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_LOCAL_FILE_SYSTEM_OPERATION_H_ | |
| 6 #define WEBKIT_FILEAPI_LOCAL_FILE_SYSTEM_OPERATION_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "webkit/blob/scoped_file.h" | |
| 13 #include "webkit/fileapi/file_system_operation.h" | |
| 14 #include "webkit/fileapi/file_system_url.h" | |
| 15 #include "webkit/fileapi/file_writer_delegate.h" | |
| 16 #include "webkit/quota/quota_types.h" | |
| 17 #include "webkit/storage/webkit_storage_export.h" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 class CrosMountPointProvider; | |
| 21 } | |
| 22 | |
| 23 namespace sync_file_system { | |
| 24 class SyncableFileSystemOperation; | |
| 25 } | |
| 26 | |
| 27 namespace fileapi { | |
| 28 | |
| 29 class AsyncFileUtil; | |
| 30 class FileSystemContext; | |
| 31 class RecursiveOperationDelegate; | |
| 32 | |
| 33 // FileSystemOperation implementation for local file systems. | |
| 34 class WEBKIT_STORAGE_EXPORT LocalFileSystemOperation | |
| 35 : public NON_EXPORTED_BASE(FileSystemOperation) { | |
| 36 public: | |
| 37 // NOTE: This constructor should not be called outside MountPointProviders; | |
| 38 // instead please consider using | |
| 39 // file_system_context->CreateFileSystemOperation() to instantiate | |
| 40 // an appropriate FileSystemOperation. | |
| 41 LocalFileSystemOperation( | |
| 42 FileSystemContext* file_system_context, | |
| 43 scoped_ptr<FileSystemOperationContext> operation_context); | |
| 44 | |
| 45 virtual ~LocalFileSystemOperation(); | |
| 46 | |
| 47 // FileSystemOperation overrides. | |
| 48 virtual void CreateFile(const FileSystemURL& url, | |
| 49 bool exclusive, | |
| 50 const StatusCallback& callback) OVERRIDE; | |
| 51 virtual void CreateDirectory(const FileSystemURL& url, | |
| 52 bool exclusive, | |
| 53 bool recursive, | |
| 54 const StatusCallback& callback) OVERRIDE; | |
| 55 virtual void Copy(const FileSystemURL& src_url, | |
| 56 const FileSystemURL& dest_url, | |
| 57 const StatusCallback& callback) OVERRIDE; | |
| 58 virtual void Move(const FileSystemURL& src_url, | |
| 59 const FileSystemURL& dest_url, | |
| 60 const StatusCallback& callback) OVERRIDE; | |
| 61 virtual void DirectoryExists(const FileSystemURL& url, | |
| 62 const StatusCallback& callback) OVERRIDE; | |
| 63 virtual void FileExists(const FileSystemURL& url, | |
| 64 const StatusCallback& callback) OVERRIDE; | |
| 65 virtual void GetMetadata(const FileSystemURL& url, | |
| 66 const GetMetadataCallback& callback) OVERRIDE; | |
| 67 virtual void ReadDirectory(const FileSystemURL& url, | |
| 68 const ReadDirectoryCallback& callback) OVERRIDE; | |
| 69 virtual void Remove(const FileSystemURL& url, bool recursive, | |
| 70 const StatusCallback& callback) OVERRIDE; | |
| 71 virtual void Write(const net::URLRequestContext* url_request_context, | |
| 72 const FileSystemURL& url, | |
| 73 const GURL& blob_url, | |
| 74 int64 offset, | |
| 75 const WriteCallback& callback) OVERRIDE; | |
| 76 virtual void Truncate(const FileSystemURL& url, int64 length, | |
| 77 const StatusCallback& callback) OVERRIDE; | |
| 78 virtual void TouchFile(const FileSystemURL& url, | |
| 79 const base::Time& last_access_time, | |
| 80 const base::Time& last_modified_time, | |
| 81 const StatusCallback& callback) OVERRIDE; | |
| 82 virtual void OpenFile(const FileSystemURL& url, | |
| 83 int file_flags, | |
| 84 base::ProcessHandle peer_handle, | |
| 85 const OpenFileCallback& callback) OVERRIDE; | |
| 86 virtual void Cancel(const StatusCallback& cancel_callback) OVERRIDE; | |
| 87 virtual LocalFileSystemOperation* AsLocalFileSystemOperation() OVERRIDE; | |
| 88 virtual void CreateSnapshotFile( | |
| 89 const FileSystemURL& path, | |
| 90 const SnapshotFileCallback& callback) OVERRIDE; | |
| 91 | |
| 92 // Creates a nestable operation that inherits operation context | |
| 93 // from this operation. The operation created by this method have to | |
| 94 // be die before this operation goes away. | |
| 95 virtual LocalFileSystemOperation* CreateNestedOperation(); | |
| 96 | |
| 97 // Copies in a single file from a different filesystem. | |
| 98 // | |
| 99 // This returns: | |
| 100 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_file_path| | |
| 101 // or the parent directory of |dest_url| does not exist. | |
| 102 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and | |
| 103 // is not a file. | |
| 104 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and | |
| 105 // its parent path is a file. | |
| 106 // | |
| 107 virtual void CopyInForeignFile(const base::FilePath& src_local_disk_path, | |
| 108 const FileSystemURL& dest_url, | |
| 109 const StatusCallback& callback); | |
| 110 | |
| 111 // Removes a single file. | |
| 112 // | |
| 113 // This returns: | |
| 114 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist. | |
| 115 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |url| is not a file. | |
| 116 // | |
| 117 void RemoveFile(const FileSystemURL& url, | |
| 118 const StatusCallback& callback); | |
| 119 | |
| 120 // Removes a single empty directory. | |
| 121 // | |
| 122 // This returns: | |
| 123 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist. | |
| 124 // - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory. | |
| 125 // - PLATFORM_FILE_ERROR_NOT_EMPTY if |url| is not empty. | |
| 126 // | |
| 127 void RemoveDirectory(const FileSystemURL& url, | |
| 128 const StatusCallback& callback); | |
| 129 | |
| 130 // Copies a file from |src_url| to |dest_url|. | |
| 131 // This must be called for files that belong to the same filesystem | |
| 132 // (i.e. type() and origin() of the |src_url| and |dest_url| must match). | |
| 133 // | |
| 134 // This returns: | |
| 135 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url| | |
| 136 // or the parent directory of |dest_url| does not exist. | |
| 137 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file. | |
| 138 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and | |
| 139 // is not a file. | |
| 140 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and | |
| 141 // its parent path is a file. | |
| 142 // | |
| 143 void CopyFileLocal(const FileSystemURL& src_url, | |
| 144 const FileSystemURL& dest_url, | |
| 145 const StatusCallback& callback); | |
| 146 | |
| 147 // Moves a local file from |src_url| to |dest_url|. | |
| 148 // This must be called for files that belong to the same filesystem | |
| 149 // (i.e. type() and origin() of the |src_url| and |dest_url| must match). | |
| 150 // | |
| 151 // This returns: | |
| 152 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url| | |
| 153 // or the parent directory of |dest_url| does not exist. | |
| 154 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file. | |
| 155 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and | |
| 156 // is not a file. | |
| 157 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and | |
| 158 // its parent path is a file. | |
| 159 // | |
| 160 void MoveFileLocal(const FileSystemURL& src_url, | |
| 161 const FileSystemURL& dest_url, | |
| 162 const StatusCallback& callback); | |
| 163 | |
| 164 // Synchronously gets the platform path for the given |url|. | |
| 165 void SyncGetPlatformPath(const FileSystemURL& url, | |
| 166 base::FilePath* platform_path); | |
| 167 | |
| 168 FileSystemContext* file_system_context() const { | |
| 169 return file_system_context_; | |
| 170 } | |
| 171 | |
| 172 FileSystemOperationContext* operation_context() const { | |
| 173 if (parent_operation_) | |
| 174 return parent_operation_->operation_context(); | |
| 175 return operation_context_.get(); | |
| 176 } | |
| 177 | |
| 178 private: | |
| 179 enum OperationMode { | |
| 180 OPERATION_MODE_READ, | |
| 181 OPERATION_MODE_WRITE, | |
| 182 }; | |
| 183 | |
| 184 friend class sync_file_system::SyncableFileSystemOperation; | |
| 185 | |
| 186 // Queries the quota and usage and then runs the given |task|. | |
| 187 // If an error occurs during the quota query it runs |error_callback| instead. | |
| 188 void GetUsageAndQuotaThenRunTask( | |
| 189 const FileSystemURL& url, | |
| 190 const base::Closure& task, | |
| 191 const base::Closure& error_callback); | |
| 192 | |
| 193 // Called after the quota info is obtained from the quota manager | |
| 194 // (which is triggered by GetUsageAndQuotaThenRunTask). | |
| 195 // Sets the quota info in the operation_context_ and then runs the given | |
| 196 // |task| if the returned quota status is successful, otherwise runs | |
| 197 // |error_callback|. | |
| 198 void DidGetUsageAndQuotaAndRunTask( | |
| 199 const base::Closure& task, | |
| 200 const base::Closure& error_callback, | |
| 201 quota::QuotaStatusCode status, | |
| 202 int64 usage, int64 quota); | |
| 203 | |
| 204 // returns a closure which actually perform the write operation. | |
| 205 base::Closure GetWriteClosure( | |
| 206 const net::URLRequestContext* url_request_context, | |
| 207 const FileSystemURL& url, | |
| 208 const GURL& blob_url, | |
| 209 int64 offset, | |
| 210 const WriteCallback& callback); | |
| 211 void DidFailWrite( | |
| 212 const WriteCallback& callback, | |
| 213 base::PlatformFileError result); | |
| 214 | |
| 215 // The 'body' methods that perform the actual work (i.e. posting the | |
| 216 // file task on proxy_) after the quota check. | |
| 217 void DoCreateFile(const FileSystemURL& url, | |
| 218 const StatusCallback& callback, bool exclusive); | |
| 219 void DoCreateDirectory(const FileSystemURL& url, | |
| 220 const StatusCallback& callback, | |
| 221 bool exclusive, | |
| 222 bool recursive); | |
| 223 void DoCopyFileLocal(const FileSystemURL& src, | |
| 224 const FileSystemURL& dest, | |
| 225 const StatusCallback& callback); | |
| 226 void DoMoveFileLocal(const FileSystemURL& src, | |
| 227 const FileSystemURL& dest, | |
| 228 const StatusCallback& callback); | |
| 229 void DoCopyInForeignFile(const base::FilePath& src_local_disk_file_path, | |
| 230 const FileSystemURL& dest, | |
| 231 const StatusCallback& callback); | |
| 232 void DoTruncate(const FileSystemURL& url, | |
| 233 const StatusCallback& callback, int64 length); | |
| 234 void DoOpenFile(const FileSystemURL& url, | |
| 235 const OpenFileCallback& callback, int file_flags); | |
| 236 | |
| 237 // Callback for CreateFile for |exclusive|=true cases. | |
| 238 void DidEnsureFileExistsExclusive(const StatusCallback& callback, | |
| 239 base::PlatformFileError rv, | |
| 240 bool created); | |
| 241 | |
| 242 // Callback for CreateFile for |exclusive|=false cases. | |
| 243 void DidEnsureFileExistsNonExclusive(const StatusCallback& callback, | |
| 244 base::PlatformFileError rv, | |
| 245 bool created); | |
| 246 | |
| 247 // Generic callback that translates platform errors to WebKit error codes. | |
| 248 void DidFinishFileOperation(const StatusCallback& callback, | |
| 249 base::PlatformFileError rv); | |
| 250 | |
| 251 // Generic callback when we delegated the operation. | |
| 252 void DidFinishDelegatedOperation(const StatusCallback& callback, | |
| 253 base::PlatformFileError rv); | |
| 254 | |
| 255 void DidDirectoryExists(const StatusCallback& callback, | |
| 256 base::PlatformFileError rv, | |
| 257 const base::PlatformFileInfo& file_info, | |
| 258 const base::FilePath& unused); | |
| 259 void DidFileExists(const StatusCallback& callback, | |
| 260 base::PlatformFileError rv, | |
| 261 const base::PlatformFileInfo& file_info, | |
| 262 const base::FilePath& unused); | |
| 263 void DidGetMetadata(const GetMetadataCallback& callback, | |
| 264 base::PlatformFileError rv, | |
| 265 const base::PlatformFileInfo& file_info, | |
| 266 const base::FilePath& platform_path); | |
| 267 void DidReadDirectory(const ReadDirectoryCallback& callback, | |
| 268 base::PlatformFileError rv, | |
| 269 const std::vector<DirectoryEntry>& entries, | |
| 270 bool has_more); | |
| 271 void DidWrite(const FileSystemURL& url, | |
| 272 base::PlatformFileError rv, | |
| 273 int64 bytes, | |
| 274 FileWriterDelegate::WriteProgressStatus write_status); | |
| 275 void DidTouchFile(const StatusCallback& callback, | |
| 276 base::PlatformFileError rv); | |
| 277 void DidOpenFile(const OpenFileCallback& callback, | |
| 278 base::PlatformFileError rv, | |
| 279 base::PassPlatformFile file, | |
| 280 bool created); | |
| 281 void DidCreateSnapshotFile( | |
| 282 const SnapshotFileCallback& callback, | |
| 283 base::PlatformFileError result, | |
| 284 const base::PlatformFileInfo& file_info, | |
| 285 const base::FilePath& platform_path, | |
| 286 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref); | |
| 287 | |
| 288 // Checks the validity of a given |url| and populates |file_util| for |mode|. | |
| 289 base::PlatformFileError SetUp( | |
| 290 const FileSystemURL& url, | |
| 291 OperationMode mode); | |
| 292 | |
| 293 // Used only for internal assertions. | |
| 294 // Returns false if there's another inflight pending operation. | |
| 295 bool SetPendingOperationType(OperationType type); | |
| 296 | |
| 297 scoped_refptr<FileSystemContext> file_system_context_; | |
| 298 | |
| 299 scoped_ptr<FileSystemOperationContext> operation_context_; | |
| 300 AsyncFileUtil* async_file_util_; // Not owned. | |
| 301 | |
| 302 // If this operation is created as a sub-operation for nested operation, | |
| 303 // this holds non-null value and points to the parent operation. | |
| 304 // TODO(kinuko): Cleanup this when we finish cleaning up the | |
| 305 // FileSystemOperation lifetime issue. | |
| 306 base::WeakPtr<LocalFileSystemOperation> parent_operation_; | |
| 307 | |
| 308 // These are all used only by Write(). | |
| 309 friend class FileWriterDelegate; | |
| 310 scoped_ptr<FileWriterDelegate> file_writer_delegate_; | |
| 311 | |
| 312 scoped_ptr<RecursiveOperationDelegate> recursive_operation_delegate_; | |
| 313 | |
| 314 // write_callback is kept in this class for so that we can dispatch it when | |
| 315 // the operation is cancelled. calcel_callback is kept for canceling a | |
| 316 // Truncate() operation. We can't actually stop Truncate in another thread; | |
| 317 // after it resumed from the working thread, cancellation takes place. | |
| 318 WriteCallback write_callback_; | |
| 319 StatusCallback cancel_callback_; | |
| 320 | |
| 321 // Used only by OpenFile, in order to clone the file handle back to the | |
| 322 // requesting process. | |
| 323 base::ProcessHandle peer_handle_; | |
| 324 | |
| 325 // A flag to make sure we call operation only once per instance. | |
| 326 OperationType pending_operation_; | |
| 327 | |
| 328 // We keep track of the file to be modified by this operation so that | |
| 329 // we can notify observers when we're done. | |
| 330 FileSystemURL write_target_url_; | |
| 331 | |
| 332 // LocalFileSystemOperation instance is usually deleted upon completion but | |
| 333 // could be deleted while it has inflight callbacks when Cancel is called. | |
| 334 base::WeakPtrFactory<LocalFileSystemOperation> weak_factory_; | |
| 335 | |
| 336 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperation); | |
| 337 }; | |
| 338 | |
| 339 } // namespace fileapi | |
| 340 | |
| 341 #endif // WEBKIT_FILEAPI_LOCAL_FILE_SYSTEM_OPERATION_H_ | |
| OLD | NEW |