license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/icon_manager.h" |
| 6 | |
| 7 | #include "base/file_util.h" |
| 8 | #include "base/scoped_ptr.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 9 | #include "base/stl_util-inl.h" |
[email protected] | c43c668 | 2009-05-19 14:51:44 | [diff] [blame^] | 10 | #include "third_party/skia/include/core/SkBitmap.h" |
| 11 | #include "third_party/skia/include/core/SkCanvas.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | |
| 13 | IconManager::IconManager() { |
| 14 | } |
| 15 | |
| 16 | IconManager::~IconManager() { |
| 17 | STLDeleteValues(&icon_cache_); |
| 18 | } |
| 19 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 20 | SkBitmap* IconManager::LookupIcon(const FilePath& file_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | IconLoader::IconSize size) { |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 22 | IconGroupID group = GetGroupIDFromFilepath(file_name); |
| 23 | IconMap::iterator it = icon_cache_.find(CacheKey(group, size)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | if (it != icon_cache_.end()) |
| 25 | return it->second; |
| 26 | |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | IconManager::Handle IconManager::LoadIcon( |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 31 | const FilePath& file_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | IconLoader::IconSize size, |
| 33 | CancelableRequestConsumerBase* consumer, |
| 34 | IconRequestCallback* callback) { |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 35 | IconGroupID group = GetGroupIDFromFilepath(file_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | IconRequest* request = new IconRequest(callback); |
| 37 | AddRequest(request, consumer); |
| 38 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 39 | IconLoader* loader = new IconLoader(group, size, this); |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 40 | loader->AddRef(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | loader->Start(); |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 42 | ClientRequest client_request = { request, group, size }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | requests_[loader] = client_request; |
| 44 | return request->handle(); |
| 45 | } |
| 46 | |
| 47 | // IconLoader::Delegate implementation ----------------------------------------- |
| 48 | |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 49 | bool IconManager::OnBitmapLoaded(IconLoader* source, SkBitmap* result) { |
| 50 | ClientRequests::iterator rit = requests_.find(source); |
| 51 | // Balances the AddRef() in LoadIcon(). |
| 52 | source->Release(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
| 54 | // Look up our client state. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | if (rit == requests_.end()) { |
| 56 | NOTREACHED(); |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 57 | return false; // Return false to indicate result should be deleted. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ClientRequest client_request = rit->second; |
| 61 | if (client_request.request->canceled()) { |
| 62 | requests_.erase(rit); |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 63 | return false; // Return false to indicate result should be deleted. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | e79f10f3 | 2008-11-07 16:13:31 | [diff] [blame] | 66 | // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to |
| 67 | // indicate a current or past failure. |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 68 | CacheKey key(client_request.group, client_request.size); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | IconMap::iterator it = icon_cache_.find(key); |
[email protected] | e79f10f3 | 2008-11-07 16:13:31 | [diff] [blame] | 70 | if (it != icon_cache_.end() && result && it->second) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | it->second->swap(*result); |
| 72 | delete result; |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 73 | result = it->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | } else { |
| 75 | icon_cache_[key] = result; |
| 76 | } |
| 77 | |
| 78 | // Inform our client that the request has completed. |
| 79 | IconRequest* icon_request = client_request.request; |
| 80 | icon_request->ForwardResult(IconRequest::TupleType(icon_request->handle(), |
| 81 | result)); |
| 82 | requests_.erase(rit); |
| 83 | |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 84 | return true; // Indicates we took ownership of result. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 87 | IconManager::CacheKey::CacheKey(const IconGroupID& group, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | IconLoader::IconSize size) |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 89 | : group(group), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | size(size) { |
| 91 | } |
| 92 | |
| 93 | bool IconManager::CacheKey::operator<(const CacheKey &other) const { |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 94 | if (group != other.group) |
| 95 | return group < other.group; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | return size < other.size; |
| 97 | } |