Index: chrome/browser/ui/webui/quota_internals_ui.cc |
diff --git a/chrome/browser/ui/webui/quota_internals_ui.cc b/chrome/browser/ui/webui/quota_internals_ui.cc |
index 30c68d4ae8aeba74d64f475fe6fcc2c14d4d7182..683d3dddb95454bbbb7a911ac3ec0fd6df62100f 100644 |
--- a/chrome/browser/ui/webui/quota_internals_ui.cc |
+++ b/chrome/browser/ui/webui/quota_internals_ui.cc |
@@ -7,43 +7,516 @@ |
#include <algorithm> |
#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" |
-QuotaInternalsUI::QuotaInternalsUI(TabContents* contents) |
- : WebUI(contents) { |
- // TODO(tzik): implement and attach message handler |
- contents->profile()->GetChromeURLDataManager()-> |
- AddDataSource(new quota_internals::QuotaInternalsHTMLSource); |
+namespace { |
Evan Stade
2011/06/01 01:15:04
insert blank line
tzik
2011/06/03 16:01:19
Done.
|
+template<typename T> |
+std::string ToString(const T& v) { |
+ std::ostringstream out; |
+ out << v; |
+ return out.str(); |
+} |
+ |
+std::string StorageTypeToString(quota::StorageType type) { |
+ switch (type) { |
+ case quota::kStorageTypeTemporary: |
+ return "temporary"; |
+ case quota::kStorageTypePersistent: |
+ return "persistent"; |
+ default: |
+ return "unknown"; |
+ } |
} |
Evan Stade
2011/06/01 01:15:04
insert blank line
tzik
2011/06/03 16:01:19
Done.
|
+} // anonymous namespace |
namespace quota_internals { |
-QuotaInternalsHTMLSource::QuotaInternalsHTMLSource() |
- : DataSource(chrome::kChromeUIQuotaInternalsHost, |
- MessageLoop::current()) { |
-} |
+class GlobalData { |
+ public: |
+ GlobalData(quota::StorageType type, |
+ int64 usage, |
+ int64 unlimited_usage, |
+ int64 quota) |
+ : type_(type), |
+ usage_(usage), |
+ unlimited_usage_(unlimited_usage), |
+ quota_(quota) { |
+ } |
+ |
+ Value* NewValue() const { |
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
+ dict->SetString("type", StorageTypeToString(type_)); |
+ if (usage_ >= 0) |
+ dict->SetString("usage", ToString(usage_)); |
+ if (unlimited_usage_ >= 0) |
+ dict->SetString("unlimited_usage", ToString(unlimited_usage_)); |
+ if (quota_ >= 0) |
+ dict->SetString("quota", ToString(quota_)); |
+ return dict.release(); |
+ } |
+ |
+ private: |
+ quota::StorageType type_; |
+ int64 usage_; |
+ int64 unlimited_usage_; |
+ int64 quota_; |
+}; |
+ |
+class HostData { |
+ public: |
+ HostData(const std::string& host, |
+ quota::StorageType type, |
+ int64 usage, |
+ int64 quota) |
+ : host_(host), |
+ type_(type), |
+ usage_(usage), |
+ quota_(quota) { |
+ } |
+ |
+ Value* NewValue() const { |
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
+ DCHECK(!host_.empty()); |
+ dict->SetString("host", host_); |
+ dict->SetString("type", StorageTypeToString(type_)); |
+ if (usage_ >= 0) |
+ dict->SetString("usage", ToString(usage_)); |
+ if (quota_ >= 0) |
+ dict->SetString("quota", ToString(quota_)); |
+ return dict.release(); |
+ } |
+ |
+ private: |
+ std::string host_; |
+ quota::StorageType type_; |
+ int64 usage_; |
+ int64 quota_; |
+}; |
+ |
+struct OriginData { |
+ public: |
+ OriginData(const GURL& origin, |
+ quota::StorageType type, |
+ int in_use, |
+ int used_count, |
+ base::Time last_access_time) |
+ : origin_(origin), |
+ type_(type), |
+ host_(net::GetHostOrSpecFromURL(origin)), |
+ in_use_(in_use), |
+ used_count_(used_count), |
+ last_access_time_(last_access_time) { |
+ } |
+ |
+ Value* 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", ToString(host_)); |
+ if (in_use_ >= 0) |
+ dict->SetBoolean("in_use", in_use_ ? true : false); |
+ if (used_count_ >= 0) |
+ dict->SetInteger("used_count", used_count_); |
+ if (!last_access_time_.is_null()) |
+ dict->SetDouble("last_access_time", last_access_time_.ToDoubleT()); |
+ return dict.release(); |
+ } |
+ |
+ private: |
+ GURL origin_; |
+ quota::StorageType type_; |
+ std::string host_; |
+ int in_use_; |
+ int used_count_; |
+ base::Time last_access_time_; |
+}; |
+ |
+typedef std::map<std::string, std::string> Statistics; |
+ |
+class QuotaInternalsProxy; |
+ |
+class QuotaInternalsHTMLSource : public ChromeURLDataManager::DataSource { |
+ public: |
+ QuotaInternalsHTMLSource() |
+ : DataSource(chrome::kChromeUIQuotaInternalsHost, |
+ MessageLoop::current()) { |
+ } |
+ |
+ virtual void StartDataRequest(const std::string& path, |
+ bool is_incognito, |
+ int request_id) OVERRIDE { |
+ base::StringPiece html( |
+ ResourceBundle::GetSharedInstance().GetRawDataResource( |
+ IDR_QUOTA_INTERNALS_INDEX_HTML)); |
+ |
+ scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
+ html_bytes->data.resize(html.size()); |
+ std::copy(html.data(), html.data() + html.size(), |
+ html_bytes->data.begin()); |
+ SendResponse(request_id, html_bytes); |
+ return; |
+ } |
+ |
+ virtual std::string GetMimeType(const std::string&) const OVERRIDE { |
+ return "text/html"; |
+ } |
+ |
+ private: |
+ virtual ~QuotaInternalsHTMLSource() {} |
+ DISALLOW_COPY_AND_ASSIGN(QuotaInternalsHTMLSource); |
+}; |
+ |
+class QuotaInternalsMessageHandler : public WebUIMessageHandler { |
Evan Stade
2011/06/01 01:15:04
this class needs to go in its own file
tzik
2011/06/03 16:01:19
Done. Should I move QuotaInternalsProxy to another
|
+ public: |
+ QuotaInternalsMessageHandler() {} |
+ |
+ virtual ~QuotaInternalsMessageHandler(); |
+ |
+ virtual void RegisterMessages() OVERRIDE { |
+ DCHECK(web_ui_); |
+ web_ui_->RegisterMessageCallback( |
+ "requestData", |
+ NewCallback(this, &QuotaInternalsMessageHandler::OnRequestData)); |
+ } |
+ |
+ void ReportAvailableSpace(int64 available_space) { |
+ scoped_ptr<Value> avail( |
+ Value::CreateStringValue(ToString(available_space))); |
+ SendMessage("AvailableSpaceUpdated", *avail.get()); |
+ } |
+ |
+ void ReportGlobalData(const GlobalData& data) { |
+ scoped_ptr<Value> value(data.NewValue()); |
+ SendMessage("GlobalDataUpdated", *value); |
+ } |
+ |
+ void ReportHostData(const std::vector<HostData>& hosts) { |
+ ListValue values; |
+ typedef std::vector<HostData>::const_iterator iterator; |
+ for (iterator itr(hosts.begin()), end(hosts.end()); |
+ itr != end; ++itr) |
Evan Stade
2011/06/01 01:15:04
need curlies
tzik
2011/06/03 16:01:19
Done.
|
+ values.Append(itr->NewValue()); |
Evan Stade
2011/06/01 01:15:04
newline
tzik
2011/06/03 16:01:19
Done.
|
+ SendMessage("HostDataUpdated", values); |
+ } |
+ |
+ void ReportOriginData(const std::vector<OriginData>& origins) { |
+ ListValue origins_value; |
+ typedef std::vector<OriginData>::const_iterator iterator; |
+ for (iterator itr(origins.begin()), end(origins.end()); |
+ itr != end; ++itr) |
Evan Stade
2011/06/01 01:15:04
need curlies
tzik
2011/06/03 16:01:19
Done.
|
+ origins_value.Append(itr->NewValue()); |
+ |
+ SendMessage("OriginDataUpdated", origins_value); |
+ } |
+ |
+ void ReportStatistics(const Statistics& stats) { |
+ DictionaryValue dict; |
+ typedef Statistics::const_iterator iterator; |
+ for (iterator itr(stats.begin()), end(stats.end()); |
+ itr != end; ++itr) |
Evan Stade
2011/06/01 01:15:04
need curlies
tzik
2011/06/03 16:01:19
Done.
|
+ dict.SetString(itr->first, itr->second); |
Evan Stade
2011/06/01 01:15:04
newline
tzik
2011/06/03 16:01:19
Done.
|
+ SendMessage("StatisticsUpdated", dict); |
+ } |
+ |
+ private: |
+ void OnRequestData(const ListValue*); |
+ |
+ void SendMessage(const std::string& message, const Value& value) { |
+ scoped_ptr<Value> message_data(Value::CreateStringValue(message)); |
+ web_ui_->CallJavascriptFunction("cr.quota.messageHandler_", |
+ *message_data.get(), |
+ value); |
+ } |
+ |
+ scoped_refptr<QuotaInternalsProxy> proxy_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(QuotaInternalsMessageHandler); |
+}; |
+ |
+typedef QuotaInternalsProxy QuotaInternalsProxyDeleter; |
michaeln
2011/06/01 01:09:32
doesn't look used anywhere
tzik
2011/06/03 16:01:19
Done.
|
+ |
+class QuotaInternalsProxy |
+ : public base::RefCountedThreadSafe<QuotaInternalsProxy, |
+ BrowserThread::DeleteOnIOThread> { |
michaeln
2011/06/01 01:09:32
Extending the life of the QM shouldn't be an issue
tzik
2011/06/03 16:01:19
I see. Done.
|
+ public: |
+ explicit QuotaInternalsProxy(QuotaInternalsMessageHandler* handler) |
+ : handler_(handler), |
+ callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
+ } |
+ |
+ // Called on IO Thread. |
+ void ReportAvailableSpace(int64 available_space) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportAvailableSpaceOnUIThread, |
+ available_space)); |
+ } |
+ |
+ void ReportGlobalData(const GlobalData& data) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportGlobalDataOnUIThread, |
+ data)); |
+ } |
+ |
+ void ReportHostData(const std::vector<HostData>& hosts) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportHostDataOnUIThread, |
+ hosts)); |
+ } |
+ |
+ void ReportOriginData(const std::vector<OriginData>& origins) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportOriginDataOnUIThread, |
+ origins)); |
+ } |
+ |
+ void ReportStatistics(const Statistics& stats) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ NewRunnableMethod( |
+ this, &QuotaInternalsProxy::RunReportStatisticsOnUIThread, |
+ stats)); |
+ } |
+ |
+ // Called on UI Thread. |
+ void RequestData(quota::QuotaManager* quota_manager) { |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ NewRunnableMethod(this, &QuotaInternalsProxy::RunRequestDataOnIOThread, |
+ make_scoped_refptr(quota_manager))); |
+ } |
-void QuotaInternalsHTMLSource::StartDataRequest(const std::string& path, |
- bool is_incognito, |
- int request_id) OVERRIDE { |
- base::StringPiece html( |
- ResourceBundle::GetSharedInstance().GetRawDataResource( |
- IDR_QUOTA_INTERNALS_INDEX_HTML)); |
- |
- scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
- html_bytes->data.resize(html.size()); |
- std::copy(html.data(), html.data() + html.size(), html_bytes->data.begin()); |
- SendResponse(request_id, html_bytes); |
- return; |
+ private: |
+ typedef quota::QuotaManager::QuotaTableEntry QuotaTableEntry; |
+ typedef quota::QuotaManager::LastAccessTimeTableEntry |
+ LastAccessTimeTableEntry; |
+ typedef quota::QuotaManager::QuotaTableEntries QuotaTableEntries; |
+ typedef quota::QuotaManager::LastAccessTimeTableEntries |
+ LastAccessTimeTableEntries; |
+ |
+ // Called on UI Thread. |
+ void RunReportAvailableSpaceOnUIThread(int64 available_space) { |
+ if (handler_) |
+ handler_->ReportAvailableSpace(available_space); |
+ } |
+ |
+ void RunReportGlobalDataOnUIThread(const GlobalData& data) { |
+ if (handler_) |
+ handler_->ReportGlobalData(data); |
+ } |
+ |
+ void RunReportHostDataOnUIThread(const std::vector<HostData>& hosts) { |
+ if (handler_) |
+ handler_->ReportHostData(hosts); |
+ } |
+ |
+ void RunReportOriginDataOnUIThread(const std::vector<OriginData>& origins) { |
+ if (handler_) |
+ handler_->ReportOriginData(origins); |
+ } |
+ |
+ void RunReportStatisticsOnUIThread(const Statistics& stats) { |
+ if (handler_) |
+ handler_->ReportStatistics(stats); |
+ } |
+ |
+ // Called on IO Thread. |
+ void RunRequestDataOnIOThread( |
+ scoped_refptr<quota::QuotaManager> quota_manager) { |
+ quota_manager_ = quota_manager->AsWeakPtr(); |
+ |
+ 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->DumpLastAccessTimeTable( |
+ callback_factory_.NewCallback( |
+ &QuotaInternalsProxy::DidDumpLastAccessTimeTable)); |
+ |
+ // TODO(tzik): |
+ // Call QuotaManager::GetStatistics() (to be implemented.) |
+ } |
+ |
+ // Called on IO Thread by QuotaManager as callback. |
+ void DidGetAvailableSpace(quota::QuotaStatusCode status, int64 space) { |
+ if (status == quota::kQuotaStatusOk) |
+ ReportAvailableSpace(space); |
+ } |
+ |
+ void DidGetGlobalQuota(quota::QuotaStatusCode status, |
+ quota::StorageType type, |
+ int64 quota) { |
+ if (status == quota::kQuotaStatusOk) |
+ ReportGlobalData(GlobalData(type, -1, -1, quota)); |
+ } |
+ |
+ void DidGetGlobalUsage(quota::StorageType type, |
+ int64 usage, |
+ int64 unlimited_usage) { |
+ ReportGlobalData(GlobalData(type, usage, unlimited_usage, -1)); |
+ RequestPerOriginData(type); |
+ } |
+ |
+ void DidDumpQuotaTable(const QuotaTableEntries& entries) { |
+ std::vector<HostData> host_data; |
+ host_data.reserve(entries.size()); |
+ |
+ typedef QuotaTableEntries::const_iterator iterator; |
+ for (iterator itr(entries.begin()), end(entries.end()); |
+ itr != end; ++itr) |
+ host_data.push_back(HostData(itr->host, itr->type, -1, itr->quota)); |
+ ReportHostData(host_data); |
+ } |
+ |
+ void DidDumpLastAccessTimeTable(const LastAccessTimeTableEntries& entries) { |
+ std::vector<OriginData> origin_data; |
+ origin_data.reserve(entries.size()); |
+ |
+ typedef LastAccessTimeTableEntries::const_iterator iterator; |
+ for (iterator itr(entries.begin()), end(entries.end()); |
+ itr != end; ++itr) |
+ origin_data.push_back( |
+ OriginData(itr->origin, itr->type, |
+ -1, itr->used_count, itr->last_access_time)); |
+ ReportOriginData(origin_data); |
+ } |
+ |
+ void 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}); |
michaeln
2011/06/01 01:09:32
use the ctor here instead
tzik
2011/06/03 16:01:19
Done.
|
+ 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); |
+ } |
+ |
+ // Helper. Called on IO Thread. |
+ void RequestPerOriginData(quota::StorageType type); |
+ |
+ void 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 GetHostUsage(const std::string& host, quota::StorageType type) { |
+ if (quota_manager_) |
+ quota_manager_->GetHostUsage( |
+ host, type, |
+ callback_factory_.NewCallback( |
+ &QuotaInternalsProxy::DidGetHostUsage)); |
+ } |
+ |
+ // Used on UI Thread. |
+ QuotaInternalsMessageHandler* handler_; |
+ |
+ // Used on IO Thread. |
+ base::ScopedCallbackFactory<QuotaInternalsProxy> callback_factory_; |
+ base::WeakPtr<quota::QuotaManager> quota_manager_; |
michaeln
2011/05/30 19:36:57
can this be a scoped_refptr<>
tzik
2011/06/03 16:01:19
Done.
|
+ std::set<std::pair<std::string, quota::StorageType> > |
+ hosts_visited_, hosts_pending_; |
+ std::vector<HostData> report_pending_; |
+ |
+ friend class QuotaInternalsMessageHandler; |
+ friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
+ friend class DeleteTask<QuotaInternalsProxy>; |
+ |
+ virtual ~QuotaInternalsProxy() {} |
+}; |
+ |
+void QuotaInternalsMessageHandler::OnRequestData(const ListValue*) { |
michaeln
2011/06/01 01:09:32
I'd move this method definition next to the other
tzik
2011/06/03 16:01:19
Done.
|
+ if (!proxy_) |
+ proxy_ = new QuotaInternalsProxy(this); |
+ proxy_->RequestData(web_ui_->GetProfile()->GetQuotaManager()); |
} |
-std::string QuotaInternalsHTMLSource::GetMimeType( |
- const std::string& path_unused) const { |
- return "text/html"; |
+void QuotaInternalsProxy::RequestPerOriginData(quota::StorageType type) { |
+ if (!quota_manager_) |
+ return; |
+ |
+ 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()), end(origins.end()); |
Evan Stade
2011/06/01 01:15:04
it's confusing to define a variable here that's no
tzik
2011/06/03 16:01:19
Done.
|
+ itr != end; ++itr) { |
+ origin_data.push_back( |
+ OriginData(*itr, type, |
+ quota_manager_->IsOriginInUse(*itr) ? 1 : 0, -1, |
+ 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); |
} |
+QuotaInternalsMessageHandler::~QuotaInternalsMessageHandler() { |
+ if (proxy_) |
+ proxy_->handler_ = NULL; |
+} |
} // namespace quota_internals |
+ |
+QuotaInternalsUI::QuotaInternalsUI(TabContents* contents) |
+ : WebUI(contents) { |
+ WebUIMessageHandler* handler = |
+ new quota_internals::QuotaInternalsMessageHandler; |
+ AddMessageHandler(handler->Attach(this)); |
+ contents->profile()->GetChromeURLDataManager()-> |
+ AddDataSource(new quota_internals::QuotaInternalsHTMLSource); |
michaeln
2011/05/30 19:36:57
When is this data source removed? It looks like a
tzik
2011/05/31 04:53:01
Yes, we add DataSource in each quota-internals tab
michaeln
2011/06/01 01:09:32
I see, any old DataSource under this ones name is
|
+} |