blob: 7b20ef323a79675a2357d52ab5c286a5c206d12a [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,
[email protected]c1896982012-12-05 20:26:1719 const IconManager::IconRequestCallback& callback,
20 gfx::Image* image) {
21 if (is_canceled.Run())
22 return;
23 callback.Run(image);
24}
25
26} // namespace
27
avif0a7b5b812016-12-17 19:01:3128IconManager::IconManager() : weak_factory_(this) {}
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,
[email protected]bc0147b2013-04-03 20:50:5934 IconLoader::IconSize size) {
avi381f719f2016-12-16 00:05:0235 auto group_it = group_cache_.find(file_path);
36 if (group_it == group_cache_.end())
37 return nullptr;
[email protected]bc0147b2013-04-03 20:50:5938
avi381f719f2016-12-16 00:05:0239 CacheKey key(group_it->second, size);
40 auto icon_it = icon_cache_.find(key);
41 if (icon_it == icon_cache_.end())
42 return nullptr;
[email protected]bc0147b2013-04-03 20:50:5943
avi381f719f2016-12-16 00:05:0244 return icon_it->second.get();
initial.commit09911bf2008-07-26 23:55:2945}
46
[email protected]e95b717f2014-02-06 13:47:1347base::CancelableTaskTracker::TaskId IconManager::LoadIcon(
avi381f719f2016-12-16 00:05:0248 const base::FilePath& file_path,
initial.commit09911bf2008-07-26 23:55:2949 IconLoader::IconSize size,
[email protected]c1896982012-12-05 20:26:1750 const IconRequestCallback& callback,
[email protected]e95b717f2014-02-06 13:47:1351 base::CancelableTaskTracker* tracker) {
[email protected]e95b717f2014-02-06 13:47:1352 base::CancelableTaskTracker::IsCanceledCallback is_canceled;
53 base::CancelableTaskTracker::TaskId id =
54 tracker->NewTrackedTaskId(&is_canceled);
[email protected]c1896982012-12-05 20:26:1755 IconRequestCallback callback_runner = base::Bind(
56 &RunCallbackIfNotCanceled, is_canceled, callback);
57
avif0a7b5b812016-12-17 19:01:3158 IconLoader* loader = IconLoader::Create(
59 file_path, size,
60 base::Bind(&IconManager::OnIconLoaded, weak_factory_.GetWeakPtr(),
61 callback_runner, file_path, size));
62 loader->Start();
63
[email protected]c1896982012-12-05 20:26:1764 return id;
initial.commit09911bf2008-07-26 23:55:2965}
66
avif0a7b5b812016-12-17 19:01:3167void IconManager::OnIconLoaded(IconRequestCallback callback,
68 base::FilePath file_path,
69 IconLoader::IconSize size,
70 std::unique_ptr<gfx::Image> result,
71 const IconLoader::IconGroup& group) {
avi381f719f2016-12-16 00:05:0272 // Cache the bitmap. Watch out: |result| may be null, which indicates a
73 // failure. We assume that if we have an entry in |icon_cache_| it must not be
74 // null.
avif0a7b5b812016-12-17 19:01:3175 CacheKey key(group, size);
avi381f719f2016-12-16 00:05:0276 if (result) {
avif0a7b5b812016-12-17 19:01:3177 callback.Run(result.get());
avi381f719f2016-12-16 00:05:0278 icon_cache_[key] = std::move(result);
79 } else {
avif0a7b5b812016-12-17 19:01:3180 callback.Run(nullptr);
avi381f719f2016-12-16 00:05:0281 icon_cache_.erase(key);
initial.commit09911bf2008-07-26 23:55:2982 }
83
avif0a7b5b812016-12-17 19:01:3184 group_cache_[file_path] = group;
initial.commit09911bf2008-07-26 23:55:2985}
86
avi381f719f2016-12-16 00:05:0287IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group,
initial.commit09911bf2008-07-26 23:55:2988 IconLoader::IconSize size)
avi381f719f2016-12-16 00:05:0289 : group(group), size(size) {}
initial.commit09911bf2008-07-26 23:55:2990
91bool IconManager::CacheKey::operator<(const CacheKey &other) const {
jsbell50f0e752015-11-16 19:36:5392 return std::tie(group, size) < std::tie(other.group, other.size);
initial.commit09911bf2008-07-26 23:55:2993}