Chromium Code Reviews| Index: chrome/browser/ui/webui/quota_internals_proxy.cc |
| diff --git a/chrome/browser/ui/webui/quota_internals_proxy.cc b/chrome/browser/ui/webui/quota_internals_proxy.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a428fd24b6ef2b931155fda84642aea51b02b192 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/quota_internals_proxy.cc |
| @@ -0,0 +1,241 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/quota_internals_proxy.h" |
| + |
| +#include <set> |
| +#include <string> |
| + |
| +#include "chrome/browser/ui/webui/quota_internals_handler.h" |
| +#include "chrome/browser/ui/webui/quota_internals_types.h" |
| +#include "net/base/net_util.h" |
| + |
| +namespace quota_internals { |
| + |
| +QuotaInternalsProxy::QuotaInternalsProxy(QuotaInternalsHandler* handler) |
| + : handler_(handler), |
| + callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| +} |
| + |
| +void QuotaInternalsProxy::ReportAvailableSpace(int64 available_space) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, &QuotaInternalsProxy::ReportAvailableSpace, |
| + available_space)); |
| + return; |
| + } |
| + if (handler_) |
| + handler_->ReportAvailableSpace(available_space); |
| +} |
| + |
| +void QuotaInternalsProxy::ReportGlobalData(const GlobalData& data) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, &QuotaInternalsProxy::ReportGlobalData, |
| + data)); |
| + return; |
| + } |
| + if (handler_) |
| + handler_->ReportGlobalData(data); |
| +} |
| + |
| +void QuotaInternalsProxy::ReportHostData(const std::vector<HostData>& hosts) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, &QuotaInternalsProxy::ReportHostData, |
| + hosts)); |
| + return; |
| + } |
| + if (handler_) |
| + handler_->ReportHostData(hosts); |
| +} |
| + |
| +void QuotaInternalsProxy::ReportOriginData( |
| + const std::vector<OriginData>& origins) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, &QuotaInternalsProxy::ReportOriginData, |
| + origins)); |
| + return; |
| + } |
| + if (handler_) |
| + handler_->ReportOriginData(origins); |
| +} |
| + |
| +void QuotaInternalsProxy::ReportStatistics(const Statistics& stats) { |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, &QuotaInternalsProxy::ReportStatistics, |
| + stats)); |
| + return; |
| + } |
| + if (handler_) |
| + handler_->ReportStatistics(stats); |
| +} |
| + |
| +void QuotaInternalsProxy::RequestData( |
| + scoped_refptr<quota::QuotaManager> quota_manager) { |
| + DCHECK(quota_manager); |
| + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + NewRunnableMethod(this, &QuotaInternalsProxy::RequestData, |
| + quota_manager)); |
| + return; |
| + } |
| + |
| + quota_manager_ = quota_manager; |
| + quota_manager_->GetAvailableSpace( |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidGetAvailableSpace)); |
| + |
| + quota_manager_->GetTemporaryGlobalQuota( |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidGetGlobalQuota)); |
| + |
| + quota_manager_->GetGlobalUsage( |
| + quota::kStorageTypeTemporary, |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidGetGlobalUsage)); |
| + |
| + quota_manager_->GetGlobalUsage( |
| + quota::kStorageTypePersistent, |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidGetGlobalUsage)); |
| + |
| + quota_manager_->DumpQuotaTable( |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidDumpQuotaTable)); |
| + |
| + quota_manager_->DumpOriginInfoTable( |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidDumpOriginInfoTable)); |
| + |
| + std::map<std::string, std::string> stats; |
| + quota_manager_->GetStatistics(&stats); |
| + ReportStatistics(stats); |
| +} |
| + |
| +void QuotaInternalsProxy::DidGetAvailableSpace(quota::QuotaStatusCode status, |
| + int64 space) { |
| + if (status == quota::kQuotaStatusOk) |
| + ReportAvailableSpace(space); |
| +} |
| + |
| +void QuotaInternalsProxy::DidGetGlobalQuota(quota::QuotaStatusCode status, |
| + quota::StorageType type, |
| + int64 quota) { |
| + if (status == quota::kQuotaStatusOk) |
| + ReportGlobalData(GlobalData(type, -1, -1, quota)); |
| +} |
| + |
| +void QuotaInternalsProxy::DidGetGlobalUsage(quota::StorageType type, |
| + int64 usage, |
| + int64 unlimited_usage) { |
| + ReportGlobalData(GlobalData(type, usage, unlimited_usage, -1)); |
| + RequestPerOriginData(type); |
| +} |
| + |
| +void QuotaInternalsProxy::DidDumpQuotaTable(const QuotaTableEntries& entries) { |
| + std::vector<HostData> host_data; |
| + host_data.reserve(entries.size()); |
| + |
| + typedef QuotaTableEntries::const_iterator iterator; |
| + for (iterator itr(entries.begin()); itr != entries.end(); ++itr) { |
| + host_data.push_back(HostData(itr->host, itr->type, -1, itr->quota)); |
| + } |
| + |
| + ReportHostData(host_data); |
| +} |
| + |
| +void QuotaInternalsProxy::DidDumpOriginInfoTable( |
| + const OriginInfoTableEntries& entries) { |
| + std::vector<OriginData> origin_data; |
| + origin_data.reserve(entries.size()); |
| + |
| + typedef OriginInfoTableEntries::const_iterator iterator; |
| + for (iterator itr(entries.begin()); itr != entries.end(); ++itr) { |
| + origin_data.push_back( |
| + OriginData(itr->origin, itr->type, |
| + -1, itr->used_count, |
| + itr->last_access_time, |
| + itr->last_modified_time)); |
| + } |
| + |
| + ReportOriginData(origin_data); |
| +} |
| + |
| +void QuotaInternalsProxy::DidGetHostUsage(const std::string& host, |
| + quota::StorageType type, |
| + int64 usage) { |
| + DCHECK(type == quota::kStorageTypeTemporary || |
| + type == quota::kStorageTypePersistent); |
| + |
| + report_pending_.push_back(HostData(host, type, usage, -1)); |
| + hosts_pending_.erase(make_pair(host, type)); |
| + if (report_pending_.size() >= 10 || hosts_pending_.empty()) { |
| + ReportHostData(report_pending_); |
| + report_pending_.clear(); |
| + } |
| + |
| + if (!hosts_pending_.empty()) |
| + GetHostUsage(hosts_pending_.begin()->first, |
| + hosts_pending_.begin()->second); |
| +} |
| + |
| +void QuotaInternalsProxy::VisitHost(const std::string& host, |
| + quota::StorageType type) { |
| + if (hosts_visited_.insert(std::make_pair(host, type)).second) { |
| + hosts_pending_.insert(std::make_pair(host, type)); |
| + if (hosts_pending_.size() == 1) { |
| + GetHostUsage(host, type); |
| + } |
| + } |
| +} |
| + |
| +void QuotaInternalsProxy::GetHostUsage(const std::string& host, |
| + quota::StorageType type) { |
| + DCHECK(quota_manager_); |
| + quota_manager_->GetHostUsage( |
| + host, type, |
| + callback_factory_.NewCallback( |
| + &QuotaInternalsProxy::DidGetHostUsage)); |
| +} |
| + |
| +void QuotaInternalsProxy::RequestPerOriginData(quota::StorageType type) { |
| + DCHECK(quota_manager_); |
| + |
| + std::set<GURL> origins; |
| + quota_manager_->GetCachedOrigins(type, &origins); |
| + |
| + std::vector<OriginData> origin_data; |
| + origin_data.reserve(origins.size()); |
| + |
| + std::set<std::string> hosts; |
| + std::vector<HostData> host_data; |
| + |
| + for (std::set<GURL>::iterator itr(origins.begin()); |
| + itr != origins.end(); ++itr) { |
| + origin_data.push_back( |
| + OriginData(*itr, type, |
| + quota_manager_->IsOriginInUse(*itr) ? 1 : 0, -1, |
|
kinuko
2011/07/04 08:58:29
The fact that the caller needs to explicitly give
tzik
2011/07/06 22:49:04
Done. Sounds good.
|
| + base::Time(), |
| + base::Time())); |
| + |
| + std::string host(net::GetHostOrSpecFromURL(*itr)); |
| + if (hosts.insert(host).second) { |
| + host_data.push_back(HostData(host, type, -1, -1)); |
| + VisitHost(host, type); |
| + } |
| + } |
| + ReportOriginData(origin_data); |
| + ReportHostData(host_data); |
| +} |
| + |
| +} // namespace quota_internals |