| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_FILE_UTIL_PROXY_H_ | 5 #ifndef BASE_FILE_UTIL_PROXY_H_ |
| 6 #define BASE_FILE_UTIL_PROXY_H_ | 6 #define BASE_FILE_UTIL_PROXY_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 16 #include "base/tracked_objects.h" | 16 #include "base/tracked_objects.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 class MessageLoopProxy; | 20 class TaskRunner; |
| 21 class Time; | 21 class Time; |
| 22 | 22 |
| 23 // This class provides asynchronous access to common file routines. | 23 // This class provides asynchronous access to common file routines. |
| 24 class BASE_EXPORT FileUtilProxy { | 24 class BASE_EXPORT FileUtilProxy { |
| 25 public: | 25 public: |
| 26 // Holds metadata for file or directory entry. | 26 // Holds metadata for file or directory entry. |
| 27 struct Entry { | 27 struct Entry { |
| 28 FilePath::StringType name; | 28 FilePath::StringType name; |
| 29 bool is_directory; | 29 bool is_directory; |
| 30 int64 size; | 30 int64 size; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 52 int /* bytes written */)> WriteCallback; | 52 int /* bytes written */)> WriteCallback; |
| 53 | 53 |
| 54 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask; | 54 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask; |
| 55 typedef Callback<PlatformFileError(PlatformFile)> CloseTask; | 55 typedef Callback<PlatformFileError(PlatformFile)> CloseTask; |
| 56 typedef Callback<PlatformFileError(void)> FileTask; | 56 typedef Callback<PlatformFileError(void)> FileTask; |
| 57 | 57 |
| 58 // Creates or opens a file with the given flags. It is invalid to pass a null | 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 | 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 | 60 // create a new file at the given |file_path| and calls back with |
| 61 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | 61 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. |
| 62 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 62 static bool CreateOrOpen(TaskRunner* task_runner, |
| 63 const FilePath& file_path, | 63 const FilePath& file_path, |
| 64 int file_flags, | 64 int file_flags, |
| 65 const CreateOrOpenCallback& callback); | 65 const CreateOrOpenCallback& callback); |
| 66 | 66 |
| 67 // Creates a temporary file for writing. The path and an open file handle are | 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 | 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: | 69 // will be added on top of the default file flags which are: |
| 70 // base::PLATFORM_FILE_CREATE_ALWAYS | 70 // base::PLATFORM_FILE_CREATE_ALWAYS |
| 71 // base::PLATFORM_FILE_WRITE | 71 // base::PLATFORM_FILE_WRITE |
| 72 // base::PLATFORM_FILE_TEMPORARY. | 72 // base::PLATFORM_FILE_TEMPORARY. |
| 73 // Set |additional_file_flags| to 0 for synchronous writes and set to | 73 // Set |additional_file_flags| to 0 for synchronous writes and set to |
| 74 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. | 74 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. |
| 75 static bool CreateTemporary( | 75 static bool CreateTemporary( |
| 76 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 76 TaskRunner* task_runner, |
| 77 int additional_file_flags, | 77 int additional_file_flags, |
| 78 const CreateTemporaryCallback& callback); | 78 const CreateTemporaryCallback& callback); |
| 79 | 79 |
| 80 // Close the given file handle. | 80 // Close the given file handle. |
| 81 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 81 static bool Close(TaskRunner* task_runner, |
| 82 PlatformFile, | 82 PlatformFile, |
| 83 const StatusCallback& callback); | 83 const StatusCallback& callback); |
| 84 | 84 |
| 85 // Retrieves the information about a file. It is invalid to pass a null | 85 // Retrieves the information about a file. It is invalid to pass a null |
| 86 // callback. | 86 // callback. |
| 87 static bool GetFileInfo( | 87 static bool GetFileInfo( |
| 88 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 88 TaskRunner* task_runner, |
| 89 const FilePath& file_path, | 89 const FilePath& file_path, |
| 90 const GetFileInfoCallback& callback); | 90 const GetFileInfoCallback& callback); |
| 91 | 91 |
| 92 static bool GetFileInfoFromPlatformFile( | 92 static bool GetFileInfoFromPlatformFile( |
| 93 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 93 TaskRunner* task_runner, |
| 94 PlatformFile file, | 94 PlatformFile file, |
| 95 const GetFileInfoCallback& callback); | 95 const GetFileInfoCallback& callback); |
| 96 | 96 |
| 97 // Deletes a file or a directory. | 97 // Deletes a file or a directory. |
| 98 // It is an error to delete a non-empty directory with recursive=false. | 98 // It is an error to delete a non-empty directory with recursive=false. |
| 99 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, | 99 static bool Delete(TaskRunner* task_runner, |
| 100 const FilePath& file_path, | 100 const FilePath& file_path, |
| 101 bool recursive, | 101 bool recursive, |
| 102 const StatusCallback& callback); | 102 const StatusCallback& callback); |
| 103 | 103 |
| 104 // Deletes a directory and all of its contents. | 104 // Deletes a directory and all of its contents. |
| 105 static bool RecursiveDelete( | 105 static bool RecursiveDelete( |
| 106 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 106 TaskRunner* task_runner, |
| 107 const FilePath& file_path, | 107 const FilePath& file_path, |
| 108 const StatusCallback& callback); | 108 const StatusCallback& callback); |
| 109 | 109 |
| 110 // Reads from a file. On success, the file pointer is moved to position | 110 // Reads from a file. On success, the file pointer is moved to position |
| 111 // |offset + bytes_to_read| in the file. The callback can be null. | 111 // |offset + bytes_to_read| in the file. The callback can be null. |
| 112 static bool Read( | 112 static bool Read( |
| 113 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 113 TaskRunner* task_runner, |
| 114 PlatformFile file, | 114 PlatformFile file, |
| 115 int64 offset, | 115 int64 offset, |
| 116 int bytes_to_read, | 116 int bytes_to_read, |
| 117 const ReadCallback& callback); | 117 const ReadCallback& callback); |
| 118 | 118 |
| 119 // Writes to a file. If |offset| is greater than the length of the file, | 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 | 120 // |false| is returned. On success, the file pointer is moved to position |
| 121 // |offset + bytes_to_write| in the file. The callback can be null. | 121 // |offset + bytes_to_write| in the file. The callback can be null. |
| 122 // |bytes_to_write| must be greater than zero. | 122 // |bytes_to_write| must be greater than zero. |
| 123 static bool Write( | 123 static bool Write( |
| 124 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 124 TaskRunner* task_runner, |
| 125 PlatformFile file, | 125 PlatformFile file, |
| 126 int64 offset, | 126 int64 offset, |
| 127 const char* buffer, | 127 const char* buffer, |
| 128 int bytes_to_write, | 128 int bytes_to_write, |
| 129 const WriteCallback& callback); | 129 const WriteCallback& callback); |
| 130 | 130 |
| 131 // Touches a file. The callback can be null. | 131 // Touches a file. The callback can be null. |
| 132 static bool Touch( | 132 static bool Touch( |
| 133 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 133 TaskRunner* task_runner, |
| 134 PlatformFile file, | 134 PlatformFile file, |
| 135 const Time& last_access_time, | 135 const Time& last_access_time, |
| 136 const Time& last_modified_time, | 136 const Time& last_modified_time, |
| 137 const StatusCallback& callback); | 137 const StatusCallback& callback); |
| 138 | 138 |
| 139 // Touches a file. The callback can be null. | 139 // Touches a file. The callback can be null. |
| 140 static bool Touch( | 140 static bool Touch( |
| 141 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 141 TaskRunner* task_runner, |
| 142 const FilePath& file_path, | 142 const FilePath& file_path, |
| 143 const Time& last_access_time, | 143 const Time& last_access_time, |
| 144 const Time& last_modified_time, | 144 const Time& last_modified_time, |
| 145 const StatusCallback& callback); | 145 const StatusCallback& callback); |
| 146 | 146 |
| 147 // Truncates a file to the given length. If |length| is greater than the | 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. | 148 // current length of the file, the file will be extended with zeroes. |
| 149 // The callback can be null. | 149 // The callback can be null. |
| 150 static bool Truncate( | 150 static bool Truncate( |
| 151 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 151 TaskRunner* task_runner, |
| 152 PlatformFile file, | 152 PlatformFile file, |
| 153 int64 length, | 153 int64 length, |
| 154 const StatusCallback& callback); | 154 const StatusCallback& callback); |
| 155 | 155 |
| 156 // Truncates a file to the given length. If |length| is greater than the | 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. | 157 // current length of the file, the file will be extended with zeroes. |
| 158 // The callback can be null. | 158 // The callback can be null. |
| 159 static bool Truncate( | 159 static bool Truncate( |
| 160 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 160 TaskRunner* task_runner, |
| 161 const FilePath& path, | 161 const FilePath& path, |
| 162 int64 length, | 162 int64 length, |
| 163 const StatusCallback& callback); | 163 const StatusCallback& callback); |
| 164 | 164 |
| 165 // Flushes a file. The callback can be null. | 165 // Flushes a file. The callback can be null. |
| 166 static bool Flush( | 166 static bool Flush( |
| 167 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 167 TaskRunner* task_runner, |
| 168 PlatformFile file, | 168 PlatformFile file, |
| 169 const StatusCallback& callback); | 169 const StatusCallback& callback); |
| 170 | 170 |
| 171 // Relay helpers. | 171 // Relay helpers. |
| 172 static bool RelayFileTask( | 172 static bool RelayFileTask( |
| 173 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 173 TaskRunner* task_runner, |
| 174 const tracked_objects::Location& from_here, | 174 const tracked_objects::Location& from_here, |
| 175 const FileTask& task, | 175 const FileTask& task, |
| 176 const StatusCallback& callback); | 176 const StatusCallback& callback); |
| 177 | 177 |
| 178 static bool RelayCreateOrOpen( | 178 static bool RelayCreateOrOpen( |
| 179 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 179 TaskRunner* task_runner, |
| 180 const CreateOrOpenTask& open_task, | 180 const CreateOrOpenTask& open_task, |
| 181 const CloseTask& close_task, | 181 const CloseTask& close_task, |
| 182 const CreateOrOpenCallback& callback); | 182 const CreateOrOpenCallback& callback); |
| 183 | 183 |
| 184 static bool RelayClose( | 184 static bool RelayClose( |
| 185 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 185 TaskRunner* task_runner, |
| 186 const CloseTask& close_task, | 186 const CloseTask& close_task, |
| 187 PlatformFile, | 187 PlatformFile, |
| 188 const StatusCallback& callback); | 188 const StatusCallback& callback); |
| 189 | 189 |
| 190 private: | 190 private: |
| 191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); | 191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 } // namespace base | 194 } // namespace base |
| 195 | 195 |
| 196 #endif // BASE_FILE_UTIL_PROXY_H_ | 196 #endif // BASE_FILE_UTIL_PROXY_H_ |
| OLD | NEW |