Flutter Engine
fl_method_channel.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
6 
7 #include <gmodule.h>
8 
9 #include "flutter/shell/platform/linux/fl_method_call_private.h"
10 #include "flutter/shell/platform/linux/fl_method_channel_private.h"
11 #include "flutter/shell/platform/linux/fl_method_codec_private.h"
12 
14  GObject parent_instance;
15 
16  // Messenger to communicate on.
17  FlBinaryMessenger* messenger;
18 
19  // TRUE if the channel has been closed.
20  gboolean channel_closed;
21 
22  // Channel name.
23  gchar* name;
24 
25  // Codec to en/decode messages.
26  FlMethodCodec* codec;
27 
28  // Function called when a method call is received.
29  FlMethodChannelMethodCallHandler method_call_handler;
32 };
33 
34 // Added here to stop the compiler from optimising this function away.
35 G_MODULE_EXPORT GType fl_method_channel_get_type();
36 
37 G_DEFINE_TYPE(FlMethodChannel, fl_method_channel, G_TYPE_OBJECT)
38 
39 // Called when a binary message is received on this channel.
40 static void message_cb(FlBinaryMessenger* messenger,
41  const gchar* channel,
42  GBytes* message,
43  FlBinaryMessengerResponseHandle* response_handle,
44  gpointer user_data) {
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 }
64 
65 // Called when a response is received to a sent message.
66 static void message_response_cb(GObject* object,
67  GAsyncResult* result,
68  gpointer user_data) {
69  GTask* task = G_TASK(user_data);
70  g_task_return_pointer(task, result, g_object_unref);
71 }
72 
73 // Called when the channel handler is closed.
74 static void channel_closed_cb(gpointer user_data) {
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 }
87 
88 static void fl_method_channel_dispose(GObject* object) {
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 }
109 
110 static void fl_method_channel_class_init(FlMethodChannelClass* klass) {
111  G_OBJECT_CLASS(klass)->dispose = fl_method_channel_dispose;
112 }
113 
114 static void fl_method_channel_init(FlMethodChannel* self) {}
115 
116 G_MODULE_EXPORT FlMethodChannel* fl_method_channel_new(
117  FlBinaryMessenger* messenger,
118  const gchar* name,
119  FlMethodCodec* codec) {
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 }
137 
139  FlMethodChannel* self,
140  FlMethodChannelMethodCallHandler handler,
141  gpointer user_data,
142  GDestroyNotify destroy_notify) {
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 }
165 
166 G_MODULE_EXPORT void fl_method_channel_invoke_method(
167  FlMethodChannel* self,
168  const gchar* method,
169  FlValue* args,
170  GCancellable* cancellable,
171  GAsyncReadyCallback callback,
172  gpointer user_data) {
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 }
195 
196 G_MODULE_EXPORT FlMethodResponse* fl_method_channel_invoke_method_finish(
197  FlMethodChannel* self,
198  GAsyncResult* result,
199  GError** error) {
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 =
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 }
214 
216  FlMethodChannel* self,
217  FlBinaryMessengerResponseHandle* response_handle,
218  FlMethodResponse* response,
219  GError** error) {
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);
232  self->codec, fl_method_success_response_get_result(r), error);
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 }
gboolean fl_method_channel_respond(FlMethodChannel *self, FlBinaryMessengerResponseHandle *response_handle, FlMethodResponse *response, GError **error)
GDestroyNotify method_call_handler_destroy_notify
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_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)
G_BEGIN_DECLS FlMethodCall * method_call
G_MODULE_EXPORT FlMethodResponse * fl_method_channel_invoke_method_finish(FlMethodChannel *self, GAsyncResult *result, GError **error)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
G_MODULE_EXPORT GBytes * fl_binary_messenger_send_on_channel_finish(FlBinaryMessenger *self, GAsyncResult *result, GError **error)
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
FlMethodCall * fl_method_call_new(const gchar *name, FlValue *args, FlMethodChannel *channel, FlBinaryMessengerResponseHandle *response_handle)
static void fl_method_channel_class_init(FlMethodChannelClass *klass)
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
G_MODULE_EXPORT GType fl_method_channel_get_type()
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
FlMethodCodec * codec
static void fl_method_channel_init(FlMethodChannel *self)
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
FlBinaryMessenger * messenger
FlMethodChannelMethodCallHandler method_call_handler
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
static void fl_method_channel_dispose(GObject *object)
G_BEGIN_DECLS FlMethodCall gpointer user_data
gpointer method_call_handler_data
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
G_MODULE_EXPORT void fl_method_channel_invoke_method(FlMethodChannel *self, const gchar *method, FlValue *args, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
static void channel_closed_cb(gpointer user_data)
return TRUE
Definition: fl_view.cc:107
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
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)
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)