OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/browsing_data_quota_helper_impl.h" | |
6 | |
7 #include <map> | |
8 #include <set> | |
9 | |
10 #include "base/logging.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "webkit/quota/quota_manager.h" | |
13 | |
14 BrowsingDataQuotaHelper::QuotaInfo::QuotaInfo() | |
15 : temporary_usage(-1), | |
16 persistent_usage(-1), | |
17 persistent_quota(-1) { } | |
18 | |
19 BrowsingDataQuotaHelper::QuotaInfo::QuotaInfo(const std::string& host) | |
20 : host(host), | |
21 temporary_usage(-1), | |
22 persistent_usage(-1), | |
23 persistent_quota(-1) { } | |
24 | |
25 BrowsingDataQuotaHelper::QuotaInfo::QuotaInfo(const std::string& host, | |
26 int64 temporary_usage, | |
27 int64 persistent_usage, | |
28 int64 persistent_quota) | |
29 : host(host), | |
30 temporary_usage(temporary_usage), | |
31 persistent_usage(persistent_usage), | |
32 persistent_quota(persistent_quota) { } | |
33 | |
34 BrowsingDataQuotaHelper::QuotaInfo::~QuotaInfo() { } | |
35 | |
36 // static | |
37 BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) { | |
38 return new BrowsingDataQuotaHelperImpl( | |
39 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | |
40 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | |
41 profile->GetQuotaManager()); | |
42 } | |
43 | |
44 BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl( | |
45 base::MessageLoopProxy* ui_thread, | |
46 base::MessageLoopProxy* io_thread, | |
47 quota::QuotaManager* quota_manager) | |
48 : quota_manager_(quota_manager), | |
49 is_fetching_(false), | |
50 ui_thread_(ui_thread), | |
51 io_thread_(io_thread), | |
52 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
53 DCHECK(quota_manager); | |
54 } | |
55 | |
56 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {} | |
57 | |
58 void BrowsingDataQuotaHelperImpl::StartFetching(FetchResultCallback* callback) { | |
59 DCHECK(callback); | |
60 DCHECK(!callback_.get()); | |
61 DCHECK(!is_fetching_); | |
62 callback_.reset(callback); | |
63 quota_info_.clear(); | |
64 is_fetching_ = true; | |
65 | |
66 FetchQuotaInfo(); | |
67 } | |
68 | |
69 void BrowsingDataQuotaHelperImpl::CancelNotification() { | |
70 callback_.reset(); | |
71 } | |
72 | |
73 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() { | |
74 if (!io_thread_->BelongsToCurrentThread()) { | |
75 io_thread_->PostTask( | |
76 FROM_HERE, | |
77 NewRunnableMethod( | |
78 this, | |
79 &BrowsingDataQuotaHelperImpl::FetchQuotaInfo)); | |
80 return; | |
81 } | |
82 | |
83 quota_manager_->GetOriginsModifiedSince( | |
84 quota::kStorageTypeTemporary, | |
85 base::Time(), | |
86 callback_factory_.NewCallback( | |
87 &BrowsingDataQuotaHelperImpl::GotTemporaryStorageOrigins)); | |
88 } | |
89 | |
90 void BrowsingDataQuotaHelperImpl::GotTemporaryStorageOrigins( | |
91 const std::set<GURL>& origins) { | |
92 GotOrigins(quota::kStorageTypeTemporary, origins); | |
93 | |
94 quota_manager_->GetOriginsModifiedSince( | |
95 quota::kStorageTypePersistent, | |
96 base::Time(), | |
97 callback_factory_.NewCallback( | |
98 &BrowsingDataQuotaHelperImpl::GotPersistentStorageOrigins)); | |
99 } | |
100 | |
101 void BrowsingDataQuotaHelperImpl::GotPersistentStorageOrigins( | |
102 const std::set<GURL>& origins) { | |
103 GotOrigins(quota::kStorageTypePersistent, origins); | |
104 | |
105 ProcessPendingHosts(); | |
106 } | |
107 | |
108 void BrowsingDataQuotaHelperImpl::GotOrigins( | |
109 quota::StorageType type, const std::set<GURL>& origins) { | |
110 for (std::set<GURL>::const_iterator itr = origins.begin(); | |
111 itr != origins.end(); | |
112 ++itr) | |
113 pending_hosts_.insert(std::make_pair(itr->host(), type)); | |
114 } | |
115 | |
116 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() { | |
117 if (pending_hosts_.empty()) { | |
118 OnComplete(); | |
119 return; | |
120 } | |
121 | |
122 PendingHosts::iterator itr = pending_hosts_.begin(); | |
123 std::string host = itr->first; | |
124 quota::StorageType type = itr->second; | |
125 pending_hosts_.erase(itr); | |
126 GetHostUsage(host, type); | |
127 } | |
128 | |
129 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host, | |
130 quota::StorageType type) { | |
kinuko
2011/07/25 13:22:41
nit: weird indentation
tzik
2011/07/26 10:52:41
Done.
| |
131 DCHECK(quota_manager_.get()); | |
132 quota_manager_->GetHostUsage( | |
133 host, type, | |
134 callback_factory_.NewCallback( | |
135 &BrowsingDataQuotaHelperImpl::GotHostUsage)); | |
136 } | |
137 | |
138 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host, | |
139 quota::StorageType type, | |
140 int64 usage) { | |
kinuko
2011/07/25 13:22:41
ditto
tzik
2011/07/26 10:52:41
Done.
| |
141 switch (type) { | |
142 case quota::kStorageTypeTemporary: | |
143 quota_info_[host].temporary_usage = usage; | |
144 break; | |
145 case quota::kStorageTypePersistent: | |
146 quota_info_[host].persistent_usage = usage; | |
147 break; | |
148 default: | |
149 NOTREACHED(); | |
150 } | |
151 ProcessPendingHosts(); | |
152 } | |
153 | |
154 void BrowsingDataQuotaHelperImpl::OnComplete() { | |
155 // Check if CancelNotification was called | |
156 if (!callback_.get()) | |
157 return; | |
158 | |
159 if (!ui_thread_->BelongsToCurrentThread()) { | |
160 ui_thread_->PostTask( | |
161 FROM_HERE, | |
162 NewRunnableMethod( | |
163 this, | |
164 &BrowsingDataQuotaHelperImpl::OnComplete)); | |
165 return; | |
166 } | |
167 | |
168 is_fetching_ = false; | |
169 | |
170 QuotaInfoList result; | |
171 result.reserve(quota_info_.size()); | |
172 | |
173 for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin(); | |
174 itr != quota_info_.end(); | |
175 ++itr) { | |
176 itr->second.host = itr->first; | |
177 result.push_back(itr->second); | |
178 } | |
179 | |
180 callback_->Run(result); | |
181 callback_.reset(); | |
182 } | |
183 | |
184 bool operator <(const BrowsingDataQuotaHelper::QuotaInfo& lhs, | |
185 const BrowsingDataQuotaHelper::QuotaInfo& rhs) { | |
186 if (lhs.host != rhs.host) | |
187 return lhs.host < rhs.host; | |
188 if (lhs.temporary_usage != rhs.temporary_usage) | |
189 return lhs.temporary_usage < rhs.temporary_usage; | |
190 if (lhs.persistent_usage != rhs.persistent_usage) | |
191 return lhs.persistent_usage < rhs.persistent_usage; | |
192 return lhs.persistent_quota < rhs.persistent_quota; | |
193 } | |
194 | |
195 bool operator ==(const BrowsingDataQuotaHelper::QuotaInfo& lhs, | |
196 const BrowsingDataQuotaHelper::QuotaInfo& rhs) { | |
kinuko
2011/07/25 13:22:41
nit: indent
tzik
2011/07/26 10:52:41
Done.
| |
197 return lhs.host == rhs.host && | |
198 lhs.temporary_usage == rhs.temporary_usage && | |
199 lhs.persistent_usage == rhs.persistent_usage && | |
200 lhs.persistent_quota == rhs.persistent_quota; | |
201 } | |
OLD | NEW |