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

Commit 73f55dc

Browse files
authored
[Linux] Fix channel buffers control commands error handling (#45056)
## Description This PR fixes a mistake I made on #44636 where the error handling code wrongly relied on `fl_method_channel_invoke_method_finish` instead of `fl_binary_messenger_send_on_channel_finish`. The error handling code was not called when running the tests added in #44636 so this mistake did not pop up. @robert-ancell I added a test that simulates an error response and I had to rely on `g_idle_add` to make it works. Is this approach ok? ## Related Issue Linux implementation for flutter/flutter#132386 ## Tests Adds one test.
1 parent ced8f1a commit 73f55dc

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

shell/platform/linux/fl_binary_messenger.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,14 @@ static GBytes* send_on_channel_finish(FlBinaryMessenger* messenger,
324324
static gboolean finish_method(GObject* object,
325325
GAsyncResult* result,
326326
GError** error) {
327-
g_autoptr(FlMethodResponse) response = fl_method_channel_invoke_method_finish(
328-
FL_METHOD_CHANNEL(object), result, error);
327+
g_autoptr(GBytes) response = fl_binary_messenger_send_on_channel_finish(
328+
FL_BINARY_MESSENGER(object), result, error);
329329
if (response == nullptr) {
330330
return FALSE;
331331
}
332-
return fl_method_response_get_result(response, error) != nullptr;
332+
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
333+
return fl_method_codec_decode_response(FL_METHOD_CODEC(codec), response,
334+
error) != nullptr;
333335
}
334336

335337
// Called when a response is received for the resize channel message.

shell/platform/linux/fl_binary_messenger_test.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,56 @@ TEST(FlBinaryMessengerTest, AllowOverflowChannel) {
501501
EXPECT_TRUE(called);
502502
}
503503

504+
static gboolean quit_main_loop_cb(gpointer user_data) {
505+
g_main_loop_quit(static_cast<GMainLoop*>(user_data));
506+
return FALSE;
507+
}
508+
509+
// Checks if error returned when invoking a command on the control channel
510+
// are handled.
511+
TEST(FlBinaryMessengerTest, ControlChannelErrorResponse) {
512+
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
513+
g_autoptr(FlEngine) engine = make_mock_engine();
514+
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
515+
516+
g_autoptr(GError) error = nullptr;
517+
EXPECT_TRUE(fl_engine_start(engine, &error));
518+
EXPECT_EQ(error, nullptr);
519+
520+
FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
521+
522+
bool called = false;
523+
524+
FlutterEngineSendPlatformMessageFnPtr old_handler =
525+
embedder_api->SendPlatformMessage;
526+
embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
527+
SendPlatformMessage,
528+
([&called, old_handler, loop](auto engine,
529+
const FlutterPlatformMessage* message) {
530+
// Expect to receive a message on the "control" channel.
531+
if (strcmp(message->channel, "dev.flutter/channel-buffers") != 0) {
532+
return old_handler(engine, message);
533+
}
534+
535+
called = true;
536+
537+
// Register a callback to quit the main loop when binary messenger work
538+
// ends.
539+
g_idle_add(quit_main_loop_cb, loop);
540+
541+
// Simulates an internal error.
542+
return kInvalidArguments;
543+
}));
544+
545+
fl_binary_messenger_set_allow_channel_overflow(messenger, "flutter/test",
546+
true);
547+
548+
EXPECT_TRUE(called);
549+
550+
// Blocks here until quit_main_loop_cb is called.
551+
g_main_loop_run(loop);
552+
}
553+
504554
// NOLINTEND(clang-analyzer-core.StackAddressEscape)
505555

506556
struct RespondsOnBackgroundThreadInfo {

0 commit comments

Comments
 (0)