From d9cd50c219abd1a6bf6a2d459b433a49dbbcd79a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 15 Apr 2014 15:50:00 +0800 Subject: [PATCH] Use native_mate to simplify clipboard api. --- atom.gyp | 1 - atom/common/api/atom_api_clipboard.cc | 66 ++++++++------------------- atom/common/api/atom_api_clipboard.h | 33 -------------- atom/common/api/atom_api_shell.cc | 2 - 4 files changed, 20 insertions(+), 82 deletions(-) delete mode 100644 atom/common/api/atom_api_clipboard.h diff --git a/atom.gyp b/atom.gyp index b4caea84d89c..990924509b05 100644 --- a/atom.gyp +++ b/atom.gyp @@ -143,7 +143,6 @@ 'atom/common/api/api_messages.cc', 'atom/common/api/api_messages.h', 'atom/common/api/atom_api_clipboard.cc', - 'atom/common/api/atom_api_clipboard.h', 'atom/common/api/atom_api_crash_reporter.cc', 'atom/common/api/atom_api_crash_reporter.h', 'atom/common/api/atom_api_event_emitter.cc', diff --git a/atom/common/api/atom_api_clipboard.cc b/atom/common/api/atom_api_clipboard.cc index a48ec9687ae5..d41e0ff8859b 100644 --- a/atom/common/api/atom_api_clipboard.cc +++ b/atom/common/api/atom_api_clipboard.cc @@ -2,64 +2,40 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "atom/common/api/atom_api_clipboard.h" - #include #include -#include "atom/common/v8/native_type_conversions.h" +#include "native_mate/object_template_builder.h" #include "ui/base/clipboard/clipboard.h" #include "atom/common/v8/node_common.h" -namespace atom { - -namespace api { - -// static -void Clipboard::Has(const v8::FunctionCallbackInfo& args) { - std::string format_string; - if (!FromV8Arguments(args, &format_string)) - return node::ThrowTypeError("Bad argument"); +namespace { +bool Has(const std::string& format_string) { ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string)); - - args.GetReturnValue().Set( - clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD)); + return clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD); } -// static -void Clipboard::Read(const v8::FunctionCallbackInfo& args) { - std::string format_string; - if (!FromV8Arguments(args, &format_string)) - return node::ThrowTypeError("Bad argument"); - +std::string Read(const std::string& format_string) { ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string)); std::string data; clipboard->ReadData(format, &data); - - args.GetReturnValue().Set(ToV8Value(data)); + return data; } -// static -void Clipboard::ReadText(const v8::FunctionCallbackInfo& args) { +std::string ReadText() { ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); std::string data; clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data); - - args.GetReturnValue().Set(ToV8Value(data)); + return data; } -// static -void Clipboard::WriteText(const v8::FunctionCallbackInfo& args) { - std::string text; - if (!FromV8Arguments(args, &text)) - return node::ThrowTypeError("Bad argument"); - +void WriteText(const std::string text) { ui::Clipboard::ObjectMap object_map; object_map[ui::Clipboard::CBF_TEXT].push_back( std::vector(text.begin(), text.end())); @@ -68,22 +44,20 @@ void Clipboard::WriteText(const v8::FunctionCallbackInfo& args) { clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map); } -// static -void Clipboard::Clear(const v8::FunctionCallbackInfo& args) { +void Clear() { ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD); } -// static -void Clipboard::Initialize(v8::Handle target) { - 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); +void Initialize(v8::Handle exports) { + mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent()); + builder.SetMethod("has", &Has) + .SetMethod("read", &Read) + .SetMethod("readText", &ReadText) + .SetMethod("writeText", &WriteText) + .SetMethod("clear", &Clear); + exports->SetPrototype(builder.Build()->NewInstance()); } -} // namespace api +} // namespace -} // namespace atom - -NODE_MODULE(atom_common_clipboard, atom::api::Clipboard::Initialize) +NODE_MODULE(atom_common_clipboard, Initialize) diff --git a/atom/common/api/atom_api_clipboard.h b/atom/common/api/atom_api_clipboard.h deleted file mode 100644 index 70360d28ad25..000000000000 --- a/atom/common/api/atom_api_clipboard.h +++ /dev/null @@ -1,33 +0,0 @@ -// 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 void Has(const v8::FunctionCallbackInfo& args); - static void Read(const v8::FunctionCallbackInfo& args); - static void ReadText(const v8::FunctionCallbackInfo& args); - static void WriteText(const v8::FunctionCallbackInfo& args); - static void Clear(const v8::FunctionCallbackInfo& args); - - DISALLOW_IMPLICIT_CONSTRUCTORS(Clipboard); -}; - -} // namespace api - -} // namespace atom - -#endif // ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_ diff --git a/atom/common/api/atom_api_shell.cc b/atom/common/api/atom_api_shell.cc index 222e086c8335..a58bc74aaa8c 100644 --- a/atom/common/api/atom_api_shell.cc +++ b/atom/common/api/atom_api_shell.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "atom/common/api/atom_api_shell.h" - #include #include "atom/common/platform_util.h"