Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5d69998

Browse files
committed
[Windows] Make the engine own PlatformHandler
1 parent f75287a commit 5d69998

7 files changed

+162
-81
lines changed

shell/platform/windows/flutter_windows_engine.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ FlutterWindowsEngine::FlutterWindowsEngine(
207207
// Set up internal channels.
208208
// TODO: Replace this with an embedder.h API. See
209209
// https://github.com/flutter/flutter/issues/71099
210+
platform_handler_ =
211+
std::make_unique<PlatformHandler>(messenger_wrapper_.get(), this);
210212
settings_plugin_ = std::make_unique<SettingsPlugin>(messenger_wrapper_.get(),
211213
task_runner_.get());
212214
}

shell/platform/windows/flutter_windows_engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "flutter/shell/platform/windows/flutter_desktop_messenger.h"
2424
#include "flutter/shell/platform/windows/flutter_project_bundle.h"
2525
#include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
26+
#include "flutter/shell/platform/windows/platform_handler.h"
2627
#include "flutter/shell/platform/windows/public/flutter_windows.h"
2728
#include "flutter/shell/platform/windows/settings_plugin.h"
2829
#include "flutter/shell/platform/windows/task_runner.h"
@@ -306,6 +307,9 @@ class FlutterWindowsEngine {
306307
// May be nullptr if ANGLE failed to initialize.
307308
std::unique_ptr<AngleSurfaceManager> surface_manager_;
308309

310+
// Handler for the flutter/platform channel.
311+
std::unique_ptr<PlatformHandler> platform_handler_;
312+
309313
// The settings plugin.
310314
std::unique_ptr<SettingsPlugin> settings_plugin_;
311315

shell/platform/windows/flutter_windows_view.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ void FlutterWindowsView::SetEngine(
6363
// Set up the system channel handlers.
6464
auto internal_plugin_messenger = internal_plugin_registrar_->messenger();
6565
InitializeKeyboard();
66-
platform_handler_ =
67-
std::make_unique<PlatformHandler>(internal_plugin_messenger, this);
6866
cursor_handler_ = std::make_unique<CursorHandler>(internal_plugin_messenger,
6967
binding_handler_.get());
7068

shell/platform/windows/flutter_windows_view.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
2323
#include "flutter/shell/platform/windows/keyboard_handler_base.h"
2424
#include "flutter/shell/platform/windows/keyboard_key_embedder_handler.h"
25-
#include "flutter/shell/platform/windows/platform_handler.h"
2625
#include "flutter/shell/platform/windows/public/flutter_windows.h"
2726
#include "flutter/shell/platform/windows/text_input_plugin.h"
2827
#include "flutter/shell/platform/windows/text_input_plugin_delegate.h"
@@ -380,9 +379,6 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
380379
// Handlers for text events from Windows.
381380
std::unique_ptr<TextInputPlugin> text_input_plugin_;
382381

383-
// Handler for the flutter/platform channel.
384-
std::unique_ptr<PlatformHandler> platform_handler_;
385-
386382
// Handler for cursor events.
387383
std::unique_ptr<CursorHandler> cursor_handler_;
388384

shell/platform/windows/platform_handler.cc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ int ScopedClipboard::SetString(const std::wstring string) {
199199

200200
PlatformHandler::PlatformHandler(
201201
BinaryMessenger* messenger,
202-
FlutterWindowsView* view,
202+
FlutterWindowsEngine* engine,
203203
std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
204204
scoped_clipboard_provider)
205205
: channel_(std::make_unique<MethodChannel<rapidjson::Document>>(
206206
messenger,
207207
kChannelName,
208208
&JsonMethodCodec::GetInstance())),
209-
view_(view) {
209+
engine_(engine) {
210210
channel_->SetMethodCallHandler(
211211
[this](const MethodCall<rapidjson::Document>& call,
212212
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
@@ -226,10 +226,17 @@ PlatformHandler::~PlatformHandler() = default;
226226
void PlatformHandler::GetPlainText(
227227
std::unique_ptr<MethodResult<rapidjson::Document>> result,
228228
std::string_view key) {
229+
const FlutterWindowsView* view = engine_->view();
230+
if (view == nullptr) {
231+
result->Error(kClipboardError,
232+
"Clipboard is not available in Windows headless mode");
233+
return;
234+
}
235+
229236
std::unique_ptr<ScopedClipboardInterface> clipboard =
230237
scoped_clipboard_provider_();
231238

232-
int open_result = clipboard->Open(std::get<HWND>(*view_->GetRenderTarget()));
239+
int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
233240
if (open_result != kErrorSuccess) {
234241
rapidjson::Document error_code;
235242
error_code.SetInt(open_result);
@@ -262,11 +269,18 @@ void PlatformHandler::GetPlainText(
262269

263270
void PlatformHandler::GetHasStrings(
264271
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
272+
const FlutterWindowsView* view = engine_->view();
273+
if (view == nullptr) {
274+
result->Error(kClipboardError,
275+
"Clipboard is not available in Windows headless mode");
276+
return;
277+
}
278+
265279
std::unique_ptr<ScopedClipboardInterface> clipboard =
266280
scoped_clipboard_provider_();
267281

268282
bool hasStrings;
269-
int open_result = clipboard->Open(std::get<HWND>(*view_->GetRenderTarget()));
283+
int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
270284
if (open_result != kErrorSuccess) {
271285
// Swallow errors of type ERROR_ACCESS_DENIED. These happen when the app is
272286
// not in the foreground and GetHasStrings is irrelevant.
@@ -293,10 +307,17 @@ void PlatformHandler::GetHasStrings(
293307
void PlatformHandler::SetPlainText(
294308
const std::string& text,
295309
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
310+
const FlutterWindowsView* view = engine_->view();
311+
if (view == nullptr) {
312+
result->Error(kClipboardError,
313+
"Clipboard is not available in Windows headless mode");
314+
return;
315+
}
316+
296317
std::unique_ptr<ScopedClipboardInterface> clipboard =
297318
scoped_clipboard_provider_();
298319

299-
int open_result = clipboard->Open(std::get<HWND>(*view_->GetRenderTarget()));
320+
int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
300321
if (open_result != kErrorSuccess) {
301322
rapidjson::Document error_code;
302323
error_code.SetInt(open_result);

shell/platform/windows/platform_handler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
namespace flutter {
2020

21-
class FlutterWindowsView;
21+
class FlutterWindowsEngine;
2222
class ScopedClipboardInterface;
2323

2424
// Handler for internal system channels.
2525
class PlatformHandler {
2626
public:
2727
explicit PlatformHandler(
2828
BinaryMessenger* messenger,
29-
FlutterWindowsView* view,
29+
FlutterWindowsEngine* engine,
3030
std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
3131
scoped_clipboard_provider = std::nullopt);
3232

@@ -68,8 +68,8 @@ class PlatformHandler {
6868
// The MethodChannel used for communication with the Flutter engine.
6969
std::unique_ptr<MethodChannel<rapidjson::Document>> channel_;
7070

71-
// A reference to the Flutter view.
72-
FlutterWindowsView* view_;
71+
// A reference to the Flutter engine.
72+
FlutterWindowsEngine* engine_;
7373

7474
// A scoped clipboard provider that can be passed in for mocking in tests.
7575
// Use this to acquire clipboard in each operation to avoid blocking clipboard

0 commit comments

Comments
 (0)