OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/arc/net/arc_net_host_impl.h" | 5 #include "components/arc/net/arc_net_host_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 | 359 |
360 void ArcNetHostImpl::ScanCompleted(const chromeos::DeviceState* /*unused*/) { | 360 void ArcNetHostImpl::ScanCompleted(const chromeos::DeviceState* /*unused*/) { |
361 if (arc_bridge_service()->net_version() < 1) { | 361 if (arc_bridge_service()->net_version() < 1) { |
362 VLOG(1) << "ArcBridgeService does not support ScanCompleted."; | 362 VLOG(1) << "ArcBridgeService does not support ScanCompleted."; |
363 return; | 363 return; |
364 } | 364 } |
365 | 365 |
366 arc_bridge_service()->net_instance()->ScanCompleted(); | 366 arc_bridge_service()->net_instance()->ScanCompleted(); |
367 } | 367 } |
368 | 368 |
| 369 mojom::WiFiPtr TranslateONCWifi(const base::DictionaryValue* dict) { |
| 370 mojom::WiFiPtr mojo = mojom::WiFi::New(); |
| 371 std::string tmp; |
| 372 |
| 373 if (!dict->GetInteger(onc::wifi::kFrequency, &mojo->frequency)) |
| 374 NOTREACHED(); |
| 375 |
| 376 if (!dict->GetString(onc::wifi::kBSSID, &tmp)) |
| 377 NOTREACHED(); |
| 378 DCHECK(!tmp.empty()); |
| 379 mojo->bssid = tmp; |
| 380 |
| 381 tmp.clear(); |
| 382 if (!dict->GetString(onc::wifi::kHexSSID, &tmp)) |
| 383 NOTREACHED(); |
| 384 DCHECK(!tmp.empty()); |
| 385 mojo->hex_ssid = tmp; |
| 386 |
| 387 if (!dict->GetBoolean(onc::wifi::kHiddenSSID, &mojo->hidden_ssid)) |
| 388 NOTREACHED(); |
| 389 |
| 390 tmp.clear(); |
| 391 if (!dict->GetString(onc::wifi::kSecurity, &tmp)) |
| 392 NOTREACHED(); |
| 393 DCHECK(!tmp.empty()); |
| 394 if (tmp == onc::wifi::kWEP_PSK) |
| 395 mojo->security = mojom::SecurityType::WEP_PSK; |
| 396 else if (tmp == onc::wifi::kWEP_8021X) |
| 397 mojo->security = mojom::SecurityType::WEP_8021X; |
| 398 else if (tmp == onc::wifi::kWPA_PSK) |
| 399 mojo->security = mojom::SecurityType::WPA_PSK; |
| 400 else if (tmp == onc::wifi::kWPA_EAP) |
| 401 mojo->security = mojom::SecurityType::WPA_EAP; |
| 402 else |
| 403 NOTREACHED(); |
| 404 |
| 405 if (!dict->GetInteger(onc::wifi::kSignalStrength, &mojo->signal_strength)) |
| 406 NOTREACHED(); |
| 407 |
| 408 return mojo; |
| 409 } |
| 410 |
| 411 mojo::Array<mojo::String> TranslateStringArray(const base::ListValue* list) { |
| 412 mojo::Array<mojo::String> mojos = mojo::Array<mojo::String>::New(0); |
| 413 |
| 414 for (size_t i = 0; i < list->GetSize(); i++) { |
| 415 std::string tmp; |
| 416 if (!list->GetString(i, &tmp)) |
| 417 NOTREACHED(); |
| 418 DCHECK(!tmp.empty()); |
| 419 |
| 420 mojo::String element; |
| 421 element = tmp; |
| 422 mojos.push_back(element); |
| 423 } |
| 424 |
| 425 return mojos; |
| 426 } |
| 427 |
| 428 mojo::Array<mojom::IPConfigurationPtr> TranslateONCIPConfigs( |
| 429 const base::ListValue* list) { |
| 430 mojo::Array<mojom::IPConfigurationPtr> mojos = |
| 431 mojo::Array<mojom::IPConfigurationPtr>::New(0); |
| 432 |
| 433 for (size_t i = 0; i < list->GetSize(); i++) { |
| 434 const base::DictionaryValue* ip_dict = nullptr; |
| 435 mojom::IPConfigurationPtr mojo = mojom::IPConfiguration::New(); |
| 436 |
| 437 list->GetDictionary(i, &ip_dict); |
| 438 DCHECK(ip_dict); |
| 439 |
| 440 std::string tmp; |
| 441 if (!ip_dict->GetString(onc::ipconfig::kGateway, &tmp)) |
| 442 NOTREACHED(); |
| 443 DCHECK(!tmp.empty()); |
| 444 mojo->gateway = tmp; |
| 445 |
| 446 tmp.clear(); |
| 447 if (!ip_dict->GetString(onc::ipconfig::kIPAddress, &tmp)) |
| 448 NOTREACHED(); |
| 449 DCHECK(!tmp.empty()); |
| 450 mojo->ip_address = tmp; |
| 451 |
| 452 const base::ListValue* dns_list; |
| 453 if (ip_dict->GetList(onc::ipconfig::kNameServers, &dns_list)) |
| 454 mojo->name_servers = TranslateStringArray(dns_list); |
| 455 else |
| 456 NOTREACHED(); |
| 457 |
| 458 if (!ip_dict->GetInteger(onc::ipconfig::kRoutingPrefix, |
| 459 &mojo->routing_prefix)) |
| 460 NOTREACHED(); |
| 461 |
| 462 tmp.clear(); |
| 463 if (!ip_dict->GetString(onc::ipconfig::kType, &tmp)) |
| 464 NOTREACHED(); |
| 465 DCHECK(!tmp.empty()); |
| 466 if (tmp == onc::ipconfig::kIPv4) |
| 467 mojo->type = mojom::IPAddressType::IPV4; |
| 468 else if (tmp == onc::ipconfig::kIPv6) |
| 469 mojo->type = mojom::IPAddressType::IPV6; |
| 470 else |
| 471 NOTREACHED(); |
| 472 |
| 473 tmp.clear(); |
| 474 if (!ip_dict->GetString(onc::ipconfig::kWebProxyAutoDiscoveryUrl, &tmp)) |
| 475 NOTREACHED(); |
| 476 DCHECK(!tmp.empty()); |
| 477 mojo->web_proxy_auto_discovery_url = tmp; |
| 478 |
| 479 mojos.push_back(std::move(mojo)); |
| 480 } |
| 481 return mojos; |
| 482 } |
| 483 |
| 484 mojom::NetworkConfigurationPtr TranslateONCConfiguration( |
| 485 const base::DictionaryValue* dict) { |
| 486 mojom::NetworkConfigurationPtr mojo = mojom::NetworkConfiguration::New(); |
| 487 std::string tmp; |
| 488 |
| 489 if (!dict->GetString(onc::network_config::kConnectionState, &tmp)) |
| 490 NOTREACHED(); |
| 491 DCHECK(!tmp.empty()); |
| 492 if (tmp == onc::connection_state::kConnected) |
| 493 mojo->connection_state = mojom::ConnectionStateType::CONNECTED; |
| 494 else if (tmp == onc::connection_state::kConnecting) |
| 495 mojo->connection_state = mojom::ConnectionStateType::CONNECTING; |
| 496 else if (tmp == onc::connection_state::kNotConnected) |
| 497 mojo->connection_state = mojom::ConnectionStateType::NOT_CONNECTED; |
| 498 else |
| 499 NOTREACHED(); |
| 500 |
| 501 tmp.clear(); |
| 502 if (!dict->GetString(onc::network_config::kGUID, &tmp)) |
| 503 NOTREACHED(); |
| 504 DCHECK(!tmp.empty()); |
| 505 mojo->guid = tmp; |
| 506 |
| 507 const base::ListValue* ip_config_list = nullptr; |
| 508 if (dict->GetList(onc::network_config::kIPConfigs, &ip_config_list)) { |
| 509 DCHECK(ip_config_list); |
| 510 mojo->ip_configs = TranslateONCIPConfigs(ip_config_list); |
| 511 } |
| 512 |
| 513 tmp.clear(); |
| 514 if (dict->GetString(onc::network_config::kMacAddress, &tmp)) { |
| 515 DCHECK(!tmp.empty()); |
| 516 mojo->mac_address = tmp; |
| 517 } |
| 518 |
| 519 tmp.clear(); |
| 520 if (!dict->GetString(onc::network_config::kType, &tmp)) |
| 521 NOTREACHED(); |
| 522 DCHECK(!tmp.empty()); |
| 523 if (tmp == onc::network_type::kCellular) { |
| 524 mojo->type = mojom::NetworkType::CELLULAR; |
| 525 } else if (tmp == onc::network_type::kEthernet) { |
| 526 mojo->type = mojom::NetworkType::ETHERNET; |
| 527 } else if (tmp == onc::network_type::kVPN) { |
| 528 mojo->type = mojom::NetworkType::VPN; |
| 529 } else if (tmp == onc::network_type::kWiFi) { |
| 530 mojo->type = mojom::NetworkType::WIFI; |
| 531 |
| 532 const base::DictionaryValue* wifi_dict = nullptr; |
| 533 dict->GetDictionary(onc::network_config::kWiFi, &wifi_dict); |
| 534 DCHECK(wifi_dict); |
| 535 mojo->wifi = TranslateONCWifi(wifi_dict); |
| 536 } else if (tmp == onc::network_type::kWimax) { |
| 537 mojo->type = mojom::NetworkType::WIMAX; |
| 538 } |
| 539 |
| 540 return mojo; |
| 541 } |
| 542 |
| 543 void GetDefaultNetworkSuccessCallback( |
| 544 const ArcNetHostImpl::GetDefaultNetworkCallback& callback, |
| 545 const std::string& service_path, |
| 546 const base::DictionaryValue& dictionary) { |
| 547 // TODO(cernekee): Figure out how to query Chrome for the default physical |
| 548 // service if a VPN is connected, rather than just reporting the |
| 549 // default logical service in both fields. |
| 550 callback.Run(TranslateONCConfiguration(&dictionary), |
| 551 TranslateONCConfiguration(&dictionary)); |
| 552 } |
| 553 |
| 554 void GetDefaultNetworkFailureCallback( |
| 555 const ArcNetHostImpl::GetDefaultNetworkCallback& callback, |
| 556 const std::string& error_name, |
| 557 std::unique_ptr<base::DictionaryValue> error_data) { |
| 558 LOG(ERROR) << "Failed to query default logical network"; |
| 559 callback.Run(nullptr, nullptr); |
| 560 } |
| 561 |
| 562 void ArcNetHostImpl::GetDefaultNetwork( |
| 563 const GetDefaultNetworkCallback& callback) { |
| 564 std::string user_id_hash = chromeos::LoginState::Get()->primary_user_hash(); |
| 565 GetManagedConfigurationHandler()->GetProperties( |
| 566 user_id_hash, default_network_, |
| 567 base::Bind(&GetDefaultNetworkSuccessCallback, callback), |
| 568 base::Bind(&GetDefaultNetworkFailureCallback, callback)); |
| 569 } |
| 570 |
| 571 void DefaultNetworkSuccessCallback(ArcNetHostImpl* instance, |
| 572 const std::string& service_path, |
| 573 const base::DictionaryValue& dictionary) { |
| 574 instance->arc_bridge_service()->net_instance()->DefaultNetworkChanged( |
| 575 TranslateONCConfiguration(&dictionary), |
| 576 TranslateONCConfiguration(&dictionary)); |
| 577 } |
| 578 |
| 579 void DefaultNetworkFailureCallback( |
| 580 const std::string& error_name, |
| 581 std::unique_ptr<base::DictionaryValue> error_data) { |
| 582 LOG(ERROR) << "Failed to query default logical network"; |
| 583 } |
| 584 |
| 585 void ArcNetHostImpl::DefaultNetworkChanged( |
| 586 const chromeos::NetworkState* network) { |
| 587 if (!network) { |
| 588 default_network_.clear(); |
| 589 VLOG(1) << "No default network"; |
| 590 } else { |
| 591 default_network_ = network->path(); |
| 592 VLOG(1) << "New default network: " << default_network_; |
| 593 } |
| 594 |
| 595 if (arc_bridge_service()->net_version() < 2) { |
| 596 VLOG(1) << "ArcBridgeService does not support DefaultNetworkChanged."; |
| 597 return; |
| 598 } |
| 599 |
| 600 if (default_network_.empty()) { |
| 601 arc_bridge_service()->net_instance()->DefaultNetworkChanged(nullptr, |
| 602 nullptr); |
| 603 } else { |
| 604 std::string user_id_hash = chromeos::LoginState::Get()->primary_user_hash(); |
| 605 GetManagedConfigurationHandler()->GetProperties( |
| 606 user_id_hash, default_network_, |
| 607 base::Bind(&DefaultNetworkSuccessCallback, base::Unretained(this)), |
| 608 base::Bind(&DefaultNetworkFailureCallback)); |
| 609 } |
| 610 } |
| 611 |
369 void ArcNetHostImpl::OnShuttingDown() { | 612 void ArcNetHostImpl::OnShuttingDown() { |
370 GetStateHandler()->RemoveObserver(this, FROM_HERE); | 613 GetStateHandler()->RemoveObserver(this, FROM_HERE); |
371 } | 614 } |
372 | 615 |
373 } // namespace arc | 616 } // namespace arc |
OLD | NEW |