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
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/network/tray_vpn.h"
7 #include "ash/metrics/user_metrics_recorder.h"
8 #include "ash/session/session_state_delegate.h"
10 #include "ash/system/chromeos/network/network_state_list_detailed_view.h"
11 #include "ash/system/chromeos/network/vpn_delegate.h"
12 #include "ash/system/tray/system_tray.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/system/tray/tray_constants.h"
15 #include "ash/system/tray/tray_item_more.h"
16 #include "ash/system/tray/tray_popup_label_button.h"
17 #include "chromeos/network/network_state.h"
18 #include "chromeos/network/network_state_handler.h"
19 #include "grit/ash_strings.h"
20 #include "grit/ui_chromeos_strings.h"
21 #include "third_party/cros_system_api/dbus/service_constants.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/chromeos/network/network_icon.h"
24 #include "ui/chromeos/network/network_icon_animation.h"
25 #include "ui/chromeos/network/network_icon_animation_observer.h"
27 using chromeos::NetworkHandler
;
28 using chromeos::NetworkState
;
29 using chromeos::NetworkStateHandler
;
30 using chromeos::NetworkTypePattern
;
35 class VpnDefaultView
: public TrayItemMore
,
36 public ui::network_icon::AnimationObserver
{
38 VpnDefaultView(SystemTrayItem
* owner
, bool show_more
)
39 : TrayItemMore(owner
, show_more
) {
43 ~VpnDefaultView() override
{
44 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
47 static bool ShouldShow() {
48 // Show the VPN entry in the ash tray bubble if at least one third-party VPN
49 // provider is installed.
50 if (Shell::GetInstance()
51 ->system_tray_delegate()
53 ->HaveThirdPartyVPNProviders()) {
57 // Also show the VPN entry if at least one VPN network is configured.
58 NetworkStateHandler
* const handler
=
59 NetworkHandler::Get()->network_state_handler();
60 if (handler
->FirstNetworkByType(NetworkTypePattern::VPN()))
68 bool animating
= false;
69 GetNetworkStateHandlerImageAndLabel(&image
, &label
, &animating
);
71 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
73 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(
77 SetAccessibleName(label
);
80 // ui::network_icon::AnimationObserver
81 void NetworkIconChanged() override
{ Update(); }
84 void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia
* image
,
85 base::string16
* label
,
87 NetworkStateHandler
* handler
=
88 NetworkHandler::Get()->network_state_handler();
89 const NetworkState
* vpn
=
90 handler
->FirstNetworkByType(NetworkTypePattern::VPN());
91 if (!vpn
|| (!vpn
->IsConnectedState() && !vpn
->IsConnectingState())) {
92 *image
= ui::network_icon::GetImageForDisconnectedNetwork(
93 ui::network_icon::ICON_TYPE_DEFAULT_VIEW
, shill::kTypeVPN
);
96 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED
);
101 *animating
= vpn
->IsConnectingState();
102 *image
= ui::network_icon::GetImageForNetwork(
103 vpn
, ui::network_icon::ICON_TYPE_DEFAULT_VIEW
);
105 *label
= ui::network_icon::GetLabelForNetwork(
106 vpn
, ui::network_icon::ICON_TYPE_DEFAULT_VIEW
);
110 DISALLOW_COPY_AND_ASSIGN(VpnDefaultView
);
115 TrayVPN::TrayVPN(SystemTray
* system_tray
)
116 : SystemTrayItem(system_tray
), default_(NULL
), detailed_(NULL
) {
117 network_state_observer_
.reset(new TrayNetworkStateObserver(this));
120 TrayVPN::~TrayVPN() {
123 views::View
* TrayVPN::CreateTrayView(user::LoginStatus status
) {
127 views::View
* TrayVPN::CreateDefaultView(user::LoginStatus status
) {
128 CHECK(default_
== NULL
);
129 if (!chromeos::NetworkHandler::IsInitialized())
131 if (status
== user::LOGGED_IN_NONE
)
133 if (!tray::VpnDefaultView::ShouldShow())
136 bool userAddingRunning
= ash::Shell::GetInstance()
137 ->session_state_delegate()
138 ->IsInSecondaryLoginScreen();
140 default_
= new tray::VpnDefaultView(
141 this, status
!= user::LOGGED_IN_LOCKED
&& !userAddingRunning
);
146 views::View
* TrayVPN::CreateDetailedView(user::LoginStatus status
) {
147 CHECK(detailed_
== NULL
);
148 if (!chromeos::NetworkHandler::IsInitialized())
151 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
152 ash::UMA_STATUS_AREA_DETAILED_VPN_VIEW
);
153 detailed_
= new tray::NetworkStateListDetailedView(
154 this, tray::NetworkStateListDetailedView::LIST_TYPE_VPN
, status
);
159 void TrayVPN::DestroyTrayView() {
162 void TrayVPN::DestroyDefaultView() {
166 void TrayVPN::DestroyDetailedView() {
170 void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
173 void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment
) {
176 void TrayVPN::NetworkStateChanged() {