Chromium Code Reviews| 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/ui/webui/quota_internals_proxy.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "chrome/browser/ui/webui/quota_internals_handler.h" | |
| 11 #include "chrome/browser/ui/webui/quota_internals_types.h" | |
| 12 #include "net/base/net_util.h" | |
| 13 | |
| 14 namespace quota_internals { | |
| 15 | |
| 16 QuotaInternalsProxy::QuotaInternalsProxy(QuotaInternalsHandler* handler) | |
| 17 : handler_(handler), | |
| 18 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 19 } | |
| 20 | |
| 21 void QuotaInternalsProxy::ReportAvailableSpace(int64 available_space) { | |
| 22 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 23 BrowserThread::PostTask( | |
| 24 BrowserThread::UI, FROM_HERE, | |
| 25 NewRunnableMethod(this, &QuotaInternalsProxy::ReportAvailableSpace, | |
| 26 available_space)); | |
| 27 return; | |
| 28 } | |
| 29 if (handler_) | |
| 30 handler_->ReportAvailableSpace(available_space); | |
| 31 } | |
| 32 | |
| 33 void QuotaInternalsProxy::ReportGlobalInfo(const GlobalStorageInfo& info) { | |
| 34 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 35 BrowserThread::PostTask( | |
| 36 BrowserThread::UI, FROM_HERE, | |
| 37 NewRunnableMethod(this, &QuotaInternalsProxy::ReportGlobalInfo, | |
| 38 info)); | |
| 39 return; | |
| 40 } | |
| 41 if (handler_) | |
| 42 handler_->ReportGlobalInfo(info); | |
| 43 } | |
| 44 | |
| 45 void QuotaInternalsProxy::ReportPerHostInfo( | |
| 46 const std::vector<PerHostStorageInfo>& hosts) { | |
| 47 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 48 BrowserThread::PostTask( | |
| 49 BrowserThread::UI, FROM_HERE, | |
| 50 NewRunnableMethod(this, &QuotaInternalsProxy::ReportPerHostInfo, | |
| 51 hosts)); | |
| 52 return; | |
| 53 } | |
| 54 if (handler_) | |
|
Evan Stade
2011/07/07 20:58:17
seems like you could move this check above the thr
tzik
2011/07/07 23:00:35
Done.
| |
| 55 handler_->ReportPerHostInfo(hosts); | |
| 56 } | |
| 57 | |
| 58 void QuotaInternalsProxy::ReportPerOriginInfo( | |
| 59 const std::vector<PerOriginStorageInfo>& origins) { | |
| 60 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 61 BrowserThread::PostTask( | |
| 62 BrowserThread::UI, FROM_HERE, | |
| 63 NewRunnableMethod(this, &QuotaInternalsProxy::ReportPerOriginInfo, | |
| 64 origins)); | |
| 65 return; | |
| 66 } | |
| 67 if (handler_) | |
| 68 handler_->ReportPerOriginInfo(origins); | |
| 69 } | |
| 70 | |
| 71 void QuotaInternalsProxy::ReportStatistics(const Statistics& stats) { | |
|
Evan Stade
2011/07/07 20:58:17
you use this pattern a lot, it seems ripe for a ma
tzik
2011/07/07 23:00:35
Done.
| |
| 72 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 73 BrowserThread::PostTask( | |
| 74 BrowserThread::UI, FROM_HERE, | |
| 75 NewRunnableMethod(this, &QuotaInternalsProxy::ReportStatistics, | |
| 76 stats)); | |
| 77 return; | |
| 78 } | |
| 79 if (handler_) | |
| 80 handler_->ReportStatistics(stats); | |
| 81 } | |
| 82 | |
| 83 void QuotaInternalsProxy::RequestInfo( | |
| 84 scoped_refptr<quota::QuotaManager> quota_manager) { | |
| 85 DCHECK(quota_manager); | |
| 86 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 87 BrowserThread::PostTask( | |
| 88 BrowserThread::IO, FROM_HERE, | |
| 89 NewRunnableMethod(this, &QuotaInternalsProxy::RequestInfo, | |
| 90 quota_manager)); | |
| 91 return; | |
| 92 } | |
| 93 | |
| 94 quota_manager_ = quota_manager; | |
| 95 quota_manager_->GetAvailableSpace( | |
| 96 callback_factory_.NewCallback( | |
| 97 &QuotaInternalsProxy::DidGetAvailableSpace)); | |
| 98 | |
| 99 quota_manager_->GetTemporaryGlobalQuota( | |
| 100 callback_factory_.NewCallback( | |
| 101 &QuotaInternalsProxy::DidGetGlobalQuota)); | |
| 102 | |
| 103 quota_manager_->GetGlobalUsage( | |
| 104 quota::kStorageTypeTemporary, | |
| 105 callback_factory_.NewCallback( | |
| 106 &QuotaInternalsProxy::DidGetGlobalUsage)); | |
| 107 | |
| 108 quota_manager_->GetGlobalUsage( | |
| 109 quota::kStorageTypePersistent, | |
| 110 callback_factory_.NewCallback( | |
| 111 &QuotaInternalsProxy::DidGetGlobalUsage)); | |
| 112 | |
| 113 quota_manager_->DumpQuotaTable( | |
| 114 callback_factory_.NewCallback( | |
| 115 &QuotaInternalsProxy::DidDumpQuotaTable)); | |
| 116 | |
| 117 quota_manager_->DumpOriginInfoTable( | |
| 118 callback_factory_.NewCallback( | |
| 119 &QuotaInternalsProxy::DidDumpOriginInfoTable)); | |
| 120 | |
| 121 std::map<std::string, std::string> stats; | |
| 122 quota_manager_->GetStatistics(&stats); | |
| 123 ReportStatistics(stats); | |
| 124 } | |
| 125 | |
| 126 void QuotaInternalsProxy::DidGetAvailableSpace(quota::QuotaStatusCode status, | |
| 127 int64 space) { | |
| 128 if (status == quota::kQuotaStatusOk) | |
| 129 ReportAvailableSpace(space); | |
| 130 } | |
| 131 | |
| 132 void QuotaInternalsProxy::DidGetGlobalQuota(quota::QuotaStatusCode status, | |
| 133 quota::StorageType type, | |
| 134 int64 quota) { | |
| 135 if (status == quota::kQuotaStatusOk) { | |
| 136 GlobalStorageInfo info(type); | |
| 137 info.set_quota(quota); | |
| 138 ReportGlobalInfo(info); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void QuotaInternalsProxy::DidGetGlobalUsage(quota::StorageType type, | |
| 143 int64 usage, | |
| 144 int64 unlimited_usage) { | |
| 145 GlobalStorageInfo info(type); | |
| 146 info.set_usage(usage); | |
| 147 info.set_unlimited_usage(unlimited_usage); | |
| 148 | |
| 149 ReportGlobalInfo(info); | |
| 150 RequestPerOriginInfo(type); | |
| 151 } | |
| 152 | |
| 153 void QuotaInternalsProxy::DidDumpQuotaTable(const QuotaTableEntries& entries) { | |
| 154 std::vector<PerHostStorageInfo> host_info; | |
| 155 host_info.reserve(entries.size()); | |
| 156 | |
| 157 typedef QuotaTableEntries::const_iterator iterator; | |
| 158 for (iterator itr(entries.begin()); itr != entries.end(); ++itr) { | |
| 159 PerHostStorageInfo info(itr->host, itr->type); | |
| 160 info.set_quota(itr->quota); | |
| 161 host_info.push_back(info); | |
| 162 } | |
| 163 | |
| 164 ReportPerHostInfo(host_info); | |
| 165 } | |
| 166 | |
| 167 void QuotaInternalsProxy::DidDumpOriginInfoTable( | |
| 168 const OriginInfoTableEntries& entries) { | |
| 169 std::vector<PerOriginStorageInfo> origin_info; | |
| 170 origin_info.reserve(entries.size()); | |
| 171 | |
| 172 typedef OriginInfoTableEntries::const_iterator iterator; | |
| 173 for (iterator itr(entries.begin()); itr != entries.end(); ++itr) { | |
| 174 PerOriginStorageInfo info(itr->origin, itr->type); | |
| 175 info.set_used_count(itr->used_count); | |
| 176 info.set_last_access_time(itr->last_access_time); | |
| 177 info.set_last_modified_time(itr->last_modified_time); | |
| 178 | |
| 179 origin_info.push_back(info); | |
| 180 } | |
| 181 | |
| 182 ReportPerOriginInfo(origin_info); | |
| 183 } | |
| 184 | |
| 185 void QuotaInternalsProxy::DidGetHostUsage(const std::string& host, | |
| 186 quota::StorageType type, | |
| 187 int64 usage) { | |
| 188 DCHECK(type == quota::kStorageTypeTemporary || | |
| 189 type == quota::kStorageTypePersistent); | |
| 190 | |
| 191 PerHostStorageInfo info(host, type); | |
| 192 info.set_usage(usage); | |
| 193 | |
| 194 report_pending_.push_back(info); | |
| 195 hosts_pending_.erase(make_pair(host, type)); | |
| 196 if (report_pending_.size() >= 10 || hosts_pending_.empty()) { | |
| 197 ReportPerHostInfo(report_pending_); | |
| 198 report_pending_.clear(); | |
| 199 } | |
| 200 | |
| 201 if (!hosts_pending_.empty()) | |
| 202 GetHostUsage(hosts_pending_.begin()->first, | |
| 203 hosts_pending_.begin()->second); | |
| 204 } | |
| 205 | |
| 206 void QuotaInternalsProxy::VisitHost(const std::string& host, | |
| 207 quota::StorageType type) { | |
| 208 if (hosts_visited_.insert(std::make_pair(host, type)).second) { | |
| 209 hosts_pending_.insert(std::make_pair(host, type)); | |
| 210 if (hosts_pending_.size() == 1) { | |
| 211 GetHostUsage(host, type); | |
| 212 } | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 void QuotaInternalsProxy::GetHostUsage(const std::string& host, | |
| 217 quota::StorageType type) { | |
| 218 DCHECK(quota_manager_); | |
| 219 quota_manager_->GetHostUsage( | |
| 220 host, type, | |
| 221 callback_factory_.NewCallback( | |
| 222 &QuotaInternalsProxy::DidGetHostUsage)); | |
| 223 } | |
| 224 | |
| 225 void QuotaInternalsProxy::RequestPerOriginInfo(quota::StorageType type) { | |
| 226 DCHECK(quota_manager_); | |
| 227 | |
| 228 std::set<GURL> origins; | |
| 229 quota_manager_->GetCachedOrigins(type, &origins); | |
| 230 | |
| 231 std::vector<PerOriginStorageInfo> origin_info; | |
| 232 origin_info.reserve(origins.size()); | |
| 233 | |
| 234 std::set<std::string> hosts; | |
| 235 std::vector<PerHostStorageInfo> host_info; | |
| 236 | |
| 237 for (std::set<GURL>::iterator itr(origins.begin()); | |
| 238 itr != origins.end(); ++itr) { | |
| 239 PerOriginStorageInfo info(*itr, type); | |
| 240 info.set_in_use(quota_manager_->IsOriginInUse(*itr)); | |
| 241 origin_info.push_back(info); | |
| 242 | |
| 243 std::string host(net::GetHostOrSpecFromURL(*itr)); | |
| 244 if (hosts.insert(host).second) { | |
| 245 PerHostStorageInfo info(host, type); | |
| 246 host_info.push_back(info); | |
| 247 VisitHost(host, type); | |
| 248 } | |
| 249 } | |
| 250 ReportPerOriginInfo(origin_info); | |
| 251 ReportPerHostInfo(host_info); | |
| 252 } | |
| 253 | |
| 254 } // namespace quota_internals | |
| OLD | NEW |