Flutter Engine
fl_binary_messenger.cc File Reference
#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_engine.h"
#include <gmodule.h>

Go to the source code of this file.

Classes

struct  _FlBinaryMessengerResponseHandle
 
struct  PlatformMessageHandler
 

Functions

 G_DEFINE_QUARK (fl_binary_messenger_codec_error_quark, fl_binary_messenger_codec_error) struct _FlBinaryMessenger
 
 G_DEFINE_TYPE (FlBinaryMessengerResponseHandle, fl_binary_messenger_response_handle, G_TYPE_OBJECT) static void fl_binary_messenger_response_handle_dispose(GObject *object)
 
static void fl_binary_messenger_response_handle_class_init (FlBinaryMessengerResponseHandleClass *klass)
 
static void fl_binary_messenger_response_handle_init (FlBinaryMessengerResponseHandle *self)
 
static FlBinaryMessengerResponseHandle * fl_binary_messenger_response_handle_new (FlBinaryMessenger *messenger, const FlutterPlatformMessageResponseHandle *response_handle)
 
static PlatformMessageHandlerplatform_message_handler_new (FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
 
static void platform_message_handler_free (gpointer data)
 
static void engine_weak_notify_cb (gpointer user_data, GObject *where_the_object_was)
 
static gboolean fl_binary_messenger_platform_message_cb (FlEngine *engine, const gchar *channel, GBytes *message, const FlutterPlatformMessageResponseHandle *response_handle, void *user_data)
 
static void fl_binary_messenger_dispose (GObject *object)
 
static void fl_binary_messenger_class_init (FlBinaryMessengerClass *klass)
 
static void fl_binary_messenger_init (FlBinaryMessenger *self)
 
FlBinaryMessenger * fl_binary_messenger_new (FlEngine *engine)
 
G_MODULE_EXPORT void fl_binary_messenger_set_message_handler_on_channel (FlBinaryMessenger *self, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
 
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response (FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
 
static void platform_message_ready_cb (GObject *object, GAsyncResult *result, gpointer user_data)
 
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel (FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
 
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish (FlBinaryMessenger *self, GAsyncResult *result, GError **error)
 

Function Documentation

◆ engine_weak_notify_cb()

static void engine_weak_notify_cb ( gpointer  user_data,
GObject *  where_the_object_was 
)
static

Definition at line 103 of file fl_binary_messenger.cc.

References platform_message_handler_free().

Referenced by fl_binary_messenger_dispose(), and fl_binary_messenger_new().

104  {
105  FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data);
106  self->engine = nullptr;
107 
108  // Disconnect any handlers.
109  // Take the reference in case a handler tries to modify this table.
110  g_autoptr(GHashTable) handlers = self->platform_message_handlers;
111  self->platform_message_handlers = g_hash_table_new_full(
112  g_str_hash, g_str_equal, g_free, platform_message_handler_free);
113  g_hash_table_remove_all(handlers);
114 }
void * user_data
static void platform_message_handler_free(gpointer data)

◆ fl_binary_messenger_class_init()

static void fl_binary_messenger_class_init ( FlBinaryMessengerClass *  klass)
static

Definition at line 151 of file fl_binary_messenger.cc.

References fl_binary_messenger_dispose().

151  {
152  G_OBJECT_CLASS(klass)->dispose = fl_binary_messenger_dispose;
153 }
static void fl_binary_messenger_dispose(GObject *object)

◆ fl_binary_messenger_dispose()

static void fl_binary_messenger_dispose ( GObject *  object)
static

Definition at line 138 of file fl_binary_messenger.cc.

References engine_weak_notify_cb().

Referenced by fl_binary_messenger_class_init().

138  {
139  FlBinaryMessenger* self = FL_BINARY_MESSENGER(object);
140 
141  if (self->engine != nullptr) {
142  g_object_weak_unref(G_OBJECT(self->engine), engine_weak_notify_cb, self);
143  self->engine = nullptr;
144  }
145 
146  g_clear_pointer(&self->platform_message_handlers, g_hash_table_unref);
147 
148  G_OBJECT_CLASS(fl_binary_messenger_parent_class)->dispose(object);
149 }
static void engine_weak_notify_cb(gpointer user_data, GObject *where_the_object_was)

◆ fl_binary_messenger_init()

static void fl_binary_messenger_init ( FlBinaryMessenger *  self)
static

Definition at line 155 of file fl_binary_messenger.cc.

References platform_message_handler_free().

155  {
156  self->platform_message_handlers = g_hash_table_new_full(
157  g_str_hash, g_str_equal, g_free, platform_message_handler_free);
158 }
static void platform_message_handler_free(gpointer data)

◆ fl_binary_messenger_new()

FlBinaryMessenger* fl_binary_messenger_new ( FlEngine *  engine)

fl_binary_messenger_new: : The #FlEngine to communicate with.

Creates a new #FlBinaryMessenger. The binary messenger will take control of the engines platform message handler.

Returns: a new #FlBinaryMessenger.

Definition at line 160 of file fl_binary_messenger.cc.

References engine, engine_weak_notify_cb(), fl_binary_messenger_platform_message_cb(), and fl_engine_set_platform_message_handler().

Referenced by fl_engine_init(), TEST(), and test_lock_event().

160  {
161  g_return_val_if_fail(FL_IS_ENGINE(engine), nullptr);
162 
163  FlBinaryMessenger* self = FL_BINARY_MESSENGER(
164  g_object_new(fl_binary_messenger_get_type(), nullptr));
165 
166  self->engine = engine;
167  g_object_weak_ref(G_OBJECT(engine), engine_weak_notify_cb, self);
168 
171 
172  return self;
173 }
static gboolean fl_binary_messenger_platform_message_cb(FlEngine *engine, const gchar *channel, GBytes *message, const FlutterPlatformMessageResponseHandle *response_handle, void *user_data)
static void engine_weak_notify_cb(gpointer user_data, GObject *where_the_object_was)
void fl_engine_set_platform_message_handler(FlEngine *self, FlEnginePlatformMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_engine.cc:514

◆ fl_binary_messenger_platform_message_cb()

static gboolean fl_binary_messenger_platform_message_cb ( FlEngine *  engine,
const gchar *  channel,
GBytes *  message,
const FlutterPlatformMessageResponseHandle response_handle,
void *  user_data 
)
static

Definition at line 116 of file fl_binary_messenger.cc.

References FALSE, fl_binary_messenger_response_handle_new(), PlatformMessageHandler::message_handler, PlatformMessageHandler::message_handler_data, and TRUE.

Referenced by fl_binary_messenger_new().

121  {
122  FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data);
123 
124  PlatformMessageHandler* handler = static_cast<PlatformMessageHandler*>(
125  g_hash_table_lookup(self->platform_message_handlers, channel));
126  if (handler == nullptr) {
127  return FALSE;
128  }
129 
130  g_autoptr(FlBinaryMessengerResponseHandle) handle =
131  fl_binary_messenger_response_handle_new(self, response_handle);
132  handler->message_handler(self, channel, message, handle,
133  handler->message_handler_data);
134 
135  return TRUE;
136 }
void * user_data
FlBinaryMessengerMessageHandler message_handler
static FlBinaryMessengerResponseHandle * fl_binary_messenger_response_handle_new(FlBinaryMessenger *messenger, const FlutterPlatformMessageResponseHandle *response_handle)
return FALSE

◆ fl_binary_messenger_response_handle_class_init()

static void fl_binary_messenger_response_handle_class_init ( FlBinaryMessengerResponseHandleClass *  klass)
static

Definition at line 57 of file fl_binary_messenger.cc.

58  {
59  G_OBJECT_CLASS(klass)->dispose = fl_binary_messenger_response_handle_dispose;
60 }

◆ fl_binary_messenger_response_handle_init()

static void fl_binary_messenger_response_handle_init ( FlBinaryMessengerResponseHandle *  self)
static

Definition at line 62 of file fl_binary_messenger.cc.

63  {}

◆ fl_binary_messenger_response_handle_new()

static FlBinaryMessengerResponseHandle* fl_binary_messenger_response_handle_new ( FlBinaryMessenger *  messenger,
const FlutterPlatformMessageResponseHandle response_handle 
)
static

Definition at line 65 of file fl_binary_messenger.cc.

Referenced by fl_binary_messenger_platform_message_cb().

67  {
68  FlBinaryMessengerResponseHandle* self = FL_BINARY_MESSENGER_RESPONSE_HANDLE(
69  g_object_new(fl_binary_messenger_response_handle_get_type(), nullptr));
70 
71  self->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
72  self->response_handle = response_handle;
73 
74  return self;
75 }

◆ fl_binary_messenger_send_on_channel()

G_MODULE_EXPORT void fl_binary_messenger_send_on_channel ( FlBinaryMessenger *  messenger,
const gchar *  channel,
GBytes *  message,
GCancellable *  cancellable,
GAsyncReadyCallback  callback,
gpointer  user_data 
)

fl_binary_messenger_send_on_channel: : an #FlBinaryMessenger. : channel to send to. : (allow-none): message buffer to send or NULL for an empty message. : (allow-none): a #GCancellable or NULL. : (scope async): a #GAsyncReadyCallback to call when the request is satisfied. : (closure): user data to pass to .

Asynchronously sends a platform message.

Definition at line 242 of file fl_binary_messenger.cc.

References fl_engine_send_platform_message(), and platform_message_ready_cb().

Referenced by fl_basic_message_channel_send(), fl_event_channel_send(), fl_event_channel_send_end_of_stream(), fl_event_channel_send_error(), fl_method_channel_invoke_method(), and TEST().

248  {
249  g_return_if_fail(FL_IS_BINARY_MESSENGER(self));
250  g_return_if_fail(channel != nullptr);
251 
252  if (self->engine == nullptr) {
253  return;
254  }
255 
257  self->engine, channel, message, cancellable,
258  callback != nullptr ? platform_message_ready_cb : nullptr,
259  callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
260  : nullptr);
261 }
void * user_data
FlKeyEvent FlKeyResponderAsyncCallback callback
void fl_engine_send_platform_message(FlEngine *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_engine.cc:598
static void platform_message_ready_cb(GObject *object, GAsyncResult *result, gpointer user_data)

◆ fl_binary_messenger_send_on_channel_finish()

G_MODULE_EXPORT GBytes* fl_binary_messenger_send_on_channel_finish ( FlBinaryMessenger *  messenger,
GAsyncResult *  result,
GError **  error 
)

fl_binary_messenger_send_on_channel_finish: : an #FlBinaryMessenger.

Returns
: a #GAsyncResult. : (allow-none): #GError location to store the error occurring, or NULL to ignore.

Completes request started with fl_binary_messenger_send_on_channel().

Returns: (transfer full): message response on success or NULL on error.

Definition at line 263 of file fl_binary_messenger.cc.

References FALSE, and fl_engine_send_platform_message_finish().

Referenced by echo_response_cb(), failure_response_cb(), fl_basic_message_channel_send_finish(), fl_method_channel_invoke_method_finish(), and nullptr_response_cb().

266  {
267  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(self), FALSE);
268  g_return_val_if_fail(g_task_is_valid(result, self), FALSE);
269 
270  g_autoptr(GTask) task = G_TASK(result);
271  GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task, nullptr));
272 
273  if (self->engine == nullptr) {
274  return nullptr;
275  }
276 
277  return fl_engine_send_platform_message_finish(self->engine, r, error);
278 }
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
return FALSE
GBytes * fl_engine_send_platform_message_finish(FlEngine *self, GAsyncResult *result, GError **error)
Definition: fl_engine.cc:657

