vpn_feature_pod_controller.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/network/vpn_feature_pod_controller.h"
  5. #include "ash/resources/vector_icons/vector_icons.h"
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/strings/grit/ash_strings.h"
  9. #include "ash/system/model/system_tray_model.h"
  10. #include "ash/system/network/network_icon.h"
  11. #include "ash/system/network/tray_network_state_model.h"
  12. #include "ash/system/network/vpn_list.h"
  13. #include "ash/system/tray/tray_constants.h"
  14. #include "ash/system/unified/feature_pod_button.h"
  15. #include "ash/system/unified/unified_system_tray_controller.h"
  16. #include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
  17. #include "ui/base/l10n/l10n_util.h"
  18. #include "ui/gfx/paint_vector_icon.h"
  19. using chromeos::network_config::mojom::ConnectionStateType;
  20. using chromeos::network_config::mojom::NetworkStateProperties;
  21. namespace ash {
  22. namespace {
  23. bool IsVPNVisibleInSystemTray() {
  24. TrayNetworkStateModel* model =
  25. Shell::Get()->system_tray_model()->network_state_model();
  26. // Show the VPN entry in the ash tray bubble if at least one third-party VPN
  27. // provider is installed.
  28. if (model->vpn_list()->HaveExtensionOrArcVpnProviders())
  29. return true;
  30. // Note: At this point, only built-in VPNs are considered.
  31. return !model->IsBuiltinVpnProhibited() && model->has_vpn();
  32. }
  33. } // namespace
  34. VPNFeaturePodController::VPNFeaturePodController(
  35. UnifiedSystemTrayController* tray_controller)
  36. : tray_controller_(tray_controller) {
  37. Shell::Get()->system_tray_model()->network_state_model()->AddObserver(this);
  38. }
  39. VPNFeaturePodController::~VPNFeaturePodController() {
  40. Shell::Get()->system_tray_model()->network_state_model()->RemoveObserver(
  41. this);
  42. }
  43. FeaturePodButton* VPNFeaturePodController::CreateButton() {
  44. DCHECK(!button_);
  45. button_ = new FeaturePodButton(this);
  46. button_->SetVectorIcon(kUnifiedMenuVpnIcon);
  47. button_->SetLabel(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_SHORT));
  48. button_->SetIconAndLabelTooltips(
  49. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_TOOLTIP));
  50. button_->ShowDetailedViewArrow();
  51. button_->DisableLabelButtonFocus();
  52. Update();
  53. return button_;
  54. }
  55. void VPNFeaturePodController::OnIconPressed() {
  56. tray_controller_->ShowVPNDetailedView();
  57. }
  58. SystemTrayItemUmaType VPNFeaturePodController::GetUmaType() const {
  59. return SystemTrayItemUmaType::UMA_VPN;
  60. }
  61. void VPNFeaturePodController::ActiveNetworkStateChanged() {
  62. Update();
  63. }
  64. void VPNFeaturePodController::Update() {
  65. button_->SetVisible(IsVPNVisibleInSystemTray());
  66. if (!button_->GetVisible())
  67. return;
  68. const NetworkStateProperties* vpn =
  69. Shell::Get()->system_tray_model()->network_state_model()->active_vpn();
  70. bool is_active =
  71. vpn && vpn->connection_state != ConnectionStateType::kNotConnected;
  72. button_->SetSubLabel(l10n_util::GetStringUTF16(
  73. is_active ? IDS_ASH_STATUS_TRAY_VPN_CONNECTED_SHORT
  74. : IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED_SHORT));
  75. button_->SetToggled(is_active);
  76. }
  77. } // namespace ash