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

Side by Side Diff: webkit/fileapi/file_system_operation.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_H_ 5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_ 6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 21 matching lines...) Expand all
32 32
33 namespace net { 33 namespace net {
34 class URLRequest; 34 class URLRequest;
35 class URLRequestContext; 35 class URLRequestContext;
36 } // namespace net 36 } // namespace net
37 37
38 class GURL; 38 class GURL;
39 39
40 namespace fileapi { 40 namespace fileapi {
41 41
42 class FileSystemCallbackDispatcher;
43 class FileSystemContext; 42 class FileSystemContext;
44 class FileWriterDelegate; 43 class FileWriterDelegate;
45 class FileSystemOperationTest; 44 class FileSystemOperationTest;
46 45
47 // FileSystemOperation implementation for local file systems. 46 // FileSystemOperation implementation for local file systems.
48 class FileSystemOperation : public FileSystemOperationInterface { 47 class FileSystemOperation : public FileSystemOperationInterface {
49 public: 48 public:
50 virtual ~FileSystemOperation(); 49 virtual ~FileSystemOperation();
51 50
52 // FileSystemOperation overrides. 51 // FileSystemOperation overrides.
53 virtual void CreateFile(const GURL& path, 52 virtual void CreateFile(const GURL& path,
54 bool exclusive) OVERRIDE; 53 bool exclusive,
54 const StatusCallback& callback) OVERRIDE;
55 virtual void CreateDirectory(const GURL& path, 55 virtual void CreateDirectory(const GURL& path,
56 bool exclusive, 56 bool exclusive,
57 bool recursive) OVERRIDE; 57 bool recursive,
58 const StatusCallback& callback) OVERRIDE;
58 virtual void Copy(const GURL& src_path, 59 virtual void Copy(const GURL& src_path,
59 const GURL& dest_path) OVERRIDE; 60 const GURL& dest_path,
61 const StatusCallback& callback) OVERRIDE;
60 virtual void Move(const GURL& src_path, 62 virtual void Move(const GURL& src_path,
61 const GURL& dest_path) OVERRIDE; 63 const GURL& dest_path,
62 virtual void DirectoryExists(const GURL& path) OVERRIDE; 64 const StatusCallback& callback) OVERRIDE;
63 virtual void FileExists(const GURL& path) OVERRIDE; 65 virtual void DirectoryExists(const GURL& path,
64 virtual void GetMetadata(const GURL& path) OVERRIDE; 66 const StatusCallback& callback) OVERRIDE;
65 virtual void ReadDirectory(const GURL& path) OVERRIDE; 67 virtual void FileExists(const GURL& path,
66 virtual void Remove(const GURL& path, bool recursive) OVERRIDE; 68 const StatusCallback& callback) OVERRIDE;
69 virtual void GetMetadata(const GURL& path,
70 const GetMetadataCallback& callback) OVERRIDE;
71 virtual void ReadDirectory(const GURL& path,
72 const ReadDirectoryCallback& callback) OVERRIDE;
73 virtual void Remove(const GURL& path, bool recursive,
74 const StatusCallback& callback) OVERRIDE;
67 virtual void Write(const net::URLRequestContext* url_request_context, 75 virtual void Write(const net::URLRequestContext* url_request_context,
68 const GURL& path, 76 const GURL& path,
69 const GURL& blob_url, 77 const GURL& blob_url,
70 int64 offset) OVERRIDE; 78 int64 offset,
71 virtual void Truncate(const GURL& path, int64 length) OVERRIDE; 79 const WriteCallback& callback) OVERRIDE;
80 virtual void Truncate(const GURL& path, int64 length,
81 const StatusCallback& callback) OVERRIDE;
72 virtual void TouchFile(const GURL& path, 82 virtual void TouchFile(const GURL& path,
73 const base::Time& last_access_time, 83 const base::Time& last_access_time,
74 const base::Time& last_modified_time) OVERRIDE; 84 const base::Time& last_modified_time,
75 virtual void OpenFile( 85 const StatusCallback& callback) OVERRIDE;
76 const GURL& path, 86 virtual void OpenFile(const GURL& path,
77 int file_flags, 87 int file_flags,
78 base::ProcessHandle peer_handle) OVERRIDE; 88 base::ProcessHandle peer_handle,
79 virtual void Cancel( 89 const OpenFileCallback& callback) OVERRIDE;
80 scoped_ptr<FileSystemCallbackDispatcher> cancel_dispatcher) OVERRIDE; 90 virtual void Cancel(const StatusCallback& cancel_callback) OVERRIDE;
81 virtual FileSystemOperation* AsFileSystemOperation() OVERRIDE; 91 virtual FileSystemOperation* AsFileSystemOperation() OVERRIDE;
82 92
83 // Synchronously gets the platform path for the given |path|. 93 // Synchronously gets the platform path for the given |path|.
84 void SyncGetPlatformPath(const GURL& path, FilePath* platform_path); 94 void SyncGetPlatformPath(const GURL& path, FilePath* platform_path);
85 95
86 private: 96 private:
87 class ScopedQuotaUtilHelper; 97 class ScopedQuotaUtilHelper;
88 98
89 // Only MountPointProviders or testing class can create a 99 // Only MountPointProviders or testing class can create a
90 // new operation directly. 100 // new operation directly.
91 friend class SandboxMountPointProvider; 101 friend class SandboxMountPointProvider;
92 friend class FileSystemTestHelper; 102 friend class FileSystemTestHelper;
93 friend class chromeos::CrosMountPointProvider; 103 friend class chromeos::CrosMountPointProvider;
94 104
95 FileSystemOperation(scoped_ptr<FileSystemCallbackDispatcher> dispatcher, 105 FileSystemOperation(scoped_refptr<base::MessageLoopProxy> proxy,
96 scoped_refptr<base::MessageLoopProxy> proxy,
97 FileSystemContext* file_system_context); 106 FileSystemContext* file_system_context);
98 107
99 FileSystemContext* file_system_context() const { 108 FileSystemContext* file_system_context() const {
100 return operation_context_.file_system_context(); 109 return operation_context_.file_system_context();
101 } 110 }
102 111
103 FileSystemOperationContext* file_system_operation_context() { 112 FileSystemOperationContext* file_system_operation_context() {
104 return &operation_context_; 113 return &operation_context_;
105 } 114 }
106 115
107 friend class FileSystemOperationTest; 116 friend class FileSystemOperationTest;
108 friend class FileSystemOperationWriteTest; 117 friend class FileSystemOperationWriteTest;
109 friend class FileWriterDelegateTest; 118 friend class FileWriterDelegateTest;
110 friend class FileSystemTestOriginHelper; 119 friend class FileSystemTestOriginHelper;
111 friend class FileSystemQuotaTest; 120 friend class FileSystemQuotaTest;
112 121
113 // The unit tests that need to specify and control the lifetime of the 122 // The unit tests that need to specify and control the lifetime of the
114 // file_util on their own should call this before performing the actual 123 // file_util on their own should call this before performing the actual
115 // operation. If it is given it will not be overwritten by the class. 124 // operation. If it is given it will not be overwritten by the class.
116 void set_override_file_util(FileSystemFileUtil* file_util) { 125 void set_override_file_util(FileSystemFileUtil* file_util) {
117 operation_context_.set_src_file_util(file_util); 126 operation_context_.set_src_file_util(file_util);
118 operation_context_.set_dest_file_util(file_util); 127 operation_context_.set_dest_file_util(file_util);
119 } 128 }
120 129
121 void GetUsageAndQuotaThenCallback( 130 void GetUsageAndQuotaThenCallback(
122 const GURL& origin_url, 131 const GURL& origin_url,
123 const quota::QuotaManager::GetUsageAndQuotaCallback& callback); 132 const quota::QuotaManager::GetUsageAndQuotaCallback& callback);
124 133
125 void DelayedCreateFileForQuota(bool exclusive, 134 void DelayedCreateFileForQuota(const StatusCallback& callback,
135 bool exclusive,
126 quota::QuotaStatusCode status, 136 quota::QuotaStatusCode status,
127 int64 usage, int64 quota); 137 int64 usage, int64 quota);
128 void DelayedCreateDirectoryForQuota(bool exclusive, bool recursive, 138 void DelayedCreateDirectoryForQuota(const StatusCallback& callback,
139 bool exclusive, bool recursive,
129 quota::QuotaStatusCode status, 140 quota::QuotaStatusCode status,
130 int64 usage, int64 quota); 141 int64 usage, int64 quota);
131 void DelayedCopyForQuota(quota::QuotaStatusCode status, 142 void DelayedCopyForQuota(const StatusCallback& callback,
143 quota::QuotaStatusCode status,
132 int64 usage, int64 quota); 144 int64 usage, int64 quota);
133 void DelayedMoveForQuota(quota::QuotaStatusCode status, 145 void DelayedMoveForQuota(const StatusCallback& callback,
146 quota::QuotaStatusCode status,
134 int64 usage, int64 quota); 147 int64 usage, int64 quota);
135 void DelayedWriteForQuota(quota::QuotaStatusCode status, 148 void DelayedWriteForQuota(quota::QuotaStatusCode status,
136 int64 usage, int64 quota); 149 int64 usage, int64 quota);
137 void DelayedTruncateForQuota(int64 length, 150 void DelayedTruncateForQuota(const StatusCallback& callback,
151 int64 length,
138 quota::QuotaStatusCode status, 152 quota::QuotaStatusCode status,
139 int64 usage, int64 quota); 153 int64 usage, int64 quota);
140 void DelayedOpenFileForQuota(int file_flags, 154 void DelayedOpenFileForQuota(const OpenFileCallback& callback,
155 int file_flags,
141 quota::QuotaStatusCode status, 156 quota::QuotaStatusCode status,
142 int64 usage, int64 quota); 157 int64 usage, int64 quota);
143 158
144 // Callback for CreateFile for |exclusive|=true cases. 159 // Callback for CreateFile for |exclusive|=true cases.
145 void DidEnsureFileExistsExclusive(base::PlatformFileError rv, 160 void DidEnsureFileExistsExclusive(const StatusCallback& callback,
161 base::PlatformFileError rv,
146 bool created); 162 bool created);
147 163
148 // Callback for CreateFile for |exclusive|=false cases. 164 // Callback for CreateFile for |exclusive|=false cases.
149 void DidEnsureFileExistsNonExclusive(base::PlatformFileError rv, 165 void DidEnsureFileExistsNonExclusive(const StatusCallback& callback,
166 base::PlatformFileError rv,
150 bool created); 167 bool created);
151 168
152 // Generic callback that translates platform errors to WebKit error codes. 169 // Generic callback that translates platform errors to WebKit error codes.
153 void DidFinishFileOperation(base::PlatformFileError rv); 170 void DidFinishFileOperation(const StatusCallback& callback,
171 base::PlatformFileError rv);
154 172
155 void DidDirectoryExists(base::PlatformFileError rv, 173 void DidDirectoryExists(const StatusCallback& callback,
174 base::PlatformFileError rv,
156 const base::PlatformFileInfo& file_info, 175 const base::PlatformFileInfo& file_info,
157 const FilePath& unused); 176 const FilePath& unused);
158 void DidFileExists(base::PlatformFileError rv, 177 void DidFileExists(const StatusCallback& callback,
178 base::PlatformFileError rv,
159 const base::PlatformFileInfo& file_info, 179 const base::PlatformFileInfo& file_info,
160 const FilePath& unused); 180 const FilePath& unused);
161 void DidGetMetadata(base::PlatformFileError rv, 181 void DidGetMetadata(const GetMetadataCallback& callback,
182 base::PlatformFileError rv,
162 const base::PlatformFileInfo& file_info, 183 const base::PlatformFileInfo& file_info,
163 const FilePath& platform_path); 184 const FilePath& platform_path);
164 void DidReadDirectory( 185 void DidReadDirectory(
186 const ReadDirectoryCallback& callback,
165 base::PlatformFileError rv, 187 base::PlatformFileError rv,
166 const std::vector<base::FileUtilProxy::Entry>& entries); 188 const std::vector<base::FileUtilProxy::Entry>& entries);
167 void DidWrite( 189 void DidWrite(base::PlatformFileError rv,
168 base::PlatformFileError rv, 190 int64 bytes,
169 int64 bytes, 191 bool complete);
170 bool complete); 192 void DidTouchFile(const StatusCallback& callback,
171 void DidTouchFile(base::PlatformFileError rv); 193 base::PlatformFileError rv);
172 void DidOpenFile( 194 void DidOpenFile(const OpenFileCallback& callback,
173 base::PlatformFileError rv, 195 base::PlatformFileError rv,
174 base::PassPlatformFile file, 196 base::PassPlatformFile file,
175 bool created); 197 bool created);
176 198
177 // Helper for Write(). 199 // Helper for Write().
178 void OnFileOpenedForWrite( 200 void OnFileOpenedForWrite(base::PlatformFileError rv,
179 base::PlatformFileError rv, 201 base::PassPlatformFile file,
180 base::PassPlatformFile file, 202 bool created);
181 bool created);
182 203
183 // Checks the validity of a given |path| for reading, cracks the path into 204 // Checks the validity of a given |path| for reading, cracks the path into
184 // root URL and virtual path components, and returns the correct 205 // root URL and virtual path components, and returns the correct
185 // FileSystemFileUtil subclass for this type. 206 // FileSystemFileUtil subclass for this type.
186 // Returns true if the given |path| is a valid FileSystem path. 207 // Returns true if the given |path| is a valid FileSystem path.
187 // Otherwise it calls dispatcher's DidFail method with 208 // Otherwise it calls dispatcher's DidFail method with
188 // PLATFORM_FILE_ERROR_SECURITY and returns false. 209 // PLATFORM_FILE_ERROR_SECURITY and returns false.
189 // (Note: this doesn't delete this when it calls DidFail and returns false; 210 // (Note: this doesn't delete this when it calls DidFail and returns false;
190 // it's the caller's responsibility.) 211 // it's the caller's responsibility.)
191 bool VerifyFileSystemPathForRead(const GURL& path, 212 base::PlatformFileError VerifyFileSystemPathForRead(
192 GURL* root_url, 213 const GURL& path,
193 FileSystemType* type, 214 GURL* root_url,
194 FilePath* virtual_path, 215 FileSystemType* type,
195 FileSystemFileUtil** file_util); 216 FilePath* virtual_path,
217 FileSystemFileUtil** file_util);
196 218
197 // Checks the validity of a given |path| for writing, cracks the path into 219 // Checks the validity of a given |path| for writing, cracks the path into
198 // root URL and virtual path components, and returns the correct 220 // root URL and virtual path components, and returns the correct
199 // FileSystemFileUtil subclass for this type. 221 // FileSystemFileUtil subclass for this type.
200 // Returns true if the given |path| is a valid FileSystem path, and 222 // Returns true if the given |path| is a valid FileSystem path, and
201 // its origin embedded in the path has the right to write. 223 // its origin embedded in the path has the right to write.
202 // Otherwise it fires dispatcher's DidFail method with 224 // Otherwise it fires dispatcher's DidFail method with
203 // PLATFORM_FILE_ERROR_SECURITY if the path is not valid for writing, 225 // PLATFORM_FILE_ERROR_SECURITY if the path is not valid for writing,
204 // or with PLATFORM_FILE_ERROR_NO_SPACE if the origin is not allowed to 226 // or with PLATFORM_FILE_ERROR_NO_SPACE if the origin is not allowed to
205 // write to the storage. 227 // write to the storage.
206 // In either case it returns false after firing DidFail. 228 // In either case it returns false after firing DidFail.
207 // If |create| flag is true this also checks if the |path| contains 229 // If |create| flag is true this also checks if the |path| contains
208 // any restricted names and chars. If it does, the call fires dispatcher's 230 // any restricted names and chars. If it does, the call fires dispatcher's
209 // DidFail with PLATFORM_FILE_ERROR_SECURITY and returns false. 231 // DidFail with PLATFORM_FILE_ERROR_SECURITY and returns false.
210 // (Note: this doesn't delete this when it calls DidFail and returns false; 232 // (Note: this doesn't delete this when it calls DidFail and returns false;
211 // it's the caller's responsibility.) 233 // it's the caller's responsibility.)
212 bool VerifyFileSystemPathForWrite(const GURL& path, 234 base::PlatformFileError VerifyFileSystemPathForWrite(
213 bool create, 235 const GURL& path,
214 GURL* root_url, 236 bool create,
215 FileSystemType* type, 237 GURL* root_url,
216 FilePath* virtual_path, 238 FileSystemType* type,
217 FileSystemFileUtil** file_util); 239 FilePath* virtual_path,
240 FileSystemFileUtil** file_util);
218 241
219 // Common internal routine for VerifyFileSystemPathFor{Read,Write}. 242 // Common internal routine for VerifyFileSystemPathFor{Read,Write}.
220 bool VerifyFileSystemPath(const GURL& path, 243 base::PlatformFileError VerifyFileSystemPath(const GURL& path,
221 GURL* root_url, 244 GURL* root_url,
222 FileSystemType* type, 245 FileSystemType* type,
223 FilePath* virtual_path, 246 FilePath* virtual_path,
224 FileSystemFileUtil** file_util); 247 FileSystemFileUtil** file_util);
225 248
226 // Setup*Context*() functions will call the appropriate VerifyFileSystem 249 // Setup*Context*() functions will call the appropriate VerifyFileSystem
227 // function and store the results to operation_context_ and 250 // function and store the results to operation_context_ and
228 // *_virtual_path_. 251 // *_virtual_path_.
229 // Return the result of VerifyFileSystem*(). 252 // Return the result of VerifyFileSystem*().
230 bool SetupSrcContextForRead(const GURL& path); 253 base::PlatformFileError SetupSrcContextForRead(const GURL& path);
231 bool SetupSrcContextForWrite(const GURL& path, bool create); 254 base::PlatformFileError SetupSrcContextForWrite(const GURL& path,
232 bool SetupDestContextForWrite(const GURL& path, bool create); 255 bool create);
256 base::PlatformFileError SetupDestContextForWrite(const GURL& path,
257 bool create);
233 258
234 #ifndef NDEBUG 259 #ifndef NDEBUG
235 enum OperationType { 260 enum OperationType {
236 kOperationNone, 261 kOperationNone,
237 kOperationCreateFile, 262 kOperationCreateFile,
238 kOperationCreateDirectory, 263 kOperationCreateDirectory,
239 kOperationCopy, 264 kOperationCopy,
240 kOperationMove, 265 kOperationMove,
241 kOperationDirectoryExists, 266 kOperationDirectoryExists,
242 kOperationFileExists, 267 kOperationFileExists,
243 kOperationGetMetadata, 268 kOperationGetMetadata,
244 kOperationReadDirectory, 269 kOperationReadDirectory,
245 kOperationRemove, 270 kOperationRemove,
246 kOperationWrite, 271 kOperationWrite,
247 kOperationTruncate, 272 kOperationTruncate,
248 kOperationTouchFile, 273 kOperationTouchFile,
249 kOperationOpenFile, 274 kOperationOpenFile,
250 kOperationGetLocalPath, 275 kOperationGetLocalPath,
251 kOperationCancel, 276 kOperationCancel,
252 }; 277 };
253 278
254 // A flag to make sure we call operation only once per instance. 279 // A flag to make sure we call operation only once per instance.
255 OperationType pending_operation_; 280 OperationType pending_operation_;
256 #endif 281 #endif
257 282
258 // Proxy for calling file_util_proxy methods. 283 // Proxy for calling file_util_proxy methods.
259 scoped_refptr<base::MessageLoopProxy> proxy_; 284 scoped_refptr<base::MessageLoopProxy> proxy_;
260 285
261 // This can be NULL if the operation is cancelled on the way.
262 scoped_ptr<FileSystemCallbackDispatcher> dispatcher_;
263
264 FileSystemOperationContext operation_context_; 286 FileSystemOperationContext operation_context_;
265 287
266 scoped_ptr<ScopedQuotaUtilHelper> quota_util_helper_; 288 scoped_ptr<ScopedQuotaUtilHelper> quota_util_helper_;
267 289
268 // These are all used only by Write(). 290 // These are all used only by Write().
269 friend class FileWriterDelegate; 291 friend class FileWriterDelegate;
270 scoped_ptr<FileWriterDelegate> file_writer_delegate_; 292 scoped_ptr<FileWriterDelegate> file_writer_delegate_;
271 scoped_ptr<net::URLRequest> blob_request_; 293 scoped_ptr<net::URLRequest> blob_request_;
272 scoped_ptr<FileSystemCallbackDispatcher> cancel_dispatcher_; 294
295 // write_callback is kept in this class for so that we can dispatch it when
296 // the operation is cancelled. calcel_callback is kept for canceling a
297 // Truncate() operation. We can't actually stop Truncate in another thread;
298 // after it resumed from the working thread, cancellation takes place.
299 WriteCallback write_callback_;
300 StatusCallback cancel_callback_;
301 void set_write_callback(const WriteCallback& write_callback) {
302 write_callback_ = write_callback;
303 }
273 304
274 // Used only by OpenFile, in order to clone the file handle back to the 305 // Used only by OpenFile, in order to clone the file handle back to the
275 // requesting process. 306 // requesting process.
276 base::ProcessHandle peer_handle_; 307 base::ProcessHandle peer_handle_;
277 308
278 // Used to keep a virtual path around while we check for quota. 309 // Used to keep a virtual path around while we check for quota.
279 // If an operation needs only one path, use src_virtual_path_, even if it's a 310 // If an operation needs only one path, use src_virtual_path_, even if it's a
280 // write. 311 // write.
281 FilePath src_virtual_path_; 312 FilePath src_virtual_path_;
282 FilePath dest_virtual_path_; 313 FilePath dest_virtual_path_;
283 314
284 DISALLOW_COPY_AND_ASSIGN(FileSystemOperation); 315 DISALLOW_COPY_AND_ASSIGN(FileSystemOperation);
285 }; 316 };
286 317
287 } // namespace fileapi 318 } // namespace fileapi
288 319
289 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_ 320 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_mount_point_provider.h ('k') | webkit/fileapi/file_system_operation.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698