Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(870)

Side by Side Diff: webkit/fileapi/file_system_operation_interface.h

Issue 9372044: Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reflected kinuko's comments + Fixture for failing-Write -> Cancel pattern. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ 5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ 6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_
7 7
8 #include "base/file_util_proxy.h"
9 #include "base/platform_file.h"
8 #include "base/process.h" 10 #include "base/process.h"
9 11
10 namespace base { 12 namespace base {
11 class Time; 13 class Time;
12 } // namespace base 14 } // namespace base
13 15
14 namespace net { 16 namespace net {
15 class URLRequest; 17 class URLRequest;
16 class URLRequestContext; 18 class URLRequestContext;
17 } // namespace net 19 } // namespace net
18 20
19 class GURL; 21 class GURL;
20 22
21 namespace fileapi { 23 namespace fileapi {
22 24
23 class FileSystemCallbackDispatcher;
24 class FileSystemOperation; 25 class FileSystemOperation;
25 26
26 // The interface class for FileSystemOperation implementations. 27 // The interface class for FileSystemOperation implementations.
27 // 28 //
28 // This interface defines file system operations required to implement 29 // This interface defines file system operations required to implement
29 // "File API: Directories and System" 30 // "File API: Directories and System"
30 // http://www.w3.org/TR/file-system-api/ 31 // http://www.w3.org/TR/file-system-api/
31 // 32 //
32 // DESIGN NOTES 33 // DESIGN NOTES
33 // 34 //
34 // This class is designed to 35 // This class is designed to
35 // 36 //
36 // 1) Serve one-time file system operation per instance. Only one 37 // 1) Serve one-time file system operation per instance. Only one
37 // method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists, 38 // method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists,
38 // GetMetadata, ReadDirectory and Remove) may be called during the 39 // GetMetadata, ReadDirectory and Remove) may be called during the
39 // lifetime of this object and it should be called no more than once. 40 // lifetime of this object and it should be called no more than once.
40 // 41 //
41 // 2) Be self-destructed, or get deleted via base::Owned() after the 42 // 2) Be self-destructed, or get deleted via base::Owned() after the
42 // operation finishes and completion callback is called. 43 // operation finishes and completion callback is called.
43 // 44 //
44 // 3) Deliver the results of operations to the client via 45 // 3) Deliver the results of operations to the client via the callback function
45 // FileSystemCallbackDispatcher. 46 // passed as the last parameter of the method.
46 // TODO(kinuko): Change the functions to take callbacks instead.
47 // 47 //
48 class FileSystemOperationInterface { 48 class FileSystemOperationInterface {
49 public: 49 public:
50 virtual ~FileSystemOperationInterface() {} 50 virtual ~FileSystemOperationInterface() {}
51 51
52 // Used for CreateFile(), etc. |result| is the return code of the operation.
53 typedef base::Callback<void(base::PlatformFileError result)> StatusCallback;
54
55 // Used for GetMetadata(). |result| is the return code of the operation,
56 // |file_info| is the obtained file info, and |platform_path| is the path
57 // of the file.
58 typedef base::Callback<void(
59 base::PlatformFileError result,
60 const base::PlatformFileInfo& file_info,
61 const FilePath& platform_path)> GetMetadataCallback;
62
63 // Used for OpenFile(). |result| is the return code of the operation.
64 typedef base::Callback<void(
65 base::PlatformFileError result,
66 base::PlatformFile file,
67 base::ProcessHandle peer_handle)> OpenFileCallback;
68
69 // Used for ReadDirectory(). |result| is the return code of the operation,
70 // |file_list| is the list of files read, and |has_more| is true if some files
71 // are yet to be read.
72 typedef base::Callback<void(
73 base::PlatformFileError result,
74 const std::vector<base::FileUtilProxy::Entry>& file_list,
75 bool has_more)> ReadDirectoryCallback;
76
77 // Used for Write().
78 typedef base::Callback<void(base::PlatformFileError result,
79 int64 bytes,
80 bool complete)> WriteCallback;
81
52 // Creates a file at |path|. If |exclusive| is true, an error is raised 82 // Creates a file at |path|. If |exclusive| is true, an error is raised
53 // in case a file is already present at the URL. 83 // in case a file is already present at the URL.
54 virtual void CreateFile(const GURL& path, 84 virtual void CreateFile(const GURL& path,
55 bool exclusive) = 0; 85 bool exclusive,
86 const StatusCallback& callback) = 0;
56 87
57 // Creates a directory at |path|. If |exclusive| is true, an error is 88 // Creates a directory at |path|. If |exclusive| is true, an error is
58 // raised in case a directory is already present at the URL. If 89 // raised in case a directory is already present at the URL. If
59 // |recursive| is true, create parent directories as needed just like 90 // |recursive| is true, create parent directories as needed just like
60 // mkdir -p does. 91 // mkdir -p does.
61 virtual void CreateDirectory(const GURL& path, 92 virtual void CreateDirectory(const GURL& path,
62 bool exclusive, 93 bool exclusive,
63 bool recursive) = 0; 94 bool recursive,
95 const StatusCallback& callback) = 0;
64 96
65 // Copes a file or directory from |src_path| to |dest_path|. If 97 // Copes a file or directory from |src_path| to |dest_path|. If
66 // |src_path| is a directory, the contents of |src_path| are copied to 98 // |src_path| is a directory, the contents of |src_path| are copied to
67 // |dest_path| recursively. A new file or directory is created at 99 // |dest_path| recursively. A new file or directory is created at
68 // |dest_path| as needed. 100 // |dest_path| as needed.
69 virtual void Copy(const GURL& src_path, 101 virtual void Copy(const GURL& src_path,
70 const GURL& dest_path) = 0; 102 const GURL& dest_path,
103 const StatusCallback& callback) = 0;
71 104
72 // Moves a file or directory from |src_path| to |dest_path|. A new file 105 // Moves a file or directory from |src_path| to |dest_path|. A new file
73 // or directory is created at |dest_path| as needed. 106 // or directory is created at |dest_path| as needed.
74 virtual void Move(const GURL& src_path, 107 virtual void Move(const GURL& src_path,
75 const GURL& dest_path) = 0; 108 const GURL& dest_path,
109 const StatusCallback& callback) = 0;
76 110
77 // Checks if a directory is present at |path|. 111 // Checks if a directory is present at |path|.
78 virtual void DirectoryExists(const GURL& path) = 0; 112 virtual void DirectoryExists(const GURL& path,
113 const StatusCallback& callback) = 0;
79 114
80 // Checks if a file is present at |path|. 115 // Checks if a file is present at |path|.
81 virtual void FileExists(const GURL& path) = 0; 116 virtual void FileExists(const GURL& path,
117 const StatusCallback& callback) = 0;
82 118
83 // Gets the metadata of a file or directory at |path|. 119 // Gets the metadata of a file or directory at |path|.
84 virtual void GetMetadata(const GURL& path) = 0; 120 virtual void GetMetadata(const GURL& path,
121 const GetMetadataCallback& callback) = 0;
85 122
86 // Reads contents of a directory at |path|. 123 // Reads contents of a directory at |path|.
87 virtual void ReadDirectory(const GURL& path) = 0; 124 virtual void ReadDirectory(const GURL& path,
125 const ReadDirectoryCallback& callback) = 0;
88 126
89 // Removes a file or directory at |path|. If |recursive| is true, remove 127 // Removes a file or directory at |path|. If |recursive| is true, remove
90 // all files and directories under the directory at |path| recursively. 128 // all files and directories under the directory at |path| recursively.
91 virtual void Remove(const GURL& path, bool recursive) = 0; 129 virtual void Remove(const GURL& path, bool recursive,
130 const StatusCallback& callback) = 0;
92 131
93 // Writes contents of |blob_url| to |path| at |offset|. 132 // Writes contents of |blob_url| to |path| at |offset|.
94 // |url_request_context| is used to read contents in |blob_url|. 133 // |url_request_context| is used to read contents in |blob_url|.
95 virtual void Write(const net::URLRequestContext* url_request_context, 134 virtual void Write(const net::URLRequestContext* url_request_context,
96 const GURL& path, 135 const GURL& path,
97 const GURL& blob_url, 136 const GURL& blob_url,
98 int64 offset) = 0; 137 int64 offset,
138 const WriteCallback& callback) = 0;
99 139
100 // Truncates a file at |path| to |length|. If |length| is larger than 140 // Truncates a file at |path| to |length|. If |length| is larger than
101 // the original file size, the file will be extended, and the extended 141 // the original file size, the file will be extended, and the extended
102 // part is filled with null bytes. 142 // part is filled with null bytes.
103 virtual void Truncate(const GURL& path, int64 length) = 0; 143 virtual void Truncate(const GURL& path, int64 length,
144 const StatusCallback& callback) = 0;
104 145
105 // Tries to cancel the current operation [we support cancelling write or 146 // Tries to cancel the current operation [we support cancelling write or
106 // truncate only]. Reports failure for the current operation, then reports 147 // truncate only]. Reports failure for the current operation, then reports
107 // success for the cancel operation itself via the |cancel_dispatcher|. 148 // success for the cancel operation itself via the |cancel_dispatcher|.
108 // 149 //
109 // E.g. a typical cancel implementation would look like: 150 // E.g. a typical cancel implementation would look like:
110 // 151 //
111 // virtual void SomeOperationImpl::Cancel( 152 // virtual void SomeOperationImpl::Cancel(
112 // scoped_ptr<FileSystemCallbackDispatcher> cancel_dispatcher) { 153 // const StatusCallback& cancel_callback) {
113 // // Abort the current inflight operation first. 154 // // Abort the current inflight operation first.
114 // ... 155 // ...
115 // 156 //
116 // // Dispatch ABORT error for the current operation by calling 157 // // Dispatch ABORT error for the current operation by invoking
117 // // DidFail() callback of the dispatcher attached to this operation. 158 // // the callback function for the ongoing operation,
118 // // (dispatcher_ in this example) 159 // operation_callback.Run(base::PLATFORM_FILE_ERROR_ABORT, ...);
119 // dispatcher_->DidFail(base::PLATFORM_FILE_ERROR_ABORT);
120 // 160 //
121 // // Dispatch 'success' for the cancel (or dispatch appropriate 161 // // Dispatch 'success' for the cancel (or dispatch appropriate
122 // // error code with DidFail() if the cancel has somehow failed). 162 // // error code with DidFail() if the cancel has somehow failed).
123 // cancel_dispatcher->DidSucceed(); 163 // cancel_callback.Run(base::PLATFORM_FILE_OK);
124 // } 164 // }
125 // 165 //
126 virtual void Cancel( 166 // Note that, for reporting failure, the callback function passed to a
127 scoped_ptr<FileSystemCallbackDispatcher> cancel_dispatcher) = 0; 167 // cancellable operations are kept around with the operation instance
168 // (as |operation_callback_| in the code example).
169 virtual void Cancel(const StatusCallback& cancel_callback) = 0;
128 170
129 // Modifies timestamps of a file or directory at |path| with 171 // Modifies timestamps of a file or directory at |path| with
130 // |last_access_time| and |last_modified_time|. The function DOES NOT 172 // |last_access_time| and |last_modified_time|. The function DOES NOT
131 // create a file unlike 'touch' command on Linux. 173 // create a file unlike 'touch' command on Linux.
132 // 174 //
133 // This function is used only by Pepper as of writing. 175 // This function is used only by Pepper as of writing.
134 virtual void TouchFile(const GURL& path, 176 virtual void TouchFile(const GURL& path,
135 const base::Time& last_access_time, 177 const base::Time& last_access_time,
136 const base::Time& last_modified_time) = 0; 178 const base::Time& last_modified_time,
179 const StatusCallback& callback) = 0;
137 180
138 // Opens a file at |path| with |file_flags|, where flags are OR'ed 181 // Opens a file at |path| with |file_flags|, where flags are OR'ed
139 // values of base::PlatformFileFlags. 182 // values of base::PlatformFileFlags.
140 // 183 //
141 // |peer_handle| is the process handle of a pepper plugin process, which 184 // |peer_handle| is the process handle of a pepper plugin process, which
142 // is necessary for underlying IPC calls with Pepper plugins. 185 // is necessary for underlying IPC calls with Pepper plugins.
143 // 186 //
144 // This function is used only by Pepper as of writing. 187 // This function is used only by Pepper as of writing.
145 virtual void OpenFile( 188 virtual void OpenFile(const GURL& path,
146 const GURL& path, 189 int file_flags,
147 int file_flags, 190 base::ProcessHandle peer_handle,
148 base::ProcessHandle peer_handle) = 0; 191 const OpenFileCallback& callback) = 0;
149 192
150 // For downcasting to FileSystemOperation. 193 // For downcasting to FileSystemOperation.
151 // TODO(kinuko): this hack should go away once appropriate upload-stream 194 // TODO(kinuko): this hack should go away once appropriate upload-stream
152 // handling based on element types is supported. 195 // handling based on element types is supported.
153 virtual FileSystemOperation* AsFileSystemOperation() = 0; 196 virtual FileSystemOperation* AsFileSystemOperation() = 0;
154 }; 197 };
155 198
156 } // namespace fileapi 199 } // namespace fileapi
157 200
158 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_ 201 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_INTERFACE_H_
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_operation.cc ('k') | webkit/fileapi/file_system_operation_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698