Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / chrome / browser / icon_manager.cc
blob6c7f9fc3753a9b5104fd7fab2e20c0a158f38d33
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
3 // found in the LICENSE file.
5 #include "chrome/browser/icon_manager.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h"
10 #include "base/task_runner.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkCanvas.h"
14 namespace {
16 void RunCallbackIfNotCanceled(
17 const CancelableTaskTracker::IsCanceledCallback& is_canceled,
18 const IconManager::IconRequestCallback& callback,
19 gfx::Image* image) {
20 if (is_canceled.Run())
21 return;
22 callback.Run(image);
25 } // namespace
27 struct IconManager::ClientRequest {
28 IconRequestCallback callback;
29 base::FilePath file_path;
30 IconLoader::IconSize size;
33 IconManager::IconManager() {
36 IconManager::~IconManager() {
37 STLDeleteValues(&icon_cache_);
40 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_name,
41 IconLoader::IconSize size) {
42 GroupMap::iterator it = group_cache_.find(file_name);
43 if (it != group_cache_.end())
44 return LookupIconFromGroup(it->second, size);
46 return NULL;
49 gfx::Image* IconManager::LookupIconFromGroup(const IconGroupID& group,
50 IconLoader::IconSize size) {
51 IconMap::iterator it = icon_cache_.find(CacheKey(group, size));
52 if (it != icon_cache_.end())
53 return it->second;
55 return NULL;
58 CancelableTaskTracker::TaskId IconManager::LoadIcon(
59 const base::FilePath& file_name,
60 IconLoader::IconSize size,
61 const IconRequestCallback& callback,
62 CancelableTaskTracker* tracker) {
63 IconLoader* loader = new IconLoader(file_name, size, this);
64 loader->AddRef();
65 loader->Start();
67 CancelableTaskTracker::IsCanceledCallback is_canceled;
68 CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled);
69 IconRequestCallback callback_runner = base::Bind(
70 &RunCallbackIfNotCanceled, is_canceled, callback);
72 ClientRequest client_request = { callback_runner, file_name, size };
73 requests_[loader] = client_request;
74 return id;
77 // IconLoader::Delegate implementation -----------------------------------------
79 bool IconManager::OnGroupLoaded(IconLoader* loader,
80 const IconGroupID& group) {
81 ClientRequests::iterator rit = requests_.find(loader);
82 if (rit == requests_.end()) {
83 NOTREACHED();
84 return false;
87 gfx::Image* result = LookupIconFromGroup(group, rit->second.size);
88 if (!result) {
89 return false;
92 return OnImageLoaded(loader, result, group);
95 bool IconManager::OnImageLoaded(
96 IconLoader* loader, gfx::Image* result, const IconGroupID& group) {
97 ClientRequests::iterator rit = requests_.find(loader);
99 // Balances the AddRef() in LoadIcon().
100 loader->Release();
102 // Look up our client state.
103 if (rit == requests_.end()) {
104 NOTREACHED();
105 return false; // Return false to indicate result should be deleted.
108 const ClientRequest& client_request = rit->second;
110 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to
111 // indicate a current or past failure.
112 CacheKey key(group, client_request.size);
113 IconMap::iterator it = icon_cache_.find(key);
114 if (it != icon_cache_.end() && result && it->second) {
115 if (it->second != result) {
116 it->second->SwapRepresentations(result);
117 delete result;
118 result = it->second;
120 } else {
121 icon_cache_[key] = result;
124 group_cache_[client_request.file_path] = group;
126 // Inform our client that the request has completed.
127 client_request.callback.Run(result);
128 requests_.erase(rit);
130 return true; // Indicates we took ownership of result.
133 IconManager::CacheKey::CacheKey(const IconGroupID& group,
134 IconLoader::IconSize size)
135 : group(group),
136 size(size) {
139 bool IconManager::CacheKey::operator<(const CacheKey &other) const {
140 if (group != other.group)
141 return group < other.group;
142 return size < other.size;