Chromium Code Reviews| 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 | 20 static int kNoTaskId = -1; |
|
Luis Héctor Chávez
2017/04/26 15:34:59
nit: constexpr int32_t
having something be static
David Tseng
2017/04/28 00:27:44
Done.
| |
| 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 | 21 |
| 43 exo::Surface* GetArcSurface(const aura::Window* window) { | 22 exo::Surface* GetArcSurface(const aura::Window* window) { |
| 44 if (!window) | 23 if (!window) |
| 45 return nullptr; | 24 return nullptr; |
| 46 | 25 |
| 47 exo::Surface* arc_surface = exo::Surface::AsSurface(window); | 26 exo::Surface* arc_surface = exo::Surface::AsSurface(window); |
| 48 if (!arc_surface) | 27 if (!arc_surface) |
| 49 arc_surface = exo::ShellSurface::GetMainSurface(window); | 28 arc_surface = exo::ShellSurface::GetMainSurface(window); |
| 50 return arc_surface; | 29 return arc_surface; |
| 51 } | 30 } |
| 52 | 31 |
| 32 int32_t GetTaskId(aura::Window* window) { | |
| 33 const std::string arc_app_id = exo::ShellSurface::GetApplicationId(window); | |
| 34 if (arc_app_id.empty()) | |
| 35 return kNoTaskId; | |
| 36 | |
| 37 int task_id = kNoTaskId; | |
|
Luis Héctor Chávez
2017/04/26 15:34:59
nit: int32_t
David Tseng
2017/04/28 00:27:43
Done.
| |
| 38 if (sscanf(arc_app_id.c_str(), "org.chromium.arc.%d", &task_id) != 1) | |
| 39 return kNoTaskId; | |
| 40 | |
| 41 return task_id; | |
| 42 } | |
| 43 | |
| 53 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { | 44 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { |
| 54 chromeos::AccessibilityManager* accessibility_manager = | 45 chromeos::AccessibilityManager* accessibility_manager = |
| 55 chromeos::AccessibilityManager::Get(); | 46 chromeos::AccessibilityManager::Get(); |
| 56 if (!accessibility_manager) | 47 if (!accessibility_manager) |
| 57 return; | 48 return; |
| 58 | 49 |
| 59 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); | 50 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); |
| 60 aura::Window* focused_window = wmHelper->GetFocusedWindow(); | 51 aura::Window* focused_window = wmHelper->GetFocusedWindow(); |
| 61 if (!focused_window) | 52 if (!focused_window) |
| 62 return; | 53 return; |
| 63 | 54 |
| 64 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); | 55 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); |
| 65 | 56 |
| 66 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( | 57 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( |
| 67 node_data->boundsInScreen, | 58 node_data->boundsInScreen, |
| 68 1.0f / toplevel_window->layer()->device_scale_factor()); | 59 1.0f / toplevel_window->layer()->device_scale_factor()); |
| 69 | 60 |
| 70 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); | 61 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); |
| 71 } | 62 } |
| 72 | 63 |
| 64 arc::mojom::AccessibilityFilterType GetFilterType() { | |
| 65 chromeos::AccessibilityManager* accessibility_manager = | |
| 66 chromeos::AccessibilityManager::Get(); | |
| 67 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 68 chromeos::switches::kEnableChromeVoxArcSupport)) { | |
| 69 return arc::mojom::AccessibilityFilterType::ALL; | |
| 70 } | |
| 71 | |
| 72 if (!accessibility_manager) | |
| 73 return arc::mojom::AccessibilityFilterType::OFF; | |
| 74 | |
| 75 if (accessibility_manager->IsSpokenFeedbackEnabled()) | |
| 76 return arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME; | |
| 77 | |
| 78 if (accessibility_manager->IsFocusHighlightEnabled()) | |
| 79 return arc::mojom::AccessibilityFilterType::FOCUS; | |
| 80 | |
| 81 return arc::mojom::AccessibilityFilterType::OFF; | |
| 82 } | |
| 83 | |
| 73 } // namespace | 84 } // namespace |
| 74 | 85 |
| 75 namespace arc { | 86 namespace arc { |
| 76 | 87 |
| 77 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( | 88 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( |
| 78 ArcBridgeService* bridge_service) | 89 ArcBridgeService* bridge_service) |
| 79 : ArcService(bridge_service), binding_(this) { | 90 : ArcService(bridge_service), binding_(this), current_task_id_(0) { |
|
yawano
2017/04/27 10:01:27
nit: current_task_id_(kNoTaskId) instead of 0?
David Tseng
2017/04/28 00:27:43
Done.
| |
| 80 arc_bridge_service()->accessibility_helper()->AddObserver(this); | 91 arc_bridge_service()->accessibility_helper()->AddObserver(this); |
| 81 } | 92 } |
| 82 | 93 |
| 83 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { | 94 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { |
| 84 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); | 95 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); |
| 96 ArcAppListPrefs::Get(chromeos::AccessibilityManager::Get()->profile()) | |
| 97 ->RemoveObserver(this); | |
| 85 } | 98 } |
| 86 | 99 |
| 87 void ArcAccessibilityHelperBridge::OnInstanceReady() { | 100 void ArcAccessibilityHelperBridge::OnInstanceReady() { |
| 101 ArcAppListPrefs::Get(chromeos::AccessibilityManager::Get()->profile()) | |
| 102 ->AddObserver(this); | |
| 88 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 103 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 89 arc_bridge_service()->accessibility_helper(), Init); | 104 arc_bridge_service()->accessibility_helper(), Init); |
| 90 DCHECK(instance); | 105 DCHECK(instance); |
| 91 instance->Init(binding_.CreateInterfacePtrAndBind()); | 106 instance->Init(binding_.CreateInterfacePtrAndBind()); |
| 92 | 107 |
| 93 chromeos::AccessibilityManager* accessibility_manager = | 108 arc::mojom::AccessibilityFilterType filter_type = GetFilterType(); |
| 94 chromeos::AccessibilityManager::Get(); | 109 instance->SetFilter(filter_type); |
| 95 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 110 |
| 96 chromeos::switches::kEnableChromeVoxArcSupport)) { | 111 if (filter_type == arc::mojom::AccessibilityFilterType::ALL || |
| 97 instance->SetFilter(arc::mojom::AccessibilityFilterType::ALL); | 112 filter_type == |
| 98 if (!tree_source_) { | 113 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) |
|
hidehiko
2017/04/26 17:13:55
missing brace, for consistency with L67?
David Tseng
2017/04/28 00:27:43
Done.
| |
| 99 tree_source_.reset(new AXTreeSourceArc(tree_id())); | 114 exo::WMHelper::GetInstance()->AddActivationObserver(this); |
|
yawano
2017/04/27 10:01:27
Remove this from activation observer in the destru
David Tseng
2017/04/28 00:27:43
I don't think we can do this without potentially h
yawano
2017/04/28 11:17:52
I might be missing something, but I think RemoveOb
David Tseng
2017/04/28 15:21:17
No, you're right. I was thinking of the case where
| |
| 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 } | 115 } |
| 108 | 116 |
| 109 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( | 117 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( |
| 110 mojom::AccessibilityEventType event_type, | 118 mojom::AccessibilityEventType event_type, |
| 111 mojom::AccessibilityNodeInfoDataPtr event_source) { | 119 mojom::AccessibilityNodeInfoDataPtr event_source) { |
| 112 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) | 120 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 113 DispatchFocusChange(event_source.get()); | 121 DispatchFocusChange(event_source.get()); |
| 114 } | 122 } |
| 115 | 123 |
| 116 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( | 124 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( |
| 117 mojom::AccessibilityEventDataPtr event_data) { | 125 mojom::AccessibilityEventDataPtr event_data) { |
| 118 if (tree_source_) { | 126 arc::mojom::AccessibilityFilterType filter_type = GetFilterType(); |
| 119 tree_source_->NotifyAccessibilityEvent(event_data.get()); | 127 |
| 128 if (filter_type == arc::mojom::AccessibilityFilterType::ALL || | |
| 129 filter_type == | |
| 130 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) { | |
| 131 // Get the task id for this package. | |
| 132 if (event_data->nodeData.size() == 0) | |
| 133 return; | |
| 134 | |
| 135 arc::mojom::AccessibilityNodeInfoData* node = event_data->nodeData[0].get(); | |
| 136 if (!node->stringProperties) | |
| 137 return; | |
| 138 | |
| 139 auto package_it = node->stringProperties->find( | |
| 140 arc::mojom::AccessibilityStringProperty::PACKAGE_NAME); | |
| 141 if (package_it == node->stringProperties->end()) | |
| 142 return; | |
| 143 | |
| 144 auto task_ids_it = package_name_to_task_ids_.find(package_it->second); | |
| 145 if (task_ids_it == package_name_to_task_ids_.end()) | |
| 146 return; | |
| 147 | |
| 148 auto task_ids = task_ids_it->second; | |
|
hidehiko
2017/04/26 17:13:55
this copies whole set, which looks unexpected.
may
David Tseng
2017/04/28 00:27:43
Done.
| |
| 149 | |
| 150 // Reject updates to non-current task ids. We can do this currently | |
| 151 // because all events include the entire tree. | |
| 152 if (task_ids.count(current_task_id_) == 0) | |
| 153 return; | |
| 154 | |
| 155 auto tree_it = package_name_to_tree_.find(package_it->second); | |
| 156 AXTreeSourceArc* tree_source; | |
| 157 if (tree_it == package_name_to_tree_.end()) { | |
| 158 package_name_to_tree_[package_it->second].reset( | |
| 159 new AXTreeSourceArc(this)); | |
| 160 tree_source = package_name_to_tree_[package_it->second].get(); | |
| 161 } else { | |
| 162 tree_source = tree_it->second.get(); | |
| 163 } | |
| 164 tree_source->NotifyAccessibilityEvent(event_data.get()); | |
| 120 return; | 165 return; |
| 121 } | 166 } |
| 122 | 167 |
| 123 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) | 168 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 124 return; | 169 return; |
| 125 | 170 |
| 126 CHECK_EQ(1U, event_data.get()->nodeData.size()); | 171 CHECK_EQ(1U, event_data.get()->nodeData.size()); |
| 127 DispatchFocusChange(event_data.get()->nodeData[0].get()); | 172 DispatchFocusChange(event_data.get()->nodeData[0].get()); |
| 128 } | 173 } |
| 129 | 174 |
| 130 void ArcAccessibilityHelperBridge::OnWindowActivated( | 175 void ArcAccessibilityHelperBridge::OnAction( |
| 131 aura::Window* gained_active, | 176 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; | 177 arc::mojom::AccessibilityActionType mojo_action; |
| 165 switch (data.action) { | 178 switch (data.action) { |
| 166 case ui::AX_ACTION_DO_DEFAULT: | 179 case ui::AX_ACTION_DO_DEFAULT: |
| 167 mojo_action = arc::mojom::AccessibilityActionType::CLICK; | 180 mojo_action = arc::mojom::AccessibilityActionType::CLICK; |
| 168 break; | 181 break; |
| 169 default: | 182 default: |
| 170 return; | 183 return; |
| 171 } | 184 } |
| 172 | 185 |
| 173 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 186 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 174 arc_bridge_service()->accessibility_helper(), PerformAction); | 187 arc_bridge_service()->accessibility_helper(), PerformAction); |
| 175 instance->PerformAction(data.target_node_id, mojo_action); | 188 instance->PerformAction(data.target_node_id, mojo_action); |
| 176 } | 189 } |
| 177 | 190 |
| 191 void ArcAccessibilityHelperBridge::OnWindowActivated( | |
| 192 aura::Window* gained_active, | |
| 193 aura::Window* lost_active) { | |
| 194 if (gained_active == lost_active) | |
| 195 return; | |
| 196 | |
| 197 if (!GetArcSurface(gained_active)) | |
| 198 return; | |
| 199 | |
| 200 // Grab the tree source associated with this app. | |
| 201 int32_t task_id = GetTaskId(gained_active); | |
| 202 std::string package_name; | |
| 203 for (auto task_ids_it : package_name_to_task_ids_) { | |
|
hidehiko
2017/04/26 17:13:55
This copies entry (pair of key/value) for each ele
David Tseng
2017/04/28 00:27:43
Done.
| |
| 204 if (task_ids_it.second.count(task_id) != 0) { | |
| 205 package_name = task_ids_it.first; | |
| 206 break; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 if (package_name.empty()) | |
| 211 return; | |
| 212 | |
| 213 auto it = package_name_to_tree_.find(package_name); | |
| 214 if (it != package_name_to_tree_.end()) | |
| 215 it->second->Focus(gained_active); | |
| 216 } | |
| 217 | |
| 218 void ArcAccessibilityHelperBridge::OnTaskCreated( | |
| 219 int task_id, | |
| 220 const std::string& package_name, | |
| 221 const std::string& activity, | |
| 222 const std::string& intent) { | |
| 223 package_name_to_task_ids_[package_name].insert(task_id); | |
| 224 } | |
| 225 | |
| 226 void ArcAccessibilityHelperBridge::OnTaskDestroyed(int task_id) { | |
| 227 std::string package_name; | |
| 228 for (auto task_ids_it : package_name_to_task_ids_) { | |
|
hidehiko
2017/04/26 17:13:55
Ditto.
David Tseng
2017/04/28 00:27:43
Yes, partly. Definitely not an iterator, but canno
| |
| 229 if (task_ids_it.second.count(task_id)) { | |
| 230 package_name = task_ids_it.first; | |
| 231 task_ids_it.second.erase(task_id); | |
| 232 | |
| 233 if (task_ids_it.second.size() == 0) | |
| 234 package_name_to_task_ids_.erase(task_ids_it.first); | |
|
hidehiko
2017/04/26 17:13:55
erase can take iterator.
David Tseng
2017/04/28 00:27:43
Done.
| |
| 235 | |
| 236 break; | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 if (package_name.empty()) | |
| 241 return; | |
| 242 | |
| 243 auto it = package_name_to_tree_.find(package_name); | |
|
hidehiko
2017/04/26 17:13:55
package_name_to_tree_.erase(package_name);
does w
David Tseng
2017/04/28 00:27:43
True and removed though the subtle issue here was
hidehiko
2017/05/01 05:15:26
Which convention do you mean...? Anyway, commented
| |
| 244 if (it != package_name_to_tree_.end()) { | |
| 245 it->second.reset(); | |
| 246 package_name_to_tree_.erase(it); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 void ArcAccessibilityHelperBridge::OnTaskSetActive(int32_t task_id) { | |
| 251 current_task_id_ = task_id; | |
| 252 } | |
| 253 | |
| 178 } // namespace arc | 254 } // namespace arc |
| OLD | NEW |