Flutter Engine
fl_method_channel.cc File Reference
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
#include <gmodule.h>
#include "flutter/shell/platform/linux/fl_method_call_private.h"
#include "flutter/shell/platform/linux/fl_method_channel_private.h"
#include "flutter/shell/platform/linux/fl_method_codec_private.h"

Go to the source code of this file.

Classes

struct  _FlMethodChannel
 

Functions

G_MODULE_EXPORT GType fl_method_channel_get_type ()
 
static void message_cb (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
 
static void message_response_cb (GObject *object, GAsyncResult *result, gpointer user_data)
 
static void channel_closed_cb (gpointer user_data)
 
static void fl_method_channel_dispose (GObject *object)
 
static void fl_method_channel_class_init (FlMethodChannelClass *klass)
 
static void fl_method_channel_init (FlMethodChannel *self)
 
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new (FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
 
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler (FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
 
G_MODULE_EXPORT void fl_method_channel_invoke_method (FlMethodChannel *self, const gchar *method, FlValue *args, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
 
G_MODULE_EXPORT FlMethodResponse * fl_method_channel_invoke_method_finish (FlMethodChannel *self, GAsyncResult *result, GError **error)
 
gboolean fl_method_channel_respond (FlMethodChannel *self, FlBinaryMessengerResponseHandle *response_handle, FlMethodResponse *response, GError **error)
 

Function Documentation

◆ channel_closed_cb()

static void channel_closed_cb ( gpointer  user_data)
static

Definition at line 74 of file fl_method_channel.cc.

References TRUE.

Referenced by fl_method_channel_new().

74  {
75  g_autoptr(FlMethodChannel) self = FL_METHOD_CHANNEL(user_data);
76 
77  self->channel_closed = TRUE;
78 
79  // Disconnect handler.
80  if (self->method_call_handler_destroy_notify != nullptr) {
81  self->method_call_handler_destroy_notify(self->method_call_handler_data);
82  }
83  self->method_call_handler = nullptr;
84  self->method_call_handler_data = nullptr;
85  self->method_call_handler_destroy_notify = nullptr;
86 }
G_BEGIN_DECLS FlValue gpointer user_data
return TRUE
Definition: fl_view.cc:107

◆ fl_method_channel_class_init()

static void fl_method_channel_class_init ( FlMethodChannelClass *  klass)
static

Definition at line 110 of file fl_method_channel.cc.

References fl_method_channel_dispose().

110  {
111  G_OBJECT_CLASS(klass)->dispose = fl_method_channel_dispose;
112 }
static void fl_method_channel_dispose(GObject *object)

◆ fl_method_channel_dispose()

static void fl_method_channel_dispose ( GObject *  object)
static

Definition at line 88 of file fl_method_channel.cc.

References fl_binary_messenger_set_message_handler_on_channel().

Referenced by fl_method_channel_class_init().

88  {
89  FlMethodChannel* self = FL_METHOD_CHANNEL(object);
90 
91  if (self->messenger != nullptr) {
93  self->messenger, self->name, nullptr, nullptr, nullptr);
94  }
95 
96  g_clear_object(&self->messenger);
97  g_clear_pointer(&self->name, g_free);
98  g_clear_object(&self->codec);
99 
100  if (self->method_call_handler_destroy_notify != nullptr) {
101  self->method_call_handler_destroy_notify(self->method_call_handler_data);
102  }
103  self->method_call_handler = nullptr;
104  self->method_call_handler_data = nullptr;
105  self->method_call_handler_destroy_notify = nullptr;
106 
107  G_OBJECT_CLASS(fl_method_channel_parent_class)->dispose(object);
108 }
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)

◆ fl_method_channel_get_type()

G_MODULE_EXPORT GType fl_method_channel_get_type ( )

Referenced by fl_method_channel_new().

◆ fl_method_channel_init()

static void fl_method_channel_init ( FlMethodChannel *  self)
static

Definition at line 114 of file fl_method_channel.cc.

114 {}

◆ fl_method_channel_invoke_method()

G_MODULE_EXPORT void fl_method_channel_invoke_method ( FlMethodChannel *  channel,
const gchar *  method,
FlValue args,
GCancellable *  cancellable,
GAsyncReadyCallback  callback,
gpointer  user_data 
)

fl_method_channel_invoke_method: : an #FlMethodChannel. : the method to call. : (allow-none): arguments to the method, must match what the #FlMethodCodec supports. : (allow-none): a #GCancellable or NULL. : (scope async): (allow-none): a #GAsyncReadyCallback to call when the request is satisfied or NULL to ignore the response. : (closure): user data to pass to .

Calls a method on this channel.

Definition at line 166 of file fl_method_channel.cc.

References error, fl_binary_messenger_send_on_channel(), fl_method_codec_encode_method_call(), and message_response_cb().

Referenced by cancel_channel(), listen_channel(), perform_action(), TEST(), and update_editing_state().

172  {
173  g_return_if_fail(FL_IS_METHOD_CHANNEL(self));
174  g_return_if_fail(method != nullptr);
175 
176  g_autoptr(GTask) task =
177  callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
178  : nullptr;
179 
180  g_autoptr(GError) error = nullptr;
181  g_autoptr(GBytes) message =
182  fl_method_codec_encode_method_call(self->codec, method, args, &error);
183  if (message == nullptr) {
184  if (task != nullptr) {
185  g_task_return_error(task, error);
186  }
187  return;
188  }
189 
191  self->messenger, self->name, message, cancellable,
192  callback != nullptr ? message_response_cb : nullptr,
193  g_steal_pointer(&task));
194 }
G_BEGIN_DECLS FlValue * args
static void message_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
FlMethodResponse GError ** error
G_BEGIN_DECLS FlValue 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)

