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