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 | |
28 | #include "config.h" |
29 | #include "AccessibilityController.h" |
30 | |
31 | #if HAVE(ACCESSIBILITY) |
32 | |
33 | #include "InjectedBundle.h" |
34 | #include "InjectedBundlePage.h" |
35 | |
36 | #include <WebKit/WKBundlePagePrivate.h> |
37 | #include <atk/atk.h> |
38 | #include <cstdio> |
39 | #include <wtf/glib/GUniquePtr.h> |
40 | #include <wtf/text/StringBuilder.h> |
41 | |
42 | namespace WTR { |
43 | |
44 | void AccessibilityController::resetToConsistentState() |
45 | { |
46 | m_globalNotificationHandler = nullptr; |
47 | } |
48 | |
49 | static AtkObject* childElementById(AtkObject* parent, const char* id) |
50 | { |
51 | if (!ATK_IS_OBJECT(parent)) |
52 | return nullptr; |
53 | |
54 | bool parentFound = false; |
55 | AtkAttributeSet* attributeSet = atk_object_get_attributes(parent); |
56 | for (AtkAttributeSet* attributes = attributeSet; attributes; attributes = attributes->next) { |
57 | AtkAttribute* attribute = static_cast<AtkAttribute*>(attributes->data); |
58 | if (!strcmp(attribute->name, "html-id" )) { |
59 | if (!strcmp(attribute->value, id)) |
60 | parentFound = true; |
61 | break; |
62 | } |
63 | } |
64 | atk_attribute_set_free(attributeSet); |
65 | |
66 | if (parentFound) |
67 | return parent; |
68 | |
69 | int childCount = atk_object_get_n_accessible_children(parent); |
70 | for (int i = 0; i < childCount; i++) { |
71 | AtkObject* result = childElementById(atk_object_ref_accessible_child(parent, i), id); |
72 | if (ATK_IS_OBJECT(result)) |
73 | return result; |
74 | } |
75 | |
76 | return nullptr; |
77 | } |
78 | |
79 | RefPtr<AccessibilityUIElement> AccessibilityController::accessibleElementById(JSStringRef id) |
80 | { |
81 | AtkObject* root = ATK_OBJECT(WKAccessibilityRootObject(InjectedBundle::singleton().page()->page())); |
82 | if (!root) |
83 | return nullptr; |
84 | |
85 | size_t bufferSize = JSStringGetMaximumUTF8CStringSize(id); |
86 | GUniquePtr<gchar> idBuffer(static_cast<gchar*>(g_malloc(bufferSize))); |
87 | JSStringGetUTF8CString(id, idBuffer.get(), bufferSize); |
88 | |
89 | AtkObject* result = childElementById(root, idBuffer.get()); |
90 | if (ATK_IS_OBJECT(result)) |
91 | return AccessibilityUIElement::create(result); |
92 | |
93 | return nullptr; |
94 | } |
95 | |
96 | JSRetainPtr<JSStringRef> AccessibilityController::platformName() |
97 | { |
98 | JSRetainPtr<JSStringRef> platformName(Adopt, JSStringCreateWithUTF8CString("atk" )); |
99 | return platformName; |
100 | } |
101 | |
102 | Ref<AccessibilityUIElement> AccessibilityController::rootElement() |
103 | { |
104 | WKBundlePageRef page = InjectedBundle::singleton().page()->page(); |
105 | void* root = WKAccessibilityRootObject(page); |
106 | |
107 | return AccessibilityUIElement::create(static_cast<AtkObject*>(root)); |
108 | } |
109 | |
110 | Ref<AccessibilityUIElement> AccessibilityController::focusedElement() |
111 | { |
112 | WKBundlePageRef page = InjectedBundle::singleton().page()->page(); |
113 | void* root = WKAccessibilityFocusedObject(page); |
114 | |
115 | return AccessibilityUIElement::create(static_cast<AtkObject*>(root)); |
116 | } |
117 | |
118 | bool AccessibilityController::addNotificationListener(JSValueRef functionCallback) |
119 | { |
120 | if (!functionCallback) |
121 | return false; |
122 | |
123 | // Only one global notification listener. |
124 | if (!m_globalNotificationHandler) |
125 | m_globalNotificationHandler = AccessibilityNotificationHandler::create(); |
126 | m_globalNotificationHandler->setNotificationFunctionCallback(functionCallback); |
127 | |
128 | return true; |
129 | } |
130 | |
131 | bool AccessibilityController::removeNotificationListener() |
132 | { |
133 | // Programmers should not be trying to remove a listener that's already removed. |
134 | ASSERT(m_globalNotificationHandler); |
135 | |
136 | m_globalNotificationHandler = nullptr; |
137 | return false; |
138 | } |
139 | |
140 | } // namespace WTR |
141 | |
142 | #endif // HAVE(ACCESSIBILITY) |
143 | |