Return backed up TemplateURL on default search change
[chromium-blink-merge.git] / chrome / browser / browsing_data_quota_helper_impl.cc
blobb1980689be997da07907cf9991e67a18553cfcc9
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"
7 #include <map>
8 #include <set>
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;
17 // static
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_);
30 callback_ = callback;
31 quota_info_.clear();
32 is_fetching_ = true;
34 FetchQuotaInfo();
37 void BrowsingDataQuotaHelperImpl::CancelNotification() {
38 callback_.Reset();
41 void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string& host) {
42 if (!io_thread_->BelongsToCurrentThread()) {
43 io_thread_->PostTask(
44 FROM_HERE,
45 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota, this, host));
46 return;
49 quota_manager_->SetPersistentHostQuota(
50 host, 0,
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),
61 is_fetching_(false),
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()) {
72 io_thread_->PostTask(
73 FROM_HERE,
74 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo, this));
75 return;
78 quota_manager_->GetOriginsModifiedSince(
79 quota::kStorageTypeTemporary,
80 base::Time(),
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();
88 itr != origins.end();
89 ++itr)
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,
98 base::Time(),
99 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins,
100 weak_factory_.GetWeakPtr()));
101 } else {
102 // type == quota::kStorageTypePersistent
103 ProcessPendingHosts();
107 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() {
108 if (pending_hosts_.empty()) {
109 OnComplete();
110 return;
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(
124 host, type,
125 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage,
126 weak_factory_.GetWeakPtr()));
129 void 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();
142 ProcessPendingHosts();
145 void BrowsingDataQuotaHelperImpl::OnComplete() {
146 // Check if CancelNotification was called
147 if (callback_.is_null())
148 return;
150 if (!ui_thread_->BelongsToCurrentThread()) {
151 ui_thread_->PostTask(
152 FROM_HERE,
153 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete, this));
154 return;
157 is_fetching_ = false;
159 QuotaInfoArray result;
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;
170 info->host = itr->first;
171 result.push_back(*info);
174 callback_.Run(result);
175 callback_.Reset();
178 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota(
179 quota::QuotaStatusCode status_unused,
180 const std::string& host_unused,
181 quota::StorageType type_unused,
182 int64 quota_unused) {