Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Attributes | List of all members
flutter::PlatformHandler Class Reference

#include <platform_handler.h>

Public Member Functions

 PlatformHandler (flutter::BinaryMessenger *messenger, GLFWwindow *window)
 
 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 18 of file platform_handler.h.

Constructor & Destructor Documentation

◆ PlatformHandler() [1/2]

flutter::PlatformHandler::PlatformHandler ( flutter::BinaryMessenger messenger,
GLFWwindow *  window 
)
explicit

Definition at line 26 of file platform_handler.cc.

28 : channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>(
29 messenger,
32 window_(window) {
33 channel_->SetMethodCallHandler(
34 [this](
37 HandleMethodCall(call, std::move(result));
38 });
39}
static NSString *const kChannelName
static const JsonMethodCodec & GetInstance()
GLFWwindow * window
Definition main.cc:45
GAsyncResult * result

◆ PlatformHandler() [2/2]

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,
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}
FlutterEngine engine
Definition main.cc:68

◆ ~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}
FlutterWindowsView * view(FlutterViewId view_id) const
static constexpr char kClipboardError[]
static constexpr char kValueKey[]
constexpr FlutterViewId kImplicitViewId
static constexpr int kErrorSuccess
static constexpr int kAccessDeniedErrorCode

◆ 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}
std::string WideStringToUtf8(const std::wstring_view str)

◆ 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}
void OnQuit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)

◆ 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}
virtual void RequestAppExitSuccess(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, const rapidjson::Document *result, UINT exit_code)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static constexpr char kRequestAppExitMethod[]
static constexpr char kExitTypeKey[]
static constexpr const char * kExitTypeNames[]

◆ 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}
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[]
#define FML_LOG(severity)
Definition logging.h:82
#define ERROR(message)

◆ 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}
std::u16string text
std::wstring Utf8ToWideString(const std::string_view str)

◆ 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}
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[]

◆ 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}
static constexpr char kSoundTypeAlert[]

Member Data Documentation

◆ kClipboardError

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

Definition at line 104 of file platform_handler.h.

◆ kExitTypeCancelable

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

Definition at line 44 of file platform_handler.h.

◆ kExitTypeRequired

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

Definition at line 45 of file platform_handler.h.

◆ kSoundTypeAlert

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

Definition at line 106 of file platform_handler.h.


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