Flutter Windows Embedder
flutter::PlatformHandler Class Reference

#include <platform_handler.h>

Public Member Functions

 PlatformHandler (BinaryMessenger *messenger, FlutterWindowsEngine *engine, std::optional< std::function< std::unique_ptr< ScopedClipboardInterface >()>> scoped_clipboard_provider=std::nullopt)
 
virtual ~PlatformHandler ()
 
virtual void RequestAppExit (std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, AppExitType exit_type, UINT exit_code)
 

Static Public Attributes

static constexpr char kExitTypeCancelable [] = "cancelable"
 
static constexpr char kExitTypeRequired [] = "required"
 

Protected Member Functions

virtual void GetPlainText (std::unique_ptr< MethodResult< rapidjson::Document >> result, std::string_view key)
 
virtual void GetHasStrings (std::unique_ptr< MethodResult< rapidjson::Document >> result)
 
virtual void SetPlainText (std::string_view text, std::unique_ptr< MethodResult< rapidjson::Document >> result)
 
virtual void SystemSoundPlay (const std::string &sound_type, std::unique_ptr< MethodResult< rapidjson::Document >> result)
 
virtual void SystemExitApplication (AppExitType exit_type, UINT exit_code, std::unique_ptr< MethodResult< rapidjson::Document >> result)
 
