Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_file_system_helper.cc
blob72c50a6c87b88e2211a5483ee2c0052a014a5dcf
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 <set>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/sequenced_task_runner.h"
13 #include "chrome/browser/browsing_data/browsing_data_helper.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "storage/browser/fileapi/file_system_context.h"
16 #include "storage/browser/fileapi/file_system_quota_util.h"
17 #include "storage/common/fileapi/file_system_types.h"
19 using content::BrowserThread;
21 namespace storage {
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 storage::FileSystemContext* filesystem_context);
35 void StartFetching(const FetchCallback& callback) override;
36 void DeleteFileSystemOrigin(const GURL& origin) override;
38 private:
39 ~BrowsingDataFileSystemHelperImpl() override;
41 // Enumerates all filesystem files, storing the resulting list into
42 // file_system_file_ for later use. This must be called on the file
43 // task runner.
44 void FetchFileSystemInfoInFileThread(const FetchCallback& callback);
46 // Deletes all file systems associated with |origin|. This must be called on
47 // the file task runner.
48 void DeleteFileSystemOriginInFileThread(const GURL& origin);
50 // Returns the file task runner for the |filesystem_context_|.
51 base::SequencedTaskRunner* file_task_runner() {
52 return filesystem_context_->default_file_task_runner();
55 // Keep a reference to the FileSystemContext object for the current profile
56 // for use on the file task runner.
57 scoped_refptr<storage::FileSystemContext> filesystem_context_;
59 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl);
62 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl(
63 storage::FileSystemContext* filesystem_context)
64 : filesystem_context_(filesystem_context) {
65 DCHECK(filesystem_context_.get());
68 BrowsingDataFileSystemHelperImpl::~BrowsingDataFileSystemHelperImpl() {
71 void BrowsingDataFileSystemHelperImpl::StartFetching(
72 const FetchCallback& callback) {
73 DCHECK_CURRENTLY_ON(BrowserThread::UI);
74 DCHECK(!callback.is_null());
75 file_task_runner()->PostTask(
76 FROM_HERE,
77 base::Bind(
78 &BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread,
79 this, callback));
82 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOrigin(
83 const GURL& origin) {
84 DCHECK_CURRENTLY_ON(BrowserThread::UI);
85 file_task_runner()->PostTask(
86 FROM_HERE,
87 base::Bind(
88 &BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread,
89 this, origin));
92 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread(
93 const FetchCallback& callback) {
94 DCHECK(file_task_runner()->RunsTasksOnCurrentThread());
95 DCHECK(!callback.is_null());
97 // We check usage for these filesystem types.
98 const storage::FileSystemType types[] = {
99 storage::kFileSystemTypeTemporary,
100 storage::kFileSystemTypePersistent,
101 #if defined(ENABLE_EXTENSIONS)
102 storage::kFileSystemTypeSyncable,
103 #endif
106 std::list<FileSystemInfo> result;
107 typedef std::map<GURL, FileSystemInfo> OriginInfoMap;
108 OriginInfoMap file_system_info_map;
109 for (size_t i = 0; i < arraysize(types); ++i) {
110 storage::FileSystemType type = types[i];
111 storage::FileSystemQuotaUtil* quota_util =
112 filesystem_context_->GetQuotaUtil(type);
113 DCHECK(quota_util);
114 std::set<GURL> origins;
115 quota_util->GetOriginsForTypeOnFileTaskRunner(type, &origins);
116 for (const GURL& current : origins) {
117 if (!BrowsingDataHelper::HasWebScheme(current))
118 continue; // Non-websafe state is not considered browsing data.
119 int64 usage = quota_util->GetOriginUsageOnFileTaskRunner(
120 filesystem_context_.get(), current, type);
121 OriginInfoMap::iterator inserted =
122 file_system_info_map.insert(
123 std::make_pair(current, FileSystemInfo(current))).first;
124 inserted->second.usage_map[type] = usage;
128 for (const auto& iter : file_system_info_map)
129 result.push_back(iter.second);
131 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
132 base::Bind(callback, result));
135 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread(
136 const GURL& origin) {
137 DCHECK(file_task_runner()->RunsTasksOnCurrentThread());
138 filesystem_context_->DeleteDataForOriginOnFileTaskRunner(origin);
141 } // namespace
143 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo(
144 const GURL& origin) : origin(origin) {}
146 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {}
148 // static
149 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create(
150 storage::FileSystemContext* filesystem_context) {
151 return new BrowsingDataFileSystemHelperImpl(filesystem_context);
154 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper(
155 Profile* profile) {
158 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {}
160 void CannedBrowsingDataFileSystemHelper::AddFileSystem(
161 const GURL& origin,
162 const storage::FileSystemType type,
163 const int64 size) {
164 DCHECK_CURRENTLY_ON(BrowserThread::UI);
165 // This canned implementation of AddFileSystem uses an O(n^2) algorithm; which
166 // is fine, as it isn't meant for use in a high-volume context. If it turns
167 // out that we want to start using this in a context with many, many origins,
168 // we should think about reworking the implementation.
169 bool duplicate_origin = false;
170 for (FileSystemInfo& file_system : file_system_info_) {
171 if (file_system.origin == origin) {
172 file_system.usage_map[type] = size;
173 duplicate_origin = true;
174 break;
177 if (duplicate_origin)
178 return;
180 if (!BrowsingDataHelper::HasWebScheme(origin))
181 return; // Non-websafe state is not considered browsing data.
183 FileSystemInfo info(origin);
184 info.usage_map[type] = size;
185 file_system_info_.push_back(info);
188 void CannedBrowsingDataFileSystemHelper::Reset() {
189 file_system_info_.clear();
192 bool CannedBrowsingDataFileSystemHelper::empty() const {
193 return file_system_info_.empty();
196 size_t CannedBrowsingDataFileSystemHelper::GetFileSystemCount() const {
197 DCHECK_CURRENTLY_ON(BrowserThread::UI);
198 return file_system_info_.size();
201 void CannedBrowsingDataFileSystemHelper::StartFetching(
202 const FetchCallback& callback) {
203 DCHECK_CURRENTLY_ON(BrowserThread::UI);
204 DCHECK(!callback.is_null());
206 BrowserThread::PostTask(
207 BrowserThread::UI, FROM_HERE, base::Bind(callback, file_system_info_));