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 (const std::string &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 447 of file platform_handler.cc.

450  {
451  engine_->OnQuit(hwnd, wparam, lparam, exit_code);
452 }
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 408 of file platform_handler.cc.

412  {
413  auto callback = std::make_unique<MethodResultFunctions<rapidjson::Document>>(
414  [this, exit_code, hwnd, wparam,
415  lparam](const rapidjson::Document* response) {
416  RequestAppExitSuccess(hwnd, wparam, lparam, response, exit_code);
417  },
418  nullptr, nullptr);
419  auto args = std::make_unique<rapidjson::Document>();
420  args->SetObject();
421  args->GetObjectW().AddMember(
422  kExitTypeKey, std::string(kExitTypeNames[static_cast<int>(exit_type)]),
423  args->GetAllocator());
424  channel_->InvokeMethod(kRequestAppExitMethod, std::move(args),
425  std::move(callback));
426 }
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 428 of file platform_handler.cc.

432  {
433  rapidjson::Value::ConstMemberIterator itr =
434  result->FindMember(kExitResponseKey);
435  if (itr == result->MemberEnd() || !itr->value.IsString()) {
436  FML_LOG(ERROR) << "Application request response did not contain a valid "
437  "response value";
438  return;
439  }
440  const std::string& exit_type = itr->value.GetString();
441 
442  if (exit_type.compare(kExitResponseExit) == 0) {
443  QuitApplication(hwnd, wparam, lparam, exit_code);
444  }
445 }
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 ( const std::string &  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  int set_result = clipboard->SetString(fml::Utf8ToWideString(text));
357  if (set_result != kErrorSuccess) {
358  rapidjson::Document error_code;
359  error_code.SetInt(set_result);
360  result->Error(kClipboardError, "Unable to set clipboard data", error_code);
361  return;
362  }
363  result->Success();
364 }
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 383 of file platform_handler.cc.

386  {
387  rapidjson::Document result_doc;
388  result_doc.SetObject();
389  if (exit_type == AppExitType::required) {
390  QuitApplication(std::nullopt, std::nullopt, std::nullopt, exit_code);
391  result_doc.GetObjectW().AddMember(kExitResponseKey, kExitResponseExit,
392  result_doc.GetAllocator());
393  result->Success(result_doc);
394  } else {
395  RequestAppExit(std::nullopt, std::nullopt, std::nullopt, exit_type,
396  exit_code);
397  result_doc.GetObjectW().AddMember(kExitResponseKey, kExitResponseCancel,
398  result_doc.GetAllocator());
399  result->Success(result_doc);
400  }
401 }
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 366 of file platform_handler.cc.

368  {
369  if (sound_type.compare(kSoundTypeAlert) == 0) {
370  MessageBeep(MB_OK);
371  result->Success();
372  } else if (sound_type.compare(kSoundTypeClick) == 0) {
373  // No-op, as there is no system sound for key presses.
374  result->Success();
375  } else if (sound_type.compare(kSoundTypeTick) == 0) {
376  // No-op, as there is no system sound for ticks.
377  result->Success();
378  } else {
379  result->NotImplemented();
380  }
381 }
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: