OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/browsing_data_quota_helper_impl.h" | |
6 | |
7 #include <map> | |
8 #include <set> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/logging.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/common/url_constants.h" | |
14 #include "chrome/browser/browsing_data_helper.h" | |
15 #include "webkit/quota/quota_manager.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 // static | |
20 BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) { | |
21 return new BrowsingDataQuotaHelperImpl( | |
22 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | |
23 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | |
24 content::BrowserContext::GetQuotaManager(profile)); | |
25 } | |
26 | |
27 void BrowsingDataQuotaHelperImpl::StartFetching( | |
28 const FetchResultCallback& callback) { | |
29 DCHECK_EQ(false, callback.is_null()); | |
30 DCHECK(callback_.is_null()); | |
31 DCHECK(!is_fetching_); | |
32 callback_ = callback; | |
33 quota_info_.clear(); | |
34 is_fetching_ = true; | |
35 | |
36 FetchQuotaInfo(); | |
37 } | |
38 | |
39 void BrowsingDataQuotaHelperImpl::RevokeHostQuota(const std::string& host) { | |
40 if (!io_thread_->BelongsToCurrentThread()) { | |
41 io_thread_->PostTask( | |
42 FROM_HERE, | |
43 base::Bind(&BrowsingDataQuotaHelperImpl::RevokeHostQuota, this, host)); | |
44 return; | |
45 } | |
46 | |
47 quota_manager_->SetPersistentHostQuota( | |
48 host, 0, | |
49 base::Bind(&BrowsingDataQuotaHelperImpl::DidRevokeHostQuota, | |
50 weak_factory_.GetWeakPtr())); | |
51 } | |
52 | |
53 BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl( | |
54 base::MessageLoopProxy* ui_thread, | |
55 base::MessageLoopProxy* io_thread, | |
56 quota::QuotaManager* quota_manager) | |
57 : BrowsingDataQuotaHelper(io_thread), | |
58 quota_manager_(quota_manager), | |
59 is_fetching_(false), | |
60 ui_thread_(ui_thread), | |
61 io_thread_(io_thread), | |
62 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
63 DCHECK(quota_manager); | |
64 } | |
65 | |
66 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {} | |
67 | |
68 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() { | |
69 if (!io_thread_->BelongsToCurrentThread()) { | |
70 io_thread_->PostTask( | |
71 FROM_HERE, | |
72 base::Bind(&BrowsingDataQuotaHelperImpl::FetchQuotaInfo, this)); | |
73 return; | |
74 } | |
75 | |
76 quota_manager_->GetOriginsModifiedSince( | |
77 quota::kStorageTypeTemporary, | |
78 base::Time(), | |
79 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins, | |
80 weak_factory_.GetWeakPtr())); | |
81 } | |
82 | |
83 void BrowsingDataQuotaHelperImpl::GotOrigins( | |
84 const std::set<GURL>& origins, quota::StorageType type) { | |
85 for (std::set<GURL>::const_iterator itr = origins.begin(); | |
86 itr != origins.end(); | |
87 ++itr) | |
88 if (BrowsingDataHelper::HasWebScheme(*itr)) | |
89 pending_hosts_.insert(std::make_pair(itr->host(), type)); | |
90 | |
91 DCHECK(type == quota::kStorageTypeTemporary || | |
92 type == quota::kStorageTypePersistent); | |
93 | |
94 if (type == quota::kStorageTypeTemporary) { | |
95 quota_manager_->GetOriginsModifiedSince( | |
96 quota::kStorageTypePersistent, | |
97 base::Time(), | |
98 base::Bind(&BrowsingDataQuotaHelperImpl::GotOrigins, | |
99 weak_factory_.GetWeakPtr())); | |
100 } else { | |
101 // type == quota::kStorageTypePersistent | |
102 ProcessPendingHosts(); | |
103 } | |
104 } | |
105 | |
106 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() { | |
107 if (pending_hosts_.empty()) { | |
108 OnComplete(); | |
109 return; | |
110 } | |
111 | |
112 PendingHosts::iterator itr = pending_hosts_.begin(); | |
113 std::string host = itr->first; | |
114 quota::StorageType type = itr->second; | |
115 pending_hosts_.erase(itr); | |
116 GetHostUsage(host, type); | |
117 } | |
118 | |
119 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host, | |
120 quota::StorageType type) { | |
121 DCHECK(quota_manager_.get()); | |
122 quota_manager_->GetHostUsage( | |
123 host, type, | |
124 base::Bind(&BrowsingDataQuotaHelperImpl::GotHostUsage, | |
125 weak_factory_.GetWeakPtr())); | |
126 } | |
127 | |
128 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host, | |
129 quota::StorageType type, | |
130 int64 usage) { | |
131 switch (type) { | |
132 case quota::kStorageTypeTemporary: | |
133 quota_info_[host].temporary_usage = usage; | |
134 break; | |
135 case quota::kStorageTypePersistent: | |
136 quota_info_[host].persistent_usage = usage; | |
137 break; | |
138 default: | |
139 NOTREACHED(); | |
140 } | |
141 ProcessPendingHosts(); | |
142 } | |
143 | |
144 void BrowsingDataQuotaHelperImpl::OnComplete() { | |
145 if (!ui_thread_->BelongsToCurrentThread()) { | |
146 ui_thread_->PostTask( | |
147 FROM_HERE, | |
148 base::Bind(&BrowsingDataQuotaHelperImpl::OnComplete, this)); | |
149 return; | |
150 } | |
151 | |
152 is_fetching_ = false; | |
153 | |
154 QuotaInfoArray result; | |
155 | |
156 for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin(); | |
157 itr != quota_info_.end(); | |
158 ++itr) { | |
159 QuotaInfo* info = &itr->second; | |
160 // Skip unused entries | |
161 if (info->temporary_usage <= 0 && | |
162 info->persistent_usage <= 0) | |
163 continue; | |
164 | |
165 info->host = itr->first; | |
166 result.push_back(*info); | |
167 } | |
168 | |
169 callback_.Run(result); | |
170 callback_.Reset(); | |
171 } | |
172 | |
173 void BrowsingDataQuotaHelperImpl::DidRevokeHostQuota( | |
174 quota::QuotaStatusCode status_unused, | |
175 const std::string& host_unused, | |
176 quota::StorageType type_unused, | |
177 int64 quota_unused) { | |
178 } | |
OLD | NEW |