blob: 9857ba8aa6fd508c7ba5ef25f6ba0a050eff67fa [file] [log] [blame]
[email protected]93079e02012-05-15 15:42:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]46728b1f2009-05-07 20:42:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]0f38ceae2009-05-08 19:01:025#include "chrome/browser/icon_loader.h"
[email protected]46728b1f2009-05-07 20:42:246
7#include <windows.h>
8#include <shellapi.h>
9
[email protected]a27a2bc2011-11-15 21:25:5110#include "base/bind.h"
Gabriel Charette44db1422018-08-06 11:19:3311#include "base/task/post_task.h"
[email protected]34b99632011-01-01 01:01:0612#include "base/threading/thread.h"
[email protected]93079e02012-05-15 15:42:5913#include "third_party/skia/include/core/SkBitmap.h"
robliao18e220e82016-04-19 16:47:1214#include "ui/display/win/dpi.h"
tfarinaebe974f02015-01-03 04:25:3215#include "ui/gfx/geometry/size.h"
[email protected]08397d52011-02-05 01:53:3816#include "ui/gfx/icon_util.h"
[email protected]2b8ac342012-08-29 03:46:2717#include "ui/gfx/image/image_skia.h"
[email protected]46728b1f2009-05-07 20:42:2418
[email protected]bc0147b2013-04-03 20:50:5919// static
avi381f719f2016-12-16 00:05:0220IconLoader::IconGroup IconLoader::GroupForFilepath(
21 const base::FilePath& file_path) {
22 if (file_path.MatchesExtension(L".exe") ||
23 file_path.MatchesExtension(L".dll") ||
24 file_path.MatchesExtension(L".ico")) {
25 return file_path.value();
26 }
[email protected]681b4b82013-04-09 23:34:2127
avi381f719f2016-12-16 00:05:0228 return file_path.Extension();
[email protected]bc0147b2013-04-03 20:50:5929}
30
[email protected]b3b6a372014-03-12 01:48:0431// static
avif4d431c2017-06-22 23:30:5332scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
33 // Technically speaking, only a thread with COM is needed, not one that has
34 // a COM STA. However, this is what is available for now.
Sami Kyostila7d640eb2019-07-31 18:50:2635 return base::CreateCOMSTATaskRunner(traits());
[email protected]b3b6a372014-03-12 01:48:0436}
37
[email protected]0f38ceae2009-05-08 19:01:0238void IconLoader::ReadIcon() {
[email protected]46728b1f2009-05-07 20:42:2439 int size = 0;
40 switch (icon_size_) {
41 case IconLoader::SMALL:
42 size = SHGFI_SMALLICON;
43 break;
44 case IconLoader::NORMAL:
45 size = 0;
46 break;
47 case IconLoader::LARGE:
48 size = SHGFI_LARGEICON;
49 break;
50 default:
51 NOTREACHED();
52 }
[email protected]46728b1f2009-05-07 20:42:2453
Dana Friedfa14d5f2019-04-21 20:49:3654 gfx::Image image;
[email protected]ab6d23f2012-09-07 18:43:1255
56 SHFILEINFO file_info = { 0 };
57 if (SHGetFileInfo(group_.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info,
58 sizeof(SHFILEINFO),
59 SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) {
Yuri Wiitala2508fd42018-09-19 23:20:1460 const SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(file_info.hIcon);
61 if (!bitmap.isNull()) {
62 gfx::ImageSkia image_skia(
63 gfx::ImageSkiaRep(bitmap, display::win::GetDPIScale()));
[email protected]ab6d23f2012-09-07 18:43:1264 image_skia.MakeThreadSafe();
Dana Friedfa14d5f2019-04-21 20:49:3665 image = gfx::Image(image_skia);
[email protected]ab6d23f2012-09-07 18:43:1266 DestroyIcon(file_info.hIcon);
67 }
68 }
69
avif0a7b5b812016-12-17 19:01:3170 target_task_runner_->PostTask(
Avi Drissmanefe4dc82018-02-23 17:55:3971 FROM_HERE,
72 base::BindOnce(std::move(callback_), std::move(image), group_));
avif0a7b5b812016-12-17 19:01:3173 delete this;
[email protected]46728b1f2009-05-07 20:42:2474}