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"
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"
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
{
36 NetworkMessageHandler();
37 virtual ~NetworkMessageHandler();
39 // WebUIMessageHandler implementation.
40 virtual void RegisterMessages() OVERRIDE
;
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
);
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
,
84 chromeos::network_event_log::LOG_LEVEL_DEBUG
,
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
=
97 it
!= network_list
.end();
99 base::DictionaryValue
* properties
= new base::DictionaryValue
;
100 (*it
)->GetProperties(properties
);
101 output
->Set((*it
)->path(), properties
);
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