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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
// 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/cros/network_library.h"
#include <algorithm>
#include "base/string_util.h"
#include "base/utf_string_conversions.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::NetworkLibraryImpl> {
void RetainCallee(chromeos::NetworkLibraryImpl* obj) {}
void ReleaseCallee(chromeos::NetworkLibraryImpl* obj) {}
};
namespace chromeos {
static const std::string kGoogleAWifi = "Google-A";
// Helper function to wrap Html with <th> tag.
static std::string WrapWithTH(std::string text) {
return "<th>" + text + "</th>";
}
// Helper function to wrap Html with <td> tag.
static std::string WrapWithTD(std::string text) {
return "<td>" + text + "</td>";
}
// Helper function to create an Html table header for a Network.
static std::string ToHtmlTableHeader(Network* network) {
std::string str;
if (network->type() == TYPE_WIFI || network->type() == TYPE_CELLULAR) {
str += WrapWithTH("Name") + WrapWithTH("Auto-Connect") +
WrapWithTH("Strength");
if (network->type() == TYPE_WIFI)
str += WrapWithTH("Encryption") + WrapWithTH("Passphrase") +
WrapWithTH("Identity") + WrapWithTH("Certificate");
}
str += WrapWithTH("State") + WrapWithTH("Error") + WrapWithTH("IP Address");
return str;
}
// Helper function to create an Html table row for a Network.
static std::string ToHtmlTableRow(Network* network) {
std::string str;
if (network->type() == TYPE_WIFI || network->type() == TYPE_CELLULAR) {
WirelessNetwork* wireless = static_cast<WirelessNetwork*>(network);
str += WrapWithTD(wireless->name()) +
WrapWithTD(IntToString(wireless->auto_connect())) +
WrapWithTD(IntToString(wireless->strength()));
if (network->type() == TYPE_WIFI) {
WifiNetwork* wifi = static_cast<WifiNetwork*>(network);
str += WrapWithTD(wifi->GetEncryptionString()) +
WrapWithTD(wifi->passphrase()) + WrapWithTD(wifi->identity()) +
WrapWithTD(wifi->cert_path());
}
}
str += WrapWithTD(network->GetStateString()) +
WrapWithTD(network->GetErrorString()) + WrapWithTD(network->ip_address());
return str;
}
////////////////////////////////////////////////////////////////////////////////
// Network
void Network::Clear() {
type_ = TYPE_UNKNOWN;
state_ = STATE_UNKNOWN;
error_ = ERROR_UNKNOWN;
service_path_.clear();
device_path_.clear();
ip_address_.clear();
}
void Network::ConfigureFromService(const ServiceInfo& service) {
type_ = service.type;
state_ = service.state;
error_ = service.error;
service_path_ = service.service_path;
device_path_ = service.device_path ? service.device_path : std::string();
ip_address_.clear();
// If connected, get ip config.
if (connected() && service.device_path) {
IPConfigStatus* ipconfig_status = ListIPConfigs(service.device_path);
if (ipconfig_status) {
for (int i = 0; i < ipconfig_status->size; i++) {
IPConfig ipconfig = ipconfig_status->ips[i];
if (strlen(ipconfig.address) > 0)
ip_address_ = ipconfig.address;
}
FreeIPConfigStatus(ipconfig_status);
}
}
}
std::string Network::GetStateString() {
switch (state_) {
case STATE_UNKNOWN:
break;
case STATE_IDLE:
return "Idle";
case STATE_CARRIER:
return "Carrier";
case STATE_ASSOCIATION:
return "Association";
case STATE_CONFIGURATION:
return "Configuration";
case STATE_READY:
return "Ready";
case STATE_DISCONNECT:
return "Disconnect";
case STATE_FAILURE:
return "Failure";
}
return "Unknown";
}
std::string Network::GetErrorString() {
switch (error_) {
case ERROR_UNKNOWN:
break;
case ERROR_OUT_OF_RANGE:
return "Out Of Range";
case ERROR_PIN_MISSING:
return "Pin Missing";
case ERROR_DHCP_FAILED:
return "DHCP Failed";
case ERROR_CONNECT_FAILED:
return "Connect Failed";
}
return "";
}
////////////////////////////////////////////////////////////////////////////////
// WirelessNetwork
void WirelessNetwork::Clear() {
Network::Clear();
name_.clear();
strength_ = 0;
auto_connect_ = false;
}
void WirelessNetwork::ConfigureFromService(const ServiceInfo& service) {
Network::ConfigureFromService(service);
name_ = service.name;
strength_ = service.strength;
auto_connect_ = service.auto_connect;
}
////////////////////////////////////////////////////////////////////////////////
// WifiNetwork
void WifiNetwork::Clear() {
WirelessNetwork::Clear();
encryption_ = SECURITY_NONE;
passphrase_.clear();
identity_.clear();
cert_path_.clear();
}
void WifiNetwork::ConfigureFromService(const ServiceInfo& service) {
WirelessNetwork::ConfigureFromService(service);
encryption_ = service.security;
passphrase_ = service.passphrase;
identity_ = service.identity;
cert_path_ = service.cert_path;
}
std::string WifiNetwork::GetEncryptionString() {
switch (encryption_) {
case SECURITY_UNKNOWN:
break;
case SECURITY_NONE:
return "";
case SECURITY_WEP:
return "WEP";
case SECURITY_WPA:
return "WPA";
case SECURITY_RSN:
return "RSN";
case SECURITY_8021X:
return "8021X";
}
return "Unknown";}
////////////////////////////////////////////////////////////////////////////////
// NetworkLibrary
// static
const int NetworkLibraryImpl::kNetworkTrafficeTimerSecs = 1;
NetworkLibraryImpl::NetworkLibraryImpl()
: traffic_type_(0),
network_status_connection_(NULL),
available_devices_(0),
enabled_devices_(0),
connected_devices_(0),
offline_mode_(false) {
if (CrosLibrary::Get()->EnsureLoaded()) {
Init();
}
g_url_request_job_tracker.AddObserver(this);
}
NetworkLibraryImpl::~NetworkLibraryImpl() {
if (network_status_connection_) {
DisconnectMonitorNetwork(network_status_connection_);
}
g_url_request_job_tracker.RemoveObserver(this);
}
////////////////////////////////////////////////////////////////////////////////
// NetworkLibraryImpl, URLRequestJobTracker::JobObserver implementation:
void NetworkLibraryImpl::OnJobAdded(URLRequestJob* job) {
CheckNetworkTraffic(false);
}
void NetworkLibraryImpl::OnJobRemoved(URLRequestJob* job) {
CheckNetworkTraffic(false);
}
void NetworkLibraryImpl::OnJobDone(URLRequestJob* job,
const URLRequestStatus& status) {
CheckNetworkTraffic(false);
}
void NetworkLibraryImpl::OnJobRedirect(URLRequestJob* job, const GURL& location,
int status_code) {
CheckNetworkTraffic(false);
}
void NetworkLibraryImpl::OnBytesRead(URLRequestJob* job, int byte_count) {
CheckNetworkTraffic(true);
}
void NetworkLibraryImpl::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NetworkLibraryImpl::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void NetworkLibraryImpl::RequestWifiScan() {
if (CrosLibrary::Get()->EnsureLoaded()) {
RequestScan(TYPE_WIFI);
}
}
bool NetworkLibraryImpl::ConnectToPreferredNetworkIfAvailable() {
// TODO(chocobo): Add the concept of preferred network to libcros.
// So that we don't have to hard-code Google-A here.
if (CrosLibrary::Get()->EnsureLoaded()) {
LOG(INFO) << "Attempting to auto-connect to Google-A wifi.";
// If ethernet is connected, then don't bother.
if (ethernet_connected()) {
LOG(INFO) << "Ethernet connected, so don't need Google-A.";
return false;
}
// Find Google-A network.
WifiNetwork wifi;
bool found = GetWifiNetworkByName(kGoogleAWifi, &wifi);
if (!found) {
LOG(INFO) << "Google-A wifi not found.";
return false;
}
// See if identity and certpath are available.
if (wifi.identity().empty() || wifi.cert_path().empty()) {
LOG(INFO) << "Google-A wifi not set up.";
return false;
}
// Make sure we want to auto-connect.
if (!wifi.auto_connect()) {
LOG(INFO) << "Google-A wifi is set to not auto-connect.";
return false;
}
// Connect to Google-A.
ConnectToNetwork(wifi.service_path().c_str(), NULL);
return true;
}
return false;
}
bool NetworkLibraryImpl::PreferredNetworkConnected() {
WifiNetwork wifi;
bool found = GetWifiNetworkByName(kGoogleAWifi, &wifi);
return found && wifi.connected();
}
bool NetworkLibraryImpl::PreferredNetworkFailed() {
WifiNetwork wifi;
bool found = GetWifiNetworkByName(kGoogleAWifi, &wifi);
return !found || wifi.failed();
}
void NetworkLibraryImpl::ConnectToWifiNetwork(WifiNetwork network,
const string16& password,
const string16& identity,
const string16& certpath) {
if (CrosLibrary::Get()->EnsureLoaded()) {
ConnectToNetworkWithCertInfo(network.service_path().c_str(),
password.empty() ? NULL : UTF16ToUTF8(password).c_str(),
identity.empty() ? NULL : UTF16ToUTF8(identity).c_str(),
certpath.empty() ? NULL : UTF16ToUTF8(certpath).c_str());
}
}
void NetworkLibraryImpl::ConnectToWifiNetwork(const string16& ssid,
const string16& password,
const string16& identity,
const string16& certpath,
bool auto_connect) {
if (CrosLibrary::Get()->EnsureLoaded()) {
// First create a service from hidden network.
ServiceInfo* service = GetWifiService(UTF16ToUTF8(ssid).c_str(),
SECURITY_UNKNOWN);
if (service) {
// Set auto-connect.
SetAutoConnect(service->service_path, auto_connect);
// Now connect to that service.
ConnectToNetworkWithCertInfo(service->service_path,
password.empty() ? NULL : UTF16ToUTF8(password).c_str(),
identity.empty() ? NULL : UTF16ToUTF8(identity).c_str(),
certpath.empty() ? NULL : UTF16ToUTF8(certpath).c_str());
// Clean up ServiceInfo object.
FreeServiceInfo(service);
} else {
LOG(WARNING) << "Cannot find hidden network: " << ssid;
// TODO(chocobo): Show error message.
}
}
}
void NetworkLibraryImpl::ConnectToCellularNetwork(CellularNetwork network) {
if (CrosLibrary::Get()->EnsureLoaded()) {
ConnectToNetwork(network.service_path().c_str(), NULL);
}
}
void NetworkLibraryImpl::DisconnectFromWirelessNetwork(
const WirelessNetwork& network) {
if (CrosLibrary::Get()->EnsureLoaded()) {
DisconnectFromNetwork(network.service_path().c_str());
}
}
void NetworkLibraryImpl::SaveWifiNetwork(const WifiNetwork& network) {
if (CrosLibrary::Get()->EnsureLoaded()) {
SetPassphrase(network.service_path().c_str(), network.passphrase().c_str());
SetIdentity(network.service_path().c_str(), network.identity().c_str());
SetCertPath(network.service_path().c_str(), network.cert_path().c_str());
SetAutoConnect(network.service_path().c_str(), network.auto_connect());
}
}
void NetworkLibraryImpl::ForgetWirelessNetwork(const WirelessNetwork& network) {
if (CrosLibrary::Get()->EnsureLoaded()) {
DeleteRememberedService(network.service_path().c_str());
}
}
void NetworkLibraryImpl::EnableEthernetNetworkDevice(bool enable) {
EnableNetworkDeviceType(TYPE_ETHERNET, enable);
}
void NetworkLibraryImpl::EnableWifiNetworkDevice(bool enable) {
EnableNetworkDeviceType(TYPE_WIFI, enable);
}
void NetworkLibraryImpl::EnableCellularNetworkDevice(bool enable) {
EnableNetworkDeviceType(TYPE_CELLULAR, enable);
}
void NetworkLibraryImpl::EnableOfflineMode(bool enable) {
if (!CrosLibrary::Get()->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 (SetOfflineMode(enable)) {
offline_mode_ = enable;
}
}
NetworkIPConfigVector NetworkLibraryImpl::GetIPConfigs(
const std::string& device_path) {
NetworkIPConfigVector ipconfig_vector;
if (!device_path.empty()) {
IPConfigStatus* ipconfig_status = ListIPConfigs(device_path.c_str());
if (ipconfig_status) {
for (int i = 0; i < ipconfig_status->size; i++) {
IPConfig ipconfig = ipconfig_status->ips[i];
ipconfig_vector.push_back(
NetworkIPConfig(device_path, ipconfig.type, ipconfig.address,
ipconfig.netmask, ipconfig.gateway,
ipconfig.name_servers));
}
FreeIPConfigStatus(ipconfig_status);
// Sort the list of ip configs by type.
std::sort(ipconfig_vector.begin(), ipconfig_vector.end());
}
}
return ipconfig_vector;
}
std::string NetworkLibraryImpl::GetHtmlInfo(int refresh) {
std::string output;
output.append("<html><head><title>About Network</title>");
if (refresh > 0)
output.append("<meta http-equiv=\"refresh\" content=\"" +
IntToString(refresh) + "\"/>");
output.append("</head><body>");
if (refresh > 0)
output.append("(Auto-refreshing page every " + IntToString(refresh) + "s)");
else
output.append("(To auto-refresh this page: about:network/<secs>)");
output.append("<h3>Ethernet:</h3><table border=1>");
output.append("<tr>" + ToHtmlTableHeader(ðernet_) + "</tr>");
output.append("<tr>" + ToHtmlTableRow(ðernet_) + "</tr>");
output.append("</table><h3>Wifi:</h3><table border=1>");
for (size_t i = 0; i < wifi_networks_.size(); ++i) {
if (i == 0)
output.append("<tr>" + ToHtmlTableHeader(&wifi_networks_[i]) + "</tr>");
output.append("<tr>" + ToHtmlTableRow(&wifi_networks_[i]) + "</tr>");
}
output.append("</table><h3>Cellular:</h3><table border=1>");
for (size_t i = 0; i < cellular_networks_.size(); ++i) {
if (i == 0)
output.append("<tr>" + ToHtmlTableHeader(&cellular_networks_[i]) +
"</tr>");
output.append("<tr>" + ToHtmlTableRow(&cellular_networks_[i]) + "</tr>");
}
output.append("</table><h3>Remembered Wifi:</h3><table border=1>");
for (size_t i = 0; i < remembered_wifi_networks_.size(); ++i) {
if (i == 0)
output.append("<tr>" + ToHtmlTableHeader(&remembered_wifi_networks_[i]) +
"</tr>");
output.append("<tr>" + ToHtmlTableRow(&remembered_wifi_networks_[i]) +
"</tr>");
}
output.append("</table><h3>Remembered Cellular:</h3><table border=1>");
for (size_t i = 0; i < remembered_cellular_networks_.size(); ++i) {
if (i == 0)
output.append("<tr>" +
ToHtmlTableHeader(&remembered_cellular_networks_[i]) + "</tr>");
output.append("<tr>" + ToHtmlTableRow(&remembered_cellular_networks_[i]) +
"</tr>");
}
output.append("</table></body></html>");
return output;
}
// static
void NetworkLibraryImpl::NetworkStatusChangedHandler(void* object) {
NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object);
SystemInfo* system = GetSystemInfo();
if (system) {
network->UpdateNetworkStatus(system);
FreeSystemInfo(system);
}
}
// static
void NetworkLibraryImpl::ParseSystem(SystemInfo* system,
EthernetNetwork* ethernet,
WifiNetworkVector* wifi_networks,
CellularNetworkVector* cellular_networks,
WifiNetworkVector* remembered_wifi_networks,
CellularNetworkVector* remembered_cellular_networks) {
DLOG(INFO) << "ParseSystem:";
ethernet->Clear();
for (int i = 0; i < system->service_size; i++) {
const ServiceInfo& service = system->services[i];
DLOG(INFO) << " (" << service.type <<
") " << service.name <<
" mode=" << service.mode <<
" state=" << service.state <<
" sec=" << service.security <<
" req=" << service.passphrase_required <<
" pass=" << service.passphrase <<
" id=" << service.identity <<
" certpath=" << service.cert_path <<
" str=" << service.strength <<
" fav=" << service.favorite <<
" auto=" << service.auto_connect <<
" error=" << service.error;
if (service.type == TYPE_ETHERNET)
ethernet->ConfigureFromService(service);
else if (service.type == TYPE_WIFI)
wifi_networks->push_back(WifiNetwork(service));
else if (service.type == TYPE_CELLULAR)
cellular_networks->push_back(CellularNetwork(service));
}
DLOG(INFO) << "Remembered networks:";
for (int i = 0; i < system->remembered_service_size; i++) {
const ServiceInfo& service = system->remembered_services[i];
// Only serices marked as auto_connect are considered remembered networks.
// TODO(chocobo): Don't add to remembered service if currently available.
if (service.auto_connect) {
DLOG(INFO) << " (" << service.type <<
") " << service.name <<
" mode=" << service.mode <<
" sec=" << service.security <<
" pass=" << service.passphrase <<
" id=" << service.identity <<
" certpath=" << service.cert_path <<
" auto=" << service.auto_connect;
if (service.type == TYPE_WIFI)
remembered_wifi_networks->push_back(WifiNetwork(service));
else if (service.type == TYPE_CELLULAR)
remembered_cellular_networks->push_back(CellularNetwork(service));
}
}
}
void NetworkLibraryImpl::Init() {
// First, get the currently available networks. This data is cached
// on the connman side, so the call should be quick.
SystemInfo* system = GetSystemInfo();
if (system) {
LOG(INFO) << "Getting initial CrOS network info.";
UpdateNetworkStatus(system);
FreeSystemInfo(system);
}
LOG(INFO) << "Registering for network status updates.";
// Now, register to receive updates on network status.
network_status_connection_ = MonitorNetwork(&NetworkStatusChangedHandler,
this);
}
bool NetworkLibraryImpl::GetWifiNetworkByName(const std::string& name,
WifiNetwork* wifi) {
for (size_t i = 0; i < wifi_networks_.size(); ++i) {
if (wifi_networks_[i].name().compare(name) == 0) {
*wifi = wifi_networks_[i];
return true;
}
}
return false;
}
void NetworkLibraryImpl::EnableNetworkDeviceType(ConnectionType device,
bool enable) {
if (!CrosLibrary::Get()->EnsureLoaded())
return;
// If network device is already enabled/disabled, then don't do anything.
if (enable && (enabled_devices_ & (1 << device))) {
LOG(WARNING) << "Trying to enable a device that's already enabled: "
<< device;
return;
}
if (!enable && !(enabled_devices_ & (1 << device))) {
LOG(WARNING) << "Trying to disable a device that's already disabled: "
<< device;
return;
}
EnableNetworkDevice(device, enable);
}
void NetworkLibraryImpl::UpdateNetworkStatus(SystemInfo* system) {
// Make sure we run on UI thread.
if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this,
&NetworkLibraryImpl::UpdateNetworkStatus, system));
return;
}
wifi_networks_.clear();
cellular_networks_.clear();
remembered_wifi_networks_.clear();
remembered_cellular_networks_.clear();
ParseSystem(system, ðernet_, &wifi_networks_, &cellular_networks_,
&remembered_wifi_networks_, &remembered_cellular_networks_);
wifi_ = WifiNetwork();
for (size_t i = 0; i < wifi_networks_.size(); i++) {
if (wifi_networks_[i].connecting_or_connected()) {
wifi_ = wifi_networks_[i];
break; // There is only one connected or connecting wifi network.
}
}
cellular_ = CellularNetwork();
for (size_t i = 0; i < cellular_networks_.size(); i++) {
if (cellular_networks_[i].connecting_or_connected()) {
cellular_ = cellular_networks_[i];
break; // There is only one connected or connecting cellular network.
}
}
available_devices_ = system->available_technologies;
enabled_devices_ = system->enabled_technologies;
connected_devices_ = system->connected_technologies;
offline_mode_ = system->offline_mode;
FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this));
}
void NetworkLibraryImpl::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,
&NetworkLibraryImpl::NetworkTrafficTimerFired);
}
}
void NetworkLibraryImpl:: NetworkTrafficTimerFired() {
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &NetworkLibraryImpl::NotifyNetworkTraffic,
traffic_type_));
// Reset traffic type so that we don't send the same data next time.
traffic_type_ = 0;
}
void NetworkLibraryImpl::NotifyNetworkTraffic(int traffic_type) {
FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type));
}
bool NetworkLibraryImpl::Connected() const {
return ethernet_connected() || wifi_connected() || cellular_connected();
}
bool NetworkLibraryImpl::Connecting() const {
return ethernet_connecting() || wifi_connecting() || cellular_connecting();
}
const std::string& NetworkLibraryImpl::IPAddress() const {
// Returns highest priority IP address.
if (ethernet_connected())
return ethernet_.ip_address();
if (wifi_connected())
return wifi_.ip_address();
if (cellular_connected())
return cellular_.ip_address();
return ethernet_.ip_address();
}
} // namespace chromeos
|