icon_loader_win.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/browser/icon_loader.h"
  5. #include <shellapi.h>
  6. #include <windows.h>
  7. #include "base/bind.h"
  8. #include "base/message_loop/message_loop.h"
  9. #include "base/task_scheduler/post_task.h"
  10. #include "base/threading/thread.h"
  11. #include "third_party/skia/include/core/SkBitmap.h"
  12. #include "ui/display/win/dpi.h"
  13. #include "ui/gfx/geometry/size.h"
  14. #include "ui/gfx/icon_util.h"
  15. #include "ui/gfx/image/image_skia.h"
  16. // static
  17. IconLoader::IconGroup IconLoader::GroupForFilepath(
  18. const base::FilePath& file_path) {
  19. if (file_path.MatchesExtension(L".exe") ||
  20. file_path.MatchesExtension(L".dll") ||
  21. file_path.MatchesExtension(L".ico")) {
  22. return file_path.value();
  23. }
  24. return file_path.Extension();
  25. }
  26. // static
  27. scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
  28. // Technically speaking, only a thread with COM is needed, not one that has
  29. // a COM STA. However, this is what is available for now.
  30. return base::CreateCOMSTATaskRunnerWithTraits(traits());
  31. }
  32. void IconLoader::ReadIcon() {
  33. int size = 0;
  34. switch (icon_size_) {
  35. case IconLoader::SMALL:
  36. size = SHGFI_SMALLICON;
  37. break;
  38. case IconLoader::NORMAL:
  39. size = 0;
  40. break;
  41. case IconLoader::LARGE:
  42. size = SHGFI_LARGEICON;
  43. break;
  44. default:
  45. NOTREACHED();
  46. }
  47. std::unique_ptr<gfx::Image> image;
  48. SHFILEINFO file_info = {0};
  49. if (SHGetFileInfo(group_.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info,
  50. sizeof(SHFILEINFO),
  51. SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) {
  52. std::unique_ptr<SkBitmap> bitmap(
  53. IconUtil::CreateSkBitmapFromHICON(file_info.hIcon));
  54. if (bitmap.get()) {
  55. gfx::ImageSkia image_skia(
  56. gfx::ImageSkiaRep(*bitmap, display::win::GetDPIScale()));
  57. image_skia.MakeThreadSafe();
  58. image = std::make_unique<gfx::Image>(image_skia);
  59. DestroyIcon(file_info.hIcon);
  60. }
  61. }
  62. target_task_runner_->PostTask(
  63. FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
  64. delete this;
  65. }