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/extensions/api/networking_private/networking_private_api.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/browser_process_platform_part_chromeos.h"
13 #include "chrome/browser/chromeos/profiles/profile_helper.h"
14 #include "chrome/browser/extensions/extension_function_registry.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/extensions/api/networking_private.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/shill_manager_client.h"
19 #include "chromeos/network/managed_network_configuration_handler.h"
20 #include "chromeos/network/network_connection_handler.h"
21 #include "chromeos/network/network_device_handler.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
24 #include "chromeos/network/onc/onc_signature.h"
25 #include "chromeos/network/onc/onc_translator.h"
26 #include "chromeos/network/shill_property_util.h"
27 #include "components/onc/onc_constants.h"
29 namespace api = extensions::api::networking_private;
31 using chromeos::DBusThreadManager;
32 using chromeos::ManagedNetworkConfigurationHandler;
33 using chromeos::NetworkHandler;
34 using chromeos::NetworkState;
35 using chromeos::NetworkStateHandler;
36 using chromeos::NetworkTypePattern;
37 using chromeos::ShillManagerClient;
41 // Helper function that converts between the two types of verification
42 // properties. They should always have the same fields, but we do this here to
43 // prevent ShillManagerClient from depending directly on the extension API.
44 ShillManagerClient::VerificationProperties ConvertVerificationProperties(
45 const api::VerificationProperties& input) {
46 ShillManagerClient::VerificationProperties output;
47 COMPILE_ASSERT(sizeof(api::VerificationProperties) ==
48 sizeof(ShillManagerClient::VerificationProperties),
49 verification_properties_no_longer_match);
51 output.certificate = input.certificate;
52 output.public_key = input.public_key;
53 output.nonce = input.nonce;
54 output.signed_data = input.signed_data;
55 output.device_serial = input.device_serial;
56 output.device_ssid = input.device_ssid;
57 output.device_bssid = input.device_bssid;
61 std::string GetUserIdHash(Profile* profile) {
62 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles)) {
63 return g_browser_process->platform_part()->
64 profile_helper()->GetUserIdHashFromProfile(profile);
66 return g_browser_process->platform_part()->
67 profile_helper()->active_user_id_hash();
73 ////////////////////////////////////////////////////////////////////////////////
74 // NetworkingPrivateGetPropertiesFunction
76 NetworkingPrivateGetPropertiesFunction::
77 ~NetworkingPrivateGetPropertiesFunction() {
80 bool NetworkingPrivateGetPropertiesFunction::RunImpl() {
81 scoped_ptr<api::GetProperties::Params> params =
82 api::GetProperties::Params::Create(*args_);
83 EXTENSION_FUNCTION_VALIDATE(params);
85 NetworkHandler::Get()->managed_network_configuration_handler()->GetProperties(
86 params->network_guid, // service path
87 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess,
89 base::Bind(&NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed,
94 void NetworkingPrivateGetPropertiesFunction::GetPropertiesSuccess(
95 const std::string& service_path,
96 const base::DictionaryValue& dictionary) {
97 base::DictionaryValue* network_properties = dictionary.DeepCopy();
98 network_properties->SetStringWithoutPathExpansion(onc::network_config::kGUID,
100 SetResult(network_properties);
104 void NetworkingPrivateGetPropertiesFunction::GetPropertiesFailed(
105 const std::string& error_name,
106 scoped_ptr<base::DictionaryValue> error_data) {
111 ////////////////////////////////////////////////////////////////////////////////
112 // NetworkingPrivateGetManagedPropertiesFunction
114 NetworkingPrivateGetManagedPropertiesFunction::
115 ~NetworkingPrivateGetManagedPropertiesFunction() {
118 bool NetworkingPrivateGetManagedPropertiesFunction::RunImpl() {
119 scoped_ptr<api::GetManagedProperties::Params> params =
120 api::GetManagedProperties::Params::Create(*args_);
121 EXTENSION_FUNCTION_VALIDATE(params);
123 std::string user_id_hash;
124 GetUserIdHash(GetProfile());
125 NetworkHandler::Get()->managed_network_configuration_handler()->
126 GetManagedProperties(
128 params->network_guid, // service path
129 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Success,
131 base::Bind(&NetworkingPrivateGetManagedPropertiesFunction::Failure,
136 void NetworkingPrivateGetManagedPropertiesFunction::Success(
137 const std::string& service_path,
138 const base::DictionaryValue& dictionary) {
139 base::DictionaryValue* network_properties = dictionary.DeepCopy();
140 network_properties->SetStringWithoutPathExpansion(onc::network_config::kGUID,
142 SetResult(network_properties);
146 void NetworkingPrivateGetManagedPropertiesFunction::Failure(
147 const std::string& error_name,
148 scoped_ptr<base::DictionaryValue> error_data) {
153 ////////////////////////////////////////////////////////////////////////////////
154 // NetworkingPrivateGetStateFunction
156 NetworkingPrivateGetStateFunction::
157 ~NetworkingPrivateGetStateFunction() {
160 bool NetworkingPrivateGetStateFunction::RunImpl() {
161 scoped_ptr<api::GetState::Params> params =
162 api::GetState::Params::Create(*args_);
163 EXTENSION_FUNCTION_VALIDATE(params);
164 // The |network_guid| parameter is storing the service path.
165 std::string service_path = params->network_guid;
167 const NetworkState* state = NetworkHandler::Get()->network_state_handler()->
168 GetNetworkState(service_path);
170 error_ = "Error.InvalidParameter";
174 scoped_ptr<base::DictionaryValue> result_dict(new base::DictionaryValue);
175 state->GetProperties(result_dict.get());
176 scoped_ptr<base::DictionaryValue> onc_network_part =
177 chromeos::onc::TranslateShillServiceToONCPart(*result_dict,
178 &chromeos::onc::kNetworkWithStateSignature);
179 SetResult(onc_network_part.release());
185 ////////////////////////////////////////////////////////////////////////////////
186 // NetworkingPrivateSetPropertiesFunction
188 NetworkingPrivateSetPropertiesFunction::
189 ~NetworkingPrivateSetPropertiesFunction() {
192 bool NetworkingPrivateSetPropertiesFunction::RunImpl() {
193 scoped_ptr<api::SetProperties::Params> params =
194 api::SetProperties::Params::Create(*args_);
195 EXTENSION_FUNCTION_VALIDATE(params);
197 scoped_ptr<base::DictionaryValue> properties_dict(
198 params->properties.ToValue());
200 NetworkHandler::Get()->managed_network_configuration_handler()->SetProperties(
201 params->network_guid, // service path
203 base::Bind(&NetworkingPrivateSetPropertiesFunction::ResultCallback,
205 base::Bind(&NetworkingPrivateSetPropertiesFunction::ErrorCallback,
210 void NetworkingPrivateSetPropertiesFunction::ErrorCallback(
211 const std::string& error_name,
212 const scoped_ptr<base::DictionaryValue> error_data) {
217 void NetworkingPrivateSetPropertiesFunction::ResultCallback() {
221 ////////////////////////////////////////////////////////////////////////////////
222 // NetworkingPrivateCreateNetworkFunction
224 NetworkingPrivateCreateNetworkFunction::
225 ~NetworkingPrivateCreateNetworkFunction() {
228 bool NetworkingPrivateCreateNetworkFunction::RunImpl() {
229 scoped_ptr<api::CreateNetwork::Params> params =
230 api::CreateNetwork::Params::Create(*args_);
231 EXTENSION_FUNCTION_VALIDATE(params);
233 std::string user_id_hash;
235 user_id_hash = GetUserIdHash(GetProfile());
237 scoped_ptr<base::DictionaryValue> properties_dict(
238 params->properties.ToValue());
240 NetworkHandler::Get()->managed_network_configuration_handler()->
244 base::Bind(&NetworkingPrivateCreateNetworkFunction::ResultCallback,
246 base::Bind(&NetworkingPrivateCreateNetworkFunction::ErrorCallback,
251 void NetworkingPrivateCreateNetworkFunction::ErrorCallback(
252 const std::string& error_name,
253 const scoped_ptr<base::DictionaryValue> error_data) {
258 void NetworkingPrivateCreateNetworkFunction::ResultCallback(
259 const std::string& guid) {
260 results_ = api::CreateNetwork::Results::Create(guid);
264 ////////////////////////////////////////////////////////////////////////////////
265 // NetworkingPrivateGetVisibleNetworksFunction
267 NetworkingPrivateGetVisibleNetworksFunction::
268 ~NetworkingPrivateGetVisibleNetworksFunction() {
271 bool NetworkingPrivateGetVisibleNetworksFunction::RunImpl() {
272 scoped_ptr<api::GetVisibleNetworks::Params> params =
273 api::GetVisibleNetworks::Params::Create(*args_);
274 EXTENSION_FUNCTION_VALIDATE(params);
275 std::string type_filter =
276 api::GetVisibleNetworks::Params::ToString(params->type);
278 NetworkStateHandler::NetworkStateList network_states;
279 NetworkHandler::Get()->network_state_handler()->GetNetworkList(
282 base::ListValue* network_properties_list = new base::ListValue;
283 for (NetworkStateHandler::NetworkStateList::iterator it =
284 network_states.begin();
285 it != network_states.end(); ++it) {
286 const std::string& service_path = (*it)->path();
287 base::DictionaryValue shill_dictionary;
288 (*it)->GetProperties(&shill_dictionary);
290 scoped_ptr<base::DictionaryValue> onc_network_part =
291 chromeos::onc::TranslateShillServiceToONCPart(shill_dictionary,
292 &chromeos::onc::kNetworkWithStateSignature);
294 std::string onc_type;
295 onc_network_part->GetStringWithoutPathExpansion(onc::network_config::kType,
297 if (type_filter == onc::network_type::kAllTypes ||
298 onc_type == type_filter) {
299 onc_network_part->SetStringWithoutPathExpansion(
300 onc::network_config::kGUID,
302 network_properties_list->Append(onc_network_part.release());
306 SetResult(network_properties_list);
311 ////////////////////////////////////////////////////////////////////////////////
312 // NetworkingPrivateGetEnabledNetworkTypesFunction
314 NetworkingPrivateGetEnabledNetworkTypesFunction::
315 ~NetworkingPrivateGetEnabledNetworkTypesFunction() {
318 bool NetworkingPrivateGetEnabledNetworkTypesFunction::RunImpl() {
319 NetworkStateHandler* state_handler =
320 NetworkHandler::Get()->network_state_handler();
322 base::ListValue* network_list = new base::ListValue;
324 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Ethernet()))
325 network_list->AppendString("Ethernet");
326 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::WiFi()))
327 network_list->AppendString("WiFi");
328 if (state_handler->IsTechnologyEnabled(NetworkTypePattern::Cellular()))
329 network_list->AppendString("Cellular");
331 SetResult(network_list);
335 ////////////////////////////////////////////////////////////////////////////////
336 // NetworkingPrivateEnableNetworkTypeFunction
338 NetworkingPrivateEnableNetworkTypeFunction::
339 ~NetworkingPrivateEnableNetworkTypeFunction() {
342 bool NetworkingPrivateEnableNetworkTypeFunction::RunImpl() {
343 scoped_ptr<api::EnableNetworkType::Params> params =
344 api::EnableNetworkType::Params::Create(*args_);
345 EXTENSION_FUNCTION_VALIDATE(params);
346 NetworkStateHandler* state_handler =
347 NetworkHandler::Get()->network_state_handler();
349 switch (params->network_type) {
350 case api::NETWORK_TYPE_ETHERNET:
351 state_handler->SetTechnologyEnabled(
352 NetworkTypePattern::Ethernet(), true,
353 chromeos::network_handler::ErrorCallback());
356 case api::NETWORK_TYPE_WIFI:
357 state_handler->SetTechnologyEnabled(
358 NetworkTypePattern::WiFi(), true,
359 chromeos::network_handler::ErrorCallback());
362 case api::NETWORK_TYPE_CELLULAR:
363 state_handler->SetTechnologyEnabled(
364 NetworkTypePattern::Cellular(), true,
365 chromeos::network_handler::ErrorCallback());
374 ////////////////////////////////////////////////////////////////////////////////
375 // NetworkingPrivateDisableNetworkTypeFunction
377 NetworkingPrivateDisableNetworkTypeFunction::
378 ~NetworkingPrivateDisableNetworkTypeFunction() {
381 bool NetworkingPrivateDisableNetworkTypeFunction::RunImpl() {
382 scoped_ptr<api::DisableNetworkType::Params> params =
383 api::DisableNetworkType::Params::Create(*args_);
384 NetworkStateHandler* state_handler =
385 NetworkHandler::Get()->network_state_handler();
387 switch (params->network_type) {
388 case api::NETWORK_TYPE_ETHERNET:
389 state_handler->SetTechnologyEnabled(
390 NetworkTypePattern::Ethernet(), false,
391 chromeos::network_handler::ErrorCallback());
394 case api::NETWORK_TYPE_WIFI:
395 state_handler->SetTechnologyEnabled(
396 NetworkTypePattern::WiFi(), false,
397 chromeos::network_handler::ErrorCallback());
400 case api::NETWORK_TYPE_CELLULAR:
401 state_handler->SetTechnologyEnabled(
402 NetworkTypePattern::Cellular(), false,
403 chromeos::network_handler::ErrorCallback());
413 ////////////////////////////////////////////////////////////////////////////////
414 // NetworkingPrivateRequestNetworkScanFunction
416 NetworkingPrivateRequestNetworkScanFunction::
417 ~NetworkingPrivateRequestNetworkScanFunction() {
420 bool NetworkingPrivateRequestNetworkScanFunction::RunImpl() {
421 NetworkHandler::Get()->network_state_handler()->RequestScan();
425 ////////////////////////////////////////////////////////////////////////////////
426 // NetworkingPrivateStartConnectFunction
428 NetworkingPrivateStartConnectFunction::
429 ~NetworkingPrivateStartConnectFunction() {
432 void NetworkingPrivateStartConnectFunction::ConnectionStartSuccess() {
436 void NetworkingPrivateStartConnectFunction::ConnectionStartFailed(
437 const std::string& error_name,
438 const scoped_ptr<base::DictionaryValue> error_data) {
443 bool NetworkingPrivateStartConnectFunction::RunImpl() {
444 scoped_ptr<api::StartConnect::Params> params =
445 api::StartConnect::Params::Create(*args_);
446 EXTENSION_FUNCTION_VALIDATE(params);
448 const bool check_error_state = false;
449 NetworkHandler::Get()->network_connection_handler()->ConnectToNetwork(
450 params->network_guid, // service path
452 &NetworkingPrivateStartConnectFunction::ConnectionStartSuccess,
455 &NetworkingPrivateStartConnectFunction::ConnectionStartFailed,
461 ////////////////////////////////////////////////////////////////////////////////
462 // NetworkingPrivateStartDisconnectFunction
464 NetworkingPrivateStartDisconnectFunction::
465 ~NetworkingPrivateStartDisconnectFunction() {
468 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess() {
472 void NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed(
473 const std::string& error_name,
474 const scoped_ptr<base::DictionaryValue> error_data) {
479 bool NetworkingPrivateStartDisconnectFunction::RunImpl() {
480 scoped_ptr<api::StartDisconnect::Params> params =
481 api::StartDisconnect::Params::Create(*args_);
482 EXTENSION_FUNCTION_VALIDATE(params);
484 NetworkHandler::Get()->network_connection_handler()->DisconnectNetwork(
485 params->network_guid, // service path
487 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartSuccess,
490 &NetworkingPrivateStartDisconnectFunction::DisconnectionStartFailed,
495 ////////////////////////////////////////////////////////////////////////////////
496 // NetworkingPrivateVerifyDestinationFunction
498 NetworkingPrivateVerifyDestinationFunction::
499 ~NetworkingPrivateVerifyDestinationFunction() {
502 bool NetworkingPrivateVerifyDestinationFunction::RunImpl() {
503 scoped_ptr<api::VerifyDestination::Params> params =
504 api::VerifyDestination::Params::Create(*args_);
505 EXTENSION_FUNCTION_VALIDATE(params);
507 ShillManagerClient::VerificationProperties verification_properties =
508 ConvertVerificationProperties(params->properties);
510 DBusThreadManager::Get()->GetShillManagerClient()->VerifyDestination(
511 verification_properties,
513 &NetworkingPrivateVerifyDestinationFunction::ResultCallback,
516 &NetworkingPrivateVerifyDestinationFunction::ErrorCallback,
521 void NetworkingPrivateVerifyDestinationFunction::ResultCallback(
523 results_ = api::VerifyDestination::Results::Create(result);
527 void NetworkingPrivateVerifyDestinationFunction::ErrorCallback(
528 const std::string& error_name, const std::string& error) {
533 ////////////////////////////////////////////////////////////////////////////////
534 // NetworkingPrivateVerifyAndEncryptCredentialsFunction
536 NetworkingPrivateVerifyAndEncryptCredentialsFunction::
537 ~NetworkingPrivateVerifyAndEncryptCredentialsFunction() {
540 bool NetworkingPrivateVerifyAndEncryptCredentialsFunction::RunImpl() {
541 scoped_ptr<api::VerifyAndEncryptCredentials::Params> params =
542 api::VerifyAndEncryptCredentials::Params::Create(*args_);
543 EXTENSION_FUNCTION_VALIDATE(params);
544 ShillManagerClient* shill_manager_client =
545 DBusThreadManager::Get()->GetShillManagerClient();
547 ShillManagerClient::VerificationProperties verification_properties =
548 ConvertVerificationProperties(params->properties);
550 shill_manager_client->VerifyAndEncryptCredentials(
551 verification_properties,
554 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::ResultCallback,
557 &NetworkingPrivateVerifyAndEncryptCredentialsFunction::ErrorCallback,
562 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::ResultCallback(
563 const std::string& result) {
564 results_ = api::VerifyAndEncryptCredentials::Results::Create(result);
568 void NetworkingPrivateVerifyAndEncryptCredentialsFunction::ErrorCallback(
569 const std::string& error_name, const std::string& error) {
574 ////////////////////////////////////////////////////////////////////////////////
575 // NetworkingPrivateVerifyAndEncryptDataFunction
577 NetworkingPrivateVerifyAndEncryptDataFunction::
578 ~NetworkingPrivateVerifyAndEncryptDataFunction() {
581 bool NetworkingPrivateVerifyAndEncryptDataFunction::RunImpl() {
582 scoped_ptr<api::VerifyAndEncryptData::Params> params =
583 api::VerifyAndEncryptData::Params::Create(*args_);
584 EXTENSION_FUNCTION_VALIDATE(params);
586 ShillManagerClient::VerificationProperties verification_properties =
587 ConvertVerificationProperties(params->properties);
589 DBusThreadManager::Get()->GetShillManagerClient()->VerifyAndEncryptData(
590 verification_properties,
593 &NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback,
596 &NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback,
601 void NetworkingPrivateVerifyAndEncryptDataFunction::ResultCallback(
602 const std::string& result) {
603 results_ = api::VerifyAndEncryptData::Results::Create(result);
607 void NetworkingPrivateVerifyAndEncryptDataFunction::ErrorCallback(
608 const std::string& error_name,
609 const std::string& error) {
614 ////////////////////////////////////////////////////////////////////////////////
615 // NetworkingPrivateSetWifiTDLSEnabledStateFunction
617 NetworkingPrivateSetWifiTDLSEnabledStateFunction::
618 ~NetworkingPrivateSetWifiTDLSEnabledStateFunction() {
621 bool NetworkingPrivateSetWifiTDLSEnabledStateFunction::RunImpl() {
622 scoped_ptr<api::SetWifiTDLSEnabledState::Params> params =
623 api::SetWifiTDLSEnabledState::Params::Create(*args_);
624 EXTENSION_FUNCTION_VALIDATE(params);
626 std::string ip_or_mac_address = params->ip_or_mac_address;
627 bool enable = params->enabled;
629 NetworkHandler::Get()->network_device_handler()->
633 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success,
635 base::Bind(&NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure,
641 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Success(
642 const std::string& result) {
643 results_ = api::SetWifiTDLSEnabledState::Results::Create(result);
647 void NetworkingPrivateSetWifiTDLSEnabledStateFunction::Failure(
648 const std::string& error_name,
649 scoped_ptr<base::DictionaryValue> error_data) {
654 ////////////////////////////////////////////////////////////////////////////////
655 // NetworkingPrivateGetWifiTDLSStatusFunction
657 NetworkingPrivateGetWifiTDLSStatusFunction::
658 ~NetworkingPrivateGetWifiTDLSStatusFunction() {
661 bool NetworkingPrivateGetWifiTDLSStatusFunction::RunImpl() {
662 scoped_ptr<api::GetWifiTDLSStatus::Params> params =
663 api::GetWifiTDLSStatus::Params::Create(*args_);
664 EXTENSION_FUNCTION_VALIDATE(params);
666 std::string ip_or_mac_address = params->ip_or_mac_address;
668 NetworkHandler::Get()->network_device_handler()->
671 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Success,
673 base::Bind(&NetworkingPrivateGetWifiTDLSStatusFunction::Failure,
679 void NetworkingPrivateGetWifiTDLSStatusFunction::Success(
680 const std::string& result) {
681 results_ = api::GetWifiTDLSStatus::Results::Create(result);
685 void NetworkingPrivateGetWifiTDLSStatusFunction::Failure(
686 const std::string& error_name,
687 scoped_ptr<base::DictionaryValue> error_data) {