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/quota_internals_handler.h"
6
7#include <string>
8
9#include "base/bind.h"
10#include "base/bind_helpers.h"
11#include "base/values.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/ui/webui/quota_internals/quota_internals_proxy.h"
14#include "chrome/browser/ui/webui/quota_internals/quota_internals_types.h"
15#include "content/public/browser/storage_partition.h"
16#include "content/public/browser/web_ui.h"
17#include "net/base/net_util.h"
18
19using content::BrowserContext;
20
21namespace quota_internals {
22
23QuotaInternalsHandler::QuotaInternalsHandler() {}
24
25QuotaInternalsHandler::~QuotaInternalsHandler() {
26  if (proxy_.get())
27    proxy_->handler_ = NULL;
28}
29
30void QuotaInternalsHandler::RegisterMessages() {
31  web_ui()->RegisterMessageCallback("requestInfo",
32      base::Bind(&QuotaInternalsHandler::OnRequestInfo,
33                 base::Unretained(this)));
34}
35
36void QuotaInternalsHandler::ReportAvailableSpace(int64 available_space) {
37  SendMessage("AvailableSpaceUpdated",
38              base::FundamentalValue(static_cast<double>(available_space)));
39}
40
41void QuotaInternalsHandler::ReportGlobalInfo(const GlobalStorageInfo& data) {
42  scoped_ptr<base::Value> value(data.NewValue());
43  SendMessage("GlobalInfoUpdated", *value);
44}
45
46void QuotaInternalsHandler::ReportPerHostInfo(
47    const std::vector<PerHostStorageInfo>& hosts) {
48  base::ListValue values;
49  typedef std::vector<PerHostStorageInfo>::const_iterator iterator;
50  for (iterator itr(hosts.begin()); itr != hosts.end(); ++itr) {
51    values.Append(itr->NewValue());
52  }
53
54  SendMessage("PerHostInfoUpdated", values);
55}
56
57void QuotaInternalsHandler::ReportPerOriginInfo(
58    const std::vector<PerOriginStorageInfo>& origins) {
59  base::ListValue origins_value;
60  typedef std::vector<PerOriginStorageInfo>::const_iterator iterator;
61  for (iterator itr(origins.begin()); itr != origins.end(); ++itr) {
62    origins_value.Append(itr->NewValue());
63  }
64
65  SendMessage("PerOriginInfoUpdated", origins_value);
66}
67
68void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) {
69  base::DictionaryValue dict;
70  typedef Statistics::const_iterator iterator;
71  for (iterator itr(stats.begin()); itr != stats.end(); ++itr) {
72    dict.SetString(itr->first, itr->second);
73  }
74
75  SendMessage("StatisticsUpdated", dict);
76}
77
78void QuotaInternalsHandler::SendMessage(const std::string& message,
79                                        const base::Value& value) {
80  web_ui()->CallJavascriptFunction(
81      "cr.quota.messageHandler", base::StringValue(message), value);
82}
83
84void QuotaInternalsHandler::OnRequestInfo(const base::ListValue*) {
85  if (!proxy_.get())
86    proxy_ = new QuotaInternalsProxy(this);
87  proxy_->RequestInfo(
88      BrowserContext::GetDefaultStoragePartition(
89          Profile::FromWebUI(web_ui()))->GetQuotaManager());
90}
91
92}  // namespace quota_internals
93