Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shell/platform/windows/flutter_windows_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ FlutterWindowsEngine::FlutterWindowsEngine(
// Set up internal channels.
// TODO: Replace this with an embedder.h API. See
// https://github.com/flutter/flutter/issues/71099
platform_handler_ =
std::make_unique<PlatformHandler>(messenger_wrapper_.get(), this);
settings_plugin_ = std::make_unique<SettingsPlugin>(messenger_wrapper_.get(),
task_runner_.get());
}
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/windows/flutter_windows_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "flutter/shell/platform/windows/flutter_desktop_messenger.h"
#include "flutter/shell/platform/windows/flutter_project_bundle.h"
#include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
#include "flutter/shell/platform/windows/platform_handler.h"
#include "flutter/shell/platform/windows/public/flutter_windows.h"
#include "flutter/shell/platform/windows/settings_plugin.h"
#include "flutter/shell/platform/windows/task_runner.h"
Expand Down Expand Up @@ -306,6 +307,9 @@ class FlutterWindowsEngine {
// May be nullptr if ANGLE failed to initialize.
std::unique_ptr<AngleSurfaceManager> surface_manager_;

// Handler for the flutter/platform channel.
std::unique_ptr<PlatformHandler> platform_handler_;

// The settings plugin.
std::unique_ptr<SettingsPlugin> settings_plugin_;

Expand Down
2 changes: 0 additions & 2 deletions shell/platform/windows/flutter_windows_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ void FlutterWindowsView::SetEngine(
// Set up the system channel handlers.
auto internal_plugin_messenger = internal_plugin_registrar_->messenger();
InitializeKeyboard();
platform_handler_ =
std::make_unique<PlatformHandler>(internal_plugin_messenger, this);
cursor_handler_ = std::make_unique<CursorHandler>(internal_plugin_messenger,
binding_handler_.get());

Expand Down
4 changes: 0 additions & 4 deletions shell/platform/windows/flutter_windows_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
#include "flutter/shell/platform/windows/keyboard_handler_base.h"
#include "flutter/shell/platform/windows/keyboard_key_embedder_handler.h"
#include "flutter/shell/platform/windows/platform_handler.h"
#include "flutter/shell/platform/windows/public/flutter_windows.h"
#include "flutter/shell/platform/windows/text_input_plugin.h"
#include "flutter/shell/platform/windows/text_input_plugin_delegate.h"
Expand Down Expand Up @@ -380,9 +379,6 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate,
// Handlers for text events from Windows.
std::unique_ptr<TextInputPlugin> text_input_plugin_;

// Handler for the flutter/platform channel.
std::unique_ptr<PlatformHandler> platform_handler_;

// Handler for cursor events.
std::unique_ptr<CursorHandler> cursor_handler_;

Expand Down
31 changes: 26 additions & 5 deletions shell/platform/windows/platform_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ int ScopedClipboard::SetString(const std::wstring string) {

PlatformHandler::PlatformHandler(
BinaryMessenger* messenger,
FlutterWindowsView* view,
FlutterWindowsEngine* engine,
std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
scoped_clipboard_provider)
: channel_(std::make_unique<MethodChannel<rapidjson::Document>>(
messenger,
kChannelName,
&JsonMethodCodec::GetInstance())),
view_(view) {
engine_(engine) {
channel_->SetMethodCallHandler(
[this](const MethodCall<rapidjson::Document>& call,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
Expand All @@ -226,10 +226,17 @@ PlatformHandler::~PlatformHandler() = default;
void PlatformHandler::GetPlainText(
std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) {
const FlutterWindowsView* view = engine_->view();
if (view == nullptr) {
result->Error(kClipboardError,
"Clipboard is not available in Windows headless mode");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message mirrors the GLFW message:

if (!window_) {
result->Error(kNoWindowError,
"Clipboard is not available in GLFW headless mode.");
return;
}

Please let me know if you have suggestions though!

return;
}

std::unique_ptr<ScopedClipboardInterface> clipboard =
scoped_clipboard_provider_();

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

void PlatformHandler::GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
const FlutterWindowsView* view = engine_->view();
if (view == nullptr) {
result->Error(kClipboardError,
"Clipboard is not available in Windows headless mode");
return;
}

std::unique_ptr<ScopedClipboardInterface> clipboard =
scoped_clipboard_provider_();

bool hasStrings;
int open_result = clipboard->Open(std::get<HWND>(*view_->GetRenderTarget()));
int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
if (open_result != kErrorSuccess) {
// Swallow errors of type ERROR_ACCESS_DENIED. These happen when the app is
// not in the foreground and GetHasStrings is irrelevant.
Expand All @@ -293,10 +307,17 @@ void PlatformHandler::GetHasStrings(
void PlatformHandler::SetPlainText(
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
const FlutterWindowsView* view = engine_->view();
if (view == nullptr) {
result->Error(kClipboardError,
"Clipboard is not available in Windows headless mode");
return;
}

std::unique_ptr<ScopedClipboardInterface> clipboard =
scoped_clipboard_provider_();

int open_result = clipboard->Open(std::get<HWND>(*view_->GetRenderTarget()));
int open_result = clipboard->Open(std::get<HWND>(*view->GetRenderTarget()));
if (open_result != kErrorSuccess) {
rapidjson::Document error_code;
error_code.SetInt(open_result);
Expand Down
8 changes: 4 additions & 4 deletions shell/platform/windows/platform_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

namespace flutter {

class FlutterWindowsView;
class FlutterWindowsEngine;
class ScopedClipboardInterface;

// Handler for internal system channels.
class PlatformHandler {
public:
explicit PlatformHandler(
BinaryMessenger* messenger,
FlutterWindowsView* view,
FlutterWindowsEngine* engine,
std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
scoped_clipboard_provider = std::nullopt);

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

// A reference to the Flutter view.
FlutterWindowsView* view_;
// A reference to the Flutter engine.
FlutterWindowsEngine* engine_;

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