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
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "content/browser/accessibility/web_contents_accessibility_android.h"
#include <algorithm>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "base/android/callback_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/debug/crash_logging.h"
#include "base/feature_list.h"
#include "base/hash/hash.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/types/fixed_array.h"
#include "content/browser/accessibility/accessibility_tree_snapshot_combiner.h"
#include "content/browser/accessibility/browser_accessibility_android.h"
#include "content/browser/accessibility/browser_accessibility_manager_android.h"
#include "content/browser/accessibility/browser_accessibility_state_impl_android.h"
#include "content/browser/android/render_widget_host_connector.h"
#include "content/browser/renderer_host/render_widget_host_view_android.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/scoped_accessibility_mode.h"
#include "content/public/common/content_features.h"
#include "net/base/data_url.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/accessibility_prefs.h"
#include "ui/accessibility/ax_assistant_structure.h"
#include "ui/accessibility/ax_node_id_forward.h"
#include "ui/accessibility/platform/ax_android_constants.h"
#include "ui/accessibility/platform/one_shot_accessibility_tree_search.h"
#include "ui/events/android/motion_event_android.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "content/public/android/content_jni_headers/AccessibilityNodeInfoBuilder_jni.h"
#include "content/public/android/content_jni_headers/AssistDataBuilder_jni.h"
#include "content/public/android/content_jni_headers/WebContentsAccessibilityImpl_jni.h"
using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
namespace content {
namespace {
// This map contains key value pairs of a string to a tree search predicate. The
// set of keys represents the ways in which an AT can navigate a page by HTML
// element (by next or previous navigation). A Java-side AT sends a key with a
// next/previous action request, and this map is used to map the string to the
// correct predicate.
using SearchKeyToPredicateMap =
std::unordered_map<std::u16string, ui::AccessibilityMatchPredicate>;
static const char kHtmlTypeRow[] = "ROW";
static const char kHtmlTypeColumn[] = "COLUMN";
static const char kHtmlTypeRowBounds[] = "ROW_BOUNDS";
static const char kHtmlTypeColumnBounds[] = "COLUMN_BOUNDS";
static const char kHtmlTypeTableBounds[] = "TABLE_BOUNDS";
// IMPORTANT!
// These values are written to logs. Do not renumber or delete
// existing items; add new entries to the end of the list.
//
// LINT.IfChange
enum class AccessibilityPredicateType {
// Used for a string we do not support/recognize.
kUnknown = 0,
// Used for an empty string, which is default and maps to
// "IsInterestingOnAndroid".
kDefault = 1,
// Currently supported navigation types.
kArticle = 2,
kBlockQuote = 3,
kButton = 4,
kCheckbox = 5,
kCombobox = 6,
kControl = 7,
kFocusable = 8,
kFrame = 9,
kGraphic = 10,
kH1 = 11,
kH2 = 12,
kH3 = 13,
kH4 = 14,
kH5 = 15,
kH6 = 16,
kHeading = 17,
kHeadingSame = 18,
kLandmark = 19,
kLink = 20,
kList = 21,
kListItem = 22,
kLive = 23,
kMain = 24,
kMedia = 25,
kParagraph = 26,
kRadio = 27,
kRadioGroup = 28,
kSection = 29,
kTable = 30,
kTextfield = 31,
kTextBold = 32,
kTextItalic = 33,
kTextUnderline = 34,
kTree = 35,
kUnvisitedLink = 36,
kVisitedLink = 37,
kRow = 38,
kColumn = 39,
kRowBounds = 40,
kColumnBounds = 41,
kTableBounds = 42,
// Max value, must always be equal to the largest entry logged, remember to
// increment.
kMaxValue = 42
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/accessibility/enums.xml:AccessibilityPredicateType)
// This map contains key value pairs of a string to an internal enum identifying
// a tree search predicate. A Java-side AT sends a key with a next/previous
// action request, and this map is used to map the string to an enum so we can
// log a histogram.
using PredicateToEnumMap =
std::unordered_map<std::u16string, AccessibilityPredicateType>;
bool AllInterestingNodesPredicate(ui::BrowserAccessibility* start,
ui::BrowserAccessibility* node) {
BrowserAccessibilityAndroid* android_node =
static_cast<BrowserAccessibilityAndroid*>(node);
return android_node->IsInterestingOnAndroid();
}
bool AccessibilityNoOpPredicate(ui::BrowserAccessibility* start,
ui::BrowserAccessibility* node) {
return true;
}
// Getter function for the search key to predicate map and all keys string.
std::tuple<SearchKeyToPredicateMap&, std::u16string&, PredicateToEnumMap&>
GetSearchKeyData() {
// These are special unofficial strings sent from TalkBack/BrailleBack
// to jump to certain categories of web elements.
static base::NoDestructor<SearchKeyToPredicateMap>
search_key_to_predicate_map;
static base::NoDestructor<std::u16string> all_search_keys;
static base::NoDestructor<PredicateToEnumMap> predicate_to_enum_map;
static bool initialized = false;
if (!initialized) {
initialized = true;
auto add_to_map = [&](const std::u16string& key,
ui::AccessibilityMatchPredicate predicate,
AccessibilityPredicateType type) {
(*search_key_to_predicate_map)[key] = predicate;
(*predicate_to_enum_map)[key] = type;
// Build the all_search_keys string.
if (!all_search_keys->empty()) {
*all_search_keys += u",";
}
*all_search_keys += key;
};
add_to_map(u"ARTICLE", ui::AccessibilityArticlePredicate,
AccessibilityPredicateType::kArticle);
add_to_map(u"BLOCKQUOTE", ui::AccessibilityBlockquotePredicate,
AccessibilityPredicateType::kBlockQuote);
add_to_map(u"BUTTON", ui::AccessibilityButtonPredicate,
AccessibilityPredicateType::kButton);
add_to_map(u"CHECKBOX", ui::AccessibilityCheckboxPredicate,
AccessibilityPredicateType::kCheckbox);
add_to_map(u"COMBOBOX", ui::AccessibilityComboboxPredicate,
AccessibilityPredicateType::kCombobox);
add_to_map(u"CONTROL", ui::AccessibilityControlPredicate,
AccessibilityPredicateType::kControl);
add_to_map(u"FOCUSABLE", ui::AccessibilityFocusablePredicate,
AccessibilityPredicateType::kFocusable);
add_to_map(u"FRAME", ui::AccessibilityFramePredicate,
AccessibilityPredicateType::kFrame);
add_to_map(u"GRAPHIC", ui::AccessibilityGraphicPredicate,
AccessibilityPredicateType::kGraphic);
add_to_map(u"H1", ui::AccessibilityH1Predicate,
AccessibilityPredicateType::kH1);
add_to_map(u"H2", ui::AccessibilityH2Predicate,
AccessibilityPredicateType::kH2);
add_to_map(u"H3", ui::AccessibilityH3Predicate,
AccessibilityPredicateType::kH3);
add_to_map(u"H4", ui::AccessibilityH4Predicate,
AccessibilityPredicateType::kH4);
add_to_map(u"H5", ui::AccessibilityH5Predicate,
AccessibilityPredicateType::kH5);
add_to_map(u"H6", ui::AccessibilityH6Predicate,
AccessibilityPredicateType::kH6);
add_to_map(u"HEADING", ui::AccessibilityHeadingPredicate,
AccessibilityPredicateType::kHeading);
add_to_map(u"HEADING_SAME", ui::AccessibilityHeadingSameLevelPredicate,
AccessibilityPredicateType::kHeadingSame);
add_to_map(u"LANDMARK", ui::AccessibilityLandmarkPredicate,
AccessibilityPredicateType::kLandmark);
add_to_map(u"LINK", ui::AccessibilityLinkPredicate,
AccessibilityPredicateType::kLink);
add_to_map(u"LIST", ui::AccessibilityListPredicate,
AccessibilityPredicateType::kList);
add_to_map(u"LIST_ITEM", ui::AccessibilityListItemPredicate,
AccessibilityPredicateType::kListItem);
add_to_map(u"LIVE", ui::AccessibilityLiveRegionPredicate,
AccessibilityPredicateType::kLive);
add_to_map(u"MAIN", ui::AccessibilityMainPredicate,
AccessibilityPredicateType::kMain);
add_to_map(u"MEDIA", ui::AccessibilityMediaPredicate,
AccessibilityPredicateType::kMedia);
add_to_map(u"PARAGRAPH", ui::AccessibilityParagraphPredicate,
AccessibilityPredicateType::kParagraph);
add_to_map(u"RADIO", ui::AccessibilityRadioButtonPredicate,
AccessibilityPredicateType::kRadio);
add_to_map(u"RADIO_GROUP", ui::AccessibilityRadioGroupPredicate,
AccessibilityPredicateType::kRadioGroup);
add_to_map(u"SECTION", ui::AccessibilitySectionPredicate,
AccessibilityPredicateType::kSection);
add_to_map(u"TABLE", ui::AccessibilityTablePredicate,
AccessibilityPredicateType::kTable);
add_to_map(u"TEXT_FIELD", ui::AccessibilityTextfieldPredicate,
AccessibilityPredicateType::kTextfield);
add_to_map(u"TEXT_BOLD", ui::AccessibilityTextStyleBoldPredicate,
AccessibilityPredicateType::kTextBold);
add_to_map(u"TEXT_ITALIC", ui::AccessibilityTextStyleItalicPredicate,
AccessibilityPredicateType::kTextItalic);
add_to_map(u"TEXT_UNDER", ui::AccessibilityTextStyleUnderlinePredicate,
AccessibilityPredicateType::kTextUnderline);
add_to_map(u"TREE", ui::AccessibilityTreePredicate,
AccessibilityPredicateType::kTree);
add_to_map(u"UNVISITED_LINK", ui::AccessibilityUnvisitedLinkPredicate,
AccessibilityPredicateType::kUnvisitedLink);
add_to_map(u"VISITED_LINK", ui::AccessibilityVisitedLinkPredicate,
AccessibilityPredicateType::kVisitedLink);
// These are surfaced simply to document the html types, but do not do a
// tree/predicate search.
add_to_map(u"ROW", AccessibilityNoOpPredicate,
AccessibilityPredicateType::kRow);
add_to_map(u"ROW", AccessibilityNoOpPredicate,
AccessibilityPredicateType::kRow);
add_to_map(u"COLUMN", AccessibilityNoOpPredicate,
AccessibilityPredicateType::kColumn);
add_to_map(u"ROW_BOUNDS", AccessibilityNoOpPredicate,
AccessibilityPredicateType::kRowBounds);
add_to_map(u"COLUMN_BOUNDS", AccessibilityNoOpPredicate,
AccessibilityPredicateType::kColumnBounds);
add_to_map(u"TABLE_BOUNDS", AccessibilityNoOpPredicate,
AccessibilityPredicateType::kTableBounds);
// These do not have search predicates and are for metrics tracking.
(*predicate_to_enum_map)[u"UNKNOWN"] = AccessibilityPredicateType::kUnknown;
(*predicate_to_enum_map)[u"DEFAULT"] = AccessibilityPredicateType::kDefault;
}
return std::make_tuple(std::ref(*search_key_to_predicate_map),
std::ref(*all_search_keys),
std::ref(*predicate_to_enum_map));
}
ui::AccessibilityMatchPredicate PredicateForSearchKey(
std::u16string& element_type) {
const auto& [search_map, all_keys, enum_map] = GetSearchKeyData();
const auto& iter = search_map.find(element_type);
if (iter != search_map.end()) {
return iter->second;
} else {
element_type = u"UNKNOWN";
}
// If we don't recognize the selector, return any element that a
// screen reader should navigate to.
return AllInterestingNodesPredicate;
}
AccessibilityPredicateType EnumForPredicate(
const std::u16string& element_type) {
const auto& [search_map, all_keys, enum_map] = GetSearchKeyData();
const auto& it = enum_map.find(element_type);
if (it != enum_map.end()) {
return it->second;
} else {
NOTREACHED();
}
}
// The element in the document for which we may be displaying an autofill popup.
int32_t g_element_hosting_autofill_popup_unique_id = ui::kInvalidAXNodeID;
// The element in the document that is the next element after
// |g_element_hosting_autofill_popup_unique_id|.
int32_t g_element_after_element_hosting_autofill_popup_unique_id =
ui::kInvalidAXNodeID;
// Autofill popup will not be part of the |AXTree| that is sent by renderer.
// Hence, we need a proxy |AXNode| to represent the autofill popup.
ui::BrowserAccessibility* g_autofill_popup_proxy_node = nullptr;
ui::AXNode* g_autofill_popup_proxy_node_ax_node = nullptr;
void DeleteAutofillPopupProxy() {
if (g_autofill_popup_proxy_node) {
delete g_autofill_popup_proxy_node;
delete g_autofill_popup_proxy_node_ax_node;
g_autofill_popup_proxy_node = nullptr;
}
}
// The most common use of the EXTRA_DATA_TEXT_CHARACTER_LOCATION_KEY
// API is to retrieve character bounds for one word at a time. There
// should never need to be a reason to return more than this many
// character bounding boxes at once. Set the limit much higher than needed
// but small enough to prevent wasting memory and cpu if abused.
const int kMaxCharacterBoundingBoxLen = 1024;
// This is the value of View.NO_ID, used to mark a View that has no ID.
const int kViewNoId = -1;
std::optional<int> MaybeFindRowColumn(ui::BrowserAccessibility* start_node,
std::u16string element_type,
jboolean forwards) {
bool want_row = base::EqualsASCII(element_type, kHtmlTypeRow);
bool want_col = base::EqualsASCII(element_type, kHtmlTypeColumn);
bool want_row_bounds = base::EqualsASCII(element_type, kHtmlTypeRowBounds);
bool want_col_bounds = base::EqualsASCII(element_type, kHtmlTypeColumnBounds);
bool want_table_bounds =
base::EqualsASCII(element_type, kHtmlTypeTableBounds);
if (!want_row && !want_col && !want_row_bounds && !want_col_bounds &&
!want_table_bounds) {
// The search should continue for other types.
return std::nullopt;
}
// See if we're in a table and grab the cell-like node we're under on the way.
ui::BrowserAccessibility* table_node = start_node;
ui::AXNode* cell_node = nullptr;
int cur_row_index, cur_col_index;
while (table_node) {
if (AccessibilityTablePredicate(start_node, table_node)) {
break;
}
auto* node = table_node->node();
if (std::optional<int> row = node->GetTableCellRowIndex(),
col = node->GetTableCellColIndex();
row && col) {
cell_node = node;
cur_row_index = *row;
cur_col_index = *col;
}
table_node = table_node->PlatformGetParent();
}
if (!table_node) {
return ui::kInvalidAXNodeID;
}
ui::AXTree* tree = start_node->node()->tree();
ui::AXTableInfo* table_info = tree->GetTableInfo(table_node->node());
if (!table_info) {
return ui::kInvalidAXNodeID;
}
// Nothing more to do if the table is empty.
if (table_info->cell_ids.empty()) {
return ui::kInvalidAXNodeID;
}
// This may occur if we're somewhere in the table but not within a cell e.g.
// on the table node itself. In these cases, try to go to the first cell.
if (!cell_node) {
return table_info->cell_ids[0].empty() ? ui::kInvalidAXNodeID
: table_info->cell_ids[0][0];
}
// Move in the desired direction by the element type.
int want_row_index = cur_row_index, want_col_index = cur_col_index;
if (want_row) {
want_row_index += forwards ? 1 : -1;
}
if (want_col) {
want_col_index += forwards ? 1 : -1;
}
if (want_col_bounds || want_table_bounds) {
want_row_index = forwards ? table_info->row_count - 1 : 0;
}
if (want_row_bounds || want_table_bounds) {
want_col_index = forwards ? table_info->col_count - 1 : 0;
}
// This causes the caller to stop its search and indicate appropriately when
// trying to move past a boundary.
if (want_row_index < 0 || (size_t)want_row_index >= table_info->row_count ||
want_col_index < 0 || (size_t)want_col_index >= table_info->col_count) {
return ui::kInvalidAXNodeID;
}
// This causes the caller to stop its search and indicate appropriately when
// trying to move to the same cell.
if ((want_row_bounds && want_col_index == cur_col_index) ||
(want_col_bounds && want_row_index == cur_row_index) ||
(want_table_bounds && want_row_index == cur_row_index &&
want_col_index == cur_col_index)) {
return ui::kInvalidAXNodeID;
}
return table_info->cell_ids[want_row_index][want_col_index];
}
} // anonymous namespace
class WebContentsAccessibilityAndroid::Connector
: public RenderWidgetHostConnector {
public:
Connector(WebContents* web_contents,
WebContentsAccessibilityAndroid* accessibility);
~Connector() override = default;
void DeleteEarly();
// RenderWidgetHostConnector:
void UpdateRenderProcessConnection(
RenderWidgetHostViewAndroid* old_rwhva,
RenderWidgetHostViewAndroid* new_rhwva) override;
private:
std::unique_ptr<WebContentsAccessibilityAndroid> accessibility_;
};
WebContentsAccessibilityAndroid::Connector::Connector(
WebContents* web_contents,
WebContentsAccessibilityAndroid* accessibility)
: RenderWidgetHostConnector(web_contents), accessibility_(accessibility) {
Initialize();
}
void WebContentsAccessibilityAndroid::Connector::DeleteEarly() {
RenderWidgetHostConnector::DestroyEarly();
}
void WebContentsAccessibilityAndroid::Connector::UpdateRenderProcessConnection(
RenderWidgetHostViewAndroid* old_rwhva,
RenderWidgetHostViewAndroid* new_rwhva) {
if (old_rwhva) {
old_rwhva->SetWebContentsAccessibility(nullptr);
}
if (new_rwhva) {
new_rwhva->SetWebContentsAccessibility(accessibility_.get());
}
accessibility_->UpdateBrowserAccessibilityManager();
}
WebContentsAccessibilityAndroid::WebContentsAccessibilityAndroid(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
WebContents* web_contents,
const JavaParamRef<jobject>& jaccessibility_node_info_builder)
: java_ref_(env, obj),
java_anib_ref_(env, jaccessibility_node_info_builder),
web_contents_(static_cast<WebContentsImpl*>(web_contents)),
frame_info_initialized_(false) {
// We must initialize this after weak_ptr_factory_ because it can result in
// calling UpdateBrowserAccessibilityManager() which accesses
// weak_ptr_factory_.
connector_ = new Connector(web_contents, this);
}
WebContentsAccessibilityAndroid::WebContentsAccessibilityAndroid(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jlong ax_tree_update_ptr,
const JavaParamRef<jobject>& jaccessibility_node_info_builder)
: java_ref_(env, obj),
java_anib_ref_(env, jaccessibility_node_info_builder),
web_contents_(nullptr),
frame_info_initialized_(false) {
std::unique_ptr<ui::AXTreeUpdate> ax_tree_snapshot(
reinterpret_cast<ui::AXTreeUpdate*>(ax_tree_update_ptr));
snapshot_root_manager_ = std::make_unique<BrowserAccessibilityManagerAndroid>(
*ax_tree_snapshot, GetWeakPtr(), *this, nullptr);
snapshot_root_manager_->BuildAXTreeHitTestCache();
connector_ = nullptr;
}
WebContentsAccessibilityAndroid::WebContentsAccessibilityAndroid(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jassist_data_builder,
WebContents* web_contents)
: java_ref_(env, obj),
java_adb_ref_(env, jassist_data_builder),
web_contents_(static_cast<WebContentsImpl*>(web_contents)) {
// A Connector is not required for a simple snapshot.
connector_ = nullptr;
}
WebContentsAccessibilityAndroid::~WebContentsAccessibilityAndroid() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
// Clean up autofill popup proxy node in case the popup was not dismissed.
DeleteAutofillPopupProxy();
Java_WebContentsAccessibilityImpl_onNativeObjectDestroyed(env, obj);
}
ui::AXPlatformNodeId WebContentsAccessibilityAndroid::GetOrCreateAXNodeUniqueId(
ui::AXNodeID ax_node_id) {
// Per-tab uniqueness is not necessary in snapshots, so return the blink node
// id.
return ui::AXPlatformNodeId(MakePassKey(), ax_node_id);
}
void WebContentsAccessibilityAndroid::OnAXNodeDeleted(ui::AXNodeID ax_node_id) {
}
void WebContentsAccessibilityAndroid::ConnectInstanceToRootManager(
JNIEnv* env) {
BrowserAccessibilityManagerAndroid* manager =
GetRootBrowserAccessibilityManager();
if (manager) {
manager->set_web_contents_accessibility(GetWeakPtr());
}
}
void WebContentsAccessibilityAndroid::UpdateBrowserAccessibilityManager() {
BrowserAccessibilityManagerAndroid* manager =
GetRootBrowserAccessibilityManager();
if (manager) {
manager->set_web_contents_accessibility(GetWeakPtr());
}
}
void WebContentsAccessibilityAndroid::DeleteEarly(JNIEnv* env) {
if (connector_) {
connector_->DeleteEarly();
} else {
delete this;
}
}
void WebContentsAccessibilityAndroid::DisableRendererAccessibility(
JNIEnv* env) {
// This method should only be called when |snapshot_root_manager_| is null,
// which means this instance was constructed via a web contents and not an
// AXTreeUpdate (e.g. for snapshots, frozen tabs, paint preview, etc).
DCHECK(!snapshot_root_manager_);
// To disable the renderer, the root manager /should/ already be connected to
// this instance, and we need to reset the weak pointer it has to |this|. In
// some rare cases, such as if a user rapidly toggles accessibility on/off,
// a manager may not be connected, in which case a reset is not needed.
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (root_manager) {
root_manager->ResetWebContentsAccessibility();
}
// The local cache of Java strings can be cleared, and we should reset any
// local state variables. The Connector should continue to live, since we want
// the RFHI to still have access to this object for possible re-enables,
// or frame notifications.
common_string_cache_.clear();
ResetContentChangedEventsCounter();
// Turn off accessibility on the renderer side by resetting the AXMode.
scoped_accessibility_mode_.reset();
}
void WebContentsAccessibilityAndroid::ReEnableRendererAccessibility(
JNIEnv* env,
const JavaParamRef<jobject>& jweb_contents) {
// This method should only be called when |snapshot_root_manager_| is null,
// which means this instance was constructed via a web contents and not an
// AXTreeUpdate (e.g. for snapshots, frozen tabs, paint preview, etc).
DCHECK(!snapshot_root_manager_);
WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
DCHECK(web_contents);
// A request to re-enable renderer accessibility implies AT use on the
// Java-side, so we need to set the root manager's reference to |this| to
// rebuild the C++ -> Java bridge. The web contents may have changed, so
// update the reference just in case.
web_contents_ = static_cast<WebContentsImpl*>(web_contents);
// If we are re-enabling, the root manager may already be connected, in which
// case we can set its weak pointer to |this|. However, if a user has rapidly
// turned accessibility on/off, the manager may not be ready. If the manager
// is not ready, the framework will continue polling until it is connected.
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (root_manager) {
root_manager->set_web_contents_accessibility(GetWeakPtr());
}
}
void WebContentsAccessibilityAndroid::SetBrowserAXMode(
JNIEnv* env,
jboolean is_known_screen_reader_enabled,
jboolean is_complex_accessibility_service_enabled,
jboolean is_form_controls_candidate,
jboolean is_on_screen_mode_candidate) {
// Set the AXMode based on currently running services, sent from Java-side
// code, in the following priority:
//
// (Note: This must /always/ take the top priority.)
// 1. If the performance filtering enterprise policy is disabled, then the
// mode must be the most complete mode possible (i.e. no performance
// optimizations). (ui::kAXModeComplete)
//
// 2. When a known screen reader is running, use
// ui::kAXModeComplete | ui::AXMode::kAXModeScreenReader.
// 2a. (Experimental) If a known screen reader is the only service
// running, then this is a candidate to use the "on screen only"
// experimental mode. (ui::kAXModeOnScreen | ui::kAXModeScreenReader)
//
// 3. When services are running that require detailed information according to
// the Java-side code (i.e. a "complex" accessibility service), use the
// complete mode. (ui::kAXModeComplete)
//
// 4. When the only services running are password managers, then this is a
// candidate for filtering for only form controls. (ui::kAXModeFormControls)
//
// 5. As a final case - at least one accessibility service must be enabled,
// and the union of all services has not requested enough information to be in
// a complete state, but has requested more than a limited state such as form
// controls, so fallback to a basic state. (ui::kAXModeBasic)
//
// TODO(crbug.com/413016129): Consider adding the ability to turn off the
// AXMode here by sending whether or not any service is running. This case is
// currently handled by the AutoDisableAccessibilityHandler, which waits ~5
// seconds before turning off accessibility, to account for quick toggling of
// the state. Analysis of existing data could show whether the 5 second wait
// is necessary.
BrowserAccessibilityStateImpl* accessibility_state =
BrowserAccessibilityStateImpl::GetInstance();
ui::AXMode target_mode;
if (!accessibility_state->IsPerformanceFilteringAllowed()) {
// Adds kScreenReader to ensure no filtering via the non-screen-reader case.
target_mode = ui::kAXModeComplete | ui::AXMode::kScreenReader;
} else if (is_known_screen_reader_enabled) {
target_mode = ui::kAXModeComplete | ui::AXMode::kScreenReader;
if (is_on_screen_mode_candidate &&
features::IsAccessibilityOnScreenAXModeEnabled()) {
target_mode = ui::kAXModeOnScreen | ui::AXMode::kScreenReader;
}
} else if (is_complex_accessibility_service_enabled) {
target_mode = ui::kAXModeComplete;
} else if (is_form_controls_candidate) {
target_mode = ui::kAXModeFormControls;
} else {
target_mode = ui::kAXModeBasic;
}
target_mode |= ui::AXMode::kFromPlatform;
scoped_accessibility_mode_ =
accessibility_state->CreateScopedModeForProcess(target_mode);
}
jboolean WebContentsAccessibilityAndroid::IsRootManagerConnected(JNIEnv* env) {
return !!GetRootBrowserAccessibilityManager();
}
void WebContentsAccessibilityAndroid::SetAllowImageDescriptions(
JNIEnv* env,
jboolean allow_image_descriptions) {
allow_image_descriptions_ = allow_image_descriptions;
}
void WebContentsAccessibilityAndroid::HandleContentChanged(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
// If there are a large number of changes it's too expensive to fire all of
// them, so we just fire one on the root instead.
content_changed_events_++;
if (content_changed_events_ < max_content_changed_events_to_fire_) {
// If it's less than the max event count, fire the event on the specific
// node that changed.
Java_WebContentsAccessibilityImpl_handleContentChanged(env, obj, unique_id);
} else if (content_changed_events_ == max_content_changed_events_to_fire_) {
// If it's equal to the max event count, fire the event on the
// root instead.
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (root_manager) {
auto* root_node = static_cast<BrowserAccessibilityAndroid*>(
root_manager->GetBrowserAccessibilityRoot());
if (root_node) {
Java_WebContentsAccessibilityImpl_handleContentChanged(
env, obj, root_node->GetUniqueId());
}
}
}
}
void WebContentsAccessibilityAndroid::HandleFocusChanged(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleFocusChanged(env, obj, unique_id);
}
void WebContentsAccessibilityAndroid::HandleCheckStateChanged(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleCheckStateChanged(env, obj,
unique_id);
}
void WebContentsAccessibilityAndroid::HandleClicked(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleClicked(env, obj, unique_id);
}
void WebContentsAccessibilityAndroid::HandleMenuOpened(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleMenuOpened(env, obj, unique_id);
}
void WebContentsAccessibilityAndroid::HandleWindowContentChange(
int32_t unique_id,
int32_t subType) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleWindowContentChange(
env, obj, unique_id, subType);
}
void WebContentsAccessibilityAndroid::HandleScrollPositionChanged(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleScrollPositionChanged(env, obj,
unique_id);
}
void WebContentsAccessibilityAndroid::HandleScrolledToAnchor(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleScrolledToAnchor(env, obj, unique_id);
}
void WebContentsAccessibilityAndroid::HandlePaneOpened(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handlePaneOpened(env, obj, unique_id);
}
void WebContentsAccessibilityAndroid::AnnounceLiveRegionText(
const std::u16string& text) {
CHECK(!base::FeatureList::IsEnabled(
features::kAccessibilityDeprecateTypeAnnounce), )
<< "No views should be forcing an announcement outside approved "
"instances.";
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
// Do not announce empty text.
if (text.empty()) {
return;
}
Java_WebContentsAccessibilityImpl_announceLiveRegionText(
env, obj, base::android::ConvertUTF16ToJavaString(env, text));
}
void WebContentsAccessibilityAndroid::HandleTextSelectionChanged(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleTextSelectionChanged(env, obj,
unique_id);
}
void WebContentsAccessibilityAndroid::HandleEditableTextChanged(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleEditableTextChanged(env, obj,
unique_id);
}
void WebContentsAccessibilityAndroid::HandleActiveDescendantChanged(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
int active_descendant_id_attribute =
node->GetIntAttribute(ax::mojom::IntAttribute::kActivedescendantId);
ui::BrowserAccessibility* active_descendant_element =
node->manager()->GetFromID(active_descendant_id_attribute);
int active_descendant_id = kViewNoId;
if (active_descendant_element) {
active_descendant_id =
static_cast<BrowserAccessibilityAndroid*>(active_descendant_element)
->GetUniqueId();
}
Java_WebContentsAccessibilityImpl_handleActiveDescendantChanged(
env, obj, unique_id, active_descendant_id);
}
void WebContentsAccessibilityAndroid::SignalEndOfTestForTesting(JNIEnv* env) {
ui::BrowserAccessibilityManager* manager =
web_contents_->GetRootBrowserAccessibilityManager();
manager->SignalEndOfTest();
}
void WebContentsAccessibilityAndroid::HandleEndOfTestSignal() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleEndOfTestSignal(env, obj);
}
void WebContentsAccessibilityAndroid::HandleSliderChanged(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleSliderChanged(env, obj, unique_id);
}
void WebContentsAccessibilityAndroid::SendDelayedWindowContentChangedEvent() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_sendDelayedWindowContentChangedEvent(env,
obj);
}
void WebContentsAccessibilityAndroid::HandleHover(int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleHover(env, obj, unique_id);
}
bool WebContentsAccessibilityAndroid::OnHoverEvent(
const ui::MotionEventAndroid& event) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return false;
}
if (!Java_WebContentsAccessibilityImpl_onHoverEvent(
env, obj,
ui::MotionEventAndroid::GetAndroidAction(event.GetAction()))) {
return false;
}
// |HitTest| sends an IPC to the render process to do the hit testing.
// The response is handled by HandleHover when it returns.
// Hover event was consumed by accessibility by now. Return true to
// stop the event from proceeding.
if (event.GetAction() != ui::MotionEvent::Action::HOVER_EXIT &&
GetRootBrowserAccessibilityManager()) {
gfx::PointF point = event.GetPointPix();
point.Scale(1 / page_scale_);
GetRootBrowserAccessibilityManager()->HitTest(gfx::ToFlooredPoint(point),
/*request_id=*/0);
}
return true;
}
bool WebContentsAccessibilityAndroid::OnHoverEventNoRenderer(JNIEnv* env,
jfloat x,
jfloat y) {
gfx::PointF point = gfx::PointF(x, y);
if (BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager()) {
auto* hover_node = static_cast<BrowserAccessibilityAndroid*>(
root_manager->ApproximateHitTest(gfx::ToFlooredPoint(point)));
if (hover_node &&
hover_node != root_manager->GetBrowserAccessibilityRoot()) {
HandleHover(hover_node->GetUniqueId());
return true;
}
}
return false;
}
void WebContentsAccessibilityAndroid::HandleNavigate(int32_t root_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_handleNavigate(env, obj, root_id);
}
void WebContentsAccessibilityAndroid::UpdateMaxNodesInCache() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_updateMaxNodesInCache(env, obj);
}
void WebContentsAccessibilityAndroid::ClearNodeInfoCacheForGivenId(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_clearNodeInfoCacheForGivenId(env, obj,
unique_id);
}
std::u16string
WebContentsAccessibilityAndroid::GenerateAccessibilityNodeInfoString(
int32_t unique_id) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return {};
}
return base::android::ConvertJavaStringToUTF16(
Java_WebContentsAccessibilityImpl_generateAccessibilityNodeInfoString(
env, obj, unique_id));
}
base::android::ScopedJavaLocalRef<jstring>
WebContentsAccessibilityAndroid::GetSupportedHtmlElementTypes(JNIEnv* env) {
const auto& [search_map, all_keys, enum_map] = GetSearchKeyData();
return GetCanonicalJNIString(env, all_keys).AsLocalRef(env);
}
WebContentsAccessibilityAndroid::WebContentsAccessibilityAndroid() {}
jint WebContentsAccessibilityAndroid::GetRootId(JNIEnv* env) {
if (BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager()) {
auto* root = static_cast<BrowserAccessibilityAndroid*>(
root_manager->GetBrowserAccessibilityRoot());
if (root) {
return static_cast<jint>(root->GetUniqueId());
}
}
return ui::kAXAndroidInvalidViewId;
}
jboolean WebContentsAccessibilityAndroid::IsNodeValid(JNIEnv* env,
jint unique_id) {
return GetAXFromUniqueID(unique_id) != nullptr;
}
void WebContentsAccessibilityAndroid::HitTest(JNIEnv* env, jint x, jint y) {
if (BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager()) {
root_manager->HitTest(gfx::Point(x, y), /*request_id=*/0);
}
}
jboolean WebContentsAccessibilityAndroid::IsEditableText(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->IsTextField();
}
jboolean WebContentsAccessibilityAndroid::IsFocused(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->IsFocused();
}
jint WebContentsAccessibilityAndroid::GetEditableTextSelectionStart(
JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->GetSelectionStart();
}
jint WebContentsAccessibilityAndroid::GetEditableTextSelectionEnd(
JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->GetSelectionEnd();
}
ScopedJavaLocalRef<jintArray>
WebContentsAccessibilityAndroid::GetAbsolutePositionForNode(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return nullptr;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return nullptr;
}
float dip_scale = 1 / root_manager->device_scale_factor();
gfx::Rect absolute_rect = gfx::ScaleToEnclosingRect(
node->GetUnclippedRootFrameBoundsRect(), dip_scale, dip_scale);
int rect[] = {absolute_rect.x(), absolute_rect.y(), absolute_rect.right(),
absolute_rect.bottom()};
return base::android::ToJavaIntArray(env, rect);
}
static size_t ActualUnignoredChildCount(const ui::AXNode* node) {
size_t count = 0;
for (const ui::AXNode* child : node->children()) {
if (child->IsIgnored()) {
count += ActualUnignoredChildCount(child);
} else {
++count;
}
}
return count;
}
void WebContentsAccessibilityAndroid::UpdateAccessibilityNodeInfoBoundsRect(
JNIEnv* env,
const ScopedJavaLocalRef<jobject>& obj,
const JavaParamRef<jobject>& info,
jint unique_id,
BrowserAccessibilityAndroid* node) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return;
}
ui::AXOffscreenResult offscreen_result = ui::AXOffscreenResult::kOnscreen;
float dip_scale = 1 / root_manager->device_scale_factor();
gfx::Rect absolute_rect = gfx::ScaleToEnclosingRect(
node->GetUnclippedRootFrameBoundsRect(&offscreen_result), dip_scale,
dip_scale);
gfx::Rect parent_relative_rect = absolute_rect;
if (node->PlatformGetParent()) {
gfx::Rect parent_rect = gfx::ScaleToEnclosingRect(
node->PlatformGetParent()->GetUnclippedRootFrameBoundsRect(), dip_scale,
dip_scale);
parent_relative_rect.Offset(-parent_rect.OffsetFromOrigin());
}
bool is_offscreen = offscreen_result == ui::AXOffscreenResult::kOffscreen;
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoLocation(
env, obj, info, unique_id, absolute_rect.x(), absolute_rect.y(),
parent_relative_rect.x(), parent_relative_rect.y(), absolute_rect.width(),
absolute_rect.height(), is_offscreen);
}
jboolean WebContentsAccessibilityAndroid::UpdateCachedAccessibilityNodeInfo(
JNIEnv* env,
const JavaParamRef<jobject>& info,
jint unique_id) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return false;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
ScopedJavaLocalRef<jobject> obj = java_anib_ref_.get(env);
if (obj.is_null()) {
return false;
}
// Update cached nodes by providing new enclosing Rects
UpdateAccessibilityNodeInfoBoundsRect(env, obj, info, unique_id, node);
return true;
}
jboolean WebContentsAccessibilityAndroid::PopulateAccessibilityNodeInfo(
JNIEnv* env,
const JavaParamRef<jobject>& info,
jint unique_id) {
if (!GetRootBrowserAccessibilityManager()) {
return false;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
ScopedJavaLocalRef<jobject> obj = java_anib_ref_.get(env);
if (obj.is_null()) {
return false;
}
int parent_id = ui::kAXAndroidInvalidViewId;
auto* parent_node =
static_cast<BrowserAccessibilityAndroid*>(node->PlatformGetParent());
if (parent_node) {
parent_id = parent_node->GetUniqueId();
}
// Build a vector of child ids
std::vector<int> child_ids;
for (const auto& child : node->PlatformChildren()) {
const auto& android_node =
static_cast<const BrowserAccessibilityAndroid&>(child);
child_ids.push_back(android_node.GetUniqueId());
}
if (child_ids.size()) {
Java_AccessibilityNodeInfoBuilder_addAccessibilityNodeInfoChildren(
env, obj, info, base::android::ToJavaIntArray(env, child_ids));
}
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoBooleanAttributes(
env, obj, info, unique_id, node->IsCheckable(), node->IsClickable(),
node->IsContentInvalid(), node->IsEnabled(), node->IsFocusable(),
node->IsFocused(), node->HasImage(), node->IsPasswordField(),
node->IsScrollable(), node->IsSelected(), node->IsVisibleToUser(),
node->HasCharacterLocations(), node->IsRequired(), node->IsHeading());
Java_AccessibilityNodeInfoBuilder_addAccessibilityNodeInfoActions(
env, obj, info, unique_id, node->CanScrollForward(),
node->CanScrollBackward(), node->CanScrollUp(), node->CanScrollDown(),
node->CanScrollLeft(), node->CanScrollRight(), node->IsClickable(),
node->IsTextField(), node->IsEnabled(), node->IsFocusable(),
node->IsFocused(), node->IsCollapsed(), node->IsExpanded(),
node->HasNonEmptyValue(), !node->GetAccessibleNameUTF16().empty(),
node->IsSeekControl(), node->IsFormDescendant());
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoBaseAttributes(
env, obj, info, unique_id, parent_id,
GetCanonicalJNIString(env, node->GetClassName()),
GetCanonicalJNIString(env, node->GetRoleString()),
GetCanonicalJNIString(env, node->GetRoleDescription()),
base::android::ConvertUTF16ToJavaString(env, node->GetHint()),
base::android::ConvertUTF16ToJavaString(env, node->GetTooltipText()),
base::android::ConvertUTF16ToJavaString(env, node->GetTargetUrl()),
node->CanOpenPopup(), node->IsMultiLine(), node->AndroidInputType(),
node->AndroidLiveRegionType(),
GetCanonicalJNIString(env, node->GetContentInvalidErrorMessage()),
node->ClickableScore(), GetCanonicalJNIString(env, node->GetCSSDisplay()),
base::android::ConvertUTF16ToJavaString(env, node->GetBrailleLabel()),
GetCanonicalJNIString(env, node->GetBrailleRoleDescription()),
node->ExpandedState(), node->GetChecked());
ScopedJavaLocalRef<jintArray> suggestion_starts_java;
ScopedJavaLocalRef<jintArray> suggestion_ends_java;
ScopedJavaLocalRef<jobjectArray> suggestion_text_java;
std::vector<int> suggestion_starts;
std::vector<int> suggestion_ends;
node->GetSuggestions(&suggestion_starts, &suggestion_ends);
if (suggestion_starts.size() && suggestion_ends.size()) {
suggestion_starts_java =
base::android::ToJavaIntArray(env, suggestion_starts);
suggestion_ends_java = base::android::ToJavaIntArray(env, suggestion_ends);
// Currently we don't retrieve the text of each suggestion, so
// store a blank string for now.
std::vector<std::string> suggestion_text(suggestion_starts.size());
suggestion_text_java =
base::android::ToJavaArrayOfStrings(env, suggestion_text);
}
bool is_link = ui::IsLink(node->GetRole());
if (::features::IsAccessibilityTextFormattingEnabled()) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoText(
env, obj, info,
base::android::ConvertUTF16ToJavaString(env,
node->GetTextContentUTF16()),
is_link
? base::android::ConvertUTF16ToJavaString(env, node->GetTargetUrl())
: base::android::ConvertUTF16ToJavaString(env, std::u16string()),
is_link, node->IsTextField(),
GetCanonicalJNIString(env, node->GetInheritedString16Attribute(
ax::mojom::StringAttribute::kLanguage)),
suggestion_starts_java, suggestion_ends_java, suggestion_text_java,
base::android::ConvertUTF16ToJavaString(env,
node->GetStateDescription()),
base::android::ConvertUTF16ToJavaString(env, node->GetContainerTitle()),
base::android::ConvertUTF16ToJavaString(env,
node->GetContentDescription()),
base::android::ConvertUTF16ToJavaString(
env, node->GetSupplementalDescription()),
node->GetTextSize(), node->GetTextStyle(), node->GetTextColor(),
node->GetTextBackgroundColor(),
GetCanonicalJNIString(env, node->GetFontFamily()), node->IsSubscript(),
node->IsSuperscript());
} else {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoText(
env, obj, info,
base::android::ConvertUTF16ToJavaString(env,
node->GetTextContentUTF16()),
is_link
? base::android::ConvertUTF16ToJavaString(env, node->GetTargetUrl())
: base::android::ConvertUTF16ToJavaString(env, std::u16string()),
is_link, node->IsTextField(),
GetCanonicalJNIString(env, node->GetInheritedString16Attribute(
ax::mojom::StringAttribute::kLanguage)),
suggestion_starts_java, suggestion_ends_java, suggestion_text_java,
base::android::ConvertUTF16ToJavaString(env,
node->GetStateDescription()),
base::android::ConvertUTF16ToJavaString(env, node->GetContainerTitle()),
base::android::ConvertUTF16ToJavaString(env,
node->GetContentDescription()),
base::android::ConvertUTF16ToJavaString(
env, node->GetSupplementalDescription()));
}
std::u16string element_id;
if (node->GetString16Attribute(ax::mojom::StringAttribute::kHtmlId,
&element_id)) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoViewIdResourceName(
env, obj, info,
base::android::ConvertUTF16ToJavaString(env, element_id));
}
UpdateAccessibilityNodeInfoBoundsRect(env, obj, info, unique_id, node);
if (node->IsCollection()) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoCollectionInfo(
env, obj, info, node->RowCount(), node->ColumnCount(),
node->IsHierarchical(), node->GetSelectionMode());
}
if (node->IsCollectionItem() || node->IsTableHeader()) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoCollectionItemInfo(
env, obj, info, node->RowIndex(), node->RowSpan(), node->ColumnIndex(),
node->ColumnSpan(), node->IsTableHeader());
}
// For sliders that are numeric, use the AccessibilityNodeInfo.RangeInfo
// object as expected. But for non-numeric ranges (e.g. "small", "medium",
// "large"), do not set the RangeInfo object and instead rely on announcing
// the aria-valuetext value, which will be included in the node's text value.
if (node->IsRangeControlWithoutAriaValueText()) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoRangeInfo(
env, obj, info, node->AndroidRangeType(), node->RangeMin(),
node->RangeMax(), node->RangeCurrentValue());
}
if (node->ShouldUsePaneTitle()) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoPaneTitle(
env, obj, info,
base::android::ConvertUTF16ToJavaString(env, node->GetPaneTitle()));
}
if (node->IsTextField()) {
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoSelectionAttrs(
env, obj, info, node->GetSelectionStart(), node->GetSelectionEnd());
}
return true;
}
jboolean WebContentsAccessibilityAndroid::PopulateAccessibilityEvent(
JNIEnv* env,
const JavaParamRef<jobject>& event,
jint unique_id,
jint event_type) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return false;
}
// We will always set boolean, classname, list and scroll attributes.
Java_WebContentsAccessibilityImpl_setAccessibilityEventBaseAttributes(
env, obj, event, node->IsChecked(), node->IsEnabled(),
node->IsPasswordField(), node->IsScrollable(), node->GetItemIndex(),
node->GetItemCount(), node->GetScrollX(), node->GetScrollY(),
node->GetMaxScrollX(), node->GetMaxScrollY(),
GetCanonicalJNIString(env, node->GetClassName()));
switch (event_type) {
case ANDROID_ACCESSIBILITY_EVENT_TEXT_CHANGED: {
std::u16string before_text = node->GetTextChangeBeforeText();
std::u16string text = node->GetTextContentUTF16();
Java_WebContentsAccessibilityImpl_setAccessibilityEventTextChangedAttrs(
env, obj, event, node->GetTextChangeFromIndex(),
node->GetTextChangeAddedCount(), node->GetTextChangeRemovedCount(),
base::android::ConvertUTF16ToJavaString(env, before_text),
base::android::ConvertUTF16ToJavaString(env, text));
break;
}
case ANDROID_ACCESSIBILITY_EVENT_TEXT_SELECTION_CHANGED: {
std::u16string text = node->GetTextContentUTF16();
Java_WebContentsAccessibilityImpl_setAccessibilityEventSelectionAttrs(
env, obj, event, node->GetSelectionStart(), node->GetSelectionEnd(),
node->GetEditableTextLength(),
base::android::ConvertUTF16ToJavaString(env, text));
break;
}
default:
break;
}
return true;
}
void WebContentsAccessibilityAndroid::Click(JNIEnv* env, jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return;
}
// If it's a heading consisting of only a link, click the link.
if (node->IsHeadingLink()) {
node = static_cast<BrowserAccessibilityAndroid*>(
node->InternalChildrenBegin().get());
}
// Only perform the default action on a node that is enabled. Having the
// ACTION_CLICK action on the node is not sufficient, since TalkBack won't
// announce a control as disabled unless it's also marked as clickable, so
// disabled nodes are secretly clickable if we do not check here.
// Children of disabled controls/widgets will also have the click action, so
// ensure that parents/ancestry chain is enabled as well.
if (node->IsEnabled() && !node->IsDisabledDescendant()) {
node->manager()->DoDefaultAction(*node);
}
}
void WebContentsAccessibilityAndroid::Focus(JNIEnv* env, jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (node) {
node->manager()->SetFocus(*node);
}
}
void WebContentsAccessibilityAndroid::Blur(JNIEnv* env) {
if (BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager()) {
root_manager->SetFocus(*root_manager->GetBrowserAccessibilityRoot());
}
}
void WebContentsAccessibilityAndroid::ScrollToMakeNodeVisible(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (node) {
node->manager()->ScrollToMakeVisible(
*node, gfx::Rect(node->GetUnclippedFrameBoundsRect().size()));
}
}
void WebContentsAccessibilityAndroid::SetTextFieldValue(
JNIEnv* env,
jint unique_id,
const JavaParamRef<jstring>& value) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (node) {
node->manager()->SetValue(
*node, base::android::ConvertJavaStringToUTF8(env, value));
}
}
void WebContentsAccessibilityAndroid::SetSelection(JNIEnv* env,
jint unique_id,
jint start,
jint end) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (node) {
node->manager()->SetSelection(ui::BrowserAccessibility::AXRange(
node->CreatePositionForSelectionAt(start),
node->CreatePositionForSelectionAt(end)));
}
}
jboolean WebContentsAccessibilityAndroid::AdjustSlider(JNIEnv* env,
jint unique_id,
jboolean increment) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
BrowserAccessibilityAndroid* android_node =
static_cast<BrowserAccessibilityAndroid*>(node);
if (!android_node->IsSlider() || !android_node->IsEnabled()) {
return false;
}
float value =
node->GetFloatAttribute(ax::mojom::FloatAttribute::kValueForRange);
float min =
node->GetFloatAttribute(ax::mojom::FloatAttribute::kMinValueForRange);
float max =
node->GetFloatAttribute(ax::mojom::FloatAttribute::kMaxValueForRange);
if (max <= min) {
return false;
}
// If this node has defined a step value, move by that amount. Otherwise, to
// behave similarly to an Android SeekBar, move by an increment of ~5%.
float delta;
if (node->HasFloatAttribute(ax::mojom::FloatAttribute::kStepValueForRange)) {
delta =
node->GetFloatAttribute(ax::mojom::FloatAttribute::kStepValueForRange);
} else {
delta = (max - min) / kDefaultNumberOfTicksForSliders;
}
// Add/Subtract based on |increment| boolean, then clamp to range.
float original_value = value;
value += (increment ? delta : -delta);
value = std::clamp(value, min, max);
if (value != original_value) {
node->manager()->SetValue(*node, base::NumberToString(value));
return true;
}
return false;
}
void WebContentsAccessibilityAndroid::ShowContextMenu(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (node) {
node->manager()->ShowContextMenu(*node);
}
}
jint WebContentsAccessibilityAndroid::FindElementType(
JNIEnv* env,
jint start_id,
const JavaParamRef<jstring>& element_type_str,
jboolean forwards,
jboolean can_wrap_to_last_element,
jboolean use_default_predicate,
jboolean is_known_screen_reader_enabled,
jboolean is_only_one_accessibility_service_enabled) {
base::ElapsedTimer timer;
BrowserAccessibilityAndroid* start_node = GetAXFromUniqueID(start_id);
if (!start_node) {
return ui::kInvalidAXNodeID;
}
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return ui::kInvalidAXNodeID;
}
ui::BrowserAccessibility* root = root_manager->GetBrowserAccessibilityRoot();
if (!root) {
return ui::kInvalidAXNodeID;
}
// If |element_type_str| was empty, we can skip to the default predicate.
ui::AccessibilityMatchPredicate predicate;
std::u16string element_type;
if (use_default_predicate) {
predicate = AllInterestingNodesPredicate;
element_type = u"DEFAULT";
} else {
element_type =
base::android::ConvertJavaStringToUTF16(env, element_type_str);
if (std::optional<int> ret =
MaybeFindRowColumn(start_node, element_type, forwards);
ret) {
ui::BrowserAccessibility* node = start_node->manager()->GetFromID(*ret);
return node ? static_cast<BrowserAccessibilityAndroid*>(node)
->GetUniqueId()
: ui::kInvalidAXNodeID;
}
predicate = PredicateForSearchKey(element_type);
}
// Record the type of element that was used for navigation, splitting by
// whether or not a screen reader was running to see how frequently other apps
// use this functionality.
if (is_known_screen_reader_enabled &&
is_only_one_accessibility_service_enabled) {
base::UmaHistogramEnumeration(
"Accessibility.Android.FindElementType.Usage2.TalkBack",
EnumForPredicate(element_type));
} else if (is_known_screen_reader_enabled) {
base::UmaHistogramEnumeration(
"Accessibility.Android.FindElementType.Usage2."
"TalkBackWithOtherAT",
EnumForPredicate(element_type));
} else {
// When TalkBack isn't running, split by AXMode (for TB we know we will for
// sure be in a mode with kExtendedProperties).
BrowserAccessibilityStateImpl* accessibility_state =
BrowserAccessibilityStateImpl::GetInstance();
ui::AXMode mode = accessibility_state->GetAccessibilityMode();
std::string suffix;
if (mode == ui::kAXModeBasic) {
suffix = "Basic";
} else if (mode == ui::kAXModeWebContentsOnly) {
suffix = "WebContentsOnly";
} else if (mode == ui::kAXModeFormControls) {
suffix = "FormControls";
} else {
suffix = "Unnamed";
}
base::UmaHistogramEnumeration(
"Accessibility.Android.FindElementType.Usage2.NoTalkBack." + suffix,
EnumForPredicate(element_type));
}
ui::OneShotAccessibilityTreeSearch tree_search(root);
tree_search.SetStartNode(start_node);
tree_search.SetDirection(forwards
? ui::OneShotAccessibilityTreeSearch::FORWARDS
: ui::OneShotAccessibilityTreeSearch::BACKWARDS);
tree_search.SetResultLimit(1);
tree_search.SetImmediateDescendantsOnly(false);
tree_search.SetCanWrapToLastElement(can_wrap_to_last_element);
tree_search.SetOnscreenOnly(false);
tree_search.AddPredicate(predicate);
if (tree_search.CountMatches() == 0) {
return ui::kInvalidAXNodeID;
}
auto* android_node =
static_cast<BrowserAccessibilityAndroid*>(tree_search.GetMatchAtIndex(0));
int32_t element_id = android_node->GetUniqueId();
// Navigate forwards to the autofill popup's proxy node if focus is currently
// on the element hosting the autofill popup. Once within the popup, a back
// press will navigate back to the element hosting the popup. If user swipes
// past last suggestion in the popup, or swipes left from the first suggestion
// in the popup, we will navigate to the element that is the next element in
// the document after the element hosting the popup.
if (forwards && start_id == g_element_hosting_autofill_popup_unique_id &&
g_autofill_popup_proxy_node) {
g_element_after_element_hosting_autofill_popup_unique_id = element_id;
auto* proxy_android_node =
static_cast<BrowserAccessibilityAndroid*>(g_autofill_popup_proxy_node);
return proxy_android_node->GetUniqueId();
}
// TODO(mschillaci): The current max is set to 1000 microseconda, check scale
// after initial data.
base::UmaHistogramCustomMicrosecondsTimes(
"Accessibility.Android.Performance.OneShotTreeSearch",
base::Microseconds(timer.Elapsed().InMicrosecondsF()),
base::Microseconds(1), base::Microseconds(1000), 80);
return element_id;
}
jboolean WebContentsAccessibilityAndroid::NextAtGranularity(
JNIEnv* env,
jint granularity,
jboolean extend_selection,
jint unique_id,
jint cursor_index) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return false;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
jint start_index = -1;
int end_index = -1;
if (root_manager->NextAtGranularity(granularity, cursor_index, node,
&start_index, &end_index)) {
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return false;
}
std::u16string text = node->GetTextContentUTF16();
Java_WebContentsAccessibilityImpl_finishGranularityMoveNext(
env, obj, base::android::ConvertUTF16ToJavaString(env, text),
extend_selection, start_index, end_index);
return true;
}
return false;
}
jint WebContentsAccessibilityAndroid::GetTextLength(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return -1;
}
std::u16string text = node->GetTextContentUTF16();
return text.size();
}
void WebContentsAccessibilityAndroid::AddSpellingErrorForTesting(
JNIEnv* env,
jint unique_id,
jint start_offset,
jint end_offset) {
ui::BrowserAccessibility* node = GetAXFromUniqueID(unique_id);
CHECK(node);
while (node->GetRole() != ax::mojom::Role::kStaticText &&
node->InternalChildCount() > 0) {
node = node->InternalChildrenBegin().get();
}
CHECK(node->GetRole() == ax::mojom::Role::kStaticText);
std::u16string text = node->GetTextContentUTF16();
CHECK_LT(start_offset, static_cast<int>(text.size()));
CHECK_LE(end_offset, static_cast<int>(text.size()));
ui::AXNodeData data = node->GetData();
data.AddIntListAttribute(ax::mojom::IntListAttribute::kMarkerStarts,
{start_offset});
data.AddIntListAttribute(ax::mojom::IntListAttribute::kMarkerEnds,
{end_offset});
data.AddIntListAttribute(
ax::mojom::IntListAttribute::kMarkerTypes,
{static_cast<int>(ax::mojom::MarkerType::kSuggestion)});
node->node()->SetData(data);
}
jboolean WebContentsAccessibilityAndroid::PreviousAtGranularity(
JNIEnv* env,
jint granularity,
jboolean extend_selection,
jint unique_id,
jint cursor_index) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return false;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
jint start_index = -1;
int end_index = -1;
if (root_manager->PreviousAtGranularity(granularity, cursor_index, node,
&start_index, &end_index)) {
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return false;
}
Java_WebContentsAccessibilityImpl_finishGranularityMovePrevious(
env, obj,
base::android::ConvertUTF16ToJavaString(env,
node->GetTextContentUTF16()),
extend_selection, start_index, end_index);
return true;
}
return false;
}
void WebContentsAccessibilityAndroid::RecordInlineTextBoxMetrics(
bool from_focus) {
// Track the current AXMode via UMA. We do this here and not in the
// manager so that we can get a signal of whether we are making requests
// for modes that don't have kInlineTextBoxes mode flag.
BrowserAccessibilityStateImpl* accessibility_state =
BrowserAccessibilityStateImpl::GetInstance();
ui::AXMode mode = accessibility_state->GetAccessibilityMode();
ui::AXMode::BundleHistogramValue bundle;
// Clear out any modes that will confuse the bundle detection.
mode &= ui::kAXModeComplete | ui::kAXModeFormControls;
if (mode == ui::kAXModeBasic) {
bundle = ui::AXMode::BundleHistogramValue::kBasic;
} else if (mode == ui::kAXModeWebContentsOnly) {
bundle = ui::AXMode::BundleHistogramValue::kWebContentsOnly;
} else if (mode == ui::kAXModeComplete) {
bundle = ui::AXMode::BundleHistogramValue::kComplete;
} else if (mode == ui::kAXModeFormControls) {
bundle = ui::AXMode::BundleHistogramValue::kFormControls;
} else {
bundle = ui::AXMode::BundleHistogramValue::kUnnamed;
}
if (from_focus) {
base::UmaHistogramEnumeration(
"Accessibility.Android.InlineTextBoxes.Bundle.FromFocus", bundle);
} else {
base::UmaHistogramEnumeration(
"Accessibility.Android.InlineTextBoxes.Bundle.ExtraData", bundle);
}
}
void WebContentsAccessibilityAndroid::MoveAccessibilityFocus(
JNIEnv* env,
jint old_unique_id,
jint new_unique_id) {
BrowserAccessibilityAndroid* old_node = GetAXFromUniqueID(old_unique_id);
if (old_node) {
old_node->manager()->ClearAccessibilityFocus(*old_node);
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(new_unique_id);
if (!node) {
return;
}
node->manager()->SetAccessibilityFocus(*node);
// When Android sets accessibility focus to a node, we load inline text
// boxes for that node so that subsequent requests for character bounding
// boxes will succeed. However, don't do that for the root of the tree,
// as that will result in loading inline text boxes for the whole tree.
if (node != node->manager()->GetBrowserAccessibilityRoot()) {
node->manager()->LoadInlineTextBoxes(*node);
RecordInlineTextBoxMetrics(/*from_focus=*/true);
}
}
void WebContentsAccessibilityAndroid::SetSequentialFocusStartingPoint(
JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return;
}
node->manager()->SetSequentialFocusNavigationStartingPoint(*node);
}
bool WebContentsAccessibilityAndroid::IsSlider(JNIEnv* env, jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->GetRole() == ax::mojom::Role::kSlider;
}
void WebContentsAccessibilityAndroid::OnAutofillPopupDisplayed(JNIEnv* env) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return;
}
ui::BrowserAccessibility* current_focus = root_manager->GetFocus();
if (!current_focus) {
return;
}
DeleteAutofillPopupProxy();
// The node is isolated (tree is nullptr) and its id is not important,
// it should only be not equal to kInvalidAXNodeID to be considered valid.
ui::AXNodeID id = ~ui::kInvalidAXNodeID;
g_autofill_popup_proxy_node_ax_node = new ui::AXNode(nullptr, nullptr, id, 0);
ui::AXNodeData ax_node_data;
ax_node_data.id = id;
ax_node_data.role = ax::mojom::Role::kMenu;
ax_node_data.SetName("Autofill");
ax_node_data.SetRestriction(ax::mojom::Restriction::kReadOnly);
ax_node_data.AddState(ax::mojom::State::kFocusable);
ax_node_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, false);
g_autofill_popup_proxy_node_ax_node->SetData(ax_node_data);
g_autofill_popup_proxy_node =
ui::BrowserAccessibility::Create(root_manager,
g_autofill_popup_proxy_node_ax_node)
.release();
auto* android_node = static_cast<BrowserAccessibilityAndroid*>(current_focus);
g_element_hosting_autofill_popup_unique_id = android_node->GetUniqueId();
}
void WebContentsAccessibilityAndroid::OnAutofillPopupDismissed(JNIEnv* env) {
g_element_hosting_autofill_popup_unique_id = -1;
g_element_after_element_hosting_autofill_popup_unique_id = -1;
DeleteAutofillPopupProxy();
}
jint WebContentsAccessibilityAndroid::
GetIdForElementAfterElementHostingAutofillPopup(JNIEnv* env) {
if (g_element_after_element_hosting_autofill_popup_unique_id == -1 ||
!GetAXFromUniqueID(
g_element_after_element_hosting_autofill_popup_unique_id)) {
return ui::kInvalidAXNodeID;
}
return g_element_after_element_hosting_autofill_popup_unique_id;
}
jboolean WebContentsAccessibilityAndroid::IsAutofillPopupNode(JNIEnv* env,
jint unique_id) {
auto* android_node =
static_cast<BrowserAccessibilityAndroid*>(g_autofill_popup_proxy_node);
return g_autofill_popup_proxy_node &&
android_node->GetUniqueId() == unique_id;
}
bool WebContentsAccessibilityAndroid::Scroll(JNIEnv* env,
jint unique_id,
int direction,
bool is_page_scroll) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->Scroll(direction, is_page_scroll);
}
bool WebContentsAccessibilityAndroid::SetRangeValue(JNIEnv* env,
jint unique_id,
float value) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
BrowserAccessibilityAndroid* android_node =
static_cast<BrowserAccessibilityAndroid*>(node);
if (!android_node->GetData().IsRangeValueSupported()) {
return false;
}
float min =
node->GetFloatAttribute(ax::mojom::FloatAttribute::kMinValueForRange);
float max =
node->GetFloatAttribute(ax::mojom::FloatAttribute::kMaxValueForRange);
if (max <= min) {
return false;
}
value = std::clamp(value, min, max);
node->manager()->SetValue(*node, base::NumberToString(value));
return true;
}
jboolean WebContentsAccessibilityAndroid::AreInlineTextBoxesLoaded(
JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
return node->AreInlineTextBoxesLoaded();
}
void WebContentsAccessibilityAndroid::LoadInlineTextBoxes(JNIEnv* env,
jint unique_id) {
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (node) {
node->manager()->LoadInlineTextBoxes(*node);
RecordInlineTextBoxMetrics(/*from_focus=*/false);
}
}
ScopedJavaLocalRef<jintArray>
WebContentsAccessibilityAndroid::GetCharacterBoundingBoxes(JNIEnv* env,
jint unique_id,
jint start,
jint len) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return nullptr;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return nullptr;
}
if (len <= 0 || len > kMaxCharacterBoundingBoxLen) {
LOG(ERROR) << "Trying to request EXTRA_DATA_TEXT_CHARACTER_LOCATION_KEY "
<< "with a length of " << len << ". Valid values are between 1 "
<< "and " << kMaxCharacterBoundingBoxLen;
return nullptr;
}
float dip_scale = 1 / root_manager->device_scale_factor();
gfx::Rect object_bounds = node->GetUnclippedRootFrameBoundsRect();
base::FixedArray<int> coords(4 * len);
for (int i = 0; i < len; i++) {
gfx::Rect char_bounds = node->GetUnclippedRootFrameInnerTextRangeBoundsRect(
start + i, start + i + 1);
if (char_bounds.IsEmpty()) {
char_bounds = object_bounds;
}
char_bounds = gfx::ScaleToEnclosingRect(char_bounds, dip_scale, dip_scale);
coords[4 * i + 0] = char_bounds.x();
coords[4 * i + 1] = char_bounds.y();
coords[4 * i + 2] = char_bounds.right();
coords[4 * i + 3] = char_bounds.bottom();
}
return base::android::ToJavaIntArray(env, coords);
}
jboolean WebContentsAccessibilityAndroid::GetImageData(
JNIEnv* env,
const JavaParamRef<jobject>& info,
jint unique_id,
jboolean has_sent_previous_request) {
BrowserAccessibilityManagerAndroid* root_manager =
GetRootBrowserAccessibilityManager();
if (!root_manager) {
return false;
}
BrowserAccessibilityAndroid* node = GetAXFromUniqueID(unique_id);
if (!node) {
return false;
}
// If this is a valid node, try to get an image data url for it.
std::string image_data_url =
node->GetStringAttribute(ax::mojom::StringAttribute::kImageDataUrl);
std::string mimetype;
std::string charset;
std::string image_data;
bool success = net::DataURL::Parse(GURL(image_data_url), &mimetype, &charset,
&image_data);
if (!success) {
// If getting the image data url was not successful, then request that the
// information be added to the node asynchronously (if it has not previously
// been requested), and return false.
if (!has_sent_previous_request) {
root_manager->GetImageData(*node, kMaxImageSize);
}
return false;
}
ScopedJavaLocalRef<jobject> obj = java_anib_ref_.get(env);
if (obj.is_null()) {
return false;
}
// If the image data has been retrieved from the image data url successfully,
// then convert it to a Java byte array and add it in the Bundle extras of
// the node, and return true.
Java_AccessibilityNodeInfoBuilder_setAccessibilityNodeInfoImageData(
env, obj, info, base::android::ToJavaByteArray(env, image_data));
return true;
}
BrowserAccessibilityManagerAndroid*
WebContentsAccessibilityAndroid::GetRootBrowserAccessibilityManager() {
if (snapshot_root_manager_) {
return snapshot_root_manager_.get();
}
return static_cast<BrowserAccessibilityManagerAndroid*>(
web_contents_->GetRootBrowserAccessibilityManager());
}
BrowserAccessibilityAndroid* WebContentsAccessibilityAndroid::GetAXFromUniqueID(
int32_t unique_id) {
return BrowserAccessibilityAndroid::GetFromUniqueId(unique_id);
}
void WebContentsAccessibilityAndroid::UpdateFrameInfo(float page_scale) {
page_scale_ = page_scale;
if (frame_info_initialized_) {
return;
}
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
if (obj.is_null()) {
return;
}
Java_WebContentsAccessibilityImpl_notifyFrameInfoInitialized(env, obj);
frame_info_initialized_ = true;
}
void WebContentsAccessibilityAndroid::RequestAccessibilityTreeSnapshot(
JNIEnv* env,
const JavaParamRef<jobject>& view_structure_root,
const JavaParamRef<jobject>& accessibility_coordinates,
const JavaParamRef<jobject>& view,
const JavaParamRef<jobject>& on_done_callback) {
// This method should only be called by the unified snapshots feature.
CHECK(base::FeatureList::IsEnabled(features::kAccessibilityUnifiedSnapshots));
// This is the callback provided by the Java-side code and will be called
// after the snapshot has been requested and fully processed. This is not to
// be confused with the ProcessCompletedAccessibilityTreeSnapshot callback
// below, which is called once the renderer has returned all AXTreeUpdates.
on_done_callback_ = std::move(on_done_callback);
accessibility_coordinates_ = accessibility_coordinates;
view_ = view;
base::android::ScopedJavaGlobalRef<jobject> movable_view_structure_root;
movable_view_structure_root.Reset(env, view_structure_root);
// Define snapshot parameters:
auto params = mojom::SnapshotAccessibilityTreeParams::New();
params->ax_mode = ui::AXMode(ui::kAXModeComplete.flags() | ui::AXMode::kHTML |
ui::AXMode::kHTMLMetadata)
.flags();
params->max_nodes = 5000;
params->timeout = base::Seconds(2);
// Use AccessibilityTreeSnapshotCombiner to perform snapshots
auto combiner = base::MakeRefCounted<AccessibilityTreeSnapshotCombiner>(
base::BindOnce(&WebContentsAccessibilityAndroid::
ProcessCompletedAccessibilityTreeSnapshot,
GetWeakPtr(), env, std::move(movable_view_structure_root)),
std::move(params));
web_contents_->GetPrimaryMainFrame()->ForEachRenderFrameHostImpl(
[&combiner](RenderFrameHostImpl* rfhi) {
combiner->RequestSnapshotOnRenderFrameHost(rfhi);
});
}
void WebContentsAccessibilityAndroid::ProcessCompletedAccessibilityTreeSnapshot(
JNIEnv* env,
const base::android::JavaRef<jobject>& view_structure_root,
ui::AXTreeUpdate& result) {
// If we don't have a connection back to the Java-side objects, then stop. It
// may be that the Java-side object was destroyed (e.g. tab closed) before the
// snapshot was able to finish.
ScopedJavaLocalRef<jobject> obj = java_adb_ref_.get(env);
if (!obj) {
on_done_callback_ = nullptr;
accessibility_coordinates_ = nullptr;
view_ = nullptr;
return;
}
// Construct a root manager without a delegate using the snapshot result.
snapshot_root_manager_ = std::make_unique<BrowserAccessibilityManagerAndroid>(
result, GetWeakPtr(), *this, /* delegate= */ nullptr);
auto* root = static_cast<BrowserAccessibilityAndroid*>(
snapshot_root_manager_->GetBrowserAccessibilityRoot());
CHECK(root);
// Construct the Java-side tree, use the JNI builder `java_adb_ref_` to
// recursively construct each node of the tree starting with the provided
// root.
RecursivelyPopulateViewStructureTree(env, obj, root, view_structure_root,
/* is_root= */ true);
// Add tree-level (root only) data to Java-side tree (e.g. HTML metadata).
const auto& metadata_strings =
GetRootBrowserAccessibilityManager()->GetMetadataForTree();
if (!metadata_strings.empty()) {
Java_AssistDataBuilder_populateHTMLMetadataProperties(
env, obj, view_structure_root,
base::android::ToJavaArrayOfStrings(env, metadata_strings));
}
// We have fulfilled the request for an accessibility tree snapshot, so we can
// now call the provided Java-side callback to inform original client that the
// async construction is complete.
base::android::RunRunnableAndroid(on_done_callback_);
}
void WebContentsAccessibilityAndroid::RecursivelyPopulateViewStructureTree(
JNIEnv* env,
ScopedJavaLocalRef<jobject> obj,
const BrowserAccessibilityAndroid* node,
const base::android::JavaRef<jobject>& java_side_assist_data_object,
bool is_root) {
PopulateViewStructureNode(env, obj, node, java_side_assist_data_object);
for (size_t child_index = 0; const auto& child : node->PlatformChildren()) {
const auto& child_node =
static_cast<const BrowserAccessibilityAndroid&>(child);
ScopedJavaLocalRef<jobject> java_side_child_object =
Java_AssistDataBuilder_addChildNode(
env, obj, java_side_assist_data_object, child_index);
child_index++;
RecursivelyPopulateViewStructureTree(env, obj, &child_node,
java_side_child_object,
/* is_root= */ false);
}
if (!is_root) {
Java_AssistDataBuilder_commitNode(env, obj, java_side_assist_data_object);
}
}
void WebContentsAccessibilityAndroid::PopulateViewStructureNode(
JNIEnv* env,
ScopedJavaLocalRef<jobject> obj,
const BrowserAccessibilityAndroid* node,
const base::android::JavaRef<jobject>& java_side_assist_data_object) {
Java_AssistDataBuilder_populateBaseProperties(
env, obj, java_side_assist_data_object,
GetCanonicalJNIString(env, node->GetClassName()), node->GetChildCount());
int bgcolor = 0;
int color = 0;
int text_size = -1.0;
if (node->HasFloatAttribute(ax::mojom::FloatAttribute::kFontSize)) {
color = node->GetIntAttribute(ax::mojom::IntAttribute::kColor);
bgcolor = node->GetIntAttribute(ax::mojom::IntAttribute::kBackgroundColor);
text_size = node->GetFloatAttribute(ax::mojom::FloatAttribute::kFontSize);
}
Java_AssistDataBuilder_populateTextProperties(
env, obj, java_side_assist_data_object,
base::android::ConvertUTF16ToJavaString(env, node->GetTextContentUTF16()),
node->GetSelectedItemCount() > 0, node->GetSelectionStart(),
node->GetSelectionEnd(), color, bgcolor, text_size,
node->HasTextStyle(ax::mojom::TextStyle::kBold),
node->HasTextStyle(ax::mojom::TextStyle::kItalic),
node->HasTextStyle(ax::mojom::TextStyle::kUnderline),
node->HasTextStyle(ax::mojom::TextStyle::kLineThrough));
float dip_scale =
1 /
web_contents_->GetPrimaryMainFrame()->AccessibilityGetDeviceScaleFactor();
gfx::Rect absolute_rect = gfx::ScaleToEnclosingRect(
node->GetUnclippedRootFrameBoundsRect(), dip_scale, dip_scale);
Java_AssistDataBuilder_populateBoundsProperties(
env, obj, java_side_assist_data_object, absolute_rect.x(),
absolute_rect.y(), absolute_rect.width(), absolute_rect.height(),
accessibility_coordinates_, view_);
std::vector<std::vector<std::u16string>> html_attrs;
for (const auto& attr : node->GetHtmlAttributes()) {
html_attrs.push_back(
{base::UTF8ToUTF16(attr.first), base::UTF8ToUTF16(attr.second)});
}
Java_AssistDataBuilder_populateHTMLProperties(
env, obj, java_side_assist_data_object,
GetCanonicalJNIString(
env, node->GetStringAttribute(ax::mojom::StringAttribute::kHtmlTag)),
GetCanonicalJNIString(
env, node->GetStringAttribute(ax::mojom::StringAttribute::kDisplay)),
base::android::ToJavaArrayOfStringArray(env, html_attrs));
}
base::WeakPtr<WebContentsAccessibilityAndroid>
WebContentsAccessibilityAndroid::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
jlong JNI_WebContentsAccessibilityImpl_InitWithAXTree(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jlong ax_tree_update_ptr,
const JavaParamRef<jobject>& jaccessibility_node_info_builder) {
return reinterpret_cast<intptr_t>(new WebContentsAccessibilityAndroid(
env, obj, ax_tree_update_ptr, jaccessibility_node_info_builder));
}
jlong JNI_WebContentsAccessibilityImpl_Init(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jweb_contents,
const JavaParamRef<jobject>& jaccessibility_node_info_builder) {
WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
DCHECK(web_contents);
return reinterpret_cast<intptr_t>(new WebContentsAccessibilityAndroid(
env, obj, web_contents, jaccessibility_node_info_builder));
}
jlong JNI_WebContentsAccessibilityImpl_InitForAssistData(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jweb_contents,
const JavaParamRef<jobject>& jassist_data_builder) {
WebContents* web_contents = WebContents::FromJavaWebContents(jweb_contents);
DCHECK(web_contents);
return reinterpret_cast<intptr_t>(new WebContentsAccessibilityAndroid(
env, obj, jassist_data_builder, web_contents));
}
} // namespace content
|