| Flutter Linux Embedder |

Classes | Functions
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
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 71 of file fl_method_channel.cc.

71 {
72 g_autoptr(FlMethodChannel) self = FL_METHOD_CHANNEL(user_data);
73
74 self->channel_closed = TRUE;
75
76 // Disconnect handler.
77 if (self->method_call_handler_destroy_notify != nullptr) {
78 self->method_call_handler_destroy_notify(self->method_call_handler_data);
79 }
80 self->method_call_handler = nullptr;
81 self->method_call_handler_data = nullptr;
82 self->method_call_handler_destroy_notify = nullptr;
83 }
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53

References TRUE, and user_data.

Referenced by fl_method_channel_new().

## ◆ fl_method_channel_class_init()

static void fl_method_channel_class_init | ( | FlMethodChannelClass * | klass | ) |
| static

Definition at line 106 of file fl_method_channel.cc.

106 {
107 G_OBJECT_CLASS(klass)->dispose = fl_method_channel_dispose;
108 }
fl_method_channel_dispose
static void fl_method_channel_dispose(GObject *object)
Definition: fl_method_channel.cc:85

References fl_method_channel_dispose().

## ◆ fl_method_channel_dispose()

static void fl_method_channel_dispose | ( | GObject * | object | ) |
| static

Definition at line 85 of file fl_method_channel.cc.

85 {
86 FlMethodChannel* self = FL_METHOD_CHANNEL(object);
87
88 // Note we don't have to clear the handler in messenger as it holds
89 // a reference to this object so the following code is only run after
90 // the messenger has closed the channel already.
91
92 g_clear_object(&self->messenger);
93 g_clear_pointer(&self->name, g_free);
94 g_clear_object(&self->codec);
95
96 if (self->method_call_handler_destroy_notify != nullptr) {
97 self->method_call_handler_destroy_notify(self->method_call_handler_data);
98 }
99 self->method_call_handler = nullptr;
100 self->method_call_handler_data = nullptr;
101 self->method_call_handler_destroy_notify = nullptr;
102
103 G_OBJECT_CLASS(fl_method_channel_parent_class)->dispose(object);
104 }

Referenced by fl_method_channel_class_init().

## ◆ fl_method_channel_init()

static void fl_method_channel_init | ( | FlMethodChannel * | self | ) |
| static

Definition at line 110 of file fl_method_channel.cc.

110 {}

## ◆ 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: @channel: an #FlMethodChannel. @method: the method to call. @args: (allow-none): arguments to the method, must match what the #FlMethodCodec supports. @cancellable: (allow-none): a #GCancellable or NULL. @callback: (scope async): (allow-none): a #GAsyncReadyCallback to call when the request is satisfied or NULL to ignore the response. @user_data: (closure): user data to pass to @callback.

Calls a method on this channel.

Definition at line 162 of file fl_method_channel.cc.

168 {
169 g_return_if_fail(FL_IS_METHOD_CHANNEL(self));
170 g_return_if_fail(method != nullptr);
171
172 g_autoptr(GTask) task =
173 callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
174 : nullptr;
175
176 g_autoptr(GError) error = nullptr;
177 g_autoptr(GBytes) message =
178 fl_method_codec_encode_method_call(self->codec, method, args, &error);
179 if (message == nullptr) {
180 if (task != nullptr) {
181 g_task_return_error(task, g_error_copy(error));
182 }
183 return;
184 }
185
186 fl_binary_messenger_send_on_channel(
187 self->messenger, self->name, message, cancellable,
188 callback != nullptr ? message_response_cb : nullptr,
189 g_steal_pointer(&task));
190 }
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:435
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
message_response_cb
static void message_response_cb(GObject *object, GAsyncResult *result, gpointer user_data)
Definition: fl_method_channel.cc:63
fl_method_codec_encode_method_call
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
Definition: fl_method_codec.cc:16
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40

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

Referenced by fl_platform_channel_system_request_app_exit(), fl_text_input_channel_perform_action(), fl_text_input_channel_update_editing_state(), fl_text_input_channel_update_editing_state_with_deltas(), and TEST().

## ◆ 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: @channel: an #FlMethodChannel.

Returns
  : #GAsyncResult. @error: (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 192 of file fl_method_channel.cc.

