Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(793)

Side by Side Diff: chrome/browser/icon_manager.cc

Issue 2440273002: Clean up the IconLoader. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <memory> 7 #include <memory>
8 #include <tuple> 8 #include <tuple>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/stl_util.h"
12 #include "base/task_runner.h" 11 #include "base/task_runner.h"
13 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/core/SkCanvas.h" 13 #include "third_party/skia/include/core/SkCanvas.h"
15 14
16 namespace { 15 namespace {
17 16
18 void RunCallbackIfNotCanceled( 17 void RunCallbackIfNotCanceled(
19 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, 18 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
20 const IconManager::IconRequestCallback& callback, 19 const IconManager::IconRequestCallback& callback,
21 gfx::Image* image) { 20 gfx::Image* image) {
22 if (is_canceled.Run()) 21 if (is_canceled.Run())
23 return; 22 return;
24 callback.Run(image); 23 callback.Run(image);
25 } 24 }
26 25
27 } // namespace 26 } // namespace
28 27
29 struct IconManager::ClientRequest { 28 struct IconManager::ClientRequest {
30 IconRequestCallback callback; 29 IconRequestCallback callback;
31 base::FilePath file_path; 30 IconLoader::IconGroup group;
32 IconLoader::IconSize size; 31 IconLoader::IconSize size;
33 }; 32 };
34 33
35 IconManager::IconManager() { 34 IconManager::IconManager() {
36 } 35 }
37 36
38 IconManager::~IconManager() { 37 IconManager::~IconManager() {
39 base::STLDeleteValues(&icon_cache_);
40 } 38 }
41 39
42 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_name, 40 gfx::Image* IconManager::LookupIconFromFilepath(const base::FilePath& file_path,
43 IconLoader::IconSize size) { 41 IconLoader::IconSize size) {
44 GroupMap::iterator it = group_cache_.find(file_name); 42 IconLoader::IconGroup group = IconLoader::GroupForFilepath(file_path);
45 if (it != group_cache_.end()) 43 auto it = icon_cache_.find(CacheKey(group, size));
46 return LookupIconFromGroup(it->second, size); 44 if (it != icon_cache_.end())
45 return it->second.get();
47 46
48 return NULL; 47 return nullptr;
49 }
50
51 gfx::Image* IconManager::LookupIconFromGroup(const IconGroupID& group,
52 IconLoader::IconSize size) {
53 IconMap::iterator it = icon_cache_.find(CacheKey(group, size));
54 if (it != icon_cache_.end())
55 return it->second;
56
57 return NULL;
58 } 48 }
59 49
60 base::CancelableTaskTracker::TaskId IconManager::LoadIcon( 50 base::CancelableTaskTracker::TaskId IconManager::LoadIcon(
61 const base::FilePath& file_name, 51 const base::FilePath& file_path,
62 IconLoader::IconSize size, 52 IconLoader::IconSize size,
63 const IconRequestCallback& callback, 53 const IconRequestCallback& callback,
64 base::CancelableTaskTracker* tracker) { 54 base::CancelableTaskTracker* tracker) {
65 IconLoader* loader = new IconLoader(file_name, size, this); 55 IconLoader::IconGroup group = IconLoader::GroupForFilepath(file_path);
56 IconLoader* loader = new IconLoader(group, size, this);
66 loader->AddRef(); 57 loader->AddRef();
67 loader->Start(); 58 loader->Start();
68 59
69 base::CancelableTaskTracker::IsCanceledCallback is_canceled; 60 base::CancelableTaskTracker::IsCanceledCallback is_canceled;
70 base::CancelableTaskTracker::TaskId id = 61 base::CancelableTaskTracker::TaskId id =
71 tracker->NewTrackedTaskId(&is_canceled); 62 tracker->NewTrackedTaskId(&is_canceled);
72 IconRequestCallback callback_runner = base::Bind( 63 IconRequestCallback callback_runner = base::Bind(
73 &RunCallbackIfNotCanceled, is_canceled, callback); 64 &RunCallbackIfNotCanceled, is_canceled, callback);
74 65
75 ClientRequest client_request = { callback_runner, file_name, size }; 66 requests_[loader] = {callback_runner, group, size};
76 requests_[loader] = client_request;
77 return id; 67 return id;
78 } 68 }
79 69
80 // IconLoader::Delegate implementation ----------------------------------------- 70 // IconLoader::Delegate implementation -----------------------------------------
81 71
82 bool IconManager::OnGroupLoaded(IconLoader* loader, 72 void IconManager::OnImageLoaded(IconLoader* loader,
83 const IconGroupID& group) { 73 std::unique_ptr<gfx::Image> result,
84 ClientRequests::iterator rit = requests_.find(loader); 74 const IconLoader::IconGroup& group) {
85 if (rit == requests_.end()) { 75 auto rit = requests_.find(loader);
86 NOTREACHED(); 76 DCHECK(rit != requests_.end());
87 return false;
88 }
89
90 gfx::Image* result = LookupIconFromGroup(group, rit->second.size);
91 if (!result) {
92 return false;
93 }
94
95 return OnImageLoaded(loader, result, group);
96 }
97
98 bool IconManager::OnImageLoaded(
99 IconLoader* loader, gfx::Image* result, const IconGroupID& group) {
100 ClientRequests::iterator rit = requests_.find(loader);
101 77
102 // Balances the AddRef() in LoadIcon(). 78 // Balances the AddRef() in LoadIcon().
103 loader->Release(); 79 loader->Release();
104 80
105 // Look up our client state. 81 const ClientRequest& client_request = rit->second;
106 if (rit == requests_.end()) { 82
107 NOTREACHED(); 83 // Cache the bitmap. Watch out: |result| may be null to indicate a current
108 return false; // Return false to indicate result should be deleted. 84 // failure. We assume that if we have an entry in |icon_cache_|
85 // it must not be null.
86 CacheKey key(group, client_request.size);
87 if (result) {
88 client_request.callback.Run(result.get());
89 icon_cache_[key] = std::move(result);
90 } else {
91 client_request.callback.Run(nullptr);
92 icon_cache_.erase(key);
109 } 93 }
110 94
111 const ClientRequest& client_request = rit->second;
112
113 // Cache the bitmap. Watch out: |result| may be NULL to indicate a current
114 // failure. We assume that if we have an entry in |icon_cache_|
115 // it must not be NULL.
116 CacheKey key(group, client_request.size);
117 IconMap::iterator it = icon_cache_.find(key);
118 if (it != icon_cache_.end()) {
119 if (!result) {
120 delete it->second;
121 icon_cache_.erase(it);
122 } else if (result != it->second) {
123 it->second->SwapRepresentations(result);
124 delete result;
125 result = it->second;
126 }
127 } else if (result) {
128 icon_cache_[key] = result;
129 }
130
131 group_cache_[client_request.file_path] = group;
132
133 // Inform our client that the request has completed.
134 client_request.callback.Run(result);
135 requests_.erase(rit); 95 requests_.erase(rit);
136
137 return true; // Indicates we took ownership of result.
138 } 96 }
139 97
140 IconManager::CacheKey::CacheKey(const IconGroupID& group, 98 IconManager::CacheKey::CacheKey(const IconLoader::IconGroup& group,
141 IconLoader::IconSize size) 99 IconLoader::IconSize size)
142 : group(group), 100 : group(group), size(size) {}
143 size(size) {
144 }
145 101
146 bool IconManager::CacheKey::operator<(const CacheKey &other) const { 102 bool IconManager::CacheKey::operator<(const CacheKey &other) const {
147 return std::tie(group, size) < std::tie(other.group, other.size); 103 return std::tie(group, size) < std::tie(other.group, other.size);
148 } 104 }
OLDNEW
« chrome/browser/icon_loader_auralinux.cc ('K') | « chrome/browser/icon_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698