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

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: Initial patch. 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_configuration_handler.h"
16 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
16 #include "chromeos/network/onc/onc_constants.h" 18 #include "chromeos/network/onc/onc_constants.h"
17 #include "chromeos/network/onc/onc_signature.h"
18 #include "chromeos/network/onc/onc_translation_tables.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 19
23 using namespace chromeos; 20 using namespace chromeos;
24 namespace api = extensions::api::networking_private; 21 namespace api = extensions::api::networking_private;
25 22
26 namespace { 23 namespace {
27 24
28 // An error returned when no valid services were found. 25 // Given the ONC translation of a network state, creates the state dictionary
29 const char kInvalidResponseError[] = "Error.invalidResponse"; 26 // which will be passed to JavaScript.
30 27 scoped_ptr<api::NetworkProperties> CreateNetworkProperties(
31 // Filters from the given ONC dictionary the information we're interested in 28 const std::string& service_path,
32 // before passing it to JavaScript.
33 scoped_ptr<api::NetworkProperties> CreateFilteredResult(
34 const base::DictionaryValue& onc_dictionary) { 29 const base::DictionaryValue& onc_dictionary) {
35 static const char* const desired_fields[] = { 30 scoped_ptr<api::NetworkProperties> result(
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); 31 new api::NetworkProperties);
45 for (size_t i = 0; i < arraysize(desired_fields); ++i) { 32 result->additional_properties.MergeDictionary(&onc_dictionary);
46 const base::Value* value; 33 result->additional_properties.SetStringWithoutPathExpansion(
47 if (onc_dictionary.GetWithoutPathExpansion(desired_fields[i], &value)) { 34 onc::network_config::kGUID, service_path);
48 filtered_result->additional_properties.SetWithoutPathExpansion( 35 return result.Pass();
49 desired_fields[i],
50 value->DeepCopy());
51 }
52 }
53
54 return filtered_result.Pass();
55 }
56
57 class ResultList : public base::RefCounted<ResultList> {
Greg Spencer (Chromium) 2013/03/02 00:09:11 It feels nice to get rid of this complexity finall
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::kNetworkConfigurationSignature));
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 } 36 }
168 37
169 } // namespace 38 } // namespace
170 39
171 //////////////////////////////////////////////////////////////////////////////// 40 ////////////////////////////////////////////////////////////////////////////////
172 // NetworkingPrivateGetPropertiesFunction 41 // NetworkingPrivateGetPropertiesFunction
173 42
174 NetworkingPrivateGetPropertiesFunction:: 43 NetworkingPrivateGetPropertiesFunction::
175 ~NetworkingPrivateGetPropertiesFunction() { 44 ~NetworkingPrivateGetPropertiesFunction() {
176 } 45 }
177 46
178 bool NetworkingPrivateGetPropertiesFunction::RunImpl() { 47 bool NetworkingPrivateGetPropertiesFunction::RunImpl() {
179 scoped_ptr<api::GetProperties::Params> params = 48 scoped_ptr<api::GetProperties::Params> params =
180 api::GetProperties::Params::Create(*args_); 49 api::GetProperties::Params::Create(*args_);
181 EXTENSION_FUNCTION_VALIDATE(params); 50 EXTENSION_FUNCTION_VALIDATE(params);
182 if (ManagedNetworkConfigurationHandler::IsInitialized()) { 51 // The |network_guid| parameter is storing the service path.
183 ManagedNetworkConfigurationHandler::Get()->GetProperties( 52 std::string service_path = params->network_guid;
184 params->network_guid, 53
185 base::Bind( 54 ManagedNetworkConfigurationHandler::Get()->GetProperties(
186 &NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess, 55 service_path,
187 this), 56 base::Bind(
188 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed, 57 &NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess,
189 this)); 58 this),
190 } else { 59 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed,
191 // TODO(gspencer): Currently we're using the service path as the 60 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; 61 return true;
200 } 62 }
201 63
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::kNetworkConfigurationSignature));
210 GetPropertiesSuccess(service_path,
211 *onc_properties);
212 }
213
214 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess( 64 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess(
215 const std::string& service_path, 65 const std::string& service_path,
216 const base::DictionaryValue& dictionary) { 66 const base::DictionaryValue& dictionary) {
217 scoped_ptr<api::NetworkProperties> filtered_result( 67 scoped_ptr<api::NetworkProperties> result(
218 CreateFilteredResult(dictionary)); 68 CreateNetworkProperties(service_path,
219 filtered_result->additional_properties.SetStringWithoutPathExpansion( 69 dictionary));
220 onc::network_config::kGUID, service_path); 70 results_ = api::GetProperties::Results::Create(*result);
221 results_ = api::GetProperties::Results::Create(*filtered_result);
222 SendResponse(true); 71 SendResponse(true);
223 } 72 }
224 73
225 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed( 74 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed(
226 const std::string& error_name, 75 const std::string& error_name,
227 scoped_ptr<base::DictionaryValue> error_data) { 76 scoped_ptr<base::DictionaryValue> error_data) {
228 error_ = error_name; 77 error_ = error_name;
229 SendResponse(false); 78 SendResponse(false);
230 } 79 }
231 80
232 //////////////////////////////////////////////////////////////////////////////// 81 ////////////////////////////////////////////////////////////////////////////////
233 // NetworkingPrivateGetVisibleNetworksFunction 82 // NetworkingPrivateGetVisibleNetworksFunction
234 83
235 NetworkingPrivateGetVisibleNetworksFunction:: 84 NetworkingPrivateGetVisibleNetworksFunction::
236 ~NetworkingPrivateGetVisibleNetworksFunction() { 85 ~NetworkingPrivateGetVisibleNetworksFunction() {
237 } 86 }
238 87
239 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() { 88 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() {
240 scoped_ptr<api::GetVisibleNetworks::Params> params = 89 scoped_ptr<api::GetVisibleNetworks::Params> params =
241 api::GetVisibleNetworks::Params::Create(*args_); 90 api::GetVisibleNetworks::Params::Create(*args_);
242 EXTENSION_FUNCTION_VALIDATE(params); 91 EXTENSION_FUNCTION_VALIDATE(params);
92 std::string type_filter =
93 api::GetVisibleNetworks::Params::ToString(params->type);
243 94
244 scoped_refptr<ResultList> result_list(new ResultList( 95 NetworkStateHandler* state_handler =
245 api::GetVisibleNetworks::Params::ToString(params->type), 96 NetworkStateHandler::Get();
246 base::Bind( 97 NetworkStateHandler::NetworkStateList network_states;
247 &NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback, 98 state_handler->GetNetworkList(&network_states);
248 this))); 99
100 std::vector<linked_ptr<api::NetworkProperties> > result;
101 for (NetworkStateHandler::NetworkStateList::iterator it =
102 network_states.begin();
103 it != network_states.end(); ++it) {
104 const std::string& service_path = (*it)->path();
105 scoped_ptr<base::DictionaryValue> onc_network_part =
106 (*it)->TranslateToONC();
107
108 std::string onc_type;
109 onc_network_part->GetStringWithoutPathExpansion(onc::network_config::kType,
110 &onc_type);
111 if (onc_type != type_filter &&
112 type_filter != onc::network_type::kAllTypes)
113 continue;
114
115 result.push_back(linked_ptr<api::NetworkProperties>(
116 CreateNetworkProperties(service_path,
117 *onc_network_part).release()));
118 }
119
120 results_ = api::GetVisibleNetworks::Results::Create(result);
249 return true; 121 return true;
250 } 122 }
251 123
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 //////////////////////////////////////////////////////////////////////////////// 124 ////////////////////////////////////////////////////////////////////////////////
265 // NetworkingPrivateStartConnectFunction 125 // NetworkingPrivateStartConnectFunction
266 126
267 NetworkingPrivateStartConnectFunction:: 127 NetworkingPrivateStartConnectFunction::
268 ~NetworkingPrivateStartConnectFunction() { 128 ~NetworkingPrivateStartConnectFunction() {
269 } 129 }
270 130
271 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { 131 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() {
272 SendResponse(true); 132 SendResponse(true);
273 } 133 }
274 134
275 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( 135 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed(
276 const std::string& error_name, 136 const std::string& error_name,
277 const std::string& error_message) { 137 const scoped_ptr<base::DictionaryValue> error_data) {
278 error_ = error_name; 138 error_ = error_name;
279 SendResponse(false); 139 SendResponse(false);
280 } 140 }
281 141
282 bool NetworkingPrivateStartConnectFunction::RunImpl() { 142 bool NetworkingPrivateStartConnectFunction::RunImpl() {
283 scoped_ptr<api::StartConnect::Params> params = 143 scoped_ptr<api::StartConnect::Params> params =
284 api::StartConnect::Params::Create(*args_); 144 api::StartConnect::Params::Create(*args_);
285 EXTENSION_FUNCTION_VALIDATE(params); 145 EXTENSION_FUNCTION_VALIDATE(params);
286 146
287 // TODO(gspencer): For now, the "GUID" we receive from the JavaScript is going 147 // 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; 148 std::string service_path = params->network_guid;
291 149
292 DBusThreadManager::Get()->GetShillServiceClient()->Connect( 150 NetworkConfigurationHandler::Get()->Connect(
293 dbus::ObjectPath(service_path), 151 service_path,
294 base::Bind( 152 base::Bind(
295 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, 153 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess,
296 this), 154 this),
297 base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, 155 base::Bind(
298 this)); 156 &NetworkingPrivateStartConnectFunction::ConnectionStartFailed,
157 this));
299 return true; 158 return true;
300 } 159 }
301 160
302 //////////////////////////////////////////////////////////////////////////////// 161 ////////////////////////////////////////////////////////////////////////////////
303 // NetworkingPrivateStartDisconnectFunction 162 // NetworkingPrivateStartDisconnectFunction
304 163
305 NetworkingPrivateStartDisconnectFunction:: 164 NetworkingPrivateStartDisconnectFunction::
306 ~NetworkingPrivateStartDisconnectFunction() { 165 ~NetworkingPrivateStartDisconnectFunction() {
307 } 166 }
308 167
309 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { 168 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() {
310 SendResponse(true); 169 SendResponse(true);
311 } 170 }
312 171
313 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( 172 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed(
314 const std::string& error_name, 173 const std::string& error_name,
315 const std::string& error_message) { 174 const scoped_ptr<base::DictionaryValue> error_data) {
316 error_ = error_name; 175 error_ = error_name;
317 SendResponse(false); 176 SendResponse(false);
318 } 177 }
319 178
320 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { 179 bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
321 scoped_ptr<api::StartDisconnect::Params> params = 180 scoped_ptr<api::StartDisconnect::Params> params =
322 api::StartDisconnect::Params::Create(*args_); 181 api::StartDisconnect::Params::Create(*args_);
323 EXTENSION_FUNCTION_VALIDATE(params); 182 EXTENSION_FUNCTION_VALIDATE(params);
324 183
325 // TODO(gspencer): Currently the |network_guid| parameter is storing the 184 // The |network_guid| parameter is storing the service path.
326 // service path. Convert to using the actual GUID when we start using 185 std::string service_path = params->network_guid;
327 // the NetworkStateHandler. 186
328 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect( 187 NetworkConfigurationHandler::Get()->Disconnect(
329 dbus::ObjectPath(params->network_guid), 188 service_path,
330 base::Bind( 189 base::Bind(
331 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, 190 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess,
332 this), 191 this),
333 base::Bind( 192 base::Bind(
334 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, 193 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed,
335 this)); 194 this));
336 return true; 195 return true;
337 } 196 }
338 197
339 //////////////////////////////////////////////////////////////////////////////// 198 ////////////////////////////////////////////////////////////////////////////////
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 const std::string& result) { 306 const std::string& result) {
448 results_ = api::VerifyAndSignData::Results::Create(result); 307 results_ = api::VerifyAndSignData::Results::Create(result);
449 SendResponse(true); 308 SendResponse(true);
450 } 309 }
451 310
452 void NetworkingPrivateVerifyAndSignDataFunction::ErrorCallback( 311 void NetworkingPrivateVerifyAndSignDataFunction::ErrorCallback(
453 const std::string& error_name, const std::string& error) { 312 const std::string& error_name, const std::string& error) {
454 error_ = error_name; 313 error_ = error_name;
455 SendResponse(false); 314 SendResponse(false);
456 } 315 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698