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" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "SkBitmap.h" |
| 11 | #include "SkCanvas.h" |
| 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] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 22 | FilePath path = file_name; |
| 23 | FilePath::StringType extension = file_util::GetFileExtensionFromPath(path); |
| 24 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | if (extension != L"exe" && extension != L"dll" && extension != L"ico") |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 26 | path = FilePath(L'.' + extension); |
| 27 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
| 29 | IconMap::iterator it = icon_cache_.find(CacheKey(path, size)); |
| 30 | if (it != icon_cache_.end()) |
| 31 | return it->second; |
| 32 | |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | IconManager::Handle IconManager::LoadIcon( |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 37 | const FilePath& file_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | IconLoader::IconSize size, |
| 39 | CancelableRequestConsumerBase* consumer, |
| 40 | IconRequestCallback* callback) { |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 41 | FilePath path = file_name; |
| 42 | FilePath::StringType extension = file_util::GetFileExtensionFromPath(path); |
| 43 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | if (extension != L"exe" && extension != L"dll" && extension != L"ico") |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 45 | path = FilePath(L'.' + extension); |
| 46 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
| 48 | IconRequest* request = new IconRequest(callback); |
| 49 | AddRequest(request, consumer); |
| 50 | |
| 51 | IconLoader* loader = |
| 52 | IconLoader::CreateIconLoaderForFileResource(path, size, this); |
| 53 | loader->Start(); |
| 54 | ClientRequest client_request = { request, path, size }; |
| 55 | requests_[loader] = client_request; |
| 56 | return request->handle(); |
| 57 | } |
| 58 | |
| 59 | // IconLoader::Delegate implementation ----------------------------------------- |
| 60 | |
| 61 | bool IconManager::OnSkBitmapLoaded(IconLoader* source, SkBitmap* result) { |
| 62 | scoped_ptr<IconLoader> scoped_source(source); |
| 63 | |
| 64 | // Look up our client state. |
| 65 | ClientRequests::iterator rit = requests_.find(source); |
| 66 | if (rit == requests_.end()) { |
| 67 | NOTREACHED(); |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 68 | return false; // Return false to indicate result should be deleted. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ClientRequest client_request = rit->second; |
| 72 | if (client_request.request->canceled()) { |
| 73 | requests_.erase(rit); |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 74 | return false; // Return false to indicate result should be deleted. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | e79f10f3 | 2008-11-07 16:13:31 | [diff] [blame] | 77 | // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to |
| 78 | // indicate a current or past failure. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | CacheKey key(client_request.file_name, client_request.size); |
| 80 | IconMap::iterator it = icon_cache_.find(key); |
[email protected] | e79f10f3 | 2008-11-07 16:13:31 | [diff] [blame] | 81 | if (it != icon_cache_.end() && result && it->second) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | it->second->swap(*result); |
| 83 | delete result; |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 84 | result = it->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | } else { |
| 86 | icon_cache_[key] = result; |
| 87 | } |
| 88 | |
| 89 | // Inform our client that the request has completed. |
| 90 | IconRequest* icon_request = client_request.request; |
| 91 | icon_request->ForwardResult(IconRequest::TupleType(icon_request->handle(), |
| 92 | result)); |
| 93 | requests_.erase(rit); |
| 94 | |
[email protected] | cf2ef75 | 2008-09-11 21:19:29 | [diff] [blame] | 95 | return true; // Indicates we took ownership of result. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 98 | IconManager::CacheKey::CacheKey(const FilePath& file_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | IconLoader::IconSize size) |
| 100 | : file_name(file_name), |
| 101 | size(size) { |
| 102 | } |
| 103 | |
| 104 | bool IconManager::CacheKey::operator<(const CacheKey &other) const { |
| 105 | if (file_name != other.file_name) |
| 106 | return file_name < other.file_name; |
| 107 | return size < other.size; |
| 108 | } |