34 G_DEFINE_TYPE(FlMethodChannel, fl_method_channel, G_TYPE_OBJECT)
40 FlBinaryMessengerResponseHandle* response_handle,
42 FlMethodChannel*
self = FL_METHOD_CHANNEL(
user_data);
44 if (self->method_call_handler ==
nullptr) {
48 g_autofree gchar* method =
nullptr;
50 g_autoptr(GError)
error =
nullptr;
53 g_warning(
"Failed to decode method call: %s",
error->message);
59 self->method_call_handler(
self,
method_call, self->method_call_handler_data);
67 g_task_return_pointer(task,
result, g_object_unref);
72 g_autoptr(FlMethodChannel)
self = FL_METHOD_CHANNEL(
user_data);
74 self->channel_closed =
TRUE;
77 if (self->method_call_handler_destroy_notify !=
nullptr) {
78 self->method_call_handler_destroy_notify(self->method_call_handler_data);
80 self->method_call_handler =
nullptr;
81 self->method_call_handler_data =
nullptr;
82 self->method_call_handler_destroy_notify =
nullptr;
86 FlMethodChannel*
self = FL_METHOD_CHANNEL(
object);
92 g_clear_object(&self->messenger);
93 g_clear_pointer(&self->name, g_free);
94 g_clear_object(&self->codec);
96 if (self->method_call_handler_destroy_notify !=
nullptr) {
97 self->method_call_handler_destroy_notify(self->method_call_handler_data);
99 self->method_call_handler =
nullptr;
100 self->method_call_handler_data =
nullptr;
101 self->method_call_handler_destroy_notify =
nullptr;
103 G_OBJECT_CLASS(fl_method_channel_parent_class)->dispose(
object);
113 FlBinaryMessenger* messenger,
115 FlMethodCodec* codec) {
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);
120 FlMethodChannel*
self =
121 FL_METHOD_CHANNEL(g_object_new(fl_method_channel_get_type(),
nullptr));
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));
128 self->messenger, self->name,
message_cb, g_object_ref(
self),
135 FlMethodChannel*
self,
136 FlMethodChannelMethodCallHandler handler,
138 GDestroyNotify destroy_notify) {
139 g_return_if_fail(FL_IS_METHOD_CHANNEL(
self));
142 if (self->channel_closed) {
143 if (handler !=
nullptr) {
145 "Attempted to set method call handler on a closed FlMethodChannel");
147 if (destroy_notify !=
nullptr) {
153 if (self->method_call_handler_destroy_notify !=
nullptr) {
154 self->method_call_handler_destroy_notify(self->method_call_handler_data);
157 self->method_call_handler = handler;
158 self->method_call_handler_data =
user_data;
159 self->method_call_handler_destroy_notify = destroy_notify;
163 FlMethodChannel*
self,
166 GCancellable* cancellable,
169 g_return_if_fail(FL_IS_METHOD_CHANNEL(
self));
170 g_return_if_fail(method !=
nullptr);
172 g_autoptr(GTask) task =
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);
187 self->messenger, self->name, message, cancellable,
189 g_steal_pointer(&task));
193 FlMethodChannel*
self,
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);
199 g_autoptr(GTask) task = G_TASK(
result);
200 GAsyncResult* r = G_ASYNC_RESULT(g_task_propagate_pointer(task,
nullptr));
202 g_autoptr(GBytes) response =
204 if (response ==
nullptr) {
212 FlMethodChannel*
self,
213 FlBinaryMessengerResponseHandle* response_handle,
214 FlMethodResponse* response,
216 g_return_val_if_fail(FL_IS_METHOD_CHANNEL(
self), FALSE);
217 g_return_val_if_fail(FL_IS_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle),
219 g_return_val_if_fail(FL_IS_METHOD_SUCCESS_RESPONSE(response) ||
220 FL_IS_METHOD_ERROR_RESPONSE(response) ||
221 FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response),
224 g_autoptr(GBytes) message =
nullptr;
225 if (FL_IS_METHOD_SUCCESS_RESPONSE(response)) {
226 FlMethodSuccessResponse* r = FL_METHOD_SUCCESS_RESPONSE(response);
229 if (message ==
nullptr) {
232 }
else if (FL_IS_METHOD_ERROR_RESPONSE(response)) {
233 FlMethodErrorResponse* r = FL_METHOD_ERROR_RESPONSE(response);
238 if (message ==
nullptr) {
241 }
else if (FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response)) {
244 g_assert_not_reached();