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.
5 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browsing_data/browsing_data_helper.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "webkit/browser/fileapi/file_system_context.h"
16 #include "webkit/browser/fileapi/file_system_quota_util.h"
17 #include "webkit/common/fileapi/file_system_types.h"
19 using content::BrowserThread;
22 class FileSystemContext;
27 // An implementation of the BrowsingDataFileSystemHelper interface that pulls
28 // data from a given |filesystem_context| and returns a list of FileSystemInfo
30 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper {
32 // BrowsingDataFileSystemHelper implementation
33 explicit BrowsingDataFileSystemHelperImpl(
34 fileapi::FileSystemContext* filesystem_context);
35 virtual void StartFetching(const base::Callback<
36 void(const std::list<FileSystemInfo>&)>& callback) OVERRIDE;
37 virtual void DeleteFileSystemOrigin(const GURL& origin) OVERRIDE;
40 virtual ~BrowsingDataFileSystemHelperImpl();
42 // Enumerates all filesystem files, storing the resulting list into
43 // file_system_file_ for later use. This must be called on the file
45 void FetchFileSystemInfoInFileThread();
47 // Triggers the success callback as the end of a StartFetching workflow. This
48 // must be called on the UI thread.
49 void NotifyOnUIThread();
51 // Deletes all file systems associated with |origin|. This must be called on
52 // the file task runner.
53 void DeleteFileSystemOriginInFileThread(const GURL& origin);
55 // Returns the file task runner for the |filesystem_context_|.
56 base::SequencedTaskRunner* file_task_runner() {
57 return filesystem_context_->default_file_task_runner();
60 // Keep a reference to the FileSystemContext object for the current profile
61 // for use on the file task runner.
62 scoped_refptr<fileapi::FileSystemContext> filesystem_context_;
64 // Holds the current list of file systems returned to the client after
65 // StartFetching is called. Access to |file_system_info_| is triggered
66 // indirectly via the UI thread and guarded by |is_fetching_|. This means
67 // |file_system_info_| is only accessed while |is_fetching_| is true. The
68 // flag |is_fetching_| is only accessed on the UI thread. In the context of
69 // this class |file_system_info_| only mutates on the file task runner.
70 std::list<FileSystemInfo> file_system_info_;
72 // Holds the callback passed in at the beginning of the StartFetching workflow
73 // so that it can be triggered via NotifyOnUIThread. This only mutates on the
75 base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_;
77 // Indicates whether or not we're currently fetching information: set to true
78 // when StartFetching is called on the UI thread, and reset to false when
79 // NotifyOnUIThread triggers the success callback.
80 // This property only mutates on the UI thread.
83 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl);
86 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl(
87 fileapi::FileSystemContext* filesystem_context)
88 : filesystem_context_(filesystem_context),
90 DCHECK(filesystem_context_.get());
93 BrowsingDataFileSystemHelperImpl::~BrowsingDataFileSystemHelperImpl() {
96 void BrowsingDataFileSystemHelperImpl::StartFetching(
97 const base::Callback<void(const std::list<FileSystemInfo>&)>& callback) {
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 DCHECK(!is_fetching_);
100 DCHECK_EQ(false, callback.is_null());
102 completion_callback_ = callback;
103 file_task_runner()->PostTask(
106 &BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread,
110 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOrigin(
111 const GURL& origin) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 file_task_runner()->PostTask(
116 &BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread,
120 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() {
121 DCHECK(file_task_runner()->RunsTasksOnCurrentThread());
123 // We check usage for these filesystem types.
124 const fileapi::FileSystemType types[] = {
125 fileapi::kFileSystemTypeTemporary,
126 fileapi::kFileSystemTypePersistent,
127 fileapi::kFileSystemTypeSyncable,
130 typedef std::map<GURL, FileSystemInfo> OriginInfoMap;
131 OriginInfoMap file_system_info_map;
132 for (size_t i = 0; i < arraysize(types); ++i) {
133 fileapi::FileSystemType type = types[i];
134 fileapi::FileSystemQuotaUtil* quota_util =
135 filesystem_context_->GetQuotaUtil(type);
137 std::set<GURL> origins;
138 quota_util->GetOriginsForTypeOnFileTaskRunner(type, &origins);
139 for (std::set<GURL>::iterator iter = origins.begin();
140 iter != origins.end(); ++iter) {
141 const GURL& current = *iter;
142 if (!BrowsingDataHelper::HasWebScheme(current))
143 continue; // Non-websafe state is not considered browsing data.
144 int64 usage = quota_util->GetOriginUsageOnFileTaskRunner(
145 filesystem_context_.get(), current, type);
146 OriginInfoMap::iterator inserted =
147 file_system_info_map.insert(
148 std::make_pair(current, FileSystemInfo(current))).first;
149 inserted->second.usage_map[type] = usage;
153 for (OriginInfoMap::iterator iter = file_system_info_map.begin();
154 iter != file_system_info_map.end(); ++iter) {
155 file_system_info_.push_back(iter->second);
158 BrowserThread::PostTask(
159 BrowserThread::UI, FROM_HERE,
160 base::Bind(&BrowsingDataFileSystemHelperImpl::NotifyOnUIThread, this));
163 void BrowsingDataFileSystemHelperImpl::NotifyOnUIThread() {
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
165 DCHECK(is_fetching_);
166 completion_callback_.Run(file_system_info_);
167 completion_callback_.Reset();
168 is_fetching_ = false;
171 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread(
172 const GURL& origin) {
173 DCHECK(file_task_runner()->RunsTasksOnCurrentThread());
174 filesystem_context_->DeleteDataForOriginOnFileTaskRunner(origin);
179 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo(
180 const GURL& origin) : origin(origin) {}
182 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {}
185 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create(
186 fileapi::FileSystemContext* filesystem_context) {
187 return new BrowsingDataFileSystemHelperImpl(filesystem_context);
190 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper(
194 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper() {
197 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {}
199 CannedBrowsingDataFileSystemHelper*
200 CannedBrowsingDataFileSystemHelper::Clone() {
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
202 CannedBrowsingDataFileSystemHelper* clone =
203 new CannedBrowsingDataFileSystemHelper();
204 // This list only mutates on the UI thread, so it's safe to work with it here
205 // (given the DCHECK above).
206 clone->file_system_info_ = file_system_info_;
210 void CannedBrowsingDataFileSystemHelper::AddFileSystem(
211 const GURL& origin, const fileapi::FileSystemType type, const int64 size) {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213 // This canned implementation of AddFileSystem uses an O(n^2) algorithm; which
214 // is fine, as it isn't meant for use in a high-volume context. If it turns
215 // out that we want to start using this in a context with many, many origins,
216 // we should think about reworking the implementation.
217 bool duplicate_origin = false;
218 for (std::list<FileSystemInfo>::iterator
219 file_system = file_system_info_.begin();
220 file_system != file_system_info_.end();
222 if (file_system->origin == origin) {
223 file_system->usage_map[type] = size;
224 duplicate_origin = true;
228 if (duplicate_origin)
231 if (!BrowsingDataHelper::HasWebScheme(origin))
232 return; // Non-websafe state is not considered browsing data.
234 FileSystemInfo info(origin);
235 info.usage_map[type] = size;
236 file_system_info_.push_back(info);
239 void CannedBrowsingDataFileSystemHelper::Reset() {
240 file_system_info_.clear();
243 bool CannedBrowsingDataFileSystemHelper::empty() const {
244 return file_system_info_.empty();
247 size_t CannedBrowsingDataFileSystemHelper::GetFileSystemCount() const {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 return file_system_info_.size();
252 void CannedBrowsingDataFileSystemHelper::StartFetching(
253 const base::Callback<void(const std::list<FileSystemInfo>&)>& callback) {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 DCHECK(!callback.is_null());
257 BrowserThread::PostTask(
258 BrowserThread::UI, FROM_HERE, base::Bind(callback, file_system_info_));