tray_vpn.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.
4
5#include "ash/system/chromeos/network/tray_vpn.h"
6
7#include "ash/metrics/user_metrics_recorder.h"
8#include "ash/shell.h"
9#include "ash/system/chromeos/network/network_state_list_detailed_view.h"
10#include "ash/system/tray/system_tray.h"
11#include "ash/system/tray/system_tray_delegate.h"
12#include "ash/system/tray/tray_constants.h"
13#include "ash/system/tray/tray_item_more.h"
14#include "ash/system/tray/tray_popup_label_button.h"
15#include "chromeos/network/network_state.h"
16#include "chromeos/network/network_state_handler.h"
17#include "grit/ash_strings.h"
18#include "grit/ui_chromeos_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"
22#include "ui/chromeos/network/network_icon.h"
23#include "ui/chromeos/network/network_icon_animation.h"
24
25using chromeos::NetworkHandler;
26using chromeos::NetworkState;
27using chromeos::NetworkStateHandler;
28using chromeos::NetworkTypePattern;
29
30namespace ash {
31namespace tray {
32
33class VpnDefaultView : public TrayItemMore,
34                       public ui::network_icon::AnimationObserver {
35 public:
36  VpnDefaultView(SystemTrayItem* owner, bool show_more)
37      : TrayItemMore(owner, show_more) {
38    Update();
39  }
40
41  virtual ~VpnDefaultView() {
42    ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
43  }
44
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());
51    return vpn != NULL;
52  }
53
54  void Update() {
55    gfx::ImageSkia image;
56    base::string16 label;
57    bool animating = false;
58    GetNetworkStateHandlerImageAndLabel(&image, &label, &animating);
59    if (animating)
60      ui::network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
61    else
62      ui::network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(
63          this);
64    SetImage(&image);
65    SetLabel(label);
66    SetAccessibleName(label);
67  }
68
69  // ui::network_icon::AnimationObserver
70  virtual void NetworkIconChanged() OVERRIDE {
71    Update();
72  }
73
74 private:
75  void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia* image,
76                                           base::string16* label,
77                                           bool* animating) {
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);
85      if (label) {
86        *label = l10n_util::GetStringUTF16(
87            IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED);
88      }
89      *animating = false;
90      return;
91    }
92    *animating = vpn->IsConnectingState();
93    *image = ui::network_icon::GetImageForNetwork(
94        vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
95    if (label) {
96      *label = ui::network_icon::GetLabelForNetwork(
97          vpn, ui::network_icon::ICON_TYPE_DEFAULT_VIEW);
98    }
99  }
100
101  DISALLOW_COPY_AND_ASSIGN(VpnDefaultView);
102};
103
104}  // namespace tray
105
106TrayVPN::TrayVPN(SystemTray* system_tray)
107    : SystemTrayItem(system_tray),
108      default_(NULL),
109      detailed_(NULL) {
110  network_state_observer_.reset(new TrayNetworkStateObserver(this));
111}
112
113TrayVPN::~TrayVPN() {
114}
115
116views::View* TrayVPN::CreateTrayView(user::LoginStatus status) {
117  return NULL;
118}
119
120views::View* TrayVPN::CreateDefaultView(user::LoginStatus status) {
121  CHECK(default_ == NULL);
122  if (!chromeos::NetworkHandler::IsInitialized())
123    return NULL;
124  if (status == user::LOGGED_IN_NONE)
125    return NULL;
126  if (!tray::VpnDefaultView::ShouldShow())
127    return NULL;
128
129  default_ = new tray::VpnDefaultView(this, status != user::LOGGED_IN_LOCKED);
130  return default_;
131}
132
133views::View* TrayVPN::CreateDetailedView(user::LoginStatus status) {
134  CHECK(detailed_ == NULL);
135  if (!chromeos::NetworkHandler::IsInitialized())
136    return NULL;
137
138  Shell::GetInstance()->metrics()->RecordUserMetricsAction(
139      ash::UMA_STATUS_AREA_DETAILED_VPN_VIEW);
140  detailed_ = new tray::NetworkStateListDetailedView(
141      this, tray::NetworkStateListDetailedView::LIST_TYPE_VPN, status);
142  detailed_->Init();
143  return detailed_;
144}
145
146void TrayVPN::DestroyTrayView() {
147}
148
149void TrayVPN::DestroyDefaultView() {
150  default_ = NULL;
151}
152
153void TrayVPN::DestroyDetailedView() {
154  detailed_ = NULL;
155}
156
157void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status) {
158}
159
160void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
161}
162
163void TrayVPN::NetworkStateChanged(bool list_changed) {
164  if (default_)
165    default_->Update();
166  if (detailed_) {
167    if (list_changed)
168      detailed_->NetworkListChanged();
169    else
170      detailed_->ManagerChanged();
171  }
172}
173
174void TrayVPN::NetworkServiceChanged(const chromeos::NetworkState* network) {
175  if (detailed_)
176    detailed_->NetworkServiceChanged(network);
177}
178
179}  // namespace ash
180