OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/browsing_data_file_system_helper.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/file_util.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/message_loop.h" | |
12 #include "base/string_util.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "chrome/browser/browsing_data_helper.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "content/public/browser/browser_thread.h" | |
17 #include "webkit/fileapi/file_system_context.h" | |
18 #include "webkit/fileapi/file_system_quota_util.h" | |
19 #include "webkit/fileapi/file_system_types.h" | |
20 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
21 | |
22 using content::BrowserContext; | |
23 using content::BrowserThread; | |
24 | |
25 namespace { | |
26 | |
27 // An implementation of the BrowsingDataFileSystemHelper interface that pulls | |
28 // data from a given |profile| and returns a list of FileSystemInfo items to a | |
29 // client. | |
30 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper { | |
31 public: | |
32 // BrowsingDataFileSystemHelper implementation | |
33 explicit BrowsingDataFileSystemHelperImpl(Profile* profile); | |
34 virtual void StartFetching(const base::Callback< | |
35 void(const std::list<FileSystemInfo>&)>& callback) OVERRIDE; | |
36 virtual void DeleteFileSystemOrigin(const GURL& origin) OVERRIDE; | |
37 | |
38 private: | |
39 virtual ~BrowsingDataFileSystemHelperImpl(); | |
40 | |
41 // Enumerates all filesystem files, storing the resulting list into | |
42 // file_system_file_ for later use. This must be called on the FILE thread. | |
43 void FetchFileSystemInfoInFileThread(); | |
44 | |
45 // Triggers the success callback as the end of a StartFetching workflow. This | |
46 // must be called on the UI thread. | |
47 void NotifyOnUIThread(); | |
48 | |
49 // Deletes all file systems associated with |origin|. This must be called on | |
50 // the FILE thread. | |
51 void DeleteFileSystemOriginInFileThread(const GURL& origin); | |
52 | |
53 // Keep a reference to the FileSystemContext object for the current profile | |
54 // for use on the FILE thread. | |
55 scoped_refptr<fileapi::FileSystemContext> filesystem_context_; | |
56 | |
57 // Holds the current list of file systems returned to the client after | |
58 // StartFetching is called. Access to |file_system_info_| is triggered | |
59 // indirectly via the UI thread and guarded by |is_fetching_|. This means | |
60 // |file_system_info_| is only accessed while |is_fetching_| is true. The | |
61 // flag |is_fetching_| is only accessed on the UI thread. In the context of | |
62 // this class |file_system_info_| only mutates on the FILE thread. | |
63 std::list<FileSystemInfo> file_system_info_; | |
64 | |
65 // Holds the callback passed in at the beginning of the StartFetching workflow | |
66 // so that it can be triggered via NotifyOnUIThread. This only mutates on the | |
67 // UI thread. | |
68 base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_; | |
69 | |
70 // Indicates whether or not we're currently fetching information: set to true | |
71 // when StartFetching is called on the UI thread, and reset to false when | |
72 // NotifyOnUIThread triggers the success callback. | |
73 // This property only mutates on the UI thread. | |
74 bool is_fetching_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl); | |
77 }; | |
78 | |
79 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl( | |
80 Profile* profile) | |
81 : filesystem_context_(BrowserContext::GetFileSystemContext(profile)), | |
82 is_fetching_(false) { | |
83 DCHECK(filesystem_context_); | |
84 } | |
85 | |
86 BrowsingDataFileSystemHelperImpl::~BrowsingDataFileSystemHelperImpl() { | |
87 } | |
88 | |
89 void BrowsingDataFileSystemHelperImpl::StartFetching( | |
90 const base::Callback<void(const std::list<FileSystemInfo>&)>& callback) { | |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
92 DCHECK(!is_fetching_); | |
93 DCHECK_EQ(false, callback.is_null()); | |
94 is_fetching_ = true; | |
95 completion_callback_ = callback; | |
96 BrowserThread::PostTask( | |
97 BrowserThread::FILE, FROM_HERE, | |
98 base::Bind( | |
99 &BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread, | |
100 this)); | |
101 } | |
102 | |
103 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOrigin( | |
104 const GURL& origin) { | |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
106 BrowserThread::PostTask( | |
107 BrowserThread::FILE, FROM_HERE, | |
108 base::Bind( | |
109 &BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread, | |
110 this, origin)); | |
111 } | |
112 | |
113 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() { | |
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
115 scoped_ptr<fileapi::SandboxMountPointProvider::OriginEnumerator> | |
116 origin_enumerator(filesystem_context_-> | |
117 sandbox_provider()->CreateOriginEnumerator()); | |
118 | |
119 // We don't own this pointer; it's a magic singleton generated by the | |
120 // profile's FileSystemContext. Deleting it would be a bad idea. | |
121 fileapi::FileSystemQuotaUtil* quota_util = | |
122 filesystem_context_->GetQuotaUtil(fileapi::kFileSystemTypeTemporary); | |
123 | |
124 GURL current; | |
125 | |
126 while (!(current = origin_enumerator->Next()).is_empty()) { | |
127 if (!BrowsingDataHelper::HasWebScheme(current)) | |
128 continue; // Non-websafe state is not considered browsing data. | |
129 | |
130 // We can call these synchronous methods as we've already verified that | |
131 // we're running on the FILE thread. | |
132 int64 persistent_usage = quota_util->GetOriginUsageOnFileThread( | |
133 filesystem_context_, current, | |
134 fileapi::kFileSystemTypePersistent); | |
135 int64 temporary_usage = quota_util->GetOriginUsageOnFileThread( | |
136 filesystem_context_, current, | |
137 fileapi::kFileSystemTypeTemporary); | |
138 file_system_info_.push_back( | |
139 FileSystemInfo( | |
140 current, | |
141 origin_enumerator->HasFileSystemType( | |
142 fileapi::kFileSystemTypePersistent), | |
143 origin_enumerator->HasFileSystemType( | |
144 fileapi::kFileSystemTypeTemporary), | |
145 persistent_usage, | |
146 temporary_usage)); | |
147 } | |
148 | |
149 BrowserThread::PostTask( | |
150 BrowserThread::UI, FROM_HERE, | |
151 base::Bind(&BrowsingDataFileSystemHelperImpl::NotifyOnUIThread, this)); | |
152 } | |
153 | |
154 void BrowsingDataFileSystemHelperImpl::NotifyOnUIThread() { | |
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
156 DCHECK(is_fetching_); | |
157 completion_callback_.Run(file_system_info_); | |
158 completion_callback_.Reset(); | |
159 is_fetching_ = false; | |
160 } | |
161 | |
162 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread( | |
163 const GURL& origin) { | |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
165 filesystem_context_->DeleteDataForOriginOnFileThread(origin); | |
166 } | |
167 | |
168 } // namespace | |
169 | |
170 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo( | |
171 const GURL& origin, | |
172 bool has_persistent, | |
173 bool has_temporary, | |
174 int64 usage_persistent, | |
175 int64 usage_temporary) | |
176 : origin(origin), | |
177 has_persistent(has_persistent), | |
178 has_temporary(has_temporary), | |
179 usage_persistent(usage_persistent), | |
180 usage_temporary(usage_temporary) { | |
181 } | |
182 | |
183 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {} | |
184 | |
185 // static | |
186 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create( | |
187 Profile* profile) { | |
188 return new BrowsingDataFileSystemHelperImpl(profile); | |
189 } | |
190 | |
191 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper( | |
192 Profile* profile) | |
193 : is_fetching_(false) { | |
194 } | |
195 | |
196 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper() | |
197 : is_fetching_(false) { | |
198 } | |
199 | |
200 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {} | |
201 | |
202 CannedBrowsingDataFileSystemHelper* | |
203 CannedBrowsingDataFileSystemHelper::Clone() { | |
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
205 CannedBrowsingDataFileSystemHelper* clone = | |
206 new CannedBrowsingDataFileSystemHelper(); | |
207 // This list only mutates on the UI thread, so it's safe to work with it here | |
208 // (given the DCHECK above). | |
209 clone->file_system_info_ = file_system_info_; | |
210 return clone; | |
211 } | |
212 | |
213 void CannedBrowsingDataFileSystemHelper::AddFileSystem( | |
214 const GURL& origin, const fileapi::FileSystemType type, const int64 size) { | |
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
216 // This canned implementation of AddFileSystem uses an O(n^2) algorithm; which | |
217 // is fine, as it isn't meant for use in a high-volume context. If it turns | |
218 // out that we want to start using this in a context with many, many origins, | |
219 // we should think about reworking the implementation. | |
220 bool duplicate_origin = false; | |
221 for (std::list<FileSystemInfo>::iterator | |
222 file_system = file_system_info_.begin(); | |
223 file_system != file_system_info_.end(); | |
224 ++file_system) { | |
225 if (file_system->origin == origin) { | |
226 if (type == fileapi::kFileSystemTypePersistent) { | |
227 file_system->has_persistent = true; | |
228 file_system->usage_persistent = size; | |
229 } else { | |
230 file_system->has_temporary = true; | |
231 file_system->usage_temporary = size; | |
232 } | |
233 duplicate_origin = true; | |
234 break; | |
235 } | |
236 } | |
237 if (duplicate_origin) | |
238 return; | |
239 | |
240 if (!BrowsingDataHelper::HasWebScheme(origin)) | |
241 return; // Non-websafe state is not considered browsing data. | |
242 | |
243 file_system_info_.push_back(FileSystemInfo( | |
244 origin, | |
245 (type == fileapi::kFileSystemTypePersistent), | |
246 (type == fileapi::kFileSystemTypeTemporary), | |
247 (type == fileapi::kFileSystemTypePersistent) ? size : 0, | |
248 (type == fileapi::kFileSystemTypeTemporary) ? size : 0)); | |
249 } | |
250 | |
251 void CannedBrowsingDataFileSystemHelper::Reset() { | |
252 file_system_info_.clear(); | |
253 } | |
254 | |
255 bool CannedBrowsingDataFileSystemHelper::empty() const { | |
256 return file_system_info_.empty(); | |
257 } | |
258 | |
259 size_t CannedBrowsingDataFileSystemHelper::GetFileSystemCount() const { | |
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
261 return file_system_info_.size(); | |
262 } | |
263 | |
264 void CannedBrowsingDataFileSystemHelper::StartFetching( | |
265 const base::Callback<void(const std::list<FileSystemInfo>&)>& callback) { | |
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
267 DCHECK(!is_fetching_); | |
268 DCHECK_EQ(false, callback.is_null()); | |
269 is_fetching_ = true; | |
270 completion_callback_ = callback; | |
271 | |
272 BrowserThread::PostTask( | |
273 BrowserThread::UI, FROM_HERE, | |
274 base::Bind(&CannedBrowsingDataFileSystemHelper::NotifyOnUIThread, this)); | |
275 } | |
276 | |
277 void CannedBrowsingDataFileSystemHelper::NotifyOnUIThread() { | |
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
279 DCHECK(is_fetching_); | |
280 completion_callback_.Run(file_system_info_); | |
281 completion_callback_.Reset(); | |
282 is_fetching_ = false; | |
283 } | |
OLD | NEW |