Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(724)

Side by Side Diff: chrome/browser/chromeos/extensions/networking_private_api.cc

Issue 12319145: Using the new Network*Handlers in networkingPrivate Extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed some closing periods from comments. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/extensions/networking_private_api.h" 5 #include "chrome/browser/chromeos/extensions/networking_private_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "chrome/browser/chromeos/net/managed_network_configuration_handler.h" 10 #include "chrome/browser/chromeos/net/managed_network_configuration_handler.h"
11 #include "chrome/browser/extensions/extension_function_registry.h" 11 #include "chrome/browser/extensions/extension_function_registry.h"
12 #include "chrome/common/extensions/api/networking_private.h" 12 #include "chrome/common/extensions/api/networking_private.h"
13 #include "chromeos/dbus/dbus_thread_manager.h" 13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/shill_manager_client.h" 14 #include "chromeos/dbus/shill_manager_client.h"
15 #include "chromeos/dbus/shill_service_client.h" 15 #include "chromeos/network/network_state.h"
16 #include "chromeos/network/network_state_handler.h"
16 #include "chromeos/network/onc/onc_constants.h" 17 #include "chromeos/network/onc/onc_constants.h"
17 #include "chromeos/network/onc/onc_signature.h" 18 #include "chromeos/network/onc/onc_signature.h"
18 #include "chromeos/network/onc/onc_translation_tables.h"
19 #include "chromeos/network/onc/onc_translator.h" 19 #include "chromeos/network/onc/onc_translator.h"
20 #include "dbus/object_path.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h"
22 20
23 using namespace chromeos; 21 using namespace chromeos;
24 namespace api = extensions::api::networking_private; 22 namespace api = extensions::api::networking_private;
25 23
26 namespace {
27
28 // An error returned when no valid services were found.
29 const char kInvalidResponseError[] = "Error.invalidResponse";
30
31 // Filters from the given ONC dictionary the information we're interested in
32 // before passing it to JavaScript.
33 scoped_ptr<api::NetworkProperties> CreateFilteredResult(
34 const base::DictionaryValue& onc_dictionary) {
35 static const char* const desired_fields[] = {
36 onc::network_config::kWiFi,
37 onc::network_config::kName,
38 onc::network_config::kGUID,
39 onc::network_config::kType,
40 onc::network_config::kConnectionState,
41 };
42
43 scoped_ptr<api::NetworkProperties> filtered_result(
44 new api::NetworkProperties);
45 for (size_t i = 0; i < arraysize(desired_fields); ++i) {
46 const base::Value* value;
47 if (onc_dictionary.GetWithoutPathExpansion(desired_fields[i], &value)) {
48 filtered_result->additional_properties.SetWithoutPathExpansion(
49 desired_fields[i],
50 value->DeepCopy());
51 }
52 }
53
54 return filtered_result.Pass();
55 }
56
57 class ResultList : public base::RefCounted<ResultList> {
58 public:
59 typedef base::Callback<void(const std::string& error,
60 scoped_ptr<base::ListValue>)> ResultCallback;
61
62 ResultList(const std::string& type, const ResultCallback& callback)
63 : callback_(callback) {
64 DBusThreadManager::Get()->GetShillManagerClient()->GetProperties(
65 base::Bind(&ResultList::ManagerPropertiesCallback, this, type));
66 }
67
68 scoped_ptr<base::ListValue> GetResults() {
69 return api::GetVisibleNetworks::Results::Create(list_);
70 }
71
72 private:
73 friend class base::RefCounted<ResultList>;
74
75 ~ResultList() {
76 callback_.Run(std::string(), GetResults());
77 }
78
79 void Append(api::NetworkProperties* value) {
80 list_.push_back(linked_ptr<api::NetworkProperties>(value));
81 }
82
83 // Receives the result of a call to GetProperties on the Shill Manager API.
84 void ManagerPropertiesCallback(const std::string& network_type,
85 chromeos::DBusMethodCallStatus call_status,
86 const base::DictionaryValue& result);
87
88 // Receives the result of a call to GetProperties on the Shill Service API.
89 void ServicePropertiesCallback(const std::string& service_path,
90 const std::string& network_type,
91 chromeos::DBusMethodCallStatus call_status,
92 const base::DictionaryValue& result);
93
94 std::vector<linked_ptr<api::NetworkProperties> > list_;
95 ResultCallback callback_;
96
97 DISALLOW_COPY_AND_ASSIGN(ResultList);
98 };
99
100 // For each of the available services, fire off a request for its properties.
101 void ResultList::ManagerPropertiesCallback(
102 const std::string& network_type,
103 DBusMethodCallStatus call_status,
104 const base::DictionaryValue& result) {
105 const base::ListValue* available_services;
106 if (!result.GetList(flimflam::kServicesProperty, &available_services)) {
107 LOG(ERROR)
108 << "ShillManagerClient::GetProperties returned malformed service list.";
109 callback_.Run(kInvalidResponseError, make_scoped_ptr(new base::ListValue));
110 return;
111 }
112 // If there just are no services, return an empty list.
113 if (available_services->GetSize() == 0) {
114 callback_.Run(std::string(), make_scoped_ptr(new base::ListValue));
115 return;
116 }
117 for (base::ListValue::const_iterator iter = available_services->begin();
118 iter != available_services->end(); ++iter) {
119 std::string service_path;
120 if (!(*iter)->GetAsString(&service_path)) {
121 LOG(ERROR)
122 << "ShillManagerClient::GetProperties returned malformed service.";
123 continue;
124 }
125
126 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
127 dbus::ObjectPath(service_path),
128 base::Bind(
129 &ResultList::ServicePropertiesCallback,
130 this,
131 service_path,
132 network_type));
133 }
134 }
135
136 void ResultList::ServicePropertiesCallback(
137 const std::string& service_path,
138 const std::string& network_type,
139 DBusMethodCallStatus call_status,
140 const base::DictionaryValue& result) {
141 if (call_status == DBUS_METHOD_CALL_SUCCESS) {
142 scoped_ptr<base::DictionaryValue> onc_properties(
143 onc::TranslateShillServiceToONCPart(
144 result,
145 &onc::kNetworkWithStateSignature));
146
147 scoped_ptr<api::NetworkProperties> filtered_result(
148 CreateFilteredResult(*onc_properties));
149
150 std::string onc_type;
151 if (filtered_result->additional_properties.GetString(
152 onc::network_config::kType, &onc_type) &&
153 (onc_type == network_type ||
154 network_type == onc::network_type::kAllTypes)) {
155 // TODO(gspencer): For now the "GUID" we send back is going to look
156 // remarkably like the service path. Once this code starts using the
157 // NetworkStateHandler instead of Shill directly, we should remove
158 // this line so that we're sending back the actual GUID. The
159 // JavaScript shouldn't care: this ID is opaque to it, and it
160 // shouldn't store it anywhere.
161 filtered_result->additional_properties.SetStringWithoutPathExpansion(
162 onc::network_config::kGUID, service_path);
163
164 Append(filtered_result.release());
165 }
166 }
167 }
168
169 } // namespace
170
171 //////////////////////////////////////////////////////////////////////////////// 24 ////////////////////////////////////////////////////////////////////////////////
172 // NetworkingPrivateGetPropertiesFunction 25 // NetworkingPrivateGetPropertiesFunction
173 26
174 NetworkingPrivateGetPropertiesFunction:: 27 NetworkingPrivateGetPropertiesFunction::
175 ~NetworkingPrivateGetPropertiesFunction() { 28 ~NetworkingPrivateGetPropertiesFunction() {
176 } 29 }
177 30
178 bool NetworkingPrivateGetPropertiesFunction::RunImpl() { 31 bool NetworkingPrivateGetPropertiesFunction::RunImpl() {
179 scoped_ptr<api::GetProperties::Params> params = 32 scoped_ptr<api::GetProperties::Params> params =
180 api::GetProperties::Params::Create(*args_); 33 api::GetProperties::Params::Create(*args_);
181 EXTENSION_FUNCTION_VALIDATE(params); 34 EXTENSION_FUNCTION_VALIDATE(params);
182 if (ManagedNetworkConfigurationHandler::IsInitialized()) { 35 // The |network_guid| parameter is storing the service path.
183 ManagedNetworkConfigurationHandler::Get()->GetProperties( 36 std::string service_path = params->network_guid;
184 params->network_guid, 37
185 base::Bind( 38 ManagedNetworkConfigurationHandler::Get()->GetProperties(
186 &NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess, 39 service_path,
187 this), 40 base::Bind(
188 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed, 41 &NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess,
189 this)); 42 this),
190 } else { 43 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed,
191 // TODO(gspencer): Currently we're using the service path as the 44 this));
192 // |network_guid|. Eventually this should be using the real GUID.
193 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
194 dbus::ObjectPath(params->network_guid),
195 base::Bind(&NetworkingPrivateGetPropertiesFunction::ResultCallback,
196 this,
197 params->network_guid));
198 }
199 return true; 45 return true;
200 } 46 }
201 47
202 void NetworkingPrivateGetPropertiesFunction::ResultCallback(
203 const std::string& service_path,
204 DBusMethodCallStatus call_status,
205 const base::DictionaryValue& result) {
206 scoped_ptr<base::DictionaryValue> onc_properties(
207 onc::TranslateShillServiceToONCPart(
208 result,
209 &onc::kNetworkWithStateSignature));
210 GetPropertiesSuccess(service_path,
211 *onc_properties);
212 }
213
214 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess( 48 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess(
215 const std::string& service_path, 49 const std::string& service_path,
216 const base::DictionaryValue& dictionary) { 50 const base::DictionaryValue& dictionary) {
217 scoped_ptr<api::NetworkProperties> filtered_result( 51 base::DictionaryValue* network_properties = dictionary.DeepCopy();
218 CreateFilteredResult(dictionary)); 52 network_properties->SetStringWithoutPathExpansion(onc::network_config::kGUID,
219 filtered_result->additional_properties.SetStringWithoutPathExpansion( 53 service_path);
220 onc::network_config::kGUID, service_path); 54 SetResult(network_properties);
221 results_ = api::GetProperties::Results::Create(*filtered_result);
222 SendResponse(true); 55 SendResponse(true);
223 } 56 }
224 57
225 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed( 58 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed(
226 const std::string& error_name, 59 const std::string& error_name,
227 scoped_ptr<base::DictionaryValue> error_data) { 60 scoped_ptr<base::DictionaryValue> error_data) {
228 error_ = error_name; 61 error_ = error_name;
229 SendResponse(false); 62 SendResponse(false);
230 } 63 }
231 64
232 //////////////////////////////////////////////////////////////////////////////// 65 ////////////////////////////////////////////////////////////////////////////////
233 // NetworkingPrivateGetVisibleNetworksFunction 66 // NetworkingPrivateGetVisibleNetworksFunction
234 67
235 NetworkingPrivateGetVisibleNetworksFunction:: 68 NetworkingPrivateGetVisibleNetworksFunction::
236 ~NetworkingPrivateGetVisibleNetworksFunction() { 69 ~NetworkingPrivateGetVisibleNetworksFunction() {
237 } 70 }
238 71
239 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { 72 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() {
240 scoped_ptr<api::GetVisibleNetworks::Params> params = 73 scoped_ptr<api::GetVisibleNetworks::Params> params =
241 api::GetVisibleNetworks::Params::Create(*args_); 74 api::GetVisibleNetworks::Params::Create(*args_);
242 EXTENSION_FUNCTION_VALIDATE(params); 75 EXTENSION_FUNCTION_VALIDATE(params);
76 std::string type_filter =
77 api::GetVisibleNetworks::Params::ToString(params->type);
243 78
244 scoped_refptr<ResultList> result_list(new ResultList( 79 NetworkStateHandler::NetworkStateList network_states;
245 api::GetVisibleNetworks::Params::ToString(params->type), 80 NetworkStateHandler::Get()->GetNetworkList(&network_states);
246 base::Bind( 81
247 &NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback, 82 base::ListValue* network_properties_list = new base::ListValue;
248 this))); 83 for (NetworkStateHandler::NetworkStateList::iterator it =
84 network_states.begin();
85 it != network_states.end(); ++it) {
86 const std::string& service_path = (*it)->path();
87 base::DictionaryValue shill_dictionary;
88 (*it)->GetProperties(&shill_dictionary);
89
90 scoped_ptr<base::DictionaryValue> onc_network_part =
91 onc::TranslateShillServiceToONCPart(shill_dictionary,
92 &onc::kNetworkWithStateSignature);
93
94 std::string onc_type;
95 onc_network_part->GetStringWithoutPathExpansion(onc::network_config::kType,
96 &onc_type);
97 if (type_filter == onc::network_type::kAllTypes ||
98 onc_type == type_filter) {
99 onc_network_part->SetStringWithoutPathExpansion(
100 onc::network_config::kGUID,
101 service_path);
102 network_properties_list->Append(onc_network_part.release());
103 }
104 }
105
106 SetResult(network_properties_list);
249 return true; 107 return true;
250 } 108 }
251 109
252 void NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback(
253 const std::string& error,
254 scoped_ptr<base::ListValue> result_list) {
255 if (!error.empty()) {
256 error_ = error;
257 SendResponse(false);
258 } else {
259 results_.reset(result_list.release());
260 SendResponse(true);
261 }
262 }
263
264 //////////////////////////////////////////////////////////////////////////////// 110 ////////////////////////////////////////////////////////////////////////////////
265 // NetworkingPrivateStartConnectFunction 111 // NetworkingPrivateStartConnectFunction
266 112
267 NetworkingPrivateStartConnectFunction:: 113 NetworkingPrivateStartConnectFunction::
268 ~NetworkingPrivateStartConnectFunction() { 114 ~NetworkingPrivateStartConnectFunction() {
269 } 115 }
270 116
271 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { 117 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() {
272 SendResponse(true); 118 SendResponse(true);
273 } 119 }
274 120
275 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( 121 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed(
276 const std::string& error_name, 122 const std::string& error_name,
277 const std::string& error_message) { 123 const scoped_ptr<base::DictionaryValue> error_data) {
278 error_ = error_name; 124 error_ = error_name;
279 SendResponse(false); 125 SendResponse(false);
280 } 126 }
281 127
282 bool NetworkingPrivateStartConnectFunction::RunImpl() { 128 bool NetworkingPrivateStartConnectFunction::RunImpl() {
283 scoped_ptr<api::StartConnect::Params> params = 129 scoped_ptr<api::StartConnect::Params> params =
284 api::StartConnect::Params::Create(*args_); 130 api::StartConnect::Params::Create(*args_);
285 EXTENSION_FUNCTION_VALIDATE(params); 131 EXTENSION_FUNCTION_VALIDATE(params);
286 132
287 // TODO(gspencer): For now, the "GUID" we receive from the JavaScript is going 133 // The |network_guid| parameter is storing the service path.
288 // to be the service path. Fix this so it actually looks up the service path
289 // from the GUID once we're using the NetworkStateHandler.
290 std::string service_path = params->network_guid; 134 std::string service_path = params->network_guid;
291 135
292 DBusThreadManager::Get()->GetShillServiceClient()->Connect( 136 ManagedNetworkConfigurationHandler::Get()->Connect(
293 dbus::ObjectPath(service_path), 137 service_path,
294 base::Bind( 138 base::Bind(
295 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, 139 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess,
296 this), 140 this),
297 base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, 141 base::Bind(
298 this)); 142 &NetworkingPrivateStartConnectFunction::ConnectionStartFailed,
143 this));
299 return true; 144 return true;
300 } 145 }
301 146
302 //////////////////////////////////////////////////////////////////////////////// 147 ////////////////////////////////////////////////////////////////////////////////
303 // NetworkingPrivateStartDisconnectFunction 148 // NetworkingPrivateStartDisconnectFunction
304 149
305 NetworkingPrivateStartDisconnectFunction:: 150 NetworkingPrivateStartDisconnectFunction::
306 ~NetworkingPrivateStartDisconnectFunction() { 151 ~NetworkingPrivateStartDisconnectFunction() {
307 } 152 }
308 153
309 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { 154 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() {
310 SendResponse(true); 155 SendResponse(true);
311 } 156 }
312 157
313 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( 158 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed(
314 const std::string& error_name, 159 const std::string& error_name,
315 const std::string& error_message) { 160 const scoped_ptr<base::DictionaryValue> error_data) {
316 error_ = error_name; 161 error_ = error_name;
317 SendResponse(false); 162 SendResponse(false);
318 } 163 }
319 164
320 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { 165 bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
321 scoped_ptr<api::StartDisconnect::Params> params = 166 scoped_ptr<api::StartDisconnect::Params> params =
322 api::StartDisconnect::Params::Create(*args_); 167 api::StartDisconnect::Params::Create(*args_);
323 EXTENSION_FUNCTION_VALIDATE(params); 168 EXTENSION_FUNCTION_VALIDATE(params);
324 169
325 // TODO(gspencer): Currently the |network_guid| parameter is storing the 170 // The |network_guid| parameter is storing the service path.
326 // service path. Convert to using the actual GUID when we start using 171 std::string service_path = params->network_guid;
327 // the NetworkStateHandler. 172
328 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect( 173 ManagedNetworkConfigurationHandler::Get()->Disconnect(
329 dbus::ObjectPath(params->network_guid), 174 service_path,
330 base::Bind( 175 base::Bind(
331 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, 176 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess,
332 this), 177 this),
333 base::Bind( 178 base::Bind(
334 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, 179 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed,
335 this)); 180 this));
336 return true; 181 return true;
337 } 182 }
338 183
339 //////////////////////////////////////////////////////////////////////////////// 184 ////////////////////////////////////////////////////////////////////////////////
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 const std::string& result) { 293 const std::string& result) {
449 results_ = api::VerifyAndEncryptData::Results::Create(result); 294 results_ = api::VerifyAndEncryptData::Results::Create(result);
450 SendResponse(true); 295 SendResponse(true);
451 } 296 }
452 297
453 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback( 298 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback(
454 const std::string& error_name, const std::string& error) { 299 const std::string& error_name, const std::string& error) {
455 error_ = error_name; 300 error_ = error_name;
456 SendResponse(false); 301 SendResponse(false);
457 } 302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698