| 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" |
| 12 #include "components/exo/shell_surface.h" |
| 13 #include "components/exo/surface.h" |
| 9 #include "components/exo/wm_helper.h" | 14 #include "components/exo/wm_helper.h" |
| 15 #include "ui/aura/client/aura_constants.h" |
| 10 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
| 11 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
| 18 #include "ui/views/view.h" |
| 19 #include "ui/views/widget/widget.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // This class keeps focus on a |ShellSurface| without interfering with default |
| 24 // focus management in |ShellSurface|. For example, touch causes the |
| 25 // |ShellSurface| to lose focus to its ancestor containing View. |
| 26 class FocusStealer : public views::View { |
| 27 public: |
| 28 explicit FocusStealer(int32_t id) : id_(id) { |
| 29 SetFocusBehavior(views::View::FocusBehavior::ALWAYS); |
| 30 set_owned_by_client(); |
| 31 } |
| 32 |
| 33 // views::View overrides. |
| 34 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| 35 node_data->AddIntAttribute(ui::AX_ATTR_CHILD_TREE_ID, id_); |
| 36 } |
| 37 |
| 38 private: |
| 39 int32_t id_; |
| 40 DISALLOW_COPY_AND_ASSIGN(FocusStealer); |
| 41 }; |
| 42 |
| 43 exo::Surface* GetArcSurface(const aura::Window* window) { |
| 44 if (!window) |
| 45 return nullptr; |
| 46 |
| 47 exo::Surface* arc_surface = exo::Surface::AsSurface(window); |
| 48 if (!arc_surface) |
| 49 arc_surface = exo::ShellSurface::GetMainSurface(window); |
| 50 return arc_surface; |
| 51 } |
| 52 |
| 53 void DispatchFocusChange(arc::mojom::AccessibilityNodeInfoData* node_data) { |
| 54 chromeos::AccessibilityManager* accessibility_manager = |
| 55 chromeos::AccessibilityManager::Get(); |
| 56 if (!accessibility_manager) |
| 57 return; |
| 58 |
| 59 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); |
| 60 aura::Window* focused_window = wmHelper->GetFocusedWindow(); |
| 61 if (!focused_window) |
| 62 return; |
| 63 |
| 64 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); |
| 65 |
| 66 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( |
| 67 node_data->boundsInScreen, |
| 68 1.0f / toplevel_window->layer()->device_scale_factor()); |
| 69 |
| 70 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); |
| 71 } |
| 72 |
| 73 } // namespace |
| 12 | 74 |
| 13 namespace arc { | 75 namespace arc { |
| 14 | 76 |
| 15 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( | 77 ArcAccessibilityHelperBridge::ArcAccessibilityHelperBridge( |
| 16 ArcBridgeService* bridge_service) | 78 ArcBridgeService* bridge_service) |
| 17 : ArcService(bridge_service), binding_(this) { | 79 : ArcService(bridge_service), binding_(this) { |
| 18 arc_bridge_service()->accessibility_helper()->AddObserver(this); | 80 arc_bridge_service()->accessibility_helper()->AddObserver(this); |
| 19 } | 81 } |
| 20 | 82 |
| 21 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { | 83 ArcAccessibilityHelperBridge::~ArcAccessibilityHelperBridge() { |
| 22 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); | 84 arc_bridge_service()->accessibility_helper()->RemoveObserver(this); |
| 23 } | 85 } |
| 24 | 86 |
| 25 void ArcAccessibilityHelperBridge::OnInstanceReady() { | 87 void ArcAccessibilityHelperBridge::OnInstanceReady() { |
| 26 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 88 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 27 arc_bridge_service()->accessibility_helper(), Init); | 89 arc_bridge_service()->accessibility_helper(), Init); |
| 28 DCHECK(instance); | 90 DCHECK(instance); |
| 29 instance->Init(binding_.CreateInterfacePtrAndBind()); | 91 instance->Init(binding_.CreateInterfacePtrAndBind()); |
| 92 |
| 93 chromeos::AccessibilityManager* accessibility_manager = |
| 94 chromeos::AccessibilityManager::Get(); |
| 95 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 96 chromeos::switches::kEnableChromeVoxArcSupport)) { |
| 97 instance->SetFilter(arc::mojom::AccessibilityFilterType::ALL); |
| 98 if (!tree_source_) { |
| 99 tree_source_.reset(new AXTreeSourceArc()); |
| 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 } |
| 108 |
| 109 void ArcAccessibilityHelperBridge::OnAccessibilityEventDeprecated( |
| 110 mojom::AccessibilityEventType event_type, |
| 111 mojom::AccessibilityNodeInfoDataPtr event_source) { |
| 112 if (event_type == arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 113 DispatchFocusChange(event_source.get()); |
| 30 } | 114 } |
| 31 | 115 |
| 32 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( | 116 void ArcAccessibilityHelperBridge::OnAccessibilityEvent( |
| 33 mojom::AccessibilityEventType event_type, | 117 mojom::AccessibilityEventDataPtr event_data) { |
| 34 mojom::AccessibilityNodeInfoDataPtr event_source) { | 118 if (tree_source_) { |
| 35 if (event_type != mojom::AccessibilityEventType::VIEW_FOCUSED || | 119 tree_source_->NotifyAccessibilityEvent(event_data.get()); |
| 36 event_source.is_null()) { | |
| 37 return; | 120 return; |
| 38 } | 121 } |
| 39 | 122 |
| 40 chromeos::AccessibilityManager* accessibility_manager = | 123 if (event_data->eventType != arc::mojom::AccessibilityEventType::VIEW_FOCUSED) |
| 41 chromeos::AccessibilityManager::Get(); | 124 return; |
| 42 | 125 |
| 43 if (!accessibility_manager || | 126 CHECK_EQ(1U, event_data.get()->nodeData.size()); |
| 44 !accessibility_manager->IsFocusHighlightEnabled()) { | 127 DispatchFocusChange(event_data.get()->nodeData[0].get()); |
| 128 } |
| 129 |
| 130 void ArcAccessibilityHelperBridge::OnWindowActivated( |
| 131 aura::Window* gained_active, |
| 132 aura::Window* lost_active) { |
| 133 if (gained_active == lost_active || !tree_source_) |
| 45 return; | 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 } |
| 46 } | 148 } |
| 47 | 149 |
| 48 exo::WMHelper* wmHelper = exo::WMHelper::GetInstance(); | 150 if (!active_surface) |
| 49 | |
| 50 aura::Window* focused_window = wmHelper->GetFocusedWindow(); | |
| 51 if (!focused_window) | |
| 52 return; | 151 return; |
| 53 | 152 |
| 54 aura::Window* toplevel_window = focused_window->GetToplevelWindow(); | 153 views::Widget* widget = views::Widget::GetWidgetForNativeView(gained_active); |
| 55 | 154 if (widget && widget->GetContentsView()) { |
| 56 gfx::Rect bounds_in_screen = gfx::ScaleToEnclosingRect( | 155 views::View* view = widget->GetContentsView(); |
| 57 event_source.get()->boundsInScreen, | 156 if (!view->Contains(focus_stealer_.get())) |
| 58 1.0f / toplevel_window->layer()->device_scale_factor()); | 157 view->AddChildView(focus_stealer_.get()); |
| 59 | 158 focus_stealer_->RequestFocus(); |
| 60 accessibility_manager->OnViewFocusedInArc(bounds_in_screen); | 159 view->NotifyAccessibilityEvent(ui::AX_EVENT_CHILDREN_CHANGED, false); |
| 160 } |
| 61 } | 161 } |
| 62 | 162 |
| 63 } // namespace arc | 163 } // namespace arc |
| OLD | NEW |