NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / network_ui.cc
blob42c391f250e1500c818d364df42353cdf8756cc6
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/webui/chromeos/network_ui.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/json/json_string_value_serializer.h"
12 #include "base/values.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/url_constants.h"
15 #include "chromeos/network/network_event_log.h"
16 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
18 #include "content/public/browser/web_ui.h"
19 #include "content/public/browser/web_ui_data_source.h"
20 #include "content/public/browser/web_ui_message_handler.h"
21 #include "grit/browser_resources.h"
22 #include "grit/generated_resources.h"
24 namespace chromeos {
26 namespace {
28 const char kStringsJsFile[] = "strings.js";
29 const char kRequestNetworkInfoCallback[] = "requestNetworkInfo";
30 const char kNetworkEventLogTag[] = "networkEventLog";
31 const char kNetworkStateTag[] = "networkState";
32 const char kOnNetworkInfoReceivedFunction[] = "NetworkUI.onNetworkInfoReceived";
34 class NetworkMessageHandler : public content::WebUIMessageHandler {
35 public:
36 NetworkMessageHandler();
37 virtual ~NetworkMessageHandler();
39 // WebUIMessageHandler implementation.
40 virtual void RegisterMessages() OVERRIDE;
42 private:
43 void CollectNetworkInfo(const base::ListValue* value) const;
44 std::string GetNetworkEventLog() const;
45 void GetNetworkState(base::DictionaryValue* output) const;
46 void RespondToPage(const base::DictionaryValue& value) const;
48 DISALLOW_COPY_AND_ASSIGN(NetworkMessageHandler);
51 NetworkMessageHandler::NetworkMessageHandler() {
54 NetworkMessageHandler::~NetworkMessageHandler() {
57 void NetworkMessageHandler::RegisterMessages() {
58 web_ui()->RegisterMessageCallback(
59 kRequestNetworkInfoCallback,
60 base::Bind(&NetworkMessageHandler::CollectNetworkInfo,
61 base::Unretained(this)));
64 void NetworkMessageHandler::CollectNetworkInfo(
65 const base::ListValue* value) const {
66 base::DictionaryValue data;
67 data.SetString(kNetworkEventLogTag, GetNetworkEventLog());
68 base::DictionaryValue* networkState = new base::DictionaryValue;
69 GetNetworkState(networkState);
70 data.Set(kNetworkStateTag, networkState);
71 RespondToPage(data);
74 void NetworkMessageHandler::RespondToPage(
75 const base::DictionaryValue& value) const {
76 web_ui()->CallJavascriptFunction(kOnNetworkInfoReceivedFunction, value);
79 std::string NetworkMessageHandler::GetNetworkEventLog() const {
80 std::string format = "json";
81 return chromeos::network_event_log::GetAsString(
82 chromeos::network_event_log::NEWEST_FIRST,
83 format,
84 chromeos::network_event_log::LOG_LEVEL_DEBUG,
85 0);
88 void NetworkMessageHandler::GetNetworkState(
89 base::DictionaryValue* output) const {
90 chromeos::NetworkStateHandler* handler =
91 chromeos::NetworkHandler::Get()->network_state_handler();
92 chromeos::NetworkStateHandler::NetworkStateList network_list;
93 handler->GetNetworkList(&network_list);
95 for (chromeos::NetworkStateHandler::NetworkStateList::const_iterator it =
96 network_list.begin();
97 it != network_list.end();
98 ++it) {
99 base::DictionaryValue* properties = new base::DictionaryValue;
100 (*it)->GetProperties(properties);
101 output->Set((*it)->path(), properties);
105 } // namespace
107 NetworkUI::NetworkUI(content::WebUI* web_ui)
108 : content::WebUIController(web_ui) {
109 web_ui->AddMessageHandler(new NetworkMessageHandler());
111 content::WebUIDataSource* html =
112 content::WebUIDataSource::Create(chrome::kChromeUINetworkHost);
113 html->SetUseJsonJSFormatV2();
115 html->AddLocalizedString("titleText", IDS_NETWORK_TITLE);
116 html->AddLocalizedString("autoRefreshText", IDS_NETWORK_AUTO_REFRESH);
117 html->AddLocalizedString("logRefreshText", IDS_NETWORK_LOG_REFRESH);
118 html->AddLocalizedString("logLevelShowText", IDS_NETWORK_LOG_LEVEL_SHOW);
119 html->AddLocalizedString("logLevelErrorText", IDS_NETWORK_LOG_LEVEL_ERROR);
120 html->AddLocalizedString("logLevelUserText", IDS_NETWORK_LOG_LEVEL_USER);
121 html->AddLocalizedString("logLevelEventText", IDS_NETWORK_LOG_LEVEL_EVENT);
122 html->AddLocalizedString("logLevelDebugText", IDS_NETWORK_LOG_LEVEL_DEBUG);
123 html->AddLocalizedString("logLevelFileinfoText",
124 IDS_NETWORK_LOG_LEVEL_FILEINFO);
125 html->AddLocalizedString("logEntryFormat", IDS_NETWORK_LOG_ENTRY);
126 html->SetJsonPath(kStringsJsFile);
128 html->AddResourcePath("network.css", IDR_NETWORK_CSS);
129 html->AddResourcePath("network.js", IDR_NETWORK_JS);
130 html->SetDefaultResource(IDR_NETWORK_HTML);
132 Profile* profile = Profile::FromWebUI(web_ui);
133 content::WebUIDataSource::Add(profile, html);
136 NetworkUI::~NetworkUI() {
139 } // namespace chromeos