summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_list.cc
blob: 926df02bfa9392292508c64e329615bec4f008de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/network_list.h"

#include "app/l10n_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "grit/generated_resources.h"

namespace chromeos {

////////////////////////////////////////////////////////////////////////////////
// NetworkList, public:

NetworkList::NetworkList() {
}

NetworkList::NetworkItem* NetworkList::GetNetworkAt(int index) {
  return index >= 0 && index < static_cast<int>(networks_.size()) ?
      &networks_[index] : NULL;
}

const NetworkList::NetworkItem* NetworkList::GetNetworkById(
    NetworkType type, const string16& id) {
  return GetNetworkAt(GetNetworkIndexById(type, id));
}

int NetworkList::GetNetworkIndexById(NetworkType type,
                                     const string16& id) const {
  if (type == NETWORK_EMPTY)
    return -1;
  std::string network_id = UTF16ToASCII(id);
  for (size_t i = 0; i < networks_.size(); ++i) {
    if (IsSameNetwork(&networks_[i], type, network_id))
      return i;
  }
  return -1;
}

bool NetworkList::IsNetworkConnected(NetworkType type,
                                     const string16& id) const {
  return IsInNetworkList(connected_networks_, type, id);
}

bool NetworkList::IsNetworkConnecting(NetworkType type,
                                      const string16& id) const {
  return IsInNetworkList(connecting_networks_, type, id);
}

void NetworkList::NetworkChanged(chromeos::NetworkLibrary* network_lib) {
  networks_.clear();
  connected_networks_.clear();
  connecting_networks_.clear();
  // Index of the last added network item.
  size_t index = 0;
  if (!network_lib || !CrosLibrary::Get()->EnsureLoaded())
    return;

  bool ethernet_connected = network_lib->ethernet_connected();
  bool ethernet_connecting = network_lib->ethernet_connecting();
  if (ethernet_connected || ethernet_connecting) {
    string16 label = l10n_util::GetStringUTF16(
        IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
    networks_.push_back(NetworkItem(NETWORK_ETHERNET,
                                    label,
                                    WifiNetwork(),
                                    CellularNetwork()));
    AddNetworkIndexToList(index++, ethernet_connected, ethernet_connecting);
  }

  // TODO(nkostylev): Show public WiFi networks first.
  WifiNetworkVector wifi = network_lib->wifi_networks();
  for (WifiNetworkVector::const_iterator it = wifi.begin();
       it != wifi.end(); ++it, ++index) {
    networks_.push_back(NetworkItem(NETWORK_WIFI,
                                    ASCIIToUTF16(it->name()),
                                    *it,
                                    CellularNetwork()));
    if (network_lib->wifi_network().service_path() == it->service_path()) {
      AddNetworkIndexToList(index,
                            network_lib->wifi_connected(),
                            network_lib->wifi_connecting());
    }
  }

  CellularNetworkVector cellular = network_lib->cellular_networks();
  for (CellularNetworkVector::const_iterator it = cellular.begin();
       it != cellular.end(); ++it, ++index) {
    networks_.push_back(NetworkItem(NETWORK_CELLULAR,
                                    ASCIIToUTF16(it->name()),
                                    WifiNetwork(),
                                    *it));
    if (network_lib->cellular_network().service_path() == it->service_path()) {
      AddNetworkIndexToList(index,
                            network_lib->cellular_connected(),
                            network_lib->cellular_connecting());
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
// NetworkList, private:

bool NetworkList::IsInNetworkList(const NetworkIndexVector& list,
                                  NetworkType type,
                                  const string16& id) const {
  if (type == NETWORK_EMPTY)
    return false;
  std::string network_id = UTF16ToASCII(id);
  for (size_t i = 0; i < list.size(); ++i) {
    if (IsSameNetwork(&networks_[list[i]], type, network_id))
      return true;
  }
  return false;
}

bool NetworkList::IsSameNetwork(const NetworkList::NetworkItem* network,
                                NetworkType type,
                                const std::string& id) const {
  if (type != network->network_type)
    return false;
  switch (type) {
    case NETWORK_ETHERNET:
      // Assuming that there's only single Ethernet network.
      return true;
    case NETWORK_WIFI:
      return id == network->wifi_network.name();
      break;
    case NETWORK_CELLULAR:
      return id == network->cellular_network.name();
      break;
    default:
      return false;
      break;
  }
}

void NetworkList::AddNetworkIndexToList(size_t index,
                                        bool connected,
                                        bool connecting) {
  if (connected) {
    connected_networks_.push_back(index);
  } else if (connecting) {
    connecting_networks_.push_back(index);
  }
}

}  // namespace chromeos