Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6a91c6a

Browse files
committed
started supporting backwards compatible usage of setting handlers
1 parent 4e078f7 commit 6a91c6a

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

shell/common/shell.cc

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,13 @@ bool Shell::Setup(std::unique_ptr<PlatformView> platform_view,
632632

633633
platform_view_ = std::move(platform_view);
634634
platform_message_handler_ = platform_view_->GetPlatformMessageHandler();
635+
route_messages_through_platform_thread_.store(true);
636+
task_runners_.GetPlatformTaskRunner()->PostTask(
637+
[self = weak_factory_.GetWeakPtr()] {
638+
if (self) {
639+
self->route_messages_through_platform_thread_.store(false);
640+
}
641+
});
635642
engine_ = std::move(engine);
636643
rasterizer_ = std::move(rasterizer);
637644
io_manager_ = io_manager;
@@ -1183,7 +1190,30 @@ void Shell::OnEngineHandlePlatformMessage(
11831190
}
11841191

11851192
if (platform_message_handler_) {
1186-
platform_message_handler_->HandlePlatformMessage(std::move(message));
1193+
if (route_messages_through_platform_thread_) {
1194+
// We route messages through the platform thread temporarily when the
1195+
// shell is being initialized to be backwards compatible with setting
1196+
// message handlers in the same event as starting the isolate, but after
1197+
// it is started.
1198+
auto ui_task_runner = task_runners_.GetUITaskRunner();
1199+
task_runners_.GetPlatformTaskRunner()->PostTask(fml::MakeCopyable(
1200+
[weak_platform_message_handler =
1201+
std::weak_ptr<PlatformMessageHandler>(platform_message_handler_),
1202+
message = std::move(message), ui_task_runner]() mutable {
1203+
ui_task_runner->PostTask(fml::MakeCopyable(
1204+
[weak_platform_message_handler, message = std::move(message),
1205+
ui_task_runner]() mutable {
1206+
auto platform_message_handler =
1207+
weak_platform_message_handler.lock();
1208+
if (platform_message_handler) {
1209+
platform_message_handler->HandlePlatformMessage(
1210+
std::move(message));
1211+
}
1212+
}));
1213+
}));
1214+
} else {
1215+
platform_message_handler_->HandlePlatformMessage(std::move(message));
1216+
}
11871217
} else {
11881218
task_runners_.GetPlatformTaskRunner()->PostTask(
11891219
fml::MakeCopyable([view = platform_view_->GetWeakPtr(),

shell/common/shell.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ class Shell final : public PlatformView::Delegate,
419419
std::shared_ptr<fml::SyncSwitch> is_gpu_disabled_sync_switch_;
420420
std::shared_ptr<VolatilePathTracker> volatile_path_tracker_;
421421
std::shared_ptr<PlatformMessageHandler> platform_message_handler_;
422+
std::atomic<bool> route_messages_through_platform_thread_ = false;
422423

423424
fml::WeakPtr<Engine> weak_engine_; // to be shared across threads
424425
fml::TaskRunnerAffineWeakPtr<Rasterizer>

shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,24 @@ - (void)testDeallocNotification {
220220

221221
- (void)testSetHandlerAfterRun {
222222
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar"];
223-
NSObject<FlutterPluginRegistrar>* registrar = [engine registrarForPlugin:@"foo"];
224223
XCTestExpectation* gotMessage = [self expectationWithDescription:@"gotMessage"];
225-
fml::AutoResetWaitableEvent latch;
226-
[engine run];
227-
flutter::Shell& shell = engine.shell;
228-
engine.shell.GetTaskRunners().GetUITaskRunner()->PostTask([&latch, &shell] {
229-
flutter::Engine::Delegate& delegate = shell;
230-
auto message = std::make_unique<flutter::PlatformMessage>("foo", nullptr);
231-
delegate.OnEngineHandlePlatformMessage(std::move(message));
232-
latch.Signal();
224+
dispatch_async(dispatch_get_main_queue(), ^{
225+
NSObject<FlutterPluginRegistrar>* registrar = [engine registrarForPlugin:@"foo"];
226+
fml::AutoResetWaitableEvent latch;
227+
[engine run];
228+
flutter::Shell& shell = engine.shell;
229+
engine.shell.GetTaskRunners().GetUITaskRunner()->PostTask([&latch, &shell] {
230+
flutter::Engine::Delegate& delegate = shell;
231+
auto message = std::make_unique<flutter::PlatformMessage>("foo", nullptr);
232+
delegate.OnEngineHandlePlatformMessage(std::move(message));
233+
latch.Signal();
234+
});
235+
latch.Wait();
236+
[registrar.messenger setMessageHandlerOnChannel:@"foo"
237+
binaryMessageHandler:^(NSData* message, FlutterBinaryReply reply) {
238+
[gotMessage fulfill];
239+
}];
233240
});
234-
latch.Wait();
235-
[registrar.messenger setMessageHandlerOnChannel:@"foo"
236-
binaryMessageHandler:^(NSData* message, FlutterBinaryReply reply) {
237-
[gotMessage fulfill];
238-
}];
239241
[self waitForExpectationsWithTimeout:1 handler:nil];
240242
}
241243

0 commit comments

Comments
 (0)