2013-04-30 08:10:25 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "common/api/atom_api_clipboard.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "common/v8/native_type_conversions.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
#include "ui/base/clipboard/clipboard.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
|
|
|
#include "common/v8/node_common.h"
|
2013-04-30 08:10:25 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Clipboard::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
std::string format_string;
|
|
|
|
if (!FromV8Arguments(args, &format_string))
|
2013-04-30 08:10:25 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
args.GetReturnValue().Set(
|
|
|
|
clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD));
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Clipboard::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
std::string format_string;
|
|
|
|
if (!FromV8Arguments(args, &format_string))
|
2013-04-30 08:10:25 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
clipboard->ReadData(format, &data);
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
args.GetReturnValue().Set(ToV8Value(data));
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Clipboard::ReadText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-30 08:10:25 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
args.GetReturnValue().Set(ToV8Value(data));
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Clipboard::WriteText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
std::string text;
|
|
|
|
if (!FromV8Arguments(args, &text))
|
2013-04-30 08:10:25 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
|
|
|
ui::Clipboard::ObjectMap object_map;
|
|
|
|
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
|
|
|
std::vector<char>(text.begin(), text.end()));
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
|
|
|
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void Clipboard::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void Clipboard::Initialize(v8::Handle<v8::Object> target) {
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
|
|
|
|
|
|
|
NODE_SET_METHOD(target, "has", Has);
|
|
|
|
NODE_SET_METHOD(target, "read", Read);
|
|
|
|
NODE_SET_METHOD(target, "readText", ReadText);
|
|
|
|
NODE_SET_METHOD(target, "writeText", WriteText);
|
|
|
|
NODE_SET_METHOD(target, "clear", Clear);
|
2013-04-30 08:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
NODE_MODULE(atom_common_clipboard, atom::api::Clipboard::Initialize)
|