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