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"
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
;
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_
);
43 void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string
& host
) {
44 if (!io_thread_
->BelongsToCurrentThread()) {
47 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota
, this, host
));
51 quota_manager_
->SetPersistentHostQuota(
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
),
66 DCHECK(quota_manager
);
69 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {}
71 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() {
72 if (!io_thread_
->BelongsToCurrentThread()) {
75 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo
, this));
79 quota_manager_
->GetOriginsModifiedSince(
80 storage::kStorageTypeTemporary
,
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
,
102 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
103 weak_factory_
.GetWeakPtr()));
104 } else if (type
== storage::kStorageTypePersistent
) {
105 quota_manager_
->GetOriginsModifiedSince(
106 storage::kStorageTypeSyncable
,
108 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
109 weak_factory_
.GetWeakPtr()));
111 DCHECK(type
== storage::kStorageTypeSyncable
);
112 ProcessPendingHosts();
116 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() {
117 if (pending_hosts_
.empty()) {
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(
134 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage
,
135 weak_factory_
.GetWeakPtr(), host
, type
));
138 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string
& host
,
139 storage::StorageType type
,
142 case storage::kStorageTypeTemporary
:
143 quota_info_
[host
].temporary_usage
= usage
;
145 case storage::kStorageTypePersistent
:
146 quota_info_
[host
].persistent_usage
= usage
;
148 case storage::kStorageTypeSyncable
:
149 quota_info_
[host
].syncable_usage
= usage
;
154 ProcessPendingHosts();
157 void BrowsingDataQuotaHelperImpl::OnComplete() {
158 if (!ui_thread_
->BelongsToCurrentThread()) {
159 ui_thread_
->PostTask(
161 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete
, this));
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)
176 info
.host
= pair
.first
;
177 result
.push_back(info
);
180 callback_
.Run(result
);
184 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
185 storage::QuotaStatusCode status_unused
,
186 int64 quota_unused
) {