electron/shell/common/api/electron_api_clipboard.cc
electron-roller[bot] 8532e1239e
chore: bump chromium to 93.0.4530.0 (master) (#29256)
* chore: bump chromium in DEPS to 92.0.4512.6

* 2887336: [CaptureHandle][#2] Propagate CaptureHandleConfig in browser process

https://chromium-review.googlesource.com/c/chromium/src/+/2887336

* refactor: base::Optional -> absl::optional

* chore: fixup patch indices

* chore: bump chromium in DEPS to 92.0.4514.0

* 2899417: Make build work when enable_pdf is set to false.

https://chromium-review.googlesource.com/c/chromium/src/+/2899417

* 2904731: use BrowserContext instead of Profile in PreconnectManager

https://chromium-review.googlesource.com/c/chromium/src/+/2904731

* 2295749: fix: check IsSecureEventInputEnabled in constructor before setting SetPasswordInputEnabled to true

https://chromium-review.googlesource.com/c/chromium/src/+/2295749

* 2893803: Add a GetWebView to RenderFrame.

https://chromium-review.googlesource.com/c/chromium/src/+/2893803

* 2892345: Implement WebContents::ForEachRenderFrameHost

https://chromium-review.googlesource.com/c/chromium/src/+/2892345

* chore: fixup patch indices

* 2892048: Real instance methods for BrowserContext: remaining 5 methods.

https://chromium-review.googlesource.com/c/chromium/src/+/2892048

* 2902821: [mojo] Don't require full header includes for referenced interfaces

https://chromium-review.googlesource.com/c/chromium/src/+/2902821

* 2496500: Remove last deprecated extension Event ctor.

https://chromium-review.googlesource.com/c/chromium/src/+/2496500

* chore: fixup malformed pepper support patch

* chore: bump chromium in DEPS to 92.0.4515.0

* 2908461: Add CreateEmptyPrintPagesParamsPtr() inside print_view_manager_base.cc.

https://chromium-review.googlesource.com/c/chromium/src/+/2908461

* 2880838: viz: add optional HDRMetadata to TransferableResource

https://chromium-review.googlesource.com/c/chromium/src/+/2880838

* chore: fixup patch indices

* chore: bump chromium in DEPS to 92.0.4515.5

* chore: update patches

* chore: bump chromium in DEPS to 92.0.4515.7

* chore: bump chromium in DEPS to 92.0.4515.9

* chore: bump chromium in DEPS to 93.0.4522.0

* chore: bump chromium in DEPS to 93.0.4523.0

* chore: bump chromium in DEPS to 93.0.4524.0

* chore: update patches

* chore: enable_pak_file_integrity_checks was reverted

* chore: update patches

* refactor: base/optional was replaced with absl::optional

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2910202

* refactor: replace all usages of base::nullopt with absl::nullopt

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2910202

* chore: add missing base::Contains include

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2910202

* refactor: replace all usages of base::make_optional with
absl::make_optional

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2910202

* refactor: replace WorldScriptContext() with GetScriptContextFromWorldId

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2893213

* chore: clean up left over opening namespace

Refs: 95bfe6d08f

* chore: add missing base::Contains include

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2910202

* refactor: replace GetCurrentDisplayIterator with the hard checker
GetCurrentDisplay

This code looks suspicious but if the iterator was invalid before it
will also be invalid now.

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2893191

* refactor: headers are now passed directly in extensions client

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2918906

* refactor: base::DictionaryValue::empty() has been removed

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2912424

* chore: add missing includes for network URLLoaderFactory

Refs: unknown, probably a side effect of header changes

* refactor: make convenience wrapper around AppendArg

There is no converter FromV8 for base::StringPiece (apparently its not
possible).  So we now take in an std::string and use the construct for
StringPiece to do implicit conversion.

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2905544

* chore: add patch

* chore: bump chromium in DEPS to 93.0.4525.0

* chore: update patches

* refactor: CanResize has been de-virtualized

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2485774

* chore: update resource integrity patch

* chore: add character encoding idl patch

* chore: bump chromium in DEPS to 93.0.4526.0

* chore: update patches

* chore: bump chromium in DEPS to 93.0.4527.0

* chore: bump chromium in DEPS to 93.0.4528.0

* chore: update patches

* chore: update idl encoding patch

* chore: bump chromium in DEPS to 93.0.4529.0

* chore: update patches

* chore: bump chromium in DEPS to 93.0.4530.0

* chore: update patches

* fix: only SetCanResize after the widget has been initialized

* chore: add patch for vr on windows gn gen

* spec: fix focus related tests on linux due to delay in focus swap

* chore: remove new usages of base::Optional from main

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: Samuel Attard <sattard@slack-corp.com>
Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
2021-06-03 01:05:04 -07:00

255 lines
9 KiB
C++

// Copyright (c) 2013 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/api/electron_api_clipboard.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "third_party/skia/include/core/SkPixmap.h"
#include "ui/base/clipboard/clipboard_format_type.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
namespace electron {
namespace api {
ui::ClipboardBuffer Clipboard::GetClipboardBuffer(gin_helper::Arguments* args) {
std::string type;
if (args->GetNext(&type) && type == "selection")
return ui::ClipboardBuffer::kSelection;
else
return ui::ClipboardBuffer::kCopyPaste;
}
std::vector<std::u16string> Clipboard::AvailableFormats(
gin_helper::Arguments* args) {
std::vector<std::u16string> format_types;
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->ReadAvailableTypes(GetClipboardBuffer(args),
/* data_dst = */ nullptr, &format_types);
return format_types;
}
bool Clipboard::Has(const std::string& format_string,
gin_helper::Arguments* args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::ClipboardFormatType format(
ui::ClipboardFormatType::GetType(format_string));
return clipboard->IsFormatAvailable(format, GetClipboardBuffer(args),
/* data_dst = */ nullptr);
}
std::string Clipboard::Read(const std::string& format_string) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::ClipboardFormatType format(
ui::ClipboardFormatType::GetType(format_string));
std::string data;
clipboard->ReadData(format, /* data_dst = */ nullptr, &data);
return data;
}
v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
gin_helper::Arguments* args) {
std::string data = Read(format_string);
return node::Buffer::Copy(args->isolate(), data.data(), data.length())
.ToLocalChecked();
}
void Clipboard::WriteBuffer(const std::string& format,
const v8::Local<v8::Value> buffer,
gin_helper::Arguments* args) {
if (!node::Buffer::HasInstance(buffer)) {
args->ThrowError("buffer must be a node Buffer");
return;
}
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
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));
}
void Clipboard::Write(const gin_helper::Dictionary& data,
gin_helper::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
std::u16string text, html, bookmark;
gfx::Image image;
if (data.Get("text", &text)) {
writer.WriteText(text);
if (data.Get("bookmark", &bookmark))
writer.WriteBookmark(bookmark, base::UTF16ToUTF8(text));
}
if (data.Get("rtf", &text)) {
std::string rtf = base::UTF16ToUTF8(text);
writer.WriteRTF(rtf);
}
if (data.Get("html", &html))
writer.WriteHTML(html, std::string());
if (data.Get("image", &image))
writer.WriteImage(image.AsBitmap());
}
std::u16string Clipboard::ReadText(gin_helper::Arguments* args) {
std::u16string data;
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
auto type = GetClipboardBuffer(args);
if (clipboard->IsFormatAvailable(ui::ClipboardFormatType::GetPlainTextType(),
type, /* data_dst = */ nullptr)) {
clipboard->ReadText(type, /* data_dst = */ nullptr, &data);
} else {
#if defined(OS_WIN)
if (clipboard->IsFormatAvailable(
ui::ClipboardFormatType::GetPlainTextAType(), type,
/* data_dst = */ nullptr)) {
std::string result;
clipboard->ReadAsciiText(type, /* data_dst = */ nullptr, &result);
data = base::ASCIIToUTF16(result);
}
#endif
}
return data;
}
void Clipboard::WriteText(const std::u16string& text,
gin_helper::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
writer.WriteText(text);
}
std::u16string Clipboard::ReadRTF(gin_helper::Arguments* args) {
std::string data;
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->ReadRTF(GetClipboardBuffer(args), /* data_dst = */ nullptr, &data);
return base::UTF8ToUTF16(data);
}
void Clipboard::WriteRTF(const std::string& text, gin_helper::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
writer.WriteRTF(text);
}
std::u16string Clipboard::ReadHTML(gin_helper::Arguments* args) {
std::u16string data;
std::u16string html;
std::string url;
uint32_t start;
uint32_t end;
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->ReadHTML(GetClipboardBuffer(args), /* data_dst = */ nullptr, &html,
&url, &start, &end);
data = html.substr(start, end - start);
return data;
}
void Clipboard::WriteHTML(const std::u16string& html,
gin_helper::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
writer.WriteHTML(html, std::string());
}
v8::Local<v8::Value> Clipboard::ReadBookmark(gin_helper::Arguments* args) {
std::u16string title;
std::string url;
gin_helper::Dictionary dict =
gin_helper::Dictionary::CreateEmpty(args->isolate());
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->ReadBookmark(/* data_dst = */ nullptr, &title, &url);
dict.Set("title", title);
dict.Set("url", url);
return dict.GetHandle();
}
void Clipboard::WriteBookmark(const std::u16string& title,
const std::string& url,
gin_helper::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
writer.WriteBookmark(title, url);
}
gfx::Image Clipboard::ReadImage(gin_helper::Arguments* args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
absl::optional<gfx::Image> image;
clipboard->ReadImage(
GetClipboardBuffer(args),
/* data_dst = */ nullptr,
base::BindOnce(
[](absl::optional<gfx::Image>* image, const SkBitmap& result) {
image->emplace(gfx::Image::CreateFrom1xBitmap(result));
},
&image));
DCHECK(image.has_value());
return image.value();
}
void Clipboard::WriteImage(const gfx::Image& image,
gin_helper::Arguments* args) {
ui::ScopedClipboardWriter writer(GetClipboardBuffer(args));
SkBitmap orig = image.AsBitmap();
SkBitmap bmp;
if (bmp.tryAllocPixels(orig.info()) &&
orig.readPixels(bmp.info(), bmp.getPixels(), bmp.rowBytes(), 0, 0)) {
writer.WriteImage(bmp);
}
}
#if !defined(OS_MAC)
void Clipboard::WriteFindText(const std::u16string& text) {}
std::u16string Clipboard::ReadFindText() {
return std::u16string();
}
#endif
void Clipboard::Clear(gin_helper::Arguments* args) {
ui::Clipboard::GetForCurrentThread()->Clear(GetClipboardBuffer(args));
}
} // namespace api
} // namespace electron
namespace {
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
gin_helper::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("availableFormats",
&electron::api::Clipboard::AvailableFormats);
dict.SetMethod("has", &electron::api::Clipboard::Has);
dict.SetMethod("read", &electron::api::Clipboard::Read);
dict.SetMethod("write", &electron::api::Clipboard::Write);
dict.SetMethod("readText", &electron::api::Clipboard::ReadText);
dict.SetMethod("writeText", &electron::api::Clipboard::WriteText);
dict.SetMethod("readRTF", &electron::api::Clipboard::ReadRTF);
dict.SetMethod("writeRTF", &electron::api::Clipboard::WriteRTF);
dict.SetMethod("readHTML", &electron::api::Clipboard::ReadHTML);
dict.SetMethod("writeHTML", &electron::api::Clipboard::WriteHTML);
dict.SetMethod("readBookmark", &electron::api::Clipboard::ReadBookmark);
dict.SetMethod("writeBookmark", &electron::api::Clipboard::WriteBookmark);
dict.SetMethod("readImage", &electron::api::Clipboard::ReadImage);
dict.SetMethod("writeImage", &electron::api::Clipboard::WriteImage);
dict.SetMethod("readFindText", &electron::api::Clipboard::ReadFindText);
dict.SetMethod("writeFindText", &electron::api::Clipboard::WriteFindText);
dict.SetMethod("readBuffer", &electron::api::Clipboard::ReadBuffer);
dict.SetMethod("writeBuffer", &electron::api::Clipboard::WriteBuffer);
dict.SetMethod("clear", &electron::api::Clipboard::Clear);
}
} // namespace
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_clipboard, Initialize)