| 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 "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" |
| 10 #include "chromeos/chromeos_switches.h" |
| 8 #include "components/arc/arc_bridge_service.h" | 11 #include "components/arc/arc_bridge_service.h" |
| 9 #include "components/exo/wm_helper.h" | 12 #include "components/exo/shell_surface.h" |
| 13 #include "components/exo/surface.h" |
| 14 #include "ui/aura/client/aura_constants.h" |
| 10 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 11 #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 |
| 20 namespace { |
| 21 |
| 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 } |
| 36 |
| 37 private: |
| 38 int32_t id_; |
| 39 DISALLOW_COPY_AND_ASSIGN(FocusStealer); |
| 40 }; |
| 41 |
| 42 exo::Surface* GetArcSurface(const aura::Window* window) { |
| 43 if (!window) |
| 44 return nullptr; |
| 45 |
| 46 exo::Surface* arc_surface = exo::Surface::AsSurface(window); |
| 47 if (!arc_surface) |
| 48 arc_surface = exo::ShellSurface::GetMainSurface(window); |
| 49 return arc_surface; |
| 50 } |
| 51 |
| 52 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { |
| 53 chromeos::AccessibilityManager* accessibility_manager = |
| 54 chromeos::AccessibilityManager::Get(); |
| 55 if (!accessibility_manager) |
| 56 return; |
| 57 |
| 58 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); |
| 59 aura::Window* focused_window = wmHelper->GetFocusedWindow(); |
| 60 if (!focused_window) |
| 61 return; |
| 62 |
| 63 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); |
| 64 |
| 65 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( |
| 66 node_data->boundsInScreen, |
| 67 1.0f / toplevel_window->layer()->device_scale_factor()); |
| 68 |
| 69 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); |
| 70 } |
| 71 |
| 72 } // namespace |
| 12 | 73 |
| 13 namespace arc { | 74 namespace arc { |
| 14 | 75 |
| 15 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( | 76 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( |
| 16 ArcBridgeService* bridge_service) | 77 ArcBridgeService* bridge_service) |
| 17 : ArcService(bridge_service), binding_(this) { | 78 : ArcService(bridge_service), binding_(this) { |
| 18 arc_bridge_service()->accessibility_helper()->AddObserver(this); | 79 arc_bridge_service()->accessibility_helper()->AddObserver(this); |
| 19 } | 80 } |
| 20 | 81 |
| 21 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { | 82 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { |
| 22 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); | 83 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); |
| 23 } | 84 } |
| 24 | 85 |
| 25 void ArcAccessibilityHelperBridge::OnInstanceReady() { | 86 void ArcAccessibilityHelperBridge::OnInstanceReady() { |
| 26 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 87 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 27 arc_bridge_service()->accessibility_helper(), Init); | 88 arc_bridge_service()->accessibility_helper(), Init); |
| 28 DCHECK(instance); | 89 DCHECK(instance); |
| 29 instance->Init(binding_.CreateInterfacePtrAndBind()); | 90 instance->Init(binding_.CreateInterfacePtrAndBind()); |
| 91 |
| 92 chromeos::AccessibilityManager* accessibility_manager = |
| 93 chromeos::AccessibilityManager::Get(); |
| 94 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 95 chromeos::switches::kEnableChromeVoxArcSupport)) { |
| 96 instance->SetFilter(arc::mojom::AccessibilityFilterType::ALL); |
| 97 if (!tree_source_) { |
| 98 tree_source_.reset(new AXTreeSourceArc()); |
| 99 focus_stealer_.reset(new FocusStealer(tree_source_->tree_id())); |
| 100 exo::WMHelper::GetInstance()->AddActivationObserver(this); |
| 101 } |
| 102 } else if (accessibility_manager && |
| 103 accessibility_manager->IsFocusHighlightEnabled()) { |
| 104 instance->SetFilter(arc::mojom::AccessibilityFilterType::FOCUS); |
| 105 } |
| 106 } |
| 107 |
| 108 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( |
| 109 mojom::AccessibilityEventType event_type, |
| 110 mojom::AccessibilityNodeInfoDataPtr event_source) { |
| 111 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 112 DispatchFocusChange(event_source.get()); |
| 30 } | 113 } |
| 31 | 114 |
| 32 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( | 115 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( |
| 33 mojom::AccessibilityEventType event_type, | 116 mojom::AccessibilityEventDataPtr event_data) { |
| 34 mojom::AccessibilityNodeInfoDataPtr event_source) { | 117 if (tree_source_) { |
| 35 if (event_type != mojom::AccessibilityEventType::VIEW_FOCUSED || | 118 tree_source_->NotifyAccessibilityEvent(event_data.get()); |
| 36 event_source.is_null()) { | |
| 37 return; | 119 return; |
| 38 } | 120 } |
| 39 | 121 |
| 40 chromeos::AccessibilityManager* accessibility_manager = | 122 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 41 chromeos::AccessibilityManager::Get(); | 123 return; |
| 42 | 124 |
| 43 if (!accessibility_manager || | 125 CHECK_EQ(1U, event_data.get()->nodeData.size()); |
| 44 !accessibility_manager->IsFocusHighlightEnabled()) { | 126 DispatchFocusChange(event_data.get()->nodeData[0].get()); |
| 127 } |
| 128 |
| 129 void ArcAccessibilityHelperBridge::OnWindowActivated( |
| 130 aura::Window* gained_active, |
| 131 aura::Window* lost_active) { |
| 132 if (gained_active == lost_active || !tree_source_) |
| 45 return; | 133 return; |
| 134 |
| 135 exo::Surface* active_surface = GetArcSurface(gained_active); |
| 136 exo::Surface* inactive_surface = GetArcSurface(lost_active); |
| 137 |
| 138 // Detach the accessibility tree from an inactive ShellSurface so that any |
| 139 // client walking the desktop tree gets non-duplicated linearization. |
| 140 if (inactive_surface) { |
| 141 views::Widget* widget = views::Widget::GetWidgetForNativeView(lost_active); |
| 142 if (widget && widget->GetContentsView()) { |
| 143 views::View* view = widget->GetContentsView(); |
| 144 view->RemoveChildView(focus_stealer_.get()); |
| 145 view->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false); |
| 146 } |
| 46 } | 147 } |
| 47 | 148 |
| 48 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); | 149 if (!active_surface) |
| 49 | |
| 50 aura::Window* focused_window = wmHelper->GetFocusedWindow(); | |
| 51 if (!focused_window) | |
| 52 return; | 150 return; |
| 53 | 151 |
| 54 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); | 152 views::Widget* widget = views::Widget::GetWidgetForNativeView(gained_active); |
| 55 | 153 if (widget && widget->GetContentsView()) { |
| 56 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( | 154 views::View* view = widget->GetContentsView(); |
| 57 event_source.get()->boundsInScreen, | 155 if (!view->Contains(focus_stealer_.get())) |
| 58 1.0f / toplevel_window->layer()->device_scale_factor()); | 156 view->AddChildView(focus_stealer_.get()); |
| 59 | 157 focus_stealer_->RequestFocus(); |
| 60 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); | 158 view->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false); |
| 159 } |
| 61 } | 160 } |
| 62 | 161 |
| 63 } // namespace arc | 162 } // namespace arc |
| OLD | NEW |