Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: chrome/browser/chromeos/arc/accessibility/ax_tree_source_arc.cc

Issue 2640123004: Initial support for native accessibility in ARC (Closed)
Patch Set: Experimental changes Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/arc/accessibility/ax_tree_source_arc.h"
6
7 #include "chrome/browser/extensions/api/automation_internal/automation_event_rou ter.h"
8 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
9 #include "chrome/common/extensions/chrome_extension_messages.h"
10 #include "components/exo/shell_surface.h"
11 #include "components/exo/surface.h"
12 #include "ui/accessibility/ax_tree_id_registry.h"
13 #include "ui/aura/window.h"
14
15 namespace {
16
17 exo::Surface* GetArcSurface(const aura::Window* window) {
18 exo::Surface* arc_surface = exo::Surface::AsSurface(window);
19 if (!arc_surface)
20 arc_surface = exo::ShellSurface::GetMainSurface(window);
21 return arc_surface;
22 }
23
24 } // namespace
25
26 namespace arc {
27
28 AXTreeSourceArc::AXTreeSourceArc()
29 : id_(ui::AXTreeIDRegistry::GetInstance()->CreateID()),
30 current_tree_serializer_(new AXTreeArcSerializer(this)) {}
31
32 AXTreeSourceArc::~AXTreeSourceArc() {}
33
34 void AXTreeSourceArc::NotifyAccessibilityEvent(
35 mojom::AccessibilityEventData* event_data) {
36 if (exo::WMHelper::GetInstance())
37 exo::WMHelper::GetInstance()->AddFocusObserver(this);
38
39 tree_map_.clear();
40 parent_map_.clear();
41 for (size_t i = 0; i < event_data->nodeData.size(); ++i) {
42 for (size_t j = 0; j < event_data->nodeData[i]->childIds.size(); ++j)
43 parent_map_[event_data->nodeData[i]->childIds[j]] =
44 event_data->nodeData[i]->id;
45 }
46
47 for (size_t i = 0; i < event_data->nodeData.size(); ++i) {
48 int32_t id = event_data->nodeData[i]->id;
49 tree_map_[id] = event_data->nodeData[i].get();
50 if (parent_map_.find(id) == parent_map_.end())
51 root_ = id;
52 }
53
54 ExtensionMsg_AccessibilityEventParams params;
55 current_tree_serializer_->SerializeChanges(GetFromId(event_data->sourceId),
56 &params.update);
57
58 params.tree_id = id_;
59 params.id = event_data->sourceId;
60 params.event_type = ui::AX_EVENT_FOCUS;
61
62 extensions::AutomationEventRouter* router =
63 extensions::AutomationEventRouter::GetInstance();
64 router->DispatchAccessibilityEvent(params);
65 }
66
67 bool AXTreeSourceArc::GetTreeData(ui::AXTreeData* data) const {
68 data->tree_id = id_;
69 return true;
70 }
71
72 mojom::AccessibilityNodeInfoData* AXTreeSourceArc::GetRoot() const {
73 mojom::AccessibilityNodeInfoData* root = GetFromId(root_);
74 return root;
75 }
76
77 mojom::AccessibilityNodeInfoData* AXTreeSourceArc::GetFromId(int32_t id) const {
78 auto it = tree_map_.find(id);
79 if (it == tree_map_.end())
80 return nullptr;
81 return it->second;
82 }
83
84 int32_t AXTreeSourceArc::GetId(mojom::AccessibilityNodeInfoData* node) const {
85 if (!node)
86 return -11;
87 return node->id;
88 }
89
90 void AXTreeSourceArc::GetChildren(
91 mojom::AccessibilityNodeInfoData* node,
92 std::vector<mojom::AccessibilityNodeInfoData*>* out_children) const {
93 if (!node)
94 return;
95 for (size_t i = 0; i < node->childIds.size(); ++i) {
96 out_children->push_back(GetFromId(node->childIds[i]));
97 }
98 }
99
100 mojom::AccessibilityNodeInfoData* AXTreeSourceArc::GetParent(
101 mojom::AccessibilityNodeInfoData* node) const {
102 if (!node)
103 return nullptr;
104 auto it = parent_map_.find(node->id);
105 if (it != parent_map_.end())
106 return GetFromId(it->second);
107 return nullptr;
108 }
109
110 bool AXTreeSourceArc::IsValid(mojom::AccessibilityNodeInfoData* node) const {
111 return node;
112 }
113
114 bool AXTreeSourceArc::IsEqual(mojom::AccessibilityNodeInfoData* node1,
115 mojom::AccessibilityNodeInfoData* node2) const {
116 if (!node1 || !node2)
117 return false;
118 return node1->id == node2->id;
119 }
120
121 mojom::AccessibilityNodeInfoData* AXTreeSourceArc::GetNull() const {
122 return nullptr;
123 }
124
125 void AXTreeSourceArc::SerializeNode(mojom::AccessibilityNodeInfoData* node,
126 ui::AXNodeData* out_data) const {
127 if (!node)
128 return;
129 out_data->id = node->id;
130 out_data->state = 0;
131 if (!node->text.empty())
132 out_data->SetName(node->text);
133 else if (!node->contentDescription.empty())
134 out_data->SetName(node->contentDescription);
135
136 int32_t id = node->id;
137 if (id == root_)
138 out_data->role = ui::AX_ROLE_ROOT_WEB_AREA;
139 else if (!node->text.empty())
140 out_data->role = ui::AX_ROLE_STATIC_TEXT;
141 else
142 out_data->role = ui::AX_ROLE_DIV;
143 out_data->location.SetRect(node->boundsInScreen.x(), node->boundsInScreen.y(),
144 node->boundsInScreen.width(),
145 node->boundsInScreen.height());
146 }
147
148 void AXTreeSourceArc::Reset() {
149 tree_map_.clear();
150 parent_map_.clear();
151 current_tree_serializer_.reset(new AXTreeArcSerializer(this));
152 extensions::AutomationEventRouter* router =
153 extensions::AutomationEventRouter::GetInstance();
154 router->DispatchTreeDestroyedEvent(id_, nullptr);
155 }
156
157 void AXTreeSourceArc::OnWindowFocused(aura::Window* gained_focus,
158 aura::Window* lost_focus) {
159 exo::Surface* focused_surface = GetArcSurface(gained_focus);
160 if (gained_focus == lost_focus)
161 return;
162
163 exo::Surface* unfocused_surface = GetArcSurface(lost_focus);
164 if (unfocused_surface) {
165 unfocused_surface->UpdateAXTreeID(0);
166 }
167
168 Reset();
169 if (!focused_surface)
170 return;
171
172 focused_surface->UpdateAXTreeID(id_);
173 }
174
175 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698