virtual void QuitApplication (std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
 
virtual void RequestAppExitSuccess (std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, const rapidjson::Document *result, UINT exit_code)
 

Static Protected Attributes

static constexpr char kClipboardError [] = "Clipboard error"
 
static constexpr char kSoundTypeAlert [] = "SystemSoundType.alert"
 
static constexpr char kSoundTypeClick [] = "SystemSoundType.click"
 
static constexpr char kSoundTypeTick [] = "SystemSoundType.tick"
 

Detailed Description

Definition at line 33 of file platform_handler.h.

Constructor & Destructor Documentation

◆ PlatformHandler()

flutter::PlatformHandler::PlatformHandler ( BinaryMessenger *  messenger,
FlutterWindowsEngine *  engine,
std::optional< std::function< std::unique_ptr< ScopedClipboardInterface >()>>  scoped_clipboard_provider = std::nullopt 
)
explicit

Definition at line 224 of file platform_handler.cc.

229  : channel_(std::make_unique<MethodChannel<rapidjson::Document>>(
230  messenger,
231  kChannelName,
233  engine_(engine) {
234  channel_->SetMethodCallHandler(
235  [this](const MethodCall<rapidjson::Document>& call,
236  std::unique_ptr<MethodResult<rapidjson::Document>> result) {
237  HandleMethodCall(call, std::move(result));
238  });
239  if (scoped_clipboard_provider.has_value()) {
240  scoped_clipboard_provider_ = scoped_clipboard_provider.value();
241  } else {
242  scoped_clipboard_provider_ = []() {
243  return std::make_unique<ScopedClipboard>();
244  };
245  }
246 
247  WNDCLASS window_class = RegisterWindowClass();
248  window_handle_ =
249  CreateWindowEx(0, window_class.lpszClassName, L"", 0, 0, 0, 0, 0,
250  HWND_MESSAGE, nullptr, window_class.hInstance, nullptr);
251 
252  if (window_handle_) {
253  SetWindowLongPtr(window_handle_, GWLP_USERDATA,
254  reinterpret_cast<LONG_PTR>(this));
255  } else {
256  auto error = GetLastError();
257  LPWSTR message = nullptr;
258  size_t size = FormatMessageW(
259  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
260  FORMAT_MESSAGE_IGNORE_INSERTS,
261  NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
262  reinterpret_cast<LPWSTR>(&message), 0, NULL);
263  OutputDebugString(message);
264  LocalFree(message);
265  }
266 }
static const JsonMethodCodec & GetInstance()
Win32Message message
static constexpr char kChannelName[]

◆ ~PlatformHandler()

flutter::PlatformHandler::~PlatformHandler ( )
virtual

Definition at line 268 of file platform_handler.cc.

268  {
269  if (window_handle_) {
270  DestroyWindow(window_handle_);
271  window_handle_ = nullptr;
272  }
273  UnregisterClass(kWindowClassName.c_str(), nullptr);
274 }

Member Function Documentation

◆ GetHasStrings()

void flutter::PlatformHandler::GetHasStrings ( std::unique_ptr< MethodResult< rapidjson::Document >>  result)
protectedvirtual

Definition at line 313 of file platform_handler.cc.

314  {
315  std::unique_ptr<ScopedClipboardInterface> clipboard =
316  scoped_clipboard_provider_();
317 
318  bool hasStrings;
319  int open_result = clipboard->Open(window_handle_);
320  if (open_result != kErrorSuccess) {
321  // Swallow errors of type ERROR_ACCESS_DENIED. These happen when the app is
322  // not in the foreground and GetHasStrings is irrelevant.
323  // See https://github.com/flutter/flutter/issues/95817.
324  if (open_result != kAccessDeniedErrorCode) {
325  rapidjson::Document error_code;
326  error_code.SetInt(open_result);
327  result->Error(kClipboardError, "Unable to open clipboard", error_code);
328  return;
329  }
330  hasStrings = false;
331  } else {
332  hasStrings = clipboard->HasString();
333  }
334 
335  rapidjson::Document document;
336  document.SetObject();
337  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
338  document.AddMember(rapidjson::Value(kValueKey, allocator),
339  rapidjson::Value(hasStrings), allocator);
340  result->Success(document);
341 }
static constexpr char kClipboardError[]
static constexpr char kValueKey[]
static constexpr int kErrorSuccess
static constexpr int kAccessDeniedErrorCode

References kAccessDeniedErrorCode, kClipboardError, kErrorSuccess, and kValueKey.

◆ GetPlainText()

void flutter::PlatformHandler::GetPlainText ( std::unique_ptr< MethodResult< rapidjson::Document >>  result,
std::string_view  key 
)
protectedvirtual

Definition at line 276 of file platform_handler.cc.

278  {
279  std::unique_ptr<ScopedClipboardInterface> clipboard =
280  scoped_clipboard_provider_();
281 
282  int open_result = clipboard->Open(window_handle_);
283  if (open_result != kErrorSuccess) {
284  rapidjson::Document error_code;
285  error_code.SetInt(open_result);
286  result->Error(kClipboardError, "Unable to open clipboard", error_code);
287  return;
288  }
289  if (!clipboard->HasString()) {
290  result->Success(rapidjson::Document());
291  return;
292  }
293  std::variant<std::wstring, int> get_string_result = clipboard->GetString();
294  if (std::holds_alternative<int>(get_string_result)) {
295  rapidjson::Document error_code;
296  error_code.SetInt(std::get<int>(get_string_result));
297  result->Error(kClipboardError, "Unable to get clipboard data", error_code);
298  return;
299  }
300 
301  rapidjson::Document document;
302  document.SetObject();
303  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
304  document.AddMember(
305  rapidjson::Value(key.data(), allocator),
306  rapidjson::Value(
307  fml::WideStringToUtf8(std::get<std::wstring>(get_string_result)),
308  allocator),
309  allocator);
310  result->Success(document);
311 }

References kClipboardError, kErrorSuccess, and key.

◆ QuitApplication()

void flutter::PlatformHandler::QuitApplication ( std::optional< HWND >  hwnd,
std::optional< WPARAM >  wparam,
std::optional< LPARAM >  lparam,
UINT  exit_code 
)
protectedvirtual

Definition at line 451 of file platform_handler.cc.

454  {
455  engine_->OnQuit(hwnd, wparam, lparam, exit_code);
456 }
void OnQuit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)

References flutter::FlutterWindowsEngine::OnQuit().

Referenced by RequestAppExitSuccess(), and SystemExitApplication().

◆ RequestAppExit()

void flutter::PlatformHandler::RequestAppExit ( std::optional< HWND >  hwnd,
std::optional< WPARAM >  wparam,
std::optional< LPARAM >  lparam,
AppExitType  exit_type,
UINT  exit_code 
)
virtual

Definition at line 412 of file platform_handler.cc.

416  {
417  auto callback = std::make_unique<MethodResultFunctions<rapidjson::Document>>(
418  [this, exit_code, hwnd, wparam,
419  lparam](const rapidjson::Document* response) {
420  RequestAppExitSuccess(hwnd, wparam, lparam, response, exit_code);
421  },
422  nullptr, nullptr);
423  auto args = std::make_unique<rapidjson::Document>();
424  args->SetObject();
425  args->GetObj().AddMember(
426  kExitTypeKey, std::string(kExitTypeNames[static_cast<int>(exit_type)]),
427  args->GetAllocator());
428  channel_->InvokeMethod(kRequestAppExitMethod, std::move(args),
429  std::move(callback));
430 }
virtual void RequestAppExitSuccess(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, const rapidjson::Document *result, UINT exit_code)
FlutterDesktopBinaryReply callback
static constexpr const char * kExitTypeNames[]
static constexpr char kRequestAppExitMethod[]
static constexpr char kExitTypeKey[]

References callback, kExitTypeKey, flutter::kExitTypeNames, kRequestAppExitMethod, and RequestAppExitSuccess().

Referenced by SystemExitApplication().

◆ RequestAppExitSuccess()

void flutter::PlatformHandler::RequestAppExitSuccess ( std::optional< HWND >  hwnd,
std::optional< WPARAM >  wparam,
std::optional< LPARAM >  lparam,
const rapidjson::Document *  result,
UINT  exit_code 
)
protectedvirtual

Definition at line 432 of file platform_handler.cc.

436  {
437  rapidjson::Value::ConstMemberIterator itr =
438  result->FindMember(kExitResponseKey);
439  if (itr == result->MemberEnd() || !itr->value.IsString()) {
440  FML_LOG(ERROR) << "Application request response did not contain a valid "
441  "response value";
442  return;
443  }
444  const std::string& exit_type = itr->value.GetString();
445 
446  if (exit_type.compare(kExitResponseExit) == 0) {
447  QuitApplication(hwnd, wparam, lparam, exit_code);
448  }
449 }
virtual void QuitApplication(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
static constexpr char kExitResponseExit[]
static constexpr char kExitResponseKey[]

References kExitResponseExit, kExitResponseKey, and QuitApplication().

Referenced by RequestAppExit().

◆ SetPlainText()

void flutter::PlatformHandler::SetPlainText ( std::string_view  text,
std::unique_ptr< MethodResult< rapidjson::Document >>  result 
)
protectedvirtual

Definition at line 343 of file platform_handler.cc.

345  {
346  std::unique_ptr<ScopedClipboardInterface> clipboard =
347  scoped_clipboard_provider_();
348 
349  int open_result = clipboard->Open(window_handle_);
350  if (open_result != kErrorSuccess) {
351  rapidjson::Document error_code;
352  error_code.SetInt(open_result);
353  result->Error(kClipboardError, "Unable to open clipboard", error_code);
354  return;
355  }
356  std::wstring clipboard_text = fml::Utf8ToWideString(text);
357  // Windows clipboard strings are null-terminated, so an embedded null
358  // character causes other applications to paste only the text before it.
359  std::replace(clipboard_text.begin(), clipboard_text.end(), L'\0', L'\uFFFD');
360  int set_result = clipboard->SetString(clipboard_text);
361  if (set_result != kErrorSuccess) {
362  rapidjson::Document error_code;
363  error_code.SetInt(set_result);
364  result->Error(kClipboardError, "Unable to set clipboard data", error_code);
365  return;
366  }
367  result->Success();
368 }
std::u16string text

References kClipboardError, kErrorSuccess, and text.

◆ SystemExitApplication()

void flutter::PlatformHandler::SystemExitApplication ( AppExitType  exit_type,
UINT  exit_code,
std::unique_ptr< MethodResult< rapidjson::Document >>  result 
)
protectedvirtual

Definition at line 387 of file platform_handler.cc.

390  {
391  rapidjson::Document result_doc;
392  result_doc.SetObject();
393  if (exit_type == AppExitType::required) {
394  QuitApplication(std::nullopt, std::nullopt, std::nullopt, exit_code);
395  result_doc.GetObj().AddMember(kExitResponseKey, kExitResponseExit,
396  result_doc.GetAllocator());
397  result->Success(result_doc);
398  } else {
399  RequestAppExit(std::nullopt, std::nullopt, std::nullopt, exit_type,
400  exit_code);
401  result_doc.GetObj().AddMember(kExitResponseKey, kExitResponseCancel,
402  result_doc.GetAllocator());
403  result->Success(result_doc);
404  }
405 }
virtual void RequestAppExit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, AppExitType exit_type, UINT exit_code)
static constexpr char kExitResponseCancel[]

References kExitResponseCancel, kExitResponseExit, kExitResponseKey, QuitApplication(), RequestAppExit(), and flutter::required.

◆ SystemSoundPlay()

void flutter::PlatformHandler::SystemSoundPlay ( const std::string &  sound_type,
std::unique_ptr< MethodResult< rapidjson::Document >>  result 
)
protectedvirtual

Definition at line 370 of file platform_handler.cc.

372  {
373  if (sound_type.compare(kSoundTypeAlert) == 0) {
374  MessageBeep(MB_OK);
375  result->Success();
376  } else if (sound_type.compare(kSoundTypeClick) == 0) {
377  // No-op, as there is no system sound for key presses.
378  result->Success();
379  } else if (sound_type.compare(kSoundTypeTick) == 0) {
380  // No-op, as there is no system sound for ticks.
381  result->Success();
382  } else {
383  result->NotImplemented();
384  }
385 }
static constexpr char kSoundTypeTick[]
static constexpr char kSoundTypeClick[]
static constexpr char kSoundTypeAlert[]

References kSoundTypeAlert, kSoundTypeClick, and kSoundTypeTick.

Member Data Documentation

◆ kClipboardError

constexpr char flutter::PlatformHandler::kClipboardError[] = "Clipboard error"
staticconstexprprotected

Definition at line 104 of file platform_handler.h.

Referenced by GetHasStrings(), GetPlainText(), and SetPlainText().

◆ kExitTypeCancelable

constexpr char flutter::PlatformHandler::kExitTypeCancelable[] = "cancelable"
staticconstexpr

Definition at line 44 of file platform_handler.h.

Referenced by flutter::StringToAppExitType().

◆ kExitTypeRequired

constexpr char flutter::PlatformHandler::kExitTypeRequired[] = "required"
staticconstexpr

Definition at line 45 of file platform_handler.h.

Referenced by flutter::StringToAppExitType().

◆ kSoundTypeAlert

constexpr char flutter::PlatformHandler::kSoundTypeAlert[] = "SystemSoundType.alert"
staticconstexprprotected

Definition at line 106 of file platform_handler.h.

Referenced by SystemSoundPlay().

◆ kSoundTypeClick

constexpr char flutter::PlatformHandler::kSoundTypeClick[] = "SystemSoundType.click"
staticconstexprprotected

Definition at line 107 of file platform_handler.h.

Referenced by SystemSoundPlay().

◆ kSoundTypeTick

constexpr char flutter::PlatformHandler::kSoundTypeTick[] = "SystemSoundType.tick"
staticconstexprprotected

Definition at line 108 of file platform_handler.h.

Referenced by SystemSoundPlay().


The documentation for this class was generated from the following files: