File: AXIsolatedTree.cpp

package info (click to toggle)
webkit2gtk 2.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 445,780 kB
  • sloc: cpp: 3,798,013; javascript: 197,914; ansic: 161,337; python: 49,141; asm: 21,990; ruby: 18,540; perl: 16,723; xml: 4,623; yacc: 2,360; sh: 2,246; java: 2,019; lex: 1,327; pascal: 366; makefile: 298
file content (2174 lines) | stat: -rw-r--r-- 95,796 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2019-2025 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
#include "AXIsolatedTree.h"

#include "AXIsolatedObject.h"
#include "AXLogger.h"
#include "AccessibilityTable.h"
#include "AccessibilityTableCell.h"
#include "AccessibilityTableRow.h"
#include "DocumentInlines.h"
#include "FrameSelection.h"
#include "HTMLNames.h"
#include "LocalFrameView.h"
#include "Page.h"
#include <wtf/MonotonicTime.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/SetForScope.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>

namespace WebCore {

using namespace HTMLNames;

DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(AXIsolatedTree);
WTF_MAKE_TZONE_ALLOCATED_IMPL(AXIsolatedTree);

static const Seconds CreationFeedbackInterval { 3_s };

HashMap<PageIdentifier, Ref<AXIsolatedTree>>& AXIsolatedTree::treePageCache()
{
    static NeverDestroyed<HashMap<PageIdentifier, Ref<AXIsolatedTree>>> map;
    return map;
}

AXIsolatedTree::AXIsolatedTree(AXObjectCache& axObjectCache)
    : AXTreeStore(axObjectCache.treeID())
    , m_axObjectCache(&axObjectCache)
    , m_geometryManager(axObjectCache.m_geometryManager.ptr())
    , m_pageActivityState(axObjectCache.pageActivityState())
{
    AXTRACE("AXIsolatedTree::AXIsolatedTree"_s);
    ASSERT(isMainThread());
}

AXIsolatedTree::~AXIsolatedTree()
{
    AXTRACE("AXIsolatedTree::~AXIsolatedTree"_s);
}

void AXIsolatedTree::queueForDestruction()
{
    AXTRACE("AXIsolatedTree::queueForDestruction"_s);
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    m_queuedForDestruction = true;
}

Ref<AXIsolatedTree> AXIsolatedTree::createEmpty(AXObjectCache& axObjectCache)
{
    AXTRACE("AXIsolatedTree::createEmpty"_s);
    ASSERT(isMainThread());
    ASSERT(axObjectCache.pageID());

    auto tree = adoptRef(*new AXIsolatedTree(axObjectCache));

    if (RefPtr axRoot = axObjectCache.document() ? axObjectCache.getOrCreate(axObjectCache.document()->view()) : nullptr) {
        tree->updatingSubtree(axRoot.get());
        tree->createEmptyContent(*axRoot);
    }

    tree->updateLoadingProgress(axObjectCache.loadingProgress());
    tree->m_processingProgress = 0;

    // Now that the tree is ready to take client requests, add it to the tree maps so that it can be found.
    storeTree(axObjectCache, tree);

    return tree;
}

void AXIsolatedTree::createEmptyContent(AccessibilityObject& axRoot)
{
    ASSERT(isMainThread());
    ASSERT(!axRoot.isDetached());
    ASSERT(axRoot.isScrollView() && !axRoot.parentObject());

    // An empty content tree consists only of the ScrollView and WebArea objects.
    m_isEmptyContentTree = true;

    // Create the IsolatedObjects for the root/ScrollView and WebArea.
    auto rootData = createIsolatedObjectData(axRoot, *this);
    rootData.setProperty(AXProperty::ScreenRelativePosition, axRoot.screenRelativePosition());
    NodeChange rootAppend { WTFMove(rootData), axRoot.wrapper() };

    RefPtr axWebArea = Accessibility::findUnignoredChild(axRoot, [] (auto& object) {
        return object->isWebArea();
    });
    if (!axWebArea) {
        ASSERT_NOT_REACHED();
        return;
    }
    auto webAreaData = createIsolatedObjectData(*axWebArea, *this);
    webAreaData.setProperty(AXProperty::ScreenRelativePosition, axWebArea->screenRelativePosition());
    NodeChange webAreaAppend { WTFMove(webAreaData), axWebArea->wrapper() };

    m_nodeMap.set(axRoot.objectID(), ParentChildrenIDs { std::nullopt, { axWebArea->objectID() } });
    m_nodeMap.set(axWebArea->objectID(), ParentChildrenIDs { axRoot.objectID(), { } });

    {
        Locker locker { m_changeLogLock };
        setPendingRootNodeIDLocked(axRoot.objectID());
        m_pendingFocusedNodeID = axWebArea->objectID();
    }
    queueAppendsAndRemovals({ rootAppend, webAreaAppend }, { });
}

RefPtr<AXIsolatedTree> AXIsolatedTree::create(AXObjectCache& axObjectCache)
{
    AXTRACE("AXIsolatedTree::create"_s);
    ASSERT(isMainThread());
    ASSERT(axObjectCache.pageID());

    auto tree = adoptRef(*new AXIsolatedTree(axObjectCache));
    if (RefPtr existingTree = isolatedTreeForID(tree->treeID()))
        tree->m_replacingTree = existingTree;

    RefPtr document = axObjectCache.document();
    if (!document)
        return nullptr;
    if (!Accessibility::inRenderTreeOrStyleUpdate(*document))
        document->updateLayoutIgnorePendingStylesheets();

    // Generate the nodes of the tree and set its root and focused objects.
    // For this, we need the root and focused objects of the AXObject tree.
    RefPtr axRoot = axObjectCache.getOrCreate(document->view());
    if (axRoot)
        tree->generateSubtree(*axRoot);

    RefPtr axFocus = axObjectCache.focusedObjectForPage(document->page());
    if (axFocus)
        tree->setFocusedNodeID(axFocus->objectID());
    tree->setSelectedTextMarkerRange(document->selection().selection());
    tree->setInitialSortedLiveRegions(axIDs(axObjectCache.sortedLiveRegions()));
    tree->setInitialSortedNonRootWebAreas(axIDs(axObjectCache.sortedNonRootWebAreas()));
    tree->updateLoadingProgress(axObjectCache.loadingProgress());

    auto relations = axObjectCache.relations();
    for (auto& relatedObjectID : relations.keys()) {
        RefPtr axObject = axObjectCache.objectForID(relatedObjectID);
        if (axObject && axObject->isIgnored())
            tree->addUnconnectedNode(axObject.releaseNonNull());
    }
    tree->updateRelations(WTFMove(relations));

    // Now that the tree is ready to take client requests, add it to the tree maps so that it can be found.
    storeTree(axObjectCache, tree);
    return tree;
}

void AXIsolatedTree::applyPendingRootNodeLocked()
{
    ASSERT(!isMainThread());
    ASSERT(m_changeLogLock.isLocked());

    if (m_pendingRootNodeID) {
        if (RefPtr root = objectForID(m_pendingRootNodeID)) {
            m_rootNode = WTFMove(root);
            m_pendingRootNodeID = std::nullopt;

#if ASSERT_ENABLED
            auto markReachableNodes = [](AXCoreObject* object, HashSet<AXID>& reachableNodes, auto& self) -> void {
                reachableNodes.add(object->objectID());
                for (auto& child : object->children())
                    self(&child.get(), reachableNodes, self);
            };
            HashSet<AXID> reachableNodes;
            if (m_rootNode) {
                markReachableNodes(m_rootNode.get(), reachableNodes, markReachableNodes);
                ASSERT_WITH_MESSAGE(reachableNodes.size() == m_readerThreadNodeMap.size(), "AX: After applying pending root node, %u reachable nodes but %u are in the node map", reachableNodes.size(), m_readerThreadNodeMap.size());
            }
#endif
        }
    }
}

void AXIsolatedTree::storeTree(AXObjectCache& cache, const Ref<AXIsolatedTree>& tree)
{
    ASSERT(isMainThread());

    // Once we've added this new tree to the AXTreeStore, clients will be able to use
    // it off the main-thread. Make any final state mutations while we are the only thread
    // that can touch this tree.
    cache.setIsolatedTree(tree);
    AXTreeStore::set(tree->treeID(), tree.ptr());
    tree->m_replacingTree = nullptr;
    Locker locker { s_storeLock };
    treePageCache().set(*cache.pageID(), tree.copyRef());
}

double AXIsolatedTree::loadingProgress()
{
    return .50 * m_loadingProgress + .50 * m_processingProgress;
}

void AXIsolatedTree::reportLoadingProgress(double processingProgress)
{
    AXTRACE("AXIsolatedTree::reportLoadingProgress"_s);
    ASSERT(isMainThread());

    if (!isEmptyContentTree()) {
        ASSERT_NOT_REACHED();
        return;
    }

    m_processingProgress = processingProgress;
    String title = AXProcessingPage(loadingProgress());
    AXLOG(title);

    WeakPtr cache = axObjectCache();
    if (RefPtr axWebArea = cache ? cache->rootWebArea() : nullptr) {
        overrideNodeProperties(axWebArea->objectID(), {
            { AXProperty::TitleAttributeValue, WTFMove(title) },
        });
        if (cache)
            cache->postPlatformNotification(*axWebArea, AXNotification::LayoutComplete);
    }
}

void AXIsolatedTree::removeTreeForPageID(PageIdentifier pageID)
{
    AXTRACE("AXIsolatedTree::removeTreeForPageID"_s);
    ASSERT(isMainThread());

    Locker locker { s_storeLock };
    if (RefPtr tree = treePageCache().take(pageID)) {
        tree->m_geometryManager = nullptr;
        tree->queueForDestruction();
    }
}

RefPtr<AXIsolatedTree> AXIsolatedTree::treeForPageID(PageIdentifier pageID)
{
    Locker locker { s_storeLock };
    if (RefPtr tree = treePageCache().get(pageID))
        return tree;
    return nullptr;
}

void AXIsolatedTree::generateSubtree(AccessibilityObject& axObject)
{
    AXTRACE("AXIsolatedTree::generateSubtree"_s);
    ASSERT(isMainThread());

    if (axObject.isDetached())
        return;

    // We're about to a lot of read-only work, so start the attribute cache.
    AXAttributeCacheEnabler enableCache(axObject.axObjectCache());
    collectNodeChangesForSubtree(axObject);
    queueRemovalsAndUnresolvedChanges();
}

bool AXIsolatedTree::shouldCreateNodeChange(AccessibilityObject& axObject)
{
    // We should never create an isolated object from a detached or ignored object, unless we aren't
    // enforcing ignored in the core accessibility tree.
    return !axObject.isDetached()
        && (axObject.includeIgnoredInCoreTree()
            || !axObject.isIgnored()
            || m_unconnectedNodes.contains(axObject.objectID()));
}

std::optional<AXIsolatedTree::NodeChange> AXIsolatedTree::nodeChangeForObject(Ref<AccessibilityObject> axObject)
{
    ASSERT(isMainThread());
    ASSERT(!axObject->isDetached());

    if (!shouldCreateNodeChange(axObject.get()))
        return std::nullopt;

    ASSERT(axObject->wrapper());
    auto data = createIsolatedObjectData(axObject, *this);
    Markable parentID = data.parentID;
    m_nodeMap.set(axObject->objectID(), ParentChildrenIDs { parentID, data.childrenIDs });
    NodeChange nodeChange { WTFMove(data), axObject->wrapper() };

    if (axObject->isRoot())
        setPendingRootNodeID(axObject->objectID());
    return nodeChange;
}

void AXIsolatedTree::queueChange(const NodeChange& nodeChange)
{
    ASSERT(isMainThread());
    ASSERT(m_changeLogLock.isLocked());

    m_pendingAppends.append(nodeChange);

    Markable parentID = nodeChange.data.parentID;
    if (parentID) {
        auto siblingsIDs = m_nodeMap.get(*parentID).childrenIDs;
        m_pendingChildrenUpdates.append({ *parentID, WTFMove(siblingsIDs) });
    }

    AXID objectID = nodeChange.data.axID;
    ASSERT_WITH_MESSAGE(objectID != parentID, "object ID was the same as its parent ID (%s) when queueing a node change", objectID.loggingString().utf8().data());
    ASSERT_WITH_MESSAGE(m_nodeMap.contains(objectID), "node map should've contained objectID: %s", objectID.loggingString().utf8().data());
    auto childrenIDs = m_nodeMap.get(objectID).childrenIDs;
    m_pendingChildrenUpdates.append({ objectID, WTFMove(childrenIDs) });
}

void AXIsolatedTree::addUnconnectedNode(Ref<AccessibilityObject> axObject)
{
    AXTRACE("AXIsolatedTree::addUnconnectedNode"_s);
    ASSERT(isMainThread());

    auto objectID = axObject->objectID();
    if (m_unconnectedNodes.contains(objectID)) {
        AXLOG(makeString("AXIsolatedTree::addUnconnectedNode exiting because an isolated object for "_s, objectID.loggingString(), " already exists."_s));
        return;
    }

    if (axObject->isDetached() || !axObject->wrapper()) {
        AXLOG(makeString("AXIsolatedTree::addUnconnectedNode bailing because associated live object ID "_s, objectID.loggingString(), " had no wrapper or is detached. Object is:"_s));
        AXLOG(axObject.ptr());
        return;
    }
    AXLOG(makeString("AXIsolatedTree::addUnconnectedNode creating isolated object from live object ID "_s, objectID.loggingString()));

    // Because we are queuing a change for an object not intended to be connected to the rest of the tree,
    // we don't need to update m_nodeMap or m_pendingChildrenUpdates for this object or its parent as is
    // done in AXIsolatedTree::nodeChangeForObject and AXIsolatedTree::queueChange.
    //
    // Instead, just directly create and queue the node change so m_readerThreadNodeMap can hold a reference
    // to it. It will be removed from m_readerThreadNodeMap when the corresponding DOM element, renderer, or
    // other entity is removed from the page.
    NodeChange nodeChange { createIsolatedObjectData(axObject, *this), axObject->wrapper() };
    Locker locker { m_changeLogLock };
    m_pendingAppends.append(WTFMove(nodeChange));
    m_unconnectedNodes.add(objectID);
}

void AXIsolatedTree::queueRemovals(Vector<AXID>&& subtreeRemovals)
{
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    queueRemovalsLocked(WTFMove(subtreeRemovals));
}

void AXIsolatedTree::queueRemovalsLocked(Vector<AXID>&& subtreeRemovals)
{
    ASSERT(isMainThread());
    ASSERT(m_changeLogLock.isLocked());

    m_pendingSubtreeRemovals.addAll(WTFMove(subtreeRemovals));
    m_pendingProtectedFromDeletionIDs.addAll(std::exchange(m_protectedFromDeletionIDs, { }));
}

void AXIsolatedTree::queueRemovalsAndUnresolvedChanges()
{
    ASSERT(isMainThread());

    queueAppendsAndRemovals(resolveAppends(), std::exchange(m_subtreesToRemove, { }));
}

Vector<AXIsolatedTree::NodeChange> AXIsolatedTree::resolveAppends()
{
    ASSERT(isMainThread());

    if (m_unresolvedPendingAppends.isEmpty())
        return { };

    auto* cache = axObjectCache();
    if (!cache)
        return { };

    auto lastFeedbackTime = MonotonicTime::now();
    double counter = 0;
    Vector<NodeChange> resolvedAppends;
    resolvedAppends.reserveInitialCapacity(m_unresolvedPendingAppends.size());
    // The process of resolving appends can add more IDs to m_unresolvedPendingAppends as we iterate over it, so
    // iterate over an exchanged map instead. Any late-appended IDs will get picked up in the next cycle.
    auto unresolvedPendingAppends = std::exchange(m_unresolvedPendingAppends, { });
    for (const auto& axID : unresolvedPendingAppends) {
        if (m_replacingTree) {
            ++counter;
            if (MonotonicTime::now() - lastFeedbackTime > CreationFeedbackInterval) {
                m_replacingTree->reportLoadingProgress(counter / unresolvedPendingAppends.size());
                lastFeedbackTime = MonotonicTime::now();
            }
        }

        if (RefPtr axObject = cache->objectForID(axID)) {
            if (std::optional nodeChange = nodeChangeForObject(*axObject))
                resolvedAppends.append(WTFMove(*nodeChange));
        }
    }
    resolvedAppends.shrinkToFit();

    if (m_replacingTree)
        m_replacingTree->reportLoadingProgress(1);
    return resolvedAppends;
}

void AXIsolatedTree::queueAppendsAndRemovals(Vector<NodeChange>&& appends, Vector<AXID>&& subtreeRemovals)
{
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    for (const auto& append : appends)
        queueChange(append);

    auto parentUpdateIDs = std::exchange(m_needsParentUpdate, { });
    for (const auto& axID : parentUpdateIDs) {
        ASSERT_WITH_MESSAGE(m_nodeMap.contains(axID), "An object marked as needing a parent update should've had an entry in the node map by now. ID was %s", axID.loggingString().utf8().data());
        m_pendingParentUpdates.set(axID, *m_nodeMap.get(axID).parentID);
    }

    queueRemovalsLocked(WTFMove(subtreeRemovals));
}

void AXIsolatedTree::collectNodeChangesForSubtree(AccessibilityObject& axObject)
{
    AXTRACE("AXIsolatedTree::collectNodeChangesForSubtree"_s);
    AXLOG(axObject);
    ASSERT(isMainThread());

    if (axObject.isDetached()) {
        AXLOG("Can't build an isolated tree branch rooted at a detached object.");
        return;
    }

    SetForScope isCollectingNodeChanges(m_isCollectingNodeChanges, true);

    RefPtr axParent = axObject.parentInCoreTree();
    auto parentID = axParent ? std::optional { axParent->objectID() } : std::nullopt;
    auto axChildrenCopy = axObject.children();

    auto iterator = m_nodeMap.find(axObject.objectID());
    if (iterator == m_nodeMap.end()) {
        m_unresolvedPendingAppends.add(axObject.objectID());

        Vector<AXID> axChildrenIDs;
        axChildrenIDs.reserveInitialCapacity(axChildrenCopy.size());
        for (const auto& axChild : axChildrenCopy) {
            if (axChild.ptr() == &axObject) {
                ASSERT_NOT_REACHED();
                continue;
            }

            axChildrenIDs.append(axChild->objectID());
            collectNodeChangesForSubtree(downcast<AccessibilityObject>(axChild.get()));
        }
        axChildrenIDs.shrinkToFit();

        m_nodeMap.set(axObject.objectID(), ParentChildrenIDs { parentID, WTFMove(axChildrenIDs) });
    } else {
        // This object is already in the isolated tree, so there's no need to create full node change for it (doing so is expensive).
        // Protect this object from being deleted. This is important when |axObject| was a child of some other object,
        // but no longer is, and thus the other object will try to queue it for removal. But the fact that we're here
        // indicates this object isn't ready to be removed, just a child of a different parent, so prevent this removal.
        m_protectedFromDeletionIDs.add(axObject.objectID());
        // Update the object's parent if it has changed (but only if we aren't going to create a node change for it,
        // as the act of creating a new node change will correct this as part of creating the new AXIsolatedObject).
        if (parentID && iterator->value.parentID != *parentID && !m_unresolvedPendingAppends.contains(axObject.objectID()))
            m_needsParentUpdate.add(axObject.objectID());

        // Only update the parentID so that we have the right one set for when we process m_needsParentUpdate. We explicitly
        // don't want to update the children IDs in this case, as we need to keep the "old" children around in order for
        // `AXIsolatedTree::updateChildren` to behave correctly.
        iterator->value.parentID = parentID;

        for (const auto& axChild : axChildrenCopy) {
            if (axChild.ptr() == &axObject) {
                ASSERT_NOT_REACHED();
                continue;
            }
            collectNodeChangesForSubtree(downcast<AccessibilityObject>(axChild.get()));
        }
    }
}

void AXIsolatedTree::updateNode(AccessibilityObject& axObject)
{
    AXTRACE("AXIsolatedTree::updateNode"_s);
    AXLOG(&axObject);
    ASSERT(isMainThread());

    if (isUpdatingSubtree())
        return;

    // If we update a node as the result of some side effect while collecting node changes (e.g. a role change from
    // AccessibilityRenderObject::updateRoleAfterChildrenCreation), queue the append up to be resolved with the rest
    // of the collected changes. This prevents us from creating two node changes for the same object.
    if (isCollectingNodeChanges() || !m_unresolvedPendingAppends.isEmpty()) {
        m_unresolvedPendingAppends.add(axObject.objectID());
        return;
    }

    // Otherwise, resolve the change immediately and queue it up.
    // In both cases, we can't attach the wrapper immediately on the main thread, since the wrapper could be in use
    // on the AX thread (because this function updates an existing node).
    if (auto change = nodeChangeForObject(axObject)) {
        Locker locker { m_changeLogLock };
        queueChange(WTFMove(*change));
        return;
    }

    // Not able to update axObject. This may be because it is a descendant of a barren object such as a button. In that case, try to update its parent.
    if (!axObject.isDescendantOfBarrenParent())
        return;

    RefPtr axParent = axObject.parentInCoreTree();
    if (!axParent)
        return;

    if (auto change = nodeChangeForObject(downcast<AccessibilityObject>(*axParent))) {
        Locker locker { m_changeLogLock };
        queueChange(WTFMove(*change));
    }
}

void AXIsolatedTree::objectChangedIgnoredState(const AccessibilityObject& object)
{
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
    ASSERT(isMainThread());

    if (RefPtr cell = dynamicDowncast<AccessibilityTableCell>(object)) {
        if (RefPtr parentTable = cell->parentTable()) {
            // FIXME: This should be as simple as:
            //     queueNodeUpdate(*parentTable->objectID(), { { AXProperty::Cells, AXProperty::CellSlots, AXProperty::Columns } });
            // As these are the table properties that depend on cells. But we can't do that, because we compute "new" column accessibility objects
            // every time we clearChildren() and addChildren(), so just re-computing AXProperty::Columns means that we won't have AXIsolatedObjects
            // for the columns. Instead we have to do a significantly more wasteful children update.
            queueNodeUpdate(parentTable->objectID(), NodeUpdateOptions::childrenUpdate());
            queueNodeUpdate(parentTable->objectID(), { { AXProperty::Cells, AXProperty::CellSlots } });
        }
    }

    if (object.isLink()) {
        if (RefPtr webArea = m_axObjectCache ? m_axObjectCache->rootWebArea() : nullptr)
            queueNodeUpdate(webArea->objectID(), { AXProperty::DocumentLinks });
    }
#else
    UNUSED_PARAM(object);
#endif // ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
}

void AXIsolatedTree::updatePropertiesForSelfAndDescendants(AccessibilityObject& axObject, const AXPropertySet& propertySet)
{
    AXTRACE("AXIsolatedTree::updatePropertiesForSelfAndDescendants"_s);
    ASSERT(isMainThread());

    if (isUpdatingSubtree())
        return;

    Accessibility::enumerateDescendantsIncludingIgnored<AXCoreObject>(axObject, true, [&propertySet, this] (auto& descendant) {
        queueNodeUpdate(descendant.objectID(), { propertySet });
    });
}

void AXIsolatedTree::updateNodeProperties(AccessibilityObject& axObject, const AXPropertySet& propertySet)
{
    AXTRACE("AXIsolatedTree::updateNodeProperties"_s);
    AXLOG(makeString("Updating properties for objectID "_s, axObject.objectID().loggingString(), ": "_s));
    ASSERT(isMainThread());

    if (isUpdatingSubtree())
        return;

    AXPropertyVector properties;
    properties.reserveInitialCapacity(propertySet.size());
    for (const auto& property : propertySet) {
        AXLOG(makeString("Property: "_s, property));
        switch (property) {
        case AXProperty::AccessKey:
            properties.append({ AXProperty::AccessKey, axObject.accessKey().isolatedCopy() });
            break;
        case AXProperty::AccessibilityText: {
            Vector<AccessibilityText> texts;
            axObject.accessibilityText(texts);
            auto axTextValue = texts.map([] (const auto& text) -> AccessibilityText {
                return { text.text.isolatedCopy(), text.textSource };
            });
            properties.append({ AXProperty::AccessibilityText, WTFMove(axTextValue) });
            break;
        }
        case AXProperty::ARIARoleDescription:
            properties.append({ AXProperty::ARIARoleDescription, axObject.ariaRoleDescription().isolatedCopy() });
            break;
        case AXProperty::ARIALevel:
            properties.append({ AXProperty::ARIALevel, axObject.ariaLevel() });
            break;
        case AXProperty::ValueAutofillButtonType:
            properties.append({ AXProperty::ValueAutofillButtonType, static_cast<int>(axObject.valueAutofillButtonType()) });
            properties.append({ AXProperty::IsValueAutofillAvailable, axObject.isValueAutofillAvailable() });
            break;
        case AXProperty::AXColumnCount:
            properties.append({ AXProperty::AXColumnCount, axObject.axColumnCount() });
            break;
        case AXProperty::BrailleLabel:
            properties.append({ AXProperty::BrailleLabel, axObject.brailleLabel().isolatedCopy() });
            break;
        case AXProperty::BrailleRoleDescription:
            properties.append({ AXProperty::BrailleRoleDescription, axObject.brailleRoleDescription().isolatedCopy() });
            break;
        case AXProperty::AXColumnIndex: {
            if (std::optional<unsigned> columnIndex = axObject.axColumnIndex())
                properties.append({ AXProperty::AXColumnIndex, *columnIndex });
            break;
        }
        case AXProperty::AXColumnIndexText:
            properties.append({ AXProperty::AXColumnIndexText, axObject.axColumnIndexText().isolatedCopy() });
            break;
        case AXProperty::CanSetFocusAttribute:
            properties.append({ AXProperty::CanSetFocusAttribute, axObject.canSetFocusAttribute() });
            break;
        case AXProperty::CanSetSelectedAttribute:
            properties.append({ AXProperty::CanSetSelectedAttribute, axObject.canSetSelectedAttribute() });
            break;
        case AXProperty::CanSetValueAttribute:
            properties.append({ AXProperty::CanSetValueAttribute, axObject.canSetValueAttribute() });
            break;
        case AXProperty::Cells:
            properties.append({ AXProperty::Cells, axIDs(axObject.cells()) });
            break;
        case AXProperty::CellSlots:
            properties.append({ AXProperty::CellSlots, axObject.cellSlots() });
            break;
        case AXProperty::ColumnIndex:
            properties.append({ AXProperty::ColumnIndex, axObject.columnIndex() });
            break;
        case AXProperty::ColumnIndexRange:
            properties.append({ AXProperty::ColumnIndexRange, axObject.columnIndexRange() });
            break;
        case AXProperty::CurrentState:
            properties.append({ AXProperty::CurrentState, static_cast<int>(axObject.currentState()) });
            break;
        case AXProperty::DatetimeAttributeValue:
            properties.append({ AXProperty::DatetimeAttributeValue, axObject.datetimeAttributeValue().isolatedCopy() });
            break;
        case AXProperty::DisclosedRows:
            properties.append({ AXProperty::DisclosedRows, axIDs(axObject.disclosedRows()) });
            break;
        case AXProperty::DocumentLinks:
            properties.append({ AXProperty::DocumentLinks, axIDs(axObject.documentLinks()) });
            break;
        case AXProperty::ExplicitOrientation: {
            if (std::optional<AccessibilityOrientation> orientation = axObject.explicitOrientation())
                properties.append({ AXProperty::ExplicitOrientation, *orientation });
            break;
        }
        case AXProperty::ExtendedDescription:
            properties.append({ AXProperty::ExtendedDescription, axObject.extendedDescription().isolatedCopy() });
            break;
        case AXProperty::HasClickHandler:
            properties.append({ AXProperty::HasClickHandler, axObject.hasClickHandler() });
            break;
        case AXProperty::IdentifierAttribute:
            properties.append({ AXProperty::IdentifierAttribute, axObject.identifierAttribute().isolatedCopy() });
            break;
        case AXProperty::InputType:
            if (std::optional inputType = axObject.inputType())
                properties.append({ AXProperty::InputType, *inputType });
            break;
        case AXProperty::InternalLinkElement: {
            auto* linkElement = axObject.internalLinkElement();
            properties.append({ AXProperty::InternalLinkElement, linkElement ? std::optional { linkElement->objectID() } : std::nullopt });
            break;
        }
        case AXProperty::IsChecked:
            ASSERT(axObject.supportsCheckedState());
            properties.append({ AXProperty::IsChecked, axObject.isChecked() });
            properties.append({ AXProperty::ButtonState, axObject.checkboxOrRadioValue() });
            break;
        case AXProperty::IsColumnHeader:
            properties.append({ AXProperty::IsColumnHeader, axObject.isColumnHeader() });
            break;
        case AXProperty::IsEditableWebArea:
            properties.append({ AXProperty::IsEditableWebArea, axObject.isEditableWebArea() });
            break;
        case AXProperty::IsEnabled:
            properties.append({ AXProperty::IsEnabled, axObject.isEnabled() });
            break;
        case AXProperty::IsExpanded:
            properties.append({ AXProperty::IsExpanded, axObject.isExpanded() });
            break;
        case AXProperty::IsIgnored:
            properties.append({ AXProperty::IsIgnored, axObject.isIgnored() });
            break;
        case AXProperty::IsRequired:
            properties.append({ AXProperty::IsRequired, axObject.isRequired() });
            break;
        case AXProperty::IsSelected:
            properties.append({ AXProperty::IsSelected, axObject.isSelected() });
            break;
        case AXProperty::IsRowHeader:
            properties.append({ AXProperty::IsRowHeader, axObject.isRowHeader() });
            break;
        case AXProperty::IsVisible:
            properties.append({ AXProperty::IsVisible, axObject.isVisible() });
            break;
        case AXProperty::IsVisited:
            properties.append({ AXProperty::IsVisited, axObject.isVisited() });
            break;
        case AXProperty::MaxValueForRange:
            properties.append({ AXProperty::MaxValueForRange, axObject.maxValueForRange() });
            break;
        case AXProperty::MinValueForRange:
            properties.append({ AXProperty::MinValueForRange, axObject.minValueForRange() });
            break;
        case AXProperty::NameAttribute:
            properties.append({ AXProperty::NameAttribute, axObject.nameAttribute().isolatedCopy() });
            break;
        case AXProperty::PosInSet:
            properties.append({ AXProperty::PosInSet, axObject.posInSet() });
            break;
        case AXProperty::RemoteFramePlatformElement:
            properties.append({ AXProperty::RemoteFramePlatformElement, axObject.remoteFramePlatformElement() });
            break;
        case AXProperty::StringValue:
            properties.append({ AXProperty::StringValue, axObject.stringValue().isolatedCopy() });
            break;
        case AXProperty::HasRemoteFrameChild:
            properties.append({ AXProperty::HasRemoteFrameChild, axObject.hasRemoteFrameChild() });
            break;
        case AXProperty::RowIndex:
            properties.append({ AXProperty::RowIndex, axObject.rowIndex() });
            break;
        case AXProperty::RowIndexRange:
            properties.append({ AXProperty::RowIndexRange, axObject.rowIndexRange() });
            break;
        case AXProperty::AXRowIndex: {
            if (std::optional<unsigned> rowIndex = axObject.axRowIndex())
                properties.append({ AXProperty::AXRowIndex, *rowIndex });
            break;
        }
        case AXProperty::AXRowIndexText:
            properties.append({ AXProperty::AXRowIndexText, axObject.axRowIndexText().isolatedCopy() });
            break;
        case AXProperty::CellScope:
            properties.append({ AXProperty::CellScope, axObject.cellScope().isolatedCopy() });
            break;
        case AXProperty::ScreenRelativePosition:
            properties.append({ AXProperty::ScreenRelativePosition, axObject.screenRelativePosition() });
            break;
        case AXProperty::SelectedTextRange:
            properties.append({ AXProperty::SelectedTextRange, axObject.selectedTextRange() });
            break;
        case AXProperty::SetSize:
            properties.append({ AXProperty::SetSize, axObject.setSize() });
            break;
        case AXProperty::SortDirection:
            properties.append({ AXProperty::SortDirection, static_cast<int>(axObject.sortDirection()) });
            break;
        case AXProperty::SpeakAs:
            properties.append({ AXProperty::SpeakAs, axObject.speakAs() });
            break;
        case AXProperty::KeyShortcuts:
            properties.append({ AXProperty::SupportsKeyShortcuts, axObject.supportsKeyShortcuts() });
            properties.append({ AXProperty::KeyShortcuts, axObject.keyShortcuts().isolatedCopy() });
            break;
        case AXProperty::SupportsARIAOwns:
            properties.append({ AXProperty::SupportsARIAOwns, axObject.supportsARIAOwns() });
            break;
        case AXProperty::SupportsExpanded:
            properties.append({ AXProperty::SupportsExpanded, axObject.supportsExpanded() });
            break;
        case AXProperty::SupportsDragging:
            properties.append({ AXProperty::SupportsDragging, axObject.supportsDragging() });
            break;
        case AXProperty::SupportsPosInSet:
            properties.append({ AXProperty::SupportsPosInSet, axObject.supportsPosInSet() });
            break;
        case AXProperty::SupportsSetSize:
            properties.append({ AXProperty::SupportsSetSize, axObject.supportsSetSize() });
            break;
        case AXProperty::TextInputMarkedTextMarkerRange: {
            AXIDAndCharacterRange value;
            auto range = axObject.textInputMarkedTextMarkerRange();
            if (auto characterRange = range.characterRange(); range && characterRange)
                value = { range.start().objectID(), *characterRange };
            properties.append({ AXProperty::TextInputMarkedTextMarkerRange, std::make_shared<AXIDAndCharacterRange>(value) });
            break;
        }
#if ENABLE(AX_THREAD_TEXT_APIS)
        case AXProperty::BackgroundColor:
            properties.append({ AXProperty::BackgroundColor, axObject.backgroundColor() });
            break;
        case AXProperty::Font: {
            if (RefPtr parent = axObject.parentObject()) {
                RetainPtr font = axObject.font();
                if (font != parent->font()) {
                    properties.append({ AXProperty::Font, WTFMove(font) });
                    break;
                }
            }
            properties.append({ AXProperty::Font, nullptr });
            break;
        }
        case AXProperty::HasLinethrough:
            properties.append({ AXProperty::HasLinethrough, axObject.lineDecorationStyle().hasLinethrough });
            break;
        case AXProperty::HasTextShadow:
            properties.append({ AXProperty::HasTextShadow, axObject.hasTextShadow() });
            break;
        case AXProperty::IsSubscript:
            properties.append({ AXProperty::IsSubscript, axObject.isSubscript() });
            break;
        case AXProperty::IsSuperscript:
            properties.append({ AXProperty::IsSuperscript, axObject.isSuperscript() });
            break;
        case AXProperty::LinethroughColor:
            properties.append({ AXProperty::LinethroughColor, axObject.lineDecorationStyle().linethroughColor });
            break;
        case AXProperty::TextColor: {
            if (RefPtr parent = axObject.parentObject()) {
                auto color = axObject.textColor();
                if (color != parent->textColor()) {
                    properties.append({ AXProperty::TextColor, color });
                    break;
                }
            }
            // Setting text color to nullptr will remove it from the property map, and allow it to be inherited from an ancestor.
            properties.append({ AXProperty::TextColor, nullptr });
            break;
        }
        case AXProperty::TextRuns:
            properties.append({ AXProperty::TextRuns, std::make_shared<AXTextRuns>(axObject.textRuns()) });
            break;
        case AXProperty::UnderlineColor: {
            if (axObject.hasUnderline())
                properties.append({ AXProperty::UnderlineColor, axObject.lineDecorationStyle().underlineColor });
            else {
                // Queue the default color to remove it from the property map.
                properties.append({ AXProperty::UnderlineColor, Accessibility::defaultColor() });
            }
            break;
        }
#endif // ENABLE(AX_THREAD_TEXT_APIS)
        case AXProperty::Title:
            properties.append({ AXProperty::Title, axObject.title().isolatedCopy() });
            break;
        case AXProperty::URL:
            properties.append({ AXProperty::URL, std::make_shared<URL>(axObject.url().isolatedCopy()) });
            break;
        case AXProperty::ValueForRange:
            properties.append({ AXProperty::ValueForRange, axObject.valueForRange() });
            break;
        default:
            break;
        }
    }

    if (properties.isEmpty())
        return;

    Locker locker { m_changeLogLock };
    m_pendingPropertyChanges.append({ axObject.objectID(), WTFMove(properties) });
}

void AXIsolatedTree::overrideNodeProperties(AXID axID, AXPropertyVector&& properties)
{
    ASSERT(isMainThread());

    if (properties.isEmpty())
        return;

    Locker locker { m_changeLogLock };
    m_pendingPropertyChanges.append({ axID, WTFMove(properties) });
}

void AXIsolatedTree::updateDependentProperties(AccessibilityObject& axObject)
{
    ASSERT(isMainThread());

    auto updateRelatedObjects = [this] (const AccessibilityObject& object) {
        for (const auto& labeledObject : object.labelForObjects())
            queueNodeUpdate(labeledObject->objectID(), NodeUpdateOptions::nodeUpdate());

        for (const auto& describedByObject : object.descriptionForObjects())
            queueNodeUpdate(describedByObject->objectID(), { { AXProperty::AccessibilityText, AXProperty::ExtendedDescription } });
    };
    updateRelatedObjects(axObject);

    // When a row gains or loses cells, or a table changes rows in a row group, the column count of the table can change.
    bool updateTableAncestorColumns = is<AccessibilityTableRow>(axObject);
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
    updateTableAncestorColumns = updateTableAncestorColumns || isRowGroup(axObject.node());
#endif
    for (RefPtr ancestor = axObject.parentObject(); ancestor; ancestor = ancestor->parentObject()) {
        if (updateTableAncestorColumns && is<AccessibilityTable>(*ancestor)) {
            // Only `updateChildren` if the table is unignored, because otherwise `updateChildren` will ascend and update the next highest unignored ancestor, which doesn't accomplish our goal of updating table columns.
            if (ancestor->isIgnored())
                break;
            // Use `NodeUpdateOptions::childrenUpdate()` rather than `updateNodeProperty` because `childrenUpdate()` will ensure the columns (which are children) will have associated isolated objects created.
            queueNodeUpdate(ancestor->objectID(), NodeUpdateOptions::childrenUpdate());
            break;
        }

        updateRelatedObjects(*ancestor);
    }
}

void AXIsolatedTree::updateChildren(AccessibilityObject& axObject, ResolveNodeChanges resolveNodeChanges)
{
    AXTRACE("AXIsolatedTree::updateChildren"_s);
    AXLOG("For AXObject:");
    AXLOG(&axObject);
    ASSERT(isMainThread());

    if (isUpdatingSubtree())
        return;

    if (m_nodeMap.isEmpty()) {
        ASSERT_NOT_REACHED();
        return;
    }

    if (!axObject.document() || !axObject.document()->hasLivingRenderTree())
        return;

    // We're about to do a lot of work, so start the attribute cache.
    AXAttributeCacheEnabler enableCache(axObject.axObjectCache());

    // updateChildren may be called as the result of a children changed
    // notification for an axObject that has no associated isolated object.
    // An example of this is when an empty element such as a <canvas> or <div>
    // has added a new child. So find the closest ancestor of axObject that has
    // an associated isolated object and update its children.
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
    auto* axAncestor = &axObject;
#else
    auto* axAncestor = Accessibility::findAncestor(axObject, true, [this] (auto& ancestor) {
        return m_nodeMap.contains(ancestor.objectID());
    });
#endif // ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)

    if (!axAncestor || axAncestor->isDetached()) {
        // This update was triggered before the isolated tree has been repopulated.
        // Return here since there is nothing to update.
        AXLOG("Bailing because no ancestor could be found, or ancestor is detached");
        return;
    }

#if !ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
    if (axAncestor != &axObject) {
        AXLOG(makeString("Original object with ID "_s, axObject.objectID().loggingString(), " wasn't in the isolated tree, so instead updating the closest in-isolated-tree ancestor:"_s));
        AXLOG(axAncestor);

        // An explicit copy is necessary here because the nested calls to updateChildren
        // can cause this objects children to be invalidated as we iterate.
        auto childrenCopy = axObject.children();
        for (auto& child : childrenCopy) {
            Ref liveChild = downcast<AccessibilityObject>(child.get());
            if (liveChild->childrenInitialized())
                continue;

            if (!m_nodeMap.contains(liveChild->objectID())) {
                if (!shouldCreateNodeChange(liveChild))
                    continue;

                // This child should be added to the isolated tree but hasn't been yet.
                // Add it to the nodemap so the recursive call to updateChildren below properly builds the subtree for this object.
                RefPtr parent = axObject.parentInCoreTree();
                m_nodeMap.set(liveChild->objectID(), ParentChildrenIDs { parent ? std::optional { parent->objectID() } : std::nullopt, liveChild->childrenIDs() });
                m_unresolvedPendingAppends.add(liveChild->objectID());
            }

            AXLOG(makeString(
                "Child ID "_s, liveChild->objectID().loggingString(), " of original object ID "_s, axObject.objectID().loggingString(), " was found in the isolated tree with uninitialized live children. Updating its isolated children."_s
            ));
            // Don't immediately resolve node changes in these recursive calls to updateChildren. This avoids duplicate node change creation in this scenario:
            //   1. Some subtree is updated in the below call to updateChildren.
            //   2. Later in this function, when updating axAncestor, we update some higher subtree that includes the updated subtree from step 1.
            queueNodeUpdate(liveChild->objectID(), NodeUpdateOptions::childrenUpdate());
        }
    }
#endif // !ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)

    // FIXME: This copy out of the hashmap seems unnecessary — can we use HashMap::find instead?
    auto oldIDs = m_nodeMap.get(axAncestor->objectID());
    auto& oldChildrenIDs = oldIDs.childrenIDs;

    const auto& newChildren = axAncestor->children();
    auto newChildrenIDs = axIDs(newChildren);

    bool childrenChanged = oldChildrenIDs.size() != newChildrenIDs.size();
    for (size_t i = 0; i < newChildren.size(); ++i) {
        ASSERT(newChildren[i]->objectID() == newChildrenIDs[i]);
        size_t index = oldChildrenIDs.find(newChildrenIDs[i]);
        if (index != notFound) {
            // Prevent deletion of this object below by removing it from oldChildrenIDs.
            oldChildrenIDs.removeAt(index);

            // Propagate any subtree updates downwards for this already-existing child.
            if (RefPtr liveChild = dynamicDowncast<AccessibilityObject>(newChildren[i].get()); liveChild && liveChild->hasDirtySubtree())
                queueNodeUpdate(liveChild->objectID(), NodeUpdateOptions::childrenUpdate());
        } else {
            // This is a new child, add it to the tree.
            childrenChanged = true;
            AXLOG(makeString("AXID "_s, axAncestor->objectID().loggingString(), " gaining new subtree, starting at ID "_s, newChildren[i]->objectID().loggingString(), ':'));
            AXLOG(newChildren[i]);
            collectNodeChangesForSubtree(downcast<AccessibilityObject>(newChildren[i].get()));
        }
    }
    m_nodeMap.set(axAncestor->objectID(), ParentChildrenIDs { oldIDs.parentID, WTFMove(newChildrenIDs) });
    // Since axAncestor is definitively part of the AX tree by way of getting here, protect it from being
    // deleted in case it has been re-parented.
    m_protectedFromDeletionIDs.add(axAncestor->objectID());

    // What is left in oldChildrenIDs are the IDs that are no longer children of axAncestor.
    // Thus, remove them from m_nodeMap and queue them to be removed from the tree.
    for (auto& axID : oldChildrenIDs)
        removeSubtreeFromNodeMap(axID, axAncestor->objectID());

    auto unconditionallyUpdate = [] (AccessibilityRole role) {
        // These are the roles that should be updated even if AX children don't change. This is necessary because
        // these roles are not allowed to have children according to accessibility semantics, but can have render
        // tree or DOM children, changes of which affect many properties (e.g. anything downstream of textUnderElement).
        // Note this is a subset of the roles in AccessibilityObject::canHaveChildren, deliberately only those that
        // could reasonably have meaningful-to-accessibility DOM / render tree children.
        switch (role) {
        case AccessibilityRole::Button:
        case AccessibilityRole::PopUpButton:
        case AccessibilityRole::Tab:
        case AccessibilityRole::ToggleButton:
        case AccessibilityRole::ListBoxOption:
        case AccessibilityRole::ProgressIndicator:
        case AccessibilityRole::Switch:
        case AccessibilityRole::MenuItemCheckbox:
        case AccessibilityRole::MenuItemRadio:
        case AccessibilityRole::Meter:
            return true;
        default:
            return false;
        }
    };

    // Also queue updates to the target node itself and any properties that depend on children().
    if (childrenChanged || unconditionallyUpdate(axAncestor->role())) {
        queueNodeUpdate(axAncestor->objectID(), NodeUpdateOptions::nodeUpdate());
        updateDependentProperties(*axAncestor);
    }

    m_subtreesToRemove.appendVector(WTFMove(oldChildrenIDs));
    if (resolveNodeChanges == ResolveNodeChanges::Yes)
        queueRemovalsAndUnresolvedChanges();
}

void AXIsolatedTree::updateChildrenForObjects(const ListHashSet<Ref<AccessibilityObject>>& axObjects)
{
    AXTRACE("AXIsolatedTree::updateChildrenForObjects"_s);

    if (isUpdatingSubtree())
        return;

    AXAttributeCacheEnabler enableCache(axObjectCache());
    for (auto& axObject : axObjects)
        queueNodeUpdate(axObject->objectID(), NodeUpdateOptions::childrenUpdate());

    queueRemovalsAndUnresolvedChanges();
}

void AXIsolatedTree::setPageActivityState(OptionSet<ActivityState> state)
{
    ASSERT(isMainThread());

    Locker locker { s_storeLock };
    m_pageActivityState = state;
}

OptionSet<ActivityState> AXIsolatedTree::pageActivityState() const
{
    Locker locker { s_storeLock };
    return m_pageActivityState;
}

OptionSet<ActivityState> AXIsolatedTree::lockedPageActivityState() const
{
    ASSERT(s_storeLock.isLocked());
    return m_pageActivityState;
}

AXCoreObject::AccessibilityChildrenVector AXIsolatedTree::sortedLiveRegions()
{
    ASSERT(!isMainThread());
    return objectsForIDs(m_sortedLiveRegionIDs);
}

AXCoreObject::AccessibilityChildrenVector AXIsolatedTree::sortedNonRootWebAreas()
{
    ASSERT(!isMainThread());
    return objectsForIDs(m_sortedNonRootWebAreaIDs);
}

void AXIsolatedTree::setInitialSortedLiveRegions(Vector<AXID> liveRegionIDs)
{
    ASSERT(isMainThread());

    m_sortedLiveRegionIDs = WTFMove(liveRegionIDs);
}

void AXIsolatedTree::setInitialSortedNonRootWebAreas(Vector<AXID> webAreaIDs)
{
    ASSERT(isMainThread());

    m_sortedNonRootWebAreaIDs = WTFMove(webAreaIDs);
}

std::optional<AXID> AXIsolatedTree::focusedNodeID()
{
    ASSERT(!isMainThread());
    // applyPendingChanges can destroy `this` tree, so protect it until the end of this method.
    Ref protectedThis { *this };
    // Apply pending changes in case focus has changed and hasn't been updated.
    applyPendingChanges();
    return m_focusedNodeID;
}

RefPtr<AXIsolatedObject> AXIsolatedTree::focusedNode()
{
    AXTRACE("AXIsolatedTree::focusedNode"_s);
    ASSERT(!isMainThread());
    AXLOG("focused node:");
    AXLOG(objectForID(focusedNodeID()));
    return objectForID(focusedNodeID());
}

RefPtr<AXIsolatedObject> AXIsolatedTree::rootWebArea()
{
    AXTRACE("AXIsolatedTree::rootWebArea"_s);
    ASSERT(!isMainThread());

    RefPtr root = rootNode();
    return root ? Accessibility::findUnignoredChild(*root, [] (auto& object) {
        return object->isWebArea();
    }) : nullptr;
}

void AXIsolatedTree::setPendingRootNodeID(AXID axID)
{
    Locker locker { m_changeLogLock };
    setPendingRootNodeIDLocked(axID);
}

void AXIsolatedTree::setPendingRootNodeIDLocked(AXID axID)
{
    AXTRACE("AXIsolatedTree::setPendingRootNodeIDLocked"_s);
    ASSERT(isMainThread());
    ASSERT(m_changeLogLock.isLocked());

    m_pendingRootNodeID = axID;
}

void AXIsolatedTree::setFocusedNodeID(std::optional<AXID> axID)
{
    AXTRACE("AXIsolatedTree::setFocusedNodeID"_s);
    AXLOG(makeString("axID "_s, axID ? axID->loggingString() : ""_str));
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    m_pendingFocusedNodeID = axID;
}

void AXIsolatedTree::updateRelations(HashMap<AXID, AXRelations>&& relations)
{
    AXTRACE("AXIsolatedTree::updateRelations"_s);
    ASSERT(isMainThread());

    m_relationsNeedUpdate = false;
    Locker locker { m_changeLogLock };
    m_pendingRelations = WTFMove(relations);
}

void AXIsolatedTree::setSelectedTextMarkerRange(AXTextMarkerRange&& range)
{
    AXTRACE("AXIsolatedTree::setSelectedTextMarkerRange"_s);
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    m_pendingSelectedTextMarkerRange = WTFMove(range);
}

void AXIsolatedTree::updateLoadingProgress(double newProgressValue)
{
    AXTRACE("AXIsolatedTree::updateLoadingProgress"_s);
    AXLOG(makeString("Updating loading progress to "_s, newProgressValue, " for treeID "_s, treeID().loggingString()));
    ASSERT(isMainThread());

    m_loadingProgress = newProgressValue;
}

void AXIsolatedTree::updateFrame(AXID axID, IntRect&& newFrame)
{
    AXTRACE("AXIsolatedTree::updateFrame"_s);
    ASSERT(isMainThread());

    if (isUpdatingSubtree())
        return;

    AXPropertyVector properties;
    properties.append({ AXProperty::RelativeFrame, WTFMove(newFrame) });
    // We can clear the initially-cached rough frame, since the object's frame has been cached
    properties.append({ AXProperty::InitialFrameRect, FloatRect() });
    Locker locker { m_changeLogLock };
    m_pendingPropertyChanges.append({ axID, WTFMove(properties) });
}

void AXIsolatedTree::updateRootScreenRelativePosition()
{
    AXTRACE("AXIsolatedTree::updateRootScreenRelativePosition"_s);
    ASSERT(isMainThread());

    CheckedPtr cache = m_axObjectCache.get();
    if (RefPtr axRoot = cache && cache->document() ? cache->getOrCreate(cache->document()->view()) : nullptr)
        queueNodeUpdate(axRoot->objectID(), { AXProperty::ScreenRelativePosition });
}

void AXIsolatedTree::removeNode(AXID axID, std::optional<AXID> parentID)
{
    AXTRACE("AXIsolatedTree::removeNode"_s);
    AXLOG(makeString("objectID "_s, axID.loggingString()));
    ASSERT(isMainThread());

    m_unresolvedPendingAppends.remove(axID);
    removeSubtreeFromNodeMap(axID, parentID);
    queueRemovals({ axID });
}

void AXIsolatedTree::removeSubtreeFromNodeMap(std::optional<AXID> objectID, std::optional<AXID> axParentID)
{
    AXTRACE("AXIsolatedTree::removeSubtreeFromNodeMap"_s);
    AXLOG(makeString("Removing subtree for objectID "_s,  objectID ? objectID->loggingString() : ""_str));
    ASSERT(isMainThread());

    if (!objectID)
        return;

    if (m_unconnectedNodes.remove(*objectID))
        return;

    if (!m_nodeMap.contains(*objectID)) {
        AXLOG(makeString("Tried to remove AXID "_s, objectID->loggingString(), " that is no longer in m_nodeMap."_s));
        return;
    }

    Markable<AXID> actualParentID = m_nodeMap.get(*objectID).parentID;
    // If the axParentID and actualParentID differ in whether they are null, or if the values differ, break early.
    // If both are null, we are likely at the parent.
    if (static_cast<bool>(axParentID) != static_cast<bool>(actualParentID) || axParentID != actualParentID) {
        AXLOG(makeString("Tried to remove object ID "_s, objectID->loggingString(), " from a different parent "_s, axParentID ? axParentID->loggingString() : ""_str, ", actual parent "_s, actualParentID ? actualParentID->loggingString() : ""_str, ", bailing out."_s));
        return;
    }

    Vector<std::optional<AXID>> removals = { *objectID };
    while (removals.size()) {
        auto axID = removals.takeLast();
        if (!axID || m_unresolvedPendingAppends.contains(*axID) || m_protectedFromDeletionIDs.contains(*axID))
            continue;

        auto it = m_nodeMap.find(*axID);
        if (it != m_nodeMap.end()) {
            removals.appendVector(it->value.childrenIDs);
            m_nodeMap.remove(*axID);
        }
    }
}

std::optional<ListHashSet<AXID>> AXIsolatedTree::relatedObjectIDsFor(const AXIsolatedObject& object, AXRelation relation)
{
    ASSERT(!isMainThread());

    auto relationsIterator = m_relations.find(object.objectID());
    if (relationsIterator == m_relations.end())
        return std::nullopt;

    auto targetsIterator = relationsIterator->value.find(static_cast<uint8_t>(relation));
    if (targetsIterator == relationsIterator->value.end())
        return std::nullopt;
    return targetsIterator->value;
}

void AXIsolatedTree::applyPendingChanges()
{
    Locker locker { m_changeLogLock };
    applyPendingChangesLocked();
}

void AXIsolatedTree::applyPendingChangesUnlessQueuedForDestruction()
{
    ASSERT(!isMainThread());

    Locker locker { m_changeLogLock };

    if (m_queuedForDestruction)
        return;
    applyPendingChangesLocked();
}

void AXIsolatedTree::applyPendingChangesLocked()
{
    AXTRACE("AXIsolatedTree::applyPendingChanges"_s);
    ASSERT(!isMainThread());
    ASSERT(m_changeLogLock.isLocked());

    if (m_queuedForDestruction) [[unlikely]] {
        for (const auto& object : m_readerThreadNodeMap.values())
            object->detach(AccessibilityDetachmentType::CacheDestroyed);

        // Because each AXIsolatedObject holds a RefPtr to this tree, clear out any member variable
        // that holds an AXIsolatedObject so the ref-cycle is broken and this tree can be destroyed.
        m_readerThreadNodeMap.clear();
        m_rootNode = nullptr;
        m_pendingAppends.clear();
        // We don't need to bother clearing out any other non-cycle-causing member variables as they
        // will be cleaned up automatically when the tree is destroyed.

        ASSERT(AXTreeStore::contains(treeID()));
        AXTreeStore::remove(treeID());
        return;
    }

    if (m_pendingFocusedNodeID != m_focusedNodeID) {
        AXLOG(makeString("focusedNodeID "_s, m_focusedNodeID ? m_focusedNodeID->loggingString() : ""_str, " pendingFocusedNodeID "_s, m_pendingFocusedNodeID ? m_pendingFocusedNodeID->loggingString() : ""_str));
        m_focusedNodeID = m_pendingFocusedNodeID;
    }

    while (m_pendingSubtreeRemovals.size()) {
        // WTF_IGNORES_THREAD_SAFETY_ANALYSIS because we _do_ hold the m_changeLogLock, but the thread-safety
        // analysis throws a false-positive compile error when we access m_pendingProtectedFromDeletionIDs in
        // this lambda.
        std::function<void(Ref<AXCoreObject>&&)> deleteSubtree = [&] (Ref<AXCoreObject>&& coreObjectToDelete) WTF_IGNORES_THREAD_SAFETY_ANALYSIS {

            auto& objectToDelete = downcast<AXIsolatedObject>(coreObjectToDelete.get());
            while (objectToDelete.m_children.size()) {
                Ref child = objectToDelete.m_children.takeLast();
                if (!m_pendingProtectedFromDeletionIDs.contains(child->objectID()))
                    deleteSubtree(WTFMove(child));
            }

            // There's no need to call the more comprehensive AXCoreObject::detach here since
            // we're deleting the entire subtree of this object and thus don't need to `detachRemoteParts`.
            objectToDelete.detachWrapper(AccessibilityDetachmentType::ElementDestroyed);

            auto deleteAXID = objectToDelete.objectID();
            m_readerThreadNodeMap.remove(deleteAXID);
            m_pendingSubtreeRemovals.remove(deleteAXID);

            for (const AXID& childID : objectToDelete.m_unresolvedChildrenIDs) {
                // Ideally, assuming m_children has been initialized, there would be no unresolved children IDs.
                // But sometimes when initializing m_children, AXIsolatedTree::objectForID fails for an unknown
                // reason, and thus we are left with an entry in m_unresolvedChildrenIDs. See the ASSERT in
                // AXIsolatedObject::children. In case any of our unresolved IDs got populated with an object
                // later somehow, try to clean them up.
                if (!m_pendingProtectedFromDeletionIDs.contains(childID)) {
                    if (RefPtr child = m_readerThreadNodeMap.take(childID))
                        deleteSubtree(child.releaseNonNull());
                }
            }
        };

        // This dereference is safe because we checked m_pendingSubtreeRemovals.size() to get here.
        auto axID = *m_pendingSubtreeRemovals.takeAny();
        if (m_pendingProtectedFromDeletionIDs.contains(axID))
            continue;

        if (RefPtr object = m_readerThreadNodeMap.take(axID))
            deleteSubtree(object.releaseNonNull());
    }
    m_pendingProtectedFromDeletionIDs.clear();

    for (auto& item : m_pendingAppends) {
        auto axID = item.data.axID;
        AXLOG(makeString("appending axID "_s, axID.loggingString()));

        auto& wrapper = item.wrapper;
        if (!wrapper)
            continue;
        Ref newObject = AXIsolatedObject::create(WTFMove(item.data));

        if (RefPtr existingObject = m_readerThreadNodeMap.take(axID)) {
            if (existingObject->wrapper() == wrapper.get()) {
                // The new IsolatedObject is a replacement for an existing object
                // as the result of an update. Thus detach the existing object
                // and attach the wrapper to the new one.
                existingObject->detach(AccessibilityDetachmentType::ElementChanged);
                newObject->attachPlatformWrapper(wrapper.get());
            }
        }

        // If the new object hasn't been attached to a wrapper yet, or if it was detached from
        // the wrapper when processing removals above, we must attach / re-attach it.
        if (newObject->isDetached())
            newObject->attachPlatformWrapper(wrapper.get());

        auto addResult = m_readerThreadNodeMap.add(axID, WTFMove(newObject));
        // The newly added object must have a wrapper.
        ASSERT_UNUSED(addResult, addResult.iterator->value->wrapper());
        // The reference count of the just added IsolatedObject must be 2
        // because it is referenced by m_readerThreadNodeMap and m_pendingAppends.
        // When m_pendingAppends is cleared, the object will be held only by m_readerThreadNodeMap. The exception is the root node whose reference count is 3.
    }
    m_pendingAppends.clear();

    for (const auto& parentUpdate : m_pendingParentUpdates) {
        if (RefPtr object = objectForID(parentUpdate.key))
            object->setParent(parentUpdate.value);
    }
    m_pendingParentUpdates.clear();

    for (auto& update : m_pendingChildrenUpdates) {
        AXLOG(makeString("updating children for axID "_s, update.first.loggingString()));
        if (RefPtr object = objectForID(update.first))
            object->setChildrenIDs(WTFMove(update.second));
    }
    m_pendingChildrenUpdates.clear();

    for (auto& change : m_pendingPropertyChanges) {
        if (RefPtr object = objectForID(change.axID)) {
            for (auto& property : change.properties)
                object->setProperty(property.first, WTFMove(property.second));
            object->shrinkPropertiesAfterUpdates();
        }
    }
    m_pendingPropertyChanges.clear();

    if (m_pendingSortedLiveRegionIDs)
        m_sortedLiveRegionIDs = std::exchange(m_pendingSortedLiveRegionIDs, std::nullopt).value();

    if (m_pendingSortedNonRootWebAreaIDs)
        m_sortedNonRootWebAreaIDs = std::exchange(m_pendingSortedNonRootWebAreaIDs, std::nullopt).value();

    if (m_pendingMostRecentlyPaintedText)
        m_mostRecentlyPaintedText = std::exchange(m_pendingMostRecentlyPaintedText, std::nullopt).value();

    if (m_pendingRelations)
        m_relations = std::exchange(m_pendingRelations, std::nullopt).value();

    if (m_pendingSelectedTextMarkerRange)
        m_selectedTextMarkerRange = std::exchange(m_pendingSelectedTextMarkerRange, std::nullopt).value();

    // Do this at the end because it requires looking up the root node by ID, so doing it at the end
    // ensures all additions to m_readerThreadNodeMap have been made by now.
    applyPendingRootNodeLocked();
}

void AXIsolatedTree::sortedLiveRegionsDidChange(Vector<AXID> liveRegionIDs)
{
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    m_pendingSortedLiveRegionIDs = WTFMove(liveRegionIDs);
}

void AXIsolatedTree::sortedNonRootWebAreasDidChange(Vector<AXID> webAreaIDs)
{
    ASSERT(isMainThread());

    Locker locker { m_changeLogLock };
    // FIXME: m_pendingSortedLiveRegionIDs and m_pendingSortedNonRootWebAreaIDs should be synced in AXIsolatedTree::processQueuedNodeUpdates(),
    // not ad-hoc whenever the main-thread changes them. Otherwise we could sync IDs to the accessibility thread that don't have isolated objects
    // until the next actual tree-update cycle.
    m_pendingSortedNonRootWebAreaIDs = WTFMove(webAreaIDs);
}

AXTreePtr findAXTree(Function<bool(AXTreePtr)>&& match)
{
    if (isMainThread()) {
        for (WeakPtr tree : AXTreeStore<AXObjectCache>::liveTreeMap().values()) {
            if (!tree)
                continue;

            if (match(tree))
                return tree;
        }
        return nullptr;
    }

#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
    Locker locker { AXTreeStore<AXIsolatedTree>::s_storeLock };
    for (auto it = AXTreeStore<AXIsolatedTree>::isolatedTreeMap().begin(); it != AXTreeStore<AXIsolatedTree>::isolatedTreeMap().end(); ++it) {
        RefPtr tree = it->value.get();
        if (!tree)
            continue;

        if (match(tree))
            return tree;
    }
    return nullptr;
#endif
}

void AXIsolatedTree::queueNodeUpdate(AXID objectID, const NodeUpdateOptions& options)
{
    ASSERT(isMainThread());

    if (!options.shouldUpdateNode && options.properties.size()) {
        // If we're going to recompute all properties for the node (i.e., the node is in m_needsUpdateNode),
        // don't bother queueing any individual property updates.
        if (m_needsUpdateNode.contains(objectID))
            return;

        auto addResult = m_needsPropertyUpdates.add(objectID, options.properties);
        if (!addResult.isNewEntry)
            addResult.iterator->value.addAll(options.properties);
    }

    if (options.shouldUpdateChildren)
        m_needsUpdateChildren.add(objectID);

    if (options.shouldUpdateNode)
        m_needsUpdateNode.add(objectID);

    if (auto* cache = axObjectCache())
        cache->startUpdateTreeSnapshotTimer();
}

void AXIsolatedTree::queueNodeRemoval(const AccessibilityObject& axObject)
{
    ASSERT(isMainThread());

    std::optional labeledObjectIDs = axObjectCache() ? axObjectCache()->relatedObjectIDsFor(axObject, AXRelation::LabelFor, AXObjectCache::UpdateRelations::No) : std::nullopt;
    if (labeledObjectIDs) {
        // Update the labeled objects since axObject is one of their labels and it is being removed.
        for (AXID labeledObjectID : *labeledObjectIDs) {
            // The label/title of an isolated object is computed based on its AccessibilityText property, thus update it.
            queueNodeUpdate(labeledObjectID, { AXProperty::AccessibilityText });
        }
    }

    RefPtr parent = axObject.parentInCoreTree();
    std::optional<AXID> parentID = parent ? std::optional { parent->objectID() } : std::nullopt;

    m_needsNodeRemoval.add(axObject.objectID(), parentID);
    if (auto* cache = axObjectCache())
        cache->startUpdateTreeSnapshotTimer();
}

void AXIsolatedTree::processQueuedNodeUpdates()
{
    ASSERT(isMainThread());

    WeakPtr cache = axObjectCache();
    if (!cache)
        return;

    for (const auto& nodeIDs : m_needsNodeRemoval)
        removeNode(nodeIDs.key, nodeIDs.value);
    m_needsNodeRemoval.clear();

    for (AXID nodeID : m_needsUpdateChildren) {
        if (!cache)
            break;

        if (RefPtr axObject = cache->objectForID(nodeID))
            updateChildren(*axObject, ResolveNodeChanges::No);
    }
    m_needsUpdateChildren.clear();

    for (AXID objectID : m_needsUpdateNode)
        m_unresolvedPendingAppends.add(objectID);
    m_needsUpdateNode.clear();

    for (const auto& propertyUpdate : m_needsPropertyUpdates) {
        if (m_unresolvedPendingAppends.contains(propertyUpdate.key))
            continue;

        if (!cache)
            break;

        if (RefPtr axObject = cache->objectForID(propertyUpdate.key))
            updateNodeProperties(*axObject, propertyUpdate.value);
    }
    m_needsPropertyUpdates.clear();

    if (m_relationsNeedUpdate)
        updateRelations(cache->relations());

    if (m_mostRecentlyPaintedTextIsDirty) {
        Locker lock { m_changeLogLock };
        m_pendingMostRecentlyPaintedText = cache->mostRecentlyPaintedText();
        m_mostRecentlyPaintedTextIsDirty = false;
    }

    queueRemovalsAndUnresolvedChanges();
}

#if ENABLE(AX_THREAD_TEXT_APIS)
AXTextMarker AXIsolatedTree::firstMarker()
{
    ASSERT(!isMainThread());
    // The first marker should be constructed from the root WebArea, not the true root of the tree
    // which is the ScrollView, so that when we convert the marker to a CharacterPosition, there
    // is an associated node. Otherwise, the CharacterPosition will be null.
    RefPtr webArea = rootWebArea();
    return webArea ? AXTextMarker { *webArea, 0 } : AXTextMarker();
}

AXTextMarker AXIsolatedTree::lastMarker()
{
    RefPtr root = rootNode();
    if (!root)
        return { };

    const auto& children = root->unignoredChildren();
    // Start the `findLast` traversal from the last child of the root to reduce the amount of traversal done.
    RefPtr endObject = children.isEmpty() ? root : dynamicDowncast<AXIsolatedObject>(children[children.size() - 1].get());
    return endObject ? AXTextMarker { *endObject, 0 }.findLast() : AXTextMarker();
}
#endif // ENABLE(AX_THREAD_TEXT_APIS)

std::optional<AXPropertyFlag> convertToPropertyFlag(AXProperty property)
{
    switch (property) {
    case AXProperty::CanSetFocusAttribute:
        return AXPropertyFlag::CanSetFocusAttribute;
    case AXProperty::CanSetSelectedAttribute:
        return AXPropertyFlag::CanSetSelectedAttribute;
    case AXProperty::CanSetValueAttribute:
        return AXPropertyFlag::CanSetValueAttribute;
    case AXProperty::HasBoldFont:
        return AXPropertyFlag::HasBoldFont;
    case AXProperty::HasClickHandler:
        return AXPropertyFlag::HasClickHandler;
    case AXProperty::HasItalicFont:
        return AXPropertyFlag::HasItalicFont;
    case AXProperty::HasPlainText:
        return AXPropertyFlag::HasPlainText;
    case AXProperty::IsEnabled:
        return AXPropertyFlag::IsEnabled;
    case AXProperty::IsExposedTableCell:
        return AXPropertyFlag::IsExposedTableCell;
    case AXProperty::IsGrabbed:
        return AXPropertyFlag::IsGrabbed;
    case AXProperty::IsIgnored:
        return AXPropertyFlag::IsIgnored;
    case AXProperty::IsInlineText:
        return AXPropertyFlag::IsInlineText;
    case AXProperty::IsKeyboardFocusable:
        return AXPropertyFlag::IsKeyboardFocusable;
    case AXProperty::IsNonLayerSVGObject:
        return AXPropertyFlag::IsNonLayerSVGObject;
    case AXProperty::IsTableRow:
        return AXPropertyFlag::IsTableRow;
    case AXProperty::IsVisited:
        return AXPropertyFlag::IsVisited;
    case AXProperty::SupportsCheckedState:
        return AXPropertyFlag::SupportsCheckedState;
    case AXProperty::SupportsDragging:
        return AXPropertyFlag::SupportsDragging;
    case AXProperty::SupportsExpanded:
        return AXPropertyFlag::SupportsExpanded;
    case AXProperty::SupportsPath:
        return AXPropertyFlag::SupportsPath;
    case AXProperty::SupportsPosInSet:
        return AXPropertyFlag::SupportsPosInSet;
    case AXProperty::SupportsSetSize:
        return AXPropertyFlag::SupportsSetSize;
    default:
        return std::nullopt;
    }
}

void setPropertyIn(AXProperty property, AXPropertyValueVariant&& value, AXPropertyVector& properties, OptionSet<AXPropertyFlag>& propertyFlags)
{
    if (const bool* boolValue = std::get_if<bool>(&value); boolValue && *boolValue) {
        if (std::optional propertyFlag = convertToPropertyFlag(property)) {
            propertyFlags.add(*propertyFlag);
            return;
        }
    }

    if (!isDefaultValue(property, value))
        properties.append(std::pair(property, WTFMove(value)));
}

static bool shouldCacheElementName(ElementName name)
{
    switch (name) {
    case ElementName::HTML_body:
    case ElementName::HTML_del:
    case ElementName::HTML_h1:
    case ElementName::HTML_h2:
    case ElementName::HTML_h3:
    case ElementName::HTML_h4:
    case ElementName::HTML_h5:
    case ElementName::HTML_h6:
    case ElementName::HTML_ins:
    case ElementName::HTML_th:
    case ElementName::HTML_time:
#if ENABLE(AX_THREAD_TEXT_APIS)
    case ElementName::HTML_mark:
    case ElementName::HTML_attachment:
    case ElementName::HTML_thead:
    case ElementName::HTML_tbody:
    case ElementName::HTML_tfoot:
    case ElementName::HTML_output:
#endif // ENABLE(AX_THREAD_TEXT_APIS)
        return true;
    default:
        return false;
    }
}

static bool canBeMultilineTextField(AccessibilityObject& object)
{
    if (object.isNonNativeTextControl())
        return !object.hasAttribute(aria_multilineAttr) || object.ariaIsMultiline();

    auto* renderer = object.renderer();
    if (renderer && renderer->isRenderTextControl())
        return renderer->isRenderTextControlMultiLine();

    // If we're not sure, return true, it means we can't use this as an optimization to avoid computing the line index.
    return true;
}

// Allocate a capacity based on the most common property count objects have (based on measurements from a real webpage).
// Based on said measurements, 59.6% objects have 2 or less properties. We'll shrink the vector at the end for objects
// that have less than 2.
static constexpr unsigned unignoredSizeToReserve = 2;
IsolatedObjectData createIsolatedObjectData(const Ref<AccessibilityObject>& axObject, Ref<AXIsolatedTree> tree)
{
    auto& object = axObject.get();

    bool getsGeometryFromChildren = false;
    AXPropertyVector properties;
    OptionSet<AXPropertyFlag> propertyFlags;
    auto setProperty = [&] (AXProperty property, AXPropertyValueVariant&& value) {
        setPropertyIn(property, WTFMove(value), properties, propertyFlags);
    };
    auto setObjectProperty = [&] (AXProperty property, AXCoreObject* object) {
        setProperty(property, object ? Markable { object->objectID() } : std::nullopt);
    };
    auto setObjectVectorProperty = [&] (AXProperty property, const AXCoreObject::AccessibilityChildrenVector& objects) {
        setProperty(property, axIDs(objects));
    };
    auto setMathscripts = [&] (AXProperty property, AccessibilityObject& object) {
        AXCoreObject::AccessibilityMathMultiscriptPairs pairs;
        if (property == AXProperty::MathPrescripts)
            object.mathPrescripts(pairs);
        else if (property == AXProperty::MathPostscripts)
            object.mathPostscripts(pairs);

        if (pairs.isEmpty())
            return;

        auto idPairs = pairs.map([](auto& mathPair) {
            return std::pair { mathPair.first ? Markable { mathPair.first->objectID() } : std::nullopt, mathPair.second ? Markable { mathPair.second->objectID() } : std::nullopt };
        });
        setProperty(property, WTFMove(idPairs));
    };

    auto reserveCapacityAndCacheBaseProperties = [&] (unsigned sizeToReserve) {
        if (sizeToReserve)
            properties.reserveInitialCapacity(sizeToReserve);

        // These properties are cached for all objects, ignored and unignored.
        setProperty(AXProperty::HasClickHandler, object.hasClickHandler());
        auto elementName = object.elementName();
        if (shouldCacheElementName(elementName))
            setProperty(AXProperty::ElementName, elementName);

#if ENABLE(AX_THREAD_TEXT_APIS)
        setProperty(AXProperty::TextRuns, std::make_shared<AXTextRuns>(object.textRuns()));
        switch (object.textEmissionBehavior()) {
        case TextEmissionBehavior::DoubleNewline:
            propertyFlags.add(AXPropertyFlag::IsTextEmissionBehaviorDoubleNewline);
            break;
        case TextEmissionBehavior::Newline:
            propertyFlags.add(AXPropertyFlag::IsTextEmissionBehaviorNewline);
            break;
        case TextEmissionBehavior::Tab:
            propertyFlags.add(AXPropertyFlag::IsTextEmissionBehaviorTab);
            break;
        case TextEmissionBehavior::None:
            break;
        }
        if (object.role() == AccessibilityRole::ListMarker) {
            setProperty(AXProperty::ListMarkerText, object.listMarkerText().isolatedCopy());
            setProperty(AXProperty::ListMarkerLineID, object.listMarkerLineID());
        }
#endif // ENABLE(AX_THREAD_TEXT_APIS)

        String language = object.language();
        if (!language.isEmpty())
            setProperty(AXProperty::Language, WTFMove(language).isolatedCopy());
        setProperty(AXProperty::IsEnabled, object.isEnabled());
        appendBasePlatformProperties(properties, propertyFlags, axObject);
    };

    bool needsAllProperties = true;
#if ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)
    if (object.includeIgnoredInCoreTree()) {
        bool isIgnored = object.isIgnored();
        setProperty(AXProperty::IsIgnored, isIgnored);
        // Maintain full properties for objects meeting this criteria:
        //   - Unconnected objects, which are involved in relations or outgoing notifications
        //   - Static text. We sometimes ignore static text (e.g. because it descends from a text field),
        //     but need full properties for proper text marker behavior.
        // FIXME: We shouldn't cache all properties for empty / non-rendered text?
        needsAllProperties = !isIgnored || tree->isUnconnectedNode(object.objectID()) || is<RenderText>(object.renderer());
    }
#else
    reserveCapacityAndCacheBaseProperties(unignoredSizeToReserve);
#endif // ENABLE(INCLUDE_IGNORED_IN_CORE_AX_TREE)

    if (!needsAllProperties)
        reserveCapacityAndCacheBaseProperties(0);
    else {
        reserveCapacityAndCacheBaseProperties(unignoredSizeToReserve);

        setProperty(AXProperty::IsAttachment, object.isAttachment());
        setProperty(AXProperty::IsBusy, object.isBusy());
        setProperty(AXProperty::IsExpanded, object.isExpanded());

        // FIXME: Caching isSecureField would require caching an additional property (on top of input type), so for now, let's still cache this.
        setProperty(AXProperty::IsSecureField, object.isSecureField());

        setProperty(AXProperty::IsIndeterminate, object.isIndeterminate());
        setProperty(AXProperty::IsInlineText, object.isInlineText());
        setProperty(AXProperty::IsMultiSelectable, object.isMultiSelectable());
        setProperty(AXProperty::IsRequired, object.isRequired());
        setProperty(AXProperty::IsSelected, object.isSelected());
        setProperty(AXProperty::IsVisited, object.isVisited());
        setProperty(AXProperty::IsValueAutofillAvailable, object.isValueAutofillAvailable());
        setProperty(AXProperty::ARIARoleDescription, object.ariaRoleDescription().isolatedCopy());
        setProperty(AXProperty::SubrolePlatformString, object.subrolePlatformString().isolatedCopy());
        setProperty(AXProperty::CanSetFocusAttribute, object.canSetFocusAttribute());
        setProperty(AXProperty::CanSetValueAttribute, object.canSetValueAttribute());
        setProperty(AXProperty::CanSetSelectedAttribute, object.canSetSelectedAttribute());
        setProperty(AXProperty::ValueDescription, object.valueDescription().isolatedCopy());
        setProperty(AXProperty::ValueForRange, object.valueForRange());
        setProperty(AXProperty::MaxValueForRange, object.maxValueForRange());
        setProperty(AXProperty::MinValueForRange, object.minValueForRange());
        setProperty(AXProperty::SupportsARIAOwns, object.supportsARIAOwns());
        setProperty(AXProperty::ExplicitPopupValue, object.explicitPopupValue().isolatedCopy());
        setProperty(AXProperty::ExplicitInvalidStatus, object.explicitInvalidStatus().isolatedCopy());
        setProperty(AXProperty::SupportsExpanded, object.supportsExpanded());
        setProperty(AXProperty::SortDirection, static_cast<int>(object.sortDirection()));
#if !LOG_DISABLED
        // Eagerly cache ID when logging is enabled so that we can log isolated objects without constant deadlocks.
        // Don't cache ID when logging is disabled because we don't expect non-test AX clients to actually request it.
        setProperty(AXProperty::IdentifierAttribute, object.identifierAttribute().isolatedCopy());
#endif
        // FIXME: We never update AXProperty::SupportsDropping.
        setProperty(AXProperty::SupportsDropping, object.supportsDropping());
        setProperty(AXProperty::SupportsDragging, object.supportsDragging());
        setProperty(AXProperty::IsGrabbed, object.isGrabbed());
        setProperty(AXProperty::PlaceholderValue, object.placeholderValue().isolatedCopy());
        setProperty(AXProperty::ValueAutofillButtonType, static_cast<int>(object.valueAutofillButtonType()));
        setProperty(AXProperty::URL, std::make_shared<URL>(object.url().isolatedCopy()));
        setProperty(AXProperty::AccessKey, object.accessKey().isolatedCopy());
        setProperty(AXProperty::ExplicitAutoCompleteValue, object.explicitAutoCompleteValue().isolatedCopy());
        setProperty(AXProperty::ColorValue, object.colorValue());
        if (std::optional orientation = object.explicitOrientation())
            setProperty(AXProperty::ExplicitOrientation, *orientation);
        setProperty(AXProperty::ExplicitLiveRegionStatus, object.explicitLiveRegionStatus().isolatedCopy());
        setProperty(AXProperty::ExplicitLiveRegionRelevant, object.explicitLiveRegionRelevant().isolatedCopy());
        setProperty(AXProperty::LiveRegionAtomic, object.liveRegionAtomic());
        setProperty(AXProperty::HasBoldFont, object.hasBoldFont());
        setProperty(AXProperty::HasItalicFont, object.hasItalicFont());
        setProperty(AXProperty::HasPlainText, object.hasPlainText());
#if !ENABLE(AX_THREAD_TEXT_APIS)
        setProperty(AXProperty::TextContentPrefixFromListMarker, object.textContentPrefixFromListMarker());
#endif
        setProperty(AXProperty::IsKeyboardFocusable, object.isKeyboardFocusable());
        setProperty(AXProperty::BrailleRoleDescription, object.brailleRoleDescription().isolatedCopy());
        setProperty(AXProperty::BrailleLabel, object.brailleLabel().isolatedCopy());
        setProperty(AXProperty::IsNonLayerSVGObject, object.isNonLayerSVGObject());

        // Only cache input types on things that are an input.
        if (std::optional inputType = axObject->inputType())
            setProperty(AXProperty::InputType, *inputType);

        bool isWebArea = axObject->isWebArea();
        bool isScrollArea = axObject->isScrollView();
        if (isScrollArea && !axObject->parentObject()) {
            // Eagerly cache the screen relative position for the root. AXIsolatedObject::screenRelativePosition()
            // of non-root objects depend on the root object's screen relative position, so make sure it's there
            // from the start. We keep this up-to-date via AXIsolatedTree::updateRootScreenRelativePosition().
            setProperty(AXProperty::ScreenRelativePosition, axObject->screenRelativePosition());
            // FIXME: We never update this property, e.g. when the iframe is moved in the hosting web content process.
            setProperty(AXProperty::RemoteFrameOffset, object.remoteFrameOffset());
        }

        RefPtr geometryManager = tree->geometryManager();
        std::optional frame = geometryManager ? geometryManager->cachedRectForID(object.objectID()) : std::nullopt;
        if (frame)
            setProperty(AXProperty::RelativeFrame, WTFMove(*frame));
        else if (isScrollArea || isWebArea || object.isScrollbar()) {
            // The GeometryManager does not have a relative frame for ScrollViews, WebAreas, or scrollbars yet. We need to get it from the
            // live object so that we don't need to hit the main thread in the case a request comes in while the whole isolated tree is being built.
            setProperty(AXProperty::RelativeFrame, enclosingIntRect(object.relativeFrame()));
        } else if (!object.renderer() && object.node() && is<AccessibilityNodeObject>(object)) {
            // The frame of node-only AX objects is made up of their children.
            getsGeometryFromChildren = true;
        } else if (object.isMenuListPopup()) {
            // AccessibilityMenuListPopup's elementRect is hardcoded to return an empty rect, so preserve that behavior.
            setProperty(AXProperty::RelativeFrame, IntRect());
        } else
            setProperty(AXProperty::InitialFrameRect, object.frameRect());

        if (isWebArea)
            setProperty(AXProperty::IsEditableWebArea, object.isEditableWebArea());

        if (object.supportsPath()) {
            setProperty(AXProperty::SupportsPath, true);
            setProperty(AXProperty::Path, std::make_shared<Path>(object.elementPath()));
        }

        if (object.supportsKeyShortcuts()) {
            setProperty(AXProperty::SupportsKeyShortcuts, true);
            setProperty(AXProperty::KeyShortcuts, object.keyShortcuts().isolatedCopy());
        }

        if (object.supportsCurrent()) {
            setProperty(AXProperty::SupportsCurrent, true);
            setProperty(AXProperty::CurrentState, static_cast<int>(object.currentState()));
        }

        if (object.supportsSetSize()) {
            setProperty(AXProperty::SupportsSetSize, true);
            setProperty(AXProperty::SetSize, object.setSize());
        }

        if (object.supportsPosInSet()) {
            setProperty(AXProperty::SupportsPosInSet, true);
            setProperty(AXProperty::PosInSet, object.posInSet());
        }

        if (object.supportsExpandedTextValue()) {
            setProperty(AXProperty::SupportsExpandedTextValue, true);
            setProperty(AXProperty::ExpandedTextValue, object.expandedTextValue().isolatedCopy());
        }

        if (object.supportsDatetimeAttribute())
            setProperty(AXProperty::DatetimeAttributeValue, object.datetimeAttributeValue().isolatedCopy());

        if (object.supportsCheckedState()) {
            setProperty(AXProperty::SupportsCheckedState, true);
            setProperty(AXProperty::IsChecked, object.isChecked());
            setProperty(AXProperty::ButtonState, object.checkboxOrRadioValue());
        }

        if (object.isTable()) {
            setProperty(AXProperty::IsTable, true);
            setProperty(AXProperty::IsExposable, object.isExposable());
            setObjectVectorProperty(AXProperty::Columns, object.columns());
            setObjectVectorProperty(AXProperty::Rows, object.rows());
            setObjectVectorProperty(AXProperty::Cells, object.cells());
            setObjectVectorProperty(AXProperty::VisibleRows, object.visibleRows());
            setProperty(AXProperty::AXColumnCount, object.axColumnCount());
            setProperty(AXProperty::AXRowCount, object.axRowCount());
            setProperty(AXProperty::CellSlots, object.cellSlots());
        }

        if (object.isExposedTableCell()) {
            setProperty(AXProperty::IsExposedTableCell, true);
            setProperty(AXProperty::ColumnIndexRange, object.columnIndexRange());
            setProperty(AXProperty::RowIndexRange, object.rowIndexRange());
            if (std::optional columnIndex = object.axColumnIndex())
                setProperty(AXProperty::AXColumnIndex, *columnIndex);
            if (std::optional rowIndex = object.axRowIndex())
                setProperty(AXProperty::AXRowIndex, *rowIndex);
            setProperty(AXProperty::AXColumnIndexText, object.axColumnIndexText().isolatedCopy());
            setProperty(AXProperty::AXRowIndexText, object.axRowIndexText().isolatedCopy());
            setProperty(AXProperty::IsColumnHeader, object.isColumnHeader());
            setProperty(AXProperty::IsRowHeader, object.isRowHeader());
            setProperty(AXProperty::CellScope, object.cellScope().isolatedCopy());
        }

        bool isTableRow = object.isTableRow();
        if (isTableRow) {
            setProperty(AXProperty::IsTableRow, true);
            setProperty(AXProperty::RowIndex, object.rowIndex());
        } else if (object.isTableColumn())
            setProperty(AXProperty::ColumnIndex, object.columnIndex());

        if (object.isARIATreeGridRow()) {
            setProperty(AXProperty::IsARIATreeGridRow, true);
            setObjectVectorProperty(AXProperty::DisclosedRows, object.disclosedRows());
            setObjectProperty(AXProperty::DisclosedByRow, object.disclosedByRow());
        } else if (object.isARIAGridRow())
            setProperty(AXProperty::IsARIAGridRow, true);

        bool isTreeItem = object.isTreeItem();
        if (isTreeItem) {
            setProperty(AXProperty::IsTreeItem, true);
            setObjectVectorProperty(AXProperty::DisclosedRows, object.disclosedRows());
        }

        setProperty(AXProperty::IsTree, object.isTree());
        if (object.isRadioButton()) {
            setProperty(AXProperty::NameAttribute, object.nameAttribute().isolatedCopy());
            // FIXME: This property doesn't get updated when a page changes dynamically.
            setObjectVectorProperty(AXProperty::RadioButtonGroup, object.radioButtonGroup());
        }

        if (object.isImage())
            setProperty(AXProperty::EmbeddedImageDescription, object.embeddedImageDescription().isolatedCopy());

        // On macOS, we only advertise support for the visible children attribute for lists and listboxes.
        if (object.isList() || object.isListBox())
            setObjectVectorProperty(AXProperty::VisibleChildren, object.visibleChildren());

        if (object.isDateTime()) {
            setProperty(AXProperty::DateTimeValue, object.dateTimeValue().isolatedCopy());
            setProperty(AXProperty::DateTimeComponentsType, object.dateTimeComponentsType());
        }

        if (object.isSpinButton()) {
            setObjectProperty(AXProperty::DecrementButton, object.decrementButton());
            setObjectProperty(AXProperty::IncrementButton, object.incrementButton());
        }

        if (object.isMathElement()) {
            setProperty(AXProperty::IsMathElement, true);
            setProperty(AXProperty::IsMathFraction, object.isMathFraction());
            setProperty(AXProperty::IsMathFenced, object.isMathFenced());
            setProperty(AXProperty::IsMathSubscriptSuperscript, object.isMathSubscriptSuperscript());
            setProperty(AXProperty::IsMathRow, object.isMathRow());
            setProperty(AXProperty::IsMathUnderOver, object.isMathUnderOver());
            setProperty(AXProperty::IsMathTable, object.isMathTable());
            setProperty(AXProperty::IsMathTableRow, object.isMathTableRow());
            setProperty(AXProperty::IsMathTableCell, object.isMathTableCell());
            setProperty(AXProperty::IsMathMultiscript, object.isMathMultiscript());
            setProperty(AXProperty::IsMathToken, object.isMathToken());
            setProperty(AXProperty::MathFencedOpenString, object.mathFencedOpenString().isolatedCopy());
            setProperty(AXProperty::MathFencedCloseString, object.mathFencedCloseString().isolatedCopy());
            setProperty(AXProperty::MathLineThickness, object.mathLineThickness());
            setProperty(AXProperty::IsAnonymousMathOperator, object.isAnonymousMathOperator());

            bool isMathRoot = object.isMathRoot();
            setProperty(AXProperty::IsMathRoot, isMathRoot);
            setProperty(AXProperty::IsMathSquareRoot, object.isMathSquareRoot());
            if (isMathRoot) {
                if (auto radicand = object.mathRadicand())
                    setObjectVectorProperty(AXProperty::MathRadicand, *radicand);

                setObjectProperty(AXProperty::MathRootIndexObject, object.mathRootIndexObject());
            }

            setObjectProperty(AXProperty::MathUnderObject, object.mathUnderObject());
            setObjectProperty(AXProperty::MathOverObject, object.mathOverObject());
            setObjectProperty(AXProperty::MathNumeratorObject, object.mathNumeratorObject());
            setObjectProperty(AXProperty::MathDenominatorObject, object.mathDenominatorObject());
            setObjectProperty(AXProperty::MathBaseObject, object.mathBaseObject());
            setObjectProperty(AXProperty::MathSubscriptObject, object.mathSubscriptObject());
            setObjectProperty(AXProperty::MathSuperscriptObject, object.mathSuperscriptObject());
            setMathscripts(AXProperty::MathPrescripts, object);
            setMathscripts(AXProperty::MathPostscripts, object);
        }

        Vector<AccessibilityText> texts;
        object.accessibilityText(texts);
        auto axTextValue = texts.map([] (const auto& text) -> AccessibilityText {
            return { text.text.isolatedCopy(), text.textSource };
        });
        setProperty(AXProperty::AccessibilityText, axTextValue);

        if (isScrollArea) {
            setObjectProperty(AXProperty::VerticalScrollBar, object.scrollBar(AccessibilityOrientation::Vertical));
            setObjectProperty(AXProperty::HorizontalScrollBar, object.scrollBar(AccessibilityOrientation::Horizontal));
            setProperty(AXProperty::HasRemoteFrameChild, object.hasRemoteFrameChild());
        } else if (isWebArea && !tree->isEmptyContentTree()) {
            // We expose DocumentLinks only for the web area objects when the tree is not an empty content tree. This property is expensive and makes no sense in an empty content tree.
            // FIXME: compute DocumentLinks on the AX thread instead of caching it.
            setObjectVectorProperty(AXProperty::DocumentLinks, object.documentLinks());
        }

        if (object.isWidget()) {
            if (object.isPlugin()) {
                // Plugins are a subclass of widget, so we only need to cache IsPlugin, and we implicitly know
                // this is also a widget (see AXIsolatedObject::isWidget).
                setProperty(AXProperty::IsPlugin, true);
            } else
                setProperty(AXProperty::IsWidget, true);

            setProperty(AXProperty::IsVisible, object.isVisible());
        }

        auto descriptor = object.title();
        if (descriptor.length())
            setProperty(AXProperty::Title, descriptor.isolatedCopy());

        descriptor = object.description();
        if (descriptor.length())
            setProperty(AXProperty::Description, descriptor.isolatedCopy());

        descriptor = object.extendedDescription();
        if (descriptor.length())
            setProperty(AXProperty::ExtendedDescription, descriptor.isolatedCopy());

        if (object.isTextControl()) {
            // FIXME: We don't keep this property up-to-date, and we can probably just compute it using
            // AXIsolatedObject::selectedTextMarkerRange() (which does stay up-to-date).
            setProperty(AXProperty::SelectedTextRange, object.selectedTextRange());

            auto range = object.textInputMarkedTextMarkerRange();
            if (auto characterRange = range.characterRange(); range && characterRange)
                setProperty(AXProperty::TextInputMarkedTextMarkerRange, std::make_shared<AXIDAndCharacterRange>(AXIDAndCharacterRange(range.start().objectID(), *characterRange)));

            setProperty(AXProperty::CanBeMultilineTextField, canBeMultilineTextField(object));
        }

        if (object.isHeading() || isTableRow || isTreeItem)
            setProperty(AXProperty::ARIALevel, object.ariaLevel());

        // These properties are only needed on the AXCoreObject interface due to their use in ATSPI,
        // so only cache them for ATSPI.
#if USE(ATSPI)
        // We cache IsVisible on all platforms just for Widgets above. In ATSPI, this should be cached on all objects.
        if (!object.isWidget())
            setProperty(AXProperty::IsVisible, object.isVisible());

        setProperty(AXProperty::ActionVerb, object.actionVerb().isolatedCopy());
        setProperty(AXProperty::IsFieldset, object.isFieldset());
        setProperty(AXProperty::IsPressed, object.isPressed());
        setProperty(AXProperty::IsSelectedOptionActive, object.isSelectedOptionActive());
        setProperty(AXProperty::LocalizedActionVerb, object.localizedActionVerb().isolatedCopy());
#endif // USE(ATSPI)
        setObjectProperty(AXProperty::InternalLinkElement, object.internalLinkElement());
    }

    appendPlatformProperties(properties, propertyFlags, axObject);

    RefPtr axParent = object.parentInCoreTree();
    Markable<AXID> parentID = axParent ? std::optional(axParent->objectID()) : std::nullopt;

    properties.shrinkToFit();
    return {
        object.childrenIDs(),
        WTFMove(properties),
        WTFMove(tree),
        parentID,
        object.objectID(),
        object.role(),
        propertyFlags,
        getsGeometryFromChildren
    };
}

} // namespace WebCore
#endif // ENABLE(ACCESSIBILITY_ISOLATED_TREE)