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_FILES_FILE_UTIL_PROXY_H_ | 5 #ifndef BASE_FILES_FILE_UTIL_PROXY_H_ |
6 #define BASE_FILES_FILE_UTIL_PROXY_H_ | 6 #define BASE_FILES_FILE_UTIL_PROXY_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/platform_file.h" | |
14 | |
15 namespace tracked_objects { | |
16 class Location; | |
17 }; | |
18 | 12 |
19 namespace base { | 13 namespace base { |
20 | 14 |
21 class TaskRunner; | 15 class TaskRunner; |
22 class Time; | 16 class Time; |
23 | 17 |
24 // This class provides asynchronous access to common file routines. | 18 // This class provides asynchronous access to common file routines. |
25 class BASE_EXPORT FileUtilProxy { | 19 class BASE_EXPORT FileUtilProxy { |
26 public: | 20 public: |
27 // This callback is used by methods that report only an error code. It is | 21 // This callback is used by methods that report only an error code. It is |
28 // valid to pass a null callback to any function that takes a StatusCallback, | 22 // valid to pass a null callback to any function that takes a StatusCallback, |
29 // in which case the operation will complete silently. | 23 // in which case the operation will complete silently. |
30 typedef Callback<void(File::Error)> StatusCallback; | 24 typedef Callback<void(File::Error)> StatusCallback; |
31 | 25 |
32 typedef Callback<void(File::Error, | 26 typedef Callback<void(File::Error, |
33 PassPlatformFile, | |
34 bool /* created */)> CreateOrOpenCallback; | |
35 typedef Callback<void(File::Error, | |
36 PassPlatformFile, | |
37 const FilePath&)> CreateTemporaryCallback; | |
38 typedef Callback<void(File::Error, | |
39 const File::Info&)> GetFileInfoCallback; | 27 const File::Info&)> GetFileInfoCallback; |
40 typedef Callback<void(File::Error, | |
41 const char* /* data */, | |
42 int /* bytes read */)> ReadCallback; | |
43 typedef Callback<void(File::Error, | |
44 int /* bytes written */)> WriteCallback; | |
45 | |
46 typedef Callback<File::Error(PlatformFile*, bool*)> CreateOrOpenTask; | |
47 typedef Callback<File::Error(PlatformFile)> CloseTask; | |
48 typedef Callback<File::Error(void)> FileTask; | |
49 | |
50 // Creates or opens a file with the given flags. It is invalid to pass a null | |
51 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to | |
52 // create a new file at the given |file_path| and calls back with | |
53 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists. | |
54 // | |
55 // This returns false if task posting to |task_runner| has failed. | |
56 static bool CreateOrOpen(TaskRunner* task_runner, | |
57 const FilePath& file_path, | |
58 int file_flags, | |
59 const CreateOrOpenCallback& callback); | |
60 | |
61 // Creates a temporary file for writing. The path and an open file handle are | |
62 // returned. It is invalid to pass a null callback. The additional file flags | |
63 // will be added on top of the default file flags which are: | |
64 // base::PLATFORM_FILE_CREATE_ALWAYS | |
65 // base::PLATFORM_FILE_WRITE | |
66 // base::PLATFORM_FILE_TEMPORARY. | |
67 // Set |additional_file_flags| to 0 for synchronous writes and set to | |
68 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations. | |
69 // | |
70 // This returns false if task posting to |task_runner| has failed. | |
71 static bool CreateTemporary( | |
72 TaskRunner* task_runner, | |
73 int additional_file_flags, | |
74 const CreateTemporaryCallback& callback); | |
75 | |
76 // Close the given file handle. | |
77 // This returns false if task posting to |task_runner| has failed. | |
78 static bool Close(TaskRunner* task_runner, | |
79 PlatformFile, | |
80 const StatusCallback& callback); | |
81 | 28 |
82 // Retrieves the information about a file. It is invalid to pass a null | 29 // Retrieves the information about a file. It is invalid to pass a null |
83 // callback. | 30 // callback. |
84 // This returns false if task posting to |task_runner| has failed. | 31 // This returns false if task posting to |task_runner| has failed. |
85 static bool GetFileInfo( | 32 static bool GetFileInfo( |
86 TaskRunner* task_runner, | 33 TaskRunner* task_runner, |
87 const FilePath& file_path, | 34 const FilePath& file_path, |
88 const GetFileInfoCallback& callback); | 35 const GetFileInfoCallback& callback); |
89 | 36 |
90 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath. | |
91 // This returns false if task posting to |task_runner| has failed. | |
92 static bool GetFileInfoFromPlatformFile( | |
93 TaskRunner* task_runner, | |
94 PlatformFile file, | |
95 const GetFileInfoCallback& callback); | |
96 | |
97 // Deletes a file or a directory. | 37 // Deletes a file or a directory. |
98 // It is an error to delete a non-empty directory with recursive=false. | 38 // It is an error to delete a non-empty directory with recursive=false. |
99 // This returns false if task posting to |task_runner| has failed. | 39 // This returns false if task posting to |task_runner| has failed. |
100 static bool DeleteFile(TaskRunner* task_runner, | 40 static bool DeleteFile(TaskRunner* task_runner, |
101 const FilePath& file_path, | 41 const FilePath& file_path, |
102 bool recursive, | 42 bool recursive, |
103 const StatusCallback& callback); | 43 const StatusCallback& callback); |
104 | 44 |
105 // Reads from a file. On success, the file pointer is moved to position | |
106 // |offset + bytes_to_read| in the file. The callback can be null. | |
107 // | |
108 // This returns false if |bytes_to_read| is less than zero, or | |
109 // if task posting to |task_runner| has failed. | |
110 static bool Read( | |
111 TaskRunner* task_runner, | |
112 PlatformFile file, | |
113 int64 offset, | |
114 int bytes_to_read, | |
115 const ReadCallback& callback); | |
116 | |
117 // Writes to a file. If |offset| is greater than the length of the file, | |
118 // |false| is returned. On success, the file pointer is moved to position | |
119 // |offset + bytes_to_write| in the file. The callback can be null. | |
120 // |bytes_to_write| must be greater than zero. | |
121 // | |
122 // This returns false if |bytes_to_write| is less than or equal to zero, | |
123 // if |buffer| is NULL, or if task posting to |task_runner| has failed. | |
124 static bool Write( | |
125 TaskRunner* task_runner, | |
126 PlatformFile file, | |
127 int64 offset, | |
128 const char* buffer, | |
129 int bytes_to_write, | |
130 const WriteCallback& callback); | |
131 | |
132 // Touches a file. The callback can be null. | 45 // Touches a file. The callback can be null. |
133 // This returns false if task posting to |task_runner| has failed. | 46 // This returns false if task posting to |task_runner| has failed. |
134 static bool Touch( | 47 static bool Touch( |
135 TaskRunner* task_runner, | |
136 PlatformFile file, | |
137 const Time& last_access_time, | |
138 const Time& last_modified_time, | |
139 const StatusCallback& callback); | |
140 | |
141 // Touches a file. The callback can be null. | |
142 // This returns false if task posting to |task_runner| has failed. | |
143 static bool Touch( | |
144 TaskRunner* task_runner, | 48 TaskRunner* task_runner, |
145 const FilePath& file_path, | 49 const FilePath& file_path, |
146 const Time& last_access_time, | 50 const Time& last_access_time, |
147 const Time& last_modified_time, | 51 const Time& last_modified_time, |
148 const StatusCallback& callback); | 52 const StatusCallback& callback); |
149 | 53 |
150 // Truncates a file to the given length. If |length| is greater than the | |
151 // current length of the file, the file will be extended with zeroes. | |
152 // The callback can be null. | |
153 // This returns false if task posting to |task_runner| has failed. | |
154 static bool Truncate( | |
155 TaskRunner* task_runner, | |
156 PlatformFile file, | |
157 int64 length, | |
158 const StatusCallback& callback); | |
159 | |
160 // Truncates a file to the given length. If |length| is greater than the | |
161 // current length of the file, the file will be extended with zeroes. | |
162 // The callback can be null. | |
163 // This returns false if task posting to |task_runner| has failed. | |
164 static bool Truncate( | |
165 TaskRunner* task_runner, | |
166 const FilePath& path, | |
167 int64 length, | |
168 const StatusCallback& callback); | |
169 | |
170 // Flushes a file. The callback can be null. | |
171 // This returns false if task posting to |task_runner| has failed. | |
172 static bool Flush( | |
173 TaskRunner* task_runner, | |
174 PlatformFile file, | |
175 const StatusCallback& callback); | |
176 | |
177 // Relay helpers. | |
178 // They return false if posting a given task to |task_runner| has failed. | |
179 static bool RelayCreateOrOpen( | |
180 TaskRunner* task_runner, | |
181 const CreateOrOpenTask& open_task, | |
182 const CloseTask& close_task, | |
183 const CreateOrOpenCallback& callback); | |
184 static bool RelayClose( | |
185 TaskRunner* task_runner, | |
186 const CloseTask& close_task, | |
187 PlatformFile, | |
188 const StatusCallback& callback); | |
189 | |
190 private: | 54 private: |
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); | 55 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); |
192 }; | 56 }; |
193 | 57 |
194 } // namespace base | 58 } // namespace base |
195 | 59 |
196 #endif // BASE_FILES_FILE_UTIL_PROXY_H_ | 60 #endif // BASE_FILES_FILE_UTIL_PROXY_H_ |
OLD | NEW |