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.


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)


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 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 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);
176  g_autoptr(GTask) task =
177  callback != nullptr ? g_task_new(self, cancellable, callback, user_data)
178  : nullptr;
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  }
191  self->messenger, self->name, message, cancellable,
192  callback != nullptr ? message_response_cb : nullptr,
193  g_steal_pointer(&task));
194 }
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_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 FlMethodCall 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.

: #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);
203  g_autoptr(GTask) task = G_TASK(result);
204  GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task, nullptr));
206  g_autoptr(GBytes) response =
208  if (response == nullptr) {
209  return nullptr;
210  }
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()

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 fl_mouse_cursor_plugin_new(), fl_platform_plugin_new(), fl_text_input_plugin_new(), 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);
124  FlMethodChannel* self =
125  FL_METHOD_CHANNEL(g_object_new(fl_method_channel_get_type(), nullptr));
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));
132  self->messenger, self->name, message_cb, g_object_ref(self),
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_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 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));
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  }
157  if (self->method_call_handler_destroy_notify != nullptr) {
158  self->method_call_handler_destroy_notify(self->method_call_handler_data);
159  }
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 FlMethodCall gpointer user_data


fl_method_channel  ,
FL  ,


#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.

Referenced by flutter::testing::EmbedderTestBackingStoreProducer::Create(), CreateEmbedderRenderTarget(), flutter::CreateEmbedderTaskRunner(), flutter::testing::EmbedderTestTaskRunner::EmbedderTestTaskRunner(), flutter::enableSkiaTracingCallback(), engine_weak_notify_cb(), EngineClearContext(), EngineMakeContextCurrent(), EngineMakeResourceContextCurrent(), EngineOnFlutterPlatformMessage(), EnginePresent(), fl_basic_message_channel_set_message_handler(), fl_engine_gl_clear_current(), fl_engine_gl_get_fbo(), fl_engine_gl_make_current(), fl_engine_gl_make_resource_current(), fl_engine_gl_present(), fl_engine_gl_proc_resolver(), fl_engine_post_task(), fl_engine_runs_task_on_current_thread(), fl_engine_set_platform_message_handler(), fl_method_channel_set_method_call_handler(), FlutterDesktopMessengerSendWithReply(), FlutterEngineInitialize(), FlutterEnginePostDartObject(), FlutterPlatformMessageCreateResponseHandle(), flutter::testing::EmbedderTestContext::GetIsolateCreateCallbackHook(), flutter::testing::EmbedderTestContext::GetUpdateSemanticsCustomActionCallbackHook(), flutter::testing::EmbedderTestContext::GetUpdateSemanticsNodeCallbackHook(), InferExternalViewEmbedderFromArgs(), InferOpenGLPlatformViewCreationCallback(), InferSoftwarePlatformViewCreationCallback(), MakeSkSurfaceFromBackingStore(), flutter::Win32WindowProcDelegateManager::OnTopLevelWindowProc(), platform_message_handler_new(), flutter::Win32WindowProcDelegateManager::RegisterTopLevelWindowProcDelegate(), flutter::FlutterWindowsEngine::RunWithEntrypoint(), flutter::BinaryMessengerImpl::Send(), flutter::testing::EmbedderConfigBuilder::SetCompositor(), FlutterEngine::shutDownEngine, flutter::testing::TEST_F(), flutter::PluginRegistrarWindows::UnregisterTopLevelWindowProcDelegate(), and flutter::WriteServerErrorResponse().