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 void WindowsLifecycleManager::SetLifecycleState(AppLifecycleState state) {
196  if (state_ == state) {
197  return;
198  }
199  state_ = state;
200  if (engine_ && process_lifecycle_) {
201  const char* state_name = AppLifecycleStateToString(state);
202  engine_->SendPlatformMessage("flutter/lifecycle",
203  reinterpret_cast<const uint8_t*>(state_name),
204  strlen(state_name), nullptr, nullptr);
205  }
206 }
207 
208 void WindowsLifecycleManager::UpdateState() {
209  AppLifecycleState new_state = AppLifecycleState::kResumed;
210  if (visible_windows_.empty()) {
211  new_state = AppLifecycleState::kHidden;
212  } else if (focused_windows_.empty()) {
213  new_state = AppLifecycleState::kInactive;
214  }
215  SetLifecycleState(new_state);
216 }
217 
218 void WindowsLifecycleManager::OnWindowStateEvent(HWND hwnd,
219  WindowStateEvent event) {
220  // Instead of updating the state immediately, remember all
221  // changes to individual window and then update the state in next run loop
222  // turn. Otherwise the application would be temporarily deactivated when
223  // switching focus between windows for example.
224  if (!update_state_scheduled_) {
225  update_state_scheduled_ = true;
226  // Task runner will be destroyed together with engine so it is safe
227  // to keep reference to it.
228  engine_->task_runner()->PostTask([this]() {
229  update_state_scheduled_ = false;
230  UpdateState();
231  });
232  }
233 
234  switch (event) {
235  case WindowStateEvent::kShow: {
236  visible_windows_.insert(hwnd);
237  break;
238  }
239  case WindowStateEvent::kHide: {
240  visible_windows_.erase(hwnd);
241  focused_windows_.erase(hwnd);
242  break;
243  }
244  case WindowStateEvent::kFocus: {
245  focused_windows_.insert(hwnd);
246  break;
247  }
248  case WindowStateEvent::kUnfocus: {
249  focused_windows_.erase(hwnd);
250  break;
251  }
252  }
253 }
254 
255 std::optional<LRESULT> WindowsLifecycleManager::ExternalWindowMessage(
256  HWND hwnd,
257  UINT message,
258  WPARAM wparam,
259  LPARAM lparam) {
260  std::optional<flutter::WindowStateEvent> event = std::nullopt;
261 
262  // TODO (schectman): Handle WM_CLOSE messages.
263  // https://github.com/flutter/flutter/issues/131497
264  switch (message) {
265  case WM_SHOWWINDOW:
266  event = wparam ? flutter::WindowStateEvent::kShow
268  break;
269  case WM_SIZE:
270  switch (wparam) {
271  case SIZE_MINIMIZED:
273  break;
274  case SIZE_RESTORED:
275  case SIZE_MAXIMIZED:
277  break;
278  }
279  break;
280  case WM_SETFOCUS:
282  break;
283  case WM_KILLFOCUS:
285  break;
286  case WM_DESTROY:
288  break;
289  case WM_CLOSE:
290  if (HandleCloseMessage(hwnd, wparam, lparam)) {
291  return NULL;
292  }
293  break;
294  }
295 
296  if (event.has_value()) {
297  OnWindowStateEvent(hwnd, *event);
298  }
299 
300  return std::nullopt;
301 }
302 
303 } // namespace flutter
void RequestApplicationQuit(HWND hwnd, WPARAM wparam, LPARAM lparam, AppExitType exit_type)
bool GetNextThread(THREADENTRY32 &thread)
std::optional< THREADENTRY32 > GetFirstThread()
virtual void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
WindowsLifecycleManager(FlutterWindowsEngine *engine)
bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT *result)
virtual void Quit(std::optional< HWND > window, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
virtual void DispatchMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
Win32Message message
constexpr const char * AppLifecycleStateToString(AppLifecycleState state)
static int64_t NumWindowsForThread(const THREADENTRY32 &thread)
WindowStateEvent
An event representing a change in window state that may update the.