[email protected] | f5ae493 | 2012-01-12 01:59:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 5 | #ifndef BASE_FILE_UTIL_PROXY_H_ |
| 6 | #define BASE_FILE_UTIL_PROXY_H_ |
| 7 | |
| 8 | #include <vector> |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 9 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 12 | #include "base/file_path.h" |
| 13 | #include "base/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 15 | #include "base/platform_file.h" |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 16 | #include "base/tracked_objects.h" |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | class MessageLoopProxy; |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 21 | class Time; |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 22 | |
| 23 | // This class provides asynchronous access to common file routines. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 24 | class BASE_EXPORT FileUtilProxy { |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 25 | public: |
[email protected] | 0a3bd7e | 2011-10-19 07:21:57 | [diff] [blame] | 26 | // Holds metadata for file or directory entry. |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 27 | struct Entry { |
| 28 | FilePath::StringType name; |
| 29 | bool is_directory; |
[email protected] | f1ddaa4 | 2011-07-19 05:03:03 | [diff] [blame] | 30 | int64 size; |
| 31 | base::Time last_modified_time; |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 32 | }; |
| 33 | |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 34 | // This callback is used by methods that report only an error code. It is |
[email protected] | 01ce105 | 2011-11-08 09:04:16 | [diff] [blame] | 35 | // valid to pass a null callback to any function that takes a StatusCallback, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 36 | // in which case the operation will complete silently. |
[email protected] | 0a3bd7e | 2011-10-19 07:21:57 | [diff] [blame] | 37 | typedef Callback<void(PlatformFileError)> StatusCallback; |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 38 | |
[email protected] | 0a3bd7e | 2011-10-19 07:21:57 | [diff] [blame] | 39 | typedef Callback<void(PlatformFileError, |
| 40 | PassPlatformFile, |
| 41 | bool /* created */)> CreateOrOpenCallback; |
| 42 | typedef Callback<void(PlatformFileError, |
| 43 | PassPlatformFile, |
[email protected] | f5ae493 | 2012-01-12 01:59:47 | [diff] [blame] | 44 | const FilePath&)> CreateTemporaryCallback; |
[email protected] | 0a3bd7e | 2011-10-19 07:21:57 | [diff] [blame] | 45 | typedef Callback<void(PlatformFileError, |
| 46 | const PlatformFileInfo& |
| 47 | )> GetFileInfoCallback; |
| 48 | typedef Callback<void(PlatformFileError, |
| 49 | const char* /* data */, |
| 50 | int /* bytes read */)> ReadCallback; |
| 51 | typedef Callback<void(PlatformFileError, |
| 52 | int /* bytes written */)> WriteCallback; |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 53 | |
[email protected] | 01ce105 | 2011-11-08 09:04:16 | [diff] [blame] | 54 | typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask; |
| 55 | typedef Callback<PlatformFileError(PlatformFile)> CloseTask; |
[email protected] | 4412cd0 | 2011-11-09 06:46:39 | [diff] [blame] | 56 | typedef Callback<PlatformFileError(void)> FileTask; |
[email protected] | 01ce105 | 2011-11-08 09:04:16 | [diff] [blame] | 57 | |
[email protected] | d6c18221 | 2011-10-17 20:44:47 | [diff] [blame] | 58 | // Creates or opens a file with the given flags. It is invalid to pass a null |
| 59 | // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to |
| 60 | // create a new file at the given |file_path| and calls back with |
[email protected] | 0a3bd7e | 2011-10-19 07:21:57 | [diff] [blame] | 61 | // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 62 | static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 63 | const FilePath& file_path, |
| 64 | int file_flags, |
[email protected] | 30c1eea | 2011-10-17 18:40:30 | [diff] [blame] | 65 | const CreateOrOpenCallback& callback); |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 66 | |
[email protected] | d6c18221 | 2011-10-17 20:44:47 | [diff] [blame] | 67 | // Creates a temporary file for writing. The path and an open file handle are |
| 68 | // returned. It is invalid to pass a null callback. The additional file flags |
| 69 | // will be added on top of the default file flags which are: |
[email protected] | 86ecf51 | 2011-06-13 20:29:50 | [diff] [blame] | 70 | // base::PLATFORM_FILE_CREATE_ALWAYS |
| 71 | // base::PLATFORM_FILE_WRITE |
| 72 | // base::PLATFORM_FILE_TEMPORARY. |
| 73 | // Set |additional_file_flags| to 0 for synchronous writes and set to |
| 74 | // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 75 | static bool CreateTemporary( |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 76 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 86ecf51 | 2011-06-13 20:29:50 | [diff] [blame] | 77 | int additional_file_flags, |
[email protected] | 7bf4a8e | 2011-10-17 19:29:29 | [diff] [blame] | 78 | const CreateTemporaryCallback& callback); |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 79 | |
| 80 | // Close the given file handle. |
[email protected] | d480a3e | 2010-08-24 20:26:23 | [diff] [blame] | 81 | static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 82 | PlatformFile, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 83 | const StatusCallback& callback); |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 84 | |
[email protected] | d6c18221 | 2011-10-17 20:44:47 | [diff] [blame] | 85 | // Retrieves the information about a file. It is invalid to pass a null |
[email protected] | 8107004 | 2010-08-31 02:42:36 | [diff] [blame] | 86 | // callback. |
[email protected] | 8107004 | 2010-08-31 02:42:36 | [diff] [blame] | 87 | static bool GetFileInfo( |
| 88 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 89 | const FilePath& file_path, |
[email protected] | 0f695a7 | 2011-10-17 20:12:05 | [diff] [blame] | 90 | const GetFileInfoCallback& callback); |
[email protected] | 8107004 | 2010-08-31 02:42:36 | [diff] [blame] | 91 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 92 | static bool GetFileInfoFromPlatformFile( |
| 93 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 94 | PlatformFile file, |
[email protected] | 0f695a7 | 2011-10-17 20:12:05 | [diff] [blame] | 95 | const GetFileInfoCallback& callback); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 96 | |
[email protected] | 81b18ca | 2010-10-07 08:35:09 | [diff] [blame] | 97 | // Deletes a file or a directory. |
| 98 | // It is an error to delete a non-empty directory with recursive=false. |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 99 | static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 100 | const FilePath& file_path, |
[email protected] | 81b18ca | 2010-10-07 08:35:09 | [diff] [blame] | 101 | bool recursive, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 102 | const StatusCallback& callback); |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 103 | |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 104 | // Deletes a directory and all of its contents. |
| 105 | static bool RecursiveDelete( |
| 106 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 107 | const FilePath& file_path, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 108 | const StatusCallback& callback); |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 109 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 110 | // Reads from a file. On success, the file pointer is moved to position |
[email protected] | d6c18221 | 2011-10-17 20:44:47 | [diff] [blame] | 111 | // |offset + bytes_to_read| in the file. The callback can be null. |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 112 | static bool Read( |
| 113 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 114 | PlatformFile file, |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 115 | int64 offset, |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 116 | int bytes_to_read, |
[email protected] | d6c18221 | 2011-10-17 20:44:47 | [diff] [blame] | 117 | const ReadCallback& callback); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 118 | |
| 119 | // Writes to a file. If |offset| is greater than the length of the file, |
| 120 | // |false| is returned. On success, the file pointer is moved to position |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 121 | // |offset + bytes_to_write| in the file. The callback can be null. |
[email protected] | a88016d1 | 2011-08-17 16:45:48 | [diff] [blame] | 122 | // |bytes_to_write| must be greater than zero. |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 123 | static bool Write( |
| 124 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 125 | PlatformFile file, |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 126 | int64 offset, |
| 127 | const char* buffer, |
| 128 | int bytes_to_write, |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 129 | const WriteCallback& callback); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 130 | |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 131 | // Touches a file. The callback can be null. |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 132 | static bool Touch( |
| 133 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 134 | PlatformFile file, |
| 135 | const Time& last_access_time, |
| 136 | const Time& last_modified_time, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 137 | const StatusCallback& callback); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 138 | |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 139 | // Touches a file. The callback can be null. |
[email protected] | 9fae63af | 2010-09-24 01:09:32 | [diff] [blame] | 140 | static bool Touch( |
| 141 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 142 | const FilePath& file_path, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 143 | const Time& last_access_time, |
| 144 | const Time& last_modified_time, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 145 | const StatusCallback& callback); |
[email protected] | 9fae63af | 2010-09-24 01:09:32 | [diff] [blame] | 146 | |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 147 | // Truncates a file to the given length. If |length| is greater than the |
| 148 | // current length of the file, the file will be extended with zeroes. |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 149 | // The callback can be null. |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 150 | static bool Truncate( |
| 151 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 152 | PlatformFile file, |
[email protected] | 7eb68a2 | 2010-10-05 02:26:47 | [diff] [blame] | 153 | int64 length, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 154 | const StatusCallback& callback); |
[email protected] | 7eb68a2 | 2010-10-05 02:26:47 | [diff] [blame] | 155 | |
| 156 | // Truncates a file to the given length. If |length| is greater than the |
| 157 | // current length of the file, the file will be extended with zeroes. |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 158 | // The callback can be null. |
[email protected] | 7eb68a2 | 2010-10-05 02:26:47 | [diff] [blame] | 159 | static bool Truncate( |
| 160 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 161 | const FilePath& path, |
| 162 | int64 length, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 163 | const StatusCallback& callback); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 164 | |
[email protected] | 4958f9f | 2011-10-17 20:56:52 | [diff] [blame] | 165 | // Flushes a file. The callback can be null. |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 166 | static bool Flush( |
| 167 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
[email protected] | 841daf1d | 2010-11-02 20:36:52 | [diff] [blame] | 168 | PlatformFile file, |
[email protected] | aa0b33e | 2011-10-17 21:33:35 | [diff] [blame] | 169 | const StatusCallback& callback); |
[email protected] | 3fb43ed1 | 2010-09-10 03:01:14 | [diff] [blame] | 170 | |
[email protected] | 01ce105 | 2011-11-08 09:04:16 | [diff] [blame] | 171 | // Relay helpers. |
[email protected] | 4412cd0 | 2011-11-09 06:46:39 | [diff] [blame] | 172 | static bool RelayFileTask( |
| 173 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 174 | const tracked_objects::Location& from_here, |
| 175 | const FileTask& task, |
| 176 | const StatusCallback& callback); |
| 177 | |
[email protected] | 01ce105 | 2011-11-08 09:04:16 | [diff] [blame] | 178 | static bool RelayCreateOrOpen( |
| 179 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 180 | const CreateOrOpenTask& open_task, |
| 181 | const CloseTask& close_task, |
| 182 | const CreateOrOpenCallback& callback); |
| 183 | |
| 184 | static bool RelayClose( |
| 185 | scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 186 | const CloseTask& close_task, |
| 187 | PlatformFile, |
| 188 | const StatusCallback& callback); |
| 189 | |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 190 | private: |
| 191 | DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
| 192 | }; |
| 193 | |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 194 | } // namespace base |
[email protected] | b90c93ff | 2010-08-20 22:42:50 | [diff] [blame] | 195 | |
[email protected] | 61da1db | 2010-09-02 03:43:36 | [diff] [blame] | 196 | #endif // BASE_FILE_UTIL_PROXY_H_ |