blob: 98ff3227c94434ff8dcc402ecec0cabd2510d4cc [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/icon_manager.h"
6
7#include "base/file_util.h"
8#include "base/scoped_ptr.h"
[email protected]807204142009-05-05 03:31:449#include "base/stl_util-inl.h"
[email protected]c43c6682009-05-19 14:51:4410#include "third_party/skia/include/core/SkBitmap.h"
11#include "third_party/skia/include/core/SkCanvas.h"
initial.commit09911bf2008-07-26 23:55:2912
13IconManager::IconManager() {
14}
15
16IconManager::~IconManager() {
17 STLDeleteValues(&icon_cache_);
18}
19
[email protected]7ced67af2009-04-14 23:25:1620SkBitmap* IconManager::LookupIcon(const FilePath& file_name,
initial.commit09911bf2008-07-26 23:55:2921 IconLoader::IconSize size) {
[email protected]0f38ceae2009-05-08 19:01:0222 IconGroupID group = GetGroupIDFromFilepath(file_name);
23 IconMap::iterator it = icon_cache_.find(CacheKey(group, size));
initial.commit09911bf2008-07-26 23:55:2924 if (it != icon_cache_.end())
25 return it->second;
26
27 return NULL;
28}
29
30IconManager::Handle IconManager::LoadIcon(
[email protected]7ced67af2009-04-14 23:25:1631 const FilePath& file_name,
initial.commit09911bf2008-07-26 23:55:2932 IconLoader::IconSize size,
33 CancelableRequestConsumerBase* consumer,
34 IconRequestCallback* callback) {
[email protected]0f38ceae2009-05-08 19:01:0235 IconGroupID group = GetGroupIDFromFilepath(file_name);
initial.commit09911bf2008-07-26 23:55:2936 IconRequest* request = new IconRequest(callback);
37 AddRequest(request, consumer);
38
[email protected]0f38ceae2009-05-08 19:01:0239 IconLoader* loader = new IconLoader(group, size, this);
[email protected]46728b1f2009-05-07 20:42:2440 loader->AddRef();
initial.commit09911bf2008-07-26 23:55:2941 loader->Start();
[email protected]0f38ceae2009-05-08 19:01:0242 ClientRequest client_request = { request, group, size };
initial.commit09911bf2008-07-26 23:55:2943 requests_[loader] = client_request;
44 return request->handle();
45}
46
47// IconLoader::Delegate implementation -----------------------------------------
48
[email protected]46728b1f2009-05-07 20:42:2449bool IconManager::OnBitmapLoaded(IconLoader* source, SkBitmap* result) {
50 ClientRequests::iterator rit = requests_.find(source);
51 // Balances the AddRef() in LoadIcon().
52 source->Release();
initial.commit09911bf2008-07-26 23:55:2953
54 // Look up our client state.
initial.commit09911bf2008-07-26 23:55:2955 if (rit == requests_.end()) {
56 NOTREACHED();
[email protected]cf2ef752008-09-11 21:19:2957 return false; // Return false to indicate result should be deleted.
initial.commit09911bf2008-07-26 23:55:2958 }
59
60 ClientRequest client_request = rit->second;
61 if (client_request.request->canceled()) {
62 requests_.erase(rit);
[email protected]cf2ef752008-09-11 21:19:2963 return false; // Return false to indicate result should be deleted.
initial.commit09911bf2008-07-26 23:55:2964 }
65
[email protected]e79f10f32008-11-07 16:13:3166 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to
67 // indicate a current or past failure.
[email protected]0f38ceae2009-05-08 19:01:0268 CacheKey key(client_request.group, client_request.size);
initial.commit09911bf2008-07-26 23:55:2969 IconMap::iterator it = icon_cache_.find(key);
[email protected]e79f10f32008-11-07 16:13:3170 if (it != icon_cache_.end() && result && it->second) {
initial.commit09911bf2008-07-26 23:55:2971 it->second->swap(*result);
72 delete result;
[email protected]cf2ef752008-09-11 21:19:2973 result = it->second;
initial.commit09911bf2008-07-26 23:55:2974 } 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]cf2ef752008-09-11 21:19:2984 return true; // Indicates we took ownership of result.
initial.commit09911bf2008-07-26 23:55:2985}
86
[email protected]0f38ceae2009-05-08 19:01:0287IconManager::CacheKey::CacheKey(const IconGroupID& group,
initial.commit09911bf2008-07-26 23:55:2988 IconLoader::IconSize size)
[email protected]0f38ceae2009-05-08 19:01:0289 : group(group),
initial.commit09911bf2008-07-26 23:55:2990 size(size) {
91}
92
93bool IconManager::CacheKey::operator<(const CacheKey &other) const {
[email protected]0f38ceae2009-05-08 19:01:0294 if (group != other.group)
95 return group < other.group;
initial.commit09911bf2008-07-26 23:55:2996 return size < other.size;
97}