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_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/platform_file.h"
14 namespace tracked_objects
{
23 // This class provides asynchronous access to common file routines.
24 class BASE_EXPORT FileUtilProxy
{
26 // This callback is used by methods that report only an error code. It is
27 // valid to pass a null callback to any function that takes a StatusCallback,
28 // in which case the operation will complete silently.
29 typedef Callback
<void(PlatformFileError
)> StatusCallback
;
31 typedef Callback
<void(PlatformFileError
,
33 bool /* created */)> CreateOrOpenCallback
;
34 typedef Callback
<void(PlatformFileError
,
36 const FilePath
&)> CreateTemporaryCallback
;
37 typedef Callback
<void(PlatformFileError
,
38 const PlatformFileInfo
&)> GetFileInfoCallback
;
39 typedef Callback
<void(PlatformFileError
,
40 const char* /* data */,
41 int /* bytes read */)> ReadCallback
;
42 typedef Callback
<void(PlatformFileError
,
43 int /* bytes written */)> WriteCallback
;
45 typedef Callback
<PlatformFileError(PlatformFile
*, bool*)> CreateOrOpenTask
;
46 typedef Callback
<PlatformFileError(PlatformFile
)> CloseTask
;
47 typedef Callback
<PlatformFileError(void)> FileTask
;
49 // Creates or opens a file with the given flags. It is invalid to pass a null
50 // callback. If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to
51 // create a new file at the given |file_path| and calls back with
52 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
54 // This returns false if task posting to |task_runner| has failed.
55 static bool CreateOrOpen(TaskRunner
* task_runner
,
56 const FilePath
& file_path
,
58 const CreateOrOpenCallback
& callback
);
60 // Creates a temporary file for writing. The path and an open file handle are
61 // returned. It is invalid to pass a null callback. The additional file flags
62 // will be added on top of the default file flags which are:
63 // base::PLATFORM_FILE_CREATE_ALWAYS
64 // base::PLATFORM_FILE_WRITE
65 // base::PLATFORM_FILE_TEMPORARY.
66 // Set |additional_file_flags| to 0 for synchronous writes and set to
67 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
69 // This returns false if task posting to |task_runner| has failed.
70 static bool CreateTemporary(
71 TaskRunner
* task_runner
,
72 int additional_file_flags
,
73 const CreateTemporaryCallback
& callback
);
75 // Close the given file handle.
76 // This returns false if task posting to |task_runner| has failed.
77 static bool Close(TaskRunner
* task_runner
,
79 const StatusCallback
& callback
);
81 // Retrieves the information about a file. It is invalid to pass a null
83 // This returns false if task posting to |task_runner| has failed.
84 static bool GetFileInfo(
85 TaskRunner
* task_runner
,
86 const FilePath
& file_path
,
87 const GetFileInfoCallback
& callback
);
89 // Does the same as GetFileInfo but takes PlatformFile instead of FilePath.
90 // This returns false if task posting to |task_runner| has failed.
91 static bool GetFileInfoFromPlatformFile(
92 TaskRunner
* task_runner
,
94 const GetFileInfoCallback
& callback
);
96 // Deletes a file or a directory.
97 // It is an error to delete a non-empty directory with recursive=false.
98 // This returns false if task posting to |task_runner| has failed.
99 static bool DeleteFile(TaskRunner
* task_runner
,
100 const FilePath
& file_path
,
102 const StatusCallback
& callback
);
104 // Reads from a file. On success, the file pointer is moved to position
105 // |offset + bytes_to_read| in the file. The callback can be null.
107 // This returns false if |bytes_to_read| is less than zero, or
108 // if task posting to |task_runner| has failed.
110 TaskRunner
* task_runner
,
114 const ReadCallback
& callback
);
116 // Writes to a file. If |offset| is greater than the length of the file,
117 // |false| is returned. On success, the file pointer is moved to position
118 // |offset + bytes_to_write| in the file. The callback can be null.
119 // |bytes_to_write| must be greater than zero.
121 // This returns false if |bytes_to_write| is less than or equal to zero,
122 // if |buffer| is NULL, or if task posting to |task_runner| has failed.
124 TaskRunner
* task_runner
,
129 const WriteCallback
& callback
);
131 // Touches a file. The callback can be null.
132 // This returns false if task posting to |task_runner| has failed.
134 TaskRunner
* task_runner
,
136 const Time
& last_access_time
,
137 const Time
& last_modified_time
,
138 const StatusCallback
& callback
);
140 // Touches a file. The callback can be null.
141 // This returns false if task posting to |task_runner| has failed.
143 TaskRunner
* task_runner
,
144 const FilePath
& file_path
,
145 const Time
& last_access_time
,
146 const Time
& last_modified_time
,
147 const StatusCallback
& callback
);
149 // Truncates a file to the given length. If |length| is greater than the
150 // current length of the file, the file will be extended with zeroes.
151 // The callback can be null.
152 // This returns false if task posting to |task_runner| has failed.
153 static bool Truncate(
154 TaskRunner
* task_runner
,
157 const StatusCallback
& callback
);
159 // Truncates a file to the given length. If |length| is greater than the
160 // current length of the file, the file will be extended with zeroes.
161 // The callback can be null.
162 // This returns false if task posting to |task_runner| has failed.
163 static bool Truncate(
164 TaskRunner
* task_runner
,
165 const FilePath
& path
,
167 const StatusCallback
& callback
);
169 // Flushes a file. The callback can be null.
170 // This returns false if task posting to |task_runner| has failed.
172 TaskRunner
* task_runner
,
174 const StatusCallback
& callback
);
177 // They return false if posting a given task to |task_runner| has failed.
178 static bool RelayCreateOrOpen(
179 TaskRunner
* task_runner
,
180 const CreateOrOpenTask
& open_task
,
181 const CloseTask
& close_task
,
182 const CreateOrOpenCallback
& callback
);
183 static bool RelayClose(
184 TaskRunner
* task_runner
,
185 const CloseTask
& close_task
,
187 const StatusCallback
& callback
);
190 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy
);
195 #endif // BASE_FILES_FILE_UTIL_PROXY_H_