◆ fl_binary_messenger_send_response()

G_MODULE_EXPORT gboolean fl_binary_messenger_send_response ( FlBinaryMessenger *  messenger,
FlBinaryMessengerResponseHandle *  response_handle,
GBytes *  response,
GError **  error 
)

fl_binary_messenger_send_response: : an #FlBinaryMessenger. : handle that was provided in a FlBinaryMessengerMessageHandler. : (allow-none): response to send or NULL for an empty response. : (allow-none): #GError location to store the error occurring, or NULL to ignore.

Responds to a platform message.

Returns: TRUE on success.

Definition at line 206 of file fl_binary_messenger.cc.

References FALSE, FL_BINARY_MESSENGER_ERROR, FL_BINARY_MESSENGER_ERROR_ALREADY_RESPONDED, fl_engine_send_platform_message_response(), result, and TRUE.

Referenced by cancel_exception_response_cb(), fl_basic_message_channel_respond(), fl_method_channel_respond(), listen_exception_response_cb(), message_cb(), method_call_error_response_cb(), method_call_not_implemented_response_cb(), method_call_success_response_cb(), response_cb(), and send_events_events_cb().

210  {
211  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(self), FALSE);
212  g_return_val_if_fail(response_handle != nullptr, FALSE);
213  g_return_val_if_fail(response_handle->messenger == self, FALSE);
214  g_return_val_if_fail(response_handle->response_handle != nullptr, FALSE);
215 
216  if (self->engine == nullptr) {
217  return TRUE;
218  }
219 
220  if (response_handle->response_handle == nullptr) {
221  g_set_error(
224  "Attempted to respond to a message that is already responded to");
225  return FALSE;
226  }
227 
229  self->engine, response_handle->response_handle, response, error);
230  response_handle->response_handle = nullptr;
231 
232  return result;
233 }
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
gboolean fl_engine_send_platform_message_response(FlEngine *self, const FlutterPlatformMessageResponseHandle *handle, GBytes *response, GError **error)
Definition: fl_engine.cc:566
#define FL_BINARY_MESSENGER_ERROR
return FALSE

