Flutter Engine
The Flutter Engine
Functions | Variables
fl_method_channel.h File Reference
#include <gio/gio.h>
#include <glib-object.h>
#include <gmodule.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_MODULE_EXPORT G_DECLARE_FINAL_TYPE (FlMethodChannel, fl_method_channel, FL, METHOD_CHANNEL, GObject) typedef void(*FlMethodChannelMethodCallHandler)(FlMethodChannel *channel
 
: a channel name.

fl_method_channel_new: @messenger: an #FlBinaryMessenger.

@codec: the method codec.

Creates a new method channel. @codec 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 G_MODULE_EXPORT FlMethodCall * method_call
 
G_BEGIN_DECLS G_MODULE_EXPORT 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: @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 =
179 if (message == nullptr) {
180 if (task != nullptr) {
181 g_task_return_error(task, error);
182 }
183 return;
184 }
185
187 self->messenger, self->name, message, cancellable,
188 callback != nullptr ? message_response_cb : nullptr,
189 g_steal_pointer(&task));
190}
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_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
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
Win32Message message
void * 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: @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 g_autoptr(GTask) task = G_TASK(result);
200 GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task, nullptr));
201
202 g_autoptr(GBytes) response =
204 if (response == nullptr) {
205 return nullptr;
206 }
207
208 return fl_method_codec_decode_response(self->codec, response, error);
209}
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)
GAsyncResult * result

◆ fl_method_channel_new()

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
128 self->messenger, self->name, message_cb, g_object_ref(self),
130
131 return self;
132}
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)
static void channel_closed_cb(gpointer user_data)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32

◆ 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: @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}

◆ G_DECLARE_FINAL_TYPE()

G_BEGIN_DECLS G_MODULE_EXPORT 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, nullptr); } 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, &error)) 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: @channel: an #FlMethodChannel. @method_call: an #FlMethodCall. @user_data: (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 @method_call and release that once it has been responded to. Failing to respond before the last reference to @method_call is dropped is a programming error.

Variable Documentation

◆ method_call

G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall* method_call

Definition at line 120 of file fl_method_channel.h.

◆ user_data

G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall gpointer user_data

Definition at line 121 of file fl_method_channel.h.