Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / network / tray_vpn.cc
blob36c55b7339e78e8975babd1c6e1d4299cdcce8b0
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"
9 #include "ash/shell.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;
32 namespace ash {
33 namespace tray {
35 class VpnDefaultView : public TrayItemMore,
36 public ui::network_icon::AnimationObserver {
37 public:
38 VpnDefaultView(SystemTrayItem* owner, bool show_more)
39 : TrayItemMore(owner, show_more) {
40 Update();
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()
52 ->GetVPNDelegate()
53 ->HaveThirdPartyVPNProviders()) {
54 return true;
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()))
61 return true;
62 return false;
65 void Update() {
66 gfx::ImageSkia image;
67 base::string16 label;
68 bool animating = false;
69 GetNetworkStateHandlerImageAndLabel(&image, &label, &animating);
70 if (animating)
71 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
72 else
73 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(
74 this);
75 SetImage(&image);
76 SetLabel(label);
77 SetAccessibleName(label);
80 // ui::network_icon::AnimationObserver
81 void NetworkIconChanged() override { Update(); }
83 private:
84 void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia* image,
85 base::string16* label,
86 bool* animating) {
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);
94 if (label) {
95 *label =
96 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED);
98 *animating = false;
99 return;
101 *animating = vpn->IsConnectingState();
102 *image = ui::network_icon::GetImageForNetwork(
103 vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
104 if (label) {
105 *label = ui::network_icon::GetLabelForNetwork(
106 vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
110 DISALLOW_COPY_AND_ASSIGN(VpnDefaultView);
113 } // namespace tray
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) {
124 return NULL;
127 views::View* TrayVPN::CreateDefaultView(user::LoginStatus status) {
128 CHECK(default_ == NULL);
129 if (!chromeos::NetworkHandler::IsInitialized())
130 return NULL;
131 if (status == user::LOGGED_IN_NONE)
132 return NULL;
133 if (!tray::VpnDefaultView::ShouldShow())
134 return NULL;
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);
143 return default_;
146 views::View* TrayVPN::CreateDetailedView(user::LoginStatus status) {
147 CHECK(detailed_ == NULL);
148 if (!chromeos::NetworkHandler::IsInitialized())
149 return NULL;
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);
155 detailed_->Init();
156 return detailed_;
159 void TrayVPN::DestroyTrayView() {
162 void TrayVPN::DestroyDefaultView() {
163 default_ = NULL;
166 void TrayVPN::DestroyDetailedView() {
167 detailed_ = NULL;
170 void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status) {
173 void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
176 void TrayVPN::NetworkStateChanged() {
177 if (default_)
178 default_->Update();
179 if (detailed_)
180 detailed_->Update();
183 } // namespace ash