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 "webkit/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_EQ(false, 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::MessageLoopProxy
* ui_thread
,
59 base::MessageLoopProxy
* io_thread
,
60 quota::QuotaManager
* quota_manager
)
61 : BrowsingDataQuotaHelper(io_thread
),
62 quota_manager_(quota_manager
),
64 ui_thread_(ui_thread
),
65 io_thread_(io_thread
),
67 DCHECK(quota_manager
);
70 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {}
72 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() {
73 if (!io_thread_
->BelongsToCurrentThread()) {
76 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo
, this));
80 quota_manager_
->GetOriginsModifiedSince(
81 quota::kStorageTypeTemporary
,
83 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
84 weak_factory_
.GetWeakPtr()));
87 void BrowsingDataQuotaHelperImpl::GotOrigins(
88 const std::set
<GURL
>& origins
, quota::StorageType type
) {
89 for (std::set
<GURL
>::const_iterator itr
= origins
.begin();
92 if (BrowsingDataHelper::HasWebScheme(*itr
))
93 pending_hosts_
.insert(std::make_pair(itr
->host(), type
));
95 DCHECK(type
== quota::kStorageTypeTemporary
||
96 type
== quota::kStorageTypePersistent
||
97 type
== quota::kStorageTypeSyncable
);
99 // Calling GetOriginsModifiedSince() for all types by chaining callbacks.
100 if (type
== quota::kStorageTypeTemporary
) {
101 quota_manager_
->GetOriginsModifiedSince(
102 quota::kStorageTypePersistent
,
104 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
105 weak_factory_
.GetWeakPtr()));
106 } else if (type
== quota::kStorageTypePersistent
) {
107 quota_manager_
->GetOriginsModifiedSince(
108 quota::kStorageTypeSyncable
,
110 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
111 weak_factory_
.GetWeakPtr()));
113 DCHECK(type
== quota::kStorageTypeSyncable
);
114 ProcessPendingHosts();
118 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() {
119 if (pending_hosts_
.empty()) {
124 PendingHosts::iterator itr
= pending_hosts_
.begin();
125 std::string host
= itr
->first
;
126 quota::StorageType type
= itr
->second
;
127 pending_hosts_
.erase(itr
);
128 GetHostUsage(host
, type
);
131 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string
& host
,
132 quota::StorageType type
) {
133 DCHECK(quota_manager_
.get());
134 quota_manager_
->GetHostUsage(
136 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage
,
137 weak_factory_
.GetWeakPtr(), host
, type
));
140 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string
& host
,
141 quota::StorageType type
,
144 case quota::kStorageTypeTemporary
:
145 quota_info_
[host
].temporary_usage
= usage
;
147 case quota::kStorageTypePersistent
:
148 quota_info_
[host
].persistent_usage
= usage
;
150 case quota::kStorageTypeSyncable
:
151 quota_info_
[host
].syncable_usage
= usage
;
156 ProcessPendingHosts();
159 void BrowsingDataQuotaHelperImpl::OnComplete() {
160 if (!ui_thread_
->BelongsToCurrentThread()) {
161 ui_thread_
->PostTask(
163 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete
, this));
167 is_fetching_
= false;
169 QuotaInfoArray result
;
171 for (std::map
<std::string
, QuotaInfo
>::iterator itr
= quota_info_
.begin();
172 itr
!= quota_info_
.end();
174 QuotaInfo
* info
= &itr
->second
;
175 // Skip unused entries
176 if (info
->temporary_usage
<= 0 &&
177 info
->persistent_usage
<= 0 &&
178 info
->syncable_usage
<= 0)
181 info
->host
= itr
->first
;
182 result
.push_back(*info
);
185 callback_
.Run(result
);
189 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
190 quota::QuotaStatusCode status_unused
,
191 int64 quota_unused
) {