Skip to content

Commit 07a705b

Browse files
committed
Fix Windows plugin destruction #2
1 parent b556f1f commit 07a705b

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

shell/platform/windows/flutter_windows.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger(
213213
void FlutterDesktopPluginRegistrarSetDestructionHandler(
214214
FlutterDesktopPluginRegistrarRef registrar,
215215
FlutterDesktopOnPluginRegistrarDestroyed callback) {
216-
registrar->engine->AddPluginRegistrarDestructionCallback(callback);
216+
registrar->engine->AddPluginRegistrarDestructionCallback(callback, registrar);
217217
}
218218

219219
bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,

shell/platform/windows/flutter_windows_engine.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
314314

315315
bool FlutterWindowsEngine::Stop() {
316316
if (engine_) {
317-
for (const auto callback : plugin_registrar_destruction_callbacks_) {
318-
callback(plugin_registrar_.get());
317+
for (const auto& [callback, registrar] :
318+
plugin_registrar_destruction_callbacks_) {
319+
callback(registrar);
319320
}
320321

321322
FlutterEngineResult result = embedder_api_.Shutdown(engine_);
@@ -335,8 +336,9 @@ FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() {
335336
}
336337

337338
void FlutterWindowsEngine::AddPluginRegistrarDestructionCallback(
338-
FlutterDesktopOnPluginRegistrarDestroyed callback) {
339-
plugin_registrar_destruction_callbacks_.insert(callback);
339+
FlutterDesktopOnPluginRegistrarDestroyed callback,
340+
FlutterDesktopPluginRegistrarRef registrar) {
341+
plugin_registrar_destruction_callbacks_[callback] = registrar;
340342
}
341343

342344
void FlutterWindowsEngine::SendWindowMetricsEvent(

shell/platform/windows/flutter_windows_engine.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <map>
99
#include <memory>
1010
#include <optional>
11-
#include <set>
1211
#include <vector>
1312

1413
#include "flutter/shell/platform/common/accessibility_bridge.h"
@@ -74,7 +73,8 @@ class FlutterWindowsEngine {
7473

7574
// Registers a |callback| to be called when the plugin registrar is destroyed.
7675
void AddPluginRegistrarDestructionCallback(
77-
FlutterDesktopOnPluginRegistrarDestroyed callback);
76+
FlutterDesktopOnPluginRegistrarDestroyed callback,
77+
FlutterDesktopPluginRegistrarRef registrar);
7878

7979
// Sets switches member to the given switches.
8080
void SetSwitches(const std::vector<std::string>& switches);
@@ -219,9 +219,10 @@ class FlutterWindowsEngine {
219219
// The MethodChannel used for communication with the Flutter engine.
220220
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> settings_channel_;
221221

222-
// A set of callbacks to be called when the engine (and thus the plugin
222+
// Callbacks to be called when the engine (and thus the plugin
223223
// registrar) is being destroyed.
224-
std::set<FlutterDesktopOnPluginRegistrarDestroyed>
224+
std::map<FlutterDesktopOnPluginRegistrarDestroyed,
225+
FlutterDesktopPluginRegistrarRef>
225226
plugin_registrar_destruction_callbacks_;
226227

227228
bool semantics_enabled_ = false;

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "flutter/shell/platform/embedder/embedder.h"
88
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
99
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
10+
#include "flutter/shell/platform/windows/testing/test_keyboard.h"
1011
#include "gtest/gtest.h"
1112

1213
namespace flutter {
@@ -255,5 +256,35 @@ TEST(FlutterWindowsEngine, DispatchSemanticsAction) {
255256
EXPECT_TRUE(called);
256257
}
257258

259+
TEST(FlutterWindowsEngine, AddPluginRegistrarDestructionCallback) {
260+
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
261+
EngineModifier modifier(engine.get());
262+
263+
MockEmbedderApiForKeyboard(modifier,
264+
std::make_shared<MockKeyResponseController>());
265+
266+
engine->RunWithEntrypoint(nullptr);
267+
268+
// Verify that destruction handlers don't overwrite each other.
269+
int result1 = 0;
270+
int result2 = 0;
271+
engine->AddPluginRegistrarDestructionCallback(
272+
[](FlutterDesktopPluginRegistrarRef ref) {
273+
auto result = reinterpret_cast<int*>(ref);
274+
*result = 1;
275+
},
276+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(&result1));
277+
engine->AddPluginRegistrarDestructionCallback(
278+
[](FlutterDesktopPluginRegistrarRef ref) {
279+
auto result = reinterpret_cast<int*>(ref);
280+
*result = 2;
281+
},
282+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(&result2));
283+
284+
engine->Stop();
285+
EXPECT_EQ(result1, 1);
286+
EXPECT_EQ(result2, 2);
287+
}
288+
258289
} // namespace testing
259290
} // namespace flutter

0 commit comments

Comments
 (0)