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

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

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

References flutter::FlutterWindowsView::GetWindowHandle(), kClipboardError, kErrorSuccess, key, flutter::kImplicitViewId, 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 440 of file platform_handler.cc.

443  {
444  engine_->OnQuit(hwnd, wparam, lparam, exit_code);
445 }

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

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

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

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

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

335  {
336  // TODO(loicsharma): Remove implicit view assumption.
337  // https://github.com/flutter/flutter/issues/142845
338  const FlutterWindowsView* view = engine_->view(kImplicitViewId);
339  if (view == nullptr) {
340  result->Error(kClipboardError,
341  "Clipboard is not available in Windows headless mode");
342  return;
343  }
344 
345  std::unique_ptr<ScopedClipboardInterface> clipboard =
346  scoped_clipboard_provider_();
347 
348  int open_result = clipboard->Open(view->GetWindowHandle());
349  if (open_result != kErrorSuccess) {
350  rapidjson::Document error_code;
351  error_code.SetInt(open_result);
352  result->Error(kClipboardError, "Unable to open clipboard", error_code);
353  return;
354  }
355  int set_result = clipboard->SetString(fml::Utf8ToWideString(text));
356  if (set_result != kErrorSuccess) {
357  rapidjson::Document error_code;
358  error_code.SetInt(set_result);
359  result->Error(kClipboardError, "Unable to set clipboard data", error_code);
360  return;
361  }
362  result->Success();
363 }

References flutter::FlutterWindowsView::GetWindowHandle(), kClipboardError, kErrorSuccess, flutter::kImplicitViewId, 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 376 of file platform_handler.cc.

379  {
380  rapidjson::Document result_doc;
381  result_doc.SetObject();
382  if (exit_type == AppExitType::required) {
383  QuitApplication(std::nullopt, std::nullopt, std::nullopt, exit_code);
384  result_doc.GetObjectW().AddMember(kExitResponseKey, kExitResponseExit,
385  result_doc.GetAllocator());
386  result->Success(result_doc);
387  } else {
388  RequestAppExit(std::nullopt, std::nullopt, std::nullopt, exit_type,
389  exit_code);
390  result_doc.GetObjectW().AddMember(kExitResponseKey, kExitResponseCancel,
391  result_doc.GetAllocator());
392  result->Success(result_doc);
393  }
394 }

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

367  {
368  if (sound_type.compare(kSoundTypeAlert) == 0) {
369  MessageBeep(MB_OK);
370  result->Success();
371  } else {
372  result->NotImplemented();
373  }
374 }

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:
flutter::kImplicitViewId
constexpr FlutterViewId kImplicitViewId
Definition: flutter_windows_engine.h:55
flutter::FlutterWindowsEngine::view
FlutterWindowsView * view(FlutterViewId view_id) const
Definition: flutter_windows_engine.cc:656
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:440
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:946
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:401
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
kExitResponseExit
static constexpr char kExitResponseExit[]
Definition: platform_handler.cc:36
flutter::kExitTypeNames
static constexpr const char * kExitTypeNames[]
Definition: platform_handler.cc:398
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:421
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:52
kExitTypeKey
static constexpr char kExitTypeKey[]
Definition: platform_handler.cc:32