diff --git a/atom.gyp b/atom.gyp index 9813108306b..fd4c7467587 100644 --- a/atom.gyp +++ b/atom.gyp @@ -12,6 +12,7 @@ 'browser/atom/atom.coffee', 'browser/atom/objects_registry.coffee', 'browser/atom/rpc_server.coffee', + 'common/api/lib/clipboard.coffee', 'common/api/lib/id_weak_map.coffee', 'common/api/lib/shell.coffee', 'renderer/api/lib/ipc.coffee', @@ -45,6 +46,8 @@ 'browser/native_window_observer.h', 'common/api/api_messages.cc', 'common/api/api_messages.h', + 'common/api/atom_api_clipboard.cc', + 'common/api/atom_api_clipboard.h', 'common/api/atom_api_idle_gc.cc', 'common/api/atom_api_id_weak_map.cc', 'common/api/atom_api_id_weak_map.h', diff --git a/common/api/atom_api_clipboard.cc b/common/api/atom_api_clipboard.cc new file mode 100644 index 00000000000..4c26750d2cd --- /dev/null +++ b/common/api/atom_api_clipboard.cc @@ -0,0 +1,101 @@ +// 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 + +#include "ui/base/clipboard/clipboard.h" +#include "vendor/node/src/node.h" + +namespace atom { + +namespace api { + +// static +v8::Handle Clipboard::Has(const v8::Arguments &args) { + v8::HandleScope scope; + + if (!args[0]->IsString()) + return node::ThrowTypeError("Bad argument"); + + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + + std::string format_string(*v8::String::Utf8Value(args[0])); + ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string)); + + return scope.Close(v8::Boolean::New( + clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD))); +} + +// static +v8::Handle Clipboard::Read(const v8::Arguments &args) { + v8::HandleScope scope; + + if (!args[0]->IsString()) + return node::ThrowTypeError("Bad argument"); + + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + + std::string format_string(*v8::String::Utf8Value(args[0])); + ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string)); + + std::string data; + clipboard->ReadData(format, &data); + + return scope.Close(v8::String::New(data.c_str(), data.size())); +} + +// static +v8::Handle Clipboard::ReadText(const v8::Arguments &args) { + v8::HandleScope scope; + + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + + std::string data; + clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data); + + return scope.Close(v8::String::New(data.c_str(), data.size())); +} + +// static +v8::Handle Clipboard::WriteText(const v8::Arguments &args) { + v8::HandleScope scope; + + if (!args[0]->IsString()) + return node::ThrowTypeError("Bad argument"); + + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + std::string text(*v8::String::Utf8Value(args[0])); + + ui::Clipboard::ObjectMap object_map; + object_map[ui::Clipboard::CBF_TEXT].push_back( + std::vector(text.begin(), text.end())); + clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map, NULL); + + return v8::Undefined(); +} + +// static +v8::Handle Clipboard::Clear(const v8::Arguments &args) { + ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); + clipboard->Clear(ui::Clipboard::BUFFER_STANDARD); + + return v8::Undefined(); +} + +// static +void Clipboard::Initialize(v8::Handle target) { + node::SetMethod(target, "has", Has); + node::SetMethod(target, "read", Read); + node::SetMethod(target, "readText", ReadText); + node::SetMethod(target, "writeText", WriteText); + node::SetMethod(target, "clear", Clear); +} + +} // namespace api + +} // namespace atom + +NODE_MODULE(atom_common_clipboard, atom::api::Clipboard::Initialize) diff --git a/common/api/atom_api_clipboard.h b/common/api/atom_api_clipboard.h new file mode 100644 index 00000000000..38495a0db2f --- /dev/null +++ b/common/api/atom_api_clipboard.h @@ -0,0 +1,33 @@ +// 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. + +#ifndef ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_ +#define ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_ + +#include "base/basictypes.h" +#include "v8/include/v8.h" + +namespace atom { + +namespace api { + +class Clipboard { + public: + static void Initialize(v8::Handle target); + + private: + static v8::Handle Has(const v8::Arguments &args); + static v8::Handle Read(const v8::Arguments &args); + static v8::Handle ReadText(const v8::Arguments &args); + static v8::Handle WriteText(const v8::Arguments &args); + static v8::Handle Clear(const v8::Arguments &args); + + DISALLOW_IMPLICIT_CONSTRUCTORS(Clipboard); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_ diff --git a/common/api/atom_extensions.h b/common/api/atom_extensions.h index 1aa20408a66..a511d4ba91b 100644 --- a/common/api/atom_extensions.h +++ b/common/api/atom_extensions.h @@ -18,6 +18,7 @@ NODE_EXT_LIST_ITEM(atom_renderer_ipc) // Module names start with `atom_common_` can be used by both browser and // renderer processes. +NODE_EXT_LIST_ITEM(atom_common_clipboard) NODE_EXT_LIST_ITEM(atom_common_idle_gc) NODE_EXT_LIST_ITEM(atom_common_id_weak_map) NODE_EXT_LIST_ITEM(atom_common_shell) diff --git a/common/api/lib/clipboard.coffee b/common/api/lib/clipboard.coffee new file mode 100644 index 00000000000..d83a5a5944c --- /dev/null +++ b/common/api/lib/clipboard.coffee @@ -0,0 +1 @@ +module.exports = process.atomBinding 'clipboard'