◆ fl_method_channel_invoke_method_finish()

G_MODULE_EXPORT FlMethodResponse* fl_method_channel_invoke_method_finish ( FlMethodChannel *  channel,
GAsyncResult *  result,
GError **  error 
)

fl_method_channel_invoke_method_finish: : an #FlMethodChannel.

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

Completes request started with fl_method_channel_invoke_method().

Returns: (transfer full): an #FlMethodResponse or NULL on error.

Definition at line 196 of file fl_method_channel.cc.

References fl_binary_messenger_send_on_channel_finish(), and fl_method_codec_decode_response().

Referenced by error_response_cb(), failure_response_cb(), finish_method(), method_response_cb(), not_implemented_response_cb(), and nullptr_args_response_cb().

199  {
200  g_return_val_if_fail(FL_IS_METHOD_CHANNEL(self), nullptr);
201  g_return_val_if_fail(g_task_is_valid(result, self), nullptr);
202 
203  g_autoptr(GTask) task = G_TASK(result);
204  GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task, nullptr));
205 
206  g_autoptr(GBytes) response =
208  if (response == nullptr) {
209  return nullptr;
210  }
211 
212  return fl_method_codec_decode_response(self->codec, response, error);
213 }
FlMethodResponse GError ** error
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)

◆ fl_method_channel_new()

G_MODULE_EXPORT FlMethodChannel* fl_method_channel_new ( FlBinaryMessenger *  messenger,
const gchar *  name,
FlMethodCodec *  codec 
)

Definition at line 116 of file fl_method_channel.cc.

References channel_closed_cb(), fl_binary_messenger_set_message_handler_on_channel(), fl_method_channel_get_type(), and message_cb().

Referenced by cancel_channel(), fl_mouse_cursor_plugin_new(), fl_platform_plugin_new(), fl_text_input_plugin_new(), listen_channel(), and TEST().

119  {
120  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
121  g_return_val_if_fail(name != nullptr, nullptr);
122  g_return_val_if_fail(FL_IS_METHOD_CODEC(codec), nullptr);
123 
124  FlMethodChannel* self =
125  FL_METHOD_CHANNEL(g_object_new(fl_method_channel_get_type(), nullptr));
126 
127  self->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
128  self->name = g_strdup(name);
129  self->codec = FL_METHOD_CODEC(g_object_ref(codec));
130 
132  self->messenger, self->name, message_cb, g_object_ref(self),
134 
135  return self;
136 }
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)
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
G_MODULE_EXPORT GType fl_method_channel_get_type()
const char * name
Definition: fuchsia.cc:50
static void channel_closed_cb(gpointer user_data)

◆ fl_method_channel_respond()

gboolean fl_method_channel_respond ( FlMethodChannel *  channel,
FlBinaryMessengerResponseHandle *  response_handle,
FlMethodResponse *  response,
GError **  error 
)

fl_method_channel_respond: : an #FlMethodChannel. : an #FlBinaryMessengerResponseHandle. : an #FlMethodResponse. : (allow-none): #GError location to store the error occurring, or NULL to ignore.

Responds to a method call.

Returns: TRUE on success.

Definition at line 215 of file fl_method_channel.cc.

References fl_binary_messenger_send_response(), fl_method_codec_encode_error_envelope(), fl_method_codec_encode_success_envelope(), fl_method_error_response_get_code(), fl_method_error_response_get_details(), fl_method_error_response_get_message(), and fl_method_success_response_get_result().

Referenced by fl_method_call_respond(), fl_method_call_respond_error(), fl_method_call_respond_not_implemented(), and fl_method_call_respond_success().

