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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/accessibility/browser_accessibility_android.h"
#include <algorithm>
#include "base/check_deref.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/break_iterator.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/accessibility/ax_style_data.h"
#include "content/browser/accessibility/browser_accessibility_manager_android.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "skia/ext/skia_utils_base.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/android/accessibility_state.h"
#include "ui/accessibility/ax_assistant_structure.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_position.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_selection.h"
#include "ui/accessibility/platform/ax_android_constants.h"
#include "ui/accessibility/platform/ax_unique_id.h"
#include "ui/accessibility/platform/browser_accessibility.h"
#include "ui/strings/grit/auto_image_annotation_strings.h"
#include "ui/strings/grit/ax_strings.h"
namespace {
// These are enums from android.text.InputType in Java:
enum {
ANDROID_TEXT_INPUTTYPE_TYPE_NULL = 0,
ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME = 0x4,
ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE = 0x14,
ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME = 0x24,
ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER = 0x2,
ANDROID_TEXT_INPUTTYPE_TYPE_PHONE = 0x3,
ANDROID_TEXT_INPUTTYPE_TYPE_TEXT = 0x1,
ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI = 0x11,
ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EDIT_TEXT = 0xa1,
ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL = 0xd1,
ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD = 0xe1
};
// These are enums from android.view.View in Java:
enum {
ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE = 0,
ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE = 1,
ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE = 2
};
// These are enums from android.view.accessibility.AccessibilityNodeInfo in
// Java:
enum {
ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_UNDEFINED = 0,
ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_COLLAPSED = 1,
ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_PARTIAL = 2,
ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_FULL = 3,
};
// These are enums from
// android.view.accessibility.AccessibilityNodeInfo.RangeInfo in Java:
enum { ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT = 1 };
// These define reasons a node may be marked as clickable and provide a
// relative score to AT. Higher means more likely to be the clickable node.
enum {
kNotClickable = 0,
kHasClickAncestor = 100,
kHasClickListener = 200,
kHasClickListenerAndIsControl = 300
};
// These are enums from
// android.view.accessibility.AccessibilityNodeInfo.CollectionInfo in Java:
enum {
ANDROID_VIEW_ACCESSIBILITY_SELECTION_MODE_NONE = 0,
ANDROID_VIEW_ACCESSIBILITY_SELECTION_MODE_SINGLE = 1,
ANDROID_VIEW_ACCESSIBILITY_SELECTION_MODE_MULTIPLE = 2,
};
// These are enums from
// android.view.accessibility.AccessibilityNodeInfo.CheckedState in Java:
enum {
ANDROID_VIEW_ACCESSIBILITY_CHECKED_STATE_FALSE = 0,
ANDROID_VIEW_ACCESSIBILITY_CHECKED_STATE_TRUE = 1,
ANDROID_VIEW_ACCESSIBILITY_CHECKED_STATE_PARTIAL = 2,
};
using AXStyleData = content::AXStyleData;
using UniqueIdMap =
absl::flat_hash_map<int32_t, content::BrowserAccessibilityAndroid*>;
using LeafMap =
absl::flat_hash_map<const content::BrowserAccessibilityAndroid*, bool>;
// Map from each AXPlatformNode's unique id to its instance.
UniqueIdMap& GetUniqueIdMap() {
static base::NoDestructor<UniqueIdMap> unique_id_map;
return *unique_id_map;
}
// Map from BrowserAccessibilityAndroid nodes to whether they qualify as a
// "leaf". Must be cleared on any tree mutation.
LeafMap& GetLeafMap() {
static base::NoDestructor<LeafMap> leaf_map;
return *leaf_map;
}
// Populates the computed style data from `node` into `style_data`.
// Non-opaque colors also are blended from ancestors.
void PopulateStyleData(const content::BrowserAccessibilityAndroid& node,
const std::u16string& parent_text,
const std::u16string& text,
AXStyleData* style_data) {
if (!style_data) {
return;
}
int start = parent_text.size();
int end = start + text.size();
if (node.IsTextField()) {
std::vector<int> suggestion_starts;
std::vector<int> suggestion_ends;
node.GetSuggestions(&suggestion_starts, &suggestion_ends);
CHECK_EQ(suggestion_starts.size(), suggestion_ends.size());
for (size_t i = 0; i < suggestion_starts.size(); ++i) {
// TODO: crbug.com/425974312 - Currently we don't retrieve the text of
// each suggestion, so store a blank string for now.
AXStyleData::AddRange(style_data->suggestions, std::u16string(),
start + suggestion_starts[i],
start + suggestion_ends[i]);
}
}
if (ui::IsLink(node.GetRole())) {
AXStyleData::AddRange(style_data->links, node.GetTargetUrl(), start, end);
}
if (node.GetRole() == ax::mojom::Role::kStaticText) {
if (node.HasFloatAttribute(ax::mojom::FloatAttribute::kFontSize)) {
// Zero font size is valid in CSS, which makes the text invisible.
if (float size = node.GetTextSize(); size >= 0) {
AXStyleData::AddRange(style_data->text_sizes, size, start, end);
}
}
if (node.GetTextStyle() != 0) {
// GetTextStyle returns a bit field shifted by ax::mojom::TextStyle enum
// values, so we need to parse out the individual enum values. See:
// https://source.chromium.org/chromium/chromium/src/+/main:ui/accessibility/ax_node_data.cc?q=HasTextStyle
for (int i = static_cast<int>(ax::mojom::TextStyle::kMinValue);
i <= static_cast<int>(ax::mojom::TextStyle::kMaxValue); ++i) {
ax::mojom::TextStyle style = static_cast<ax::mojom::TextStyle>(i);
if (style != ax::mojom::TextStyle::kNone && node.HasTextStyle(style)) {
AXStyleData::AddRange(style_data->text_styles, style, start, end);
}
}
}
if (auto pos = static_cast<ax::mojom::TextPosition>(node.GetTextPosition());
pos != ax::mojom::TextPosition::kNone) {
AXStyleData::AddRange(style_data->text_positions, pos, start, end);
}
// GetColor() gets the blended color.
AXStyleData::AddRange(style_data->foreground_colors,
static_cast<int>(node.GetColor()), start, end);
// GetBackgroundColor() gets blended background color.
AXStyleData::AddRange(style_data->background_colors,
static_cast<int>(node.GetBackgroundColor()), start,
end);
if (const auto& family = node.GetInheritedFontFamilyName();
!family.empty()) {
AXStyleData::AddRange(style_data->font_families, std::move(family), start,
end);
}
// GetLanguage() gets the inherited language locale.
if (const auto& lang = node.GetLanguage(); !lang.empty()) {
AXStyleData::AddRange(style_data->locales, std::move(lang), start, end);
}
}
}
} // namespace
namespace ui {
// static
std::unique_ptr<BrowserAccessibility> BrowserAccessibility::Create(
BrowserAccessibilityManager* manager,
AXNode* node) {
return base::WrapUnique(
new content::BrowserAccessibilityAndroid(manager, node));
}
} // namespace ui
namespace content {
// static
BrowserAccessibilityAndroid* BrowserAccessibilityAndroid::GetFromUniqueId(
int32_t unique_id) {
const UniqueIdMap& unique_ids = GetUniqueIdMap();
auto iter = unique_ids.find(unique_id);
if (iter != unique_ids.end()) {
return iter->second;
}
return nullptr;
}
// static
void BrowserAccessibilityAndroid::ResetLeafCache() {
GetLeafMap().clear();
}
BrowserAccessibilityAndroid::BrowserAccessibilityAndroid(
ui::BrowserAccessibilityManager* manager,
ui::AXNode* node)
: BrowserAccessibility(manager, node) {
GetUniqueIdMap()[GetUniqueId()] = this;
}
BrowserAccessibilityAndroid::~BrowserAccessibilityAndroid() {
if (auto id = GetUniqueId()) {
GetUniqueIdMap().erase(id);
}
}
std::u16string BrowserAccessibilityAndroid::GetLocalizedString(
int message_id) const {
return CHECK_DEREF(GetContentClient()).GetLocalizedString(message_id);
}
void BrowserAccessibilityAndroid::OnLocationChanged() {
auto* manager =
static_cast<BrowserAccessibilityManagerAndroid*>(this->manager());
manager->FireLocationChanged(this);
}
std::u16string
BrowserAccessibilityAndroid::GetLocalizedStringForImageAnnotationStatus(
ax::mojom::ImageAnnotationStatus status) const {
// Default to standard text, except for special case of eligible.
if (status != ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation) {
return BrowserAccessibility::GetLocalizedStringForImageAnnotationStatus(
status);
}
return GetLocalizedString(IDS_AX_IMAGE_ELIGIBLE_FOR_ANNOTATION_ANDROID);
}
void BrowserAccessibilityAndroid::AppendTextToString(
std::u16string extra_text,
std::u16string* string) const {
if (extra_text.empty()) {
return;
}
if (string->empty()) {
*string = std::move(extra_text);
return;
}
base::StrAppend(string, {u", ", std::move(extra_text)});
}
bool BrowserAccessibilityAndroid::IsCheckable() const {
return GetData().HasCheckedState();
}
bool BrowserAccessibilityAndroid::IsChecked() const {
return GetData().GetCheckedState() == ax::mojom::CheckedState::kTrue;
}
bool BrowserAccessibilityAndroid::IsClickable() const {
// If it has a custom default action verb except for
// ax::mojom::DefaultActionVerb::kClickAncestor, it's definitely clickable.
// ax::mojom::DefaultActionVerb::kClickAncestor is used when an element with a
// click listener is present in its ancestry chain.
if (HasIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb) &&
(GetData().GetDefaultActionVerb() !=
ax::mojom::DefaultActionVerb::kClickAncestor)) {
return true;
}
if (IsHeadingLink()) {
return true;
}
// Skip web areas, PDFs and iframes, they're focusable but not clickable.
if (ui::IsIframe(GetRole()) || ui::IsPlatformDocument(GetRole())) {
return false;
}
// Otherwise it's clickable if it's a control. We include disabled nodes
// because TalkBack won't announce a control as disabled unless it's also
// marked as clickable. In other words, Talkback wants to know if the control
// might be clickable, if it wasn't disabled.
return ui::IsControlOnAndroid(GetRole(), IsFocusable());
}
bool BrowserAccessibilityAndroid::IsCollapsed() const {
return HasState(ax::mojom::State::kCollapsed);
}
bool BrowserAccessibilityAndroid::IsCollection() const {
switch (GetRole()) {
case ax::mojom::Role::kDescriptionList:
case ax::mojom::Role::kList:
case ax::mojom::Role::kListBox:
case ax::mojom::Role::kTree:
case ax::mojom::Role::kMenu:
case ax::mojom::Role::kMenuBar:
case ax::mojom::Role::kMenuListPopup:
return true;
default:
return ui::IsTableLike(GetRole());
}
}
bool BrowserAccessibilityAndroid::IsCollectionItem() const {
switch (GetRole()) {
case ax::mojom::Role::kListBoxOption:
case ax::mojom::Role::kListItem:
case ax::mojom::Role::kTerm:
case ax::mojom::Role::kTreeItem:
case ax::mojom::Role::kMenuItem:
case ax::mojom::Role::kMenuItemCheckBox:
case ax::mojom::Role::kMenuItemRadio:
case ax::mojom::Role::kMenuListOption:
return true;
default:
return ui::IsCellOrTableHeader(GetRole());
}
}
bool BrowserAccessibilityAndroid::IsContentInvalid() const {
return HasIntAttribute(ax::mojom::IntAttribute::kInvalidState) &&
GetData().GetInvalidState() != ax::mojom::InvalidState::kFalse;
}
bool BrowserAccessibilityAndroid::IsDisabledDescendant() const {
// Iterate over parents and see if any are disabled.
BrowserAccessibilityAndroid* parent =
static_cast<BrowserAccessibilityAndroid*>(PlatformGetParent());
while (parent != nullptr) {
if (!parent->IsEnabled()) {
return true;
}
parent =
static_cast<BrowserAccessibilityAndroid*>(parent->PlatformGetParent());
}
return false;
}
bool BrowserAccessibilityAndroid::IsEnabled() const {
switch (GetData().GetRestriction()) {
case ax::mojom::Restriction::kNone:
return true;
case ax::mojom::Restriction::kReadOnly:
case ax::mojom::Restriction::kDisabled:
// On Android, both Disabled and ReadOnly are treated the same.
// For both of them, we set AccessibilityNodeInfo.IsEnabled to false
// and we don't expose certain actions like SET_VALUE and PASTE.
return false;
}
NOTREACHED();
}
bool BrowserAccessibilityAndroid::IsExpanded() const {
return HasState(ax::mojom::State::kExpanded);
}
bool BrowserAccessibilityAndroid::IsFocusable() const {
// If it's an iframe element, only mark it as focusable if the element has an
// explicit name. Otherwise mark it as not focusable to avoid the user landing
// on empty container elements in the tree.
if (ui::IsIframe(GetRole()) ||
(ui::IsPlatformDocument(GetRole()) && PlatformGetParent())) {
return HasStringAttribute(ax::mojom::StringAttribute::kName);
}
return BrowserAccessibility::IsFocusable();
}
bool BrowserAccessibilityAndroid::IsFormDescendant() const {
// Iterate over parents and see if any are a form.
const BrowserAccessibility* parent = PlatformGetParent();
while (parent != nullptr) {
if (ui::IsForm(parent->GetRole())) {
return true;
}
parent = parent->PlatformGetParent();
}
return false;
}
bool BrowserAccessibilityAndroid::IsHeading() const {
BrowserAccessibilityAndroid* parent =
static_cast<BrowserAccessibilityAndroid*>(PlatformGetParent());
if (parent && parent->IsHeading()) {
return true;
}
return ui::IsHeading(GetRole());
}
bool BrowserAccessibilityAndroid::IsHierarchical() const {
return (GetRole() == ax::mojom::Role::kTree || IsHierarchicalList());
}
bool BrowserAccessibilityAndroid::IsMultiLine() const {
return HasState(ax::mojom::State::kMultiline);
}
bool BrowserAccessibilityAndroid::IsMultiselectable() const {
return HasState(ax::mojom::State::kMultiselectable);
}
bool BrowserAccessibilityAndroid::IsRangeControlWithoutAriaValueText() const {
return GetData().IsRangeValueSupported() &&
!HasStringAttribute(ax::mojom::StringAttribute::kValue) &&
HasFloatAttribute(ax::mojom::FloatAttribute::kValueForRange);
}
bool BrowserAccessibilityAndroid::IsRequired() const {
return HasState(ax::mojom::State::kRequired);
}
bool BrowserAccessibilityAndroid::IsScrollable() const {
return GetBoolAttribute(ax::mojom::BoolAttribute::kScrollable);
}
bool BrowserAccessibilityAndroid::IsSeekControl() const {
// Range types should have seek control options, except progress bars.
return GetData().IsRangeValueSupported() &&
(GetRole() != ax::mojom::Role::kProgressIndicator);
}
bool BrowserAccessibilityAndroid::IsSelected() const {
return GetBoolAttribute(ax::mojom::BoolAttribute::kSelected);
}
bool BrowserAccessibilityAndroid::IsSlider() const {
return GetRole() == ax::mojom::Role::kSlider;
}
bool BrowserAccessibilityAndroid::IsSubscript() const {
return static_cast<ax::mojom::TextPosition>(GetTextPosition()) ==
ax::mojom::TextPosition::kSubscript;
}
bool BrowserAccessibilityAndroid::IsSuperscript() const {
return static_cast<ax::mojom::TextPosition>(GetTextPosition()) ==
ax::mojom::TextPosition::kSuperscript;
}
bool BrowserAccessibilityAndroid::IsTableHeader() const {
return ui::IsTableHeader(GetRole());
}
bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
return !IsInvisibleOrIgnored();
}
bool BrowserAccessibilityAndroid::ShouldUsePaneTitle() const {
// Dialogs should use paneTitles, as well as comboboxes but only when the
// combobox is expanded.
return ui::IsDialog(GetRole()) || (ui::IsComboBox(GetRole()) && IsExpanded());
}
bool BrowserAccessibilityAndroid::IsInterestingOnAndroid() const {
// The root is not interesting if it doesn't have a title, even
// though it's focusable.
if (ui::IsPlatformDocument(GetRole()) &&
GetSubstringTextContentUTF16(1).empty()) {
return false;
}
// Mark as uninteresting if it's hidden, even if it is focusable.
if (IsInvisibleOrIgnored()) {
return false;
}
// Walk up the ancestry. A non-focusable child of a control is not
// interesting. A child of an invisible iframe is also not interesting.
// A link is never a leaf node so that its children can be navigated
// when swiping by heading, landmark, etc. So we will also mark the
// children of a link as not interesting to prevent double utterances.
const BrowserAccessibility* parent = PlatformGetParent();
// Should not read options in a multiselect combobox as it is invisible.
// Adding IsFocusable() to handle an edge case in crbug.com/395134019 to allow
// select options in aria list box. This is also able to handle edge case in
// crbug.com/358195473 to not allow TalkBack to read out collapsed
// multi-selectable options.
if (parent && parent->GetRole() == ax::mojom::Role::kListBox &&
parent->HasState(ax::mojom::State::kMultiselectable) &&
GetRole() == ax::mojom::Role::kListBoxOption && IsFocusable()) {
return false;
}
// Allows users to select options in a listbox with touch interaction.
if (GetRole() == ax::mojom::Role::kListBoxOption) {
return true;
}
while (parent) {
if (ui::IsControl(parent->GetRole()) && !IsFocusable()) {
return false;
}
if (parent->GetRole() == ax::mojom::Role::kIframe &&
parent->IsInvisibleOrIgnored()) {
return false;
}
if (parent->GetRole() == ax::mojom::Role::kLink) {
return false;
}
parent = parent->PlatformGetParent();
}
// A kMenu container should not be interesting and navigatable.
if (GetRole() == ax::mojom::Role::kMenu) {
return false;
}
// Otherwise, focusable nodes are always interesting. Note that IsFocusable()
// already skips over things like iframes and child frames that are
// technically focusable but shouldn't be exposed as focusable on Android.
if (IsFocusable()) {
return true;
}
// If it's not focusable but has a control role, then it's interesting.
if (ui::IsControl(GetRole())) {
return true;
}
// Mark progress indicators as interesting, since they are not focusable and
// not a control, but users should be able to swipe/navigate to them.
if (GetRole() == ax::mojom::Role::kProgressIndicator) {
return true;
}
// If we are the direct descendant of a link and have no siblings/children,
// then we are not interesting, return false
parent = PlatformGetParent();
if (parent != nullptr && ui::IsLink(parent->GetRole()) &&
parent->PlatformChildCount() == 1 && PlatformChildCount() == 0) {
return false;
}
// Otherwise, the interesting nodes are leaf nodes with non-whitespace accessible name.
return IsLeaf() && !base::ContainsOnlyChars(GetAccessibleNameUTF16(),
base::kWhitespaceUTF16);
}
bool BrowserAccessibilityAndroid::IsHeadingLink() const {
if (!(GetRole() == ax::mojom::Role::kHeading && InternalChildCount() == 1)) {
return false;
}
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(InternalChildrenBegin().get());
return ui::IsLink(child->GetRole());
}
const BrowserAccessibilityAndroid*
BrowserAccessibilityAndroid::GetSoleInterestingNodeFromSubtree() const {
if (IsInterestingOnAndroid()) {
return this;
}
const BrowserAccessibilityAndroid* sole_interesting_node = nullptr;
for (const auto& child : PlatformChildren()) {
const BrowserAccessibilityAndroid* interesting_node =
static_cast<const BrowserAccessibilityAndroid&>(child)
.GetSoleInterestingNodeFromSubtree();
if (interesting_node && sole_interesting_node) {
// If there are two interesting nodes, return nullptr.
return nullptr;
} else if (interesting_node) {
sole_interesting_node = interesting_node;
}
}
return sole_interesting_node;
}
bool BrowserAccessibilityAndroid::AreInlineTextBoxesLoaded() const {
if (IsText()) {
return InternalChildCount() > 0;
}
// Return false if any descendant needs to load inline text boxes.
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
if (!child->AreInlineTextBoxesLoaded()) {
return false;
}
}
// Otherwise return true - either they're all loaded, or there aren't
// any descendants that need to load inline text boxes.
return true;
}
int BrowserAccessibilityAndroid::ClickableScore() const {
// For nodes that do not have the default action verb, return not clickable.
if (!HasIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb)) {
return kNotClickable;
}
switch (GetData().GetDefaultActionVerb()) {
// Differentiate between nodes that are clickable because of an ancestor.
case ax::mojom::DefaultActionVerb::kClickAncestor:
return kHasClickAncestor;
// For all other clickable nodes, check whether the node is also a control
// on Android, and return score based on the result.
case ax::mojom::DefaultActionVerb::kActivate:
case ax::mojom::DefaultActionVerb::kCheck:
case ax::mojom::DefaultActionVerb::kClick:
case ax::mojom::DefaultActionVerb::kJump:
case ax::mojom::DefaultActionVerb::kOpen:
case ax::mojom::DefaultActionVerb::kPress:
case ax::mojom::DefaultActionVerb::kSelect:
case ax::mojom::DefaultActionVerb::kUncheck: {
return ui::IsControlOnAndroid(GetRole(), IsFocusable())
? kHasClickListenerAndIsControl
: kHasClickListener;
}
case ax::mojom::DefaultActionVerb::kNone:
default:
return kNotClickable;
}
}
int BrowserAccessibilityAndroid::ExpandedState() const {
if (IsExpanded() && IsCollapsed()) {
return ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_PARTIAL;
} else if (IsExpanded()) {
return ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_FULL;
} else if (IsCollapsed()) {
return ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_COLLAPSED;
} else {
return ANDROID_VIEW_ACCESSIBILITY_EXPANDED_STATE_UNDEFINED;
}
}
bool BrowserAccessibilityAndroid::CanOpenPopup() const {
return HasIntAttribute(ax::mojom::IntAttribute::kHasPopup);
}
const char* BrowserAccessibilityAndroid::GetClassName() const {
ax::mojom::Role role = GetRole();
if (IsTextField()) {
// On Android, contenteditable needs to be handled the same as any
// other text field.
role = ax::mojom::Role::kTextField;
} else if (IsAndroidTextView()) {
// On Android, we want to report some extra nodes as TextViews. For example,
// a <div> that only contains text, or a <p> that only contains text.
role = ax::mojom::Role::kStaticText;
}
return ui::AXRoleToAndroidClassName(role, PlatformGetParent() != nullptr);
}
bool BrowserAccessibilityAndroid::IsAndroidTextView() const {
return ui::IsAndroidTextViewCandidate(GetRole()) && HasOnlyTextChildren();
}
bool BrowserAccessibilityAndroid::IsChildOfLeaf() const {
BrowserAccessibility* ancestor = InternalGetParent();
while (ancestor) {
if (ancestor->IsLeaf()) {
return true;
}
ancestor = ancestor->InternalGetParent();
}
return false;
}
bool BrowserAccessibilityAndroid::IsLeaf() const {
if (base::Contains(GetLeafMap(), this)) {
return GetLeafMap()[this];
}
if (BrowserAccessibility::IsLeaf()) {
return true;
}
// Document roots (e.g. kRootWebArea and kPdfRoot), and iframes are always
// allowed to contain children.
if (ui::IsIframe(GetRole()) || ui::IsPlatformDocument(GetRole())) {
return false;
}
// Button, date and time controls should not expose their children to Android
// accessibility APIs.
switch (GetRole()) {
case ax::mojom::Role::kButton:
case ax::mojom::Role::kDate:
case ax::mojom::Role::kDateTime:
case ax::mojom::Role::kInputTime:
return true;
default:
break;
}
// Links are never leaves.
if (ui::IsLink(GetRole())) {
return false;
}
// Listboxes with children should never be a leaf node.
if (GetRole() == ax::mojom::Role::kListBox && InternalChildCount() > 0) {
return false;
}
// For Android only, tab-panels and tab-lists are never leaves. We do this to
// temporarily get around the gap for aria-labelledby in the Android API.
// See b/241526393.
if (GetRole() == ax::mojom::Role::kTabPanel ||
GetRole() == ax::mojom::Role::kTabList) {
return false;
}
// Focusable nodes with name from attribute should never drop children.
if (HasState(ax::mojom::State::kFocusable) &&
HasIntAttribute(ax::mojom::IntAttribute::kNameFrom) &&
GetNameFrom() == ax::mojom::NameFrom::kAttribute) {
// We exclude menuItems and comboBoxMenuButtons to prevent double utterance.
if (GetRole() != ax::mojom::Role::kMenuItem &&
GetRole() != ax::mojom::Role::kComboBoxMenuButton) {
return false;
}
}
BrowserAccessibilityManagerAndroid* manager_android =
static_cast<BrowserAccessibilityManagerAndroid*>(manager());
if (manager_android->prune_tree_for_screen_reader()) {
// For some nodes, we will consider children before determining if the node
// is a leaf. For nodes with relevant children, we will return false here
// and allow the child nodes to be set as a leaf.
// Headings with text can drop their children (with exceptions).
std::u16string name = GetSubstringTextContentUTF16(1);
if (GetRole() == ax::mojom::Role::kHeading && !name.empty()) {
bool ret = IsLeafConsideringChildren();
GetLeafMap()[this] = ret;
return ret;
}
// Focusable nodes with text can drop their children (with exceptions).
if (HasState(ax::mojom::State::kFocusable) && !name.empty()) {
bool ret = IsLeafConsideringChildren();
GetLeafMap()[this] = ret;
return ret;
}
// Nodes with only static text can drop their children, with the exception
// that list markers have a different role and should not be dropped.
if (HasOnlyTextChildren() && !HasListMarkerChild()) {
GetLeafMap()[this] = true;
return true;
}
}
GetLeafMap()[this] = false;
return false;
}
bool BrowserAccessibilityAndroid::IsLeafConsideringChildren() const {
// This is called from IsLeaf, so don't call PlatformChildCount
// from within this!
// Check for any children that should be exposed and return false if found (by
// returning false we are saying the parent node is NOT a leaf and this child
// node should instead be the leaf).
//
// If a node has a child that meets any of these criteria, it is NOT a leaf:
//
// * child is focusable, and NOT a menu option
// * child is a table, cell, or row
//
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibility* child = it.get();
if (child->HasState(ax::mojom::State::kFocusable) &&
child->GetRole() != ax::mojom::Role::kMenuListOption) {
return false;
}
if (child->GetRole() == ax::mojom::Role::kTable ||
child->GetRole() == ax::mojom::Role::kCell ||
child->GetRole() == ax::mojom::Role::kGridCell ||
child->GetRole() == ax::mojom::Role::kRow ||
child->GetRole() == ax::mojom::Role::kLayoutTable ||
child->GetRole() == ax::mojom::Role::kLayoutTableCell ||
child->GetRole() == ax::mojom::Role::kLayoutTableRow) {
return false;
}
// Check nested children and return false if any meet above criteria.
if (!static_cast<BrowserAccessibilityAndroid*>(child)
->IsLeafConsideringChildren()) {
return false;
}
}
// If no such children were found, return true signaling the parent node can
// be the leaf node.
return true;
}
std::u16string BrowserAccessibilityAndroid::GetBrailleLabel() const {
if (HasStringAttribute(ax::mojom::StringAttribute::kAriaBrailleLabel)) {
return GetString16Attribute(ax::mojom::StringAttribute::kAriaBrailleLabel);
}
return std::u16string();
}
std::u16string BrowserAccessibilityAndroid::GetBrailleRoleDescription() const {
if (HasStringAttribute(
ax::mojom::StringAttribute::kAriaBrailleRoleDescription)) {
return GetString16Attribute(
ax::mojom::StringAttribute::kAriaBrailleRoleDescription);
}
return std::u16string();
}
std::u16string BrowserAccessibilityAndroid::GetTextContentUTF16() const {
return GetSubstringTextContentUTF16(std::nullopt);
}
int BrowserAccessibilityAndroid::GetTextContentLengthUTF16() const {
return GetTextContentUTF16().length();
}
std::u16string BrowserAccessibilityAndroid::GetSubstringTextContentUTF16(
std::optional<size_t> min_length,
AXStyleData* style_data) const {
std::u16string text;
AccumulateSubstringTextContentUTF16(&text, min_length, style_data);
return text;
}
void BrowserAccessibilityAndroid::AccumulateSubstringTextContentUTF16(
std::u16string* accumulated_text,
std::optional<size_t> min_length,
AXStyleData* style_data) const {
CHECK(accumulated_text);
std::u16string text;
base::ScopedClosureRunner accumulate_text_and_styling(base::BindOnce(
[](const BrowserAccessibilityAndroid& node, std::u16string* dest,
std::u16string& src, AXStyleData* style_data) {
PopulateStyleData(node, *dest, src, style_data);
base::StrAppend(dest, {std::move(src)});
},
std::cref(*this), base::Unretained(accumulated_text), std::ref(text),
base::Unretained(style_data)));
if (ui::IsIframe(GetRole())) {
return;
}
// First, always return the `value` attribute if this is an input field.
std::u16string value = GetValueForControl();
if (ShouldExposeValueAsName(value)) {
text = std::move(value);
return;
}
// For color wells, the color is stored in separate attributes.
// Perhaps we could return color names in the future?
if (GetRole() == ax::mojom::Role::kColorWell) {
unsigned int color = static_cast<unsigned int>(
GetIntAttribute(ax::mojom::IntAttribute::kColorValue));
text = base::UTF8ToUTF16(skia::SkColorToHexString(color));
return;
}
// In the case of accessible name from kAttribute, the aria-label will be
// mapped to one of the container title, content description or supplemental
// description, we should exclude aria-label from mapping to text.
text = IsAccessibleNameFromAttribute() ? u"" : GetNameAsString16();
if (ui::IsRangeValueSupported(GetRole())) {
// For controls that support range values such as sliders, when a non-empty
// name is present (e.g. a label), append this to the value so both the
// valuetext and label are included, rather than replacing the value.
// If the value itself is empty on a progress indicator, then this would
// suggest it is indeterminate, so add that keyword.
if (value.empty() && GetRole() == ax::mojom::Role::kProgressIndicator) {
value = GetLocalizedString(IDS_AX_INDETERMINATE_VALUE);
}
// To prevent extra commas, only add if the text is non-empty
if (!text.empty() && !value.empty()) {
text = base::JoinString({std::move(value), std::move(text)}, u", ");
} else if (!value.empty()) {
text = std::move(value);
}
} else if (text.empty()) {
// When a node does not have a name (e.g. a label), use its value instead.
text = std::move(value);
}
// For almost all focusable nodes we try to get text from contents, but for
// the root node that's redundant and often way too verbose.
if (ui::IsPlatformDocument(GetRole())) {
return;
}
// A role="separator" is a leaf, and cannot get name from contents, even if
// author appends text children.
if (GetRole() == ax::mojom::Role::kSplitter) {
return;
}
// Append image description strings to the text.
auto* manager =
static_cast<BrowserAccessibilityManagerAndroid*>(this->manager());
if (manager->ShouldAllowImageDescriptions()) {
auto status = GetData().GetImageAnnotationStatus();
switch (status) {
case ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kAnnotationPending:
case ax::mojom::ImageAnnotationStatus::kAnnotationEmpty:
case ax::mojom::ImageAnnotationStatus::kAnnotationAdult:
case ax::mojom::ImageAnnotationStatus::kAnnotationProcessFailed:
AppendTextToString(GetLocalizedStringForImageAnnotationStatus(status),
&text);
break;
case ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded:
AppendTextToString(
GetString16Attribute(ax::mojom::StringAttribute::kImageAnnotation),
&text);
break;
case ax::mojom::ImageAnnotationStatus::kNone:
case ax::mojom::ImageAnnotationStatus::kWillNotAnnotateDueToScheme:
case ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kSilentlyEligibleForAnnotation:
break;
}
}
// This is called from IsLeaf, so don't call PlatformChildCount
// from within this!
// Only for roles that do not support naming with child content, we loop
// through the children, in order to populate the visual content (use Android
// text API), in addition to populating the aria label information.
if (text.size() == 0 && !ui::SupportsNamingWithChildContent(GetRole()) &&
((HasOnlyTextChildren() && !HasListMarkerChild()) ||
(IsFocusable() && HasOnlyTextAndImageChildren()))) {
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
static_cast<BrowserAccessibilityAndroid*>(it.get())
->AccumulateSubstringTextContentUTF16(&text, min_length, style_data);
if (min_length && text.size() >= *min_length) {
break;
}
}
}
if (text.empty() &&
(ui::IsLink(GetRole()) || ui::IsImageOrVideo(GetRole())) &&
!HasExplicitlyEmptyName()) {
std::u16string url = GetString16Attribute(ax::mojom::StringAttribute::kUrl);
text = ui::AXUrlBaseText(url);
}
}
BrowserAccessibilityAndroid::EarlyExitPredicate
BrowserAccessibilityAndroid::NonEmptyPredicate() {
return base::BindRepeating(
[](const std::u16string& partial) { return partial.size() > 0; });
}
BrowserAccessibilityAndroid::EarlyExitPredicate
BrowserAccessibilityAndroid::LengthAtLeast(size_t length) {
return base::BindRepeating(
[](size_t length, const std::u16string& partial) {
return partial.length() > length;
},
length);
}
std::u16string BrowserAccessibilityAndroid::GetValueForControl() const {
std::u16string value = BrowserAccessibility::GetValueForControl();
// Optionally replace entered password text with bullet characters
// based on a user preference.
if (IsPasswordField()) {
if (ui::AccessibilityState::ShouldRespectDisplayedPasswordText()) {
// In the Chrome accessibility tree, the value of a password node is
// unobscured. However, if ShouldRespectDisplayedPasswordText() returns
// true we should try to expose whatever's actually visually displayed,
// whether that's the actual password or dots or whatever. To do this
// we rely on the password field's shadow dom.
value = BrowserAccessibility::GetTextContentUTF16();
} else if (!ui::AccessibilityState::ShouldExposePasswordText()) {
value = std::u16string(value.size(), ui::kSecurePasswordBullet);
}
}
return value;
}
std::u16string BrowserAccessibilityAndroid::GetHint() const {
std::vector<std::u16string> strings;
// If we're returning the value as the main text, the name needs to be
// part of the hint.
if (ShouldExposeValueAsName(GetValueForControl())) {
// In the case of accessible name from kAttribute, the name will be
// mapped to one of the container title, content description or supplemental
// description, we should exclude name from mapping to hint.
std::u16string name =
IsAccessibleNameFromAttribute() ? u"" : GetNameAsString16();
if (!name.empty()) {
strings.push_back(name);
}
}
if (GetData().GetNameFrom() != ax::mojom::NameFrom::kPlaceholder) {
std::u16string placeholder =
GetString16Attribute(ax::mojom::StringAttribute::kPlaceholder);
if (!placeholder.empty()) {
strings.push_back(placeholder);
}
}
std::u16string description =
GetString16Attribute(ax::mojom::StringAttribute::kDescription);
// If the description is the same as tooltip text and is already mapped to
// Android tooltip text API, do not map it to Android hint.
if (!description.empty() && description != GetTooltipText()) {
strings.push_back(description);
}
return base::JoinString(strings, u" ");
}
std::u16string BrowserAccessibilityAndroid::GetTooltipText() const {
return GetString16Attribute(ax::mojom::StringAttribute::kTooltip);
}
std::u16string BrowserAccessibilityAndroid::GetPaneTitle() const {
if (ui::IsDialog(GetRole())) {
return GetDialogModalMessageText();
} else if (ui::IsComboBox(GetRole()) && IsExpanded()) {
return GetComboboxExpandedText();
} else {
NOTREACHED();
}
}
std::u16string BrowserAccessibilityAndroid::GetDialogModalMessageText() const {
// For a dialog/modal, first check for a name, and then a description. If
// both are empty, fallback to a default "dialog opened." text.
if (HasStringAttribute(ax::mojom::StringAttribute::kName)) {
return GetString16Attribute(ax::mojom::StringAttribute::kName);
}
if (HasStringAttribute(ax::mojom::StringAttribute::kDescription)) {
return GetString16Attribute(ax::mojom::StringAttribute::kDescription);
}
return GetLocalizedString(IDS_AX_DIALOG_MODAL_OPENED);
}
std::u16string BrowserAccessibilityAndroid::GetStateDescription() const {
std::vector<std::u16string> state_descs;
// For multiselectable state, generate a state description. We do not set a
// state description for pop up/<select> to prevent double utterances.
// TODO(crbug.com/40864556): Consider whether other combobox roles should be
// accounted for.
if (IsMultiselectable() && GetRole() != ax::mojom::Role::kPopUpButton &&
GetRole() != ax::mojom::Role::kComboBoxSelect) {
state_descs.push_back(GetMultiselectableStateDescription());
}
if (GetRole() == ax::mojom::Role::kToggleButton ||
GetRole() == ax::mojom::Role::kSwitch) {
// For Toggle buttons and switches, we will append "on"/"off" in the state
// description.
state_descs.push_back(GetToggleStateDescription());
}
// For radio buttons, we will communicate how many radio buttons are in the
// group and which one is selected/checked (e.g. "in group, option x of y")
if (GetRole() == ax::mojom::Role::kRadioButton) {
state_descs.push_back(GetRadioButtonStateDescription());
}
// For nodes with non-trivial aria-current values, communicate state.
if (HasAriaCurrent()) {
state_descs.push_back(GetAriaCurrentStateDescription());
}
// Concatenate all state descriptions and return.
return base::JoinString(state_descs, u" ");
}
std::u16string BrowserAccessibilityAndroid::GetContainerTitle() const {
// Accessible name from kAttribute, is Android container role.
if (IsAccessibleNameFromAttribute() && ui::IsContainerOnAndroid(GetRole())) {
return GetNameAsString16();
}
return u"";
}
std::u16string BrowserAccessibilityAndroid::GetContentDescription() const {
// Accessible name from kAttribute, is not Android container role, supports
// naming from child content.
if (IsAccessibleNameFromAttribute() && !ui::IsContainerOnAndroid(GetRole()) &&
ui::SupportsNamingWithChildContent(GetRole())) {
return GetNameAsString16();
}
return u"";
}
std::u16string BrowserAccessibilityAndroid::GetSupplementalDescription() const {
// Accessible name from kAttribute, is not Android container role, does not
// support naming from child content.
if (IsAccessibleNameFromAttribute() && !ui::IsContainerOnAndroid(GetRole()) &&
!ui::SupportsNamingWithChildContent(GetRole())) {
return GetNameAsString16();
}
return u"";
}
bool BrowserAccessibilityAndroid::IsAccessibleNameFromAttribute() const {
return base::FeatureList::IsEnabled(
features::kAccessibilityPopulateSupplementalDescriptionApi) &&
HasIntAttribute(ax::mojom::IntAttribute::kNameFrom) &&
GetNameFrom() == ax::mojom::NameFrom::kAttribute;
}
std::u16string BrowserAccessibilityAndroid::GetAccessibleNameUTF16() const {
std::u16string name = GetTextContentUTF16();
if (name.empty()) {
name = GetContainerTitle();
}
if (name.empty()) {
name = GetContentDescription();
}
if (name.empty()) {
name = GetSupplementalDescription();
}
return name;
}
std::u16string BrowserAccessibilityAndroid::GetMultiselectableStateDescription()
const {
// Count the number of children and selected children.
int child_count = 0;
int selected_count = 0;
for (const auto& child : PlatformChildren()) {
child_count++;
const BrowserAccessibilityAndroid& android_child =
static_cast<const BrowserAccessibilityAndroid&>(child);
if (android_child.IsSelected()) {
selected_count++;
}
}
// If none are selected, return special case.
if (!selected_count) {
return GetLocalizedString(IDS_AX_MULTISELECTABLE_STATE_DESCRIPTION_NONE);
}
// Generate a state description of the form: "multiselectable, x of y
// selected.".
std::vector<std::u16string> values;
values.push_back(base::NumberToString16(selected_count));
values.push_back(base::NumberToString16(child_count));
return base::ReplaceStringPlaceholders(
GetLocalizedString(IDS_AX_MULTISELECTABLE_STATE_DESCRIPTION), values,
nullptr);
}
std::u16string BrowserAccessibilityAndroid::GetToggleStateDescription() const {
// For checked Toggle buttons and switches, we will return "on", otherwise
// "off".
if (IsChecked()) {
return GetLocalizedString(IDS_AX_TOGGLE_BUTTON_ON);
}
return GetLocalizedString(IDS_AX_TOGGLE_BUTTON_OFF);
}
std::u16string BrowserAccessibilityAndroid::GetAriaCurrentStateDescription()
const {
int message_id;
switch (static_cast<ax::mojom::AriaCurrentState>(
GetIntAttribute(ax::mojom::IntAttribute::kAriaCurrentState))) {
case ax::mojom::AriaCurrentState::kPage:
message_id = IDS_AX_ARIA_CURRENT_PAGE;
break;
case ax::mojom::AriaCurrentState::kStep:
message_id = IDS_AX_ARIA_CURRENT_STEP;
break;
case ax::mojom::AriaCurrentState::kLocation:
message_id = IDS_AX_ARIA_CURRENT_LOCATION;
break;
case ax::mojom::AriaCurrentState::kDate:
message_id = IDS_AX_ARIA_CURRENT_DATE;
break;
case ax::mojom::AriaCurrentState::kTime:
message_id = IDS_AX_ARIA_CURRENT_TIME;
break;
case ax::mojom::AriaCurrentState::kTrue:
default:
message_id = IDS_AX_ARIA_CURRENT_TRUE;
break;
}
return GetLocalizedString(message_id);
}
std::u16string BrowserAccessibilityAndroid::GetRadioButtonStateDescription()
const {
// The radio button should have an IntListAttribute of kRadioGroupIds, with
// a length of the total number of radio buttons in this group. Blink sets
// these attributes for all nodes automatically, including for nodes of
// <input type="radio"> which share a common name. If the list is empty,
// escape with empty string.
std::vector<ui::AXNodeID> group_ids =
GetIntListAttribute(ax::mojom::IntListAttribute::kRadioGroupIds);
if (group_ids.empty() || group_ids.size() == 1) {
return std::u16string();
}
// Adding a stateDescription will override the 'checked' utterance in some
// downstream services like TalkBack, so add it to state as well.
int message_id = IsChecked()
? IDS_AX_RADIO_BUTTON_STATE_DESCRIPTION_CHECKED
: IDS_AX_RADIO_BUTTON_STATE_DESCRIPTION_UNCHECKED;
return base::ReplaceStringPlaceholders(
GetLocalizedString(message_id),
std::vector<std::u16string>({base::NumberToString16(GetItemIndex() + 1),
base::NumberToString16(group_ids.size())}),
/* offsets */ nullptr);
}
std::u16string BrowserAccessibilityAndroid::GetComboboxExpandedText() const {
// We consider comboboxes of the form:
//
// <div role="combobox">
// <input type="text" aria-controls="options">
// <ul role="listbox" id="options">...</ul> (Can be outside <div>)
// </div>
//
// Find child input node:
const BrowserAccessibilityAndroid* input_node = nullptr;
for (const auto& child : PlatformChildren()) {
const BrowserAccessibilityAndroid& android_child =
static_cast<const BrowserAccessibilityAndroid&>(child);
if (android_child.IsTextField()) {
input_node = &android_child;
break;
}
}
// If we have not found a child input element, consider aria 1.0 spec:
//
// <input type="text" role="combobox" aria-owns="options">
// <ul role="listbox" id="options">...</ul>
//
// Check if |this| is the input, otherwise try our fallbacks.
if (!input_node) {
if (IsTextField()) {
input_node = this;
} else {
return GetComboboxExpandedTextFallback();
}
}
// Get the aria-controls nodes of |input_node|.
std::vector<BrowserAccessibility*> controls =
manager()->GetAriaControls(input_node);
// |input_node| should control only one element, if it doesn't, try fallbacks.
if (controls.size() != 1) {
return GetComboboxExpandedTextFallback();
}
// |controlled_node| needs to be a combobox container, if not, try fallbacks.
BrowserAccessibilityAndroid* controlled_node =
static_cast<BrowserAccessibilityAndroid*>(controls[0]);
if (!ui::IsComboBoxContainer(controlled_node->GetRole())) {
return GetComboboxExpandedTextFallback();
}
// For dialogs, return special case string.
if (controlled_node->GetRole() == ax::mojom::Role::kDialog) {
return GetLocalizedString(IDS_AX_COMBOBOX_EXPANDED_DIALOG);
}
// Find |controlled_node| set size, or return default string.
if (!controlled_node->GetSetSize()) {
return GetLocalizedString(IDS_AX_COMBOBOX_EXPANDED_AUTOCOMPLETE_DEFAULT);
}
// Replace placeholder with count and return string.
return base::ReplaceStringPlaceholders(
GetLocalizedString(
IDS_AX_COMBOBOX_EXPANDED_AUTOCOMPLETE_X_OPTIONS_AVAILABLE),
base::NumberToString16(*controlled_node->GetSetSize()), nullptr);
}
std::u16string BrowserAccessibilityAndroid::GetComboboxExpandedTextFallback()
const {
// If a combobox was of an indeterminate form, attempt any special cases here,
// or return "expanded" as a final option.
// Check for child nodes that are collections.
int child_collection_count = 0;
const BrowserAccessibilityAndroid* collection_node = nullptr;
for (const auto& child : PlatformChildren()) {
const auto& android_child =
static_cast<const BrowserAccessibilityAndroid&>(child);
if (android_child.IsCollection()) {
child_collection_count++;
collection_node = &android_child;
}
}
// If we find none, or more than one, we will not be able to determine the
// correct utterance, so return a default string instead.
if (child_collection_count != 1) {
return GetLocalizedString(IDS_AX_COMBOBOX_EXPANDED);
}
// Find |collection_node| set size, or return defaul string.
if (!collection_node->GetSetSize()) {
return GetLocalizedString(IDS_AX_COMBOBOX_EXPANDED_AUTOCOMPLETE_DEFAULT);
}
// Replace placeholder with count and return string.
return base::ReplaceStringPlaceholders(
GetLocalizedString(
IDS_AX_COMBOBOX_EXPANDED_AUTOCOMPLETE_X_OPTIONS_AVAILABLE),
base::NumberToString16(*collection_node->GetSetSize()), nullptr);
}
std::string BrowserAccessibilityAndroid::GetRoleString() const {
return ui::ToString(GetRole());
}
int BrowserAccessibilityAndroid::GetChecked() const {
ax::mojom::CheckedState checkedState = GetData().GetCheckedState();
switch (checkedState) {
case ax::mojom::CheckedState::kNone:
case ax::mojom::CheckedState::kFalse:
return ANDROID_VIEW_ACCESSIBILITY_CHECKED_STATE_FALSE;
case ax::mojom::CheckedState::kTrue:
return ANDROID_VIEW_ACCESSIBILITY_CHECKED_STATE_TRUE;
case ax::mojom::CheckedState::kMixed:
return ANDROID_VIEW_ACCESSIBILITY_CHECKED_STATE_PARTIAL;
}
}
std::u16string BrowserAccessibilityAndroid::GetRoleDescription() const {
// If an element has an aria-roledescription set, use that value by default.
if (HasStringAttribute(ax::mojom::StringAttribute::kRoleDescription)) {
return GetString16Attribute(ax::mojom::StringAttribute::kRoleDescription);
}
// As a special case, if we have a heading level return a string like
// "heading level 1", etc. - and if the heading consists of a link,
// append the word link as well.
if (GetRole() == ax::mojom::Role::kHeading) {
std::vector<std::u16string> role_description;
int level = GetIntAttribute(ax::mojom::IntAttribute::kHierarchicalLevel);
if (level >= 1 && level <= 6) {
std::vector<std::u16string> values;
values.push_back(base::NumberToString16(level));
role_description.push_back(base::ReplaceStringPlaceholders(
GetLocalizedString(IDS_AX_ROLE_HEADING_WITH_LEVEL), values, nullptr));
} else {
role_description.push_back(GetLocalizedString(IDS_AX_ROLE_HEADING));
}
if (IsHeadingLink()) {
role_description.push_back(GetLocalizedString(IDS_AX_ROLE_LINK));
}
// For visited links, we additionally want to append "visited" to the
// description.
if (HasState(ax::mojom::State::kVisited)) {
role_description.push_back(GetLocalizedString(IDS_AX_STATE_LINK_VISITED));
}
return base::JoinString(role_description, u" ");
}
// If this node is a link and the parent is a heading, return the role
// description of the parent (e.g. "heading 1 link").
if (ui::IsLink(GetRole()) && PlatformGetParent()) {
BrowserAccessibilityAndroid* parent =
static_cast<BrowserAccessibilityAndroid*>(PlatformGetParent());
if (parent->IsHeadingLink()) {
return parent->GetRoleDescription();
}
}
// If this node is a link and visited, append "visited" to the description.
if (ui::IsLink(GetRole())) {
std::vector<std::u16string> role_description = {
GetLocalizedStringForRoleDescription()};
if (HasState(ax::mojom::State::kVisited)) {
role_description.push_back(GetLocalizedString(IDS_AX_STATE_LINK_VISITED));
}
return base::JoinString(role_description, u" ");
}
// If this node is an image, check status and potentially add unlabeled role.
auto* manager =
static_cast<BrowserAccessibilityManagerAndroid*>(this->manager());
if (manager->ShouldAllowImageDescriptions()) {
auto status = GetData().GetImageAnnotationStatus();
switch (status) {
case ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kAnnotationPending:
case ax::mojom::ImageAnnotationStatus::kAnnotationEmpty:
case ax::mojom::ImageAnnotationStatus::kAnnotationAdult:
case ax::mojom::ImageAnnotationStatus::kAnnotationProcessFailed:
return GetLocalizedRoleDescriptionForUnlabeledImage();
case ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded:
case ax::mojom::ImageAnnotationStatus::kNone:
case ax::mojom::ImageAnnotationStatus::kWillNotAnnotateDueToScheme:
case ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kSilentlyEligibleForAnnotation:
break;
}
}
// For buttons with a kHasPopup attribute, return a more specific role.
if (ui::IsButton(GetRole())) {
switch (static_cast<ax::mojom::HasPopup>(
GetIntAttribute(ax::mojom::IntAttribute::kHasPopup))) {
case ax::mojom::HasPopup::kTrue:
case ax::mojom::HasPopup::kMenu:
return GetLocalizedString(IDS_AX_ROLE_POP_UP_BUTTON_MENU);
case ax::mojom::HasPopup::kDialog:
return GetLocalizedString(IDS_AX_ROLE_POP_UP_BUTTON_DIALOG);
case ax::mojom::HasPopup::kListbox:
case ax::mojom::HasPopup::kTree:
case ax::mojom::HasPopup::kGrid:
return GetLocalizedString(IDS_AX_ROLE_POP_UP_BUTTON);
case ax::mojom::HasPopup::kFalse:
break;
}
}
switch (GetRole()) {
case ax::mojom::Role::kAudio:
case ax::mojom::Role::kCode:
case ax::mojom::Role::kDescriptionList:
case ax::mojom::Role::kDetails:
case ax::mojom::Role::kEmphasis:
case ax::mojom::Role::kForm:
case ax::mojom::Role::kRowGroup:
case ax::mojom::Role::kSectionFooter:
case ax::mojom::Role::kSectionHeader:
case ax::mojom::Role::kSectionWithoutName:
case ax::mojom::Role::kStrong:
case ax::mojom::Role::kSubscript:
case ax::mojom::Role::kSuperscript:
case ax::mojom::Role::kTextField:
case ax::mojom::Role::kTime:
// No role description on Android.
break;
case ax::mojom::Role::kFigure:
// Default is IDS_AX_ROLE_FIGURE.
return GetLocalizedString(IDS_AX_ROLE_GRAPHIC);
case ax::mojom::Role::kHeader:
// Default is IDS_AX_ROLE_HEADER.
return GetLocalizedString(IDS_AX_ROLE_BANNER);
case ax::mojom::Role::kListGrid:
// Default is no special role description.
return GetLocalizedString(IDS_AX_ROLE_TABLE);
case ax::mojom::Role::kMenuItemCheckBox:
// Default is no special role description.
return GetLocalizedString(IDS_AX_ROLE_CHECK_BOX);
case ax::mojom::Role::kMenuItemRadio:
// Default is no special role description.
return GetLocalizedString(IDS_AX_ROLE_RADIO);
case ax::mojom::Role::kVideo:
// Default is no special role description.
return GetLocalizedString(IDS_AX_MEDIA_VIDEO_ELEMENT);
default:
return GetLocalizedStringForRoleDescription();
}
return std::u16string();
}
std::string BrowserAccessibilityAndroid::GetCSSDisplay() const {
std::string display =
node()->GetStringAttribute(ax::mojom::StringAttribute::kDisplay);
// Since this method is used to determine whether a text node is inline or
// block, we can filter out other values like list-item or table-cell
if (display == "inline" || display == "block" || display == "inline-block") {
return display;
}
return std::string();
}
float BrowserAccessibilityAndroid::GetTextSize() const {
return GetFloatAttribute(ax::mojom::FloatAttribute::kFontSize);
}
int BrowserAccessibilityAndroid::GetTextStyle() const {
return GetIntAttribute(ax::mojom::IntAttribute::kTextStyle);
}
int BrowserAccessibilityAndroid::GetTextPosition() const {
return GetIntAttribute(ax::mojom::IntAttribute::kTextPosition);
}
int BrowserAccessibilityAndroid::GetTextColor() const {
return GetIntAttribute(ax::mojom::IntAttribute::kColor);
}
int BrowserAccessibilityAndroid::GetTextBackgroundColor() const {
return GetIntAttribute(ax::mojom::IntAttribute::kBackgroundColor);
}
std::string BrowserAccessibilityAndroid::GetFontFamily() const {
return GetStringAttribute(ax::mojom::StringAttribute::kFontFamily);
}
int BrowserAccessibilityAndroid::GetItemIndex() const {
int index = 0;
if (IsRangeControlWithoutAriaValueText()) {
// Return a percentage here for live feedback in an AccessibilityEvent.
// The exact value is returned in RangeCurrentValue. Exclude sliders with
// an aria-valuetext set, as a percentage is not meaningful in those cases.
float min = GetFloatAttribute(ax::mojom::FloatAttribute::kMinValueForRange);
float max = GetFloatAttribute(ax::mojom::FloatAttribute::kMaxValueForRange);
float value = GetFloatAttribute(ax::mojom::FloatAttribute::kValueForRange);
if (max > min && value >= min && value <= max) {
index = static_cast<int>(((value - min)) * 100 / (max - min));
}
} else {
std::optional<int> pos_in_set = GetPosInSet();
if (pos_in_set && *pos_in_set > 0) {
index = *pos_in_set - 1;
}
}
return index;
}
int BrowserAccessibilityAndroid::GetItemCount() const {
int count = 0;
if (IsRangeControlWithoutAriaValueText()) {
// An AccessibilityEvent can only return integer information about a
// seek control, so we return a percentage. The real range is returned
// in RangeMin and RangeMax. Exclude sliders with an aria-valuetext set,
// as a percentage is not meaningful in those cases.
count = 100;
} else {
if (IsCollection() && GetSetSize()) {
count = *GetSetSize();
}
}
return count;
}
int BrowserAccessibilityAndroid::GetSelectedItemCount() const {
// Count the number of selected children.
int selected_count = 0;
for (const auto& child : PlatformChildren()) {
const BrowserAccessibilityAndroid& android_child =
static_cast<const BrowserAccessibilityAndroid&>(child);
if (android_child.IsSelected()) {
selected_count++;
}
}
return selected_count;
}
int BrowserAccessibilityAndroid::GetSelectionMode() const {
if (IsMultiselectable()) {
return ANDROID_VIEW_ACCESSIBILITY_SELECTION_MODE_MULTIPLE;
} else if (HasSelectActionVerbChildren()) {
return ANDROID_VIEW_ACCESSIBILITY_SELECTION_MODE_SINGLE;
} else {
return ANDROID_VIEW_ACCESSIBILITY_SELECTION_MODE_NONE;
}
}
bool BrowserAccessibilityAndroid::HasSelectActionVerb() const {
return HasIntAttribute(ax::mojom::IntAttribute::kDefaultActionVerb) &&
(GetData().GetDefaultActionVerb() ==
ax::mojom::DefaultActionVerb::kSelect);
}
bool BrowserAccessibilityAndroid::HasSelectActionVerbChildren() const {
// This is called from IsLeaf, so don't call PlatformChildCount
// from within this!
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
if (child->HasSelectActionVerb()) {
return true;
}
}
return false;
}
bool BrowserAccessibilityAndroid::CanScrollForward() const {
if (IsSlider()) {
float value = GetFloatAttribute(ax::mojom::FloatAttribute::kValueForRange);
float max = GetFloatAttribute(ax::mojom::FloatAttribute::kMaxValueForRange);
return value < max;
} else {
return CanScrollRight() || CanScrollDown();
}
}
bool BrowserAccessibilityAndroid::CanScrollBackward() const {
if (IsSlider()) {
float value = GetFloatAttribute(ax::mojom::FloatAttribute::kValueForRange);
float min = GetFloatAttribute(ax::mojom::FloatAttribute::kMinValueForRange);
return value > min;
} else {
return CanScrollLeft() || CanScrollUp();
}
}
bool BrowserAccessibilityAndroid::CanScrollUp() const {
return GetScrollY() > GetMinScrollY() && IsScrollable();
}
bool BrowserAccessibilityAndroid::CanScrollDown() const {
return GetScrollY() < GetMaxScrollY() && IsScrollable();
}
bool BrowserAccessibilityAndroid::CanScrollLeft() const {
return GetScrollX() > GetMinScrollX() && IsScrollable();
}
bool BrowserAccessibilityAndroid::CanScrollRight() const {
return GetScrollX() < GetMaxScrollX() && IsScrollable();
}
int BrowserAccessibilityAndroid::GetScrollX() const {
int value = 0;
GetIntAttribute(ax::mojom::IntAttribute::kScrollX, &value);
return value;
}
int BrowserAccessibilityAndroid::GetScrollY() const {
int value = 0;
GetIntAttribute(ax::mojom::IntAttribute::kScrollY, &value);
return value;
}
int BrowserAccessibilityAndroid::GetMinScrollX() const {
return GetIntAttribute(ax::mojom::IntAttribute::kScrollXMin);
}
int BrowserAccessibilityAndroid::GetMinScrollY() const {
return GetIntAttribute(ax::mojom::IntAttribute::kScrollYMin);
}
int BrowserAccessibilityAndroid::GetMaxScrollX() const {
return GetIntAttribute(ax::mojom::IntAttribute::kScrollXMax);
}
int BrowserAccessibilityAndroid::GetMaxScrollY() const {
return GetIntAttribute(ax::mojom::IntAttribute::kScrollYMax);
}
bool BrowserAccessibilityAndroid::Scroll(int direction,
bool is_page_scroll) const {
int x_initial = GetIntAttribute(ax::mojom::IntAttribute::kScrollX);
int x_min = GetIntAttribute(ax::mojom::IntAttribute::kScrollXMin);
int x_max = GetIntAttribute(ax::mojom::IntAttribute::kScrollXMax);
int y_initial = GetIntAttribute(ax::mojom::IntAttribute::kScrollY);
int y_min = GetIntAttribute(ax::mojom::IntAttribute::kScrollYMin);
int y_max = GetIntAttribute(ax::mojom::IntAttribute::kScrollYMax);
// Figure out the bounding box of the visible portion of this scrollable
// view so we know how much to scroll by.
gfx::Rect bounds;
if (ui::IsPlatformDocument(GetRole()) && !PlatformGetParent()) {
// If this is the root node, use the bounds of the view to determine how big
// one page is.
if (!manager()->delegate()) {
return false;
}
bounds = manager()->delegate()->AccessibilityGetViewBounds();
} else if (ui::IsPlatformDocument(GetRole()) && PlatformGetParent()) {
// If this is a web area inside of an iframe, try to use the bounds of
// the containing element.
BrowserAccessibility* parent = PlatformGetParent();
while (parent && (parent->GetClippedRootFrameBoundsRect().width() == 0 ||
parent->GetClippedRootFrameBoundsRect().height() == 0)) {
parent = parent->PlatformGetParent();
}
if (parent) {
bounds = parent->GetClippedRootFrameBoundsRect();
} else {
bounds = GetClippedRootFrameBoundsRect();
}
} else {
// Otherwise this is something like a scrollable div, just use the
// bounds of this object itself.
bounds = GetClippedRootFrameBoundsRect();
}
// Scroll by 80% of one page, or 100% for page scrolls.
int page_x, page_y;
if (is_page_scroll) {
page_x = std::max(bounds.width(), 1);
page_y = std::max(bounds.height(), 1);
} else {
page_x = std::max(bounds.width() * 4 / 5, 1);
page_y = std::max(bounds.height() * 4 / 5, 1);
}
if (direction == FORWARD) {
direction = y_max > y_min ? DOWN : RIGHT;
}
if (direction == BACKWARD) {
direction = y_max > y_min ? UP : LEFT;
}
int x = x_initial;
int y = y_initial;
switch (direction) {
case UP:
if (y_initial == y_min) {
return false;
}
y = std::clamp(y_initial - page_y, y_min, y_max);
break;
case DOWN:
if (y_initial == y_max) {
return false;
}
y = std::clamp(y_initial + page_y, y_min, y_max);
break;
case LEFT:
if (x_initial == x_min) {
return false;
}
x = std::clamp(x_initial - page_x, x_min, x_max);
break;
case RIGHT:
if (x_initial == x_max) {
return false;
}
x = std::clamp(x_initial + page_x, x_min, x_max);
break;
default:
NOTREACHED();
}
manager()->SetScrollOffset(*this, gfx::Point(x, y));
return true;
}
// Given arbitrary old_value_ and new_value_, we must come up with reasonable
// edit metrics. Although edits like "apple" > "apples" are typical, anything
// is possible, such as "apple" > "applesauce", "apple" > "boot", or "" >
// "supercalifragilisticexpialidocious". So we consider old_value_ to be of the
// form AXB and new_value_ to be of the form AYB, where X and Y are the pieces
// that don't match. We take the X to be the "removed" characters and Y to be
// the "added" characters.
int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
// This is len(A)
return CommonPrefixLength(old_value_, new_value_);
}
int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
// This is len(AYB) - (len(A) + len(B)), or len(Y), the added characters.
return new_value_.length() - CommonEndLengths(old_value_, new_value_);
}
int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
// This is len(AXB) - (len(A) + len(B)), or len(X), the removed characters.
return old_value_.length() - CommonEndLengths(old_value_, new_value_);
}
// static
size_t BrowserAccessibilityAndroid::CommonPrefixLength(
const std::u16string& a,
const std::u16string& b) {
size_t a_len = a.length();
size_t b_len = b.length();
size_t i = 0;
while (i < a_len && i < b_len && a[i] == b[i]) {
i++;
}
return i;
}
// static
size_t BrowserAccessibilityAndroid::CommonSuffixLength(
const std::u16string& a,
const std::u16string& b) {
size_t a_len = a.length();
size_t b_len = b.length();
size_t i = 0;
while (i < a_len && i < b_len && a[a_len - i - 1] == b[b_len - i - 1]) {
i++;
}
return i;
}
// TODO(nektar): Merge this function with
// |BrowserAccessibilityCocoa::computeTextEdit|.
//
// static
size_t BrowserAccessibilityAndroid::CommonEndLengths(const std::u16string& a,
const std::u16string& b) {
size_t prefix_len = CommonPrefixLength(a, b);
// Remove the matching prefix before finding the suffix. Otherwise, if
// old_value_ is "a" and new_value_ is "aa", "a" will be double-counted as
// both a prefix and a suffix of "aa".
std::u16string a_body = a.substr(prefix_len, std::string::npos);
std::u16string b_body = b.substr(prefix_len, std::string::npos);
size_t suffix_len = CommonSuffixLength(a_body, b_body);
return prefix_len + suffix_len;
}
std::u16string BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
return old_value_;
}
int BrowserAccessibilityAndroid::GetSelectionStart() const {
int sel_start = 0;
if (IsAtomicTextField() &&
GetIntAttribute(ax::mojom::IntAttribute::kTextSelStart, &sel_start)) {
return sel_start;
}
ui::AXSelection unignored_selection =
manager()->ax_tree()->GetUnignoredSelection();
int32_t anchor_id = unignored_selection.anchor_object_id;
BrowserAccessibility* anchor_object = manager()->GetFromID(anchor_id);
if (!anchor_object) {
return 0;
}
AXPosition position = anchor_object->CreateTextPositionAt(
unignored_selection.anchor_offset, unignored_selection.anchor_affinity);
while (position->GetAnchor() && position->GetAnchor() != node()) {
position = position->CreateParentPosition();
}
return !position->IsNullPosition() ? position->text_offset() : 0;
}
int BrowserAccessibilityAndroid::GetSelectionEnd() const {
int sel_end = 0;
if (IsAtomicTextField() &&
GetIntAttribute(ax::mojom::IntAttribute::kTextSelEnd, &sel_end)) {
return sel_end;
}
ui::AXSelection unignored_selection =
manager()->ax_tree()->GetUnignoredSelection();
int32_t focus_id = unignored_selection.focus_object_id;
BrowserAccessibility* focus_object = manager()->GetFromID(focus_id);
if (!focus_object) {
return 0;
}
AXPosition position = focus_object->CreateTextPositionAt(
unignored_selection.focus_offset, unignored_selection.focus_affinity);
while (position->GetAnchor() && position->GetAnchor() != node()) {
position = position->CreateParentPosition();
}
return !position->IsNullPosition() ? position->text_offset() : 0;
}
int BrowserAccessibilityAndroid::GetEditableTextLength() const {
if (IsTextField()) {
return static_cast<int>(GetValueForControl().size());
}
return 0;
}
int BrowserAccessibilityAndroid::AndroidInputType() const {
if (!HasStringAttribute(ax::mojom::StringAttribute::kInputType)) {
return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
}
if (!node()->HasStringAttribute(ax::mojom::StringAttribute::kInputType)) {
return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
}
const std::string& type =
node()->GetStringAttribute(ax::mojom::StringAttribute::kInputType);
if (type.empty() || type == "text" || type == "search") {
return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
} else if (type == "date") {
return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
} else if (type == "datetime" || type == "datetime-local") {
return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
} else if (type == "email") {
return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
} else if (type == "month") {
return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
} else if (type == "number") {
return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
} else if (type == "password") {
return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
} else if (type == "tel") {
return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
} else if (type == "time") {
return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
} else if (type == "url") {
return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
} else if (type == "week") {
return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
}
return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
}
int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
std::string live =
GetStringAttribute(ax::mojom::StringAttribute::kLiveStatus);
if (live == "polite") {
return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
} else if (live == "assertive") {
return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
}
return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
}
int BrowserAccessibilityAndroid::AndroidRangeType() const {
return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
}
int BrowserAccessibilityAndroid::RowCount() const {
if (!IsCollection()) {
return 0;
}
if (GetSetSize()) {
return *GetSetSize();
}
return node()->GetTableRowCount().value_or(0);
}
int BrowserAccessibilityAndroid::ColumnCount() const {
if (!IsCollection()) {
return 0;
}
// For <ol> and <ul> elements on Android (e.g. role kList, kListBox, kMenu,
// kMenuBar and kMenuListPopup), the AX code may consider these 0 columns (or
// more if the element is inside of a table), but on Android they are 1.
int ax_cols = node()->GetTableColCount().value_or(0);
if (GetRole() == ax::mojom::Role::kList ||
GetRole() == ax::mojom::Role::kListBox ||
GetRole() == ax::mojom::Role::kMenu ||
GetRole() == ax::mojom::Role::kMenuBar ||
GetRole() == ax::mojom::Role::kMenuListPopup) {
ax_cols = 1;
}
return ax_cols;
}
int BrowserAccessibilityAndroid::RowIndex() const {
std::optional<int> pos_in_set = GetPosInSet();
if (pos_in_set && pos_in_set > 0) {
return *pos_in_set - 1;
}
return node()->GetTableCellRowIndex().value_or(0);
}
int BrowserAccessibilityAndroid::RowSpan() const {
// For <ol> and <ul> elements on Android (e.g. role kListItem), the AX
// code will consider these 0 span, but on Android they are 1.
int ax_row_span = node()->GetTableCellRowSpan().value_or(0);
if (GetRole() == ax::mojom::Role::kListItem ||
GetRole() == ax::mojom::Role::kListBoxOption) {
DCHECK_EQ(ax_row_span, 0);
ax_row_span = 1;
}
return ax_row_span;
}
int BrowserAccessibilityAndroid::ColumnIndex() const {
return node()->GetTableCellColIndex().value_or(0);
}
int BrowserAccessibilityAndroid::ColumnSpan() const {
// For <ol> and <ul> elements on Android (e.g. role kListItem), the AX
// code will consider these 0 span, but on Android they are 1.
int ax_col_span = node()->GetTableCellColSpan().value_or(0);
if (GetRole() == ax::mojom::Role::kListItem ||
GetRole() == ax::mojom::Role::kListBoxOption) {
DCHECK_EQ(ax_col_span, 0);
ax_col_span = 1;
}
return ax_col_span;
}
float BrowserAccessibilityAndroid::RangeMin() const {
return GetFloatAttribute(ax::mojom::FloatAttribute::kMinValueForRange);
}
float BrowserAccessibilityAndroid::RangeMax() const {
return GetFloatAttribute(ax::mojom::FloatAttribute::kMaxValueForRange);
}
float BrowserAccessibilityAndroid::RangeCurrentValue() const {
return GetFloatAttribute(ax::mojom::FloatAttribute::kValueForRange);
}
void BrowserAccessibilityAndroid::GetGranularityBoundaries(
int granularity,
std::vector<int32_t>* starts,
std::vector<int32_t>* ends,
int offset) {
switch (granularity) {
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE:
GetLineBoundaries(starts, ends, offset);
break;
case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
GetWordBoundaries(starts, ends, offset);
break;
default:
NOTREACHED();
}
}
void BrowserAccessibilityAndroid::GetLineBoundaries(
std::vector<int32_t>* line_starts,
std::vector<int32_t>* line_ends,
int offset) {
// If this node has no children, treat it as all one line.
if (GetSubstringTextContentUTF16(1).size() > 0 && !InternalChildCount()) {
line_starts->push_back(offset);
line_ends->push_back(offset + GetTextContentLengthUTF16());
}
// If this is a static text node, get the line boundaries from the
// inline text boxes if possible.
if (GetRole() == ax::mojom::Role::kStaticText) {
int last_y = 0;
bool is_first = true;
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
CHECK_EQ(ax::mojom::Role::kInlineTextBox, child->GetRole());
// TODO(dmazzoni): replace this with a proper API to determine
// if two inline text boxes are on the same line. http://crbug.com/421771
int y = child->GetClippedRootFrameBoundsRect().y();
if (is_first) {
is_first = false;
line_starts->push_back(offset);
} else if (y != last_y) {
line_ends->push_back(offset);
line_starts->push_back(offset);
}
offset += child->GetTextContentLengthUTF16();
last_y = y;
}
line_ends->push_back(offset);
return;
}
// Otherwise, call GetLineBoundaries recursively on the children.
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
child->GetLineBoundaries(line_starts, line_ends, offset);
offset += child->GetTextContentLengthUTF16();
}
}
void BrowserAccessibilityAndroid::GetWordBoundaries(
std::vector<int32_t>* word_starts,
std::vector<int32_t>* word_ends,
int offset) {
if (GetRole() == ax::mojom::Role::kInlineTextBox) {
const std::vector<int32_t>& starts =
GetIntListAttribute(ax::mojom::IntListAttribute::kWordStarts);
const std::vector<int32_t>& ends =
GetIntListAttribute(ax::mojom::IntListAttribute::kWordEnds);
for (size_t i = 0; i < starts.size(); ++i) {
word_starts->push_back(offset + starts[i]);
word_ends->push_back(offset + ends[i]);
}
return;
}
std::u16string concatenated_text;
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
concatenated_text += child->GetAccessibleNameUTF16();
}
std::u16string text = GetAccessibleNameUTF16();
if (text.empty() || concatenated_text == text) {
// Great - this node is just the concatenation of its children, so
// we can get the word boundaries recursively.
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
child->GetWordBoundaries(word_starts, word_ends, offset);
offset += child->GetTextContentLengthUTF16();
}
} else {
// This node has its own accessible text that doesn't match its
// visible text - like alt text for an image or something with an
// aria-label, so split the text into words locally.
base::i18n::BreakIterator iter(text, base::i18n::BreakIterator::BREAK_WORD);
if (!iter.Init()) {
return;
}
while (iter.Advance()) {
if (iter.IsWord()) {
word_starts->push_back(iter.prev());
word_ends->push_back(iter.pos());
}
}
}
}
std::u16string BrowserAccessibilityAndroid::GetTargetUrl() const {
if (ui::IsImageOrVideo(GetRole()) || ui::IsLink(GetRole())) {
return GetString16Attribute(ax::mojom::StringAttribute::kUrl);
}
return {};
}
void BrowserAccessibilityAndroid::GetSuggestions(
std::vector<int>* suggestion_starts,
std::vector<int>* suggestion_ends) const {
DCHECK(suggestion_starts);
DCHECK(suggestion_ends);
if (!IsTextField()) {
return;
}
// TODO(accessibility): using FindTextOnlyObjectsInRange or NextInTreeOrder
// doesn't work because Android's IsLeaf
// implementation deliberately excludes a lot of nodes. We need a version of
// FindTextOnlyObjectsInRange and/or NextInTreeOrder that only walk
// the internal tree.
BrowserAccessibility* node = InternalGetFirstChild();
int start_offset = 0;
while (node && node != this) {
if (node->IsText()) {
const std::vector<int32_t>& marker_types =
node->GetIntListAttribute(ax::mojom::IntListAttribute::kMarkerTypes);
if (!marker_types.empty()) {
const std::vector<int>& marker_starts = node->GetIntListAttribute(
ax::mojom::IntListAttribute::kMarkerStarts);
const std::vector<int>& marker_ends =
node->GetIntListAttribute(ax::mojom::IntListAttribute::kMarkerEnds);
for (size_t i = 0; i < marker_types.size(); ++i) {
// On Android, both spelling errors and alternatives from dictation
// are both encoded as suggestions.
if (!(marker_types[i] &
static_cast<int32_t>(ax::mojom::MarkerType::kSuggestion))) {
continue;
}
int suggestion_start = start_offset + marker_starts[i];
int suggestion_end = start_offset + marker_ends[i];
suggestion_starts->push_back(suggestion_start);
suggestion_ends->push_back(suggestion_end);
}
}
start_offset += node->GetTextContentLengthUTF16();
}
// Implementation of NextInTreeOrder, but walking the internal tree.
if (node->InternalChildCount()) {
node = node->InternalGetFirstChild();
} else {
while (node && node != this) {
BrowserAccessibility* sibling = node->InternalGetNextSibling();
if (sibling) {
node = sibling;
break;
}
node = node->InternalGetParent();
}
}
}
}
bool BrowserAccessibilityAndroid::HasAriaCurrent() const {
if (!HasIntAttribute(ax::mojom::IntAttribute::kAriaCurrentState)) {
return false;
}
auto current = static_cast<ax::mojom::AriaCurrentState>(
GetIntAttribute(ax::mojom::IntAttribute::kAriaCurrentState));
return current != ax::mojom::AriaCurrentState::kNone &&
current != ax::mojom::AriaCurrentState::kFalse;
}
bool BrowserAccessibilityAndroid::HasNonEmptyValue() const {
return IsTextField() && !GetValueForControl().empty();
}
bool BrowserAccessibilityAndroid::HasCharacterLocations() const {
if (GetRole() == ax::mojom::Role::kStaticText) {
return true;
}
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
if (static_cast<BrowserAccessibilityAndroid*>(it.get())
->HasCharacterLocations()) {
return true;
}
}
return false;
}
bool BrowserAccessibilityAndroid::HasImage() const {
if (ui::IsImageOrVideo(GetRole())) {
return true;
}
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
if (static_cast<BrowserAccessibilityAndroid*>(it.get())->HasImage()) {
return true;
}
}
return false;
}
ui::BrowserAccessibility*
BrowserAccessibilityAndroid::PlatformGetLowestPlatformAncestor() const {
ui::BrowserAccessibility* current_object =
const_cast<BrowserAccessibilityAndroid*>(this);
ui::BrowserAccessibility* lowest_unignored_node = current_object;
if (lowest_unignored_node->IsIgnored()) {
lowest_unignored_node = lowest_unignored_node->PlatformGetParent();
}
DCHECK(!lowest_unignored_node || !lowest_unignored_node->IsIgnored())
<< "`BrowserAccessibility::PlatformGetParent()` should return either an "
"unignored object or nullptr.";
// `highest_leaf_node` could be nullptr.
ui::BrowserAccessibility* highest_leaf_node = lowest_unignored_node;
// For the purposes of this method, a leaf node does not include leaves in the
// internal accessibility tree, only in the platform exposed tree.
for (ui::BrowserAccessibility* ancestor_node = lowest_unignored_node;
ancestor_node; ancestor_node = ancestor_node->PlatformGetParent()) {
if (ancestor_node->IsLeaf()) {
highest_leaf_node = ancestor_node;
}
}
if (highest_leaf_node) {
return highest_leaf_node;
}
if (lowest_unignored_node) {
return lowest_unignored_node;
}
return current_object;
}
bool BrowserAccessibilityAndroid::HasOnlyTextChildren() const {
// This is called from IsLeaf, so don't call PlatformChildCount
// from within this!
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
if (!it->IsText()) {
return false;
}
}
return true;
}
bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
// This is called from IsLeaf, so don't call PlatformChildCount
// from within this!
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
BrowserAccessibility* child = it.get();
if (!child->IsText() && !ui::IsImageOrVideo(child->GetRole())) {
return false;
}
}
return true;
}
bool BrowserAccessibilityAndroid::HasListMarkerChild() const {
// This is called from IsLeaf, so don't call PlatformChildCount
// from within this!
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd(); ++it) {
if (it->GetRole() == ax::mojom::Role::kListMarker) {
return true;
}
}
return false;
}
bool BrowserAccessibilityAndroid::ShouldExposeValueAsName(
const std::u16string& value) const {
switch (GetRole()) {
case ax::mojom::Role::kDate:
case ax::mojom::Role::kDateTime:
case ax::mojom::Role::kInputTime:
return true;
case ax::mojom::Role::kColorWell:
return false;
default:
break;
}
if (GetData().IsRangeValueSupported()) {
return false;
} else if (!value.empty()) {
return true;
}
if (IsTextField()) {
return true;
}
if (ui::IsComboBox(GetRole())) {
return true;
}
if (GetRole() == ax::mojom::Role::kPopUpButton &&
!GetValueForControl().empty()) {
return true;
}
return false;
}
bool BrowserAccessibilityAndroid::CanFireEvents() const {
return !IsChildOfLeaf();
}
void BrowserAccessibilityAndroid::OnDataChanged() {
BrowserAccessibility::OnDataChanged();
if (IsTextField()) {
std::u16string value = GetValueForControl();
if (value != new_value_) {
old_value_ = new_value_;
new_value_ = value;
}
}
auto* manager =
static_cast<BrowserAccessibilityManagerAndroid*>(this->manager());
manager->ClearNodeInfoCacheForGivenId(GetUniqueId());
if (BrowserAccessibilityAndroid* parent =
static_cast<BrowserAccessibilityAndroid*>(PlatformGetParent())) {
manager->ClearNodeInfoCacheForGivenId(parent->GetUniqueId());
}
}
int BrowserAccessibilityAndroid::CountChildrenWithRole(
ax::mojom::Role role) const {
int count = 0;
for (const auto& child : PlatformChildren()) {
if (child.GetRole() == role) {
count++;
}
}
return count;
}
std::u16string BrowserAccessibilityAndroid::GetContentInvalidErrorMessage()
const {
if (!IsContentInvalid()) {
return std::u16string();
}
int message_id = -1;
switch (GetData().GetInvalidState()) {
case ax::mojom::InvalidState::kNone:
case ax::mojom::InvalidState::kFalse:
// No error message necessary
break;
case ax::mojom::InvalidState::kTrue:
message_id = CONTENT_INVALID_TRUE;
// Handle Grammar or Spelling errors.
// TODO(accessibility): using FindTextOnlyObjectsInRange or
// NextInTreeOrder doesn't work because Android's IsLeaf implementation
// deliberately excludes a lot of nodes.
for (auto it = InternalChildrenBegin(); it != InternalChildrenEnd();
++it) {
BrowserAccessibilityAndroid* child =
static_cast<BrowserAccessibilityAndroid*>(it.get());
if (child->IsText()) {
const std::vector<int32_t>& marker_types = child->GetIntListAttribute(
ax::mojom::IntListAttribute::kMarkerTypes);
for (int marker_type : marker_types) {
if (marker_type &
static_cast<int32_t>(ax::mojom::MarkerType::kSpelling)) {
message_id = CONTENT_INVALID_SPELLING;
break;
} else if (marker_type &
static_cast<int32_t>(ax::mojom::MarkerType::kGrammar)) {
message_id = CONTENT_INVALID_GRAMMAR;
break;
}
}
}
}
break;
}
std::vector<std::u16string> error_messages;
if (message_id != -1) {
error_messages.push_back(GetLocalizedString(message_id));
}
for (int error_message_id :
GetIntListAttribute(ax::mojom::IntListAttribute::kErrormessageIds)) {
BrowserAccessibility* node = manager()->GetFromID(error_message_id);
if (!node) {
continue;
}
const auto& name =
node->HasStringAttribute(ax::mojom::StringAttribute::kName)
? node->GetString16Attribute(ax::mojom::StringAttribute::kName)
: node->GetTextContentUTF16();
if (name.empty()) {
continue;
}
error_messages.push_back(name);
}
return base::JoinString(error_messages, u" ");
}
std::u16string
BrowserAccessibilityAndroid::GenerateAccessibilityNodeInfoString() const {
auto* manager =
static_cast<BrowserAccessibilityManagerAndroid*>(this->manager());
return manager->GenerateAccessibilityNodeInfoString(GetUniqueId());
}
} // namespace content
|