electron/atom/common/api/atom_api_clipboard.cc

83 lines
2.4 KiB
C++
Raw Normal View History

// 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>
#include <vector>
2013-04-30 08:10:25 +00:00
#include "atom/common/native_mate_converters/string16_converter.h"
2014-04-16 07:31:59 +00:00
#include "native_mate/dictionary.h"
2013-04-30 08:10:25 +00:00
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "atom/common/node_includes.h"
2013-04-30 08:10:25 +00:00
namespace mate {
template<>
struct Converter<ui::ClipboardType> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
ui::ClipboardType* out) {
std::string type;
if (!Converter<std::string>::FromV8(isolate, val, &type))
return false;
if (type == "selection")
*out = ui::CLIPBOARD_TYPE_SELECTION;
2014-06-05 07:22:36 +00:00
else
*out = ui::CLIPBOARD_TYPE_COPY_PASTE;
return true;
}
};
} // namespace mate
namespace {
2013-04-30 08:10:25 +00:00
bool Has(const std::string& format_string, 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));
return clipboard->IsFormatAvailable(format, type);
2013-04-30 08:10:25 +00:00
}
std::string Read(const std::string& format_string,
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);
return data;
2013-04-30 08:10:25 +00:00
}
base::string16 ReadText(ui::ClipboardType type) {
base::string16 data;
ui::Clipboard::GetForCurrentThread()->ReadText(type, &data);
return data;
2013-04-30 08:10:25 +00:00
}
void WriteText(const base::string16& text, ui::ClipboardType type) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::ScopedClipboardWriter writer(clipboard, type);
writer.WriteText(text);
2013-04-30 08:10:25 +00:00
}
void Clear(ui::ClipboardType type) {
ui::Clipboard::GetForCurrentThread()->Clear(type);
2013-04-30 08:10:25 +00:00
}
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
v8::Handle<v8::Context> context, void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("_has", &Has);
dict.SetMethod("_read", &Read);
dict.SetMethod("_readText", &ReadText);
dict.SetMethod("_writeText", &WriteText);
dict.SetMethod("_clear", &Clear);
2013-04-30 08:10:25 +00:00
}
} // namespace
2013-04-30 08:10:25 +00:00
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_clipboard, Initialize)