tray_vpn.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/chromeos/network/tray_vpn.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/shell.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/chromeos/network/network_icon_animation.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/chromeos/network/network_state_list_detailed_view.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray_delegate.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/system_tray_notifier.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_constants.h"
1458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "ash/system/tray/tray_item_more.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ash/system/tray/tray_popup_label_button.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chromeos/network/network_state.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chromeos/network/network_state_handler.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/ash_strings.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using chromeos::NetworkHandler;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using chromeos::NetworkState;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using chromeos::NetworkStateHandler;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ash {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace tray {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class VpnDefaultView : public TrayItemMore,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       public network_icon::AnimationObserver {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VpnDefaultView(SystemTrayItem* owner, bool show_more)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : TrayItemMore(owner, show_more) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Update();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~VpnDefaultView() {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static bool ShouldShow() {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Do not show VPN line in uber tray bubble if VPN is not configured.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NetworkStateHandler* handler =
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NetworkHandler::Get()->network_state_handler();
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const NetworkState* vpn = handler->FirstNetworkByType(flimflam::kTypeVPN);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return vpn != NULL;
50  }
51
52  void Update() {
53    gfx::ImageSkia image;
54    base::string16 label;
55    bool animating = false;
56    GetNetworkStateHandlerImageAndLabel(&image, &label, &animating);
57    if (animating)
58      network_icon::NetworkIconAnimation::GetInstance()->AddObserver(this);
59    else
60      network_icon::NetworkIconAnimation::GetInstance()->RemoveObserver(this);
61    SetImage(&image);
62    SetLabel(label);
63    SetAccessibleName(label);
64  }
65
66  // network_icon::AnimationObserver
67  virtual void NetworkIconChanged() OVERRIDE {
68    Update();
69  }
70
71 private:
72  void GetNetworkStateHandlerImageAndLabel(gfx::ImageSkia* image,
73                                           base::string16* label,
74                                           bool* animating) {
75    NetworkStateHandler* handler =
76        NetworkHandler::Get()->network_state_handler();
77    const NetworkState* vpn = handler->FirstNetworkByType(
78        flimflam::kTypeVPN);
79    if (!vpn || (vpn->connection_state() == flimflam::kStateIdle)) {
80      *image = network_icon::GetImageForDisconnectedNetwork(
81          network_icon::ICON_TYPE_DEFAULT_VIEW, flimflam::kTypeVPN);
82      if (label) {
83        *label = l10n_util::GetStringUTF16(
84            IDS_ASH_STATUS_TRAY_VPN_DISCONNECTED);
85      }
86      *animating = false;
87      return;
88    }
89    *animating = vpn->IsConnectingState();
90    *image = network_icon::GetImageForNetwork(
91        vpn, network_icon::ICON_TYPE_DEFAULT_VIEW);
92    if (label) {
93      *label = network_icon::GetLabelForNetwork(
94          vpn, network_icon::ICON_TYPE_DEFAULT_VIEW);
95    }
96  }
97
98  DISALLOW_COPY_AND_ASSIGN(VpnDefaultView);
99};
100
101}  // namespace tray
102
103TrayVPN::TrayVPN(SystemTray* system_tray)
104    : SystemTrayItem(system_tray),
105      default_(NULL),
106      detailed_(NULL) {
107  network_state_observer_.reset(new TrayNetworkStateObserver(this));
108}
109
110TrayVPN::~TrayVPN() {
111}
112
113views::View* TrayVPN::CreateTrayView(user::LoginStatus status) {
114  return NULL;
115}
116
117views::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;
125
126  default_ = new tray::VpnDefaultView(this, status != user::LOGGED_IN_LOCKED);
127  return default_;
128}
129
130views::View* TrayVPN::CreateDetailedView(user::LoginStatus status) {
131  CHECK(detailed_ == NULL);
132  if (!chromeos::NetworkHandler::IsInitialized())
133    return NULL;
134
135  detailed_ = new tray::NetworkStateListDetailedView(
136      this, tray::NetworkStateListDetailedView::LIST_TYPE_VPN, status);
137  detailed_->Init();
138  return detailed_;
139}
140
141views::View* TrayVPN::CreateNotificationView(user::LoginStatus status) {
142  return NULL;
143}
144
145void TrayVPN::DestroyTrayView() {
146}
147
148void TrayVPN::DestroyDefaultView() {
149  default_ = NULL;
150}
151
152void TrayVPN::DestroyDetailedView() {
153  detailed_ = NULL;
154}
155
156void TrayVPN::DestroyNotificationView() {
157}
158
159void TrayVPN::UpdateAfterLoginStatusChange(user::LoginStatus status) {
160}
161
162void TrayVPN::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
163}
164
165void TrayVPN::NetworkStateChanged(bool list_changed) {
166  if (default_)
167    default_->Update();
168  if (detailed_) {
169    if (list_changed)
170      detailed_->NetworkListChanged();
171    else
172      detailed_->ManagerChanged();
173  }
174}
175
176void TrayVPN::NetworkServiceChanged(const chromeos::NetworkState* network) {
177  if (detailed_)
178    detailed_->NetworkServiceChanged(network);
179}
180
181}  // namespace internal
182}  // namespace ash
183