From bf3c3c766c3caf0c219f83c815bf4e3fb0fce602 Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 16 Aug 2023 13:40:18 -0400 Subject: [PATCH 01/31] Skeleton cpp function --- lib/ui/dart_ui.cc | 1 + lib/ui/window/platform_configuration.cc | 5 +++++ lib/ui/window/platform_configuration.h | 2 ++ 3 files changed, 8 insertions(+) diff --git a/lib/ui/dart_ui.cc b/lib/ui/dart_ui.cc index b2fdbf88d4fc4..ae8ed1779e920 100644 --- a/lib/ui/dart_ui.cc +++ b/lib/ui/dart_ui.cc @@ -110,6 +110,7 @@ typedef CanvasPath Path; V(PlatformConfigurationNativeApi::GetRootIsolateToken, 0) \ V(PlatformConfigurationNativeApi::RegisterBackgroundIsolate, 1) \ V(PlatformConfigurationNativeApi::SendPortPlatformMessage, 4) \ + V(PlatformConfigurationNativeApi::PlatformChannelListenedTo, 1) \ V(DartRuntimeHooks::Logger_PrintDebugString, 1) \ V(DartRuntimeHooks::Logger_PrintString, 1) \ V(DartRuntimeHooks::ScheduleMicrotask, 1) \ diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index 8d11b747fd53b..5594ede605752 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -539,4 +539,9 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( dart_state->SetPlatformMessageHandler(weak_platform_message_handler); } +void PlatformConfigurationNativeApi::PlatformChannelListenedTo(const std::string& name) { + // TODO implement this function + FML_LOG(ERROR) << "Listening to platform channel named " << name; +} + } // namespace flutter diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index 9226b457d07e8..8392b74e95f3e 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -521,6 +521,8 @@ class PlatformConfigurationNativeApi { static void RespondToPlatformMessage(int response_id, const tonic::DartByteData& data); + static void PlatformChannelListenedTo(const std::string& name); + //-------------------------------------------------------------------------- /// @brief Requests the Dart VM to adjusts the GC heuristics based on /// the requested `performance_mode`. Returns the old performance From 839f8d1f64af576516877f71f8a9c9253b72eaad Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 16 Aug 2023 16:34:28 -0400 Subject: [PATCH 02/31] Calling --- lib/ui/channel_buffers.dart | 7 +++++++ lib/ui/dart_ui.cc | 2 +- lib/ui/window/platform_configuration.cc | 2 +- lib/ui/window/platform_configuration.h | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/ui/channel_buffers.dart b/lib/ui/channel_buffers.dart index a93208b3b8bb7..3e6b927487d42 100644 --- a/lib/ui/channel_buffers.dart +++ b/lib/ui/channel_buffers.dart @@ -380,6 +380,7 @@ class ChannelBuffers { assert(!name.contains('\u0000'), 'Channel names must not contain U+0000 NULL characters.'); final _Channel channel = _channels.putIfAbsent(name, () => _Channel()); channel.setListener(callback); + channelListenedTo(name, true); } /// Clears the listener for the specified channel. @@ -392,9 +393,15 @@ class ChannelBuffers { final _Channel? channel = _channels[name]; if (channel != null) { channel.clearListener(); + channelListenedTo(name, false); } } + @Native(symbol: 'PlatformConfigurationNativeApi::PlatformChannelListenedTo') + external static void _channelListenedTo(String name, bool listening); + + void channelListenedTo(String name, bool listening) => _channelListenedTo(name, listening); + /// Deprecated. Migrate to [setListener] instead. /// /// Remove and process all stored messages for a given channel. diff --git a/lib/ui/dart_ui.cc b/lib/ui/dart_ui.cc index ae8ed1779e920..fa1ca02c2a328 100644 --- a/lib/ui/dart_ui.cc +++ b/lib/ui/dart_ui.cc @@ -110,7 +110,7 @@ typedef CanvasPath Path; V(PlatformConfigurationNativeApi::GetRootIsolateToken, 0) \ V(PlatformConfigurationNativeApi::RegisterBackgroundIsolate, 1) \ V(PlatformConfigurationNativeApi::SendPortPlatformMessage, 4) \ - V(PlatformConfigurationNativeApi::PlatformChannelListenedTo, 1) \ + V(PlatformConfigurationNativeApi::PlatformChannelListenedTo, 2) \ V(DartRuntimeHooks::Logger_PrintDebugString, 1) \ V(DartRuntimeHooks::Logger_PrintString, 1) \ V(DartRuntimeHooks::ScheduleMicrotask, 1) \ diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index 5594ede605752..eb63d3db8c545 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -539,7 +539,7 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( dart_state->SetPlatformMessageHandler(weak_platform_message_handler); } -void PlatformConfigurationNativeApi::PlatformChannelListenedTo(const std::string& name) { +void PlatformConfigurationNativeApi::PlatformChannelListenedTo(const std::string& name, bool listening) { // TODO implement this function FML_LOG(ERROR) << "Listening to platform channel named " << name; } diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index 8392b74e95f3e..013141fc6bc80 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -521,7 +521,7 @@ class PlatformConfigurationNativeApi { static void RespondToPlatformMessage(int response_id, const tonic::DartByteData& data); - static void PlatformChannelListenedTo(const std::string& name); + static void PlatformChannelListenedTo(const std::string& name, bool listening); //-------------------------------------------------------------------------- /// @brief Requests the Dart VM to adjusts the GC heuristics based on From 5dd476eda3a25288458d3261bf61a7c6189ad3c0 Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 16 Aug 2023 17:01:46 -0400 Subject: [PATCH 03/31] Message makes it to Shell --- lib/ui/window/platform_configuration.cc | 3 +-- lib/ui/window/platform_configuration.h | 2 ++ runtime/runtime_controller.cc | 5 +++++ runtime/runtime_controller.h | 3 +++ runtime/runtime_delegate.h | 2 ++ shell/common/engine.cc | 4 ++++ shell/common/engine.h | 6 ++++++ shell/common/engine_unittests.cc | 2 ++ shell/common/shell.cc | 5 +++++ shell/common/shell.h | 3 +++ 10 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index eb63d3db8c545..e70baefabcf1f 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -540,8 +540,7 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( } void PlatformConfigurationNativeApi::PlatformChannelListenedTo(const std::string& name, bool listening) { - // TODO implement this function - FML_LOG(ERROR) << "Listening to platform channel named " << name; + UIDartState::Current()->platform_configuration()->client()->ChannelListenedTo(name, listening); } } // namespace flutter diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index 013141fc6bc80..0356574405e11 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -217,6 +217,8 @@ class PlatformConfigurationClient { /// virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id) = 0; + virtual void ChannelListenedTo(const std::string& name, bool listening) = 0; + protected: virtual ~PlatformConfigurationClient(); }; diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index 751fa696a5719..79abea6af0127 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -388,6 +388,11 @@ RuntimeController::ComputePlatformResolvedLocale( return client_.ComputePlatformResolvedLocale(supported_locale_data); } +// |PlatformConfigurationClient| +void RuntimeController::ChannelListenedTo(const std::string& name, bool listening) { + client_.ChannelListenedTo(name, listening); +} + Dart_Port RuntimeController::GetMainPort() { std::shared_ptr root_isolate = root_isolate_.lock(); return root_isolate ? root_isolate->main_port() : ILLEGAL_PORT; diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index aaef6da631694..758e26a8e3bf7 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -656,6 +656,9 @@ class RuntimeController : public PlatformConfigurationClient { std::unique_ptr> ComputePlatformResolvedLocale( const std::vector& supported_locale_data) override; + // |PlatformConfigurationClient| + void ChannelListenedTo(const std::string& name, bool listening) override; + FML_DISALLOW_COPY_AND_ASSIGN(RuntimeController); }; diff --git a/runtime/runtime_delegate.h b/runtime/runtime_delegate.h index 18e4dbfdfd31c..318a417dfce99 100644 --- a/runtime/runtime_delegate.h +++ b/runtime/runtime_delegate.h @@ -56,6 +56,8 @@ class RuntimeDelegate { virtual std::weak_ptr GetPlatformMessageHandler() const = 0; + virtual void ChannelListenedTo(const std::string& name, bool listening) = 0; + protected: virtual ~RuntimeDelegate(); }; diff --git a/shell/common/engine.cc b/shell/common/engine.cc index 829e3fd60bed0..1b4b3250713c5 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -565,6 +565,10 @@ std::weak_ptr Engine::GetPlatformMessageHandler() return delegate_.GetPlatformMessageHandler(); } +void Engine::ChannelListenedTo(const std::string& name, bool listening) { + delegate_.OnEngineChannelListenedTo(name, listening); +} + void Engine::LoadDartDeferredLibrary( intptr_t loading_unit_id, std::unique_ptr snapshot_data, diff --git a/shell/common/engine.h b/shell/common/engine.h index e0a4701f87b0e..78e04154eeb52 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -293,6 +293,9 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { /// Flutter to the host platform (and its responses). virtual const std::shared_ptr& GetPlatformMessageHandler() const = 0; + + //---------------------------------------------------------------------------- + virtual void OnEngineChannelListenedTo(const std::string& name, bool listening) = 0; }; //---------------------------------------------------------------------------- @@ -933,6 +936,9 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { std::weak_ptr GetPlatformMessageHandler() const override; + // |RuntimeDelegate| + void ChannelListenedTo(const std::string& name, bool listening) override; + void SetNeedsReportTimings(bool value) override; bool HandleLifecyclePlatformMessage(PlatformMessage* message); diff --git a/shell/common/engine_unittests.cc b/shell/common/engine_unittests.cc index d1a7d24d39cbc..d0dcf67bd0241 100644 --- a/shell/common/engine_unittests.cc +++ b/shell/common/engine_unittests.cc @@ -40,6 +40,7 @@ class MockDelegate : public Engine::Delegate { MOCK_METHOD0(GetCurrentTimePoint, fml::TimePoint()); MOCK_CONST_METHOD0(GetPlatformMessageHandler, const std::shared_ptr&()); + MOCK_METHOD2(OnEngineChannelListenedTo, void(const std::string&, bool)); }; class MockResponse : public PlatformMessageResponse { @@ -68,6 +69,7 @@ class MockRuntimeDelegate : public RuntimeDelegate { MOCK_METHOD1(RequestDartDeferredLibrary, void(intptr_t)); MOCK_CONST_METHOD0(GetPlatformMessageHandler, std::weak_ptr()); + MOCK_METHOD2(ChannelListenedTo, void(const std::string&, bool)); }; class MockRuntimeController : public RuntimeController { diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 136c09b9c39d0..04e27a60cce70 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1332,6 +1332,11 @@ void Shell::OnEngineHandlePlatformMessage( } } +void Shell::OnEngineChannelListenedTo(const std::string& name, bool listening) { + FML_LOG(ERROR) << "Shell received indication of listening to " << name; + // TODO forward to platform view +} + void Shell::HandleEngineSkiaMessage(std::unique_ptr message) { const auto& data = message->data(); diff --git a/shell/common/shell.h b/shell/common/shell.h index 23689332233c9..d0233f0f93a25 100644 --- a/shell/common/shell.h +++ b/shell/common/shell.h @@ -649,6 +649,9 @@ class Shell final : public PlatformView::Delegate, // |Engine::Delegate| fml::TimePoint GetCurrentTimePoint() override; + // |Engine::Delegate| + void OnEngineChannelListenedTo(const std::string& name, bool listening) override; + // |Rasterizer::Delegate| void OnFrameRasterized(const FrameTiming&) override; From 0bdacd0edc8b806526397ab335765f4b029c8228 Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 16 Aug 2023 17:18:08 -0400 Subject: [PATCH 04/31] Part of project args --- shell/common/platform_view.cc | 2 ++ shell/common/platform_view.h | 3 +++ shell/common/shell.cc | 11 +++++++++-- shell/platform/embedder/embedder.cc | 9 +++++++++ shell/platform/embedder/embedder.h | 10 ++++++++++ shell/platform/embedder/platform_view_embedder.cc | 7 +++++++ shell/platform/embedder/platform_view_embedder.h | 5 +++++ 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/shell/common/platform_view.cc b/shell/common/platform_view.cc index 77a33bfb76e2a..f6918d7fa422f 100644 --- a/shell/common/platform_view.cc +++ b/shell/common/platform_view.cc @@ -114,6 +114,8 @@ void PlatformView::UpdateSemantics( // NOLINTNEXTLINE(performance-unnecessary-value-param) CustomAccessibilityActionUpdates actions) {} +void PlatformView::ChannelListenedTo(const std::string& name, bool listening) {} + void PlatformView::HandlePlatformMessage( std::unique_ptr message) { if (auto response = message->response()) { diff --git a/shell/common/platform_view.h b/shell/common/platform_view.h index e6c7dab924e5c..b24c1e460dab5 100644 --- a/shell/common/platform_view.h +++ b/shell/common/platform_view.h @@ -465,6 +465,9 @@ class PlatformView { virtual void UpdateSemantics(SemanticsNodeUpdates updates, CustomAccessibilityActionUpdates actions); + //---------------------------------------------------------------------------- + virtual void ChannelListenedTo(const std::string& name, bool listening); + //---------------------------------------------------------------------------- /// @brief Used by embedders to specify the updated viewport metrics for /// a view. In response to this call, on the raster thread, the diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 04e27a60cce70..d827a2bc21584 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1333,8 +1333,15 @@ void Shell::OnEngineHandlePlatformMessage( } void Shell::OnEngineChannelListenedTo(const std::string& name, bool listening) { - FML_LOG(ERROR) << "Shell received indication of listening to " << name; - // TODO forward to platform view + FML_DCHECK(is_set_up_); + FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); + + task_runners_.GetPlatformTaskRunner()->PostTask( + [view = platform_view_->GetWeakPtr(), name, listening] { + if (view) { + view->ChannelListenedTo(name, listening); + } + }); } void Shell::HandleEngineSkiaMessage(std::unique_ptr message) { diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index e571001a26588..918ebdf300b0e 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1881,6 +1881,14 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, user_data]() { return ptr(user_data); }; } + flutter::PlatformViewEmbedder::ChannelListenedToCallback channel_listened_to_callback = nullptr; + if (SAFE_ACCESS(args, channel_listened_to_callback, nullptr) != nullptr) { + channel_listened_to_callback = [ptr = args->channel_listened_to_callback, user_data](const std::string& name, bool listening) { + size_t name_len = name.size(); + ptr(name.data(), name_len, listening, user_data); + }; + } + auto external_view_embedder_result = InferExternalViewEmbedderFromArgs( SAFE_ACCESS(args, compositor, nullptr), settings.enable_impeller); if (external_view_embedder_result.second) { @@ -1895,6 +1903,7 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, vsync_callback, // compute_platform_resolved_locale_callback, // on_pre_engine_restart_callback, // + channel_listened_to_callback, // }; auto on_create_platform_view = InferPlatformViewCreationCallback( diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index a1e9036b689fa..0e684eddf84e4 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -1326,6 +1326,12 @@ typedef void (*FlutterUpdateSemanticsCallback2)( const FlutterSemanticsUpdate2* /* semantics update */, void* /* user data*/); +typedef void (*FlutterChannelListenedToCallback)( + const char* data /* name */, + size_t data_len /* name.size */, + bool /* listening */, + void* /* user data */); + typedef struct _FlutterTaskRunner* FlutterTaskRunner; typedef struct { @@ -2118,6 +2124,10 @@ typedef struct { /// and `update_semantics_callback2` may be provided; the others must be set /// to null. FlutterUpdateSemanticsCallback2 update_semantics_callback2; + + /// The callback invoked by the engine in response to a channel listener + /// being registered on the framework side. + FlutterChannelListenedToCallback channel_listened_to_callback; } FlutterProjectArgs; #ifndef FLUTTER_ENGINE_NO_PROTOTYPES diff --git a/shell/platform/embedder/platform_view_embedder.cc b/shell/platform/embedder/platform_view_embedder.cc index 7e7ed026079f3..75c992deefd99 100644 --- a/shell/platform/embedder/platform_view_embedder.cc +++ b/shell/platform/embedder/platform_view_embedder.cc @@ -199,6 +199,13 @@ void PlatformViewEmbedder::OnPreEngineRestart() const { } } +// |PlatformView| +void PlatformViewEmbedder::ChannelListenedTo(const std::string& name, bool listening) { + if (platform_dispatch_table_.on_channel_listened_to != nullptr) { + platform_dispatch_table_.on_channel_listened_to(name, listening); + } +} + std::shared_ptr PlatformViewEmbedder::GetPlatformMessageHandler() const { return platform_message_handler_; diff --git a/shell/platform/embedder/platform_view_embedder.h b/shell/platform/embedder/platform_view_embedder.h index 896e11a7101c6..74517f087a870 100644 --- a/shell/platform/embedder/platform_view_embedder.h +++ b/shell/platform/embedder/platform_view_embedder.h @@ -41,6 +41,7 @@ class PlatformViewEmbedder final : public PlatformView { std::function>( const std::vector& supported_locale_data)>; using OnPreEngineRestartCallback = std::function; + using ChannelListenedToCallback = std::function; struct PlatformDispatchTable { UpdateSemanticsCallback update_semantics_callback; // optional @@ -50,6 +51,7 @@ class PlatformViewEmbedder final : public PlatformView { ComputePlatformResolvedLocaleCallback compute_platform_resolved_locale_callback; OnPreEngineRestartCallback on_pre_engine_restart_callback; // optional + ChannelListenedToCallback on_channel_listened_to; // optional }; // Create a platform view that sets up a software rasterizer. @@ -134,6 +136,9 @@ class PlatformViewEmbedder final : public PlatformView { std::unique_ptr> ComputePlatformResolvedLocales( const std::vector& supported_locale_data) override; + // |PlatformView| + void ChannelListenedTo(const std::string& name, bool listening) override; + FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewEmbedder); }; From c2091cffaf63d9f0533001e7e24a8f743e491b2f Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 16 Aug 2023 17:24:07 -0400 Subject: [PATCH 05/31] FlutterWindowsEngine creates a callback --- shell/platform/windows/flutter_windows_engine.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 6a2169579e6c0..43cdf5669fcf7 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -373,6 +373,11 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { host->root_isolate_create_callback_(); } }; + args.channel_listened_to_callback = [](const char* name, size_t name_len, bool listening, void* user_data) { + auto host = static_cast(user_data); + std::string channel_name(name, name + name_len); + FML_LOG(ERROR) << "Engine receives notification for " << channel_name; + }; args.custom_task_runners = &custom_task_runners; From b3cf3733d91d8fb3678105f4104bbf2b41578c0d Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 17 Aug 2023 14:25:54 -0400 Subject: [PATCH 06/31] Engine listens --- .../windows/flutter_windows_engine.cc | 10 +++++- .../platform/windows/flutter_windows_engine.h | 2 ++ .../flutter_windows_engine_unittests.cc | 34 +++++++++++++++---- .../windows/windows_lifecycle_manager.cc | 6 +++- .../windows/windows_lifecycle_manager.h | 7 ++-- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 43cdf5669fcf7..754743a3ece35 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -376,7 +376,7 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { args.channel_listened_to_callback = [](const char* name, size_t name_len, bool listening, void* user_data) { auto host = static_cast(user_data); std::string channel_name(name, name + name_len); - FML_LOG(ERROR) << "Engine receives notification for " << channel_name; + host->OnChannelListenedTo(channel_name, listening); }; args.custom_task_runners = &custom_task_runners; @@ -824,4 +824,12 @@ std::optional FlutterWindowsEngine::ProcessExternalWindowMessage( return std::nullopt; } +void FlutterWindowsEngine::OnChannelListenedTo(const std::string& name, bool listening) { + if (name.compare("flutter/platform") == 0) { + lifecycle_manager_->BeginProcessingExit(); + } else if (name.compare("flutter/lifecycle") == 0) { + lifecycle_manager_->BeginProcessingLifecycle(); + } +} + } // namespace flutter diff --git a/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index 36147c00c821c..889150dff03ff 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -301,6 +301,8 @@ class FlutterWindowsEngine { // created. This is typically caused by a hot restart (Shift-R in CLI.) void OnPreEngineRestart(); + virtual void OnChannelListenedTo(const std::string& name, bool listening); + private: // Allows swapping out embedder_api_ calls in tests. friend class EngineModifier; diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 66afc098ff17b..7469ee1fd0988 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -703,7 +703,9 @@ TEST_F(FlutterWindowsEngineTest, TestExit) { ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; }); EXPECT_CALL(*handler, Quit).Times(1); modifier.SetLifecycleManager(std::move(handler)); - engine->OnApplicationLifecycleEnabled(); + // engine->OnApplicationLifecycleEnabled(); + + engine->lifecycle_manager()->BeginProcessingExit(); engine->Run(); @@ -740,7 +742,8 @@ TEST_F(FlutterWindowsEngineTest, TestExitCancel) { ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; }); EXPECT_CALL(*handler, Quit).Times(0); modifier.SetLifecycleManager(std::move(handler)); - engine->OnApplicationLifecycleEnabled(); + // engine->OnApplicationLifecycleEnabled(); + engine->lifecycle_manager()->BeginProcessingExit(); auto binary_messenger = std::make_unique(engine->messenger()); @@ -801,7 +804,8 @@ TEST_F(FlutterWindowsEngineTest, TestExitSecondCloseMessage) { hwnd, msg, wparam, lparam); }); modifier.SetLifecycleManager(std::move(handler)); - engine->OnApplicationLifecycleEnabled(); + // engine->OnApplicationLifecycleEnabled(); + engine->lifecycle_manager()->BeginProcessingExit(); engine->Run(); @@ -852,7 +856,8 @@ TEST_F(FlutterWindowsEngineTest, TestExitCloseMultiWindow) { // Quit should not be called when there is more than one window. EXPECT_CALL(*handler, Quit).Times(0); modifier.SetLifecycleManager(std::move(handler)); - engine->OnApplicationLifecycleEnabled(); + // engine->OnApplicationLifecycleEnabled(); + engine->lifecycle_manager()->BeginProcessingExit(); engine->Run(); @@ -900,7 +905,8 @@ TEST_F(FlutterWindowsEngineTest, EnableApplicationLifecycle) { }); EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1); modifier.SetLifecycleManager(std::move(handler)); - engine->OnApplicationLifecycleEnabled(); + //engine->OnApplicationLifecycleEnabled(); + engine->lifecycle_manager()->BeginProcessingExit(); engine->window_proc_delegate_manager()->OnTopLevelWindowProc(0, WM_CLOSE, 0, 0); @@ -1054,7 +1060,9 @@ TEST_F(FlutterWindowsEngineTest, EnableLifecycleState) { EXPECT_FALSE(finished); // Test that we can set the state afterwards. - engine->OnApplicationLifecycleEnabled(); + // engine->OnApplicationLifecycleEnabled(); + + engine->lifecycle_manager()->BeginProcessingLifecycle(); view.OnWindowStateEvent(hwnd, WindowStateEvent::kShow); while (!finished) { @@ -1111,5 +1119,19 @@ TEST_F(FlutterWindowsEngineTest, LifecycleStateToFrom) { } } +TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { + FlutterWindowsEngineBuilder builder{GetContext()}; + builder.SetDartEntrypoint("enableLifecycleToFrom"); + + auto window_binding_handler = + std::make_unique<::testing::NiceMock>(); + MockFlutterWindowsView view(std::move(window_binding_handler)); + view.SetEngine(builder.Build()); + FlutterWindowsEngine* engine = view.GetEngine(); + EngineModifier modifier(engine); + modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; + engine->Run(); +} + } // namespace testing } // namespace flutter diff --git a/shell/platform/windows/windows_lifecycle_manager.cc b/shell/platform/windows/windows_lifecycle_manager.cc index fc3bf469cf9e2..a41b1617ba6c2 100644 --- a/shell/platform/windows/windows_lifecycle_manager.cc +++ b/shell/platform/windows/windows_lifecycle_manager.cc @@ -49,7 +49,7 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd, // is, we re-dispatch a new WM_CLOSE message. In order to allow the new // message to reach other delegates, we ignore it here. case WM_CLOSE: { - if (!process_lifecycle_) { + if (!process_exit_) { return false; } auto key = std::make_tuple(hwnd, wpar, lpar); @@ -183,6 +183,10 @@ void WindowsLifecycleManager::BeginProcessingLifecycle() { process_lifecycle_ = true; } +void WindowsLifecycleManager::BeginProcessingExit() { + process_exit_ = true; +} + // TODO(schectman): Wait until the platform channel is registered to send // the platform message. // https://github.com/flutter/flutter/issues/131616 diff --git a/shell/platform/windows/windows_lifecycle_manager.h b/shell/platform/windows/windows_lifecycle_manager.h index 18afd82412db6..c56bfc306b0fb 100644 --- a/shell/platform/windows/windows_lifecycle_manager.h +++ b/shell/platform/windows/windows_lifecycle_manager.h @@ -52,10 +52,12 @@ class WindowsLifecycleManager { // update the application lifecycle. bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT* result); - // Signal to start consuming WM_CLOSE messages and sending lifecycle state - // update messages. + // Signal to start sending lifecycle state update messages. virtual void BeginProcessingLifecycle(); + // Signal to start consuming WM_CLOSE messages. + virtual void BeginProcessingExit(); + // Update the app lifecycle state in response to a change in window state. // When the app lifecycle state actually changes, this sends a platform // message to the framework notifying it of the state change. @@ -102,6 +104,7 @@ class WindowsLifecycleManager { std::map, int> sent_close_messages_; bool process_lifecycle_ = false; + bool process_exit_ = false; std::set visible_windows_; From 0ca00f7a0edbf1ab0f77b649df3dad90b526ccaf Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 17 Aug 2023 14:28:43 -0400 Subject: [PATCH 07/31] Remove OnApplicationLifecycleEnabled --- shell/platform/windows/flutter_windows_engine.cc | 10 ---------- shell/platform/windows/flutter_windows_engine.h | 4 ---- .../windows/flutter_windows_engine_unittests.cc | 6 ------ shell/platform/windows/platform_handler.cc | 2 +- 4 files changed, 1 insertion(+), 21 deletions(-) diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 754743a3ece35..820aaefe53ceb 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -797,16 +797,6 @@ void FlutterWindowsEngine::OnDwmCompositionChanged() { view_->OnDwmCompositionChanged(); } -// TODO(yaakovschectman): This enables the flutter/lifecycle channel -// once the System.initializationComplete message is received on -// the flutter/system channel. This is a short-term workaround to -// ensure the framework is initialized and ready to accept lifecycle -// messages. This cross-channel dependency should be removed. -// See: https://github.com/flutter/flutter/issues/132514 -void FlutterWindowsEngine::OnApplicationLifecycleEnabled() { - lifecycle_manager_->BeginProcessingLifecycle(); -} - void FlutterWindowsEngine::OnWindowStateEvent(HWND hwnd, WindowStateEvent event) { lifecycle_manager_->OnWindowStateEvent(hwnd, event); diff --git a/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index 889150dff03ff..a7eef1cd6b119 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -258,10 +258,6 @@ class FlutterWindowsEngine { // Called when a WM_DWMCOMPOSITIONCHANGED message is received. void OnDwmCompositionChanged(); - // Called in response to the framework registering a ServiceBindings. - // Registers the top level handler for the WM_CLOSE window message. - void OnApplicationLifecycleEnabled(); - // Called when a Window receives an event that may alter the application // lifecycle state. void OnWindowStateEvent(HWND hwnd, WindowStateEvent event); diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 7469ee1fd0988..38033f181f1e6 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -703,7 +703,6 @@ TEST_F(FlutterWindowsEngineTest, TestExit) { ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; }); EXPECT_CALL(*handler, Quit).Times(1); modifier.SetLifecycleManager(std::move(handler)); - // engine->OnApplicationLifecycleEnabled(); engine->lifecycle_manager()->BeginProcessingExit(); @@ -742,7 +741,6 @@ TEST_F(FlutterWindowsEngineTest, TestExitCancel) { ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; }); EXPECT_CALL(*handler, Quit).Times(0); modifier.SetLifecycleManager(std::move(handler)); - // engine->OnApplicationLifecycleEnabled(); engine->lifecycle_manager()->BeginProcessingExit(); auto binary_messenger = @@ -804,7 +802,6 @@ TEST_F(FlutterWindowsEngineTest, TestExitSecondCloseMessage) { hwnd, msg, wparam, lparam); }); modifier.SetLifecycleManager(std::move(handler)); - // engine->OnApplicationLifecycleEnabled(); engine->lifecycle_manager()->BeginProcessingExit(); engine->Run(); @@ -856,7 +853,6 @@ TEST_F(FlutterWindowsEngineTest, TestExitCloseMultiWindow) { // Quit should not be called when there is more than one window. EXPECT_CALL(*handler, Quit).Times(0); modifier.SetLifecycleManager(std::move(handler)); - // engine->OnApplicationLifecycleEnabled(); engine->lifecycle_manager()->BeginProcessingExit(); engine->Run(); @@ -905,7 +901,6 @@ TEST_F(FlutterWindowsEngineTest, EnableApplicationLifecycle) { }); EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1); modifier.SetLifecycleManager(std::move(handler)); - //engine->OnApplicationLifecycleEnabled(); engine->lifecycle_manager()->BeginProcessingExit(); engine->window_proc_delegate_manager()->OnTopLevelWindowProc(0, WM_CLOSE, 0, @@ -1060,7 +1055,6 @@ TEST_F(FlutterWindowsEngineTest, EnableLifecycleState) { EXPECT_FALSE(finished); // Test that we can set the state afterwards. - // engine->OnApplicationLifecycleEnabled(); engine->lifecycle_manager()->BeginProcessingLifecycle(); view.OnWindowStateEvent(hwnd, WindowStateEvent::kShow); diff --git a/shell/platform/windows/platform_handler.cc b/shell/platform/windows/platform_handler.cc index 771d3d56b9b24..c6bdde6b4ca8a 100644 --- a/shell/platform/windows/platform_handler.cc +++ b/shell/platform/windows/platform_handler.cc @@ -498,7 +498,7 @@ void PlatformHandler::HandleMethodCall( SystemSoundPlay(sound_type.GetString(), std::move(result)); } else if (method.compare(kInitializationCompleteMethod) == 0) { - engine_->OnApplicationLifecycleEnabled(); + // Deprecated but should not cause an error. result->Success(); } else { result->NotImplemented(); From 0ea137c102660d7ebc0fb69fcd31fbb3106deafb Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 17 Aug 2023 15:07:40 -0400 Subject: [PATCH 08/31] Unit test --- .../flutter_windows_engine_unittests.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 38033f181f1e6..18466e436a98a 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1124,7 +1124,25 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { FlutterWindowsEngine* engine = view.GetEngine(); EngineModifier modifier(engine); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; + auto old_run = modifier.embedder_api().Run; + + static bool listened_to = false; + modifier.embedder_api().Run = MOCK_ENGINE_PROC( + Run, ([old_run]( + size_t version, const FlutterRendererConfig* config, + const FlutterProjectArgs* args, void* user_data, + FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { + FlutterProjectArgs new_args = *args; + new_args.channel_listened_to_callback = [](const char* name, size_t len, bool listening, void* user_data) { + listened_to = true; + }; + return old_run(version, config, &new_args, user_data, engine_out); + })); engine->Run(); + + while (!listened_to) { + engine->task_runner()->ProcessTasks(); + } } } // namespace testing From af8cf2d43ee49ae2c2801d773b5dd239406a06c1 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 17 Aug 2023 16:46:01 -0400 Subject: [PATCH 09/31] Doc comments --- lib/ui/window/platform_configuration.h | 9 +++++++++ shell/common/engine.h | 10 +++++++++- shell/platform/windows/flutter_windows_engine.h | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index 0356574405e11..dce7e6f320249 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -217,6 +217,15 @@ class PlatformConfigurationClient { /// virtual void RequestDartDeferredLibrary(intptr_t loading_unit_id) = 0; + //-------------------------------------------------------------------------- + /// @brief Invoked when a listener is registered on a platform channel. + /// + /// @param[in] name The name of the platform channel to which a + /// listener has been registered or cleared. + /// + /// @param[in] listening Whether the listener has been set (true) or + /// cleared (false). + /// virtual void ChannelListenedTo(const std::string& name, bool listening) = 0; protected: diff --git a/shell/common/engine.h b/shell/common/engine.h index 78e04154eeb52..29e7767fdec94 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -294,7 +294,15 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { virtual const std::shared_ptr& GetPlatformMessageHandler() const = 0; - //---------------------------------------------------------------------------- + //-------------------------------------------------------------------------- + /// @brief Invoked when a listener is registered on a platform channel. + /// + /// @param[in] name The name of the platform channel to which a + /// listener has been registered or cleared. + /// + /// @param[in] listening Whether the listener has been set (true) or + /// cleared (false). + /// virtual void OnEngineChannelListenedTo(const std::string& name, bool listening) = 0; }; diff --git a/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index a7eef1cd6b119..02b0638da427e 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -297,6 +297,8 @@ class FlutterWindowsEngine { // created. This is typically caused by a hot restart (Shift-R in CLI.) void OnPreEngineRestart(); + // Invoked by the engine when a listener is set or cleared on a platform + // channel. virtual void OnChannelListenedTo(const std::string& name, bool listening); private: From 711ebac54d095717f3448cda5cf73fd99153e4a1 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 17 Aug 2023 17:04:02 -0400 Subject: [PATCH 10/31] Formatting --- lib/ui/window/platform_configuration.cc | 7 +++++-- lib/ui/window/platform_configuration.h | 5 +++-- runtime/runtime_controller.cc | 3 ++- shell/common/engine.h | 5 +++-- shell/common/shell.h | 3 ++- shell/platform/embedder/embedder.cc | 7 +++++-- shell/platform/embedder/embedder.h | 8 ++++---- shell/platform/embedder/platform_view_embedder.cc | 3 ++- shell/platform/embedder/platform_view_embedder.h | 5 +++-- shell/platform/windows/flutter_windows_engine.cc | 6 ++++-- .../windows/flutter_windows_engine_unittests.cc | 14 +++++++------- 11 files changed, 40 insertions(+), 26 deletions(-) diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index e70baefabcf1f..a232cd97c0b35 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -539,8 +539,11 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( dart_state->SetPlatformMessageHandler(weak_platform_message_handler); } -void PlatformConfigurationNativeApi::PlatformChannelListenedTo(const std::string& name, bool listening) { - UIDartState::Current()->platform_configuration()->client()->ChannelListenedTo(name, listening); +void PlatformConfigurationNativeApi::PlatformChannelListenedTo( + const std::string& name, + bool listening) { + UIDartState::Current()->platform_configuration()->client()->ChannelListenedTo( + name, listening); } } // namespace flutter diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index dce7e6f320249..6e646d1709804 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -222,7 +222,7 @@ class PlatformConfigurationClient { /// /// @param[in] name The name of the platform channel to which a /// listener has been registered or cleared. - /// + /// /// @param[in] listening Whether the listener has been set (true) or /// cleared (false). /// @@ -532,7 +532,8 @@ class PlatformConfigurationNativeApi { static void RespondToPlatformMessage(int response_id, const tonic::DartByteData& data); - static void PlatformChannelListenedTo(const std::string& name, bool listening); + static void PlatformChannelListenedTo(const std::string& name, + bool listening); //-------------------------------------------------------------------------- /// @brief Requests the Dart VM to adjusts the GC heuristics based on diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index 79abea6af0127..4aa1674e82866 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -389,7 +389,8 @@ RuntimeController::ComputePlatformResolvedLocale( } // |PlatformConfigurationClient| -void RuntimeController::ChannelListenedTo(const std::string& name, bool listening) { +void RuntimeController::ChannelListenedTo(const std::string& name, + bool listening) { client_.ChannelListenedTo(name, listening); } diff --git a/shell/common/engine.h b/shell/common/engine.h index 29e7767fdec94..5c97175cf01f0 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -299,11 +299,12 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { /// /// @param[in] name The name of the platform channel to which a /// listener has been registered or cleared. - /// + /// /// @param[in] listening Whether the listener has been set (true) or /// cleared (false). /// - virtual void OnEngineChannelListenedTo(const std::string& name, bool listening) = 0; + virtual void OnEngineChannelListenedTo(const std::string& name, + bool listening) = 0; }; //---------------------------------------------------------------------------- diff --git a/shell/common/shell.h b/shell/common/shell.h index d0233f0f93a25..1a6f47f1231fa 100644 --- a/shell/common/shell.h +++ b/shell/common/shell.h @@ -650,7 +650,8 @@ class Shell final : public PlatformView::Delegate, fml::TimePoint GetCurrentTimePoint() override; // |Engine::Delegate| - void OnEngineChannelListenedTo(const std::string& name, bool listening) override; + void OnEngineChannelListenedTo(const std::string& name, + bool listening) override; // |Rasterizer::Delegate| void OnFrameRasterized(const FrameTiming&) override; diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 918ebdf300b0e..d0596e89da6fd 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1881,9 +1881,12 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, user_data]() { return ptr(user_data); }; } - flutter::PlatformViewEmbedder::ChannelListenedToCallback channel_listened_to_callback = nullptr; + flutter::PlatformViewEmbedder::ChannelListenedToCallback + channel_listened_to_callback = nullptr; if (SAFE_ACCESS(args, channel_listened_to_callback, nullptr) != nullptr) { - channel_listened_to_callback = [ptr = args->channel_listened_to_callback, user_data](const std::string& name, bool listening) { + channel_listened_to_callback = [ptr = args->channel_listened_to_callback, + user_data](const std::string& name, + bool listening) { size_t name_len = name.size(); ptr(name.data(), name_len, listening, user_data); }; diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index 0e684eddf84e4..caaa470348cab 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -1327,10 +1327,10 @@ typedef void (*FlutterUpdateSemanticsCallback2)( void* /* user data*/); typedef void (*FlutterChannelListenedToCallback)( - const char* data /* name */, - size_t data_len /* name.size */, - bool /* listening */, - void* /* user data */); + const char* data /* name */, + size_t data_len /* name.size */, + bool /* listening */, + void* /* user data */); typedef struct _FlutterTaskRunner* FlutterTaskRunner; diff --git a/shell/platform/embedder/platform_view_embedder.cc b/shell/platform/embedder/platform_view_embedder.cc index 75c992deefd99..c67a66efb3faf 100644 --- a/shell/platform/embedder/platform_view_embedder.cc +++ b/shell/platform/embedder/platform_view_embedder.cc @@ -200,7 +200,8 @@ void PlatformViewEmbedder::OnPreEngineRestart() const { } // |PlatformView| -void PlatformViewEmbedder::ChannelListenedTo(const std::string& name, bool listening) { +void PlatformViewEmbedder::ChannelListenedTo(const std::string& name, + bool listening) { if (platform_dispatch_table_.on_channel_listened_to != nullptr) { platform_dispatch_table_.on_channel_listened_to(name, listening); } diff --git a/shell/platform/embedder/platform_view_embedder.h b/shell/platform/embedder/platform_view_embedder.h index 74517f087a870..a299e309baf5e 100644 --- a/shell/platform/embedder/platform_view_embedder.h +++ b/shell/platform/embedder/platform_view_embedder.h @@ -41,7 +41,8 @@ class PlatformViewEmbedder final : public PlatformView { std::function>( const std::vector& supported_locale_data)>; using OnPreEngineRestartCallback = std::function; - using ChannelListenedToCallback = std::function; + using ChannelListenedToCallback = + std::function; struct PlatformDispatchTable { UpdateSemanticsCallback update_semantics_callback; // optional @@ -51,7 +52,7 @@ class PlatformViewEmbedder final : public PlatformView { ComputePlatformResolvedLocaleCallback compute_platform_resolved_locale_callback; OnPreEngineRestartCallback on_pre_engine_restart_callback; // optional - ChannelListenedToCallback on_channel_listened_to; // optional + ChannelListenedToCallback on_channel_listened_to; // optional }; // Create a platform view that sets up a software rasterizer. diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 820aaefe53ceb..ee4d0de85a288 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -373,7 +373,8 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { host->root_isolate_create_callback_(); } }; - args.channel_listened_to_callback = [](const char* name, size_t name_len, bool listening, void* user_data) { + args.channel_listened_to_callback = [](const char* name, size_t name_len, + bool listening, void* user_data) { auto host = static_cast(user_data); std::string channel_name(name, name + name_len); host->OnChannelListenedTo(channel_name, listening); @@ -814,7 +815,8 @@ std::optional FlutterWindowsEngine::ProcessExternalWindowMessage( return std::nullopt; } -void FlutterWindowsEngine::OnChannelListenedTo(const std::string& name, bool listening) { +void FlutterWindowsEngine::OnChannelListenedTo(const std::string& name, + bool listening) { if (name.compare("flutter/platform") == 0) { lifecycle_manager_->BeginProcessingExit(); } else if (name.compare("flutter/lifecycle") == 0) { diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 18466e436a98a..bdbfd3b73b791 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1128,14 +1128,14 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { static bool listened_to = false; modifier.embedder_api().Run = MOCK_ENGINE_PROC( - Run, ([old_run]( - size_t version, const FlutterRendererConfig* config, - const FlutterProjectArgs* args, void* user_data, - FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { + Run, ([old_run](size_t version, const FlutterRendererConfig* config, + const FlutterProjectArgs* args, void* user_data, + FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { FlutterProjectArgs new_args = *args; - new_args.channel_listened_to_callback = [](const char* name, size_t len, bool listening, void* user_data) { - listened_to = true; - }; + new_args.channel_listened_to_callback = + [](const char* name, size_t len, bool listening, void* user_data) { + listened_to = true; + }; return old_run(version, config, &new_args, user_data, engine_out); })); engine->Run(); From 17c85e1284ebabf44b03808986d03d7d3b5f9409 Mon Sep 17 00:00:00 2001 From: schectman Date: Fri, 18 Aug 2023 13:13:08 -0400 Subject: [PATCH 11/31] Update to info struct --- shell/platform/embedder/embedder.cc | 4 ++-- shell/platform/embedder/embedder.h | 9 ++++++--- shell/platform/windows/flutter_windows_engine.cc | 8 ++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index d0596e89da6fd..d78ba86e638f8 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1887,8 +1887,8 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, channel_listened_to_callback = [ptr = args->channel_listened_to_callback, user_data](const std::string& name, bool listening) { - size_t name_len = name.size(); - ptr(name.data(), name_len, listening, user_data); + FlutterChannelUpdate update{name.c_str(), listening}; + ptr(&update, user_data); }; } diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index caaa470348cab..0149e4df2e8e3 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -1326,10 +1326,13 @@ typedef void (*FlutterUpdateSemanticsCallback2)( const FlutterSemanticsUpdate2* /* semantics update */, void* /* user data*/); +typedef struct { + const char* channel; + bool listening; +} FlutterChannelUpdate; + typedef void (*FlutterChannelListenedToCallback)( - const char* data /* name */, - size_t data_len /* name.size */, - bool /* listening */, + const FlutterChannelUpdate* /* channel update */, void* /* user data */); typedef struct _FlutterTaskRunner* FlutterTaskRunner; diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index ee4d0de85a288..34c253a177a78 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -373,11 +373,11 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { host->root_isolate_create_callback_(); } }; - args.channel_listened_to_callback = [](const char* name, size_t name_len, - bool listening, void* user_data) { + args.channel_listened_to_callback = [](const FlutterChannelUpdate* update, + void* user_data) { auto host = static_cast(user_data); - std::string channel_name(name, name + name_len); - host->OnChannelListenedTo(channel_name, listening); + std::string channel_name(update->channel); + host->OnChannelListenedTo(channel_name, update->listening); }; args.custom_task_runners = &custom_task_runners; From da9083ac90bd7a3566833b372a4d68ad629328a3 Mon Sep 17 00:00:00 2001 From: schectman Date: Fri, 18 Aug 2023 14:28:10 -0400 Subject: [PATCH 12/31] Update test --- shell/platform/windows/flutter_windows_engine_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index bdbfd3b73b791..658efa1c627cc 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1133,7 +1133,7 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { FlutterProjectArgs new_args = *args; new_args.channel_listened_to_callback = - [](const char* name, size_t len, bool listening, void* user_data) { + [](const FlutterChannelUpdate* update, void* user_data) { listened_to = true; }; return old_run(version, config, &new_args, user_data, engine_out); From f10ec7298af38eb7f88751d464687028c15734fd Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 21 Aug 2023 16:20:16 -0400 Subject: [PATCH 13/31] Web parity --- lib/web_ui/lib/channel_buffers.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/web_ui/lib/channel_buffers.dart b/lib/web_ui/lib/channel_buffers.dart index 4d0e3fb4a4d13..ecae68f59156c 100644 --- a/lib/web_ui/lib/channel_buffers.dart +++ b/lib/web_ui/lib/channel_buffers.dart @@ -257,6 +257,8 @@ class ChannelBuffers { return true; }()); } + + void channelListenedTo(String name, bool listening) {} } final ChannelBuffers channelBuffers = ChannelBuffers(); From 6d86f71cca7a253e0f1997087fe026c0ee507c36 Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 21 Aug 2023 17:27:54 -0400 Subject: [PATCH 14/31] Add callback to test context --- .../embedder/tests/embedder_config_builder.cc | 5 +++++ .../embedder/tests/embedder_config_builder.h | 2 ++ .../embedder/tests/embedder_test_context.cc | 18 ++++++++++++++++++ .../embedder/tests/embedder_test_context.h | 6 ++++++ .../embedder/tests/embedder_unittests.cc | 14 ++++++++++++++ 5 files changed, 45 insertions(+) diff --git a/shell/platform/embedder/tests/embedder_config_builder.cc b/shell/platform/embedder/tests/embedder_config_builder.cc index f12a9e58b130a..faeb43a9a53be 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/shell/platform/embedder/tests/embedder_config_builder.cc @@ -263,6 +263,11 @@ void EmbedderConfigBuilder::SetLogMessageCallbackHook() { EmbedderTestContext::GetLogMessageCallbackHook(); } +void EmbedderConfigBuilder::SetChannelListenedToCallbackHook() { + project_args_.channel_listened_to_callback = + context_.GetChannelListenedToCallbackHook(); +} + void EmbedderConfigBuilder::SetLogTag(std::string tag) { log_tag_ = std::move(tag); project_args_.log_tag = log_tag_.c_str(); diff --git a/shell/platform/embedder/tests/embedder_config_builder.h b/shell/platform/embedder/tests/embedder_config_builder.h index d3b133c4e402e..4a709d45078c1 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.h +++ b/shell/platform/embedder/tests/embedder_config_builder.h @@ -80,6 +80,8 @@ class EmbedderConfigBuilder { // Used to set a custom log message handler. void SetLogMessageCallbackHook(); + void SetChannelListenedToCallbackHook(); + // Used to set a custom log tag. void SetLogTag(std::string tag); diff --git a/shell/platform/embedder/tests/embedder_test_context.cc b/shell/platform/embedder/tests/embedder_test_context.cc index 99b2fe0f5f8af..7d875c03ef2f3 100644 --- a/shell/platform/embedder/tests/embedder_test_context.cc +++ b/shell/platform/embedder/tests/embedder_test_context.cc @@ -147,6 +147,10 @@ void EmbedderTestContext::SetPlatformMessageCallback( platform_message_callback_ = callback; } +void EmbedderTestContext::SetChannelListenedToCallback(const ChannelListenedToCallback& callback) { + channel_listened_to_callback_ = callback; +} + void EmbedderTestContext::PlatformMessageCallback( const FlutterPlatformMessage* message) { if (platform_message_callback_) { @@ -232,6 +236,20 @@ EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook() { }; } +FlutterChannelListenedToCallback +EmbedderTestContext::GetChannelListenedToCallbackHook() { + if (channel_listened_to_callback_ == nullptr) { + return nullptr; + } + + return [](const FlutterChannelUpdate* update, void* user_data) { + auto context = reinterpret_cast(user_data); + if (context->channel_listened_to_callback_) { + context->channel_listened_to_callback_(update); + } + }; +} + FlutterTransformation EmbedderTestContext::GetRootSurfaceTransformation() { return FlutterTransformationMake(root_surface_transformation_); } diff --git a/shell/platform/embedder/tests/embedder_test_context.h b/shell/platform/embedder/tests/embedder_test_context.h index d59afad2d4777..8bccda385eded 100644 --- a/shell/platform/embedder/tests/embedder_test_context.h +++ b/shell/platform/embedder/tests/embedder_test_context.h @@ -33,6 +33,7 @@ using SemanticsActionCallback = std::function; using LogMessageCallback = std::function; +using ChannelListenedToCallback = std::function; struct AOTDataDeleter { void operator()(FlutterEngineAOTData aot_data) { @@ -89,6 +90,8 @@ class EmbedderTestContext { void SetLogMessageCallback(const LogMessageCallback& log_message_callback); + void SetChannelListenedToCallback(const ChannelListenedToCallback& callback); + std::future> GetNextSceneImage(); EmbedderTestCompositor& GetCompositor(); @@ -132,6 +135,7 @@ class EmbedderTestContext { SemanticsUpdateCallback update_semantics_callback_; SemanticsNodeCallback update_semantics_node_callback_; SemanticsActionCallback update_semantics_custom_action_callback_; + ChannelListenedToCallback channel_listened_to_callback_; std::function platform_message_callback_; LogMessageCallback log_message_callback_; std::unique_ptr compositor_; @@ -155,6 +159,8 @@ class EmbedderTestContext { static FlutterComputePlatformResolvedLocaleCallback GetComputePlatformResolvedLocaleCallbackHook(); + FlutterChannelListenedToCallback GetChannelListenedToCallbackHook(); + void SetupAOTMappingsIfNecessary(); void SetupAOTDataIfNecessary(); diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index cbbf8f5bd9d60..949deacc35a8c 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2562,6 +2562,20 @@ TEST_F(EmbedderTest, CanSendPointer) { message_latch.Wait(); } +TEST_F(EmbedderTest, RegisterChannelListener) { + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); + + fml::AutoResetWaitableEvent latch; + context.AddNativeCallback("SignalNativeTest", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments){ + latch.Signal(); + })); + + EmbedderConfigBuilder builder(context); + builder.SetSoftwareRendererConfig(); + builder.SetDartEntrypoint("channel_listener_response"); +} + } // namespace testing } // namespace flutter From c7a6454c323ec552d98e68df3deaa75caf207602 Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 23 Aug 2023 13:55:26 -0400 Subject: [PATCH 15/31] Start unit test --- shell/platform/embedder/fixtures/main.dart | 9 ++++++++ .../embedder/tests/embedder_config_builder.cc | 1 + .../embedder/tests/embedder_test_context.cc | 3 ++- .../embedder/tests/embedder_test_context.h | 3 ++- .../embedder/tests/embedder_unittests.cc | 23 +++++++++++++++---- 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/shell/platform/embedder/fixtures/main.dart b/shell/platform/embedder/fixtures/main.dart index 5015c1af8ce9d..c48de1daaab81 100644 --- a/shell/platform/embedder/fixtures/main.dart +++ b/shell/platform/embedder/fixtures/main.dart @@ -1289,3 +1289,12 @@ void pointer_data_packet() { signalNativeTest(); } + +@pragma('vm:entry-point') +void channel_listener_response() async { + channelBuffers.setListener('test/listen', + (ByteData? data, PlatformMessageResponseCallback callback) { + callback(null); + }); + signalNativeTest(); +} diff --git a/shell/platform/embedder/tests/embedder_config_builder.cc b/shell/platform/embedder/tests/embedder_config_builder.cc index faeb43a9a53be..57b47d6e2c991 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/shell/platform/embedder/tests/embedder_config_builder.cc @@ -114,6 +114,7 @@ EmbedderConfigBuilder::EmbedderConfigBuilder( SetSemanticsCallbackHooks(); SetLogMessageCallbackHook(); SetLocalizationCallbackHooks(); + SetChannelListenedToCallbackHook(); AddCommandLineArgument("--disable-vm-service"); if (preference == InitializationPreference::kSnapshotsInitialize || diff --git a/shell/platform/embedder/tests/embedder_test_context.cc b/shell/platform/embedder/tests/embedder_test_context.cc index 7d875c03ef2f3..8e91e16555cc0 100644 --- a/shell/platform/embedder/tests/embedder_test_context.cc +++ b/shell/platform/embedder/tests/embedder_test_context.cc @@ -147,7 +147,8 @@ void EmbedderTestContext::SetPlatformMessageCallback( platform_message_callback_ = callback; } -void EmbedderTestContext::SetChannelListenedToCallback(const ChannelListenedToCallback& callback) { +void EmbedderTestContext::SetChannelListenedToCallback( + const ChannelListenedToCallback& callback) { channel_listened_to_callback_ = callback; } diff --git a/shell/platform/embedder/tests/embedder_test_context.h b/shell/platform/embedder/tests/embedder_test_context.h index 8bccda385eded..2b12065881b58 100644 --- a/shell/platform/embedder/tests/embedder_test_context.h +++ b/shell/platform/embedder/tests/embedder_test_context.h @@ -33,7 +33,8 @@ using SemanticsActionCallback = std::function; using LogMessageCallback = std::function; -using ChannelListenedToCallback = std::function; +using ChannelListenedToCallback = + std::function; struct AOTDataDeleter { void operator()(FlutterEngineAOTData aot_data) { diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index 949deacc35a8c..c03bff05e71de 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2566,14 +2566,29 @@ TEST_F(EmbedderTest, RegisterChannelListener) { auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch; - context.AddNativeCallback("SignalNativeTest", - CREATE_NATIVE_ENTRY([&](Dart_NativeArguments){ - latch.Signal(); - })); + fml::AutoResetWaitableEvent latch2; + bool listening = false; + context.AddNativeCallback( + "SignalNativeTest", + CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) { latch.Signal(); })); + context.SetChannelListenedToCallback([&](const FlutterChannelUpdate* update) { + if (strcmp(update->channel, "test/listen") == 0) { + listening = true; + } + latch2.Signal(); + }); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.SetDartEntrypoint("channel_listener_response"); + + auto engine = builder.LaunchEngine(); + ASSERT_TRUE(engine.is_valid()); + + latch.Wait(); + latch2.Wait(); + + ASSERT_TRUE(listening); } } // namespace testing From d136a934084ca8d1dda974a35767662a5592c47b Mon Sep 17 00:00:00 2001 From: schectman Date: Wed, 23 Aug 2023 18:00:53 -0400 Subject: [PATCH 16/31] Run expired tasks --- shell/platform/embedder/tests/embedder_unittests.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index c03bff05e71de..8c9fd223fd5e3 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2586,6 +2586,7 @@ TEST_F(EmbedderTest, RegisterChannelListener) { ASSERT_TRUE(engine.is_valid()); latch.Wait(); + fml::MessageLoop::GetCurrent().RunExpiredTasksNow(); latch2.Wait(); ASSERT_TRUE(listening); From 3413635a24a53d75cdfe42209f07f00029c065c6 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 12:34:07 -0400 Subject: [PATCH 17/31] Rename methods --- lib/ui/channel_buffers.dart | 10 +++++----- lib/ui/dart_ui.cc | 2 +- lib/ui/window/platform_configuration.cc | 7 +++---- lib/ui/window/platform_configuration.h | 5 ++--- lib/web_ui/lib/channel_buffers.dart | 2 +- runtime/runtime_controller.cc | 4 ++-- runtime/runtime_controller.h | 2 +- runtime/runtime_delegate.h | 2 +- shell/common/engine.cc | 4 ++-- shell/common/engine.h | 6 +++--- shell/common/engine_unittests.cc | 4 ++-- shell/common/platform_view.cc | 2 +- shell/common/platform_view.h | 10 +++++++++- shell/common/shell.cc | 4 ++-- shell/common/shell.h | 3 +-- shell/platform/embedder/embedder.cc | 13 ++++++------- shell/platform/embedder/embedder.h | 4 ++-- .../platform/embedder/platform_view_embedder.cc | 6 +++--- shell/platform/embedder/platform_view_embedder.h | 7 +++---- .../embedder/tests/embedder_config_builder.cc | 8 ++++---- .../embedder/tests/embedder_config_builder.h | 2 +- .../embedder/tests/embedder_test_context.cc | 16 ++++++++-------- .../embedder/tests/embedder_test_context.h | 9 ++++----- .../embedder/tests/embedder_unittests.cc | 2 +- shell/platform/windows/flutter_windows_engine.cc | 10 +++++----- shell/platform/windows/flutter_windows_engine.h | 2 +- .../windows/flutter_windows_engine_unittests.cc | 2 +- 27 files changed, 75 insertions(+), 73 deletions(-) diff --git a/lib/ui/channel_buffers.dart b/lib/ui/channel_buffers.dart index 3e6b927487d42..f5db1613b8cfb 100644 --- a/lib/ui/channel_buffers.dart +++ b/lib/ui/channel_buffers.dart @@ -380,7 +380,7 @@ class ChannelBuffers { assert(!name.contains('\u0000'), 'Channel names must not contain U+0000 NULL characters.'); final _Channel channel = _channels.putIfAbsent(name, () => _Channel()); channel.setListener(callback); - channelListenedTo(name, true); + sendChannelUpdate(name, true); } /// Clears the listener for the specified channel. @@ -393,14 +393,14 @@ class ChannelBuffers { final _Channel? channel = _channels[name]; if (channel != null) { channel.clearListener(); - channelListenedTo(name, false); + sendChannelUpdate(name, false); } } - @Native(symbol: 'PlatformConfigurationNativeApi::PlatformChannelListenedTo') - external static void _channelListenedTo(String name, bool listening); + @Native(symbol: 'PlatformConfigurationNativeApi::SendChannelUpdate') + external static void _sendChannelUpdate(String name, bool listening); - void channelListenedTo(String name, bool listening) => _channelListenedTo(name, listening); + void sendChannelUpdate(String name, bool listening) => _sendChannelUpdate(name, listening); /// Deprecated. Migrate to [setListener] instead. /// diff --git a/lib/ui/dart_ui.cc b/lib/ui/dart_ui.cc index 224a506638cb1..976b373d29d08 100644 --- a/lib/ui/dart_ui.cc +++ b/lib/ui/dart_ui.cc @@ -110,7 +110,7 @@ typedef CanvasPath Path; V(PlatformConfigurationNativeApi::GetRootIsolateToken, 0) \ V(PlatformConfigurationNativeApi::RegisterBackgroundIsolate, 1) \ V(PlatformConfigurationNativeApi::SendPortPlatformMessage, 4) \ - V(PlatformConfigurationNativeApi::PlatformChannelListenedTo, 2) \ + V(PlatformConfigurationNativeApi::SendChannelUpdate, 2) \ V(PlatformConfigurationNativeApi::GetScaledFontSize, 2) \ V(DartRuntimeHooks::Logger_PrintDebugString, 1) \ V(DartRuntimeHooks::Logger_PrintString, 1) \ diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index 0cbf5652b0ddc..d19c80a7a8028 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -631,10 +631,9 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( dart_state->SetPlatformMessageHandler(weak_platform_message_handler); } -void PlatformConfigurationNativeApi::PlatformChannelListenedTo( - const std::string& name, - bool listening) { - UIDartState::Current()->platform_configuration()->client()->ChannelListenedTo( +void PlatformConfigurationNativeApi::SendChannelUpdate(const std::string& name, + bool listening) { + UIDartState::Current()->platform_configuration()->client()->SendChannelUpdate( name, listening); } diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index fda46c7e99fff..ddbec2b875709 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -216,7 +216,7 @@ class PlatformConfigurationClient { /// @param[in] listening Whether the listener has been set (true) or /// cleared (false). /// - virtual void ChannelListenedTo(const std::string& name, bool listening) = 0; + virtual void SendChannelUpdate(const std::string& name, bool listening) = 0; //-------------------------------------------------------------------------- /// @brief Synchronously invokes platform-specific APIs to apply the @@ -582,8 +582,7 @@ class PlatformConfigurationNativeApi { static void RespondToPlatformMessage(int response_id, const tonic::DartByteData& data); - static void PlatformChannelListenedTo(const std::string& name, - bool listening); + static void SendChannelUpdate(const std::string& name, bool listening); //-------------------------------------------------------------------------- /// @brief Requests the Dart VM to adjusts the GC heuristics based on diff --git a/lib/web_ui/lib/channel_buffers.dart b/lib/web_ui/lib/channel_buffers.dart index ecae68f59156c..9962cf6dde7cf 100644 --- a/lib/web_ui/lib/channel_buffers.dart +++ b/lib/web_ui/lib/channel_buffers.dart @@ -258,7 +258,7 @@ class ChannelBuffers { }()); } - void channelListenedTo(String name, bool listening) {} + void sendChannelUpdate(String name, bool listening) {} } final ChannelBuffers channelBuffers = ChannelBuffers(); diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index 4bdf1c85200c4..c954687af71a7 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -402,9 +402,9 @@ RuntimeController::ComputePlatformResolvedLocale( } // |PlatformConfigurationClient| -void RuntimeController::ChannelListenedTo(const std::string& name, +void RuntimeController::SendChannelUpdate(const std::string& name, bool listening) { - client_.ChannelListenedTo(name, listening); + client_.SendChannelUpdate(name, listening); } Dart_Port RuntimeController::GetMainPort() { diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index c042b6d266ce0..7054386aca2cc 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -684,7 +684,7 @@ class RuntimeController : public PlatformConfigurationClient { const std::vector& supported_locale_data) override; // |PlatformConfigurationClient| - void ChannelListenedTo(const std::string& name, bool listening) override; + void SendChannelUpdate(const std::string& name, bool listening) override; // |PlatformConfigurationClient| double GetScaledFontSize(double unscaled_font_size, diff --git a/runtime/runtime_delegate.h b/runtime/runtime_delegate.h index 99f383ad1869a..dede8f3d52b89 100644 --- a/runtime/runtime_delegate.h +++ b/runtime/runtime_delegate.h @@ -54,7 +54,7 @@ class RuntimeDelegate { virtual std::weak_ptr GetPlatformMessageHandler() const = 0; - virtual void ChannelListenedTo(const std::string& name, bool listening) = 0; + virtual void SendChannelUpdate(const std::string& name, bool listening) = 0; virtual double GetScaledFontSize(double unscaled_font_size, int configuration_id) const = 0; diff --git a/shell/common/engine.cc b/shell/common/engine.cc index 131381da4075d..4d88e75b636af 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -570,8 +570,8 @@ std::weak_ptr Engine::GetPlatformMessageHandler() return delegate_.GetPlatformMessageHandler(); } -void Engine::ChannelListenedTo(const std::string& name, bool listening) { - delegate_.OnEngineChannelListenedTo(name, listening); +void Engine::SendChannelUpdate(const std::string& name, bool listening) { + delegate_.OnEngineChannelUpdate(name, listening); } void Engine::LoadDartDeferredLibrary( diff --git a/shell/common/engine.h b/shell/common/engine.h index 8a2a58174d3b8..403065fc5315a 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -303,8 +303,8 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { /// @param[in] listening Whether the listener has been set (true) or /// cleared (false). /// - virtual void OnEngineChannelListenedTo(const std::string& name, - bool listening) = 0; + virtual void OnEngineChannelUpdate(const std::string& name, + bool listening) = 0; //-------------------------------------------------------------------------- /// @brief Synchronously invokes platform-specific APIs to apply the @@ -990,7 +990,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { const override; // |RuntimeDelegate| - void ChannelListenedTo(const std::string& name, bool listening) override; + void SendChannelUpdate(const std::string& name, bool listening) override; // |RuntimeDelegate| double GetScaledFontSize(double unscaled_font_size, diff --git a/shell/common/engine_unittests.cc b/shell/common/engine_unittests.cc index c7266b337c719..a5139fb5e0d77 100644 --- a/shell/common/engine_unittests.cc +++ b/shell/common/engine_unittests.cc @@ -39,7 +39,7 @@ class MockDelegate : public Engine::Delegate { MOCK_METHOD0(GetCurrentTimePoint, fml::TimePoint()); MOCK_CONST_METHOD0(GetPlatformMessageHandler, const std::shared_ptr&()); - MOCK_METHOD2(OnEngineChannelListenedTo, void(const std::string&, bool)); + MOCK_METHOD2(OnEngineChannelUpdate, void(const std::string&, bool)); MOCK_CONST_METHOD2(GetScaledFontSize, double(double font_size, int configuration_id)); }; @@ -70,7 +70,7 @@ class MockRuntimeDelegate : public RuntimeDelegate { MOCK_METHOD1(RequestDartDeferredLibrary, void(intptr_t)); MOCK_CONST_METHOD0(GetPlatformMessageHandler, std::weak_ptr()); - MOCK_METHOD2(ChannelListenedTo, void(const std::string&, bool)); + MOCK_METHOD2(SendChannelUpdate, void(const std::string&, bool)); MOCK_CONST_METHOD2(GetScaledFontSize, double(double font_size, int configuration_id)); }; diff --git a/shell/common/platform_view.cc b/shell/common/platform_view.cc index e826a6aa7a607..48439c252447a 100644 --- a/shell/common/platform_view.cc +++ b/shell/common/platform_view.cc @@ -114,7 +114,7 @@ void PlatformView::UpdateSemantics( // NOLINTNEXTLINE(performance-unnecessary-value-param) CustomAccessibilityActionUpdates actions) {} -void PlatformView::ChannelListenedTo(const std::string& name, bool listening) {} +void PlatformView::SendChannelUpdate(const std::string& name, bool listening) {} void PlatformView::HandlePlatformMessage( std::unique_ptr message) { diff --git a/shell/common/platform_view.h b/shell/common/platform_view.h index ae393981e748a..5e0a4882f1f0d 100644 --- a/shell/common/platform_view.h +++ b/shell/common/platform_view.h @@ -466,7 +466,15 @@ class PlatformView { CustomAccessibilityActionUpdates actions); //---------------------------------------------------------------------------- - virtual void ChannelListenedTo(const std::string& name, bool listening); + /// @brief Used by the framework to tell the embedder that it has + /// registered a listener on a given channel + /// + /// @param[in] name The name of the channel on which the listener has + /// set or cleared a listener. + /// @param[in] listening True if a listener has been set, false if it has + /// been cleared. + /// + virtual void SendChannelUpdate(const std::string& name, bool listening); //---------------------------------------------------------------------------- /// @brief Used by embedders to specify the updated viewport metrics for diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 00eba740470c0..4538f24a703ee 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1328,14 +1328,14 @@ void Shell::OnEngineHandlePlatformMessage( } } -void Shell::OnEngineChannelListenedTo(const std::string& name, bool listening) { +void Shell::OnEngineChannelUpdate(const std::string& name, bool listening) { FML_DCHECK(is_set_up_); FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); task_runners_.GetPlatformTaskRunner()->PostTask( [view = platform_view_->GetWeakPtr(), name, listening] { if (view) { - view->ChannelListenedTo(name, listening); + view->SendChannelUpdate(name, listening); } }); } diff --git a/shell/common/shell.h b/shell/common/shell.h index 112589898f4c6..c3023aff0ffa6 100644 --- a/shell/common/shell.h +++ b/shell/common/shell.h @@ -681,8 +681,7 @@ class Shell final : public PlatformView::Delegate, fml::TimePoint GetCurrentTimePoint() override; // |Engine::Delegate| - void OnEngineChannelListenedTo(const std::string& name, - bool listening) override; + void OnEngineChannelUpdate(const std::string& name, bool listening) override; // |Engine::Delegate| double GetScaledFontSize(double unscaled_font_size, diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index d78ba86e638f8..93822bf9337e5 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1881,12 +1881,11 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, user_data]() { return ptr(user_data); }; } - flutter::PlatformViewEmbedder::ChannelListenedToCallback - channel_listened_to_callback = nullptr; - if (SAFE_ACCESS(args, channel_listened_to_callback, nullptr) != nullptr) { - channel_listened_to_callback = [ptr = args->channel_listened_to_callback, - user_data](const std::string& name, - bool listening) { + flutter::PlatformViewEmbedder::ChanneUpdateCallback channel_update_callback = + nullptr; + if (SAFE_ACCESS(args, channel_update_callback, nullptr) != nullptr) { + channel_update_callback = [ptr = args->channel_update_callback, user_data]( + const std::string& name, bool listening) { FlutterChannelUpdate update{name.c_str(), listening}; ptr(&update, user_data); }; @@ -1906,7 +1905,7 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, vsync_callback, // compute_platform_resolved_locale_callback, // on_pre_engine_restart_callback, // - channel_listened_to_callback, // + channel_update_callback, // }; auto on_create_platform_view = InferPlatformViewCreationCallback( diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index b4e6037e3a96d..ef2d0a1f48152 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -1407,7 +1407,7 @@ typedef struct { bool listening; } FlutterChannelUpdate; -typedef void (*FlutterChannelListenedToCallback)( +typedef void (*FlutterChannelUpdateCallback)( const FlutterChannelUpdate* /* channel update */, void* /* user data */); @@ -2206,7 +2206,7 @@ typedef struct { /// The callback invoked by the engine in response to a channel listener /// being registered on the framework side. - FlutterChannelListenedToCallback channel_listened_to_callback; + FlutterChannelUpdateCallback channel_update_callback; } FlutterProjectArgs; #ifndef FLUTTER_ENGINE_NO_PROTOTYPES diff --git a/shell/platform/embedder/platform_view_embedder.cc b/shell/platform/embedder/platform_view_embedder.cc index c67a66efb3faf..01bfbe97cb8eb 100644 --- a/shell/platform/embedder/platform_view_embedder.cc +++ b/shell/platform/embedder/platform_view_embedder.cc @@ -200,10 +200,10 @@ void PlatformViewEmbedder::OnPreEngineRestart() const { } // |PlatformView| -void PlatformViewEmbedder::ChannelListenedTo(const std::string& name, +void PlatformViewEmbedder::SendChannelUpdate(const std::string& name, bool listening) { - if (platform_dispatch_table_.on_channel_listened_to != nullptr) { - platform_dispatch_table_.on_channel_listened_to(name, listening); + if (platform_dispatch_table_.on_channel_update != nullptr) { + platform_dispatch_table_.on_channel_update(name, listening); } } diff --git a/shell/platform/embedder/platform_view_embedder.h b/shell/platform/embedder/platform_view_embedder.h index a299e309baf5e..a6452c6b7e18c 100644 --- a/shell/platform/embedder/platform_view_embedder.h +++ b/shell/platform/embedder/platform_view_embedder.h @@ -41,8 +41,7 @@ class PlatformViewEmbedder final : public PlatformView { std::function>( const std::vector& supported_locale_data)>; using OnPreEngineRestartCallback = std::function; - using ChannelListenedToCallback = - std::function; + using ChanneUpdateCallback = std::function; struct PlatformDispatchTable { UpdateSemanticsCallback update_semantics_callback; // optional @@ -52,7 +51,7 @@ class PlatformViewEmbedder final : public PlatformView { ComputePlatformResolvedLocaleCallback compute_platform_resolved_locale_callback; OnPreEngineRestartCallback on_pre_engine_restart_callback; // optional - ChannelListenedToCallback on_channel_listened_to; // optional + ChanneUpdateCallback on_channel_update; // optional }; // Create a platform view that sets up a software rasterizer. @@ -138,7 +137,7 @@ class PlatformViewEmbedder final : public PlatformView { const std::vector& supported_locale_data) override; // |PlatformView| - void ChannelListenedTo(const std::string& name, bool listening) override; + void SendChannelUpdate(const std::string& name, bool listening) override; FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewEmbedder); }; diff --git a/shell/platform/embedder/tests/embedder_config_builder.cc b/shell/platform/embedder/tests/embedder_config_builder.cc index 57b47d6e2c991..48b8ba3686a49 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/shell/platform/embedder/tests/embedder_config_builder.cc @@ -114,7 +114,7 @@ EmbedderConfigBuilder::EmbedderConfigBuilder( SetSemanticsCallbackHooks(); SetLogMessageCallbackHook(); SetLocalizationCallbackHooks(); - SetChannelListenedToCallbackHook(); + SetChannelUpdateCallbackHook(); AddCommandLineArgument("--disable-vm-service"); if (preference == InitializationPreference::kSnapshotsInitialize || @@ -264,9 +264,9 @@ void EmbedderConfigBuilder::SetLogMessageCallbackHook() { EmbedderTestContext::GetLogMessageCallbackHook(); } -void EmbedderConfigBuilder::SetChannelListenedToCallbackHook() { - project_args_.channel_listened_to_callback = - context_.GetChannelListenedToCallbackHook(); +void EmbedderConfigBuilder::SetChannelUpdateCallbackHook() { + project_args_.channel_update_callback = + context_.GetChannelUpdateCallbackHook(); } void EmbedderConfigBuilder::SetLogTag(std::string tag) { diff --git a/shell/platform/embedder/tests/embedder_config_builder.h b/shell/platform/embedder/tests/embedder_config_builder.h index 4a709d45078c1..dfa9ce4908f08 100644 --- a/shell/platform/embedder/tests/embedder_config_builder.h +++ b/shell/platform/embedder/tests/embedder_config_builder.h @@ -80,7 +80,7 @@ class EmbedderConfigBuilder { // Used to set a custom log message handler. void SetLogMessageCallbackHook(); - void SetChannelListenedToCallbackHook(); + void SetChannelUpdateCallbackHook(); // Used to set a custom log tag. void SetLogTag(std::string tag); diff --git a/shell/platform/embedder/tests/embedder_test_context.cc b/shell/platform/embedder/tests/embedder_test_context.cc index 8e91e16555cc0..71dc2309dfe30 100644 --- a/shell/platform/embedder/tests/embedder_test_context.cc +++ b/shell/platform/embedder/tests/embedder_test_context.cc @@ -147,9 +147,9 @@ void EmbedderTestContext::SetPlatformMessageCallback( platform_message_callback_ = callback; } -void EmbedderTestContext::SetChannelListenedToCallback( - const ChannelListenedToCallback& callback) { - channel_listened_to_callback_ = callback; +void EmbedderTestContext::SetChannelUpdateCallback( + const ChannelUpdateCallback& callback) { + channel_update_callback_ = callback; } void EmbedderTestContext::PlatformMessageCallback( @@ -237,16 +237,16 @@ EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook() { }; } -FlutterChannelListenedToCallback -EmbedderTestContext::GetChannelListenedToCallbackHook() { - if (channel_listened_to_callback_ == nullptr) { +FlutterChannelUpdateCallback +EmbedderTestContext::GetChannelUpdateCallbackHook() { + if (channel_update_callback_ == nullptr) { return nullptr; } return [](const FlutterChannelUpdate* update, void* user_data) { auto context = reinterpret_cast(user_data); - if (context->channel_listened_to_callback_) { - context->channel_listened_to_callback_(update); + if (context->channel_update_callback_) { + context->channel_update_callback_(update); } }; } diff --git a/shell/platform/embedder/tests/embedder_test_context.h b/shell/platform/embedder/tests/embedder_test_context.h index 2b12065881b58..4bb675b2ced06 100644 --- a/shell/platform/embedder/tests/embedder_test_context.h +++ b/shell/platform/embedder/tests/embedder_test_context.h @@ -33,8 +33,7 @@ using SemanticsActionCallback = std::function; using LogMessageCallback = std::function; -using ChannelListenedToCallback = - std::function; +using ChannelUpdateCallback = std::function; struct AOTDataDeleter { void operator()(FlutterEngineAOTData aot_data) { @@ -91,7 +90,7 @@ class EmbedderTestContext { void SetLogMessageCallback(const LogMessageCallback& log_message_callback); - void SetChannelListenedToCallback(const ChannelListenedToCallback& callback); + void SetChannelUpdateCallback(const ChannelUpdateCallback& callback); std::future> GetNextSceneImage(); @@ -136,7 +135,7 @@ class EmbedderTestContext { SemanticsUpdateCallback update_semantics_callback_; SemanticsNodeCallback update_semantics_node_callback_; SemanticsActionCallback update_semantics_custom_action_callback_; - ChannelListenedToCallback channel_listened_to_callback_; + ChannelUpdateCallback channel_update_callback_; std::function platform_message_callback_; LogMessageCallback log_message_callback_; std::unique_ptr compositor_; @@ -160,7 +159,7 @@ class EmbedderTestContext { static FlutterComputePlatformResolvedLocaleCallback GetComputePlatformResolvedLocaleCallbackHook(); - FlutterChannelListenedToCallback GetChannelListenedToCallbackHook(); + FlutterChannelUpdateCallback GetChannelUpdateCallbackHook(); void SetupAOTMappingsIfNecessary(); diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index 8c9fd223fd5e3..4679d611d10d0 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2571,7 +2571,7 @@ TEST_F(EmbedderTest, RegisterChannelListener) { context.AddNativeCallback( "SignalNativeTest", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) { latch.Signal(); })); - context.SetChannelListenedToCallback([&](const FlutterChannelUpdate* update) { + context.SetChannelUpdateCallback([&](const FlutterChannelUpdate* update) { if (strcmp(update->channel, "test/listen") == 0) { listening = true; } diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 34c253a177a78..d220a7413ca0e 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -373,11 +373,11 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { host->root_isolate_create_callback_(); } }; - args.channel_listened_to_callback = [](const FlutterChannelUpdate* update, - void* user_data) { + args.channel_update_callback = [](const FlutterChannelUpdate* update, + void* user_data) { auto host = static_cast(user_data); std::string channel_name(update->channel); - host->OnChannelListenedTo(channel_name, update->listening); + host->OnChannelUpdate(channel_name, update->listening); }; args.custom_task_runners = &custom_task_runners; @@ -815,8 +815,8 @@ std::optional FlutterWindowsEngine::ProcessExternalWindowMessage( return std::nullopt; } -void FlutterWindowsEngine::OnChannelListenedTo(const std::string& name, - bool listening) { +void FlutterWindowsEngine::OnChannelUpdate(const std::string& name, + bool listening) { if (name.compare("flutter/platform") == 0) { lifecycle_manager_->BeginProcessingExit(); } else if (name.compare("flutter/lifecycle") == 0) { diff --git a/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index 02b0638da427e..c924b5a7a1fef 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -299,7 +299,7 @@ class FlutterWindowsEngine { // Invoked by the engine when a listener is set or cleared on a platform // channel. - virtual void OnChannelListenedTo(const std::string& name, bool listening); + virtual void OnChannelUpdate(const std::string& name, bool listening); private: // Allows swapping out embedder_api_ calls in tests. diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 658efa1c627cc..eb4924003ad75 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1132,7 +1132,7 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { const FlutterProjectArgs* args, void* user_data, FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { FlutterProjectArgs new_args = *args; - new_args.channel_listened_to_callback = + new_args.channel_update_callback = [](const FlutterChannelUpdate* update, void* user_data) { listened_to = true; }; From ff1e6c271b3c205d0db7948a10fc6f5bc263a2c3 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 14:04:27 -0400 Subject: [PATCH 18/31] Copy channel name in lambda --- shell/common/shell.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 4538f24a703ee..82ff0a84ccb3a 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1333,7 +1333,7 @@ void Shell::OnEngineChannelUpdate(const std::string& name, bool listening) { FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); task_runners_.GetPlatformTaskRunner()->PostTask( - [view = platform_view_->GetWeakPtr(), name, listening] { + [view = platform_view_->GetWeakPtr(), name = std::move(name), listening] { if (view) { view->SendChannelUpdate(name, listening); } From 6cf73380a6951694ad482abe39a599f894692a6e Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 14:37:49 -0400 Subject: [PATCH 19/31] Meet mac tidy --- shell/common/shell.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 82ff0a84ccb3a..7a7c73d44b0c2 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1333,7 +1333,7 @@ void Shell::OnEngineChannelUpdate(const std::string& name, bool listening) { FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); task_runners_.GetPlatformTaskRunner()->PostTask( - [view = platform_view_->GetWeakPtr(), name = std::move(name), listening] { + [view = platform_view_->GetWeakPtr(), name = name, listening] { if (view) { view->SendChannelUpdate(name, listening); } From f0ac39768883975fe1396cc9d779a43758d51554 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 15:46:08 -0400 Subject: [PATCH 20/31] Required named param --- lib/ui/channel_buffers.dart | 6 +++--- lib/web_ui/lib/channel_buffers.dart | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ui/channel_buffers.dart b/lib/ui/channel_buffers.dart index f5db1613b8cfb..881d34c39c4af 100644 --- a/lib/ui/channel_buffers.dart +++ b/lib/ui/channel_buffers.dart @@ -380,7 +380,7 @@ class ChannelBuffers { assert(!name.contains('\u0000'), 'Channel names must not contain U+0000 NULL characters.'); final _Channel channel = _channels.putIfAbsent(name, () => _Channel()); channel.setListener(callback); - sendChannelUpdate(name, true); + sendChannelUpdate(name, listening: true); } /// Clears the listener for the specified channel. @@ -393,14 +393,14 @@ class ChannelBuffers { final _Channel? channel = _channels[name]; if (channel != null) { channel.clearListener(); - sendChannelUpdate(name, false); + sendChannelUpdate(name, listening: false); } } @Native(symbol: 'PlatformConfigurationNativeApi::SendChannelUpdate') external static void _sendChannelUpdate(String name, bool listening); - void sendChannelUpdate(String name, bool listening) => _sendChannelUpdate(name, listening); + void sendChannelUpdate(String name, {required bool listening}) => _sendChannelUpdate(name, listening); /// Deprecated. Migrate to [setListener] instead. /// diff --git a/lib/web_ui/lib/channel_buffers.dart b/lib/web_ui/lib/channel_buffers.dart index 9962cf6dde7cf..48d777ce2d539 100644 --- a/lib/web_ui/lib/channel_buffers.dart +++ b/lib/web_ui/lib/channel_buffers.dart @@ -258,7 +258,7 @@ class ChannelBuffers { }()); } - void sendChannelUpdate(String name, bool listening) {} + void sendChannelUpdate(String name, {required bool listening}) {} } final ChannelBuffers channelBuffers = ChannelBuffers(); From bff347a6ac0c3b070995bc3fbeb7efc32b0dbf81 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 16:15:15 -0400 Subject: [PATCH 21/31] Check for listener set --- shell/platform/windows/flutter_windows_engine.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index d220a7413ca0e..76e2dd8e8b661 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -817,9 +817,9 @@ std::optional FlutterWindowsEngine::ProcessExternalWindowMessage( void FlutterWindowsEngine::OnChannelUpdate(const std::string& name, bool listening) { - if (name.compare("flutter/platform") == 0) { + if (name.compare("flutter/platform") == 0 && listening) { lifecycle_manager_->BeginProcessingExit(); - } else if (name.compare("flutter/lifecycle") == 0) { + } else if (name.compare("flutter/lifecycle") == 0 && listening) { lifecycle_manager_->BeginProcessingLifecycle(); } } From 2e87fa915b865e4a9a42ab6a5eb2c9a2fc0d6d27 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 16:21:59 -0400 Subject: [PATCH 22/31] Check values in unittest --- shell/platform/windows/flutter_windows_engine_unittests.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index eb4924003ad75..79a9403f0672f 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1135,6 +1135,8 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { new_args.channel_update_callback = [](const FlutterChannelUpdate* update, void* user_data) { listened_to = true; + EXPECT_EQ(strcmp(update->channel, "flutter/lifecycle"), 0); + EXPECT_TRUE(update->listening); }; return old_run(version, config, &new_args, user_data, engine_out); })); From cf671c8e6fbec34b4e04694955bb5b7545f18649 Mon Sep 17 00:00:00 2001 From: schectman Date: Thu, 24 Aug 2023 17:12:49 -0400 Subject: [PATCH 23/31] PR feedback --- shell/platform/embedder/embedder.h | 3 +++ shell/platform/embedder/tests/embedder_unittests.cc | 6 +++--- shell/platform/windows/flutter_windows_engine.cc | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index ef2d0a1f48152..c9e58b603fefb 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -1402,8 +1402,11 @@ typedef void (*FlutterUpdateSemanticsCallback2)( const FlutterSemanticsUpdate2* /* semantics update */, void* /* user data*/); +/// An update to whether a message channel has a listener set or not. typedef struct { + /// The name of the channel. const char* channel; + /// True if a listener has been set, false if one has been cleared. bool listening; } FlutterChannelUpdate; diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index 4679d611d10d0..2026279daa544 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2572,9 +2572,9 @@ TEST_F(EmbedderTest, RegisterChannelListener) { "SignalNativeTest", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) { latch.Signal(); })); context.SetChannelUpdateCallback([&](const FlutterChannelUpdate* update) { - if (strcmp(update->channel, "test/listen") == 0) { - listening = true; - } + EXPECT_EQ(strcmp(update->channel, "test/listen"), 0); + EXPECT_TRUE(update->listening); + listening = true; latch2.Signal(); }); diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 76e2dd8e8b661..7a10891e8262a 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -817,9 +817,9 @@ std::optional FlutterWindowsEngine::ProcessExternalWindowMessage( void FlutterWindowsEngine::OnChannelUpdate(const std::string& name, bool listening) { - if (name.compare("flutter/platform") == 0 && listening) { + if (name == "flutter/platform" && listening) { lifecycle_manager_->BeginProcessingExit(); - } else if (name.compare("flutter/lifecycle") == 0 && listening) { + } else if (name == "flutter/lifecycle" && listening) { lifecycle_manager_->BeginProcessingLifecycle(); } } From 62b015c7ee52105934a1bfe0e17f1bf9f989b74f Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 28 Aug 2023 11:42:40 -0400 Subject: [PATCH 24/31] Remove const reference --- lib/ui/window/platform_configuration.cc | 2 +- lib/ui/window/platform_configuration.h | 2 +- runtime/runtime_controller.cc | 5 ++--- runtime/runtime_controller.h | 2 +- runtime/runtime_delegate.h | 2 +- shell/common/engine.cc | 4 ++-- shell/common/engine.h | 5 ++--- shell/common/engine_unittests.cc | 4 ++-- shell/common/platform_view.cc | 2 +- shell/common/platform_view.h | 2 +- shell/common/shell.cc | 4 ++-- shell/common/shell.h | 2 +- shell/platform/embedder/embedder.cc | 2 +- shell/platform/embedder/platform_view_embedder.cc | 5 ++--- shell/platform/embedder/platform_view_embedder.h | 4 ++-- shell/platform/windows/flutter_windows_engine.cc | 5 ++--- shell/platform/windows/flutter_windows_engine.h | 2 +- 17 files changed, 25 insertions(+), 29 deletions(-) diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index d19c80a7a8028..0aff121b815a6 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -634,7 +634,7 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( void PlatformConfigurationNativeApi::SendChannelUpdate(const std::string& name, bool listening) { UIDartState::Current()->platform_configuration()->client()->SendChannelUpdate( - name, listening); + std::move(name), listening); } double PlatformConfigurationNativeApi::GetScaledFontSize( diff --git a/lib/ui/window/platform_configuration.h b/lib/ui/window/platform_configuration.h index ddbec2b875709..7965ba10a7b91 100644 --- a/lib/ui/window/platform_configuration.h +++ b/lib/ui/window/platform_configuration.h @@ -216,7 +216,7 @@ class PlatformConfigurationClient { /// @param[in] listening Whether the listener has been set (true) or /// cleared (false). /// - virtual void SendChannelUpdate(const std::string& name, bool listening) = 0; + virtual void SendChannelUpdate(std::string name, bool listening) = 0; //-------------------------------------------------------------------------- /// @brief Synchronously invokes platform-specific APIs to apply the diff --git a/runtime/runtime_controller.cc b/runtime/runtime_controller.cc index c954687af71a7..9dc6a944de7cc 100644 --- a/runtime/runtime_controller.cc +++ b/runtime/runtime_controller.cc @@ -402,9 +402,8 @@ RuntimeController::ComputePlatformResolvedLocale( } // |PlatformConfigurationClient| -void RuntimeController::SendChannelUpdate(const std::string& name, - bool listening) { - client_.SendChannelUpdate(name, listening); +void RuntimeController::SendChannelUpdate(std::string name, bool listening) { + client_.SendChannelUpdate(std::move(name), listening); } Dart_Port RuntimeController::GetMainPort() { diff --git a/runtime/runtime_controller.h b/runtime/runtime_controller.h index 7054386aca2cc..7a65f2b074282 100644 --- a/runtime/runtime_controller.h +++ b/runtime/runtime_controller.h @@ -684,7 +684,7 @@ class RuntimeController : public PlatformConfigurationClient { const std::vector& supported_locale_data) override; // |PlatformConfigurationClient| - void SendChannelUpdate(const std::string& name, bool listening) override; + void SendChannelUpdate(std::string name, bool listening) override; // |PlatformConfigurationClient| double GetScaledFontSize(double unscaled_font_size, diff --git a/runtime/runtime_delegate.h b/runtime/runtime_delegate.h index dede8f3d52b89..a036d4723cb34 100644 --- a/runtime/runtime_delegate.h +++ b/runtime/runtime_delegate.h @@ -54,7 +54,7 @@ class RuntimeDelegate { virtual std::weak_ptr GetPlatformMessageHandler() const = 0; - virtual void SendChannelUpdate(const std::string& name, bool listening) = 0; + virtual void SendChannelUpdate(std::string name, bool listening) = 0; virtual double GetScaledFontSize(double unscaled_font_size, int configuration_id) const = 0; diff --git a/shell/common/engine.cc b/shell/common/engine.cc index 4d88e75b636af..920122c9a23dc 100644 --- a/shell/common/engine.cc +++ b/shell/common/engine.cc @@ -570,8 +570,8 @@ std::weak_ptr Engine::GetPlatformMessageHandler() return delegate_.GetPlatformMessageHandler(); } -void Engine::SendChannelUpdate(const std::string& name, bool listening) { - delegate_.OnEngineChannelUpdate(name, listening); +void Engine::SendChannelUpdate(std::string name, bool listening) { + delegate_.OnEngineChannelUpdate(std::move(name), listening); } void Engine::LoadDartDeferredLibrary( diff --git a/shell/common/engine.h b/shell/common/engine.h index 403065fc5315a..f7d888de425f2 100644 --- a/shell/common/engine.h +++ b/shell/common/engine.h @@ -303,8 +303,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { /// @param[in] listening Whether the listener has been set (true) or /// cleared (false). /// - virtual void OnEngineChannelUpdate(const std::string& name, - bool listening) = 0; + virtual void OnEngineChannelUpdate(std::string name, bool listening) = 0; //-------------------------------------------------------------------------- /// @brief Synchronously invokes platform-specific APIs to apply the @@ -990,7 +989,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate { const override; // |RuntimeDelegate| - void SendChannelUpdate(const std::string& name, bool listening) override; + void SendChannelUpdate(std::string name, bool listening) override; // |RuntimeDelegate| double GetScaledFontSize(double unscaled_font_size, diff --git a/shell/common/engine_unittests.cc b/shell/common/engine_unittests.cc index a5139fb5e0d77..9d8339321d999 100644 --- a/shell/common/engine_unittests.cc +++ b/shell/common/engine_unittests.cc @@ -39,7 +39,7 @@ class MockDelegate : public Engine::Delegate { MOCK_METHOD0(GetCurrentTimePoint, fml::TimePoint()); MOCK_CONST_METHOD0(GetPlatformMessageHandler, const std::shared_ptr&()); - MOCK_METHOD2(OnEngineChannelUpdate, void(const std::string&, bool)); + MOCK_METHOD2(OnEngineChannelUpdate, void(std::string, bool)); MOCK_CONST_METHOD2(GetScaledFontSize, double(double font_size, int configuration_id)); }; @@ -70,7 +70,7 @@ class MockRuntimeDelegate : public RuntimeDelegate { MOCK_METHOD1(RequestDartDeferredLibrary, void(intptr_t)); MOCK_CONST_METHOD0(GetPlatformMessageHandler, std::weak_ptr()); - MOCK_METHOD2(SendChannelUpdate, void(const std::string&, bool)); + MOCK_METHOD2(SendChannelUpdate, void(std::string, bool)); MOCK_CONST_METHOD2(GetScaledFontSize, double(double font_size, int configuration_id)); }; diff --git a/shell/common/platform_view.cc b/shell/common/platform_view.cc index 48439c252447a..defb62e5c396d 100644 --- a/shell/common/platform_view.cc +++ b/shell/common/platform_view.cc @@ -114,7 +114,7 @@ void PlatformView::UpdateSemantics( // NOLINTNEXTLINE(performance-unnecessary-value-param) CustomAccessibilityActionUpdates actions) {} -void PlatformView::SendChannelUpdate(const std::string& name, bool listening) {} +void PlatformView::SendChannelUpdate(std::string name, bool listening) {} void PlatformView::HandlePlatformMessage( std::unique_ptr message) { diff --git a/shell/common/platform_view.h b/shell/common/platform_view.h index 5e0a4882f1f0d..3d6ead79001cf 100644 --- a/shell/common/platform_view.h +++ b/shell/common/platform_view.h @@ -474,7 +474,7 @@ class PlatformView { /// @param[in] listening True if a listener has been set, false if it has /// been cleared. /// - virtual void SendChannelUpdate(const std::string& name, bool listening); + virtual void SendChannelUpdate(std::string name, bool listening); //---------------------------------------------------------------------------- /// @brief Used by embedders to specify the updated viewport metrics for diff --git a/shell/common/shell.cc b/shell/common/shell.cc index 7a7c73d44b0c2..a48c696da58a5 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1328,14 +1328,14 @@ void Shell::OnEngineHandlePlatformMessage( } } -void Shell::OnEngineChannelUpdate(const std::string& name, bool listening) { +void Shell::OnEngineChannelUpdate(std::string name, bool listening) { FML_DCHECK(is_set_up_); FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); task_runners_.GetPlatformTaskRunner()->PostTask( [view = platform_view_->GetWeakPtr(), name = name, listening] { if (view) { - view->SendChannelUpdate(name, listening); + view->SendChannelUpdate(std::move(name), listening); } }); } diff --git a/shell/common/shell.h b/shell/common/shell.h index c3023aff0ffa6..b8c9c31052df6 100644 --- a/shell/common/shell.h +++ b/shell/common/shell.h @@ -681,7 +681,7 @@ class Shell final : public PlatformView::Delegate, fml::TimePoint GetCurrentTimePoint() override; // |Engine::Delegate| - void OnEngineChannelUpdate(const std::string& name, bool listening) override; + void OnEngineChannelUpdate(std::string name, bool listening) override; // |Engine::Delegate| double GetScaledFontSize(double unscaled_font_size, diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 93822bf9337e5..5eaeedeec1d67 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1885,7 +1885,7 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, nullptr; if (SAFE_ACCESS(args, channel_update_callback, nullptr) != nullptr) { channel_update_callback = [ptr = args->channel_update_callback, user_data]( - const std::string& name, bool listening) { + std::string name, bool listening) { FlutterChannelUpdate update{name.c_str(), listening}; ptr(&update, user_data); }; diff --git a/shell/platform/embedder/platform_view_embedder.cc b/shell/platform/embedder/platform_view_embedder.cc index 01bfbe97cb8eb..c97011d663182 100644 --- a/shell/platform/embedder/platform_view_embedder.cc +++ b/shell/platform/embedder/platform_view_embedder.cc @@ -200,10 +200,9 @@ void PlatformViewEmbedder::OnPreEngineRestart() const { } // |PlatformView| -void PlatformViewEmbedder::SendChannelUpdate(const std::string& name, - bool listening) { +void PlatformViewEmbedder::SendChannelUpdate(std::string name, bool listening) { if (platform_dispatch_table_.on_channel_update != nullptr) { - platform_dispatch_table_.on_channel_update(name, listening); + platform_dispatch_table_.on_channel_update(std::move(name), listening); } } diff --git a/shell/platform/embedder/platform_view_embedder.h b/shell/platform/embedder/platform_view_embedder.h index a6452c6b7e18c..f585820493a3c 100644 --- a/shell/platform/embedder/platform_view_embedder.h +++ b/shell/platform/embedder/platform_view_embedder.h @@ -41,7 +41,7 @@ class PlatformViewEmbedder final : public PlatformView { std::function>( const std::vector& supported_locale_data)>; using OnPreEngineRestartCallback = std::function; - using ChanneUpdateCallback = std::function; + using ChanneUpdateCallback = std::function; struct PlatformDispatchTable { UpdateSemanticsCallback update_semantics_callback; // optional @@ -137,7 +137,7 @@ class PlatformViewEmbedder final : public PlatformView { const std::vector& supported_locale_data) override; // |PlatformView| - void SendChannelUpdate(const std::string& name, bool listening) override; + void SendChannelUpdate(std::string name, bool listening) override; FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewEmbedder); }; diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 7a10891e8262a..95db407aa3aaf 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -377,7 +377,7 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { void* user_data) { auto host = static_cast(user_data); std::string channel_name(update->channel); - host->OnChannelUpdate(channel_name, update->listening); + host->OnChannelUpdate(std::move(channel_name), update->listening); }; args.custom_task_runners = &custom_task_runners; @@ -815,8 +815,7 @@ std::optional FlutterWindowsEngine::ProcessExternalWindowMessage( return std::nullopt; } -void FlutterWindowsEngine::OnChannelUpdate(const std::string& name, - bool listening) { +void FlutterWindowsEngine::OnChannelUpdate(std::string name, bool listening) { if (name == "flutter/platform" && listening) { lifecycle_manager_->BeginProcessingExit(); } else if (name == "flutter/lifecycle" && listening) { diff --git a/shell/platform/windows/flutter_windows_engine.h b/shell/platform/windows/flutter_windows_engine.h index c924b5a7a1fef..cdbcd375fd119 100644 --- a/shell/platform/windows/flutter_windows_engine.h +++ b/shell/platform/windows/flutter_windows_engine.h @@ -299,7 +299,7 @@ class FlutterWindowsEngine { // Invoked by the engine when a listener is set or cleared on a platform // channel. - virtual void OnChannelUpdate(const std::string& name, bool listening); + virtual void OnChannelUpdate(std::string name, bool listening); private: // Allows swapping out embedder_api_ calls in tests. From 478b6ddd8f80107305f441072d3d3d23f9a76362 Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 28 Aug 2023 14:49:40 -0400 Subject: [PATCH 25/31] STREQ --- shell/platform/embedder/tests/embedder_unittests.cc | 2 +- shell/platform/windows/flutter_windows_engine_unittests.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index 2026279daa544..d8802bce19c86 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2572,7 +2572,7 @@ TEST_F(EmbedderTest, RegisterChannelListener) { "SignalNativeTest", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) { latch.Signal(); })); context.SetChannelUpdateCallback([&](const FlutterChannelUpdate* update) { - EXPECT_EQ(strcmp(update->channel, "test/listen"), 0); + EXPECT_STREQ(update->channel, "test/listen"); EXPECT_TRUE(update->listening); listening = true; latch2.Signal(); diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 79a9403f0672f..bb588038b28ba 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1135,7 +1135,7 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { new_args.channel_update_callback = [](const FlutterChannelUpdate* update, void* user_data) { listened_to = true; - EXPECT_EQ(strcmp(update->channel, "flutter/lifecycle"), 0); + EXPECT_STREQ(update->channel, "flutter/lifecycle"); EXPECT_TRUE(update->listening); }; return old_run(version, config, &new_args, user_data, engine_out); From 96e841c8685b57f37a0c078eefb36fd9420c893d Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 28 Aug 2023 15:07:41 -0400 Subject: [PATCH 26/31] Test lifecycle begins --- .../flutter_windows_engine_unittests.cc | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index bb588038b28ba..7b8cac6cbf40d 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1126,23 +1126,14 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto old_run = modifier.embedder_api().Run; - static bool listened_to = false; - modifier.embedder_api().Run = MOCK_ENGINE_PROC( - Run, ([old_run](size_t version, const FlutterRendererConfig* config, - const FlutterProjectArgs* args, void* user_data, - FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { - FlutterProjectArgs new_args = *args; - new_args.channel_update_callback = - [](const FlutterChannelUpdate* update, void* user_data) { - listened_to = true; - EXPECT_STREQ(update->channel, "flutter/lifecycle"); - EXPECT_TRUE(update->listening); - }; - return old_run(version, config, &new_args, user_data, engine_out); - })); + bool lifecycle_began = false; + auto handler = std::make_unique(engine); + handler->begin_processing_callback = [&]() { lifecycle_began = true; }; + modifier.SetLifecycleManager(std::move(handler)); + engine->Run(); - while (!listened_to) { + while (!lifecycle_began) { engine->task_runner()->ProcessTasks(); } } From d01b3998980d885d2c04c2e8705764f6ab052bf4 Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 28 Aug 2023 16:02:59 -0400 Subject: [PATCH 27/31] Test lifecycle begins --- shell/platform/windows/flutter_windows_engine_unittests.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 7b8cac6cbf40d..f812460989789 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -1124,7 +1124,6 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { FlutterWindowsEngine* engine = view.GetEngine(); EngineModifier modifier(engine); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; - auto old_run = modifier.embedder_api().Run; bool lifecycle_began = false; auto handler = std::make_unique(engine); From 48a17ff9a7b3de5e019a27d0897ab52dae0afd84 Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:40:24 -0400 Subject: [PATCH 28/31] Update shell/platform/embedder/tests/embedder_unittests.cc Co-authored-by: Chris Bracken --- shell/platform/embedder/tests/embedder_unittests.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index d8802bce19c86..a2f403d8f0ac6 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -2586,6 +2586,7 @@ TEST_F(EmbedderTest, RegisterChannelListener) { ASSERT_TRUE(engine.is_valid()); latch.Wait(); + // Drain tasks posted to platform thread task runner. fml::MessageLoop::GetCurrent().RunExpiredTasksNow(); latch2.Wait(); From 6b9b5729693d70e3223da6684f36271f580f09aa Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:40:32 -0400 Subject: [PATCH 29/31] Update shell/common/platform_view.h Co-authored-by: Chris Bracken --- shell/common/platform_view.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/common/platform_view.h b/shell/common/platform_view.h index 3d6ead79001cf..7abd0aa32e04a 100644 --- a/shell/common/platform_view.h +++ b/shell/common/platform_view.h @@ -467,7 +467,7 @@ class PlatformView { //---------------------------------------------------------------------------- /// @brief Used by the framework to tell the embedder that it has - /// registered a listener on a given channel + /// registered a listener on a given channel. /// /// @param[in] name The name of the channel on which the listener has /// set or cleared a listener. From bdbb8295fd231b2a38a4f712681ec1a89067e762 Mon Sep 17 00:00:00 2001 From: schectman Date: Mon, 28 Aug 2023 17:04:01 -0400 Subject: [PATCH 30/31] Add struct size --- shell/platform/embedder/embedder.cc | 3 ++- shell/platform/embedder/embedder.h | 5 ++++- shell/platform/windows/flutter_windows_engine.cc | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 5eaeedeec1d67..31dc0a52466e5 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1886,7 +1886,8 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, if (SAFE_ACCESS(args, channel_update_callback, nullptr) != nullptr) { channel_update_callback = [ptr = args->channel_update_callback, user_data]( std::string name, bool listening) { - FlutterChannelUpdate update{name.c_str(), listening}; + FlutterChannelUpdate update{sizeof(FlutterChannelUpdate), name.c_str(), + listening}; ptr(&update, user_data); }; } diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index c9e58b603fefb..65c959956c17d 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -1404,6 +1404,8 @@ typedef void (*FlutterUpdateSemanticsCallback2)( /// An update to whether a message channel has a listener set or not. typedef struct { + // The size of the struct. Must be sizeof(FlutterChannelUpdate). + size_t struct_size; /// The name of the channel. const char* channel; /// True if a listener has been set, false if one has been cleared. @@ -2208,7 +2210,8 @@ typedef struct { FlutterUpdateSemanticsCallback2 update_semantics_callback2; /// The callback invoked by the engine in response to a channel listener - /// being registered on the framework side. + /// being registered on the framework side. The callback is invoked from + /// a task posted to the UI task runner. FlutterChannelUpdateCallback channel_update_callback; } FlutterProjectArgs; diff --git a/shell/platform/windows/flutter_windows_engine.cc b/shell/platform/windows/flutter_windows_engine.cc index 95db407aa3aaf..fce1c0531e48c 100644 --- a/shell/platform/windows/flutter_windows_engine.cc +++ b/shell/platform/windows/flutter_windows_engine.cc @@ -15,6 +15,7 @@ #include "flutter/shell/platform/common/client_wrapper/binary_messenger_impl.h" #include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h" #include "flutter/shell/platform/common/path_utils.h" +#include "flutter/shell/platform/embedder/embedder_struct_macros.h" #include "flutter/shell/platform/windows/accessibility_bridge_windows.h" #include "flutter/shell/platform/windows/flutter_windows_view.h" #include "flutter/shell/platform/windows/keyboard_key_channel_handler.h" @@ -376,8 +377,11 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) { args.channel_update_callback = [](const FlutterChannelUpdate* update, void* user_data) { auto host = static_cast(user_data); - std::string channel_name(update->channel); - host->OnChannelUpdate(std::move(channel_name), update->listening); + if (SAFE_ACCESS(update, channel, nullptr) != nullptr) { + std::string channel_name(update->channel); + host->OnChannelUpdate(std::move(channel_name), + SAFE_ACCESS(update, listening, false)); + } }; args.custom_task_runners = &custom_task_runners; From f4d292b830bbb6689b7d545e1824b829762d02c1 Mon Sep 17 00:00:00 2001 From: schectman Date: Tue, 29 Aug 2023 15:20:51 -0400 Subject: [PATCH 31/31] Reintroduce const ref --- lib/ui/window/platform_configuration.cc | 2 +- shell/common/platform_view.cc | 2 +- shell/common/platform_view.h | 2 +- shell/common/shell.cc | 4 ++-- shell/platform/embedder/embedder.cc | 2 +- shell/platform/embedder/platform_view_embedder.cc | 5 +++-- shell/platform/embedder/platform_view_embedder.h | 4 ++-- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/ui/window/platform_configuration.cc b/lib/ui/window/platform_configuration.cc index 0aff121b815a6..d19c80a7a8028 100644 --- a/lib/ui/window/platform_configuration.cc +++ b/lib/ui/window/platform_configuration.cc @@ -634,7 +634,7 @@ void PlatformConfigurationNativeApi::RegisterBackgroundIsolate( void PlatformConfigurationNativeApi::SendChannelUpdate(const std::string& name, bool listening) { UIDartState::Current()->platform_configuration()->client()->SendChannelUpdate( - std::move(name), listening); + name, listening); } double PlatformConfigurationNativeApi::GetScaledFontSize( diff --git a/shell/common/platform_view.cc b/shell/common/platform_view.cc index defb62e5c396d..48439c252447a 100644 --- a/shell/common/platform_view.cc +++ b/shell/common/platform_view.cc @@ -114,7 +114,7 @@ void PlatformView::UpdateSemantics( // NOLINTNEXTLINE(performance-unnecessary-value-param) CustomAccessibilityActionUpdates actions) {} -void PlatformView::SendChannelUpdate(std::string name, bool listening) {} +void PlatformView::SendChannelUpdate(const std::string& name, bool listening) {} void PlatformView::HandlePlatformMessage( std::unique_ptr message) { diff --git a/shell/common/platform_view.h b/shell/common/platform_view.h index 7abd0aa32e04a..09ac7ed7c64f7 100644 --- a/shell/common/platform_view.h +++ b/shell/common/platform_view.h @@ -474,7 +474,7 @@ class PlatformView { /// @param[in] listening True if a listener has been set, false if it has /// been cleared. /// - virtual void SendChannelUpdate(std::string name, bool listening); + virtual void SendChannelUpdate(const std::string& name, bool listening); //---------------------------------------------------------------------------- /// @brief Used by embedders to specify the updated viewport metrics for diff --git a/shell/common/shell.cc b/shell/common/shell.cc index a48c696da58a5..4f71b68594f4c 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -1333,9 +1333,9 @@ void Shell::OnEngineChannelUpdate(std::string name, bool listening) { FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); task_runners_.GetPlatformTaskRunner()->PostTask( - [view = platform_view_->GetWeakPtr(), name = name, listening] { + [view = platform_view_->GetWeakPtr(), name = std::move(name), listening] { if (view) { - view->SendChannelUpdate(std::move(name), listening); + view->SendChannelUpdate(name, listening); } }); } diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 31dc0a52466e5..b54452c20999d 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -1885,7 +1885,7 @@ FlutterEngineResult FlutterEngineInitialize(size_t version, nullptr; if (SAFE_ACCESS(args, channel_update_callback, nullptr) != nullptr) { channel_update_callback = [ptr = args->channel_update_callback, user_data]( - std::string name, bool listening) { + const std::string& name, bool listening) { FlutterChannelUpdate update{sizeof(FlutterChannelUpdate), name.c_str(), listening}; ptr(&update, user_data); diff --git a/shell/platform/embedder/platform_view_embedder.cc b/shell/platform/embedder/platform_view_embedder.cc index c97011d663182..01bfbe97cb8eb 100644 --- a/shell/platform/embedder/platform_view_embedder.cc +++ b/shell/platform/embedder/platform_view_embedder.cc @@ -200,9 +200,10 @@ void PlatformViewEmbedder::OnPreEngineRestart() const { } // |PlatformView| -void PlatformViewEmbedder::SendChannelUpdate(std::string name, bool listening) { +void PlatformViewEmbedder::SendChannelUpdate(const std::string& name, + bool listening) { if (platform_dispatch_table_.on_channel_update != nullptr) { - platform_dispatch_table_.on_channel_update(std::move(name), listening); + platform_dispatch_table_.on_channel_update(name, listening); } } diff --git a/shell/platform/embedder/platform_view_embedder.h b/shell/platform/embedder/platform_view_embedder.h index f585820493a3c..a6452c6b7e18c 100644 --- a/shell/platform/embedder/platform_view_embedder.h +++ b/shell/platform/embedder/platform_view_embedder.h @@ -41,7 +41,7 @@ class PlatformViewEmbedder final : public PlatformView { std::function>( const std::vector& supported_locale_data)>; using OnPreEngineRestartCallback = std::function; - using ChanneUpdateCallback = std::function; + using ChanneUpdateCallback = std::function; struct PlatformDispatchTable { UpdateSemanticsCallback update_semantics_callback; // optional @@ -137,7 +137,7 @@ class PlatformViewEmbedder final : public PlatformView { const std::vector& supported_locale_data) override; // |PlatformView| - void SendChannelUpdate(std::string name, bool listening) override; + void SendChannelUpdate(const std::string& name, bool listening) override; FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewEmbedder); };