forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathax_live_region_tracker.cc
89 lines (69 loc) · 2.68 KB
/
ax_live_region_tracker.cc
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/ax_live_region_tracker.h"
#include "ui/accessibility/ax_event_generator.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_role_properties.h"
namespace ui {
// static
bool AXLiveRegionTracker::IsLiveRegionRoot(const AXNode& node) {
return node.HasStringAttribute(ax::mojom::StringAttribute::kLiveStatus) &&
node.GetStringAttribute(ax::mojom::StringAttribute::kLiveStatus) !=
"off";
}
AXLiveRegionTracker::AXLiveRegionTracker(const AXTree& tree) : tree_(tree) {
DCHECK(tree.root());
const AXNode& root = *tree.root();
WalkTreeAndAssignLiveRootsToNodes(root, nullptr);
}
AXLiveRegionTracker::~AXLiveRegionTracker() = default;
void AXLiveRegionTracker::UpdateCachedLiveRootForNode(const AXNode& node) {
const AXNode* live_root = &node;
while (live_root && !IsLiveRegionRoot(*live_root))
live_root = live_root->GetUnignoredParent();
if (live_root)
live_region_node_to_root_id_[node.id()] = live_root->id();
}
void AXLiveRegionTracker::OnNodeWillBeDeleted(const AXNode& node) {
if (AXNode* root = GetLiveRoot(node))
QueueChangeEventForDeletedNode(*root);
live_region_node_to_root_id_.erase(node.id());
deleted_node_ids_.insert(node.id());
}
void AXLiveRegionTracker::QueueChangeEventForDeletedNode(const AXNode& root) {
live_region_roots_with_changes_.insert(root.id());
}
void AXLiveRegionTracker::OnAtomicUpdateFinished() {
deleted_node_ids_.clear();
live_region_roots_with_changes_.clear();
}
AXNode* AXLiveRegionTracker::GetLiveRoot(const AXNode& node) const {
auto iter = live_region_node_to_root_id_.find(node.id());
if (iter == live_region_node_to_root_id_.end())
return nullptr;
AXNodeID id = iter->second;
if (deleted_node_ids_.find(id) != deleted_node_ids_.end())
return nullptr;
return tree_.GetFromId(id);
}
AXNode* AXLiveRegionTracker::GetLiveRootIfNotBusy(const AXNode& node) const {
AXNode* live_root = GetLiveRoot(node);
if (!live_root)
return nullptr;
// Don't return the live root if the live region is busy.
if (live_root->GetBoolAttribute(ax::mojom::BoolAttribute::kBusy))
return nullptr;
return live_root;
}
void AXLiveRegionTracker::WalkTreeAndAssignLiveRootsToNodes(
const AXNode& node,
const AXNode* current_root) {
if (IsLiveRegionRoot(node))
current_root = &node;
if (current_root)
live_region_node_to_root_id_[node.id()] = current_root->id();
for (AXNode* child : node.GetAllChildren())
WalkTreeAndAssignLiveRootsToNodes(*child, current_root);
}
} // namespace ui