Browse Source

Merge pull request #6278 from electron/ico-native-image-crash

Fix nativeImage.createFromPath for non-existent .ico
Kevin Sawicki 8 years ago
parent
commit
49bec9165c

+ 5 - 1
atom/common/api/atom_api_native_image.cc

@@ -160,10 +160,14 @@ base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {
                 LR_LOADFROMFILE)));
 }
 
-void ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
+bool ReadImageSkiaFromICO(gfx::ImageSkia* image, HICON icon) {
   // Convert the icon from the Windows specific HICON to gfx::ImageSkia.
   std::unique_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon));
+  if (!bitmap)
+    return false;
+
   image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f));
+  return true;
 }
 #endif
 

+ 11 - 0
spec/api-native-image-spec.js

@@ -9,6 +9,7 @@ describe('nativeImage module', () => {
     it('returns an empty image for invalid paths', () => {
       assert(nativeImage.createFromPath('').isEmpty())
       assert(nativeImage.createFromPath('does-not-exist.png').isEmpty())
+      assert(nativeImage.createFromPath('does-not-exist.ico').isEmpty())
     })
 
     it('loads images from paths relative to the current working directory', () => {
@@ -47,5 +48,15 @@ describe('nativeImage module', () => {
       // If all bytes are null, that's Bad
       assert.equal(nsimage.reduce((acc, x) => acc || (x !== 0), false), true)
     })
+
+    it('loads images from .ico files on Windows', () => {
+      if (process.platform !== 'win32') return
+
+      const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')
+      const image = nativeImage.createFromPath(imagePath)
+      assert(!image.isEmpty())
+      assert.equal(image.getSize().height, 256)
+      assert.equal(image.getSize().width, 256)
+    })
   })
 })

BIN
spec/fixtures/assets/icon.ico