• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef WEBKIT_BROWSER_FILEAPI_ASYNC_FILE_UTIL_H_
6 #define WEBKIT_BROWSER_FILEAPI_ASYNC_FILE_UTIL_H_
7 
8 #include <vector>
9 
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/files/file.h"
13 #include "base/files/file_util_proxy.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "webkit/browser/fileapi/file_system_operation.h"
16 #include "webkit/browser/webkit_storage_browser_export.h"
17 #include "webkit/common/fileapi/directory_entry.h"
18 
19 namespace base {
20 class Time;
21 }
22 
23 namespace webkit_blob {
24 class ShareableFileReference;
25 }
26 
27 namespace fileapi {
28 
29 class FileSystemOperationContext;
30 class FileSystemURL;
31 
32 // An interface which provides filesystem-specific file operations for
33 // FileSystemOperationImpl.
34 //
35 // Each filesystem which needs to be dispatched from FileSystemOperationImpl
36 // must implement this interface or a synchronous version of interface:
37 // FileSystemFileUtil.
38 //
39 // As far as an instance of this class is owned by a FileSystemBackend
40 // (which is owned by FileSystemContext), it's guaranteed that this instance's
41 // alive while FileSystemOperationContext given to each operation is kept
42 // alive. (Note that this instance might be freed on different thread
43 // from the thread it is created.)
44 //
45 // It is NOT valid to give null callback to this class, and implementors
46 // can assume that they don't get any null callbacks.
47 //
48 class AsyncFileUtil {
49  public:
50   typedef base::Callback<void(base::File::Error result)> StatusCallback;
51 
52   // |on_close_callback| will be called after the |file| is closed in the
53   // child process. |on_close_callback|.is_null() can be true, if no operation
54   // is needed on closing the file.
55   typedef base::Callback<
56       void(base::File file,
57            const base::Closure& on_close_callback)> CreateOrOpenCallback;
58 
59   typedef base::Callback<
60       void(base::File::Error result,
61            bool created)> EnsureFileExistsCallback;
62 
63   typedef base::Callback<
64       void(base::File::Error result,
65            const base::File::Info& file_info)> GetFileInfoCallback;
66 
67   typedef std::vector<DirectoryEntry> EntryList;
68   typedef base::Callback<
69       void(base::File::Error result,
70            const EntryList& file_list,
71            bool has_more)> ReadDirectoryCallback;
72 
73   typedef base::Callback<
74       void(base::File::Error result,
75            const base::File::Info& file_info,
76            const base::FilePath& platform_path,
77            const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref)>
78       CreateSnapshotFileCallback;
79 
80 
81   typedef base::Callback<void(int64 size)> CopyFileProgressCallback;
82 
83   typedef FileSystemOperation::CopyOrMoveOption CopyOrMoveOption;
84 
85   // Creates an AsyncFileUtil instance which performs file operations on
86   // local native file system. The created instance assumes
87   // FileSystemURL::path() has the target platform path.
88   WEBKIT_STORAGE_BROWSER_EXPORT static AsyncFileUtil*
89       CreateForLocalFileSystem();
90 
AsyncFileUtil()91   AsyncFileUtil() {}
~AsyncFileUtil()92   virtual ~AsyncFileUtil() {}
93 
94   // Creates or opens a file with the given flags.
95   // If File::FLAG_CREATE is set in |file_flags| it always tries to create
96   // a new file at the given |url| and calls back with
97   // File::FILE_ERROR_FILE_EXISTS if the |url| already exists.
98   //
99   // FileSystemOperationImpl::OpenFile calls this.
100   // This is used only by Pepper/NaCl File API.
101   //
102   virtual void CreateOrOpen(
103       scoped_ptr<FileSystemOperationContext> context,
104       const FileSystemURL& url,
105       int file_flags,
106       const CreateOrOpenCallback& callback) = 0;
107 
108   // Ensures that the given |url| exist.  This creates a empty new file
109   // at |url| if the |url| does not exist.
110   //
111   // FileSystemOperationImpl::CreateFile calls this.
112   //
113   // This reports following error code via |callback|:
114   // - File::FILE_OK and created==true if a file has not existed and
115   //   is created at |url|.
116   // - File::FILE_OK and created==false if the file already exists.
117   // - Other error code (with created=false) if a file hasn't existed yet
118   //   and there was an error while creating a new file.
119   //
120   virtual void EnsureFileExists(
121       scoped_ptr<FileSystemOperationContext> context,
122       const FileSystemURL& url,
123       const EnsureFileExistsCallback& callback) = 0;
124 
125   // Creates directory at given url.
126   //
127   // FileSystemOperationImpl::CreateDirectory calls this.
128   //
129   // This reports following error code via |callback|:
130   // - File::FILE_ERROR_NOT_FOUND if the |url|'s parent directory
131   //   does not exist and |recursive| is false.
132   // - File::FILE_ERROR_EXISTS if a directory already exists at |url|
133   //   and |exclusive| is true.
134   // - File::FILE_ERROR_EXISTS if a file already exists at |url|
135   //   (regardless of |exclusive| value).
136   // - Other error code if it failed to create a directory.
137   //
138   virtual void CreateDirectory(
139       scoped_ptr<FileSystemOperationContext> context,
140       const FileSystemURL& url,
141       bool exclusive,
142       bool recursive,
143       const StatusCallback& callback) = 0;
144 
145   // Retrieves the information about a file.
146   //
147   // FileSystemOperationImpl::GetMetadata calls this.
148   //
149   // This reports following error code via |callback|:
150   // - File::FILE_ERROR_NOT_FOUND if the file doesn't exist.
151   // - Other error code if there was an error while retrieving the file info.
152   //
153   virtual void GetFileInfo(
154       scoped_ptr<FileSystemOperationContext> context,
155       const FileSystemURL& url,
156       const GetFileInfoCallback& callback) = 0;
157 
158   // Reads contents of a directory at |path|.
159   //
160   // FileSystemOperationImpl::ReadDirectory calls this.
161   //
162   // Note that the |name| field of each entry in |file_list|
163   // returned by |callback| should have a base file name
164   // of the entry relative to the directory, but not an absolute path.
165   //
166   // (E.g. if ReadDirectory is called for a directory
167   // 'path/to/dir' and the directory has entries 'a' and 'b',
168   // the returned |file_list| should include entries whose names
169   // are 'a' and 'b', but not '/path/to/dir/a' and '/path/to/dir/b'.)
170   //
171   // This reports following error code via |callback|:
172   // - File::FILE_ERROR_NOT_FOUND if the target directory doesn't exist.
173   // - File::FILE_ERROR_NOT_A_DIRECTORY if an entry exists at |url| but
174   //   is a file (not a directory).
175   //
176   virtual void ReadDirectory(
177       scoped_ptr<FileSystemOperationContext> context,
178       const FileSystemURL& url,
179       const ReadDirectoryCallback& callback) = 0;
180 
181   // Modifies timestamps of a file or directory at |url| with
182   // |last_access_time| and |last_modified_time|. The function DOES NOT
183   // create a file unlike 'touch' command on Linux.
184   //
185   // FileSystemOperationImpl::TouchFile calls this.
186   // This is used only by Pepper/NaCl File API.
187   //
188   virtual void Touch(
189       scoped_ptr<FileSystemOperationContext> context,
190       const FileSystemURL& url,
191       const base::Time& last_access_time,
192       const base::Time& last_modified_time,
193       const StatusCallback& callback) = 0;
194 
195   // Truncates a file at |path| to |length|. If |length| is larger than
196   // the original file size, the file will be extended, and the extended
197   // part is filled with null bytes.
198   //
199   // FileSystemOperationImpl::Truncate calls this.
200   //
201   // This reports following error code via |callback|:
202   // - File::FILE_ERROR_NOT_FOUND if the file doesn't exist.
203   //
204   virtual void Truncate(
205       scoped_ptr<FileSystemOperationContext> context,
206       const FileSystemURL& url,
207       int64 length,
208       const StatusCallback& callback) = 0;
209 
210   // Copies a file from |src_url| to |dest_url|.
211   // This must be called for files that belong to the same filesystem
212   // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
213   // |progress_callback| is a callback to report the progress update.
214   // See file_system_operations.h for details. This should be called on the
215   // same thread as where the method's called (IO thread). Calling this
216   // is optional. It is recommended to use this callback for heavier operations
217   // (such as file network downloading), so that, e.g., clients (UIs) can
218   // update its state to show progress to users. This may be a null callback.
219   //
220   // FileSystemOperationImpl::Copy calls this for same-filesystem copy case.
221   //
222   // This reports following error code via |callback|:
223   // - File::FILE_ERROR_NOT_FOUND if |src_url|
224   //   or the parent directory of |dest_url| does not exist.
225   // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
226   // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
227   //   is not a file.
228   // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
229   //   its parent path is a file.
230   //
231   virtual void CopyFileLocal(
232       scoped_ptr<FileSystemOperationContext> context,
233       const FileSystemURL& src_url,
234       const FileSystemURL& dest_url,
235       CopyOrMoveOption option,
236       const CopyFileProgressCallback& progress_callback,
237       const StatusCallback& callback) = 0;
238 
239   // Moves a local file from |src_url| to |dest_url|.
240   // This must be called for files that belong to the same filesystem
241   // (i.e. type() and origin() of the |src_url| and |dest_url| must match).
242   //
243   // FileSystemOperationImpl::Move calls this for same-filesystem move case.
244   //
245   // This reports following error code via |callback|:
246   // - File::FILE_ERROR_NOT_FOUND if |src_url|
247   //   or the parent directory of |dest_url| does not exist.
248   // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
249   // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
250   //   is not a file.
251   // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
252   //   its parent path is a file.
253   //
254   virtual void MoveFileLocal(
255       scoped_ptr<FileSystemOperationContext> context,
256       const FileSystemURL& src_url,
257       const FileSystemURL& dest_url,
258       CopyOrMoveOption option,
259       const StatusCallback& callback) = 0;
260 
261   // Copies in a single file from a different filesystem.
262   //
263   // FileSystemOperationImpl::Copy or Move calls this for cross-filesystem
264   // cases.
265   //
266   // This reports following error code via |callback|:
267   // - File::FILE_ERROR_NOT_FOUND if |src_file_path|
268   //   or the parent directory of |dest_url| does not exist.
269   // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
270   //   is not a file.
271   // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
272   //   its parent path is a file.
273   //
274   virtual void CopyInForeignFile(
275         scoped_ptr<FileSystemOperationContext> context,
276         const base::FilePath& src_file_path,
277         const FileSystemURL& dest_url,
278         const StatusCallback& callback) = 0;
279 
280   // Deletes a single file.
281   //
282   // FileSystemOperationImpl::RemoveFile calls this.
283   //
284   // This reports following error code via |callback|:
285   // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
286   // - File::FILE_ERROR_NOT_A_FILE if |url| is not a file.
287   //
288   virtual void DeleteFile(
289       scoped_ptr<FileSystemOperationContext> context,
290       const FileSystemURL& url,
291       const StatusCallback& callback) = 0;
292 
293   // Removes a single empty directory.
294   //
295   // FileSystemOperationImpl::RemoveDirectory calls this.
296   //
297   // This reports following error code via |callback|:
298   // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
299   // - File::FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory.
300   // - File::FILE_ERROR_NOT_EMPTY if |url| is not empty.
301   //
302   virtual void DeleteDirectory(
303       scoped_ptr<FileSystemOperationContext> context,
304       const FileSystemURL& url,
305       const StatusCallback& callback) = 0;
306 
307   // Removes a single file or a single directory with its contents
308   // (i.e. files/subdirectories under the directory).
309   //
310   // FileSystemOperationImpl::Remove calls this.
311   // On some platforms, such as Chrome OS Drive File System, recursive file
312   // deletion can be implemented more efficiently than calling DeleteFile() and
313   // DeleteDirectory() for each files/directories.
314   // This method is optional, so if not supported,
315   // File::FILE_ERROR_INVALID_OPERATION should be returned via |callback|.
316   //
317   // This reports following error code via |callback|:
318   // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
319   // - File::FILE_ERROR_INVALID_OPERATION if this operation is not supported.
320   virtual void DeleteRecursively(
321       scoped_ptr<FileSystemOperationContext> context,
322       const FileSystemURL& url,
323       const StatusCallback& callback) = 0;
324 
325   // Creates a local snapshot file for a given |url| and returns the
326   // metadata and platform path of the snapshot file via |callback|.
327   // In regular filesystem cases the implementation may simply return
328   // the metadata of the file itself (as well as GetMetadata does),
329   // while in non-regular filesystem case the backend may create a
330   // temporary snapshot file which holds the file data and return
331   // the metadata of the temporary file.
332   //
333   // In the callback, it returns:
334   // |file_info| is the metadata of the snapshot file created.
335   // |platform_path| is the full absolute platform path to the snapshot
336   // file created.  If a file is not backed by a real local file in
337   // the implementor's FileSystem, the implementor must create a
338   // local snapshot file and return the path of the created file.
339   //
340   // If implementors creates a temporary file for snapshotting and wants
341   // FileAPI backend to take care of the lifetime of the file (so that
342   // it won't get deleted while JS layer has any references to the created
343   // File/Blob object), it should return non-empty |file_ref|.
344   // Via the |file_ref| implementors can schedule a file deletion
345   // or arbitrary callbacks when the last reference of File/Blob is dropped.
346   //
347   // FileSystemOperationImpl::CreateSnapshotFile calls this.
348   //
349   // This reports following error code via |callback|:
350   // - File::FILE_ERROR_NOT_FOUND if |url| does not exist.
351   // - File::FILE_ERROR_NOT_A_FILE if |url| exists but is a directory.
352   //
353   // The field values of |file_info| are undefined (implementation
354   // dependent) in error cases, and the caller should always
355   // check the return code.
356   virtual void CreateSnapshotFile(
357       scoped_ptr<FileSystemOperationContext> context,
358       const FileSystemURL& url,
359       const CreateSnapshotFileCallback& callback) = 0;
360 
361  private:
362   DISALLOW_COPY_AND_ASSIGN(AsyncFileUtil);
363 };
364 
365 }  // namespace fileapi
366 
367 #endif  // WEBKIT_BROWSER_FILEAPI_ASYNC_FILE_UTIL_H_
368