[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" |
tzik | c70c615 | 2017-02-22 05:35:34 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
[email protected] | 467a106e | 2013-07-18 12:07:19 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 13 | #include "base/task_scheduler/post_task.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
[email protected] | 93079e0 | 2012-05-15 15:42:59 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkBitmap.h" |
robliao | 18e220e8 | 2016-04-19 16:47:12 | [diff] [blame] | 16 | #include "ui/display/win/dpi.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 17 | #include "ui/gfx/geometry/size.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 18 | #include "ui/gfx/icon_util.h" |
[email protected] | 2b8ac34 | 2012-08-29 03:46:27 | [diff] [blame] | 19 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 20 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 21 | // static |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 22 | IconLoader::IconGroup IconLoader::GroupForFilepath( |
| 23 | const base::FilePath& file_path) { |
| 24 | if (file_path.MatchesExtension(L".exe") || |
| 25 | file_path.MatchesExtension(L".dll") || |
| 26 | file_path.MatchesExtension(L".ico")) { |
| 27 | return file_path.value(); |
| 28 | } |
[email protected] | 681b4b8 | 2013-04-09 23:34:21 | [diff] [blame] | 29 | |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 30 | return file_path.Extension(); |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 33 | // static |
avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 34 | scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() { |
| 35 | // Technically speaking, only a thread with COM is needed, not one that has |
| 36 | // a COM STA. However, this is what is available for now. |
| 37 | return base::CreateCOMSTATaskRunnerWithTraits(traits()); |
[email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 40 | void IconLoader::ReadIcon() { |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 41 | int size = 0; |
| 42 | switch (icon_size_) { |
| 43 | case IconLoader::SMALL: |
| 44 | size = SHGFI_SMALLICON; |
| 45 | break; |
| 46 | case IconLoader::NORMAL: |
| 47 | size = 0; |
| 48 | break; |
| 49 | case IconLoader::LARGE: |
| 50 | size = SHGFI_LARGEICON; |
| 51 | break; |
| 52 | default: |
| 53 | NOTREACHED(); |
| 54 | } |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 55 | |
tzik | c70c615 | 2017-02-22 05:35:34 | [diff] [blame] | 56 | std::unique_ptr<gfx::Image> image; |
[email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 57 | |
| 58 | SHFILEINFO file_info = { 0 }; |
| 59 | if (SHGetFileInfo(group_.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info, |
| 60 | sizeof(SHFILEINFO), |
| 61 | SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) { |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 62 | std::unique_ptr<SkBitmap> bitmap( |
| 63 | IconUtil::CreateSkBitmapFromHICON(file_info.hIcon)); |
[email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 64 | if (bitmap.get()) { |
robliao | 18e220e8 | 2016-04-19 16:47:12 | [diff] [blame] | 65 | gfx::ImageSkia image_skia(gfx::ImageSkiaRep(*bitmap, |
| 66 | display::win::GetDPIScale())); |
[email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 67 | image_skia.MakeThreadSafe(); |
tzik | c70c615 | 2017-02-22 05:35:34 | [diff] [blame] | 68 | image = base::MakeUnique<gfx::Image>(image_skia); |
[email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 69 | DestroyIcon(file_info.hIcon); |
| 70 | } |
| 71 | } |
| 72 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 73 | target_task_runner_->PostTask( |
tzik | c70c615 | 2017-02-22 05:35:34 | [diff] [blame] | 74 | FROM_HERE, base::Bind(callback_, base::Passed(&image), group_)); |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 75 | delete this; |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 76 | } |