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"
 

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 222 of file platform_handler.cc.

227  : channel_(std::make_unique<MethodChannel<rapidjson::Document>>(
228  messenger,
229  kChannelName,
231  engine_(engine) {
232  channel_->SetMethodCallHandler(
233  [this](const MethodCall<rapidjson::Document>& call,
234  std::unique_ptr<MethodResult<rapidjson::Document>> result) {
235  HandleMethodCall(call, std::move(result));
236  });
237  if (scoped_clipboard_provider.has_value()) {
238  scoped_clipboard_provider_ = scoped_clipboard_provider.value();
239  } else {
240  scoped_clipboard_provider_ = []() {
241  return std::make_unique<ScopedClipboard>();
242  };
243  }
244 }

◆ ~PlatformHandler()

flutter::PlatformHandler::~PlatformHandler ( )
virtualdefault

Member Function Documentation

◆ GetHasStrings()

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

Definition at line 292 of file platform_handler.cc.

293  {
294  const FlutterWindowsView* view = engine_->view();
295  if (view == nullptr) {
296  result->Error(kClipboardError,
297  "Clipboard is not available in Windows headless mode");
298  return;
299  }
300 
301  std::unique_ptr<ScopedClipboardInterface> clipboard =
302  scoped_clipboard_provider_();
303 
304  bool hasStrings;
305  int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
306  if (open_result != kErrorSuccess) {
307  // Swallow errors of type ERROR_ACCESS_DENIED. These happen when the app is
308  // not in the foreground and GetHasStrings is irrelevant.
309  // See https://github.com/flutter/flutter/issues/95817.
310  if (open_result != kAccessDeniedErrorCode) {
311  rapidjson::Document error_code;
312  error_code.SetInt(open_result);
313  result->Error(kClipboardError, "Unable to open clipboard", error_code);
314  return;
315  }
316  hasStrings = false;
317  } else {
318  hasStrings = clipboard->HasString();
319  }
320 
321  rapidjson::Document document;
322  document.SetObject();
323  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
324  document.AddMember(rapidjson::Value(kValueKey, allocator),
325  rapidjson::Value(hasStrings), allocator);
326  result->Success(document);
327 }

References flutter::FlutterWindowsView::GetRenderTarget(), kAccessDeniedErrorCode, kClipboardError, kErrorSuccess, kValueKey, and flutter::FlutterWindowsEngine::view().

◆ GetPlainText()

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

Definition at line 248 of file platform_handler.cc.

250  {
251  const FlutterWindowsView* view = engine_->view();
252  if (view == nullptr) {
253  result->Error(kClipboardError,
254  "Clipboard is not available in Windows headless mode");
255  return;
256  }
257 
258  std::unique_ptr<ScopedClipboardInterface> clipboard =
259  scoped_clipboard_provider_();
260 
261  int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
262  if (open_result != kErrorSuccess) {
263  rapidjson::Document error_code;
264  error_code.SetInt(open_result);
265  result->Error(kClipboardError, "Unable to open clipboard", error_code);
266  return;
267  }
268  if (!clipboard->HasString()) {
269  result->Success(rapidjson::Document());
270  return;
271  }
272  std::variant<std::wstring, int> get_string_result = clipboard->GetString();
273  if (std::holds_alternative<int>(get_string_result)) {
274  rapidjson::Document error_code;
275  error_code.SetInt(std::get<int>(get_string_result));
276  result->Error(kClipboardError, "Unable to get clipboard data", error_code);
277  return;
278  }
279 
280  rapidjson::Document document;
281  document.SetObject();
282  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
283  document.AddMember(
284  rapidjson::Value(key.data(), allocator),
285  rapidjson::Value(
286  fml::WideStringToUtf8(std::get<std::wstring>(get_string_result)),
287  allocator),
288  allocator);
289  result->Success(document);
290 }

References flutter::FlutterWindowsView::GetRenderTarget(), kClipboardError, kErrorSuccess, key, and flutter::FlutterWindowsEngine::view().

◆ QuitApplication()

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

Definition at line 434 of file platform_handler.cc.

437  {
438  engine_->OnQuit(hwnd, wparam, lparam, exit_code);
439 }

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 395 of file platform_handler.cc.

399  {
400  auto callback = std::make_unique<MethodResultFunctions<rapidjson::Document>>(
401  [this, exit_code, hwnd, wparam,
402  lparam](const rapidjson::Document* response) {
403  RequestAppExitSuccess(hwnd, wparam, lparam, response, exit_code);
404  },
405  nullptr, nullptr);
406  auto args = std::make_unique<rapidjson::Document>();
407  args->SetObject();
408  args->GetObjectW().AddMember(
409  kExitTypeKey, std::string(kExitTypeNames[static_cast<int>(exit_type)]),
410  args->GetAllocator());
411  channel_->InvokeMethod(kRequestAppExitMethod, std::move(args),
412  std::move(callback));
413 }

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 415 of file platform_handler.cc.

419  {
420  rapidjson::Value::ConstMemberIterator itr =
421  result->FindMember(kExitResponseKey);
422  if (itr == result->MemberEnd() || !itr->value.IsString()) {
423  FML_LOG(ERROR) << "Application request response did not contain a valid "
424  "response value";
425  return;
426  }
427  const std::string& exit_type = itr->value.GetString();
428 
429  if (exit_type.compare(kExitResponseExit) == 0) {
430  QuitApplication(hwnd, wparam, lparam, exit_code);
431  }
432 }

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 329 of file platform_handler.cc.

331  {
332  const FlutterWindowsView* view = engine_->view();
333  if (view == nullptr) {
334  result->Error(kClipboardError,
335  "Clipboard is not available in Windows headless mode");
336  return;
337  }
338 
339  std::unique_ptr<ScopedClipboardInterface> clipboard =
340  scoped_clipboard_provider_();
341 
342  int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
343  if (open_result != kErrorSuccess) {
344  rapidjson::Document error_code;
345  error_code.SetInt(open_result);
346  result->Error(kClipboardError, "Unable to open clipboard", error_code);
347  return;
348  }
349  int set_result = clipboard->SetString(fml::Utf8ToWideString(text));
350  if (set_result != kErrorSuccess) {
351  rapidjson::Document error_code;
352  error_code.SetInt(set_result);
353  result->Error(kClipboardError, "Unable to set clipboard data", error_code);
354  return;
355  }
356  result->Success();
357 }

References flutter::FlutterWindowsView::GetRenderTarget(), kClipboardError, kErrorSuccess, text, and flutter::FlutterWindowsEngine::view().

◆ SystemExitApplication()

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

Definition at line 370 of file platform_handler.cc.

373  {
374  rapidjson::Document result_doc;
375  result_doc.SetObject();
376  if (exit_type == AppExitType::required) {
377  QuitApplication(std::nullopt, std::nullopt, std::nullopt, exit_code);
378  result_doc.GetObjectW().AddMember(kExitResponseKey, kExitResponseExit,
379  result_doc.GetAllocator());
380  result->Success(result_doc);
381  } else {
382  RequestAppExit(std::nullopt, std::nullopt, std::nullopt, exit_type,
383  exit_code);
384  result_doc.GetObjectW().AddMember(kExitResponseKey, kExitResponseCancel,
385  result_doc.GetAllocator());
386  result->Success(result_doc);
387  }
388 }

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 359 of file platform_handler.cc.

361  {
362  if (sound_type.compare(kSoundTypeAlert) == 0) {
363  MessageBeep(MB_OK);
364  result->Success();
365  } else {
366  result->NotImplemented();
367  }
368 }

References kSoundTypeAlert.

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().


The documentation for this class was generated from the following files:
kValueKey
static constexpr char kValueKey[]
Definition: platform_handler.cc:43
flutter::PlatformHandler::QuitApplication
virtual void QuitApplication(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
Definition: platform_handler.cc:434
flutter::FlutterWindowsEngine::OnQuit
void OnQuit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
Definition: flutter_windows_engine.cc:798
flutter::JsonMethodCodec::GetInstance
static const JsonMethodCodec & GetInstance()
Definition: json_method_codec.cc:36
kRequestAppExitMethod
static constexpr char kRequestAppExitMethod[]
Definition: platform_handler.cc:25
flutter::PlatformHandler::RequestAppExit
virtual void RequestAppExit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, AppExitType exit_type, UINT exit_code)
Definition: platform_handler.cc:395
text
std::u16string text
Definition: keyboard_unittests.cc:332
flutter::PlatformHandler::kClipboardError
static constexpr char kClipboardError[]
Definition: platform_handler.h:104
kExitResponseKey
static constexpr char kExitResponseKey[]
Definition: platform_handler.cc:34
kChannelName
static constexpr char kChannelName[]
Definition: platform_handler.cc:19
kErrorSuccess
static constexpr int kErrorSuccess
Definition: platform_handler.cc:45
flutter::FlutterWindowsEngine::view
FlutterWindowsView * view()
Definition: flutter_windows_engine.h:115
kExitResponseExit
static constexpr char kExitResponseExit[]
Definition: platform_handler.cc:36
flutter::kExitTypeNames
static constexpr const char * kExitTypeNames[]
Definition: platform_handler.cc:392
kAccessDeniedErrorCode
static constexpr int kAccessDeniedErrorCode
Definition: platform_handler.cc:44
flutter::PlatformHandler::RequestAppExitSuccess
virtual void RequestAppExitSuccess(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, const rapidjson::Document *result, UINT exit_code)
Definition: platform_handler.cc:415
flutter::PlatformHandler::kSoundTypeAlert
static constexpr char kSoundTypeAlert[]
Definition: platform_handler.h:106
key
int key
Definition: keyboard_key_handler_unittests.cc:114
flutter::AppExitType::required
@ required
kExitResponseCancel
static constexpr char kExitResponseCancel[]
Definition: platform_handler.cc:35
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:46
kExitTypeKey
static constexpr char kExitTypeKey[]
Definition: platform_handler.cc:32