Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_file_system_helper.cc
blob62c7ee453951bd247ca6a8a6534cb980b683a19f
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"
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"
19 using content::BrowserThread;
21 namespace fileapi {
22 class FileSystemContext;
25 namespace {
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;
39 private:
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
44 // task runner.
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
74 // UI thread.
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.
81 bool is_fetching_;
83 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl);
86 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl(
87 fileapi::FileSystemContext* filesystem_context)
88 : filesystem_context_(filesystem_context),
89 is_fetching_(false) {
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());
101 is_fetching_ = true;
102 completion_callback_ = callback;
103 file_task_runner()->PostTask(
104 FROM_HERE,
105 base::Bind(
106 &BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread,
107 this));
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));
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);
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;
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);
177 } // namespace
179 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo(
180 const GURL& origin) : origin(origin) {}
182 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {}
184 // static
185 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create(
186 fileapi::FileSystemContext* filesystem_context) {
187 return new BrowsingDataFileSystemHelperImpl(filesystem_context);
190 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper(
191 Profile* profile)
192 : is_fetching_(false) {
195 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper()
196 : is_fetching_(false) {
199 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {}
201 CannedBrowsingDataFileSystemHelper*
202 CannedBrowsingDataFileSystemHelper::Clone() {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
204 CannedBrowsingDataFileSystemHelper* clone =
205 new CannedBrowsingDataFileSystemHelper();
206 // This list only mutates on the UI thread, so it's safe to work with it here
207 // (given the DCHECK above).
208 clone->file_system_info_ = file_system_info_;
209 return clone;
212 void CannedBrowsingDataFileSystemHelper::AddFileSystem(
213 const GURL& origin, const fileapi::FileSystemType type, const int64 size) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 // This canned implementation of AddFileSystem uses an O(n^2) algorithm; which
216 // is fine, as it isn't meant for use in a high-volume context. If it turns
217 // out that we want to start using this in a context with many, many origins,
218 // we should think about reworking the implementation.
219 bool duplicate_origin = false;
220 for (std::list<FileSystemInfo>::iterator
221 file_system = file_system_info_.begin();
222 file_system != file_system_info_.end();
223 ++file_system) {
224 if (file_system->origin == origin) {
225 file_system->usage_map[type] = size;
226 duplicate_origin = true;
227 break;
230 if (duplicate_origin)
231 return;
233 if (!BrowsingDataHelper::HasWebScheme(origin))
234 return; // Non-websafe state is not considered browsing data.
236 FileSystemInfo info(origin);
237 info.usage_map[type] = size;
238 file_system_info_.push_back(info);
241 void CannedBrowsingDataFileSystemHelper::Reset() {
242 file_system_info_.clear();
245 bool CannedBrowsingDataFileSystemHelper::empty() const {
246 return file_system_info_.empty();
249 size_t CannedBrowsingDataFileSystemHelper::GetFileSystemCount() const {
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
251 return file_system_info_.size();
254 void CannedBrowsingDataFileSystemHelper::StartFetching(
255 const base::Callback<void(const std::list<FileSystemInfo>&)>& callback) {
256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
257 DCHECK(!callback.is_null());
259 BrowserThread::PostTask(
260 BrowserThread::UI, FROM_HERE, base::Bind(callback, file_system_info_));