Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / base / files / file_util_proxy.h
blobd15535577e5a76e7a68022e3ea027df5966bc438
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
3 // found in the LICENSE file.
5 #ifndef BASE_FILES_FILE_UTIL_PROXY_H_
6 #define BASE_FILES_FILE_UTIL_PROXY_H_
8 #include "base/base_export.h"
9 #include "base/callback_forward.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/platform_file.h"
15 namespace tracked_objects {
16 class Location;
19 namespace base {
21 class TaskRunner;
22 class Time;
24 // This class provides asynchronous access to common file routines.
25 class BASE_EXPORT FileUtilProxy {
26 public:
27 // Holds metadata for file or directory entry.
28 struct Entry {
29 FilePath::StringType name;
30 bool is_directory;
31 int64 size;
32 base::Time last_modified_time;
35 // This callback is used by methods that report only an error code. It is
36 // valid to pass a null callback to any function that takes a StatusCallback,
37 // in which case the operation will complete silently.
38 typedef Callback<void(PlatformFileError)> StatusCallback;
40 typedef Callback<void(PlatformFileError,
41 PassPlatformFile,
42 bool /* created */)> CreateOrOpenCallback;
43 typedef Callback<void(PlatformFileError,
44 PassPlatformFile,
45 const FilePath&)> CreateTemporaryCallback;
46 typedef Callback<void(PlatformFileError,
47 const PlatformFileInfo&)> 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;
54 typedef Callback<PlatformFileError(PlatformFile*, bool*)> CreateOrOpenTask;
55 typedef Callback<PlatformFileError(PlatformFile)> CloseTask;
56 typedef Callback<PlatformFileError(void)> FileTask;
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
61 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
63 // This returns false if task posting to |task_runner| has failed.
64 static bool CreateOrOpen(TaskRunner* task_runner,
65 const FilePath& file_path,
66 int file_flags,
67 const CreateOrOpenCallback& callback);
69 // Creates a temporary file for writing. The path and an open file handle are
70 // returned. It is invalid to pass a null callback. The additional file flags
71 // will be added on top of the default file flags which are:
72 // base::PLATFORM_FILE_CREATE_ALWAYS
73 // base::PLATFORM_FILE_WRITE
74 // base::PLATFORM_FILE_TEMPORARY.
75 // Set |additional_file_flags| to 0 for synchronous writes and set to
76 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
78 // This returns false if task posting to |task_runner| has failed.
79 static bool CreateTemporary(
80 TaskRunner* task_runner,
81 int additional_file_flags,
82 const CreateTemporaryCallback& callback);
84 // Close the given file handle.
85 // This returns false if task posting to |task_runner| has failed.
86 static bool Close(TaskRunner* task_runner,
87 PlatformFile,
88 const StatusCallback& callback);
90 // Retrieves the information about a file. It is invalid to pass a null
91 // callback.
92 // This returns false if task posting to |task_runner| has failed.
93 static bool GetFileInfo(
94 TaskRunner* task_runner,
95 const FilePath& file_path,
96 const GetFileInfoCallback& callback);
98 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath.
99 // This returns false if task posting to |task_runner| has failed.
100 static bool GetFileInfoFromPlatformFile(
101 TaskRunner* task_runner,
102 PlatformFile file,
103 const GetFileInfoCallback& callback);
105 // Deletes a file or a directory.
106 // It is an error to delete a non-empty directory with recursive=false.
107 // This returns false if task posting to |task_runner| has failed.
108 static bool Delete(TaskRunner* task_runner,
109 const FilePath& file_path,
110 bool recursive,
111 const StatusCallback& callback);
113 // Deletes a directory and all of its contents.
114 // This returns false if task posting to |task_runner| has failed.
115 static bool RecursiveDelete(
116 TaskRunner* task_runner,
117 const FilePath& file_path,
118 const StatusCallback& callback);
120 // Reads from a file. On success, the file pointer is moved to position
121 // |offset + bytes_to_read| in the file. The callback can be null.
123 // This returns false if |bytes_to_read| is less than zero, or
124 // if task posting to |task_runner| has failed.
125 static bool Read(
126 TaskRunner* task_runner,
127 PlatformFile file,
128 int64 offset,
129 int bytes_to_read,
130 const ReadCallback& callback);
132 // Writes to a file. If |offset| is greater than the length of the file,
133 // |false| is returned. On success, the file pointer is moved to position
134 // |offset + bytes_to_write| in the file. The callback can be null.
135 // |bytes_to_write| must be greater than zero.
137 // This returns false if |bytes_to_write| is less than or equal to zero,
138 // if |buffer| is NULL, or if task posting to |task_runner| has failed.
139 static bool Write(
140 TaskRunner* task_runner,
141 PlatformFile file,
142 int64 offset,
143 const char* buffer,
144 int bytes_to_write,
145 const WriteCallback& callback);
147 // Touches a file. The callback can be null.
148 // This returns false if task posting to |task_runner| has failed.
149 static bool Touch(
150 TaskRunner* task_runner,
151 PlatformFile file,
152 const Time& last_access_time,
153 const Time& last_modified_time,
154 const StatusCallback& callback);
156 // Touches a file. The callback can be null.
157 // This returns false if task posting to |task_runner| has failed.
158 static bool Touch(
159 TaskRunner* task_runner,
160 const FilePath& file_path,
161 const Time& last_access_time,
162 const Time& last_modified_time,
163 const StatusCallback& callback);
165 // Truncates a file to the given length. If |length| is greater than the
166 // current length of the file, the file will be extended with zeroes.
167 // The callback can be null.
168 // This returns false if task posting to |task_runner| has failed.
169 static bool Truncate(
170 TaskRunner* task_runner,
171 PlatformFile file,
172 int64 length,
173 const StatusCallback& callback);
175 // Truncates a file to the given length. If |length| is greater than the
176 // current length of the file, the file will be extended with zeroes.
177 // The callback can be null.
178 // This returns false if task posting to |task_runner| has failed.
179 static bool Truncate(
180 TaskRunner* task_runner,
181 const FilePath& path,
182 int64 length,
183 const StatusCallback& callback);
185 // Flushes a file. The callback can be null.
186 // This returns false if task posting to |task_runner| has failed.
187 static bool Flush(
188 TaskRunner* task_runner,
189 PlatformFile file,
190 const StatusCallback& callback);
192 // Relay helpers.
193 // They return false if posting a given task to |task_runner| has failed.
194 static bool RelayCreateOrOpen(
195 TaskRunner* task_runner,
196 const CreateOrOpenTask& open_task,
197 const CloseTask& close_task,
198 const CreateOrOpenCallback& callback);
199 static bool RelayClose(
200 TaskRunner* task_runner,
201 const CloseTask& close_task,
202 PlatformFile,
203 const StatusCallback& callback);
205 private:
206 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
209 } // namespace base
211 #endif // BASE_FILES_FILE_UTIL_PROXY_H_