219  {
220  g_return_val_if_fail(FL_IS_METHOD_CHANNEL(self), FALSE);
221  g_return_val_if_fail(FL_IS_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle),
222  FALSE);
223  g_return_val_if_fail(FL_IS_METHOD_SUCCESS_RESPONSE(response) ||
224  FL_IS_METHOD_ERROR_RESPONSE(response) ||
225  FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response),
226  FALSE);
227 
228  g_autoptr(GBytes) message = nullptr;
229  if (FL_IS_METHOD_SUCCESS_RESPONSE(response)) {
230  FlMethodSuccessResponse* r = FL_METHOD_SUCCESS_RESPONSE(response);
233  if (message == nullptr) {
234  return FALSE;
235  }
236  } else if (FL_IS_METHOD_ERROR_RESPONSE(response)) {
237  FlMethodErrorResponse* r = FL_METHOD_ERROR_RESPONSE(response);
239  self->codec, fl_method_error_response_get_code(r),
242  if (message == nullptr) {
243  return FALSE;
244  }
245  } else if (FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response)) {
246  message = nullptr;
247  } else {
248  g_assert_not_reached();
249  }
250 
251  return fl_binary_messenger_send_response(self->messenger, response_handle,
252  message, error);
253 }
FlMethodResponse GError ** error
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
GBytes * fl_method_codec_encode_error_envelope(FlMethodCodec *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
GBytes * fl_method_codec_encode_success_envelope(FlMethodCodec *self, FlValue *result, GError **error)
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)

◆ fl_method_channel_set_method_call_handler()

G_MODULE_EXPORT void fl_method_channel_set_method_call_handler ( FlMethodChannel *  channel,
FlMethodChannelMethodCallHandler  handler,
gpointer  user_data,
GDestroyNotify  destroy_notify 
)

fl_method_channel_set_method_call_handler: : an #FlMethodChannel. : function to call when a method call is received on this channel. : (closure): user data to pass to . : (allow-none): a function which gets called to free , or NULL.

Sets the function called when a method call is received from the Dart side of the channel. See #FlMethodChannelMethodCallHandler for details on how to respond to method calls.

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 138 of file fl_method_channel.cc.

References user_data.

Referenced by fl_mouse_cursor_plugin_new(), fl_platform_plugin_new(), fl_text_input_plugin_new(), and TEST().

142  {
143  g_return_if_fail(FL_IS_METHOD_CHANNEL(self));
144 
145  // Don't set handler if channel closed.
146  if (self->channel_closed) {
147  if (handler != nullptr) {
148  g_warning(
149  "Attempted to set method call handler on a closed FlMethodChannel");
150  }
151  if (destroy_notify != nullptr) {
152  destroy_notify(user_data);
153  }
154  return;
155  }
156 
157  if (self->method_call_handler_destroy_notify != nullptr) {
158  self->method_call_handler_destroy_notify(self->method_call_handler_data);
159  }
160 
161  self->method_call_handler = handler;
162  self->method_call_handler_data = user_data;
163  self->method_call_handler_destroy_notify = destroy_notify;
164 }
G_BEGIN_DECLS FlValue gpointer user_data

◆ message_cb()

static void message_cb ( FlBinaryMessenger *  messenger,
const gchar *  channel,
GBytes *  message,
FlBinaryMessengerResponseHandle *  response_handle,
gpointer  user_data 
)
static

Definition at line 40 of file fl_method_channel.cc.

References args, error, fl_method_call_new(), fl_method_codec_decode_method_call(), and method_call.

Referenced by fl_method_channel_new().

44  {
45  FlMethodChannel* self = FL_METHOD_CHANNEL(user_data);
46 
47  if (self->method_call_handler == nullptr) {
48  return;
49  }
50 
51  g_autofree gchar* method = nullptr;
52  g_autoptr(FlValue) args = nullptr;
53  g_autoptr(GError) error = nullptr;
54  if (!fl_method_codec_decode_method_call(self->codec, message, &method, &args,
55  &error)) {
56  g_warning("Failed to decode method call: %s", error->message);
57  return;
58  }
59 
60  g_autoptr(FlMethodCall) method_call =
61  fl_method_call_new(method, args, self, response_handle);
62  self->method_call_handler(self, method_call, self->method_call_handler_data);
63 }
G_BEGIN_DECLS FlValue * args
G_BEGIN_DECLS FlMethodCall * method_call
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
FlMethodCall * fl_method_call_new(const gchar *name, FlValue *args, FlMethodChannel *channel, FlBinaryMessengerResponseHandle *response_handle)
G_BEGIN_DECLS FlValue gpointer user_data
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)

◆ message_response_cb()

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

Definition at line 66 of file fl_method_channel.cc.

Referenced by fl_method_channel_invoke_method().

68  {
69  GTask* task = G_TASK(user_data);
70  g_task_return_pointer(task, result, g_object_unref);
71 }
G_BEGIN_DECLS FlValue gpointer user_data