forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 17
[Clipboard] getData implementation added #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,25 @@ | |
|
||
#include "platform_channel.h" | ||
|
||
#include <mutex> | ||
#include <app.h> | ||
#include <cbhm.h> | ||
|
||
#include "flutter/shell/platform/common/cpp/json_method_codec.h" | ||
#include "flutter/shell/platform/tizen/tizen_log.h" | ||
|
||
static constexpr char kChannelName[] = "flutter/platform"; | ||
|
||
// Clipboard.getData constants and variables | ||
std::mutex is_processing_mutex; | ||
static bool is_processing = false; | ||
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"; | ||
|
||
PlatformChannel::PlatformChannel(flutter::BinaryMessenger* messenger) | ||
: channel_(std::make_unique<flutter::MethodChannel<rapidjson::Document>>( | ||
messenger, kChannelName, &flutter::JsonMethodCodec::GetInstance())) { | ||
|
@@ -37,7 +49,99 @@ void PlatformChannel::HandleMethodCall( | |
} else if (method == "HapticFeedback.vibrate") { | ||
result->NotImplemented(); | ||
} else if (method == "Clipboard.getData") { | ||
result->NotImplemented(); | ||
const rapidjson::Value& format = call.arguments()[0]; | ||
|
||
// https://api.flutter.dev/flutter/services/Clipboard/kTextPlain-constant.html | ||
// API supports only kTextPlain format, however cbhm API supports also other formats | ||
if (strcmp(format.GetString(), kTextPlainFormat) != 0) { | ||
result->Error(kUnknownClipboardFormatError, | ||
"Clipboard API only supports text."); | ||
return; | ||
} | ||
|
||
// Report error on next calls until current will be finished. | ||
// Native API - cbhm_selection_get works on static struct, so accessing clipboard parallelly will end | ||
// with race regarding returning values - cbhm_selection_data_cb will be triggered only for latest call. | ||
// TODO consider some queuing mechnism instead of returning error for next calls | ||
{ | ||
std::lock_guard<std::mutex> lock(is_processing_mutex); | ||
if (is_processing) { | ||
result->Error(kUnknownClipboardError, "Already processing by other thread."); | ||
return; | ||
} | ||
is_processing = true; | ||
} | ||
|
||
cbhm_sel_type_e selection_type = CBHM_SEL_TYPE_TEXT; | ||
|
||
cbhm_h cbhm_handle = nullptr; | ||
int ret = cbhm_open_service (&cbhm_handle); | ||
if (CBHM_ERROR_NONE != ret) { | ||
result->Error(kUnknownClipboardError, "Failed to initialize cbhm service."); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't |
||
} | ||
|
||
// additional check if data in clipboard | ||
ret = cbhm_item_count_get(cbhm_handle); | ||
if (ret <= 0) { | ||
result->Error(kUnknownClipboardError, "No clipboard data available."); | ||
// release the data | ||
cbhm_close_service (cbhm_handle); | ||
// unlock guard for further processing | ||
std::lock_guard<std::mutex> lock(is_processing_mutex); | ||
is_processing = false; | ||
return; | ||
} | ||
|
||
struct method_data_t { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we use Google coding style here and name this |
||
std::unique_ptr<flutter::MethodResult<rapidjson::Document>> result; | ||
cbhm_h cbhm_handle; | ||
}; | ||
// invalidates the result pointer | ||
method_data_t* data = new method_data_t{}; | ||
data->result = std::move(result); | ||
data->cbhm_handle = cbhm_handle; | ||
|
||
auto cbhm_selection_data_cb = [](cbhm_h cbhm_handle, const char *buf, size_t len, void *user_data) -> int { | ||
auto data = static_cast<method_data_t*>(user_data); | ||
// move unique_ptr from method_data_t and then release memory | ||
auto result = std::move(data->result); | ||
cbhm_close_service (data->cbhm_handle); | ||
delete data; | ||
|
||
FT_LOGD("cbhm_selection_get SUCCESS (%d) %s", len, buf); | ||
{ | ||
std::lock_guard<std::mutex> lock(is_processing_mutex); | ||
is_processing = false; | ||
} | ||
if (buf) { | ||
rapidjson::Document document; | ||
document.SetObject(); | ||
rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); | ||
document.AddMember(rapidjson::Value(kTextKey, allocator), | ||
rapidjson::Value(std::string{buf, len}, allocator), allocator); | ||
result->Success(document); | ||
return CBHM_ERROR_NONE; | ||
} else { | ||
result->Error(kUnknownClipboardError, "Data buffer is null."); | ||
return CBHM_ERROR_NO_DATA; | ||
} | ||
}; | ||
|
||
FT_LOGD("cbhm_selection_get call"); | ||
ret = cbhm_selection_get(cbhm_handle, selection_type, cbhm_selection_data_cb, data); | ||
if (CBHM_ERROR_NONE != ret) { | ||
FT_LOGD("cbhm_selection_get error"); | ||
// return error | ||
data->result->Error(kUnknownClipboardError, "Failed to gather data."); | ||
// release the data | ||
cbhm_close_service (data->cbhm_handle); | ||
delete data; | ||
// unlock guard for further processing | ||
std::lock_guard<std::mutex> lock(is_processing_mutex); | ||
is_processing = false; | ||
return; | ||
} | ||
} else if (method == "Clipboard.setData") { | ||
result->NotImplemented(); | ||
} else if (method == "Clipboard.hasStrings") { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider moving the body of this "if" to a separate method to make
PlatformChannel::HandleMethodCall
shorter.