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

Side by Side Diff: ash/system/chromeos/network/network_icon.cc

Issue 23712002: Cleanup network type matching. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit tests and rebased. Created 7 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ash/system/chromeos/network/network_icon.h" 5 #include "ash/system/chromeos/network/network_icon.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/system/chromeos/network/network_icon_animation.h" 8 #include "ash/system/chromeos/network/network_icon_animation.h"
9 #include "ash/system/chromeos/network/network_icon_animation_observer.h" 9 #include "ash/system/chromeos/network/network_icon_animation_observer.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chromeos/network/device_state.h" 11 #include "chromeos/network/device_state.h"
12 #include "chromeos/network/network_connection_handler.h" 12 #include "chromeos/network/network_connection_handler.h"
13 #include "chromeos/network/network_state.h" 13 #include "chromeos/network/network_state.h"
14 #include "chromeos/network/network_state_handler.h" 14 #include "chromeos/network/network_state_handler.h"
15 #include "chromeos/network/shill_property_util.h"
15 #include "grit/ash_resources.h" 16 #include "grit/ash_resources.h"
16 #include "grit/ash_strings.h" 17 #include "grit/ash_strings.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h" 18 #include "third_party/cros_system_api/dbus/service_constants.h"
18 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/canvas.h" 21 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/image_skia_operations.h" 22 #include "ui/gfx/image/image_skia_operations.h"
22 #include "ui/gfx/image/image_skia_source.h" 23 #include "ui/gfx/image/image_skia_source.h"
23 #include "ui/gfx/rect.h" 24 #include "ui/gfx/rect.h"
24 #include "ui/gfx/size_conversions.h" 25 #include "ui/gfx/size_conversions.h"
25 26
26 using chromeos::DeviceState; 27 using chromeos::DeviceState;
27 using chromeos::NetworkConnectionHandler; 28 using chromeos::NetworkConnectionHandler;
28 using chromeos::NetworkHandler; 29 using chromeos::NetworkHandler;
29 using chromeos::NetworkState; 30 using chromeos::NetworkState;
30 using chromeos::NetworkStateHandler; 31 using chromeos::NetworkStateHandler;
32 using chromeos::NetworkTypePattern;
31 33
32 namespace ash { 34 namespace ash {
33 namespace network_icon { 35 namespace network_icon {
34 36
35 namespace { 37 namespace {
36 38
37 //------------------------------------------------------------------------------ 39 //------------------------------------------------------------------------------
38 // Struct to pass icon badges to NetworkIconImageSource. 40 // Struct to pass icon badges to NetworkIconImageSource.
39 struct Badges { 41 struct Badges {
40 Badges() 42 Badges()
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 458
457 const gfx::ImageSkia* BadgeForVPN(IconType icon_type) { 459 const gfx::ImageSkia* BadgeForVPN(IconType icon_type) {
458 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 460 return ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
459 IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE); 461 IDR_AURA_UBER_TRAY_NETWORK_VPN_BADGE);
460 } 462 }
461 463
462 gfx::ImageSkia GetIcon(const NetworkState* network, 464 gfx::ImageSkia GetIcon(const NetworkState* network,
463 IconType icon_type, 465 IconType icon_type,
464 int strength_index) { 466 int strength_index) {
465 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 467 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
466 const std::string& type = network->type(); 468 if (network->Matches(NetworkTypePattern::Ethernet())) {
467 if (type == flimflam::kTypeEthernet) {
468 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); 469 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED);
469 } else if (type == flimflam::kTypeWifi || 470 } else if (network->Matches(NetworkTypePattern::Wireless())) {
470 type == flimflam::kTypeWimax ||
471 type == flimflam::kTypeCellular) {
472 DCHECK(strength_index > 0); 471 DCHECK(strength_index > 0);
473 return GetImageForIndex( 472 return GetImageForIndex(
474 ImageTypeForNetworkType(type), icon_type, strength_index); 473 ImageTypeForNetworkType(network->type()), icon_type, strength_index);
475 } else if (type == flimflam::kTypeVPN) { 474 } else if (network->Matches(NetworkTypePattern::VPN())) {
476 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN); 475 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_VPN);
477 } else { 476 } else {
478 LOG(WARNING) << "Request for icon for unsupported type: " << type; 477 LOG(WARNING) << "Request for icon for unsupported type: "
478 << network->type();
479 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED); 479 return *rb.GetImageSkiaNamed(IDR_AURA_UBER_TRAY_NETWORK_WIRED);
480 } 480 }
481 } 481 }
482 482
483 //------------------------------------------------------------------------------ 483 //------------------------------------------------------------------------------
484 // Get connecting images 484 // Get connecting images
485 485
486 gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) { 486 gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) {
487 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); 487 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
488 const NetworkState* connected_network = NULL; 488 const NetworkState* connected_network = NULL;
489 if (icon_type == ICON_TYPE_TRAY) { 489 if (icon_type == ICON_TYPE_TRAY) {
490 connected_network = handler->ConnectedNetworkByType( 490 connected_network =
491 NetworkStateHandler::kMatchTypeNonVirtual); 491 handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual());
492 } 492 }
493 double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); 493 double animation = NetworkIconAnimation::GetInstance()->GetAnimation();
494 494
495 if (connected_network) { 495 if (connected_network) {
496 gfx::ImageSkia icon = GetImageForNetwork(connected_network, icon_type); 496 gfx::ImageSkia icon = GetImageForNetwork(connected_network, icon_type);
497 Badges badges; 497 Badges badges;
498 badges.bottom_left = ConnectingVpnBadge(animation); 498 badges.bottom_left = ConnectingVpnBadge(animation);
499 return gfx::ImageSkia( 499 return gfx::ImageSkia(
500 new NetworkIconImageSource(icon, badges), icon.size()); 500 new NetworkIconImageSource(icon, badges), icon.size());
501 } else { 501 } else {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 DCHECK(network); 537 DCHECK(network);
538 // Determine whether or not we need to update the icon. 538 // Determine whether or not we need to update the icon.
539 bool dirty = image_.isNull(); 539 bool dirty = image_.isNull();
540 540
541 // If the network state has changed, the icon needs updating. 541 // If the network state has changed, the icon needs updating.
542 if (state_ != network->connection_state()) { 542 if (state_ != network->connection_state()) {
543 state_ = network->connection_state(); 543 state_ = network->connection_state();
544 dirty = true; 544 dirty = true;
545 } 545 }
546 546
547 const std::string& type = network->type(); 547 if (network->Matches(NetworkTypePattern::Wireless()))
548 if (type != flimflam::kTypeEthernet)
549 dirty |= UpdateWirelessStrengthIndex(network); 548 dirty |= UpdateWirelessStrengthIndex(network);
550 549
551 if (type == flimflam::kTypeCellular) 550 if (network->Matches(NetworkTypePattern::Cellular()))
552 dirty |= UpdateCellularState(network); 551 dirty |= UpdateCellularState(network);
553 552
554 if (IconTypeHasVPNBadge(icon_type_) && type != flimflam::kTypeVPN) 553 if (IconTypeHasVPNBadge(icon_type_) &&
554 network->Matches(NetworkTypePattern::NonVirtual())) {
555 dirty |= UpdateVPNBadge(); 555 dirty |= UpdateVPNBadge();
556 }
556 557
557 if (dirty) { 558 if (dirty) {
558 // Set the icon and badges based on the network and generate the image. 559 // Set the icon and badges based on the network and generate the image.
559 GenerateImage(network); 560 GenerateImage(network);
560 } 561 }
561 } 562 }
562 563
563 bool NetworkIconImpl::UpdateWirelessStrengthIndex(const NetworkState* network) { 564 bool NetworkIconImpl::UpdateWirelessStrengthIndex(const NetworkState* network) {
564 int index = GetStrengthIndex(network); 565 int index = GetStrengthIndex(network);
565 if (index != strength_index_) { 566 if (index != strength_index_) {
(...skipping 14 matching lines...) Expand all
580 std::string roaming_state = network->roaming(); 581 std::string roaming_state = network->roaming();
581 if (roaming_state != roaming_state_) { 582 if (roaming_state != roaming_state_) {
582 roaming_state_ = roaming_state; 583 roaming_state_ = roaming_state;
583 dirty = true; 584 dirty = true;
584 } 585 }
585 return dirty; 586 return dirty;
586 } 587 }
587 588
588 bool NetworkIconImpl::UpdateVPNBadge() { 589 bool NetworkIconImpl::UpdateVPNBadge() {
589 const NetworkState* vpn = NetworkHandler::Get()->network_state_handler()-> 590 const NetworkState* vpn = NetworkHandler::Get()->network_state_handler()->
590 ConnectedNetworkByType(flimflam::kTypeVPN); 591 ConnectedNetworkByType(NetworkTypePattern::VPN());
591 if (vpn && vpn_badge_ == NULL) { 592 if (vpn && vpn_badge_ == NULL) {
592 vpn_badge_ = BadgeForVPN(icon_type_); 593 vpn_badge_ = BadgeForVPN(icon_type_);
593 return true; 594 return true;
594 } else if (!vpn && vpn_badge_ != NULL) { 595 } else if (!vpn && vpn_badge_ != NULL) {
595 vpn_badge_ = NULL; 596 vpn_badge_ = NULL;
596 return true; 597 return true;
597 } 598 }
598 return false; 599 return false;
599 } 600 }
600 601
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 return l10n_util::GetStringFUTF16( 719 return l10n_util::GetStringFUTF16(
719 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING, UTF8ToUTF16(network->name())); 720 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING, UTF8ToUTF16(network->name()));
720 } 721 }
721 if (activation_state == flimflam::kActivationStateActivating) { 722 if (activation_state == flimflam::kActivationStateActivating) {
722 return l10n_util::GetStringFUTF16( 723 return l10n_util::GetStringFUTF16(
723 IDS_ASH_STATUS_TRAY_NETWORK_ACTIVATING, UTF8ToUTF16(network->name())); 724 IDS_ASH_STATUS_TRAY_NETWORK_ACTIVATING, UTF8ToUTF16(network->name()));
724 } 725 }
725 } 726 }
726 727
727 // Otherwise just show the network name or 'Ethernet'. 728 // Otherwise just show the network name or 'Ethernet'.
728 if (network->type() == flimflam::kTypeEthernet) { 729 if (network->Matches(NetworkTypePattern::Ethernet())) {
729 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ETHERNET); 730 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ETHERNET);
730 } else { 731 } else {
731 return UTF8ToUTF16(network->name()); 732 return UTF8ToUTF16(network->name());
732 } 733 }
733 } 734 }
734 735
735 int GetCellularUninitializedMsg() { 736 int GetCellularUninitializedMsg() {
736 static base::Time s_uninitialized_state_time; 737 static base::Time s_uninitialized_state_time;
737 static int s_uninitialized_msg(0); 738 static int s_uninitialized_msg(0);
738 739
739 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); 740 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
740 if (handler->GetTechnologyState(NetworkStateHandler::kMatchTypeMobile) 741 if (handler->GetTechnologyState(NetworkTypePattern::Mobile())
741 == NetworkStateHandler::TECHNOLOGY_UNINITIALIZED) { 742 == NetworkStateHandler::TECHNOLOGY_UNINITIALIZED) {
742 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_INITIALIZING_CELLULAR; 743 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_INITIALIZING_CELLULAR;
743 s_uninitialized_state_time = base::Time::Now(); 744 s_uninitialized_state_time = base::Time::Now();
744 return s_uninitialized_msg; 745 return s_uninitialized_msg;
745 } else if (handler->GetScanningByType( 746 } else if (handler->GetScanningByType(NetworkTypePattern::Mobile())) {
746 NetworkStateHandler::kMatchTypeMobile)) {
747 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_CELLULAR_SCANNING; 747 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_CELLULAR_SCANNING;
748 s_uninitialized_state_time = base::Time::Now(); 748 s_uninitialized_state_time = base::Time::Now();
749 return s_uninitialized_msg; 749 return s_uninitialized_msg;
750 } 750 }
751 // There can be a delay between leaving the Initializing state and when 751 // There can be a delay between leaving the Initializing state and when
752 // a Cellular device shows up, so keep showing the initializing 752 // a Cellular device shows up, so keep showing the initializing
753 // animation for a bit to avoid flashing the disconnect icon. 753 // animation for a bit to avoid flashing the disconnect icon.
754 const int kInitializingDelaySeconds = 1; 754 const int kInitializingDelaySeconds = 1;
755 base::TimeDelta dtime = base::Time::Now() - s_uninitialized_state_time; 755 base::TimeDelta dtime = base::Time::Now() - s_uninitialized_state_time;
756 if (dtime.InSeconds() < kInitializingDelaySeconds) 756 if (dtime.InSeconds() < kInitializingDelaySeconds)
757 return s_uninitialized_msg; 757 return s_uninitialized_msg;
758 return 0; 758 return 0;
759 } 759 }
760 760
761 void GetDefaultNetworkImageAndLabel(IconType icon_type, 761 void GetDefaultNetworkImageAndLabel(IconType icon_type,
762 gfx::ImageSkia* image, 762 gfx::ImageSkia* image,
763 base::string16* label, 763 base::string16* label,
764 bool* animating) { 764 bool* animating) {
765 NetworkStateHandler* state_handler = 765 NetworkStateHandler* state_handler =
766 NetworkHandler::Get()->network_state_handler(); 766 NetworkHandler::Get()->network_state_handler();
767 NetworkConnectionHandler* connect_handler = 767 NetworkConnectionHandler* connect_handler =
768 NetworkHandler::Get()->network_connection_handler(); 768 NetworkHandler::Get()->network_connection_handler();
769 const NetworkState* connected_network = 769 const NetworkState* connected_network =
770 state_handler->ConnectedNetworkByType( 770 state_handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual());
771 NetworkStateHandler::kMatchTypeNonVirtual);
772 const NetworkState* connecting_network = 771 const NetworkState* connecting_network =
773 state_handler->ConnectingNetworkByType( 772 state_handler->ConnectingNetworkByType(NetworkTypePattern::Wireless());
774 NetworkStateHandler::kMatchTypeWireless);
775 if (!connecting_network && icon_type == ICON_TYPE_TRAY) { 773 if (!connecting_network && icon_type == ICON_TYPE_TRAY) {
776 connecting_network = 774 connecting_network =
777 state_handler->ConnectingNetworkByType(flimflam::kTypeVPN); 775 state_handler->ConnectingNetworkByType(NetworkTypePattern::VPN());
778 } 776 }
779 777
780 const NetworkState* network; 778 const NetworkState* network;
781 // If we are connecting to a network, and there is either no connected 779 // If we are connecting to a network, and there is either no connected
782 // network, or the connection was user requested, use the connecting 780 // network, or the connection was user requested, use the connecting
783 // network. 781 // network.
784 if (connecting_network && 782 if (connecting_network &&
785 (!connected_network || 783 (!connected_network ||
786 connect_handler->HasConnectingNetwork(connecting_network->path()))) { 784 connect_handler->HasConnectingNetwork(connecting_network->path()))) {
787 network = connecting_network; 785 network = connecting_network;
788 } else { 786 } else {
789 network = connected_network; 787 network = connected_network;
790 } 788 }
791 789
792 // Don't show ethernet in the tray 790 // Don't show ethernet in the tray
793 if (icon_type == ICON_TYPE_TRAY && 791 if (icon_type == ICON_TYPE_TRAY && network &&
794 network && network->type() == flimflam::kTypeEthernet) { 792 network->Matches(NetworkTypePattern::Ethernet())) {
795 *image = gfx::ImageSkia(); 793 *image = gfx::ImageSkia();
796 *animating = false; 794 *animating = false;
797 return; 795 return;
798 } 796 }
799 797
800 if (!network) { 798 if (!network) {
801 // If no connecting network, check if we are activating a network. 799 // If no connecting network, check if we are activating a network.
802 const NetworkState* mobile_network = state_handler->FirstNetworkByType( 800 const NetworkState* mobile_network =
803 NetworkStateHandler::kMatchTypeMobile); 801 state_handler->FirstNetworkByType(NetworkTypePattern::Mobile());
804 if (mobile_network && (mobile_network->activation_state() == 802 if (mobile_network && (mobile_network->activation_state() ==
805 flimflam::kActivationStateActivating)) { 803 flimflam::kActivationStateActivating)) {
806 network = mobile_network; 804 network = mobile_network;
807 } 805 }
808 } 806 }
809 if (!network) { 807 if (!network) {
810 // If no connecting network, check for cellular initializing. 808 // If no connecting network, check for cellular initializing.
811 int uninitialized_msg = GetCellularUninitializedMsg(); 809 int uninitialized_msg = GetCellularUninitializedMsg();
812 if (uninitialized_msg != 0) { 810 if (uninitialized_msg != 0) {
813 *image = GetImageForConnectingNetwork(icon_type, flimflam::kTypeCellular); 811 *image = GetImageForConnectingNetwork(icon_type, flimflam::kTypeCellular);
(...skipping 26 matching lines...) Expand all
840 iter != networks.end(); ++iter) { 838 iter != networks.end(); ++iter) {
841 network_paths.insert((*iter)->path()); 839 network_paths.insert((*iter)->path());
842 } 840 }
843 PurgeIconMap(ICON_TYPE_TRAY, network_paths); 841 PurgeIconMap(ICON_TYPE_TRAY, network_paths);
844 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); 842 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths);
845 PurgeIconMap(ICON_TYPE_LIST, network_paths); 843 PurgeIconMap(ICON_TYPE_LIST, network_paths);
846 } 844 }
847 845
848 } // namespace network_icon 846 } // namespace network_icon
849 } // namespace ash 847 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698