Flutter Windows Embedder
windows_lifecycle_manager.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <TlHelp32.h>
8 #include <WinUser.h>
9 #include <Windows.h>
10 #include <tchar.h>
11 
13 
14 namespace flutter {
15 
17  : engine_(engine) {}
18 
20 
21 void WindowsLifecycleManager::Quit(std::optional<HWND> hwnd,
22  std::optional<WPARAM> wparam,
23  std::optional<LPARAM> lparam,
24  UINT exit_code) {
25  if (!hwnd.has_value()) {
26  ::PostQuitMessage(exit_code);
27  } else {
28  BASE_CHECK(wparam.has_value() && lparam.has_value());
29  sent_close_messages_[std::make_tuple(*hwnd, *wparam, *lparam)]++;
30  DispatchMessage(*hwnd, WM_CLOSE, *wparam, *lparam);
31  }
32 }
33 
35  UINT message,
36  WPARAM wparam,
37  LPARAM lparam) {
38  PostMessage(hwnd, message, wparam, lparam);
39 }
40 
41 bool WindowsLifecycleManager::HandleCloseMessage(HWND hwnd,
42  WPARAM wparam,
43  LPARAM lparam) {
44  if (!process_exit_) {
45  return false;
46  }
47  auto key = std::make_tuple(hwnd, wparam, lparam);
48  auto itr = sent_close_messages_.find(key);
49  if (itr != sent_close_messages_.end()) {
50  if (itr->second == 1) {
51  sent_close_messages_.erase(itr);
52  } else {
53  sent_close_messages_[key]--;
54  }
55  return false;
56  }
57  if (IsLastWindowOfProcess()) {
58  engine_->RequestApplicationQuit(hwnd, wparam, lparam,
60  return true;
61  }
62  return false;
63 }
64 
66  UINT msg,
67  WPARAM wpar,
68  LPARAM lpar,
69  LRESULT* result) {
70  switch (msg) {
71  // When WM_CLOSE is received from the final window of an application, we
72  // send a request to the framework to see if the app should exit. If it
73  // is, we re-dispatch a new WM_CLOSE message. In order to allow the new
74  // message to reach other delegates, we ignore it here.
75  case WM_CLOSE:
76  return HandleCloseMessage(hwnd, wpar, lpar);
77 
78  // DWM composition can be disabled on Windows 7.
79  // Notify the engine as this can result in screen tearing.
80  case WM_DWMCOMPOSITIONCHANGED:
81  engine_->OnDwmCompositionChanged();
82  break;
83 
84  case WM_SIZE:
85  if (wpar == SIZE_MAXIMIZED || wpar == SIZE_RESTORED) {
87  } else if (wpar == SIZE_MINIMIZED) {
89  }
90  break;
91 
92  case WM_SHOWWINDOW:
93  if (!wpar) {
95  } else {
97  }
98  break;
99 
100  case WM_DESTROY:
102  break;
103  }
104  return false;
105 }
106 
108  public:
110  thread_snapshot_ = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
111  }
113  if (thread_snapshot_ != INVALID_HANDLE_VALUE) {
114  ::CloseHandle(thread_snapshot_);
115  }
116  }
117 
118  std::optional<THREADENTRY32> GetFirstThread() {
119  if (thread_snapshot_ == INVALID_HANDLE_VALUE) {
120  FML_LOG(ERROR) << "Failed to get thread snapshot";
121  return std::nullopt;
122  }
123  THREADENTRY32 thread;
124  thread.dwSize = sizeof(thread);
125  if (!::Thread32First(thread_snapshot_, &thread)) {
126  DWORD error_num = ::GetLastError();
127  char msg[256];
128  ::FormatMessageA(
129  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
130  error_num, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, 256,
131  nullptr);
132  FML_LOG(ERROR) << "Failed to get thread(" << error_num << "): " << msg;
133  return std::nullopt;
134  }
135  return thread;
136  }
137 
138  bool GetNextThread(THREADENTRY32& thread) {
139  if (thread_snapshot_ == INVALID_HANDLE_VALUE) {
140  return false;
141  }
142  return ::Thread32Next(thread_snapshot_, &thread);
143  }
144 
145  private:
146  HANDLE thread_snapshot_;
147 };
148 
149 static int64_t NumWindowsForThread(const THREADENTRY32& thread) {
150  int64_t num_windows = 0;
151  ::EnumThreadWindows(
152  thread.th32ThreadID,
153  [](HWND hwnd, LPARAM lparam) {
154  int64_t* windows_ptr = reinterpret_cast<int64_t*>(lparam);
155  if (::GetParent(hwnd) == nullptr) {
156  (*windows_ptr)++;
157  }
158  return *windows_ptr <= 1 ? TRUE : FALSE;
159  },
160  reinterpret_cast<LPARAM>(&num_windows));
161  return num_windows;
162 }
163 
164 bool WindowsLifecycleManager::IsLastWindowOfProcess() {
165  DWORD pid = ::GetCurrentProcessId();
166  ThreadSnapshot thread_snapshot;
167  std::optional<THREADENTRY32> first_thread = thread_snapshot.GetFirstThread();
168  if (!first_thread.has_value()) {
169  FML_LOG(ERROR) << "No first thread found";
170  return true;
171  }
172 
173  int num_windows = 0;
174  THREADENTRY32 thread = *first_thread;
175  do {
176  if (thread.th32OwnerProcessID == pid) {
177  num_windows += NumWindowsForThread(thread);
178  if (num_windows > 1) {
179  return false;
180  }
181  }
182  } while (thread_snapshot.GetNextThread(thread));
183 
184  return num_windows <= 1;
185 }
186 
187 void WindowsLifecycleManager::BeginProcessingLifecycle() {
188  process_lifecycle_ = true;
189 }
190 
191 void WindowsLifecycleManager::BeginProcessingExit() {
192  process_exit_ = true;
193 }
194 
195 // TODO(schectman): Wait until the platform channel is registered to send
196 // the platform message.
197 // https://github.com/flutter/flutter/issues/131616
198 void WindowsLifecycleManager::SetLifecycleState(AppLifecycleState state) {
199  if (state_ == state) {
200  return;
201  }
202  state_ = state;
203  if (engine_ && process_lifecycle_) {
204  const char* state_name = AppLifecycleStateToString(state);
205  engine_->SendPlatformMessage("flutter/lifecycle",
206  reinterpret_cast<const uint8_t*>(state_name),
207  strlen(state_name), nullptr, nullptr);
208  }
209 }
210 
211 void WindowsLifecycleManager::OnWindowStateEvent(HWND hwnd,
212  WindowStateEvent event) {
213  // Synthesize an unfocus event when a focused window is hidden.
214  if (event == WindowStateEvent::kHide &&
215  focused_windows_.find(hwnd) != focused_windows_.end()) {
216  OnWindowStateEvent(hwnd, WindowStateEvent::kUnfocus);
217  }
218 
219  std::lock_guard guard(state_update_lock_);
220  switch (event) {
221  case WindowStateEvent::kShow: {
222  bool first_shown_window = visible_windows_.empty();
223  auto pair = visible_windows_.insert(hwnd);
224  if (first_shown_window && pair.second &&
225  state_ == AppLifecycleState::kHidden) {
226  SetLifecycleState(AppLifecycleState::kInactive);
227  }
228  break;
229  }
230  case WindowStateEvent::kHide: {
231  bool present = visible_windows_.erase(hwnd);
232  bool empty = visible_windows_.empty();
233  if (present && empty &&
234  (state_ == AppLifecycleState::kResumed ||
235  state_ == AppLifecycleState::kInactive)) {
236  SetLifecycleState(AppLifecycleState::kHidden);
237  }
238  break;
239  }
240  case WindowStateEvent::kFocus: {
241  bool first_focused_window = focused_windows_.empty();
242  auto pair = focused_windows_.insert(hwnd);
243  if (first_focused_window && pair.second &&
244  state_ == AppLifecycleState::kInactive) {
245  SetLifecycleState(AppLifecycleState::kResumed);
246  }
247  break;
248  }
249  case WindowStateEvent::kUnfocus: {
250  if (focused_windows_.erase(hwnd) && focused_windows_.empty() &&
251  state_ == AppLifecycleState::kResumed) {
252  SetLifecycleState(AppLifecycleState::kInactive);
253  }
254  break;
255  }
256  }
257 }
258 
259 std::optional<LRESULT> WindowsLifecycleManager::ExternalWindowMessage(
260  HWND hwnd,
261  UINT message,
262  WPARAM wparam,
263  LPARAM lparam) {
264  std::optional<flutter::WindowStateEvent> event = std::nullopt;
265 
266  // TODO (schectman): Handle WM_CLOSE messages.
267  // https://github.com/flutter/flutter/issues/131497
268  switch (message) {
269  case WM_SHOWWINDOW:
270  event = wparam ? flutter::WindowStateEvent::kShow
272  break;
273  case WM_SIZE:
274  switch (wparam) {
275  case SIZE_MINIMIZED:
277  break;
278  case SIZE_RESTORED:
279  case SIZE_MAXIMIZED:
281  break;
282  }
283  break;
284  case WM_SETFOCUS:
286  break;
287  case WM_KILLFOCUS:
289  break;
290  case WM_DESTROY:
292  break;
293  case WM_CLOSE:
294  if (HandleCloseMessage(hwnd, wparam, lparam)) {
295  return NULL;
296  }
297  break;
298  }
299 
300  if (event.has_value()) {
301  OnWindowStateEvent(hwnd, *event);
302  }
303 
304  return std::nullopt;
305 }
306 
307 } // namespace flutter
flutter::FlutterWindowsEngine::RequestApplicationQuit
void RequestApplicationQuit(HWND hwnd, WPARAM wparam, LPARAM lparam, AppExitType exit_type)
Definition: flutter_windows_engine.cc:791
flutter::WindowStateEvent
WindowStateEvent
An event representing a change in window state that may update the.
Definition: windows_lifecycle_manager.h:24
flutter::ThreadSnapshot::~ThreadSnapshot
~ThreadSnapshot()
Definition: windows_lifecycle_manager.cc:112
flutter::AppExitType::cancelable
@ cancelable
flutter::WindowStateEvent::kHide
@ kHide
flutter::ThreadSnapshot
Definition: windows_lifecycle_manager.cc:107
windows_lifecycle_manager.h
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:78
flutter::NumWindowsForThread
static int64_t NumWindowsForThread(const THREADENTRY32 &thread)
Definition: windows_lifecycle_manager.cc:149
flutter::WindowsLifecycleManager::~WindowsLifecycleManager
virtual ~WindowsLifecycleManager()
Definition: windows_lifecycle_manager.cc:19
flutter::ThreadSnapshot::GetNextThread
bool GetNextThread(THREADENTRY32 &thread)
Definition: windows_lifecycle_manager.cc:138
flutter::WindowsLifecycleManager::DispatchMessage
virtual void DispatchMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
Definition: windows_lifecycle_manager.cc:34
flutter::FlutterWindowsEngine::OnDwmCompositionChanged
void OnDwmCompositionChanged()
Definition: flutter_windows_engine.cc:805
flutter::WindowStateEvent::kFocus
@ kFocus
flutter::WindowStateEvent::kShow
@ kShow
flutter::WindowsLifecycleManager::WindowsLifecycleManager
WindowsLifecycleManager(FlutterWindowsEngine *engine)
Definition: windows_lifecycle_manager.cc:16
flutter::ThreadSnapshot::GetFirstThread
std::optional< THREADENTRY32 > GetFirstThread()
Definition: windows_lifecycle_manager.cc:118
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::WindowsLifecycleManager::OnWindowStateEvent
virtual void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
Definition: windows_lifecycle_manager.cc:211
flutter::ThreadSnapshot::ThreadSnapshot
ThreadSnapshot()
Definition: windows_lifecycle_manager.cc:109
flutter::WindowsLifecycleManager::IsLastWindowOfProcess
virtual bool IsLastWindowOfProcess()
Definition: windows_lifecycle_manager.cc:164
flutter_windows_engine.h
flutter::WindowsLifecycleManager::Quit
virtual void Quit(std::optional< HWND > window, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
Definition: windows_lifecycle_manager.cc:21
flutter::WindowStateEvent::kUnfocus
@ kUnfocus
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::AppLifecycleState
AppLifecycleState
Definition: app_lifecycle_state.h:32
flutter::AppLifecycleStateToString
constexpr const char * AppLifecycleStateToString(AppLifecycleState state)
Definition: app_lifecycle_state.h:72
key
int key
Definition: keyboard_key_handler_unittests.cc:114
flutter::WindowsLifecycleManager::WindowProc
bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT *result)
Definition: windows_lifecycle_manager.cc:65