2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-30 08:10:25 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
#include "atom/common/api/atom_api_clipboard.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2015-02-11 06:55:44 +00:00
|
|
|
#include "atom/common/native_mate_converters/image_converter.h"
|
2014-06-05 03:43:45 +00:00
|
|
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
2015-07-07 05:17:33 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-06-29 13:11:57 +00:00
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
2014-06-28 14:33:00 +00:00
|
|
|
#include "ui/base/clipboard/scoped_clipboard_writer.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
ui::ClipboardType Clipboard::GetClipboardType(mate::Arguments* args) {
|
2015-06-10 04:12:37 +00:00
|
|
|
std::string type;
|
|
|
|
if (args->GetNext(&type) && type == "selection")
|
|
|
|
return ui::CLIPBOARD_TYPE_SELECTION;
|
|
|
|
else
|
|
|
|
return ui::CLIPBOARD_TYPE_COPY_PASTE;
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
std::vector<base::string16> Clipboard::AvailableFormats(mate::Arguments* args) {
|
2015-05-22 09:29:11 +00:00
|
|
|
std::vector<base::string16> format_types;
|
|
|
|
bool ignore;
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
2015-06-10 04:12:37 +00:00
|
|
|
clipboard->ReadAvailableTypes(GetClipboardType(args), &format_types, &ignore);
|
2015-05-22 09:29:11 +00:00
|
|
|
return format_types;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
bool Clipboard::Has(const std::string& format_string, mate::Arguments* args) {
|
2015-05-27 08:05:51 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
2015-06-10 04:12:37 +00:00
|
|
|
return clipboard->IsFormatAvailable(format, GetClipboardType(args));
|
2015-05-27 08:05:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:58:35 +00:00
|
|
|
std::string Clipboard::Read(const std::string& format_string) {
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
clipboard->ReadData(format, &data);
|
2014-04-15 07:50:00 +00:00
|
|
|
return data;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 22:42:23 +00:00
|
|
|
v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
|
|
|
|
mate::Arguments* args) {
|
2017-03-17 16:58:35 +00:00
|
|
|
std::string data = Read(format_string);
|
2017-03-16 22:42:23 +00:00
|
|
|
return node::Buffer::Copy(
|
|
|
|
args->isolate(), data.data(), data.length()).ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:53:58 +00:00
|
|
|
void Clipboard::WriteBuffer(const std::string& format,
|
2017-04-21 05:47:04 +00:00
|
|
|
const v8::Local<v8::Value> buffer,
|
|
|
|
mate::Arguments* args) {
|
2017-05-22 20:53:58 +00:00
|
|
|
if (!node::Buffer::HasInstance(buffer)) {
|
|
|
|
args->ThrowError("buffer must be a node Buffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-21 05:47:04 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
|
|
|
writer.WriteData(node::Buffer::Data(buffer), node::Buffer::Length(buffer),
|
2017-05-22 20:53:58 +00:00
|
|
|
ui::Clipboard::GetFormatType(format));
|
2017-04-21 05:47:04 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
void Clipboard::Write(const mate::Dictionary& data, mate::Arguments* args) {
|
2015-07-07 05:17:33 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
2016-06-24 22:14:28 +00:00
|
|
|
base::string16 text, html, bookmark;
|
2015-07-07 05:17:33 +00:00
|
|
|
gfx::Image image;
|
|
|
|
|
2016-06-24 22:14:28 +00:00
|
|
|
if (data.Get("text", &text)) {
|
2015-07-07 05:17:33 +00:00
|
|
|
writer.WriteText(text);
|
|
|
|
|
2016-06-24 22:14:28 +00:00
|
|
|
if (data.Get("bookmark", &bookmark))
|
|
|
|
writer.WriteBookmark(bookmark, base::UTF16ToUTF8(text));
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:38:47 +00:00
|
|
|
if (data.Get("rtf", &text)) {
|
|
|
|
std::string rtf = base::UTF16ToUTF8(text);
|
|
|
|
writer.WriteRTF(rtf);
|
|
|
|
}
|
|
|
|
|
2015-07-07 05:17:33 +00:00
|
|
|
if (data.Get("html", &html))
|
|
|
|
writer.WriteHTML(html, std::string());
|
|
|
|
|
|
|
|
if (data.Get("image", &image))
|
|
|
|
writer.WriteImage(image.AsBitmap());
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
base::string16 Clipboard::ReadText(mate::Arguments* args) {
|
2014-06-28 14:33:00 +00:00
|
|
|
base::string16 data;
|
2015-07-07 05:17:33 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
auto type = GetClipboardType(args);
|
|
|
|
if (clipboard->IsFormatAvailable(
|
|
|
|
ui::Clipboard::GetPlainTextWFormatType(), type)) {
|
|
|
|
clipboard->ReadText(type, &data);
|
|
|
|
} else if (clipboard->IsFormatAvailable(
|
|
|
|
ui::Clipboard::GetPlainTextFormatType(), type)) {
|
|
|
|
std::string result;
|
|
|
|
clipboard->ReadAsciiText(type, &result);
|
|
|
|
data = base::ASCIIToUTF16(result);
|
|
|
|
}
|
2014-04-15 07:50:00 +00:00
|
|
|
return data;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
void Clipboard::WriteText(const base::string16& text, mate::Arguments* args) {
|
2015-06-10 04:12:37 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
2014-06-28 14:33:00 +00:00
|
|
|
writer.WriteText(text);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
base::string16 Clipboard::ReadRtf(mate::Arguments* args) {
|
2016-02-05 08:06:21 +00:00
|
|
|
std::string data;
|
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
clipboard->ReadRTF(GetClipboardType(args), &data);
|
|
|
|
return base::UTF8ToUTF16(data);
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
void Clipboard::WriteRtf(const std::string& text, mate::Arguments* args) {
|
2016-02-04 18:38:47 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
|
|
|
writer.WriteRTF(text);
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
base::string16 Clipboard::ReadHtml(mate::Arguments* args) {
|
2015-05-22 09:29:11 +00:00
|
|
|
base::string16 data;
|
|
|
|
base::string16 html;
|
|
|
|
std::string url;
|
2016-03-08 04:40:10 +00:00
|
|
|
uint32_t start;
|
|
|
|
uint32_t end;
|
2015-06-10 04:12:37 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
clipboard->ReadHTML(GetClipboardType(args), &html, &url, &start, &end);
|
2015-05-22 09:29:11 +00:00
|
|
|
data = html.substr(start, end - start);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
void Clipboard::WriteHtml(const base::string16& html, mate::Arguments* args) {
|
2015-06-10 04:12:37 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
2015-05-22 09:29:11 +00:00
|
|
|
writer.WriteHTML(html, std::string());
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
v8::Local<v8::Value> Clipboard::ReadBookmark(mate::Arguments* args) {
|
2016-06-24 22:08:12 +00:00
|
|
|
base::string16 title;
|
|
|
|
std::string url;
|
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(args->isolate());
|
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
clipboard->ReadBookmark(&title, &url);
|
|
|
|
dict.Set("title", title);
|
|
|
|
dict.Set("url", url);
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
void Clipboard::WriteBookmark(const base::string16& title,
|
|
|
|
const std::string& url,
|
|
|
|
mate::Arguments* args) {
|
2016-06-24 22:08:12 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
|
|
|
writer.WriteBookmark(title, url);
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
gfx::Image Clipboard::ReadImage(mate::Arguments* args) {
|
2015-06-10 04:12:37 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
SkBitmap bitmap = clipboard->ReadImage(GetClipboardType(args));
|
2015-02-11 06:55:44 +00:00
|
|
|
return gfx::Image::CreateFrom1xBitmap(bitmap);
|
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
void Clipboard::WriteImage(const gfx::Image& image, mate::Arguments* args) {
|
2015-06-10 04:12:37 +00:00
|
|
|
ui::ScopedClipboardWriter writer(GetClipboardType(args));
|
2017-08-08 10:02:20 +00:00
|
|
|
SkBitmap bmp;
|
2017-08-08 10:06:39 +00:00
|
|
|
// TODO(ferreus): Replace with sk_tools_utils::copy_to (chrome60)
|
2017-08-08 10:02:20 +00:00
|
|
|
if (image.AsBitmap().deepCopyTo(&bmp)) {
|
|
|
|
writer.WriteImage(bmp);
|
|
|
|
} else {
|
|
|
|
writer.WriteImage(image.AsBitmap());
|
2017-06-29 13:11:57 +00:00
|
|
|
}
|
2015-03-26 04:54:15 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
#if !defined(OS_MACOSX)
|
|
|
|
void Clipboard::WriteFindText(const base::string16& text) {}
|
2016-10-25 04:59:04 +00:00
|
|
|
base::string16 Clipboard::ReadFindText() { return base::string16(); }
|
2016-10-24 08:13:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void Clipboard::Clear(mate::Arguments* args) {
|
2015-06-10 04:12:37 +00:00
|
|
|
ui::Clipboard::GetForCurrentThread()->Clear(GetClipboardType(args));
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 08:13:34 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-06-29 12:48:44 +00:00
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
2016-10-24 08:13:34 +00:00
|
|
|
dict.SetMethod("availableFormats", &atom::api::Clipboard::AvailableFormats);
|
|
|
|
dict.SetMethod("has", &atom::api::Clipboard::Has);
|
|
|
|
dict.SetMethod("read", &atom::api::Clipboard::Read);
|
|
|
|
dict.SetMethod("write", &atom::api::Clipboard::Write);
|
|
|
|
dict.SetMethod("readText", &atom::api::Clipboard::ReadText);
|
|
|
|
dict.SetMethod("writeText", &atom::api::Clipboard::WriteText);
|
|
|
|
dict.SetMethod("readRTF", &atom::api::Clipboard::ReadRtf);
|
|
|
|
dict.SetMethod("writeRTF", &atom::api::Clipboard::WriteRtf);
|
|
|
|
dict.SetMethod("readHTML", &atom::api::Clipboard::ReadHtml);
|
|
|
|
dict.SetMethod("writeHTML", &atom::api::Clipboard::WriteHtml);
|
|
|
|
dict.SetMethod("readBookmark", &atom::api::Clipboard::ReadBookmark);
|
|
|
|
dict.SetMethod("writeBookmark", &atom::api::Clipboard::WriteBookmark);
|
|
|
|
dict.SetMethod("readImage", &atom::api::Clipboard::ReadImage);
|
|
|
|
dict.SetMethod("writeImage", &atom::api::Clipboard::WriteImage);
|
|
|
|
dict.SetMethod("readFindText", &atom::api::Clipboard::ReadFindText);
|
|
|
|
dict.SetMethod("writeFindText", &atom::api::Clipboard::WriteFindText);
|
2017-03-16 22:42:23 +00:00
|
|
|
dict.SetMethod("readBuffer", &atom::api::Clipboard::ReadBuffer);
|
2017-04-21 05:47:04 +00:00
|
|
|
dict.SetMethod("writeBuffer", &atom::api::Clipboard::WriteBuffer);
|
2016-10-24 08:13:34 +00:00
|
|
|
dict.SetMethod("clear", &atom::api::Clipboard::Clear);
|
2016-05-26 21:23:20 +00:00
|
|
|
|
2016-05-26 21:28:33 +00:00
|
|
|
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
2016-10-24 08:13:34 +00:00
|
|
|
dict.SetMethod("readRtf", &atom::api::Clipboard::ReadRtf);
|
|
|
|
dict.SetMethod("writeRtf", &atom::api::Clipboard::WriteRtf);
|
|
|
|
dict.SetMethod("readHtml", &atom::api::Clipboard::ReadHtml);
|
|
|
|
dict.SetMethod("writeHtml", &atom::api::Clipboard::WriteHtml);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
} // namespace
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_clipboard, Initialize)
|