Title: [286059] trunk
Revision
286059
Author
[email protected]
Date
2021-11-19 06:51:09 -0800 (Fri, 19 Nov 2021)

Log Message

[GTK][a11y] Add implementation of document interface when building with ATSPI
https://bugs.webkit.org/show_bug.cgi?id=232755

Reviewed by Adrian Perez de Castro.

Source/WebCore:

* SourcesGTK.txt:
* accessibility/atspi/AccessibilityObjectAtspi.cpp:
(WebCore::AccessibilityObjectAtspi::interfacesForObject):
(WebCore::AccessibilityObjectAtspi::path):
(WebCore::AccessibilityObjectAtspi::locale const):
(WebCore::AccessibilityObjectAtspi::buildInterfaces const):
* accessibility/atspi/AccessibilityObjectAtspi.h:
* accessibility/atspi/AccessibilityObjectDocumentAtspi.cpp: Added.
(WebCore::AccessibilityObjectAtspi::documentAttribute const):
(WebCore::AccessibilityObjectAtspi::documentAttributes const):
(WebCore::AccessibilityObjectAtspi::documentLocale const):

Tools:

Add unit test for the document interface and WTR implementation.

* TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
(testDocumentBasic):
(beforeAll):
* WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp:
(WTR::AccessibilityUIElement::language):
(WTR::AccessibilityUIElement::documentEncoding):
(WTR::AccessibilityUIElement::documentURI):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (286058 => 286059)


--- trunk/Source/WebCore/ChangeLog	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Source/WebCore/ChangeLog	2021-11-19 14:51:09 UTC (rev 286059)
@@ -1,3 +1,22 @@
+2021-11-19  Carlos Garcia Campos  <[email protected]>
+
+        [GTK][a11y] Add implementation of document interface when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=232755
+
+        Reviewed by Adrian Perez de Castro.
+
+        * SourcesGTK.txt:
+        * accessibility/atspi/AccessibilityObjectAtspi.cpp:
+        (WebCore::AccessibilityObjectAtspi::interfacesForObject):
+        (WebCore::AccessibilityObjectAtspi::path):
+        (WebCore::AccessibilityObjectAtspi::locale const):
+        (WebCore::AccessibilityObjectAtspi::buildInterfaces const):
+        * accessibility/atspi/AccessibilityObjectAtspi.h:
+        * accessibility/atspi/AccessibilityObjectDocumentAtspi.cpp: Added.
+        (WebCore::AccessibilityObjectAtspi::documentAttribute const):
+        (WebCore::AccessibilityObjectAtspi::documentAttributes const):
+        (WebCore::AccessibilityObjectAtspi::documentLocale const):
+
 2021-11-19  Antti Koivisto  <[email protected]>
 
         Factor child change invalidation into class

Modified: trunk/Source/WebCore/SourcesGTK.txt (286058 => 286059)


--- trunk/Source/WebCore/SourcesGTK.txt	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Source/WebCore/SourcesGTK.txt	2021-11-19 14:51:09 UTC (rev 286059)
@@ -43,6 +43,7 @@
 accessibility/atspi/AccessibilityObjectAtspi.cpp
 accessibility/atspi/AccessibilityObjectActionAtspi.cpp
 accessibility/atspi/AccessibilityObjectComponentAtspi.cpp
+accessibility/atspi/AccessibilityObjectDocumentAtspi.cpp
 accessibility/atspi/AccessibilityObjectHyperlinkAtspi.cpp
 accessibility/atspi/AccessibilityObjectHypertextAtspi.cpp
 accessibility/atspi/AccessibilityObjectTextAtspi.cpp

Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp (286058 => 286059)


--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.cpp	2021-11-19 14:51:09 UTC (rev 286059)
@@ -91,6 +91,9 @@
     if (coreObject.isLink() || (isRendererReplacedElement(renderer)))
         interfaces.add(Interface::Hyperlink);
 
+    if (coreObject.roleValue() == AccessibilityRole::WebArea)
+        interfaces.add(Interface::Document);
+
     return interfaces;
 }
 
@@ -445,7 +448,7 @@
         if (!g_strcmp0(propertyName, "Description"))
             return g_variant_new_string(atspiObject->description().data());
         if (!g_strcmp0(propertyName, "Locale"))
