blob: 91239b8ded73ba01dddf37044e4d6d70c126643e [file] [log] [blame]
[email protected]66171ab2011-03-03 15:50:071// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/icon_manager.h"
6
dcheng4af48582016-04-19 00:29:357#include <memory>
jsbell50f0e752015-11-16 19:36:538#include <tuple>
9
[email protected]c1896982012-12-05 20:26:1710#include "base/bind.h"
[email protected]c1896982012-12-05 20:26:1711#include "base/task_runner.h"
[email protected]c43c6682009-05-19 14:51:4412#include "third_party/skia/include/core/SkBitmap.h"
13#include "third_party/skia/include/core/SkCanvas.h"
initial.commit09911bf2008-07-26 23:55:2914
[email protected]c1896982012-12-05 20:26:1715namespace {
16
17void RunCallbackIfNotCanceled(
[email protected]e95b717f2014-02-06 13:47:1318 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
Avi Drissmanefe4dc82018-02-23 17:55:3919 IconManager::IconRequestCallback callback,
Dana Friedfa14d5f2019-04-21 20:49:3620 gfx::Image image) {
[email protected]c1896982012-12-05 20:26:1721 if (is_canceled.Run())
22 return;
Dana Friedfa14d5f2019-04-21 20:49:3623 std::move(callback).Run(std::move(image));
[email protected]c1896982012-12-05 20:26:1724}
25
26} // namespace
27
Jeremy Roman495db682019-07-12 16:03:2428IconManager::IconManager() {}
initial.commit09911bf2008-07-26 23:55:2929
30IconManager::~IconManager() {
initial.commit09911bf2008-07-26 23:55:2931}
32
avi381f719f2016-12-16 00:05:0233gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_path,
Alexander Zhirovf51bebd2021-04-13 09:52:0434 IconLoader::IconSize size,
35 float scale) {
avi381f719f2016-12-16 00:05:0236 auto group_it = group_cache_.find(file_path);
37 if (group_it == group_cache_.end())
38 return nullptr;
[email protected]bc0147b2013-04-03 20:50:5939
Alexander Zhirovf51bebd2021-04-13 09:52:0440 CacheKey key(group_it->second, size, scale);
avi381f719f2016-12-16 00:05:0241 auto icon_it = icon_cache_.find(key);
42 if (icon_it == icon_cache_.end())
43 return nullptr;
[email protected]bc0147b2013-04-03 20:50:5944
Dana Friedfa14d5f2019-04-21 20:49:3645 return &icon_it->second;
initial.commit09911bf2008-07-26 23:55:2946}
47
[email protected]e95b717f2014-02-06 13:47:1348base::CancelableTaskTracker::TaskId IconManager::LoadIcon(
avi381f719f2016-12-16 00:05:0249 const base::FilePath& file_path,
initial.commit09911bf2008-07-26 23:55:2950 IconLoader::IconSize size,
Alexander Zhirovf51bebd2021-04-13 09:52:0451 float scale,
Avi Drissmanefe4dc82018-02-23 17:55:3952 IconRequestCallback callback,
[email protected]e95b717f2014-02-06 13:47:1353 base::CancelableTaskTracker* tracker) {
[email protected]e95b717f2014-02-06 13:47:1354 base::CancelableTaskTracker::IsCanceledCallback is_canceled;
55 base::CancelableTaskTracker::TaskId id =
56 tracker->NewTrackedTaskId(&is_canceled);
Avi Drissmanefe4dc82018-02-23 17:55:3957 IconRequestCallback callback_runner = base::BindOnce(
58 &RunCallbackIfNotCanceled, is_canceled, std::move(callback));
[email protected]c1896982012-12-05 20:26:1759
avif0a7b5b812016-12-17 19:01:3160 IconLoader* loader = IconLoader::Create(
Alexander Zhirovf51bebd2021-04-13 09:52:0461 file_path, size, scale,
Avi Drissmanefe4dc82018-02-23 17:55:3962 base::BindOnce(&IconManager::OnIconLoaded, weak_factory_.GetWeakPtr(),
Alexander Zhirovf51bebd2021-04-13 09:52:0463 std::move(callback_runner), file_path, size, scale));
avif0a7b5b812016-12-17 19:01:3164 loader->Start();
65
[email protected]c1896982012-12-05 20:26:1766 return id;
initial.commit09911bf2008-07-26 23:55:2967}
68
avif0a7b5b812016-12-17 19:01:3169void IconManager::OnIconLoaded(IconRequestCallback callback,
70 base::FilePath file_path,
71 IconLoader::IconSize size,
Alexander Zhirovf51bebd2021-04-13 09:52:0472 float scale,
Dana Friedfa14d5f2019-04-21 20:49:3673 gfx::Image result,
avif0a7b5b812016-12-17 19:01:3174 const IconLoader::IconGroup& group) {
avi381f719f2016-12-16 00:05:0275 // Cache the bitmap. Watch out: |result| may be null, which indicates a
76 // failure. We assume that if we have an entry in |icon_cache_| it must not be
77 // null.
Alexander Zhirovf51bebd2021-04-13 09:52:0478 CacheKey key(group, size, scale);
Dana Friedfa14d5f2019-04-21 20:49:3679 std::move(callback).Run(result);
80 if (!result.IsEmpty())
avi381f719f2016-12-16 00:05:0281 icon_cache_[key] = std::move(result);
Dana Friedfa14d5f2019-04-21 20:49:3682 else
avi381f719f2016-12-16 00:05:0283 icon_cache_.erase(key);
initial.commit09911bf2008-07-26 23:55:2984
avif0a7b5b812016-12-17 19:01:3185 group_cache_[file_path] = group;
initial.commit09911bf2008-07-26 23:55:2986}
87
avi381f719f2016-12-16 00:05:0288IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group,
Alexander Zhirovf51bebd2021-04-13 09:52:0489 IconLoader::IconSize size,
90 float scale)
91 : group(group), size(size), scale(scale) {}
initial.commit09911bf2008-07-26 23:55:2992
Alexander Zhirovf51bebd2021-04-13 09:52:0493bool IconManager::CacheKey::operator<(const CacheKey& other) const {
94 return std::tie(group, size, scale) <
95 std::tie(other.group, other.size, other.scale);
initial.commit09911bf2008-07-26 23:55:2996}