summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_list.cc
blob: c22795b6eeb3a06ef37abc51bb95425718705156 (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
151
152
153
154
155
// 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::OnNetworkManagerChanged(
    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,
                                    NULL,
                                    NULL));
    AddNetworkIndexToList(index++, ethernet_connected, ethernet_connecting);
  }

  // TODO(nkostylev): Show public WiFi networks first.
  const 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,
                                    NULL));
    if (network_lib->wifi_network() &&
        network_lib->wifi_network()->service_path() == (*it)->service_path()) {
      AddNetworkIndexToList(index,
                            network_lib->wifi_connected(),
                            network_lib->wifi_connecting());
    }
  }

  const 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()),
                                    NULL,
                                    *it));
    if (network_lib->cellular_network() &&
        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 network->wifi_network && id == network->wifi_network->name();
      break;
    case NETWORK_CELLULAR:
      return network->cellular_network &&
          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