]> khamidou — GitHub mirrors - electron.git/commitdiff
Convert gfx::Image to NativeImage instance
authorCheng Zhao <zcbenz@gmail.com>
Wed, 11 Feb 2015 08:03:19 +0000 (16:03 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 12 Feb 2015 02:45:03 +0000 (10:45 +0800)
atom.gyp
atom/common/api/atom_api_native_image.cc [new file with mode: 0644]
atom/common/api/atom_api_native_image.h [new file with mode: 0644]
atom/common/native_mate_converters/image_converter.cc
atom/common/node_bindings.cc

index c83529a5d21112fece802bf3b3bd3b3a0979cbde..3c161b45d1199b6c50723ae9edf4dc858eccc284 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       '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 (file)
index 0000000..def6d09
--- /dev/null
@@ -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<v8::ObjectTemplate> 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<v8::ObjectTemplate>::New(isolate, template_));
+}
+
+v8::Handle<v8::Value> NativeImage::ToPNG(v8::Isolate* isolate) {
+  scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes();
+  return node::Buffer::New(isolate,
+                           reinterpret_cast<const char*>(png->front()),
+                           png->size());
+}
+
+// static
+mate::Handle<NativeImage> 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<v8::Object> exports, v8::Handle<v8::Value> unused,
+                v8::Handle<v8::Context> 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 (file)
index 0000000..4d1978e
--- /dev/null
@@ -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<NativeImage> 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<v8::Value> 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_
index 2c991b9236c0b62a62ef6c34446412b49d86e772..baa79055b8b58732d0f6b3d59e56ebd9dac477a3 100644 (file)
@@ -7,6 +7,7 @@
 #include <string>
 #include <vector>
 
+#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<gfx::Image>::FromV8(v8::Isolate* isolate,
 
 v8::Handle<v8::Value> Converter<gfx::Image>::ToV8(v8::Isolate* isolate,
                                                   const gfx::Image& val) {
-  scoped_refptr<base::RefCountedMemory> png = val.As1xPNGBytes();
-  return node::Buffer::New(isolate,
-                           reinterpret_cast<const char*>(png->front()),
-                           png->size());
+  return ConvertToV8(isolate, atom::api::NativeImage::Create(isolate, val));
 }
 
 }  // namespace mate
index 20ac2b3d434df7c877d1a2338ac38bb5ceea9a81..7e9c8ffdc6bc9333e71f999400395bd8f521ce3c 100644 (file)
@@ -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);