From 52ce8f488dc739547da7a7e63d2ae6344a395c23 Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Thu, 14 Sep 2023 17:07:14 -0400 Subject: [PATCH] Handle external window's `WM_CLOSE` in lifecycle manager (flutter/engine#45840) Handle the close messages from non-Flutter windows similarly to how we handle our own, for graceful app exit purposes. https://github.com/flutter/flutter/issues/131497 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../flutter_windows_engine_unittests.cc | 22 ++++++++ .../windows/windows_lifecycle_manager.cc | 52 +++++++++++-------- .../windows/windows_lifecycle_manager.h | 7 +++ 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc index d27c78c705..7b29748453 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -909,6 +909,28 @@ TEST_F(FlutterWindowsEngineTest, EnableApplicationLifecycle) { 0); } +TEST_F(FlutterWindowsEngineTest, ApplicationLifecycleExternalWindow) { + FlutterWindowsEngineBuilder builder{GetContext()}; + + 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; }; + auto handler = std::make_unique(engine); + ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { + return false; + }); + EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1); + modifier.SetLifecycleManager(std::move(handler)); + engine->lifecycle_manager()->BeginProcessingExit(); + + engine->lifecycle_manager()->ExternalWindowMessage(0, WM_CLOSE, 0, 0); +} + TEST_F(FlutterWindowsEngineTest, AppStartsInResumedState) { FlutterWindowsEngineBuilder builder{GetContext()}; diff --git a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc index a41b1617ba..90d52f300b 100644 --- a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc +++ b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc @@ -38,6 +38,30 @@ void WindowsLifecycleManager::DispatchMessage(HWND hwnd, PostMessage(hwnd, message, wparam, lparam); } +bool WindowsLifecycleManager::HandleCloseMessage(HWND hwnd, + WPARAM wparam, + LPARAM lparam) { + if (!process_exit_) { + return false; + } + auto key = std::make_tuple(hwnd, wparam, lparam); + auto itr = sent_close_messages_.find(key); + if (itr != sent_close_messages_.end()) { + if (itr->second == 1) { + sent_close_messages_.erase(itr); + } else { + sent_close_messages_[key]--; + } + return false; + } + if (IsLastWindowOfProcess()) { + engine_->RequestApplicationQuit(hwnd, wparam, lparam, + AppExitType::cancelable); + return true; + } + return false; +} + bool WindowsLifecycleManager::WindowProc(HWND hwnd, UINT msg, WPARAM wpar, @@ -48,27 +72,8 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd, // send a request to the framework to see if the app should exit. If it // 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_exit_) { - return false; - } - auto key = std::make_tuple(hwnd, wpar, lpar); - auto itr = sent_close_messages_.find(key); - if (itr != sent_close_messages_.end()) { - if (itr->second == 1) { - sent_close_messages_.erase(itr); - } else { - sent_close_messages_[key]--; - } - return false; - } - if (IsLastWindowOfProcess()) { - engine_->RequestApplicationQuit(hwnd, wpar, lpar, - AppExitType::cancelable); - return true; - } - break; - } + case WM_CLOSE: + return HandleCloseMessage(hwnd, wpar, lpar); // DWM composition can be disabled on Windows 7. // Notify the engine as this can result in screen tearing. @@ -285,6 +290,11 @@ std::optional WindowsLifecycleManager::ExternalWindowMessage( case WM_DESTROY: event = flutter::WindowStateEvent::kHide; break; + case WM_CLOSE: + if (HandleCloseMessage(hwnd, wparam, lparam)) { + return NULL; + } + break; } if (event.has_value()) { diff --git a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h index c56bfc306b..fc20b38246 100644 --- a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h +++ b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h @@ -99,6 +99,13 @@ class WindowsLifecycleManager { LPARAM lparam); private: + // Pass top-level window close notifications to the application lifecycle + // logic. If the last window of the process receives WM_CLOSE and a listener + // is registered for WidgetsBindingObserver.didRequestAppExit, the message is + // sent to the framework to query whether the application should be allowed + // to quit. + bool HandleCloseMessage(HWND hwnd, WPARAM wparam, LPARAM lparam); + FlutterWindowsEngine* engine_; std::map, int> sent_close_messages_;