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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
// 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.
#include "webkit/browser/fileapi/file_system_context.h"
#include "base/bind.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "googleurl/src/gurl.h"
#include "webkit/blob/file_stream_reader.h"
#include "webkit/browser/fileapi/copy_or_move_file_validator.h"
#include "webkit/browser/fileapi/external_mount_points.h"
#include "webkit/browser/fileapi/file_stream_writer.h"
#include "webkit/browser/fileapi/file_system_file_util.h"
#include "webkit/browser/fileapi/file_system_operation.h"
#include "webkit/browser/fileapi/file_system_options.h"
#include "webkit/browser/fileapi/file_system_quota_client.h"
#include "webkit/browser/fileapi/file_system_task_runners.h"
#include "webkit/browser/fileapi/file_system_url.h"
#include "webkit/browser/fileapi/isolated_context.h"
#include "webkit/browser/fileapi/isolated_mount_point_provider.h"
#include "webkit/browser/fileapi/mount_points.h"
#include "webkit/browser/fileapi/sandbox_mount_point_provider.h"
#include "webkit/browser/fileapi/syncable/local_file_change_tracker.h"
#include "webkit/browser/fileapi/syncable/local_file_sync_context.h"
#include "webkit/browser/fileapi/syncable/syncable_file_system_util.h"
#include "webkit/browser/fileapi/test_mount_point_provider.h"
#include "webkit/common/fileapi/file_system_util.h"
#include "webkit/quota/quota_manager.h"
#include "webkit/quota/special_storage_policy.h"
#if defined(OS_CHROMEOS)
#include "webkit/chromeos/fileapi/cros_mount_point_provider.h"
#endif
using quota::QuotaClient;
namespace fileapi {
namespace {
QuotaClient* CreateQuotaClient(
FileSystemContext* context,
bool is_incognito) {
return new FileSystemQuotaClient(context, is_incognito);
}
void DidOpenFileSystem(
const FileSystemContext::OpenFileSystemCallback& callback,
const GURL& filesystem_root,
const std::string& filesystem_name,
base::PlatformFileError error) {
callback.Run(error, filesystem_name, filesystem_root);
}
} // namespace
FileSystemContext::FileSystemContext(
scoped_ptr<FileSystemTaskRunners> task_runners,
ExternalMountPoints* external_mount_points,
quota::SpecialStoragePolicy* special_storage_policy,
quota::QuotaManagerProxy* quota_manager_proxy,
ScopedVector<FileSystemMountPointProvider> additional_providers,
const base::FilePath& partition_path,
const FileSystemOptions& options)
: task_runners_(task_runners.Pass()),
quota_manager_proxy_(quota_manager_proxy),
sandbox_provider_(
new SandboxMountPointProvider(
quota_manager_proxy,
task_runners_->file_task_runner(),
partition_path,
options,
special_storage_policy)),
isolated_provider_(new IsolatedMountPointProvider()),
additional_providers_(additional_providers.Pass()),
external_mount_points_(external_mount_points),
partition_path_(partition_path) {
DCHECK(task_runners_.get());
if (quota_manager_proxy) {
quota_manager_proxy->RegisterClient(CreateQuotaClient(
this, options.is_incognito()));
}
RegisterMountPointProvider(sandbox_provider_.get());
RegisterMountPointProvider(isolated_provider_.get());
#if defined(OS_CHROMEOS)
// TODO(kinuko): Move this out of webkit/fileapi layer.
DCHECK(external_mount_points);
external_provider_.reset(
new chromeos::CrosMountPointProvider(
special_storage_policy,
external_mount_points,
ExternalMountPoints::GetSystemInstance()));
RegisterMountPointProvider(external_provider_.get());
#endif
for (ScopedVector<FileSystemMountPointProvider>::const_iterator iter =
additional_providers_.begin();
iter != additional_providers_.end(); ++iter) {
RegisterMountPointProvider(*iter);
}
// Additional mount points must be added before regular system-wide
// mount points.
if (external_mount_points)
url_crackers_.push_back(external_mount_points);
url_crackers_.push_back(ExternalMountPoints::GetSystemInstance());
url_crackers_.push_back(IsolatedContext::GetInstance());
}
bool FileSystemContext::DeleteDataForOriginOnFileThread(
const GURL& origin_url) {
DCHECK(task_runners_->file_task_runner()->RunsTasksOnCurrentThread());
DCHECK(sandbox_provider());
DCHECK(origin_url == origin_url.GetOrigin());
// Delete temporary and persistent data.
return
(sandbox_provider()->DeleteOriginDataOnFileThread(
this, quota_manager_proxy(), origin_url,
kFileSystemTypeTemporary) ==
base::PLATFORM_FILE_OK) &&
(sandbox_provider()->DeleteOriginDataOnFileThread(
this, quota_manager_proxy(), origin_url,
kFileSystemTypePersistent) ==
base::PLATFORM_FILE_OK) &&
(sandbox_provider()->DeleteOriginDataOnFileThread(
this, quota_manager_proxy(), origin_url,
kFileSystemTypeSyncable) ==
base::PLATFORM_FILE_OK);
}
FileSystemQuotaUtil*
FileSystemContext::GetQuotaUtil(FileSystemType type) const {
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
if (!mount_point_provider)
return NULL;
return mount_point_provider->GetQuotaUtil();
}
AsyncFileUtil* FileSystemContext::GetAsyncFileUtil(
FileSystemType type) const {
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
if (!mount_point_provider)
return NULL;
return mount_point_provider->GetAsyncFileUtil(type);
}
FileSystemFileUtil* FileSystemContext::GetFileUtil(
FileSystemType type) const {
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
if (!mount_point_provider)
return NULL;
return mount_point_provider->GetFileUtil(type);
}
CopyOrMoveFileValidatorFactory*
FileSystemContext::GetCopyOrMoveFileValidatorFactory(
FileSystemType type, base::PlatformFileError* error_code) const {
DCHECK(error_code);
*error_code = base::PLATFORM_FILE_OK;
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
if (!mount_point_provider)
return NULL;
return mount_point_provider->GetCopyOrMoveFileValidatorFactory(
type, error_code);
}
FileSystemMountPointProvider* FileSystemContext::GetMountPointProvider(
FileSystemType type) const {
MountPointProviderMap::const_iterator found = provider_map_.find(type);
if (found != provider_map_.end())
return found->second;
NOTREACHED() << "Unknown filesystem type: " << type;
return NULL;
}
const UpdateObserverList* FileSystemContext::GetUpdateObservers(
FileSystemType type) const {
// Currently update observer is only available in SandboxMountPointProvider
// and TestMountPointProvider.
// TODO(kinuko): Probably GetUpdateObservers() virtual method should be
// added to FileSystemMountPointProvider interface and be called like
// other GetFoo() methods do.
if (SandboxMountPointProvider::IsSandboxType(type))
return sandbox_provider()->GetUpdateObservers(type);
if (type != kFileSystemTypeTest)
return NULL;
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
return static_cast<TestMountPointProvider*>(
mount_point_provider)->GetUpdateObservers(type);
}
SandboxMountPointProvider*
FileSystemContext::sandbox_provider() const {
return sandbox_provider_.get();
}
ExternalFileSystemMountPointProvider*
FileSystemContext::external_provider() const {
return external_provider_.get();
}
void FileSystemContext::OpenFileSystem(
const GURL& origin_url,
FileSystemType type,
bool create,
const OpenFileSystemCallback& callback) {
DCHECK(!callback.is_null());
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
if (!mount_point_provider) {
callback.Run(base::PLATFORM_FILE_ERROR_SECURITY, std::string(), GURL());
return;
}
GURL root_url = GetFileSystemRootURI(origin_url, type);
std::string name = GetFileSystemName(origin_url, type);
mount_point_provider->ValidateFileSystemRoot(
origin_url, type, create,
base::Bind(&DidOpenFileSystem, callback, root_url, name));
}
void FileSystemContext::OpenSyncableFileSystem(
const std::string& mount_name,
const GURL& origin_url,
FileSystemType type,
bool create,
const OpenFileSystemCallback& callback) {
DCHECK(!callback.is_null());
DCHECK(type == kFileSystemTypeSyncable);
GURL root_url = sync_file_system::GetSyncableFileSystemRootURI(
origin_url, mount_name);
std::string name = GetFileSystemName(origin_url, kFileSystemTypeSyncable);
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
DCHECK(mount_point_provider);
mount_point_provider->ValidateFileSystemRoot(
origin_url, type, create,
base::Bind(&DidOpenFileSystem, callback, root_url, name));
}
void FileSystemContext::DeleteFileSystem(
const GURL& origin_url,
FileSystemType type,
const DeleteFileSystemCallback& callback) {
DCHECK(origin_url == origin_url.GetOrigin());
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(type);
if (!mount_point_provider) {
callback.Run(base::PLATFORM_FILE_ERROR_SECURITY);
return;
}
mount_point_provider->DeleteFileSystem(origin_url, type, this, callback);
}
FileSystemOperation* FileSystemContext::CreateFileSystemOperation(
const FileSystemURL& url, base::PlatformFileError* error_code) {
if (!url.is_valid()) {
if (error_code)
*error_code = base::PLATFORM_FILE_ERROR_INVALID_URL;
return NULL;
}
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(url.type());
if (!mount_point_provider) {
if (error_code)
*error_code = base::PLATFORM_FILE_ERROR_FAILED;
return NULL;
}
base::PlatformFileError fs_error = base::PLATFORM_FILE_OK;
FileSystemOperation* operation =
mount_point_provider->CreateFileSystemOperation(url, this, &fs_error);
if (error_code)
*error_code = fs_error;
return operation;
}
scoped_ptr<webkit_blob::FileStreamReader>
FileSystemContext::CreateFileStreamReader(
const FileSystemURL& url,
int64 offset,
const base::Time& expected_modification_time) {
if (!url.is_valid())
return scoped_ptr<webkit_blob::FileStreamReader>();
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(url.type());
if (!mount_point_provider)
return scoped_ptr<webkit_blob::FileStreamReader>();
return mount_point_provider->CreateFileStreamReader(
url, offset, expected_modification_time, this);
}
scoped_ptr<FileStreamWriter> FileSystemContext::CreateFileStreamWriter(
const FileSystemURL& url,
int64 offset) {
if (!url.is_valid())
return scoped_ptr<FileStreamWriter>();
FileSystemMountPointProvider* mount_point_provider =
GetMountPointProvider(url.type());
if (!mount_point_provider)
return scoped_ptr<FileStreamWriter>();
return mount_point_provider->CreateFileStreamWriter(url, offset, this);
}
void FileSystemContext::SetLocalFileChangeTracker(
scoped_ptr<sync_file_system::LocalFileChangeTracker> tracker) {
DCHECK(!change_tracker_.get());
DCHECK(tracker.get());
change_tracker_ = tracker.Pass();
sandbox_provider_->AddSyncableFileUpdateObserver(
change_tracker_.get(),
task_runners_->file_task_runner());
sandbox_provider_->AddSyncableFileChangeObserver(
change_tracker_.get(),
task_runners_->file_task_runner());
}
void FileSystemContext::set_sync_context(
sync_file_system::LocalFileSyncContext* sync_context) {
sync_context_ = sync_context;
}
FileSystemURL FileSystemContext::CrackURL(const GURL& url) const {
return CrackFileSystemURL(FileSystemURL(url));
}
FileSystemURL FileSystemContext::CreateCrackedFileSystemURL(
const GURL& origin,
FileSystemType type,
const base::FilePath& path) const {
return CrackFileSystemURL(FileSystemURL(origin, type, path));
}
FileSystemContext::~FileSystemContext() {
task_runners_->file_task_runner()->DeleteSoon(
FROM_HERE, change_tracker_.release());
}
void FileSystemContext::DeleteOnCorrectThread() const {
if (!task_runners_->io_task_runner()->RunsTasksOnCurrentThread() &&
task_runners_->io_task_runner()->DeleteSoon(FROM_HERE, this)) {
return;
}
delete this;
}
FileSystemURL FileSystemContext::CrackFileSystemURL(
const FileSystemURL& url) const {
if (!url.is_valid())
return FileSystemURL();
// The returned value in case there is no crackers which can crack the url.
// This is valid situation for non isolated/external file systems.
FileSystemURL current = url;
// File system may be mounted multiple times (e.g., an isolated filesystem on
// top of an external filesystem). Hence cracking needs to be iterated.
for (;;) {
FileSystemURL cracked = current;
for (size_t i = 0; i < url_crackers_.size(); ++i) {
if (!url_crackers_[i]->HandlesFileSystemMountType(current.type()))
continue;
cracked = url_crackers_[i]->CrackFileSystemURL(current);
if (cracked.is_valid())
break;
}
if (cracked == current)
break;
current = cracked;
}
return current;
}
void FileSystemContext::RegisterMountPointProvider(
FileSystemMountPointProvider* provider) {
const FileSystemType mount_types[] = {
kFileSystemTypeTemporary,
kFileSystemTypePersistent,
kFileSystemTypeIsolated,
kFileSystemTypeExternal,
};
// Register mount point providers for public mount types.
for (size_t j = 0; j < ARRAYSIZE_UNSAFE(mount_types); ++j) {
if (provider->CanHandleType(mount_types[j])) {
const bool inserted = provider_map_.insert(
std::make_pair(mount_types[j], provider)).second;
DCHECK(inserted);
}
}
// Register mount point providers for internal types.
for (int t = kFileSystemInternalTypeEnumStart + 1;
t < kFileSystemInternalTypeEnumEnd; ++t) {
FileSystemType type = static_cast<FileSystemType>(t);
if (provider->CanHandleType(type)) {
const bool inserted = provider_map_.insert(
std::make_pair(type, provider)).second;
DCHECK(inserted);
}
}
}
} // namespace fileapi
|