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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
// Copyright (c) 2009 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/cros/network_library.h"
#include <algorithm>
#include "base/string_util.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "net/url_request/url_request_job.h"
// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
template <>
struct RunnableMethodTraits<chromeos::NetworkLibrary> {
void RetainCallee(chromeos::NetworkLibrary* obj) {}
void ReleaseCallee(chromeos::NetworkLibrary* obj) {}
};
namespace chromeos {
////////////////////////////////////////////////////////////////////////////////
// NetworkLibrary
// static
const int NetworkLibrary::kNetworkTrafficeTimerSecs = 1;
NetworkLibrary::NetworkLibrary()
: traffic_type_(0),
network_devices_(0),
offline_mode_(false) {
if (CrosLibrary::EnsureLoaded()) {
Init();
}
g_url_request_job_tracker.AddObserver(this);
}
NetworkLibrary::~NetworkLibrary() {
if (CrosLibrary::EnsureLoaded()) {
chromeos::DisconnectNetworkStatus(network_status_connection_);
}
g_url_request_job_tracker.RemoveObserver(this);
}
// static
NetworkLibrary* NetworkLibrary::Get() {
return Singleton<NetworkLibrary>::get();
}
// static
bool NetworkLibrary::EnsureLoaded() {
return CrosLibrary::EnsureLoaded();
}
////////////////////////////////////////////////////////////////////////////////
// NetworkLibrary, URLRequestJobTracker::JobObserver implementation:
void NetworkLibrary::OnJobAdded(URLRequestJob* job) {
CheckNetworkTraffic(false);
}
void NetworkLibrary::OnJobRemoved(URLRequestJob* job) {
CheckNetworkTraffic(false);
}
void NetworkLibrary::OnJobDone(URLRequestJob* job,
const URLRequestStatus& status) {
CheckNetworkTraffic(false);
}
void NetworkLibrary::OnJobRedirect(URLRequestJob* job, const GURL& location,
int status_code) {
CheckNetworkTraffic(false);
}
void NetworkLibrary::OnBytesRead(URLRequestJob* job, int byte_count) {
CheckNetworkTraffic(true);
}
void NetworkLibrary::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NetworkLibrary::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
static const char* GetEncryptionString(chromeos::EncryptionType encryption) {
switch (encryption) {
case chromeos::NONE:
return "none";
case chromeos::RSN:
return "rsn";
case chromeos::WEP:
return "wep";
case chromeos::WPA:
return "wpa";
}
return "none";
}
void NetworkLibrary::ConnectToWifiNetwork(WifiNetwork network,
const string16& password) {
if (CrosLibrary::EnsureLoaded()) {
// This call kicks off a request to connect to this network, the results of
// which we'll hear about through the monitoring we've set up in Init();
chromeos::ConnectToWifiNetwork(
network.ssid.c_str(),
password.empty() ? NULL : UTF16ToUTF8(password).c_str(),
GetEncryptionString(network.encryption));
}
}
void NetworkLibrary::ConnectToCellularNetwork(CellularNetwork network) {
if (CrosLibrary::EnsureLoaded()) {
// This call kicks off a request to connect to this network, the results of
// which we'll hear about through the monitoring we've set up in Init();
chromeos::ConnectToWifiNetwork(network.name.c_str(), NULL, NULL);
}
}
void NetworkLibrary::EnableEthernetNetworkDevice(bool enable) {
EnableNetworkDevice(chromeos::TYPE_ETHERNET, enable);
}
void NetworkLibrary::EnableWifiNetworkDevice(bool enable) {
EnableNetworkDevice(chromeos::TYPE_WIFI, enable);
}
void NetworkLibrary::EnableCellularNetworkDevice(bool enable) {
EnableNetworkDevice(chromeos::TYPE_CELLULAR, enable);
}
void NetworkLibrary::EnableOfflineMode(bool enable) {
if (!CrosLibrary::EnsureLoaded())
return;
// If network device is already enabled/disabled, then don't do anything.
if (enable && offline_mode_) {
LOG(INFO) << "Trying to enable offline mode when it's already enabled. ";
return;
}
if (!enable && !offline_mode_) {
LOG(INFO) << "Trying to disable offline mode when it's already disabled. ";
return;
}
if (chromeos::SetOfflineMode(enable)) {
offline_mode_ = enable;
}
}
NetworkIPConfigVector NetworkLibrary::GetIPConfigs(
const std::string& device_path) {
NetworkIPConfigVector ipconfig_vector;
if (!device_path.empty()) {
chromeos::IPConfigStatus* ipconfig_status =
chromeos::ListIPConfigs(device_path.c_str());
if (ipconfig_status) {
for (int i = 0; i < ipconfig_status->size; i++) {
chromeos::IPConfig ipconfig = ipconfig_status->ips[i];
ipconfig_vector.push_back(
NetworkIPConfig(device_path, ipconfig.type, ipconfig.address,
ipconfig.netmask, ipconfig.gateway,
ipconfig.name_servers));
}
chromeos::FreeIPConfigStatus(ipconfig_status);
// Sort the list of ip configs by type.
std::sort(ipconfig_vector.begin(), ipconfig_vector.end());
}
}
return ipconfig_vector;
}
// static
void NetworkLibrary::NetworkStatusChangedHandler(void* object,
const chromeos::ServiceStatus& service_status) {
NetworkLibrary* network = static_cast<NetworkLibrary*>(object);
EthernetNetwork ethernet;
WifiNetworkVector wifi_networks;
CellularNetworkVector cellular_networks;
ParseNetworks(service_status, ðernet, &wifi_networks, &cellular_networks);
network->UpdateNetworkStatus(ethernet, wifi_networks, cellular_networks);
}
// static
void NetworkLibrary::ParseNetworks(
const chromeos::ServiceStatus& service_status, EthernetNetwork* ethernet,
WifiNetworkVector* wifi_networks,
CellularNetworkVector* cellular_networks) {
DLOG(INFO) << "ParseNetworks:";
for (int i = 0; i < service_status.size; i++) {
const chromeos::ServiceInfo& service = service_status.services[i];
DLOG(INFO) << " (" << service.type <<
") " << service.ssid <<
" sta=" << service.state <<
" pas=" << service.needs_passphrase <<
" enc=" << service.encryption <<
" sig=" << service.signal_strength;
bool connecting = service.state == chromeos::STATE_ASSOCIATION ||
service.state == chromeos::STATE_CONFIGURATION ||
service.state == chromeos::STATE_CARRIER;
bool connected = service.state == chromeos::STATE_READY;
// if connected, get ip config
std::string ip_address;
if (connected) {
chromeos::IPConfigStatus* ipconfig_status =
chromeos::ListIPConfigs(service.device_path);
if (ipconfig_status) {
for (int i = 0; i < ipconfig_status->size; i++) {
chromeos::IPConfig ipconfig = ipconfig_status->ips[i];
if (strlen(ipconfig.address) > 0)
ip_address = ipconfig.address;
DLOG(INFO) << " ipconfig: " <<
" type=" << ipconfig.type <<
" address=" << ipconfig.address <<
" mtu=" << ipconfig.mtu <<
" netmask=" << ipconfig.netmask <<
" broadcast=" << ipconfig.broadcast <<
" peer_address=" << ipconfig.peer_address <<
" gateway=" << ipconfig.gateway <<
" domainname=" << ipconfig.domainname <<
" name_servers=" << ipconfig.name_servers;
}
chromeos::FreeIPConfigStatus(ipconfig_status);
}
}
if (service.type == chromeos::TYPE_ETHERNET) {
ethernet->connecting = connecting;
ethernet->connected = connected;
ethernet->device_path = service.device_path;
ethernet->ip_address = ip_address;
} else if (service.type == chromeos::TYPE_WIFI) {
wifi_networks->push_back(WifiNetwork(service.device_path,
service.ssid,
service.needs_passphrase,
service.encryption,
service.signal_strength,
connecting,
connected,
ip_address));
} else if (service.type == chromeos::TYPE_CELLULAR) {
cellular_networks->push_back(CellularNetwork(service.device_path,
service.ssid,
service.signal_strength,
connecting,
connected,
ip_address));
}
}
}
void NetworkLibrary::Init() {
// First, get the currently available networks. This data is cached
// on the connman side, so the call should be quick.
chromeos::ServiceStatus* service_status = chromeos::GetAvailableNetworks();
if (service_status) {
LOG(INFO) << "Getting initial CrOS network info.";
EthernetNetwork ethernet;
WifiNetworkVector wifi_networks;
CellularNetworkVector cellular_networks;
ParseNetworks(*service_status, ðernet, &wifi_networks,
&cellular_networks);
UpdateNetworkStatus(ethernet, wifi_networks, cellular_networks);
chromeos::FreeServiceStatus(service_status);
}
LOG(INFO) << "Registering for network status updates.";
// Now, register to receive updates on network status.
network_status_connection_ = chromeos::MonitorNetworkStatus(
&NetworkStatusChangedHandler, this);
// Get the enabled network devices bit flag. If we get a -1, then that means
// offline mode is on. So all devices are disabled. This happens when offline
// mode is persisted during a reboot and Chrome starts up with it on.
network_devices_ = chromeos::GetEnabledNetworkDevices();
if (network_devices_ == -1) {
offline_mode_ = true;
network_devices_ = 0;
}
}
void NetworkLibrary::EnableNetworkDevice(chromeos::ConnectionType device,
bool enable) {
if (!CrosLibrary::EnsureLoaded())
return;
// If network device is already enabled/disabled, then don't do anything.
if (enable && (network_devices_ & device)) {
LOG(INFO) << "Trying to enable a network device that's already enabled: "
<< device;
return;
}
if (!enable && !(network_devices_ & device)) {
LOG(INFO) << "Trying to disable a network device that's already disabled: "
<< device;
return;
}
if (chromeos::EnableNetworkDevice(device, enable)) {
if (enable)
network_devices_ |= device;
else
network_devices_ &= ~device;
}
}
void NetworkLibrary::UpdateNetworkStatus(const EthernetNetwork& ethernet,
const WifiNetworkVector& wifi_networks,
const CellularNetworkVector& cellular_networks) {
// Make sure we run on UI thread.
if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this,
&NetworkLibrary::UpdateNetworkStatus, ethernet, wifi_networks,
cellular_networks));
return;
}
ethernet_ = ethernet;
wifi_networks_ = wifi_networks;
// Sort the list of wifi networks by ssid.
std::sort(wifi_networks_.begin(), wifi_networks_.end());
wifi_ = WifiNetwork();
for (size_t i = 0; i < wifi_networks_.size(); i++) {
if (wifi_networks_[i].connecting || wifi_networks_[i].connected) {
wifi_ = wifi_networks_[i];
break; // There is only one connected or connecting wifi network.
}
}
cellular_networks_ = cellular_networks;
std::sort(cellular_networks_.begin(), cellular_networks_.end());
cellular_ = CellularNetwork();
for (size_t i = 0; i < cellular_networks_.size(); i++) {
if (cellular_networks_[i].connecting || cellular_networks_[i].connected) {
cellular_ = cellular_networks_[i];
break; // There is only one connected or connecting cellular network.
}
}
FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this));
}
void NetworkLibrary::CheckNetworkTraffic(bool download) {
// If we already have a pending upload and download notification, then
// shortcut and return.
if (traffic_type_ == (Observer::TRAFFIC_DOWNLOAD | Observer::TRAFFIC_UPLOAD))
return;
// Figure out if we are uploading and/or downloading. We are downloading
// if download == true. We are uploading if we have upload progress.
if (download)
traffic_type_ |= Observer::TRAFFIC_DOWNLOAD;
if ((traffic_type_ & Observer::TRAFFIC_UPLOAD) == 0) {
URLRequestJobTracker::JobIterator it;
for (it = g_url_request_job_tracker.begin();
it != g_url_request_job_tracker.end();
++it) {
URLRequestJob* job = *it;
if (job->GetUploadProgress() > 0) {
traffic_type_ |= Observer::TRAFFIC_UPLOAD;
break;
}
}
}
// If we have new traffic data to send out and the timer is not currently
// running, then start a new timer.
if (traffic_type_ && !timer_.IsRunning()) {
timer_.Start(base::TimeDelta::FromSeconds(kNetworkTrafficeTimerSecs), this,
&NetworkLibrary::NetworkTrafficTimerFired);
}
}
void NetworkLibrary:: NetworkTrafficTimerFired() {
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &NetworkLibrary::NotifyNetworkTraffic,
traffic_type_));
// Reset traffic type so that we don't send the same data next time.
traffic_type_ = 0;
}
void NetworkLibrary::NotifyNetworkTraffic(int traffic_type) {
FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type));
}
bool NetworkLibrary::Connected() const {
return ethernet_connected() || wifi_connected() || cellular_connected();
}
} // namespace chromeos
|