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

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: Fixed unit test in Debug. 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 return l10n_util::GetStringFUTF16( 720 return l10n_util::GetStringFUTF16(
720 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING, UTF8ToUTF16(network->name())); 721 IDS_ASH_STATUS_TRAY_NETWORK_CONNECTING, UTF8ToUTF16(network->name()));
721 } 722 }
722 if (activation_state == flimflam::kActivationStateActivating) { 723 if (activation_state == flimflam::kActivationStateActivating) {
723 return l10n_util::GetStringFUTF16( 724 return l10n_util::GetStringFUTF16(
724 IDS_ASH_STATUS_TRAY_NETWORK_ACTIVATING, UTF8ToUTF16(network->name())); 725 IDS_ASH_STATUS_TRAY_NETWORK_ACTIVATING, UTF8ToUTF16(network->name()));
725 } 726 }
726 } 727 }
727 728
728 // Otherwise just show the network name or 'Ethernet'. 729 // Otherwise just show the network name or 'Ethernet'.
729 if (network->type() == flimflam::kTypeEthernet) { 730 if (network->Matches(NetworkTypePattern::Ethernet())) {
730 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ETHERNET); 731 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ETHERNET);
731 } else { 732 } else {
732 return UTF8ToUTF16(network->name()); 733 return UTF8ToUTF16(network->name());
733 } 734 }
734 } 735 }
735 736
736 int GetCellularUninitializedMsg() { 737 int GetCellularUninitializedMsg() {
737 static base::Time s_uninitialized_state_time; 738 static base::Time s_uninitialized_state_time;
738 static int s_uninitialized_msg(0); 739 static int s_uninitialized_msg(0);
739 740
740 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler(); 741 NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
741 if (handler->GetTechnologyState(NetworkStateHandler::kMatchTypeMobile) 742 if (handler->GetTechnologyState(NetworkTypePattern::Mobile())
742 == NetworkStateHandler::TECHNOLOGY_UNINITIALIZED) { 743 == NetworkStateHandler::TECHNOLOGY_UNINITIALIZED) {
743 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_INITIALIZING_CELLULAR; 744 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_INITIALIZING_CELLULAR;
744 s_uninitialized_state_time = base::Time::Now(); 745 s_uninitialized_state_time = base::Time::Now();
745 return s_uninitialized_msg; 746 return s_uninitialized_msg;
746 } else if (handler->GetScanningByType( 747 } else if (handler->GetScanningByType(NetworkTypePattern::Mobile())) {
747 NetworkStateHandler::kMatchTypeMobile)) {
748 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_CELLULAR_SCANNING; 748 s_uninitialized_msg = IDS_ASH_STATUS_TRAY_CELLULAR_SCANNING;
749 s_uninitialized_state_time = base::Time::Now(); 749 s_uninitialized_state_time = base::Time::Now();
750 return s_uninitialized_msg; 750 return s_uninitialized_msg;
751 } 751 }
752 // There can be a delay between leaving the Initializing state and when 752 // There can be a delay between leaving the Initializing state and when
753 // a Cellular device shows up, so keep showing the initializing 753 // a Cellular device shows up, so keep showing the initializing
754 // animation for a bit to avoid flashing the disconnect icon. 754 // animation for a bit to avoid flashing the disconnect icon.
755 const int kInitializingDelaySeconds = 1; 755 const int kInitializingDelaySeconds = 1;
756 base::TimeDelta dtime = base::Time::Now() - s_uninitialized_state_time; 756 base::TimeDelta dtime = base::Time::Now() - s_uninitialized_state_time;
757 if (dtime.InSeconds() < kInitializingDelaySeconds) 757 if (dtime.InSeconds() < kInitializingDelaySeconds)
758 return s_uninitialized_msg; 758 return s_uninitialized_msg;
759 return 0; 759 return 0;
760 } 760 }
761 761
762 void GetDefaultNetworkImageAndLabel(IconType icon_type, 762 void GetDefaultNetworkImageAndLabel(IconType icon_type,
763 gfx::ImageSkia* image, 763 gfx::ImageSkia* image,
764 base::string16* label, 764 base::string16* label,
765 bool* animating) { 765 bool* animating) {
766 NetworkStateHandler* state_handler = 766 NetworkStateHandler* state_handler =
767 NetworkHandler::Get()->network_state_handler(); 767 NetworkHandler::Get()->network_state_handler();
768 NetworkConnectionHandler* connect_handler = 768 NetworkConnectionHandler* connect_handler =
769 NetworkHandler::Get()->network_connection_handler(); 769 NetworkHandler::Get()->network_connection_handler();
770 const NetworkState* connected_network = 770 const NetworkState* connected_network =
771 state_handler->ConnectedNetworkByType( 771 state_handler->ConnectedNetworkByType(NetworkTypePattern::NonVirtual());
772 NetworkStateHandler::kMatchTypeNonVirtual);
773 const NetworkState* connecting_network = 772 const NetworkState* connecting_network =
774 state_handler->ConnectingNetworkByType( 773 state_handler->ConnectingNetworkByType(NetworkTypePattern::Wireless());
775 NetworkStateHandler::kMatchTypeWireless);
776 if (!connecting_network && icon_type == ICON_TYPE_TRAY) { 774 if (!connecting_network && icon_type == ICON_TYPE_TRAY) {
777 connecting_network = 775 connecting_network =
778 state_handler->ConnectingNetworkByType(flimflam::kTypeVPN); 776 state_handler->ConnectingNetworkByType(NetworkTypePattern::VPN());
779 } 777 }
780 778
781 const NetworkState* network; 779 const NetworkState* network;
782 // If we are connecting to a network, and there is either no connected 780 // If we are connecting to a network, and there is either no connected
783 // network, or the connection was user requested, use the connecting 781 // network, or the connection was user requested, use the connecting
784 // network. 782 // network.
785 if (connecting_network && 783 if (connecting_network &&
786 (!connected_network || 784 (!connected_network ||
787 connect_handler->HasConnectingNetwork(connecting_network->path()))) { 785 connect_handler->HasConnectingNetwork(connecting_network->path()))) {
788 network = connecting_network; 786 network = connecting_network;
789 } else { 787 } else {
790 network = connected_network; 788 network = connected_network;
791 } 789 }
792 790
793 // Don't show ethernet in the tray 791 // Don't show ethernet in the tray
794 if (icon_type == ICON_TYPE_TRAY && 792 if (icon_type == ICON_TYPE_TRAY && network &&
795 network && network->type() == flimflam::kTypeEthernet) { 793 network->Matches(NetworkTypePattern::Ethernet())) {
796 *image = gfx::ImageSkia(); 794 *image = gfx::ImageSkia();
797 *animating = false; 795 *animating = false;
798 return; 796 return;
799 } 797 }
800 798
801 if (!network) { 799 if (!network) {
802 // If no connecting network, check if we are activating a network. 800 // If no connecting network, check if we are activating a network.
803 const NetworkState* mobile_network = state_handler->FirstNetworkByType( 801 const NetworkState* mobile_network =
804 NetworkStateHandler::kMatchTypeMobile); 802 state_handler->FirstNetworkByType(NetworkTypePattern::Mobile());
805 if (mobile_network && (mobile_network->activation_state() == 803 if (mobile_network && (mobile_network->activation_state() ==
806 flimflam::kActivationStateActivating)) { 804 flimflam::kActivationStateActivating)) {
807 network = mobile_network; 805 network = mobile_network;
808 } 806 }
809 } 807 }
810 if (!network) { 808 if (!network) {
811 // If no connecting network, check for cellular initializing. 809 // If no connecting network, check for cellular initializing.
812 int uninitialized_msg = GetCellularUninitializedMsg(); 810 int uninitialized_msg = GetCellularUninitializedMsg();
813 if (uninitialized_msg != 0) { 811 if (uninitialized_msg != 0) {
814 *image = GetImageForConnectingNetwork(icon_type, flimflam::kTypeCellular); 812 *image = GetImageForConnectingNetwork(icon_type, flimflam::kTypeCellular);
(...skipping 26 matching lines...) Expand all
841 iter != networks.end(); ++iter) { 839 iter != networks.end(); ++iter) {
842 network_paths.insert((*iter)->path()); 840 network_paths.insert((*iter)->path());
843 } 841 }
844 PurgeIconMap(ICON_TYPE_TRAY, network_paths); 842 PurgeIconMap(ICON_TYPE_TRAY, network_paths);
845 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); 843 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths);
846 PurgeIconMap(ICON_TYPE_LIST, network_paths); 844 PurgeIconMap(ICON_TYPE_LIST, network_paths);
847 } 845 }
848 846
849 } // namespace network_icon 847 } // namespace network_icon
850 } // namespace ash 848 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/chromeos/network/network_connect.cc ('k') | ash/system/chromeos/network/network_state_list_detailed_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698