Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2044ef9

Browse files
committed
[Linux] Allow BasicMessageChannel sending and responding to null message
1 parent 35fb29b commit 2044ef9

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

shell/platform/linux/fl_basic_message_channel.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ G_MODULE_EXPORT void fl_basic_message_channel_send(FlBasicMessageChannel* self,
232232
GAsyncReadyCallback callback,
233233
gpointer user_data) {
234234
g_return_if_fail(FL_IS_BASIC_MESSAGE_CHANNEL(self));
235-
g_return_if_fail(message != nullptr);
236235

237236
g_autoptr(GTask) task =
238237
callback != nullptr ? g_task_new(self, cancellable, callback, user_data)

shell/platform/linux/fl_basic_message_channel_test.cc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ TEST(FlBasicMessageChannelTest, SendMessageWithoutResponse) {
6262
}
6363
// NOLINTEND(clang-analyzer-core.StackAddressEscape)
6464

65-
// Called when the message response is received in the SendMessage test.
65+
// Called when the message response is received in the SendMessageWithResponse
66+
// test.
6667
static void echo_response_cb(GObject* object,
6768
GAsyncResult* result,
6869
gpointer user_data) {
@@ -189,3 +190,35 @@ TEST(FlBasicMessageChannelTest, ReceiveMessage) {
189190
// Blocks here until response_cb is called.
190191
g_main_loop_run(loop);
191192
}
193+
194+
// Called when the message response is received in the
195+
// SendNullMessageWithResponse test.
196+
static void null_message_response_cb(GObject* object,
197+
GAsyncResult* result,
198+
gpointer user_data) {
199+
g_autoptr(GError) error = nullptr;
200+
g_autoptr(FlValue) message = fl_basic_message_channel_send_finish(
201+
FL_BASIC_MESSAGE_CHANNEL(object), result, &error);
202+
EXPECT_NE(message, nullptr);
203+
EXPECT_EQ(error, nullptr);
204+
205+
EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_NULL);
206+
207+
g_main_loop_quit(static_cast<GMainLoop*>(user_data));
208+
}
209+
210+
// Checks sending a null message with a response works.
211+
TEST(FlBasicMessageChannelTest, SendNullMessageWithResponse) {
212+
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
213+
214+
g_autoptr(FlEngine) engine = make_mock_engine();
215+
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
216+
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
217+
g_autoptr(FlBasicMessageChannel) channel = fl_basic_message_channel_new(
218+
messenger, "test/echo", FL_MESSAGE_CODEC(codec));
219+
fl_basic_message_channel_send(channel, nullptr, nullptr,
220+
null_message_response_cb, loop);
221+
222+
// Blocks here until null_message_response_cb is called.
223+
g_main_loop_run(loop);
224+
}

shell/platform/linux/fl_standard_message_codec.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ static GBytes* fl_standard_message_codec_encode_message(FlMessageCodec* codec,
420420
static FlValue* fl_standard_message_codec_decode_message(FlMessageCodec* codec,
421421
GBytes* message,
422422
GError** error) {
423+
if (g_bytes_get_size(message) == 0) {
424+
return fl_value_new_null();
425+
}
426+
423427
FlStandardMessageCodec* self =
424428
reinterpret_cast<FlStandardMessageCodec*>(codec);
425429

shell/platform/linux/fl_standard_message_codec_test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ TEST(FlStandardMessageCodecTest, EncodeNull) {
6060
EXPECT_STREQ(hex_string, "00");
6161
}
6262

63+
TEST(FlStandardMessageCodecTest, DecodeNull) {
64+
// Regression test for https://github.com/flutter/flutter/issues/128704.
65+
66+
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
67+
g_autoptr(GBytes) data = g_bytes_new(nullptr, 0);
68+
g_autoptr(GError) error = nullptr;
69+
g_autoptr(FlValue) value =
70+
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
71+
72+
EXPECT_FALSE(value == nullptr);
73+
EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_NULL);
74+
}
75+
6376
static gchar* encode_bool(gboolean value) {
6477
g_autoptr(FlValue) v = fl_value_new_bool(value);
6578
return encode_message(v);

0 commit comments

Comments
 (0)