Chromium Code Reviews| 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/stl_util.h" | |
| 12 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "third_party/skia/include/core/SkCanvas.h" | 13 #include "third_party/skia/include/core/SkCanvas.h" |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 void RunCallbackIfNotCanceled( | 17 void RunCallbackIfNotCanceled( |
| 19 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, | 18 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 20 const IconManager::IconRequestCallback& callback, | 19 const IconManager::IconRequestCallback& callback, |
| 21 gfx::Image* image) { | 20 gfx::Image* image) { |
| 22 if (is_canceled.Run()) | 21 if (is_canceled.Run()) |
| 23 return; | 22 return; |
| 24 callback.Run(image); | 23 callback.Run(image); |
| 25 } | 24 } |
| 26 | 25 |
| 27 } // namespace | 26 } // namespace |
| 28 | 27 |
| 29 struct IconManager::ClientRequest { | 28 struct IconManager::ClientRequest { |
| 30 IconRequestCallback callback; | 29 IconRequestCallback callback; |
| 31 base::FilePath file_path; | 30 base::FilePath file_path; |
| 32 IconLoader::IconSize size; | 31 IconLoader::IconSize size; |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 IconManager::IconManager() { | 34 IconManager::IconManager() { |
| 36 } | 35 } |
| 37 | 36 |
| 38 IconManager::~IconManager() { | 37 IconManager::~IconManager() { |
|
sky
2016/12/15 22:36:05
In reviewing this code it seems like IconManager i
Avi (use Gerrit)
2016/12/15 23:01:10
IconManager is the only user of IconLoader. Why is
| |
| 39 base::STLDeleteValues(&icon_cache_); | |
| 40 } | 38 } |
| 41 | 39 |
| 42 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_name, | 40 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_path, |
| 43 IconLoader::IconSize size) { | 41 IconLoader::IconSize size) { |
| 44 GroupMap::iterator it = group_cache_.find(file_name); | 42 auto group_it = group_cache_.find(file_path); |
| 45 if (it != group_cache_.end()) | 43 if (group_it == group_cache_.end()) |
| 46 return LookupIconFromGroup(it->second, size); | 44 return nullptr; |
| 47 | 45 |
| 48 return NULL; | 46 CacheKey key(group_it->second, size); |
| 49 } | 47 auto icon_it = icon_cache_.find(key); |
| 48 if (icon_it == icon_cache_.end()) | |
| 49 return nullptr; | |
| 50 | 50 |
| 51 gfx::Image* IconManager::LookupIconFromGroup(const IconGroupID& group, | 51 return icon_it->second.get(); |
| 52 IconLoader::IconSize size) { | |
| 53 IconMap::iterator it = icon_cache_.find(CacheKey(group, size)); | |
| 54 if (it != icon_cache_.end()) | |
| 55 return it->second; | |
| 56 | |
| 57 return NULL; | |
| 58 } | 52 } |
| 59 | 53 |
| 60 base::CancelableTaskTracker::TaskId IconManager::LoadIcon( | 54 base::CancelableTaskTracker::TaskId IconManager::LoadIcon( |
| 61 const base::FilePath& file_name, | 55 const base::FilePath& file_path, |
| 62 IconLoader::IconSize size, | 56 IconLoader::IconSize size, |
| 63 const IconRequestCallback& callback, | 57 const IconRequestCallback& callback, |
| 64 base::CancelableTaskTracker* tracker) { | 58 base::CancelableTaskTracker* tracker) { |
| 65 IconLoader* loader = new IconLoader(file_name, size, this); | 59 IconLoader* loader = new IconLoader(file_path, size, this); |
| 66 loader->AddRef(); | 60 loader->AddRef(); |
|
sky
2016/12/15 22:36:05
This code is equally error prone. Seems like the m
Avi (use Gerrit)
2016/12/15 23:01:10
Will address in followup.
| |
| 67 loader->Start(); | 61 loader->Start(); |
| 68 | 62 |
| 69 base::CancelableTaskTracker::IsCanceledCallback is_canceled; | 63 base::CancelableTaskTracker::IsCanceledCallback is_canceled; |
| 70 base::CancelableTaskTracker::TaskId id = | 64 base::CancelableTaskTracker::TaskId id = |
| 71 tracker->NewTrackedTaskId(&is_canceled); | 65 tracker->NewTrackedTaskId(&is_canceled); |
| 72 IconRequestCallback callback_runner = base::Bind( | 66 IconRequestCallback callback_runner = base::Bind( |
| 73 &RunCallbackIfNotCanceled, is_canceled, callback); | 67 &RunCallbackIfNotCanceled, is_canceled, callback); |
| 74 | 68 |
| 75 ClientRequest client_request = { callback_runner, file_name, size }; | 69 requests_[loader] = {callback_runner, file_path, size}; |
| 76 requests_[loader] = client_request; | |
| 77 return id; | 70 return id; |
| 78 } | 71 } |
| 79 | 72 |
| 80 // IconLoader::Delegate implementation ----------------------------------------- | 73 // IconLoader::Delegate implementation ----------------------------------------- |
| 81 | 74 |
| 82 bool IconManager::OnGroupLoaded(IconLoader* loader, | 75 void IconManager::OnImageLoaded(IconLoader* loader, |
| 83 const IconGroupID& group) { | 76 std::unique_ptr<gfx::Image> result, |
| 84 ClientRequests::iterator rit = requests_.find(loader); | 77 const IconLoader::IconGroup& group) { |
| 85 if (rit == requests_.end()) { | 78 auto request_it = requests_.find(loader); |
| 86 NOTREACHED(); | 79 DCHECK(request_it != requests_.end()); |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 gfx::Image* result = LookupIconFromGroup(group, rit->second.size); | |
| 91 if (!result) { | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 return OnImageLoaded(loader, result, group); | |
| 96 } | |
| 97 | |
| 98 bool IconManager::OnImageLoaded( | |
| 99 IconLoader* loader, gfx::Image* result, const IconGroupID& group) { | |
| 100 ClientRequests::iterator rit = requests_.find(loader); | |
| 101 | 80 |
| 102 // Balances the AddRef() in LoadIcon(). | 81 // Balances the AddRef() in LoadIcon(). |
| 103 loader->Release(); | 82 loader->Release(); |
| 104 | 83 |
| 105 // Look up our client state. | 84 const ClientRequest& client_request = request_it->second; |
| 106 if (rit == requests_.end()) { | |
| 107 NOTREACHED(); | |
| 108 return false; // Return false to indicate result should be deleted. | |
| 109 } | |
| 110 | 85 |
| 111 const ClientRequest& client_request = rit->second; | 86 // Cache the bitmap. Watch out: |result| may be null, which indicates a |
| 112 | 87 // failure. We assume that if we have an entry in |icon_cache_| it must not be |
| 113 // Cache the bitmap. Watch out: |result| may be NULL to indicate a current | 88 // null. |
| 114 // failure. We assume that if we have an entry in |icon_cache_| | |
| 115 // it must not be NULL. | |
| 116 CacheKey key(group, client_request.size); | 89 CacheKey key(group, client_request.size); |
| 117 IconMap::iterator it = icon_cache_.find(key); | 90 if (result) { |
| 118 if (it != icon_cache_.end()) { | 91 client_request.callback.Run(result.get()); |
| 119 if (!result) { | 92 icon_cache_[key] = std::move(result); |
| 120 delete it->second; | 93 } else { |
| 121 icon_cache_.erase(it); | 94 client_request.callback.Run(nullptr); |
| 122 } else if (result != it->second) { | 95 icon_cache_.erase(key); |
| 123 it->second->SwapRepresentations(result); | |
| 124 delete result; | |
| 125 result = it->second; | |
| 126 } | |
| 127 } else if (result) { | |
| 128 icon_cache_[key] = result; | |
| 129 } | 96 } |
| 130 | 97 |
| 131 group_cache_[client_request.file_path] = group; | 98 group_cache_[client_request.file_path] = group; |
| 132 | 99 |
| 133 // Inform our client that the request has completed. | 100 requests_.erase(request_it); |
| 134 client_request.callback.Run(result); | |
| 135 requests_.erase(rit); | |
| 136 | |
| 137 return true; // Indicates we took ownership of result. | |
| 138 } | 101 } |
| 139 | 102 |
| 140 IconManager::CacheKey::CacheKey(const IconGroupID& group, | 103 IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group, |
| 141 IconLoader::IconSize size) | 104 IconLoader::IconSize size) |
| 142 : group(group), | 105 : group(group), size(size) {} |
| 143 size(size) { | |
| 144 } | |
| 145 | 106 |
| 146 bool IconManager::CacheKey::operator<(const CacheKey &other) const { | 107 bool IconManager::CacheKey::operator<(const CacheKey &other) const { |
| 147 return std::tie(group, size) < std::tie(other.group, other.size); | 108 return std::tie(group, size) < std::tie(other.group, other.size); |
| 148 } | 109 } |
| OLD | NEW |