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

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: Addressed Steven's 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);
gauravsh 2013/03/05 21:04:56 SetResult will take ownership of |network_properti
pneubeck (no reviews) 2013/03/07 15:53:02 yes.
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* state_handler =
245 api::GetVisibleNetworks::Params::ToString(params->type), 80 NetworkStateHandler::Get();
stevenjb 2013/03/05 17:59:18 One line?
pneubeck (no reviews) 2013/03/07 15:53:02 Done.
246 base::Bind( 81 NetworkStateHandler::NetworkStateList network_states;
247 &NetworkingPrivateGetVisibleNetworksFunction::SendResultCallback, 82 state_handler->GetNetworkList(&network_states);
gauravsh 2013/03/05 21:04:56 |state_handler| is used only once, do you really n
pneubeck (no reviews) 2013/03/07 15:53:02 Done.
248 this))); 83
84 base::ListValue* network_properties_list = new base::ListValue;
85 for (NetworkStateHandler::NetworkStateList::iterator it =
86 network_states.begin();
87 it != network_states.end(); ++it) {
88 const std::string& service_path = (*it)->path();
89 base::DictionaryValue shill_dictionary;
90 (*it)->FillDictionary(&shill_dictionary);
91
92 scoped_ptr<base::DictionaryValue> onc_network_part =
93 onc::TranslateShillServiceToONCPart(shill_dictionary,
94 &onc::kNetworkWithStateSignature);
95
96 std::string onc_type;
97 onc_network_part->GetStringWithoutPathExpansion(onc::network_config::kType,
98 &onc_type);
99 if (type_filter == onc::network_type::kAllTypes ||
100 onc_type == type_filter) {
101
stevenjb 2013/03/05 17:59:18 nit: remove blank line
pneubeck (no reviews) 2013/03/07 15:53:02 Done.
102 onc_network_part->SetStringWithoutPathExpansion(
103 onc::network_config::kGUID,
104 service_path);
105
106 network_properties_list->Append(onc_network_part.release());
107 }
108 }
109
110 SetResult(network_properties_list);
249 return true; 111 return true;
250 } 112 }
251 113
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 //////////////////////////////////////////////////////////////////////////////// 114 ////////////////////////////////////////////////////////////////////////////////
265 // NetworkingPrivateStartConnectFunction 115 // NetworkingPrivateStartConnectFunction
266 116
267 NetworkingPrivateStartConnectFunction:: 117 NetworkingPrivateStartConnectFunction::
268 ~NetworkingPrivateStartConnectFunction() { 118 ~NetworkingPrivateStartConnectFunction() {
269 } 119 }
270 120
271 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() { 121 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() {
272 SendResponse(true); 122 SendResponse(true);
273 } 123 }
274 124
275 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed( 125 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed(
276 const std::string& error_name, 126 const std::string& error_name,
277 const std::string& error_message) { 127 const scoped_ptr<base::DictionaryValue> error_data) {
278 error_ = error_name; 128 error_ = error_name;
279 SendResponse(false); 129 SendResponse(false);
280 } 130 }
281 131
282 bool NetworkingPrivateStartConnectFunction::RunImpl() { 132 bool NetworkingPrivateStartConnectFunction::RunImpl() {
283 scoped_ptr<api::StartConnect::Params> params = 133 scoped_ptr<api::StartConnect::Params> params =
284 api::StartConnect::Params::Create(*args_); 134 api::StartConnect::Params::Create(*args_);
285 EXTENSION_FUNCTION_VALIDATE(params); 135 EXTENSION_FUNCTION_VALIDATE(params);
286 136
287 // TODO(gspencer): For now, the "GUID" we receive from the JavaScript is going 137 // 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; 138 std::string service_path = params->network_guid;
291 139
292 DBusThreadManager::Get()->GetShillServiceClient()->Connect( 140 ManagedNetworkConfigurationHandler::Get()->Connect(
293 dbus::ObjectPath(service_path), 141 service_path,
294 base::Bind( 142 base::Bind(
295 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess, 143 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess,
296 this), 144 this),
297 base::Bind(&NetworkingPrivateStartConnectFunction::ConnectionStartFailed, 145 base::Bind(
298 this)); 146 &NetworkingPrivateStartConnectFunction::ConnectionStartFailed,
147 this));
299 return true; 148 return true;
300 } 149 }
301 150
302 //////////////////////////////////////////////////////////////////////////////// 151 ////////////////////////////////////////////////////////////////////////////////
303 // NetworkingPrivateStartDisconnectFunction 152 // NetworkingPrivateStartDisconnectFunction
304 153
305 NetworkingPrivateStartDisconnectFunction:: 154 NetworkingPrivateStartDisconnectFunction::
306 ~NetworkingPrivateStartDisconnectFunction() { 155 ~NetworkingPrivateStartDisconnectFunction() {
307 } 156 }
308 157
309 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() { 158 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() {
310 SendResponse(true); 159 SendResponse(true);
311 } 160 }
312 161
313 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed( 162 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed(
314 const std::string& error_name, 163 const std::string& error_name,
315 const std::string& error_message) { 164 const scoped_ptr<base::DictionaryValue> error_data) {
316 error_ = error_name; 165 error_ = error_name;
317 SendResponse(false); 166 SendResponse(false);
318 } 167 }
319 168
320 bool NetworkingPrivateStartDisconnectFunction::RunImpl() { 169 bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
321 scoped_ptr<api::StartDisconnect::Params> params = 170 scoped_ptr<api::StartDisconnect::Params> params =
322 api::StartDisconnect::Params::Create(*args_); 171 api::StartDisconnect::Params::Create(*args_);
323 EXTENSION_FUNCTION_VALIDATE(params); 172 EXTENSION_FUNCTION_VALIDATE(params);
324 173
325 // TODO(gspencer): Currently the |network_guid| parameter is storing the 174 // The |network_guid| parameter is storing the service path.
326 // service path. Convert to using the actual GUID when we start using 175 std::string service_path = params->network_guid;
327 // the NetworkStateHandler. 176
328 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect( 177 ManagedNetworkConfigurationHandler::Get()->Disconnect(
329 dbus::ObjectPath(params->network_guid), 178 service_path,
330 base::Bind( 179 base::Bind(
331 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess, 180 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess,
332 this), 181 this),
333 base::Bind( 182 base::Bind(
334 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed, 183 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed,
335 this)); 184 this));
336 return true; 185 return true;
337 } 186 }
338 187
339 //////////////////////////////////////////////////////////////////////////////// 188 ////////////////////////////////////////////////////////////////////////////////
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 const std::string& result) { 297 const std::string& result) {
449 results_ = api::VerifyAndEncryptData::Results::Create(result); 298 results_ = api::VerifyAndEncryptData::Results::Create(result);
450 SendResponse(true); 299 SendResponse(true);
451 } 300 }
452 301
453 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback( 302 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback(
454 const std::string& error_name, const std::string& error) { 303 const std::string& error_name, const std::string& error) {
455 error_ = error_name; 304 error_ = error_name;
456 SendResponse(false); 305 SendResponse(false);
457 } 306 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698