Revert 271602 "Implementation of leveldb-backed PrefStore."
[chromium-blink-merge.git] / base / files / file_util_proxy.h
blob846e8fb87800ec846a5c6e9b7f831d5e48a9bfc5
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/files/file.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 // 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,
29 // in which case the operation will complete silently.
30 typedef Callback<void(File::Error)> StatusCallback;
32 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;
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;
46 typedef Callback<File::Error(PlatformFile*, bool*)> CreateOrOpenTask;
47 typedef Callback<File::Error(PlatformFile)> CloseTask;
48 typedef Callback<File::Error(void)> FileTask;
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.
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);
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.
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);
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);
82 // Retrieves the information about a file. It is invalid to pass a null
83 // callback.
84 // This returns false if task posting to |task_runner| has failed.
85 static bool GetFileInfo(
86 TaskRunner* task_runner,
87 const FilePath& file_path,
88 const GetFileInfoCallback& callback);
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);
97 // Deletes a file or a directory.
98 // 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.
100 static bool DeleteFile(TaskRunner* task_runner,
101 const FilePath& file_path,
102 bool recursive,
103 const StatusCallback& callback);
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.
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);
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.
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);
132 // Touches a file. The callback can be null.
133 // This returns false if task posting to |task_runner| has failed.
134 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);
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,
145 const FilePath& file_path,
146 const Time& last_access_time,
147 const Time& last_modified_time,
148 const StatusCallback& callback);
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);
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);
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);
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);
190 private:
191 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
194 } // namespace base
196 #endif // BASE_FILES_FILE_UTIL_PROXY_H_