1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef WEBKIT_FILEAPI_LOCAL_FILE_SYSTEM_OPERATION_H_
#define WEBKIT_FILEAPI_LOCAL_FILE_SYSTEM_OPERATION_H_
#include <vector>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "webkit/fileapi/file_snapshot_policy.h"
#include "webkit/fileapi/file_system_file_util.h"
#include "webkit/fileapi/file_system_operation.h"
#include "webkit/fileapi/file_system_operation_context.h"
#include "webkit/fileapi/file_system_url.h"
#include "webkit/fileapi/file_writer_delegate.h"
#include "webkit/quota/quota_types.h"
#include "webkit/storage/webkit_storage_export.h"
namespace chromeos {
class CrosMountPointProvider;
}
namespace fileapi {
class AsyncFileUtil;
class FileSystemContext;
class RecursiveOperationDelegate;
// FileSystemOperation implementation for local file systems.
class WEBKIT_STORAGE_EXPORT LocalFileSystemOperation
: public NON_EXPORTED_BASE(FileSystemOperation) {
public:
virtual ~LocalFileSystemOperation();
// FileSystemOperation overrides.
virtual void CreateFile(const FileSystemURL& url,
bool exclusive,
const StatusCallback& callback) OVERRIDE;
virtual void CreateDirectory(const FileSystemURL& url,
bool exclusive,
bool recursive,
const StatusCallback& callback) OVERRIDE;
virtual void Copy(const FileSystemURL& src_url,
const FileSystemURL& dest_url,
const StatusCallback& callback) OVERRIDE;
virtual void Move(const FileSystemURL& src_url,
const FileSystemURL& dest_url,
const StatusCallback& callback) OVERRIDE;
virtual void DirectoryExists(const FileSystemURL& url,
const StatusCallback& callback) OVERRIDE;
virtual void FileExists(const FileSystemURL& url,
const StatusCallback& callback) OVERRIDE;
virtual void GetMetadata(const FileSystemURL& url,
const GetMetadataCallback& callback) OVERRIDE;
virtual void ReadDirectory(const FileSystemURL& url,
const ReadDirectoryCallback& callback) OVERRIDE;
virtual void Remove(const FileSystemURL& url, bool recursive,
const StatusCallback& callback) OVERRIDE;
virtual void Write(const net::URLRequestContext* url_request_context,
const FileSystemURL& url,
const GURL& blob_url,
int64 offset,
const WriteCallback& callback) OVERRIDE;
virtual void Truncate(const FileSystemURL& url, int64 length,
const StatusCallback& callback) OVERRIDE;
virtual void TouchFile(const FileSystemURL& url,
const base::Time& last_access_time,
const base::Time& last_modified_time,
const StatusCallback& callback) OVERRIDE;
virtual void OpenFile(const FileSystemURL& url,
int file_flags,
base::ProcessHandle peer_handle,
const OpenFileCallback& callback) OVERRIDE;
virtual void NotifyCloseFile(const FileSystemURL& url) OVERRIDE;
virtual void Cancel(const StatusCallback& cancel_callback) OVERRIDE;
virtual LocalFileSystemOperation* AsLocalFileSystemOperation() OVERRIDE;
virtual void CreateSnapshotFile(
const FileSystemURL& path,
const SnapshotFileCallback& callback) OVERRIDE;
// Copies in a single file from a different filesystem.
//
// This returns:
// - PLATFORM_FILE_ERROR_NOT_FOUND if |src_file_path|
// or the parent directory of |dest_url| does not exist.
// - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
// is not a file.
// - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
// its parent path is a file.
//
void CopyInForeignFile(const base::FilePath& src_local_disk_path,
const FileSystemURL& dest_url,
const StatusCallback& callback);
// Removes a single file.
//
// This returns:
// - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist.
// - PLATFORM_FILE_ERROR_NOT_A_FILE if |url| is not a file.
//
void RemoveFile(const FileSystemURL& url,
const StatusCallback& callback);
// Removes a single empty directory.
//
// This returns:
// - PLATFORM_FILE_ERROR_NOT_FOUND if |url| does not exist.
// - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY if |url| is not a directory.
// - PLATFORM_FILE_ERROR_NOT_EMPTY if |url| is not empty.
//
void RemoveDirectory(const FileSystemURL& url,
const StatusCallback& callback);
// Copies a file from |src_url| to |dest_url|.
// This must be called for files that belong to the same filesystem
// (i.e. type() and origin() of the |src_url| and |dest_url| must match).
//
// This returns:
// - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url|
// or the parent directory of |dest_url| does not exist.
// - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
// - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
// is not a file.
// - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
// its parent path is a file.
//
void CopyFileLocal(const FileSystemURL& src_url,
const FileSystemURL& dest_url,
const StatusCallback& callback);
// Moves a local file from |src_url| to |dest_url|.
// This must be called for files that belong to the same filesystem
// (i.e. type() and origin() of the |src_url| and |dest_url| must match).
//
// This returns:
// - PLATFORM_FILE_ERROR_NOT_FOUND if |src_url|
// or the parent directory of |dest_url| does not exist.
// - PLATFORM_FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file.
// - PLATFORM_FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
// is not a file.
// - PLATFORM_FILE_ERROR_FAILED if |dest_url| does not exist and
// its parent path is a file.
//
void MoveFileLocal(const FileSystemURL& src_url,
const FileSystemURL& dest_url,
const StatusCallback& callback);
// Synchronously gets the platform path for the given |url|.
void SyncGetPlatformPath(const FileSystemURL& url, base::FilePath* platform_path);
private:
class ScopedUpdateNotifier;
enum SetUpMode {
SETUP_FOR_READ,
SETUP_FOR_WRITE,
SETUP_FOR_CREATE,
};
// Only MountPointProviders or testing class can create a
// new operation directly.
friend class FileSystemTestHelper;
friend class IsolatedMountPointProvider;
friend class SandboxMountPointProvider;
friend class TestMountPointProvider;
friend class chromeos::CrosMountPointProvider;
friend class LocalFileSystemOperationTest;
friend class LocalFileSystemOperationWriteTest;
friend class FileWriterDelegateTest;
friend class FileSystemQuotaTest;
friend class LocalFileSystemTestOriginHelper;
friend class RecursiveOperationDelegate;
friend class CrossOperationDelegate;
friend class SyncableFileSystemOperation;
LocalFileSystemOperation(
FileSystemContext* file_system_context,
scoped_ptr<FileSystemOperationContext> operation_context);
FileSystemContext* file_system_context() const {
return file_system_context_;
}
FileSystemOperationContext* operation_context() const {
if (overriding_operation_context_)
return overriding_operation_context_;
return operation_context_.get();
}
// Queries the quota and usage and then runs the given |task|.
// If an error occurs during the quota query it runs |error_callback| instead.
void GetUsageAndQuotaThenRunTask(
const FileSystemURL& url,
const base::Closure& task,
const base::Closure& error_callback);
// Called after the quota info is obtained from the quota manager
// (which is triggered by GetUsageAndQuotaThenRunTask).
// Sets the quota info in the operation_context_ and then runs the given
// |task| if the returned quota status is successful, otherwise runs
// |error_callback|.
void DidGetUsageAndQuotaAndRunTask(
const base::Closure& task,
const base::Closure& error_callback,
quota::QuotaStatusCode status,
int64 usage, int64 quota);
// returns a closure which actually perform the write operation.
base::Closure GetWriteClosure(
const net::URLRequestContext* url_request_context,
const FileSystemURL& url,
const GURL& blob_url,
int64 offset,
const WriteCallback& callback);
void DidFailWrite(
const WriteCallback& callback,
base::PlatformFileError result);
// The 'body' methods that perform the actual work (i.e. posting the
// file task on proxy_) after the quota check.
void DoCreateFile(const FileSystemURL& url,
const StatusCallback& callback, bool exclusive);
void DoCreateDirectory(const FileSystemURL& url,
const StatusCallback& callback,
bool exclusive,
bool recursive);
void DoCopyFileLocal(const FileSystemURL& src,
const FileSystemURL& dest,
const StatusCallback& callback);
void DoMoveFileLocal(const FileSystemURL& src,
const FileSystemURL& dest,
const StatusCallback& callback);
void DoCopyInForeignFile(const base::FilePath& src_local_disk_file_path,
const FileSystemURL& dest,
const StatusCallback& callback);
void DoTruncate(const FileSystemURL& url,
const StatusCallback& callback, int64 length);
void DoOpenFile(const FileSystemURL& url,
const OpenFileCallback& callback, int file_flags);
// Callback for CreateFile for |exclusive|=true cases.
void DidEnsureFileExistsExclusive(const StatusCallback& callback,
base::PlatformFileError rv,
bool created);
// Callback for CreateFile for |exclusive|=false cases.
void DidEnsureFileExistsNonExclusive(const StatusCallback& callback,
base::PlatformFileError rv,
bool created);
// Generic callback that translates platform errors to WebKit error codes.
void DidFinishFileOperation(const StatusCallback& callback,
base::PlatformFileError rv);
// Generic callback when we delegated the operation.
void DidFinishDelegatedOperation(const StatusCallback& callback,
base::PlatformFileError rv);
void DidDirectoryExists(const StatusCallback& callback,
base::PlatformFileError rv,
const base::PlatformFileInfo& file_info,
const base::FilePath& unused);
void DidFileExists(const StatusCallback& callback,
base::PlatformFileError rv,
const base::PlatformFileInfo& file_info,
const base::FilePath& unused);
void DidGetMetadata(const GetMetadataCallback& callback,
base::PlatformFileError rv,
const base::PlatformFileInfo& file_info,
const base::FilePath& platform_path);
void DidReadDirectory(const ReadDirectoryCallback& callback,
base::PlatformFileError rv,
const std::vector<base::FileUtilProxy::Entry>& entries,
bool has_more);
void DidWrite(const FileSystemURL& url,
base::PlatformFileError rv,
int64 bytes,
FileWriterDelegate::WriteProgressStatus write_status);
void DidTouchFile(const StatusCallback& callback,
base::PlatformFileError rv);
void DidOpenFile(const OpenFileCallback& callback,
base::PlatformFileError rv,
base::PassPlatformFile file,
bool created);
void DidCreateSnapshotFile(
const SnapshotFileCallback& callback,
base::PlatformFileError rv,
const base::PlatformFileInfo& file_info,
const base::FilePath& platform_path,
SnapshotFilePolicy snapshot_policy);
// Checks the validity of a given |url| and populates |file_util| for |mode|.
base::PlatformFileError SetUp(
const FileSystemURL& url,
SetUpMode mode);
// Used only for internal assertions.
// Returns false if there's another inflight pending operation.
bool SetPendingOperationType(OperationType type);
// Overrides this operation's operation context by given |context|.
// This operation won't own |context| and the |context| needs to outlive
// this operation.
//
// Called only from operation delegates when they create sub-operations
// for performing a recursive operation.
void set_overriding_operation_context(FileSystemOperationContext* context) {
overriding_operation_context_ = context;
}
// Sets a termination callback which is called when this instance goes away
// (indicates the operation is finished).
void set_termination_callback(const base::Closure& closure) {
termination_callback_ = closure;
}
scoped_refptr<FileSystemContext> file_system_context_;
scoped_ptr<FileSystemOperationContext> operation_context_;
AsyncFileUtil* async_file_util_; // Not owned.
FileSystemOperationContext* overriding_operation_context_;
// A callback that is called when this instance goes away.
base::Closure termination_callback_;
// This is set before any write operations to dispatch
// FileUpdateObserver::StartUpdate and FileUpdateObserver::EndUpdate.
ScopedVector<ScopedUpdateNotifier> scoped_update_notifiers_;
// These are all used only by Write().
friend class FileWriterDelegate;
scoped_ptr<FileWriterDelegate> file_writer_delegate_;
scoped_ptr<RecursiveOperationDelegate> recursive_operation_delegate_;
// write_callback is kept in this class for so that we can dispatch it when
// the operation is cancelled. calcel_callback is kept for canceling a
// Truncate() operation. We can't actually stop Truncate in another thread;
// after it resumed from the working thread, cancellation takes place.
WriteCallback write_callback_;
StatusCallback cancel_callback_;
// Used only by OpenFile, in order to clone the file handle back to the
// requesting process.
base::ProcessHandle peer_handle_;
// A flag to make sure we call operation only once per instance.
OperationType pending_operation_;
// LocalFileSystemOperation instance is usually deleted upon completion but
// could be deleted while it has inflight callbacks when Cancel is called.
base::WeakPtrFactory<LocalFileSystemOperation> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperation);
};
} // namespace fileapi
#endif // WEBKIT_FILEAPI_LOCAL_FILE_SYSTEM_OPERATION_H_
|