DevTools: consistently use camel case for URL parameter names
[chromium-blink-merge.git] / base / file_util_proxy.h
blob72591c2909c04f65e8b78163883f10c251be74b8
1 // Copyright (c) 2011 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_FILE_UTIL_PROXY_H_
6 #define BASE_FILE_UTIL_PROXY_H_
8 #include <vector>
10 #include "base/base_api.h"
11 #include "base/callback_old.h"
12 #include "base/file_path.h"
13 #include "base/file_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/platform_file.h"
16 #include "base/tracked_objects.h"
18 namespace base {
20 class MessageLoopProxy;
21 class Time;
23 // This class provides asynchronous access to common file routines.
24 class BASE_API FileUtilProxy {
25 public:
26 // Holds metadata for file or directory entry. Used by ReadDirectoryCallback.
27 struct Entry {
28 FilePath::StringType name;
29 bool is_directory;
32 // This callback is used by methods that report only an error code. It is
33 // valid to pass NULL as the callback parameter to any function that takes a
34 // StatusCallback, in which case the operation will complete silently.
35 typedef Callback1<PlatformFileError /* error code */>::Type StatusCallback;
37 typedef Callback3<PlatformFileError /* error code */,
38 PassPlatformFile,
39 bool /* created */>::Type CreateOrOpenCallback;
40 typedef Callback3<PlatformFileError /* error code */,
41 PassPlatformFile,
42 FilePath>::Type CreateTemporaryCallback;
43 typedef Callback2<PlatformFileError /* error code */,
44 bool /* created */>::Type EnsureFileExistsCallback;
45 typedef Callback2<PlatformFileError /* error code */,
46 const PlatformFileInfo& /* file_info */
47 >::Type GetFileInfoCallback;
48 typedef Callback2<PlatformFileError /* error code */,
49 const std::vector<Entry>&>::Type ReadDirectoryCallback;
50 typedef Callback3<PlatformFileError /* error code */,
51 const char* /* data */,
52 int /* bytes read/written */>::Type ReadCallback;
53 typedef Callback2<PlatformFileError /* error code */,
54 int /* bytes written */>::Type WriteCallback;
56 // Creates or opens a file with the given flags. It is invalid to pass NULL
57 // for the callback.
58 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
59 // a new file at the given |file_path| and calls back with
60 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |file_path| already exists.
61 static bool CreateOrOpen(scoped_refptr<MessageLoopProxy> message_loop_proxy,
62 const FilePath& file_path,
63 int file_flags,
64 CreateOrOpenCallback* callback);
66 // Creates a temporary file for writing. The path and an open file handle
67 // are returned. It is invalid to pass NULL for the callback. The additional
68 // file flags will be added on top of the default file flags which are:
69 // base::PLATFORM_FILE_CREATE_ALWAYS
70 // base::PLATFORM_FILE_WRITE
71 // base::PLATFORM_FILE_TEMPORARY.
72 // Set |additional_file_flags| to 0 for synchronous writes and set to
73 // base::PLATFORM_FILE_ASYNC to support asynchronous file operations.
74 static bool CreateTemporary(
75 scoped_refptr<MessageLoopProxy> message_loop_proxy,
76 int additional_file_flags,
77 CreateTemporaryCallback* callback);
79 // Close the given file handle.
80 static bool Close(scoped_refptr<MessageLoopProxy> message_loop_proxy,
81 PlatformFile,
82 StatusCallback* callback);
84 // Ensures that the given |file_path| exist. This creates a empty new file
85 // at |file_path| if the |file_path| does not exist.
86 // If a new file han not existed and is created at the |file_path|,
87 // |created| of the callback argument is set true and |error code|
88 // is set PLATFORM_FILE_OK.
89 // If the file already exists, |created| is set false and |error code|
90 // is set PLATFORM_FILE_OK.
91 // If the file hasn't existed but it couldn't be created for some other
92 // reasons, |created| is set false and |error code| indicates the error.
93 static bool EnsureFileExists(
94 scoped_refptr<MessageLoopProxy> message_loop_proxy,
95 const FilePath& file_path,
96 EnsureFileExistsCallback* callback);
98 // Retrieves the information about a file. It is invalid to pass NULL for the
99 // callback.
100 static bool GetFileInfo(
101 scoped_refptr<MessageLoopProxy> message_loop_proxy,
102 const FilePath& file_path,
103 GetFileInfoCallback* callback);
105 static bool GetFileInfoFromPlatformFile(
106 scoped_refptr<MessageLoopProxy> message_loop_proxy,
107 PlatformFile file,
108 GetFileInfoCallback* callback);
110 static bool ReadDirectory(scoped_refptr<MessageLoopProxy> message_loop_proxy,
111 const FilePath& file_path,
112 ReadDirectoryCallback* callback);
114 // Creates directory at given path. It's an error to create
115 // if |exclusive| is true and dir already exists.
116 static bool CreateDirectory(
117 scoped_refptr<MessageLoopProxy> message_loop_proxy,
118 const FilePath& file_path,
119 bool exclusive,
120 bool recursive,
121 StatusCallback* callback);
123 // Copies a file or a directory from |src_file_path| to |dest_file_path|
124 // Error cases:
125 // If destination file doesn't exist or destination's parent
126 // doesn't exists.
127 // If source dir exists but destination path is an existing file.
128 // If source file exists but destination path is an existing directory.
129 // If source is a parent of destination.
130 // If source doesn't exists.
131 static bool Copy(scoped_refptr<MessageLoopProxy> message_loop_proxy,
132 const FilePath& src_file_path,
133 const FilePath& dest_file_path,
134 StatusCallback* callback);
136 // Moves a file or a directory from src_file_path to dest_file_path.
137 // Error cases are similar to Copy method's error cases.
138 static bool Move(
139 scoped_refptr<MessageLoopProxy> message_loop_proxy,
140 const FilePath& src_file_path,
141 const FilePath& dest_file_path,
142 StatusCallback* callback);
144 // Deletes a file or a directory.
145 // It is an error to delete a non-empty directory with recursive=false.
146 static bool Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy,
147 const FilePath& file_path,
148 bool recursive,
149 StatusCallback* callback);
151 // Deletes a directory and all of its contents.
152 static bool RecursiveDelete(
153 scoped_refptr<MessageLoopProxy> message_loop_proxy,
154 const FilePath& file_path,
155 StatusCallback* callback);
157 // Reads from a file. On success, the file pointer is moved to position
158 // |offset + bytes_to_read| in the file. The callback can be NULL.
159 static bool Read(
160 scoped_refptr<MessageLoopProxy> message_loop_proxy,
161 PlatformFile file,
162 int64 offset,
163 int bytes_to_read,
164 ReadCallback* callback);
166 // Writes to a file. If |offset| is greater than the length of the file,
167 // |false| is returned. On success, the file pointer is moved to position
168 // |offset + bytes_to_write| in the file. The callback can be NULL.
169 static bool Write(
170 scoped_refptr<MessageLoopProxy> message_loop_proxy,
171 PlatformFile file,
172 int64 offset,
173 const char* buffer,
174 int bytes_to_write,
175 WriteCallback* callback);
177 // Touches a file. The callback can be NULL.
178 static bool Touch(
179 scoped_refptr<MessageLoopProxy> message_loop_proxy,
180 PlatformFile file,
181 const Time& last_access_time,
182 const Time& last_modified_time,
183 StatusCallback* callback);
185 // Touches a file. The callback can be NULL.
186 static bool Touch(
187 scoped_refptr<MessageLoopProxy> message_loop_proxy,
188 const FilePath& file_path,
189 const Time& last_access_time,
190 const Time& last_modified_time,
191 StatusCallback* callback);
193 // Truncates a file to the given length. If |length| is greater than the
194 // current length of the file, the file will be extended with zeroes.
195 // The callback can be NULL.
196 static bool Truncate(
197 scoped_refptr<MessageLoopProxy> message_loop_proxy,
198 PlatformFile file,
199 int64 length,
200 StatusCallback* callback);
202 // Truncates a file to the given length. If |length| is greater than the
203 // current length of the file, the file will be extended with zeroes.
204 // The callback can be NULL.
205 static bool Truncate(
206 scoped_refptr<MessageLoopProxy> message_loop_proxy,
207 const FilePath& path,
208 int64 length,
209 StatusCallback* callback);
211 // Flushes a file. The callback can be NULL.
212 static bool Flush(
213 scoped_refptr<MessageLoopProxy> message_loop_proxy,
214 PlatformFile file,
215 StatusCallback* callback);
217 private:
218 DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
221 } // namespace base
223 #endif // BASE_FILE_UTIL_PROXY_H_