8 #include "flutter/fml/logging.h"
23 fl_binary_messenger_codec_error)
32 fl_binary_messenger_response_handle_impl,
34 BINARY_MESSENGER_RESPONSE_HANDLE_IMPL,
35 FlBinaryMessengerResponseHandle)
39 struct _FlBinaryMessengerImpl {
40 GObject parent_instance;
45 GHashTable* platform_message_handlers;
49 FlBinaryMessengerInterface* iface);
52 FlBinaryMessengerImpl,
55 G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(),
58 static void fl_binary_messenger_response_handle_class_init(
59 FlBinaryMessengerResponseHandleClass* klass) {}
65 static void fl_binary_messenger_response_handle_init(
66 FlBinaryMessengerResponseHandle*
self) {}
80 fl_binary_messenger_response_handle_impl,
81 fl_binary_messenger_response_handle_get_type())
83 static
void fl_binary_messenger_default_init(
84 FlBinaryMessengerInterface* iface) {}
87 FlBinaryMessengerResponseHandleImpl*
self =
88 FL_BINARY_MESSENGER_RESPONSE_HANDLE_IMPL(
object);
90 g_autoptr(FlEngine) engine =
91 FL_ENGINE(g_weak_ref_get(&self->messenger->engine));
92 if (self->response_handle !=
nullptr && engine !=
nullptr) {
93 g_critical(
"FlBinaryMessengerResponseHandle was not responded to");
96 g_clear_object(&self->messenger);
97 self->response_handle =
nullptr;
99 G_OBJECT_CLASS(fl_binary_messenger_response_handle_impl_parent_class)
104 FlBinaryMessengerResponseHandleImplClass* klass) {
105 G_OBJECT_CLASS(klass)->dispose =
110 FlBinaryMessengerResponseHandleImpl*
self) {}
112 static FlBinaryMessengerResponseHandleImpl*
114 FlBinaryMessengerImpl* messenger,
115 const FlutterPlatformMessageResponseHandle* response_handle) {
116 FlBinaryMessengerResponseHandleImpl*
self =
117 FL_BINARY_MESSENGER_RESPONSE_HANDLE_IMPL(g_object_new(
118 fl_binary_messenger_response_handle_impl_get_type(),
nullptr));
120 self->messenger = FL_BINARY_MESSENGER_IMPL(g_object_ref(messenger));
121 self->response_handle = response_handle;
135 GDestroyNotify destroy_notify) {
140 self->message_handler_destroy_notify = destroy_notify;
146 if (self->message_handler_destroy_notify) {
154 const gchar* channel,
156 const FlutterPlatformMessageResponseHandle* response_handle,
158 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(
user_data);
161 g_hash_table_lookup(self->platform_message_handlers, channel));
162 if (handler ==
nullptr) {
166 g_autoptr(FlBinaryMessengerResponseHandleImpl) handle =
169 FL_BINARY_MESSENGER_RESPONSE_HANDLE(handle),
176 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(
object);
178 g_weak_ref_clear(&self->engine);
180 g_clear_pointer(&self->platform_message_handlers, g_hash_table_unref);
182 G_OBJECT_CLASS(fl_binary_messenger_impl_parent_class)->dispose(
object);
186 FlBinaryMessenger* messenger,
187 const gchar* channel,
190 GDestroyNotify destroy_notify) {
191 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(messenger);
194 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
195 if (engine ==
nullptr) {
196 if (handler !=
nullptr) {
198 "Attempted to set message handler on an FlBinaryMessenger without an "
201 if (destroy_notify !=
nullptr) {
207 if (handler !=
nullptr) {
208 g_hash_table_replace(
209 self->platform_message_handlers, g_strdup(channel),
212 g_hash_table_remove(self->platform_message_handlers, channel);
217 g_object_unref(
value);
218 return G_SOURCE_REMOVE;
223 FlBinaryMessengerResponseHandle* response_handle_,
226 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(messenger);
227 g_return_val_if_fail(
228 FL_IS_BINARY_MESSENGER_RESPONSE_HANDLE_IMPL(response_handle_), FALSE);
229 FlBinaryMessengerResponseHandleImpl* response_handle =
230 FL_BINARY_MESSENGER_RESPONSE_HANDLE_IMPL(response_handle_);
232 g_return_val_if_fail(response_handle->messenger ==
self, FALSE);
233 g_return_val_if_fail(response_handle->response_handle !=
nullptr, FALSE);
235 FlEngine* engine = FL_ENGINE(g_weak_ref_get(&self->engine));
236 if (engine ==
nullptr) {
241 if (response_handle->response_handle ==
nullptr) {
245 "Attempted to respond to a message that is already responded to");
249 engine, response_handle->response_handle, response,
error);
250 response_handle->response_handle =
nullptr;
264 g_task_return_pointer(task,
result, g_object_unref);
268 const gchar* channel,
270 GCancellable* cancellable,
273 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(messenger);
275 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
276 if (engine ==
nullptr) {
281 engine, channel, message, cancellable,
290 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(messenger);
291 g_return_val_if_fail(g_task_is_valid(
result,
self), FALSE);
293 g_autoptr(GTask) task = G_TASK(
result);
294 GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task,
nullptr));
296 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
297 if (engine ==
nullptr) {
310 if (response ==
nullptr) {
322 g_autoptr(GError)
error =
nullptr;
324 g_warning(
"Failed to resize channel: %s",
error->message);
329 const gchar* channel,
331 FML_DCHECK(new_size >= 0);
347 g_autoptr(GError)
error =
nullptr;
349 g_warning(
"Failed to set warns on channel overflow: %s",
error->message);
354 const gchar* channel,
367 static void shutdown(FlBinaryMessenger* messenger) {
368 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(messenger);
372 g_autoptr(GHashTable) handlers =
self->platform_message_handlers;
373 self->platform_message_handlers = g_hash_table_new_full(
375 g_hash_table_remove_all(handlers);
379 FlBinaryMessengerImplClass* klass) {
384 FlBinaryMessengerInterface* iface) {
395 self->platform_message_handlers = g_hash_table_new_full(
400 g_return_val_if_fail(FL_IS_ENGINE(engine),
nullptr);
402 FlBinaryMessengerImpl*
self = FL_BINARY_MESSENGER_IMPL(
403 g_object_new(fl_binary_messenger_impl_get_type(),
nullptr));
406 FL_IS_BINARY_MESSENGER_IMPL(
self);
408 g_weak_ref_init(&self->engine, G_OBJECT(engine));
413 return FL_BINARY_MESSENGER(
self);
417 FlBinaryMessenger*
self,
418 const gchar* channel,
421 GDestroyNotify destroy_notify) {
422 g_return_if_fail(FL_IS_BINARY_MESSENGER(
self));
423 g_return_if_fail(channel !=
nullptr);
425 FL_BINARY_MESSENGER_GET_IFACE(
self)->set_message_handler_on_channel(
426 self, channel, handler,
user_data, destroy_notify);
431 FlBinaryMessenger*
self,
432 FlBinaryMessengerResponseHandle* response_handle,
435 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(
self), FALSE);
436 g_return_val_if_fail(FL_IS_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle),
439 return FL_BINARY_MESSENGER_GET_IFACE(
self)->send_response(
440 self, response_handle, response,
error);
444 FlBinaryMessenger*
self,
445 const gchar* channel,
447 GCancellable* cancellable,
450 g_return_if_fail(FL_IS_BINARY_MESSENGER(
self));
451 g_return_if_fail(channel !=
nullptr);
453 FL_BINARY_MESSENGER_GET_IFACE(
self)->send_on_channel(
458 FlBinaryMessenger*
self,
461 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(
self), FALSE);
463 return FL_BINARY_MESSENGER_GET_IFACE(
self)->send_on_channel_finish(
468 const gchar* channel,
470 g_return_if_fail(FL_IS_BINARY_MESSENGER(
self));
472 return FL_BINARY_MESSENGER_GET_IFACE(
self)->resize_channel(
self, channel,
477 FlBinaryMessenger*
self,
478 const gchar* channel,
480 g_return_if_fail(FL_IS_BINARY_MESSENGER(
self));
482 return FL_BINARY_MESSENGER_GET_IFACE(
self)->set_warns_on_channel_overflow(
483 self, channel, warns);
487 g_return_if_fail(FL_IS_BINARY_MESSENGER(
self));
489 return FL_BINARY_MESSENGER_GET_IFACE(
self)->shutdown(
self);