-            return g_variant_new_string(setlocale(LC_MESSAGES, nullptr));
+            return g_variant_new_string(atspiObject->locale().utf8().data());
         if (!g_strcmp0(propertyName, "AccessibleId"))
             return g_variant_new_string(atspiObject->m_axObject ? String::number(atspiObject->m_axObject->objectID().toUInt64()).utf8().data() : "");
         if (!g_strcmp0(propertyName, "Parent"))
@@ -485,6 +488,8 @@
             interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_hypertext_interface), &s_hypertextFunctions });
         if (m_interfaces.contains(Interface::Action))
             interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_action_interface), &s_actionFunctions });
+        if (m_interfaces.contains(Interface::Document))
+            interfaces.append({ const_cast<GDBusInterfaceInfo*>(&webkit_document_interface), &s_documentFunctions });
         m_path = atspiRoot->atspi().registerObject(*this, WTFMove(interfaces));
     }
 
@@ -695,6 +700,12 @@
     return "";
 }
 
+String AccessibilityObjectAtspi::locale() const
+{
+    auto* axObject = isMainThread() ? m_coreObject : m_axObject;
+    return axObject ? axObject->language() : String();
+}
+
 static bool shouldIncludeOrientationState(const AXCoreObject& coreObject)
 {
     return coreObject.isComboBox()
@@ -1119,6 +1130,8 @@
         g_variant_builder_add(builder, "s", webkit_hypertext_interface.name);
     if (m_interfaces.contains(Interface::Action))
         g_variant_builder_add(builder, "s", webkit_action_interface.name);
+    if (m_interfaces.contains(Interface::Document))
+        g_variant_builder_add(builder, "s", webkit_document_interface.name);
 }
 
 void AccessibilityObjectAtspi::serialize(GVariantBuilder* builder) const

Modified: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h (286058 => 286059)


--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectAtspi.h	2021-11-19 14:51:09 UTC (rev 286059)
@@ -49,7 +49,8 @@
         Value = 1 << 3,
         Hyperlink = 1 << 4,
         Hypertext = 1 << 5,
-        Action = 1 << 6
+        Action = 1 << 6,
+        Document = 1 << 7
     };
     const OptionSet<Interface>& interfaces() const { return m_interfaces; }
 
@@ -74,6 +75,7 @@
     WEBCORE_EXPORT String id() const;
     WEBCORE_EXPORT CString name() const;
     WEBCORE_EXPORT CString description() const;
+    WEBCORE_EXPORT String locale() const;
     WEBCORE_EXPORT unsigned role() const;
     WEBCORE_EXPORT unsigned childCount() const;
     WEBCORE_EXPORT Vector<RefPtr<AccessibilityObjectAtspi>> children() const;
@@ -126,6 +128,8 @@
     WEBCORE_EXPORT String actionName() const;
     WEBCORE_EXPORT bool doAction() const;
 
+    WEBCORE_EXPORT String documentAttribute(const String&) const;
+
 private:
     explicit AccessibilityObjectAtspi(AXCoreObject*);
 
@@ -168,6 +172,9 @@
     String localizedActionName() const;
     String actionKeyBinding() const;
 
+    HashMap<String, String> documentAttributes() const;
+    String documentLocale() const;
+
     static OptionSet<Interface> interfacesForObject(AXCoreObject&);
 
     static GDBusInterfaceVTable s_accessibleFunctions;
@@ -177,6 +184,7 @@
     static GDBusInterfaceVTable s_hyperlinkFunctions;
     static GDBusInterfaceVTable s_hypertextFunctions;
     static GDBusInterfaceVTable s_actionFunctions;
+    static GDBusInterfaceVTable s_documentFunctions;
 
     AXCoreObject* m_axObject { nullptr };
     AXCoreObject* m_coreObject { nullptr };

Added: trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectDocumentAtspi.cpp (0 => 286059)


--- trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectDocumentAtspi.cpp	                        (rev 0)
+++ trunk/Source/WebCore/accessibility/atspi/AccessibilityObjectDocumentAtspi.cpp	2021-11-19 14:51:09 UTC (rev 286059)
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2021 Igalia S.L.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+#include "AccessibilityObjectAtspi.h"
+
+#if ENABLE(ACCESSIBILITY) && USE(ATSPI)
+
+#include "Document.h"
+#include "DocumentInlines.h"
+#include "DocumentType.h"
+#include <gio/gio.h>
+#include <wtf/HashMap.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+GDBusInterfaceVTable AccessibilityObjectAtspi::s_documentFunctions = {
+    // method_call
+    [](GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData) {
+        RELEASE_ASSERT(!isMainThread());
+        auto atspiObject = Ref { *static_cast<AccessibilityObjectAtspi*>(userData) };
+        atspiObject->updateBackingStore();
+
+        if (!g_strcmp0(methodName, "GetAttributeValue")) {
+            const char* name;
+            g_variant_get(parameters, "(&s)", &name);
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", atspiObject->documentAttribute(String::fromUTF8(name)).utf8().data()));
+        } else if (!g_strcmp0(methodName, "GetAttributes")) {
+            GVariantBuilder builder = G_VARIANT_BUILDER_INIT(G_VARIANT_TYPE("(a{ss})"));
+            g_variant_builder_open(&builder, G_VARIANT_TYPE("a{ss}"));
+            auto attributes = atspiObject->documentAttributes();
+            for (const auto& it : attributes)
+                g_variant_builder_add(&builder, "{ss}", it.key.utf8().data(), it.value.utf8().data());
+            g_variant_builder_close(&builder);
+            g_dbus_method_invocation_return_value(invocation, g_variant_builder_end(&builder));
+        } else if (!g_strcmp0(methodName, "GetLocale"))
+            g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", atspiObject->documentLocale().utf8().data()));
+    },
+    // get_property
+    [](GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* propertyName, GError** error, gpointer userData) -> GVariant* {
+        RELEASE_ASSERT(!isMainThread());
+        auto atspiObject = Ref { *static_cast<AccessibilityObjectAtspi*>(userData) };
+        atspiObject->updateBackingStore();
+
+        if (!g_strcmp0(propertyName, "CurrentPageNumber"))
+            return g_variant_new_int32(-1);
+        if (!g_strcmp0(propertyName, "PageCount"))
+            return g_variant_new_int32(-1);
+
+        g_set_error(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Unknown property '%s'", propertyName);
+        return nullptr;
+    },
+    // set_property,
+    nullptr,
+    // padding
+    nullptr
+};
+
+String AccessibilityObjectAtspi::documentAttribute(const String& name) const
+{
+    return Accessibility::retrieveValueFromMainThread<String>([this, name = name.isolatedCopy()]() -> String {
+        if (m_coreObject)
+            m_coreObject->updateBackingStore();
+
+        if (!m_coreObject)
+            return { };
+
+        auto* document = m_coreObject->document();
+        if (!document)
+            return { };
+
+        if (name == "DocType"_s)
+            return document->doctype() ? document->doctype()->name().isolatedCopy() : String();
+        if (name == "Encoding"_s)
+            return document->charset().isolatedCopy();
+        if (name == "URI"_s)
+            return document->documentURI().isolatedCopy();
+        if (name == "MimeType"_s)
+            return document->contentType().isolatedCopy();
+        if (name == "Title"_s)
+            return document->title().isolatedCopy();
+
+        return { };
+    });
+}
+
+HashMap<String, String> AccessibilityObjectAtspi::documentAttributes() const
+{
+    return Accessibility::retrieveValueFromMainThread<HashMap<String, String>>([this]() -> HashMap<String, String> {
+        HashMap<String, String> map;
+        if (m_coreObject)
+            m_coreObject->updateBackingStore();
+
+        if (!m_coreObject)
+            return map;
+
+        auto* document = m_coreObject->document();
+        if (!document)
+            return map;
+
+        if (auto* doctype = document->doctype())
+            map.add("DocType"_s, doctype->name());
+
+        auto charset = document->charset();
+        if (!charset.isEmpty())
+            map.add("Encoding"_s, WTFMove(charset));
+
+        auto uri = document->documentURI();
+        if (!uri.isEmpty())
+            map.add("URI"_s, WTFMove(uri));
+
+        auto contentType = document->contentType();
+        if (!contentType.isEmpty())
+            map.add("MimeType"_s, WTFMove(contentType));
+
+        const auto& title = document->title();
+        if (!title.isEmpty())
+            map.add("Title"_s, title);
+
+        return map;
+    });
+}
+
+String AccessibilityObjectAtspi::documentLocale() const
+{
+    return Accessibility::retrieveValueFromMainThread<String>([this]() -> String {
+        if (m_coreObject)
+            m_coreObject->updateBackingStore();
+
+        if (!m_coreObject)
+            return { };
+
+        auto* document = m_coreObject->document();
+        if (!document)
+            return { };
+
+        return document->contentLanguage().isolatedCopy();
+    });
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(ACCESSIBILITY) && USE(ATSPI)

Modified: trunk/Tools/ChangeLog (286058 => 286059)


--- trunk/Tools/ChangeLog	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Tools/ChangeLog	2021-11-19 14:51:09 UTC (rev 286059)
@@ -1,5 +1,22 @@
 2021-11-19  Carlos Garcia Campos  <[email protected]>
 
+        [GTK][a11y] Add implementation of document interface when building with ATSPI
+        https://bugs.webkit.org/show_bug.cgi?id=232755
+
+        Reviewed by Adrian Perez de Castro.
+
+        Add unit test for the document interface and WTR implementation.
+
+        * TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp:
+        (testDocumentBasic):
+        (beforeAll):
+        * WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp:
+        (WTR::AccessibilityUIElement::language):
+        (WTR::AccessibilityUIElement::documentEncoding):
+        (WTR::AccessibilityUIElement::documentURI):
+
+2021-11-19  Carlos Garcia Campos  <[email protected]>
+
         [GTK][a11y] Add implementation of action interface when building with ATSPI
         https://bugs.webkit.org/show_bug.cgi?id=232749
 

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp (286058 => 286059)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitGtk/TestWebKitAccessibility.cpp	2021-11-19 14:51:09 UTC (rev 286059)
@@ -1939,6 +1939,68 @@
     g_assert_cmpstr(keyBinding.get(), ==, "");
 }
 
