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 *object)
 
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 *  object 
)
static

Definition at line 103 of file fl_binary_messenger.cc.

Referenced by fl_binary_messenger_dispose(), and fl_binary_messenger_new().

103  {
104  FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data);
105  self->engine = nullptr;
106 
107  // Disconnect any handlers.
108  g_hash_table_remove_all(self->platform_message_handlers);
109 }
G_BEGIN_DECLS FlValue gpointer user_data

◆ fl_binary_messenger_class_init()

static void fl_binary_messenger_class_init ( FlBinaryMessengerClass *  klass)
static

Definition at line 146 of file fl_binary_messenger.cc.

References fl_binary_messenger_dispose().

146  {
147  G_OBJECT_CLASS(klass)->dispose = fl_binary_messenger_dispose;
148 }
static void fl_binary_messenger_dispose(GObject *object)

◆ fl_binary_messenger_dispose()

static void fl_binary_messenger_dispose ( GObject *  object)
static

Definition at line 133 of file fl_binary_messenger.cc.

References engine_weak_notify_cb().

Referenced by fl_binary_messenger_class_init().

133  {
134  FlBinaryMessenger* self = FL_BINARY_MESSENGER(object);
135 
136  if (self->engine != nullptr) {
137  g_object_weak_unref(G_OBJECT(self->engine), engine_weak_notify_cb, self);
138  self->engine = nullptr;
139  }
140 
141  g_clear_pointer(&self->platform_message_handlers, g_hash_table_unref);
142 
143  G_OBJECT_CLASS(fl_binary_messenger_parent_class)->dispose(object);
144 }
static void engine_weak_notify_cb(gpointer user_data, GObject *object)

◆ fl_binary_messenger_init()

static void fl_binary_messenger_init ( FlBinaryMessenger *  self)
static

Definition at line 150 of file fl_binary_messenger.cc.

References platform_message_handler_free().

150  {
151  self->platform_message_handlers = g_hash_table_new_full(
152  g_str_hash, g_str_equal, g_free, platform_message_handler_free);
153 }
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 155 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().

155  {
156  g_return_val_if_fail(FL_IS_ENGINE(engine), nullptr);
157 
158  FlBinaryMessenger* self = FL_BINARY_MESSENGER(
159  g_object_new(fl_binary_messenger_get_type(), nullptr));
160 
161  self->engine = engine;
162  g_object_weak_ref(G_OBJECT(engine), engine_weak_notify_cb, self);
163 
166 
167  return self;
168 }
static gboolean fl_binary_messenger_platform_message_cb(FlEngine *engine, const gchar *channel, GBytes *message, const FlutterPlatformMessageResponseHandle *response_handle, void *user_data)
void fl_engine_set_platform_message_handler(FlEngine *self, FlEnginePlatformMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_engine.cc:433
static void engine_weak_notify_cb(gpointer user_data, GObject *object)

◆ 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 111 of file fl_binary_messenger.cc.

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

Referenced by fl_binary_messenger_new().

116  {
117  FlBinaryMessenger* self = FL_BINARY_MESSENGER(user_data);
118 
119  PlatformMessageHandler* handler = static_cast<PlatformMessageHandler*>(
120  g_hash_table_lookup(self->platform_message_handlers, channel));
121  if (handler == nullptr) {
122  return FALSE;
123  }
124 
125  g_autoptr(FlBinaryMessengerResponseHandle) handle =
126  fl_binary_messenger_response_handle_new(self, response_handle);
127  handler->message_handler(self, channel, message, handle,
128  handler->message_handler_data);
129 
130  return TRUE;
131 }
G_BEGIN_DECLS FlValue gpointer user_data
FlBinaryMessengerMessageHandler message_handler
return TRUE
Definition: fl_view.cc:107
static FlBinaryMessengerResponseHandle * fl_binary_messenger_response_handle_new(FlBinaryMessenger *messenger, const FlutterPlatformMessageResponseHandle *response_handle)

◆ 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 237 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().

243  {
244  g_return_if_fail(FL_IS_BINARY_MESSENGER(self));
245  g_return_if_fail(channel != nullptr);
246 
247  if (self->engine == nullptr) {
248  return;
249  }
250 
252  self->engine, channel, message, cancellable,
253  callback != nullptr ? platform_message_ready_cb : nullptr,
254  callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
255  : nullptr);
256 }
G_BEGIN_DECLS FlValue gpointer user_data
void fl_engine_send_platform_message(FlEngine *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_engine.cc:483
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 258 of file fl_binary_messenger.cc.

References 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().

261  {
262  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(self), FALSE);
263  g_return_val_if_fail(g_task_is_valid(result, self), FALSE);
264 
265  g_autoptr(GTask) task = G_TASK(result);
266  GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task, nullptr));
267 
268  if (self->engine == nullptr) {
269  return nullptr;
270  }
271 
272  return fl_engine_send_platform_message_finish(self->engine, r, error);
273 }
FlMethodResponse GError ** error
GBytes * fl_engine_send_platform_message_finish(FlEngine *self, GAsyncResult *result, GError **error)
Definition: fl_engine.cc:540

◆ 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 201 of file fl_binary_messenger.cc.

References FL_BINARY_MESSENGER_ERROR, FL_BINARY_MESSENGER_ERROR_ALREADY_RESPONDED, fl_engine_send_platform_message_response(), 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().

205  {
206  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(self), FALSE);
207  g_return_val_if_fail(response_handle != nullptr, FALSE);
208  g_return_val_if_fail(response_handle->messenger == self, FALSE);
209  g_return_val_if_fail(response_handle->response_handle != nullptr, FALSE);
210 
211  if (self->engine == nullptr) {
212  return TRUE;
213  }
214 
215  if (response_handle->response_handle == nullptr) {
216  g_set_error(
219  "Attempted to respond to a message that is already responded to");
220  return FALSE;
221  }
222 
224  self->engine, response_handle->response_handle, response, error);
225  response_handle->response_handle = nullptr;
226 
227  return result;
228 }
FlMethodResponse GError ** error
return TRUE
Definition: fl_view.cc:107
gboolean fl_engine_send_platform_message_response(FlEngine *self, const FlutterPlatformMessageResponseHandle *handle, GBytes *response, GError **error)
Definition: fl_engine.cc:451
#define FL_BINARY_MESSENGER_ERROR

◆ 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 170 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().

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

◆ 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 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 }
G_BEGIN_DECLS FlValue gpointer 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 230 of file fl_binary_messenger.cc.

Referenced by fl_binary_messenger_send_on_channel().

232  {
233  GTask* task = G_TASK(user_data);
234  g_task_return_pointer(task, result, g_object_unref);
235 }
G_BEGIN_DECLS FlValue gpointer user_data