From caf008b9f630534f70fb808a1819a24031508665 Mon Sep 17 00:00:00 2001 From: Niklas Schulze Date: Thu, 3 Mar 2022 22:16:11 +0100 Subject: [PATCH] Windows: Fix plugin destruction (flutter/engine#30760) --- .../shell/platform/windows/flutter_windows.cc | 2 +- .../windows/flutter_windows_engine.cc | 12 ++++--- .../platform/windows/flutter_windows_engine.h | 16 +++++----- .../flutter_windows_engine_unittests.cc | 31 +++++++++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index 81bf1eb6a7..0b9199f2a7 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -174,7 +174,7 @@ FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger( void FlutterDesktopPluginRegistrarSetDestructionHandler( FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopOnPluginRegistrarDestroyed callback) { - registrar->engine->SetPluginRegistrarDestructionCallback(callback); + registrar->engine->AddPluginRegistrarDestructionCallback(callback, registrar); } bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 8f79172f62..a36a87503d 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -346,8 +346,9 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) { bool FlutterWindowsEngine::Stop() { if (engine_) { - if (plugin_registrar_destruction_callback_) { - plugin_registrar_destruction_callback_(plugin_registrar_.get()); + for (const auto& [callback, registrar] : + plugin_registrar_destruction_callbacks_) { + callback(registrar); } FlutterEngineResult result = embedder_api_.Shutdown(engine_); engine_ = nullptr; @@ -395,9 +396,10 @@ FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() { return plugin_registrar_.get(); } -void FlutterWindowsEngine::SetPluginRegistrarDestructionCallback( - FlutterDesktopOnPluginRegistrarDestroyed callback) { - plugin_registrar_destruction_callback_ = callback; +void FlutterWindowsEngine::AddPluginRegistrarDestructionCallback( + FlutterDesktopOnPluginRegistrarDestroyed callback, + FlutterDesktopPluginRegistrarRef registrar) { + plugin_registrar_destruction_callbacks_[callback] = registrar; } void FlutterWindowsEngine::SendWindowMetricsEvent( diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index c7a47a2fcf..ccc915e08b 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -72,9 +72,10 @@ class FlutterWindowsEngine { // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef GetRegistrar(); - // Sets |callback| to be called when the plugin registrar is destroyed. - void SetPluginRegistrarDestructionCallback( - FlutterDesktopOnPluginRegistrarDestroyed callback); + // Registers |callback| to be called when the plugin registrar is destroyed. + void AddPluginRegistrarDestructionCallback( + FlutterDesktopOnPluginRegistrarDestroyed callback, + FlutterDesktopPluginRegistrarRef registrar); // Sets switches member to the given switches. void SetSwitches(const std::vector& switches); @@ -221,10 +222,11 @@ class FlutterWindowsEngine { // The MethodChannel used for communication with the Flutter engine. std::unique_ptr> settings_channel_; - // A callback to be called when the engine (and thus the plugin registrar) - // is being destroyed. - FlutterDesktopOnPluginRegistrarDestroyed - plugin_registrar_destruction_callback_ = nullptr; + // Callbacks to be called when the engine (and thus the plugin registrar) is + // being destroyed. + std::map + plugin_registrar_destruction_callbacks_; // The approximate time between vblank events. std::chrono::nanoseconds FrameInterval(); diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc index 0287c17507..1218cf8fbb 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -7,6 +7,7 @@ #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" #include "flutter/shell/platform/windows/testing/engine_modifier.h" +#include "flutter/shell/platform/windows/testing/test_keyboard.h" #include "gtest/gtest.h" // winbase.h defines GetCurrentTime as a macro. @@ -317,5 +318,35 @@ TEST(FlutterWindowsEngine, DispatchSemanticsAction) { EXPECT_TRUE(called); } +TEST(FlutterWindowsEngine, AddPluginRegistrarDestructionCallback) { + std::unique_ptr engine = GetTestEngine(); + EngineModifier modifier(engine.get()); + + MockEmbedderApiForKeyboard(modifier, + std::make_shared()); + + engine->RunWithEntrypoint(nullptr); + + // Verify that destruction handlers don't overwrite each other. + int result1 = 0; + int result2 = 0; + engine->AddPluginRegistrarDestructionCallback( + [](FlutterDesktopPluginRegistrarRef ref) { + auto result = reinterpret_cast(ref); + *result = 1; + }, + reinterpret_cast(&result1)); + engine->AddPluginRegistrarDestructionCallback( + [](FlutterDesktopPluginRegistrarRef ref) { + auto result = reinterpret_cast(ref); + *result = 2; + }, + reinterpret_cast(&result2)); + + engine->Stop(); + EXPECT_EQ(result1, 1); + EXPECT_EQ(result2, 2); +} + } // namespace testing } // namespace flutter