| 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 base::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 base::FilePath& file_name, | 41 gfx::Image* IconManager::LookupIconFromFilepath(const base::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 base::FilePath& file_name, | 60 const base::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 if (rit == requests_.end()) { |
| 84 NOTREACHED(); |
| 85 return false; |
| 86 } |
| 87 |
| 88 gfx::Image* result = LookupIconFromGroup(group, rit->second.size); |
| 89 if (!result) { |
| 90 return false; |
| 91 } |
| 92 |
| 93 return OnImageLoaded(loader, result, group); |
| 94 } |
| 95 |
| 96 bool IconManager::OnImageLoaded( |
| 97 IconLoader* loader, gfx::Image* result, const IconGroupID& group) { |
| 75 ClientRequests::iterator rit = requests_.find(loader); | 98 ClientRequests::iterator rit = requests_.find(loader); |
| 76 | 99 |
| 77 // Balances the AddRef() in LoadIcon(). | 100 // Balances the AddRef() in LoadIcon(). |
| 78 loader->Release(); | 101 loader->Release(); |
| 79 | 102 |
| 80 // Look up our client state. | 103 // Look up our client state. |
| 81 if (rit == requests_.end()) { | 104 if (rit == requests_.end()) { |
| 82 NOTREACHED(); | 105 NOTREACHED(); |
| 83 return false; // Return false to indicate result should be deleted. | 106 return false; // Return false to indicate result should be deleted. |
| 84 } | 107 } |
| 85 | 108 |
| 86 const ClientRequest& client_request = rit->second; | 109 const ClientRequest& client_request = rit->second; |
| 87 | 110 |
| 88 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to | 111 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to |
| 89 // indicate a current or past failure. | 112 // indicate a current or past failure. |
| 90 CacheKey key(client_request.group, client_request.size); | 113 CacheKey key(group, client_request.size); |
| 91 IconMap::iterator it = icon_cache_.find(key); | 114 IconMap::iterator it = icon_cache_.find(key); |
| 92 if (it != icon_cache_.end() && result && it->second) { | 115 if (it != icon_cache_.end() && result && it->second) { |
| 93 it->second->SwapRepresentations(result); | 116 if (it->second != result) { |
| 94 delete result; | 117 it->second->SwapRepresentations(result); |
| 95 result = it->second; | 118 delete result; |
| 119 result = it->second; |
| 120 } |
| 96 } else { | 121 } else { |
| 97 icon_cache_[key] = result; | 122 icon_cache_[key] = result; |
| 98 } | 123 } |
| 99 | 124 |
| 125 group_cache_[client_request.file_path] = group; |
| 126 |
| 100 // Inform our client that the request has completed. | 127 // Inform our client that the request has completed. |
| 101 client_request.callback.Run(result); | 128 client_request.callback.Run(result); |
| 102 requests_.erase(rit); | 129 requests_.erase(rit); |
| 103 | 130 |
| 104 return true; // Indicates we took ownership of result. | 131 return true; // Indicates we took ownership of result. |
| 105 } | 132 } |
| 106 | 133 |
| 107 IconManager::CacheKey::CacheKey(const IconGroupID& group, | 134 IconManager::CacheKey::CacheKey(const IconGroupID& group, |
| 108 IconLoader::IconSize size) | 135 IconLoader::IconSize size) |
| 109 : group(group), | 136 : group(group), |
| 110 size(size) { | 137 size(size) { |
| 111 } | 138 } |
| 112 | 139 |
| 113 bool IconManager::CacheKey::operator<(const CacheKey &other) const { | 140 bool IconManager::CacheKey::operator<(const CacheKey &other) const { |
| 114 if (group != other.group) | 141 if (group != other.group) |
| 115 return group < other.group; | 142 return group < other.group; |
| 116 return size < other.size; | 143 return size < other.size; |
| 117 } | 144 } |
| OLD | NEW |