◆ fl_binary_messenger_set_message_handler_on_channel()

G_MODULE_EXPORT void fl_binary_messenger_set_message_handler_on_channel ( FlBinaryMessenger *  messenger,
const gchar *  channel,
FlBinaryMessengerMessageHandler  handler,
gpointer  user_data,
GDestroyNotify  destroy_notify 
)

fl_binary_messenger_set_platform_message_handler: : an #FlBinaryMessenger. : channel to listen on. : (allow-none): function to call when a message is received on this channel or NULL to disable a handler : (closure): user data to pass to . : (allow-none): a function which gets called to free , or NULL.

Sets the function called when a platform message is received on the given channel. See FlBinaryMessengerMessageHandler for details on how to respond to messages.

The handler is removed if the channel is closed or is replaced by another handler, set if you want to detect this.

Definition at line 175 of file fl_binary_messenger.cc.

References platform_message_handler_new().

Referenced by fl_basic_message_channel_dispose(), fl_basic_message_channel_new(), fl_event_channel_dispose(), fl_event_channel_new(), fl_method_channel_dispose(), fl_method_channel_new(), and TEST().

180  {
181  g_return_if_fail(FL_IS_BINARY_MESSENGER(self));
182  g_return_if_fail(channel != nullptr);
183 
184  // Don't set handlers if engine already gone.
185  if (self->engine == nullptr) {
186  if (handler != nullptr) {
187  g_warning(
188  "Attempted to set message handler on an FlBinaryMessenger without an "
189  "engine");
190  }
191  if (destroy_notify != nullptr) {
192  destroy_notify(user_data);
193  }
194  return;
195  }
196 
197  if (handler != nullptr) {
198  g_hash_table_replace(
199  self->platform_message_handlers, g_strdup(channel),
200  platform_message_handler_new(handler, user_data, destroy_notify));
201  } else {
202  g_hash_table_remove(self->platform_message_handlers, channel);
203  }
204 }
void * user_data
static PlatformMessageHandler * platform_message_handler_new(FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)

