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 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 | 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 int32_t task_id = kNoTaskId; | |
| 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* wm_helper = exo::WMHelper::GetInstance(); |
| 60 aura::Window* focused_window = wmHelper->GetFocusedWindow(); | 51 if (!wm_helper) |
| 52 return; | |
| 53 | |
| 54 aura::Window* focused_window = wm_helper->GetFocusedWindow(); | |
| 61 if (!focused_window) | 55 if (!focused_window) |
| 62 return; | 56 return; |
| 63 | 57 |
| 64 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); | 58 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); |
| 65 | 59 |
| 66 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( | 60 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( |
| 67 node_data->boundsInScreen, | 61 node_data->boundsInScreen, |
| 68 1.0f / toplevel_window->layer()->device_scale_factor()); | 62 1.0f / toplevel_window->layer()->device_scale_factor()); |
| 69 | 63 |
| 70 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); | 64 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); |
| 71 } | 65 } |
| 72 | 66 |
| 67 arc::mojom::AccessibilityFilterType GetFilterType() { | |
| 68 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 69 chromeos::switches::kEnableChromeVoxArcSupport)) { | |
| 70 return arc::mojom::AccessibilityFilterType::ALL; | |
| 71 } | |
| 72 | |
| 73 chromeos::AccessibilityManager* accessibility_manager = | |
| 74 chromeos::AccessibilityManager::Get(); | |
| 75 if (!accessibility_manager) | |
| 76 return arc::mojom::AccessibilityFilterType::OFF; | |
| 77 | |
| 78 if (accessibility_manager->IsSpokenFeedbackEnabled()) | |
| 79 return arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME; | |
| 80 | |
| 81 if (accessibility_manager->IsFocusHighlightEnabled()) | |
| 82 return arc::mojom::AccessibilityFilterType::FOCUS; | |
| 83 | |
| 84 return arc::mojom::AccessibilityFilterType::OFF; | |
| 85 } | |
| 86 | |
| 73 } // namespace | 87 } // namespace |
| 74 | 88 |
| 75 namespace arc { | 89 namespace arc { |
| 76 | 90 |
| 77 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( | 91 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( |
| 78 ArcBridgeService* bridge_service) | 92 ArcBridgeService* bridge_service) |
| 79 : ArcService(bridge_service), binding_(this) { | 93 : ArcService(bridge_service), binding_(this), current_task_id_(kNoTaskId) { |
| 80 arc_bridge_service()->accessibility_helper()->AddObserver(this); | 94 arc_bridge_service()->accessibility_helper()->AddObserver(this); |
| 81 } | 95 } |
| 82 | 96 |
| 83 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { | 97 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { |
| 84 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); | 98 // We do not unregitser ourselves from WMHelper as an ActivationObserver |
|
David Tseng
2017/05/11 17:20:47
FYI @yawano. In response to your request to do thi
| |
| 99 // because it is always null at this point during teardown. | |
| 100 | |
| 101 chromeos::AccessibilityManager* accessibility_manager = | |
| 102 chromeos::AccessibilityManager::Get(); | |
| 103 if (accessibility_manager) { | |
| 104 Profile* profile = accessibility_manager->profile(); | |
| 105 if (profile && ArcAppListPrefs::Get(profile)) | |
| 106 ArcAppListPrefs::Get(profile)->RemoveObserver(this); | |
| 107 } | |
| 108 | |
| 109 ArcBridgeService* arc_bridge_service_ptr = arc_bridge_service(); | |
| 110 if (arc_bridge_service_ptr) | |
| 111 arc_bridge_service_ptr->accessibility_helper()->RemoveObserver(this); | |
| 85 } | 112 } |
| 86 | 113 |
| 87 void ArcAccessibilityHelperBridge::OnInstanceReady() { | 114 void ArcAccessibilityHelperBridge::OnInstanceReady() { |
| 115 chromeos::AccessibilityManager* accessibility_manager = | |
| 116 chromeos::AccessibilityManager::Get(); | |
| 117 if (!accessibility_manager) | |
| 118 return; | |
| 119 | |
| 120 Profile* profile = accessibility_manager->profile(); | |
| 121 if (!profile) | |
| 122 return; | |
| 123 | |
| 124 ArcAppListPrefs* arc_app_list_prefs = ArcAppListPrefs::Get(profile); | |
| 125 if (!arc_app_list_prefs) | |
| 126 return; | |
| 127 | |
| 128 arc_app_list_prefs->AddObserver(this); | |
| 88 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 129 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 89 arc_bridge_service()->accessibility_helper(), Init); | 130 arc_bridge_service()->accessibility_helper(), Init); |
| 90 DCHECK(instance); | 131 DCHECK(instance); |
| 91 instance->Init(binding_.CreateInterfacePtrAndBind()); | 132 instance->Init(binding_.CreateInterfacePtrAndBind()); |
| 92 | 133 |
| 93 chromeos::AccessibilityManager* accessibility_manager = | 134 arc::mojom::AccessibilityFilterType filter_type = GetFilterType(); |
| 94 chromeos::AccessibilityManager::Get(); | 135 instance->SetFilter(filter_type); |
| 95 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 136 |
| 96 chromeos::switches::kEnableChromeVoxArcSupport)) { | 137 if (filter_type == arc::mojom::AccessibilityFilterType::ALL || |
| 97 instance->SetFilter(arc::mojom::AccessibilityFilterType::ALL); | 138 filter_type == |
| 98 if (!tree_source_) { | 139 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) { |
| 99 tree_source_.reset(new AXTreeSourceArc(tree_id())); | 140 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 } | 141 } |
| 107 } | 142 } |
| 108 | 143 |
| 109 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( | 144 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( |
| 110 mojom::AccessibilityEventType event_type, | 145 mojom::AccessibilityEventType event_type, |
| 111 mojom::AccessibilityNodeInfoDataPtr event_source) { | 146 mojom::AccessibilityNodeInfoDataPtr event_source) { |
| 112 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) | 147 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 113 DispatchFocusChange(event_source.get()); | 148 DispatchFocusChange(event_source.get()); |
| 114 } | 149 } |
| 115 | 150 |
| 116 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( | 151 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( |
| 117 mojom::AccessibilityEventDataPtr event_data) { | 152 mojom::AccessibilityEventDataPtr event_data) { |
| 118 if (tree_source_) { | 153 arc::mojom::AccessibilityFilterType filter_type = GetFilterType(); |
| 119 tree_source_->NotifyAccessibilityEvent(event_data.get()); | 154 |
| 155 if (filter_type == arc::mojom::AccessibilityFilterType::ALL || | |
| 156 filter_type == | |
| 157 arc::mojom::AccessibilityFilterType::WHITELISTED_PACKAGE_NAME) { | |
| 158 // Get the task id for this package which requires inspecting node data. | |
| 159 if (event_data->nodeData.empty()) | |
| 160 return; | |
| 161 | |
| 162 arc::mojom::AccessibilityNodeInfoData* node = event_data->nodeData[0].get(); | |
| 163 if (!node->stringProperties) | |
| 164 return; | |
| 165 | |
| 166 auto package_it = node->stringProperties->find( | |
| 167 arc::mojom::AccessibilityStringProperty::PACKAGE_NAME); | |
| 168 if (package_it == node->stringProperties->end()) | |
| 169 return; | |
| 170 | |
| 171 auto task_ids_it = package_name_to_task_ids_.find(package_it->second); | |
| 172 if (task_ids_it == package_name_to_task_ids_.end()) | |
| 173 return; | |
| 174 | |
| 175 const auto& task_ids = task_ids_it->second; | |
| 176 | |
| 177 // Reject updates to non-current task ids. We can do this currently | |
| 178 // because all events include the entire tree. | |
| 179 if (task_ids.count(current_task_id_) == 0) | |
| 180 return; | |
| 181 | |
| 182 auto tree_it = package_name_to_tree_.find(package_it->second); | |
| 183 AXTreeSourceArc* tree_source; | |
| 184 if (tree_it == package_name_to_tree_.end()) { | |
| 185 package_name_to_tree_[package_it->second].reset( | |
| 186 new AXTreeSourceArc(this)); | |
| 187 tree_source = package_name_to_tree_[package_it->second].get(); | |
| 188 } else { | |
| 189 tree_source = tree_it->second.get(); | |
| 190 } | |
| 191 tree_source->NotifyAccessibilityEvent(event_data.get()); | |
| 120 return; | 192 return; |
| 121 } | 193 } |
| 122 | 194 |
| 123 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) | 195 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 124 return; | 196 return; |
| 125 | 197 |
| 126 CHECK_EQ(1U, event_data.get()->nodeData.size()); | 198 CHECK_EQ(1U, event_data.get()->nodeData.size()); |
| 127 DispatchFocusChange(event_data.get()->nodeData[0].get()); | 199 DispatchFocusChange(event_data.get()->nodeData[0].get()); |
| 128 } | 200 } |
| 129 | 201 |
| 130 void ArcAccessibilityHelperBridge::OnWindowActivated( | 202 void ArcAccessibilityHelperBridge::OnAction( |
| 131 aura::Window* gained_active, | 203 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; | 204 arc::mojom::AccessibilityActionType mojo_action; |
| 165 switch (data.action) { | 205 switch (data.action) { |
| 166 case ui::AX_ACTION_DO_DEFAULT: | 206 case ui::AX_ACTION_DO_DEFAULT: |
| 167 mojo_action = arc::mojom::AccessibilityActionType::CLICK; | 207 mojo_action = arc::mojom::AccessibilityActionType::CLICK; |
| 168 break; | 208 break; |
| 169 default: | 209 default: |
| 170 return; | 210 return; |
| 171 } | 211 } |
| 172 | 212 |
| 173 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 213 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 174 arc_bridge_service()->accessibility_helper(), PerformAction); | 214 arc_bridge_service()->accessibility_helper(), PerformAction); |
| 175 instance->PerformAction(data.target_node_id, mojo_action); | 215 instance->PerformAction(data.target_node_id, mojo_action); |
| 176 } | 216 } |
| 177 | 217 |
| 218 void ArcAccessibilityHelperBridge::OnWindowActivated( | |
| 219 aura::Window* gained_active, | |
| 220 aura::Window* lost_active) { | |
| 221 if (gained_active == lost_active) | |
| 222 return; | |
| 223 | |
| 224 if (!GetArcSurface(gained_active)) | |
| 225 return; | |
| 226 | |
| 227 // Grab the tree source associated with this app. | |
| 228 int32_t task_id = GetTaskId(gained_active); | |
| 229 const std::pair<const std::string, std::set<int32_t>>* found_entry = nullptr; | |
| 230 for (const auto& task_ids_entry : package_name_to_task_ids_) { | |
| 231 if (task_ids_entry.second.count(task_id) != 0) { | |
| 232 found_entry = &task_ids_entry; | |
| 233 break; | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 if (!found_entry) | |
| 238 return; | |
| 239 | |
| 240 auto it = package_name_to_tree_.find(found_entry->first); | |
| 241 if (it != package_name_to_tree_.end()) | |
| 242 it->second->Focus(gained_active); | |
| 243 } | |
| 244 | |
| 245 void ArcAccessibilityHelperBridge::OnTaskCreated( | |
| 246 int32_t task_id, | |
| 247 const std::string& package_name, | |
| 248 const std::string& activity, | |
| 249 const std::string& intent) { | |
| 250 package_name_to_task_ids_[package_name].insert(task_id); | |
| 251 } | |
| 252 | |
| 253 void ArcAccessibilityHelperBridge::OnTaskDestroyed(int32_t task_id) { | |
| 254 for (auto& task_ids_entry : package_name_to_task_ids_) { | |
| 255 if (task_ids_entry.second.erase(task_id) > 0) { | |
| 256 if (task_ids_entry.second.empty()) { | |
| 257 package_name_to_tree_.erase(task_ids_entry.first); | |
| 258 package_name_to_task_ids_.erase(task_ids_entry.first); | |
| 259 } | |
| 260 break; | |
| 261 } | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 void ArcAccessibilityHelperBridge::OnTaskSetActive(int32_t task_id) { | |
| 266 current_task_id_ = task_id; | |
| 267 } | |
| 268 | |
| 178 } // namespace arc | 269 } // namespace arc |
| OLD | NEW |