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 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/network/network_state_handler.h"
#include <stddef.h>
#include <limits>
#include <memory>
#include <utility>
#include "ash/constants/ash_features.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/uuid.h"
#include "base/values.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/fake_network_state_handler.h"
#include "chromeos/ash/components/network/network_connection_handler.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_handler_callbacks.h"
#include "chromeos/ash/components/network/network_state_handler_observer.h"
#include "chromeos/ash/components/network/tether_constants.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace {
// Constants used for logging.
constexpr char kReasonStateChange[] = "State Change";
constexpr char kReasonChange[] = "New Network";
constexpr char kReasonUpdate[] = "Update";
constexpr char kReasonUpdateIPConfig[] = "UpdateIPConfig";
constexpr char kReasonUpdateDeviceIPConfig[] = "UpdateDeviceIPConfig";
constexpr char kReasonTether[] = "Tether Change";
bool ConnectionStateChanged(const NetworkState* network,
const std::string& prev_connection_state) {
std::string connection_state = network->connection_state();
bool prev_idle = prev_connection_state.empty() ||
prev_connection_state == shill::kStateIdle;
bool cur_idle = connection_state == shill::kStateIdle;
if (prev_idle || cur_idle) {
return prev_idle != cur_idle;
}
return connection_state != prev_connection_state;
}
std::string GetManagedStateLogType(const ManagedState* state) {
switch (state->managed_type()) {
case ManagedState::MANAGED_TYPE_NETWORK:
return "Network";
case ManagedState::MANAGED_TYPE_DEVICE:
return "Device";
}
NOTREACHED();
}
std::string GetLogName(const ManagedState* state) {
if (!state) {
return "None";
}
const NetworkState* network = state->AsNetworkState();
if (network) {
return NetworkId(network);
}
return state->path();
}
bool ShouldIncludeNetworkInList(const NetworkState* network_state,
bool configured_only,
bool visible_only) {
if (configured_only && !network_state->IsInProfile()) {
return false;
}
if (visible_only && !network_state->visible()) {
return false;
}
if (network_state->type() == shill::kTypeWifi &&
!network_state->tether_guid().empty()) {
// Wi-Fi networks which are actually underlying Wi-Fi hotspots for a
// Tether network should not be included since they should only be shown
// to the user as Tether networks.
return false;
}
return true;
}
// ManagedState entries may not have |type| set when the network is initially
// added to a list (i.e. before the initial properties are received). Use this
// wrapper anyplace where |managed| might be uninitialized.
bool TypeMatches(const ManagedState* managed, const NetworkTypePattern& type) {
return !managed->type().empty() && managed->Matches(type);
}
} // namespace
// Class for tracking properties that affect whether a NetworkState is active.
class NetworkStateHandler::ActiveNetworkState {
public:
explicit ActiveNetworkState(const NetworkState* network)
: guid_(network->guid()),
connection_state_(network->connection_state()),
activation_state_(network->activation_state()),
connect_requested_(network->connect_requested()),
signal_strength_(network->signal_strength()),
network_technology_(network->network_technology()),
portal_state_(network->portal_state()) {}
bool MatchesNetworkState(const NetworkState* network) {
return guid_ == network->guid() &&
connection_state_ == network->connection_state() &&
activation_state_ == network->activation_state() &&
connect_requested_ == network->connect_requested() &&
(abs(signal_strength_ - network->signal_strength()) <
NetworkState::kSignalStrengthChangeThreshold) &&
network_technology_ == network->network_technology() &&
portal_state_ == network->portal_state();
}
private:
// Unique network identifier.
const std::string guid_;
// Active networks have a connected or connecting |connection_state_|, see
// NetworkState::Is{Connected|Connecting}State.
const std::string connection_state_;
// Activating Cellular networks are frequently treated like connecting
// networks in the UI, so we also track changes to Cellular activation state.
const std::string activation_state_;
// The connect_requested state affects 'connecting' in the UI.
const bool connect_requested_;
// We care about signal strength changes to active networks.
const int signal_strength_;
// Network technology is indicated in network icons in the UI, so we need to
// track changes to this value.
const std::string network_technology_;
// Portal state changes affects the network connection state. We want to make
// sure the network state gets updated each time the portal state changes.
const NetworkState::PortalState portal_state_;
};
const char NetworkStateHandler::kDefaultCheckPortalList[] =
"ethernet,wifi,cellular";
NetworkStateHandler::NetworkStateHandler() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
NetworkStateHandler::~NetworkStateHandler() {
// Normally Shutdown() will get called in ~NetworkHandler, however unit
// tests do not use that class so this needs to call Shutdown when we
// destroy the class.
if (!did_shutdown_) {
Shutdown();
}
}
void NetworkStateHandler::Shutdown() {
if (did_shutdown_) {
return; // May get called twice in tests.
}
did_shutdown_ = true;
for (Observer& observer : observers_) {
observer.OnShuttingDown();
}
}
void NetworkStateHandler::InitShillPropertyHandler() {
shill_property_handler_ =
std::make_unique<internal::ShillPropertyHandler>(this);
shill_property_handler_->Init();
}
void NetworkStateHandler::UpdateBlockedCellularNetworks(bool only_managed) {
if (allow_only_policy_cellular_networks_to_connect_ == only_managed) {
return;
}
allow_only_policy_cellular_networks_to_connect_ = only_managed;
UpdateBlockedNetworksInternal(NetworkTypePattern::Cellular());
}
void NetworkStateHandler::UpdateBlockedWifiNetworks(
bool only_managed,
bool available_only,
const std::vector<std::string>& blocked_hex_ssids) {
if (allow_only_policy_wifi_networks_to_connect_ == only_managed &&
allow_only_policy_wifi_networks_to_connect_if_available_ ==
available_only &&
blocked_hex_ssids_ == blocked_hex_ssids) {
return;
}
allow_only_policy_wifi_networks_to_connect_ = only_managed;
allow_only_policy_wifi_networks_to_connect_if_available_ = available_only;
blocked_hex_ssids_ = blocked_hex_ssids;
UpdateBlockedNetworksInternal(NetworkTypePattern::WiFi());
}
const NetworkState* NetworkStateHandler::GetAvailableManagedWifiNetwork()
const {
DeviceState* device =
GetModifiableDeviceStateByType(NetworkTypePattern::WiFi());
if (!device || !device->update_received()) {
NET_LOG(ERROR) << "GetAvailableManagedWifiNetwork() called with no WiFi "
<< "device available.";
return nullptr;
}
const std::string& available_managed_network_path =
device->available_managed_network_path();
if (available_managed_network_path.empty()) {
return nullptr;
}
return GetNetworkState(available_managed_network_path);
}
bool NetworkStateHandler::IsProfileNetworksLoaded() {
return is_profile_networks_loaded_;
}
bool NetworkStateHandler::OnlyManagedWifiNetworksAllowed() const {
return allow_only_policy_wifi_networks_to_connect_ ||
(allow_only_policy_wifi_networks_to_connect_if_available_ &&
GetAvailableManagedWifiNetwork());
}
void NetworkStateHandler::SyncStubCellularNetworks() {
bool network_list_changed = AddOrRemoveStubCellularNetworks();
if (!network_list_changed) {
return;
}
SortNetworkList();
NotifyNetworkListChanged();
}
void NetworkStateHandler::RequestTrafficCounters(
const std::string& service_path,
chromeos::DBusMethodCallback<base::Value> callback) {
const NetworkState* network = GetNetworkState(service_path);
// Return early if a network is not backed by shill, this can happen if the
// network is a Tether network or is a non shill Cellular network.
// see b/266972302.
if (!network || network->IsNonProfileType()) {
std::move(callback).Run(std::nullopt);
return;
}
shill_property_handler_->RequestTrafficCounters(service_path,
std::move(callback));
}
void NetworkStateHandler::ResetTrafficCounters(
const std::string& service_path) {
const NetworkState* network = GetNetworkState(service_path);
// Return early if a network is not backed by shill, this can happen if the
// network is a Tether network or is a non shill Cellular network.
// see b/266972302.
if (!network || network->IsNonProfileType()) {
return;
}
shill_property_handler_->ResetTrafficCounters(service_path);
}
// static
std::unique_ptr<NetworkStateHandler> NetworkStateHandler::InitializeForTest() {
auto handler = base::WrapUnique(new NetworkStateHandler());
handler->InitShillPropertyHandler();
return handler;
}
void NetworkStateHandler::AddObserver(Observer* observer,
const base::Location& from_here) {
observers_.AddObserver(observer);
device_event_log::AddEntry(
from_here.file_name(), from_here.line_number(),
device_event_log::LOG_TYPE_NETWORK, device_event_log::LOG_LEVEL_DEBUG,
base::StringPrintf("NetworkStateHandler::AddObserver: 0x%p", observer));
}
void NetworkStateHandler::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NetworkStateHandler::RemoveObserver(Observer* observer,
const base::Location& from_here) {
observers_.RemoveObserver(observer);
device_event_log::AddEntry(
from_here.file_name(), from_here.line_number(),
device_event_log::LOG_TYPE_NETWORK, device_event_log::LOG_LEVEL_DEBUG,
base::StringPrintf("NetworkStateHandler::RemoveObserver: 0x%p",
observer));
}
void NetworkStateHandler::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
NetworkStateHandler::TechnologyState NetworkStateHandler::GetTechnologyState(
const NetworkTypePattern& type) const {
std::string technology = GetTechnologyForType(type);
if (technology == kTypeTether) {
return tether_technology_state_;
}
// If a technology is not in Shill's 'AvailableTechnologies' list, it is
// always unavailable.
if (!shill_property_handler_->IsTechnologyAvailable(technology)) {
return TECHNOLOGY_UNAVAILABLE;
}
// Prohibited should take precedence over other states.
if (shill_property_handler_->IsTechnologyProhibited(technology)) {
return TECHNOLOGY_PROHIBITED;
}
// Disabling is a pseudostate used by the UI and takes precedence over
// enabled.
if (shill_property_handler_->IsTechnologyDisabling(technology)) {
DCHECK(shill_property_handler_->IsTechnologyEnabled(technology));
return TECHNOLOGY_DISABLING;
}
// Enabled and Uninitialized should be mutually exclusive. 'Enabling', which
// is a pseudo state used by the UI, takes precedence over 'Uninitialized',
// but not 'Enabled'.
if (shill_property_handler_->IsTechnologyEnabled(technology)) {
return TECHNOLOGY_ENABLED;
}
if (shill_property_handler_->IsTechnologyEnabling(technology)) {
return TECHNOLOGY_ENABLING;
}
if (shill_property_handler_->IsTechnologyUninitialized(technology)) {
return TECHNOLOGY_UNINITIALIZED;
}
// Default state is 'Available', which is equivalent to 'Initialized but not
// enabled'.
return TECHNOLOGY_AVAILABLE;
}
void NetworkStateHandler::SetTechnologiesEnabled(
const NetworkTypePattern& type,
bool enabled,
network_handler::ErrorCallback error_callback) {
std::vector<std::string> technologies = GetTechnologiesForType(type);
for (const std::string& technology : technologies) {
PerformSetTechnologyEnabled(technology, enabled, base::DoNothing(),
std::move(error_callback));
}
// Signal Device/Technology state changed.
NotifyDeviceListChanged();
}
void NetworkStateHandler::SetTechnologyEnabled(
const NetworkTypePattern& type,
bool enabled,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback) {
std::string technology = GetTechnologyForType(type);
PerformSetTechnologyEnabled(technology, enabled, std::move(success_callback),
std::move(error_callback));
// Signal Device/Technology state changed.
NotifyDeviceListChanged();
}
void NetworkStateHandler::PerformSetTechnologyEnabled(
const std::string& technology,
bool enabled,
base::OnceClosure success_callback,
network_handler::ErrorCallback error_callback) {
if (technology == kTypeTether) {
if (tether_technology_state_ != TECHNOLOGY_ENABLED &&
tether_technology_state_ != TECHNOLOGY_AVAILABLE) {
NET_LOG(ERROR) << "SetTechnologyEnabled() called for the Tether "
<< "DeviceState, but the current state was: "
<< tether_technology_state_;
network_handler::RunErrorCallback(
std::move(error_callback),
NetworkConnectionHandler::kErrorEnabledOrDisabledWhenNotAvailable);
return;
}
// Tether does not exist in Shill, so set |tether_technology_state_| and
// skip the below interactions with |shill_property_handler_|.
tether_technology_state_ =
enabled ? TECHNOLOGY_ENABLED : TECHNOLOGY_AVAILABLE;
return;
}
if (!shill_property_handler_->IsTechnologyAvailable(technology)) {
return;
}
NET_LOG(USER) << "SetTechnologyEnabled " << technology << ":" << enabled;
shill_property_handler_->SetTechnologyEnabled(technology, enabled,
std::move(error_callback),
std::move(success_callback));
}
void NetworkStateHandler::SetTetherTechnologyState(
TechnologyState technology_state) {
if (tether_technology_state_ == technology_state) {
return;
}
tether_technology_state_ = technology_state;
EnsureTetherDeviceState();
// Signal Device/Technology state changed.
NotifyDeviceListChanged();
}
void NetworkStateHandler::SetTetherScanState(bool is_scanning) {
DeviceState* tether_device_state =
GetModifiableDeviceState(kTetherDevicePath);
if (!tether_device_state) {
NET_LOG(ERROR) << "SetTetherScanState() called when Tether TechnologyState "
<< "is UNAVAILABLE; cannot set scanning state.";
return;
}
bool was_scanning = tether_device_state->scanning();
tether_device_state->set_scanning(is_scanning);
if (was_scanning && !is_scanning) {
// If a scan was in progress but has completed, notify observers.
NotifyScanCompleted(tether_device_state);
} else if (!was_scanning && is_scanning) {
// If a scan was started, notify observers.
NotifyScanStarted(tether_device_state);
}
}
void NetworkStateHandler::SetProhibitedTechnologies(
const std::vector<std::string>& prohibited_technologies) {
// Make a copy of |prohibited_technologies| since the list may be edited
// within this function.
std::vector<std::string> prohibited_technologies_copy =
prohibited_technologies;
auto it = prohibited_technologies_copy.begin();
while (it != prohibited_technologies_copy.end()) {
if (*it == kTypeTether) {
// If Tether networks are prohibited, set |tether_technology_state_| and
// remove |kTypeTether| from the list before passing it to
// |shill_property_handler_| below. Shill does not have a concept of
// Tether networks, so it cannot prohibit that technology type.
tether_technology_state_ = TECHNOLOGY_PROHIBITED;
it = prohibited_technologies_copy.erase(it);
} else {
++it;
}
}
shill_property_handler_->SetProhibitedTechnologies(
prohibited_technologies_copy);
// Signal Device/Technology state changed.
NotifyDeviceListChanged();
}
const DeviceState* NetworkStateHandler::GetDeviceState(
const std::string& device_path) const {
const DeviceState* device = GetModifiableDeviceState(device_path);
if (device && !device->update_received()) {
NET_LOG(DEBUG) << "Device exists but update not received: " << device_path;
return nullptr;
}
return device;
}
const DeviceState* NetworkStateHandler::GetDeviceStateByType(
const NetworkTypePattern& type) const {
const DeviceState* device = GetModifiableDeviceStateByType(type);
if (device && !device->update_received()) {
return nullptr;
}
return device;
}
bool NetworkStateHandler::GetScanningByType(
const NetworkTypePattern& type) const {
for (auto iter = device_list_.begin(); iter != device_list_.end(); ++iter) {
const DeviceState* device = (*iter)->AsDeviceState();
DCHECK(device);
if (!device->update_received()) {
continue;
}
if (device->Matches(type) && device->scanning()) {
return true;
}
}
return false;
}
const NetworkState* NetworkStateHandler::GetNetworkState(
const std::string& service_path) const {
const NetworkState* network = GetModifiableNetworkState(service_path);
if (network && !network->update_received()) {
return nullptr;
}
return network;
}
const NetworkState* NetworkStateHandler::DefaultNetwork() const {
if (default_network_path_.empty()) {
return nullptr;
}
return GetNetworkState(default_network_path_);
}
const NetworkState* NetworkStateHandler::ConnectedNetworkByType(
const NetworkTypePattern& type) {
NetworkStateList active_networks;
GetActiveNetworkListByType(type, &active_networks);
for (auto* network : active_networks) {
if (network->IsConnectedState()) {
return network;
}
}
return nullptr;
}
const NetworkState* NetworkStateHandler::ConnectingNetworkByType(
const NetworkTypePattern& type) {
NetworkStateList active_networks;
GetActiveNetworkListByType(type, &active_networks);
for (auto* network : active_networks) {
if (network->IsConnectingState()) {
return network;
}
}
return nullptr;
}
const NetworkState* NetworkStateHandler::ActiveNetworkByType(
const NetworkTypePattern& type) {
NetworkStateList active_networks;
GetActiveNetworkListByType(type, &active_networks);
if (active_networks.size() > 0) {
return active_networks.front();
}
return nullptr;
}
const NetworkState* NetworkStateHandler::FirstNetworkByType(
const NetworkTypePattern& type) {
// Sort to ensure visible networks are listed first.
if (!network_list_sorted_) {
SortNetworkList();
}
const NetworkState* first_network = nullptr;
for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
if (!network->update_received()) {
continue;
}
if (!network->visible()) {
break;
}
if (network->Matches(type)) {
first_network = network;
break;
}
}
// Active Ethernet networks are the highest priority.
if (first_network && first_network->type() == shill::kTypeEthernet) {
return first_network;
}
const NetworkState* first_tether_network =
type.MatchesPattern(NetworkTypePattern::Tether()) &&
!tether_network_list_.empty()
? tether_network_list_[0]->AsNetworkState()
: nullptr;
// Active Tether networks are next.
if (first_tether_network && first_tether_network->IsConnectingOrConnected()) {
return first_tether_network;
}
// Other active networks are next.
if (first_network && first_network->IsConnectingOrConnected()) {
return first_network;
}
// Non-active Tether networks are next.
if (first_tether_network) {
return first_tether_network;
}
// Other networks are last.
return first_network;
}
void NetworkStateHandler::SetNetworkConnectRequested(
const std::string& service_path,
bool connect_requested) {
NetworkState* network = GetModifiableNetworkState(service_path);
if (!network) {
return;
}
network->connect_requested_ = connect_requested;
network->shill_connect_error_.clear();
network_list_sorted_ = false;
OnNetworkConnectionStateChanged(network);
}
void NetworkStateHandler::SetShillConnectError(
const std::string& service_path,
const std::string& shill_connect_error) {
NetworkState* network = GetModifiableNetworkState(service_path);
if (!network) {
return;
}
network->shill_connect_error_ = shill_connect_error;
}
std::string NetworkStateHandler::FormattedHardwareAddressForType(
const NetworkTypePattern& type) {
const NetworkState* network = ConnectedNetworkByType(type);
if (network && network->type() == kTypeTether) {
// If this is a Tether network, get the MAC address corresponding to that
// network instead.
network = GetNetworkStateFromGuid(network->tether_guid());
}
const DeviceState* device = network ? GetDeviceState(network->device_path())
: GetDeviceStateByType(type);
if (!device || device->mac_address().empty()) {
return std::string();
}
return network_util::FormattedMacAddress(device->mac_address());
}
void NetworkStateHandler::GetVisibleNetworkListByType(
const NetworkTypePattern& type,
NetworkStateList* list) {
GetNetworkListByType(type, false /* configured_only */,
true /* visible_only */, 0 /* no limit */, list);
}
void NetworkStateHandler::GetVisibleNetworkList(NetworkStateList* list) {
GetVisibleNetworkListByType(NetworkTypePattern::Default(), list);
}
void NetworkStateHandler::GetNetworkListByType(const NetworkTypePattern& type,
bool configured_only,
bool visible_only,
size_t limit,
NetworkStateList* list) {
GetNetworkListByTypeImpl(type, configured_only, visible_only,
false /* active_only */, limit, list);
}
void NetworkStateHandler::GetActiveNetworkListByType(
const NetworkTypePattern& type,
NetworkStateList* list) {
GetNetworkListByTypeImpl(type, false /* configured_only */,
false /* visible_only */, true /* active_only */,
0 /* no limit */, list);
}
void NetworkStateHandler::GetNetworkListByTypeImpl(
const NetworkTypePattern& type,
bool configured_only,
bool visible_only,
bool active_only,
size_t limit,
NetworkStateList* list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(list);
list->clear();
// If |limit| is 0, there is no limit. Simplify the calculations below by
// setting it to the maximum size_t value.
if (limit == 0) {
limit = std::numeric_limits<size_t>::max();
}
if (!network_list_sorted_) {
SortNetworkList();
}
// First, add active Tether networks.
if (type.MatchesPattern(NetworkTypePattern::Tether())) {
AppendTetherNetworksToList(true /* get_active */, limit, list);
}
// Second, add active non-Tether networks.
for (const auto& managed : network_list_) {
const NetworkState* network = managed.get()->AsNetworkState();
DCHECK(network);
if (!network->update_received() || !network->Matches(type)) {
continue;
}
if (!network->IsActive()) {
break; // Active networks are listed first.
}
if (!ShouldIncludeNetworkInList(network, configured_only, visible_only)) {
continue;
}
if (network->type() == shill::kTypeEthernet) {
// Ethernet networks should always be in front.
list->insert(list->begin(), network);
} else {
list->push_back(network);
}
if (list->size() >= limit) {
return;
}
}
if (active_only) {
return;
}
// Third, add inactive Tether networks.
if (type.MatchesPattern(NetworkTypePattern::Tether())) {
AppendTetherNetworksToList(false /* get_active */, limit, list);
}
if (list->size() >= limit) {
return;
}
// Fourth, add inactive non-Tether networks.
for (const auto& managed : network_list_) {
const NetworkState* network = managed.get()->AsNetworkState();
DCHECK(network);
if (!network->update_received() || !network->Matches(type)) {
continue;
}
if (network->IsActive()) {
continue;
}
if (!ShouldIncludeNetworkInList(network, configured_only, visible_only)) {
continue;
}
list->push_back(network);
if (list->size() >= limit) {
return;
}
}
}
void NetworkStateHandler::AppendTetherNetworksToList(bool get_active,
size_t limit,
NetworkStateList* list) {
DCHECK(list);
DCHECK_NE(0U, limit);
if (!IsTechnologyEnabled(NetworkTypePattern::Tether())) {
return;
}
for (auto iter = tether_network_list_.begin();
iter != tether_network_list_.end() && list->size() < limit; ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
if (network->IsConnectingOrConnected() != get_active) {
continue;
}
if (!ShouldIncludeNetworkInList(network, false /* configured_only */,
false /* visible_only */)) {
continue;
}
list->push_back(network);
}
}
const NetworkState* NetworkStateHandler::GetNetworkStateFromServicePath(
const std::string& service_path,
bool configured_only) const {
ManagedState* managed =
GetModifiableManagedState(&network_list_, service_path);
if (!managed) {
managed = GetModifiableManagedState(&tether_network_list_, service_path);
if (!managed) {
return nullptr;
}
}
const NetworkState* network = managed->AsNetworkState();
DCHECK(network);
if (!network->update_received() ||
(configured_only && !network->IsInProfile())) {
return nullptr;
}
return network;
}
const NetworkState* NetworkStateHandler::GetNetworkStateFromGuid(
const std::string& guid) const {
DCHECK(!guid.empty());
return GetModifiableNetworkStateFromGuid(guid);
}
void NetworkStateHandler::AddTetherNetworkState(const std::string& guid,
const std::string& name,
const std::string& carrier,
int battery_percentage,
int signal_strength,
bool has_connected_to_host) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!guid.empty());
DCHECK(battery_percentage >= 0 && battery_percentage <= 100);
DCHECK(signal_strength >= 0 && signal_strength <= 100);
if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
NET_LOG(ERROR) << "AddTetherNetworkState() called when Tether networks "
<< "are not enabled. Cannot add NetworkState.";
return;
}
// If the network already exists, do nothing.
if (GetNetworkStateFromGuid(guid)) {
NET_LOG(ERROR) << "AddTetherNetworkState: " << name
<< " called with existing guid:" << guid;
return;
}
// Use the GUID as the network's service path.
std::unique_ptr<NetworkState> tether_network_state =
std::make_unique<NetworkState>(guid /* path */);
tether_network_state->set_name(name);
tether_network_state->set_type(kTypeTether);
tether_network_state->SetGuid(guid);
tether_network_state->set_visible(true);
tether_network_state->set_update_received();
tether_network_state->set_update_requested(false);
tether_network_state->set_connectable(true);
tether_network_state->set_tether_carrier(carrier);
tether_network_state->set_battery_percentage(battery_percentage);
tether_network_state->set_tether_has_connected_to_host(has_connected_to_host);
tether_network_state->set_signal_strength(signal_strength);
tether_network_list_.push_back(std::move(tether_network_state));
network_list_sorted_ = false;
NotifyNetworkListChanged();
}
bool NetworkStateHandler::UpdateTetherNetworkProperties(
const std::string& guid,
const std::string& carrier,
int battery_percentage,
int signal_strength) {
if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
NET_LOG(ERROR) << "UpdateTetherNetworkProperties() called when Tether "
<< "networks are not enabled. Cannot update.";
return false;
}
NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
if (!tether_network_state) {
NET_LOG(ERROR) << "UpdateTetherNetworkProperties(): No NetworkState for "
<< "Tether network with GUID \"" << guid << "\".";
return false;
}
tether_network_state->set_tether_carrier(carrier);
tether_network_state->set_battery_percentage(battery_percentage);
tether_network_state->set_signal_strength(signal_strength);
network_list_sorted_ = false;
NotifyNetworkPropertiesUpdated(tether_network_state);
if (tether_network_state->IsConnectingOrConnected()) {
NotifyIfActiveNetworksChanged();
}
return true;
}
bool NetworkStateHandler::SetTetherNetworkHasConnectedToHost(
const std::string& guid) {
NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
if (!tether_network_state) {
NET_LOG(ERROR) << "SetTetherNetworkHasConnectedToHost(): No NetworkState "
<< "for Tether network with GUID \"" << guid << "\".";
return false;
}
if (tether_network_state->tether_has_connected_to_host()) {
return false;
}
tether_network_state->set_tether_has_connected_to_host(true);
network_list_sorted_ = false;
NotifyNetworkPropertiesUpdated(tether_network_state);
return true;
}
bool NetworkStateHandler::RemoveTetherNetworkState(const std::string& guid) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!notifying_network_observers_);
for (auto iter = tether_network_list_.begin();
iter != tether_network_list_.end(); ++iter) {
if (iter->get()->AsNetworkState()->guid() == guid) {
const NetworkState* tether_network = iter->get()->AsNetworkState();
bool was_active = tether_network->IsConnectingOrConnected();
NetworkState* wifi_network =
GetModifiableNetworkStateFromGuid(tether_network->tether_guid());
if (wifi_network) {
wifi_network->set_tether_guid(std::string());
}
tether_network_list_.erase(iter);
if (was_active) {
NotifyIfActiveNetworksChanged();
}
NotifyNetworkListChanged();
return true;
}
}
return false;
}
bool NetworkStateHandler::DisassociateTetherNetworkStateFromWifiNetwork(
const std::string& tether_network_guid) {
NetworkState* tether_network_state =
GetModifiableNetworkStateFromGuid(tether_network_guid);
if (!tether_network_state) {
NET_LOG(ERROR) << "DisassociateTetherNetworkStateWithWifiNetwork(): Tether "
<< "network with ID " << tether_network_guid
<< " not registered; could not remove association.";
return false;
}
std::string wifi_network_guid = tether_network_state->tether_guid();
NetworkState* wifi_network_state =
GetModifiableNetworkStateFromGuid(wifi_network_guid);
if (!wifi_network_state) {
NET_LOG(ERROR) << "DisassociateTetherNetworkStateWithWifiNetwork(): Wi-Fi "
<< "network with ID " << wifi_network_guid
<< " not registered; could not remove association.";
return false;
}
if (wifi_network_state->tether_guid().empty() &&
tether_network_state->tether_guid().empty()) {
return true;
}
wifi_network_state->set_tether_guid(std::string());
tether_network_state->set_tether_guid(std::string());
network_list_sorted_ = false;
NotifyNetworkPropertiesUpdated(wifi_network_state);
NotifyNetworkPropertiesUpdated(tether_network_state);
return true;
}
bool NetworkStateHandler::AssociateTetherNetworkStateWithWifiNetwork(
const std::string& tether_network_guid,
const std::string& wifi_network_guid) {
if (tether_technology_state_ != TECHNOLOGY_ENABLED) {
NET_LOG(ERROR) << "AssociateTetherNetworkStateWithWifiNetwork() called "
<< "when Tether networks are not enabled. Cannot "
<< "associate.";
return false;
}
NetworkState* tether_network_state =
GetModifiableNetworkStateFromGuid(tether_network_guid);
if (!tether_network_state) {
NET_LOG(ERROR) << "Tether network does not exist: " << tether_network_guid;
return false;
}
if (!NetworkTypePattern::Tether().MatchesType(tether_network_state->type())) {
NET_LOG(ERROR) << "Network is not a Tether network: "
<< tether_network_guid;
return false;
}
NetworkState* wifi_network_state =
GetModifiableNetworkStateFromGuid(wifi_network_guid);
if (!wifi_network_state) {
NET_LOG(ERROR) << "Wi-Fi Network does not exist: " << wifi_network_guid;
return false;
}
if (!NetworkTypePattern::WiFi().MatchesType(wifi_network_state->type())) {
NET_LOG(ERROR) << "Network is not a W-Fi network: "
<< NetworkId(wifi_network_state);
return false;
}
if (wifi_network_state->tether_guid() == tether_network_guid &&
tether_network_state->tether_guid() == wifi_network_guid) {
return true;
}
tether_network_state->set_tether_guid(wifi_network_guid);
wifi_network_state->set_tether_guid(tether_network_guid);
network_list_sorted_ = false;
NotifyNetworkPropertiesUpdated(wifi_network_state);
NotifyNetworkPropertiesUpdated(tether_network_state);
return true;
}
void NetworkStateHandler::SetTetherNetworkStateDisconnected(
const std::string& guid) {
SetTetherNetworkStateConnectionState(guid, shill::kStateIdle);
}
void NetworkStateHandler::SetTetherNetworkStateConnecting(
const std::string& guid) {
// The default network should only be set if there currently is no default
// network. Otherwise, the default network should not change unless the
// connection completes successfully and the newly-connected network is
// prioritized higher than the existing default network. Note that, in
// general, a connected Ethernet network is still considered the default
// network even if a Tether or Wi-Fi network becomes connected.
if (default_network_path_.empty()) {
NET_LOG(EVENT) << "Connecting to Tether network when there is currently no "
<< "default network; setting as new default network. GUID: "
<< guid;
SetDefaultNetworkValues(guid, /*metered=*/true);
}
SetTetherNetworkStateConnectionState(guid, shill::kStateConfiguration);
}
void NetworkStateHandler::SetTetherNetworkStateConnected(
const std::string& guid) {
// Being connected implies that AssociateTetherNetworkStateWithWifiNetwork()
// was already called, so ensure that the association is still intact.
// TODO(b/278966899): Promote this to a CHECK.
DCHECK(GetNetworkStateFromGuid(GetNetworkStateFromGuid(guid)->tether_guid())
->tether_guid() == guid);
// At this point, there should be a default network set.
// TODO(b/279047073): We can hit this due to a race between
// `SetTetherNetworkStateConnected` and `DefaultNetworkServiceChange`.
DCHECK(!default_network_path_.empty());
SetTetherNetworkStateConnectionState(guid, shill::kStateOnline);
}
void NetworkStateHandler::SetTetherNetworkStateConnectionState(
const std::string& guid,
const std::string& connection_state) {
NetworkState* tether_network_state = GetModifiableNetworkStateFromGuid(guid);
if (!tether_network_state) {
NET_LOG(ERROR) << "SetTetherNetworkStateConnectionState: Tether network "
<< "not found: " << guid;
return;
}
DCHECK(
NetworkTypePattern::Tether().MatchesType(tether_network_state->type()));
std::string prev_connection_state = tether_network_state->connection_state();
tether_network_state->SetConnectionState(connection_state);
network_list_sorted_ = false;
if (ConnectionStateChanged(tether_network_state, prev_connection_state)) {
NET_LOG(EVENT) << "Changing connection state for Tether network with GUID "
<< guid << ". Old state: " << prev_connection_state << ", "
<< "New state: " << connection_state;
if (!tether_network_state->IsConnectingOrConnected() &&
tether_network_state->path() == default_network_path_) {
SetDefaultNetworkValues(/*path=*/std::string(), /*metered=*/false);
NotifyDefaultNetworkChanged(kReasonTether);
}
OnNetworkConnectionStateChanged(tether_network_state);
NotifyNetworkPropertiesUpdated(tether_network_state);
}
}
void NetworkStateHandler::EnsureTetherDeviceState() {
bool should_be_present =
tether_technology_state_ != TechnologyState::TECHNOLOGY_UNAVAILABLE;
for (auto it = device_list_.begin(); it < device_list_.end(); ++it) {
std::string path = (*it)->path();
if (path == kTetherDevicePath) {
// If the Tether DeviceState is in the list and it should not be, remove
// it and return. If it is in the list and it should be, the list is
// already valid, so return without removing it.
if (!should_be_present) {
device_list_.erase(it);
}
return;
}
}
if (!should_be_present) {
// If the Tether DeviceState was not in the list and it should not be, the
// list is already valid, so return.
return;
}
// The Tether DeviceState is not present in the list, but it should be. Since
// Tether networks are not recognized by Shill, they will never receive an
// update, so set properties on the state here.
std::unique_ptr<ManagedState> tether_device_state = ManagedState::Create(
ManagedState::ManagedType::MANAGED_TYPE_DEVICE, kTetherDevicePath);
tether_device_state->set_update_received();
tether_device_state->set_update_requested(false);
tether_device_state->set_name(kTetherDeviceName);
tether_device_state->set_type(kTypeTether);
device_list_.push_back(std::move(tether_device_state));
}
bool NetworkStateHandler::UpdateBlockedByPolicy(NetworkState* network) const {
bool is_wifi_type = TypeMatches(network, NetworkTypePattern::WiFi());
bool is_cellular_type = TypeMatches(network, NetworkTypePattern::Cellular());
if (!is_wifi_type && !is_cellular_type) {
return false;
}
bool prev_blocked_by_policy = network->blocked_by_policy();
bool blocked_by_policy = false;
if (is_wifi_type) {
blocked_by_policy =
!network->IsManagedByPolicy() &&
(OnlyManagedWifiNetworksAllowed() ||
base::Contains(blocked_hex_ssids_, network->GetHexSsid()));
} else {
blocked_by_policy = !network->IsManagedByPolicy() &&
allow_only_policy_cellular_networks_to_connect_;
}
network->set_blocked_by_policy(blocked_by_policy);
return prev_blocked_by_policy != blocked_by_policy;
}
void NetworkStateHandler::UpdateManagedWifiNetworkAvailable() {
DeviceState* device =
GetModifiableDeviceStateByType(NetworkTypePattern::WiFi());
if (!device || !device->update_received()) {
return; // May be null in tests.
}
const std::string prev_available_managed_network_path =
device->available_managed_network_path();
std::string available_managed_network_path;
NetworkStateHandler::NetworkStateList networks;
GetNetworkListByType(NetworkTypePattern::WiFi(), true, true, 0, &networks);
for (const NetworkState* network : networks) {
if (network->IsManagedByPolicy()) {
available_managed_network_path = network->path();
break;
}
}
if (prev_available_managed_network_path != available_managed_network_path) {
device->set_available_managed_network_path(available_managed_network_path);
UpdateBlockedNetworksInternal(NetworkTypePattern::WiFi());
NotifyDevicePropertiesUpdated(device);
}
}
void NetworkStateHandler::UpdateBlockedNetworksInternal(
const NetworkTypePattern& network_type) {
for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) {
NetworkState* network = (*iter)->AsNetworkState();
if (!TypeMatches(network, network_type)) {
continue;
}
if (UpdateBlockedByPolicy(network)) {
NotifyNetworkPropertiesUpdated(network);
}
}
}
void NetworkStateHandler::GetDeviceList(DeviceStateList* list) const {
GetDeviceListByType(NetworkTypePattern::Default(), list);
}
void NetworkStateHandler::GetDeviceListByType(const NetworkTypePattern& type,
DeviceStateList* list) const {
DCHECK(list);
list->clear();
for (auto iter = device_list_.begin(); iter != device_list_.end(); ++iter) {
const DeviceState* device = (*iter)->AsDeviceState();
DCHECK(device);
if (device->update_received() && device->Matches(type)) {
list->push_back(device);
}
}
}
void NetworkStateHandler::RequestScan(const NetworkTypePattern& type) {
NET_LOG(USER) << "RequestScan: " << type.ToDebugString();
if (type.MatchesPattern(NetworkTypePattern::WiFi())) {
if (IsTechnologyEnabled(NetworkTypePattern::WiFi())) {
shill_property_handler_->RequestScanByType(shill::kTypeWifi);
} else if (type.Equals(NetworkTypePattern::WiFi())) {
return; // Skip notify if disabled and wifi only requested.
}
}
if (type.Equals(NetworkTypePattern::Cellular())) {
// Only request a Cellular scan if Cellular is requested explicitly.
if (IsTechnologyEnabled(NetworkTypePattern::Cellular())) {
shill_property_handler_->RequestScanByType(shill::kTypeCellular);
} else {
return; // Skip notify if disabled and cellular only requested.
}
}
// Note: for Tether we initiate the scan in the observer.
NotifyScanRequested(type);
}
void NetworkStateHandler::RequestUpdateForNetwork(
const std::string& service_path) {
NetworkState* network = GetModifiableNetworkState(service_path);
if (network) {
// Do not request properties for networks which are not backed by Shill.
if (network->IsNonProfileType()) {
return;
}
// Do not request properties if a condition has already triggered a request.
if (network->update_requested()) {
return;
}
network->set_update_requested(true);
NET_LOG(EVENT) << "RequestUpdate for: " << NetworkId(network);
} else {
NET_LOG(EVENT) << "RequestUpdate for: " << NetworkPathId(service_path);
}
shill_property_handler_->RequestProperties(ManagedState::MANAGED_TYPE_NETWORK,
service_path);
network_service_paths_with_stale_properties_.erase(service_path);
}
void NetworkStateHandler::RequestUpdateForDevice(
const std::string& device_path) {
DeviceState* device = GetModifiableDeviceState(device_path);
if (!device) {
return;
}
device->set_update_requested(true);
NET_LOG(EVENT) << "Request update for device path: " << device_path;
shill_property_handler_->RequestProperties(ManagedState::MANAGED_TYPE_DEVICE,
device_path);
device_paths_with_stale_properties_.erase(device_path);
}
void NetworkStateHandler::SendUpdateNotificationForNetwork(
const std::string& service_path) {
const NetworkState* network = GetNetworkState(service_path);
if (!network) {
return;
}
NotifyNetworkPropertiesUpdated(network);
}
void NetworkStateHandler::ClearLastErrorForNetwork(
const std::string& service_path) {
NetworkState* network = GetModifiableNetworkState(service_path);
if (network) {
network->ClearError();
}
}
void NetworkStateHandler::SetWakeOnLanEnabled(bool enabled) {
NET_LOG(EVENT) << "SetWakeOnLanEnabled: " << enabled;
shill_property_handler_->SetWakeOnLanEnabled(enabled);
}
void NetworkStateHandler::SetHostname(const std::string& hostname) {
NET_LOG(EVENT) << "SetHostname";
shill_property_handler_->SetHostname(hostname);
}
void NetworkStateHandler::SetNetworkThrottlingStatus(
bool enabled,
uint32_t upload_rate_kbits,
uint32_t download_rate_kbits) {
if (enabled) {
NET_LOG(EVENT) << "SetNetworkThrottlingStatus: Enabled: "
<< upload_rate_kbits << ", " << download_rate_kbits;
} else {
NET_LOG(EVENT) << "SetNetworkThrottlingStatus: Disabled.";
}
shill_property_handler_->SetNetworkThrottlingStatus(
enabled, upload_rate_kbits, download_rate_kbits);
}
void NetworkStateHandler::SetFastTransitionStatus(bool enabled) {
NET_LOG(USER) << "SetFastTransitionStatus: " << enabled;
shill_property_handler_->SetFastTransitionStatus(enabled);
}
void NetworkStateHandler::RequestPortalDetection() {
const NetworkState* default_network = DefaultNetwork();
if (!default_network) {
NET_LOG(DEBUG) << "RequestPortalDetection skipped, no default network.";
return;
}
if (default_network->IsOnline()) {
NET_LOG(DEBUG) << "RequestPortalDetection skipped for online network: "
<< NetworkId(default_network);
return;
}
NET_LOG(USER) << "RequestPortalDetection for " << NetworkId(default_network);
shill_property_handler_->RequestPortalDetection(default_network_path_);
}
const NetworkState* NetworkStateHandler::GetEAPForEthernet(
const std::string& service_path,
bool connected_only) {
const NetworkState* network = GetNetworkState(service_path);
if (!network) {
NET_LOG(ERROR) << "GetEAPForEthernet: Unknown service: "
<< NetworkPathId(service_path);
return nullptr;
}
if (network->type() != shill::kTypeEthernet) {
NET_LOG(ERROR) << "GetEAPForEthernet: Not Ethernet: " << NetworkId(network);
return nullptr;
}
if (connected_only) {
if (!network->IsConnectedState()) {
NET_LOG(DEBUG) << "GetEAPForEthernet: Not connected.";
return nullptr;
}
// The same EAP service is shared for all ethernet services/devices.
// However EAP is used/enabled per device and only if the connection was
// successfully established.
const DeviceState* device = GetDeviceState(network->device_path());
if (!device) {
NET_LOG(ERROR) << "GetEAPForEthernet: Unknown device "
<< network->device_path()
<< " for connected ethernet service: "
<< NetworkId(network);
return nullptr;
}
if (!device->eap_authentication_completed()) {
NET_LOG(DEBUG) << "GetEAPForEthernet: EAP Authenticaiton not completed.";
return nullptr;
}
}
NetworkStateList list;
GetNetworkListByType(NetworkTypePattern::Primitive(shill::kTypeEthernetEap),
true /* configured_only */, false /* visible_only */,
1 /* limit */, &list);
if (list.empty()) {
if (connected_only) {
NET_LOG(ERROR)
<< "GetEAPForEthernet: Connected using EAP but no EAP service found: "
<< NetworkId(network);
}
return nullptr;
}
return list.front();
}
void NetworkStateHandler::SetErrorForTest(const std::string& service_path,
const std::string& error) {
NetworkState* network_state = GetModifiableNetworkState(service_path);
if (!network_state) {
NET_LOG(ERROR) << "No matching NetworkState for: "
<< NetworkPathId(service_path);
return;
}
network_state->last_error_ = error;
}
void NetworkStateHandler::SetDeviceStateUpdatedForTest(
const std::string& device_path) {
DeviceState* device = GetModifiableDeviceState(device_path);
DCHECK(device);
device->set_update_received();
}
//------------------------------------------------------------------------------
// ShillPropertyHandler::Delegate overrides
void NetworkStateHandler::UpdateManagedList(ManagedState::ManagedType type,
const base::Value::List& entries) {
CHECK(!notifying_network_observers_);
ManagedStateList* managed_list = GetManagedList(type);
NET_LOG(EVENT) << "UpdateManagedList: " << ManagedState::TypeToString(type)
<< ": " << entries.size();
// Create a map of existing entries. Assumes all entries in |managed_list|
// are unique.
std::map<std::string, std::unique_ptr<ManagedState>> managed_map;
for (auto& item : *managed_list) {
std::string path = item->path();
DCHECK(!base::Contains(managed_map, path));
managed_map[path] = std::move(item);
}
// Clear the list (objects are temporarily owned by managed_map).
managed_list->clear();
// Updates managed_list and request updates for new entries.
std::set<std::string> list_entries;
for (const auto& iter : entries) {
const std::string* path = iter.GetIfString();
if (!path) {
continue;
}
if (!path || (*path).empty() || *path == shill::kFlimflamServicePath) {
NET_LOG(ERROR) << "Bad path in type " << type << " Path: " << *path;
continue;
}
auto found = managed_map.find(*path);
if (found == managed_map.end()) {
if (list_entries.count(*path) != 0) {
NET_LOG(ERROR) << "Duplicate entry in list for " << *path;
continue;
}
managed_list->push_back(ManagedState::Create(type, *path));
} else {
managed_list->push_back(std::move(found->second));
managed_map.erase(found);
}
list_entries.insert(*path);
}
if (type == ManagedState::ManagedType::MANAGED_TYPE_DEVICE) {
// Also move the Tether DeviceState if it exists. This will not happen as
// part of the loop above since |entries| will never contain the Tether
// path.
auto iter = managed_map.find(kTetherDevicePath);
if (iter != managed_map.end()) {
managed_list->push_back(std::move(iter->second));
managed_map.erase(iter);
}
}
UpdateManagedWifiNetworkAvailable();
UpdateBlockedCellularNetworks();
if (type != ManagedState::ManagedType::MANAGED_TYPE_NETWORK) {
return;
}
// Non-Shill services are added in Chrome and is not present in |entries|.
// Add these services back to managed_list.
for (auto iter = managed_map.begin(); iter != managed_map.end();) {
NetworkState* network = iter->second->AsNetworkState();
if (!network->IsNonShillCellularNetwork()) {
iter++;
continue;
}
managed_list->push_back(std::move(iter->second));
iter = managed_map.erase(iter);
}
// Network list is explicitly sorted in ManagedListChanged() which is notified
// after this method. But this ensures that any intervening calls to
// GetNetworkList* methods will use the sorted list.
network_list_sorted_ = false;
// Remove associations Tether NetworkStates had with now removed Wi-Fi
// NetworkStates.
for (auto& iter : managed_map) {
ManagedState* managed = iter.second.get();
if (!TypeMatches(managed, NetworkTypePattern::WiFi())) {
continue;
}
NetworkState* tether_network = GetModifiableNetworkStateFromGuid(
managed->AsNetworkState()->tether_guid());
if (tether_network) {
tether_network->set_tether_guid(std::string());
}
}
}
void NetworkStateHandler::UpdateBlockedCellularNetworks() {
DeviceState* device =
GetModifiableDeviceStateByType(NetworkTypePattern::Cellular());
if (!device || !device->update_received()) {
return; // May be null in tests.
}
UpdateBlockedNetworksInternal(NetworkTypePattern::Cellular());
}
void NetworkStateHandler::ProfileListChanged(
const base::Value::List& profile_list) {
NET_LOG(EVENT) << "ProfileListChanged. Re-Requesting Network Properties";
ProcessIsUserLoggedIn(profile_list);
for (ManagedStateList::iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
const NetworkState* network = (*iter)->AsNetworkState();
DCHECK(network);
// Do not request properties for networks which are not backed by Shill.
if (network->IsNonProfileType()) {
continue;
}
shill_property_handler_->RequestProperties(
ManagedState::MANAGED_TYPE_NETWORK, network->path());
}
}
void NetworkStateHandler::UpdateManagedStateProperties(
ManagedState::ManagedType type,
const std::string& path,
const base::Value::Dict& properties) {
ManagedStateList* managed_list = GetManagedList(type);
ManagedState* managed = GetModifiableManagedState(managed_list, path);
if (!managed) {
// The network has been removed from the list of networks.
NET_LOG(DEBUG) << "UpdateManagedStateProperties: Not found: " << path;
return;
}
managed->set_update_received();
NET_LOG(EVENT) << GetManagedStateLogType(managed)
<< " Properties Received: " << GetLogName(managed);
if (type == ManagedState::MANAGED_TYPE_NETWORK) {
UpdateNetworkStateProperties(managed->AsNetworkState(), properties);
managed->set_update_requested(false);
if (network_service_paths_with_stale_properties_.find(path) !=
network_service_paths_with_stale_properties_.end()) {
RequestUpdateForNetwork(path);
}
return;
}
// Device
for (const auto iter : properties) {
managed->PropertyChanged(iter.first, iter.second);
}
managed->InitialPropertiesReceived(properties);
managed->set_update_requested(false);
if (device_paths_with_stale_properties_.find(path) !=
device_paths_with_stale_properties_.end()) {
RequestUpdateForDevice(path);
}
}
void NetworkStateHandler::UpdateNetworkStateProperties(
NetworkState* network,
const base::Value::Dict& properties) {
DCHECK(network);
bool network_property_updated = false;
std::string prev_connection_state = network->connection_state();
bool metered = false;
bool had_icccid_before_update = !network->iccid().empty();
for (const auto iter : properties) {
if (network->PropertyChanged(iter.first, iter.second)) {
network_property_updated = true;
}
if (iter.first == shill::kMeteredProperty) {
metered = iter.second.is_bool() && iter.second.GetBool();
}
}
if (network->path() == default_network_path_) {
default_network_is_metered_ = metered && network->IsConnectedState();
}
if (network->Matches(NetworkTypePattern::WiFi() |
NetworkTypePattern::Cellular())) {
network_property_updated |= UpdateBlockedByPolicy(network);
}
network_property_updated |= network->InitialPropertiesReceived(properties);
UpdateGuid(network);
network_list_sorted_ = false;
if (network->Matches(NetworkTypePattern::Cellular())) {
HandleCellularNetworkUpdateReceived(network, had_icccid_before_update);
}
// Notify observers of NetworkState changes.
if (network_property_updated || network->update_requested()) {
// Signal connection state changed after all properties have been updated.
if (ConnectionStateChanged(network, prev_connection_state)) {
// Also notifies that the default network changed if this is the default.
OnNetworkConnectionStateChanged(network);
} else if (network->path() == default_network_path_ &&
network->IsActive()) {
// Always notify that the default network changed for a complete update.
NET_LOG(DEBUG) << "UpdateNetworkStateProperties for default: "
<< NetworkId(network);
NotifyDefaultNetworkChanged(kReasonUpdate);
}
NotifyNetworkPropertiesUpdated(network);
}
}
void NetworkStateHandler::UpdateNetworkServiceProperty(
const std::string& service_path,
const std::string& key,
const base::Value& value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SCOPED_NET_LOG_IF_SLOW();
bool changed = false;
NetworkState* network = GetModifiableNetworkState(service_path);
if (!network) {
return;
}
// When shill::kProfileProperty is updated, the `ProfileListChanged` already
// requests the latest network properties, so we don't need to request again.
if (!network->update_received() && key != shill::kProfileProperty) {
network_service_paths_with_stale_properties_.insert(service_path);
return;
}
std::string prev_connection_state = network->connection_state();
std::string prev_profile_path = network->profile_path();
bool had_icccid_before_update = !network->iccid().empty();
changed |= network->PropertyChanged(key, value);
changed |= UpdateBlockedByPolicy(network);
if (!changed) {
return;
}
// If added or removed from a Profile, request a full update so that a
// NetworkState gets created.
bool request_update = prev_profile_path != network->profile_path();
bool sort_networks = false;
bool notify_default = network->path() == default_network_path_;
bool notify_connection_state = false;
bool notify_active = false;
if (key == shill::kStateProperty || key == shill::kVisibleProperty) {
network_list_sorted_ = false;
if (ConnectionStateChanged(network, prev_connection_state)) {
notify_connection_state = true;
notify_active = true;
if (notify_default) {
notify_default = VerifyDefaultNetworkConnectionStateChange(network);
}
// If the default network connection state changed, sort networks now
// and ensure that a default cellular network exists.
if (notify_default) {
sort_networks = true;
}
// If the connection state changes, other properties such as IPConfig
// may have changed, so request a full update.
request_update = true;
}
} else if (key == shill::kActivationStateProperty) {
// Activation state may affect "connecting" state in the UI.
notify_connection_state = true;
network_list_sorted_ = false;
}
if (network->Matches(NetworkTypePattern::Cellular())) {
HandleCellularNetworkUpdateReceived(network, had_icccid_before_update);
}
if (request_update) {
RequestUpdateForNetwork(service_path);
notify_default = false; // Notify will occur when properties are received.
}
const std::string* value_str = value.GetIfString();
if (key == shill::kSignalStrengthProperty || key == shill::kWifiBSsid ||
key == shill::kWifiFrequency ||
key == shill::kWifiFrequencyListProperty ||
key == shill::kNetworkTechnologyProperty ||
(key == shill::kDeviceProperty && value_str && *value_str == "/")) {
// Uninteresting update. This includes 'Device' property changes to "/"
// (occurs just before a service is removed).
// For non active networks do not log or send any notifications.
if (!network->IsActive()) {
return;
}
// Otherwise do not trigger 'default network changed'.
notify_default = false;
// Notify signal strength and network technology changes for active
// networks.
if (key == shill::kSignalStrengthProperty ||
key == shill::kNetworkTechnologyProperty) {
notify_active = true;
}
}
LogPropertyUpdated(network, key, value);
if (notify_connection_state) {
NotifyNetworkConnectionStateChanged(network);
}
if (notify_default) {
std::stringstream logstream;
logstream << std::string(kReasonUpdate) << ":" << key << "=" << value;
NotifyDefaultNetworkChanged(logstream.str());
}
if (notify_active) {
NotifyIfActiveNetworksChanged();
}
NotifyNetworkPropertiesUpdated(network);
if (sort_networks) {
bool network_list_changed = AddOrRemoveStubCellularNetworks();
SortNetworkList();
if (network_list_changed) {
NotifyNetworkListChanged();
}
}
}
void NetworkStateHandler::UpdateDeviceProperty(const std::string& device_path,
const std::string& key,
const base::Value& value) {
SCOPED_NET_LOG_IF_SLOW();
DeviceState* device = GetModifiableDeviceState(device_path);
if (!device) {
return;
}
if (!device->update_received()) {
device_paths_with_stale_properties_.insert(device_path);
return;
}
const bool was_scanning = device->scanning();
if (!device->PropertyChanged(key, value)) {
return;
}
LogPropertyUpdated(device, key, value);
NotifyDevicePropertiesUpdated(device);
if (key == shill::kScanningProperty && was_scanning != device->scanning()) {
if (device->scanning()) {
NotifyScanStarted(device);
} else {
NotifyScanCompleted(device);
}
if (device->type() == shill::kTypeWifi && !device->scanning()) {
UpdateManagedWifiNetworkAvailable();
}
if (device->type() == shill::kTypeCellular && !device->scanning()) {
UpdateBlockedCellularNetworks();
}
}
if (key == shill::kEapAuthenticationCompletedProperty) {
// Notify a change for each Ethernet service using this device.
NetworkStateList ethernet_services;
GetNetworkListByType(NetworkTypePattern::Ethernet(),
false /* configured_only */, false /* visible_only */,
0 /* no limit */, ðernet_services);
for (NetworkStateList::const_iterator it = ethernet_services.begin();
it != ethernet_services.end(); ++it) {
const NetworkState* ethernet_service = *it;
if (ethernet_service->update_received() ||
ethernet_service->device_path() != device->path()) {
continue;
}
RequestUpdateForNetwork(ethernet_service->path());
}
}
if (key == shill::kSIMSlotInfoProperty) {
// Change in SIM Slot info can result in changes to stub cellular services.
SyncStubCellularNetworks();
}
}
void NetworkStateHandler::UpdateIPConfigProperties(
ManagedState::ManagedType type,
const std::string& path,
const std::string& ip_config_path,
base::Value::Dict properties) {
if (type == ManagedState::MANAGED_TYPE_NETWORK) {
NetworkState* network = GetModifiableNetworkState(path);
if (!network) {
return;
}
network->IPConfigPropertiesChanged(properties);
NotifyNetworkPropertiesUpdated(network);
if (network->path() == default_network_path_) {
NotifyDefaultNetworkChanged(kReasonUpdateIPConfig);
}
if (network->IsActive()) {
NotifyIfActiveNetworksChanged();
}
} else if (type == ManagedState::MANAGED_TYPE_DEVICE) {
DeviceState* device = GetModifiableDeviceState(path);
if (!device) {
return;
}
device->IPConfigPropertiesChanged(ip_config_path, std::move(properties));
NotifyDevicePropertiesUpdated(device);
if (!default_network_path_.empty()) {
const NetworkState* default_network =
GetNetworkState(default_network_path_);
if (default_network && default_network->device_path() == path) {
NotifyNetworkPropertiesUpdated(default_network);
NotifyDefaultNetworkChanged(kReasonUpdateDeviceIPConfig);
}
}
}
}
void NetworkStateHandler::CheckPortalListChanged(
const std::string& check_portal_list) {
check_portal_list_ = check_portal_list;
}
void NetworkStateHandler::HostnameChanged(const std::string& hostname) {
NET_LOG(EVENT) << "HostnameChanged";
hostname_ = hostname;
for (Observer& observer : observers_) {
observer.HostnameChanged(hostname);
}
}
void NetworkStateHandler::TechnologyListChanged() {
// Eventually we would like to replace Technology state with Device state.
// For now, treat technology state changes as device list changes.
NotifyDeviceListChanged();
// Stub cellular networks can be affected by cellular technology state.
SyncStubCellularNetworks();
}
void NetworkStateHandler::ManagedStateListChanged(
ManagedState::ManagedType type) {
SCOPED_NET_LOG_IF_SLOW();
switch (type) {
case ManagedState::MANAGED_TYPE_NETWORK:
AddOrRemoveStubCellularNetworks();
SortNetworkList();
NotifyIfActiveNetworksChanged();
NotifyNetworkListChanged();
UpdateBlockedCellularNetworks();
UpdateManagedWifiNetworkAvailable();
// ManagedStateListChanged only gets executed if all pending updates have
// completed. Profile networks are loaded if a user is logged in and all
// pending updates are complete.
is_profile_networks_loaded_ = is_user_logged_in_;
return;
case ManagedState::MANAGED_TYPE_DEVICE:
std::string devices;
for (auto iter = device_list_.begin(); iter != device_list_.end();
++iter) {
if (iter != device_list_.begin()) {
devices += ", ";
}
devices += (*iter)->name();
}
NET_LOG(EVENT) << "DeviceList: " << devices;
NotifyDeviceListChanged();
// A change to the device list may affect the default Cellular network.
SyncStubCellularNetworks();
return;
}
NOTREACHED();
}
void NetworkStateHandler::SortNetworkList() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (tether_sort_delegate_) {
tether_sort_delegate_->SortTetherNetworkList(&tether_network_list_);
}
// Note: usually active networks will precede inactive networks, however
// this may briefly be untrue during state transitions (e.g. a network may
// transition to idle before the list is updated). Also separate inactive
// Mobile and VPN networks (see below).
ManagedStateList active, non_wifi_visible, wifi_visible, hidden, new_networks;
for (ManagedStateList::iterator iter = network_list_.begin();
iter != network_list_.end(); ++iter) {
NetworkState* network = (*iter)->AsNetworkState();
// NetworkState entries are created when they appear in the list, but the
// details are not populated until an update is received.
if (!network->update_received()) {
new_networks.push_back(std::move(*iter));
continue;
}
if (network->IsActive()) {
active.push_back(std::move(*iter));
continue;
}
if (!network->visible()) {
hidden.push_back(std::move(*iter));
continue;
}
if (NetworkTypePattern::WiFi().MatchesType(network->type())) {
wifi_visible.push_back(std::move(*iter));
} else {
non_wifi_visible.push_back(std::move(*iter));
}
}
// List active networks first (will always include Ethernet).
network_list_ = std::move(active);
// List non wifi visible networks next (Mobile and VPN).
std::move(non_wifi_visible.begin(), non_wifi_visible.end(),
std::back_inserter(network_list_));
// List WiFi networks last.
std::move(wifi_visible.begin(), wifi_visible.end(),
std::back_inserter(network_list_));
// Include hidden and new networks in the list at the end; they should not
// be shown by the UI.
std::move(hidden.begin(), hidden.end(), std::back_inserter(network_list_));
std::move(new_networks.begin(), new_networks.end(),
std::back_inserter(network_list_));
network_list_sorted_ = true;
}
void NetworkStateHandler::DefaultNetworkServiceChanged(
const std::string& service_path) {
// Shill uses '/' for empty service path values; check explicitly for that.
const char kEmptyServicePath[] = "/";
std::string new_service_path =
(service_path != kEmptyServicePath) ? service_path : "";
if (new_service_path == default_network_path_) {
return;
}
if (new_service_path.empty()) {
// If Shill reports that there is no longer a default network but there is
// still an active Tether connection corresponding to the default network,
// return early without changing |default_network_path_|. Observers will be
// notified of the default network change due to a subsequent call to
// SetTetherNetworkStateDisconnected().
const NetworkState* old_default_network = DefaultNetwork();
if (old_default_network && old_default_network->type() == kTypeTether) {
return;
}
}
NET_LOG(EVENT) << "DefaultNetworkServiceChanged: "
<< NetworkPathId(service_path);
if (new_service_path.empty()) {
// Notify that there is no default network.
SetDefaultNetworkValues(/*path=*/std::string(), /*metered=*/false);
NotifyDefaultNetworkChanged(kReasonChange);
return;
}
const NetworkState* network = GetNetworkState(service_path);
if (!network) {
// If NetworkState is not available yet, do not notify observers here,
// they will be notified when the state is received.
NET_LOG(EVENT) << "Default NetworkState not available: "
<< NetworkPathId(service_path);
// Metered will be updated to the correct value when properties arrive.
SetDefaultNetworkValues(service_path, /*metered=*/false);
return;
}
if (!network->tether_guid().empty()) {
DCHECK(network->type() == shill::kTypeWifi);
// If the new default network from Shill's point of view is a Wi-Fi
// network which corresponds to a hotspot for a Tether network, set the
// default network to be the associated Tether network instead.
network = GetNetworkStateFromGuid(network->tether_guid());
if (default_network_path_ != network->path()) {
NET_LOG(DEBUG) << "Tether network is default: " << NetworkId(network);
SetDefaultNetworkValues(network->path(), /*metered=*/true);
NotifyDefaultNetworkChanged(kReasonChange);
}
return;
}
// Request the updated default network properties which will trigger
// NotifyDefaultNetworkChanged().
// Metered will be updated to the correct value when properties arrive.
SetDefaultNetworkValues(service_path, /*metered=*/false);
RequestUpdateForNetwork(service_path);
}
//------------------------------------------------------------------------------
// Private methods
void NetworkStateHandler::UpdateGuid(NetworkState* network) {
std::string specifier = network->GetSpecifier();
DCHECK(!specifier.empty());
if (!network->guid().empty()) {
// If the network is saved in a profile, remove the entry from the map.
// Otherwise ensure that the entry matches the specified GUID. (e.g. in
// case a visible network with a specified guid gets configured with a
// new guid). Exception: Ethernet expects to have a single network and a
// consistent GUID.
if (network->type() != shill::kTypeEthernet && network->IsInProfile()) {
specifier_guid_map_.erase(specifier);
} else {
specifier_guid_map_[specifier] = network->guid();
}
return;
}
// Ensure that the NetworkState has a valid GUID.
std::string guid;
SpecifierGuidMap::iterator guid_iter = specifier_guid_map_.find(specifier);
if (guid_iter != specifier_guid_map_.end()) {
guid = guid_iter->second;
} else {
guid = base::Uuid::GenerateRandomV4().AsLowercaseString();
specifier_guid_map_[specifier] = guid;
}
network->SetGuid(guid);
}
void NetworkStateHandler::HandleCellularNetworkUpdateReceived(
NetworkState* network,
bool had_icccid_before_update) {
const DeviceState* device = GetDeviceState(network->device_path());
if (!device) {
return;
}
// One "roaming" state is shared between all cellular networks.
network->provider_requires_roaming_ = device->provider_requires_roaming();
const std::string& iccid = network->iccid();
// If this network previously did not have an ICCID but just received one via
// a property update, this may indicates that a stub cellular network has
// transitioned to a Shill-backed network.
if (!had_icccid_before_update && !iccid.empty() &&
stub_cellular_networks_provider_) {
std::string stub_service_path, stub_guid;
bool replaced_stub =
stub_cellular_networks_provider_->GetStubNetworkMetadata(
iccid, device, &stub_service_path, &stub_guid);
if (replaced_stub) {
NotifyNetworkIdentifierTransitioned(stub_service_path, network->path(),
stub_guid, network->guid());
}
}
}
bool NetworkStateHandler::AddOrRemoveStubCellularNetworks() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!notifying_network_observers_);
if (!stub_cellular_networks_provider_) {
return false;
}
const DeviceState* device_state =
GetDeviceStateByType(NetworkTypePattern::Cellular());
ManagedStateList new_stub_networks;
bool network_list_changed =
stub_cellular_networks_provider_->AddOrRemoveStubCellularNetworks(
network_list_, new_stub_networks, device_state);
if (!new_stub_networks.size()) {
return network_list_changed;
}
// Newly created stub cellular networks will not have a GUID. Assign GUIDs for
// these new networks and add to network_list_.
for (std::unique_ptr<ManagedState>& managed_state : new_stub_networks) {
NetworkState* network = managed_state->AsNetworkState();
UpdateGuid(network);
}
std::move(new_stub_networks.begin(), new_stub_networks.end(),
std::back_inserter(network_list_));
return true;
}
void NetworkStateHandler::NotifyNetworkListChanged() {
NET_LOG(EVENT) << "NOTIFY: NetworkListChanged. Size: "
<< network_list_.size();
for (Observer& observer : observers_) {
observer.NetworkListChanged();
}
}
void NetworkStateHandler::NotifyDeviceListChanged() {
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: DeviceListChanged. Size: " << device_list_.size();
for (Observer& observer : observers_) {
observer.DeviceListChanged();
}
}
DeviceState* NetworkStateHandler::GetModifiableDeviceState(
const std::string& device_path) const {
ManagedState* managed = GetModifiableManagedState(&device_list_, device_path);
if (!managed) {
return nullptr;
}
return managed->AsDeviceState();
}
DeviceState* NetworkStateHandler::GetModifiableDeviceStateByType(
const NetworkTypePattern& type) const {
for (const auto& device : device_list_) {
if (TypeMatches(device.get(), type)) {
return device->AsDeviceState();
}
}
return nullptr;
}
NetworkState* NetworkStateHandler::GetModifiableNetworkState(
const std::string& service_path) const {
ManagedState* managed =
GetModifiableManagedState(&network_list_, service_path);
if (!managed) {
managed = GetModifiableManagedState(&tether_network_list_, service_path);
if (!managed) {
return nullptr;
}
}
return managed->AsNetworkState();
}
NetworkState* NetworkStateHandler::GetModifiableNetworkStateFromGuid(
const std::string& guid) const {
for (auto iter = tether_network_list_.begin();
iter != tether_network_list_.end(); ++iter) {
NetworkState* tether_network = (*iter)->AsNetworkState();
if (tether_network->guid() == guid) {
return tether_network;
}
}
for (auto iter = network_list_.begin(); iter != network_list_.end(); ++iter) {
NetworkState* network = (*iter)->AsNetworkState();
if (network->guid() == guid) {
return network;
}
}
return nullptr;
}
ManagedState* NetworkStateHandler::GetModifiableManagedState(
const ManagedStateList* managed_list,
const std::string& path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto iter = managed_list->begin(); iter != managed_list->end(); ++iter) {
ManagedState* managed = iter->get();
if (managed->path() == path) {
return managed;
}
}
return nullptr;
}
NetworkStateHandler::ManagedStateList* NetworkStateHandler::GetManagedList(
ManagedState::ManagedType type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
switch (type) {
case ManagedState::MANAGED_TYPE_NETWORK:
return &network_list_;
case ManagedState::MANAGED_TYPE_DEVICE:
return &device_list_;
}
NOTREACHED();
}
void NetworkStateHandler::OnNetworkConnectionStateChanged(
NetworkState* network) {
DCHECK(network);
bool default_changed = false;
if (network->path() == default_network_path_) {
default_changed = VerifyDefaultNetworkConnectionStateChange(network);
}
NotifyNetworkConnectionStateChanged(network);
if (default_changed) {
NotifyDefaultNetworkChanged(kReasonStateChange);
}
}
bool NetworkStateHandler::VerifyDefaultNetworkConnectionStateChange(
NetworkState* network) {
DCHECK(network->path() == default_network_path_);
if (network->IsConnectedState() ||
NetworkState::StateIsPortalled(network->connection_state())) {
return true;
}
if (network->IsConnectingState()) {
// Wait until the network is actually connected to notify that the default
// network changed.
NET_LOG(DEBUG) << "Default network is connecting: " << NetworkId(network)
<< "State: " << network->connection_state();
return false;
}
NET_LOG(DEBUG) << "Default network not connected: " << NetworkId(network);
return false;
}
void NetworkStateHandler::NotifyNetworkConnectionStateChanged(
NetworkState* network) {
DCHECK(network);
SCOPED_NET_LOG_IF_SLOW();
std::string desc = "NetworkConnectionStateChanged";
if (network->path() == default_network_path_) {
desc = "Default" + desc;
}
NET_LOG(EVENT) << "NOTIFY: " << desc << ": " << NetworkId(network) << ": "
<< network->connection_state();
notifying_network_observers_ = true;
for (Observer& observer : observers_) {
observer.NetworkConnectionStateChanged(network);
}
notifying_network_observers_ = false;
NotifyIfActiveNetworksChanged();
}
void NetworkStateHandler::NotifyDefaultNetworkChanged(
const std::string& log_reason) {
SCOPED_NET_LOG_IF_SLOW();
// If the default network is in an invalid state, |default_network_path_|
// will be cleared; call DefaultNetworkChanged(nullptr).
const NetworkState* default_network;
if (default_network_path_.empty()) {
default_network = nullptr;
} else {
default_network = GetModifiableNetworkState(default_network_path_);
DCHECK(default_network) << "No default network: " << default_network_path_;
}
NET_LOG(EVENT) << "NOTIFY: DefaultNetworkChanged: "
<< NetworkId(default_network) << ": " << log_reason;
notifying_network_observers_ = true;
for (Observer& observer : observers_) {
observer.DefaultNetworkChanged(default_network);
}
notifying_network_observers_ = false;
UpdatePortalStateAndNotify(default_network);
}
void NetworkStateHandler::UpdatePortalStateAndNotify(
const NetworkState* default_network) {
NetworkState::PortalState new_portal_state;
std::string new_default_network_path;
if (default_network &&
(default_network->portal_state() != default_network_portal_state_ ||
default_network->proxy_config() != default_network_proxy_config_)) {
new_portal_state = default_network->portal_state();
new_default_network_path = default_network->path();
if (default_network->proxy_config()) {
default_network_proxy_config_ = default_network->proxy_config()->Clone();
} else {
default_network_proxy_config_.reset();
}
} else if (!default_network && (default_network_portal_state_ !=
NetworkState::PortalState::kUnknown ||
default_network_proxy_config_.has_value())) {
new_portal_state = NetworkState::PortalState::kUnknown;
default_network_proxy_config_.reset();
} else {
// No portal state changes.
return;
}
// Update metrics.
if (new_default_network_path != default_network_path_) {
// When the default network changes, update time histograms with a 0 result
// to indicate a failure to transition to online.
if (time_in_portal_) {
SendPortalHistogramTimes(base::TimeDelta());
time_in_portal_.reset();
}
} else {
switch (new_portal_state) {
case NetworkState::PortalState::kUnknown:
// If we transition to an unknown state, update time histograms with a 0
// result to indicate a failure to transition to online.
if (time_in_portal_) {
SendPortalHistogramTimes(base::TimeDelta());
time_in_portal_.reset();
}
break;
case NetworkState::PortalState::kOnline:
if (time_in_portal_) {
SendPortalHistogramTimes(time_in_portal_->Elapsed());
time_in_portal_.reset();
}
break;
case NetworkState::PortalState::kPortalSuspected:
[[fallthrough]];
case NetworkState::PortalState::kPortal:
time_in_portal_ = base::ElapsedTimer();
break;
case NetworkState::PortalState::kNoInternet:
// We don't track these states, reset the timer.
time_in_portal_.reset();
break;
}
}
// Update the portal state after sending histograms.
default_network_portal_state_ = new_portal_state;
// Notify observers.
NET_LOG(EVENT) << "NOTIFY: PortalStateChanged: "
<< GetLogName(default_network) << ": "
<< default_network_portal_state_;
for (Observer& observer : observers_) {
observer.PortalStateChanged(default_network, default_network_portal_state_);
}
}
void NetworkStateHandler::SendPortalHistogramTimes(base::TimeDelta elapsed) {
switch (default_network_portal_state_) {
case NetworkState::PortalState::kPortal:
base::UmaHistogramMediumTimes("Network.RedirectFoundToOnlineTime",
elapsed);
break;
case NetworkState::PortalState::kPortalSuspected:
base::UmaHistogramMediumTimes("Network.PortalSuspectedToOnlineTime",
elapsed);
break;
default:
// Previous state was not portalled, no times to report.
break;
}
}
bool NetworkStateHandler::ActiveNetworksChanged(
const NetworkStateList& active_networks) {
if (active_networks.size() != active_network_list_.size()) {
return true;
}
for (size_t i = 0; i < active_network_list_.size(); ++i) {
if (!active_network_list_[i].MatchesNetworkState(active_networks[i])) {
return true;
}
}
return false;
}
void NetworkStateHandler::NotifyIfActiveNetworksChanged() {
SCOPED_NET_LOG_IF_SLOW();
NetworkStateList active_networks;
GetActiveNetworkListByType(NetworkTypePattern::Default(), &active_networks);
if (!ActiveNetworksChanged(active_networks)) {
return;
}
NET_LOG(EVENT) << "NOTIFY:ActiveNetworksChanged";
active_network_list_.clear();
active_network_list_.reserve(active_networks.size());
for (const NetworkState* network : active_networks) {
active_network_list_.emplace_back(network);
}
notifying_network_observers_ = true;
for (Observer& observer : observers_) {
observer.ActiveNetworksChanged(active_networks);
}
notifying_network_observers_ = false;
}
void NetworkStateHandler::NotifyNetworkPropertiesUpdated(
const NetworkState* network) {
// Skip property updates before NetworkState::InitialPropertiesReceived.
if (network->type().empty()) {
return;
}
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: NetworkPropertiesUpdated: " << NetworkId(network);
notifying_network_observers_ = true;
for (Observer& observer : observers_) {
observer.NetworkPropertiesUpdated(network);
}
notifying_network_observers_ = false;
}
void NetworkStateHandler::NotifyDevicePropertiesUpdated(
const DeviceState* device) {
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: DevicePropertiesUpdated: " << device->path();
for (Observer& observer : observers_) {
observer.DevicePropertiesUpdated(device);
}
}
void NetworkStateHandler::NotifyScanRequested(const NetworkTypePattern& type) {
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: ScanRequested";
for (Observer& observer : observers_) {
observer.ScanRequested(type);
}
}
void NetworkStateHandler::NotifyScanCompleted(const DeviceState* device) {
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: ScanCompleted for: " << device->path();
for (Observer& observer : observers_) {
observer.ScanCompleted(device);
}
}
void NetworkStateHandler::NotifyScanStarted(const DeviceState* device) {
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: ScanStarted for: " << device->path();
for (Observer& observer : observers_) {
observer.ScanStarted(device);
}
}
void NetworkStateHandler::NotifyNetworkIdentifierTransitioned(
const std::string& old_service_path,
const std::string& new_service_path,
const std::string& old_guid,
const std::string& new_guid) {
SCOPED_NET_LOG_IF_SLOW();
NET_LOG(EVENT) << "NOTIFY: NetworkIdentifierTransitioned: "
<< "Service path: " << old_service_path << " => "
<< new_service_path << ", GUID: " << old_guid << " => "
<< new_guid;
for (Observer& observer : observers_) {
observer.NetworkIdentifierTransitioned(old_service_path, new_service_path,
old_guid, new_guid);
}
}
void NetworkStateHandler::LogPropertyUpdated(const ManagedState* state,
const std::string& key,
const base::Value& value) {
std::string type_str =
state->managed_type() == ManagedState::MANAGED_TYPE_DEVICE ? "Device"
: state->path() == default_network_path_ ? "DefaultNetwork"
: "Network";
device_event_log::LogLevel log_level = device_event_log::LOG_LEVEL_EVENT;
if (key == shill::kSignalStrengthProperty && !state->IsActive()) {
log_level = device_event_log::LOG_LEVEL_DEBUG;
}
DEVICE_LOG(::device_event_log::LOG_TYPE_NETWORK, log_level)
<< type_str << "PropertyUpdated: " << GetLogName(state) << ", " << key
<< " = " << value;
}
std::string NetworkStateHandler::GetTechnologyForType(
const NetworkTypePattern& type) const {
if (type.MatchesType(shill::kTypeEthernet)) {
return shill::kTypeEthernet;
}
if (type.MatchesType(shill::kTypeWifi)) {
return shill::kTypeWifi;
}
if (type.MatchesType(shill::kTypeCellular)) {
return shill::kTypeCellular;
}
if (type.MatchesType(kTypeTether)) {
return kTypeTether;
}
NET_LOG(ERROR) << "Unexpected Type for technology: " << type.ToDebugString();
return std::string();
}
std::vector<std::string> NetworkStateHandler::GetTechnologiesForType(
const NetworkTypePattern& type) const {
std::vector<std::string> technologies;
if (type.MatchesType(shill::kTypeEthernet)) {
technologies.emplace_back(shill::kTypeEthernet);
}
if (type.MatchesType(shill::kTypeWifi)) {
technologies.emplace_back(shill::kTypeWifi);
}
if (type.MatchesType(shill::kTypeCellular)) {
technologies.emplace_back(shill::kTypeCellular);
}
if (type.MatchesType(shill::kTypeVPN)) {
technologies.emplace_back(shill::kTypeVPN);
}
if (type.MatchesType(kTypeTether)) {
technologies.emplace_back(kTypeTether);
}
CHECK_GT(technologies.size(), 0ul);
return technologies;
}
void NetworkStateHandler::SetDefaultNetworkValues(const std::string& path,
bool metered) {
// If the default network changes, ensure that the portal state is updated.
if (!path.empty()) {
UpdatePortalStateAndNotify(GetNetworkState(path));
}
default_network_path_ = path;
default_network_is_metered_ = metered;
}
void NetworkStateHandler::ProcessIsUserLoggedIn(
const base::Value::List& profile_list) {
// The profile list contains the shared profile on the login screen. Once the
// user is logged in there is more than one profile in the profile list.
is_user_logged_in_ = profile_list.size() > 1;
}
} // namespace ash
|