Flutter Engine
fl_method_channel.h File Reference
#include <gio/gio.h>
#include <glib-object.h>
#include "fl_binary_messenger.h"
#include "fl_method_call.h"
#include "fl_method_codec.h"
#include "fl_method_response.h"

Go to the source code of this file.

Functions

G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (FlMethodChannel, fl_method_channel, FL, METHOD_CHANNEL, GObject) typedef void(*FlMethodChannelMethodCallHandler)(FlMethodChannel *channel
 
: a channel name.

fl_method_channel_new: : an #FlBinaryMessenger.

: the method codec.

Creates a new method channel. must match the codec used on the Dart end of the channel.

Returns: a new #FlMethodChannel.

FlMethodChannel * fl_method_channel_new (FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
 
void fl_method_channel_set_method_call_handler (FlMethodChannel *channel, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
 
void fl_method_channel_invoke_method (FlMethodChannel *channel, const gchar *method, FlValue *args, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
 
FlMethodResponse * fl_method_channel_invoke_method_finish (FlMethodChannel *channel, GAsyncResult *result, GError **error)
 

Variables

G_BEGIN_DECLS FlMethodCall * method_call
 
G_BEGIN_DECLS FlMethodCall gpointer user_data
 

Function Documentation

◆ fl_method_channel_invoke_method()

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

176  {
177  g_return_if_fail(FL_IS_METHOD_CHANNEL(self));
178  g_return_if_fail(method != nullptr);
179 
180  g_autoptr(GTask) task =
181  callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
182  : nullptr;
183 
184  g_autoptr(GError) error = nullptr;
185  g_autoptr(GBytes) message =
186  fl_method_codec_encode_method_call(self->codec, method, args, &error);
187  if (message == nullptr) {
188  if (task != nullptr) {
189  g_task_return_error(task, error);
190  }
191  return;
192  }
193 
195  self->messenger, self->name, message, cancellable,
196  callback != nullptr ? message_response_cb : nullptr,
197  g_steal_pointer(&task));
198 }
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)
const uint8_t uint32_t uint32_t GError ** error
void * user_data
FlKeyEvent FlKeyResponderAsyncCallback callback
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()

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 200 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(), method_response_cb(), not_implemented_response_cb(), and nullptr_args_response_cb().

203  {
204  g_return_val_if_fail(FL_IS_METHOD_CHANNEL(self), nullptr);
205  g_return_val_if_fail(g_task_is_valid(result, self), nullptr);
206 
207  g_autoptr(GTask) task = G_TASK(result);
208  GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task, nullptr));
209 
210  g_autoptr(GBytes) response =
212  if (response == nullptr) {
213  return nullptr;
214  }
215 
216  return fl_method_codec_decode_response(self->codec, response, error);
217 }
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
GAsyncResult * result
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)

◆ fl_method_channel_new()

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

Definition at line 120 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().

123  {
124  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
125  g_return_val_if_fail(name != nullptr, nullptr);
126  g_return_val_if_fail(FL_IS_METHOD_CODEC(codec), nullptr);
127 
128  FlMethodChannel* self =
129  FL_METHOD_CHANNEL(g_object_new(fl_method_channel_get_type(), nullptr));
130 
131  self->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
132  self->name = g_strdup(name);
133  self->codec = FL_METHOD_CODEC(g_object_ref(codec));
134 
136  self->messenger, self->name, message_cb, g_object_ref(self),
138 
139  return self;
140 }
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_set_method_call_handler()

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

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

◆ G_DECLARE_FINAL_TYPE()

G_BEGIN_DECLS G_DECLARE_FINAL_TYPE ( FlMethodChannel  ,
fl_method_channel  ,
FL  ,
METHOD_CHANNEL  ,
GObject   
)

FlMethodChannel:

#FlMethodChannel is an object that allows method calls to and from Dart code.

The following example shows how to call and handle methods on a channel. See #FlMethodResponse for how to handle errors in more detail.

|[ static FlMethodChannel *channel = NULL;

static void method_call_cb (FlMethodChannel* channel, FlMethodCall* method_call, gpointer user_data) { g_autoptr(FlMethodResponse) response = NULL; if (strcmp (fl_method_call_get_name (method_call), "Foo.bar") == 0) { g_autoptr(GError) bar_error = NULL; g_autoptr(FlValue) result = do_bar (fl_method_call_get_args (method_call), &bar_error); if (result == NULL) response = FL_METHOD_RESPONSE (fl_method_error_response_new ("bar error", bar_error->message)); else response = FL_METHOD_RESPONSE (fl_method_success_response_new (result)); } else response = FL_METHOD_RESPONSE (fl_method_not_implemented_response_new ());

g_autoptr(GError) error = NULL; if (!fl_method_call_respond(method_call, response)) g_warning ("Failed to send response: %s", error->message); }

static void method_response_cb(GObject *object, GAsyncResult *result, gpointer user_data) { g_autoptr(GError) error = NULL; g_autoptr(FlMethodResponse) response = fl_method_channel_invoke_method_finish (FL_METHOD_CODEC (object), result, &error); if (response == NULL) { g_warning ("Failed to call method: %s", error->message); return; }

g_autoptr(FlValue) value = fl_method_response_get_result (response, &error); if (response == NULL) { g_warning ("Method returned error: %s", error->message); return; }

use_result (value); }

static void call_method () { g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new (); channel = fl_method_channel_new(messenger, "flutter/foo", FL_METHOD_CODEC (codec)); fl_method_channel_set_method_call_handler (channel, method_call_cb, NULL, NULL);

g_autoptr(FlValue) args = fl_value_new_string ("Hello World"); fl_method_channel_invoke_method (channel, "Foo.foo", args, cancellable, method_response_cb, NULL); } ]|

#FlMethodChannel matches the MethodChannel class in the Flutter services library. FlMethodChannelMethodCallHandler: : an #FlMethodChannel. : an #FlMethodCall. : (closure): data provided when registering this handler.

Function called when a method call is received. Respond to the method call with fl_method_call_respond(). If the response is not occurring in this callback take a reference to and release that once it has been responded to. Failing to respond before the last reference to is dropped is a programming error.

Variable Documentation

◆ method_call

◆ user_data

G_BEGIN_DECLS FlMethodCall gpointer user_data

Definition at line 113 of file fl_method_channel.h.