Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / network_ui.cc
blob9950a67f9d408b1d8ecb6a8a13e079e73f090589
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"
26 namespace chromeos {
28 namespace {
30 bool GetServicePathFromGuid(const std::string& guid,
31 std::string* service_path) {
32 const NetworkState* network =
33 NetworkHandler::Get()->network_state_handler()->GetNetworkStateFromGuid(
34 guid);
35 if (!network)
36 return false;
37 *service_path = network->path();
38 return true;
41 void SetDeviceProperties(base::DictionaryValue* dictionary) {
42 std::string device;
43 dictionary->GetStringWithoutPathExpansion(shill::kDeviceProperty, &device);
44 const DeviceState* device_state =
45 NetworkHandler::Get()->network_state_handler()->GetDeviceState(device);
46 if (!device_state)
47 return;
49 scoped_ptr<base::DictionaryValue> device_dictionary(
50 device_state->properties().DeepCopy());
52 if (!device_state->ip_configs().empty()) {
53 // Convert IPConfig dictionary to a ListValue.
54 scoped_ptr<base::ListValue> ip_configs(new base::ListValue);
55 for (base::DictionaryValue::Iterator iter(device_state->ip_configs());
56 !iter.IsAtEnd(); iter.Advance()) {
57 ip_configs->Append(iter.value().DeepCopy());
59 device_dictionary->SetWithoutPathExpansion(shill::kIPConfigsProperty,
60 ip_configs.release());
62 if (!device_dictionary->empty())
63 dictionary->Set(shill::kDeviceProperty, device_dictionary.release());
66 class NetworkConfigMessageHandler : public content::WebUIMessageHandler {
67 public:
68 NetworkConfigMessageHandler() : weak_ptr_factory_(this) {}
69 ~NetworkConfigMessageHandler() override {}
71 // WebUIMessageHandler implementation.
72 void RegisterMessages() override {
73 web_ui()->RegisterMessageCallback(
74 "getShillProperties",
75 base::Bind(&NetworkConfigMessageHandler::GetShillProperties,
76 base::Unretained(this)));
79 private:
80 void GetShillProperties(const base::ListValue* arg_list) {
81 std::string guid;
82 if (!arg_list->GetString(0, &guid)) {
83 NOTREACHED();
84 ErrorCallback(guid, "Missing GUID in arg list", nullptr);
85 return;
87 std::string service_path;
88 if (!GetServicePathFromGuid(guid, &service_path)) {
89 ErrorCallback(guid, "Error.InvalidNetworkGuid", nullptr);
90 return;
92 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
93 service_path,
94 base::Bind(&NetworkConfigMessageHandler::GetShillPropertiesSuccess,
95 weak_ptr_factory_.GetWeakPtr()),
96 base::Bind(&NetworkConfigMessageHandler::ErrorCallback,
97 weak_ptr_factory_.GetWeakPtr(), guid));
100 void GetShillPropertiesSuccess(
101 const std::string& service_path,
102 const base::DictionaryValue& dictionary) const {
103 scoped_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy());
105 // Set the 'ServicePath' property for debugging.
106 dictionary_copy->SetStringWithoutPathExpansion("ServicePath", service_path);
107 // Set the device properties for debugging.
108 SetDeviceProperties(dictionary_copy.get());
110 base::ListValue return_arg_list;
111 return_arg_list.Append(dictionary_copy.release());
112 web_ui()->CallJavascriptFunction("NetworkUI.getShillPropertiesResult",
113 return_arg_list);
116 void ErrorCallback(
117 const std::string& guid,
118 const std::string& error_name,
119 scoped_ptr<base::DictionaryValue> /* error_data */) const {
120 NET_LOG(ERROR) << "Shill Error: " << error_name << " guid=" << guid;
121 base::ListValue return_arg_list;
122 scoped_ptr<base::DictionaryValue> dictionary;
123 dictionary->SetStringWithoutPathExpansion(shill::kGuidProperty, guid);
124 dictionary->SetStringWithoutPathExpansion("ShillError", error_name);
125 return_arg_list.Append(dictionary.release());
126 web_ui()->CallJavascriptFunction("NetworkUI.getShillPropertiesResult",
127 return_arg_list);
130 base::WeakPtrFactory<NetworkConfigMessageHandler> weak_ptr_factory_;
132 DISALLOW_COPY_AND_ASSIGN(NetworkConfigMessageHandler);
135 } // namespace
137 NetworkUI::NetworkUI(content::WebUI* web_ui)
138 : content::WebUIController(web_ui) {
139 web_ui->AddMessageHandler(new NetworkConfigMessageHandler());
141 // Enable extension API calls in the WebUI.
142 extensions::TabHelper::CreateForWebContents(web_ui->GetWebContents());
144 content::WebUIDataSource* html =
145 content::WebUIDataSource::Create(chrome::kChromeUINetworkHost);
147 html->AddLocalizedString("titleText", IDS_NETWORK_UI_TITLE);
148 html->AddLocalizedString("autoRefreshText", IDS_NETWORK_UI_AUTO_REFRESH);
149 html->AddLocalizedString("deviceLogLinkText", IDS_DEVICE_LOG_LINK_TEXT);
150 html->AddLocalizedString("networkRefreshText", IDS_NETWORK_UI_REFRESH);
151 html->AddLocalizedString("clickToExpandText", IDS_NETWORK_UI_EXPAND);
152 html->AddLocalizedString("propertyFormatText",
153 IDS_NETWORK_UI_PROPERTY_FORMAT);
155 html->AddLocalizedString("normalFormatOption", IDS_NETWORK_UI_FORMAT_NORMAL);
156 html->AddLocalizedString("managedFormatOption",
157 IDS_NETWORK_UI_FORMAT_MANAGED);
158 html->AddLocalizedString("stateFormatOption", IDS_NETWORK_UI_FORMAT_STATE);
159 html->AddLocalizedString("shillFormatOption", IDS_NETWORK_UI_FORMAT_SHILL);
161 html->AddLocalizedString("defaultNetworkText",
162 IDS_NETWORK_UI_DEFAULT_NETWORK);
163 html->AddLocalizedString("noNetworkText",
164 IDS_STATUSBAR_NO_NETWORKS_MESSAGE);
165 html->AddLocalizedString("visibleNetworksLabel",
166 IDS_NETWORK_UI_VISIBLE_NETWORKS);
167 html->AddLocalizedString("favoriteNetworksLabel",
168 IDS_NETWORK_UI_FAVORITE_NETWORKS);
170 html->SetJsonPath("strings.js");
171 html->AddResourcePath("network_ui.css", IDR_NETWORK_UI_CSS);
172 html->AddResourcePath("network_ui.js", IDR_NETWORK_UI_JS);
173 html->SetDefaultResource(IDR_NETWORK_UI_HTML);
175 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
176 html);
179 NetworkUI::~NetworkUI() {
182 } // namespace chromeos