Skip to content

Commit 015bfbf

Browse files
authored
Allow multiple plugin registrar destruction callbacks (#311)
Applies flutter#30760 to the Tizen embedder. This is an actual fix for flutter-tizen/flutter-tizen#90, which has been worked around by flutter-tizen/flutter-tizen#98. We can now support the old-fashioned "sharedLib" style plugins (each plugin is built into an individual shared object) based on this change.
1 parent 687edeb commit 015bfbf

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

shell/platform/tizen/flutter_tizen.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ FlutterDesktopMessengerRef FlutterDesktopPluginRegistrarGetMessenger(
113113
void FlutterDesktopPluginRegistrarSetDestructionHandler(
114114
FlutterDesktopPluginRegistrarRef registrar,
115115
FlutterDesktopOnPluginRegistrarDestroyed callback) {
116-
registrar->engine->SetPluginRegistrarDestructionCallback(callback);
116+
registrar->engine->AddPluginRegistrarDestructionCallback(callback, registrar);
117117
}
118118

119119
bool FlutterDesktopMessengerSend(FlutterDesktopMessengerRef messenger,

shell/platform/tizen/flutter_tizen_engine.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ bool FlutterTizenEngine::StopEngine() {
259259
if (platform_view_channel_) {
260260
platform_view_channel_->Dispose();
261261
}
262-
if (plugin_registrar_destruction_callback_) {
263-
plugin_registrar_destruction_callback_(plugin_registrar_.get());
262+
for (const auto& [callback, registrar] :
263+
plugin_registrar_destruction_callbacks_) {
264+
callback(registrar);
264265
}
265266
#ifndef TIZEN_RENDERER_EVAS_GL
266267
tizen_vsync_waiter_.reset();
@@ -276,9 +277,10 @@ void FlutterTizenEngine::SetView(FlutterTizenView* view) {
276277
view_ = view;
277278
}
278279

279-
void FlutterTizenEngine::SetPluginRegistrarDestructionCallback(
280-
FlutterDesktopOnPluginRegistrarDestroyed callback) {
281-
plugin_registrar_destruction_callback_ = callback;
280+
void FlutterTizenEngine::AddPluginRegistrarDestructionCallback(
281+
FlutterDesktopOnPluginRegistrarDestroyed callback,
282+
FlutterDesktopPluginRegistrarRef registrar) {
283+
plugin_registrar_destruction_callbacks_[callback] = registrar;
282284
}
283285

284286
bool FlutterTizenEngine::SendPlatformMessage(

shell/platform/tizen/flutter_tizen_engine.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ class FlutterTizenEngine {
117117
}
118118
#endif
119119

120-
// Sets |callback| to be called when the plugin registrar is destroyed.
121-
void SetPluginRegistrarDestructionCallback(
122-
FlutterDesktopOnPluginRegistrarDestroyed callback);
120+
// Registers |callback| to be called when the plugin registrar is destroyed.
121+
void AddPluginRegistrarDestructionCallback(
122+
FlutterDesktopOnPluginRegistrarDestroyed callback,
123+
FlutterDesktopPluginRegistrarRef registrar);
123124

124125
// Sends the given message to the engine, calling |reply| with |user_data|
125126
// when a reponse is received from the engine if they are non-null.
@@ -238,10 +239,11 @@ class FlutterTizenEngine {
238239
// The texture registrar.
239240
std::unique_ptr<FlutterTizenTextureRegistrar> texture_registrar_;
240241

241-
// A callback to be called when the engine (and thus the plugin registrar)
242-
// is being destroyed.
243-
FlutterDesktopOnPluginRegistrarDestroyed
244-
plugin_registrar_destruction_callback_{nullptr};
242+
// Callbacks to be called when the engine (and thus the plugin registrar) is
243+
// being destroyed.
244+
std::map<FlutterDesktopOnPluginRegistrarDestroyed,
245+
FlutterDesktopPluginRegistrarRef>
246+
plugin_registrar_destruction_callbacks_;
245247

246248
#ifndef WEARABLE_PROFILE
247249
// The accessibility bridge for the Tizen platform.

shell/platform/tizen/flutter_tizen_engine_unittest.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,51 @@ TEST_F(FlutterTizenEngineTest, SendPlatformMessageWithResponse) {
188188
EXPECT_TRUE(send_message_called);
189189
}
190190

191+
TEST_F(FlutterTizenEngineTest, AddPluginRegistrarDestructionCallback) {
192+
EngineModifier modifier(engine_);
193+
modifier.embedder_api().Run = MOCK_ENGINE_PROC(
194+
Run, ([](size_t version, const FlutterRendererConfig* config,
195+
const FlutterProjectArgs* args, void* user_data,
196+
FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) {
197+
*engine_out = reinterpret_cast<FLUTTER_API_SYMBOL(FlutterEngine)>(1);
198+
return kSuccess;
199+
}));
200+
201+
// Stub out UpdateLocales and SendPlatformMessage as we don't have a fully
202+
// initialized engine instance.
203+
modifier.embedder_api().UpdateLocales = MOCK_ENGINE_PROC(
204+
UpdateLocales, ([](auto engine, const FlutterLocale** locales,
205+
size_t locales_count) { return kSuccess; }));
206+
modifier.embedder_api().SendPlatformMessage =
207+
MOCK_ENGINE_PROC(SendPlatformMessage,
208+
([](auto engine, auto message) { return kSuccess; }));
209+
210+
engine_->RunEngine();
211+
212+
// Verify that destruction handlers don't overwrite each other.
213+
int result1 = 0;
214+
int result2 = 0;
215+
engine_->AddPluginRegistrarDestructionCallback(
216+
[](FlutterDesktopPluginRegistrarRef ref) {
217+
auto result = reinterpret_cast<int*>(ref);
218+
*result = 1;
219+
},
220+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(&result1));
221+
engine_->AddPluginRegistrarDestructionCallback(
222+
[](FlutterDesktopPluginRegistrarRef ref) {
223+
auto result = reinterpret_cast<int*>(ref);
224+
*result = 2;
225+
},
226+
reinterpret_cast<FlutterDesktopPluginRegistrarRef>(&result2));
227+
228+
// Ensure that deallocation doesn't call the actual Shutdown with the bogus
229+
// engine pointer that the overridden Run returned.
230+
modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; };
231+
232+
engine_->StopEngine();
233+
EXPECT_EQ(result1, 1);
234+
EXPECT_EQ(result2, 2);
235+
}
236+
191237
} // namespace testing
192238
} // namespace flutter

0 commit comments

Comments
 (0)