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

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

Issue 2826423003: Expand Chrome OS ARC support to create one tree source per package (Closed)
Patch Set: Fake out WMHelper Created 3 years, 7 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/arc/accessibility/arc_accessibility_helper_bri dge.h" 5 #include "chrome/browser/chromeos/arc/accessibility/arc_accessibility_helper_bri dge.h"
6 6
7 #include <utility>
8
7 #include "base/command_line.h" 9 #include "base/command_line.h"
8 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" 10 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
9 #include "chrome/browser/chromeos/arc/accessibility/ax_tree_source_arc.h" 11 #include "chrome/browser/profiles/profile.h"
10 #include "chromeos/chromeos_switches.h" 12 #include "chromeos/chromeos_switches.h"
11 #include "components/arc/arc_bridge_service.h" 13 #include "components/arc/arc_bridge_service.h"
12 #include "components/exo/shell_surface.h" 14 #include "components/exo/shell_surface.h"
13 #include "components/exo/surface.h" 15 #include "components/exo/surface.h"
14 #include "ui/aura/client/aura_constants.h" 16 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
16 #include "ui/gfx/geometry/rect.h" 18 #include "ui/gfx/geometry/rect.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/widget.h"
19 19
20 namespace { 20 namespace {
21 21
22 // This class keeps focus on a |ShellSurface| without interfering with default 22 constexpr int32_t kNoTaskId = -1;
23 // focus management in |ShellSurface|. For example, touch causes the
24 // |ShellSurface| to lose focus to its ancestor containing View.
25 class FocusStealer : public views::View {
26 public:
27 explicit FocusStealer(int32_t id) : id_(id) {
28 SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
29 set_owned_by_client();
30 }
31
32 // views::View overrides.
33 void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
34 node_data->AddIntAttribute(ui::AX_ATTR_CHILD_TREE_ID, id_);
35 node_data->role = ui::AX_ROLE_CLIENT;
36 }
37
38 private:
39 int32_t id_;
40 DISALLOW_COPY_AND_ASSIGN(FocusStealer);
41 };
42 23
43 exo::Surface* GetArcSurface(const aura::Window* window) { 24 exo::Surface* GetArcSurface(const aura::Window* window) {
44 if (!window) 25 if (!window)
45 return nullptr; 26 return nullptr;
46 27
47 exo::Surface* arc_surface = exo::Surface::AsSurface(window); 28 exo::Surface* arc_surface = exo::Surface::AsSurface(window);
48 if (!arc_surface) 29 if (!arc_surface)
49 arc_surface = exo::ShellSurface::GetMainSurface(window); 30 arc_surface = exo::ShellSurface::GetMainSurface(window);
50 return arc_surface; 31 return arc_surface;
51 } 32 }
52 33
34 int32_t GetTaskId(aura::Window* window) {
35 const std::string arc_app_id = exo::ShellSurface::GetApplicationId(window);
36 if (arc_app_id.empty())
37 return kNoTaskId;
38
39 int32_t task_id = kNoTaskId;
40 if (sscanf(arc_app_id.c_str(), "org.chromium.arc.%d", &task_id) != 1)
41 return kNoTaskId;
42
43 return task_id;
44 }
45
53 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { 46 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) {
54 chromeos::AccessibilityManager* accessibility_manager = 47 chromeos::AccessibilityManager* accessibility_manager =
55 chromeos::AccessibilityManager::Get(); 48 chromeos::AccessibilityManager::Get();
56 if (!accessibility_manager) 49 if (!accessibility_manager)
57 return; 50 return;
58 51
59 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); 52 exo::WMHelper* wm_helper = exo::WMHelper::GetInstance();
60 aura::Window* focused_window = wmHelper->GetFocusedWindow(); 53 if (!wm_helper)
54 return;
55
56 aura::Window* focused_window = wm_helper->GetFocusedWindow();
61 if (!focused_window) 57 if (!focused_window)
62 return; 58 return;
63 59
64 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); 60 aura::Window* toplevel_window = focused_window->GetToplevelWindow();
65 61
66 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( 62 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect(
67 node_data->boundsInScreen, 63 node_data->boundsInScreen,
68 1.0f / toplevel_window->layer()->device_scale_factor()); 64 1.0f / toplevel_window->layer()->device_scale_factor());
69 65
70 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); 66 accessibility_manager->OnViewFocusedInArc(bounds_in_screen);
71 } 67 }
72 68
69 arc::mojom::AccessibilityFilterType GetFilterType() {
70 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
71 chromeos::switches::kEnableChromeVoxArcSupport)) {
72 return arc::mojom::AccessibilityFilterType::ALL;
73 }
74
75 chromeos::AccessibilityManager* accessibility_manager =
76 chromeos::AccessibilityManager::Get();
77 if (!accessibility_manager)
78 return arc::mojom::AccessibilityFilterType::OFF;
79
80 if (accessibility_manager->IsSpokenFeedbackEnabled())
81 return arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME;
82
83 if (accessibility_manager->IsFocusHighlightEnabled())
84 return arc::mojom::AccessibilityFilterType::FOCUS;
85
86 return arc::mojom::AccessibilityFilterType::OFF;
87 }
88
73 } // namespace 89 } // namespace
74 90
75 namespace arc { 91 namespace arc {
76 92
77 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( 93 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge(
78 ArcBridgeService* bridge_service) 94 ArcBridgeService* bridge_service)
79 : ArcService(bridge_service), binding_(this) { 95 : ArcService(bridge_service), binding_(this), current_task_id_(kNoTaskId) {
80 arc_bridge_service()->accessibility_helper()->AddObserver(this); 96 arc_bridge_service()->accessibility_helper()->AddObserver(this);
81 } 97 }
82 98
83 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { 99 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() {
84 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); 100 // We do not unregister ourselves from WMHelper as an ActivationObserver
101 // because it is always null at this point during teardown.
102
103 chromeos::AccessibilityManager* accessibility_manager =
104 chromeos::AccessibilityManager::Get();
105 if (accessibility_manager) {
106 Profile* profile = accessibility_manager->profile();
107 if (profile && ArcAppListPrefs::Get(profile))
108 ArcAppListPrefs::Get(profile)->RemoveObserver(this);
109 }
110
111 ArcBridgeService* arc_bridge_service_ptr = arc_bridge_service();
112 if (arc_bridge_service_ptr)
113 arc_bridge_service_ptr->accessibility_helper()->RemoveObserver(this);
85 } 114 }
86 115
87 void ArcAccessibilityHelperBridge::OnInstanceReady() { 116 void ArcAccessibilityHelperBridge::OnInstanceReady() {
117 chromeos::AccessibilityManager* accessibility_manager =
118 chromeos::AccessibilityManager::Get();
119 if (!accessibility_manager)
120 return;
121
122 Profile* profile = accessibility_manager->profile();
123 if (!profile)
124 return;
125
126 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile);
127 if (!arc_app_list_prefs)
128 return;
129
130 arc_app_list_prefs->AddObserver(this);
88 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( 131 auto* instance = ARC_GET_INSTANCE_FOR_METHOD(
89 arc_bridge_service()->accessibility_helper(), Init); 132 arc_bridge_service()->accessibility_helper(), Init);
90 DCHECK(instance); 133 DCHECK(instance);
91 instance->Init(binding_.CreateInterfacePtrAndBind()); 134 instance->Init(binding_.CreateInterfacePtrAndBind());
92 135
93 chromeos::AccessibilityManager* accessibility_manager = 136 arc::mojom::AccessibilityFilterType filter_type = GetFilterType();
94 chromeos::AccessibilityManager::Get(); 137 instance->SetFilter(filter_type);
95 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 138
96 chromeos::switches::kEnableChromeVoxArcSupport)) { 139 if (filter_type == arc::mojom::AccessibilityFilterType::ALL ||
97 instance->SetFilter(arc::mojom::AccessibilityFilterType::ALL); 140 filter_type ==
98 if (!tree_source_) { 141 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) {
99 tree_source_.reset(new AXTreeSourceArc(tree_id())); 142 exo::WMHelper::GetInstance()->AddActivationObserver(this);
100 focus_stealer_.reset(new FocusStealer(tree_source_->tree_id()));
101 exo::WMHelper::GetInstance()->AddActivationObserver(this);
102 }
103 } else if (accessibility_manager &&
104 accessibility_manager->IsFocusHighlightEnabled()) {
105 instance->SetFilter(arc::mojom::AccessibilityFilterType::FOCUS);
106 } 143 }
107 } 144 }
108 145
109 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( 146 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated(
110 mojom::AccessibilityEventType event_type, 147 mojom::AccessibilityEventType event_type,
111 mojom::AccessibilityNodeInfoDataPtr event_source) { 148 mojom::AccessibilityNodeInfoDataPtr event_source) {
112 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) 149 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED)
113 DispatchFocusChange(event_source.get()); 150 DispatchFocusChange(event_source.get());
114 } 151 }
115 152
116 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( 153 void ArcAccessibilityHelperBridge::OnAccessibilityEvent(
117 mojom::AccessibilityEventDataPtr event_data) { 154 mojom::AccessibilityEventDataPtr event_data) {
118 if (tree_source_) { 155 arc::mojom::AccessibilityFilterType filter_type = GetFilterType();
119 tree_source_->NotifyAccessibilityEvent(event_data.get()); 156
157 if (filter_type == arc::mojom::AccessibilityFilterType::ALL ||
158 filter_type ==
159 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) {
160 // Get the task id for this package which requires inspecting node data.
161 if (event_data->nodeData.empty())
162 return;
163
164 arc::mojom::AccessibilityNodeInfoData* node = event_data->nodeData[0].get();
165 if (!node->stringProperties)
166 return;
167
168 auto package_it = node->stringProperties->find(
169 arc::mojom::AccessibilityStringProperty::PACKAGE_NAME);
170 if (package_it == node->stringProperties->end())
171 return;
172
173 auto task_ids_it = package_name_to_task_ids_.find(package_it->second);
174 if (task_ids_it == package_name_to_task_ids_.end())
175 return;
176
177 const auto& task_ids = task_ids_it->second;
178
179 // Reject updates to non-current task ids. We can do this currently
180 // because all events include the entire tree.
181 if (task_ids.count(current_task_id_) == 0)
182 return;
183
184 auto tree_it = package_name_to_tree_.find(package_it->second);
185 AXTreeSourceArc* tree_source;
186 if (tree_it == package_name_to_tree_.end()) {
187 package_name_to_tree_[package_it->second].reset(
188 new AXTreeSourceArc(this));
189 tree_source = package_name_to_tree_[package_it->second].get();
190 } else {
191 tree_source = tree_it->second.get();
192 }
193 tree_source->NotifyAccessibilityEvent(event_data.get());
120 return; 194 return;
121 } 195 }
122 196
123 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) 197 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED)
124 return; 198 return;
125 199
126 CHECK_EQ(1U, event_data.get()->nodeData.size()); 200 CHECK_EQ(1U, event_data.get()->nodeData.size());
127 DispatchFocusChange(event_data.get()->nodeData[0].get()); 201 DispatchFocusChange(event_data.get()->nodeData[0].get());
128 } 202 }
129 203
130 void ArcAccessibilityHelperBridge::OnWindowActivated( 204 void ArcAccessibilityHelperBridge::OnAction(
131 aura::Window* gained_active, 205 const ui::AXActionData& data) const {
132 aura::Window* lost_active) {
133 if (gained_active == lost_active || !tree_source_)
134 return;
135
136 exo::Surface* active_surface = GetArcSurface(gained_active);
137 exo::Surface* inactive_surface = GetArcSurface(lost_active);
138
139 // Detach the accessibility tree from an inactive ShellSurface so that any
140 // client walking the desktop tree gets non-duplicated linearization.
141 if (inactive_surface) {
142 views::Widget* widget = views::Widget::GetWidgetForNativeView(lost_active);
143 if (widget && widget->GetContentsView()) {
144 views::View* view = widget->GetContentsView();
145 view->RemoveChildView(focus_stealer_.get());
146 view->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false);
147 }
148 }
149
150 if (!active_surface)
151 return;
152
153 views::Widget* widget = views::Widget::GetWidgetForNativeView(gained_active);
154 if (widget && widget->GetContentsView()) {
155 views::View* view = widget->GetContentsView();
156 if (!view->Contains(focus_stealer_.get()))
157 view->AddChildView(focus_stealer_.get());
158 focus_stealer_->RequestFocus();
159 view->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false);
160 }
161 }
162
163 void ArcAccessibilityHelperBridge::PerformAction(const ui::AXActionData& data) {
164 arc::mojom::AccessibilityActionType mojo_action; 206 arc::mojom::AccessibilityActionType mojo_action;
165 switch (data.action) { 207 switch (data.action) {
166 case ui::AX_ACTION_DO_DEFAULT: 208 case ui::AX_ACTION_DO_DEFAULT:
167 mojo_action = arc::mojom::AccessibilityActionType::CLICK; 209 mojo_action = arc::mojom::AccessibilityActionType::CLICK;
168 break; 210 break;
169 default: 211 default:
170 return; 212 return;
171 } 213 }
172 214
173 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( 215 auto* instance = ARC_GET_INSTANCE_FOR_METHOD(
174 arc_bridge_service()->accessibility_helper(), PerformAction); 216 arc_bridge_service()->accessibility_helper(), PerformAction);
175 instance->PerformAction(data.target_node_id, mojo_action); 217 instance->PerformAction(data.target_node_id, mojo_action);
176 } 218 }
177 219
220 void ArcAccessibilityHelperBridge::OnWindowActivated(
221 aura::Window* gained_active,
222 aura::Window* lost_active) {
223 if (gained_active == lost_active)
224 return;
225
226 if (!GetArcSurface(gained_active))
227 return;
228
229 // Grab the tree source associated with this app.
230 int32_t task_id = GetTaskId(gained_active);
231 const std::pair<const std::string, std::set<int32_t>>* found_entry = nullptr;
232 for (const auto& task_ids_entry : package_name_to_task_ids_) {
233 if (task_ids_entry.second.count(task_id) != 0) {
234 found_entry = &task_ids_entry;
235 break;
236 }
237 }
238
239 if (!found_entry)
240 return;
241
242 auto it = package_name_to_tree_.find(found_entry->first);
243 if (it != package_name_to_tree_.end())
244 it->second->Focus(gained_active);
245 }
246
247 void ArcAccessibilityHelperBridge::OnTaskCreated(
248 int32_t task_id,
249 const std::string& package_name,
250 const std::string& activity,
251 const std::string& intent) {
252 package_name_to_task_ids_[package_name].insert(task_id);
253 }
254
255 void ArcAccessibilityHelperBridge::OnTaskDestroyed(int32_t task_id) {
256 for (auto& task_ids_entry : package_name_to_task_ids_) {
257 if (task_ids_entry.second.erase(task_id) > 0) {
258 if (task_ids_entry.second.empty()) {
259 package_name_to_tree_.erase(task_ids_entry.first);
260 package_name_to_task_ids_.erase(task_ids_entry.first);
261 }
262 break;
263 }
264 }
265 }
266
267 void ArcAccessibilityHelperBridge::OnTaskSetActive(int32_t task_id) {
268 current_task_id_ = task_id;
269 }
270
178 } // namespace arc 271 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698