// 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 SHELL_COMMON_API_ATOM_API_NATIVE_IMAGE_H_ #define SHELL_COMMON_API_ATOM_API_NATIVE_IMAGE_H_ #include #include #include "base/values.h" #include "gin/handle.h" #include "shell/common/gin_helper/error_thrower.h" #include "shell/common/gin_helper/wrappable.h" #include "ui/gfx/image/image.h" #if defined(OS_WIN) #include "base/files/file_path.h" #include "base/win/scoped_gdi_object.h" #endif class GURL; namespace base { class FilePath; } namespace gfx { class Rect; class Size; } // namespace gfx namespace gin_helper { class Dictionary; } namespace electron { namespace api { class NativeImage : public gin_helper::Wrappable { public: static gin::Handle CreateEmpty(v8::Isolate* isolate); static gin::Handle Create(v8::Isolate* isolate, const gfx::Image& image); static gin::Handle CreateFromPNG(v8::Isolate* isolate, const char* buffer, size_t length); static gin::Handle CreateFromJPEG(v8::Isolate* isolate, const char* buffer, size_t length); static gin::Handle CreateFromPath(v8::Isolate* isolate, const base::FilePath& path); static gin::Handle CreateFromBitmap( gin_helper::ErrorThrower thrower, v8::Local buffer, const gin_helper::Dictionary& options); static gin::Handle CreateFromBuffer( gin_helper::ErrorThrower thrower, v8::Local buffer, gin::Arguments* args); static gin::Handle CreateFromDataURL(v8::Isolate* isolate, const GURL& url); static gin::Handle CreateFromNamedImage(gin::Arguments* args, const std::string& name); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); #if defined(OS_WIN) HICON GetHICON(int size); #endif const gfx::Image& image() const { return image_; } protected: NativeImage(v8::Isolate* isolate, const gfx::Image& image); #if defined(OS_WIN) NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path); #endif ~NativeImage() override; private: v8::Local ToPNG(gin::Arguments* args); v8::Local ToJPEG(v8::Isolate* isolate, int quality); v8::Local ToBitmap(gin::Arguments* args); v8::Local GetBitmap(gin::Arguments* args); v8::Local GetNativeHandle(gin_helper::ErrorThrower thrower); gin::Handle Resize(v8::Isolate* isolate, base::DictionaryValue options); gin::Handle Crop(v8::Isolate* isolate, const gfx::Rect& rect); std::string ToDataURL(gin::Arguments* args); bool IsEmpty(); gfx::Size GetSize(); float GetAspectRatio(); void AddRepresentation(const gin_helper::Dictionary& options); // Mark the image as template image. void SetTemplateImage(bool setAsTemplate); // Determine if the image is a template image. bool IsTemplateImage(); #if defined(OS_WIN) base::FilePath hicon_path_; std::map hicons_; #endif gfx::Image image_; DISALLOW_COPY_AND_ASSIGN(NativeImage); }; } // namespace api } // namespace electron namespace gin { // A custom converter that allows converting path to NativeImage. template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, electron::api::NativeImage* val); static bool FromV8(v8::Isolate* isolate, v8::Local val, electron::api::NativeImage** out); }; } // namespace gin #endif // SHELL_COMMON_API_ATOM_API_NATIVE_IMAGE_H_