From 9d4d636bbd355e4b713db1936502727e4053cc48 Mon Sep 17 00:00:00 2001 From: "Piotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics" Date: Wed, 14 Apr 2021 10:18:21 +0200 Subject: [PATCH 1/2] [Clipboard] setData and getData implementations added and also removed code for hasStrings, as this API is not present in documentation. [Before] Clipboard.setData and Clipboard.getData feature was not implemented [After] Naive implementation of clipboard shared only inside Flutter application and only via Clipboard API. Such decision was done because of supporting partial functionality on all profiles. CBHM API is supported only on mobile and currently it is not possible to support it in other way. Dart code below is able to set/get internal clipboard data: ClipboardData cd = ClipboardData(text: 'some text'); Clipboard.setData(cd); Future d = Clipboard.getData(Clipboard.kTextPlain); void dataCallback(ClipboardData? d) { if (d != null) { String? text = d.text; if(text != null) { print("Clipboard data $text"); } } } d.then(dataCallback); --- .../tizen/channels/platform_channel.cc | 74 +++++++++++++++++-- .../tizen/channels/platform_channel.h | 9 +++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/shell/platform/tizen/channels/platform_channel.cc b/shell/platform/tizen/channels/platform_channel.cc index b79f780af3a52..adf315f09e8e4 100644 --- a/shell/platform/tizen/channels/platform_channel.cc +++ b/shell/platform/tizen/channels/platform_channel.cc @@ -5,6 +5,7 @@ #include "platform_channel.h" #include +#include #include "flutter/shell/platform/common/cpp/json_method_codec.h" #include "flutter/shell/platform/tizen/tizen_log.h" @@ -13,7 +14,9 @@ static constexpr char kChannelName[] = "flutter/platform"; PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger) : channel_(std::make_unique>( - messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())) { + messenger, + kChannelName, + &flutter::JsonMethodCodec::GetInstance())) { channel_->SetMethodCallHandler( [this]( const flutter::MethodCall& call, @@ -29,6 +32,7 @@ void PlatformChannel::HandleMethodCall( std::unique_ptr> result) { const auto method = call.method_name(); + FT_LOGI("pkosko Called method : %s", method.c_str()); if (method == "SystemNavigator.pop") { ui_app_exit(); result->Success(); @@ -37,11 +41,9 @@ void PlatformChannel::HandleMethodCall( } else if (method == "HapticFeedback.vibrate") { result->NotImplemented(); } else if (method == "Clipboard.getData") { - result->NotImplemented(); + Clipboard::GetData(call, std::move(result)); } else if (method == "Clipboard.setData") { - result->NotImplemented(); - } else if (method == "Clipboard.hasStrings") { - result->NotImplemented(); + Clipboard::SetData(call, std::move(result)); } else if (method == "SystemChrome.setPreferredOrientations") { result->NotImplemented(); } else if (method == "SystemChrome.setApplicationSwitcherDescription") { @@ -57,3 +59,65 @@ void PlatformChannel::HandleMethodCall( result->NotImplemented(); } } + +// Clipboard constants and variables +namespace Clipboard { + +// naive implementation using std::string as a container of internal clipboard +// data +std::mutex is_processing_mutex; +std::string string_clipboard = ""; + +static constexpr char kTextKey[] = "text"; +static constexpr char kTextPlainFormat[] = "text/plain"; +static constexpr char kUnknownClipboardFormatError[] = + "Unknown clipboard format error"; +static constexpr char kUnknownClipboardError[] = + "Unknown error during clipboard data retrieval"; + +void GetData( + const flutter::MethodCall& call, + std::unique_ptr> result) { + const rapidjson::Value& format = call.arguments()[0]; + + // https://api.flutter.dev/flutter/services/Clipboard/kTextPlain-constant.html + // API supports only kTextPlain format + if (strcmp(format.GetString(), kTextPlainFormat) != 0) { + result->Error(kUnknownClipboardFormatError, + "Clipboard API only supports text."); + return; + } + + { + std::unique_lock lock(is_processing_mutex); + if (string_clipboard.empty()) { + result->Error(kUnknownClipboardError, "No clipboard data available."); + } else { + rapidjson::Document document; + document.SetObject(); + rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); + document.AddMember(rapidjson::Value(kTextKey, allocator), + rapidjson::Value(string_clipboard, allocator), + allocator); + result->Success(document); + } + } +} + +void SetData( + const flutter::MethodCall& call, + std::unique_ptr> result) { + const rapidjson::Value& document = *call.arguments(); + rapidjson::Value::ConstMemberIterator itr = document.FindMember(kTextKey); + if (itr == document.MemberEnd()) { + result->Error(kUnknownClipboardError, "Invalid message format"); + return; + } + { + std::unique_lock lock(is_processing_mutex); + string_clipboard = itr->value.GetString(); + FT_LOGI("pkosko Set value of clipboard : %s", string_clipboard.c_str()); + result->Success(); + } +} +} // namespace Clipboard diff --git a/shell/platform/tizen/channels/platform_channel.h b/shell/platform/tizen/channels/platform_channel.h index 078c05f5a2b28..90d2e01c558b2 100644 --- a/shell/platform/tizen/channels/platform_channel.h +++ b/shell/platform/tizen/channels/platform_channel.h @@ -22,4 +22,13 @@ class PlatformChannel { std::unique_ptr> result); }; +namespace Clipboard { +void GetData( + const flutter::MethodCall& call, + std::unique_ptr> result); +void SetData( + const flutter::MethodCall& call, + std::unique_ptr> result); +}; + #endif // EMBEDDER_PLATFORM_CHANNEL_H_ From b86408ac6021c5ff4699edd451a5e5a0235a30a9 Mon Sep 17 00:00:00 2001 From: "Piotr Kosko/Tizen API (PLT) /SRPOL/Engineer/Samsung Electronics" Date: Wed, 28 Apr 2021 09:13:02 +0200 Subject: [PATCH 2/2] [Clipboard] Fixes after review * Removed unnecessary mutex * Cleaned temporary logs --- .../tizen/channels/platform_channel.cc | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/shell/platform/tizen/channels/platform_channel.cc b/shell/platform/tizen/channels/platform_channel.cc index adf315f09e8e4..46c284e5cf716 100644 --- a/shell/platform/tizen/channels/platform_channel.cc +++ b/shell/platform/tizen/channels/platform_channel.cc @@ -5,7 +5,6 @@ #include "platform_channel.h" #include -#include #include "flutter/shell/platform/common/cpp/json_method_codec.h" #include "flutter/shell/platform/tizen/tizen_log.h" @@ -32,7 +31,6 @@ void PlatformChannel::HandleMethodCall( std::unique_ptr> result) { const auto method = call.method_name(); - FT_LOGI("pkosko Called method : %s", method.c_str()); if (method == "SystemNavigator.pop") { ui_app_exit(); result->Success(); @@ -65,7 +63,6 @@ namespace Clipboard { // naive implementation using std::string as a container of internal clipboard // data -std::mutex is_processing_mutex; std::string string_clipboard = ""; static constexpr char kTextKey[] = "text"; @@ -88,20 +85,13 @@ void GetData( return; } - { - std::unique_lock lock(is_processing_mutex); - if (string_clipboard.empty()) { - result->Error(kUnknownClipboardError, "No clipboard data available."); - } else { - rapidjson::Document document; - document.SetObject(); - rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); - document.AddMember(rapidjson::Value(kTextKey, allocator), - rapidjson::Value(string_clipboard, allocator), - allocator); - result->Success(document); - } - } + rapidjson::Document document; + document.SetObject(); + rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); + document.AddMember(rapidjson::Value(kTextKey, allocator), + rapidjson::Value(string_clipboard, allocator), + allocator); + result->Success(document); } void SetData( @@ -113,11 +103,7 @@ void SetData( result->Error(kUnknownClipboardError, "Invalid message format"); return; } - { - std::unique_lock lock(is_processing_mutex); - string_clipboard = itr->value.GetString(); - FT_LOGI("pkosko Set value of clipboard : %s", string_clipboard.c_str()); - result->Success(); - } + string_clipboard = itr->value.GetString(); + result->Success(); } } // namespace Clipboard