2 * Copyright (C) 2011 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "AccessibilityCallbacks.h"
32 #include "AccessibilityController.h"
33 #include "DumpRenderTree.h"
34 #include "WebCoreSupport/DumpRenderTreeSupportGtk.h"
36 #include <webkit/webkit.h>
37 #include <wtf/gobject/GOwnPtr.h>
39 static guint stateChangeListenerId = 0;
40 static guint focusEventListenerId = 0;
41 static guint activeDescendantChangedListenerId = 0;
42 static guint childrenChangedListenerId = 0;
43 static guint propertyChangedListenerId = 0;
44 static guint visibleDataChangedListenerId = 0;
46 static void printAccessibilityEvent(AtkObject* accessible, const gchar* signalName, const gchar* signalValue)
48 // Do not handle state-change:defunct signals, as the AtkObject
49 // associated to them will not be valid at this point already.
50 if (!signalName || !g_strcmp0(signalName, "state-change:defunct"))
53 if (!accessible || !ATK_IS_OBJECT(accessible))
56 const gchar* objectName = atk_object_get_name(accessible);
57 AtkRole objectRole = atk_object_get_role(accessible);
59 // Try to always provide a name to be logged for the object.
60 if (!objectName || *objectName == '\0')
61 objectName = "(No name)";
63 GOwnPtr<gchar> signalNameAndValue(signalValue ? g_strdup_printf("%s = %s", signalName, signalValue) : g_strdup(signalName));
64 printf("Accessibility object emitted \"%s\" / Name: \"%s\" / Role: %d\n",
65 signalNameAndValue.get(), objectName, objectRole);
68 static gboolean axObjectEventListener(GSignalInvocationHint *signalHint,
70 const GValue *paramValues,
73 // At least we should receive the instance emitting the signal.
74 if (numParamValues < 1)
77 AtkObject* accessible = ATK_OBJECT(g_value_get_object(¶mValues[0]));
78 if (!accessible || !ATK_IS_OBJECT(accessible))
81 GSignalQuery signal_query;
82 GOwnPtr<gchar> signalName;
83 GOwnPtr<gchar> signalValue;
85 g_signal_query(signalHint->signal_id, &signal_query);
87 if (!g_strcmp0(signal_query.signal_name, "state-change")) {
88 signalName.set(g_strdup_printf("state-change:%s", g_value_get_string(¶mValues[1])));
89 signalValue.set(g_strdup_printf("%d", g_value_get_boolean(¶mValues[2])));
90 } else if (!g_strcmp0(signal_query.signal_name, "focus-event")) {
91 signalName.set(g_strdup("focus-event"));
92 signalValue.set(g_strdup_printf("%d", g_value_get_boolean(¶mValues[1])));
93 } else if (!g_strcmp0(signal_query.signal_name, "children-changed")) {
94 signalName.set(g_strdup("children-changed"));
95 signalValue.set(g_strdup_printf("%d", g_value_get_uint(¶mValues[1])));
96 } else if (!g_strcmp0(signal_query.signal_name, "property-change")) {
97 signalName.set(g_strdup_printf("property-change:%s", g_quark_to_string(signalHint->detail)));
99 signalName.set(g_strdup(signal_query.signal_name));
101 printAccessibilityEvent(accessible, signalName.get(), signalValue.get());
106 void connectAccessibilityCallbacks()
108 // Ensure no callbacks are connected before.
109 disconnectAccessibilityCallbacks();
111 // Ensure that accessibility is initialized for the WebView by querying for
112 // the root accessible object, which will create the full hierarchy.
113 DumpRenderTreeSupportGtk::getRootAccessibleElement(mainFrame);
115 // Add global listeners for AtkObject's signals.
116 stateChangeListenerId = atk_add_global_event_listener(axObjectEventListener, "Gtk:AtkObject:state-change");
117 focusEventListenerId = atk_add_global_event_listener(axObjectEventListener, "Gtk:AtkObject:focus-event");
118 activeDescendantChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "Gtk:AtkObject:active-descendant-changed");
119 childrenChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "Gtk:AtkObject:children-changed");
120 propertyChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "Gtk:AtkObject:property-change");
121 visibleDataChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "Gtk:AtkObject:visible-data-changed");
123 // Ensure the Atk interface types are registered, otherwise
124 // the AtkDocument signal handlers below won't get registered.
125 GObject* dummyAxObject = G_OBJECT(g_object_new(ATK_TYPE_OBJECT, 0));
126 AtkObject* dummyNoOpAxObject = atk_no_op_object_new(dummyAxObject);
127 g_object_unref(G_OBJECT(dummyNoOpAxObject));
128 g_object_unref(dummyAxObject);
131 void disconnectAccessibilityCallbacks()
133 // AtkObject signals.
134 if (stateChangeListenerId) {
135 atk_remove_global_event_listener(stateChangeListenerId);
136 stateChangeListenerId = 0;
138 if (focusEventListenerId) {
139 atk_remove_global_event_listener(focusEventListenerId);
140 focusEventListenerId = 0;
142 if (activeDescendantChangedListenerId) {
143 atk_remove_global_event_listener(activeDescendantChangedListenerId);
144 activeDescendantChangedListenerId = 0;
146 if (childrenChangedListenerId) {
147 atk_remove_global_event_listener(childrenChangedListenerId);
148 childrenChangedListenerId = 0;
150 if (propertyChangedListenerId) {
151 atk_remove_global_event_listener(propertyChangedListenerId);
152 propertyChangedListenerId = 0;
154 if (visibleDataChangedListenerId) {
155 atk_remove_global_event_listener(visibleDataChangedListenerId);
156 visibleDataChangedListenerId = 0;