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.
|
|
|
|
|
|
|
|
#include <string>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <vector>
|
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"
|
2014-04-16 07:31:59 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-02-11 06:55:44 +00:00
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
#include "ui/base/clipboard/clipboard.h"
|
2014-06-28 14:33:00 +00:00
|
|
|
#include "ui/base/clipboard/scoped_clipboard_writer.h"
|
2015-02-11 06:55:44 +00:00
|
|
|
#include "ui/gfx/image/image.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
|
|
|
|
2014-06-05 06:48:12 +00:00
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template<>
|
2014-06-28 14:33:00 +00:00
|
|
|
struct Converter<ui::ClipboardType> {
|
2015-05-22 11:11:22 +00:00
|
|
|
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
|
2014-06-28 14:33:00 +00:00
|
|
|
ui::ClipboardType* out) {
|
2014-06-05 06:48:12 +00:00
|
|
|
std::string type;
|
|
|
|
if (!Converter<std::string>::FromV8(isolate, val, &type))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (type == "selection")
|
2014-06-28 14:33:00 +00:00
|
|
|
*out = ui::CLIPBOARD_TYPE_SELECTION;
|
2014-06-05 07:22:36 +00:00
|
|
|
else
|
2014-06-28 14:33:00 +00:00
|
|
|
*out = ui::CLIPBOARD_TYPE_COPY_PASTE;
|
2014-06-05 06:48:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2014-04-15 07:50:00 +00:00
|
|
|
namespace {
|
2013-04-30 08:10:25 +00:00
|
|
|
|
2015-05-22 09:29:11 +00:00
|
|
|
std::vector<base::string16> AvailableFormats(ui::ClipboardType type) {
|
|
|
|
std::vector<base::string16> format_types;
|
|
|
|
bool ignore;
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
2015-05-22 09:29:11 +00:00
|
|
|
clipboard->ReadAvailableTypes(type, &format_types, &ignore);
|
|
|
|
return format_types;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 08:05:51 +00:00
|
|
|
bool Has(const std::string& format_string, ui::ClipboardType type) {
|
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
|
|
|
return clipboard->IsFormatAvailable(format, type);
|
|
|
|
}
|
|
|
|
|
2014-06-05 06:48:12 +00:00
|
|
|
std::string Read(const std::string& format_string,
|
2014-06-28 14:33:00 +00:00
|
|
|
ui::ClipboardType type) {
|
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
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
base::string16 ReadText(ui::ClipboardType type) {
|
|
|
|
base::string16 data;
|
|
|
|
ui::Clipboard::GetForCurrentThread()->ReadText(type, &data);
|
2014-04-15 07:50:00 +00:00
|
|
|
return data;
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
void WriteText(const base::string16& text, ui::ClipboardType type) {
|
2014-12-07 15:43:26 +00:00
|
|
|
ui::ScopedClipboardWriter writer(type);
|
2014-06-28 14:33:00 +00:00
|
|
|
writer.WriteText(text);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 09:29:11 +00:00
|
|
|
base::string16 ReadHtml(ui::ClipboardType type) {
|
|
|
|
base::string16 data;
|
|
|
|
base::string16 html;
|
|
|
|
std::string url;
|
|
|
|
uint32 start;
|
|
|
|
uint32 end;
|
|
|
|
ui::Clipboard::GetForCurrentThread()->ReadHTML(type, &html, &url,
|
|
|
|
&start, &end);
|
|
|
|
data = html.substr(start, end - start);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteHtml(const base::string16& html,
|
|
|
|
ui::ClipboardType type) {
|
|
|
|
ui::ScopedClipboardWriter writer(type);
|
|
|
|
writer.WriteHTML(html, std::string());
|
|
|
|
}
|
|
|
|
|
2015-02-11 06:55:44 +00:00
|
|
|
gfx::Image ReadImage(ui::ClipboardType type) {
|
|
|
|
SkBitmap bitmap = ui::Clipboard::GetForCurrentThread()->ReadImage(type);
|
|
|
|
return gfx::Image::CreateFrom1xBitmap(bitmap);
|
|
|
|
}
|
|
|
|
|
2015-03-26 04:54:15 +00:00
|
|
|
void WriteImage(const gfx::Image& image, ui::ClipboardType type) {
|
|
|
|
ui::ScopedClipboardWriter writer(type);
|
|
|
|
writer.WriteImage(image.AsBitmap());
|
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
void Clear(ui::ClipboardType type) {
|
|
|
|
ui::Clipboard::GetForCurrentThread()->Clear(type);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-05-22 09:29:11 +00:00
|
|
|
dict.SetMethod("_availableFormats", &AvailableFormats);
|
2015-05-27 08:05:51 +00:00
|
|
|
dict.SetMethod("_has", &Has);
|
2014-06-05 06:48:12 +00:00
|
|
|
dict.SetMethod("_read", &Read);
|
|
|
|
dict.SetMethod("_readText", &ReadText);
|
|
|
|
dict.SetMethod("_writeText", &WriteText);
|
2015-05-22 09:29:11 +00:00
|
|
|
dict.SetMethod("_readHtml", &ReadHtml);
|
|
|
|
dict.SetMethod("_writeHtml", &WriteHtml);
|
2015-02-11 06:55:44 +00:00
|
|
|
dict.SetMethod("_readImage", &ReadImage);
|
2015-03-26 04:54:15 +00:00
|
|
|
dict.SetMethod("_writeImage", &WriteImage);
|
2014-06-05 06:48:12 +00:00
|
|
|
dict.SetMethod("_clear", &Clear);
|
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)
|