Use MakeAbsoluteFilePath when creating native image from path

This commit is contained in:
Kevin Sawicki 2016-03-04 12:05:36 -08:00
parent dda7740399
commit b90c0c7895
2 changed files with 13 additions and 5 deletions

View file

@ -13,6 +13,7 @@
#include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
#include "base/base64.h" #include "base/base64.h"
#include "base/files/file_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/pattern.h" #include "base/strings/pattern.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
@ -254,17 +255,24 @@ mate::Handle<NativeImage> NativeImage::CreateFromJPEG(
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; gfx::ImageSkia image_skia;
if (path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
base::FilePath absolute_path = MakeAbsoluteFilePath(path);
// MakeAbsoluteFilePath returns an empty path on failures
if (absolute_path.empty()) {
absolute_path = path;
}
if (absolute_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) {
#if defined(OS_WIN) #if defined(OS_WIN)
ReadImageSkiaFromICO(&image_skia, path); ReadImageSkiaFromICO(&image_skia, absolute_path);
#endif #endif
} else { } else {
PopulateImageSkiaRepsFromPath(&image_skia, path); PopulateImageSkiaRepsFromPath(&image_skia, absolute_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(path)) if (IsTemplateFilename(absolute_path))
handle->SetTemplateImage(true); handle->SetTemplateImage(true);
#endif #endif
return handle; return handle;

View file

@ -4,7 +4,7 @@ const path = require('path');
describe('nativeImage module', function () { describe('nativeImage module', function () {
describe('createFromPath(path)', function () { describe('createFromPath(path)', function () {
it('normalizes paths', function () { it('normalizes the path', function () {
const nonAbsolutePath = path.join(__dirname, 'fixtures', 'api') + path.sep + '..' + path.sep + path.join('assets', 'logo.png'); const nonAbsolutePath = path.join(__dirname, 'fixtures', 'api') + path.sep + '..' + path.sep + path.join('assets', 'logo.png');
const image = nativeImage.createFromPath(nonAbsolutePath); const image = nativeImage.createFromPath(nonAbsolutePath);
assert(!image.isEmpty()); assert(!image.isEmpty());