Flutter Engine
 
Loading...
Searching...
No Matches
platform_handler.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_
6#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_
7
8#include <Windows.h>
9
10#include <functional>
11#include <memory>
12#include <optional>
13#include <variant>
14
15#include "flutter/fml/macros.h"
18#include "rapidjson/document.h"
19
20namespace flutter {
21
22class FlutterWindowsEngine;
23class ScopedClipboardInterface;
24
25// Indicates whether an exit request may be canceled by the framework.
26// These values must be kept in sync with kExitTypeNames in platform_handler.cc
27enum class AppExitType {
30};
31
32// Handler for internal system channels.
33class PlatformHandler {
34 public:
35 explicit PlatformHandler(
36 BinaryMessenger* messenger,
37 FlutterWindowsEngine* engine,
38 std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
39 scoped_clipboard_provider = std::nullopt);
40
41 virtual ~PlatformHandler();
42
43 // String values used for encoding/decoding exit requests.
44 static constexpr char kExitTypeCancelable[] = "cancelable";
45 static constexpr char kExitTypeRequired[] = "required";
46
47 // Send a request to the framework to test if a cancelable exit request
48 // should be canceled or honored. hwnd is std::nullopt for a request to quit
49 // the process, otherwise it holds the HWND of the window that initiated the
50 // quit request.
51 virtual void RequestAppExit(std::optional<HWND> hwnd,
52 std::optional<WPARAM> wparam,
53 std::optional<LPARAM> lparam,
54 AppExitType exit_type,
55 UINT exit_code);
56
57 protected:
58 // Gets plain text from the clipboard and provides it to |result| as the
59 // value in a dictionary with the given |key|.
60 virtual void GetPlainText(
61 std::unique_ptr<MethodResult<rapidjson::Document>> result,
62 std::string_view key);
63
64 // Provides a boolean to |result| as the value in a dictionary at key
65 // "value" representing whether or not the clipboard has a non-empty string.
66 virtual void GetHasStrings(
67 std::unique_ptr<MethodResult<rapidjson::Document>> result);
68
69 // Sets the clipboard's plain text to |text|, and reports the result (either
70 // an error, or null for success) to |result|.
71 virtual void SetPlainText(
72 const std::string& text,
73 std::unique_ptr<MethodResult<rapidjson::Document>> result);
74
75 virtual void SystemSoundPlay(
76 const std::string& sound_type,
77 std::unique_ptr<MethodResult<rapidjson::Document>> result);
78
79 // Handle a request from the framework to exit the application.
80 virtual void SystemExitApplication(
81 AppExitType exit_type,
82 UINT exit_code,
83 std::unique_ptr<MethodResult<rapidjson::Document>> result);
84
85 // Actually quit the application with the provided exit code. hwnd is
86 // std::nullopt for a request to quit the process, otherwise it holds the HWND
87 // of the window that initiated the quit request.
88 virtual void QuitApplication(std::optional<HWND> hwnd,
89 std::optional<WPARAM> wparam,
90 std::optional<LPARAM> lparam,
91 UINT exit_code);
92
93 // Callback from when the cancelable exit request response request is
94 // answered by the framework. hwnd is std::nullopt for a request to quit the
95 // process, otherwise it holds the HWND of the window that initiated the quit
96 // request.
97 virtual void RequestAppExitSuccess(std::optional<HWND> hwnd,
98 std::optional<WPARAM> wparam,
99 std::optional<LPARAM> lparam,
100 const rapidjson::Document* result,
101 UINT exit_code);
102
103 // A error type to use for error responses.
104 static constexpr char kClipboardError[] = "Clipboard error";
105
106 static constexpr char kSoundTypeAlert[] = "SystemSoundType.alert";
107 static constexpr char kSoundTypeClick[] = "SystemSoundType.click";
108 static constexpr char kSoundTypeTick[] = "SystemSoundType.tick";
109
110 private:
111 WNDCLASS RegisterWindowClass();
112
113 // Called when a method is called on |channel_|;
114 void HandleMethodCall(
116 std::unique_ptr<MethodResult<rapidjson::Document>> result);
117
118 static LRESULT CALLBACK WndProc(HWND const window,
119 UINT const message,
120 WPARAM const wparam,
121 LPARAM const lparam) noexcept;
122
123 // The MethodChannel used for communication with the Flutter engine.
124 std::unique_ptr<MethodChannel<rapidjson::Document>> channel_;
125
126 // A reference to the Flutter engine.
127 FlutterWindowsEngine* engine_;
128
129 HWND window_handle_ = nullptr;
130
131 // A scoped clipboard provider that can be passed in for mocking in tests.
132 // Use this to acquire clipboard in each operation to avoid blocking clipboard
133 // unnecessarily. See flutter/flutter#103205.
134 std::function<std::unique_ptr<ScopedClipboardInterface>()>
135 scoped_clipboard_provider_;
136
138};
139
140// A public interface for ScopedClipboard, so that it can be injected into
141// PlatformHandler.
143 public:
145
146 // Attempts to open the clipboard for the given window, returning the error
147 // code in the case of failure and 0 otherwise.
148 virtual int Open(HWND window) = 0;
149
150 // Returns true if there is string data available to get.
151 virtual bool HasString() = 0;
152
153 // Returns string data from the clipboard.
154 //
155 // If getting a string fails, returns the error code.
156 //
157 // Open(...) must have succeeded to call this method.
158 virtual std::variant<std::wstring, int> GetString() = 0;
159
160 // Sets the string content of the clipboard, returning the error code on
161 // failure and 0 otherwise.
162 //
163 // Open(...) must have succeeded to call this method.
164 virtual int SetString(const std::wstring string) = 0;
165};
166
167} // namespace flutter
168
169#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_
virtual void QuitApplication(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
static constexpr char kSoundTypeTick[]
virtual void RequestAppExitSuccess(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, const rapidjson::Document *result, UINT exit_code)
static constexpr char kClipboardError[]
static constexpr char kSoundTypeClick[]
virtual void GetHasStrings(std::unique_ptr< MethodResult< rapidjson::Document > > result)
static constexpr char kExitTypeCancelable[]
virtual void GetPlainText(std::unique_ptr< MethodResult< rapidjson::Document > > result, std::string_view key)
virtual void RequestAppExit(std::optional< HWND > hwnd, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, AppExitType exit_type, UINT exit_code)
static constexpr char kExitTypeRequired[]
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)
static constexpr char kSoundTypeAlert[]
virtual std::variant< std::wstring, int > GetString()=0
virtual int SetString(const std::wstring string)=0
virtual int Open(HWND window)=0
GLFWwindow * window
Definition main.cc:60
FlutterEngine engine
Definition main.cc:84
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
G_BEGIN_DECLS GBytes * message
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
std::u16string text
LONG_PTR LRESULT
unsigned int UINT
LONG_PTR LPARAM
UINT_PTR WPARAM
#define CALLBACK