OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/chromeos/network_config_message_handler.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/logging.h" | |
12 #include "base/values.h" | |
13 #include "chromeos/login/login_state.h" | |
14 #include "chromeos/network/device_state.h" | |
15 #include "chromeos/network/managed_network_configuration_handler.h" | |
16 #include "chromeos/network/network_configuration_handler.h" | |
17 #include "chromeos/network/network_state.h" | |
18 #include "chromeos/network/network_state_handler.h" | |
19 #include "chromeos/network/network_util.h" | |
20 #include "chromeos/network/onc/onc_signature.h" | |
21 #include "chromeos/network/onc/onc_utils.h" | |
22 #include "chromeos/network/shill_property_util.h" | |
23 #include "components/onc/onc_constants.h" | |
24 #include "content/public/browser/web_ui.h" | |
25 #include "third_party/cros_system_api/dbus/service_constants.h" | |
26 | |
27 namespace chromeos { | |
28 | |
29 namespace { | |
30 | |
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; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 NetworkConfigMessageHandler::NetworkConfigMessageHandler() | |
45 : weak_ptr_factory_(this) { | |
46 } | |
47 | |
48 NetworkConfigMessageHandler::~NetworkConfigMessageHandler() { | |
49 } | |
50 | |
51 void NetworkConfigMessageHandler::RegisterMessages() { | |
52 web_ui()->RegisterMessageCallback( | |
53 "networkConfig.getNetworks", | |
54 base::Bind(&NetworkConfigMessageHandler::GetNetworks, | |
55 base::Unretained(this))); | |
56 web_ui()->RegisterMessageCallback( | |
57 "networkConfig.getProperties", | |
58 base::Bind(&NetworkConfigMessageHandler::GetProperties, | |
59 base::Unretained(this))); | |
60 web_ui()->RegisterMessageCallback( | |
61 "networkConfig.getManagedProperties", | |
62 base::Bind(&NetworkConfigMessageHandler::GetManagedProperties, | |
63 base::Unretained(this))); | |
64 web_ui()->RegisterMessageCallback( | |
65 "networkConfig.getShillProperties", | |
66 base::Bind(&NetworkConfigMessageHandler::GetShillProperties, | |
67 base::Unretained(this))); | |
68 } | |
69 | |
70 void NetworkConfigMessageHandler::GetNetworks( | |
71 const base::ListValue* arg_list) const { | |
72 int callback_id = 0; | |
73 const base::DictionaryValue* filter = NULL; | |
74 if (!arg_list->GetInteger(0, &callback_id) || | |
75 !arg_list->GetDictionary(1, &filter)) { | |
76 NOTREACHED(); | |
77 } | |
78 std::string type = ::onc::network_type::kAllTypes; | |
79 bool visible_only = false; | |
80 bool configured_only = false; | |
81 int limit = 1000; | |
82 filter->GetString("type", &type); | |
83 NetworkTypePattern pattern = onc::NetworkTypePatternFromOncType(type); | |
84 filter->GetBoolean("visible", &visible_only); | |
85 filter->GetBoolean("configured", &configured_only); | |
86 filter->GetInteger("limit", &limit); | |
87 | |
88 base::ListValue return_arg_list; | |
89 return_arg_list.AppendInteger(callback_id); | |
90 | |
91 scoped_ptr<base::ListValue> network_properties_list = | |
92 chromeos::network_util::TranslateNetworkListToONC( | |
93 pattern, configured_only, visible_only, limit, | |
94 true /* debugging_properties */); | |
95 | |
96 return_arg_list.Append(network_properties_list.release()); | |
97 | |
98 InvokeCallback(return_arg_list); | |
99 } | |
100 | |
101 void NetworkConfigMessageHandler::GetProperties( | |
102 const base::ListValue* arg_list) { | |
103 int callback_id = 0; | |
104 std::string guid; | |
105 if (!arg_list->GetInteger(0, &callback_id) || | |
106 !arg_list->GetString(1, &guid)) { | |
107 NOTREACHED(); | |
108 } | |
109 std::string service_path; | |
110 if (!GetServicePathFromGuid(guid, &service_path)) { | |
111 scoped_ptr<base::DictionaryValue> error_data; | |
112 ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass()); | |
113 return; | |
114 } | |
115 NetworkHandler::Get()->managed_network_configuration_handler()->GetProperties( | |
116 service_path, | |
117 base::Bind(&NetworkConfigMessageHandler::GetPropertiesSuccess, | |
118 weak_ptr_factory_.GetWeakPtr(), callback_id), | |
119 base::Bind(&NetworkConfigMessageHandler::ErrorCallback, | |
120 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
121 } | |
122 | |
123 void NetworkConfigMessageHandler::GetManagedProperties( | |
124 const base::ListValue* arg_list) { | |
125 int callback_id = 0; | |
126 std::string guid; | |
127 if (!arg_list->GetInteger(0, &callback_id) || | |
128 !arg_list->GetString(1, &guid)) { | |
129 NOTREACHED(); | |
130 } | |
131 std::string service_path; | |
132 if (!GetServicePathFromGuid(guid, &service_path)) { | |
133 scoped_ptr<base::DictionaryValue> error_data; | |
134 ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass()); | |
135 return; | |
136 } | |
137 NetworkHandler::Get()->managed_network_configuration_handler()-> | |
138 GetManagedProperties( | |
139 LoginState::Get()->primary_user_hash(), | |
140 service_path, | |
141 base::Bind(&NetworkConfigMessageHandler::GetPropertiesSuccess, | |
142 weak_ptr_factory_.GetWeakPtr(), callback_id), | |
143 base::Bind(&NetworkConfigMessageHandler::ErrorCallback, | |
144 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
145 } | |
146 | |
147 void NetworkConfigMessageHandler::GetPropertiesSuccess( | |
148 int callback_id, | |
149 const std::string& service_path, | |
150 const base::DictionaryValue& dictionary) const { | |
151 base::ListValue return_arg_list; | |
152 return_arg_list.AppendInteger(callback_id); | |
153 | |
154 base::DictionaryValue* network_properties = dictionary.DeepCopy(); | |
155 // Set the 'ServicePath' property for debugging. | |
156 network_properties->SetStringWithoutPathExpansion( | |
157 "ServicePath", service_path); | |
158 | |
159 return_arg_list.Append(network_properties); | |
160 InvokeCallback(return_arg_list); | |
161 } | |
162 | |
163 void NetworkConfigMessageHandler::GetShillProperties( | |
164 const base::ListValue* arg_list) { | |
165 int callback_id = 0; | |
166 std::string guid; | |
167 if (!arg_list->GetInteger(0, &callback_id) || | |
168 !arg_list->GetString(1, &guid)) { | |
169 NOTREACHED(); | |
170 } | |
171 std::string service_path; | |
172 if (!GetServicePathFromGuid(guid, &service_path)) { | |
173 scoped_ptr<base::DictionaryValue> error_data; | |
174 ErrorCallback(callback_id, "Error.InvalidNetworkGuid", error_data.Pass()); | |
175 return; | |
176 } | |
177 NetworkHandler::Get()->network_configuration_handler()->GetProperties( | |
178 service_path, | |
179 base::Bind(&NetworkConfigMessageHandler::GetShillPropertiesSuccess, | |
180 weak_ptr_factory_.GetWeakPtr(), callback_id), | |
181 base::Bind(&NetworkConfigMessageHandler::ErrorCallback, | |
182 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
183 } | |
184 | |
185 void NetworkConfigMessageHandler::GetShillPropertiesSuccess( | |
186 int callback_id, | |
187 const std::string& service_path, | |
188 const base::DictionaryValue& dictionary) const { | |
189 scoped_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy()); | |
190 // Set the 'ServicePath' property for debugging. | |
191 dictionary_copy->SetStringWithoutPathExpansion("ServicePath", service_path); | |
192 | |
193 // Get the device properties for debugging. | |
194 std::string device; | |
195 dictionary_copy->GetStringWithoutPathExpansion( | |
196 shill::kDeviceProperty, &device); | |
197 const DeviceState* device_state = | |
198 NetworkHandler::Get()->network_state_handler()->GetDeviceState(device); | |
199 if (device_state) { | |
200 base::DictionaryValue* device_dictionary = | |
201 device_state->properties().DeepCopy(); | |
202 dictionary_copy->Set(shill::kDeviceProperty, device_dictionary); | |
203 | |
204 // Convert IPConfig dictionary to a ListValue. | |
205 base::ListValue* ip_configs = new base::ListValue; | |
206 for (base::DictionaryValue::Iterator iter(device_state->ip_configs()); | |
207 !iter.IsAtEnd(); iter.Advance()) { | |
208 ip_configs->Append(iter.value().DeepCopy()); | |
209 } | |
210 device_dictionary->SetWithoutPathExpansion( | |
211 shill::kIPConfigsProperty, ip_configs); | |
212 } | |
213 | |
214 base::ListValue return_arg_list; | |
215 return_arg_list.AppendInteger(callback_id); | |
216 return_arg_list.Append(dictionary_copy.release()); | |
217 InvokeCallback(return_arg_list); | |
218 } | |
219 | |
220 void NetworkConfigMessageHandler::InvokeCallback( | |
221 const base::ListValue& arg_list) const { | |
222 web_ui()->CallJavascriptFunction( | |
223 "networkConfig.chromeCallbackSuccess", arg_list); | |
224 } | |
225 | |
226 void NetworkConfigMessageHandler::ErrorCallback( | |
227 int callback_id, | |
228 const std::string& error_name, | |
229 scoped_ptr<base::DictionaryValue> error_data) const { | |
230 LOG(ERROR) << "NetworkConfigMessageHandler Error: " << error_name; | |
231 base::ListValue arg_list; | |
232 arg_list.AppendInteger(callback_id); | |
233 arg_list.AppendString(error_name); | |
234 web_ui()->CallJavascriptFunction( | |
235 "networkConfig.chromeCallbackError", arg_list); | |
236 } | |
237 | |
238 } // namespace chromeos | |
OLD | NEW |