ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / ash / system / chromeos / network / tray_vpn.cc
blobd6c71bf84f773b19941d79abef1ebe64b0c972db
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/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;
31 namespace ash {
32 namespace tray {
34 class VpnDefaultView : public TrayItemMore,
35 public ui::network_icon::AnimationObserver {
36 public:
37 VpnDefaultView(SystemTrayItem* owner, bool show_more)
38 : TrayItemMore(owner, show_more) {
39 Update();
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());
52 return vpn != NULL;
55 void Update() {
56 gfx::ImageSkia image;
57 base::string16 label;
58 bool animating = false;
59 GetNetworkStateHandlerImageAndLabel(&image, &label, &animating);
60 if (animating)
61 ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
62 else
63 ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(
64 this);
65 SetImage(&image);
66 SetLabel(label);
67 SetAccessibleName(label);
70 // ui::network_icon::AnimationObserver
71 void NetworkIconChanged() override { Update(); }
73 private:
74 void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia* image,
75 base::string16* label,
76 bool* animating) {
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);
84 if (label) {
85 *label =
86 l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED);
88 *animating = false;
89 return;
91 *animating = vpn->IsConnectingState();
92 *image = ui::network_icon::GetImageForNetwork(
93 vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
94 if (label) {
95 *label = ui::network_icon::GetLabelForNetwork(
96 vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
100 DISALLOW_COPY_AND_ASSIGN(VpnDefaultView);
103 } // namespace tray
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) {
114 return NULL;
117 views::View* TrayVPN::CreateDefaultView(user::LoginStatus status) {
118 CHECK(default_ == NULL);
119 if (!chromeos::NetworkHandler::IsInitialized())
120 return NULL;
121 if (status == user::LOGGED_IN_NONE)
122 return NULL;
123 if (!tray::VpnDefaultView::ShouldShow())
124 return NULL;
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);
133 return default_;
136 views::View* TrayVPN::CreateDetailedView(user::LoginStatus status) {
137 CHECK(detailed_ == NULL);
138 if (!chromeos::NetworkHandler::IsInitialized())
139 return NULL;
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);
145 detailed_->Init();
146 return detailed_;
149 void TrayVPN::DestroyTrayView() {
152 void TrayVPN::DestroyDefaultView() {
153 default_ = NULL;
156 void TrayVPN::DestroyDetailedView() {
157 detailed_ = NULL;
160 void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status) {
163 void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
166 void TrayVPN::NetworkStateChanged() {
167 if (default_)
168 default_->Update();
169 if (detailed_)
170 detailed_->Update();
173 } // namespace ash