From bdcc4f6e95bf9f24f1570f7e7645ee9eddb886ea Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Thu, 9 Mar 2023 13:27:37 -0800 Subject: [PATCH] [ios] refactor platform_message_handler_ios (flutter/engine#40182) [ios] refactor platform_message_handler_ios --- .../darwin/ios/platform_message_handler_ios.h | 10 ++++++---- .../darwin/ios/platform_message_handler_ios.mm | 15 ++++++++------- .../ios/platform_message_handler_ios_test.mm | 8 ++++---- .../platform/darwin/ios/platform_view_ios.mm | 3 ++- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.h b/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.h index 09ef281daa..3b176a1076 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.h +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.h @@ -13,7 +13,9 @@ #include "flutter/shell/common/platform_message_handler.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" -@protocol FlutterTaskQueue; +@protocol FlutterTaskQueue +- (void)dispatch:(dispatch_block_t)block; +@end namespace flutter { @@ -21,11 +23,11 @@ class PlatformMessageHandlerIos : public PlatformMessageHandler { public: static NSObject* MakeBackgroundTaskQueue(); - PlatformMessageHandlerIos(const TaskRunners& task_runners); + PlatformMessageHandlerIos(fml::RefPtr platform_task_runner); void HandlePlatformMessage(std::unique_ptr message) override; - bool DoesHandlePlatformMessageOnPlatformThread() const override { return false; } + bool DoesHandlePlatformMessageOnPlatformThread() const override; void InvokePlatformMessageResponseCallback(int response_id, std::unique_ptr mapping) override; @@ -43,7 +45,7 @@ class PlatformMessageHandlerIos : public PlatformMessageHandler { private: std::unordered_map message_handlers_; - TaskRunners task_runners_; + const fml::RefPtr platform_task_runner_; std::mutex message_handlers_mutex_; FML_DISALLOW_COPY_AND_ASSIGN(PlatformMessageHandlerIos); }; diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.mm index cf510bc301..df6582882f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios.mm @@ -11,10 +11,6 @@ static uint64_t platform_message_counter = 1; -@protocol FlutterTaskQueue -- (void)dispatch:(dispatch_block_t)block; -@end - @interface FLTSerialTaskQueue : NSObject @property(nonatomic, strong) dispatch_queue_t queue; @end @@ -44,8 +40,9 @@ NSObject* PlatformMessageHandlerIos::MakeBackgroundTaskQueue() return [[[FLTSerialTaskQueue alloc] init] autorelease]; } -PlatformMessageHandlerIos::PlatformMessageHandlerIos(const TaskRunners& task_runners) - : task_runners_(task_runners) {} +PlatformMessageHandlerIos::PlatformMessageHandlerIos( + fml::RefPtr platform_task_runner) + : platform_task_runner_(std::move(platform_task_runner)) {} void PlatformMessageHandlerIos::HandlePlatformMessage(std::unique_ptr message) { // This can be called from any isolate's thread. @@ -97,6 +94,10 @@ void PlatformMessageHandlerIos::HandlePlatformMessage(std::unique_ptr mapping) { @@ -114,7 +115,7 @@ void PlatformMessageHandlerIos::InvokePlatformMessageEmptyResponseCallback(int r void PlatformMessageHandlerIos::SetMessageHandler(const std::string& channel, FlutterBinaryMessageHandler handler, NSObject* task_queue) { - FML_CHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); + FML_CHECK(platform_task_runner_->RunsTasksOnCurrentThread()); /// TODO(gaaclarke): This should be migrated to a lockfree datastructure. std::lock_guard lock(message_handlers_mutex_); message_handlers_.erase(channel); diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios_test.mm b/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios_test.mm index 7249722c80..f1af3bef93 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios_test.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_message_handler_ios_test.mm @@ -46,7 +46,7 @@ class MockPlatformMessageResponse : public PlatformMessageResponse { - (void)testCreate { flutter::TaskRunners task_runners("test", GetCurrentTaskRunner(), CreateNewThread("raster"), CreateNewThread("ui"), CreateNewThread("io")); - auto handler = std::make_unique(task_runners); + auto handler = std::make_unique(task_runners.GetPlatformTaskRunner()); XCTAssertTrue(handler); } @@ -57,7 +57,7 @@ class MockPlatformMessageResponse : public PlatformMessageResponse { "test", GetCurrentTaskRunner(), thread_host.raster_thread->GetTaskRunner(), thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner()); - auto handler = std::make_unique(task_runners); + auto handler = std::make_unique(task_runners.GetPlatformTaskRunner()); std::string channel = "foo"; XCTestExpectation* didCallReply = [self expectationWithDescription:@"didCallReply"]; handler->SetMessageHandler( @@ -83,7 +83,7 @@ class MockPlatformMessageResponse : public PlatformMessageResponse { "test", GetCurrentTaskRunner(), thread_host.raster_thread->GetTaskRunner(), thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner()); - auto handler = std::make_unique(task_runners); + auto handler = std::make_unique(task_runners.GetPlatformTaskRunner()); std::string channel = "foo"; XCTestExpectation* didCallMessage = [self expectationWithDescription:@"didCallMessage"]; handler->SetMessageHandler( @@ -111,7 +111,7 @@ class MockPlatformMessageResponse : public PlatformMessageResponse { "test", GetCurrentTaskRunner(), thread_host.raster_thread->GetTaskRunner(), thread_host.ui_thread->GetTaskRunner(), thread_host.io_thread->GetTaskRunner()); - auto handler = std::make_unique(task_runners); + auto handler = std::make_unique(task_runners.GetPlatformTaskRunner()); std::string channel = "foo"; XCTestExpectation* didCallReply = [self expectationWithDescription:@"didCallReply"]; NSObject* taskQueue = PlatformMessageHandlerIos::MakeBackgroundTaskQueue(); diff --git a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm index 7e054fcb80..219dab4ec9 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm @@ -48,7 +48,8 @@ PlatformViewIOS::PlatformViewIOS( ios_context_(context), platform_views_controller_(platform_views_controller), accessibility_bridge_([this](bool enabled) { PlatformView::SetSemanticsEnabled(enabled); }), - platform_message_handler_(new PlatformMessageHandlerIos(task_runners)) {} + platform_message_handler_( + new PlatformMessageHandlerIos(task_runners.GetPlatformTaskRunner())) {} PlatformViewIOS::PlatformViewIOS( PlatformView::Delegate& delegate,