blob: 404c9f74e9e0a075e4fb8c06a64790b506916141 [file] [log] [blame]
[email protected]60f60f82012-01-11 10:26:101// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7546c9e2011-12-16 11:31:432// 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_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_
6#define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_
7
[email protected]c671c8a2012-04-05 10:28:398#include <vector>
9
[email protected]81f8a3d2012-02-14 08:40:3710#include "base/file_util_proxy.h"
11#include "base/platform_file.h"
[email protected]7546c9e2011-12-16 11:31:4312#include "base/process.h"
[email protected]a52305082012-02-29 23:00:0013#include "webkit/blob/shareable_file_reference.h"
[email protected]7546c9e2011-12-16 11:31:4314
15namespace base {
16class Time;
17} // namespace base
18
19namespace net {
20class URLRequest;
21class URLRequestContext;
22} // namespace net
23
[email protected]47373872012-02-28 06:27:3024namespace webkit_blob {
[email protected]a52305082012-02-29 23:00:0025class ShareableFileReference;
[email protected]47373872012-02-28 06:27:3026}
27
[email protected]7546c9e2011-12-16 11:31:4328class GURL;
29
30namespace fileapi {
31
[email protected]949f25a2012-06-27 01:53:0932class FileSystemURL;
[email protected]02a60542012-07-24 20:05:3333class LocalFileSystemOperation;
[email protected]60f60f82012-01-11 10:26:1034
[email protected]7546c9e2011-12-16 11:31:4335// The interface class for FileSystemOperation implementations.
36//
37// This interface defines file system operations required to implement
38// "File API: Directories and System"
39// http://www.w3.org/TR/file-system-api/
40//
41// DESIGN NOTES
42//
43// This class is designed to
44//
45// 1) Serve one-time file system operation per instance. Only one
46// method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists,
47// GetMetadata, ReadDirectory and Remove) may be called during the
48// lifetime of this object and it should be called no more than once.
49//
50// 2) Be self-destructed, or get deleted via base::Owned() after the
51// operation finishes and completion callback is called.
52//
[email protected]81f8a3d2012-02-14 08:40:3753// 3) Deliver the results of operations to the client via the callback function
54// passed as the last parameter of the method.
[email protected]7546c9e2011-12-16 11:31:4355//
56class FileSystemOperationInterface {
57 public:
58 virtual ~FileSystemOperationInterface() {}
59
[email protected]81f8a3d2012-02-14 08:40:3760 // Used for CreateFile(), etc. |result| is the return code of the operation.
61 typedef base::Callback<void(base::PlatformFileError result)> StatusCallback;
62
63 // Used for GetMetadata(). |result| is the return code of the operation,
64 // |file_info| is the obtained file info, and |platform_path| is the path
65 // of the file.
[email protected]c671c8a2012-04-05 10:28:3966 typedef base::Callback<
67 void(base::PlatformFileError result,
68 const base::PlatformFileInfo& file_info,
69 const FilePath& platform_path)> GetMetadataCallback;
[email protected]81f8a3d2012-02-14 08:40:3770
71 // Used for OpenFile(). |result| is the return code of the operation.
[email protected]c671c8a2012-04-05 10:28:3972 typedef base::Callback<
73 void(base::PlatformFileError result,
74 base::PlatformFile file,
75 base::ProcessHandle peer_handle)> OpenFileCallback;
[email protected]81f8a3d2012-02-14 08:40:3776
77 // Used for ReadDirectory(). |result| is the return code of the operation,
78 // |file_list| is the list of files read, and |has_more| is true if some files
79 // are yet to be read.
[email protected]c671c8a2012-04-05 10:28:3980 typedef base::Callback<
81 void(base::PlatformFileError result,
82 const std::vector<base::FileUtilProxy::Entry>& file_list,
83 bool has_more)> ReadDirectoryCallback;
[email protected]81f8a3d2012-02-14 08:40:3784
[email protected]47373872012-02-28 06:27:3085 // Used for CreateSnapshotFile(). (Please see the comment at
86 // CreateSnapshotFile() below for how the method is called)
87 // |result| is the return code of the operation.
88 // |file_info| is the metadata of the snapshot file created.
89 // |platform_path| is the path to the snapshot file created.
90 //
91 // The snapshot file could simply be of the local file pointed by the given
92 // filesystem URL in local filesystem cases; remote filesystems
93 // may want to download the file into a temporary snapshot file and then
94 // return the metadata of the temporary file.
95 //
[email protected]a52305082012-02-29 23:00:0096 // |file_ref| is used to manage the lifetime of the returned
[email protected]47373872012-02-28 06:27:3097 // snapshot file. It can be set to let the chromium backend take
98 // care of the life time of the snapshot file. Otherwise (if the returned
[email protected]a52305082012-02-29 23:00:0099 // file does not require any handling) the implementation can just
[email protected]47373872012-02-28 06:27:30100 // return NULL. In a more complex case, the implementaiton can manage
101 // the lifetime of the snapshot file on its own (e.g. by its cache system)
102 // but also can be notified via the reference when the file becomes no
103 // longer necessary in the javascript world.
[email protected]a52305082012-02-29 23:00:00104 // Please see the comment for ShareableFileReference for details.
[email protected]47373872012-02-28 06:27:30105 //
[email protected]c671c8a2012-04-05 10:28:39106 typedef base::Callback<
107 void(base::PlatformFileError result,
108 const base::PlatformFileInfo& file_info,
109 const FilePath& platform_path,
110 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref)>
111 SnapshotFileCallback;
[email protected]47373872012-02-28 06:27:30112
[email protected]81f8a3d2012-02-14 08:40:37113 // Used for Write().
114 typedef base::Callback<void(base::PlatformFileError result,
115 int64 bytes,
116 bool complete)> WriteCallback;
117
[email protected]7546c9e2011-12-16 11:31:43118 // Creates a file at |path|. If |exclusive| is true, an error is raised
119 // in case a file is already present at the URL.
[email protected]949f25a2012-06-27 01:53:09120 virtual void CreateFile(const FileSystemURL& path,
[email protected]81f8a3d2012-02-14 08:40:37121 bool exclusive,
122 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43123
124 // Creates a directory at |path|. If |exclusive| is true, an error is
125 // raised in case a directory is already present at the URL. If
126 // |recursive| is true, create parent directories as needed just like
127 // mkdir -p does.
[email protected]949f25a2012-06-27 01:53:09128 virtual void CreateDirectory(const FileSystemURL& path,
[email protected]7546c9e2011-12-16 11:31:43129 bool exclusive,
[email protected]81f8a3d2012-02-14 08:40:37130 bool recursive,
131 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43132
133 // Copes a file or directory from |src_path| to |dest_path|. If
134 // |src_path| is a directory, the contents of |src_path| are copied to
135 // |dest_path| recursively. A new file or directory is created at
136 // |dest_path| as needed.
[email protected]949f25a2012-06-27 01:53:09137 virtual void Copy(const FileSystemURL& src_path,
138 const FileSystemURL& dest_path,
[email protected]81f8a3d2012-02-14 08:40:37139 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43140
141 // Moves a file or directory from |src_path| to |dest_path|. A new file
142 // or directory is created at |dest_path| as needed.
[email protected]949f25a2012-06-27 01:53:09143 virtual void Move(const FileSystemURL& src_path,
144 const FileSystemURL& dest_path,
[email protected]81f8a3d2012-02-14 08:40:37145 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43146
147 // Checks if a directory is present at |path|.
[email protected]949f25a2012-06-27 01:53:09148 virtual void DirectoryExists(const FileSystemURL& path,
[email protected]81f8a3d2012-02-14 08:40:37149 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43150
151 // Checks if a file is present at |path|.
[email protected]949f25a2012-06-27 01:53:09152 virtual void FileExists(const FileSystemURL& path,
[email protected]81f8a3d2012-02-14 08:40:37153 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43154
155 // Gets the metadata of a file or directory at |path|.
[email protected]949f25a2012-06-27 01:53:09156 virtual void GetMetadata(const FileSystemURL& path,
[email protected]81f8a3d2012-02-14 08:40:37157 const GetMetadataCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43158
159 // Reads contents of a directory at |path|.
[email protected]949f25a2012-06-27 01:53:09160 virtual void ReadDirectory(const FileSystemURL& path,
[email protected]81f8a3d2012-02-14 08:40:37161 const ReadDirectoryCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43162
163 // Removes a file or directory at |path|. If |recursive| is true, remove
164 // all files and directories under the directory at |path| recursively.
[email protected]949f25a2012-06-27 01:53:09165 virtual void Remove(const FileSystemURL& path, bool recursive,
[email protected]81f8a3d2012-02-14 08:40:37166 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43167
168 // Writes contents of |blob_url| to |path| at |offset|.
169 // |url_request_context| is used to read contents in |blob_url|.
170 virtual void Write(const net::URLRequestContext* url_request_context,
[email protected]949f25a2012-06-27 01:53:09171 const FileSystemURL& path,
[email protected]7546c9e2011-12-16 11:31:43172 const GURL& blob_url,
[email protected]81f8a3d2012-02-14 08:40:37173 int64 offset,
174 const WriteCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43175
176 // Truncates a file at |path| to |length|. If |length| is larger than
177 // the original file size, the file will be extended, and the extended
178 // part is filled with null bytes.
[email protected]949f25a2012-06-27 01:53:09179 virtual void Truncate(const FileSystemURL& path, int64 length,
[email protected]81f8a3d2012-02-14 08:40:37180 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43181
[email protected]60f60f82012-01-11 10:26:10182 // Tries to cancel the current operation [we support cancelling write or
183 // truncate only]. Reports failure for the current operation, then reports
184 // success for the cancel operation itself via the |cancel_dispatcher|.
185 //
186 // E.g. a typical cancel implementation would look like:
187 //
188 // virtual void SomeOperationImpl::Cancel(
[email protected]81f8a3d2012-02-14 08:40:37189 // const StatusCallback& cancel_callback) {
[email protected]60f60f82012-01-11 10:26:10190 // // Abort the current inflight operation first.
191 // ...
192 //
[email protected]81f8a3d2012-02-14 08:40:37193 // // Dispatch ABORT error for the current operation by invoking
194 // // the callback function for the ongoing operation,
195 // operation_callback.Run(base::PLATFORM_FILE_ERROR_ABORT, ...);
[email protected]60f60f82012-01-11 10:26:10196 //
197 // // Dispatch 'success' for the cancel (or dispatch appropriate
198 // // error code with DidFail() if the cancel has somehow failed).
[email protected]81f8a3d2012-02-14 08:40:37199 // cancel_callback.Run(base::PLATFORM_FILE_OK);
[email protected]60f60f82012-01-11 10:26:10200 // }
201 //
[email protected]81f8a3d2012-02-14 08:40:37202 // Note that, for reporting failure, the callback function passed to a
203 // cancellable operations are kept around with the operation instance
204 // (as |operation_callback_| in the code example).
205 virtual void Cancel(const StatusCallback& cancel_callback) = 0;
[email protected]60f60f82012-01-11 10:26:10206
[email protected]7546c9e2011-12-16 11:31:43207 // Modifies timestamps of a file or directory at |path| with
208 // |last_access_time| and |last_modified_time|. The function DOES NOT
209 // create a file unlike 'touch' command on Linux.
210 //
211 // This function is used only by Pepper as of writing.
[email protected]949f25a2012-06-27 01:53:09212 virtual void TouchFile(const FileSystemURL& path,
[email protected]7546c9e2011-12-16 11:31:43213 const base::Time& last_access_time,
[email protected]81f8a3d2012-02-14 08:40:37214 const base::Time& last_modified_time,
215 const StatusCallback& callback) = 0;
[email protected]7546c9e2011-12-16 11:31:43216
217 // Opens a file at |path| with |file_flags|, where flags are OR'ed
218 // values of base::PlatformFileFlags.
219 //
220 // |peer_handle| is the process handle of a pepper plugin process, which
221 // is necessary for underlying IPC calls with Pepper plugins.
222 //
223 // This function is used only by Pepper as of writing.
[email protected]949f25a2012-06-27 01:53:09224 virtual void OpenFile(const FileSystemURL& path,
[email protected]81f8a3d2012-02-14 08:40:37225 int file_flags,
226 base::ProcessHandle peer_handle,
227 const OpenFileCallback& callback) = 0;
[email protected]3eb080d2012-01-16 04:20:06228
[email protected]549d087a2012-07-10 09:03:36229 // Notifies a file at |path| opened by OpenFile is closed in plugin process.
230 // File system will run some cleanup task such as uploading the modified file
231 // content to a remote storage.
232 //
233 // This function is used only by Pepper as of writing.
234 virtual void NotifyCloseFile(const FileSystemURL& path) = 0;
235
[email protected]3eb080d2012-01-16 04:20:06236 // For downcasting to FileSystemOperation.
237 // TODO(kinuko): this hack should go away once appropriate upload-stream
238 // handling based on element types is supported.
[email protected]02a60542012-07-24 20:05:33239 virtual LocalFileSystemOperation* AsLocalFileSystemOperation() = 0;
[email protected]bf48bed2012-02-24 20:49:07240
[email protected]47373872012-02-28 06:27:30241 // Creates a local snapshot file for a given |path| and returns the
242 // metadata and platform path of the snapshot file via |callback|.
243 // In local filesystem cases the implementation may simply return
244 // the metadata of the file itself (as well as GetMetadata does),
245 // while in remote filesystem case the backend may want to download the file
246 // into a temporary snapshot file and return the metadata of the
247 // temporary file. Or if the implementaiton already has the local cache
248 // data for |path| it can simply return the path to the cache.
[email protected]949f25a2012-06-27 01:53:09249 virtual void CreateSnapshotFile(const FileSystemURL& path,
[email protected]47373872012-02-28 06:27:30250 const SnapshotFileCallback& callback) = 0;
251
[email protected]bf48bed2012-02-24 20:49:07252 protected:
253 // Used only for internal assertions.
254 enum OperationType {
255 kOperationNone,
256 kOperationCreateFile,
257 kOperationCreateDirectory,
[email protected]823096a2012-03-14 05:34:57258 kOperationCreateSnapshotFile,
[email protected]bf48bed2012-02-24 20:49:07259 kOperationCopy,
260 kOperationMove,
261 kOperationDirectoryExists,
262 kOperationFileExists,
263 kOperationGetMetadata,
264 kOperationReadDirectory,
265 kOperationRemove,
266 kOperationWrite,
267 kOperationTruncate,
268 kOperationTouchFile,
269 kOperationOpenFile,
[email protected]549d087a2012-07-10 09:03:36270 kOperationCloseFile,
[email protected]bf48bed2012-02-24 20:49:07271 kOperationGetLocalPath,
272 kOperationCancel,
273 };
[email protected]7546c9e2011-12-16 11:31:43274};
275
276} // namespace fileapi
277
278#endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_