◆ G_DEFINE_QUARK()

G_DEFINE_QUARK ( fl_binary_messenger_codec_error_quark  ,
fl_binary_messenger_codec_error   
)

Definition at line 13 of file fl_binary_messenger.cc.

References engine, and G_DEFINE_TYPE().

16  {
17  GObject parent_instance;
18 
19  FlEngine* engine;
20 
21  // PlatformMessageHandler keyed by channel name.
22  GHashTable* platform_message_handlers;
23 };

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlBinaryMessengerResponseHandle  ,
fl_binary_messenger_response_handle  ,
G_TYPE_OBJECT   
)

Definition at line 38 of file fl_binary_messenger.cc.

Referenced by G_DEFINE_QUARK().

42  {
43  FlBinaryMessengerResponseHandle* self =
44  FL_BINARY_MESSENGER_RESPONSE_HANDLE(object);
45 
46  if (self->response_handle != nullptr && self->messenger->engine != nullptr) {
47  g_critical("FlBinaryMessengerResponseHandle was not responded to");
48  }
49 
50  g_clear_object(&self->messenger);
51  self->response_handle = nullptr;
52 
53  G_OBJECT_CLASS(fl_binary_messenger_response_handle_parent_class)
54  ->dispose(object);
55 }

◆ platform_message_handler_free()

static void platform_message_handler_free ( gpointer  data)
static

Definition at line 95 of file fl_binary_messenger.cc.

References PlatformMessageHandler::message_handler_destroy_notify.

Referenced by engine_weak_notify_cb(), and fl_binary_messenger_init().

95  {
96  PlatformMessageHandler* self = static_cast<PlatformMessageHandler*>(data);
97  if (self->message_handler_destroy_notify) {
98  self->message_handler_destroy_notify(self->message_handler_data);
99  }
100  g_free(self);
101 }
GDestroyNotify message_handler_destroy_notify

◆ platform_message_handler_new()

static PlatformMessageHandler* platform_message_handler_new ( FlBinaryMessengerMessageHandler  handler,
gpointer  user_data,
GDestroyNotify  destroy_notify 
)
static

Definition at line 83 of file fl_binary_messenger.cc.

References PlatformMessageHandler::message_handler, and user_data.

Referenced by fl_binary_messenger_set_message_handler_on_channel().

86  {
87  PlatformMessageHandler* self = static_cast<PlatformMessageHandler*>(
88  g_malloc0(sizeof(PlatformMessageHandler)));
89  self->message_handler = handler;
90  self->message_handler_data = user_data;
91  self->message_handler_destroy_notify = destroy_notify;
92  return self;
93 }
void * user_data
FlBinaryMessengerMessageHandler message_handler

◆ platform_message_ready_cb()

static void platform_message_ready_cb ( GObject *  object,
GAsyncResult *  result,
gpointer  user_data 
)
static

Definition at line 235 of file fl_binary_messenger.cc.

Referenced by fl_binary_messenger_send_on_channel().

237  {
238  GTask* task = G_TASK(user_data);
239  g_task_return_pointer(task, result, g_object_unref);
240 }
void * user_data
GAsyncResult * result