OLD | NEW |
---|---|
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 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" | 5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 kIncognito, | 57 kIncognito, |
58 kInvalidSchemeError, | 58 kInvalidSchemeError, |
59 kCreateDirectoryError, | 59 kCreateDirectoryError, |
60 kNotFound, | 60 kNotFound, |
61 kUnknownError, | 61 kUnknownError, |
62 kFileSystemErrorMax, | 62 kFileSystemErrorMax, |
63 }; | 63 }; |
64 | 64 |
65 const char kTemporaryOriginsCountLabel[] = "FileSystem.TemporaryOriginsCount"; | 65 const char kTemporaryOriginsCountLabel[] = "FileSystem.TemporaryOriginsCount"; |
66 const char kPersistentOriginsCountLabel[] = "FileSystem.PersistentOriginsCount"; | 66 const char kPersistentOriginsCountLabel[] = "FileSystem.PersistentOriginsCount"; |
67 const char kSyncableOriginsCountLabel[] = "FileSystem.SyncableOriginsCount"; | |
68 | 67 |
69 // Restricted names. | 68 // Restricted names. |
70 // http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#naming-restrictions | 69 // http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#naming-restrictions |
71 const base::FilePath::CharType* const kRestrictedNames[] = { | 70 const base::FilePath::CharType* const kRestrictedNames[] = { |
72 FILE_PATH_LITERAL("."), FILE_PATH_LITERAL(".."), | 71 FILE_PATH_LITERAL("."), FILE_PATH_LITERAL(".."), |
73 }; | 72 }; |
74 | 73 |
75 // Restricted chars. | 74 // Restricted chars. |
76 const base::FilePath::CharType kRestrictedChars[] = { | 75 const base::FilePath::CharType kRestrictedChars[] = { |
77 FILE_PATH_LITERAL('/'), FILE_PATH_LITERAL('\\'), | 76 FILE_PATH_LITERAL('/'), FILE_PATH_LITERAL('\\'), |
(...skipping 13 matching lines...) Expand all Loading... | |
91 | 90 |
92 virtual bool HasFileSystemType(fileapi::FileSystemType type) const OVERRIDE { | 91 virtual bool HasFileSystemType(fileapi::FileSystemType type) const OVERRIDE { |
93 return enum_->HasFileSystemType(type); | 92 return enum_->HasFileSystemType(type); |
94 } | 93 } |
95 | 94 |
96 private: | 95 private: |
97 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> enum_; | 96 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> enum_; |
98 }; | 97 }; |
99 | 98 |
100 void DidOpenFileSystem( | 99 void DidOpenFileSystem( |
101 base::WeakPtr<SandboxFileSystemBackend> mount_point_provider, | 100 base::WeakPtr<SandboxFileSystemBackend> file_system_backend, |
102 const base::Callback<void(base::PlatformFileError error)>& callback, | 101 const base::Callback<void(base::PlatformFileError error)>& callback, |
103 base::PlatformFileError* error) { | 102 base::PlatformFileError* error) { |
104 if (mount_point_provider.get()) | 103 if (file_system_backend.get()) |
105 mount_point_provider.get()->CollectOpenFileSystemMetrics(*error); | 104 file_system_backend.get()->CollectOpenFileSystemMetrics(*error); |
106 callback.Run(*error); | 105 callback.Run(*error); |
107 } | 106 } |
108 | 107 |
109 void OpenFileSystemOnFileThread( | 108 void OpenFileSystemOnFileThread( |
110 ObfuscatedFileUtil* file_util, | 109 ObfuscatedFileUtil* file_util, |
111 const GURL& origin_url, | 110 const GURL& origin_url, |
112 FileSystemType type, | 111 FileSystemType type, |
113 OpenFileSystemMode mode, | 112 OpenFileSystemMode mode, |
114 base::PlatformFileError* error_ptr) { | 113 base::PlatformFileError* error_ptr) { |
115 DCHECK(error_ptr); | 114 DCHECK(error_ptr); |
(...skipping 11 matching lines...) Expand all Loading... | |
127 } | 126 } |
128 // The reference of file_util will be derefed on the FILE thread | 127 // The reference of file_util will be derefed on the FILE thread |
129 // when the storage of this callback gets deleted regardless of whether | 128 // when the storage of this callback gets deleted regardless of whether |
130 // this method is called or not. | 129 // this method is called or not. |
131 } | 130 } |
132 | 131 |
133 } // anonymous namespace | 132 } // anonymous namespace |
134 | 133 |
135 SandboxFileSystemBackend::SandboxFileSystemBackend( | 134 SandboxFileSystemBackend::SandboxFileSystemBackend( |
136 SandboxContext* sandbox_context, | 135 SandboxContext* sandbox_context, |
136 base::SequencedTaskRunner* file_task_runner, | |
137 const FileSystemOptions& file_system_options) | 137 const FileSystemOptions& file_system_options) |
138 : file_system_options_(file_system_options), | 138 : file_task_runner_(file_task_runner), |
kinuko
2013/07/17 15:01:30
Why does this need to be passed separately in this
kinuko
2013/07/17 15:02:59
I mean, it used to be passed via sandbox_context b
nhiroki
2013/07/22 04:34:14
This is because that SyncFileSystemBackend::set_ch
nhiroki
2013/07/22 06:33:38
Pushed file_task_runner into SandboxContext again.
| |
139 file_system_options_(file_system_options), | |
139 sandbox_context_(sandbox_context), | 140 sandbox_context_(sandbox_context), |
140 enable_temporary_file_system_in_incognito_(false), | 141 enable_temporary_file_system_in_incognito_(false), |
141 enable_usage_tracking_( | 142 enable_usage_tracking_( |
142 !CommandLine::ForCurrentProcess()->HasSwitch( | 143 !CommandLine::ForCurrentProcess()->HasSwitch( |
143 kDisableUsageTracking)), | 144 kDisableUsageTracking)), |
144 weak_factory_(this) { | 145 weak_factory_(this) { |
146 // SyncFileSystemBackend is instantiated with null context. | |
147 if (!sandbox_context) | |
148 return; | |
149 | |
145 // Set quota observers. | 150 // Set quota observers. |
146 if (enable_usage_tracking_) { | 151 if (enable_usage_tracking_) { |
147 update_observers_ = update_observers_.AddObserver( | 152 update_observers_ = update_observers_.AddObserver( |
148 sandbox_context_->quota_observer(), | 153 sandbox_context_->quota_observer(), file_task_runner_.get()); |
149 sandbox_context_->file_task_runner()); | |
150 access_observers_ = access_observers_.AddObserver( | 154 access_observers_ = access_observers_.AddObserver( |
151 sandbox_context_->quota_observer(), NULL); | 155 sandbox_context_->quota_observer(), NULL); |
152 } | 156 } |
153 | 157 |
154 syncable_update_observers_ = update_observers_; | 158 if (!file_task_runner->RunsTasksOnCurrentThread()) { |
155 | |
156 if (!sandbox_context_->file_task_runner()->RunsTasksOnCurrentThread()) { | |
157 // Post prepopulate task only if it's not already running on | 159 // Post prepopulate task only if it's not already running on |
158 // file_task_runner (which implies running in tests). | 160 // file_task_runner (which implies running in tests). |
159 sandbox_context_->file_task_runner()->PostTask( | 161 file_task_runner->PostTask( |
160 FROM_HERE, | 162 FROM_HERE, |
161 base::Bind(&ObfuscatedFileUtil::MaybePrepopulateDatabase, | 163 base::Bind(&ObfuscatedFileUtil::MaybePrepopulateDatabase, |
162 base::Unretained(sandbox_sync_file_util()))); | 164 base::Unretained(sandbox_sync_file_util()))); |
163 } | 165 } |
164 } | 166 } |
165 | 167 |
166 SandboxFileSystemBackend::~SandboxFileSystemBackend() { | 168 SandboxFileSystemBackend::~SandboxFileSystemBackend() { |
167 } | 169 } |
168 | 170 |
169 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const { | 171 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const { |
170 return type == kFileSystemTypeTemporary || | 172 return type == kFileSystemTypeTemporary || |
171 type == kFileSystemTypePersistent || | 173 type == kFileSystemTypePersistent; |
172 type == kFileSystemTypeSyncable || | |
173 type == kFileSystemTypeSyncableForInternalSync; | |
174 } | 174 } |
175 | 175 |
176 void SandboxFileSystemBackend::InitializeFileSystem( | 176 void SandboxFileSystemBackend::InitializeFileSystem( |
177 const GURL& origin_url, | 177 const GURL& origin_url, |
178 fileapi::FileSystemType type, | 178 fileapi::FileSystemType type, |
179 OpenFileSystemMode mode, | 179 OpenFileSystemMode mode, |
180 FileSystemContext* context, | 180 FileSystemContext* context, |
181 const InitializeFileSystemCallback& callback) { | 181 const InitializeFileSystemCallback& callback) { |
182 if (file_system_options_.is_incognito() && | 182 if (file_system_options_.is_incognito() && |
183 !(type == kFileSystemTypeTemporary && | 183 !(type == kFileSystemTypeTemporary && |
(...skipping 15 matching lines...) Expand all Loading... | |
199 } | 199 } |
200 | 200 |
201 // TODO(nhiroki): Factor out SyncFS related code to SyncFileSystemBackend we | 201 // TODO(nhiroki): Factor out SyncFS related code to SyncFileSystemBackend we |
202 // plan to introduce. (http://crbug.com/242422/) | 202 // plan to introduce. (http://crbug.com/242422/) |
203 GURL root_url = (type == kFileSystemTypeSyncable) | 203 GURL root_url = (type == kFileSystemTypeSyncable) |
204 ? sync_file_system::GetSyncableFileSystemRootURI(origin_url) | 204 ? sync_file_system::GetSyncableFileSystemRootURI(origin_url) |
205 : GetFileSystemRootURI(origin_url, type); | 205 : GetFileSystemRootURI(origin_url, type); |
206 std::string name = GetFileSystemName(origin_url, type); | 206 std::string name = GetFileSystemName(origin_url, type); |
207 | 207 |
208 base::PlatformFileError* error_ptr = new base::PlatformFileError; | 208 base::PlatformFileError* error_ptr = new base::PlatformFileError; |
209 sandbox_context_->file_task_runner()->PostTaskAndReply( | 209 file_task_runner_->PostTaskAndReply( |
210 FROM_HERE, | 210 FROM_HERE, |
211 base::Bind(&OpenFileSystemOnFileThread, | 211 base::Bind(&OpenFileSystemOnFileThread, |
212 sandbox_sync_file_util(), | 212 sandbox_sync_file_util(), |
213 origin_url, type, mode, | 213 origin_url, type, mode, |
214 base::Unretained(error_ptr)), | 214 base::Unretained(error_ptr)), |
215 base::Bind(&DidOpenFileSystem, | 215 base::Bind(&DidOpenFileSystem, |
216 weak_factory_.GetWeakPtr(), | 216 weak_factory_.GetWeakPtr(), |
217 base::Bind(callback, root_url, name), | 217 base::Bind(callback, root_url, name), |
218 base::Owned(error_ptr))); | 218 base::Owned(error_ptr))); |
219 | 219 |
220 if (enable_usage_tracking_) | 220 if (enable_usage_tracking_) |
221 return; | 221 return; |
222 | 222 |
223 // Schedule full usage recalculation on the next launch without | 223 // Schedule full usage recalculation on the next launch without |
224 // --disable-file-system-usage-tracking. | 224 // --disable-file-system-usage-tracking. |
225 sandbox_context_->file_task_runner()->PostTask( | 225 file_task_runner_->PostTask( |
226 FROM_HERE, | 226 FROM_HERE, |
227 base::Bind(&SandboxFileSystemBackend::InvalidateUsageCacheOnFileThread, | 227 base::Bind(&SandboxFileSystemBackend::InvalidateUsageCacheOnFileThread, |
228 sandbox_sync_file_util(), origin_url, type, usage_cache())); | 228 sandbox_sync_file_util(), origin_url, type, usage_cache())); |
229 }; | 229 }; |
230 | 230 |
231 FileSystemFileUtil* SandboxFileSystemBackend::GetFileUtil( | 231 FileSystemFileUtil* SandboxFileSystemBackend::GetFileUtil( |
232 FileSystemType type) { | 232 FileSystemType type, |
233 const FileSystemContext* context) { | |
233 DCHECK(sandbox_context_); | 234 DCHECK(sandbox_context_); |
234 return sandbox_context_->sync_file_util(); | 235 return sandbox_context_->sync_file_util(); |
235 } | 236 } |
236 | 237 |
237 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil( | 238 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil( |
238 FileSystemType type) { | 239 FileSystemType type, |
240 const FileSystemContext* context) { | |
239 DCHECK(sandbox_context_); | 241 DCHECK(sandbox_context_); |
240 return sandbox_context_->file_util(); | 242 return sandbox_context_->file_util(); |
241 } | 243 } |
242 | 244 |
243 CopyOrMoveFileValidatorFactory* | 245 CopyOrMoveFileValidatorFactory* |
244 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory( | 246 SandboxFileSystemBackend::GetCopyOrMoveFileValidatorFactory( |
245 FileSystemType type, | 247 FileSystemType type, |
246 base::PlatformFileError* error_code) { | 248 base::PlatformFileError* error_code) { |
247 DCHECK(error_code); | 249 DCHECK(error_code); |
248 *error_code = base::PLATFORM_FILE_OK; | 250 *error_code = base::PLATFORM_FILE_OK; |
249 return NULL; | 251 return NULL; |
250 } | 252 } |
251 | 253 |
252 FileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation( | 254 FileSystemOperation* SandboxFileSystemBackend::CreateFileSystemOperation( |
253 const FileSystemURL& url, | 255 const FileSystemURL& url, |
254 FileSystemContext* context, | 256 FileSystemContext* context, |
255 base::PlatformFileError* error_code) const { | 257 base::PlatformFileError* error_code) const { |
256 if (!IsAccessValid(url)) { | 258 if (!IsAccessValid(url)) { |
257 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; | 259 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; |
258 return NULL; | 260 return NULL; |
259 } | 261 } |
260 | 262 |
261 scoped_ptr<FileSystemOperationContext> operation_context( | 263 scoped_ptr<FileSystemOperationContext> operation_context( |
262 new FileSystemOperationContext(context)); | 264 new FileSystemOperationContext(context)); |
263 | |
264 // Copy the observer lists (assuming we only have small number of observers). | |
265 if (url.type() == kFileSystemTypeSyncable) { | |
266 operation_context->set_update_observers(syncable_update_observers_); | |
267 operation_context->set_change_observers(syncable_change_observers_); | |
268 return new sync_file_system::SyncableFileSystemOperation( | |
269 url, context, operation_context.Pass()); | |
270 } | |
271 | |
272 // For regular sandboxed types. | |
273 operation_context->set_update_observers(update_observers_); | 265 operation_context->set_update_observers(update_observers_); |
274 operation_context->set_change_observers(change_observers_); | 266 operation_context->set_change_observers(change_observers_); |
275 | 267 |
276 SpecialStoragePolicy* policy = sandbox_context_->special_storage_policy(); | 268 SpecialStoragePolicy* policy = sandbox_context_->special_storage_policy(); |
277 if (policy && policy->IsStorageUnlimited(url.origin())) | 269 if (policy && policy->IsStorageUnlimited(url.origin())) |
278 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited); | 270 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeUnlimited); |
279 else | 271 else |
280 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeLimited); | 272 operation_context->set_quota_limit_type(quota::kQuotaLimitTypeLimited); |
281 | 273 |
282 return new LocalFileSystemOperation(url, context, operation_context.Pass()); | 274 return new LocalFileSystemOperation(url, context, operation_context.Pass()); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 if (enumerator->HasFileSystemType(type)) | 352 if (enumerator->HasFileSystemType(type)) |
361 origins->insert(origin); | 353 origins->insert(origin); |
362 } | 354 } |
363 switch (type) { | 355 switch (type) { |
364 case kFileSystemTypeTemporary: | 356 case kFileSystemTypeTemporary: |
365 UMA_HISTOGRAM_COUNTS(kTemporaryOriginsCountLabel, origins->size()); | 357 UMA_HISTOGRAM_COUNTS(kTemporaryOriginsCountLabel, origins->size()); |
366 break; | 358 break; |
367 case kFileSystemTypePersistent: | 359 case kFileSystemTypePersistent: |
368 UMA_HISTOGRAM_COUNTS(kPersistentOriginsCountLabel, origins->size()); | 360 UMA_HISTOGRAM_COUNTS(kPersistentOriginsCountLabel, origins->size()); |
369 break; | 361 break; |
370 case kFileSystemTypeSyncable: | |
371 UMA_HISTOGRAM_COUNTS(kSyncableOriginsCountLabel, origins->size()); | |
372 break; | |
373 default: | 362 default: |
374 break; | 363 break; |
375 } | 364 } |
376 } | 365 } |
377 | 366 |
378 void SandboxFileSystemBackend::GetOriginsForHostOnFileThread( | 367 void SandboxFileSystemBackend::GetOriginsForHostOnFileThread( |
379 fileapi::FileSystemType type, const std::string& host, | 368 fileapi::FileSystemType type, const std::string& host, |
380 std::set<GURL>* origins) { | 369 std::set<GURL>* origins) { |
381 DCHECK(CanHandleType(type)); | 370 DCHECK(CanHandleType(type)); |
382 DCHECK(origins); | 371 DCHECK(origins); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 sandbox_context_->quota_observer()->SetUsageCacheEnabled(origin, type, false); | 440 sandbox_context_->quota_observer()->SetUsageCacheEnabled(origin, type, false); |
452 InvalidateUsageCache(origin, type); | 441 InvalidateUsageCache(origin, type); |
453 } | 442 } |
454 | 443 |
455 void SandboxFileSystemBackend::AddFileUpdateObserver( | 444 void SandboxFileSystemBackend::AddFileUpdateObserver( |
456 FileSystemType type, | 445 FileSystemType type, |
457 FileUpdateObserver* observer, | 446 FileUpdateObserver* observer, |
458 base::SequencedTaskRunner* task_runner) { | 447 base::SequencedTaskRunner* task_runner) { |
459 DCHECK(CanHandleType(type)); | 448 DCHECK(CanHandleType(type)); |
460 UpdateObserverList* list = &update_observers_; | 449 UpdateObserverList* list = &update_observers_; |
461 if (type == kFileSystemTypeSyncable) | |
462 list = &syncable_update_observers_; | |
463 *list = list->AddObserver(observer, task_runner); | 450 *list = list->AddObserver(observer, task_runner); |
464 } | 451 } |
465 | 452 |
466 void SandboxFileSystemBackend::AddFileChangeObserver( | 453 void SandboxFileSystemBackend::AddFileChangeObserver( |
467 FileSystemType type, | 454 FileSystemType type, |
468 FileChangeObserver* observer, | 455 FileChangeObserver* observer, |
469 base::SequencedTaskRunner* task_runner) { | 456 base::SequencedTaskRunner* task_runner) { |
470 DCHECK(CanHandleType(type)); | 457 DCHECK(CanHandleType(type)); |
471 ChangeObserverList* list = &change_observers_; | 458 ChangeObserverList* list = &change_observers_; |
472 if (type == kFileSystemTypeSyncable) | |
473 list = &syncable_change_observers_; | |
474 *list = list->AddObserver(observer, task_runner); | 459 *list = list->AddObserver(observer, task_runner); |
475 } | 460 } |
476 | 461 |
477 void SandboxFileSystemBackend::AddFileAccessObserver( | 462 void SandboxFileSystemBackend::AddFileAccessObserver( |
478 FileSystemType type, | 463 FileSystemType type, |
479 FileAccessObserver* observer, | 464 FileAccessObserver* observer, |
480 base::SequencedTaskRunner* task_runner) { | 465 base::SequencedTaskRunner* task_runner) { |
481 DCHECK(CanHandleType(type)); | 466 DCHECK(CanHandleType(type)); |
482 access_observers_ = access_observers_.AddObserver(observer, task_runner); | 467 access_observers_ = access_observers_.AddObserver(observer, task_runner); |
483 } | 468 } |
484 | 469 |
485 const UpdateObserverList* SandboxFileSystemBackend::GetUpdateObservers( | 470 const UpdateObserverList* SandboxFileSystemBackend::GetUpdateObservers( |
486 FileSystemType type) const { | 471 FileSystemType type) const { |
487 DCHECK(CanHandleType(type)); | 472 DCHECK(CanHandleType(type)); |
488 if (type == kFileSystemTypeSyncable) | |
489 return &syncable_update_observers_; | |
490 return &update_observers_; | 473 return &update_observers_; |
491 } | 474 } |
492 | 475 |
493 const ChangeObserverList* SandboxFileSystemBackend::GetChangeObservers( | 476 const ChangeObserverList* SandboxFileSystemBackend::GetChangeObservers( |
494 FileSystemType type) const { | 477 FileSystemType type) const { |
495 DCHECK(CanHandleType(type)); | 478 DCHECK(CanHandleType(type)); |
496 if (type == kFileSystemTypeSyncable) | |
497 return &syncable_change_observers_; | |
498 return &change_observers_; | 479 return &change_observers_; |
499 } | 480 } |
500 | 481 |
501 const AccessObserverList* SandboxFileSystemBackend::GetAccessObservers( | 482 const AccessObserverList* SandboxFileSystemBackend::GetAccessObservers( |
502 FileSystemType type) const { | 483 FileSystemType type) const { |
503 DCHECK(CanHandleType(type)); | 484 DCHECK(CanHandleType(type)); |
504 return &access_observers_; | 485 return &access_observers_; |
505 } | 486 } |
506 | 487 |
507 void SandboxFileSystemBackend::CollectOpenFileSystemMetrics( | 488 void SandboxFileSystemBackend::CollectOpenFileSystemMetrics( |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
658 | 639 |
659 while (!(file_path_each = enumerator->Next()).empty()) { | 640 while (!(file_path_each = enumerator->Next()).empty()) { |
660 usage += enumerator->Size(); | 641 usage += enumerator->Size(); |
661 usage += ObfuscatedFileUtil::ComputeFilePathCost(file_path_each); | 642 usage += ObfuscatedFileUtil::ComputeFilePathCost(file_path_each); |
662 } | 643 } |
663 | 644 |
664 return usage; | 645 return usage; |
665 } | 646 } |
666 | 647 |
667 } // namespace fileapi | 648 } // namespace fileapi |
OLD | NEW |