summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/quota_internals_handler.cc
blob: 2c68c5d93dfa7d589560bc6b05dde445b7220180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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 <string>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/quota_internals_proxy.h"
#include "chrome/browser/ui/webui/quota_internals_types.h"
#include "net/base/net_util.h"

namespace quota_internals {

QuotaInternalsHandler::QuotaInternalsHandler() {}

QuotaInternalsHandler::~QuotaInternalsHandler() {
  if (proxy_)
    proxy_->handler_ = NULL;
}

void QuotaInternalsHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback("requestInfo",
      base::Bind(&QuotaInternalsHandler::OnRequestInfo,
                 base::Unretained(this)));
}

void QuotaInternalsHandler::ReportAvailableSpace(int64 available_space) {
  scoped_ptr<base::Value> avail(
      base::Value::CreateDoubleValue(static_cast<double>(available_space)));
  SendMessage("AvailableSpaceUpdated", *avail);
}

void QuotaInternalsHandler::ReportGlobalInfo(const GlobalStorageInfo& data) {
  scoped_ptr<base::Value> value(data.NewValue());
  SendMessage("GlobalInfoUpdated", *value);
}

void QuotaInternalsHandler::ReportPerHostInfo(
    const std::vector<PerHostStorageInfo>& hosts) {
  base::ListValue values;
  typedef std::vector<PerHostStorageInfo>::const_iterator iterator;
  for (iterator itr(hosts.begin()); itr != hosts.end(); ++itr) {
    values.Append(itr->NewValue());
  }

  SendMessage("PerHostInfoUpdated", values);
}

void QuotaInternalsHandler::ReportPerOriginInfo(
    const std::vector<PerOriginStorageInfo>& origins) {
  base::ListValue origins_value;
  typedef std::vector<PerOriginStorageInfo>::const_iterator iterator;
  for (iterator itr(origins.begin()); itr != origins.end(); ++itr) {
    origins_value.Append(itr->NewValue());
  }

  SendMessage("PerOriginInfoUpdated", origins_value);
}

void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) {
  base::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 base::Value& value) {
  scoped_ptr<base::Value> message_data(base::Value::CreateStringValue(message));
  web_ui()->CallJavascriptFunction("cr.quota.messageHandler",
                                   *message_data,
                                   value);
}

void QuotaInternalsHandler::OnRequestInfo(const base::ListValue*) {
  if (!proxy_)
    proxy_ = new QuotaInternalsProxy(this);
  proxy_->RequestInfo(Profile::FromWebUI(web_ui())->GetQuotaManager());
}

}  // namespace quota_internals