Skip to content

Truly independent web browser

LibWebView: Apply incremental changes to the accessibility tree

sideshowbarkere4896950e9e9
diff --git a/Libraries/LibWebCommon/WebView/AccessibilityNodeData.h b/Libraries/LibWebCommon/WebView/AccessibilityNodeData.h
index 2faf848e87d..4425a1d9b08 100644
--- a/Libraries/LibWebCommon/WebView/AccessibilityNodeData.h
+++ b/Libraries/LibWebCommon/WebView/AccessibilityNodeData.h
@@ -144,6 +144,15 @@ struct WEBCOMMON_API AccessibilityNodeData {
bool operator==(AccessibilityNodeData const&) const = default;
};
+// A change to an accessibility tree the receiver already holds: the complete data of every node that's new or whose data
+// changed (a node whose children changed among them, with its new child IDs), in tree order, and the IDs of the nodes no
+// longer in the tree. A node that moves keeps its ID: Its old and new parents are both among the changed nodes. The order
+// decides which of several nodes inserted into a live region the region announces: the first.
+struct WEBCOMMON_API AccessibilityTreeDelta {
+ Vector<AccessibilityNodeData> changed_nodes;
+ Vector<i64> removed_ids;
+};
+
}
#include <LibWebCommon/WebView/AccessibilityNodeDataCodecs.h>
diff --git a/Libraries/LibWebView/AccessibilityTreeManager.cpp b/Libraries/LibWebView/AccessibilityTreeManager.cpp
index 329ed42c692..f8f4bb4e7ee 100644
--- a/Libraries/LibWebView/AccessibilityTreeManager.cpp
+++ b/Libraries/LibWebView/AccessibilityTreeManager.cpp
@@ -164,6 +164,212 @@ void AccessibilityTreeManager::update_tree(Vector<AccessibilityNodeData> nodes)
m_nodes.set(node.id, move(node));
}
+Optional<Vector<i64>> AccessibilityTreeManager::apply_delta(AccessibilityTreeDelta const& delta)
+{
+ // The tree held was checked when it arrived, so only what the delta touches needs checking here: the changed and
+ // removed nodes, the nodes whose parent changes, and the subtrees that move. Nothing is changed until all of it
+ // passes, so a delta that doesn't fit leaves the tree exactly as it was.
+ if (m_root_id == -1)
+ return {};
+
+ HashMap<i64, AccessibilityNodeData const*> changed;
+ changed.ensure_capacity(delta.changed_nodes.size());
+ // Node IDs are never negative, and -1 means "no parent".
+ for (auto const& node : delta.changed_nodes) {
+ if (node.id < 0)
+ return {};
+ if (changed.set(node.id, &node) != HashSetResult::InsertedNewEntry)
+ return {}; // the same node twice
+ }
+
+ HashTable<i64> removed;
+ removed.ensure_capacity(delta.removed_ids.size());
+ for (auto id : delta.removed_ids) {
+ if (id < 0 || id == m_root_id || !m_nodes.contains(id) || changed.contains(id))
+ return {};
+ if (removed.set(id) != HashSetResult::InsertedNewEntry)
+ return {};
+ }
+
+ // The node with this ID once the delta is applied, or null if there's none.
+ auto final_node = [&](i64 id) -> AccessibilityNodeData const* {
+ if (auto it = changed.find(id); it != changed.end())
+ return it->value;
+ if (removed.contains(id))
+ return nullptr;
+ return node(id);
+ };
+
+ // A node's children are listed by its parent alone, so a node gets a new parent only from a changed node's child
+ // list, and no node may appear in two of them (or twice in one).
+ HashMap<i64, i64> new_parent;
+ for (auto const& node : delta.changed_nodes) {
+ for (auto child_id : node.child_ids) {
+ if (child_id < 0 || child_id == m_root_id || !final_node(child_id))
+ return {};
+ if (new_parent.set(child_id, node.id) != HashSetResult::InsertedNewEntry)
+ return {};
+ }
+ }
+
+ auto old_parent_of = [&](i64 id) -> Optional<i64> {
+ if (auto it = m_nodes.find(id); it != m_nodes.end())
+ return it->value.parent_id;
+ return {};
+ };
+ auto keeps_its_child_list = [&](i64 id) { return !changed.contains(id) && !removed.contains(id); };
+
+ for (auto const& node : delta.changed_nodes) {
+ // A node that's new has to be attached somewhere.
+ if (!m_nodes.contains(node.id) && !new_parent.contains(node.id))
+ return {};
+ }
+ for (auto const& [child_id, parent_id] : new_parent) {
+ // A node that moves has to leave its old parent's child list, so that list has to be one the delta replaces.
+ auto old_parent = old_parent_of(child_id);
+ if (old_parent.has_value() && *old_parent != parent_id && keeps_its_child_list(*old_parent))
+ return {};
+ }
+
+ // A child that a replaced child list or a removed node leaves behind has to be removed too, or attached elsewhere;
+ // and a removed node can't stay in a child list the delta doesn't replace.
+ auto check_left_behind_children = [&](i64 id) {
+ for (auto child_id : m_nodes.get(id)->child_ids) {
+ if (!removed.contains(child_id) && !new_parent.contains(child_id))
+ return false;
+ }
+ return true;
+ };
+ for (auto const& node : delta.changed_nodes) {
+ if (m_nodes.contains(node.id) && !check_left_behind_children(node.id))
+ return {};
+ }
+ for (auto id : removed) {
+ if (!check_left_behind_children(id) || keeps_its_child_list(*old_parent_of(id)))
+ return {};
+ }
+
+ auto final_parent_of = [&](i64 id) -> i64 {
+ if (auto it = new_parent.find(id); it != new_parent.end())
+ return it->value;
+ return *old_parent_of(id);
+ };
+
+ // Every node whose parent changes has to reach the root, through no more than the depth the tree allows. Any cycle
+ // runs through such a node, since the tree held had none, so this also rejects every cycle. Then each moved
+ // subtree has to fit under that depth too.
+ HashMap<i64, size_t> depth_of;
+ depth_of.set(m_root_id, 0);
+ auto depth_in_final_tree = [&](i64 id) -> Optional<size_t> {
+ Vector<i64, 32> chain;
+ auto current = id;
+ while (!depth_of.contains(current)) {
+ if (chain.size() > max_accessibility_tree_depth)
+ return {};
+ chain.append(current);
+ current = final_parent_of(current);
+ if (current == -1)
+ return {};
+ }
+ auto depth = depth_of.get(current).value();
+ for (size_t i = chain.size(); i > 0; --i) {
+ ++depth;
+ depth_of.set(chain[i - 1], depth);
+ }
+ if (depth > max_accessibility_tree_depth)
+ return {};
+ return depth;
+ };
+
+ HashTable<i64> walked;
+ for (auto const& [moved_id, parent_id] : new_parent) {
+ auto old_parent = old_parent_of(moved_id);
+ if (old_parent.has_value() && *old_parent == parent_id)
+ continue;
+ if (walked.contains(moved_id))
+ continue;
+ auto depth = depth_in_final_tree(moved_id);
+ if (!depth.has_value())
+ return {};
+
+ struct Entry {
+ i64 id { 0 };
+ size_t depth { 0 };
+ };
+ Vector<Entry> stack;
+ stack.append({ moved_id, *depth });
+ while (!stack.is_empty()) {
+ auto [id, node_depth] = stack.take_last();
+ if (walked.set(id) != HashSetResult::InsertedNewEntry)
+ continue;
+ auto const* node = final_node(id);
+ if (!node || (!node->child_ids.is_empty() && node_depth == max_accessibility_tree_depth))
+ return {};
+ for (auto child_id : node->child_ids)
+ stack.append({ child_id, node_depth + 1 });
+ }
+ }
+
+ // The delta fits. Find what it announces in the tree it produces, while the old data is still at hand to compare
+ // against: A changed, nonempty name in a live region that isn't off, or a new node in a live region that was already
+ // there, once per region.
+ Vector<String> announcement_texts;
+ Vector<String> announcement_politeness;
+ if (on_live_region_changed) {
+ HashTable<i64> announced_regions;
+ for (auto const& changed_node : delta.changed_nodes) {
+ if (changed_node.name.is_empty())
+ continue;
+ i64 live_region_id = -1;
+ String live_value;
+ for (auto id = changed_node.id; id != -1; id = final_parent_of(id)) {
+ auto const* ancestor = final_node(id);
+ if (!ancestor->live.is_empty()) {
+ live_region_id = id;
+ live_value = ancestor->live;
+ break;
+ }
+ }
+ if (live_value.is_empty() || live_value == "off"sv)
+ continue;
+
+ auto old_it = m_nodes.find(changed_node.id);
+ if (old_it == m_nodes.end()) {
+ if (!m_nodes.contains(live_region_id) || announced_regions.contains(live_region_id))
+ continue;
+ announced_regions.set(live_region_id);
+ } else if (old_it->value.name == changed_node.name) {
+ continue;
+ }
+ announcement_texts.append(changed_node.name);
+ announcement_politeness.append(live_value);
+ }
+ }
+
+ Vector<i64> removed_ids;
+ removed_ids.ensure_capacity(removed.size());
+ for (auto id : delta.removed_ids) {
+ m_nodes.remove(id);
+ removed_ids.append(id);
+ }
+ for (auto const& [child_id, parent_id] : new_parent) {
+ if (!changed.contains(child_id))
+ m_nodes.find(child_id)->value.parent_id = parent_id;
+ }
+ for (auto const& changed_node : delta.changed_nodes) {
+ auto parent_id = changed_node.id == m_root_id ? -1 : final_parent_of(changed_node.id);
+ auto& stored = m_nodes.ensure(changed_node.id);
+ stored = changed_node;
+ stored.parent_id = parent_id;
+ }
+ ++m_generation;
+
+ for (size_t i = 0; i < announcement_texts.size(); ++i)
+ on_live_region_changed(move(announcement_texts[i]), move(announcement_politeness[i]));
+
+ return removed_ids;
+}
+
AccessibilityNodeData const* AccessibilityTreeManager::node(i64 id) const
{
auto it = m_nodes.find(id);
diff --git a/Libraries/LibWebView/AccessibilityTreeManager.h b/Libraries/LibWebView/AccessibilityTreeManager.h
index b93cf16c80e..62aabf8e705 100644
--- a/Libraries/LibWebView/AccessibilityTreeManager.h
+++ b/Libraries/LibWebView/AccessibilityTreeManager.h
@@ -19,6 +19,10 @@ public:
AK_ALLOC_WITH_KMALLOC;
void update_tree(Vector<AccessibilityNodeData>);
+ // Applies a change to the tree held, if it fits that tree, and returns the IDs of the nodes it removed. A change that
+ // doesn't fit (it names nodes the tree doesn't hold, or would leave a node with no parent or two, a cycle, or a tree
+ // too deep) changes nothing, and announces nothing: The caller should get the whole tree again instead.
+ Optional<Vector<i64>> apply_delta(AccessibilityTreeDelta const&);
Function<void(String, String)> on_live_region_changed;
@@ -31,7 +35,7 @@ public:
i64 root_id() const { return m_root_id; }
bool is_empty() const { return m_nodes.is_empty(); }
- // Monotonic counter incremented by update_tree(). Consumers that cache derived data keyed on node contents can
+ // Monotonic counter incremented by update_tree() and apply_delta(). Consumers that cache derived data keyed on node contents can
// compare against the generation they last computed at — to know when a cache is stale.
u64 generation() const { return m_generation; }
diff --git a/Tests/LibWebView/TestAccessibilityTreeManager.cpp b/Tests/LibWebView/TestAccessibilityTreeManager.cpp
index 8f2587c47a6..2cd284977e5 100644
--- a/Tests/LibWebView/TestAccessibilityTreeManager.cpp
+++ b/Tests/LibWebView/TestAccessibilityTreeManager.cpp
@@ -297,3 +297,218 @@ TEST_CASE(insertion_into_off_live_region_is_not_announced)
EXPECT(announcements.is_empty());
}
+
+namespace {
+
+// Every node of the tree held, in depth-first order, as "id(parent)[children] name" - so a test can check a tree
+// exactly, and check that a rejected delta left it exactly as it was.
+ByteString describe_tree(WebView::AccessibilityTreeManager const& manager)
+{
+ StringBuilder builder;
+ Vector<i64> stack;
+ if (manager.root_id() != -1)
+ stack.append(manager.root_id());
+ while (!stack.is_empty()) {
+ auto const* node = manager.node(stack.take_last());
+ if (!node) {
+ builder.append("missing "sv);
+ continue;
+ }
+ builder.appendff("{}({})[", node->id, node->parent_id);
+ builder.join(',', node->child_ids);
+ builder.appendff("]{} ", node->name);
+ for (size_t i = node->child_ids.size(); i > 0; --i)
+ stack.append(node->child_ids[i - 1]);
+ }
+ return builder.to_byte_string();
+}
+
+// 0 holds 1 and 2; 1 holds 3 and 4. 0 is a live region, so any change a delta makes to a name would announce.
+void seed_base_tree(WebView::AccessibilityTreeManager& manager)
+{
+ Vector<WebView::AccessibilityNodeData> nodes;
+ nodes.append(make_named_node(0, -1, ""sv, "polite"sv, { 1, 2 }));
+ nodes.append(make_node(1, 0, { 3, 4 }));
+ nodes.append(make_node(2, 0));
+ nodes.append(make_node(3, 1));
+ nodes.append(make_node(4, 1));
+ manager.update_tree(move(nodes));
+}
+
+}
+
+TEST_CASE(delta_changes_inserts_moves_and_removes_nodes)
+{
+ WebView::AccessibilityTreeManager manager;
+ seed_base_tree(manager);
+ auto generation = manager.generation();
+
+ // Rename 3, move 4 from 1 to 2, and give 2 a new child 5. 4's own data doesn't change, and 5 claims a wrong parent:
+ // The parent comes from the child lists alone.
+ WebView::AccessibilityTreeDelta delta;
+ delta.changed_nodes.append(make_node(1, 0, { 3 }));
+ delta.changed_nodes.append(make_node(2, 0, { 4, 5 }));
+ delta.changed_nodes.append(make_named_node(3, 1, "renamed"sv));
+ delta.changed_nodes.append(make_node(5, 3));
+ auto removed = manager.apply_delta(delta);
+ EXPECT(removed.has_value());
+ EXPECT(removed->is_empty());
+ EXPECT_EQ(describe_tree(manager), "0(-1)[1,2] 1(0)[3] 3(1)[]renamed 2(0)[4,5] 4(2)[] 5(2)[] "sv);
+ EXPECT_EQ(manager.generation(), generation + 1);
+
+ // Remove 2 with its subtree.
+ WebView::AccessibilityTreeDelta removal;
+ removal.changed_nodes.append(make_node(0, -1, { 1 }));
+ removal.removed_ids = { 2, 4, 5 };
+ removed = manager.apply_delta(removal);
+ EXPECT(removed.has_value());
+ EXPECT_EQ(removed->size(), 3u);
+ EXPECT_EQ(describe_tree(manager), "0(-1)[1] 1(0)[3] 3(1)[]renamed "sv);
+ EXPECT(!manager.node(2) && !manager.node(4) && !manager.node(5));
+ EXPECT_EQ(manager.generation(), generation + 2);
+}
+
+TEST_CASE(delta_that_does_not_fit_changes_nothing)
+{
+ struct Case {
+ StringView description;
+ Function<void(WebView::AccessibilityTreeDelta&)> build;
+ };
+ Vector<Case> cases;
+ cases.append({ "removes a node the tree doesn't hold"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_node(2, 0));
+ delta.removed_ids = { 99 };
+ } });
+ cases.append({ "removes the root"sv, [](auto& delta) { delta.removed_ids = { 0 }; } });
+ cases.append({ "removes a node its unchanged parent still lists"sv, [](auto& delta) { delta.removed_ids = { 3 }; } });
+ cases.append({ "removes a node but not its children"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_node(0, -1, { 2 }));
+ delta.removed_ids = { 1 };
+ } });
+ cases.append({ "adds a node no child list holds"sv, [](auto& delta) { delta.changed_nodes.append(make_node(9, 2)); } });
+ cases.append({ "lists one node in two child lists"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_node(1, 0, { 3, 4 }));
+ delta.changed_nodes.append(make_node(2, 0, { 4 }));
+ } });
+ cases.append({ "lists one node twice in a child list"sv, [](auto& delta) { delta.changed_nodes.append(make_node(2, 0, { 3, 3 })); } });
+ cases.append({ "moves a node its unchanged old parent still lists"sv, [](auto& delta) { delta.changed_nodes.append(make_node(2, 0, { 4 })); } });
+ cases.append({ "drops a child without removing or moving it"sv, [](auto& delta) { delta.changed_nodes.append(make_node(1, 0, { 3 })); } });
+ cases.append({ "forms a cycle"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_node(0, -1, { 2 }));
+ delta.changed_nodes.append(make_node(3, 1, { 1 }));
+ } });
+ cases.append({ "makes a node its own child"sv, [](auto& delta) { delta.changed_nodes.append(make_node(2, 0, { 2 })); } });
+ cases.append({ "lists a node that doesn't exist"sv, [](auto& delta) { delta.changed_nodes.append(make_node(2, 0, { 77 })); } });
+ cases.append({ "lists the root as a child"sv, [](auto& delta) { delta.changed_nodes.append(make_node(2, 0, { 0 })); } });
+ cases.append({ "changes one node twice"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_named_node(2, 0, "a"sv));
+ delta.changed_nodes.append(make_named_node(2, 0, "b"sv));
+ } });
+ cases.append({ "changes and removes the same node"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_node(1, 0, { 4 }));
+ delta.changed_nodes.append(make_named_node(3, 1, "a"sv));
+ delta.removed_ids = { 3 };
+ } });
+ cases.append({ "changes a node with a negative ID"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_node(2, 0, { -1 }));
+ delta.changed_nodes.append(make_named_node(-1, 2, "negative"sv));
+ } });
+ cases.append({ "renames a node and removes one that doesn't exist"sv, [](auto& delta) {
+ delta.changed_nodes.append(make_named_node(3, 1, "renamed"sv));
+ delta.removed_ids = { 99 };
+ } });
+ cases.append({ "makes the tree too deep"sv, [](auto& delta) {
+ // 2 is at depth 1, so a chain of 1,000 new nodes under it ends at depth 1,001.
+ delta.changed_nodes.append(make_node(2, 0, { 100 }));
+ for (i64 id = 100; id < 1100; ++id)
+ delta.changed_nodes.append(make_named_node(id, id - 1, "deep"sv, {}, id + 1 < 1100 ? Vector<i64> { id + 1 } : Vector<i64> {}));
+ } });
+
+ for (auto& test_case : cases) {
+ WebView::AccessibilityTreeManager manager;
+ Vector<String> announcements;
+ manager.on_live_region_changed = [&](String const& text, String const&) { announcements.append(text); };
+ seed_base_tree(manager);
+ auto before = describe_tree(manager);
+ Vector<WebView::AccessibilityNodeData> base_nodes;
+ for (i64 id = 0; id <= 4; ++id)
+ base_nodes.append(*manager.node(id));
+ auto generation = manager.generation();
+
+ WebView::AccessibilityTreeDelta delta;
+ test_case.build(delta);
+ auto result = manager.apply_delta(delta);
+ if (result.has_value())
+ warnln("accepted a delta that {}", test_case.description);
+ EXPECT(!result.has_value());
+ EXPECT_EQ(describe_tree(manager), before);
+ for (auto const& base_node : base_nodes) {
+ auto const* node = manager.node(base_node.id);
+ EXPECT(node && *node == base_node);
+ }
+ for (auto const& changed_node : delta.changed_nodes) {
+ if (changed_node.id < 0 || changed_node.id > 4)
+ EXPECT(!manager.node(changed_node.id));
+ }
+ EXPECT_EQ(manager.generation(), generation);
+ EXPECT(announcements.is_empty());
+ }
+
+ // The deepest chain allowed fits: 999 new nodes under 2 end at depth 1,000.
+ WebView::AccessibilityTreeManager manager;
+ seed_base_tree(manager);
+ WebView::AccessibilityTreeDelta delta;
+ delta.changed_nodes.append(make_node(2, 0, { 100 }));
+ for (i64 id = 100; id < 1099; ++id)
+ delta.changed_nodes.append(make_node(id, id - 1, id + 1 < 1099 ? Vector<i64> { id + 1 } : Vector<i64> {}));
+ EXPECT(manager.apply_delta(delta).has_value());
+
+ // And with no tree held, there's nothing a delta can apply to.
+ WebView::AccessibilityTreeManager empty_manager;
+ WebView::AccessibilityTreeDelta any_delta;
+ EXPECT(!empty_manager.apply_delta(any_delta).has_value());
+}
+
+TEST_CASE(delta_announces_live_region_changes)
+{
+ WebView::AccessibilityTreeManager manager;
+ Vector<String> announcements;
+ manager.on_live_region_changed = [&](String const& text, String const&) { announcements.append(text); };
+
+ Vector<WebView::AccessibilityNodeData> nodes;
+ nodes.append(make_node(0, -1, { 1, 2 }));
+ nodes.append(make_named_node(1, 0, ""sv, "polite"sv, { 3 }));
+ nodes.append(make_node(2, 0));
+ nodes.append(make_named_node(3, 1, "first"sv));
+ manager.update_tree(move(nodes));
+
+ // A subtree inserted into a region that was already there announces once: the text of its topmost node, which
+ // comes first in tree order.
+ WebView::AccessibilityTreeDelta insertion;
+ insertion.changed_nodes.append(make_named_node(1, 0, ""sv, "polite"sv, { 3, 4 }));
+ insertion.changed_nodes.append(make_named_node(4, 1, "inserted"sv, {}, { 5 }));
+ insertion.changed_nodes.append(make_named_node(5, 4, "fragment"sv));
+ EXPECT(manager.apply_delta(insertion).has_value());
+ EXPECT_EQ(announcements.size(), 1u);
+ EXPECT_EQ(announcements[0], "inserted"sv);
+
+ // A changed name in the region announces; a node that changes with its name the same doesn't.
+ announcements.clear();
+ WebView::AccessibilityTreeDelta renaming;
+ renaming.changed_nodes.append(make_named_node(3, 1, "second"sv));
+ auto moved_but_same_name = make_named_node(4, 1, "inserted"sv, {}, { 5 });
+ moved_but_same_name.bounds = { 1, 2, 3, 4 };
+ renaming.changed_nodes.append(moved_but_same_name);
+ EXPECT(manager.apply_delta(renaming).has_value());
+ EXPECT_EQ(announcements.size(), 1u);
+ EXPECT_EQ(announcements[0], "second"sv);
+
+ // A region that arrives with its content announces nothing.
+ announcements.clear();
+ WebView::AccessibilityTreeDelta new_region;
+ new_region.changed_nodes.append(make_node(2, 0, { 6 }));
+ new_region.changed_nodes.append(make_named_node(6, 2, ""sv, "assertive"sv, { 7 }));
+ new_region.changed_nodes.append(make_named_node(7, 6, "born with this"sv));
+ EXPECT(manager.apply_delta(new_region).has_value());
+ EXPECT(announcements.is_empty());
+}