From 70a0f5e670725a5f583181fd32ff10d021b0a5fc Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 4 Aug 2021 10:38:13 -0700 Subject: [PATCH 01/18] Working hasStrings on windows --- shell/platform/windows/platform_handler.cc | 134 ++--- shell/platform/windows/platform_handler.h | 115 ++-- .../windows/platform_handler_win32.cc | 523 +++++++++--------- .../platform/windows/platform_handler_win32.h | 89 +-- .../windows/platform_handler_winuwp.h | 90 +-- 5 files changed, 497 insertions(+), 454 deletions(-) diff --git a/shell/platform/windows/platform_handler.cc b/shell/platform/windows/platform_handler.cc index 85254e5653a62..fc2372a3cfed4 100644 --- a/shell/platform/windows/platform_handler.cc +++ b/shell/platform/windows/platform_handler.cc @@ -1,62 +1,72 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/windows/platform_handler.h" - -#include "flutter/shell/platform/common/json_method_codec.h" - -static constexpr char kChannelName[] = "flutter/platform"; - -static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"; -static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; - -static constexpr char kTextPlainFormat[] = "text/plain"; -static constexpr char kTextKey[] = "text"; - -static constexpr char kUnknownClipboardFormatMessage[] = - "Unknown clipboard format"; - -namespace flutter { - -PlatformHandler::PlatformHandler(BinaryMessenger* messenger) - : channel_(std::make_unique>( - messenger, - kChannelName, - &JsonMethodCodec::GetInstance())) { - channel_->SetMethodCallHandler( - [this](const MethodCall& call, - std::unique_ptr> result) { - HandleMethodCall(call, std::move(result)); - }); -} - -PlatformHandler::~PlatformHandler() = default; - -void PlatformHandler::HandleMethodCall( - const MethodCall& method_call, - std::unique_ptr> result) { - const std::string& method = method_call.method_name(); - if (method.compare(kGetClipboardDataMethod) == 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; - } - GetPlainText(std::move(result), kTextKey); - } else if (method.compare(kSetClipboardDataMethod) == 0) { - const rapidjson::Value& document = *method_call.arguments(); - rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); - if (itr == document.MemberEnd()) { - result->Error(kClipboardError, kUnknownClipboardFormatMessage); - return; - } - SetPlainText(itr->value.GetString(), std::move(result)); - } else { - result->NotImplemented(); - } -} - -} // namespace flutter +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/windows/platform_handler.h" + +#include "flutter/shell/platform/common/json_method_codec.h" + +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 kTextKey[] = "text"; + +static constexpr char kUnknownClipboardFormatMessage[] = + "Unknown clipboard format"; + +namespace flutter { + +PlatformHandler::PlatformHandler(BinaryMessenger* messenger) + : channel_(std::make_unique>( + messenger, + kChannelName, + &JsonMethodCodec::GetInstance())) { + channel_->SetMethodCallHandler( + [this](const MethodCall& call, + std::unique_ptr> result) { + HandleMethodCall(call, std::move(result)); + }); +} + +PlatformHandler::~PlatformHandler() = default; + +void PlatformHandler::HandleMethodCall( + const MethodCall& method_call, + std::unique_ptr> result) { + const std::string& method = method_call.method_name(); + if (method.compare(kGetClipboardDataMethod) == 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; + } + 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; + } + HasStrings(std::move(result)); + } else if (method.compare(kSetClipboardDataMethod) == 0) { + const rapidjson::Value& document = *method_call.arguments(); + rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); + if (itr == document.MemberEnd()) { + result->Error(kClipboardError, kUnknownClipboardFormatMessage); + return; + } + SetPlainText(itr->value.GetString(), std::move(result)); + } else { + result->NotImplemented(); + } +} + +} // namespace flutter diff --git a/shell/platform/windows/platform_handler.h b/shell/platform/windows/platform_handler.h index dc16e8b779921..577de9258ecbd 100644 --- a/shell/platform/windows/platform_handler.h +++ b/shell/platform/windows/platform_handler.h @@ -1,55 +1,60 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "rapidjson/document.h" - -namespace flutter { - -class FlutterWindowsView; - -// Handler for internal system channels. -class PlatformHandler { - public: - explicit PlatformHandler(BinaryMessenger* messenger); - - virtual ~PlatformHandler(); - - // Creates a new platform handler using the given messenger and view. - static std::unique_ptr Create(BinaryMessenger* messenger, - FlutterWindowsView* view); - - protected: - // Gets plain text from the clipboard and provides it to |result| as the - // value in a dictionary with the given |key|. - virtual void GetPlainText( - std::unique_ptr> result, - std::string_view key) = 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( - const std::string& text, - std::unique_ptr> result) = 0; - - // A error type to use for error responses. - static constexpr char kClipboardError[] = "Clipboard error"; - - private: - // Called when a method is called on |channel_|; - void HandleMethodCall( - const MethodCall& method_call, - std::unique_ptr> result); - - // The MethodChannel used for communication with the Flutter engine. - std::unique_ptr> channel_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "rapidjson/document.h" + +namespace flutter { + +class FlutterWindowsView; + +// Handler for internal system channels. +class PlatformHandler { + public: + explicit PlatformHandler(BinaryMessenger* messenger); + + virtual ~PlatformHandler(); + + // Creates a new platform handler using the given messenger and view. + static std::unique_ptr Create(BinaryMessenger* messenger, + FlutterWindowsView* view); + + protected: + // Gets plain text from the clipboard and provides it to |result| as the + // value in a dictionary with the given |key|. + virtual void GetPlainText( + std::unique_ptr> 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 HasStrings( + std::unique_ptr> 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( + const std::string& text, + std::unique_ptr> result) = 0; + + // A error type to use for error responses. + static constexpr char kClipboardError[] = "Clipboard error"; + + private: + // Called when a method is called on |channel_|; + void HandleMethodCall( + const MethodCall& method_call, + std::unique_ptr> result); + + // The MethodChannel used for communication with the Flutter engine. + std::unique_ptr> channel_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ diff --git a/shell/platform/windows/platform_handler_win32.cc b/shell/platform/windows/platform_handler_win32.cc index b4f0ea351601e..949c4a82a21df 100644 --- a/shell/platform/windows/platform_handler_win32.cc +++ b/shell/platform/windows/platform_handler_win32.cc @@ -1,251 +1,272 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/windows/platform_handler_win32.h" - -#include - -#include -#include -#include - -#include "flutter/shell/platform/windows/flutter_windows_view.h" -#include "flutter/shell/platform/windows/string_conversion.h" - -namespace flutter { - -namespace { - -// A scoped wrapper for GlobalAlloc/GlobalFree. -class ScopedGlobalMemory { - public: - // Allocates |bytes| bytes of global memory with the given flags. - ScopedGlobalMemory(unsigned int flags, size_t bytes) { - memory_ = ::GlobalAlloc(flags, bytes); - if (!memory_) { - std::cerr << "Unable to allocate global memory: " << ::GetLastError(); - } - } - - ~ScopedGlobalMemory() { - if (memory_) { - if (::GlobalFree(memory_) != nullptr) { - std::cerr << "Failed to free global allocation: " << ::GetLastError(); - } - } - } - - // Prevent copying. - ScopedGlobalMemory(ScopedGlobalMemory const&) = delete; - ScopedGlobalMemory& operator=(ScopedGlobalMemory const&) = delete; - - // Returns the memory pointer, which will be nullptr if allocation failed. - void* get() { return memory_; } - - void* release() { - void* memory = memory_; - memory_ = nullptr; - return memory; - } - - private: - HGLOBAL memory_; -}; - -// A scoped wrapper for GlobalLock/GlobalUnlock. -class ScopedGlobalLock { - public: - // Attempts to acquire a global lock on |memory| for the life of this object. - ScopedGlobalLock(HGLOBAL memory) { - source_ = memory; - if (memory) { - locked_memory_ = ::GlobalLock(memory); - if (!locked_memory_) { - std::cerr << "Unable to acquire global lock: " << ::GetLastError(); - } - } - } - - ~ScopedGlobalLock() { - if (locked_memory_) { - if (!::GlobalUnlock(source_)) { - DWORD error = ::GetLastError(); - if (error != NO_ERROR) { - std::cerr << "Unable to release global lock: " << ::GetLastError(); - } - } - } - } - - // Prevent copying. - ScopedGlobalLock(ScopedGlobalLock const&) = delete; - ScopedGlobalLock& operator=(ScopedGlobalLock const&) = delete; - - // Returns the locked memory pointer, which will be nullptr if acquiring the - // lock failed. - void* get() { return locked_memory_; } - - private: - HGLOBAL source_; - void* locked_memory_; -}; - -// A Clipboard wrapper that automatically closes the clipboard when it goes out -// of scope. -class ScopedClipboard { - public: - ScopedClipboard(); - ~ScopedClipboard(); - - // Prevent copying. - ScopedClipboard(ScopedClipboard const&) = delete; - ScopedClipboard& operator=(ScopedClipboard const&) = delete; - - // Attempts to open the clipboard for the given window, returning true if - // successful. - bool Open(HWND window); - - // Returns true if there is string data available to get. - bool HasString(); - - // Returns string data from the clipboard. - // - // If getting a string fails, returns no value. Get error information with - // ::GetLastError(). - // - // Open(...) must have succeeded to call this method. - std::optional GetString(); - - // Sets the string content of the clipboard, returning true on success. - // - // On failure, get error information with ::GetLastError(). - // - // Open(...) must have succeeded to call this method. - bool SetString(const std::wstring string); - - private: - bool opened_ = false; -}; - -ScopedClipboard::ScopedClipboard() {} - -ScopedClipboard::~ScopedClipboard() { - if (opened_) { - ::CloseClipboard(); - } -} - -bool ScopedClipboard::Open(HWND window) { - opened_ = ::OpenClipboard(window); - return opened_; -} - -bool ScopedClipboard::HasString() { - // Allow either plain text format, since getting data will auto-interpolate. - return ::IsClipboardFormatAvailable(CF_UNICODETEXT) || - ::IsClipboardFormatAvailable(CF_TEXT); -} - -std::optional ScopedClipboard::GetString() { - assert(opened_); - - HANDLE data = ::GetClipboardData(CF_UNICODETEXT); - if (data == nullptr) { - return std::nullopt; - } - ScopedGlobalLock locked_data(data); - if (!locked_data.get()) { - return std::nullopt; - } - return std::optional(static_cast(locked_data.get())); -} - -bool ScopedClipboard::SetString(const std::wstring string) { - assert(opened_); - if (!::EmptyClipboard()) { - return false; - } - size_t null_terminated_byte_count = - sizeof(decltype(string)::traits_type::char_type) * (string.size() + 1); - ScopedGlobalMemory destination_memory(GMEM_MOVEABLE, - null_terminated_byte_count); - ScopedGlobalLock locked_memory(destination_memory.get()); - if (!locked_memory.get()) { - return false; - } - memcpy(locked_memory.get(), string.c_str(), null_terminated_byte_count); - if (!::SetClipboardData(CF_UNICODETEXT, locked_memory.get())) { - return false; - } - // The clipboard now owns the global memory. - destination_memory.release(); - return true; -} - -} // namespace - -// static -std::unique_ptr PlatformHandler::Create( - BinaryMessenger* messenger, - FlutterWindowsView* view) { - return std::make_unique(messenger, view); -} - -PlatformHandlerWin32::PlatformHandlerWin32(BinaryMessenger* messenger, - FlutterWindowsView* view) - : PlatformHandler(messenger), view_(view) {} - -PlatformHandlerWin32::~PlatformHandlerWin32() = default; - -void PlatformHandlerWin32::GetPlainText( - std::unique_ptr> result, - std::string_view key) { - ScopedClipboard clipboard; - if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to open clipboard", error_code); - return; - } - if (!clipboard.HasString()) { - result->Success(rapidjson::Document()); - return; - } - std::optional clipboard_string = clipboard.GetString(); - if (!clipboard_string) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to get clipboard data", error_code); - return; - } - - rapidjson::Document document; - document.SetObject(); - rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); - document.AddMember( - rapidjson::Value(key.data(), allocator), - rapidjson::Value(Utf8FromUtf16(*clipboard_string), allocator), allocator); - result->Success(document); -} - -void PlatformHandlerWin32::SetPlainText( - const std::string& text, - std::unique_ptr> result) { - ScopedClipboard clipboard; - if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to open clipboard", error_code); - return; - } - if (!clipboard.SetString(Utf16FromUtf8(text))) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to set clipboard data", error_code); - return; - } - result->Success(); -} - -} // namespace flutter +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/windows/platform_handler_win32.h" + +#include + +#include +#include +#include + +#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 { + +// A scoped wrapper for GlobalAlloc/GlobalFree. +class ScopedGlobalMemory { + public: + // Allocates |bytes| bytes of global memory with the given flags. + ScopedGlobalMemory(unsigned int flags, size_t bytes) { + memory_ = ::GlobalAlloc(flags, bytes); + if (!memory_) { + std::cerr << "Unable to allocate global memory: " << ::GetLastError(); + } + } + + ~ScopedGlobalMemory() { + if (memory_) { + if (::GlobalFree(memory_) != nullptr) { + std::cerr << "Failed to free global allocation: " << ::GetLastError(); + } + } + } + + // Prevent copying. + ScopedGlobalMemory(ScopedGlobalMemory const&) = delete; + ScopedGlobalMemory& operator=(ScopedGlobalMemory const&) = delete; + + // Returns the memory pointer, which will be nullptr if allocation failed. + void* get() { return memory_; } + + void* release() { + void* memory = memory_; + memory_ = nullptr; + return memory; + } + + private: + HGLOBAL memory_; +}; + +// A scoped wrapper for GlobalLock/GlobalUnlock. +class ScopedGlobalLock { + public: + // Attempts to acquire a global lock on |memory| for the life of this object. + ScopedGlobalLock(HGLOBAL memory) { + source_ = memory; + if (memory) { + locked_memory_ = ::GlobalLock(memory); + if (!locked_memory_) { + std::cerr << "Unable to acquire global lock: " << ::GetLastError(); + } + } + } + + ~ScopedGlobalLock() { + if (locked_memory_) { + if (!::GlobalUnlock(source_)) { + DWORD error = ::GetLastError(); + if (error != NO_ERROR) { + std::cerr << "Unable to release global lock: " << ::GetLastError(); + } + } + } + } + + // Prevent copying. + ScopedGlobalLock(ScopedGlobalLock const&) = delete; + ScopedGlobalLock& operator=(ScopedGlobalLock const&) = delete; + + // Returns the locked memory pointer, which will be nullptr if acquiring the + // lock failed. + void* get() { return locked_memory_; } + + private: + HGLOBAL source_; + void* locked_memory_; +}; + +// A Clipboard wrapper that automatically closes the clipboard when it goes out +// of scope. +class ScopedClipboard { + public: + ScopedClipboard(); + ~ScopedClipboard(); + + // Prevent copying. + ScopedClipboard(ScopedClipboard const&) = delete; + ScopedClipboard& operator=(ScopedClipboard const&) = delete; + + // Attempts to open the clipboard for the given window, returning true if + // successful. + bool Open(HWND window); + + // Returns true if there is string data available to get. + bool HasString(); + + // Returns string data from the clipboard. + // + // If getting a string fails, returns no value. Get error information with + // ::GetLastError(). + // + // Open(...) must have succeeded to call this method. + std::optional GetString(); + + // Sets the string content of the clipboard, returning true on success. + // + // On failure, get error information with ::GetLastError(). + // + // Open(...) must have succeeded to call this method. + bool SetString(const std::wstring string); + + private: + bool opened_ = false; +}; + +ScopedClipboard::ScopedClipboard() {} + +ScopedClipboard::~ScopedClipboard() { + if (opened_) { + ::CloseClipboard(); + } +} + +bool ScopedClipboard::Open(HWND window) { + opened_ = ::OpenClipboard(window); + return opened_; +} + +bool ScopedClipboard::HasString() { + // Allow either plain text format, since getting data will auto-interpolate. + return ::IsClipboardFormatAvailable(CF_UNICODETEXT) || + ::IsClipboardFormatAvailable(CF_TEXT); +} + +std::optional ScopedClipboard::GetString() { + assert(opened_); + + HANDLE data = ::GetClipboardData(CF_UNICODETEXT); + if (data == nullptr) { + return std::nullopt; + } + ScopedGlobalLock locked_data(data); + if (!locked_data.get()) { + return std::nullopt; + } + return std::optional(static_cast(locked_data.get())); +} + +bool ScopedClipboard::SetString(const std::wstring string) { + assert(opened_); + if (!::EmptyClipboard()) { + return false; + } + size_t null_terminated_byte_count = + sizeof(decltype(string)::traits_type::char_type) * (string.size() + 1); + ScopedGlobalMemory destination_memory(GMEM_MOVEABLE, + null_terminated_byte_count); + ScopedGlobalLock locked_memory(destination_memory.get()); + if (!locked_memory.get()) { + return false; + } + memcpy(locked_memory.get(), string.c_str(), null_terminated_byte_count); + if (!::SetClipboardData(CF_UNICODETEXT, locked_memory.get())) { + return false; + } + // The clipboard now owns the global memory. + destination_memory.release(); + return true; +} + +} // namespace + +// static +std::unique_ptr PlatformHandler::Create( + BinaryMessenger* messenger, + FlutterWindowsView* view) { + return std::make_unique(messenger, view); +} + +PlatformHandlerWin32::PlatformHandlerWin32(BinaryMessenger* messenger, + FlutterWindowsView* view) + : PlatformHandler(messenger), view_(view) {} + +PlatformHandlerWin32::~PlatformHandlerWin32() = default; + +void PlatformHandlerWin32::GetPlainText( + std::unique_ptr> result, + std::string_view key) { + ScopedClipboard clipboard; + if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to open clipboard", error_code); + return; + } + if (!clipboard.HasString()) { + result->Success(rapidjson::Document()); + return; + } + std::optional clipboard_string = clipboard.GetString(); + if (!clipboard_string) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to get clipboard data", error_code); + return; + } + + rapidjson::Document document; + document.SetObject(); + rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); + document.AddMember( + rapidjson::Value(key.data(), allocator), + rapidjson::Value(Utf8FromUtf16(*clipboard_string), allocator), allocator); + result->Success(document); +} + +void PlatformHandlerWin32::HasStrings( + std::unique_ptr> result) { + ScopedClipboard clipboard; + if (!clipboard.Open(std::get(*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> result) { + ScopedClipboard clipboard; + if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to open clipboard", error_code); + return; + } + if (!clipboard.SetString(Utf16FromUtf8(text))) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to set clipboard data", error_code); + return; + } + result->Success(); +} + +} // namespace flutter diff --git a/shell/platform/windows/platform_handler_win32.h b/shell/platform/windows/platform_handler_win32.h index 2005f75b84ee5..207810cc9396c 100644 --- a/shell/platform/windows/platform_handler_win32.h +++ b/shell/platform/windows/platform_handler_win32.h @@ -1,43 +1,46 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "flutter/shell/platform/windows/flutter_windows_view.h" -#include "flutter/shell/platform/windows/platform_handler.h" -#include "rapidjson/document.h" - -namespace flutter { - -class FlutterWindowsView; - -// Win32 implementation of PlatformHandler. -class PlatformHandlerWin32 : public PlatformHandler { - public: - explicit PlatformHandlerWin32(BinaryMessenger* messenger, - FlutterWindowsView* view); - - virtual ~PlatformHandlerWin32(); - - protected: - // |PlatformHandler| - void GetPlainText(std::unique_ptr> result, - std::string_view key) override; - - // |PlatformHandler| - void SetPlainText( - const std::string& text, - std::unique_ptr> result) override; - - private: - // A reference to the Flutter view. - FlutterWindowsView* view_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "flutter/shell/platform/windows/flutter_windows_view.h" +#include "flutter/shell/platform/windows/platform_handler.h" +#include "rapidjson/document.h" + +namespace flutter { + +class FlutterWindowsView; + +// Win32 implementation of PlatformHandler. +class PlatformHandlerWin32 : public PlatformHandler { + public: + explicit PlatformHandlerWin32(BinaryMessenger* messenger, + FlutterWindowsView* view); + + virtual ~PlatformHandlerWin32(); + + protected: + // |PlatformHandler| + void GetPlainText(std::unique_ptr> result, + std::string_view key) override; + protected: + // |PlatformHandler| + void HasStrings(std::unique_ptr> result) override; + + // |PlatformHandler| + void SetPlainText( + const std::string& text, + std::unique_ptr> result) override; + + private: + // A reference to the Flutter view. + FlutterWindowsView* view_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ diff --git a/shell/platform/windows/platform_handler_winuwp.h b/shell/platform/windows/platform_handler_winuwp.h index e067988c1415d..fb4cd42de9808 100644 --- a/shell/platform/windows/platform_handler_winuwp.h +++ b/shell/platform/windows/platform_handler_winuwp.h @@ -1,43 +1,47 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "flutter/shell/platform/windows/flutter_windows_view.h" -#include "flutter/shell/platform/windows/platform_handler.h" -#include "rapidjson/document.h" - -namespace flutter { - -class FlutterWindowsView; - -// UWP implementation of PlatformHandler. -class PlatformHandlerWinUwp : public PlatformHandler { - public: - explicit PlatformHandlerWinUwp(BinaryMessenger* messenger, - FlutterWindowsView* view); - - virtual ~PlatformHandlerWinUwp(); - - protected: - // |PlatformHandler| - void GetPlainText(std::unique_ptr> result, - std::string_view key) override; - - // |PlatformHandler| - void SetPlainText( - const std::string& text, - std::unique_ptr> result) override; - - private: - // A reference to the Flutter view. - FlutterWindowsView* view_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "flutter/shell/platform/windows/flutter_windows_view.h" +#include "flutter/shell/platform/windows/platform_handler.h" +#include "rapidjson/document.h" + +namespace flutter { + +class FlutterWindowsView; + +// UWP implementation of PlatformHandler. +class PlatformHandlerWinUwp : public PlatformHandler { + public: + explicit PlatformHandlerWinUwp(BinaryMessenger* messenger, + FlutterWindowsView* view); + + virtual ~PlatformHandlerWinUwp(); + + protected: + // |PlatformHandler| + void GetPlainText(std::unique_ptr> result, + std::string_view key) override; + + // |PlatformHandler| + void HasStrings(std::unique_ptr> result, + std::string_view key) override; + + // |PlatformHandler| + void SetPlainText( + const std::string& text, + std::unique_ptr> result) override; + + private: + // A reference to the Flutter view. + FlutterWindowsView* view_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ From c9d633a520c2dda59ac63fe85e9a75aaa03f8dbe Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 4 Aug 2021 10:59:12 -0700 Subject: [PATCH 02/18] Analyzer --- shell/platform/windows/platform_handler_win32.cc | 5 ++--- shell/platform/windows/platform_handler_win32.h | 5 +++-- shell/platform/windows/platform_handler_winuwp.h | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/platform/windows/platform_handler_win32.cc b/shell/platform/windows/platform_handler_win32.cc index 949c4a82a21df..761fa38f4ec4f 100644 --- a/shell/platform/windows/platform_handler_win32.cc +++ b/shell/platform/windows/platform_handler_win32.cc @@ -244,9 +244,8 @@ void PlatformHandlerWin32::HasStrings( rapidjson::Document document; document.SetObject(); rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); - document.AddMember( - rapidjson::Value(kValueKey, allocator), - rapidjson::Value(clipboard.HasString()), allocator); + document.AddMember(rapidjson::Value(kValueKey, allocator), + rapidjson::Value(clipboard.HasString()), allocator); result->Success(document); } diff --git a/shell/platform/windows/platform_handler_win32.h b/shell/platform/windows/platform_handler_win32.h index 207810cc9396c..d482aa2dcd596 100644 --- a/shell/platform/windows/platform_handler_win32.h +++ b/shell/platform/windows/platform_handler_win32.h @@ -27,9 +27,10 @@ class PlatformHandlerWin32 : public PlatformHandler { // |PlatformHandler| void GetPlainText(std::unique_ptr> result, std::string_view key) override; - protected: + // |PlatformHandler| - void HasStrings(std::unique_ptr> result) override; + void HasStrings( + std::unique_ptr> result) override; // |PlatformHandler| void SetPlainText( diff --git a/shell/platform/windows/platform_handler_winuwp.h b/shell/platform/windows/platform_handler_winuwp.h index fb4cd42de9808..999235e063f92 100644 --- a/shell/platform/windows/platform_handler_winuwp.h +++ b/shell/platform/windows/platform_handler_winuwp.h @@ -27,10 +27,10 @@ class PlatformHandlerWinUwp : public PlatformHandler { // |PlatformHandler| void GetPlainText(std::unique_ptr> result, std::string_view key) override; - + // |PlatformHandler| void HasStrings(std::unique_ptr> result, - std::string_view key) override; + std::string_view key) override; // |PlatformHandler| void SetPlainText( From 39990f33f589ebe63a1722873dd91d7f5d8f2c30 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 4 Aug 2021 11:32:50 -0700 Subject: [PATCH 03/18] Blind attempt at tests since I'm unable to run them right now --- .../windows/platform_handler_unittests.cc | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index c427ac080c7d2..38f8f631f6982 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -22,6 +22,7 @@ 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"; @@ -103,6 +104,52 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { })); } +TEST(PlatformHandler, HasStringsCallsThrough) { + TestBinaryMessenger messenger; + TestPlatformHandler platform_handler(&messenger); + + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack(kTextPlainFormat, allocator); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(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, HasStrings) + .WillByDefault( + [](std::unique_ptr> result, + auto key) { result->NotImplemented(); }); + + EXPECT_CALL(platform_handler, HasStrings(_, ::testing::StrEq("text"))); + EXPECT_TRUE(messenger.SimulateEngineMessage( + kChannelName, encoded->data(), encoded->size(), + [](const uint8_t* reply, size_t reply_size) {})); +} + +TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { + TestBinaryMessenger messenger; + TestPlatformHandler platform_handler(&messenger); + + auto args = std::make_unique(rapidjson::kArrayType); + auto& allocator = args->GetAllocator(); + args->PushBack("madeup/contenttype", allocator); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(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); From c3060cd91d8673e6622813f18f246f47a54b1ff7 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 4 Aug 2021 21:49:57 -0700 Subject: [PATCH 04/18] Analyzer fix --- shell/platform/windows/platform_handler_winuwp.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/shell/platform/windows/platform_handler_winuwp.cc b/shell/platform/windows/platform_handler_winuwp.cc index e984d1fc9fd79..90df390ceb162 100644 --- a/shell/platform/windows/platform_handler_winuwp.cc +++ b/shell/platform/windows/platform_handler_winuwp.cc @@ -28,6 +28,12 @@ void PlatformHandlerWinUwp::GetPlainText( result->NotImplemented(); } +void PlatformHandlerWinUwp::HasStrings( + std::unique_ptr> result) { + // TODO: Implement. See https://github.com/flutter/flutter/issues/70214. + result->NotImplemented(); +} + void PlatformHandlerWinUwp::SetPlainText( const std::string& text, std::unique_ptr> result) { From 83af63377bba287827cc2def1a02bf3bb95f2beb Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 4 Aug 2021 21:53:25 -0700 Subject: [PATCH 05/18] Fix windows newlines --- shell/platform/windows/platform_handler.cc | 144 ++--- shell/platform/windows/platform_handler.h | 120 ++-- .../windows/platform_handler_win32.cc | 542 +++++++++--------- .../platform/windows/platform_handler_win32.h | 94 +-- .../windows/platform_handler_winuwp.h | 94 +-- 5 files changed, 497 insertions(+), 497 deletions(-) diff --git a/shell/platform/windows/platform_handler.cc b/shell/platform/windows/platform_handler.cc index fc2372a3cfed4..619bd9f039544 100644 --- a/shell/platform/windows/platform_handler.cc +++ b/shell/platform/windows/platform_handler.cc @@ -1,72 +1,72 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/windows/platform_handler.h" - -#include "flutter/shell/platform/common/json_method_codec.h" - -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 kTextKey[] = "text"; - -static constexpr char kUnknownClipboardFormatMessage[] = - "Unknown clipboard format"; - -namespace flutter { - -PlatformHandler::PlatformHandler(BinaryMessenger* messenger) - : channel_(std::make_unique>( - messenger, - kChannelName, - &JsonMethodCodec::GetInstance())) { - channel_->SetMethodCallHandler( - [this](const MethodCall& call, - std::unique_ptr> result) { - HandleMethodCall(call, std::move(result)); - }); -} - -PlatformHandler::~PlatformHandler() = default; - -void PlatformHandler::HandleMethodCall( - const MethodCall& method_call, - std::unique_ptr> result) { - const std::string& method = method_call.method_name(); - if (method.compare(kGetClipboardDataMethod) == 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; - } - 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; - } - HasStrings(std::move(result)); - } else if (method.compare(kSetClipboardDataMethod) == 0) { - const rapidjson::Value& document = *method_call.arguments(); - rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); - if (itr == document.MemberEnd()) { - result->Error(kClipboardError, kUnknownClipboardFormatMessage); - return; - } - SetPlainText(itr->value.GetString(), std::move(result)); - } else { - result->NotImplemented(); - } -} - -} // namespace flutter +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/windows/platform_handler.h" + +#include "flutter/shell/platform/common/json_method_codec.h" + +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 kTextKey[] = "text"; + +static constexpr char kUnknownClipboardFormatMessage[] = + "Unknown clipboard format"; + +namespace flutter { + +PlatformHandler::PlatformHandler(BinaryMessenger* messenger) + : channel_(std::make_unique>( + messenger, + kChannelName, + &JsonMethodCodec::GetInstance())) { + channel_->SetMethodCallHandler( + [this](const MethodCall& call, + std::unique_ptr> result) { + HandleMethodCall(call, std::move(result)); + }); +} + +PlatformHandler::~PlatformHandler() = default; + +void PlatformHandler::HandleMethodCall( + const MethodCall& method_call, + std::unique_ptr> result) { + const std::string& method = method_call.method_name(); + if (method.compare(kGetClipboardDataMethod) == 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; + } + 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; + } + HasStrings(std::move(result)); + } else if (method.compare(kSetClipboardDataMethod) == 0) { + const rapidjson::Value& document = *method_call.arguments(); + rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); + if (itr == document.MemberEnd()) { + result->Error(kClipboardError, kUnknownClipboardFormatMessage); + return; + } + SetPlainText(itr->value.GetString(), std::move(result)); + } else { + result->NotImplemented(); + } +} + +} // namespace flutter diff --git a/shell/platform/windows/platform_handler.h b/shell/platform/windows/platform_handler.h index 577de9258ecbd..f02194aa2da33 100644 --- a/shell/platform/windows/platform_handler.h +++ b/shell/platform/windows/platform_handler.h @@ -1,60 +1,60 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "rapidjson/document.h" - -namespace flutter { - -class FlutterWindowsView; - -// Handler for internal system channels. -class PlatformHandler { - public: - explicit PlatformHandler(BinaryMessenger* messenger); - - virtual ~PlatformHandler(); - - // Creates a new platform handler using the given messenger and view. - static std::unique_ptr Create(BinaryMessenger* messenger, - FlutterWindowsView* view); - - protected: - // Gets plain text from the clipboard and provides it to |result| as the - // value in a dictionary with the given |key|. - virtual void GetPlainText( - std::unique_ptr> 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 HasStrings( - std::unique_ptr> 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( - const std::string& text, - std::unique_ptr> result) = 0; - - // A error type to use for error responses. - static constexpr char kClipboardError[] = "Clipboard error"; - - private: - // Called when a method is called on |channel_|; - void HandleMethodCall( - const MethodCall& method_call, - std::unique_ptr> result); - - // The MethodChannel used for communication with the Flutter engine. - std::unique_ptr> channel_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "rapidjson/document.h" + +namespace flutter { + +class FlutterWindowsView; + +// Handler for internal system channels. +class PlatformHandler { + public: + explicit PlatformHandler(BinaryMessenger* messenger); + + virtual ~PlatformHandler(); + + // Creates a new platform handler using the given messenger and view. + static std::unique_ptr Create(BinaryMessenger* messenger, + FlutterWindowsView* view); + + protected: + // Gets plain text from the clipboard and provides it to |result| as the + // value in a dictionary with the given |key|. + virtual void GetPlainText( + std::unique_ptr> 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 HasStrings( + std::unique_ptr> 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( + const std::string& text, + std::unique_ptr> result) = 0; + + // A error type to use for error responses. + static constexpr char kClipboardError[] = "Clipboard error"; + + private: + // Called when a method is called on |channel_|; + void HandleMethodCall( + const MethodCall& method_call, + std::unique_ptr> result); + + // The MethodChannel used for communication with the Flutter engine. + std::unique_ptr> channel_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_H_ diff --git a/shell/platform/windows/platform_handler_win32.cc b/shell/platform/windows/platform_handler_win32.cc index 761fa38f4ec4f..9a615c027a2f3 100644 --- a/shell/platform/windows/platform_handler_win32.cc +++ b/shell/platform/windows/platform_handler_win32.cc @@ -1,271 +1,271 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/windows/platform_handler_win32.h" - -#include - -#include -#include -#include - -#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 { - -// A scoped wrapper for GlobalAlloc/GlobalFree. -class ScopedGlobalMemory { - public: - // Allocates |bytes| bytes of global memory with the given flags. - ScopedGlobalMemory(unsigned int flags, size_t bytes) { - memory_ = ::GlobalAlloc(flags, bytes); - if (!memory_) { - std::cerr << "Unable to allocate global memory: " << ::GetLastError(); - } - } - - ~ScopedGlobalMemory() { - if (memory_) { - if (::GlobalFree(memory_) != nullptr) { - std::cerr << "Failed to free global allocation: " << ::GetLastError(); - } - } - } - - // Prevent copying. - ScopedGlobalMemory(ScopedGlobalMemory const&) = delete; - ScopedGlobalMemory& operator=(ScopedGlobalMemory const&) = delete; - - // Returns the memory pointer, which will be nullptr if allocation failed. - void* get() { return memory_; } - - void* release() { - void* memory = memory_; - memory_ = nullptr; - return memory; - } - - private: - HGLOBAL memory_; -}; - -// A scoped wrapper for GlobalLock/GlobalUnlock. -class ScopedGlobalLock { - public: - // Attempts to acquire a global lock on |memory| for the life of this object. - ScopedGlobalLock(HGLOBAL memory) { - source_ = memory; - if (memory) { - locked_memory_ = ::GlobalLock(memory); - if (!locked_memory_) { - std::cerr << "Unable to acquire global lock: " << ::GetLastError(); - } - } - } - - ~ScopedGlobalLock() { - if (locked_memory_) { - if (!::GlobalUnlock(source_)) { - DWORD error = ::GetLastError(); - if (error != NO_ERROR) { - std::cerr << "Unable to release global lock: " << ::GetLastError(); - } - } - } - } - - // Prevent copying. - ScopedGlobalLock(ScopedGlobalLock const&) = delete; - ScopedGlobalLock& operator=(ScopedGlobalLock const&) = delete; - - // Returns the locked memory pointer, which will be nullptr if acquiring the - // lock failed. - void* get() { return locked_memory_; } - - private: - HGLOBAL source_; - void* locked_memory_; -}; - -// A Clipboard wrapper that automatically closes the clipboard when it goes out -// of scope. -class ScopedClipboard { - public: - ScopedClipboard(); - ~ScopedClipboard(); - - // Prevent copying. - ScopedClipboard(ScopedClipboard const&) = delete; - ScopedClipboard& operator=(ScopedClipboard const&) = delete; - - // Attempts to open the clipboard for the given window, returning true if - // successful. - bool Open(HWND window); - - // Returns true if there is string data available to get. - bool HasString(); - - // Returns string data from the clipboard. - // - // If getting a string fails, returns no value. Get error information with - // ::GetLastError(). - // - // Open(...) must have succeeded to call this method. - std::optional GetString(); - - // Sets the string content of the clipboard, returning true on success. - // - // On failure, get error information with ::GetLastError(). - // - // Open(...) must have succeeded to call this method. - bool SetString(const std::wstring string); - - private: - bool opened_ = false; -}; - -ScopedClipboard::ScopedClipboard() {} - -ScopedClipboard::~ScopedClipboard() { - if (opened_) { - ::CloseClipboard(); - } -} - -bool ScopedClipboard::Open(HWND window) { - opened_ = ::OpenClipboard(window); - return opened_; -} - -bool ScopedClipboard::HasString() { - // Allow either plain text format, since getting data will auto-interpolate. - return ::IsClipboardFormatAvailable(CF_UNICODETEXT) || - ::IsClipboardFormatAvailable(CF_TEXT); -} - -std::optional ScopedClipboard::GetString() { - assert(opened_); - - HANDLE data = ::GetClipboardData(CF_UNICODETEXT); - if (data == nullptr) { - return std::nullopt; - } - ScopedGlobalLock locked_data(data); - if (!locked_data.get()) { - return std::nullopt; - } - return std::optional(static_cast(locked_data.get())); -} - -bool ScopedClipboard::SetString(const std::wstring string) { - assert(opened_); - if (!::EmptyClipboard()) { - return false; - } - size_t null_terminated_byte_count = - sizeof(decltype(string)::traits_type::char_type) * (string.size() + 1); - ScopedGlobalMemory destination_memory(GMEM_MOVEABLE, - null_terminated_byte_count); - ScopedGlobalLock locked_memory(destination_memory.get()); - if (!locked_memory.get()) { - return false; - } - memcpy(locked_memory.get(), string.c_str(), null_terminated_byte_count); - if (!::SetClipboardData(CF_UNICODETEXT, locked_memory.get())) { - return false; - } - // The clipboard now owns the global memory. - destination_memory.release(); - return true; -} - -} // namespace - -// static -std::unique_ptr PlatformHandler::Create( - BinaryMessenger* messenger, - FlutterWindowsView* view) { - return std::make_unique(messenger, view); -} - -PlatformHandlerWin32::PlatformHandlerWin32(BinaryMessenger* messenger, - FlutterWindowsView* view) - : PlatformHandler(messenger), view_(view) {} - -PlatformHandlerWin32::~PlatformHandlerWin32() = default; - -void PlatformHandlerWin32::GetPlainText( - std::unique_ptr> result, - std::string_view key) { - ScopedClipboard clipboard; - if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to open clipboard", error_code); - return; - } - if (!clipboard.HasString()) { - result->Success(rapidjson::Document()); - return; - } - std::optional clipboard_string = clipboard.GetString(); - if (!clipboard_string) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to get clipboard data", error_code); - return; - } - - rapidjson::Document document; - document.SetObject(); - rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); - document.AddMember( - rapidjson::Value(key.data(), allocator), - rapidjson::Value(Utf8FromUtf16(*clipboard_string), allocator), allocator); - result->Success(document); -} - -void PlatformHandlerWin32::HasStrings( - std::unique_ptr> result) { - ScopedClipboard clipboard; - if (!clipboard.Open(std::get(*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> result) { - ScopedClipboard clipboard; - if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to open clipboard", error_code); - return; - } - if (!clipboard.SetString(Utf16FromUtf8(text))) { - rapidjson::Document error_code; - error_code.SetInt(::GetLastError()); - result->Error(kClipboardError, "Unable to set clipboard data", error_code); - return; - } - result->Success(); -} - -} // namespace flutter +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/windows/platform_handler_win32.h" + +#include + +#include +#include +#include + +#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 { + +// A scoped wrapper for GlobalAlloc/GlobalFree. +class ScopedGlobalMemory { + public: + // Allocates |bytes| bytes of global memory with the given flags. + ScopedGlobalMemory(unsigned int flags, size_t bytes) { + memory_ = ::GlobalAlloc(flags, bytes); + if (!memory_) { + std::cerr << "Unable to allocate global memory: " << ::GetLastError(); + } + } + + ~ScopedGlobalMemory() { + if (memory_) { + if (::GlobalFree(memory_) != nullptr) { + std::cerr << "Failed to free global allocation: " << ::GetLastError(); + } + } + } + + // Prevent copying. + ScopedGlobalMemory(ScopedGlobalMemory const&) = delete; + ScopedGlobalMemory& operator=(ScopedGlobalMemory const&) = delete; + + // Returns the memory pointer, which will be nullptr if allocation failed. + void* get() { return memory_; } + + void* release() { + void* memory = memory_; + memory_ = nullptr; + return memory; + } + + private: + HGLOBAL memory_; +}; + +// A scoped wrapper for GlobalLock/GlobalUnlock. +class ScopedGlobalLock { + public: + // Attempts to acquire a global lock on |memory| for the life of this object. + ScopedGlobalLock(HGLOBAL memory) { + source_ = memory; + if (memory) { + locked_memory_ = ::GlobalLock(memory); + if (!locked_memory_) { + std::cerr << "Unable to acquire global lock: " << ::GetLastError(); + } + } + } + + ~ScopedGlobalLock() { + if (locked_memory_) { + if (!::GlobalUnlock(source_)) { + DWORD error = ::GetLastError(); + if (error != NO_ERROR) { + std::cerr << "Unable to release global lock: " << ::GetLastError(); + } + } + } + } + + // Prevent copying. + ScopedGlobalLock(ScopedGlobalLock const&) = delete; + ScopedGlobalLock& operator=(ScopedGlobalLock const&) = delete; + + // Returns the locked memory pointer, which will be nullptr if acquiring the + // lock failed. + void* get() { return locked_memory_; } + + private: + HGLOBAL source_; + void* locked_memory_; +}; + +// A Clipboard wrapper that automatically closes the clipboard when it goes out +// of scope. +class ScopedClipboard { + public: + ScopedClipboard(); + ~ScopedClipboard(); + + // Prevent copying. + ScopedClipboard(ScopedClipboard const&) = delete; + ScopedClipboard& operator=(ScopedClipboard const&) = delete; + + // Attempts to open the clipboard for the given window, returning true if + // successful. + bool Open(HWND window); + + // Returns true if there is string data available to get. + bool HasString(); + + // Returns string data from the clipboard. + // + // If getting a string fails, returns no value. Get error information with + // ::GetLastError(). + // + // Open(...) must have succeeded to call this method. + std::optional GetString(); + + // Sets the string content of the clipboard, returning true on success. + // + // On failure, get error information with ::GetLastError(). + // + // Open(...) must have succeeded to call this method. + bool SetString(const std::wstring string); + + private: + bool opened_ = false; +}; + +ScopedClipboard::ScopedClipboard() {} + +ScopedClipboard::~ScopedClipboard() { + if (opened_) { + ::CloseClipboard(); + } +} + +bool ScopedClipboard::Open(HWND window) { + opened_ = ::OpenClipboard(window); + return opened_; +} + +bool ScopedClipboard::HasString() { + // Allow either plain text format, since getting data will auto-interpolate. + return ::IsClipboardFormatAvailable(CF_UNICODETEXT) || + ::IsClipboardFormatAvailable(CF_TEXT); +} + +std::optional ScopedClipboard::GetString() { + assert(opened_); + + HANDLE data = ::GetClipboardData(CF_UNICODETEXT); + if (data == nullptr) { + return std::nullopt; + } + ScopedGlobalLock locked_data(data); + if (!locked_data.get()) { + return std::nullopt; + } + return std::optional(static_cast(locked_data.get())); +} + +bool ScopedClipboard::SetString(const std::wstring string) { + assert(opened_); + if (!::EmptyClipboard()) { + return false; + } + size_t null_terminated_byte_count = + sizeof(decltype(string)::traits_type::char_type) * (string.size() + 1); + ScopedGlobalMemory destination_memory(GMEM_MOVEABLE, + null_terminated_byte_count); + ScopedGlobalLock locked_memory(destination_memory.get()); + if (!locked_memory.get()) { + return false; + } + memcpy(locked_memory.get(), string.c_str(), null_terminated_byte_count); + if (!::SetClipboardData(CF_UNICODETEXT, locked_memory.get())) { + return false; + } + // The clipboard now owns the global memory. + destination_memory.release(); + return true; +} + +} // namespace + +// static +std::unique_ptr PlatformHandler::Create( + BinaryMessenger* messenger, + FlutterWindowsView* view) { + return std::make_unique(messenger, view); +} + +PlatformHandlerWin32::PlatformHandlerWin32(BinaryMessenger* messenger, + FlutterWindowsView* view) + : PlatformHandler(messenger), view_(view) {} + +PlatformHandlerWin32::~PlatformHandlerWin32() = default; + +void PlatformHandlerWin32::GetPlainText( + std::unique_ptr> result, + std::string_view key) { + ScopedClipboard clipboard; + if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to open clipboard", error_code); + return; + } + if (!clipboard.HasString()) { + result->Success(rapidjson::Document()); + return; + } + std::optional clipboard_string = clipboard.GetString(); + if (!clipboard_string) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to get clipboard data", error_code); + return; + } + + rapidjson::Document document; + document.SetObject(); + rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); + document.AddMember( + rapidjson::Value(key.data(), allocator), + rapidjson::Value(Utf8FromUtf16(*clipboard_string), allocator), allocator); + result->Success(document); +} + +void PlatformHandlerWin32::HasStrings( + std::unique_ptr> result) { + ScopedClipboard clipboard; + if (!clipboard.Open(std::get(*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> result) { + ScopedClipboard clipboard; + if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to open clipboard", error_code); + return; + } + if (!clipboard.SetString(Utf16FromUtf8(text))) { + rapidjson::Document error_code; + error_code.SetInt(::GetLastError()); + result->Error(kClipboardError, "Unable to set clipboard data", error_code); + return; + } + result->Success(); +} + +} // namespace flutter diff --git a/shell/platform/windows/platform_handler_win32.h b/shell/platform/windows/platform_handler_win32.h index d482aa2dcd596..c3fe9fd9c04ba 100644 --- a/shell/platform/windows/platform_handler_win32.h +++ b/shell/platform/windows/platform_handler_win32.h @@ -1,47 +1,47 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "flutter/shell/platform/windows/flutter_windows_view.h" -#include "flutter/shell/platform/windows/platform_handler.h" -#include "rapidjson/document.h" - -namespace flutter { - -class FlutterWindowsView; - -// Win32 implementation of PlatformHandler. -class PlatformHandlerWin32 : public PlatformHandler { - public: - explicit PlatformHandlerWin32(BinaryMessenger* messenger, - FlutterWindowsView* view); - - virtual ~PlatformHandlerWin32(); - - protected: - // |PlatformHandler| - void GetPlainText(std::unique_ptr> result, - std::string_view key) override; - - // |PlatformHandler| - void HasStrings( - std::unique_ptr> result) override; - - // |PlatformHandler| - void SetPlainText( - const std::string& text, - std::unique_ptr> result) override; - - private: - // A reference to the Flutter view. - FlutterWindowsView* view_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "flutter/shell/platform/windows/flutter_windows_view.h" +#include "flutter/shell/platform/windows/platform_handler.h" +#include "rapidjson/document.h" + +namespace flutter { + +class FlutterWindowsView; + +// Win32 implementation of PlatformHandler. +class PlatformHandlerWin32 : public PlatformHandler { + public: + explicit PlatformHandlerWin32(BinaryMessenger* messenger, + FlutterWindowsView* view); + + virtual ~PlatformHandlerWin32(); + + protected: + // |PlatformHandler| + void GetPlainText(std::unique_ptr> result, + std::string_view key) override; + + // |PlatformHandler| + void HasStrings( + std::unique_ptr> result) override; + + // |PlatformHandler| + void SetPlainText( + const std::string& text, + std::unique_ptr> result) override; + + private: + // A reference to the Flutter view. + FlutterWindowsView* view_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WIN32_H_ diff --git a/shell/platform/windows/platform_handler_winuwp.h b/shell/platform/windows/platform_handler_winuwp.h index 999235e063f92..dbc2e98947020 100644 --- a/shell/platform/windows/platform_handler_winuwp.h +++ b/shell/platform/windows/platform_handler_winuwp.h @@ -1,47 +1,47 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" -#include "flutter/shell/platform/windows/flutter_windows_view.h" -#include "flutter/shell/platform/windows/platform_handler.h" -#include "rapidjson/document.h" - -namespace flutter { - -class FlutterWindowsView; - -// UWP implementation of PlatformHandler. -class PlatformHandlerWinUwp : public PlatformHandler { - public: - explicit PlatformHandlerWinUwp(BinaryMessenger* messenger, - FlutterWindowsView* view); - - virtual ~PlatformHandlerWinUwp(); - - protected: - // |PlatformHandler| - void GetPlainText(std::unique_ptr> result, - std::string_view key) override; - - // |PlatformHandler| - void HasStrings(std::unique_ptr> result, - std::string_view key) override; - - // |PlatformHandler| - void SetPlainText( - const std::string& text, - std::unique_ptr> result) override; - - private: - // A reference to the Flutter view. - FlutterWindowsView* view_; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ +#define FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h" +#include "flutter/shell/platform/windows/flutter_windows_view.h" +#include "flutter/shell/platform/windows/platform_handler.h" +#include "rapidjson/document.h" + +namespace flutter { + +class FlutterWindowsView; + +// UWP implementation of PlatformHandler. +class PlatformHandlerWinUwp : public PlatformHandler { + public: + explicit PlatformHandlerWinUwp(BinaryMessenger* messenger, + FlutterWindowsView* view); + + virtual ~PlatformHandlerWinUwp(); + + protected: + // |PlatformHandler| + void GetPlainText(std::unique_ptr> result, + std::string_view key) override; + + // |PlatformHandler| + void HasStrings(std::unique_ptr> result, + std::string_view key) override; + + // |PlatformHandler| + void SetPlainText( + const std::string& text, + std::unique_ptr> result) override; + + private: + // A reference to the Flutter view. + FlutterWindowsView* view_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_PLATFORM_HANDLER_WINUWP_H_ From b7c562eda706f5db4aa4f7e4505d5fc11d8afaad Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Wed, 4 Aug 2021 22:14:18 -0700 Subject: [PATCH 06/18] Analyzer fix --- shell/platform/windows/platform_handler_winuwp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/windows/platform_handler_winuwp.h b/shell/platform/windows/platform_handler_winuwp.h index dbc2e98947020..d4a415445b3d3 100644 --- a/shell/platform/windows/platform_handler_winuwp.h +++ b/shell/platform/windows/platform_handler_winuwp.h @@ -29,8 +29,8 @@ class PlatformHandlerWinUwp : public PlatformHandler { std::string_view key) override; // |PlatformHandler| - void HasStrings(std::unique_ptr> result, - std::string_view key) override; + void HasStrings( + std::unique_ptr> result) override; // |PlatformHandler| void SetPlainText( From 64c5751107823e89b52f19bd6883549ace8d7c74 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Thu, 5 Aug 2021 13:08:20 -0700 Subject: [PATCH 07/18] Add unittests file to build, it must have been missed accidentally --- shell/platform/windows/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index 4ed1b0a8e7cfe..9ac5b97b3fb10 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -243,6 +243,7 @@ executable("flutter_windows_unittests") { "keyboard_key_channel_handler_unittests.cc", "keyboard_key_embedder_handler_unittests.cc", "keyboard_key_handler_unittests.cc", + "platform_handler_unittests.cc", "testing/flutter_window_win32_test.cc", "testing/flutter_window_win32_test.h", "testing/mock_window_binding_handler.cc", From 85528fd95ad1635d18a49b959a447d19043ed060 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Mon, 9 Aug 2021 12:59:40 -0700 Subject: [PATCH 08/18] Tests compiling but not passsing --- .../windows/platform_handler_unittests.cc | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index 38f8f631f6982..a3ace334d5c32 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -36,13 +36,16 @@ class TestPlatformHandler : public PlatformHandler { virtual ~TestPlatformHandler() {} + // |PlatformHandler| MOCK_METHOD2(GetPlainText, - void(std::unique_ptr>, - const char*)); + void(std::unique_ptr> result, + std::string_view key)); + MOCK_METHOD1(HasStrings, + void(std::unique_ptr> result)); MOCK_METHOD2(SetPlainText, - void(const std::string&, - std::unique_ptr>)); + void(const std::string& text, + std::unique_ptr> result)); }; // Mock result to inspect results of PlatformHandler calls. @@ -119,10 +122,11 @@ TEST(PlatformHandler, HasStringsCallsThrough) { // on destruction about leaking. ON_CALL(platform_handler, HasStrings) .WillByDefault( - [](std::unique_ptr> result, - auto key) { result->NotImplemented(); }); + [](std::unique_ptr> result) { + result->NotImplemented(); + }); - EXPECT_CALL(platform_handler, HasStrings(_, ::testing::StrEq("text"))); + EXPECT_CALL(platform_handler, HasStrings(_)); EXPECT_TRUE(messenger.SimulateEngineMessage( kChannelName, encoded->data(), encoded->size(), [](const uint8_t* reply, size_t reply_size) {})); From a640fcc5d4435479d6eb25e158ebe6cbfa2d7676 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Mon, 9 Aug 2021 14:37:38 -0700 Subject: [PATCH 09/18] Analyzer fixes --- shell/platform/windows/platform_handler_unittests.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index a3ace334d5c32..3ce54f97dca12 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -6,7 +6,6 @@ #include -#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" #include "flutter/shell/platform/common/json_method_codec.h" #include "flutter/shell/platform/windows/testing/test_binary_messenger.h" #include "gmock/gmock.h" @@ -36,7 +35,6 @@ class TestPlatformHandler : public PlatformHandler { virtual ~TestPlatformHandler() {} - // |PlatformHandler| MOCK_METHOD2(GetPlainText, void(std::unique_ptr> result, @@ -123,8 +121,8 @@ TEST(PlatformHandler, HasStringsCallsThrough) { ON_CALL(platform_handler, HasStrings) .WillByDefault( [](std::unique_ptr> result) { - result->NotImplemented(); - }); + result->NotImplemented(); + }); EXPECT_CALL(platform_handler, HasStrings(_)); EXPECT_TRUE(messenger.SimulateEngineMessage( From 61e2fa5ed12e0753d715c60e15997a62e7a0aa65 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 20 Aug 2021 10:47:31 -0700 Subject: [PATCH 10/18] Old tests pass thanks to work in fix-platform-handler-unittests branch --- .../windows/platform_handler_unittests.cc | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index 3ce54f97dca12..db91422dd8cb0 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -25,6 +25,7 @@ static constexpr char kHasStringsClipboardMethod[] = "Clipboard.hasStrings"; static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"; static constexpr char kTextPlainFormat[] = "text/plain"; +static constexpr char kFakeContentType[] = "text/madeupcontenttype"; // Test implementation of PlatformHandler to allow testing the PlatformHandler // logic. @@ -37,13 +38,13 @@ class TestPlatformHandler : public PlatformHandler { // |PlatformHandler| MOCK_METHOD2(GetPlainText, - void(std::unique_ptr> result, + void(std::unique_ptr>, std::string_view key)); MOCK_METHOD1(HasStrings, - void(std::unique_ptr> result)); + void(std::unique_ptr>)); MOCK_METHOD2(SetPlainText, - void(const std::string& text, - std::unique_ptr> result)); + void(const std::string&, + std::unique_ptr>)); }; // Mock result to inspect results of PlatformHandler calls. @@ -63,12 +64,12 @@ TEST(PlatformHandler, GettingTextCallsThrough) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - auto args = std::make_unique(rapidjson::kArrayType); - auto& allocator = args->GetAllocator(); - args->PushBack(kTextPlainFormat, allocator); - auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( - MethodCall(kGetClipboardDataMethod, - std::move(args))); + std::ostringstream jsonStringStream; + jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod << "\",\"args\":\"" << kTextPlainFormat << "\"}"; + std::string jsonString = jsonStringStream.str(); + unsigned char json [256]; + std::copy(jsonString.begin(), jsonString.end(), json); + unsigned char* data = &json[0]; // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. @@ -79,7 +80,7 @@ TEST(PlatformHandler, GettingTextCallsThrough) { EXPECT_CALL(platform_handler, GetPlainText(_, ::testing::StrEq("text"))); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, encoded->data(), encoded->size(), + kChannelName, data, strlen((char*)data), [](const uint8_t* reply, size_t reply_size) {})); } @@ -87,24 +88,25 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - auto args = std::make_unique(rapidjson::kArrayType); - auto& allocator = args->GetAllocator(); - args->PushBack("madeup/contenttype", allocator); - auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( - MethodCall(kGetClipboardDataMethod, - std::move(args))); + std::ostringstream jsonStringStream; + jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod << "\",\"args\":\"" << kFakeContentType << "\"}"; + std::string jsonString = jsonStringStream.str(); + unsigned char json [256]; + std::copy(jsonString.begin(), jsonString.end(), json); + unsigned char* data = &json[0]; MockMethodResult result; // Requsting an unknow content type is an error. EXPECT_CALL(result, ErrorInternal(_, _, _)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, encoded->data(), encoded->size(), + kChannelName, data, strlen((char*)data), [&](const uint8_t* reply, size_t reply_size) { JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope( reply, reply_size, &result); })); } +/* TEST(PlatformHandler, HasStringsCallsThrough) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); @@ -151,6 +153,7 @@ TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { reply, reply_size, &result); })); } +*/ TEST(PlatformHandler, SettingTextCallsThrough) { TestBinaryMessenger messenger; From 5a35ed26cfee3f648f5e642b986368b1dcb77412 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 20 Aug 2021 10:50:46 -0700 Subject: [PATCH 11/18] First new test passes too --- .../windows/platform_handler_unittests.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index db91422dd8cb0..63dca20b168a1 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -106,17 +106,16 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { })); } -/* TEST(PlatformHandler, HasStringsCallsThrough) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - auto args = std::make_unique(rapidjson::kArrayType); - auto& allocator = args->GetAllocator(); - args->PushBack(kTextPlainFormat, allocator); - auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( - MethodCall(kHasStringsClipboardMethod, - std::move(args))); + std::ostringstream jsonStringStream; + jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod << "\",\"args\":\"" << kTextPlainFormat << "\"}"; + std::string jsonString = jsonStringStream.str(); + unsigned char json [256]; + std::copy(jsonString.begin(), jsonString.end(), json); + unsigned char* data = &json[0]; // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. @@ -128,10 +127,11 @@ TEST(PlatformHandler, HasStringsCallsThrough) { EXPECT_CALL(platform_handler, HasStrings(_)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, encoded->data(), encoded->size(), + kChannelName, data, strlen((char*)data), [](const uint8_t* reply, size_t reply_size) {})); } +/* TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); From 6cef1f202729966184b18a22dabe5f525d60dcae Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 20 Aug 2021 10:52:42 -0700 Subject: [PATCH 12/18] All tests pass --- .../windows/platform_handler_unittests.cc | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index 63dca20b168a1..b4e63be91fa30 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -131,29 +131,27 @@ TEST(PlatformHandler, HasStringsCallsThrough) { [](const uint8_t* reply, size_t reply_size) {})); } -/* TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - auto args = std::make_unique(rapidjson::kArrayType); - auto& allocator = args->GetAllocator(); - args->PushBack("madeup/contenttype", allocator); - auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( - MethodCall(kHasStringsClipboardMethod, - std::move(args))); + std::ostringstream jsonStringStream; + jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod << "\",\"args\":\"" << kFakeContentType << "\"}"; + std::string jsonString = jsonStringStream.str(); + unsigned char json [256]; + std::copy(jsonString.begin(), jsonString.end(), json); + unsigned char* data = &json[0]; MockMethodResult result; // Requsting an unknow content type is an error. EXPECT_CALL(result, ErrorInternal(_, _, _)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, encoded->data(), encoded->size(), + kChannelName, data, strlen((char*)data), [&](const uint8_t* reply, size_t reply_size) { JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope( reply, reply_size, &result); })); } -*/ TEST(PlatformHandler, SettingTextCallsThrough) { TestBinaryMessenger messenger; From 1c6744aa4346bd698b354c2935040e2cff85f0a9 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 20 Aug 2021 10:54:00 -0700 Subject: [PATCH 13/18] Attempt at formatting --- shell/platform/windows/platform_handler_unittests.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index b4e63be91fa30..08001f209f3b8 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -65,7 +65,8 @@ TEST(PlatformHandler, GettingTextCallsThrough) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod << "\",\"args\":\"" << kTextPlainFormat << "\"}"; + jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod + << "\",\"args\":\"" << kTextPlainFormat << "\"}"; std::string jsonString = jsonStringStream.str(); unsigned char json [256]; std::copy(jsonString.begin(), jsonString.end(), json); @@ -89,7 +90,8 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod << "\",\"args\":\"" << kFakeContentType << "\"}"; + jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod + << "\",\"args\":\"" << kFakeContentType << "\"}"; std::string jsonString = jsonStringStream.str(); unsigned char json [256]; std::copy(jsonString.begin(), jsonString.end(), json); @@ -111,7 +113,8 @@ TEST(PlatformHandler, HasStringsCallsThrough) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod << "\",\"args\":\"" << kTextPlainFormat << "\"}"; + jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod + << "\",\"args\":\"" << kTextPlainFormat << "\"}"; std::string jsonString = jsonStringStream.str(); unsigned char json [256]; std::copy(jsonString.begin(), jsonString.end(), json); @@ -136,7 +139,8 @@ TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod << "\",\"args\":\"" << kFakeContentType << "\"}"; + jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod + << "\",\"args\":\"" << kFakeContentType << "\"}"; std::string jsonString = jsonStringStream.str(); unsigned char json [256]; std::copy(jsonString.begin(), jsonString.end(), json); From e4587a8ac90cc3e901ad408ce977287ce1a69d1c Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 20 Aug 2021 11:21:00 -0700 Subject: [PATCH 14/18] Analyzer fixes --- .../windows/platform_handler_unittests.cc | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index 08001f209f3b8..d350f15cb1ef5 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -65,10 +65,10 @@ TEST(PlatformHandler, GettingTextCallsThrough) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod - << "\",\"args\":\"" << kTextPlainFormat << "\"}"; + jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod + << "\",\"args\":\"" << kTextPlainFormat << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json [256]; + unsigned char json[256]; std::copy(jsonString.begin(), jsonString.end(), json); unsigned char* data = &json[0]; @@ -90,10 +90,10 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod - << "\",\"args\":\"" << kFakeContentType << "\"}"; + jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod + << "\",\"args\":\"" << kFakeContentType << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json [256]; + unsigned char json[256]; std::copy(jsonString.begin(), jsonString.end(), json); unsigned char* data = &json[0]; @@ -113,10 +113,10 @@ TEST(PlatformHandler, HasStringsCallsThrough) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod - << "\",\"args\":\"" << kTextPlainFormat << "\"}"; + jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod + << "\",\"args\":\"" << kTextPlainFormat << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json [256]; + unsigned char json[256]; std::copy(jsonString.begin(), jsonString.end(), json); unsigned char* data = &json[0]; @@ -139,10 +139,10 @@ TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { TestPlatformHandler platform_handler(&messenger); std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod - << "\",\"args\":\"" << kFakeContentType << "\"}"; + jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod + << "\",\"args\":\"" << kFakeContentType << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json [256]; + unsigned char json[256]; std::copy(jsonString.begin(), jsonString.end(), json); unsigned char* data = &json[0]; From 4c5afb3d9c105a51fcbb4f40e0da2e06a75622cb Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 20 Aug 2021 17:08:14 -0700 Subject: [PATCH 15/18] Fix my poor string conversions --- .../windows/platform_handler_unittests.cc | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index d350f15cb1ef5..6585f64a52bdd 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -68,9 +68,6 @@ TEST(PlatformHandler, GettingTextCallsThrough) { jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod << "\",\"args\":\"" << kTextPlainFormat << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json[256]; - std::copy(jsonString.begin(), jsonString.end(), json); - unsigned char* data = &json[0]; // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. @@ -81,8 +78,8 @@ TEST(PlatformHandler, GettingTextCallsThrough) { EXPECT_CALL(platform_handler, GetPlainText(_, ::testing::StrEq("text"))); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, data, strlen((char*)data), - [](const uint8_t* reply, size_t reply_size) {})); + kChannelName, reinterpret_cast(jsonString.c_str()), + jsonString.size(), [](const uint8_t* reply, size_t reply_size) {})); } TEST(PlatformHandler, RejectsGettingUnknownTypes) { @@ -93,16 +90,13 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod << "\",\"args\":\"" << kFakeContentType << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json[256]; - std::copy(jsonString.begin(), jsonString.end(), json); - unsigned char* data = &json[0]; MockMethodResult result; // Requsting an unknow content type is an error. EXPECT_CALL(result, ErrorInternal(_, _, _)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, data, strlen((char*)data), - [&](const uint8_t* reply, size_t reply_size) { + kChannelName, reinterpret_cast(jsonString.c_str()), + jsonString.size(), [&](const uint8_t* reply, size_t reply_size) { JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope( reply, reply_size, &result); })); @@ -116,9 +110,6 @@ TEST(PlatformHandler, HasStringsCallsThrough) { jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod << "\",\"args\":\"" << kTextPlainFormat << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json[256]; - std::copy(jsonString.begin(), jsonString.end(), json); - unsigned char* data = &json[0]; // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. @@ -130,8 +121,8 @@ TEST(PlatformHandler, HasStringsCallsThrough) { EXPECT_CALL(platform_handler, HasStrings(_)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, data, strlen((char*)data), - [](const uint8_t* reply, size_t reply_size) {})); + kChannelName, reinterpret_cast(jsonString.c_str()), + jsonString.size(), [](const uint8_t* reply, size_t reply_size) {})); } TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { @@ -142,16 +133,13 @@ TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod << "\",\"args\":\"" << kFakeContentType << "\"}"; std::string jsonString = jsonStringStream.str(); - unsigned char json[256]; - std::copy(jsonString.begin(), jsonString.end(), json); - unsigned char* data = &json[0]; MockMethodResult result; // Requsting an unknow content type is an error. EXPECT_CALL(result, ErrorInternal(_, _, _)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, data, strlen((char*)data), - [&](const uint8_t* reply, size_t reply_size) { + kChannelName, reinterpret_cast(jsonString.c_str()), + jsonString.size(), [&](const uint8_t* reply, size_t reply_size) { JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope( reply, reply_size, &result); })); From b81e8f3ae7b377697f73e759b78fc2324dc2ab6e Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Mon, 23 Aug 2021 10:15:18 -0700 Subject: [PATCH 16/18] Rename HasStrings to GetHasStrings --- shell/platform/windows/platform_handler.cc | 2 +- shell/platform/windows/platform_handler.h | 2 +- shell/platform/windows/platform_handler_unittests.cc | 10 +++++----- shell/platform/windows/platform_handler_win32.cc | 2 +- shell/platform/windows/platform_handler_win32.h | 2 +- shell/platform/windows/platform_handler_winuwp.cc | 2 +- shell/platform/windows/platform_handler_winuwp.h | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/shell/platform/windows/platform_handler.cc b/shell/platform/windows/platform_handler.cc index 619bd9f039544..3b6d87aebf002 100644 --- a/shell/platform/windows/platform_handler.cc +++ b/shell/platform/windows/platform_handler.cc @@ -55,7 +55,7 @@ void PlatformHandler::HandleMethodCall( result->Error(kClipboardError, kUnknownClipboardFormatMessage); return; } - HasStrings(std::move(result)); + GetHasStrings(std::move(result)); } else if (method.compare(kSetClipboardDataMethod) == 0) { const rapidjson::Value& document = *method_call.arguments(); rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); diff --git a/shell/platform/windows/platform_handler.h b/shell/platform/windows/platform_handler.h index f02194aa2da33..a4ceaa837abdf 100644 --- a/shell/platform/windows/platform_handler.h +++ b/shell/platform/windows/platform_handler.h @@ -33,7 +33,7 @@ class PlatformHandler { // 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 HasStrings( + virtual void GetHasStrings( std::unique_ptr> result) = 0; // Sets the clipboard's plain text to |text|, and reports the result (either diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index 6585f64a52bdd..58c166f54624b 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -40,7 +40,7 @@ class TestPlatformHandler : public PlatformHandler { MOCK_METHOD2(GetPlainText, void(std::unique_ptr>, std::string_view key)); - MOCK_METHOD1(HasStrings, + MOCK_METHOD1(GetHasStrings, void(std::unique_ptr>)); MOCK_METHOD2(SetPlainText, void(const std::string&, @@ -102,7 +102,7 @@ TEST(PlatformHandler, RejectsGettingUnknownTypes) { })); } -TEST(PlatformHandler, HasStringsCallsThrough) { +TEST(PlatformHandler, GetHasStringsCallsThrough) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); @@ -113,19 +113,19 @@ TEST(PlatformHandler, HasStringsCallsThrough) { // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. - ON_CALL(platform_handler, HasStrings) + ON_CALL(platform_handler, GetHasStrings) .WillByDefault( [](std::unique_ptr> result) { result->NotImplemented(); }); - EXPECT_CALL(platform_handler, HasStrings(_)); + EXPECT_CALL(platform_handler, GetHasStrings(_)); EXPECT_TRUE(messenger.SimulateEngineMessage( kChannelName, reinterpret_cast(jsonString.c_str()), jsonString.size(), [](const uint8_t* reply, size_t reply_size) {})); } -TEST(PlatformHandler, RejectsHasStringsOnUnknownTypes) { +TEST(PlatformHandler, RejectsGetHasStringsOnUnknownTypes) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); diff --git a/shell/platform/windows/platform_handler_win32.cc b/shell/platform/windows/platform_handler_win32.cc index 9a615c027a2f3..1985f85ed27b5 100644 --- a/shell/platform/windows/platform_handler_win32.cc +++ b/shell/platform/windows/platform_handler_win32.cc @@ -231,7 +231,7 @@ void PlatformHandlerWin32::GetPlainText( result->Success(document); } -void PlatformHandlerWin32::HasStrings( +void PlatformHandlerWin32::GetHasStrings( std::unique_ptr> result) { ScopedClipboard clipboard; if (!clipboard.Open(std::get(*view_->GetRenderTarget()))) { diff --git a/shell/platform/windows/platform_handler_win32.h b/shell/platform/windows/platform_handler_win32.h index c3fe9fd9c04ba..8477830f12b4e 100644 --- a/shell/platform/windows/platform_handler_win32.h +++ b/shell/platform/windows/platform_handler_win32.h @@ -29,7 +29,7 @@ class PlatformHandlerWin32 : public PlatformHandler { std::string_view key) override; // |PlatformHandler| - void HasStrings( + void GetHasStrings( std::unique_ptr> result) override; // |PlatformHandler| diff --git a/shell/platform/windows/platform_handler_winuwp.cc b/shell/platform/windows/platform_handler_winuwp.cc index 90df390ceb162..e3ea7e5b9566a 100644 --- a/shell/platform/windows/platform_handler_winuwp.cc +++ b/shell/platform/windows/platform_handler_winuwp.cc @@ -28,7 +28,7 @@ void PlatformHandlerWinUwp::GetPlainText( result->NotImplemented(); } -void PlatformHandlerWinUwp::HasStrings( +void PlatformHandlerWinUwp::GetHasStrings( std::unique_ptr> result) { // TODO: Implement. See https://github.com/flutter/flutter/issues/70214. result->NotImplemented(); diff --git a/shell/platform/windows/platform_handler_winuwp.h b/shell/platform/windows/platform_handler_winuwp.h index d4a415445b3d3..cae4f5ae65fab 100644 --- a/shell/platform/windows/platform_handler_winuwp.h +++ b/shell/platform/windows/platform_handler_winuwp.h @@ -29,7 +29,7 @@ class PlatformHandlerWinUwp : public PlatformHandler { std::string_view key) override; // |PlatformHandler| - void HasStrings( + void GetHasStrings( std::unique_ptr> result) override; // |PlatformHandler| From 620e7aa3dd648a21cafae6ef8275ff90cb32c826 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Tue, 24 Aug 2021 15:48:19 -0700 Subject: [PATCH 17/18] Use original MethodCall format with correct type --- .../windows/platform_handler_unittests.cc | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index 58c166f54624b..f55c0e9ab28f1 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -64,10 +64,12 @@ TEST(PlatformHandler, GettingTextCallsThrough) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod - << "\",\"args\":\"" << kTextPlainFormat << "\"}"; - std::string jsonString = jsonStringStream.str(); + auto args = std::make_unique(rapidjson::kStringType); + auto& allocator = args->GetAllocator(); + args->SetString(kTextPlainFormat); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kGetClipboardDataMethod, + std::move(args))); // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. @@ -77,26 +79,29 @@ TEST(PlatformHandler, GettingTextCallsThrough) { auto key) { result->NotImplemented(); }); EXPECT_CALL(platform_handler, GetPlainText(_, ::testing::StrEq("text"))); + printf("justin make the call"); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, reinterpret_cast(jsonString.c_str()), - jsonString.size(), [](const uint8_t* reply, size_t reply_size) {})); + kChannelName, encoded->data(), encoded->size(), + [](const uint8_t* reply, size_t reply_size) {})); } TEST(PlatformHandler, RejectsGettingUnknownTypes) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kGetClipboardDataMethod - << "\",\"args\":\"" << kFakeContentType << "\"}"; - std::string jsonString = jsonStringStream.str(); + auto args = std::make_unique(rapidjson::kStringType); + auto& allocator = args->GetAllocator(); + args->SetString(kFakeContentType); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kGetClipboardDataMethod, + std::move(args))); MockMethodResult result; // Requsting an unknow content type is an error. EXPECT_CALL(result, ErrorInternal(_, _, _)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, reinterpret_cast(jsonString.c_str()), - jsonString.size(), [&](const uint8_t* reply, size_t reply_size) { + kChannelName, encoded->data(), encoded->size(), + [&](const uint8_t* reply, size_t reply_size) { JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope( reply, reply_size, &result); })); @@ -106,10 +111,12 @@ TEST(PlatformHandler, GetHasStringsCallsThrough) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod - << "\",\"args\":\"" << kTextPlainFormat << "\"}"; - std::string jsonString = jsonStringStream.str(); + auto args = std::make_unique(rapidjson::kStringType); + auto& allocator = args->GetAllocator(); + args->SetString(kTextPlainFormat); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kHasStringsClipboardMethod, + std::move(args))); // Set up a handler to call a response on |result| so that it doesn't log // on destruction about leaking. @@ -121,25 +128,27 @@ TEST(PlatformHandler, GetHasStringsCallsThrough) { EXPECT_CALL(platform_handler, GetHasStrings(_)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, reinterpret_cast(jsonString.c_str()), - jsonString.size(), [](const uint8_t* reply, size_t reply_size) {})); + kChannelName, encoded->data(), encoded->size(), + [](const uint8_t* reply, size_t reply_size) {})); } TEST(PlatformHandler, RejectsGetHasStringsOnUnknownTypes) { TestBinaryMessenger messenger; TestPlatformHandler platform_handler(&messenger); - std::ostringstream jsonStringStream; - jsonStringStream << "{\"method\":\"" << kHasStringsClipboardMethod - << "\",\"args\":\"" << kFakeContentType << "\"}"; - std::string jsonString = jsonStringStream.str(); + auto args = std::make_unique(rapidjson::kStringType); + auto& allocator = args->GetAllocator(); + args->SetString(kFakeContentType); + auto encoded = JsonMethodCodec::GetInstance().EncodeMethodCall( + MethodCall(kHasStringsClipboardMethod, + std::move(args))); MockMethodResult result; // Requsting an unknow content type is an error. EXPECT_CALL(result, ErrorInternal(_, _, _)); EXPECT_TRUE(messenger.SimulateEngineMessage( - kChannelName, reinterpret_cast(jsonString.c_str()), - jsonString.size(), [&](const uint8_t* reply, size_t reply_size) { + kChannelName, encoded->data(), encoded->size(), + [&](const uint8_t* reply, size_t reply_size) { JsonMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope( reply, reply_size, &result); })); From b06f2c80e57d4cc7a796901532791de2b317cf9b Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Tue, 24 Aug 2021 16:27:33 -0700 Subject: [PATCH 18/18] Remove accidental printf! --- shell/platform/windows/platform_handler_unittests.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/windows/platform_handler_unittests.cc b/shell/platform/windows/platform_handler_unittests.cc index f55c0e9ab28f1..6340cb6aa0a62 100644 --- a/shell/platform/windows/platform_handler_unittests.cc +++ b/shell/platform/windows/platform_handler_unittests.cc @@ -79,7 +79,6 @@ TEST(PlatformHandler, GettingTextCallsThrough) { auto key) { result->NotImplemented(); }); EXPECT_CALL(platform_handler, GetPlainText(_, ::testing::StrEq("text"))); - printf("justin make the call"); EXPECT_TRUE(messenger.SimulateEngineMessage( kChannelName, encoded->data(), encoded->size(), [](const uint8_t* reply, size_t reply_size) {}));