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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
|
// 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 <map>
#include "app/l10n_util.h"
#include "base/stl_util-inl.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "grit/generated_resources.h"
namespace {
// FlimFlam may send multiple notifications for single network change.
// We wait small amount of time before retrieving the status to
// avoid send multiple sync request to flim flam.
const int kNetworkUpdateDelayMs = 50;
} // namespace
namespace chromeos {
namespace {
// TODO(ers) These string constants and Parse functions are copied
// straight out of libcros:chromeos_network.cc. Fix this by moving
// all handling of properties into libcros.
// Network service properties we are interested in monitoring
static const char* kIsActiveProperty = "IsActive";
static const char* kStateProperty = "State";
static const char* kSignalStrengthProperty = "Strength";
static const char* kActivationStateProperty = "Cellular.ActivationState";
static const char* kNetworkTechnologyProperty = "Cellular.NetworkTechnology";
static const char* kPaymentURLProperty = "Cellular.OlpUrl";
static const char* kRestrictedPoolProperty = "Cellular.RestrictedPool";
static const char* kRoamingStateProperty = "Cellular.RoamingState";
// Connman state options.
static const char* kStateIdle = "idle";
static const char* kStateCarrier = "carrier";
static const char* kStateAssociation = "association";
static const char* kStateConfiguration = "configuration";
static const char* kStateReady = "ready";
static const char* kStateDisconnect = "disconnect";
static const char* kStateFailure = "failure";
static const char* kStateActivationFailure = "activation-failure";
// Connman activation state options
static const char* kActivationStateActivated = "activated";
static const char* kActivationStateActivating = "activating";
static const char* kActivationStateNotActivated = "not-activated";
static const char* kActivationStatePartiallyActivated = "partially-activated";
static const char* kActivationStateUnknown = "unknown";
// Connman network technology options.
static const char* kNetworkTechnology1Xrtt = "1xRTT";
static const char* kNetworkTechnologyEvdo = "EVDO";
static const char* kNetworkTechnologyGprs = "GPRS";
static const char* kNetworkTechnologyEdge = "EDGE";
static const char* kNetworkTechnologyUmts = "UMTS";
static const char* kNetworkTechnologyHspa = "HSPA";
static const char* kNetworkTechnologyHspaPlus = "HSPA+";
static const char* kNetworkTechnologyLte = "LTE";
static const char* kNetworkTechnologyLteAdvanced = "LTE Advanced";
// Connman roaming state options
static const char* kRoamingStateHome = "home";
static const char* kRoamingStateRoaming = "roaming";
static const char* kRoamingStateUnknown = "unknown";
static ConnectionState ParseState(const std::string& state) {
if (state == kStateIdle)
return STATE_IDLE;
if (state == kStateCarrier)
return STATE_CARRIER;
if (state == kStateAssociation)
return STATE_ASSOCIATION;
if (state == kStateConfiguration)
return STATE_CONFIGURATION;
if (state == kStateReady)
return STATE_READY;
if (state == kStateDisconnect)
return STATE_DISCONNECT;
if (state == kStateFailure)
return STATE_FAILURE;
if (state == kStateActivationFailure)
return STATE_ACTIVATION_FAILURE;
return STATE_UNKNOWN;
}
static ActivationState ParseActivationState(
const std::string& activation_state) {
if (activation_state == kActivationStateActivated)
return ACTIVATION_STATE_ACTIVATED;
if (activation_state == kActivationStateActivating)
return ACTIVATION_STATE_ACTIVATING;
if (activation_state == kActivationStateNotActivated)
return ACTIVATION_STATE_NOT_ACTIVATED;
if (activation_state == kActivationStateUnknown)
return ACTIVATION_STATE_UNKNOWN;
if (activation_state == kActivationStatePartiallyActivated)
return ACTIVATION_STATE_PARTIALLY_ACTIVATED;
return ACTIVATION_STATE_UNKNOWN;
}
static NetworkTechnology ParseNetworkTechnology(
const std::string& technology) {
if (technology == kNetworkTechnology1Xrtt)
return NETWORK_TECHNOLOGY_1XRTT;
if (technology == kNetworkTechnologyEvdo)
return NETWORK_TECHNOLOGY_EVDO;
if (technology == kNetworkTechnologyGprs)
return NETWORK_TECHNOLOGY_GPRS;
if (technology == kNetworkTechnologyEdge)
return NETWORK_TECHNOLOGY_EDGE;
if (technology == kNetworkTechnologyUmts)
return NETWORK_TECHNOLOGY_UMTS;
if (technology == kNetworkTechnologyHspa)
return NETWORK_TECHNOLOGY_HSPA;
if (technology == kNetworkTechnologyHspaPlus)
return NETWORK_TECHNOLOGY_HSPA_PLUS;
if (technology == kNetworkTechnologyLte)
return NETWORK_TECHNOLOGY_LTE;
if (technology == kNetworkTechnologyLteAdvanced)
return NETWORK_TECHNOLOGY_LTE_ADVANCED;
return NETWORK_TECHNOLOGY_UNKNOWN;
}
static NetworkRoamingState ParseRoamingState(
const std::string& roaming_state) {
if (roaming_state == kRoamingStateHome)
return ROAMING_STATE_HOME;
if (roaming_state == kRoamingStateRoaming)
return ROAMING_STATE_ROAMING;
if (roaming_state == kRoamingStateUnknown)
return ROAMING_STATE_UNKNOWN;
return ROAMING_STATE_UNKNOWN;
}
}
// 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(base::IntToString(wireless->auto_connect())) +
WrapWithTD(base::IntToString(wireless->strength()));
if (network->type() == TYPE_WIFI) {
WifiNetwork* wifi = static_cast<WifiNetwork*>(network);
str += WrapWithTD(wifi->GetEncryptionString()) +
WrapWithTD(std::string(wifi->passphrase().length(), '*')) +
WrapWithTD(wifi->identity()) + WrapWithTD(wifi->cert_path());
}
}
str += WrapWithTD(network->GetStateString()) +
WrapWithTD(network->failed() ? network->GetErrorString() : "") +
WrapWithTD(network->ip_address());
return str;
}
// Safe string constructor since we can't rely on non NULL pointers
// for string values from libcros.
static std::string SafeString(const char* s) {
return s ? std::string(s) : std::string();
}
static bool EnsureCrosLoaded() {
if (!CrosLibrary::Get()->EnsureLoaded()) {
return false;
} else {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
LOG(ERROR) << "chromeos_library calls made from non UI thread!";
NOTREACHED();
}
return true;
}
}
////////////////////////////////////////////////////////////////////////////////
// Network
Network::Network(const Network& network) {
service_path_ = network.service_path();
device_path_ = network.device_path();
ip_address_ = network.ip_address();
type_ = network.type();
state_ = network.state();
error_ = network.error();
}
void Network::Clear() {
state_ = STATE_UNKNOWN;
error_ = ERROR_UNKNOWN;
service_path_.clear();
device_path_.clear();
ip_address_.clear();
is_active_ = false;
}
Network::Network(const ServiceInfo* service) {
type_ = service->type;
state_ = service->state;
error_ = service->error;
service_path_ = SafeString(service->service_path);
device_path_ = SafeString(service->device_path);
is_active_ = service->is_active;
ip_address_.clear();
// If connected, get ip config.
if (EnsureCrosLoaded() && 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);
}
}
}
// Used by GetHtmlInfo() which is called from the about:network handler.
std::string Network::GetStateString() const {
switch (state_) {
case STATE_UNKNOWN:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_UNKNOWN);
case STATE_IDLE:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_IDLE);
case STATE_CARRIER:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_CARRIER);
case STATE_ASSOCIATION:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_ASSOCIATION);
case STATE_CONFIGURATION:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_CONFIGURATION);
case STATE_READY:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_READY);
case STATE_DISCONNECT:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_DISCONNECT);
case STATE_FAILURE:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_FAILURE);
case STATE_ACTIVATION_FAILURE:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_STATE_ACTIVATION_FAILURE);
default:
// Usually no default, but changes to libcros may add states.
break;
}
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_UNRECOGNIZED);
}
std::string Network::GetErrorString() const {
switch (error_) {
case ERROR_UNKNOWN:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_UNKNOWN);
case ERROR_OUT_OF_RANGE:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_OUT_OF_RANGE);
case ERROR_PIN_MISSING:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_PIN_MISSING);
case ERROR_DHCP_FAILED:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_DHCP_FAILED);
case ERROR_CONNECT_FAILED:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ERROR_CONNECT_FAILED);
case ERROR_BAD_PASSPHRASE:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ERROR_BAD_PASSPHRASE);
case ERROR_BAD_WEPKEY:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_BAD_WEPKEY);
case ERROR_ACTIVATION_FAILED:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ERROR_ACTIVATION_FAILED);
case ERROR_NEED_EVDO:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_NEED_EVDO);
case ERROR_NEED_HOME_NETWORK:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ERROR_NEED_HOME_NETWORK);
case ERROR_OTASP_FAILED:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_OTASP_FAILED);
case ERROR_AAA_FAILED:
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_ERROR_AAA_FAILED);
default:
// Usually no default, but changes to libcros may add errors.
break;
}
return l10n_util::GetStringUTF8(IDS_CHROMEOS_NETWORK_STATE_UNRECOGNIZED);
}
////////////////////////////////////////////////////////////////////////////////
// WirelessNetwork
WirelessNetwork::WirelessNetwork(const WirelessNetwork& network)
: Network(network) {
name_ = network.name();
strength_ = network.strength();
auto_connect_ = network.auto_connect();
favorite_ = network.favorite();
}
WirelessNetwork::WirelessNetwork(const ServiceInfo* service)
: Network(service) {
name_ = SafeString(service->name);
strength_ = service->strength;
auto_connect_ = service->auto_connect;
favorite_ = service->favorite;
}
void WirelessNetwork::Clear() {
Network::Clear();
name_.clear();
strength_ = 0;
auto_connect_ = false;
favorite_ = false;
}
////////////////////////////////////////////////////////////////////////////////
// CellularNetwork
CellularNetwork::CellularNetwork()
: WirelessNetwork(),
activation_state_(ACTIVATION_STATE_UNKNOWN),
network_technology_(NETWORK_TECHNOLOGY_UNKNOWN),
roaming_state_(ROAMING_STATE_UNKNOWN),
restricted_pool_(false),
prl_version_(0) {
type_ = TYPE_CELLULAR;
}
CellularNetwork::CellularNetwork(const CellularNetwork& network)
: WirelessNetwork(network) {
activation_state_ = network.activation_state();
network_technology_ = network.network_technology();
roaming_state_ = network.roaming_state();
restricted_pool_ = network.restricted_pool();
service_name_ = network.service_name();
operator_name_ = network.operator_name();
operator_code_ = network.operator_code();
payment_url_ = network.payment_url();
meid_ = network.meid();
imei_ = network.imei();
imsi_ = network.imsi();
esn_ = network.esn();
mdn_ = network.mdn();
min_ = network.min();
model_id_ = network.model_id();
manufacturer_ = network.manufacturer();
firmware_revision_ = network.firmware_revision();
hardware_revision_ = network.hardware_revision();
last_update_ = network.last_update();
prl_version_ = network.prl_version();
type_ = TYPE_CELLULAR;
}
CellularNetwork::CellularNetwork(const ServiceInfo* service)
: WirelessNetwork(service) {
service_name_ = SafeString(service->name);
activation_state_ = service->activation_state;
network_technology_ = service->network_technology;
roaming_state_ = service->roaming_state;
restricted_pool_ = service->restricted_pool;
// Carrier Info
if (service->carrier_info) {
operator_name_ = SafeString(service->carrier_info->operator_name);
operator_code_ = SafeString(service->carrier_info->operator_code);
payment_url_ = SafeString(service->carrier_info->payment_url);
}
// Device Info
if (service->device_info) {
meid_ = SafeString(service->device_info->MEID);
imei_ = SafeString(service->device_info->IMEI);
imsi_ = SafeString(service->device_info->IMSI);
esn_ = SafeString(service->device_info->ESN);
mdn_ = SafeString(service->device_info->MDN);
min_ = SafeString(service->device_info->MIN);
model_id_ = SafeString(service->device_info->model_id);
manufacturer_ = SafeString(service->device_info->manufacturer);
firmware_revision_ = SafeString(service->device_info->firmware_revision);
hardware_revision_ = SafeString(service->device_info->hardware_revision);
last_update_ = SafeString(service->device_info->last_update);
prl_version_ = service->device_info->PRL_version;
}
type_ = TYPE_CELLULAR;
}
CellularNetwork::~CellularNetwork() {
}
bool CellularNetwork::StartActivation() const {
if (!EnsureCrosLoaded())
return false;
return ActivateCellularModem(service_path_.c_str(), NULL);
}
void CellularNetwork::Clear() {
WirelessNetwork::Clear();
activation_state_ = ACTIVATION_STATE_UNKNOWN;
roaming_state_ = ROAMING_STATE_UNKNOWN;
network_technology_ = NETWORK_TECHNOLOGY_UNKNOWN;
restricted_pool_ = false;
service_name_.clear();
operator_name_.clear();
operator_code_.clear();
payment_url_.clear();
meid_.clear();
imei_.clear();
imsi_.clear();
esn_.clear();
mdn_.clear();
min_.clear();
model_id_.clear();
manufacturer_.clear();
firmware_revision_.clear();
hardware_revision_.clear();
last_update_.clear();
prl_version_ = 0;
}
bool CellularNetwork::is_gsm() const {
return network_technology_ != NETWORK_TECHNOLOGY_EVDO &&
network_technology_ != NETWORK_TECHNOLOGY_1XRTT &&
network_technology_ != NETWORK_TECHNOLOGY_UNKNOWN;
}
CellularNetwork::DataLeft CellularNetwork::data_left() const {
if (data_plans_.empty())
return DATA_NORMAL;
const CellularDataPlan& plan(data_plans_[0]);
if (plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED) {
base::TimeDelta remaining = plan.plan_end_time - plan.update_time;
if (remaining <= base::TimeDelta::FromSeconds(0))
return DATA_NONE;
else if (remaining <=
base::TimeDelta::FromSeconds(kCellularDataVeryLowSecs))
return DATA_VERY_LOW;
else if (remaining <= base::TimeDelta::FromSeconds(kCellularDataLowSecs))
return DATA_LOW;
else
return DATA_NORMAL;
} else if (plan.plan_type == CELLULAR_DATA_PLAN_METERED_PAID ||
plan.plan_type == CELLULAR_DATA_PLAN_METERED_BASE) {
int64 remaining = plan.plan_data_bytes - plan.data_bytes_used;
if (remaining <= 0)
return DATA_NONE;
else if (remaining <= kCellularDataVeryLowBytes)
return DATA_VERY_LOW;
else if (remaining <= kCellularDataLowBytes)
return DATA_LOW;
else
return DATA_NORMAL;
}
return DATA_NORMAL;
}
std::string CellularNetwork::GetNetworkTechnologyString() const {
// No need to localize these cellular technology abbreviations.
switch (network_technology_) {
case NETWORK_TECHNOLOGY_1XRTT:
return "1xRTT";
break;
case NETWORK_TECHNOLOGY_EVDO:
return "EVDO";
break;
case NETWORK_TECHNOLOGY_GPRS:
return "GPRS";
break;
case NETWORK_TECHNOLOGY_EDGE:
return "EDGE";
break;
case NETWORK_TECHNOLOGY_UMTS:
return "UMTS";
break;
case NETWORK_TECHNOLOGY_HSPA:
return "HSPA";
break;
case NETWORK_TECHNOLOGY_HSPA_PLUS:
return "HSPA Plus";
break;
case NETWORK_TECHNOLOGY_LTE:
return "LTE";
break;
case NETWORK_TECHNOLOGY_LTE_ADVANCED:
return "LTE Advanced";
break;
default:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_CELLULAR_TECHNOLOGY_UNKNOWN);
break;
}
}
std::string CellularNetwork::ActivationStateToString(
ActivationState activation_state) {
switch (activation_state) {
case ACTIVATION_STATE_ACTIVATED:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ACTIVATION_STATE_ACTIVATED);
break;
case ACTIVATION_STATE_ACTIVATING:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ACTIVATION_STATE_ACTIVATING);
break;
case ACTIVATION_STATE_NOT_ACTIVATED:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ACTIVATION_STATE_NOT_ACTIVATED);
break;
case ACTIVATION_STATE_PARTIALLY_ACTIVATED:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ACTIVATION_STATE_PARTIALLY_ACTIVATED);
break;
default:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ACTIVATION_STATE_UNKNOWN);
break;
}
}
std::string CellularNetwork::GetActivationStateString() const {
return ActivationStateToString(this->activation_state_);
}
std::string CellularNetwork::GetRoamingStateString() const {
switch (this->roaming_state_) {
case ROAMING_STATE_HOME:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ROAMING_STATE_HOME);
break;
case ROAMING_STATE_ROAMING:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ROAMING_STATE_ROAMING);
break;
default:
return l10n_util::GetStringUTF8(
IDS_CHROMEOS_NETWORK_ROAMING_STATE_UNKNOWN);
break;
};
}
////////////////////////////////////////////////////////////////////////////////
// WifiNetwork
WifiNetwork::WifiNetwork()
: WirelessNetwork(),
encryption_(SECURITY_NONE) {
type_ = TYPE_WIFI;
}
WifiNetwork::WifiNetwork(const WifiNetwork& network)
: WirelessNetwork(network) {
encryption_ = network.encryption();
passphrase_ = network.passphrase();
passphrase_required_ = network.passphrase_required();
identity_ = network.identity();
cert_path_ = network.cert_path();
}
WifiNetwork::WifiNetwork(const ServiceInfo* service)
: WirelessNetwork(service) {
encryption_ = service->security;
passphrase_ = SafeString(service->passphrase);
// TODO(stevenjb): Remove this once flimflam is setting passphrase_required
// correctly: http://crosbug.com/8830.
if (service->state == chromeos::STATE_FAILURE &&
service->security != chromeos::SECURITY_NONE)
passphrase_required_ = true;
else
passphrase_required_ = service->passphrase_required;
identity_ = SafeString(service->identity);
cert_path_ = SafeString(service->cert_path);
type_ = TYPE_WIFI;
}
void WifiNetwork::Clear() {
WirelessNetwork::Clear();
encryption_ = SECURITY_NONE;
passphrase_.clear();
identity_.clear();
cert_path_.clear();
}
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";
}
// Parse 'path' to determine if the certificate is stored in a pkcs#11 device.
// flimflam recognizes the string "SETTINGS:" to specify authentication
// parameters. 'key_id=' indicates that the certificate is stored in a pkcs#11
// device. See src/third_party/flimflam/files/doc/service-api.txt.
bool WifiNetwork::IsCertificateLoaded() const {
static const std::string settings_string("SETTINGS:");
static const std::string pkcs11_key("key_id");
if (cert_path_.find(settings_string) == 0) {
std::string::size_type idx = cert_path_.find(pkcs11_key);
if (idx != std::string::npos)
idx = cert_path_.find_first_not_of(kWhitespaceASCII,
idx + pkcs11_key.length());
if (idx != std::string::npos && cert_path_[idx] == '=')
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// NetworkLibrary
class NetworkLibraryImpl : public NetworkLibrary {
public:
NetworkLibraryImpl()
: network_manager_monitor_(NULL),
data_plan_monitor_(NULL),
ethernet_(NULL),
wifi_(NULL),
cellular_(NULL),
available_devices_(0),
enabled_devices_(0),
connected_devices_(0),
offline_mode_(false),
update_task_(NULL) {
if (EnsureCrosLoaded()) {
Init();
network_manager_monitor_ =
MonitorNetworkManager(&NetworkManagerStatusChangedHandler,
this);
data_plan_monitor_ = MonitorCellularDataPlan(&DataPlanUpdateHandler,
this);
} else {
InitTestData();
}
}
~NetworkLibraryImpl() {
network_manager_observers_.Clear();
if (network_manager_monitor_)
DisconnectPropertyChangeMonitor(network_manager_monitor_);
data_plan_observers_.Clear();
if (data_plan_monitor_)
DisconnectDataPlanUpdateMonitor(data_plan_monitor_);
STLDeleteValues(&network_observers_);
ClearNetworks();
}
virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) {
if (!network_manager_observers_.HasObserver(observer))
network_manager_observers_.AddObserver(observer);
}
void NetworkStatusChanged() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (update_task_) {
update_task_->Cancel();
}
update_task_ =
NewRunnableMethod(this,
&NetworkLibraryImpl::UpdateNetworkManagerStatus);
BrowserThread::PostDelayedTask(
BrowserThread::UI, FROM_HERE, update_task_,
kNetworkUpdateDelayMs);
}
virtual void RemoveNetworkManagerObserver(NetworkManagerObserver* observer) {
network_manager_observers_.RemoveObserver(observer);
}
virtual void AddNetworkObserver(const std::string& service_path,
NetworkObserver* observer) {
DCHECK(observer);
if (!EnsureCrosLoaded())
return;
// First, add the observer to the callback map.
NetworkObserverMap::iterator iter = network_observers_.find(service_path);
NetworkObserverList* oblist;
if (iter != network_observers_.end()) {
oblist = iter->second;
} else {
std::pair<NetworkObserverMap::iterator, bool> inserted =
network_observers_.insert(
std::make_pair<std::string, NetworkObserverList*>(
service_path,
new NetworkObserverList(this, service_path)));
oblist = inserted.first->second;
}
if (!oblist->HasObserver(observer))
oblist->AddObserver(observer);
}
virtual void RemoveNetworkObserver(const std::string& service_path,
NetworkObserver* observer) {
DCHECK(observer);
DCHECK(service_path.size());
NetworkObserverMap::iterator map_iter =
network_observers_.find(service_path);
if (map_iter != network_observers_.end()) {
map_iter->second->RemoveObserver(observer);
if (!map_iter->second->size()) {
delete map_iter->second;
network_observers_.erase(map_iter++);
}
}
}
virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) {
DCHECK(observer);
NetworkObserverMap::iterator map_iter = network_observers_.begin();
while (map_iter != network_observers_.end()) {
map_iter->second->RemoveObserver(observer);
if (!map_iter->second->size()) {
delete map_iter->second;
network_observers_.erase(map_iter++);
} else {
++map_iter;
}
}
}
virtual void AddCellularDataPlanObserver(CellularDataPlanObserver* observer) {
if (!data_plan_observers_.HasObserver(observer))
data_plan_observers_.AddObserver(observer);
}
virtual void RemoveCellularDataPlanObserver(
CellularDataPlanObserver* observer) {
data_plan_observers_.RemoveObserver(observer);
}
virtual EthernetNetwork* ethernet_network() { return ethernet_; }
virtual bool ethernet_connecting() const {
return ethernet_ ? ethernet_->connecting() : false;
}
virtual bool ethernet_connected() const {
return ethernet_ ? ethernet_->connected() : false;
}
virtual WifiNetwork* wifi_network() { return wifi_; }
virtual bool wifi_connecting() const {
return wifi_ ? wifi_->connecting() : false;
}
virtual bool wifi_connected() const {
return wifi_ ? wifi_->connected() : false;
}
virtual CellularNetwork* cellular_network() { return cellular_; }
virtual bool cellular_connecting() const {
return cellular_ ? cellular_->connecting() : false;
}
virtual bool cellular_connected() const {
return cellular_ ? cellular_->connected() : false;
}
bool Connected() const {
return ethernet_connected() || wifi_connected() || cellular_connected();
}
bool Connecting() const {
return ethernet_connecting() || wifi_connecting() || cellular_connecting();
}
const std::string& IPAddress() const {
// Returns IP address for the active network.
const Network* active = active_network();
if (active != NULL)
return active->ip_address();
if (ethernet_)
return ethernet_->ip_address();
static std::string null_address("0.0.0.0");
return null_address;
}
virtual const WifiNetworkVector& wifi_networks() const {
return wifi_networks_;
}
virtual const WifiNetworkVector& remembered_wifi_networks() const {
return remembered_wifi_networks_;
}
virtual const CellularNetworkVector& cellular_networks() const {
return cellular_networks_;
}
/////////////////////////////////////////////////////////////////////////////
virtual WifiNetwork* FindWifiNetworkByPath(
const std::string& path) {
return GetWirelessNetworkByPath(wifi_networks_, path);
}
virtual CellularNetwork* FindCellularNetworkByPath(
const std::string& path) {
return GetWirelessNetworkByPath(cellular_networks_, path);
}
virtual void RequestWifiScan() {
if (EnsureCrosLoaded()) {
RequestScan(TYPE_WIFI);
}
}
virtual bool GetWifiAccessPoints(WifiAccessPointVector* result) {
if (!EnsureCrosLoaded())
return false;
DeviceNetworkList* network_list = GetDeviceNetworkList();
if (network_list == NULL)
return false;
result->clear();
result->reserve(network_list->network_size);
const base::Time now = base::Time::Now();
for (size_t i = 0; i < network_list->network_size; ++i) {
DCHECK(network_list->networks[i].address);
DCHECK(network_list->networks[i].name);
WifiAccessPoint ap;
ap.mac_address = SafeString(network_list->networks[i].address);
ap.name = SafeString(network_list->networks[i].name);
ap.timestamp = now -
base::TimeDelta::FromSeconds(network_list->networks[i].age_seconds);
ap.signal_strength = network_list->networks[i].strength;
ap.channel = network_list->networks[i].channel;
result->push_back(ap);
}
FreeDeviceNetworkList(network_list);
return true;
}
virtual bool ConnectToWifiNetwork(const WifiNetwork* network,
const std::string& password,
const std::string& identity,
const std::string& certpath) {
DCHECK(network);
if (!EnsureCrosLoaded())
return true; // No library loaded, don't trigger a retry attempt.
// TODO(ers) make wifi the highest priority service type
if (ConnectToNetworkWithCertInfo(network->service_path().c_str(),
password.empty() ? NULL : password.c_str(),
identity.empty() ? NULL : identity.c_str(),
certpath.empty() ? NULL : certpath.c_str())) {
// Update local cache and notify listeners.
WifiNetwork* wifi = GetWirelessNetworkByPath(
wifi_networks_, network->service_path());
if (wifi) {
wifi->set_passphrase(password);
wifi->set_identity(identity);
wifi->set_cert_path(certpath);
wifi->set_connecting(true);
wifi_ = wifi;
}
NotifyNetworkManagerChanged();
return true;
} else {
return false; // Immediate failure.
}
}
virtual bool ConnectToWifiNetwork(ConnectionSecurity security,
const std::string& ssid,
const std::string& password,
const std::string& identity,
const std::string& certpath,
bool auto_connect) {
if (!EnsureCrosLoaded())
return true; // No library loaded, don't trigger a retry attempt.
// First create a service from hidden network.
ServiceInfo* service = GetWifiService(ssid.c_str(), security);
if (service) {
// Set auto-connect.
SetAutoConnect(service->service_path, auto_connect);
// Now connect to that service.
// TODO(ers) make wifi the highest priority service type
bool res = ConnectToNetworkWithCertInfo(
service->service_path,
password.empty() ? NULL : password.c_str(),
identity.empty() ? NULL : identity.c_str(),
certpath.empty() ? NULL : certpath.c_str());
// Clean up ServiceInfo object.
FreeServiceInfo(service);
return res;
} else {
LOG(WARNING) << "Cannot find hidden network: " << ssid;
// TODO(chocobo): Show error message.
return false; // Immediate failure.
}
}
virtual bool ConnectToCellularNetwork(const CellularNetwork* network) {
DCHECK(network);
if (!EnsureCrosLoaded())
return true; // No library loaded, don't trigger a retry attempt.
// TODO(ers) make cellular the highest priority service type
if (network && ConnectToNetwork(network->service_path().c_str(), NULL)) {
// Update local cache and notify listeners.
CellularNetwork* cellular = GetWirelessNetworkByPath(
cellular_networks_, network->service_path());
if (cellular) {
cellular->set_connecting(true);
cellular_ = cellular;
}
NotifyNetworkManagerChanged();
return true;
} else {
return false; // Immediate failure.
}
}
virtual void RefreshCellularDataPlans(const CellularNetwork* network) {
DCHECK(network);
if (!EnsureCrosLoaded() || !network)
return;
RequestCellularDataPlanUpdate(network->service_path().c_str());
}
virtual void DisconnectFromWirelessNetwork(const WirelessNetwork* network) {
DCHECK(network);
if (!EnsureCrosLoaded() || !network)
return;
// TODO(ers) restore default service type priority ordering?
if (DisconnectFromNetwork(network->service_path().c_str())) {
// Update local cache and notify listeners.
if (network->type() == TYPE_WIFI) {
WifiNetwork* wifi = GetWirelessNetworkByPath(
wifi_networks_, network->service_path());
if (wifi) {
wifi->set_connected(false);
wifi_ = NULL;
}
} else if (network->type() == TYPE_CELLULAR) {
CellularNetwork* cellular = GetWirelessNetworkByPath(
cellular_networks_, network->service_path());
if (cellular) {
cellular->set_connected(false);
cellular_ = NULL;
}
}
NotifyNetworkManagerChanged();
}
}
virtual void SaveCellularNetwork(const CellularNetwork* network) {
DCHECK(network);
// Update the cellular network with libcros.
if (!EnsureCrosLoaded() || !network)
return;
SetAutoConnect(network->service_path().c_str(), network->auto_connect());
}
virtual void SaveWifiNetwork(const WifiNetwork* network) {
DCHECK(network);
// Update the wifi network with libcros.
if (!EnsureCrosLoaded() || !network)
return;
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());
}
virtual void ForgetWifiNetwork(const std::string& service_path) {
if (!EnsureCrosLoaded())
return;
if (DeleteRememberedService(service_path.c_str())) {
// Update local cache and notify listeners.
for (WifiNetworkVector::iterator iter =
remembered_wifi_networks_.begin();
iter != remembered_wifi_networks_.end();
++iter) {
if ((*iter)->service_path() == service_path) {
delete (*iter);
remembered_wifi_networks_.erase(iter);
break;
}
}
NotifyNetworkManagerChanged();
}
}
virtual bool ethernet_available() const {
return available_devices_ & (1 << TYPE_ETHERNET);
}
virtual bool wifi_available() const {
return available_devices_ & (1 << TYPE_WIFI);
}
virtual bool cellular_available() const {
return available_devices_ & (1 << TYPE_CELLULAR);
}
virtual bool ethernet_enabled() const {
return enabled_devices_ & (1 << TYPE_ETHERNET);
}
virtual bool wifi_enabled() const {
return enabled_devices_ & (1 << TYPE_WIFI);
}
virtual bool cellular_enabled() const {
return enabled_devices_ & (1 << TYPE_CELLULAR);
}
virtual bool offline_mode() const { return offline_mode_; }
virtual const Network* active_network() const {
if (ethernet_ && ethernet_->is_active())
return ethernet_;
if (wifi_ && wifi_->is_active())
return wifi_;
if (cellular_ && cellular_->is_active())
return cellular_;
return NULL;
}
virtual void EnableEthernetNetworkDevice(bool enable) {
EnableNetworkDeviceType(TYPE_ETHERNET, enable);
}
virtual void EnableWifiNetworkDevice(bool enable) {
EnableNetworkDeviceType(TYPE_WIFI, enable);
}
virtual void EnableCellularNetworkDevice(bool enable) {
EnableNetworkDeviceType(TYPE_CELLULAR, enable);
}
virtual void EnableOfflineMode(bool enable) {
if (!EnsureCrosLoaded())
return;
// If network device is already enabled/disabled, then don't do anything.
if (enable && offline_mode_) {
VLOG(1) << "Trying to enable offline mode when it's already enabled.";
return;
}
if (!enable && !offline_mode_) {
VLOG(1) << "Trying to disable offline mode when it's already disabled.";
return;
}
if (SetOfflineMode(enable)) {
offline_mode_ = enable;
}
}
virtual NetworkIPConfigVector GetIPConfigs(const std::string& device_path,
std::string* hardware_address) {
hardware_address->clear();
NetworkIPConfigVector ipconfig_vector;
if (EnsureCrosLoaded() && !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));
}
*hardware_address = ipconfig_status->hardware_address;
FreeIPConfigStatus(ipconfig_status);
// Sort the list of ip configs by type.
std::sort(ipconfig_vector.begin(), ipconfig_vector.end());
}
}
return ipconfig_vector;
}
virtual std::string GetHtmlInfo(int refresh) {
std::string output;
output.append("<html><head><title>About Network</title>");
if (refresh > 0)
output.append("<meta http-equiv=\"refresh\" content=\"" +
base::IntToString(refresh) + "\"/>");
output.append("</head><body>");
if (refresh > 0) {
output.append("(Auto-refreshing page every " +
base::IntToString(refresh) + "s)");
} else {
output.append("(To auto-refresh this page: about:network/<secs>)");
}
output.append("<h3>Ethernet:</h3><table border=1>");
if (ethernet_ && ethernet_enabled()) {
output.append("<tr>" + ToHtmlTableHeader(ethernet_) + "</tr>");
output.append("<tr>" + ToHtmlTableRow(ethernet_) + "</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></body></html>");
return output;
}
private:
class NetworkObserverList : public ObserverList<NetworkObserver> {
public:
NetworkObserverList(NetworkLibraryImpl* library,
const std::string& service_path) {
network_monitor_ = MonitorNetworkService(&NetworkStatusChangedHandler,
service_path.c_str(),
library);
}
virtual ~NetworkObserverList() {
if (network_monitor_)
DisconnectPropertyChangeMonitor(network_monitor_);
}
private:
static void NetworkStatusChangedHandler(void* object,
const char* path,
const char* key,
const Value* value) {
NetworkLibraryImpl* networklib = static_cast<NetworkLibraryImpl*>(object);
DCHECK(networklib);
networklib->UpdateNetworkStatus(path, key, value);
}
PropertyChangeMonitor network_monitor_;
};
typedef std::map<std::string, NetworkObserverList*> NetworkObserverMap;
static void NetworkManagerStatusChangedHandler(void* object,
const char* path,
const char* key,
const Value* value) {
NetworkLibraryImpl* networklib = static_cast<NetworkLibraryImpl*>(object);
DCHECK(networklib);
networklib->NetworkStatusChanged();
}
static void DataPlanUpdateHandler(void* object,
const char* modem_service_path,
const CellularDataPlanList* dataplan) {
NetworkLibraryImpl* networklib = static_cast<NetworkLibraryImpl*>(object);
if (!networklib || !networklib->cellular_network()) {
// This might happen if an update is received as we are shutting down.
return;
}
// Store data plan for currently connected cellular network.
if (networklib->cellular_network()->service_path()
.compare(modem_service_path) == 0) {
if (dataplan != NULL) {
networklib->UpdateCellularDataPlan(dataplan);
}
}
}
static void ParseSystem(SystemInfo* system,
EthernetNetwork** ethernet,
WifiNetworkVector* wifi_networks,
CellularNetworkVector* cellular_networks,
WifiNetworkVector* remembered_wifi_networks) {
DVLOG(1) << "ParseSystem:";
DCHECK(!(*ethernet));
for (int i = 0; i < system->service_size; i++) {
const ServiceInfo* service = system->GetServiceInfo(i);
DVLOG(1) << " (" << 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
<< " is_active=" << service->is_active
<< " error=" << service->error;
// Once a connected ethernet service is found, disregard other ethernet
// services that are also found
if (service->type == TYPE_ETHERNET)
(*ethernet) = new EthernetNetwork(service);
else if (service->type == TYPE_WIFI) {
wifi_networks->push_back(new WifiNetwork(service));
} else if (service->type == TYPE_CELLULAR) {
cellular_networks->push_back(new CellularNetwork(service));
}
}
// Create placeholder network for ethernet even if the service is not
// detected at this moment.
if (!(*ethernet))
(*ethernet) = new EthernetNetwork();
DVLOG(1) << "Remembered networks:";
for (int i = 0; i < system->remembered_service_size; i++) {
const ServiceInfo* service = system->GetRememberedServiceInfo(i);
// Only services marked as auto_connect are considered remembered
// networks.
// TODO(chocobo): Don't add to remembered service if currently available.
if (service->auto_connect) {
DVLOG(1) << " (" << 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(new WifiNetwork(service));
}
}
}
}
void Init() {
// First, get the currently available networks. This data is cached
// on the connman side, so the call should be quick.
VLOG(1) << "Getting initial CrOS network info.";
UpdateSystemInfo();
}
void InitTestData() {
ethernet_ = new EthernetNetwork();
ethernet_->set_connected(true);
ethernet_->set_service_path("eth1");
STLDeleteElements(&wifi_networks_);
wifi_networks_.clear();
WifiNetwork* wifi1 = new WifiNetwork();
wifi1->set_service_path("fw1");
wifi1->set_name("Fake Wifi 1");
wifi1->set_strength(90);
wifi1->set_connected(false);
wifi1->set_encryption(SECURITY_NONE);
wifi_networks_.push_back(wifi1);
WifiNetwork* wifi2 = new WifiNetwork();
wifi2->set_service_path("fw2");
wifi2->set_name("Fake Wifi 2");
wifi2->set_strength(70);
wifi2->set_connected(true);
wifi2->set_encryption(SECURITY_WEP);
wifi_networks_.push_back(wifi2);
WifiNetwork* wifi3 = new WifiNetwork();
wifi3->set_service_path("fw3");
wifi3->set_name("Fake Wifi 3");
wifi3->set_strength(50);
wifi3->set_connected(false);
wifi3->set_encryption(SECURITY_WEP);
wifi_networks_.push_back(wifi3);
wifi_ = wifi2;
STLDeleteElements(&cellular_networks_);
cellular_networks_.clear();
CellularNetwork* cellular1 = new CellularNetwork();
cellular1->set_service_path("fc1");
cellular1->set_name("Fake Cellular 1");
cellular1->set_strength(70);
cellular1->set_connected(true);
cellular1->set_activation_state(ACTIVATION_STATE_PARTIALLY_ACTIVATED);
cellular1->set_payment_url(std::string("http://www.google.com"));
cellular_networks_.push_back(cellular1);
cellular_ = cellular1;
remembered_wifi_networks_.clear();
remembered_wifi_networks_.push_back(new WifiNetwork(*wifi2));
int devices = (1 << TYPE_ETHERNET) | (1 << TYPE_WIFI) |
(1 << TYPE_CELLULAR);
available_devices_ = devices;
enabled_devices_ = devices;
connected_devices_ = devices;
offline_mode_ = false;
}
void UpdateSystemInfo() {
if (EnsureCrosLoaded()) {
UpdateNetworkManagerStatus();
}
}
WifiNetwork* GetWifiNetworkByName(const std::string& name) {
for (size_t i = 0; i < wifi_networks_.size(); ++i) {
if (wifi_networks_[i]->name().compare(name) == 0) {
return wifi_networks_[i];
}
}
return NULL;
}
template<typename T> T GetWirelessNetworkByPath(
std::vector<T>& networks, const std::string& path) {
typedef typename std::vector<T>::iterator iter_t;
iter_t iter = std::find_if(networks.begin(), networks.end(),
WirelessNetwork::ServicePathEq(path));
return (iter != networks.end()) ? *iter : NULL;
}
// const version
template<typename T> const T GetWirelessNetworkByPath(
const std::vector<T>& networks, const std::string& path) const {
typedef typename std::vector<T>::const_iterator iter_t;
iter_t iter = std::find_if(networks.begin(), networks.end(),
WirelessNetwork::ServicePathEq(path));
return (iter != networks.end()) ? *iter : NULL;
}
void EnableNetworkDeviceType(ConnectionType device, bool enable) {
if (!EnsureCrosLoaded())
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 NotifyNetworkManagerChanged() {
FOR_EACH_OBSERVER(NetworkManagerObserver,
network_manager_observers_,
OnNetworkManagerChanged(this));
}
void NotifyNetworkChanged(Network* network) {
DCHECK(network);
NetworkObserverMap::const_iterator iter = network_observers_.find(
network->service_path());
if (iter != network_observers_.end()) {
FOR_EACH_OBSERVER(NetworkObserver,
*(iter->second),
OnNetworkChanged(this, network));
} else {
NOTREACHED() <<
"There weren't supposed to be any property change observers of " <<
network->service_path();
}
}
void NotifyCellularDataPlanChanged() {
FOR_EACH_OBSERVER(CellularDataPlanObserver,
data_plan_observers_,
OnCellularDataPlanChanged(this));
}
void UpdateNetworkManagerStatus() {
// Make sure we run on UI thread.
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
update_task_ = NULL;
VLOG(1) << "Updating Network Status";
SystemInfo* system = GetSystemInfo();
if (!system)
return;
std::string prev_cellular_service_path = cellular_ ?
cellular_->service_path() : std::string();
ClearNetworks();
ParseSystem(system, ðernet_, &wifi_networks_, &cellular_networks_,
&remembered_wifi_networks_);
wifi_ = NULL;
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_ = NULL;
for (size_t i = 0; i < cellular_networks_.size(); i++) {
if (cellular_networks_[i]->connecting_or_connected()) {
cellular_ = cellular_networks_[i];
// If new cellular, then request update of the data plan list.
if (cellular_networks_[i]->service_path() !=
prev_cellular_service_path) {
RefreshCellularDataPlans(cellular_);
}
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;
NotifyNetworkManagerChanged();
FreeSystemInfo(system);
}
void UpdateNetworkStatus(const char* path,
const char* key,
const Value* value) {
if (key == NULL || value == NULL)
return;
// Make sure we run on UI thread.
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this,
&NetworkLibraryImpl::UpdateNetworkStatus,
path, key, value));
return;
}
bool boolval = false;
int intval = 0;
std::string stringval;
Network* network;
if (ethernet_->service_path() == path) {
network = ethernet_;
} else {
CellularNetwork* cellular =
GetWirelessNetworkByPath(cellular_networks_, path);
WifiNetwork* wifi =
GetWirelessNetworkByPath(wifi_networks_, path);
if (cellular == NULL && wifi == NULL)
return;
WirelessNetwork* wireless;
if (wifi != NULL)
wireless = static_cast<WirelessNetwork*>(wifi);
else
wireless = static_cast<WirelessNetwork*>(cellular);
if (strcmp(key, kSignalStrengthProperty) == 0) {
if (value->GetAsInteger(&intval))
wireless->set_strength(intval);
} else if (cellular != NULL) {
if (strcmp(key, kRestrictedPoolProperty) == 0) {
if (value->GetAsBoolean(&boolval))
cellular->set_restricted_pool(boolval);
} else if (strcmp(key, kActivationStateProperty) == 0) {
if (value->GetAsString(&stringval))
cellular->set_activation_state(ParseActivationState(stringval));
} else if (strcmp(key, kPaymentURLProperty) == 0) {
if (value->GetAsString(&stringval))
cellular->set_payment_url(stringval);
} else if (strcmp(key, kNetworkTechnologyProperty) == 0) {
if (value->GetAsString(&stringval))
cellular->set_network_technology(
ParseNetworkTechnology(stringval));
} else if (strcmp(key, kRoamingStateProperty) == 0) {
if (value->GetAsString(&stringval))
cellular->set_roaming_state(ParseRoamingState(stringval));
}
}
network = wireless;
}
if (strcmp(key, kIsActiveProperty) == 0) {
if (value->GetAsBoolean(&boolval))
network->set_active(boolval);
} else if (strcmp(key, kStateProperty) == 0) {
if (value->GetAsString(&stringval))
network->set_state(ParseState(stringval));
}
NotifyNetworkChanged(network);
}
void UpdateCellularDataPlan(const CellularDataPlanList* data_plans) {
DCHECK(cellular_);
cellular_->SetDataPlans(data_plans);
NotifyCellularDataPlanChanged();
}
void ClearNetworks() {
if (ethernet_)
delete ethernet_;
ethernet_ = NULL;
wifi_ = NULL;
cellular_ = NULL;
STLDeleteElements(&wifi_networks_);
wifi_networks_.clear();
STLDeleteElements(&cellular_networks_);
cellular_networks_.clear();
STLDeleteElements(&remembered_wifi_networks_);
remembered_wifi_networks_.clear();
}
// Network manager observer list
ObserverList<NetworkManagerObserver> network_manager_observers_;
// Cellular data plan observer list
ObserverList<CellularDataPlanObserver> data_plan_observers_;
// Network observer map
NetworkObserverMap network_observers_;
// For monitoring network manager status changes.
PropertyChangeMonitor network_manager_monitor_;
// For monitoring data plan changes to the connected cellular network.
DataPlanUpdateMonitor data_plan_monitor_;
// The ethernet network.
EthernetNetwork* ethernet_;
// The list of available wifi networks.
WifiNetworkVector wifi_networks_;
// The current connected (or connecting) wifi network.
WifiNetwork* wifi_;
// The remembered wifi networks.
WifiNetworkVector remembered_wifi_networks_;
// The list of available cellular networks.
CellularNetworkVector cellular_networks_;
// The current connected (or connecting) cellular network.
CellularNetwork* cellular_;
// The current available network devices. Bitwise flag of ConnectionTypes.
int available_devices_;
// The current enabled network devices. Bitwise flag of ConnectionTypes.
int enabled_devices_;
// The current connected network devices. Bitwise flag of ConnectionTypes.
int connected_devices_;
bool offline_mode_;
// Delayed task to retrieve the network information.
CancelableTask* update_task_;
DISALLOW_COPY_AND_ASSIGN(NetworkLibraryImpl);
};
class NetworkLibraryStubImpl : public NetworkLibrary {
public:
NetworkLibraryStubImpl()
: ip_address_("1.1.1.1"),
ethernet_(new EthernetNetwork()),
wifi_(NULL),
cellular_(NULL) {
}
~NetworkLibraryStubImpl() { if (ethernet_) delete ethernet_; }
virtual void AddNetworkManagerObserver(NetworkManagerObserver* observer) {}
virtual void RemoveNetworkManagerObserver(NetworkManagerObserver* observer) {}
virtual void AddNetworkObserver(const std::string& service_path,
NetworkObserver* observer) {}
virtual void RemoveNetworkObserver(const std::string& service_path,
NetworkObserver* observer) {}
virtual void RemoveObserverForAllNetworks(NetworkObserver* observer) {}
virtual void AddCellularDataPlanObserver(
CellularDataPlanObserver* observer) {}
virtual void RemoveCellularDataPlanObserver(
CellularDataPlanObserver* observer) {}
virtual EthernetNetwork* ethernet_network() {
return ethernet_;
}
virtual bool ethernet_connecting() const { return false; }
virtual bool ethernet_connected() const { return true; }
virtual WifiNetwork* wifi_network() {
return wifi_;
}
virtual bool wifi_connecting() const { return false; }
virtual bool wifi_connected() const { return false; }
virtual CellularNetwork* cellular_network() {
return cellular_;
}
virtual bool cellular_connecting() const { return false; }
virtual bool cellular_connected() const { return false; }
bool Connected() const { return true; }
bool Connecting() const { return false; }
const std::string& IPAddress() const { return ip_address_; }
virtual const WifiNetworkVector& wifi_networks() const {
return wifi_networks_;
}
virtual const WifiNetworkVector& remembered_wifi_networks() const {
return wifi_networks_;
}
virtual const CellularNetworkVector& cellular_networks() const {
return cellular_networks_;
}
virtual bool has_cellular_networks() const {
return cellular_networks_.begin() != cellular_networks_.end();
}
/////////////////////////////////////////////////////////////////////////////
virtual WifiNetwork* FindWifiNetworkByPath(
const std::string& path) { return NULL; }
virtual CellularNetwork* FindCellularNetworkByPath(
const std::string& path) { return NULL; }
virtual void RequestWifiScan() {}
virtual bool GetWifiAccessPoints(WifiAccessPointVector* result) {
return false;
}
virtual bool ConnectToWifiNetwork(const WifiNetwork* network,
const std::string& password,
const std::string& identity,
const std::string& certpath) {
return true;
}
virtual bool ConnectToWifiNetwork(ConnectionSecurity security,
const std::string& ssid,
const std::string& password,
const std::string& identity,
const std::string& certpath,
bool auto_connect) {
return true;
}
virtual bool ConnectToCellularNetwork(const CellularNetwork* network) {
return true;
}
virtual void RefreshCellularDataPlans(const CellularNetwork* network) {}
virtual void DisconnectFromWirelessNetwork(const WirelessNetwork* network) {}
virtual void SaveCellularNetwork(const CellularNetwork* network) {}
virtual void SaveWifiNetwork(const WifiNetwork* network) {}
virtual void ForgetWifiNetwork(const std::string& service_path) {}
virtual bool ethernet_available() const { return true; }
virtual bool wifi_available() const { return false; }
virtual bool cellular_available() const { return false; }
virtual bool ethernet_enabled() const { return true; }
virtual bool wifi_enabled() const { return false; }
virtual bool cellular_enabled() const { return false; }
virtual const Network* active_network() const { return NULL; }
virtual bool offline_mode() const { return false; }
virtual void EnableEthernetNetworkDevice(bool enable) {}
virtual void EnableWifiNetworkDevice(bool enable) {}
virtual void EnableCellularNetworkDevice(bool enable) {}
virtual void EnableOfflineMode(bool enable) {}
virtual NetworkIPConfigVector GetIPConfigs(const std::string& device_path,
std::string* hardware_address) {
hardware_address->clear();
return NetworkIPConfigVector();
}
virtual std::string GetHtmlInfo(int refresh) { return std::string(); }
private:
std::string ip_address_;
EthernetNetwork* ethernet_;
WifiNetwork* wifi_;
CellularNetwork* cellular_;
WifiNetworkVector wifi_networks_;
CellularNetworkVector cellular_networks_;
};
// static
NetworkLibrary* NetworkLibrary::GetImpl(bool stub) {
if (stub)
return new NetworkLibraryStubImpl();
else
return new NetworkLibraryImpl();
}
} // namespace chromeos
// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::NetworkLibraryImpl);
|