icon_loader_mac.mm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #import <AppKit/AppKit.h>
  6. #include "base/bind.h"
  7. #include "base/files/file_path.h"
  8. #include "base/message_loop/message_loop.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "base/task_scheduler/post_task.h"
  11. #include "base/threading/thread.h"
  12. #include "ui/gfx/image/image_skia.h"
  13. #include "ui/gfx/image/image_skia_util_mac.h"
  14. // static
  15. IconLoader::IconGroup IconLoader::GroupForFilepath(
  16. const base::FilePath& file_path) {
  17. return file_path.Extension();
  18. }
  19. // static
  20. scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
  21. // NSWorkspace is thread-safe.
  22. return base::CreateTaskRunnerWithTraits(traits());
  23. }
  24. void IconLoader::ReadIcon() {
  25. NSString* group = base::SysUTF8ToNSString(group_);
  26. NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
  27. NSImage* icon = [workspace iconForFileType:group];
  28. std::unique_ptr<gfx::Image> image;
  29. if (icon_size_ == ALL) {
  30. // The NSImage already has all sizes.
  31. image = std::make_unique<gfx::Image>([icon retain]);
  32. } else {
  33. NSSize size = NSZeroSize;
  34. switch (icon_size_) {
  35. case IconLoader::SMALL:
  36. size = NSMakeSize(16, 16);
  37. break;
  38. case IconLoader::NORMAL:
  39. size = NSMakeSize(32, 32);
  40. break;
  41. default:
  42. NOTREACHED();
  43. }
  44. gfx::ImageSkia image_skia(gfx::ImageSkiaFromResizedNSImage(icon, size));
  45. if (!image_skia.isNull()) {
  46. image_skia.MakeThreadSafe();
  47. image = std::make_unique<gfx::Image>(image_skia);
  48. }
  49. }
  50. target_task_runner_->PostTask(
  51. FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
  52. delete this;
  53. }