[email protected] | d5233ef4 | 2011-03-04 04:14:04 | [diff] [blame^] | 1 | // Copyright (c) 2011 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_FILE_UTIL_PROXY_H_ |
| 6 | #define WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_PROXY_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/callback.h" |
| 11 | #include "base/file_path.h" |
| 12 | #include "base/file_util_proxy.h" |
| 13 | #include "base/platform_file.h" |
| 14 | #include "base/ref_counted.h" |
| 15 | #include "base/tracked_objects.h" |
| 16 | |
| 17 | namespace base { |
| 18 | class MessageLoopProxy; |
| 19 | class Time; |
| 20 | } |
| 21 | |
| 22 | namespace fileapi { |
| 23 | |
| 24 | class FileSystemOperationContext; |
| 25 | |
| 26 | using base::MessageLoopProxy; |
| 27 | using base::PlatformFile; |
| 28 | |
| 29 | // This class provides asynchronous access to common file routines for the |
| 30 | // FileSystem API. |
| 31 | class FileSystemFileUtilProxy { |
| 32 | public: |
| 33 | typedef base::FileUtilProxy::StatusCallback StatusCallback; |
| 34 | typedef base::FileUtilProxy::CreateOrOpenCallback CreateOrOpenCallback; |
| 35 | typedef base::FileUtilProxy::EnsureFileExistsCallback |
| 36 | EnsureFileExistsCallback; |
| 37 | typedef base::FileUtilProxy::GetFileInfoCallback GetFileInfoCallback; |
| 38 | typedef base::FileUtilProxy::ReadDirectoryCallback ReadDirectoryCallback; |
| 39 | |
| 40 | // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 41 | // for the callback. |
| 42 | // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create |
| 43 | // a new file at the given |file_path| and calls back with |
| 44 | // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
| 45 | static bool CreateOrOpen(const FileSystemOperationContext& context, |
| 46 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 47 | const FilePath& file_path, |
| 48 | int file_flags, |
| 49 | CreateOrOpenCallback* callback); |
| 50 | |
| 51 | // Close the given file handle. |
| 52 | static bool Close(const FileSystemOperationContext& context, |
| 53 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 54 | PlatformFile, |
| 55 | StatusCallback* callback); |
| 56 | |
| 57 | // Ensures that the given |file_path| exist. This creates a empty new file |
| 58 | // at |file_path| if the |file_path| does not exist. |
| 59 | // If a new file han not existed and is created at the |file_path|, |
| 60 | // |created| of the callback argument is set true and |error code| |
| 61 | // is set PLATFORM_FILE_OK. |
| 62 | // If the file already exists, |created| is set false and |error code| |
| 63 | // is set PLATFORM_FILE_OK. |
| 64 | // If the file hasn't existed but it couldn't be created for some other |
| 65 | // reasons, |created| is set false and |error code| indicates the error. |
| 66 | static bool EnsureFileExists( |
| 67 | const FileSystemOperationContext& context, |
| 68 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 69 | const FilePath& file_path, |
| 70 | EnsureFileExistsCallback* callback); |
| 71 | |
| 72 | // Retrieves the information about a file. It is invalid to pass NULL for the |
| 73 | // callback. |
| 74 | static bool GetFileInfo( |
| 75 | const FileSystemOperationContext& context, |
| 76 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 77 | const FilePath& file_path, |
| 78 | GetFileInfoCallback* callback); |
| 79 | |
| 80 | static bool ReadDirectory(const FileSystemOperationContext& context, |
| 81 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 82 | const FilePath& file_path, |
| 83 | ReadDirectoryCallback* callback); |
| 84 | |
| 85 | // Creates directory at given path. It's an error to create |
| 86 | // if |exclusive| is true and dir already exists. |
| 87 | static bool CreateDirectory( |
| 88 | const FileSystemOperationContext& context, |
| 89 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 90 | const FilePath& file_path, |
| 91 | bool exclusive, |
| 92 | StatusCallback* callback); |
| 93 | |
| 94 | // Copies a file or a directory from |src_file_path| to |dest_file_path| |
| 95 | // Error cases: |
| 96 | // If destination file doesn't exist or destination's parent |
| 97 | // doesn't exists. |
| 98 | // If source dir exists but destination path is an existing file. |
| 99 | // If source file exists but destination path is an existing directory. |
| 100 | // If source is a parent of destination. |
| 101 | // If source doesn't exists. |
| 102 | static bool Copy(const FileSystemOperationContext& context, |
| 103 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 104 | const FilePath& src_file_path, |
| 105 | const FilePath& dest_file_path, |
| 106 | StatusCallback* callback); |
| 107 | |
| 108 | // Moves a file or a directory from src_file_path to dest_file_path. |
| 109 | // Error cases are similar to Copy method's error cases. |
| 110 | static bool Move( |
| 111 | const FileSystemOperationContext& context, |
| 112 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 113 | const FilePath& src_file_path, |
| 114 | const FilePath& dest_file_path, |
| 115 | StatusCallback* callback); |
| 116 | |
| 117 | // Deletes a file or a directory. |
| 118 | // It is an error to delete a non-empty directory with recursive=false. |
| 119 | static bool Delete(const FileSystemOperationContext& context, |
| 120 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 121 | const FilePath& file_path, |
| 122 | bool recursive, |
| 123 | StatusCallback* callback); |
| 124 | |
| 125 | // Touches a file. The callback can be NULL. |
| 126 | static bool Touch( |
| 127 | const FileSystemOperationContext& context, |
| 128 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 129 | const FilePath& file_path, |
| 130 | const base::Time& last_access_time, |
| 131 | const base::Time& last_modified_time, |
| 132 | StatusCallback* callback); |
| 133 | |
| 134 | // Truncates a file to the given length. If |length| is greater than the |
| 135 | // current length of the file, the file will be extended with zeroes. |
| 136 | // The callback can be NULL. |
| 137 | static bool Truncate( |
| 138 | const FileSystemOperationContext& context, |
| 139 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 140 | const FilePath& path, |
| 141 | int64 length, |
| 142 | StatusCallback* callback); |
| 143 | |
| 144 | private: |
| 145 | DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemFileUtilProxy); |
| 146 | }; |
| 147 | |
| 148 | } // namespace fileapi |
| 149 | |
| 150 | #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_PROXY_H_ |