| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/icon_manager.h" | 5 #include "chrome/browser/icon_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <tuple> | 8 #include <tuple> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 void RunCallbackIfNotCanceled( | 17 void RunCallbackIfNotCanceled( |
| 18 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, | 18 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 19 const IconManager::IconRequestCallback& callback, | 19 const IconManager::IconRequestCallback& callback, |
| 20 gfx::Image* image) { | 20 gfx::Image* image) { |
| 21 if (is_canceled.Run()) | 21 if (is_canceled.Run()) |
| 22 return; | 22 return; |
| 23 callback.Run(image); | 23 callback.Run(image); |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 struct IconManager::ClientRequest { | 28 IconManager::IconManager() : weak_factory_(this) {} |
| 29 IconRequestCallback callback; | |
| 30 base::FilePath file_path; | |
| 31 IconLoader::IconSize size; | |
| 32 }; | |
| 33 | |
| 34 IconManager::IconManager() { | |
| 35 } | |
| 36 | 29 |
| 37 IconManager::~IconManager() { | 30 IconManager::~IconManager() { |
| 38 } | 31 } |
| 39 | 32 |
| 40 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_path, | 33 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_path, |
| 41 IconLoader::IconSize size) { | 34 IconLoader::IconSize size) { |
| 42 auto group_it = group_cache_.find(file_path); | 35 auto group_it = group_cache_.find(file_path); |
| 43 if (group_it == group_cache_.end()) | 36 if (group_it == group_cache_.end()) |
| 44 return nullptr; | 37 return nullptr; |
| 45 | 38 |
| 46 CacheKey key(group_it->second, size); | 39 CacheKey key(group_it->second, size); |
| 47 auto icon_it = icon_cache_.find(key); | 40 auto icon_it = icon_cache_.find(key); |
| 48 if (icon_it == icon_cache_.end()) | 41 if (icon_it == icon_cache_.end()) |
| 49 return nullptr; | 42 return nullptr; |
| 50 | 43 |
| 51 return icon_it->second.get(); | 44 return icon_it->second.get(); |
| 52 } | 45 } |
| 53 | 46 |
| 54 base::CancelableTaskTracker::TaskId IconManager::LoadIcon( | 47 base::CancelableTaskTracker::TaskId IconManager::LoadIcon( |
| 55 const base::FilePath& file_path, | 48 const base::FilePath& file_path, |
| 56 IconLoader::IconSize size, | 49 IconLoader::IconSize size, |
| 57 const IconRequestCallback& callback, | 50 const IconRequestCallback& callback, |
| 58 base::CancelableTaskTracker* tracker) { | 51 base::CancelableTaskTracker* tracker) { |
| 59 IconLoader* loader = new IconLoader(file_path, size, this); | |
| 60 loader->AddRef(); | |
| 61 loader->Start(); | |
| 62 | |
| 63 base::CancelableTaskTracker::IsCanceledCallback is_canceled; | 52 base::CancelableTaskTracker::IsCanceledCallback is_canceled; |
| 64 base::CancelableTaskTracker::TaskId id = | 53 base::CancelableTaskTracker::TaskId id = |
| 65 tracker->NewTrackedTaskId(&is_canceled); | 54 tracker->NewTrackedTaskId(&is_canceled); |
| 66 IconRequestCallback callback_runner = base::Bind( | 55 IconRequestCallback callback_runner = base::Bind( |
| 67 &RunCallbackIfNotCanceled, is_canceled, callback); | 56 &RunCallbackIfNotCanceled, is_canceled, callback); |
| 68 | 57 |
| 69 requests_[loader] = {callback_runner, file_path, size}; | 58 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 |
| 70 return id; | 64 return id; |
| 71 } | 65 } |
| 72 | 66 |
| 73 // IconLoader::Delegate implementation ----------------------------------------- | 67 void IconManager::OnIconLoaded(IconRequestCallback callback, |
| 74 | 68 base::FilePath file_path, |
| 75 void IconManager::OnImageLoaded(IconLoader* loader, | 69 IconLoader::IconSize size, |
| 76 std::unique_ptr<gfx::Image> result, | 70 std::unique_ptr<gfx::Image> result, |
| 77 const IconLoader::IconGroup& group) { | 71 const IconLoader::IconGroup& group) { |
| 78 auto request_it = requests_.find(loader); | |
| 79 DCHECK(request_it != requests_.end()); | |
| 80 | |
| 81 // Balances the AddRef() in LoadIcon(). | |
| 82 loader->Release(); | |
| 83 | |
| 84 const ClientRequest& client_request = request_it->second; | |
| 85 | |
| 86 // Cache the bitmap. Watch out: |result| may be null, which indicates a | 72 // Cache the bitmap. Watch out: |result| may be null, which indicates a |
| 87 // failure. We assume that if we have an entry in |icon_cache_| it must not be | 73 // failure. We assume that if we have an entry in |icon_cache_| it must not be |
| 88 // null. | 74 // null. |
| 89 CacheKey key(group, client_request.size); | 75 CacheKey key(group, size); |
| 90 if (result) { | 76 if (result) { |
| 91 client_request.callback.Run(result.get()); | 77 callback.Run(result.get()); |
| 92 icon_cache_[key] = std::move(result); | 78 icon_cache_[key] = std::move(result); |
| 93 } else { | 79 } else { |
| 94 client_request.callback.Run(nullptr); | 80 callback.Run(nullptr); |
| 95 icon_cache_.erase(key); | 81 icon_cache_.erase(key); |
| 96 } | 82 } |
| 97 | 83 |
| 98 group_cache_[client_request.file_path] = group; | 84 group_cache_[file_path] = group; |
| 99 | |
| 100 requests_.erase(request_it); | |
| 101 } | 85 } |
| 102 | 86 |
| 103 IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group, | 87 IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group, |
| 104 IconLoader::IconSize size) | 88 IconLoader::IconSize size) |
| 105 : group(group), size(size) {} | 89 : group(group), size(size) {} |
| 106 | 90 |
| 107 bool IconManager::CacheKey::operator<(const CacheKey &other) const { | 91 bool IconManager::CacheKey::operator<(const CacheKey &other) const { |
| 108 return std::tie(group, size) < std::tie(other.group, other.size); | 92 return std::tie(group, size) < std::tie(other.group, other.size); |
| 109 } | 93 } |
| OLD | NEW |