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/tray/system_tray.h"
12 #include "ash/system/tray/system_tray_delegate.h"
13 #include "ash/system/tray/tray_constants.h"
14 #include "ash/system/tray/tray_item_more.h"
15 #include "ash/system/tray/tray_popup_label_button.h"
16 #include "chromeos/device_event_log.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"
26 using chromeos::NetworkHandler
;
27 using chromeos::NetworkState
;
28 using chromeos::NetworkStateHandler
;
29 using chromeos::NetworkTypePattern
;
34 class VpnDefaultView
: public TrayItemMore
,
35 public ui::network_icon::AnimationObserver
{
37 VpnDefaultView(SystemTrayItem
* owner
, bool show_more
)
38 : TrayItemMore(owner
, show_more
) {
42 ~VpnDefaultView() override
{
43 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
46 static bool ShouldShow() {
47 // Do not show VPN line in uber tray bubble if VPN is not configured.
48 NetworkStateHandler
* handler
=
49 NetworkHandler::Get()->network_state_handler();
50 const NetworkState
* vpn
=
51 handler
->FirstNetworkByType(NetworkTypePattern::VPN());
58 bool animating
= false;
59 GetNetworkStateHandlerImageAndLabel(&image
, &label
, &animating
);
61 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
63 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(
67 SetAccessibleName(label
);
70 // ui::network_icon::AnimationObserver
71 void NetworkIconChanged() override
{ Update(); }
74 void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia
* image
,
75 base::string16
* label
,
77 NetworkStateHandler
* handler
=
78 NetworkHandler::Get()->network_state_handler();
79 const NetworkState
* vpn
=
80 handler
->FirstNetworkByType(NetworkTypePattern::VPN());
81 if (!vpn
|| (!vpn
->IsConnectedState() && !vpn
->IsConnectingState())) {
82 *image
= ui::network_icon::GetImageForDisconnectedNetwork(
83 ui::network_icon::ICON_TYPE_DEFAULT_VIEW
, shill::kTypeVPN
);
86 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED
);
91 *animating
= vpn
->IsConnectingState();
92 *image
= ui::network_icon::GetImageForNetwork(
93 vpn
, ui::network_icon::ICON_TYPE_DEFAULT_VIEW
);
95 *label
= ui::network_icon::GetLabelForNetwork(
96 vpn
, ui::network_icon::ICON_TYPE_DEFAULT_VIEW
);
100 DISALLOW_COPY_AND_ASSIGN(VpnDefaultView
);
105 TrayVPN::TrayVPN(SystemTray
* system_tray
)
106 : SystemTrayItem(system_tray
), default_(NULL
), detailed_(NULL
) {
107 network_state_observer_
.reset(new TrayNetworkStateObserver(this));
110 TrayVPN::~TrayVPN() {
113 views::View
* TrayVPN::CreateTrayView(user::LoginStatus status
) {
117 views::View
* TrayVPN::CreateDefaultView(user::LoginStatus status
) {
118 CHECK(default_
== NULL
);
119 if (!chromeos::NetworkHandler::IsInitialized())
121 if (status
== user::LOGGED_IN_NONE
)
123 if (!tray::VpnDefaultView::ShouldShow())
126 bool userAddingRunning
= ash::Shell::GetInstance()
127 ->session_state_delegate()
128 ->IsInSecondaryLoginScreen();
130 default_
= new tray::VpnDefaultView(
131 this, status
!= user::LOGGED_IN_LOCKED
&& !userAddingRunning
);
136 views::View
* TrayVPN::CreateDetailedView(user::LoginStatus status
) {
137 CHECK(detailed_
== NULL
);
138 if (!chromeos::NetworkHandler::IsInitialized())
141 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
142 ash::UMA_STATUS_AREA_DETAILED_VPN_VIEW
);
143 detailed_
= new tray::NetworkStateListDetailedView(
144 this, tray::NetworkStateListDetailedView::LIST_TYPE_VPN
, status
);
149 void TrayVPN::DestroyTrayView() {
152 void TrayVPN::DestroyDefaultView() {
156 void TrayVPN::DestroyDetailedView() {
160 void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status
) {
163 void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment
) {
166 void TrayVPN::NetworkStateChanged() {