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 
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 
26  GObject parent_instance;
27 
28  FlBasicMessageChannel* channel;
29 
30  // Handlers for incoming method calls.
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 
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 
52  switch (value) {
53  case 0:
55  case 1:
57  default:
58  g_warning("Unknown assertiveness value %" G_GINT64_FORMAT, value);
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 =
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  }
88  parse_text_direction(fl_value_get_int(text_direction_value));
89 
90  FlValue* assertiveness_value =
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) {
108  g_warning("Got invalid accessibility event message type");
109  return;
110  }
111 
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 
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 
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,
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));
191  nullptr);
192 
193  return self;
194 }
static constexpr char kAssertivenessKey[]
FlAccessibilityChannel * fl_accessibility_channel_new(FlBinaryMessenger *messenger, FlAccessibilityChannelVTable *vtable, gpointer user_data)
static constexpr char kFocusType[]
static constexpr char kTapType[]
static void message_cb(FlBasicMessageChannel *channel, FlValue *message, FlBasicMessageChannelResponseHandle *response_handle, gpointer user_data)
static void fl_accessibility_channel_dispose(GObject *object)
static constexpr char kTooltipType[]
static constexpr char kViewIdKey[]
static void fl_accessibility_channel_class_init(FlAccessibilityChannelClass *klass)
static constexpr char kTypeKey[]
static FlTextDirection parse_text_direction(int64_t value)
static void process_announce(FlAccessibilityChannel *self, FlValue *data)
static constexpr char kChannelName[]
static FlAssertiveness parse_assertiveness(int64_t value)
static constexpr char kTextDirectionKey[]
static constexpr char kMessageKey[]
static constexpr char kDataKey[]
static void process_message(FlAccessibilityChannel *self, FlValue *message)
static constexpr char kLongPressType[]
static void fl_accessibility_channel_init(FlAccessibilityChannel *self)
static constexpr char kAnnounceType[]
@ FL_TEXT_DIRECTION_LTR
@ FL_TEXT_DIRECTION_RTL
@ FL_ASSERTIVENESS_POLITE
@ FL_ASSERTIVENESS_ASSERTIVE
const char * message
const char FlTextDirection text_direction
const char FlTextDirection FlAssertiveness assertiveness
g_autoptr(FlEngine) engine
const char FlTextDirection FlAssertiveness gpointer user_data
G_MODULE_EXPORT void fl_basic_message_channel_set_message_handler(FlBasicMessageChannel *self, FlBasicMessageChannelMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlBasicMessageChannel * fl_basic_message_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMessageCodec *codec)
G_MODULE_EXPORT gboolean fl_basic_message_channel_respond(FlBasicMessageChannel *self, FlBasicMessageChannelResponseHandle *response_handle, FlValue *message, GError **error)
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:66
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:74
G_BEGIN_DECLS FlutterViewId view_id
FlAccessibilityChannelVTable * vtable
FlBasicMessageChannel * channel