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/barrier_closure.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "chrome/browser/browsing_data/browsing_data_helper.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/storage_partition.h"
21 #include "storage/browser/quota/quota_manager.h"
23 using content::BrowserThread
;
24 using content::BrowserContext
;
27 BrowsingDataQuotaHelper
* BrowsingDataQuotaHelper::Create(Profile
* profile
) {
28 return new BrowsingDataQuotaHelperImpl(
29 BrowserContext::GetDefaultStoragePartition(profile
)->GetQuotaManager());
32 void BrowsingDataQuotaHelperImpl::StartFetching(
33 const FetchResultCallback
& callback
) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
35 DCHECK(!callback
.is_null());
37 BrowserThread::PostTask(
38 BrowserThread::IO
, FROM_HERE
,
39 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfoOnIOThread
, this,
43 void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string
& host
) {
44 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
45 BrowserThread::PostTask(
46 BrowserThread::IO
, FROM_HERE
,
47 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuotaOnIOThread
, this,
51 BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl(
52 storage::QuotaManager
* quota_manager
)
53 : BrowsingDataQuotaHelper(),
54 quota_manager_(quota_manager
),
56 DCHECK(quota_manager
);
59 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {}
61 void BrowsingDataQuotaHelperImpl::FetchQuotaInfoOnIOThread(
62 const FetchResultCallback
& callback
) {
63 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
65 const storage::StorageType types
[] = {storage::kStorageTypeTemporary
,
66 storage::kStorageTypePersistent
,
67 storage::kStorageTypeSyncable
};
69 // Query hosts for each storage types. When complete, process the collected
71 PendingHosts
* pending_hosts
= new PendingHosts();
72 base::Closure completion
= base::BarrierClosure(
74 base::Bind(&BrowsingDataQuotaHelperImpl::OnGetOriginsComplete
,
75 weak_factory_
.GetWeakPtr(), callback
,
76 base::Owned(pending_hosts
)));
78 for (const storage::StorageType
& type
: types
) {
79 quota_manager_
->GetOriginsModifiedSince(
81 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins
,
82 weak_factory_
.GetWeakPtr(), pending_hosts
, completion
));
86 void BrowsingDataQuotaHelperImpl::GotOrigins(PendingHosts
* pending_hosts
,
87 const base::Closure
& completion
,
88 const std::set
<GURL
>& origins
,
89 storage::StorageType type
) {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
91 for (const GURL
& url
: origins
) {
92 if (!BrowsingDataHelper::HasWebScheme(url
))
93 continue; // Non-websafe state is not considered browsing data.
94 pending_hosts
->insert(std::make_pair(url
.host(), type
));
99 void BrowsingDataQuotaHelperImpl::OnGetOriginsComplete(
100 const FetchResultCallback
& callback
,
101 PendingHosts
* pending_hosts
) {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
104 // Query usage for each (host, type). When complete, process the results.
105 QuotaInfoMap
* quota_info
= new QuotaInfoMap();
106 base::Closure completion
= base::BarrierClosure(
107 pending_hosts
->size(),
108 base::Bind(&BrowsingDataQuotaHelperImpl::OnGetHostsUsageComplete
,
109 weak_factory_
.GetWeakPtr(), callback
,
110 base::Owned(quota_info
)));
112 for (const auto& itr
: *pending_hosts
) {
113 const std::string
& host
= itr
.first
;
114 storage::StorageType type
= itr
.second
;
115 quota_manager_
->GetHostUsage(
116 host
, type
, base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage
,
117 weak_factory_
.GetWeakPtr(), quota_info
,
118 completion
, host
, type
));
122 void BrowsingDataQuotaHelperImpl::GotHostUsage(QuotaInfoMap
* quota_info
,
123 const base::Closure
& completion
,
124 const std::string
& host
,
125 storage::StorageType type
,
127 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
129 case storage::kStorageTypeTemporary
:
130 (*quota_info
)[host
].temporary_usage
= usage
;
132 case storage::kStorageTypePersistent
:
133 (*quota_info
)[host
].persistent_usage
= usage
;
135 case storage::kStorageTypeSyncable
:
136 (*quota_info
)[host
].syncable_usage
= usage
;
144 void BrowsingDataQuotaHelperImpl::OnGetHostsUsageComplete(
145 const FetchResultCallback
& callback
,
146 QuotaInfoMap
* quota_info
) {
147 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
149 QuotaInfoArray result
;
150 for (auto& pair
: *quota_info
) {
151 QuotaInfo
& info
= pair
.second
;
152 // Skip unused entries
153 if (info
.temporary_usage
<= 0 && info
.persistent_usage
<= 0 &&
154 info
.syncable_usage
<= 0)
157 info
.host
= pair
.first
;
158 result
.push_back(info
);
161 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
162 base::Bind(callback
, result
));
165 void BrowsingDataQuotaHelperImpl::RevokeHostQuotaOnIOThread(
166 const std::string
& host
) {
167 quota_manager_
->SetPersistentHostQuota(
168 host
, 0, base::Bind(&BrowsingDataQuotaHelperImpl::DidRevokeHostQuota
,
169 weak_factory_
.GetWeakPtr()));
172 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
173 storage::QuotaStatusCode
/*status*/,