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
|
// 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 "chrome/browser/browser_thread.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "grit/generated_resources.h"
namespace chromeos {
// 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();
}
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);
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;
}
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;
CellularDataPlan plan = data_plans_[0];
if (plan.plan_type == CELLULAR_DATA_PLAN_UNLIMITED) {
int64 remaining = plan.plan_end_time - plan.update_time;
if (remaining <= 0)
return DATA_NONE;
else if (remaining <= kCellularDataVeryLowSecs)
return DATA_VERY_LOW;
else if (remaining <= 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();
identity_ = network.identity();
cert_path_ = network.cert_path();
}
WifiNetwork::WifiNetwork(const ServiceInfo* service)
: WirelessNetwork(service) {
encryption_ = service->security;
passphrase_ = SafeString(service->passphrase);
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_status_connection_(NULL),
data_plan_monitor_(NULL),
ethernet_(NULL),
wifi_(NULL),
cellular_(NULL),
available_devices_(0),
enabled_devices_(0),
connected_devices_(0),
offline_mode_(false) {
if (EnsureCrosLoaded()) {
Init();
} else {
InitTestData();
}
}
~NetworkLibraryImpl() {
if (network_status_connection_) {
DisconnectMonitorNetwork(network_status_connection_);
}
if (data_plan_monitor_) {
DisconnectDataPlanUpdateMonitor(data_plan_monitor_);
}
// DCHECK(!observers_.size());
DCHECK(!property_observers_.size());
STLDeleteValues(&property_observers_);
ClearNetworks();
}
void AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
virtual void AddProperyObserver(const char* service_path,
PropertyObserver* observer) {
DCHECK(service_path);
DCHECK(observer);
if (!EnsureCrosLoaded())
return;
// First, add the observer to the callback map.
PropertyChangeObserverMap::iterator iter = property_observers_.find(
std::string(service_path));
if (iter != property_observers_.end()) {
iter->second->AddObserver(observer);
} else {
std::pair<PropertyChangeObserverMap::iterator, bool> inserted =
property_observers_.insert(
std::pair<std::string, PropertyObserverList*>(
std::string(service_path),
new PropertyObserverList(this, service_path)));
inserted.first->second->AddObserver(observer);
}
}
virtual void RemoveProperyObserver(PropertyObserver* observer) {
DCHECK(observer);
PropertyChangeObserverMap::iterator map_iter =
property_observers_.begin();
while (map_iter != property_observers_.end()) {
map_iter->second->RemoveObserver(observer);
if (!map_iter->second->size()) {
delete map_iter->second;
property_observers_.erase(map_iter++);
} else {
++map_iter;
}
}
}
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 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();
}
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 void ConnectToWifiNetwork(const WifiNetwork* network,
const std::string& password,
const std::string& identity,
const std::string& certpath) {
DCHECK(network);
if (!EnsureCrosLoaded())
return;
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;
}
NotifyNetworkChanged();
}
}
virtual void ConnectToWifiNetwork(const std::string& ssid,
const std::string& password,
const std::string& identity,
const std::string& certpath,
bool auto_connect) {
if (!EnsureCrosLoaded())
return;
// First create a service from hidden network.
ServiceInfo* service = GetWifiService(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 : password.c_str(),
identity.empty() ? NULL : identity.c_str(),
certpath.empty() ? NULL : certpath.c_str());
// Clean up ServiceInfo object.
FreeServiceInfo(service);
} else {
LOG(WARNING) << "Cannot find hidden network: " << ssid;
// TODO(chocobo): Show error message.
}
}
virtual void ConnectToCellularNetwork(const CellularNetwork* network) {
DCHECK(network);
if (!EnsureCrosLoaded())
return;
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;
}
NotifyNetworkChanged();
}
}
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;
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;
}
}
NotifyNetworkChanged();
}
}
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;
}
}
NotifyNetworkChanged();
}
}
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 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 PropertyObserverList : public ObserverList<PropertyObserver> {
public:
PropertyObserverList(NetworkLibraryImpl* library,
const char* service_path) {
DCHECK(service_path);
property_change_monitor_ = MonitorNetworkService(&PropertyChangeHandler,
service_path,
library);
}
virtual ~PropertyObserverList() {
if (property_change_monitor_)
DisconnectPropertyChangeMonitor(property_change_monitor_);
}
private:
static void PropertyChangeHandler(void* object,
const char* path,
const char* key,
const Value* value) {
NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object);
DCHECK(network);
network->NotifyPropertyChange(path, key, value);
}
PropertyChangeMonitor property_change_monitor_;
};
typedef std::map<std::string, PropertyObserverList*>
PropertyChangeObserverMap;
static void NetworkStatusChangedHandler(void* object) {
NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object);
DCHECK(network);
network->UpdateNetworkStatus();
}
static void DataPlanUpdateHandler(void* object,
const char* modem_service_path,
const CellularDataPlanList* dataplan) {
NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object);
DCHECK(network && network->cellular_network());
// Store data plan for currently connected cellular network.
if (network->cellular_network()->service_path()
.compare(modem_service_path) == 0) {
if (dataplan != NULL) {
network->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
<< " 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();
VLOG(1) << "Registering for network status updates.";
// Now, register to receive updates on network status.
network_status_connection_ = MonitorNetwork(&NetworkStatusChangedHandler,
this);
VLOG(1) << "Registering for cellular data plan updates.";
data_plan_monitor_ = MonitorCellularDataPlan(&DataPlanUpdateHandler, this);
}
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;
chromeos::CellularDataPlan test_plan;
test_plan.plan_name = "Fake plan";
test_plan.data_bytes_used = 5LL * 1024LL * 1024LL * 1024LL;
test_plan.plan_start_time =
(base::Time::Now() - base::TimeDelta::FromDays(15)).ToInternalValue() /
base::Time::kMicrosecondsPerSecond;
test_plan.plan_end_time =
(base::Time::Now() + base::TimeDelta::FromDays(12)).ToInternalValue() /
base::Time::kMicrosecondsPerSecond;
test_plan.plan_data_bytes = 20LL * 1024LL * 1024LL * 1024LL;
test_plan.plan_type = CELLULAR_DATA_PLAN_METERED_PAID;
test_plan.update_time = base::Time::Now().ToInternalValue() /
base::Time::kMicrosecondsPerSecond;
chromeos::CellularDataPlanList test_plans;
test_plans.push_back(test_plan);
cellular_->SetDataPlans(test_plans);
}
void UpdateSystemInfo() {
if (EnsureCrosLoaded()) {
UpdateNetworkStatus();
}
}
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 NotifyNetworkChanged() {
FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this));
}
void NotifyCellularDataPlanChanged() {
FOR_EACH_OBSERVER(Observer, observers_, CellularDataPlanChanged(this));
}
void NotifyPropertyChange(const char* service_path,
const char* key,
const Value* value) {
DCHECK(service_path);
DCHECK(key);
DCHECK(value);
PropertyChangeObserverMap::const_iterator iter = property_observers_.find(
std::string(service_path));
if (iter != property_observers_.end()) {
FOR_EACH_OBSERVER(PropertyObserver, *(iter->second),
PropertyChanged(service_path, key, value));
} else {
NOTREACHED() <<
"There weren't supposed to be any property change observers of " <<
service_path;
}
}
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();
}
void UpdateNetworkStatus() {
// Make sure we run on UI thread.
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this,
&NetworkLibraryImpl::UpdateNetworkStatus));
return;
}
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 update data plan list.
if (cellular_networks_[i]->service_path() !=
prev_cellular_service_path) {
CellularDataPlanList list;
RetrieveCellularDataPlans(cellular_->service_path().c_str(), &list);
UpdateCellularDataPlan(list);
}
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;
NotifyNetworkChanged();
FreeSystemInfo(system);
}
void UpdateCellularDataPlan(const CellularDataPlanList& data_plans) {
DCHECK(cellular_);
cellular_->SetDataPlans(data_plans);
NotifyCellularDataPlanChanged();
}
ObserverList<Observer> observers_;
// Property change observer map
PropertyChangeObserverMap property_observers_;
// The network status connection for monitoring network status changes.
MonitorNetworkConnection network_status_connection_;
// For monitoring data plan changes to the connected cellular network.
DataPlanUpdateMonitor data_plan_monitor_;
// The property change connection for monitoring service property changes.
std::map<std::string, PropertyChangeMonitor> property_change_monitors_;
// 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_;
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 AddObserver(Observer* observer) {}
virtual void RemoveObserver(Observer* observer) {}
virtual void AddProperyObserver(const char* service_path,
PropertyObserver* observer) {}
virtual void RemoveProperyObserver(PropertyObserver* 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 void ConnectToWifiNetwork(const WifiNetwork* network,
const std::string& password,
const std::string& identity,
const std::string& certpath) {}
virtual void ConnectToWifiNetwork(const std::string& ssid,
const std::string& password,
const std::string& identity,
const std::string& certpath,
bool auto_connect) {}
virtual void ConnectToCellularNetwork(const CellularNetwork* network) {}
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 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(); }
virtual void UpdateSystemInfo() {}
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);
|