AccessibilityControllerAtk.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2012 Igalia S.L.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include "AccessibilityController.h"
  29. #include "InjectedBundle.h"
  30. #include "InjectedBundlePage.h"
  31. #include <WebKit2/WKBundlePagePrivate.h>
  32. #include <atk/atk.h>
  33. #include <cstdio>
  34. #include <wtf/gobject/GOwnPtr.h>
  35. #include <wtf/text/StringBuilder.h>
  36. namespace WTR {
  37. static void printAccessibilityEvent(AtkObject* accessible, const gchar* signalName, const gchar* signalValue)
  38. {
  39. // Do not handle state-change:defunct signals, as the AtkObject
  40. // associated to them will not be valid at this point already.
  41. if (!signalName || !g_strcmp0(signalName, "state-change:defunct"))
  42. return;
  43. if (!accessible || !ATK_IS_OBJECT(accessible))
  44. return;
  45. const gchar* objectName = atk_object_get_name(accessible);
  46. AtkRole objectRole = atk_object_get_role(accessible);
  47. // Try to always provide a name to be logged for the object.
  48. if (!objectName || *objectName == '\0')
  49. objectName = "(No name)";
  50. GOwnPtr<gchar> signalNameAndValue(signalValue ? g_strdup_printf("%s = %s", signalName, signalValue) : g_strdup(signalName));
  51. GOwnPtr<gchar> accessibilityEventString(g_strdup_printf("Accessibility object emitted \"%s\" / Name: \"%s\" / Role: %d\n", signalNameAndValue.get(), objectName, objectRole));
  52. InjectedBundle::shared().outputText(String::fromUTF8(accessibilityEventString.get()));
  53. }
  54. static gboolean axObjectEventListener(GSignalInvocationHint *signalHint, guint numParamValues, const GValue *paramValues, gpointer data)
  55. {
  56. // At least we should receive the instance emitting the signal.
  57. if (numParamValues < 1)
  58. return TRUE;
  59. AtkObject* accessible = ATK_OBJECT(g_value_get_object(&paramValues[0]));
  60. if (!accessible || !ATK_IS_OBJECT(accessible))
  61. return TRUE;
  62. GSignalQuery signalQuery;
  63. GOwnPtr<gchar> signalName;
  64. GOwnPtr<gchar> signalValue;
  65. g_signal_query(signalHint->signal_id, &signalQuery);
  66. if (!g_strcmp0(signalQuery.signal_name, "state-change")) {
  67. signalName.set(g_strdup_printf("state-change:%s", g_value_get_string(&paramValues[1])));
  68. signalValue.set(g_strdup_printf("%d", g_value_get_boolean(&paramValues[2])));
  69. } else if (!g_strcmp0(signalQuery.signal_name, "focus-event")) {
  70. signalName.set(g_strdup("focus-event"));
  71. signalValue.set(g_strdup_printf("%d", g_value_get_boolean(&paramValues[1])));
  72. } else if (!g_strcmp0(signalQuery.signal_name, "children-changed")) {
  73. signalName.set(g_strdup("children-changed"));
  74. signalValue.set(g_strdup_printf("%d", g_value_get_uint(&paramValues[1])));
  75. } else if (!g_strcmp0(signalQuery.signal_name, "property-change"))
  76. signalName.set(g_strdup_printf("property-change:%s", g_quark_to_string(signalHint->detail)));
  77. else
  78. signalName.set(g_strdup(signalQuery.signal_name));
  79. printAccessibilityEvent(accessible, signalName.get(), signalValue.get());
  80. return TRUE;
  81. }
  82. void AccessibilityController::logAccessibilityEvents()
  83. {
  84. // Ensure no callbacks are connected before.
  85. resetToConsistentState();
  86. // Ensure that accessibility is initialized for the WebView by querying for
  87. // the root accessible object, which will create the full hierarchy.
  88. rootElement();
  89. // Add global listeners for AtkObject's signals.
  90. m_stateChangeListenerId = atk_add_global_event_listener(axObjectEventListener, "ATK:AtkObject:state-change");
  91. m_focusEventListenerId = atk_add_global_event_listener(axObjectEventListener, "ATK:AtkObject:focus-event");
  92. m_activeDescendantChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "ATK:AtkObject:active-descendant-changed");
  93. m_childrenChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "ATK:AtkObject:children-changed");
  94. m_propertyChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "ATK:AtkObject:property-change");
  95. m_visibleDataChangedListenerId = atk_add_global_event_listener(axObjectEventListener, "ATK:AtkObject:visible-data-changed");
  96. // Ensure the Atk interface types are registered, otherwise
  97. // the AtkDocument signal handlers below won't get registered.
  98. GObject* dummyAxObject = G_OBJECT(g_object_new(ATK_TYPE_OBJECT, 0));
  99. AtkObject* dummyNoOpAxObject = atk_no_op_object_new(dummyAxObject);
  100. g_object_unref(G_OBJECT(dummyNoOpAxObject));
  101. g_object_unref(dummyAxObject);
  102. }
  103. void AccessibilityController::resetToConsistentState()
  104. {
  105. // AtkObject signals.
  106. if (m_stateChangeListenerId) {
  107. atk_remove_global_event_listener(m_stateChangeListenerId);
  108. m_stateChangeListenerId = 0;
  109. }
  110. if (m_focusEventListenerId) {
  111. atk_remove_global_event_listener(m_focusEventListenerId);
  112. m_focusEventListenerId = 0;
  113. }
  114. if (m_activeDescendantChangedListenerId) {
  115. atk_remove_global_event_listener(m_activeDescendantChangedListenerId);
  116. m_activeDescendantChangedListenerId = 0;
  117. }
  118. if (m_childrenChangedListenerId) {
  119. atk_remove_global_event_listener(m_childrenChangedListenerId);
  120. m_childrenChangedListenerId = 0;
  121. }
  122. if (m_propertyChangedListenerId) {
  123. atk_remove_global_event_listener(m_propertyChangedListenerId);
  124. m_propertyChangedListenerId = 0;
  125. }
  126. if (m_visibleDataChangedListenerId) {
  127. atk_remove_global_event_listener(m_visibleDataChangedListenerId);
  128. m_visibleDataChangedListenerId = 0;
  129. }
  130. }
  131. static AtkObject* childElementById(AtkObject* parent, const char* id)
  132. {
  133. if (!ATK_IS_OBJECT(parent))
  134. return 0;
  135. AtkAttributeSet* attributeSet = atk_object_get_attributes(parent);
  136. for (GSList* attributes = attributeSet; attributes; attributes = attributes->next) {
  137. AtkAttribute* attribute = static_cast<AtkAttribute*>(attributes->data);
  138. if (!strcmp(attribute->name, "html-id")) {
  139. if (!strcmp(attribute->value, id))
  140. return parent;
  141. break;
  142. }
  143. }
  144. int childCount = atk_object_get_n_accessible_children(parent);
  145. for (int i = 0; i < childCount; i++) {
  146. AtkObject* result = childElementById(atk_object_ref_accessible_child(parent, i), id);
  147. if (ATK_IS_OBJECT(result))
  148. return result;
  149. }
  150. return 0;
  151. }
  152. PassRefPtr<AccessibilityUIElement> AccessibilityController::accessibleElementById(JSStringRef id)
  153. {
  154. AtkObject* root = ATK_OBJECT(WKAccessibilityRootObject(InjectedBundle::shared().page()->page()));
  155. if (!root)
  156. return 0;
  157. size_t bufferSize = JSStringGetMaximumUTF8CStringSize(id);
  158. GOwnPtr<gchar> idBuffer(static_cast<gchar*>(g_malloc(bufferSize)));
  159. JSStringGetUTF8CString(id, idBuffer.get(), bufferSize);
  160. AtkObject* result = childElementById(root, idBuffer.get());
  161. if (ATK_IS_OBJECT(result))
  162. return AccessibilityUIElement::create(result);
  163. return 0;
  164. }
  165. PassRefPtr<AccessibilityUIElement> AccessibilityController::rootElement()
  166. {
  167. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  168. void* root = WKAccessibilityRootObject(page);
  169. return AccessibilityUIElement::create(static_cast<AtkObject*>(root));
  170. }
  171. PassRefPtr<AccessibilityUIElement> AccessibilityController::focusedElement()
  172. {
  173. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  174. void* root = WKAccessibilityFocusedObject(page);
  175. return AccessibilityUIElement::create(static_cast<AtkObject*>(root));
  176. }
  177. } // namespace WTR