2016-11-02 18:57:16 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "chrome/browser/icon_loader.h"
|
|
|
|
|
2018-04-18 01:57:05 +00:00
|
|
|
#include <windows.h>
|
2018-04-26 09:43:39 +00:00
|
|
|
#include <shellapi.h>
|
2016-11-02 18:57:16 +00:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/message_loop/message_loop.h"
|
2018-01-08 10:21:22 +00:00
|
|
|
#include "base/task_scheduler/post_task.h"
|
2016-11-02 18:57:16 +00:00
|
|
|
#include "base/threading/thread.h"
|
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
|
|
|
#include "ui/display/win/dpi.h"
|
|
|
|
#include "ui/gfx/geometry/size.h"
|
|
|
|
#include "ui/gfx/icon_util.h"
|
|
|
|
#include "ui/gfx/image/image_skia.h"
|
|
|
|
|
|
|
|
// static
|
2017-02-16 19:19:19 +00:00
|
|
|
IconLoader::IconGroup IconLoader::GroupForFilepath(
|
|
|
|
const base::FilePath& file_path) {
|
|
|
|
if (file_path.MatchesExtension(L".exe") ||
|
|
|
|
file_path.MatchesExtension(L".dll") ||
|
|
|
|
file_path.MatchesExtension(L".ico")) {
|
|
|
|
return file_path.value();
|
|
|
|
}
|
2016-11-02 18:57:16 +00:00
|
|
|
|
2017-02-16 19:19:19 +00:00
|
|
|
return file_path.Extension();
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2018-01-08 10:21:22 +00:00
|
|
|
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
|
|
|
|
// Technically speaking, only a thread with COM is needed, not one that has
|
|
|
|
// a COM STA. However, this is what is available for now.
|
|
|
|
return base::CreateCOMSTATaskRunnerWithTraits(traits());
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconLoader::ReadIcon() {
|
|
|
|
int size = 0;
|
|
|
|
switch (icon_size_) {
|
|
|
|
case IconLoader::SMALL:
|
|
|
|
size = SHGFI_SMALLICON;
|
|
|
|
break;
|
|
|
|
case IconLoader::NORMAL:
|
|
|
|
size = 0;
|
|
|
|
break;
|
|
|
|
case IconLoader::LARGE:
|
|
|
|
size = SHGFI_LARGEICON;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2018-01-08 09:47:25 +00:00
|
|
|
std::unique_ptr<gfx::Image> image;
|
2016-11-02 18:57:16 +00:00
|
|
|
|
2018-04-18 01:57:05 +00:00
|
|
|
SHFILEINFO file_info = {0};
|
2016-11-02 18:57:16 +00:00
|
|
|
if (SHGetFileInfo(group_.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info,
|
2018-04-18 01:57:05 +00:00
|
|
|
sizeof(SHFILEINFO),
|
|
|
|
SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) {
|
2016-11-02 18:57:16 +00:00
|
|
|
std::unique_ptr<SkBitmap> bitmap(
|
|
|
|
IconUtil::CreateSkBitmapFromHICON(file_info.hIcon));
|
|
|
|
if (bitmap.get()) {
|
2018-04-18 01:57:05 +00:00
|
|
|
gfx::ImageSkia image_skia(
|
|
|
|
gfx::ImageSkiaRep(*bitmap, display::win::GetDPIScale()));
|
2016-11-02 18:57:16 +00:00
|
|
|
image_skia.MakeThreadSafe();
|
2018-04-12 12:48:32 +00:00
|
|
|
image = std::make_unique<gfx::Image>(image_skia);
|
2016-11-02 18:57:16 +00:00
|
|
|
DestroyIcon(file_info.hIcon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 19:19:19 +00:00
|
|
|
target_task_runner_->PostTask(
|
2018-01-08 09:47:25 +00:00
|
|
|
FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
|
2017-02-16 19:19:19 +00:00
|
|
|
delete this;
|
2016-11-02 18:57:16 +00:00
|
|
|
}
|