blob: 1d0912bd51850f3e3409353c34f883fe939dc6cd [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"
tzikc70c6152017-02-22 05:35:3411#include "base/memory/ptr_util.h"
[email protected]467a106e2013-07-18 12:07:1912#include "base/message_loop/message_loop.h"
avif4d431c2017-06-22 23:30:5313#include "base/task_scheduler/post_task.h"
[email protected]34b99632011-01-01 01:01:0614#include "base/threading/thread.h"
[email protected]93079e02012-05-15 15:42:5915#include "third_party/skia/include/core/SkBitmap.h"
robliao18e220e82016-04-19 16:47:1216#include "ui/display/win/dpi.h"
tfarinaebe974f02015-01-03 04:25:3217#include "ui/gfx/geometry/size.h"
[email protected]08397d52011-02-05 01:53:3818#include "ui/gfx/icon_util.h"
[email protected]2b8ac342012-08-29 03:46:2719#include "ui/gfx/image/image_skia.h"
[email protected]46728b1f2009-05-07 20:42:2420
[email protected]bc0147b2013-04-03 20:50:5921// static
avi381f719f2016-12-16 00:05:0222IconLoader::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]681b4b82013-04-09 23:34:2129
avi381f719f2016-12-16 00:05:0230 return file_path.Extension();
[email protected]bc0147b2013-04-03 20:50:5931}
32
[email protected]b3b6a372014-03-12 01:48:0433// static
avif4d431c2017-06-22 23:30:5334scoped_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]b3b6a372014-03-12 01:48:0438}
39
[email protected]0f38ceae2009-05-08 19:01:0240void IconLoader::ReadIcon() {
[email protected]46728b1f2009-05-07 20:42:2441 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]46728b1f2009-05-07 20:42:2455
tzikc70c6152017-02-22 05:35:3456 std::unique_ptr<gfx::Image> image;
[email protected]ab6d23f2012-09-07 18:43:1257
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)) {
dcheng4af48582016-04-19 00:29:3562 std::unique_ptr<SkBitmap> bitmap(
63 IconUtil::CreateSkBitmapFromHICON(file_info.hIcon));
[email protected]ab6d23f2012-09-07 18:43:1264 if (bitmap.get()) {
robliao18e220e82016-04-19 16:47:1265 gfx::ImageSkia image_skia(gfx::ImageSkiaRep(*bitmap,
66 display::win::GetDPIScale()));
[email protected]ab6d23f2012-09-07 18:43:1267 image_skia.MakeThreadSafe();
tzikc70c6152017-02-22 05:35:3468 image = base::MakeUnique<gfx::Image>(image_skia);
[email protected]ab6d23f2012-09-07 18:43:1269 DestroyIcon(file_info.hIcon);
70 }
71 }
72
avif0a7b5b812016-12-17 19:01:3173 target_task_runner_->PostTask(
tzikc70c6152017-02-22 05:35:3474 FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
avif0a7b5b812016-12-17 19:01:3175 delete this;
[email protected]46728b1f2009-05-07 20:42:2476}