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/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
18 #include "grit/ash_strings.h"
19 #include "grit/ui_chromeos_strings.h"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/chromeos/network/network_icon.h"
23 #include "ui/chromeos/network/network_icon_animation.h"
25 using chromeos::NetworkHandler;
26 using chromeos::NetworkState;
27 using chromeos::NetworkStateHandler;
28 using chromeos::NetworkTypePattern;
33 class VpnDefaultView : public TrayItemMore,
34 public ui::network_icon::AnimationObserver {
36 VpnDefaultView(SystemTrayItem* owner, bool show_more)
37 : TrayItemMore(owner, show_more) {
41 virtual ~VpnDefaultView() {
42 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
45 static bool ShouldShow() {
46 // Do not show VPN line in uber tray bubble if VPN is not configured.
47 NetworkStateHandler* handler =
48 NetworkHandler::Get()->network_state_handler();
49 const NetworkState* vpn =
50 handler->FirstNetworkByType(NetworkTypePattern::VPN());
57 bool animating = false;
58 GetNetworkStateHandlerImageAndLabel(&image, &label, &animating);
60 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
62 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(
66 SetAccessibleName(label);
69 // ui::network_icon::AnimationObserver
70 virtual void NetworkIconChanged() OVERRIDE {
75 void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia* image,
76 base::string16* label,
78 NetworkStateHandler* handler =
79 NetworkHandler::Get()->network_state_handler();
80 const NetworkState* vpn =
81 handler->FirstNetworkByType(NetworkTypePattern::VPN());
82 if (!vpn || (vpn->connection_state() == shill::kStateIdle)) {
83 *image = ui::network_icon::GetImageForDisconnectedNetwork(
84 ui::network_icon::ICON_TYPE_DEFAULT_VIEW, shill::kTypeVPN);
86 *label = l10n_util::GetStringUTF16(
87 IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED);
92 *animating = vpn->IsConnectingState();
93 *image = ui::network_icon::GetImageForNetwork(
94 vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
96 *label = ui::network_icon::GetLabelForNetwork(
97 vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
101 DISALLOW_COPY_AND_ASSIGN(VpnDefaultView);
106 TrayVPN::TrayVPN(SystemTray* system_tray)
107 : SystemTrayItem(system_tray),
110 network_state_observer_.reset(new TrayNetworkStateObserver(this));
113 TrayVPN::~TrayVPN() {
116 views::View* TrayVPN::CreateTrayView(user::LoginStatus status) {
120 views::View* TrayVPN::CreateDefaultView(user::LoginStatus status) {
121 CHECK(default_ == NULL);
122 if (!chromeos::NetworkHandler::IsInitialized())
124 if (status == user::LOGGED_IN_NONE)
126 if (!tray::VpnDefaultView::ShouldShow())
129 bool userAddingRunning = ash::Shell::GetInstance()
130 ->session_state_delegate()
131 ->IsInSecondaryLoginScreen();
133 default_ = new tray::VpnDefaultView(
134 this, status != user::LOGGED_IN_LOCKED && !userAddingRunning);
139 views::View* TrayVPN::CreateDetailedView(user::LoginStatus status) {
140 CHECK(detailed_ == NULL);
141 if (!chromeos::NetworkHandler::IsInitialized())
144 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
145 ash::UMA_STATUS_AREA_DETAILED_VPN_VIEW);
146 detailed_ = new tray::NetworkStateListDetailedView(
147 this, tray::NetworkStateListDetailedView::LIST_TYPE_VPN, status);
152 void TrayVPN::DestroyTrayView() {
155 void TrayVPN::DestroyDefaultView() {
159 void TrayVPN::DestroyDetailedView() {
163 void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status) {
166 void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
169 void TrayVPN::NetworkStateChanged(bool list_changed) {
174 detailed_->NetworkListChanged();
176 detailed_->ManagerChanged();
180 void TrayVPN::NetworkServiceChanged(const chromeos::NetworkState* network) {
182 detailed_->NetworkServiceChanged(network);