diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index 97cc674a3115..1bbb9d9950ac 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -142,7 +142,7 @@ bool IsTemplateFilename(const base::FilePath& path) { #endif #if defined(OS_WIN) -bool ReadImageSkiaFromICO(gfx::ImageSkia* image, const base::FilePath& path) { +base::win::ScopedHICON ReadICOFromPath(const base::FilePath& path) { // If file is in asar archive, we extract it to a temp file so LoadImage can // load it. base::FilePath asar_path, relative_path; @@ -155,14 +155,14 @@ bool ReadImageSkiaFromICO(gfx::ImageSkia* image, const base::FilePath& path) { } // Load the icon from file. - base::win::ScopedHICON icon(static_cast( + return base::win::ScopedHICON(static_cast( LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE))); - if (!icon.get()) - return false; +} +bool ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) { // Convert the icon from the Windows specific HICON to gfx::ImageSkia. - scoped_ptr bitmap(IconUtil:: CreateSkBitmapFromHICON(icon.get())); + scoped_ptr bitmap(IconUtil:: CreateSkBitmapFromHICON(icon)); image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f)); return true; } @@ -175,6 +175,18 @@ NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image) Init(isolate); } +#if defined(OS_WIN) +NativeImage::NativeImage(v8::Isolate* isolate, base::win::ScopedHICON&& hicon) + : hicon_(std::move(hicon)) { + if (hicon.get()) { + gfx::ImageSkia image_skia; + ReadImageSkiaFromICO(&image_skia, hicon.get()); + image_ = gfx::Image(image_skia); + } + Init(isolate); +} +#endif + NativeImage::~NativeImage() {} v8::Local NativeImage::ToPNG(v8::Isolate* isolate) { @@ -263,23 +275,24 @@ mate::Handle NativeImage::CreateFromJPEG( // static mate::Handle NativeImage::CreateFromPath( v8::Isolate* isolate, const base::FilePath& path) { - gfx::ImageSkia image_skia; base::FilePath image_path = NormalizePath(path); - if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) { #if defined(OS_WIN) - ReadImageSkiaFromICO(&image_skia, image_path); + base::win::ScopedHICON hicon = ReadICOFromPath(image_path); + return mate::CreateHandle(isolate, + new NativeImage(isolate, std::move(hicon))); #endif } else { + gfx::ImageSkia image_skia; PopulateImageSkiaRepsFromPath(&image_skia, image_path); - } - gfx::Image image(image_skia); - mate::Handle handle = Create(isolate, image); + gfx::Image image(image_skia); + mate::Handle handle = Create(isolate, image); #if defined(OS_MACOSX) - if (IsTemplateFilename(image_path)) - handle->SetTemplateImage(true); + if (IsTemplateFilename(image_path)) + handle->SetTemplateImage(true); #endif - return handle; + return handle; + } } // static diff --git a/atom/common/api/atom_api_native_image.h b/atom/common/api/atom_api_native_image.h index 79844604706c..981f3b8092bc 100644 --- a/atom/common/api/atom_api_native_image.h +++ b/atom/common/api/atom_api_native_image.h @@ -11,6 +11,10 @@ #include "native_mate/wrappable.h" #include "ui/gfx/image/image.h" +#if defined(OS_WIN) +#include "base/win/scoped_gdi_object.h" +#endif + class GURL; namespace base { @@ -52,6 +56,9 @@ class NativeImage : public mate::Wrappable { protected: NativeImage(v8::Isolate* isolate, const gfx::Image& image); +#if defined(OS_WIN) + NativeImage(v8::Isolate* isolate, base::win::ScopedHICON&& hicon); +#endif ~NativeImage() override; private: @@ -69,6 +76,10 @@ class NativeImage : public mate::Wrappable { // Determine if the image is a template image. bool IsTemplateImage(); +#if defined(OS_WIN) + base::win::ScopedHICON hicon_; +#endif + gfx::Image image_; DISALLOW_COPY_AND_ASSIGN(NativeImage);