OLD | NEW |
---|---|
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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | 8 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" |
9 #include "chrome/browser/chromeos/arc/accessibility/ax_tree_source_arc.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chromeos/chromeos_switches.h" | 10 #include "chromeos/chromeos_switches.h" |
11 #include "components/arc/arc_bridge_service.h" | 11 #include "components/arc/arc_bridge_service.h" |
12 #include "components/exo/shell_surface.h" | 12 #include "components/exo/shell_surface.h" |
13 #include "components/exo/surface.h" | 13 #include "components/exo/surface.h" |
14 #include "ui/aura/client/aura_constants.h" | 14 #include "ui/aura/client/aura_constants.h" |
15 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
16 #include "ui/gfx/geometry/rect.h" | 16 #include "ui/gfx/geometry/rect.h" |
17 #include "ui/views/view.h" | |
18 #include "ui/views/widget/widget.h" | |
19 | 17 |
20 namespace { | 18 namespace { |
21 | 19 |
22 // This class keeps focus on a |ShellSurface| without interfering with default | |
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 | |
43 exo::Surface* GetArcSurface(const aura::Window* window) { | 20 exo::Surface* GetArcSurface(const aura::Window* window) { |
44 if (!window) | 21 if (!window) |
45 return nullptr; | 22 return nullptr; |
46 | 23 |
47 exo::Surface* arc_surface = exo::Surface::AsSurface(window); | 24 exo::Surface* arc_surface = exo::Surface::AsSurface(window); |
48 if (!arc_surface) | 25 if (!arc_surface) |
49 arc_surface = exo::ShellSurface::GetMainSurface(window); | 26 arc_surface = exo::ShellSurface::GetMainSurface(window); |
50 return arc_surface; | 27 return arc_surface; |
51 } | 28 } |
52 | 29 |
30 int32_t GetTaskId(aura::Window* window) { | |
31 const std::string arc_app_id = exo::ShellSurface::GetApplicationId(window); | |
32 if (arc_app_id.empty()) | |
33 return -1; | |
34 | |
35 int task_id = -1; | |
36 if (sscanf(arc_app_id.c_str(), "org.chromium.arc.%d", &task_id) != 1) | |
37 return -1; | |
38 | |
39 return task_id; | |
40 } | |
41 | |
53 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { | 42 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { |
54 chromeos::AccessibilityManager* accessibility_manager = | 43 chromeos::AccessibilityManager* accessibility_manager = |
55 chromeos::AccessibilityManager::Get(); | 44 chromeos::AccessibilityManager::Get(); |
56 if (!accessibility_manager) | 45 if (!accessibility_manager) |
57 return; | 46 return; |
58 | 47 |
59 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); | 48 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); |
60 aura::Window* focused_window = wmHelper->GetFocusedWindow(); | 49 aura::Window* focused_window = wmHelper->GetFocusedWindow(); |
61 if (!focused_window) | 50 if (!focused_window) |
62 return; | 51 return; |
63 | 52 |
64 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); | 53 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); |
65 | 54 |
66 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( | 55 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( |
67 node_data->boundsInScreen, | 56 node_data->boundsInScreen, |
68 1.0f / toplevel_window->layer()->device_scale_factor()); | 57 1.0f / toplevel_window->layer()->device_scale_factor()); |
69 | 58 |
70 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); | 59 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); |
71 } | 60 } |
72 | 61 |
62 arc::mojom::AccessibilityFilterType GetFilterType() { | |
63 chromeos::AccessibilityManager* accessibility_manager = | |
64 chromeos::AccessibilityManager::Get(); | |
65 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
66 chromeos::switches::kEnableChromeVoxArcSupport)) | |
67 return arc::mojom::AccessibilityFilterType::ALL; | |
68 | |
69 if (!accessibility_manager) | |
70 return arc::mojom::AccessibilityFilterType::OFF; | |
71 | |
72 if (accessibility_manager->IsSpokenFeedbackEnabled()) | |
73 return arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME; | |
74 | |
75 if (accessibility_manager->IsFocusHighlightEnabled()) | |
76 return arc::mojom::AccessibilityFilterType::FOCUS; | |
77 | |
78 return arc::mojom::AccessibilityFilterType::OFF; | |
79 } | |
80 | |
73 } // namespace | 81 } // namespace |
74 | 82 |
75 namespace arc { | 83 namespace arc { |
76 | 84 |
77 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( | 85 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( |
78 ArcBridgeService* bridge_service) | 86 ArcBridgeService* bridge_service) |
79 : ArcService(bridge_service), binding_(this) { | 87 : ArcService(bridge_service), binding_(this), current_task_id_(0) { |
80 arc_bridge_service()->accessibility_helper()->AddObserver(this); | 88 arc_bridge_service()->accessibility_helper()->AddObserver(this); |
81 } | 89 } |
82 | 90 |
83 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { | 91 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { |
84 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); | 92 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); |
93 ArcAppListPrefs::Get(chromeos::AccessibilityManager::Get()->profile()) | |
94 ->RemoveObserver(this); | |
85 } | 95 } |
86 | 96 |
87 void ArcAccessibilityHelperBridge::OnInstanceReady() { | 97 void ArcAccessibilityHelperBridge::OnInstanceReady() { |
98 ArcAppListPrefs::Get(chromeos::AccessibilityManager::Get()->profile()) | |
99 ->AddObserver(this); | |
88 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 100 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
89 arc_bridge_service()->accessibility_helper(), Init); | 101 arc_bridge_service()->accessibility_helper(), Init); |
90 DCHECK(instance); | 102 DCHECK(instance); |
91 instance->Init(binding_.CreateInterfacePtrAndBind()); | 103 instance->Init(binding_.CreateInterfacePtrAndBind()); |
92 | 104 |
93 chromeos::AccessibilityManager* accessibility_manager = | 105 arc::mojom::AccessibilityFilterType filter_type = GetFilterType(); |
94 chromeos::AccessibilityManager::Get(); | 106 instance->SetFilter(filter_type); |
95 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 107 |
96 chromeos::switches::kEnableChromeVoxArcSupport)) { | 108 if (filter_type == arc::mojom::AccessibilityFilterType::ALL || |
97 instance->SetFilter(arc::mojom::AccessibilityFilterType::ALL); | 109 filter_type == |
98 if (!tree_source_) { | 110 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) |
99 tree_source_.reset(new AXTreeSourceArc(tree_id())); | 111 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 } | |
107 } | 112 } |
108 | 113 |
109 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( | 114 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( |
110 mojom::AccessibilityEventType event_type, | 115 mojom::AccessibilityEventType event_type, |
111 mojom::AccessibilityNodeInfoDataPtr event_source) { | 116 mojom::AccessibilityNodeInfoDataPtr event_source) { |
112 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) | 117 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
113 DispatchFocusChange(event_source.get()); | 118 DispatchFocusChange(event_source.get()); |
114 } | 119 } |
115 | 120 |
116 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( | 121 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( |
117 mojom::AccessibilityEventDataPtr event_data) { | 122 mojom::AccessibilityEventDataPtr event_data) { |
118 if (tree_source_) { | 123 arc::mojom::AccessibilityFilterType filter_type = GetFilterType(); |
119 tree_source_->NotifyAccessibilityEvent(event_data.get()); | 124 |
125 if (filter_type == arc::mojom::AccessibilityFilterType::ALL || | |
126 filter_type == | |
127 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) { | |
128 // Get the task id for this package. | |
129 if (event_data->nodeData.size()) { | |
130 arc::mojom::AccessibilityNodeInfoData* node = | |
131 event_data->nodeData[0].get(); | |
132 if (!node->stringProperties) | |
133 return; | |
134 | |
135 auto it = node->stringProperties->find( | |
136 arc::mojom::AccessibilityStringProperty::PACKAGE_NAME); | |
137 if (it == node->stringProperties->end()) | |
138 return; | |
139 | |
140 auto task_id_it = package_name_to_task_id_.find(it->second); | |
141 if (task_id_it == package_name_to_task_id_.end()) | |
142 return; | |
143 | |
144 int32_t task_id = task_id_it->second; | |
145 | |
146 // Reject updates to non-current task ids. We can do this currently | |
147 // because all events include the entire tree. | |
148 if (task_id != current_task_id_) | |
149 return; | |
150 | |
151 auto tree_it = task_id_to_tree_.find(task_id); | |
152 AXTreeSourceArc* tree_source; | |
153 if (tree_it == task_id_to_tree_.end()) { | |
154 tree_source = new AXTreeSourceArc(this); | |
155 task_id_to_tree_[task_id] = tree_source; | |
156 } else { | |
157 tree_source = tree_it->second; | |
158 } | |
159 tree_source->NotifyAccessibilityEvent(event_data.get()); | |
160 } | |
120 return; | 161 return; |
121 } | 162 } |
122 | 163 |
123 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) | 164 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
124 return; | 165 return; |
125 | 166 |
126 CHECK_EQ(1U, event_data.get()->nodeData.size()); | 167 CHECK_EQ(1U, event_data.get()->nodeData.size()); |
127 DispatchFocusChange(event_data.get()->nodeData[0].get()); | 168 DispatchFocusChange(event_data.get()->nodeData[0].get()); |
128 } | 169 } |
129 | 170 |
130 void ArcAccessibilityHelperBridge::OnWindowActivated( | 171 void ArcAccessibilityHelperBridge::OnAction(const ui::AXActionData& data) { |
131 aura::Window* gained_active, | |
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; | 172 arc::mojom::AccessibilityActionType mojo_action; |
165 switch (data.action) { | 173 switch (data.action) { |
166 case ui::AX_ACTION_DO_DEFAULT: | 174 case ui::AX_ACTION_DO_DEFAULT: |
167 mojo_action = arc::mojom::AccessibilityActionType::CLICK; | 175 mojo_action = arc::mojom::AccessibilityActionType::CLICK; |
168 break; | 176 break; |
169 default: | 177 default: |
170 return; | 178 return; |
171 } | 179 } |
172 | 180 |
173 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 181 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
174 arc_bridge_service()->accessibility_helper(), PerformAction); | 182 arc_bridge_service()->accessibility_helper(), PerformAction); |
175 instance->PerformAction(data.target_node_id, mojo_action); | 183 instance->PerformAction(data.target_node_id, mojo_action); |
176 } | 184 } |
177 | 185 |
186 void ArcAccessibilityHelperBridge::OnWindowActivated( | |
187 aura::Window* gained_active, | |
188 aura::Window* lost_active) { | |
189 if (gained_active == lost_active) | |
190 return; | |
191 | |
192 if (!GetArcSurface(gained_active)) | |
193 return; | |
194 | |
195 // Grab the tree source associated with this app. | |
196 int32_t task_id = GetTaskId(gained_active); | |
197 auto it = task_id_to_tree_.find(task_id); | |
198 if (it != task_id_to_tree_.end()) | |
199 it->second->Focus(gained_active); | |
200 } | |
201 | |
202 void ArcAccessibilityHelperBridge::OnTaskCreated( | |
203 int task_id, | |
204 const std::string& package_name, | |
205 const std::string& activity, | |
206 const std::string& intent) { | |
207 package_name_to_task_id_[package_name] = task_id; | |
yawano
2017/04/25 09:29:23
An app can open multiple activities. One example i
David Tseng
2017/04/25 16:16:54
Yes, this was a concern of mine as well so I pushe
yawano
2017/04/26 08:03:24
I've tested this today, and different task ids are
David Tseng
2017/04/26 15:29:52
Thanks for verifying.
The window id won't help be
| |
208 } | |
209 | |
210 void ArcAccessibilityHelperBridge::OnTaskDestroyed(int task_id) { | |
211 auto it = task_id_to_tree_.find(task_id); | |
212 if (it != task_id_to_tree_.end()) { | |
213 delete it->second; | |
214 task_id_to_tree_.erase(it); | |
215 } | |
216 } | |
217 | |
218 void ArcAccessibilityHelperBridge::OnTaskSetActive(int32_t task_id) { | |
219 current_task_id_ = task_id; | |
220 } | |
221 | |
178 } // namespace arc | 222 } // namespace arc |
OLD | NEW |