diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index 81bf1eb6a7a95..0b9199f2a7562 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/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/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 418c1b5baf7bf..433d528a506eb 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -314,8 +314,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; @@ -333,9 +334,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/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index 831e669fb7d62..ce538feb5a5fa 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -71,9 +71,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); @@ -215,10 +216,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_; bool semantics_enabled_ = false; diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 36185b5b7c207..42a15d95be462 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/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" namespace flutter { @@ -255,5 +256,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