| Flutter Linux Embedder |

fl_accessibility_channel.cc
Go to the documentation of this file.
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_channel.h"
6 #include "flutter/shell/platform/linux/public/flutter_linux/fl_basic_message_channel.h"
7 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
8
9 static constexpr char kChannelName[] = "flutter/accessibility";
10
11 static constexpr char kTypeKey[] = "type";
12 static constexpr char kDataKey[] = "data";
13
14 static constexpr char kAnnounceType[] = "announce";
15 static constexpr char kTooltipType[] = "tooltip";
16 static constexpr char kLongPressType[] = "longPress";
17 static constexpr char kTapType[] = "tap";
18 static constexpr char kFocusType[] = "focus";
19
20 static constexpr char kViewIdKey[] = "viewId";
21 static constexpr char kMessageKey[] = "message";
22 static constexpr char kTextDirectionKey[] = "textDirection";
23 static constexpr char kAssertivenessKey[] = "assertiveness";
24
25 struct _FlAccessibilityChannel {
26 GObject parent_instance;
27
28 FlBasicMessageChannel* channel;
29
30 // Handlers for incoming method calls.
31 FlAccessibilityChannelVTable* vtable;
32
33 // User data to pass to method call handlers.
34 gpointer user_data;
35 };
36
37 G_DEFINE_TYPE(FlAccessibilityChannel, fl_accessibility_channel, G_TYPE_OBJECT)
38
39 static FlTextDirection parse_text_direction(int64_t value) {
40 switch (value) {
41 case 0:
42 return FL_TEXT_DIRECTION_RTL;
43 case 1:
44 return FL_TEXT_DIRECTION_LTR;
45 default:
46 g_warning("Unknown text direction value %" G_GINT64_FORMAT, value);
47 return FL_TEXT_DIRECTION_LTR;
48 }
49 }
50
51 static FlAssertiveness parse_assertiveness(int64_t value) {
52 switch (value) {
53 case 0:
54 return FL_ASSERTIVENESS_POLITE;
55 case 1:
56 return FL_ASSERTIVENESS_ASSERTIVE;
57 default:
58 g_warning("Unknown assertiveness value %" G_GINT64_FORMAT, value);
59 return FL_ASSERTIVENESS_POLITE;
60 }
61 }
62
63 static void process_announce(FlAccessibilityChannel* self, FlValue* data) {
64 FlValue* view_id_value = fl_value_lookup_string(data, kViewIdKey);
65 if (view_id_value == nullptr ||
66 fl_value_get_type(view_id_value) != FL_VALUE_TYPE_INT) {
67 g_warning("Missing/invalid view ID in accessibility announce event");
68 return;
69 }
70 int64_t view_id = fl_value_get_int(view_id_value);
71
72 FlValue* message_value = fl_value_lookup_string(data, kMessageKey);
73 if (message_value == nullptr ||
74 fl_value_get_type(message_value) != FL_VALUE_TYPE_STRING) {
75 g_warning("Missing/invalid message in accessibility announce event");
76 return;
77 }
78 const char* message = fl_value_get_string(message_value);
79
80 FlValue* text_direction_value =
81 fl_value_lookup_string(data, kTextDirectionKey);
82 if (text_direction_value == nullptr ||
83 fl_value_get_type(text_direction_value) != FL_VALUE_TYPE_INT) {
84 g_warning("Missing/invalid text direction in accessibility announce event");
85 return;
86 }
87 FlTextDirection text_direction =
88 parse_text_direction(fl_value_get_int(text_direction_value));
89
90 FlValue* assertiveness_value =
91 fl_value_lookup_string(data, kAssertivenessKey);
92 FlAssertiveness assertiveness = FL_ASSERTIVENESS_POLITE;
93 if (assertiveness_value != nullptr) {
94 if (fl_value_get_type(assertiveness_value) != FL_VALUE_TYPE_INT) {
95 g_warning("Invalid assertiveness in accessibility announce event");
96 return;
97 }
98 assertiveness = parse_assertiveness(fl_value_get_int(assertiveness_value));
99 }
100
101 self->vtable->send_announcement(view_id, message, text_direction,
102 assertiveness, self->user_data);
103 }
104
105 // Process an accessibility event received from Flutter.
106 static void process_message(FlAccessibilityChannel* self, FlValue* message) {
107 if (fl_value_get_type(message) != FL_VALUE_TYPE_MAP) {
108 g_warning("Got invalid accessibility event message type");
109 return;
110 }
111
112 FlValue* type_value = fl_value_lookup_string(message, kTypeKey);
113 if (type_value == nullptr) {
114 g_warning("Accessibility event missing type");
115 return;
116 }
117 if (fl_value_get_type(type_value) != FL_VALUE_TYPE_STRING) {
118 g_warning("Got invalid accessibility event type");
119 return;
120 }
121 const char* type = fl_value_get_string(type_value);
122
123 FlValue* data = fl_value_lookup_string(message, kDataKey);
124 if (data == nullptr) {
125 g_warning("Accessibility event missing data");
126 return;
127 }
128 if (fl_value_get_type(data) != FL_VALUE_TYPE_MAP) {
129 g_warning("Got invalid accessibility data type");
130 return;
131 }
132
133 if (strcmp(type, kAnnounceType) == 0) {
134 process_announce(self, data);
135 } else if (strcmp(type, kTooltipType) == 0) {
136 } else if (strcmp(type, kLongPressType) == 0) {
137 } else if (strcmp(type, kTapType) == 0) {
138 // Only used by Android
139 } else if (strcmp(type, kFocusType) == 0) {
140 // Only used by Android and iOS.
141 } else {
142 // Silently ignore unknown types.
143 }
144 }
145
146 // Called when a message is received from Flutter.
147 static void message_cb(FlBasicMessageChannel* channel,
148 FlValue* message,
149 FlBasicMessageChannelResponseHandle* response_handle,
150 gpointer user_data) {
151 FlAccessibilityChannel* self = FL_ACCESSIBILITY_CHANNEL(user_data);
152
153 process_message(self, message);
154
155 g_autoptr(GError) error = nullptr;
156 if (!fl_basic_message_channel_respond(channel, response_handle, nullptr,
157 &error)) {
158 g_warning("Failed to send message response: %s", error->message);
159 }
160 }
161
162 static void fl_accessibility_channel_dispose(GObject* object) {
163 FlAccessibilityChannel* self = FL_ACCESSIBILITY_CHANNEL(object);
164
165 g_clear_object(&self->channel);
166
167 G_OBJECT_CLASS(fl_accessibility_channel_parent_class)->dispose(object);
168 }
169
170 static void fl_accessibility_channel_class_init(
171 FlAccessibilityChannelClass* klass) {
172 G_OBJECT_CLASS(klass)->dispose = fl_accessibility_channel_dispose;
173 }
174
175 static void fl_accessibility_channel_init(FlAccessibilityChannel* self) {}
176
177 FlAccessibilityChannel* fl_accessibility_channel_new(
178 FlBinaryMessenger* messenger,
179 FlAccessibilityChannelVTable* vtable,
180 gpointer user_data) {
181 FlAccessibilityChannel* self = FL_ACCESSIBILITY_CHANNEL(
182 g_object_new(fl_accessibility_channel_get_type(), nullptr));
183
184 self->vtable = vtable;
185 self->user_data = user_data;
186
187 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
188 self->channel = fl_basic_message_channel_new(messenger, kChannelName,
189 FL_MESSAGE_CODEC(codec));
190 fl_basic_message_channel_set_message_handler(self->channel, message_cb, self,
191 nullptr);
192
193 return self;
194 }
kAssertivenessKey
static constexpr char kAssertivenessKey[]
Definition: fl_accessibility_channel.cc:23
fl_accessibility_channel_new
FlAccessibilityChannel * fl_accessibility_channel_new(FlBinaryMessenger *messenger, FlAccessibilityChannelVTable *vtable, gpointer user_data)
Definition: fl_accessibility_channel.cc:177
kFocusType
static constexpr char kFocusType[]
Definition: fl_accessibility_channel.cc:18
kTapType
static constexpr char kTapType[]
Definition: fl_accessibility_channel.cc:17
message_cb
static void message_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
Definition: fl_accessibility_channel.cc:147
fl_accessibility_channel_dispose
static void fl_accessibility_channel_dispose(GObject *object)
Definition: fl_accessibility_channel.cc:162
kTooltipType
static constexpr char kTooltipType[]
Definition: fl_accessibility_channel.cc:15
kViewIdKey
static constexpr char kViewIdKey[]
Definition: fl_accessibility_channel.cc:20
fl_accessibility_channel_class_init
static void fl_accessibility_channel_class_init(FlAccessibilityChannelClass *klass)
Definition: fl_accessibility_channel.cc:170
kTypeKey
static constexpr char kTypeKey[]
Definition: fl_accessibility_channel.cc:11
parse_text_direction
static FlTextDirection parse_text_direction(int64_t value)
Definition: fl_accessibility_channel.cc:39
process_announce
static void process_announce(FlAccessibilityChannel *self, FlValue *data)
Definition: fl_accessibility_channel.cc:63
kChannelName
static constexpr char kChannelName[]
Definition: fl_accessibility_channel.cc:9
parse_assertiveness
static FlAssertiveness parse_assertiveness(int64_t value)
Definition: fl_accessibility_channel.cc:51
kTextDirectionKey
static constexpr char kTextDirectionKey[]
Definition: fl_accessibility_channel.cc:22
kMessageKey
static constexpr char kMessageKey[]
Definition: fl_accessibility_channel.cc:21
kDataKey
static constexpr char kDataKey[]
Definition: fl_accessibility_channel.cc:12
process_message
static void process_message(FlAccessibilityChannel *self, FlValue *message)
Definition: fl_accessibility_channel.cc:106
kLongPressType
static constexpr char kLongPressType[]
Definition: fl_accessibility_channel.cc:16
fl_accessibility_channel_init
static void fl_accessibility_channel_init(FlAccessibilityChannel *self)
Definition: fl_accessibility_channel.cc:175
kAnnounceType
static constexpr char kAnnounceType[]
Definition: fl_accessibility_channel.cc:14
fl_accessibility_channel.h
FlTextDirection
FlTextDirection
Definition: fl_accessibility_channel.h:19
FL_TEXT_DIRECTION_LTR
@ FL_TEXT_DIRECTION_LTR
Definition: fl_accessibility_channel.h:21
FL_TEXT_DIRECTION_RTL
@ FL_TEXT_DIRECTION_RTL
Definition: fl_accessibility_channel.h:20
FlAssertiveness
FlAssertiveness
Definition: fl_accessibility_channel.h:25
FL_ASSERTIVENESS_POLITE
@ FL_ASSERTIVENESS_POLITE
Definition: fl_accessibility_channel.h:26
FL_ASSERTIVENESS_ASSERTIVE
@ FL_ASSERTIVENESS_ASSERTIVE
Definition: fl_accessibility_channel.h:27
message
const char * message
Definition: fl_accessibility_handler.cc:21
text_direction
const char FlTextDirection text_direction
Definition: fl_accessibility_handler.cc:22
assertiveness
const char FlTextDirection FlAssertiveness assertiveness
Definition: fl_accessibility_handler.cc:23
g_autoptr
g_autoptr(FlEngine) engine
user_data
const char FlTextDirection FlAssertiveness gpointer user_data
Definition: fl_accessibility_handler.cc:24
fl_basic_message_channel_set_message_handler
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_basic_message_channel.cc:176
fl_basic_message_channel_new
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
Definition: fl_basic_message_channel.cc:154
fl_basic_message_channel_respond
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
Definition: fl_basic_message_channel.cc:204
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
fl_basic_message_channel.h
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_standard_message_codec_new
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
Definition: fl_standard_message_codec.cc:637
fl_standard_message_codec.h
type
uint8_t type
Definition: fl_standard_message_codec_test.cc:1117
fl_value_lookup_string
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_get_string
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:66
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:74
view_id
G_BEGIN_DECLS FlutterViewId view_id
Definition: fl_view_accessible.h:49
_FlAccessibilityChannel
Definition: fl_accessibility_channel.cc:25
_FlAccessibilityChannel::user_data
gpointer user_data
Definition: fl_accessibility_channel.cc:34
_FlAccessibilityChannel::vtable
FlAccessibilityChannelVTable * vtable
Definition: fl_accessibility_channel.cc:31
_FlAccessibilityChannel::channel
FlBasicMessageChannel * channel
Definition: fl_accessibility_channel.cc:28
_FlAccessibilityChannel::parent_instance
GObject parent_instance
Definition: fl_accessibility_channel.cc:26
FlAccessibilityChannelVTable
Definition: fl_accessibility_channel.h:37