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

Side by Side Diff: components/arc/net/arc_net_host_impl.cc

Issue 1925083003: Notify ARC of default network changes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@wifi-change
Patch Set: incorporate code review feedback Created 4 years, 7 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
OLDNEW
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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 return; 363 return;
364 } 364 }
365 if (arc_bridge_service()->net_version() < 1) { 365 if (arc_bridge_service()->net_version() < 1) {
366 VLOG(1) << "NetInstance does not support ScanCompleted."; 366 VLOG(1) << "NetInstance does not support ScanCompleted.";
367 return; 367 return;
368 } 368 }
369 369
370 arc_bridge_service()->net_instance()->ScanCompleted(); 370 arc_bridge_service()->net_instance()->ScanCompleted();
371 } 371 }
372 372
373 mojom::WiFiPtr TranslateONCWifi(const base::DictionaryValue* dict) {
374 mojom::WiFiPtr mojo = mojom::WiFi::New();
375 std::string tmp;
376
377 if (!dict->GetInteger(onc::wifi::kFrequency, &mojo->frequency))
378 NOTREACHED();
stevenjb 2016/05/16 19:29:35 Frequency is not a required ONC property, we shoul
Kevin Cernekee 2016/05/17 01:56:27 Done.
379
380 if (!dict->GetString(onc::wifi::kBSSID, &tmp))
381 NOTREACHED();
stevenjb 2016/05/16 19:29:35 Ditto.
Kevin Cernekee 2016/05/17 01:56:27 Done.
382 DCHECK(!tmp.empty());
383 mojo->bssid = tmp;
384
385 tmp.clear();
386 if (!dict->GetString(onc::wifi::kHexSSID, &tmp))
387 NOTREACHED();
388 DCHECK(!tmp.empty());
389 mojo->hex_ssid = tmp;
stevenjb 2016/05/16 19:29:35 This pattern is used a lot. I think the code in th
Kevin Cernekee 2016/05/17 01:56:27 Done.
390
391 if (!dict->GetBoolean(onc::wifi::kHiddenSSID, &mojo->hidden_ssid))
392 NOTREACHED();
stevenjb 2016/05/16 19:29:35 Optional, defaults to false.
Kevin Cernekee 2016/05/17 01:56:27 Done.
393
394 tmp.clear();
395 if (!dict->GetString(onc::wifi::kSecurity, &tmp))
396 NOTREACHED();
397 DCHECK(!tmp.empty());
398 if (tmp == onc::wifi::kWEP_PSK)
399 mojo->security = mojom::SecurityType::WEP_PSK;
400 else if (tmp == onc::wifi::kWEP_8021X)
401 mojo->security = mojom::SecurityType::WEP_8021X;
402 else if (tmp == onc::wifi::kWPA_PSK)
403 mojo->security = mojom::SecurityType::WPA_PSK;
404 else if (tmp == onc::wifi::kWPA_EAP)
405 mojo->security = mojom::SecurityType::WPA_EAP;
406 else
407 NOTREACHED();
stevenjb 2016/05/16 19:29:35 nit: Move this conversion to a helper function.
Kevin Cernekee 2016/05/17 01:56:27 Done.
408
409 if (!dict->GetInteger(onc::wifi::kSignalStrength, &mojo->signal_strength))
410 NOTREACHED();
stevenjb 2016/05/16 19:29:35 Optional, defaults to 0
Kevin Cernekee 2016/05/17 01:56:27 Done.
411
412 return mojo;
413 }
414
415 mojo::Array<mojo::String> TranslateStringArray(const base::ListValue* list) {
416 mojo::Array<mojo::String> mojos = mojo::Array<mojo::String>::New(0);
417
418 for (size_t i = 0; i < list->GetSize(); i++) {
419 std::string tmp;
420 if (!list->GetString(i, &tmp))
421 NOTREACHED();
422 DCHECK(!tmp.empty());
423
424 mojo::String element;
425 element = tmp;
426 mojos.push_back(element);
427 }
428
429 return mojos;
430 }
stevenjb 2016/05/16 19:29:35 This seems like a generally useful utility functio
Kevin Cernekee 2016/05/17 01:56:27 Acknowledged.
431
432 mojo::Array<mojom::IPConfigurationPtr> TranslateONCIPConfigs(
433 const base::ListValue* list) {
434 mojo::Array<mojom::IPConfigurationPtr> mojos =
435 mojo::Array<mojom::IPConfigurationPtr>::New(0);
436
437 for (size_t i = 0; i < list->GetSize(); i++) {
438 const base::DictionaryValue* ip_dict = nullptr;
439 mojom::IPConfigurationPtr mojo = mojom::IPConfiguration::New();
440
441 list->GetDictionary(i, &ip_dict);
442 DCHECK(ip_dict);
443
444 std::string tmp;
445 if (!ip_dict->GetString(onc::ipconfig::kGateway, &tmp))
446 NOTREACHED();
447 DCHECK(!tmp.empty());
448 mojo->gateway = tmp;
449
450 tmp.clear();
451 if (!ip_dict->GetString(onc::ipconfig::kIPAddress, &tmp))
452 NOTREACHED();
453 DCHECK(!tmp.empty());
454 mojo->ip_address = tmp;
455
456 const base::ListValue* dns_list;
457 if (ip_dict->GetList(onc::ipconfig::kNameServers, &dns_list))
458 mojo->name_servers = TranslateStringArray(dns_list);
459 else
460 NOTREACHED();
stevenjb 2016/05/16 19:29:35 This should be written: if (!...GetList...) NOTR
Kevin Cernekee 2016/05/17 01:56:27 Done.
461
462 if (!ip_dict->GetInteger(onc::ipconfig::kRoutingPrefix,
463 &mojo->routing_prefix))
464 NOTREACHED();
stevenjb 2016/05/16 19:29:35 {}
Kevin Cernekee 2016/05/17 01:56:27 Done.
465
466 tmp.clear();
467 if (!ip_dict->GetString(onc::ipconfig::kType, &tmp))
468 NOTREACHED();
469 DCHECK(!tmp.empty());
470 if (tmp == onc::ipconfig::kIPv4)
471 mojo->type = mojom::IPAddressType::IPV4;
472 else if (tmp == onc::ipconfig::kIPv6)
473 mojo->type = mojom::IPAddressType::IPV6;
474 else
475 NOTREACHED();
stevenjb 2016/05/16 19:29:35 More simply: mojo->type = tmp == onc::ipconfig::kI
Kevin Cernekee 2016/05/17 01:56:26 Done.
476
477 tmp.clear();
478 if (!ip_dict->GetString(onc::ipconfig::kWebProxyAutoDiscoveryUrl, &tmp))
479 NOTREACHED();
480 DCHECK(!tmp.empty());
481 mojo->web_proxy_auto_discovery_url = tmp;
482
483 mojos.push_back(std::move(mojo));
484 }
485 return mojos;
486 }
487
488 mojom::NetworkConfigurationPtr TranslateONCConfiguration(
489 const base::DictionaryValue* dict) {
490 mojom::NetworkConfigurationPtr mojo = mojom::NetworkConfiguration::New();
491 std::string tmp;
492
493 if (!dict->GetString(onc::network_config::kConnectionState, &tmp))
494 NOTREACHED();
495 DCHECK(!tmp.empty());
496 if (tmp == onc::connection_state::kConnected)
497 mojo->connection_state = mojom::ConnectionStateType::CONNECTED;
498 else if (tmp == onc::connection_state::kConnecting)
499 mojo->connection_state = mojom::ConnectionStateType::CONNECTING;
500 else if (tmp == onc::connection_state::kNotConnected)
501 mojo->connection_state = mojom::ConnectionStateType::NOT_CONNECTED;
502 else
503 NOTREACHED();
stevenjb 2016/05/16 19:29:35 nit: helper function
Kevin Cernekee 2016/05/17 01:56:27 Done.
504
505 tmp.clear();
506 if (!dict->GetString(onc::network_config::kGUID, &tmp))
507 NOTREACHED();
508 DCHECK(!tmp.empty());
509 mojo->guid = tmp;
510
511 const base::ListValue* ip_config_list = nullptr;
512 if (dict->GetList(onc::network_config::kIPConfigs, &ip_config_list)) {
513 DCHECK(ip_config_list);
514 mojo->ip_configs = TranslateONCIPConfigs(ip_config_list);
515 }
516
517 tmp.clear();
518 if (dict->GetString(onc::network_config::kMacAddress, &tmp)) {
519 DCHECK(!tmp.empty());
520 mojo->mac_address = tmp;
521 }
522
523 tmp.clear();
524 if (!dict->GetString(onc::network_config::kType, &tmp))
525 NOTREACHED();
526 DCHECK(!tmp.empty());
527 if (tmp == onc::network_type::kCellular) {
528 mojo->type = mojom::NetworkType::CELLULAR;
529 } else if (tmp == onc::network_type::kEthernet) {
530 mojo->type = mojom::NetworkType::ETHERNET;
531 } else if (tmp == onc::network_type::kVPN) {
532 mojo->type = mojom::NetworkType::VPN;
533 } else if (tmp == onc::network_type::kWiFi) {
534 mojo->type = mojom::NetworkType::WIFI;
535
536 const base::DictionaryValue* wifi_dict = nullptr;
537 dict->GetDictionary(onc::network_config::kWiFi, &wifi_dict);
538 DCHECK(wifi_dict);
539 mojo->wifi = TranslateONCWifi(wifi_dict);
540 } else if (tmp == onc::network_type::kWimax) {
541 mojo->type = mojom::NetworkType::WIMAX;
542 }
543
544 return mojo;
545 }
546
547 void GetDefaultNetworkSuccessCallback(
548 const ArcNetHostImpl::GetDefaultNetworkCallback& callback,
549 const std::string& service_path,
550 const base::DictionaryValue& dictionary) {
551 // TODO(cernekee): Figure out how to query Chrome for the default physical
552 // service if a VPN is connected, rather than just reporting the
553 // default logical service in both fields.
stevenjb 2016/05/16 19:29:35 string type; if (dictionary->GetString(kType, &typ
Kevin Cernekee 2016/05/17 01:56:27 Ack, will handle this in a separate CL.
554 callback.Run(TranslateONCConfiguration(&dictionary),
555 TranslateONCConfiguration(&dictionary));
556 }
557
558 void GetDefaultNetworkFailureCallback(
559 const ArcNetHostImpl::GetDefaultNetworkCallback& callback,
560 const std::string& error_name,
561 std::unique_ptr<base::DictionaryValue> error_data) {
562 LOG(ERROR) << "Failed to query default logical network";
563 callback.Run(nullptr, nullptr);
564 }
565
566 void ArcNetHostImpl::GetDefaultNetwork(
567 const GetDefaultNetworkCallback& callback) {
568 std::string user_id_hash = chromeos::LoginState::Get()->primary_user_hash();
569 GetManagedConfigurationHandler()->GetProperties(
570 user_id_hash, default_network_,
571 base::Bind(&GetDefaultNetworkSuccessCallback, callback),
572 base::Bind(&GetDefaultNetworkFailureCallback, callback));
573 }
574
575 void DefaultNetworkSuccessCallback(ArcNetHostImpl* instance,
576 const std::string& service_path,
577 const base::DictionaryValue& dictionary) {
578 instance->arc_bridge_service()->net_instance()->DefaultNetworkChanged(
579 TranslateONCConfiguration(&dictionary),
580 TranslateONCConfiguration(&dictionary));
581 }
582
583 void DefaultNetworkFailureCallback(
584 const std::string& error_name,
585 std::unique_ptr<base::DictionaryValue> error_data) {
586 LOG(ERROR) << "Failed to query default logical network";
587 }
588
589 void ArcNetHostImpl::DefaultNetworkChanged(
590 const chromeos::NetworkState* network) {
591 if (!network) {
592 default_network_.clear();
593 VLOG(1) << "No default network";
594 } else {
595 default_network_ = network->path();
596 VLOG(1) << "New default network: " << default_network_;
597 }
598
599 if (arc_bridge_service()->net_version() < 2) {
600 VLOG(1) << "ArcBridgeService does not support DefaultNetworkChanged.";
601 return;
602 }
603
604 if (default_network_.empty()) {
stevenjb 2016/05/16 19:29:35 Not possible. (You can add a DCHECK to document th
Kevin Cernekee 2016/05/17 01:56:26 We hit this case when network is NULL (i.e. all in
stevenjb 2016/05/17 18:02:41 I see. I would restructure this function: if (!ne
605 arc_bridge_service()->net_instance()->DefaultNetworkChanged(nullptr,
606 nullptr);
607 } else {
608 std::string user_id_hash = chromeos::LoginState::Get()->primary_user_hash();
609 GetManagedConfigurationHandler()->GetProperties(
610 user_id_hash, default_network_,
611 base::Bind(&DefaultNetworkSuccessCallback, base::Unretained(this)),
stevenjb 2016/05/16 19:29:35 You should just bind default_network here instead
Kevin Cernekee 2016/05/17 01:56:26 We set it as a member because we usually get a Def
stevenjb 2016/05/17 18:02:41 As noted: I think it is better to let NetworkState
612 base::Bind(&DefaultNetworkFailureCallback));
613 }
614 }
615
373 void ArcNetHostImpl::OnShuttingDown() { 616 void ArcNetHostImpl::OnShuttingDown() {
374 GetStateHandler()->RemoveObserver(this, FROM_HERE); 617 GetStateHandler()->RemoveObserver(this, FROM_HERE);
375 } 618 }
376 619
377 } // namespace arc 620 } // namespace arc
OLDNEW
« components/arc/net/arc_net_host_impl.h ('K') | « components/arc/net/arc_net_host_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698