fix: clipboard.writeBuffer raw format access (#31116)

* fix: clipboard.writeBuffer raw format access

* test: clipboard.writeBuffer raw format access

* test: clipboard win32 test skip

* fixup spec

* cleanup patch

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
henrit 2021-11-04 13:19:30 -05:00 committed by GitHub
parent f8df634197
commit 3c33d70294
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 3 deletions

View file

@ -51,7 +51,23 @@ bool Clipboard::Has(const std::string& format_string,
std::string Clipboard::Read(const std::string& format_string) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
// Prefer raw platform format names
ui::ClipboardFormatType rawFormat(
ui::ClipboardFormatType::CustomPlatformType(format_string));
bool rawFormatAvailable = clipboard->IsFormatAvailable(
rawFormat, ui::ClipboardBuffer::kCopyPaste, /* data_dst = */ nullptr);
#if defined(OS_LINUX)
if (!rawFormatAvailable) {
rawFormatAvailable = clipboard->IsFormatAvailable(
rawFormat, ui::ClipboardBuffer::kSelection, /* data_dst = */ nullptr);
}
#endif
if (rawFormatAvailable) {
std::string data;
clipboard->ReadData(rawFormat, /* data_dst = */ nullptr, &data);
return data;
}
// Otherwise, resolve custom format names
std::map<std::string, std::string> custom_format_names;
custom_format_names =
clipboard->ExtractCustomPlatformNames(ui::ClipboardBuffer::kCopyPaste,
@ -97,8 +113,8 @@ void Clipboard::WriteBuffer(const std::string& format,
base::span<const uint8_t> payload_span(
reinterpret_cast<const uint8_t*>(node::Buffer::Data(buffer)),
node::Buffer::Length(buffer));
writer.WriteData(base::UTF8ToUTF16(format),
mojo_base::BigBuffer(payload_span));
writer.WriteUnsafeRawData(base::UTF8ToUTF16(format),
mojo_base::BigBuffer(payload_span));
}
void Clipboard::Write(const gin_helper::Dictionary& data,