Store ico image in HCION

This commit is contained in:
Cheng Zhao 2016-05-20 11:31:02 +09:00
parent d5f3e5d59a
commit 7c34d8333c
2 changed files with 38 additions and 14 deletions

View file

@ -142,7 +142,7 @@ bool IsTemplateFilename(const base::FilePath& path) {
#endif #endif
#if defined(OS_WIN) #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 // If file is in asar archive, we extract it to a temp file so LoadImage can
// load it. // load it.
base::FilePath asar_path, relative_path; base::FilePath asar_path, relative_path;
@ -155,14 +155,14 @@ bool ReadImageSkiaFromICO(gfx::ImageSkia* image, const base::FilePath& path) {
} }
// Load the icon from file. // Load the icon from file.
base::win::ScopedHICON icon(static_cast<HICON>( return base::win::ScopedHICON(static_cast<HICON>(
LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, 0, 0, LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, 0, 0,
LR_DEFAULTSIZE | LR_LOADFROMFILE))); 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. // Convert the icon from the Windows specific HICON to gfx::ImageSkia.
scoped_ptr<SkBitmap> bitmap(IconUtil:: CreateSkBitmapFromHICON(icon.get())); scoped_ptr<SkBitmap> bitmap(IconUtil:: CreateSkBitmapFromHICON(icon));
image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f)); image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f));
return true; return true;
} }
@ -175,6 +175,18 @@ NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
Init(isolate); 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() {} NativeImage::~NativeImage() {}
v8::Local<v8::Value> NativeImage::ToPNG(v8::Isolate* isolate) { v8::Local<v8::Value> NativeImage::ToPNG(v8::Isolate* isolate) {
@ -263,23 +275,24 @@ mate::Handle<NativeImage> NativeImage::CreateFromJPEG(
// static // static
mate::Handle<NativeImage> NativeImage::CreateFromPath( mate::Handle<NativeImage> NativeImage::CreateFromPath(
v8::Isolate* isolate, const base::FilePath& path) { v8::Isolate* isolate, const base::FilePath& path) {
gfx::ImageSkia image_skia;
base::FilePath image_path = NormalizePath(path); base::FilePath image_path = NormalizePath(path);
if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) { if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
#if defined(OS_WIN) #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 #endif
} else { } else {
gfx::ImageSkia image_skia;
PopulateImageSkiaRepsFromPath(&image_skia, image_path); PopulateImageSkiaRepsFromPath(&image_skia, image_path);
} gfx::Image image(image_skia);
gfx::Image image(image_skia); mate::Handle<NativeImage> handle = Create(isolate, image);
mate::Handle<NativeImage> handle = Create(isolate, image);
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
if (IsTemplateFilename(image_path)) if (IsTemplateFilename(image_path))
handle->SetTemplateImage(true); handle->SetTemplateImage(true);
#endif #endif
return handle; return handle;
}
} }
// static // static

View file

@ -11,6 +11,10 @@
#include "native_mate/wrappable.h" #include "native_mate/wrappable.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#if defined(OS_WIN)
#include "base/win/scoped_gdi_object.h"
#endif
class GURL; class GURL;
namespace base { namespace base {
@ -52,6 +56,9 @@ class NativeImage : public mate::Wrappable<NativeImage> {
protected: protected:
NativeImage(v8::Isolate* isolate, const gfx::Image& image); NativeImage(v8::Isolate* isolate, const gfx::Image& image);
#if defined(OS_WIN)
NativeImage(v8::Isolate* isolate, base::win::ScopedHICON&& hicon);
#endif
~NativeImage() override; ~NativeImage() override;
private: private:
@ -69,6 +76,10 @@ class NativeImage : public mate::Wrappable<NativeImage> {
// Determine if the image is a template image. // Determine if the image is a template image.
bool IsTemplateImage(); bool IsTemplateImage();
#if defined(OS_WIN)
base::win::ScopedHICON hicon_;
#endif
gfx::Image image_; gfx::Image image_;
DISALLOW_COPY_AND_ASSIGN(NativeImage); DISALLOW_COPY_AND_ASSIGN(NativeImage);