[email protected] | 93079e0 | 2012-05-15 15:42:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 5 | #include "chrome/browser/icon_loader.h" |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 6 | |
| 7 | #include <windows.h> |
| 8 | #include <shellapi.h> |
| 9 | |
[email protected] | a27a2bc | 2011-11-15 21:25:51 | [diff] [blame] | 10 | #include "base/bind.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 11 | #include "base/task/post_task.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
[email protected] | 93079e0 | 2012-05-15 15:42:59 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkBitmap.h" |
robliao | 18e220e8 | 2016-04-19 16:47:12 | [diff] [blame] | 14 | #include "ui/display/win/dpi.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 15 | #include "ui/gfx/geometry/size.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/icon_util.h" |
[email protected] | 2b8ac34 | 2012-08-29 03:46:27 | [diff] [blame] | 17 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 18 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 19 | // static |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 20 | IconLoader::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] | 681b4b8 | 2013-04-09 23:34:21 | [diff] [blame] | 27 | |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 28 | return file_path.Extension(); |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 31 | // static |
avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 32 | scoped_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 Kyostila | 7d640eb | 2019-07-31 18:50:26 | [diff] [blame] | 35 | return base::CreateCOMSTATaskRunner(traits()); |
[email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 38 | void IconLoader::ReadIcon() { |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 39 | 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] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 53 | |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 54 | gfx::Image image; |
[email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 55 | |
| 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 Wiitala | 2508fd4 | 2018-09-19 23:20:14 | [diff] [blame] | 60 | 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] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 64 | image_skia.MakeThreadSafe(); |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 65 | image = gfx::Image(image_skia); |
[email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 66 | DestroyIcon(file_info.hIcon); |
| 67 | } |
| 68 | } |
| 69 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 70 | target_task_runner_->PostTask( |
Avi Drissman | efe4dc8 | 2018-02-23 17:55:39 | [diff] [blame] | 71 | FROM_HERE, |
| 72 | base::BindOnce(std::move(callback_), std::move(image), group_)); |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 73 | delete this; |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 74 | } |