| OLD | NEW |
| (Empty) | |
| 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.h" |
| 13 #include "base/file_util_proxy.h" |
| 14 #include "base/platform_file.h" |
| 15 #include "base/ref_counted.h" |
| 16 #include "base/tracked_objects.h" |
| 17 #include "webkit/fileapi/file_system_operation_context.h" |
| 18 |
| 19 namespace base { |
| 20 class MessageLoopProxy; |
| 21 class Time; |
| 22 } |
| 23 |
| 24 namespace fileapi { |
| 25 |
| 26 // This class provides asynchronous access to common file routines. |
| 27 class FileSystemFileUtilProxy { |
| 28 public: |
| 29 // This callback is used by methods that report only an error code. It is |
| 30 // valid to pass NULL as the callback parameter to any function that takes a |
| 31 // StatusCallback, in which case the operation will complete silently. |
| 32 typedef Callback1<base::PlatformFileError /* error code */>::Type StatusCallba
ck; |
| 33 |
| 34 typedef Callback3<base::PlatformFileError /* error code */, |
| 35 base::PassPlatformFile, |
| 36 bool /* created */>::Type CreateOrOpenCallback; |
| 37 typedef Callback3<base::PlatformFileError /* error code */, |
| 38 base::PassPlatformFile, |
| 39 FilePath>::Type CreateTemporaryCallback; |
| 40 typedef Callback2<base::PlatformFileError /* error code */, |
| 41 bool /* created */>::Type EnsureFileExistsCallback; |
| 42 typedef Callback2<base::PlatformFileError /* error code */, |
| 43 const base::PlatformFileInfo& /* file_info */ |
| 44 >::Type GetFileInfoCallback; |
| 45 typedef Callback2<base::PlatformFileError /* error code */, |
| 46 const std::vector<base::FileUtilProxy::Entry>&>::Type ReadDi
rectoryCallback; |
| 47 typedef Callback3<base::PlatformFileError /* error code */, |
| 48 const char* /* data */, |
| 49 int /* bytes read/written */>::Type ReadCallback; |
| 50 typedef Callback2<base::PlatformFileError /* error code */, |
| 51 int /* bytes written */>::Type WriteCallback; |
| 52 |
| 53 // Creates or opens a file with the given flags. It is invalid to pass NULL |
| 54 // for the callback. |
| 55 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create |
| 56 // a new file at the given |file_path| and calls back with |
| 57 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
| 58 static bool CreateOrOpen(FileSystemOperationContext* fs_context, |
| 59 scoped_refptr<base::MessageLoopProxy> message_loop_pr
oxy, |
| 60 const FilePath& file_path, |
| 61 int file_flags, |
| 62 CreateOrOpenCallback* callback); |
| 63 |
| 64 // Creates a temporary file for writing. The path and an open file handle |
| 65 // are returned. It is invalid to pass NULL for the callback. |
| 66 static bool CreateTemporary( |
| 67 FileSystemOperationContext* fs_context, |
| 68 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 69 CreateTemporaryCallback* callback); |
| 70 |
| 71 // Close the given file handle. |
| 72 static bool Close(FileSystemOperationContext* fs_context, |
| 73 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 74 base::PlatformFile, |
| 75 StatusCallback* callback); |
| 76 |
| 77 // Ensures that the given |file_path| exist. This creates a empty new file |
| 78 // at |file_path| if the |file_path| does not exist. |
| 79 // If a new file han not existed and is created at the |file_path|, |
| 80 // |created| of the callback argument is set true and |error code| |
| 81 // is set PLATFORM_FILE_OK. |
| 82 // If the file already exists, |created| is set false and |error code| |
| 83 // is set PLATFORM_FILE_OK. |
| 84 // If the file hasn't existed but it couldn't be created for some other |
| 85 // reasons, |created| is set false and |error code| indicates the error. |
| 86 static bool EnsureFileExists( |
| 87 FileSystemOperationContext* fs_context, |
| 88 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 89 const FilePath& file_path, |
| 90 EnsureFileExistsCallback* callback); |
| 91 |
| 92 // Retrieves the information about a file. It is invalid to pass NULL for the |
| 93 // callback. |
| 94 static bool GetFileInfo( |
| 95 FileSystemOperationContext* fs_context, |
| 96 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 97 const FilePath& file_path, |
| 98 GetFileInfoCallback* callback); |
| 99 |
| 100 static bool GetFileInfoFromPlatformFile( |
| 101 FileSystemOperationContext* fs_context, |
| 102 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 103 base::PlatformFile file, |
| 104 GetFileInfoCallback* callback); |
| 105 |
| 106 static bool ReadDirectory(FileSystemOperationContext* fs_context, |
| 107 scoped_refptr<base::MessageLoopProxy> message_loop_p
roxy, |
| 108 const FilePath& file_path, |
| 109 ReadDirectoryCallback* callback); |
| 110 |
| 111 // Creates directory at given path. It's an error to create |
| 112 // if |exclusive| is true and dir already exists. |
| 113 static bool CreateDirectory( |
| 114 FileSystemOperationContext* fs_context, |
| 115 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 116 const FilePath& file_path, |
| 117 bool exclusive, |
| 118 bool recursive, |
| 119 StatusCallback* callback); |
| 120 |
| 121 // Copies a file or a directory from |src_file_path| to |dest_file_path| |
| 122 // Error cases: |
| 123 // If destination file doesn't exist or destination's parent |
| 124 // doesn't exists. |
| 125 // If source dir exists but destination path is an existing file. |
| 126 // If source file exists but destination path is an existing directory. |
| 127 // If source is a parent of destination. |
| 128 // If source doesn't exists. |
| 129 static bool Copy(FileSystemOperationContext* fs_context, |
| 130 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 131 const FilePath& src_file_path, |
| 132 const FilePath& dest_file_path, |
| 133 StatusCallback* callback); |
| 134 |
| 135 // Moves a file or a directory from src_file_path to dest_file_path. |
| 136 // Error cases are similar to Copy method's error cases. |
| 137 static bool Move( |
| 138 FileSystemOperationContext* fs_context, |
| 139 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 140 const FilePath& src_file_path, |
| 141 const FilePath& dest_file_path, |
| 142 StatusCallback* callback); |
| 143 |
| 144 // Deletes a file or a directory. |
| 145 // It is an error to delete a non-empty directory with recursive=false. |
| 146 static bool Delete(FileSystemOperationContext* fs_context, |
| 147 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 148 const FilePath& file_path, |
| 149 bool recursive, |
| 150 StatusCallback* callback); |
| 151 |
| 152 // Deletes a directory and all of its contents. |
| 153 static bool RecursiveDelete( |
| 154 FileSystemOperationContext* fs_context, |
| 155 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 156 const FilePath& file_path, |
| 157 StatusCallback* callback); |
| 158 |
| 159 // Reads from a file. On success, the file pointer is moved to position |
| 160 // |offset + bytes_to_read| in the file. The callback can be NULL. |
| 161 static bool Read( |
| 162 FileSystemOperationContext* fs_context, |
| 163 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 164 base::PlatformFile file, |
| 165 int64 offset, |
| 166 int bytes_to_read, |
| 167 ReadCallback* callback); |
| 168 |
| 169 // Writes to a file. If |offset| is greater than the length of the file, |
| 170 // |false| is returned. On success, the file pointer is moved to position |
| 171 // |offset + bytes_to_write| in the file. The callback can be NULL. |
| 172 static bool Write( |
| 173 FileSystemOperationContext* fs_context, |
| 174 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 175 base::PlatformFile file, |
| 176 int64 offset, |
| 177 const char* buffer, |
| 178 int bytes_to_write, |
| 179 WriteCallback* callback); |
| 180 |
| 181 // Touches a file. The callback can be NULL. |
| 182 static bool Touch( |
| 183 FileSystemOperationContext* fs_context, |
| 184 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 185 base::PlatformFile file, |
| 186 const base::Time& last_access_time, |
| 187 const base::Time& last_modified_time, |
| 188 StatusCallback* callback); |
| 189 |
| 190 // Touches a file. The callback can be NULL. |
| 191 static bool Touch( |
| 192 FileSystemOperationContext* fs_context, |
| 193 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 194 const FilePath& file_path, |
| 195 const base::Time& last_access_time, |
| 196 const base::Time& last_modified_time, |
| 197 StatusCallback* callback); |
| 198 |
| 199 // Truncates a file to the given length. If |length| is greater than the |
| 200 // current length of the file, the file will be extended with zeroes. |
| 201 // The callback can be NULL. |
| 202 static bool Truncate( |
| 203 FileSystemOperationContext* fs_context, |
| 204 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 205 base::PlatformFile file, |
| 206 int64 length, |
| 207 StatusCallback* callback); |
| 208 |
| 209 // Truncates a file to the given length. If |length| is greater than the |
| 210 // current length of the file, the file will be extended with zeroes. |
| 211 // The callback can be NULL. |
| 212 static bool Truncate( |
| 213 FileSystemOperationContext* fs_context, |
| 214 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 215 const FilePath& path, |
| 216 int64 length, |
| 217 StatusCallback* callback); |
| 218 |
| 219 // Flushes a file. The callback can be NULL. |
| 220 static bool Flush( |
| 221 FileSystemOperationContext* fs_context, |
| 222 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 223 base::PlatformFile file, |
| 224 StatusCallback* callback); |
| 225 |
| 226 private: |
| 227 DISALLOW_IMPLICIT_CONSTRUCTORS(FileSystemFileUtilProxy); |
| 228 }; |
| 229 |
| 230 } // namespace fileapi |
| 231 |
| 232 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_FILE_UTIL_PROXY_H_ |
| OLD | NEW |