1 // Copyright (c) 2011 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_quota_helper_impl.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "webkit/quota/quota_manager.h"
15 using content::BrowserThread
;
18 BrowsingDataQuotaHelper
* BrowsingDataQuotaHelper::Create(Profile
* profile
) {
19 return new BrowsingDataQuotaHelperImpl(
20 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
21 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
22 profile
->GetQuotaManager());
25 void BrowsingDataQuotaHelperImpl::StartFetching(
26 const FetchResultCallback
& callback
) {
27 DCHECK_EQ(false, callback
.is_null());
28 DCHECK(callback_
.is_null());
29 DCHECK(!is_fetching_
);
37 void BrowsingDataQuotaHelperImpl::CancelNotification() {
41 void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string
& host
) {
42 if (!io_thread_
->BelongsToCurrentThread()) {
45 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota
, this, host
));
49 quota_manager_
->SetPersistentHostQuota(
51 base::Bind(&BrowsingDataQuotaHelperImpl::DidRevokeHostQuota
,
52 weak_factory_
.GetWeakPtr()));
55 BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl(
56 base::MessageLoopProxy
* ui_thread
,
57 base::MessageLoopProxy
* io_thread
,
58 quota::QuotaManager
* quota_manager
)
59 : BrowsingDataQuotaHelper(io_thread
),
60 quota_manager_(quota_manager
),
62 ui_thread_(ui_thread
),
63 io_thread_(io_thread
),
64 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
65 DCHECK(quota_manager
);
68 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {}
70 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() {
71 if (!io_thread_
->BelongsToCurrentThread()) {
74 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo
, this));
78 quota_manager_
->GetOriginsModifiedSince(
79 quota::kStorageTypeTemporary
,
81 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
82 weak_factory_
.GetWeakPtr()));
85 void BrowsingDataQuotaHelperImpl::GotOrigins(
86 const std::set
<GURL
>& origins
, quota::StorageType type
) {
87 for (std::set
<GURL
>::const_iterator itr
= origins
.begin();
90 pending_hosts_
.insert(std::make_pair(itr
->host(), type
));
92 DCHECK(type
== quota::kStorageTypeTemporary
||
93 type
== quota::kStorageTypePersistent
);
95 if (type
== quota::kStorageTypeTemporary
) {
96 quota_manager_
->GetOriginsModifiedSince(
97 quota::kStorageTypePersistent
,
99 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
100 weak_factory_
.GetWeakPtr()));
102 // type == quota::kStorageTypePersistent
103 ProcessPendingHosts();
107 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() {
108 if (pending_hosts_
.empty()) {
113 PendingHosts::iterator itr
= pending_hosts_
.begin();
114 std::string host
= itr
->first
;
115 quota::StorageType type
= itr
->second
;
116 pending_hosts_
.erase(itr
);
117 GetHostUsage(host
, type
);
120 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string
& host
,
121 quota::StorageType type
) {
122 DCHECK(quota_manager_
.get());
123 quota_manager_
->GetHostUsage(
125 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage
,
126 weak_factory_
.GetWeakPtr()));
129 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string
& host
,
130 quota::StorageType type
,
133 case quota::kStorageTypeTemporary
:
134 quota_info_
[host
].temporary_usage
= usage
;
136 case quota::kStorageTypePersistent
:
137 quota_info_
[host
].persistent_usage
= usage
;
142 ProcessPendingHosts();
145 void BrowsingDataQuotaHelperImpl::OnComplete() {
146 // Check if CancelNotification was called
147 if (callback_
.is_null())
150 if (!ui_thread_
->BelongsToCurrentThread()) {
151 ui_thread_
->PostTask(
153 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete
, this));
157 is_fetching_
= false;
159 QuotaInfoArray result
;
161 for (std::map
<std::string
, QuotaInfo
>::iterator itr
= quota_info_
.begin();
162 itr
!= quota_info_
.end();
164 QuotaInfo
* info
= &itr
->second
;
165 // Skip unused entries
166 if (info
->temporary_usage
<= 0 &&
167 info
->persistent_usage
<= 0)
170 info
->host
= itr
->first
;
171 result
.push_back(*info
);
174 callback_
.Run(result
);
178 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
179 quota::QuotaStatusCode status_unused
,
180 const std::string
& host_unused
,
181 quota::StorageType type_unused
,
182 int64 quota_unused
) {