icon_manager.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2011 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_manager.h"
  5. #include <memory>
  6. #include <tuple>
  7. #include "base/bind.h"
  8. #include "base/task_runner.h"
  9. #include "third_party/skia/include/core/SkBitmap.h"
  10. #include "third_party/skia/include/core/SkCanvas.h"
  11. namespace {
  12. void RunCallbackIfNotCanceled(
  13. const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
  14. const IconManager::IconRequestCallback& callback,
  15. gfx::Image* image) {
  16. if (is_canceled.Run())
  17. return;
  18. callback.Run(image);
  19. }
  20. } // namespace
  21. IconManager::IconManager() : weak_factory_(this) {}
  22. IconManager::~IconManager() {}
  23. gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_path,
  24. IconLoader::IconSize size) {
  25. auto group_it = group_cache_.find(file_path);
  26. if (group_it == group_cache_.end())
  27. return nullptr;
  28. CacheKey key(group_it->second, size);
  29. auto icon_it = icon_cache_.find(key);
  30. if (icon_it == icon_cache_.end())
  31. return nullptr;
  32. return icon_it->second.get();
  33. }
  34. base::CancelableTaskTracker::TaskId IconManager::LoadIcon(
  35. const base::FilePath& file_path,
  36. IconLoader::IconSize size,
  37. const IconRequestCallback& callback,
  38. base::CancelableTaskTracker* tracker) {
  39. base::CancelableTaskTracker::IsCanceledCallback is_canceled;
  40. base::CancelableTaskTracker::TaskId id =
  41. tracker->NewTrackedTaskId(&is_canceled);
  42. IconRequestCallback callback_runner =
  43. base::Bind(&RunCallbackIfNotCanceled, is_canceled, callback);
  44. IconLoader* loader = IconLoader::Create(
  45. file_path, size,
  46. base::Bind(&IconManager::OnIconLoaded, weak_factory_.GetWeakPtr(),
  47. callback_runner, file_path, size));
  48. loader->Start();
  49. return id;
  50. }
  51. void IconManager::OnIconLoaded(IconRequestCallback callback,
  52. base::FilePath file_path,
  53. IconLoader::IconSize size,
  54. std::unique_ptr<gfx::Image> result,
  55. const IconLoader::IconGroup& group) {
  56. // Cache the bitmap. Watch out: |result| may be null, which indicates a
  57. // failure. We assume that if we have an entry in |icon_cache_| it must not be
  58. // null.
  59. CacheKey key(group, size);
  60. if (result) {
  61. callback.Run(result.get());
  62. icon_cache_[key] = std::move(result);
  63. } else {
  64. callback.Run(nullptr);
  65. icon_cache_.erase(key);
  66. }
  67. group_cache_[file_path] = group;
  68. }
  69. IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group,
  70. IconLoader::IconSize size)
  71. : group(group), size(size) {}
  72. bool IconManager::CacheKey::operator<(const CacheKey& other) const {
  73. return std::tie(group, size) < std::tie(other.group, other.size);
  74. }