blob: b1980689be997da07907cf9991e67a18553cfcc9 [file] [log] [blame]
[email protected]a9e3ec42011-08-09 06:48:301// 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.
4
5#include "chrome/browser/browsing_data_quota_helper_impl.h"
6
7#include <map>
8#include <set>
9
[email protected]4d99be52011-10-18 14:11:0310#include "base/bind.h"
[email protected]a9e3ec42011-08-09 06:48:3011#include "base/logging.h"
12#include "chrome/browser/profiles/profile.h"
13#include "webkit/quota/quota_manager.h"
14
[email protected]631bb742011-11-02 11:29:3915using content::BrowserThread;
16
[email protected]a9e3ec42011-08-09 06:48:3017// static
18BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) {
19 return new BrowsingDataQuotaHelperImpl(
20 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
21 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
22 profile->GetQuotaManager());
23}
24
[email protected]91b06c02011-11-10 06:17:1325void BrowsingDataQuotaHelperImpl::StartFetching(
26 const FetchResultCallback& callback) {
27 DCHECK_EQ(false, callback.is_null());
28 DCHECK(callback_.is_null());
[email protected]a9e3ec42011-08-09 06:48:3029 DCHECK(!is_fetching_);
[email protected]91b06c02011-11-10 06:17:1330 callback_ = callback;
[email protected]a9e3ec42011-08-09 06:48:3031 quota_info_.clear();
32 is_fetching_ = true;
33
34 FetchQuotaInfo();
35}
36
37void BrowsingDataQuotaHelperImpl::CancelNotification() {
[email protected]91b06c02011-11-10 06:17:1338 callback_.Reset();
[email protected]a9e3ec42011-08-09 06:48:3039}
40
[email protected]7b963282011-10-03 06:00:1241void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string& host) {
42 if (!io_thread_->BelongsToCurrentThread()) {
43 io_thread_->PostTask(
44 FROM_HERE,
[email protected]006284f02011-10-19 22:06:2345 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota, this, host));
[email protected]7b963282011-10-03 06:00:1246 return;
47 }
48
49 quota_manager_->SetPersistentHostQuota(
[email protected]4d99be52011-10-18 14:11:0350 host, 0,
51 base::Bind(&BrowsingDataQuotaHelperImpl::DidRevokeHostQuota,
52 weak_factory_.GetWeakPtr()));
[email protected]7b963282011-10-03 06:00:1253}
54
55BrowsingDataQuotaHelperImpl::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),
61 is_fetching_(false),
62 ui_thread_(ui_thread),
63 io_thread_(io_thread),
[email protected]4d99be52011-10-18 14:11:0364 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]7b963282011-10-03 06:00:1265 DCHECK(quota_manager);
66}
67
68BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {}
69
[email protected]a9e3ec42011-08-09 06:48:3070void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() {
71 if (!io_thread_->BelongsToCurrentThread()) {
72 io_thread_->PostTask(
73 FROM_HERE,
[email protected]006284f02011-10-19 22:06:2374 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo, this));
[email protected]a9e3ec42011-08-09 06:48:3075 return;
76 }
77
78 quota_manager_->GetOriginsModifiedSince(
79 quota::kStorageTypeTemporary,
80 base::Time(),
[email protected]4d99be52011-10-18 14:11:0381 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins,
82 weak_factory_.GetWeakPtr()));
[email protected]a9e3ec42011-08-09 06:48:3083}
84
85void BrowsingDataQuotaHelperImpl::GotOrigins(
86 const std::set<GURL>& origins, quota::StorageType type) {
87 for (std::set<GURL>::const_iterator itr = origins.begin();
88 itr != origins.end();
89 ++itr)
90 pending_hosts_.insert(std::make_pair(itr->host(), type));
91
92 DCHECK(type == quota::kStorageTypeTemporary ||
93 type == quota::kStorageTypePersistent);
94
95 if (type == quota::kStorageTypeTemporary) {
96 quota_manager_->GetOriginsModifiedSince(
97 quota::kStorageTypePersistent,
98 base::Time(),
[email protected]4d99be52011-10-18 14:11:0399 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins,
100 weak_factory_.GetWeakPtr()));
[email protected]a9e3ec42011-08-09 06:48:30101 } else {
102 // type == quota::kStorageTypePersistent
103 ProcessPendingHosts();
104 }
105}
106
107void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() {
108 if (pending_hosts_.empty()) {
109 OnComplete();
110 return;
111 }
112
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);
118}
119
120void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host,
121 quota::StorageType type) {
122 DCHECK(quota_manager_.get());
123 quota_manager_->GetHostUsage(
124 host, type,
[email protected]4d99be52011-10-18 14:11:03125 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage,
126 weak_factory_.GetWeakPtr()));
[email protected]a9e3ec42011-08-09 06:48:30127}
128
129void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host,
130 quota::StorageType type,
131 int64 usage) {
132 switch (type) {
133 case quota::kStorageTypeTemporary:
134 quota_info_[host].temporary_usage = usage;
135 break;
136 case quota::kStorageTypePersistent:
137 quota_info_[host].persistent_usage = usage;
138 break;
139 default:
140 NOTREACHED();
141 }
142 ProcessPendingHosts();
143}
144
145void BrowsingDataQuotaHelperImpl::OnComplete() {
146 // Check if CancelNotification was called
[email protected]91b06c02011-11-10 06:17:13147 if (callback_.is_null())
[email protected]a9e3ec42011-08-09 06:48:30148 return;
149
150 if (!ui_thread_->BelongsToCurrentThread()) {
151 ui_thread_->PostTask(
152 FROM_HERE,
[email protected]006284f02011-10-19 22:06:23153 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete, this));
[email protected]a9e3ec42011-08-09 06:48:30154 return;
155 }
156
157 is_fetching_ = false;
158
159 QuotaInfoArray result;
[email protected]a9e3ec42011-08-09 06:48:30160
161 for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin();
162 itr != quota_info_.end();
163 ++itr) {
164 QuotaInfo* info = &itr->second;
165 // Skip unused entries
166 if (info->temporary_usage <= 0 &&
167 info->persistent_usage <= 0)
168 continue;
169
170 info->host = itr->first;
171 result.push_back(*info);
172 }
173
[email protected]91b06c02011-11-10 06:17:13174 callback_.Run(result);
175 callback_.Reset();
[email protected]a9e3ec42011-08-09 06:48:30176}
[email protected]7b963282011-10-03 06:00:12177
178void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
179 quota::QuotaStatusCode status_unused,
180 const std::string& host_unused,
181 quota::StorageType type_unused,
182 int64 quota_unused) {
183}