Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / accessibility / platform / atk_util_auralinux.cc
blob02563199409a73ee6e061bab124384f911e08de0
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 <atk/atk.h>
6 #if defined(USE_GCONF)
7 #include <gconf/gconf-client.h>
8 #endif
9 #include <glib-2.0/gmodule.h>
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/singleton.h"
14 #include "ui/accessibility/platform/atk_util_auralinux.h"
15 #include "ui/accessibility/platform/ax_platform_node_auralinux.h"
17 namespace {
19 #if defined(USE_GCONF)
21 const char kGnomeAccessibilityEnabledKey[] =
22 "/desktop/gnome/interface/accessibility";
24 bool ShouldEnableAccessibility() {
25 GConfClient* client = gconf_client_get_default();
26 if (!client) {
27 LOG(ERROR) << "gconf_client_get_default failed";
28 return false;
31 GError* error = nullptr;
32 gboolean value = gconf_client_get_bool(client,
33 kGnomeAccessibilityEnabledKey,
34 &error);
35 if (error) {
36 VLOG(1) << "gconf_client_get_bool failed";
37 g_error_free(error);
38 g_object_unref(client);
39 return false;
42 g_object_unref(client);
43 return value;
46 #else // !defined(USE_GCONF)
48 bool ShouldEnableAccessibility() {
49 // TODO(k.czech): implement this for non-GNOME desktops.
50 return false;
53 #endif // defined(USE_GCONF)
55 } // namespace
57 G_BEGIN_DECLS
60 // atk_util_auralinux AtkObject definition and implementation.
63 #define ATK_UTIL_AURALINUX_TYPE (atk_util_auralinux_get_type())
64 #define ATK_UTIL_AURALINUX(obj) \
65 (G_TYPE_CHECK_INSTANCE_CAST((obj), \
66 ATK_UTIL_AURALINUX_TYPE, \
67 AtkUtilAuraLinux))
68 #define ATK_UTIL_AURALINUX_CLASS(klass) \
69 (G_TYPE_CHECK_CLASS_CAST((klass), \
70 ATK_UTIL_AURALINUX_TYPE, \
71 AtkUtilAuraLinuxClass))
72 #define IS_ATK_UTIL_AURALINUX(obj) \
73 (G_TYPE_CHECK_INSTANCE_TYPE((obj), ATK_UTIL_AURALINUX_TYPE))
74 #define IS_ATK_UTIL_AURALINUX_CLASS(klass) \
75 (G_TYPE_CHECK_CLASS_TYPE((klass), ATK_UTIL_AURALINUX_TYPE))
76 #define ATK_UTIL_AURALINUX_GET_CLASS(obj) \
77 (G_TYPE_INSTANCE_GET_CLASS((obj), \
78 ATK_UTIL_AURALINUX_TYPE, \
79 AtkUtilAuraLinuxClass))
81 typedef struct _AtkUtilAuraLinux AtkUtilAuraLinux;
82 typedef struct _AtkUtilAuraLinuxClass AtkUtilAuraLinuxClass;
84 struct _AtkUtilAuraLinux
86 AtkUtil parent;
89 struct _AtkUtilAuraLinuxClass
91 AtkUtilClass parent_class;
94 GType atk_util_auralinux_get_type();
96 G_DEFINE_TYPE(AtkUtilAuraLinux, atk_util_auralinux, ATK_TYPE_UTIL);
98 static void atk_util_auralinux_init(AtkUtilAuraLinux *ax_util) {
101 static AtkObject* atk_util_auralinux_get_root() {
102 ui::AXPlatformNode* application = ui::AXPlatformNodeAuraLinux::application();
103 if (application) {
104 return application->GetNativeViewAccessible();
106 return nullptr;
109 static G_CONST_RETURN gchar* atk_util_auralinux_get_toolkit_name(void) {
110 return "Chromium";
113 static G_CONST_RETURN gchar* atk_util_auralinux_get_toolkit_version(void) {
114 return "1.0";
117 static void atk_util_auralinux_class_init(AtkUtilAuraLinuxClass *klass) {
118 AtkUtilClass *atk_class;
119 gpointer data;
121 data = g_type_class_peek(ATK_TYPE_UTIL);
122 atk_class = ATK_UTIL_CLASS(data);
124 atk_class->get_root = atk_util_auralinux_get_root;
125 atk_class->get_toolkit_name = atk_util_auralinux_get_toolkit_name;
126 atk_class->get_toolkit_version = atk_util_auralinux_get_toolkit_version;
129 G_END_DECLS
132 // AtkUtilAuraLinuxClass implementation.
135 namespace ui {
137 // static
138 AtkUtilAuraLinux* AtkUtilAuraLinux::GetInstance() {
139 return base::Singleton<AtkUtilAuraLinux>::get();
142 AtkUtilAuraLinux::AtkUtilAuraLinux() {
145 void AtkUtilAuraLinux::Initialize(
146 scoped_refptr<base::TaskRunner> /* init_task_runner */) {
147 // TODO(k.czech): use |init_task_runner| to post a task to do the
148 // initialization rather than doing it on this thread.
149 // http://crbug.com/468112
151 // Register our util class.
152 g_type_class_unref(g_type_class_ref(ATK_UTIL_AURALINUX_TYPE));
154 if (!ShouldEnableAccessibility()) {
155 VLOG(1) << "Will not enable ATK accessibility support.";
156 return;
159 VLOG(1) << "Enabling ATK accessibility support.";
161 // Try to load libatk-bridge.so.
162 base::FilePath atk_bridge_path(ATK_LIB_DIR);
163 atk_bridge_path = atk_bridge_path.Append("gtk-2.0/modules/libatk-bridge.so");
164 GModule* bridge = g_module_open(atk_bridge_path.value().c_str(),
165 static_cast<GModuleFlags>(0));
166 if (!bridge) {
167 VLOG(1) << "Unable to open module " << atk_bridge_path.value();
168 return;
171 // Try to call gnome_accessibility_module_init from libatk-bridge.so.
172 void (*gnome_accessibility_module_init)();
173 if (g_module_symbol(bridge, "gnome_accessibility_module_init",
174 (gpointer *)&gnome_accessibility_module_init)) {
175 (*gnome_accessibility_module_init)();
179 AtkUtilAuraLinux::~AtkUtilAuraLinux() {
182 } // namespace ui