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"
9 #include "ash/system/chromeos/network/network_icon_animation.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 "third_party/cros_system_api/dbus/service_constants.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
23 using chromeos::NetworkHandler;
24 using chromeos::NetworkState;
25 using chromeos::NetworkStateHandler;
26 using chromeos::NetworkTypePattern;
31 class VpnDefaultView : public TrayItemMore,
32 public network_icon::AnimationObserver {
34 VpnDefaultView(SystemTrayItem* owner, bool show_more)
35 : TrayItemMore(owner, show_more) {
39 virtual ~VpnDefaultView() {
40 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
43 static bool ShouldShow() {
44 // Do not show VPN line in uber tray bubble if VPN is not configured.
45 NetworkStateHandler* handler =
46 NetworkHandler::Get()->network_state_handler();
47 const NetworkState* vpn =
48 handler->FirstNetworkByType(NetworkTypePattern::VPN());
55 bool animating = false;
56 GetNetworkStateHandlerImageAndLabel(&image, &label, &animating);
58 network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
60 network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
63 SetAccessibleName(label);
66 // network_icon::AnimationObserver
67 virtual void NetworkIconChanged() OVERRIDE {
72 void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia* image,
73 base::string16* label,
75 NetworkStateHandler* handler =
76 NetworkHandler::Get()->network_state_handler();
77 const NetworkState* vpn =
78 handler->FirstNetworkByType(NetworkTypePattern::VPN());
79 if (!vpn || (vpn->connection_state() == shill::kStateIdle)) {
80 *image = network_icon::GetImageForDisconnectedNetwork(
81 network_icon::ICON_TYPE_DEFAULT_VIEW, shill::kTypeVPN);
83 *label = l10n_util::GetStringUTF16(
84 IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED);
89 *animating = vpn->IsConnectingState();
90 *image = network_icon::GetImageForNetwork(
91 vpn, network_icon::ICON_TYPE_DEFAULT_VIEW);
93 *label = network_icon::GetLabelForNetwork(
94 vpn, network_icon::ICON_TYPE_DEFAULT_VIEW);
98 DISALLOW_COPY_AND_ASSIGN(VpnDefaultView);
103 TrayVPN::TrayVPN(SystemTray* system_tray)
104 : SystemTrayItem(system_tray),
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 default_ = new tray::VpnDefaultView(this, status != user::LOGGED_IN_LOCKED);
130 views::View* TrayVPN::CreateDetailedView(user::LoginStatus status) {
131 CHECK(detailed_ == NULL);
132 if (!chromeos::NetworkHandler::IsInitialized())
135 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
136 ash::UMA_STATUS_AREA_DETAILED_VPN_VIEW);
137 detailed_ = new tray::NetworkStateListDetailedView(
138 this, tray::NetworkStateListDetailedView::LIST_TYPE_VPN, status);
143 void TrayVPN::DestroyTrayView() {
146 void TrayVPN::DestroyDefaultView() {
150 void TrayVPN::DestroyDetailedView() {
154 void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status) {
157 void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
160 void TrayVPN::NetworkStateChanged(bool list_changed) {
165 detailed_->NetworkListChanged();
167 detailed_->ManagerChanged();
171 void TrayVPN::NetworkServiceChanged(const chromeos::NetworkState* network) {
173 detailed_->NetworkServiceChanged(network);