Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / webkit / fileapi / async_file_util.h
blob1c4a5cefa1b6a581c4b7cba02056723d409c91cd
1 // Copyright (c) 2013 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 WEBKIT_FILEAPI_ASYNC_FILE_UTIL_H_
6 #define WEBKIT_FILEAPI_ASYNC_FILE_UTIL_H_
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "base/files/file_util_proxy.h"
11 #include "base/platform_file.h"
12 #include "webkit/fileapi/directory_entry.h"
13 #include "webkit/storage/webkit_storage_export.h"
15 namespace base {
16 class Time;
19 namespace webkit_blob {
20 class ShareableFileReference;
23 namespace fileapi {
25 class FileSystemOperationContext;
26 class FileSystemURL;
28 // An interface which provides filesystem-specific file operations for
29 // LocalFileSystemOperation.
31 // Each filesystem which needs to be dispatched from LocalFileSystemOperation
32 // must implement this interface or a synchronous version of interface:
33 // FileSystemFileUtil.
35 class WEBKIT_STORAGE_EXPORT AsyncFileUtil {
36 public:
37 typedef base::Callback<
38 void(base::PlatformFileError result)> StatusCallback;
40 typedef base::FileUtilProxy::CreateOrOpenCallback CreateOrOpenCallback;
42 typedef base::Callback<
43 void(base::PlatformFileError result,
44 bool created)> EnsureFileExistsCallback;
46 typedef base::Callback<
47 void(base::PlatformFileError result,
48 const base::PlatformFileInfo& file_info,
49 const base::FilePath& platform_path)> GetFileInfoCallback;
51 typedef std::vector<DirectoryEntry> EntryList;
52 typedef base::Callback<
53 void(base::PlatformFileError result,
54 const EntryList& file_list,
55 bool has_more)> ReadDirectoryCallback;
57 typedef base::Callback<
58 void(base::PlatformFileError result,
59 const base::PlatformFileInfo& file_info,
60 const base::FilePath& platform_path,
61 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref
62 )> CreateSnapshotFileCallback;
64 AsyncFileUtil() {}
65 virtual ~AsyncFileUtil() {}
67 // Creates or opens a file with the given flags.
68 // If PLATFORM_FILE_CREATE is set in |file_flags| it always tries to create
69 // a new file at the given |url| and calls back with
70 // PLATFORM_FILE_ERROR_FILE_EXISTS if the |url| already exists.
72 // LocalFileSystemOperation::OpenFile calls this.
73 // This is used only by Pepper/NaCL File API.
75 // This returns false if it fails to post an async task.
77 virtual bool CreateOrOpen(
78 FileSystemOperationContext* context,
79 const FileSystemURL& url,
80 int file_flags,
81 const CreateOrOpenCallback& callback) = 0;
83 // Ensures that the given |url| exist. This creates a empty new file
84 // at |url| if the |url| does not exist.
86 // LocalFileSystemOperation::CreateFile calls this.
88 // This returns false if it fails to post an async task.
90 // This reports following error code via |callback|:
91 // - PLATFORM_FILE_OK and created==true if a file has not existed and
92 // is created at |url|.
93 // - PLATFORM_FILE_OK and created==false if the file already exists.
94 // - Other error code (with created=false) if a file hasn't existed yet
95 // and there was an error while creating a new file.
97 virtual bool EnsureFileExists(
98 FileSystemOperationContext* context,
99 const FileSystemURL& url,
100 const EnsureFileExistsCallback& callback) = 0;
102 // Creates directory at given url.
104 // LocalFileSystemOperation::CreateDirectory calls this.
106 // This returns false if it fails to post an async task.
108 // This reports following error code via |callback|:
109 // - PLATFORM_FILE_ERROR_NOT_FOUND if the |url|'s parent directory
110 // does not exist and |recursive| is false.
111 // - PLATFORM_FILE_ERROR_EXISTS if a directory already exists at |url|
112 // and |exclusive| is true.
113 // - PLATFORM_FILE_ERROR_EXISTS if a file already exists at |url|
114 // (regardless of |exclusive| value).
115 // - Other error code if it failed to create a directory.
117 virtual bool CreateDirectory(
118 FileSystemOperationContext* context,
119 const FileSystemURL& url,
120 bool exclusive,
121 bool recursive,
122 const StatusCallback& callback) = 0;
124 // Retrieves the information about a file.
126 // LocalFileSystemOperation::GetMetadata calls this.
128 // This returns false if it fails to post an async task.
130 // This reports following error code via |callback|:
131 // - PLATFORM_FILE_ERROR_NOT_FOUND if the file doesn't exist.
132 // - Other error code if there was an error while retrieving the file info.
134 virtual bool GetFileInfo(
135 FileSystemOperationContext* context,
136 const FileSystemURL& url,
137 const GetFileInfoCallback& callback) = 0;
139 // Reads contents of a directory at |path|.
141 // LocalFileSystemOperation::ReadDirectory calls this.
143 // Note that the |name| field of each entry in |file_list|
144 // returned by |callback| should have a base file name
145 // of the entry relative to the directory, but not an absolute path.
147 // (E.g. if ReadDirectory is called for a directory
148 // 'path/to/dir' and the directory has entries 'a' and 'b',
149 // the returned |file_list| should include entries whose names
150 // are 'a' and 'b', but not '/path/to/dir/a' and '/path/to/dir/b'.)
152 // This returns false if it fails to post an async task.
154 // This reports following error code via |callback|:
155 // - PLATFORM_FILE_ERROR_NOT_FOUND if the target directory doesn't exist.
156 // - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY if an entry exists at |url| but
157 // is a file (not a directory).
159 virtual bool ReadDirectory(
160 FileSystemOperationContext* context,
161 const FileSystemURL& url,
162 const ReadDirectoryCallback& callback) = 0;
164 // Modifies timestamps of a file or directory at |url| with
165 // |last_access_time| and |last_modified_time|. The function DOES NOT
166 // create a file unlike 'touch' command on Linux.
168 // LocalFileSystemOperation::TouchFile calls this.
169 // This is used only by Pepper/NaCL File API.
171 // This returns false if it fails to post an async task.
172 virtual bool Touch(
173 FileSystemOperationContext* context,
174 const FileSystemURL& url,
175 const base::Time& last_access_time,
176 const base::Time& last_modified_time,
177 const StatusCallback& callback) = 0;
179 // Truncates a file at |path| to |length|. If |length| is larger than
180 // the original file size, the file will be extended, and the extended
181 // part is filled with null bytes.
183 // LocalFileSystemOperation::Truncate calls this.
185 // This returns false if it fails to post an async task.
187 // This reports following error code via |callback|:
188 // - PLATFORM_FILE_ERROR_NOT_FOUND if the file doesn't exist.
190 virtual bool Truncate(
191 FileSystemOperationContext* context,
192 const FileSystemURL& url,
193 int64 length,
194 const StatusCallback& callback) = 0;
196 // Copies a file from |src_url| to |dest_url|.
197 // This must be called for files that belong to the same filesystem
198 // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
200 // LocalFileSystemOperation::Copy calls this for same-filesystem copy case.
202 // This returns false if it fails to post an async task.
204 // This reports following error code via |callback|:
205 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url|
206 // or the parent directory of |dest_url| does not exist.
207 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
208 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
209 // is not a file.
210 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
211 // its parent path is a file.
213 virtual bool CopyFileLocal(
214 FileSystemOperationContext* context,
215 const FileSystemURL& src_url,
216 const FileSystemURL& dest_url,
217 const StatusCallback& callback) = 0;
219 // Moves a local file from |src_url| to |dest_url|.
220 // This must be called for files that belong to the same filesystem
221 // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
223 // LocalFileSystemOperation::Move calls this for same-filesystem move case.
225 // This returns false if it fails to post an async task.
227 // This reports following error code via |callback|:
228 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url|
229 // or the parent directory of |dest_url| does not exist.
230 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
231 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
232 // is not a file.
233 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
234 // its parent path is a file.
236 virtual bool MoveFileLocal(
237 FileSystemOperationContext* context,
238 const FileSystemURL& src_url,
239 const FileSystemURL& dest_url,
240 const StatusCallback& callback) = 0;
242 // Copies in a single file from a different filesystem.
244 // LocalFileSystemOperation::Copy or Move calls this for cross-filesystem
245 // cases.
247 // This returns false if it fails to post an async task.
249 // This reports following error code via |callback|:
250 // - PLATFORM_FILE_ERROR_NOT_FOUND if |src_file_path|
251 // or the parent directory of |dest_url| does not exist.
252 // - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
253 // is not a file.
254 // - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
255 // its parent path is a file.
257 virtual bool CopyInForeignFile(
258 FileSystemOperationContext* context,
259 const base::FilePath& src_file_path,
260 const FileSystemURL& dest_url,
261 const StatusCallback& callback) = 0;
263 // Deletes a single file.
265 // LocalFileSystemOperation::RemoveFile calls this.
267 // This returns false if it fails to post an async task.
269 // This reports following error code via |callback|:
270 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist.
271 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |url| is not a file.
273 virtual bool DeleteFile(
274 FileSystemOperationContext* context,
275 const FileSystemURL& url,
276 const StatusCallback& callback) = 0;
278 // Removes a single empty directory.
280 // LocalFileSystemOperation::RemoveDirectory calls this.
282 // This returns false if it fails to post an async task.
284 // This reports following error code via |callback|:
285 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist.
286 // - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory.
287 // - PLATFORM_FILE_ERROR_NOT_EMPTY if |url| is not empty.
289 virtual bool DeleteDirectory(
290 FileSystemOperationContext* context,
291 const FileSystemURL& url,
292 const StatusCallback& callback) = 0;
294 // Creates a local snapshot file for a given |url| and returns the
295 // metadata and platform path of the snapshot file via |callback|.
296 // In regular filesystem cases the implementation may simply return
297 // the metadata of the file itself (as well as GetMetadata does),
298 // while in non-regular filesystem case the backend may create a
299 // temporary snapshot file which holds the file data and return
300 // the metadata of the temporary file.
302 // In the callback, it returns:
303 // |file_info| is the metadata of the snapshot file created.
304 // |platform_path| is the full absolute platform path to the snapshot
305 // file created. If a file is not backed by a real local file in
306 // the implementor's FileSystem, the implementor must create a
307 // local snapshot file and return the path of the created file.
309 // If implementors creates a temporary file for snapshotting and wants
310 // FileAPI backend to take care of the lifetime of the file (so that
311 // it won't get deleted while JS layer has any references to the created
312 // File/Blob object), it should return non-empty |file_ref|.
313 // Via the |file_ref| implementors can schedule a file deletion
314 // or arbitrary callbacks when the last reference of File/Blob is dropped.
316 // LocalFileSystemOperation::CreateSnapshotFile calls this.
318 // This returns false if it fails to post an async task.
320 // This reports following error code via |callback|:
321 // - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist.
322 // - PLATFORM_FILE_ERROR_NOT_A_FILE if |url| exists but is a directory.
324 // The field values of |file_info| are undefined (implementation
325 // dependent) in error cases, and the caller should always
326 // check the return code.
327 virtual bool CreateSnapshotFile(
328 FileSystemOperationContext* context,
329 const FileSystemURL& url,
330 const CreateSnapshotFileCallback& callback) = 0;
332 private:
333 DISALLOW_COPY_AND_ASSIGN(AsyncFileUtil);
336 } // namespace fileapi
338 #endif // WEBKIT_FILEAPI_ASYNC_FILE_UTIL_H_