File: AccessibilityObjectSelectionAtspi.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (193 lines) | stat: -rw-r--r-- 7,023 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * 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 USE(ATSPI)

#include "AccessibilityAtspi.h"
#include "AccessibilityObject.h" // NOLINT: check-webkit-style has problems with files that do not have primary header

namespace WebCore {

GDBusInterfaceVTable AccessibilityObjectAtspi::s_selectionFunctions = {
    // method_call
    [](GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData) {
        auto atspiObject = Ref { *static_cast<AccessibilityObjectAtspi*>(userData) };
        atspiObject->updateBackingStore();

        if (!g_strcmp0(methodName, "GetSelectedChild")) {
            int index;
            g_variant_get(parameters, "(i)", &index);
            auto* child = index >= 0 ? atspiObject->selectedChild(index) : nullptr;
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(@(so))", child ? child->reference() : AccessibilityAtspi::singleton().nullReference()));
        } else if (!g_strcmp0(methodName, "SelectChild")) {
            int index;
            g_variant_get(parameters, "(i)", &index);
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", index >= 0 ? atspiObject->setChildSelected(index, true) : FALSE));
        } else if (!g_strcmp0(methodName, "DeselectSelectedChild")) {
            int index;
            g_variant_get(parameters, "(i)", &index);
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", index >= 0 ? atspiObject->deselectSelectedChild(index) : FALSE));
        } else if (!g_strcmp0(methodName, "IsChildSelected")) {
            int index;
            g_variant_get(parameters, "(i)", &index);
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", index >= 0 ? atspiObject->isChildSelected(index) : FALSE));
        } else if (!g_strcmp0(methodName, "SelectAll"))
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", atspiObject->selectAll()));
        else if (!g_strcmp0(methodName, "ClearSelection"))
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", atspiObject->clearSelection()));
        else if (!g_strcmp0(methodName, "DeselectChild")) {
            int index;
            g_variant_get(parameters, "(i)", &index);
            g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", index >= 0 ? atspiObject->setChildSelected(index, false) : FALSE));
        }
    },
    // get_property
    [](GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar* propertyName, GError** error, gpointer userData) -> GVariant* {
        auto atspiObject = Ref { *static_cast<AccessibilityObjectAtspi*>(userData) };
        atspiObject->updateBackingStore();

        if (!g_strcmp0(propertyName, "NSelectedChildren"))
            return g_variant_new_int32(atspiObject->selectionCount());

        g_set_error(error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Unknown property '%s'", propertyName);
        return nullptr;
    },
    // set_property,
    nullptr,
    // padding
    { nullptr }
};

unsigned AccessibilityObjectAtspi::selectionCount() const
{
    if (!m_coreObject)
        return 0;

    return m_coreObject->selectedChildren().size();
}

AccessibilityObjectAtspi* AccessibilityObjectAtspi::selectedChild(unsigned index) const
{
    if (!m_coreObject)
        return nullptr;

    auto selectedItems = m_coreObject->selectedChildren();
    if (index >= selectedItems.size())
        return nullptr;

    return selectedItems[index]->wrapper();
}

bool AccessibilityObjectAtspi::setChildSelected(unsigned index, bool selected) const
{
    if (!m_coreObject)
        return false;

    const auto& children = m_coreObject->children();
    if (index >= children.size())
        return false;

    if (!children[index]->canSetSelectedAttribute())
        return false;

    children[index]->setSelected(selected);
    return selected ? children[index]->isSelected() : !children[index]->isSelected();
}

bool AccessibilityObjectAtspi::deselectSelectedChild(unsigned index) const
{
    if (!m_coreObject)
        return false;

    auto selectedItems = m_coreObject->selectedChildren();
    if (index >= selectedItems.size())
        return false;

    if (!selectedItems[index]->canSetSelectedAttribute())
        return false;

    selectedItems[index]->setSelected(false);
    return !selectedItems[index]->isSelected();
}

bool AccessibilityObjectAtspi::isChildSelected(unsigned index) const
{
    if (!m_coreObject)
        return false;

    const auto& children = m_coreObject->children();
    if (index >= children.size())
        return false;

    return children[index]->isSelected();
}

bool AccessibilityObjectAtspi::selectAll() const
{
    if (!m_coreObject)
        return false;

    if (!m_coreObject->isMultiSelectable() || !m_coreObject->canSetSelectedChildren())
        return false;

    const auto& children = m_coreObject->children();
    unsigned selectableChildCount = 0;
    for (const auto& child : children) {
        if (child->canSetSelectedAttribute())
            selectableChildCount++;
    }

    if (!selectableChildCount)
        return false;

    m_coreObject->setSelectedChildren(children);
    return selectableChildCount == m_coreObject->selectedChildren().size();
}

bool AccessibilityObjectAtspi::clearSelection() const
{
    if (!m_coreObject)
        return false;

    if (!m_coreObject->canSetSelectedChildren())
        return false;

    m_coreObject->setSelectedChildren({ });
    return m_coreObject->selectedChildren().isEmpty();
}

void AccessibilityObjectAtspi::selectionChanged()
{
    // selectionChanged can be called multiple times by WebCore, so ensure we don't
    // emit it if the last one happened in the same run loop iteration.
    auto* source = g_main_current_source();
    int64_t sourceTime = source ? g_source_get_time(source) : -1;
    if (sourceTime <= m_lastSelectionChangedTime)
        return;

    m_lastSelectionChangedTime = sourceTime;
    AccessibilityAtspi::singleton().selectionChanged(*this);
}

} // namespace WebCore

#endif // USE(ATSPI)