Make pending event handling more lenient to allow out of order responses (flutter/engine#23504)

This PR makes the Linux key handling code a little more lenient when it comes to the order in which it receives responses to key events from the framework. I had assumed that there wasn't a case where responses could get out of order, but it seems that it is possible, given that you can mash on the keyboard and eventually get one out of order.

This changes the code so that instead of just looking at the first entry in the pending event deque, it searches the deque starting at the beginning to find the event, and remove it.
This commit is contained in:
Greg Spencer
2021-01-08 10:43:59 -08:00
committed by GitHub
parent f2a937ebba
commit 70bba34e51
3 changed files with 79 additions and 13 deletions

View File

@@ -161,25 +161,31 @@ static uint64_t get_event_id(GdkEventKey* event) {
// Finds an event in the event queue that was sent to the framework by its ID.
static GdkEventKey* find_pending_event(FlKeyEventPlugin* self, uint64_t id) {
if (self->pending_events->len == 0 ||
FL_KEY_EVENT_PAIR(g_ptr_array_index(self->pending_events, 0))->id != id) {
return nullptr;
for (guint i = 0; i <= self->pending_events->len; ++i) {
if (FL_KEY_EVENT_PAIR(g_ptr_array_index(self->pending_events, i))->id ==
id) {
return FL_KEY_EVENT_PAIR(g_ptr_array_index(self->pending_events, 0))
->event;
}
}
return FL_KEY_EVENT_PAIR(g_ptr_array_index(self->pending_events, 0))->event;
g_warning(
"Tried to find pending event with id %ld, but the event was not found.",
id);
return nullptr;
}
// Removes an event from the pending event queue.
static void remove_pending_event(FlKeyEventPlugin* self, uint64_t id) {
if (self->pending_events->len == 0 ||
FL_KEY_EVENT_PAIR(g_ptr_array_index(self->pending_events, 0))->id != id) {
g_warning("Tried to remove pending event with id %" PRIu64
", but the event was out of "
"order, or is unknown.",
id);
return;
for (guint i = 0; i <= self->pending_events->len; ++i) {
if (FL_KEY_EVENT_PAIR(g_ptr_array_index(self->pending_events, i))->id ==
id) {
g_ptr_array_remove_index(self->pending_events, i);
return;
}
}
g_ptr_array_remove_index(self->pending_events, 0);
g_warning(
"Tried to remove pending event with id %ld, but the event was not found.",
id);
}
// Adds an GdkEventKey to the pending event queue, with a unique ID, and the

View File

@@ -270,3 +270,43 @@ TEST(FlKeyEventPluginTest, TestKeyEventNotHandledByTextInputPlugin) {
// Blocks here until echo_response_cb is called.
g_main_loop_run(loop);
}
TEST(FlKeyEventPluginTest, TestKeyEventResponseOutOfOrderFromFramework) {
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
g_autoptr(FlEngine) engine = make_mock_engine();
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
g_autoptr(FlTextInputPlugin) text_input_plugin =
FL_TEXT_INPUT_PLUGIN(fl_mock_text_input_plugin_new(handle_keypress));
g_autoptr(FlKeyEventPlugin) plugin = fl_key_event_plugin_new(
messenger, text_input_plugin, echo_response_cb, "test/key-event-delayed");
GdkEventKey key_event = GdkEventKey{
GDK_KEY_PRESS, // event type
nullptr, // window (not needed)
FALSE, // event was sent explicitly
12345, // time
0x10, // modifier state
GDK_KEY_A, // key code
1, // length of string representation
nullptr, // string representation
0x04, // scan code
0, // keyboard group
0, // is a modifier
};
expected_value = "{handled: true}";
expected_handled = TRUE;
bool handled = fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
// Should always be true, because the event was delayed.
EXPECT_TRUE(handled);
// Send a second key event that will be out of order.
key_event.keyval = GDK_KEY_B;
key_event.hardware_keycode = 0x05;
handled = fl_key_event_plugin_send_key_event(plugin, &key_event, loop);
EXPECT_TRUE(handled);
// Blocks here until echo_response_cb is called.
g_main_loop_run(loop);
}

View File

@@ -323,6 +323,26 @@ FlutterEngineResult FlutterEngineSendPlatformMessage(
engine, message->channel, message->response_handle,
static_cast<const uint8_t*>(g_bytes_get_data(response, nullptr)),
g_bytes_get_size(response));
} else if (strcmp(message->channel, "test/key-event-delayed") == 0) {
static std::unique_ptr<const FlutterPlatformMessageResponseHandle>
delayed_response_handle = nullptr;
g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
g_autoptr(FlValue) handledValue = fl_value_new_map();
fl_value_set_string_take(handledValue, "handled", fl_value_new_bool(true));
g_autoptr(GBytes) response = fl_message_codec_encode_message(
FL_MESSAGE_CODEC(codec), handledValue, nullptr);
if (delayed_response_handle == nullptr) {
delayed_response_handle.reset(message->response_handle);
} else {
send_response(
engine, message->channel, message->response_handle,
static_cast<const uint8_t*>(g_bytes_get_data(response, nullptr)),
g_bytes_get_size(response));
send_response(
engine, message->channel, delayed_response_handle.release(),
static_cast<const uint8_t*>(g_bytes_get_data(response, nullptr)),
g_bytes_get_size(response));
}
}
return kSuccess;