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 // static | |
15 BrowsingDataQuotaHelper* BrowsingDataQuotaHelper::Create(Profile* profile) { | |
16 return new BrowsingDataQuotaHelperImpl( | |
17 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | |
18 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | |
19 profile->GetQuotaManager()); | |
20 } | |
21 | |
22 BrowsingDataQuotaHelperImpl::BrowsingDataQuotaHelperImpl( | |
23 base::MessageLoopProxy* ui_thread, | |
24 base::MessageLoopProxy* io_thread, | |
25 quota::QuotaManager* quota_manager) | |
26 : BrowsingDataQuotaHelper(io_thread), | |
27 quota_manager_(quota_manager), | |
28 is_fetching_(false), | |
29 ui_thread_(ui_thread), | |
30 io_thread_(io_thread), | |
31 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
32 DCHECK(quota_manager); | |
33 } | |
34 | |
35 BrowsingDataQuotaHelperImpl::~BrowsingDataQuotaHelperImpl() {} | |
36 | |
37 void BrowsingDataQuotaHelperImpl::StartFetching(FetchResultCallback* callback) { | |
38 DCHECK(callback); | |
39 DCHECK(!callback_.get()); | |
40 DCHECK(!is_fetching_); | |
41 callback_.reset(callback); | |
42 quota_info_.clear(); | |
43 is_fetching_ = true; | |
44 | |
45 FetchQuotaInfo(); | |
46 } | |
47 | |
48 void BrowsingDataQuotaHelperImpl::CancelNotification() { | |
49 callback_.reset(); | |
50 } | |
51 | |
52 void BrowsingDataQuotaHelperImpl::FetchQuotaInfo() { | |
53 if (!io_thread_->BelongsToCurrentThread()) { | |
54 io_thread_->PostTask( | |
55 FROM_HERE, | |
56 NewRunnableMethod( | |
57 this, | |
58 &BrowsingDataQuotaHelperImpl::FetchQuotaInfo)); | |
59 return; | |
60 } | |
61 | |
62 quota_manager_->GetOriginsModifiedSince( | |
63 quota::kStorageTypeTemporary, | |
64 base::Time(), | |
65 callback_factory_.NewCallback( | |
66 &BrowsingDataQuotaHelperImpl::GotOrigins)); | |
67 } | |
68 | |
69 void BrowsingDataQuotaHelperImpl::GotOrigins( | |
70 const std::set<GURL>& origins, quota::StorageType type) { | |
71 for (std::set<GURL>::const_iterator itr = origins.begin(); | |
72 itr != origins.end(); | |
73 ++itr) | |
74 pending_hosts_.insert(std::make_pair(itr->host(), type)); | |
75 | |
76 DCHECK(type == quota::kStorageTypeTemporary || | |
77 type == quota::kStorageTypePersistent); | |
78 | |
79 if (type == quota::kStorageTypeTemporary) { | |
80 quota_manager_->GetOriginsModifiedSince( | |
81 quota::kStorageTypePersistent, | |
82 base::Time(), | |
83 callback_factory_.NewCallback( | |
84 &BrowsingDataQuotaHelperImpl::GotOrigins)); | |
85 } else { | |
86 // type == quota::kStorageTypePersistent | |
87 ProcessPendingHosts(); | |
88 } | |
89 } | |
90 | |
91 void BrowsingDataQuotaHelperImpl::ProcessPendingHosts() { | |
92 if (pending_hosts_.empty()) { | |
93 OnComplete(); | |
94 return; | |
95 } | |
96 | |
97 PendingHosts::iterator itr = pending_hosts_.begin(); | |
98 std::string host = itr->first; | |
99 quota::StorageType type = itr->second; | |
100 pending_hosts_.erase(itr); | |
101 GetHostUsage(host, type); | |
102 } | |
103 | |
104 void BrowsingDataQuotaHelperImpl::GetHostUsage(const std::string& host, | |
105 quota::StorageType type) { | |
106 DCHECK(quota_manager_.get()); | |
107 quota_manager_->GetHostUsage( | |
108 host, type, | |
109 callback_factory_.NewCallback( | |
110 &BrowsingDataQuotaHelperImpl::GotHostUsage)); | |
111 } | |
112 | |
113 void BrowsingDataQuotaHelperImpl::GotHostUsage(const std::string& host, | |
114 quota::StorageType type, | |
115 int64 usage) { | |
116 switch (type) { | |
117 case quota::kStorageTypeTemporary: | |
118 quota_info_[host].temporary_usage = usage; | |
119 break; | |
120 case quota::kStorageTypePersistent: | |
121 quota_info_[host].persistent_usage = usage; | |
122 break; | |
123 default: | |
124 NOTREACHED(); | |
125 } | |
126 ProcessPendingHosts(); | |
127 } | |
128 | |
129 void BrowsingDataQuotaHelperImpl::OnComplete() { | |
130 // Check if CancelNotification was called | |
131 if (!callback_.get()) | |
132 return; | |
133 | |
134 if (!ui_thread_->BelongsToCurrentThread()) { | |
135 ui_thread_->PostTask( | |
136 FROM_HERE, | |
137 NewRunnableMethod( | |
138 this, | |
139 &BrowsingDataQuotaHelperImpl::OnComplete)); | |
140 return; | |
141 } | |
142 | |
143 is_fetching_ = false; | |
144 | |
145 QuotaInfoArray result; | |
146 result.reserve(quota_info_.size()); | |
147 | |
148 for (std::map<std::string, QuotaInfo>::iterator itr = quota_info_.begin(); | |
149 itr != quota_info_.end(); | |
150 ++itr) { | |
151 QuotaInfo* info = &itr->second; | |
152 // Skip unused entries | |
153 if (info->temporary_usage <= 0 && | |
154 info->persistent_usage <= 0) | |
155 continue; | |
156 | |
157 info->host = itr->first; | |
158 result.push_back(*info); | |
159 } | |
160 | |
161 callback_->Run(result); | |
162 callback_.reset(); | |
163 } | |
OLD | NEW |