network_dropdown_button.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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 "chrome/browser/chromeos/status/network_dropdown_button.h"
6
7#include "app/l10n_util.h"
8#include "app/resource_bundle.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/browser/chromeos/cros/cros_library.h"
11#include "chrome/browser/chromeos/options/network_config_view.h"
12#include "chrome/browser/chromeos/status/status_area_host.h"
13#include "gfx/canvas_skia.h"
14#include "grit/generated_resources.h"
15#include "grit/theme_resources.h"
16#include "views/window/window.h"
17
18namespace chromeos {
19
20////////////////////////////////////////////////////////////////////////////////
21// NetworkDropdownButton
22
23// static
24const int NetworkDropdownButton::kThrobDuration = 1000;
25
26NetworkDropdownButton::NetworkDropdownButton(bool browser_mode,
27                                             gfx::NativeWindow parent_window)
28    : DropDownButton(NULL,
29                     l10n_util::GetString(IDS_STATUSBAR_NO_NETWORKS_MESSAGE),
30                     this,
31                     true),
32      browser_mode_(browser_mode),
33      ALLOW_THIS_IN_INITIALIZER_LIST(animation_connecting_(this)),
34      parent_window_(parent_window) {
35  animation_connecting_.SetThrobDuration(kThrobDuration);
36  animation_connecting_.SetTweenType(Tween::EASE_IN_OUT);
37  CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this);
38  // The initial state will be updated on Refresh.
39  // See network_selection_view.cc.
40}
41
42NetworkDropdownButton::~NetworkDropdownButton() {
43  CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this);
44}
45
46////////////////////////////////////////////////////////////////////////////////
47// NetworkMenuButton, AnimationDelegate implementation:
48
49void NetworkDropdownButton::AnimationProgressed(const Animation* animation) {
50  if (animation == &animation_connecting_) {
51    SetIcon(IconForNetworkConnecting(animation_connecting_.GetCurrentValue(),
52                                     true));
53    SchedulePaint();
54  } else {
55    MenuButton::AnimationProgressed(animation);
56  }
57}
58
59void NetworkDropdownButton::Refresh() {
60  OnNetworkManagerChanged(CrosLibrary::Get()->GetNetworkLibrary());
61}
62
63////////////////////////////////////////////////////////////////////////////////
64// NetworkDropdownButton, NetworkLibrary::NetworkManagerObserver implementation:
65
66void NetworkDropdownButton::OnNetworkManagerChanged(NetworkLibrary* cros) {
67  // Show network that we will actually use. It could be another network than
68  // user selected. For example user selected WiFi network but we have Ethernet
69  // connection and Chrome OS device will actually use Ethernet.
70
71  // This gets called on initialization, so any changes should be reflected
72  // in CrosMock::SetNetworkLibraryStatusAreaExpectations().
73
74  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
75  if (CrosLibrary::Get()->EnsureLoaded()) {
76    // Always show the active network, if any
77    const Network* active_network = cros->active_network();
78    if (active_network != NULL) {
79      animation_connecting_.Stop();
80      if (active_network->type() == TYPE_ETHERNET) {
81        SetIcon(*rb.GetBitmapNamed(IDR_STATUSBAR_WIRED));
82        SetText(l10n_util::GetString(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET));
83      } else if (active_network->type() == TYPE_WIFI) {
84        const WifiNetwork* wifi =
85            static_cast<const WifiNetwork*>(active_network);
86        SetIcon(IconForNetworkStrength(wifi, true));
87        SetText(ASCIIToWide(wifi->name()));
88      } else if (active_network->type() == TYPE_CELLULAR) {
89        const CellularNetwork* cellular =
90            static_cast<const CellularNetwork*>(active_network);
91        SetIcon(IconForNetworkStrength(cellular, true));
92        SetText(ASCIIToWide(cellular->name()));
93      } else {
94        NOTREACHED();
95      }
96    } else if (cros->wifi_connecting() || cros->cellular_connecting()) {
97      if (!animation_connecting_.is_animating()) {
98        animation_connecting_.Reset();
99        animation_connecting_.StartThrobbing(-1);
100        SetIcon(IconForNetworkConnecting(0, true));
101      }
102      if (cros->wifi_connecting())
103        SetText(ASCIIToWide(cros->wifi_network()->name()));
104      else if (cros->cellular_connecting())
105        SetText(ASCIIToWide(cros->cellular_network()->name()));
106    }
107
108    if (!cros->Connected() && !cros->Connecting()) {
109      animation_connecting_.Stop();
110      SetIcon(SkBitmap());
111      SetText(l10n_util::GetString(IDS_NETWORK_SELECTION_NONE));
112    }
113  } else {
114    animation_connecting_.Stop();
115    SetIcon(SkBitmap());
116    SetText(l10n_util::GetString(IDS_STATUSBAR_NO_NETWORKS_MESSAGE));
117  }
118
119  SchedulePaint();
120  UpdateMenu();
121}
122
123}  // namespace chromeos
124