@@ -57,10 +57,6 @@ constexpr char kPortraitDown[] = "DeviceOrientation.portraitDown";
5757constexpr char kLandscapeLeft [] = " DeviceOrientation.landscapeLeft" ;
5858constexpr char kLandscapeRight [] = " DeviceOrientation.landscapeRight" ;
5959
60- // Naive implementation using std::string as a container of internal clipboard
61- // data.
62- std::string text_clipboard = " " ;
63-
6460} // namespace
6561
6662PlatformChannel::PlatformChannel (BinaryMessenger* messenger,
@@ -70,14 +66,25 @@ PlatformChannel::PlatformChannel(BinaryMessenger* messenger,
7066 kChannelName ,
7167 &JsonMethodCodec::GetInstance ())),
7268 view_(view) {
69+ #if defined(MOBILE_PROFILE) || defined(COMMON_PROFILE)
70+ int ret = cbhm_open_service (&cbhm_handle_);
71+ if (ret != CBHM_ERROR_NONE) {
72+ FT_LOG (Error) << " Failed to initialize the clipboard service." ;
73+ }
74+ #endif
75+
7376 channel_->SetMethodCallHandler (
7477 [this ](const MethodCall<rapidjson::Document>& call,
7578 std::unique_ptr<MethodResult<rapidjson::Document>> result) {
7679 HandleMethodCall (call, std::move (result));
7780 });
7881}
7982
80- PlatformChannel::~PlatformChannel () {}
83+ PlatformChannel::~PlatformChannel () {
84+ #if defined(MOBILE_PROFILE) || defined(COMMON_PROFILE)
85+ cbhm_close_service (cbhm_handle_);
86+ #endif
87+ }
8188
8289void PlatformChannel::HandleMethodCall (
8390 const MethodCall<rapidjson::Document>& method_call,
@@ -100,33 +107,36 @@ void PlatformChannel::HandleMethodCall(
100107 result->Success ();
101108 } else if (method == kGetClipboardDataMethod ) {
102109 // https://api.flutter.dev/flutter/services/Clipboard/kTextPlain-constant.html
103- // The API supports only kTextPlain format.
110+ // The API only supports the plain text format.
104111 if (strcmp (arguments[0 ].GetString (), kTextPlainFormat ) != 0 ) {
105112 result->Error (kUnknownClipboardFormatError ,
106113 " Clipboard API only supports text." );
107114 return ;
108115 }
109- rapidjson::Document document;
110- document.SetObject ();
111- rapidjson::Document::AllocatorType& allocator = document.GetAllocator ();
112- document.AddMember (rapidjson::Value (kTextKey , allocator),
113- rapidjson::Value (text_clipboard, allocator), allocator);
114- result->Success (document);
116+ GetClipboardData ([result = result.release ()](const std::string& data) {
117+ rapidjson::Document document;
118+ document.SetObject ();
119+ rapidjson::Document::AllocatorType& allocator = document.GetAllocator ();
120+ document.AddMember (rapidjson::Value (kTextKey , allocator),
121+ rapidjson::Value (data, allocator), allocator);
122+ result->Success (document);
123+ delete result;
124+ });
115125 } else if (method == kSetClipboardDataMethod ) {
116126 const rapidjson::Value& document = *arguments;
117127 auto iter = document.FindMember (kTextKey );
118128 if (iter == document.MemberEnd ()) {
119129 result->Error (kUnknownClipboardError , " Invalid message format." );
120130 return ;
121131 }
122- text_clipboard = iter->value .GetString ();
132+ SetClipboardData ( iter->value .GetString () );
123133 result->Success ();
124134 } else if (method == kClipboardHasStringsMethod ) {
125135 rapidjson::Document document;
126136 document.SetObject ();
127137 rapidjson::Document::AllocatorType& allocator = document.GetAllocator ();
128138 document.AddMember (rapidjson::Value (kValueKey , allocator),
129- rapidjson::Value (!text_clipboard. empty ()), allocator);
139+ rapidjson::Value (ClipboardHasStrings ()), allocator);
130140 result->Success (document);
131141 } else if (method == kRestoreSystemUiOverlaysMethod ) {
132142 RestoreSystemUiOverlays ();
@@ -177,6 +187,59 @@ void PlatformChannel::HapticFeedbackVibrate(const std::string& feedback_type) {
177187 FeedbackManager::GetInstance ().Vibrate ();
178188}
179189
190+ void PlatformChannel::GetClipboardData (ClipboardCallback on_data) {
191+ on_clipboard_data_ = std::move (on_data);
192+
193+ #if defined(MOBILE_PROFILE) || defined(COMMON_PROFILE)
194+ int ret = cbhm_selection_get (
195+ cbhm_handle_, CBHM_SEL_TYPE_TEXT,
196+ [](cbhm_h cbhm_handle, const char * buf, size_t len,
197+ void * user_data) -> int {
198+ auto * self = static_cast <PlatformChannel*>(user_data);
199+ std::string data;
200+ if (buf) {
201+ data = std::string (buf, len);
202+ }
203+ self->on_clipboard_data_ (data);
204+ self->on_clipboard_data_ = nullptr ;
205+ return CBHM_ERROR_NONE;
206+ },
207+ this );
208+ if (ret != CBHM_ERROR_NONE) {
209+ if (ret == CBHM_ERROR_NO_DATA) {
210+ FT_LOG (Info) << " No clipboard data available." ;
211+ } else {
212+ FT_LOG (Error) << " Failed to get clipboard data." ;
213+ }
214+ on_clipboard_data_ (" " );
215+ on_clipboard_data_ = nullptr ;
216+ }
217+ #else
218+ on_clipboard_data_ (clipboard_);
219+ on_clipboard_data_ = nullptr ;
220+ #endif
221+ }
222+
223+ void PlatformChannel::SetClipboardData (const std::string& data) {
224+ #if defined(MOBILE_PROFILE) || defined(COMMON_PROFILE)
225+ int ret = cbhm_selection_set (cbhm_handle_, CBHM_SEL_TYPE_TEXT, data.c_str (),
226+ data.length ());
227+ if (ret != CBHM_ERROR_NONE) {
228+ FT_LOG (Error) << " Failed to set clipboard data." ;
229+ }
230+ #else
231+ clipboard_ = data;
232+ #endif
233+ }
234+
235+ bool PlatformChannel::ClipboardHasStrings () {
236+ #if defined(MOBILE_PROFILE) || defined(COMMON_PROFILE)
237+ return cbhm_item_count_get (cbhm_handle_) > 0 ;
238+ #else
239+ return !clipboard_.empty ();
240+ #endif
241+ }
242+
180243void PlatformChannel::RestoreSystemUiOverlays () {
181244 if (view_->GetType () != TizenViewType::kWindow ) {
182245 return ;
0 commit comments