+static void testDocumentBasic(AccessibilityTest* test, gconstpointer)
+{
+    test->showInWindow(800, 600);
+    test->loadHtml(
+        "<!doctype html>"
+        "<html>"
+        "  <head>"
+        "    <meta http-equiv='content-language' content='en-us'/>"
+        "    <meta http-equiv='content-type' content='text/html; charset=UTF-8'/>"
+        "    <title>Document attributes</title>"
+        "  </head>"
+        "  <body>"
+        "    <p>This is a paragraph</p>"
+        "    <p lang='es'>Spanish</p>"
+        "  </body>"
+        "</html>",
+        "http://example.org");
+    test->waitUntilLoadFinished();
+
+    auto testApp = test->findTestApplication();
+    g_assert_true(ATSPI_IS_ACCESSIBLE(testApp.get()));
+
+    auto documentWeb = test->findDocumentWeb(testApp.get());
+    g_assert_true(ATSPI_IS_DOCUMENT(documentWeb.get()));
+    g_assert_cmpint(atspi_accessible_get_child_count(documentWeb.get(), nullptr), ==, 2);
+
+    GUniquePtr<char> language(atspi_document_get_locale(ATSPI_DOCUMENT(documentWeb.get()), nullptr));
+    g_assert_cmpstr(language.get(), ==, "en-us");
+    // Elements with no language attribute inhewir the document one.
+    auto p1 = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 0, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(p1.get()));
+    g_assert_cmpstr(atspi_accessible_get_object_locale(p1.get(), nullptr), ==, "en-us");
+    auto p2 = adoptGRef(atspi_accessible_get_child_at_index(documentWeb.get(), 1, nullptr));
+    g_assert_true(ATSPI_IS_ACCESSIBLE(p2.get()));
+    g_assert_cmpstr(atspi_accessible_get_object_locale(p2.get(), nullptr), ==, "es");
+
+    GRefPtr<GHashTable> attributes = adoptGRef(atspi_document_get_attributes(ATSPI_DOCUMENT(documentWeb.get()), nullptr));
+    g_assert_nonnull(attributes.get());
+#if USE(ATSPI)
+    g_assert_cmpuint(g_hash_table_size(attributes.get()), ==, 5);
+#else
+    g_assert_cmpuint(g_hash_table_size(attributes.get()), ==, 3);
+#endif
+    g_assert_cmpstr(static_cast<const char*>(g_hash_table_lookup(attributes.get(), "DocType")), ==, "html");
+    GUniquePtr<char> value(atspi_document_get_document_attribute_value(ATSPI_DOCUMENT(documentWeb.get()), const_cast<char*>("DocType"), nullptr));
+    g_assert_cmpstr(value.get(), ==, "html");
+    g_assert_cmpstr(static_cast<const char*>(g_hash_table_lookup(attributes.get(), "Encoding")), ==, "UTF-8");
+    value.reset(atspi_document_get_document_attribute_value(ATSPI_DOCUMENT(documentWeb.get()), const_cast<char*>("Encoding"), nullptr));
+    g_assert_cmpstr(value.get(), ==, "UTF-8");
+    g_assert_cmpstr(static_cast<const char*>(g_hash_table_lookup(attributes.get(), "URI")), ==, "http://example.org/");
+    value.reset(atspi_document_get_document_attribute_value(ATSPI_DOCUMENT(documentWeb.get()), const_cast<char*>("URI"), nullptr));
+    g_assert_cmpstr(value.get(), ==, "http://example.org/");
+#if USE(ATSPI)
+    g_assert_cmpstr(static_cast<const char*>(g_hash_table_lookup(attributes.get(), "MimeType")), ==, "text/html");
+    value.reset(atspi_document_get_document_attribute_value(ATSPI_DOCUMENT(documentWeb.get()), const_cast<char*>("MimeType"), nullptr));
+    g_assert_cmpstr(value.get(), ==, "text/html");
+    g_assert_cmpstr(static_cast<const char*>(g_hash_table_lookup(attributes.get(), "Title")), ==, "Document attributes");
+    value.reset(atspi_document_get_document_attribute_value(ATSPI_DOCUMENT(documentWeb.get()), const_cast<char*>("Title"), nullptr));
+    g_assert_cmpstr(value.get(), ==, "Document attributes");
+#endif
+}
+
 void beforeAll()
 {
     AccessibilityTest::add("WebKitAccessibility", "accessible/basic-hierarchy", testAccessibleBasicHierarchy);
@@ -1963,6 +2025,7 @@
     AccessibilityTest::add("WebKitAccessibility", "hyperlink/basic", testHyperlinkBasic);
     AccessibilityTest::add("WebKitAccessibility", "hypertext/basic", testHypertextBasic);
     AccessibilityTest::add("WebKitAccessibility", "action/basic", testActionBasic);
+    AccessibilityTest::add("WebKitAccessibility", "document/basic", testDocumentBasic);
 }
 
 void afterAll()

