Index: chrome/browser/ui/webui/quota_internals_handler.cc |
diff --git a/chrome/browser/ui/webui/quota_internals_handler.cc b/chrome/browser/ui/webui/quota_internals_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d45458731a0da5e252c41aedfb6299895a9d6962 |
--- /dev/null |
+++ b/chrome/browser/ui/webui/quota_internals_handler.cc |
@@ -0,0 +1,417 @@ |
+// 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_handler.h" |
+ |
+#include <algorithm> |
+#include <set> |
+#include <string> |
+ |
+#include "base/values.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/common/url_constants.h" |
+#include "content/browser/tab_contents/tab_contents.h" |
+#include "grit/quota_internals_resources.h" |
+#include "net/base/net_util.h" |
+#include "ui/base/resource/resource_bundle.h" |
+ |
+namespace { |
+ |
+std::string StorageTypeToString(quota::StorageType type) { |
+ switch (type) { |
+ case quota::kStorageTypeTemporary: |
+ return "temporary"; |
+ case quota::kStorageTypePersistent: |
+ return "persistent"; |
+ default: |
+ return "unknown"; |
+ } |
+} |
+ |
+} // anonymous namespace |
+ |
+namespace quota_internals { |
+ |
+GlobalData::GlobalData(quota::StorageType type, |
+ int64 usage, |
+ int64 unlimited_usage, |
+ int64 quota) |
+ : type_(type), |
+ usage_(usage), |
+ unlimited_usage_(unlimited_usage), |
+ quota_(quota) { |
+} |
+ |
+Value* GlobalData::NewValue() const { |
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
+ dict->SetString("type", StorageTypeToString(type_)); |
+ if (usage_ >= 0) |
+ dict->SetDouble("usage", static_cast<double>(usage_)); |
+ if (unlimited_usage_ >= 0) |
+ dict->SetDouble("unlimitedUsage", static_cast<double>(unlimited_usage_)); |
+ if (quota_ >= 0) |
+ dict->SetDouble("quota", static_cast<double>(quota_)); |
+ return dict.release(); |
+} |
+ |
+HostData::HostData(const std::string& host, |
+ quota::StorageType type, |
+ int64 usage, |
+ int64 quota) |
+ : host_(host), |
+ type_(type), |
+ usage_(usage), |
+ quota_(quota) { |
+} |
+ |
+Value* HostData::NewValue() const { |
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
+ DCHECK(!host_.empty()); |
+ dict->SetString("host", host_); |
+ dict->SetString("type", StorageTypeToString(type_)); |
+ if (usage_ >= 0) |
+ dict->SetDouble("usage", static_cast<double>(usage_)); |
+ if (quota_ >= 0) |
+ dict->SetDouble("quota", static_cast<double>(quota_)); |
+ return dict.release(); |
+} |
+ |
+OriginData::OriginData(const GURL& origin, |
+ quota::StorageType type, |
+ int in_use, |
+ int used_count, |
+ base::Time last_access_time, |
+ base::Time last_modified_time) |
+ : origin_(origin), |
+ type_(type), |
+ host_(net::GetHostOrSpecFromURL(origin)), |
+ in_use_(in_use), |
+ used_count_(used_count), |
+ last_access_time_(last_access_time), |
+ last_modified_time_(last_modified_time) { |
+} |
+ |
+Value* OriginData::NewValue() const { |
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
+ DCHECK(!origin_.is_empty()); |
+ DCHECK(!host_.empty()); |
+ dict->SetString("origin", origin_.spec()); |
+ dict->SetString("type", StorageTypeToString(type_)); |
+ dict->SetString("host", host_); |
+ if (in_use_ >= 0) |
+ dict->SetBoolean("inUse", in_use_ ? true : false); |
+ if (used_count_ >= 0) |
+ dict->SetInteger("usedCount", used_count_); |
+ if (!last_access_time_.is_null()) |
+ dict->SetDouble("lastAccessTime", last_access_time_.ToDoubleT() * 1000.0); |
+ if (!last_modified_time_.is_null()) |
+ dict->SetDouble("lastModifiedTime", |
+ last_modified_time_.ToDoubleT() * 1000.0); |
+ return dict.release(); |
+} |
+ |
+QuotaInternalsHandler::QuotaInternalsHandler() {} |
+ |
+QuotaInternalsHandler::~QuotaInternalsHandler() { |
+ if (proxy_) |
+ proxy_->handler_ = NULL; |
+} |
+ |
+void QuotaInternalsHandler::RegisterMessages() OVERRIDE { |
+ DCHECK(web_ui_); |
+ web_ui_->RegisterMessageCallback( |
+ "requestData", |
+ NewCallback(this, &QuotaInternalsHandler::OnRequestData)); |
+} |
+ |
+void QuotaInternalsHandler::ReportAvailableSpace(int64 available_space) { |
+ scoped_ptr<Value> avail( |
+ Value::CreateDoubleValue(static_cast<double>(available_space))); |
+ SendMessage("AvailableSpaceUpdated", *avail.get()); |
+} |
+ |
+void QuotaInternalsHandler::ReportGlobalData(const GlobalData& data) { |
+ scoped_ptr<Value> value(data.NewValue()); |
+ SendMessage("GlobalDataUpdated", *value); |
+} |
+ |
+void QuotaInternalsHandler::ReportHostData(const std::vector<HostData>& hosts) { |
+ ListValue values; |
+ typedef std::vector<HostData>::const_iterator iterator; |
+ for (iterator itr(hosts.begin()); itr != hosts.end(); ++itr) { |
+ values.Append(itr->NewValue()); |
+ } |
+ |
+ SendMessage("HostDataUpdated", values); |
+} |
+ |
+void QuotaInternalsHandler::ReportOriginData( |
+ const std::vector<OriginData>& origins) { |
+ ListValue origins_value; |
+ typedef std::vector<OriginData>::const_iterator iterator; |
+ for (iterator itr(origins.begin()); itr != origins.end(); ++itr) { |
+ origins_value.Append(itr->NewValue()); |
+ } |
+ |
+ SendMessage("OriginDataUpdated", origins_value); |
+} |
+ |
+void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) { |
+ DictionaryValue dict; |
+ typedef Statistics::const_iterator iterator; |
+ for (iterator itr(stats.begin()); itr != stats.end(); ++itr) { |
+ dict.SetString(itr->first, itr->second); |
+ } |
+ |
+ SendMessage("StatisticsUpdated", dict); |
+} |
+ |
+void QuotaInternalsHandler::SendMessage(const std::string& message, |
+ const Value& value) { |
+ scoped_ptr<Value> message_data(Value::CreateStringValue(message)); |
+ web_ui_->CallJavascriptFunction("cr.quota.messageHandler", |
+ *message_data, |
+ value); |
+} |
+ |
+void QuotaInternalsHandler::OnRequestData(const ListValue*) { |
+ if (!proxy_) |
+ proxy_ = new QuotaInternalsProxy(this); |
+ proxy_->RequestData(web_ui_->GetProfile()->GetQuotaManager()); |
+} |
+ |
+QuotaInternalsProxy::QuotaInternalsProxy(QuotaInternalsHandler* handler) |
+ : handler_(handler), |
+ callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+} |
+ |
+void QuotaInternalsProxy::ReportAvailableSpace(int64 available_space) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportAvailableSpaceOnUIThread, |
+ available_space)); |
+} |
+ |
+void QuotaInternalsProxy::ReportGlobalData(const GlobalData& data) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportGlobalDataOnUIThread, |
+ data)); |
+} |
+ |
+void QuotaInternalsProxy::ReportHostData(const std::vector<HostData>& hosts) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportHostDataOnUIThread, |
+ hosts)); |
+} |
+ |
+void QuotaInternalsProxy::ReportOriginData( |
+ const std::vector<OriginData>& origins) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportOriginDataOnUIThread, |
+ origins)); |
+} |
+ |
+void QuotaInternalsProxy::ReportStatistics(const Statistics& stats) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportStatisticsOnUIThread, |
+ stats)); |
+} |
+ |
+void QuotaInternalsProxy::RequestData(quota::QuotaManager* quota_manager) { |
+ DCHECK(quota_manager); |
+ quota_manager_ = quota_manager; |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ NewRunnableMethod(this, &QuotaInternalsProxy::RunRequestDataOnIOThread)); |
+} |
+ |
+void QuotaInternalsProxy::RunReportAvailableSpaceOnUIThread( |
+ int64 available_space) { |
+ if (handler_) |
+ handler_->ReportAvailableSpace(available_space); |
+} |
+ |
+void QuotaInternalsProxy::RunReportGlobalDataOnUIThread( |
+ const GlobalData& data) { |
+ if (handler_) |
+ handler_->ReportGlobalData(data); |
+} |
+ |
+void QuotaInternalsProxy::RunReportHostDataOnUIThread( |
+ const std::vector<HostData>& hosts) { |
+ if (handler_) |
+ handler_->ReportHostData(hosts); |
+} |
+ |
+void QuotaInternalsProxy::RunReportOriginDataOnUIThread( |
+ const std::vector<OriginData>& origins) { |
+ if (handler_) |
+ handler_->ReportOriginData(origins); |
+} |
+ |
+void QuotaInternalsProxy::RunReportStatisticsOnUIThread( |
+ const Statistics& stats) { |
+ if (handler_) |
+ handler_->ReportStatistics(stats); |
+} |
+ |
+void QuotaInternalsProxy::RunRequestDataOnIOThread() { |
+ DCHECK(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, |
+ 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 |