Skip to content

Commit d4cb0c9

Browse files
committed
feat(linux): add accessibility semantics store
Cache owned semantics nodes per view and apply partial updates for native accessibility backends. Cover storage, replacement, and view isolation. Use g_memdup2 with an older-GLib compatibility helper so the introducing commit also compiles with newer GLib headers.
1 parent c5e82f2 commit d4cb0c9

5 files changed

Lines changed: 301 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/linux/BUILD.gn‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ source_set("flutter_linux_sources") {
117117
sources = [
118118
"fl_accessibility_channel.cc",
119119
"fl_accessibility_handler.cc",
120+
"fl_accessibility_semantics_store.cc",
120121
"fl_accessible_node.cc",
121122
"fl_accessible_text_field.cc",
122123
"fl_application.cc",
@@ -132,6 +133,7 @@ source_set("flutter_linux_sources") {
132133
"fl_event_channel.cc",
133134
"fl_framebuffer.cc",
134135
"fl_gl_fence.cc",
136+
"fl_glib_compat.h",
135137
"fl_gnome_settings.cc",
136138
"fl_json_message_codec.cc",
137139
"fl_json_method_codec.cc",
@@ -238,6 +240,7 @@ executable("flutter_linux_unittests") {
238240

239241
sources = [
240242
"fl_accessibility_handler_test.cc",
243+
"fl_accessibility_semantics_store_test.cc",
241244
"fl_accessible_node_test.cc",
242245
"fl_accessible_text_field_test.cc",
243246
"fl_application_test.cc",
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/linux/fl_accessibility_semantics_store.h"
6+
7+
#include <cstring>
8+
9+
#include "flutter/shell/platform/linux/fl_glib_compat.h"
10+
11+
struct _FlAccessibilitySemanticsStore {
12+
GObject parent_instance;
13+
14+
FlutterViewId view_id;
15+
GHashTable* nodes_by_id;
16+
gboolean root_node_present;
17+
};
18+
19+
G_DEFINE_TYPE(FlAccessibilitySemanticsStore,
20+
fl_accessibility_semantics_store,
21+
G_TYPE_OBJECT)
22+
23+
static void fl_accessibility_semantics_node_free(gpointer data) {
24+
FlAccessibilitySemanticsNode* node =
25+
static_cast<FlAccessibilitySemanticsNode*>(data);
26+
g_free(node->label);
27+
g_free(node->value);
28+
g_free(node->children_in_traversal_order);
29+
g_free(node);
30+
}
31+
32+
static FlAccessibilitySemanticsNode* fl_accessibility_semantics_node_new(
33+
FlutterSemanticsNode2* semantics) {
34+
FlAccessibilitySemanticsNode* node = g_new0(FlAccessibilitySemanticsNode, 1);
35+
node->id = semantics->id;
36+
node->label = g_strdup(semantics->label);
37+
node->value = g_strdup(semantics->value);
38+
if (semantics->flags2 != nullptr) {
39+
node->flags = *semantics->flags2;
40+
} else {
41+
memset(&node->flags, 0, sizeof(node->flags));
42+
}
43+
node->actions = semantics->actions;
44+
node->text_selection_base = semantics->text_selection_base;
45+
node->text_selection_extent = semantics->text_selection_extent;
46+
node->text_direction = semantics->text_direction;
47+
node->rect = semantics->rect;
48+
node->transform = semantics->transform;
49+
node->child_count = semantics->child_count;
50+
if (semantics->child_count > 0 &&
51+
semantics->children_in_traversal_order != nullptr) {
52+
node->children_in_traversal_order = static_cast<int32_t*>(
53+
g_memdup2(semantics->children_in_traversal_order,
54+
sizeof(int32_t) * semantics->child_count));
55+
}
56+
57+
return node;
58+
}
59+
60+
static void fl_accessibility_semantics_store_dispose(GObject* object) {
61+
FlAccessibilitySemanticsStore* self =
62+
FL_ACCESSIBILITY_SEMANTICS_STORE(object);
63+
64+
g_clear_pointer(&self->nodes_by_id, g_hash_table_unref);
65+
66+
G_OBJECT_CLASS(fl_accessibility_semantics_store_parent_class)
67+
->dispose(object);
68+
}
69+
70+
static void fl_accessibility_semantics_store_class_init(
71+
FlAccessibilitySemanticsStoreClass* klass) {
72+
GObjectClass* object_class = G_OBJECT_CLASS(klass);
73+
object_class->dispose = fl_accessibility_semantics_store_dispose;
74+
}
75+
76+
static void fl_accessibility_semantics_store_init(
77+
FlAccessibilitySemanticsStore* self) {
78+
self->nodes_by_id =
79+
g_hash_table_new_full(g_direct_hash, g_direct_equal, nullptr,
80+
fl_accessibility_semantics_node_free);
81+
}
82+
83+
FlAccessibilitySemanticsStore* fl_accessibility_semantics_store_new(
84+
FlutterViewId view_id) {
85+
FlAccessibilitySemanticsStore* self = FL_ACCESSIBILITY_SEMANTICS_STORE(
86+
g_object_new(fl_accessibility_semantics_store_get_type(), nullptr));
87+
self->view_id = view_id;
88+
return self;
89+
}
90+
91+
void fl_accessibility_semantics_store_handle_update(
92+
FlAccessibilitySemanticsStore* self,
93+
const FlutterSemanticsUpdate2* update) {
94+
g_return_if_fail(FL_IS_ACCESSIBILITY_SEMANTICS_STORE(self));
95+
g_return_if_fail(update != nullptr);
96+
97+
if (update->view_id != self->view_id) {
98+
return;
99+
}
100+
101+
for (size_t i = 0; i < update->node_count; i++) {
102+
FlutterSemanticsNode2* semantics = update->nodes[i];
103+
FlAccessibilitySemanticsNode* node =
104+
fl_accessibility_semantics_node_new(semantics);
105+
g_hash_table_replace(self->nodes_by_id, GINT_TO_POINTER(node->id), node);
106+
if (node->id == 0) {
107+
self->root_node_present = TRUE;
108+
}
109+
}
110+
}
111+
112+
const FlAccessibilitySemanticsNode*
113+
fl_accessibility_semantics_store_lookup_node(
114+
FlAccessibilitySemanticsStore* self,
115+
int32_t node_id) {
116+
g_return_val_if_fail(FL_IS_ACCESSIBILITY_SEMANTICS_STORE(self), nullptr);
117+
118+
return static_cast<const FlAccessibilitySemanticsNode*>(
119+
g_hash_table_lookup(self->nodes_by_id, GINT_TO_POINTER(node_id)));
120+
}
121+
122+
gboolean fl_accessibility_semantics_store_has_root(
123+
FlAccessibilitySemanticsStore* self) {
124+
g_return_val_if_fail(FL_IS_ACCESSIBILITY_SEMANTICS_STORE(self), FALSE);
125+
126+
return self->root_node_present;
127+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_ACCESSIBILITY_SEMANTICS_STORE_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_ACCESSIBILITY_SEMANTICS_STORE_H_
7+
8+
#include <glib-object.h>
9+
10+
#include "flutter/shell/platform/embedder/embedder.h"
11+
12+
G_BEGIN_DECLS
13+
14+
typedef struct {
15+
int32_t id;
16+
gchar* label;
17+
gchar* value;
18+
FlutterSemanticsFlags flags;
19+
FlutterSemanticsAction actions;
20+
gint text_selection_base;
21+
gint text_selection_extent;
22+
FlutterTextDirection text_direction;
23+
FlutterRect rect;
24+
FlutterTransformation transform;
25+
size_t child_count;
26+
int32_t* children_in_traversal_order;
27+
} FlAccessibilitySemanticsNode;
28+
29+
G_DECLARE_FINAL_TYPE(FlAccessibilitySemanticsStore,
30+
fl_accessibility_semantics_store,
31+
FL,
32+
ACCESSIBILITY_SEMANTICS_STORE,
33+
GObject);
34+
35+
FlAccessibilitySemanticsStore* fl_accessibility_semantics_store_new(
36+
FlutterViewId view_id);
37+
38+
void fl_accessibility_semantics_store_handle_update(
39+
FlAccessibilitySemanticsStore* self,
40+
const FlutterSemanticsUpdate2* update);
41+
42+
const FlAccessibilitySemanticsNode*
43+
fl_accessibility_semantics_store_lookup_node(
44+
FlAccessibilitySemanticsStore* self,
45+
int32_t node_id);
46+
47+
gboolean fl_accessibility_semantics_store_has_root(
48+
FlAccessibilitySemanticsStore* self);
49+
50+
G_END_DECLS
51+
52+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_ACCESSIBILITY_SEMANTICS_STORE_H_
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/linux/fl_accessibility_semantics_store.h"
6+
7+
#include "gtest/gtest.h"
8+
9+
TEST(FlAccessibilitySemanticsStoreTest, StoresNodesForMatchingView) {
10+
g_autoptr(FlAccessibilitySemanticsStore) store =
11+
fl_accessibility_semantics_store_new(123);
12+
13+
int32_t children[] = {7, 8};
14+
FlutterSemanticsFlags flags = {};
15+
flags.is_text_field = kFlutterTristateTrue;
16+
FlutterSemanticsNode2 root = {.id = 0,
17+
.actions = kFlutterSemanticsActionTap,
18+
.text_selection_base = 2,
19+
.text_selection_extent = 5,
20+
.label = "root",
21+
.value = "value",
22+
.child_count = 2,
23+
.children_in_traversal_order = children,
24+
.flags2 = &flags,
25+
.identifier = ""};
26+
FlutterSemanticsNode2* nodes[] = {&root};
27+
FlutterSemanticsUpdate2 update = {
28+
.node_count = 1,
29+
.nodes = nodes,
30+
.view_id = 123,
31+
};
32+
33+
fl_accessibility_semantics_store_handle_update(store, &update);
34+
35+
EXPECT_TRUE(fl_accessibility_semantics_store_has_root(store));
36+
const FlAccessibilitySemanticsNode* node =
37+
fl_accessibility_semantics_store_lookup_node(store, 0);
38+
ASSERT_NE(node, nullptr);
39+
EXPECT_EQ(node->id, 0);
40+
EXPECT_STREQ(node->label, "root");
41+
EXPECT_STREQ(node->value, "value");
42+
EXPECT_TRUE(node->flags.is_text_field);
43+
EXPECT_EQ(node->actions, kFlutterSemanticsActionTap);
44+
EXPECT_EQ(node->text_selection_base, 2);
45+
EXPECT_EQ(node->text_selection_extent, 5);
46+
EXPECT_EQ(node->child_count, static_cast<size_t>(2));
47+
ASSERT_NE(node->children_in_traversal_order, nullptr);
48+
EXPECT_EQ(node->children_in_traversal_order[0], 7);
49+
EXPECT_EQ(node->children_in_traversal_order[1], 8);
50+
}
51+
52+
TEST(FlAccessibilitySemanticsStoreTest, IgnoresOtherViewUpdates) {
53+
g_autoptr(FlAccessibilitySemanticsStore) store =
54+
fl_accessibility_semantics_store_new(123);
55+
56+
FlutterSemanticsFlags flags = {};
57+
FlutterSemanticsNode2 root = {
58+
.id = 0, .label = "root", .flags2 = &flags, .identifier = ""};
59+
FlutterSemanticsNode2* nodes[] = {&root};
60+
FlutterSemanticsUpdate2 update = {
61+
.node_count = 1,
62+
.nodes = nodes,
63+
.view_id = 999,
64+
};
65+
66+
fl_accessibility_semantics_store_handle_update(store, &update);
67+
68+
EXPECT_FALSE(fl_accessibility_semantics_store_has_root(store));
69+
EXPECT_EQ(fl_accessibility_semantics_store_lookup_node(store, 0), nullptr);
70+
}
71+
72+
TEST(FlAccessibilitySemanticsStoreTest, ReplacesNodesOnSubsequentUpdates) {
73+
g_autoptr(FlAccessibilitySemanticsStore) store =
74+
fl_accessibility_semantics_store_new(123);
75+
76+
FlutterSemanticsFlags flags = {};
77+
FlutterSemanticsNode2 root1 = {
78+
.id = 0, .label = "before", .flags2 = &flags, .identifier = ""};
79+
FlutterSemanticsNode2* nodes1[] = {&root1};
80+
FlutterSemanticsUpdate2 update1 = {
81+
.node_count = 1,
82+
.nodes = nodes1,
83+
.view_id = 123,
84+
};
85+
fl_accessibility_semantics_store_handle_update(store, &update1);
86+
87+
FlutterSemanticsNode2 root2 = {
88+
.id = 0, .label = "after", .flags2 = &flags, .identifier = ""};
89+
FlutterSemanticsNode2* nodes2[] = {&root2};
90+
FlutterSemanticsUpdate2 update2 = {
91+
.node_count = 1,
92+
.nodes = nodes2,
93+
.view_id = 123,
94+
};
95+
fl_accessibility_semantics_store_handle_update(store, &update2);
96+
97+
const FlAccessibilitySemanticsNode* node =
98+
fl_accessibility_semantics_store_lookup_node(store, 0);
99+
ASSERT_NE(node, nullptr);
100+
EXPECT_STREQ(node->label, "after");
101+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_GLIB_COMPAT_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_GLIB_COMPAT_H_
7+
8+
#include <glib.h>
9+
10+
// Keep the size-safe API available with older GLib headers.
11+
#if !GLIB_CHECK_VERSION(2, 68, 0)
12+
inline gpointer g_memdup2(gconstpointer mem, gsize byte_size) {
13+
g_return_val_if_fail(byte_size <= G_MAXUINT, nullptr);
14+
return g_memdup(mem, static_cast<guint>(byte_size));
15+
}
16+
#endif
17+
18+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_GLIB_COMPAT_H_

0 commit comments

Comments
 (0)