From 5235913825cfa1d504d7bda0ce88168041e05310 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Wed, 11 Oct 2023 11:51:48 +1300 Subject: [PATCH] Make handle objects for async responses, and result objects for sync responses --- .../pigeon/example/app/linux/messages.g.cc | 250 ++++++++++++++---- .../pigeon/example/app/linux/messages.g.h | 79 +++--- .../example/app/linux/my_application.cc | 31 +-- 3 files changed, 255 insertions(+), 105 deletions(-) diff --git a/packages/pigeon/example/app/linux/messages.g.cc b/packages/pigeon/example/app/linux/messages.g.cc index 8f5d6787682c..bd10e6aa47a8 100644 --- a/packages/pigeon/example/app/linux/messages.g.cc +++ b/packages/pigeon/example/app/linux/messages.g.cc @@ -70,6 +70,171 @@ FlValue* my_message_data_get_data(MyMessageData* object) { return object->data; } +struct _MyExampleHostApiGetHostLanguageResponse { + GObject parent_instance; + + FlValue* value; +}; + +G_DEFINE_TYPE(MyExampleHostApiGetHostLanguageResponse, + my_example_host_api_get_host_language_response, G_TYPE_OBJECT) + +static void my_example_host_api_get_host_language_dispose(GObject* object) { + MyExampleHostApiGetHostLanguageResponse* self = + MY_EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE(object); + g_clear_object(&self->value); + G_OBJECT_CLASS(my_example_host_api_get_host_language_response_parent_class) + ->dispose(object); +} + +static void my_example_host_api_get_host_language_response_init( + MyExampleHostApiGetHostLanguageResponse* self) {} + +static void my_example_host_api_get_host_language_response_class_init( + MyExampleHostApiGetHostLanguageResponseClass* klass) { + GObjectClass* object_class = G_OBJECT_CLASS(klass); + object_class->dispose = my_example_host_api_get_host_language_dispose; +} + +MyExampleHostApiGetHostLanguageResponse* +my_example_host_api_get_host_language_response_new(const gchar* value) { + MyExampleHostApiGetHostLanguageResponse* self = + MY_EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE(g_object_new( + my_example_host_api_get_host_language_response_get_type(), nullptr)); + self->value = fl_value_new_list(); + fl_value_append_take(self->value, fl_value_new_string(value)); + return self; +} + +MyExampleHostApiGetHostLanguageResponse* +my_example_host_api_get_host_language_response_new_error(const gchar* code, + const gchar* message, + FlValue* details) { + MyExampleHostApiGetHostLanguageResponse* self = + MY_EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE(g_object_new( + my_example_host_api_get_host_language_response_get_type(), nullptr)); + self->value = fl_value_new_list(); + fl_value_append_take(self->value, fl_value_new_string(code)); + fl_value_append_take(self->value, fl_value_new_string(message)); + fl_value_append(self->value, details); + return self; +} + +struct _MyExampleHostApiAddResponse { + GObject parent_instance; + + FlValue* value; +}; + +G_DEFINE_TYPE(MyExampleHostApiAddResponse, my_example_host_api_add_response, + G_TYPE_OBJECT) + +static void my_example_host_api_add_response_dispose(GObject* object) { + MyExampleHostApiAddResponse* self = MY_EXAMPLE_HOST_API_ADD_RESPONSE(object); + g_clear_object(&self->value); + G_OBJECT_CLASS(my_example_host_api_add_response_parent_class) + ->dispose(object); +} + +static void my_example_host_api_add_response_init( + MyExampleHostApiAddResponse* self) {} + +static void my_example_host_api_add_response_class_init( + MyExampleHostApiAddResponseClass* klass) { + GObjectClass* object_class = G_OBJECT_CLASS(klass); + object_class->dispose = my_example_host_api_add_response_dispose; +} + +MyExampleHostApiAddResponse* my_example_host_api_add_response_new( + int64_t value) { + MyExampleHostApiAddResponse* self = MY_EXAMPLE_HOST_API_ADD_RESPONSE( + g_object_new(my_example_host_api_add_response_get_type(), nullptr)); + self->value = fl_value_new_list(); + fl_value_append_take(self->value, fl_value_new_int(value)); + return self; +} + +MyExampleHostApiAddResponse* my_example_host_api_add_response_new_error( + const gchar* code, const gchar* message, FlValue* details) { + MyExampleHostApiAddResponse* self = MY_EXAMPLE_HOST_API_ADD_RESPONSE( + g_object_new(my_example_host_api_add_response_get_type(), nullptr)); + self->value = fl_value_new_list(); + fl_value_append_take(self->value, fl_value_new_string(code)); + fl_value_append_take(self->value, fl_value_new_string(message)); + fl_value_append(self->value, details); + return self; +} + +struct _MyExampleHostApiSendMessageResponseHandle { + GObject parent_instance; + + FlBasicMessageChannel* channel; + FlBasicMessageChannelResponseHandle* response_handle; +}; + +G_DEFINE_TYPE(MyExampleHostApiSendMessageResponseHandle, + my_example_host_api_send_message_response_handle, G_TYPE_OBJECT) + +static void my_example_host_api_send_message_response_handle_dispose( + GObject* object) { + MyExampleHostApiSendMessageResponseHandle* self = + MY_EXAMPLE_HOST_API_SEND_MESSAGE_RESPONSE_HANDLE(object); + g_clear_object(&self->channel); + g_clear_object(&self->response_handle); + G_OBJECT_CLASS(my_example_host_api_send_message_response_handle_parent_class) + ->dispose(object); +} + +static void my_example_host_api_send_message_response_handle_init( + MyExampleHostApiSendMessageResponseHandle* self) {} + +static void my_example_host_api_send_message_response_handle_class_init( + MyExampleHostApiSendMessageResponseHandleClass* klass) { + GObjectClass* object_class = G_OBJECT_CLASS(klass); + object_class->dispose = + my_example_host_api_send_message_response_handle_dispose; +} + +MyExampleHostApiSendMessageResponseHandle* +my_example_host_api_send_message_response_handle_new( + FlBasicMessageChannel* channel, + FlBasicMessageChannelResponseHandle* response_handle) { + MyExampleHostApiSendMessageResponseHandle* self = + MY_EXAMPLE_HOST_API_SEND_MESSAGE_RESPONSE_HANDLE(g_object_new( + my_example_host_api_send_message_response_handle_get_type(), + nullptr)); + self->channel = g_object_ref(channel); + self->response_handle = g_object_ref(response_handle); + return self; +} + +void my_example_host_api_send_message_response_handle_respond( + MyExampleHostApiSendMessageResponseHandle* self, gboolean result) { + g_autoptr(FlValue) value = fl_value_new_list(); + fl_value_append_take(value, fl_value_new_bool(result)); + + g_autoptr(GError) error = NULL; + if (!fl_basic_message_channel_respond(self->channel, self->response_handle, + value, &error)) { + g_warning("Failed to send response to SendMessage: %s\n", error->message); + } +} + +void my_example_host_api_send_message_response_handle_respond_error( + MyExampleHostApiSendMessageResponseHandle* self, const gchar* code, + const gchar* message, FlValue* details) { + g_autoptr(FlValue) value = fl_value_new_list(); + fl_value_append_take(value, fl_value_new_string(code)); + fl_value_append_take(value, fl_value_new_string(message)); + fl_value_append(value, details); + + g_autoptr(GError) error = NULL; + if (!fl_basic_message_channel_respond(self->channel, self->response_handle, + value, &error)) { + g_warning("Failed to send response to SendMessage: %s\n", error->message); + } +} + struct _MyExampleHostApi { GObject parent_instance; @@ -89,8 +254,22 @@ static void get_host_language_cb( FlBasicMessageChannel* channel, FlValue* message, FlBasicMessageChannelResponseHandle* response_handle, gpointer user_data) { MyExampleHostApi* self = MY_EXAMPLE_HOST_API(user_data); - if (self->vtable->get_host_language != nullptr) { - self->vtable->get_host_language(self, response_handle, self->user_data); + if (self->vtable->get_host_language == nullptr) { + return; + } + + g_autoptr(MyExampleHostApiGetHostLanguageResponse) response = + self->vtable->get_host_language(self, self->user_data); + if (response == nullptr) { + g_warning("No response returned to GetHostLanguage"); + return; + } + + g_autoptr(GError) error = NULL; + if (!fl_basic_message_channel_respond(channel, response_handle, + response->value, &error)) { + g_warning("Failed to send response to GetHostLanguage: %s\n", + error->message); } } @@ -99,8 +278,21 @@ static void add_cb(FlBasicMessageChannel* channel, FlValue* message, gpointer user_data) { MyExampleHostApi* self = MY_EXAMPLE_HOST_API(user_data); int64_t a = 0, b = 0; - if (self->vtable->add != nullptr) { - self->vtable->add(self, response_handle, a, b, self->user_data); + if (self->vtable->add == nullptr) { + return; + } + + g_autoptr(MyExampleHostApiAddResponse) response = + self->vtable->add(self, a, b, self->user_data); + if (response == nullptr) { + g_warning("No response returned to Add"); + return; + } + + g_autoptr(GError) error = NULL; + if (!fl_basic_message_channel_respond(channel, response_handle, + response->value, &error)) { + g_warning("Failed to send response to Add: %s\n", error->message); } } @@ -112,8 +304,10 @@ static void send_message_cb( g_autoptr(MyMessageData) message_data = my_message_data_new(MY_CODE_ONE, data); if (self->vtable->send_message != nullptr) { - self->vtable->send_message(self, response_handle, message_data, - self->user_data); + g_autoptr(MyExampleHostApiSendMessageResponseHandle) handle = + my_example_host_api_send_message_response_handle_new(channel, + response_handle); + self->vtable->send_message(self, handle, message_data, self->user_data); } } @@ -170,50 +364,6 @@ MyExampleHostApi* my_example_host_api_new(FlBinaryMessenger* messenger, return self; } -gboolean my_example_host_api_respond_get_host_language( - MyExampleHostApi* self, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* result, - GError** error) { - g_autoptr(FlValue) message = fl_value_new_list(); - fl_value_append_take(message, fl_value_new_string(result)); - return fl_basic_message_channel_respond(self->get_host_language_channel, - response_handle, message, error); -} - -gboolean my_example_host_api_respond_error_get_host_language( - MyExampleHostApi* self, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* code, - const gchar* message_, FlValue* details, GError** error) { - g_autoptr(FlValue) message = fl_value_new_list(); - fl_value_append_take(message, fl_value_new_string(code)); - fl_value_append_take(message, fl_value_new_string(message_)); - fl_value_append(message, details); - return fl_basic_message_channel_respond(self->get_host_language_channel, - response_handle, message, error); -} - -gboolean my_example_host_api_respond_add( - MyExampleHostApi* self, - FlBasicMessageChannelResponseHandle* response_handle, int64_t result, - GError** error) { - g_autoptr(FlValue) message = fl_value_new_list(); - fl_value_append_take(message, fl_value_new_int(result)); - return fl_basic_message_channel_respond(self->add_channel, response_handle, - message, error); -} - -gboolean my_example_host_api_respond_error_add( - MyExampleHostApi* self, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* code, - const gchar* message_, FlValue* details, GError** error) { - g_autoptr(FlValue) message = fl_value_new_list(); - fl_value_append_take(message, fl_value_new_string(code)); - fl_value_append_take(message, fl_value_new_string(message_)); - fl_value_append(message, details); - return fl_basic_message_channel_respond(self->add_channel, response_handle, - message, error); -} - gboolean my_example_host_api_respond_send_message( MyExampleHostApi* self, FlBasicMessageChannelResponseHandle* response_handle, gboolean result, diff --git a/packages/pigeon/example/app/linux/messages.g.h b/packages/pigeon/example/app/linux/messages.g.h index 5e9e3d520c25..08a04bf84008 100644 --- a/packages/pigeon/example/app/linux/messages.g.h +++ b/packages/pigeon/example/app/linux/messages.g.h @@ -29,19 +29,52 @@ MyCode my_message_data_get_code(MyMessageData* object); FlValue* my_message_data_get_data(MyMessageData* object); +G_DECLARE_FINAL_TYPE(MyExampleHostApiGetHostLanguageResponse, + my_example_host_api_get_host_language_response, MY, + EXAMPLE_HOST_API_GET_HOST_LANGUAGE_RESPONSE, GObject) + +MyExampleHostApiGetHostLanguageResponse* +my_example_host_api_get_host_language_response_new(const gchar* value); + +MyExampleHostApiGetHostLanguageResponse* +my_example_host_api_get_host_language_response_new_error(const gchar* code, + const gchar* message, + FlValue* details); + +G_DECLARE_FINAL_TYPE(MyExampleHostApiAddResponse, + my_example_host_api_add_response, MY, + EXAMPLE_HOST_API_ADD_RESPONSE, GObject) + +MyExampleHostApiAddResponse* my_example_host_api_add_response_new( + int64_t value); + +MyExampleHostApiAddResponse* my_example_host_api_add_response_new_error( + const gchar* code, const gchar* message, FlValue* details); + +G_DECLARE_FINAL_TYPE(MyExampleHostApiSendMessageResponseHandle, + my_example_host_api_send_message_response_handle, MY, + EXAMPLE_HOST_API_SEND_MESSAGE_RESPONSE_HANDLE, GObject) + +void my_example_host_api_send_message_response_handle_respond( + MyExampleHostApiSendMessageResponseHandle* response_handle, + gboolean result); + +void my_example_host_api_send_message_response_handle_respond_error( + MyExampleHostApiSendMessageResponseHandle* response_handle, + const gchar* code, const gchar* message, FlValue* details); + G_DECLARE_FINAL_TYPE(MyExampleHostApi, my_example_host_api, MY, EXAMPLE_HOST_API, GObject) typedef struct { - void (*get_host_language)( + MyExampleHostApiGetHostLanguageResponse* (*get_host_language)( + MyExampleHostApi* object, gpointer user_data); + MyExampleHostApiAddResponse* (*add)(MyExampleHostApi* object, int64_t a, + int64_t b, gpointer user_data); + void (*send_message)( MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, gpointer user_data); - void (*add)(MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, int64_t a, - int64_t b, gpointer user_data); - void (*send_message)(MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, - MyMessageData* message, gpointer user_data); + MyExampleHostApiSendMessageResponseHandle* response_handle, + MyMessageData* message, gpointer user_data); } MyExampleHostApiVTable; MyExampleHostApi* my_example_host_api_new(FlBinaryMessenger* messenger, @@ -49,36 +82,6 @@ MyExampleHostApi* my_example_host_api_new(FlBinaryMessenger* messenger, gpointer user_data, GDestroyNotify user_data_free_func); -gboolean my_example_host_api_respond_get_host_language( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* result, - GError** error); - -gboolean my_example_host_api_respond_error_get_host_language( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* code, - const gchar* message, FlValue* details, GError** error); - -gboolean my_example_host_api_respond_add( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, int64_t result, - GError** error); - -gboolean my_example_host_api_respond_error_add( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* code, - const gchar* message, FlValue* details, GError** error); - -gboolean my_example_host_api_respond_send_message( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, gboolean result, - GError** error); - -gboolean my_example_host_api_respond_error_send_message( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, const gchar* code, - const gchar* message, FlValue* details, GError** error); - G_DECLARE_FINAL_TYPE(MyMessageFlutterApi, my_message_flutter_api, MY, MESSAGE_FLUTTER_API, GObject) diff --git a/packages/pigeon/example/app/linux/my_application.cc b/packages/pigeon/example/app/linux/my_application.cc index 493f2fef14fe..a6bca897bf4f 100644 --- a/packages/pigeon/example/app/linux/my_application.cc +++ b/packages/pigeon/example/app/linux/my_application.cc @@ -17,40 +17,37 @@ struct _MyApplication { G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) -static void handle_get_host_language( - MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, gpointer user_data) { - my_example_host_api_respond_get_host_language(object, response_handle, "C", - nullptr); +static MyExampleHostApiGetHostLanguageResponse* handle_get_host_language( + MyExampleHostApi* object, gpointer user_data) { + return my_example_host_api_get_host_language_response_new("C"); } -static void handle_add(MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, - int64_t a, int64_t b, gpointer user_data) { +static MyExampleHostApiAddResponse* handle_add(MyExampleHostApi* object, + int64_t a, int64_t b, + gpointer user_data) { if (a < 0 || b < 0) { g_autoptr(FlValue) details = fl_value_new_string("details"); - my_example_host_api_respond_error_add(object, response_handle, "code", - "message", details, nullptr); - return; + return my_example_host_api_add_response_new_error("code", "message", + details); } - my_example_host_api_respond_add(object, response_handle, a + b, nullptr); + return my_example_host_api_add_response_new(a + b); } static void handle_send_message( MyExampleHostApi* object, - FlBasicMessageChannelResponseHandle* response_handle, + MyExampleHostApiSendMessageResponseHandle* response_handle, MyMessageData* message, gpointer user_data) { MyCode code = my_message_data_get_code(message); if (code == MY_CODE_ONE) { g_autoptr(FlValue) details = fl_value_new_string("details"); - my_example_host_api_respond_error_send_message( - object, response_handle, "code", "message", details, nullptr); + my_example_host_api_send_message_response_handle_respond_error( + response_handle, "code", "message", details); return; } - my_example_host_api_respond_send_message(object, response_handle, TRUE, - nullptr); + my_example_host_api_send_message_response_handle_respond(response_handle, + TRUE); } // Implements GApplication::activate.