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/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "webkit/common/fileapi/file_system_types.h" | 28 #include "webkit/common/fileapi/file_system_types.h" |
29 #include "webkit/common/fileapi/file_system_util.h" | 29 #include "webkit/common/fileapi/file_system_util.h" |
30 | 30 |
31 using quota::QuotaManagerProxy; | 31 using quota::QuotaManagerProxy; |
32 using quota::SpecialStoragePolicy; | 32 using quota::SpecialStoragePolicy; |
33 | 33 |
34 namespace fileapi { | 34 namespace fileapi { |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 const char kOpenFileSystemLabel[] = "FileSystem.OpenFileSystem"; | |
39 const char kOpenFileSystemDetailLabel[] = "FileSystem.OpenFileSystemDetail"; | |
40 const char kOpenFileSystemDetailNonThrottledLabel[] = | |
41 "FileSystem.OpenFileSystemDetailNonthrottled"; | |
42 int64 kMinimumStatsCollectionIntervalHours = 1; | |
43 | |
44 enum FileSystemError { | |
45 kOK = 0, | |
46 kIncognito, | |
47 kInvalidSchemeError, | |
48 kCreateDirectoryError, | |
49 kNotFound, | |
50 kUnknownError, | |
51 kFileSystemErrorMax, | |
52 }; | |
53 | |
54 const char kTemporaryOriginsCountLabel[] = "FileSystem.TemporaryOriginsCount"; | 38 const char kTemporaryOriginsCountLabel[] = "FileSystem.TemporaryOriginsCount"; |
55 const char kPersistentOriginsCountLabel[] = "FileSystem.PersistentOriginsCount"; | 39 const char kPersistentOriginsCountLabel[] = "FileSystem.PersistentOriginsCount"; |
56 | 40 |
57 void DidOpenFileSystem( | |
58 base::WeakPtr<SandboxFileSystemBackend> sandbox_backend, | |
59 const base::Callback<void(base::PlatformFileError error)>& callback, | |
60 base::PlatformFileError* error) { | |
61 if (sandbox_backend.get()) | |
62 sandbox_backend.get()->CollectOpenFileSystemMetrics(*error); | |
63 callback.Run(*error); | |
64 } | |
65 | |
66 void OpenFileSystemOnFileThread( | |
67 ObfuscatedFileUtil* file_util, | |
68 const GURL& origin_url, | |
69 FileSystemType type, | |
70 OpenFileSystemMode mode, | |
71 base::PlatformFileError* error_ptr) { | |
72 DCHECK(error_ptr); | |
73 const bool create = (mode == OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT); | |
74 file_util->GetDirectoryForOriginAndType(origin_url, type, create, error_ptr); | |
75 if (*error_ptr != base::PLATFORM_FILE_OK) { | |
76 UMA_HISTOGRAM_ENUMERATION(kOpenFileSystemLabel, | |
77 kCreateDirectoryError, | |
78 kFileSystemErrorMax); | |
79 } else { | |
80 UMA_HISTOGRAM_ENUMERATION(kOpenFileSystemLabel, kOK, kFileSystemErrorMax); | |
81 } | |
82 // The reference of file_util will be derefed on the FILE thread | |
83 // when the storage of this callback gets deleted regardless of whether | |
84 // this method is called or not. | |
85 } | |
86 | |
87 } // anonymous namespace | 41 } // anonymous namespace |
88 | 42 |
89 SandboxFileSystemBackend::SandboxFileSystemBackend( | 43 SandboxFileSystemBackend::SandboxFileSystemBackend( |
90 SandboxContext* sandbox_context) | 44 SandboxContext* sandbox_context) |
91 : sandbox_context_(sandbox_context), | 45 : sandbox_context_(sandbox_context), |
92 enable_temporary_file_system_in_incognito_(false), | 46 enable_temporary_file_system_in_incognito_(false) { |
93 weak_factory_(this) { | |
94 } | 47 } |
95 | 48 |
96 SandboxFileSystemBackend::~SandboxFileSystemBackend() { | 49 SandboxFileSystemBackend::~SandboxFileSystemBackend() { |
97 } | 50 } |
98 | 51 |
99 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const { | 52 bool SandboxFileSystemBackend::CanHandleType(FileSystemType type) const { |
100 return type == kFileSystemTypeTemporary || | 53 return type == kFileSystemTypeTemporary || |
101 type == kFileSystemTypePersistent || | 54 type == kFileSystemTypePersistent || |
102 type == kFileSystemTypeSyncable || | 55 type == kFileSystemTypeSyncable || |
103 type == kFileSystemTypeSyncableForInternalSync; | 56 type == kFileSystemTypeSyncableForInternalSync; |
(...skipping 27 matching lines...) Expand all Loading... |
131 DCHECK(CanHandleType(type)); | 84 DCHECK(CanHandleType(type)); |
132 DCHECK(sandbox_context_); | 85 DCHECK(sandbox_context_); |
133 if (sandbox_context_->file_system_options().is_incognito() && | 86 if (sandbox_context_->file_system_options().is_incognito() && |
134 !(type == kFileSystemTypeTemporary && | 87 !(type == kFileSystemTypeTemporary && |
135 enable_temporary_file_system_in_incognito_)) { | 88 enable_temporary_file_system_in_incognito_)) { |
136 // TODO(kinuko): return an isolated temporary directory. | 89 // TODO(kinuko): return an isolated temporary directory. |
137 callback.Run(GURL(), std::string(), base::PLATFORM_FILE_ERROR_SECURITY); | 90 callback.Run(GURL(), std::string(), base::PLATFORM_FILE_ERROR_SECURITY); |
138 return; | 91 return; |
139 } | 92 } |
140 | 93 |
141 if (!sandbox_context_->IsAllowedScheme(origin_url)) { | |
142 callback.Run(GURL(), std::string(), base::PLATFORM_FILE_ERROR_SECURITY); | |
143 return; | |
144 } | |
145 | |
146 // TODO(nhiroki): Factor out SyncFS related code to SyncFileSystemBackend we | 94 // TODO(nhiroki): Factor out SyncFS related code to SyncFileSystemBackend we |
147 // plan to introduce. (http://crbug.com/242422/) | 95 // plan to introduce. (http://crbug.com/242422/) |
148 GURL root_url = (type == kFileSystemTypeSyncable) | 96 GURL root_url = (type == kFileSystemTypeSyncable) |
149 ? sync_file_system::GetSyncableFileSystemRootURI(origin_url) | 97 ? sync_file_system::GetSyncableFileSystemRootURI(origin_url) |
150 : GetFileSystemRootURI(origin_url, type); | 98 : GetFileSystemRootURI(origin_url, type); |
151 std::string name = GetFileSystemName(origin_url, type); | |
152 | 99 |
153 base::PlatformFileError* error_ptr = new base::PlatformFileError; | 100 sandbox_context_->OpenFileSystem( |
154 sandbox_context_->file_task_runner()->PostTaskAndReply( | 101 origin_url, type, mode, callback, root_url); |
155 FROM_HERE, | 102 } |
156 base::Bind(&OpenFileSystemOnFileThread, | |
157 sandbox_context_->sync_file_util(), | |
158 origin_url, type, mode, | |
159 base::Unretained(error_ptr)), | |
160 base::Bind(&DidOpenFileSystem, | |
161 weak_factory_.GetWeakPtr(), | |
162 base::Bind(callback, root_url, name), | |
163 base::Owned(error_ptr))); | |
164 }; | |
165 | 103 |
166 FileSystemFileUtil* SandboxFileSystemBackend::GetFileUtil( | 104 FileSystemFileUtil* SandboxFileSystemBackend::GetFileUtil( |
167 FileSystemType type) { | 105 FileSystemType type) { |
168 DCHECK(sandbox_context_); | |
169 return sandbox_context_->sync_file_util(); | 106 return sandbox_context_->sync_file_util(); |
170 } | 107 } |
171 | 108 |
172 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil( | 109 AsyncFileUtil* SandboxFileSystemBackend::GetAsyncFileUtil( |
173 FileSystemType type) { | 110 FileSystemType type) { |
174 DCHECK(sandbox_context_); | 111 DCHECK(sandbox_context_); |
175 return sandbox_context_->file_util(); | 112 return sandbox_context_->file_util(); |
176 } | 113 } |
177 | 114 |
178 CopyOrMoveFileValidatorFactory* | 115 CopyOrMoveFileValidatorFactory* |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 return &syncable_change_observers_; | 302 return &syncable_change_observers_; |
366 return &change_observers_; | 303 return &change_observers_; |
367 } | 304 } |
368 | 305 |
369 const AccessObserverList* SandboxFileSystemBackend::GetAccessObservers( | 306 const AccessObserverList* SandboxFileSystemBackend::GetAccessObservers( |
370 FileSystemType type) const { | 307 FileSystemType type) const { |
371 DCHECK(CanHandleType(type)); | 308 DCHECK(CanHandleType(type)); |
372 return &access_observers_; | 309 return &access_observers_; |
373 } | 310 } |
374 | 311 |
375 void SandboxFileSystemBackend::CollectOpenFileSystemMetrics( | |
376 base::PlatformFileError error_code) { | |
377 base::Time now = base::Time::Now(); | |
378 bool throttled = now < next_release_time_for_open_filesystem_stat_; | |
379 if (!throttled) { | |
380 next_release_time_for_open_filesystem_stat_ = | |
381 now + base::TimeDelta::FromHours(kMinimumStatsCollectionIntervalHours); | |
382 } | |
383 | |
384 #define REPORT(report_value) \ | |
385 UMA_HISTOGRAM_ENUMERATION(kOpenFileSystemDetailLabel, \ | |
386 (report_value), \ | |
387 kFileSystemErrorMax); \ | |
388 if (!throttled) { \ | |
389 UMA_HISTOGRAM_ENUMERATION(kOpenFileSystemDetailNonThrottledLabel, \ | |
390 (report_value), \ | |
391 kFileSystemErrorMax); \ | |
392 } | |
393 | |
394 switch (error_code) { | |
395 case base::PLATFORM_FILE_OK: | |
396 REPORT(kOK); | |
397 break; | |
398 case base::PLATFORM_FILE_ERROR_INVALID_URL: | |
399 REPORT(kInvalidSchemeError); | |
400 break; | |
401 case base::PLATFORM_FILE_ERROR_NOT_FOUND: | |
402 REPORT(kNotFound); | |
403 break; | |
404 case base::PLATFORM_FILE_ERROR_FAILED: | |
405 default: | |
406 REPORT(kUnknownError); | |
407 break; | |
408 } | |
409 #undef REPORT | |
410 } | |
411 | |
412 } // namespace fileapi | 312 } // namespace fileapi |
OLD | NEW |