Don't rely on JS for setting optional argument

It makes the code much shorter and cleaner.
This commit is contained in:
Cheng Zhao 2015-06-10 12:12:37 +08:00
parent 4b12a95d37
commit db9615f5cd
2 changed files with 41 additions and 64 deletions

View file

@ -7,6 +7,7 @@
#include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/string16_converter.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/clipboard.h"
@ -15,44 +16,32 @@
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
namespace mate {
template<>
struct Converter<ui::ClipboardType> {
static bool FromV8(v8::Isolate* isolate, v8::Local<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;
else
*out = ui::CLIPBOARD_TYPE_COPY_PASTE;
return true;
}
};
} // namespace mate
namespace { namespace {
std::vector<base::string16> AvailableFormats(ui::ClipboardType type) { ui::ClipboardType GetClipboardType(mate::Arguments* args) {
std::string type;
if (args->GetNext(&type) && type == "selection")
return ui::CLIPBOARD_TYPE_SELECTION;
else
return ui::CLIPBOARD_TYPE_COPY_PASTE;
}
std::vector<base::string16> AvailableFormats(mate::Arguments* args) {
std::vector<base::string16> format_types; std::vector<base::string16> format_types;
bool ignore; bool ignore;
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->ReadAvailableTypes(type, &format_types, &ignore); clipboard->ReadAvailableTypes(GetClipboardType(args), &format_types, &ignore);
return format_types; return format_types;
} }
bool Has(const std::string& format_string, ui::ClipboardType type) { bool Has(const std::string& format_string, mate::Arguments* args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string)); ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
return clipboard->IsFormatAvailable(format, type); return clipboard->IsFormatAvailable(format, GetClipboardType(args));
} }
std::string Read(const std::string& format_string, std::string Read(const std::string& format_string,
ui::ClipboardType type) { mate::Arguments* args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string)); ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
@ -61,62 +50,62 @@ std::string Read(const std::string& format_string,
return data; return data;
} }
base::string16 ReadText(ui::ClipboardType type) { base::string16 ReadText(mate::Arguments* args) {
base::string16 data; base::string16 data;
ui::Clipboard::GetForCurrentThread()->ReadText(type, &data); ui::Clipboard::GetForCurrentThread()->ReadText(GetClipboardType(args), &data);
return data; return data;
} }
void WriteText(const base::string16& text, ui::ClipboardType type) { void WriteText(const base::string16& text, mate::Arguments* args) {
ui::ScopedClipboardWriter writer(type); ui::ScopedClipboardWriter writer(GetClipboardType(args));
writer.WriteText(text); writer.WriteText(text);
} }
base::string16 ReadHtml(ui::ClipboardType type) { base::string16 ReadHtml(mate::Arguments* args) {
base::string16 data; base::string16 data;
base::string16 html; base::string16 html;
std::string url; std::string url;
uint32 start; uint32 start;
uint32 end; uint32 end;
ui::Clipboard::GetForCurrentThread()->ReadHTML(type, &html, &url, ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
&start, &end); clipboard->ReadHTML(GetClipboardType(args), &html, &url, &start, &end);
data = html.substr(start, end - start); data = html.substr(start, end - start);
return data; return data;
} }
void WriteHtml(const base::string16& html, void WriteHtml(const base::string16& html, mate::Arguments* args) {
ui::ClipboardType type) { ui::ScopedClipboardWriter writer(GetClipboardType(args));
ui::ScopedClipboardWriter writer(type);
writer.WriteHTML(html, std::string()); writer.WriteHTML(html, std::string());
} }
gfx::Image ReadImage(ui::ClipboardType type) { gfx::Image ReadImage(mate::Arguments* args) {
SkBitmap bitmap = ui::Clipboard::GetForCurrentThread()->ReadImage(type); ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
SkBitmap bitmap = clipboard->ReadImage(GetClipboardType(args));
return gfx::Image::CreateFrom1xBitmap(bitmap); return gfx::Image::CreateFrom1xBitmap(bitmap);
} }
void WriteImage(const gfx::Image& image, ui::ClipboardType type) { void WriteImage(const gfx::Image& image, mate::Arguments* args) {
ui::ScopedClipboardWriter writer(type); ui::ScopedClipboardWriter writer(GetClipboardType(args));
writer.WriteImage(image.AsBitmap()); writer.WriteImage(image.AsBitmap());
} }
void Clear(ui::ClipboardType type) { void Clear(mate::Arguments* args) {
ui::Clipboard::GetForCurrentThread()->Clear(type); ui::Clipboard::GetForCurrentThread()->Clear(GetClipboardType(args));
} }
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) { v8::Local<v8::Context> context, void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports); mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("_availableFormats", &AvailableFormats); dict.SetMethod("availableFormats", &AvailableFormats);
dict.SetMethod("_has", &Has); dict.SetMethod("has", &Has);
dict.SetMethod("_read", &Read); dict.SetMethod("read", &Read);
dict.SetMethod("_readText", &ReadText); dict.SetMethod("readText", &ReadText);
dict.SetMethod("_writeText", &WriteText); dict.SetMethod("writeText", &WriteText);
dict.SetMethod("_readHtml", &ReadHtml); dict.SetMethod("readHtml", &ReadHtml);
dict.SetMethod("_writeHtml", &WriteHtml); dict.SetMethod("writeHtml", &WriteHtml);
dict.SetMethod("_readImage", &ReadImage); dict.SetMethod("readImage", &ReadImage);
dict.SetMethod("_writeImage", &WriteImage); dict.SetMethod("writeImage", &WriteImage);
dict.SetMethod("_clear", &Clear); dict.SetMethod("clear", &Clear);
} }
} // namespace } // namespace

View file

@ -2,16 +2,4 @@ if process.platform is 'linux' and process.type is 'renderer'
# On Linux we could not access clipboard in renderer process. # On Linux we could not access clipboard in renderer process.
module.exports = require('remote').require 'clipboard' module.exports = require('remote').require 'clipboard'
else else
binding = process.atomBinding 'clipboard' module.exports = process.atomBinding 'clipboard'
module.exports =
availableFormats: (type='standard') -> binding._availableFormats type
has: (format, type='standard') -> binding._has format, type
read: (format, type='standard') -> binding._read format, type
readText: (type='standard') -> binding._readText type
writeText: (text, type='standard') -> binding._writeText text, type
readHtml: (type='standard') -> binding._readHtml type
writeHtml: (markup, type='standard') -> binding._writeHtml markup, type
readImage: (type='standard') -> binding._readImage type
writeImage: (image, type='standard') -> binding._writeImage image, type
clear: (type='standard') -> binding._clear type