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/browsing_data_file_system_helper.h"
6
7 #include "base/bind.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"
18
19 using content::BrowserThread;
20
21 namespace fileapi {
22 class FileSystemContext;
23 }
24
25 namespace {
26
27 // An implementation of the BrowsingDataFileSystemHelper interface that pulls
28 // data from a given |filesystem_context| and returns a list of FileSystemInfo
29 // items to a client.
30 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper {
31 public:
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;
38
39 private:
40 virtual ~BrowsingDataFileSystemHelperImpl();
41
42 // Enumerates all filesystem files, storing the resulting list into
43 // file_system_file_ for later use. This must be called on the file
44 // task runner.
45 void FetchFileSystemInfoInFileThread();
46
47 // Triggers the success callback as the end of a StartFetching workflow. This
48 // must be called on the UI thread.
49 void NotifyOnUIThread();
50
51 // Deletes all file systems associated with |origin|. This must be called on
52 // the file task runner.
53 void DeleteFileSystemOriginInFileThread(const GURL& origin);
54
55 // Returns the file task runner for the |filesystem_context_|.
file_task_runner()56 base::SequencedTaskRunner* file_task_runner() {
57 return filesystem_context_->default_file_task_runner();
58 }
59
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_;
63
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_;
71
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
74 // UI thread.
75 base::Callback<void(const std::list<FileSystemInfo>&)> completion_callback_;
76
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.
81 bool is_fetching_;
82
83 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl);
84 };
85
BrowsingDataFileSystemHelperImpl(fileapi::FileSystemContext * filesystem_context)86 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl(
87 fileapi::FileSystemContext* filesystem_context)
88 : filesystem_context_(filesystem_context),
89 is_fetching_(false) {
90 DCHECK(filesystem_context_.get());
91 }
92
~BrowsingDataFileSystemHelperImpl()93 BrowsingDataFileSystemHelperImpl::~BrowsingDataFileSystemHelperImpl() {
94 }
95
StartFetching(const base::Callback<void (const std::list<FileSystemInfo> &)> & callback)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());
101 is_fetching_ = true;
102 completion_callback_ = callback;
103 file_task_runner()->PostTask(
104 FROM_HERE,
105 base::Bind(
106 &BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread,
107 this));
108 }
109
DeleteFileSystemOrigin(const GURL & origin)110 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOrigin(
111 const GURL& origin) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 file_task_runner()->PostTask(
114 FROM_HERE,
115 base::Bind(
116 &BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread,
117 this, origin));
118 }
119
FetchFileSystemInfoInFileThread()120 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() {
121 DCHECK(file_task_runner()->RunsTasksOnCurrentThread());
122
123 // We check usage for these filesystem types.
124 const fileapi::FileSystemType types[] = {
125 fileapi::kFileSystemTypeTemporary,
126 fileapi::kFileSystemTypePersistent,
127 fileapi::kFileSystemTypeSyncable,
128 };
129
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);
136 DCHECK(quota_util);
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;
150 }
151 }
152
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);
156 }
157
158 BrowserThread::PostTask(
159 BrowserThread::UI, FROM_HERE,
160 base::Bind(&BrowsingDataFileSystemHelperImpl::NotifyOnUIThread, this));
161 }
162
NotifyOnUIThread()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;
169 }
170
DeleteFileSystemOriginInFileThread(const GURL & origin)171 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread(
172 const GURL& origin) {
173 DCHECK(file_task_runner()->RunsTasksOnCurrentThread());
174 filesystem_context_->DeleteDataForOriginOnFileTaskRunner(origin);
175 }
176
177 } // namespace
178
FileSystemInfo(const GURL & origin)179 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo(
180 const GURL& origin) : origin(origin) {}
181
~FileSystemInfo()182 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {}
183
184 // static
Create(fileapi::FileSystemContext * filesystem_context)185 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create(
186 fileapi::FileSystemContext* filesystem_context) {
187 return new BrowsingDataFileSystemHelperImpl(filesystem_context);
188 }
189
CannedBrowsingDataFileSystemHelper(Profile * profile)190 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper(
191 Profile* profile) {
192 }
193
CannedBrowsingDataFileSystemHelper()194 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper() {
195 }
196
~CannedBrowsingDataFileSystemHelper()197 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {}
198
199 CannedBrowsingDataFileSystemHelper*
Clone()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_;
207 return clone;
208 }
209
AddFileSystem(const GURL & origin,const fileapi::FileSystemType type,const int64 size)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();
221 ++file_system) {
222 if (file_system->origin == origin) {
223 file_system->usage_map[type] = size;
224 duplicate_origin = true;
225 break;
226 }
227 }
228 if (duplicate_origin)
229 return;
230
231 if (!BrowsingDataHelper::HasWebScheme(origin))
232 return; // Non-websafe state is not considered browsing data.
233
234 FileSystemInfo info(origin);
235 info.usage_map[type] = size;
236 file_system_info_.push_back(info);
237 }
238
Reset()239 void CannedBrowsingDataFileSystemHelper::Reset() {
240 file_system_info_.clear();
241 }
242
empty() const243 bool CannedBrowsingDataFileSystemHelper::empty() const {
244 return file_system_info_.empty();
245 }
246
GetFileSystemCount() const247 size_t CannedBrowsingDataFileSystemHelper::GetFileSystemCount() const {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 return file_system_info_.size();
250 }
251
StartFetching(const base::Callback<void (const std::list<FileSystemInfo> &)> & callback)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());
256
257 BrowserThread::PostTask(
258 BrowserThread::UI, FROM_HERE, base::Bind(callback, file_system_info_));
259 }
260