Chromium Code Reviews| Index: components/arc/net/arc_net_host_impl.cc |
| diff --git a/components/arc/net/arc_net_host_impl.cc b/components/arc/net/arc_net_host_impl.cc |
| index 3ffab61ca33da84bbe185c923dcfd17459a8cc07..85592498bc163cc8a3cfb5c26318637fcd3abc5f 100644 |
| --- a/components/arc/net/arc_net_host_impl.cc |
| +++ b/components/arc/net/arc_net_host_impl.cc |
| @@ -29,10 +29,6 @@ namespace { |
| const int kGetNetworksListLimit = 100; |
| -} // namespace |
| - |
| -namespace arc { |
| - |
| chromeos::NetworkStateHandler* GetStateHandler() { |
| return chromeos::NetworkHandler::Get()->network_state_handler(); |
| } |
| @@ -53,6 +49,178 @@ bool IsDeviceOwner() { |
| chromeos::LoginState::LOGGED_IN_USER_OWNER; |
| } |
| +std::string GetStringFromOncDictionary(const base::DictionaryValue* dict, |
| + const char* key, |
| + bool required) { |
| + std::string value; |
| + dict->GetString(key, &value); |
| + if (required && value.empty()) |
| + NOTREACHED(); |
| + return value; |
| +} |
| + |
| +arc::mojom::SecurityType TranslateONCWifiSecurityType( |
| + const base::DictionaryValue* dict) { |
| + std::string type = GetStringFromOncDictionary(dict, onc::wifi::kSecurity, |
| + true /* required */); |
| + if (type == onc::wifi::kWEP_PSK) |
| + return arc::mojom::SecurityType::WEP_PSK; |
| + else if (type == onc::wifi::kWEP_8021X) |
| + return arc::mojom::SecurityType::WEP_8021X; |
| + else if (type == onc::wifi::kWPA_PSK) |
| + return arc::mojom::SecurityType::WPA_PSK; |
| + else if (type == onc::wifi::kWPA_EAP) |
| + return arc::mojom::SecurityType::WPA_EAP; |
| + else |
| + return arc::mojom::SecurityType::NONE; |
| +} |
| + |
| +arc::mojom::WiFiPtr TranslateONCWifi(const base::DictionaryValue* dict) { |
| + arc::mojom::WiFiPtr wifi = arc::mojom::WiFi::New(); |
| + |
| + // Optional; defaults to 0. |
| + dict->GetInteger(onc::wifi::kFrequency, &wifi->frequency); |
| + |
| + wifi->bssid = |
| + GetStringFromOncDictionary(dict, onc::wifi::kBSSID, false /* required */); |
| + wifi->hex_ssid = GetStringFromOncDictionary(dict, onc::wifi::kHexSSID, |
| + true /* required */); |
| + |
| + // Optional; defaults to false. |
| + dict->GetBoolean(onc::wifi::kHiddenSSID, &wifi->hidden_ssid); |
| + |
| + wifi->security = TranslateONCWifiSecurityType(dict); |
| + |
| + // Optional; defaults to 0. |
| + dict->GetInteger(onc::wifi::kSignalStrength, &wifi->signal_strength); |
| + |
| + return wifi; |
| +} |
| + |
| +mojo::Array<mojo::String> TranslateStringArray(const base::ListValue* list) { |
| + mojo::Array<mojo::String> strings = mojo::Array<mojo::String>::New(0); |
| + |
| + for (size_t i = 0; i < list->GetSize(); i++) { |
| + std::string value; |
| + list->GetString(i, &value); |
| + DCHECK(!value.empty()); |
| + strings.push_back(static_cast<mojo::String>(value)); |
| + } |
| + |
| + return strings; |
| +} |
| + |
| +mojo::Array<arc::mojom::IPConfigurationPtr> TranslateONCIPConfigs( |
| + const base::ListValue* list) { |
| + mojo::Array<arc::mojom::IPConfigurationPtr> configs = |
| + mojo::Array<arc::mojom::IPConfigurationPtr>::New(0); |
| + |
| + for (size_t i = 0; i < list->GetSize(); i++) { |
| + const base::DictionaryValue* ip_dict = nullptr; |
| + arc::mojom::IPConfigurationPtr configuration = |
| + arc::mojom::IPConfiguration::New(); |
| + |
| + list->GetDictionary(i, &ip_dict); |
| + DCHECK(ip_dict); |
| + |
| + configuration->gateway = GetStringFromOncDictionary( |
| + ip_dict, onc::ipconfig::kGateway, true /* required */); |
| + configuration->ip_address = GetStringFromOncDictionary( |
| + ip_dict, onc::ipconfig::kIPAddress, true /* required */); |
| + |
| + const base::ListValue* dns_list; |
| + if (!ip_dict->GetList(onc::ipconfig::kNameServers, &dns_list)) |
| + NOTREACHED(); |
| + configuration->name_servers = TranslateStringArray(dns_list); |
| + |
| + if (!ip_dict->GetInteger(onc::ipconfig::kRoutingPrefix, |
| + &configuration->routing_prefix)) { |
| + NOTREACHED(); |
| + } |
| + |
| + std::string type = GetStringFromOncDictionary(ip_dict, onc::ipconfig::kType, |
| + true /* required */); |
| + configuration->type = type == onc::ipconfig::kIPv6 |
| + ? arc::mojom::IPAddressType::IPV6 |
| + : arc::mojom::IPAddressType::IPV4; |
| + |
| + configuration->web_proxy_auto_discovery_url = GetStringFromOncDictionary( |
| + ip_dict, onc::ipconfig::kWebProxyAutoDiscoveryUrl, true /* required */); |
| + |
| + configs.push_back(std::move(configuration)); |
| + } |
| + return configs; |
| +} |
| + |
| +arc::mojom::ConnectionStateType TranslateONCConnectionState( |
| + const base::DictionaryValue* dict) { |
| + std::string connection_state = GetStringFromOncDictionary( |
| + dict, onc::network_config::kConnectionState, true /* required */); |
| + |
| + if (connection_state == onc::connection_state::kConnected) |
| + return arc::mojom::ConnectionStateType::CONNECTED; |
| + else if (connection_state == onc::connection_state::kConnecting) |
| + return arc::mojom::ConnectionStateType::CONNECTING; |
| + else if (connection_state == onc::connection_state::kNotConnected) |
| + return arc::mojom::ConnectionStateType::NOT_CONNECTED; |
| + |
| + NOTREACHED(); |
| + return arc::mojom::ConnectionStateType::NOT_CONNECTED; |
| +} |
| + |
| +void TranslateONCNetworkTypeDetails(const base::DictionaryValue* dict, |
| + arc::mojom::NetworkConfiguration* mojo) { |
| + std::string type = GetStringFromOncDictionary( |
| + dict, onc::network_config::kType, true /* required */); |
| + if (type == onc::network_type::kCellular) { |
| + mojo->type = arc::mojom::NetworkType::CELLULAR; |
| + } else if (type == onc::network_type::kEthernet) { |
| + mojo->type = arc::mojom::NetworkType::ETHERNET; |
| + } else if (type == onc::network_type::kVPN) { |
| + mojo->type = arc::mojom::NetworkType::VPN; |
| + } else if (type == onc::network_type::kWiFi) { |
| + mojo->type = arc::mojom::NetworkType::WIFI; |
| + |
| + const base::DictionaryValue* wifi_dict = nullptr; |
| + dict->GetDictionary(onc::network_config::kWiFi, &wifi_dict); |
| + DCHECK(wifi_dict); |
| + mojo->wifi = TranslateONCWifi(wifi_dict); |
| + } else if (type == onc::network_type::kWimax) { |
| + mojo->type = arc::mojom::NetworkType::WIMAX; |
| + } else { |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +arc::mojom::NetworkConfigurationPtr TranslateONCConfiguration( |
| + const base::DictionaryValue* dict) { |
| + arc::mojom::NetworkConfigurationPtr mojo = |
| + arc::mojom::NetworkConfiguration::New(); |
| + |
| + mojo->connection_state = TranslateONCConnectionState(dict); |
| + |
| + mojo->guid = GetStringFromOncDictionary(dict, onc::network_config::kGUID, |
| + true /* required */); |
| + |
| + const base::ListValue* ip_config_list = nullptr; |
| + if (dict->GetList(onc::network_config::kIPConfigs, &ip_config_list)) { |
| + DCHECK(ip_config_list); |
| + mojo->ip_configs = TranslateONCIPConfigs(ip_config_list); |
| + } |
| + |
| + mojo->guid = GetStringFromOncDictionary(dict, onc::network_config::kGUID, |
| + true /* required */); |
| + mojo->mac_address = GetStringFromOncDictionary( |
| + dict, onc::network_config::kMacAddress, true /* required */); |
| + TranslateONCNetworkTypeDetails(dict, mojo.get()); |
| + |
| + return mojo; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace arc { |
| + |
| ArcNetHostImpl::ArcNetHostImpl(ArcBridgeService* bridge_service) |
| : ArcService(bridge_service), binding_(this) { |
| arc_bridge_service()->AddObserver(this); |
| @@ -370,6 +538,79 @@ void ArcNetHostImpl::ScanCompleted(const chromeos::DeviceState* /*unused*/) { |
| arc_bridge_service()->net_instance()->ScanCompleted(); |
| } |
| +void GetDefaultNetworkSuccessCallback( |
| + const ArcNetHostImpl::GetDefaultNetworkCallback& callback, |
| + const std::string& service_path, |
| + const base::DictionaryValue& dictionary) { |
| + // TODO(cernekee): Figure out how to query Chrome for the default physical |
| + // service if a VPN is connected, rather than just reporting the |
| + // default logical service in both fields. |
| + callback.Run(TranslateONCConfiguration(&dictionary), |
| + TranslateONCConfiguration(&dictionary)); |
| +} |
| + |
| +void GetDefaultNetworkFailureCallback( |
| + const ArcNetHostImpl::GetDefaultNetworkCallback& callback, |
| + const std::string& error_name, |
| + std::unique_ptr<base::DictionaryValue> error_data) { |
| + LOG(ERROR) << "Failed to query default logical network"; |
| + callback.Run(nullptr, nullptr); |
| +} |
|
stevenjb
2016/05/18 23:01:17
Move these two to anon namespace.
Kevin Cernekee
2016/05/18 23:24:53
Done.
|
| + |
| +void ArcNetHostImpl::GetDefaultNetwork( |
| + const GetDefaultNetworkCallback& callback) { |
| + const chromeos::NetworkState* default_network = |
| + GetStateHandler()->DefaultNetwork(); |
| + if (!default_network) { |
| + VLOG(1) << "GetDefaultNetwork: no default network"; |
| + callback.Run(nullptr, nullptr); |
| + return; |
| + } |
| + VLOG(1) << "GetDefaultNetwork: default network is " |
| + << default_network->path(); |
| + std::string user_id_hash = chromeos::LoginState::Get()->primary_user_hash(); |
| + GetManagedConfigurationHandler()->GetProperties( |
| + user_id_hash, default_network->path(), |
| + base::Bind(&GetDefaultNetworkSuccessCallback, callback), |
| + base::Bind(&GetDefaultNetworkFailureCallback, callback)); |
| +} |
| + |
| +void DefaultNetworkSuccessCallback(ArcNetHostImpl* instance, |
| + const std::string& service_path, |
| + const base::DictionaryValue& dictionary) { |
| + instance->arc_bridge_service()->net_instance()->DefaultNetworkChanged( |
| + TranslateONCConfiguration(&dictionary), |
| + TranslateONCConfiguration(&dictionary)); |
| +} |
|
stevenjb
2016/05/18 23:01:17
See comment below.
Kevin Cernekee
2016/05/18 23:24:53
Done.
|
| + |
| +void DefaultNetworkFailureCallback( |
| + const std::string& error_name, |
| + std::unique_ptr<base::DictionaryValue> error_data) { |
| + LOG(ERROR) << "Failed to query default logical network"; |
| +} |
| + |
|
stevenjb
2016/05/18 23:01:17
Move this to anon namespace.
Kevin Cernekee
2016/05/18 23:24:53
Done.
|
| +void ArcNetHostImpl::DefaultNetworkChanged( |
| + const chromeos::NetworkState* network) { |
| + if (arc_bridge_service()->net_version() < 2) { |
| + VLOG(1) << "ArcBridgeService does not support DefaultNetworkChanged."; |
| + return; |
| + } |
| + |
| + if (!network) { |
| + VLOG(1) << "No default network"; |
| + arc_bridge_service()->net_instance()->DefaultNetworkChanged(nullptr, |
| + nullptr); |
| + return; |
| + } |
| + |
| + VLOG(1) << "New default network: " << network->path(); |
| + std::string user_id_hash = chromeos::LoginState::Get()->primary_user_hash(); |
| + GetManagedConfigurationHandler()->GetProperties( |
| + user_id_hash, network->path(), |
| + base::Bind(&DefaultNetworkSuccessCallback, base::Unretained(this)), |
|
dcheng
2016/05/18 22:31:57
Why is Unretained safe here?
stevenjb
2016/05/18 23:01:17
+1, I missed this (I was mostly focused on the Net
Kevin Cernekee
2016/05/18 23:24:53
Done.
|
| + base::Bind(&DefaultNetworkFailureCallback)); |
| +} |
| + |
| void ArcNetHostImpl::OnShuttingDown() { |
| GetStateHandler()->RemoveObserver(this, FROM_HERE); |
| } |