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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.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 CancelableTaskTracker::IsCanceledCallback& is_canceled, | 18 const 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 struct IconManager::ClientRequest { |
| 29 IconRequestCallback callback; | 29 IconRequestCallback callback; |
| 30 IconGroupID group; | 30 FilePath file_path; |
| 31 IconLoader::IconSize size; | 31 IconLoader::IconSize size; |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 IconManager::IconManager() { | 34 IconManager::IconManager() { |
| 35 } | 35 } |
| 36 | 36 |
| 37 IconManager::~IconManager() { | 37 IconManager::~IconManager() { |
| 38 STLDeleteValues(&icon_cache_); | 38 STLDeleteValues(&icon_cache_); |
| 39 } | 39 } |
| 40 | 40 |
| 41 gfx::Image* IconManager::LookupIcon(const FilePath& file_name, | 41 gfx::Image* IconManager::LookupIconFromFilepath(const FilePath& file_name, |
| 42 IconLoader::IconSize size) { | 42 IconLoader::IconSize size) { |
| 43 IconGroupID group = GetGroupIDFromFilepath(file_name); | 43 GroupMap::iterator it = group_cache_.find(file_name); |
| 44 if (it != group_cache_.end()) | |
| 45 return LookupIconFromGroup(it->second, size); | |
| 46 | |
| 47 return NULL; | |
| 48 } | |
| 49 | |
| 50 gfx::Image* IconManager::LookupIconFromGroup(const IconGroupID& group, | |
| 51 IconLoader::IconSize size) { | |
| 44 IconMap::iterator it = icon_cache_.find(CacheKey(group, size)); | 52 IconMap::iterator it = icon_cache_.find(CacheKey(group, size)); |
| 45 if (it != icon_cache_.end()) | 53 if (it != icon_cache_.end()) |
| 46 return it->second; | 54 return it->second; |
| 47 | 55 |
| 48 return NULL; | 56 return NULL; |
| 49 } | 57 } |
| 50 | 58 |
| 51 CancelableTaskTracker::TaskId IconManager::LoadIcon( | 59 CancelableTaskTracker::TaskId IconManager::LoadIcon( |
| 52 const FilePath& file_name, | 60 const FilePath& file_name, |
| 53 IconLoader::IconSize size, | 61 IconLoader::IconSize size, |
| 54 const IconRequestCallback& callback, | 62 const IconRequestCallback& callback, |
| 55 CancelableTaskTracker* tracker) { | 63 CancelableTaskTracker* tracker) { |
| 56 IconGroupID group = GetGroupIDFromFilepath(file_name); | 64 IconLoader* loader = new IconLoader(file_name, size, this); |
| 57 | |
| 58 IconLoader* loader = new IconLoader(group, size, this); | |
| 59 loader->AddRef(); | 65 loader->AddRef(); |
| 60 loader->Start(); | 66 loader->Start(); |
| 61 | 67 |
| 62 CancelableTaskTracker::IsCanceledCallback is_canceled; | 68 CancelableTaskTracker::IsCanceledCallback is_canceled; |
| 63 CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled); | 69 CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled); |
| 64 IconRequestCallback callback_runner = base::Bind( | 70 IconRequestCallback callback_runner = base::Bind( |
| 65 &RunCallbackIfNotCanceled, is_canceled, callback); | 71 &RunCallbackIfNotCanceled, is_canceled, callback); |
| 66 | 72 |
| 67 ClientRequest client_request = { callback_runner, group, size }; | 73 ClientRequest client_request = { callback_runner, file_name, size }; |
| 68 requests_[loader] = client_request; | 74 requests_[loader] = client_request; |
| 69 return id; | 75 return id; |
| 70 } | 76 } |
| 71 | 77 |
| 72 // IconLoader::Delegate implementation ----------------------------------------- | 78 // IconLoader::Delegate implementation ----------------------------------------- |
| 73 | 79 |
| 74 bool IconManager::OnImageLoaded(IconLoader* loader, gfx::Image* result) { | 80 bool IconManager::OnGroupLoaded(IconLoader* loader, |
| 81 const IconGroupID& group) { | |
| 82 ClientRequests::iterator rit = requests_.find(loader); | |
| 83 | |
| 84 // Look up our client state. | |
|
Robert Sesek
2013/02/13 17:33:05
This comment doesn't add much value. Remove it and
shatch
2013/02/21 20:02:06
Done.
| |
| 85 if (rit == requests_.end()) { | |
| 86 NOTREACHED(); | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 const ClientRequest& client_request = rit->second; | |
|
Robert Sesek
2013/02/13 17:33:05
nit: you only consult this on line 92, so you can
shatch
2013/02/21 20:02:06
Done.
| |
| 91 | |
| 92 gfx::Image* result = LookupIconFromGroup(group, client_request.size); | |
| 93 if (!result) { | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 return OnImageLoaded(loader, result, group); | |
| 98 } | |
| 99 | |
| 100 bool IconManager::OnImageLoaded( | |
| 101 IconLoader* loader, gfx::Image* result, const IconGroupID& group) { | |
| 75 ClientRequests::iterator rit = requests_.find(loader); | 102 ClientRequests::iterator rit = requests_.find(loader); |
| 76 | 103 |
| 77 // Balances the AddRef() in LoadIcon(). | 104 // Balances the AddRef() in LoadIcon(). |
| 78 loader->Release(); | 105 loader->Release(); |
| 79 | 106 |
| 80 // Look up our client state. | 107 // Look up our client state. |
| 81 if (rit == requests_.end()) { | 108 if (rit == requests_.end()) { |
| 82 NOTREACHED(); | 109 NOTREACHED(); |
| 83 return false; // Return false to indicate result should be deleted. | 110 return false; // Return false to indicate result should be deleted. |
| 84 } | 111 } |
| 85 | 112 |
| 86 const ClientRequest& client_request = rit->second; | 113 const ClientRequest& client_request = rit->second; |
| 87 | 114 |
| 88 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to | 115 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to |
| 89 // indicate a current or past failure. | 116 // indicate a current or past failure. |
| 90 CacheKey key(client_request.group, client_request.size); | 117 CacheKey key(group, client_request.size); |
| 91 IconMap::iterator it = icon_cache_.find(key); | 118 IconMap::iterator it = icon_cache_.find(key); |
| 92 if (it != icon_cache_.end() && result && it->second) { | 119 if (it != icon_cache_.end() && result && it->second) { |
| 93 it->second->SwapRepresentations(result); | 120 if (it->second != result) { |
| 94 delete result; | 121 it->second->SwapRepresentations(result); |
| 95 result = it->second; | 122 delete result; |
| 123 result = it->second; | |
| 124 } | |
| 96 } else { | 125 } else { |
| 97 icon_cache_[key] = result; | 126 icon_cache_[key] = result; |
| 98 } | 127 } |
| 99 | 128 |
| 129 group_cache_[client_request.file_path] = group; | |
| 130 | |
| 100 // Inform our client that the request has completed. | 131 // Inform our client that the request has completed. |
| 101 client_request.callback.Run(result); | 132 client_request.callback.Run(result); |
| 102 requests_.erase(rit); | 133 requests_.erase(rit); |
| 103 | 134 |
| 104 return true; // Indicates we took ownership of result. | 135 return true; // Indicates we took ownership of result. |
| 105 } | 136 } |
| 106 | 137 |
| 107 IconManager::CacheKey::CacheKey(const IconGroupID& group, | 138 IconManager::CacheKey::CacheKey(const IconGroupID& group, |
| 108 IconLoader::IconSize size) | 139 IconLoader::IconSize size) |
| 109 : group(group), | 140 : group(group), |
| 110 size(size) { | 141 size(size) { |
| 111 } | 142 } |
| 112 | 143 |
| 113 bool IconManager::CacheKey::operator<(const CacheKey &other) const { | 144 bool IconManager::CacheKey::operator<(const CacheKey &other) const { |
| 114 if (group != other.group) | 145 if (group != other.group) |
| 115 return group < other.group; | 146 return group < other.group; |
| 116 return size < other.size; | 147 return size < other.size; |
| 117 } | 148 } |
| OLD | NEW |