195 {
196 g_return_val_if_fail(FL_IS_METHOD_CHANNEL(self), nullptr);
197 g_return_val_if_fail(g_task_is_valid(result, self), nullptr);
198
199 GTask* task = G_TASK(result);
200 g_autoptr(GAsyncResult) r =
201 G_ASYNC_RESULT(g_task_propagate_pointer(task, error));
202 if (r == nullptr) {
203 return nullptr;
204 }
205
206 g_autoptr(GBytes) response =
207 fl_binary_messenger_send_on_channel_finish(self->messenger, r, error);
208 if (response == nullptr) {
209 return nullptr;
210 }
211
212 return fl_method_codec_decode_response(self->codec, response, error);
213 }
fl_binary_messenger_send_on_channel_finish
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
Definition: fl_binary_messenger.cc:449
fl_method_codec_decode_response
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
Definition: fl_method_codec.cc:88

References error, fl_binary_messenger_send_on_channel_finish(), and fl_method_codec_decode_response().

Referenced by fl_platform_channel_system_request_app_exit_finish(), fl_text_input_channel_perform_action_finish(), fl_text_input_channel_update_editing_state_finish(), fl_text_input_channel_update_editing_state_with_deltas_finish(), and TEST().

## ◆ fl_method_channel_new()

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

Definition at line 112 of file fl_method_channel.cc.

115 {
116 g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
117 g_return_val_if_fail(name != nullptr, nullptr);
118 g_return_val_if_fail(FL_IS_METHOD_CODEC(codec), nullptr);
119
120 FlMethodChannel* self =
121 FL_METHOD_CHANNEL(g_object_new(fl_method_channel_get_type(), nullptr));
122
123 self->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
124 self->name = g_strdup(name);
125 self->codec = FL_METHOD_CODEC(g_object_ref(codec));
126
127 fl_binary_messenger_set_message_handler_on_channel(
128 self->messenger, self->name, message_cb, g_object_ref(self),
129 channel_closed_cb);
130
131 return self;
132 }
fl_binary_messenger_set_message_handler_on_channel
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)
Definition: fl_binary_messenger.cc:408
message_cb
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_method_channel.cc:37
channel_closed_cb
static void channel_closed_cb(gpointer user_data)
Definition: fl_method_channel.cc:71

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

Referenced by fl_keyboard_channel_new(), fl_mouse_cursor_channel_new(), fl_platform_channel_new(), fl_text_input_channel_new(), fl_windowing_channel_new(), and TEST().

## ◆ fl_method_channel_respond()

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

fl_method_channel_respond: @channel: an #FlMethodChannel. @response_handle: an #FlBinaryMessengerResponseHandle. @response: an #FlMethodResponse. @error: (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.

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_RESPONSE(response), FALSE);
224
225 g_autoptr(GBytes) message =
226 fl_method_codec_encode_response(self->codec, response, error);
227 if (message == nullptr) {
228 return FALSE;
229 }
230 return fl_binary_messenger_send_response(self->messenger, response_handle,
231 message, error);
232 }
fl_binary_messenger_send_response
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger.cc:422
fl_method_codec_encode_response
GBytes * fl_method_codec_encode_response(FlMethodCodec *self, FlMethodResponse *response, GError **error)
Definition: fl_method_codec.cc:62

References error, fl_binary_messenger_send_response(), and fl_method_codec_encode_response().

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

## ◆ 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: @channel: an #FlMethodChannel. @handler: function to call when a method call is received on this channel. @user_data: (closure): user data to pass to @handler. @destroy_notify: (allow-none): a function which gets called to free @user_data, 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 @destroy_notify if you want to detect this.

Definition at line 134 of file fl_method_channel.cc.

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

References user_data.

Referenced by fl_keyboard_channel_new(), fl_mouse_cursor_channel_new(), fl_platform_channel_new(), fl_text_input_channel_new(), fl_windowing_channel_new(), and TEST().

## ◆ message_cb()

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

Definition at line 37 of file fl_method_channel.cc.

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

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

Referenced by fl_method_channel_new().

## ◆ message_response_cb()

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

Definition at line 63 of file fl_method_channel.cc.

65 {
66 g_autoptr(GTask) task = G_TASK(user_data);
67 g_task_return_pointer(task, g_object_ref(result), g_object_unref);
68 }

References user_data.

Referenced by fl_method_channel_invoke_method().