Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / network_ui.cc
blob08d81419bf911b25967f815368979eca26a2f1b7
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/memory/weak_ptr.h"
10 #include "base/values.h"
11 #include "chrome/browser/extensions/tab_helper.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "chromeos/network/device_state.h"
15 #include "chromeos/network/network_configuration_handler.h"
16 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
18 #include "components/device_event_log/device_event_log.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_ui.h"
21 #include "content/public/browser/web_ui_data_source.h"
22 #include "content/public/browser/web_ui_message_handler.h"
23 #include "grit/browser_resources.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
25 #include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
27 namespace chromeos {
29 namespace {
31 bool GetServicePathFromGuid(const std::string& guid,
32 std::string* service_path) {
33 const NetworkState* network =
34 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
35 guid);
36 if (!network)
37 return false;
38 *service_path = network->path();
39 return true;
42 void SetDeviceProperties(base::DictionaryValue* dictionary) {
43 std::string device;
44 dictionary->GetStringWithoutPathExpansion(shill::kDeviceProperty, &device);
45 const DeviceState* device_state =
46 NetworkHandler::Get()->network_state_handler()->GetDeviceState(device);
47 if (!device_state)
48 return;
50 scoped_ptr<base::DictionaryValue> device_dictionary(
51 device_state->properties().DeepCopy());
53 if (!device_state->ip_configs().empty()) {
54 // Convert IPConfig dictionary to a ListValue.
55 scoped_ptr<base::ListValue> ip_configs(new base::ListValue);
56 for (base::DictionaryValue::Iterator iter(device_state->ip_configs());
57 !iter.IsAtEnd(); iter.Advance()) {
58 ip_configs->Append(iter.value().DeepCopy());
60 device_dictionary->SetWithoutPathExpansion(shill::kIPConfigsProperty,
61 ip_configs.release());
63 if (!device_dictionary->empty())
64 dictionary->Set(shill::kDeviceProperty, device_dictionary.release());
67 class NetworkConfigMessageHandler : public content::WebUIMessageHandler {
68 public:
69 NetworkConfigMessageHandler() : weak_ptr_factory_(this) {}
70 ~NetworkConfigMessageHandler() override {}
72 // WebUIMessageHandler implementation.
73 void RegisterMessages() override {
74 web_ui()->RegisterMessageCallback(
75 "getShillProperties",
76 base::Bind(&NetworkConfigMessageHandler::GetShillProperties,
77 base::Unretained(this)));
80 private:
81 void GetShillProperties(const base::ListValue* arg_list) {
82 std::string guid;
83 if (!arg_list->GetString(0, &guid)) {
84 NOTREACHED();
85 ErrorCallback(guid, "Missing GUID in arg list", nullptr);
86 return;
88 std::string service_path;
89 if (!GetServicePathFromGuid(guid, &service_path)) {
90 ErrorCallback(guid, "Error.InvalidNetworkGuid", nullptr);
91 return;
93 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
94 service_path,
95 base::Bind(&NetworkConfigMessageHandler::GetShillPropertiesSuccess,
96 weak_ptr_factory_.GetWeakPtr()),
97 base::Bind(&NetworkConfigMessageHandler::ErrorCallback,
98 weak_ptr_factory_.GetWeakPtr(), guid));
101 void GetShillPropertiesSuccess(
102 const std::string& service_path,
103 const base::DictionaryValue& dictionary) const {
104 scoped_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy());
106 // Set the 'ServicePath' property for debugging.
107 dictionary_copy->SetStringWithoutPathExpansion("ServicePath", service_path);
108 // Set the device properties for debugging.
109 SetDeviceProperties(dictionary_copy.get());
111 base::ListValue return_arg_list;
112 return_arg_list.Append(dictionary_copy.release());
113 web_ui()->CallJavascriptFunction("NetworkUI.getShillPropertiesResult",
114 return_arg_list);
117 void ErrorCallback(
118 const std::string& guid,
119 const std::string& error_name,
120 scoped_ptr<base::DictionaryValue> /* error_data */) const {
121 NET_LOG(ERROR) << "Shill Error: " << error_name << " guid=" << guid;
122 base::ListValue return_arg_list;
123 scoped_ptr<base::DictionaryValue> dictionary;
124 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid);
125 dictionary->SetStringWithoutPathExpansion("ShillError", error_name);
126 return_arg_list.Append(dictionary.release());
127 web_ui()->CallJavascriptFunction("NetworkUI.getShillPropertiesResult",
128 return_arg_list);
131 base::WeakPtrFactory<NetworkConfigMessageHandler> weak_ptr_factory_;
133 DISALLOW_COPY_AND_ASSIGN(NetworkConfigMessageHandler);
136 } // namespace
138 NetworkUI::NetworkUI(content::WebUI* web_ui)
139 : content::WebUIController(web_ui) {
140 web_ui->AddMessageHandler(new NetworkConfigMessageHandler());
142 // Enable extension API calls in the WebUI.
143 extensions::TabHelper::CreateForWebContents(web_ui->GetWebContents());
145 content::WebUIDataSource* html =
146 content::WebUIDataSource::Create(chrome::kChromeUINetworkHost);
148 html->AddLocalizedString("titleText", IDS_NETWORK_UI_TITLE);
149 html->AddLocalizedString("autoRefreshText", IDS_NETWORK_UI_AUTO_REFRESH);
150 html->AddLocalizedString("deviceLogLinkText", IDS_DEVICE_LOG_LINK_TEXT);
151 html->AddLocalizedString("networkRefreshText", IDS_NETWORK_UI_REFRESH);
152 html->AddLocalizedString("clickToExpandText", IDS_NETWORK_UI_EXPAND);
153 html->AddLocalizedString("propertyFormatText",
154 IDS_NETWORK_UI_PROPERTY_FORMAT);
156 html->AddLocalizedString("normalFormatOption", IDS_NETWORK_UI_FORMAT_NORMAL);
157 html->AddLocalizedString("managedFormatOption",
158 IDS_NETWORK_UI_FORMAT_MANAGED);
159 html->AddLocalizedString("stateFormatOption", IDS_NETWORK_UI_FORMAT_STATE);
160 html->AddLocalizedString("shillFormatOption", IDS_NETWORK_UI_FORMAT_SHILL);
162 html->AddLocalizedString("visibleNetworksLabel",
163 IDS_NETWORK_UI_VISIBLE_NETWORKS);
164 html->AddLocalizedString("favoriteNetworksLabel",
165 IDS_NETWORK_UI_FAVORITE_NETWORKS);
167 html->AddLocalizedString("networkConnected",
168 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTED);
169 html->AddLocalizedString("networkConnecting",
170 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING);
171 html->AddLocalizedString("networkDisabled",
172 IDS_OPTIONS_SETTINGS_NETWORK_DISABLED);
173 html->AddLocalizedString("networkNotConnected",
174 IDS_ASH_STATUS_TRAY_NETWORK_NOT_CONNECTED);
175 html->AddLocalizedString("OncTypeCellular", IDS_NETWORK_TYPE_CELLULAR);
176 html->AddLocalizedString("OncTypeEthernet", IDS_NETWORK_TYPE_ETHERNET);
177 html->AddLocalizedString("OncTypeVPN", IDS_NETWORK_TYPE_VPN);
178 html->AddLocalizedString("OncTypeWiFi", IDS_NETWORK_TYPE_WIFI);
179 html->AddLocalizedString("OncTypeWimax", IDS_NETWORK_TYPE_WIMAX);
181 html->SetJsonPath("strings.js");
182 html->AddResourcePath("network_ui.css", IDR_NETWORK_UI_CSS);
183 html->AddResourcePath("network_ui.js", IDR_NETWORK_UI_JS);
184 html->SetDefaultResource(IDR_NETWORK_UI_HTML);
186 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
187 html);
190 NetworkUI::~NetworkUI() {
193 } // namespace chromeos