Modified: trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp (286058 => 286059)


--- trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp	2021-11-19 14:48:24 UTC (rev 286058)
+++ trunk/Tools/WebKitTestRunner/InjectedBundle/atspi/AccessibilityUIElementAtspi.cpp	2021-11-19 14:51:09 UTC (rev 286059)
@@ -684,7 +684,12 @@
 
 JSRetainPtr<JSStringRef> AccessibilityUIElement::language()
 {
-    return JSStringCreateWithCharacters(0, 0);
+    m_element->updateBackingStore();
+    auto locale = m_element->locale();
+    if (locale.isEmpty())
+        return JSStringCreateWithCharacters(0, 0);
+
+    return OpaqueJSString::tryCreate(makeString("AXLanguage: ", locale)).leakRef();
 }
 
 JSRetainPtr<JSStringRef> AccessibilityUIElement::helpText() const
@@ -1122,12 +1127,18 @@
 
 JSRetainPtr<JSStringRef> AccessibilityUIElement::documentEncoding()
 {
-    return JSStringCreateWithCharacters(0, 0);
+    if (!m_element->interfaces().contains(WebCore::AccessibilityObjectAtspi::Interface::Document))
+        return JSStringCreateWithCharacters(0, 0);
+
+    return OpaqueJSString::tryCreate(m_element->documentAttribute("Encoding")).leakRef();
 }
 
 JSRetainPtr<JSStringRef> AccessibilityUIElement::documentURI()
 {
-    return JSStringCreateWithCharacters(0, 0);
+    if (!m_element->interfaces().contains(WebCore::AccessibilityObjectAtspi::Interface::Document))
+        return JSStringCreateWithCharacters(0, 0);
+
+    return OpaqueJSString::tryCreate(m_element->documentAttribute("URI")).leakRef();
 }
 
 JSRetainPtr<JSStringRef> AccessibilityUIElement::url()
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to