Windows: Fix plugin destruction (flutter/engine#30760)

This commit is contained in:
Niklas Schulze
2022-03-03 22:16:11 +01:00
committed by GitHub
parent 1e2f796278
commit caf008b9f6
4 changed files with 48 additions and 13 deletions

View File

@@ -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,

View File

@@ -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(

View File

@@ -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<std::string>& switches);
@@ -221,10 +222,11 @@ class FlutterWindowsEngine {
// The MethodChannel used for communication with the Flutter engine.
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> 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<FlutterDesktopOnPluginRegistrarDestroyed,
FlutterDesktopPluginRegistrarRef>
plugin_registrar_destruction_callbacks_;
// The approximate time between vblank events.
std::chrono::nanoseconds FrameInterval();

View File

@@ -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<FlutterWindowsEngine> engine = GetTestEngine();
EngineModifier modifier(engine.get());
MockEmbedderApiForKeyboard(modifier,
std::make_shared<MockKeyResponseController>());
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<int*>(ref);
*result = 1;
},
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(&result1));
engine->AddPluginRegistrarDestructionCallback(
[](FlutterDesktopPluginRegistrarRef ref) {
auto result = reinterpret_cast<int*>(ref);
*result = 2;
},
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(&result2));
engine->Stop();
EXPECT_EQ(result1, 1);
EXPECT_EQ(result2, 2);
}
} // namespace testing
} // namespace flutter