1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/accessibility/native_view_accessibility_auralinux.h"
10 #include "base/memory/singleton.h"
11 #include "ui/accessibility/ax_enums.h"
12 #include "ui/accessibility/ax_node_data.h"
13 #include "ui/accessibility/platform/ax_platform_node_auralinux.h"
14 #include "ui/accessibility/platform/ax_platform_node_delegate.h"
15 #include "ui/gfx/native_widget_types.h"
16 #include "ui/views/views_delegate.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/widget/widget_observer.h"
24 // ATK requires that we have a single root "application" object that's the
25 // owner of all other windows. This is a simple class that implements the
26 // AXPlatformNodeDelegate interface so we can create such an application
27 // object. Every time we create an accessibility object for a View, we add its
28 // top-level widget to a vector so we can return the list of all top-level
29 // windows as children of this application object.
30 class AuraLinuxApplication
31 : public ui::AXPlatformNodeDelegate
,
32 public WidgetObserver
{
34 // Get the single instance of this class.
35 static AuraLinuxApplication
* GetInstance() {
36 return Singleton
<AuraLinuxApplication
>::get();
39 // Called every time we create a new accessibility on a View.
40 // Add the top-level widget to our registry so that we can enumerate all
42 void RegisterWidget(Widget
* widget
) {
46 widget
= widget
->GetTopLevelWidget();
47 if (std::find(widgets_
.begin(), widgets_
.end(), widget
) != widgets_
.end())
50 widgets_
.push_back(widget
);
51 widget
->AddObserver(this);
54 gfx::NativeViewAccessible
GetNativeViewAccessible() {
55 return platform_node_
->GetNativeViewAccessible();
59 // WidgetObserver overrides.
62 void OnWidgetDestroying(Widget
* widget
) override
{
63 auto iter
= std::find(widgets_
.begin(), widgets_
.end(), widget
);
64 if (iter
!= widgets_
.end())
69 // ui::AXPlatformNodeDelegate overrides.
72 const ui::AXNodeData
& GetData() override
{
76 gfx::NativeViewAccessible
GetParent() override
{
80 int GetChildCount() override
{
81 return static_cast<int>(widgets_
.size());
84 gfx::NativeViewAccessible
ChildAtIndex(int index
) override
{
85 if (index
< 0 || index
>= GetChildCount())
88 Widget
* widget
= widgets_
[index
];
90 return widget
->GetRootView()->GetNativeViewAccessible();
93 gfx::Vector2d
GetGlobalCoordinateOffset() override
{
94 return gfx::Vector2d();
97 gfx::NativeViewAccessible
HitTestSync(int x
, int y
) override
{
101 gfx::NativeViewAccessible
GetFocus() override
{
105 gfx::AcceleratedWidget
GetTargetForNativeAccessibilityEvent() override
{
106 return gfx::kNullAcceleratedWidget
;
109 void DoDefaultAction() override
{
112 bool SetStringValue(const base::string16
& new_value
) override
{
117 friend struct DefaultSingletonTraits
<AuraLinuxApplication
>;
119 AuraLinuxApplication()
120 : platform_node_(ui::AXPlatformNode::Create(this)) {
121 data_
.role
= ui::AX_ROLE_APPLICATION
;
122 if (ViewsDelegate::GetInstance()) {
123 data_
.AddStringAttribute(
125 ViewsDelegate::GetInstance()->GetApplicationName());
127 ui::AXPlatformNodeAuraLinux::SetApplication(platform_node_
);
128 if (ViewsDelegate::GetInstance()) {
129 // This should be on the a blocking pool thread so that we can open
130 // libatk-bridge.so without blocking this thread.
131 scoped_refptr
<base::TaskRunner
> init_task_runner
=
132 ViewsDelegate::GetInstance()->GetBlockingPoolTaskRunner();
133 if (init_task_runner
)
134 ui::AXPlatformNodeAuraLinux::StaticInitialize(init_task_runner
);
138 ~AuraLinuxApplication() override
{
139 platform_node_
->Destroy();
140 platform_node_
= nullptr;
143 ui::AXPlatformNode
* platform_node_
;
144 ui::AXNodeData data_
;
145 std::vector
<Widget
*> widgets_
;
147 DISALLOW_COPY_AND_ASSIGN(AuraLinuxApplication
);
153 NativeViewAccessibility
* NativeViewAccessibility::Create(View
* view
) {
154 AuraLinuxApplication::GetInstance()->RegisterWidget(view
->GetWidget());
155 return new NativeViewAccessibilityAuraLinux(view
);
158 NativeViewAccessibilityAuraLinux::NativeViewAccessibilityAuraLinux(View
* view
)
159 : NativeViewAccessibility(view
) {
162 NativeViewAccessibilityAuraLinux::~NativeViewAccessibilityAuraLinux() {
165 gfx::NativeViewAccessible
NativeViewAccessibilityAuraLinux::GetParent() {
166 gfx::NativeViewAccessible parent
= NativeViewAccessibility::GetParent();
168 parent
= AuraLinuxApplication::GetInstance()->GetNativeViewAccessible();