Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_quota_helper_impl.cc
blob902c71f4830deae6eba77303d998b3838e7d6239
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_quota_helper_impl.h"
7 #include <map>
8 #include <set>
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "chrome/browser/browsing_data/browsing_data_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "storage/browser/quota/quota_manager.h"
20 using content::BrowserThread;
21 using content::BrowserContext;
23 // static
24 BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) {
25 return new BrowsingDataQuotaHelperImpl(
26 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
27 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
28 BrowserContext::GetDefaultStoragePartition(profile)->GetQuotaManager());
31 void BrowsingDataQuotaHelperImpl::StartFetching(
32 const FetchResultCallback& callback) {
33 DCHECK(!callback.is_null());
34 DCHECK(callback_.is_null());
35 DCHECK(!is_fetching_);
36 callback_ = callback;
37 quota_info_.clear();
38 is_fetching_ = true;
40 FetchQuotaInfo();
43 void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string& host) {
44 if (!io_thread_->BelongsToCurrentThread()) {
45 io_thread_->PostTask(
46 FROM_HERE,
47 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota, this, host));
48 return;
51 quota_manager_->SetPersistentHostQuota(
52 host, 0,
53 base::Bind(&BrowsingDataQuotaHelperImpl::DidRevokeHostQuota,
54 weak_factory_.GetWeakPtr()));
57 BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl(
58 base::SingleThreadTaskRunner* ui_thread,
59 base::SingleThreadTaskRunner* io_thread,
60 storage::QuotaManager* quota_manager)
61 : BrowsingDataQuotaHelper(io_thread),
62 quota_manager_(quota_manager),
63 ui_thread_(ui_thread),
64 io_thread_(io_thread),
65 weak_factory_(this) {
66 DCHECK(quota_manager);
69 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {}
71 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() {
72 if (!io_thread_->BelongsToCurrentThread()) {
73 io_thread_->PostTask(
74 FROM_HERE,
75 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo, this));
76 return;
79 quota_manager_->GetOriginsModifiedSince(
80 storage::kStorageTypeTemporary,
81 base::Time(),
82 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins,
83 weak_factory_.GetWeakPtr()));
86 void BrowsingDataQuotaHelperImpl::GotOrigins(const std::set<GURL>& origins,
87 storage::StorageType type) {
88 for (const GURL& url : origins) {
89 if (BrowsingDataHelper::HasWebScheme(url))
90 pending_hosts_.insert(std::make_pair(url.host(), type));
93 DCHECK(type == storage::kStorageTypeTemporary ||
94 type == storage::kStorageTypePersistent ||
95 type == storage::kStorageTypeSyncable);
97 // Calling GetOriginsModifiedSince() for all types by chaining callbacks.
98 if (type == storage::kStorageTypeTemporary) {
99 quota_manager_->GetOriginsModifiedSince(
100 storage::kStorageTypePersistent,
101 base::Time(),
102 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins,
103 weak_factory_.GetWeakPtr()));
104 } else if (type == storage::kStorageTypePersistent) {
105 quota_manager_->GetOriginsModifiedSince(
106 storage::kStorageTypeSyncable,
107 base::Time(),
108 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins,
109 weak_factory_.GetWeakPtr()));
110 } else {
111 DCHECK(type == storage::kStorageTypeSyncable);
112 ProcessPendingHosts();
116 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() {
117 if (pending_hosts_.empty()) {
118 OnComplete();
119 return;
122 PendingHosts::iterator itr = pending_hosts_.begin();
123 std::string host = itr->first;
124 storage::StorageType type = itr->second;
125 pending_hosts_.erase(itr);
126 GetHostUsage(host, type);
129 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host,
130 storage::StorageType type) {
131 DCHECK(quota_manager_.get());
132 quota_manager_->GetHostUsage(
133 host, type,
134 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage,
135 weak_factory_.GetWeakPtr(), host, type));
138 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host,
139 storage::StorageType type,
140 int64 usage) {
141 switch (type) {
142 case storage::kStorageTypeTemporary:
143 quota_info_[host].temporary_usage = usage;
144 break;
145 case storage::kStorageTypePersistent:
146 quota_info_[host].persistent_usage = usage;
147 break;
148 case storage::kStorageTypeSyncable:
149 quota_info_[host].syncable_usage = usage;
150 break;
151 default:
152 NOTREACHED();
154 ProcessPendingHosts();
157 void BrowsingDataQuotaHelperImpl::OnComplete() {
158 if (!ui_thread_->BelongsToCurrentThread()) {
159 ui_thread_->PostTask(
160 FROM_HERE,
161 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete, this));
162 return;
165 is_fetching_ = false;
167 QuotaInfoArray result;
169 for (auto& pair : quota_info_) {
170 QuotaInfo& info = pair.second;
171 // Skip unused entries
172 if (info.temporary_usage <= 0 && info.persistent_usage <= 0 &&
173 info.syncable_usage <= 0)
174 continue;
176 info.host = pair.first;
177 result.push_back(info);
180 callback_.Run(result);
181 callback_.Reset();
184 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
185 storage::QuotaStatusCode status_unused,
186 int64 quota_unused) {