Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/networking_private/networking_private_chromeos. h" | 5 #include "extensions/browser/api/networking_private/networking_private_chromeos. 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 "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/shill_manager_client.h" | 14 #include "chromeos/dbus/shill_manager_client.h" |
| 14 #include "chromeos/login/login_state.h" | 15 #include "chromeos/login/login_state.h" |
| 15 #include "chromeos/network/device_state.h" | 16 #include "chromeos/network/device_state.h" |
| 16 #include "chromeos/network/managed_network_configuration_handler.h" | 17 #include "chromeos/network/managed_network_configuration_handler.h" |
| 17 #include "chromeos/network/network_activation_handler.h" | 18 #include "chromeos/network/network_activation_handler.h" |
| 18 #include "chromeos/network/network_connection_handler.h" | 19 #include "chromeos/network/network_connection_handler.h" |
| 19 #include "chromeos/network/network_device_handler.h" | 20 #include "chromeos/network/network_device_handler.h" |
| 20 #include "chromeos/network/network_event_log.h" | 21 #include "chromeos/network/network_event_log.h" |
| 21 #include "chromeos/network/network_state.h" | 22 #include "chromeos/network/network_state.h" |
| 22 #include "chromeos/network/network_state_handler.h" | 23 #include "chromeos/network/network_state_handler.h" |
| 23 #include "chromeos/network/network_util.h" | 24 #include "chromeos/network/network_util.h" |
| 24 #include "chromeos/network/onc/onc_signature.h" | 25 #include "chromeos/network/onc/onc_signature.h" |
| 25 #include "chromeos/network/onc/onc_translator.h" | 26 #include "chromeos/network/onc/onc_translator.h" |
| 26 #include "chromeos/network/onc/onc_utils.h" | 27 #include "chromeos/network/onc/onc_utils.h" |
| 27 #include "chromeos/network/portal_detector/network_portal_detector.h" | 28 #include "chromeos/network/portal_detector/network_portal_detector.h" |
| 29 #include "chromeos/network/proxy/ui_proxy_config.h" | |
| 30 #include "chromeos/network/proxy/ui_proxy_config_service.h" | |
| 28 #include "components/onc/onc_constants.h" | 31 #include "components/onc/onc_constants.h" |
| 32 #include "components/proxy_config/proxy_prefs.h" | |
| 29 #include "content/public/browser/browser_context.h" | 33 #include "content/public/browser/browser_context.h" |
| 30 #include "extensions/browser/api/networking_private/networking_private_api.h" | 34 #include "extensions/browser/api/networking_private/networking_private_api.h" |
| 31 #include "extensions/browser/extension_registry.h" | 35 #include "extensions/browser/extension_registry.h" |
| 32 #include "extensions/browser/extensions_browser_client.h" | 36 #include "extensions/browser/extensions_browser_client.h" |
| 33 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
| 34 #include "extensions/common/extension_set.h" | 38 #include "extensions/common/extension_set.h" |
| 35 #include "extensions/common/permissions/api_permission.h" | 39 #include "extensions/common/permissions/api_permission.h" |
| 36 #include "extensions/common/permissions/permissions_data.h" | 40 #include "extensions/common/permissions/permissions_data.h" |
| 37 #include "third_party/cros_system_api/dbus/service_constants.h" | 41 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 38 | 42 |
| 39 using chromeos::NetworkHandler; | 43 using chromeos::NetworkHandler; |
| 40 using chromeos::NetworkStateHandler; | 44 using chromeos::NetworkStateHandler; |
| 41 using chromeos::NetworkTypePattern; | 45 using chromeos::NetworkTypePattern; |
| 42 using chromeos::ShillManagerClient; | 46 using chromeos::ShillManagerClient; |
| 47 using chromeos::UIProxyConfig; | |
| 43 using extensions::NetworkingPrivateDelegate; | 48 using extensions::NetworkingPrivateDelegate; |
| 44 | 49 |
| 45 namespace private_api = extensions::api::networking_private; | 50 namespace private_api = extensions::api::networking_private; |
| 46 | 51 |
| 47 namespace { | 52 namespace { |
| 48 | 53 |
| 49 chromeos::NetworkStateHandler* GetStateHandler() { | 54 chromeos::NetworkStateHandler* GetStateHandler() { |
| 50 return NetworkHandler::Get()->network_state_handler(); | 55 return NetworkHandler::Get()->network_state_handler(); |
| 51 } | 56 } |
| 52 | 57 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 device_state = | 212 device_state = |
| 208 GetStateHandler()->GetDeviceState(network_state->device_path()); | 213 GetStateHandler()->GetDeviceState(network_state->device_path()); |
| 209 } | 214 } |
| 210 if (!device_state) { | 215 if (!device_state) { |
| 211 device_state = | 216 device_state = |
| 212 GetStateHandler()->GetDeviceStateByType(NetworkTypePattern::Cellular()); | 217 GetStateHandler()->GetDeviceStateByType(NetworkTypePattern::Cellular()); |
| 213 } | 218 } |
| 214 return device_state; | 219 return device_state; |
| 215 } | 220 } |
| 216 | 221 |
| 222 base::DictionaryValue* EnsureDictionaryValue(const std::string& key, | |
|
michaelpg
2016/11/02 23:55:25
comment, please: ensure dictionary is what? set_ac
stevenjb
2016/11/03 17:48:56
Added comment and split up function.
| |
| 223 base::DictionaryValue* container, | |
| 224 bool set_active) { | |
| 225 base::DictionaryValue* dict; | |
| 226 if (!container->GetDictionary(key, &dict)) { | |
| 227 container->SetWithoutPathExpansion( | |
| 228 key, base::MakeUnique<base::DictionaryValue>()); | |
| 229 container->GetDictionary(key, &dict); | |
| 230 } | |
| 231 if (set_active) { | |
| 232 dict->SetBooleanWithoutPathExpansion(::onc::kAugmentationUserEditable, | |
| 233 false); | |
| 234 dict->SetStringWithoutPathExpansion(::onc::kAugmentationEffectiveSetting, | |
| 235 ::onc::kAugmentationActiveExtension); | |
| 236 } | |
| 237 return dict; | |
| 238 } | |
| 239 | |
| 240 std::string GetProxySettingsType(const UIProxyConfig::Mode& mode) { | |
| 241 switch (mode) { | |
| 242 case UIProxyConfig::MODE_DIRECT: | |
| 243 return ::onc::proxy::kDirect; | |
| 244 case UIProxyConfig::MODE_AUTO_DETECT: | |
| 245 return ::onc::proxy::kWPAD; | |
| 246 case UIProxyConfig::MODE_PAC_SCRIPT: | |
| 247 return ::onc::proxy::kPAC; | |
| 248 case UIProxyConfig::MODE_SINGLE_PROXY: | |
| 249 case UIProxyConfig::MODE_PROXY_PER_SCHEME: | |
| 250 return ::onc::proxy::kManual; | |
| 251 } | |
| 252 NOTREACHED(); | |
| 253 return ::onc::proxy::kDirect; | |
| 254 } | |
| 255 | |
| 256 void SetManualProxy(base::DictionaryValue* manual, | |
| 257 const std::string& key, | |
| 258 const UIProxyConfig::ManualProxy& proxy) { | |
| 259 manual->SetStringWithoutPathExpansion(::onc::proxy::kHost, | |
| 260 proxy.server.host_port_pair().host()); | |
| 261 uint16_t port = proxy.server.host_port_pair().port(); | |
| 262 manual->SetIntegerWithoutPathExpansion(::onc::proxy::kHost, | |
| 263 static_cast<int>(port)); | |
| 264 } | |
| 265 | |
| 217 } // namespace | 266 } // namespace |
| 218 | 267 |
| 219 //////////////////////////////////////////////////////////////////////////////// | 268 //////////////////////////////////////////////////////////////////////////////// |
| 220 | 269 |
| 221 namespace extensions { | 270 namespace extensions { |
| 222 | 271 |
| 223 NetworkingPrivateChromeOS::NetworkingPrivateChromeOS( | 272 NetworkingPrivateChromeOS::NetworkingPrivateChromeOS( |
| 224 content::BrowserContext* browser_context, | 273 content::BrowserContext* browser_context, |
| 225 std::unique_ptr<VerifyDelegate> verify_delegate) | 274 std::unique_ptr<VerifyDelegate> verify_delegate) |
| 226 : NetworkingPrivateDelegate(std::move(verify_delegate)), | 275 : NetworkingPrivateDelegate(std::move(verify_delegate)), |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 242 | 291 |
| 243 std::string user_id_hash; | 292 std::string user_id_hash; |
| 244 if (!GetPrimaryUserIdHash(browser_context_, &user_id_hash, &error)) { | 293 if (!GetPrimaryUserIdHash(browser_context_, &user_id_hash, &error)) { |
| 245 failure_callback.Run(error); | 294 failure_callback.Run(error); |
| 246 return; | 295 return; |
| 247 } | 296 } |
| 248 | 297 |
| 249 GetManagedConfigurationHandler()->GetProperties( | 298 GetManagedConfigurationHandler()->GetProperties( |
| 250 user_id_hash, service_path, | 299 user_id_hash, service_path, |
| 251 base::Bind(&NetworkingPrivateChromeOS::GetPropertiesCallback, | 300 base::Bind(&NetworkingPrivateChromeOS::GetPropertiesCallback, |
| 252 weak_ptr_factory_.GetWeakPtr(), success_callback), | 301 weak_ptr_factory_.GetWeakPtr(), guid, false /* managed */, |
| 302 success_callback), | |
| 253 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | 303 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); |
| 254 } | 304 } |
| 255 | 305 |
| 256 void NetworkingPrivateChromeOS::GetManagedProperties( | 306 void NetworkingPrivateChromeOS::GetManagedProperties( |
| 257 const std::string& guid, | 307 const std::string& guid, |
| 258 const DictionaryCallback& success_callback, | 308 const DictionaryCallback& success_callback, |
| 259 const FailureCallback& failure_callback) { | 309 const FailureCallback& failure_callback) { |
| 260 std::string service_path, error; | 310 std::string service_path, error; |
| 261 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | 311 if (!GetServicePathFromGuid(guid, &service_path, &error)) { |
| 262 failure_callback.Run(error); | 312 failure_callback.Run(error); |
| 263 return; | 313 return; |
| 264 } | 314 } |
| 265 | 315 |
| 266 std::string user_id_hash; | 316 std::string user_id_hash; |
| 267 if (!GetPrimaryUserIdHash(browser_context_, &user_id_hash, &error)) { | 317 if (!GetPrimaryUserIdHash(browser_context_, &user_id_hash, &error)) { |
| 268 failure_callback.Run(error); | 318 failure_callback.Run(error); |
| 269 return; | 319 return; |
| 270 } | 320 } |
| 271 | 321 |
| 272 GetManagedConfigurationHandler()->GetManagedProperties( | 322 GetManagedConfigurationHandler()->GetManagedProperties( |
| 273 user_id_hash, service_path, | 323 user_id_hash, service_path, |
| 274 base::Bind(&NetworkingPrivateChromeOS::GetPropertiesCallback, | 324 base::Bind(&NetworkingPrivateChromeOS::GetPropertiesCallback, |
| 275 weak_ptr_factory_.GetWeakPtr(), success_callback), | 325 weak_ptr_factory_.GetWeakPtr(), guid, true /* managed */, |
| 326 success_callback), | |
| 276 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); | 327 base::Bind(&NetworkHandlerFailureCallback, failure_callback)); |
| 277 } | 328 } |
| 278 | 329 |
| 279 void NetworkingPrivateChromeOS::GetState( | 330 void NetworkingPrivateChromeOS::GetState( |
| 280 const std::string& guid, | 331 const std::string& guid, |
| 281 const DictionaryCallback& success_callback, | 332 const DictionaryCallback& success_callback, |
| 282 const FailureCallback& failure_callback) { | 333 const FailureCallback& failure_callback) { |
| 283 std::string service_path, error; | 334 std::string service_path, error; |
| 284 if (!GetServicePathFromGuid(guid, &service_path, &error)) { | 335 if (!GetServicePathFromGuid(guid, &service_path, &error)) { |
| 285 failure_callback.Run(error); | 336 failure_callback.Run(error); |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 618 } | 669 } |
| 619 | 670 |
| 620 bool NetworkingPrivateChromeOS::RequestScan() { | 671 bool NetworkingPrivateChromeOS::RequestScan() { |
| 621 GetStateHandler()->RequestScan(); | 672 GetStateHandler()->RequestScan(); |
| 622 return true; | 673 return true; |
| 623 } | 674 } |
| 624 | 675 |
| 625 // Private methods | 676 // Private methods |
| 626 | 677 |
| 627 void NetworkingPrivateChromeOS::GetPropertiesCallback( | 678 void NetworkingPrivateChromeOS::GetPropertiesCallback( |
| 679 const std::string& guid, | |
| 680 bool managed, | |
| 628 const DictionaryCallback& callback, | 681 const DictionaryCallback& callback, |
| 629 const std::string& service_path, | 682 const std::string& service_path, |
| 630 const base::DictionaryValue& dictionary) { | 683 const base::DictionaryValue& dictionary) { |
| 631 std::unique_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy()); | 684 std::unique_ptr<base::DictionaryValue> dictionary_copy(dictionary.DeepCopy()); |
| 632 AppendThirdPartyProviderName(dictionary_copy.get()); | 685 AppendThirdPartyProviderName(dictionary_copy.get()); |
| 686 if (managed) | |
| 687 SetActiveProxyValues(guid, dictionary_copy.get()); | |
| 633 callback.Run(std::move(dictionary_copy)); | 688 callback.Run(std::move(dictionary_copy)); |
| 634 } | 689 } |
| 635 | 690 |
| 636 // Populate ThirdPartyVPN.kProviderName for third-party VPNs. | |
| 637 void NetworkingPrivateChromeOS::AppendThirdPartyProviderName( | 691 void NetworkingPrivateChromeOS::AppendThirdPartyProviderName( |
| 638 base::DictionaryValue* dictionary) { | 692 base::DictionaryValue* dictionary) { |
| 639 base::DictionaryValue* third_party_vpn = | 693 base::DictionaryValue* third_party_vpn = |
| 640 GetThirdPartyVPNDictionary(dictionary); | 694 GetThirdPartyVPNDictionary(dictionary); |
| 641 if (!third_party_vpn) | 695 if (!third_party_vpn) |
| 642 return; | 696 return; |
| 643 | 697 |
| 644 const std::string extension_id = GetStringFromDictionary( | 698 const std::string extension_id = GetStringFromDictionary( |
| 645 *third_party_vpn, ::onc::third_party_vpn::kExtensionID); | 699 *third_party_vpn, ::onc::third_party_vpn::kExtensionID); |
| 646 const ExtensionSet& extensions = | 700 const ExtensionSet& extensions = |
| 647 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); | 701 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); |
| 648 for (const auto& extension : extensions) { | 702 for (const auto& extension : extensions) { |
| 649 if (extension->permissions_data()->HasAPIPermission( | 703 if (extension->permissions_data()->HasAPIPermission( |
| 650 APIPermission::kVpnProvider) && | 704 APIPermission::kVpnProvider) && |
| 651 extension->id() == extension_id) { | 705 extension->id() == extension_id) { |
| 652 third_party_vpn->SetStringWithoutPathExpansion( | 706 third_party_vpn->SetStringWithoutPathExpansion( |
| 653 ::onc::third_party_vpn::kProviderName, extension->name()); | 707 ::onc::third_party_vpn::kProviderName, extension->name()); |
| 654 break; | 708 break; |
| 655 } | 709 } |
| 656 } | 710 } |
| 657 } | 711 } |
| 658 | 712 |
| 713 void NetworkingPrivateChromeOS::SetActiveProxyValues( | |
| 714 const std::string& guid, | |
| 715 base::DictionaryValue* dictionary) { | |
| 716 chromeos::UIProxyConfigService* ui_proxy_config_service = | |
| 717 NetworkHandler::Get()->ui_proxy_config_service(); | |
| 718 ui_proxy_config_service->UpdateFromPrefs(guid); | |
| 719 UIProxyConfig config; | |
| 720 ui_proxy_config_service->GetProxyConfig(guid, &config); | |
| 721 if (config.state != ProxyPrefs::CONFIG_EXTENSION) | |
| 722 return; | |
| 723 // Ensure that the ProxySettings dictionary exists. | |
|
michaelpg
2016/11/02 23:55:25
nit: line break above
stevenjb
2016/11/03 17:48:56
Done.
| |
| 724 base::DictionaryValue* proxy_settings = EnsureDictionaryValue( | |
| 725 ::onc::network_config::kProxySettings, dictionary, false); | |
| 726 | |
| 727 // Ensure that the ProxySettings.Type dictionary exists. | |
| 728 base::DictionaryValue* proxy_type_dict = | |
| 729 EnsureDictionaryValue(::onc::network_config::kType, proxy_settings, true); | |
| 730 proxy_type_dict->SetStringWithoutPathExpansion( | |
| 731 ::onc::kAugmentationActiveSetting, GetProxySettingsType(config.mode)); | |
| 732 if (config.mode == UIProxyConfig::MODE_SINGLE_PROXY) { | |
|
michaelpg
2016/11/02 23:55:25
nit: may be more readable as a switch. I don't nor
michaelpg
2016/11/02 23:55:25
nit: line break above
stevenjb
2016/11/03 17:48:56
There are a couple of modes we don't care about so
| |
| 733 base::DictionaryValue* manual = | |
| 734 EnsureDictionaryValue(::onc::proxy::kManual, proxy_settings, false); | |
| 735 SetManualProxy(manual, ::onc::proxy::kHttp, config.single_proxy); | |
| 736 manual->RemoveWithoutPathExpansion(::onc::proxy::kHttps, nullptr); | |
| 737 manual->RemoveWithoutPathExpansion(::onc::proxy::kFtp, nullptr); | |
| 738 manual->RemoveWithoutPathExpansion(::onc::proxy::kSocks, nullptr); | |
| 739 } else if (config.mode == UIProxyConfig::MODE_PROXY_PER_SCHEME) { | |
| 740 base::DictionaryValue* manual = | |
| 741 EnsureDictionaryValue(::onc::proxy::kManual, proxy_settings, false); | |
| 742 SetManualProxy(manual, ::onc::proxy::kHttp, config.http_proxy); | |
| 743 SetManualProxy(manual, ::onc::proxy::kHttps, config.https_proxy); | |
| 744 SetManualProxy(manual, ::onc::proxy::kFtp, config.ftp_proxy); | |
| 745 SetManualProxy(manual, ::onc::proxy::kSocks, config.socks_proxy); | |
| 746 } else if (config.mode == UIProxyConfig::MODE_PAC_SCRIPT) { | |
| 747 base::DictionaryValue* pac = | |
| 748 EnsureDictionaryValue(::onc::proxy::kPAC, proxy_settings, true); | |
| 749 pac->SetStringWithoutPathExpansion(::onc::kAugmentationActiveSetting, | |
| 750 config.automatic_proxy.pac_url.spec()); | |
| 751 } | |
| 752 } | |
| 753 | |
| 659 void NetworkingPrivateChromeOS::ConnectFailureCallback( | 754 void NetworkingPrivateChromeOS::ConnectFailureCallback( |
| 660 const std::string& guid, | 755 const std::string& guid, |
| 661 const VoidCallback& success_callback, | 756 const VoidCallback& success_callback, |
| 662 const FailureCallback& failure_callback, | 757 const FailureCallback& failure_callback, |
| 663 const std::string& error_name, | 758 const std::string& error_name, |
| 664 std::unique_ptr<base::DictionaryValue> error_data) { | 759 std::unique_ptr<base::DictionaryValue> error_data) { |
| 665 // TODO(stevenjb): Temporary workaround to show the configuration UI. | 760 // TODO(stevenjb): Temporary workaround to show the configuration UI. |
| 666 // Eventually the caller (e.g. Settings) should handle any failures and | 761 // Eventually the caller (e.g. Settings) should handle any failures and |
| 667 // show its own configuration UI. crbug.com/380937. | 762 // show its own configuration UI. crbug.com/380937. |
| 668 if (ui_delegate()->HandleConnectFailed(guid, error_name)) { | 763 if (ui_delegate()->HandleConnectFailed(guid, error_name)) { |
| 669 success_callback.Run(); | 764 success_callback.Run(); |
| 670 return; | 765 return; |
| 671 } | 766 } |
| 672 failure_callback.Run(error_name); | 767 failure_callback.Run(error_name); |
| 673 } | 768 } |
| 674 | 769 |
| 675 } // namespace extensions | 770 } // namespace extensions |
| OLD | NEW |