From aae576b48434385185902cdfb6777eab502aadbd Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 11 Feb 2015 16:03:19 +0800 Subject: [PATCH] Convert gfx::Image to NativeImage instance --- atom.gyp | 2 + atom/common/api/atom_api_native_image.cc | 65 +++++++++++++++++++ atom/common/api/atom_api_native_image.h | 41 ++++++++++++ .../native_mate_converters/image_converter.cc | 6 +- atom/common/node_bindings.cc | 1 + 5 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 atom/common/api/atom_api_native_image.cc create mode 100644 atom/common/api/atom_api_native_image.h diff --git a/atom.gyp b/atom.gyp index c83529a5d211..3c161b45d119 100644 --- a/atom.gyp +++ b/atom.gyp @@ -204,6 +204,8 @@ 'atom/common/api/atom_api_crash_reporter.cc', 'atom/common/api/atom_api_id_weak_map.cc', 'atom/common/api/atom_api_id_weak_map.h', + 'atom/common/api/atom_api_native_image.cc', + 'atom/common/api/atom_api_native_image.h', 'atom/common/api/atom_api_shell.cc', 'atom/common/api/atom_api_v8_util.cc', 'atom/common/api/atom_bindings.cc', diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc new file mode 100644 index 000000000000..def6d0917577 --- /dev/null +++ b/atom/common/api/atom_api_native_image.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/common/api/atom_api_native_image.h" + +#include "atom/common/native_mate_converters/image_converter.h" +#include "native_mate/constructor.h" +#include "native_mate/dictionary.h" +#include "native_mate/object_template_builder.h" + +#include "atom/common/node_includes.h" + +namespace atom { + +namespace api { + +namespace { + +v8::Persistent template_; + +} // namespace + +NativeImage::NativeImage(const gfx::Image& image) : image_(image) {} + +NativeImage::~NativeImage() {} + +mate::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + if (template_.IsEmpty()) + template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate) + .SetMethod("toPng", &NativeImage::ToPNG) + .Build()); + + return mate::ObjectTemplateBuilder( + isolate, v8::Local::New(isolate, template_)); +} + +v8::Handle NativeImage::ToPNG(v8::Isolate* isolate) { + scoped_refptr png = image_.As1xPNGBytes(); + return node::Buffer::New(isolate, + reinterpret_cast(png->front()), + png->size()); +} + +// static +mate::Handle NativeImage::Create(v8::Isolate* isolate, + const gfx::Image& image) { + return mate::CreateHandle(isolate, new NativeImage(image)); +} + +} // namespace api + +} // namespace atom + + +namespace { + +void Initialize(v8::Handle exports, v8::Handle unused, + v8::Handle context, void* priv) { +} + +} // namespace + +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_native_image, Initialize) diff --git a/atom/common/api/atom_api_native_image.h b/atom/common/api/atom_api_native_image.h new file mode 100644 index 000000000000..4d1978e69e49 --- /dev/null +++ b/atom/common/api/atom_api_native_image.h @@ -0,0 +1,41 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_ +#define ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_ + +#include "native_mate/handle.h" +#include "native_mate/wrappable.h" +#include "ui/gfx/image/image.h" + +namespace atom { + +namespace api { + +class NativeImage : public mate::Wrappable { + public: + static mate::Handle Create(v8::Isolate* isolate, + const gfx::Image& image); + + protected: + NativeImage(const gfx::Image& image); + virtual ~NativeImage(); + + // mate::Wrappable: + mate::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override; + + private: + v8::Handle ToPNG(v8::Isolate* isolate); + + gfx::Image image_; + + DISALLOW_COPY_AND_ASSIGN(NativeImage); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_ diff --git a/atom/common/native_mate_converters/image_converter.cc b/atom/common/native_mate_converters/image_converter.cc index 2c991b9236c0..baa79055b8b5 100644 --- a/atom/common/native_mate_converters/image_converter.cc +++ b/atom/common/native_mate_converters/image_converter.cc @@ -7,6 +7,7 @@ #include #include +#include "atom/common/api/atom_api_native_image.h" #include "atom/common/native_mate_converters/file_path_converter.h" #include "base/files/file_util.h" #include "base/strings/string_util.h" @@ -128,10 +129,7 @@ bool Converter::FromV8(v8::Isolate* isolate, v8::Handle Converter::ToV8(v8::Isolate* isolate, const gfx::Image& val) { - scoped_refptr png = val.As1xPNGBytes(); - return node::Buffer::New(isolate, - reinterpret_cast(png->front()), - png->size()); + return ConvertToV8(isolate, atom::api::NativeImage::Create(isolate, val)); } } // namespace mate diff --git a/atom/common/node_bindings.cc b/atom/common/node_bindings.cc index 20ac2b3d434d..7e9c8ffdc6bc 100644 --- a/atom/common/node_bindings.cc +++ b/atom/common/node_bindings.cc @@ -72,6 +72,7 @@ REFERENCE_MODULE(atom_common_asar); REFERENCE_MODULE(atom_common_clipboard); REFERENCE_MODULE(atom_common_crash_reporter); REFERENCE_MODULE(atom_common_id_weak_map); +REFERENCE_MODULE(atom_common_native_image); REFERENCE_MODULE(atom_common_screen); REFERENCE_MODULE(atom_common_shell); REFERENCE_MODULE(atom_common_v8_util);