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
1 change: 1 addition & 0 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ executable("flutter_windows_unittests") {
"keyboard_key_embedder_handler_unittests.cc",
"keyboard_key_handler_unittests.cc",
"keyboard_unittests.cc",
"platform_handler_unittests.cc",
"testing/flutter_window_win32_test.cc",
"testing/flutter_window_win32_test.h",
"testing/mock_window_binding_handler.cc",
Expand Down
10 changes: 10 additions & 0 deletions shell/platform/windows/platform_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
static constexpr char kChannelName[] = "flutter/platform";

static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings";
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";

static constexpr char kTextPlainFormat[] = "text/plain";
Expand Down Expand Up @@ -46,6 +47,15 @@ void PlatformHandler::HandleMethodCall(
return;
}
GetPlainText(std::move(result), kTextKey);
} else if (method.compare(kHasStringsClipboardMethod) == 0) {
// Only one string argument is expected.
const rapidjson::Value& format = method_call.arguments()[0];

if (strcmp(format.GetString(), kTextPlainFormat) != 0) {
result->Error(kClipboardError, kUnknownClipboardFormatMessage);
return;
}
GetHasStrings(std::move(result));
} else if (method.compare(kSetClipboardDataMethod) == 0) {
const rapidjson::Value& document = *method_call.arguments();
rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey);
Expand Down
5 changes: 5 additions & 0 deletions shell/platform/windows/platform_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class PlatformHandler {
std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) = 0;

// Provides a boolean to |result| as the value in a dictionary at key
// "value" representing whether or not the clipboard has a non-empty string.
virtual void GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) = 0;

// Sets the clipboard's plain text to |text|, and reports the result (either
// an error, or null for success) to |result|.
virtual void SetPlainText(
Expand Down
62 changes: 56 additions & 6 deletions shell/platform/windows/platform_handler_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <memory>

#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was causing a compiler error for some reason, removing it seemed to work 🤷

#include "flutter/shell/platform/common/json_method_codec.h"
#include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
#include "gmock/gmock.h"
Expand All @@ -22,9 +21,11 @@ using ::testing::_;
static constexpr char kChannelName[] = "flutter/platform";

static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings";
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";

static constexpr char kTextPlainFormat[] = "text/plain";
static constexpr char kFakeContentType[] = "text/madeupcontenttype";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fake content type must begin with "text" even though it's supposed to fail. If it doesn't, then rapidjson can't parse the json at all.


// Test implementation of PlatformHandler to allow testing the PlatformHandler
// logic.
Expand All @@ -38,7 +39,9 @@ class TestPlatformHandler : public PlatformHandler {
// |PlatformHandler|
MOCK_METHOD2(GetPlainText,
void(std::unique_ptr<MethodResult<rapidjson::Document>>,
const char*));
std::string_view key));
MOCK_METHOD1(GetHasStrings,
void(std::unique_ptr<MethodResult<rapidjson::Document>>));
MOCK_METHOD2(SetPlainText,
void(const std::string&,
std::unique_ptr<MethodResult<rapidjson::Document>>));
Expand All @@ -61,9 +64,9 @@ TEST(PlatformHandler, GettingTextCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);

auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->PushBack(kTextPlainFormat, allocator);
args->SetString(kTextPlainFormat);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kGetClipboardDataMethod,
std::move(args)));
Expand All @@ -85,9 +88,9 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);

auto args = std::make_unique<rapidjson::Document>(rapidjson::kArrayType);
auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->PushBack("madeup/contenttype", allocator);
args->SetString(kFakeContentType);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kGetClipboardDataMethod,
std::move(args)));
Expand All @@ -103,6 +106,53 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) {
}));
}

TEST(PlatformHandler, GetHasStringsCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);

auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->SetString(kTextPlainFormat);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kHasStringsClipboardMethod,
std::move(args)));

// Set up a handler to call a response on |result| so that it doesn't log
// on destruction about leaking.
ON_CALL(platform_handler, GetHasStrings)
.WillByDefault(
[](std::unique_ptr<MethodResult<rapidjson::Document>> result) {
result->NotImplemented();
});

EXPECT_CALL(platform_handler, GetHasStrings(_));
EXPECT_TRUE(messenger.SimulateEngineMessage(
kChannelName, encoded->data(), encoded->size(),
[](const uint8_t* reply, size_t reply_size) {}));
}

TEST(PlatformHandler, RejectsGetHasStringsOnUnknownTypes) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);

auto args = std::make_unique<rapidjson::Document>(rapidjson::kStringType);
auto& allocator = args->GetAllocator();
args->SetString(kFakeContentType);
auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall(
MethodCall<rapidjson::Document>(kHasStringsClipboardMethod,
std::move(args)));

MockMethodResult result;
// Requsting an unknow content type is an error.
EXPECT_CALL(result, ErrorInternal(_, _, _));
EXPECT_TRUE(messenger.SimulateEngineMessage(
kChannelName, encoded->data(), encoded->size(),
[&](const uint8_t* reply, size_t reply_size) {
JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope(
reply, reply_size, &result);
}));
}

TEST(PlatformHandler, SettingTextCallsThrough) {
TestBinaryMessenger messenger;
TestPlatformHandler platform_handler(&messenger);
Expand Down
20 changes: 20 additions & 0 deletions shell/platform/windows/platform_handler_win32.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/string_conversion.h"

static constexpr char kValueKey[] = "value";

namespace flutter {

namespace {
Expand Down Expand Up @@ -229,6 +231,24 @@ void PlatformHandlerWin32::GetPlainText(
result->Success(document);
}

void PlatformHandlerWin32::GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
ScopedClipboard clipboard;
if (!clipboard.Open(std::get<HWND>(*view_->GetRenderTarget()))) {
rapidjson::Document error_code;
error_code.SetInt(::GetLastError());
result->Error(kClipboardError, "Unable to open clipboard", error_code);
return;
}

rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
document.AddMember(rapidjson::Value(kValueKey, allocator),
rapidjson::Value(clipboard.HasString()), allocator);
result->Success(document);
}

void PlatformHandlerWin32::SetPlainText(
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/windows/platform_handler_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class PlatformHandlerWin32 : public PlatformHandler {
void GetPlainText(std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) override;

// |PlatformHandler|
void GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;

// |PlatformHandler|
void SetPlainText(
const std::string& text,
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/windows/platform_handler_winuwp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ void PlatformHandlerWinUwp::GetPlainText(
result->NotImplemented();
}

void PlatformHandlerWinUwp::GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
// TODO: Implement. See https://github.com/flutter/flutter/issues/70214.
result->NotImplemented();
}

void PlatformHandlerWinUwp::SetPlainText(
const std::string& text,
std::unique_ptr<MethodResult<rapidjson::Document>> result) {
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/windows/platform_handler_winuwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class PlatformHandlerWinUwp : public PlatformHandler {
void GetPlainText(std::unique_ptr<MethodResult<rapidjson::Document>> result,
std::string_view key) override;

// |PlatformHandler|
void GetHasStrings(
std::unique_ptr<MethodResult<rapidjson::Document>> result) override;

// |PlatformHandler|
void SetPlainText(
const std::string& text,
Expand Down