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

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

Issue 11441006: Convert IconManager to use new CancelableTaskTracker (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and fix mac compiling Created 8 years 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/icon_manager.h ('k') | chrome/browser/ui/cocoa/download/download_item_mac.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/bind.h"
7 #include "base/file_util.h" 8 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/task_runner.h"
10 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "third_party/skia/include/core/SkCanvas.h" 13 #include "third_party/skia/include/core/SkCanvas.h"
12 14
15 namespace {
16
17 void RunCallbackIfNotCanceled(
18 const CancelableTaskTracker::IsCanceledCallback& is_canceled,
19 const IconManager::IconRequestCallback& callback,
20 gfx::Image* image) {
21 if (is_canceled.Run())
22 return;
23 callback.Run(image);
24 }
25
26 } // namespace
27
13 struct IconManager::ClientRequest { 28 struct IconManager::ClientRequest {
14 scoped_refptr<IconRequest> request; 29 IconRequestCallback callback;
15 IconGroupID group; 30 IconGroupID group;
16 IconLoader::IconSize size; 31 IconLoader::IconSize size;
17 }; 32 };
18 33
19 IconManager::IconManager() { 34 IconManager::IconManager() {
20 } 35 }
21 36
22 IconManager::~IconManager() { 37 IconManager::~IconManager() {
23 STLDeleteValues(&icon_cache_); 38 STLDeleteValues(&icon_cache_);
24 } 39 }
25 40
26 gfx::Image* IconManager::LookupIcon(const FilePath& file_name, 41 gfx::Image* IconManager::LookupIcon(const FilePath& file_name,
27 IconLoader::IconSize size) { 42 IconLoader::IconSize size) {
28 IconGroupID group = GetGroupIDFromFilepath(file_name); 43 IconGroupID group = GetGroupIDFromFilepath(file_name);
29 IconMap::iterator it = icon_cache_.find(CacheKey(group, size)); 44 IconMap::iterator it = icon_cache_.find(CacheKey(group, size));
30 if (it != icon_cache_.end()) 45 if (it != icon_cache_.end())
31 return it->second; 46 return it->second;
32 47
33 return NULL; 48 return NULL;
34 } 49 }
35 50
36 IconManager::Handle IconManager::LoadIcon( 51 CancelableTaskTracker::TaskId IconManager::LoadIcon(
37 const FilePath& file_name, 52 const FilePath& file_name,
38 IconLoader::IconSize size, 53 IconLoader::IconSize size,
39 CancelableRequestConsumerBase* consumer, 54 const IconRequestCallback& callback,
40 const IconRequestCallback& callback) { 55 CancelableTaskTracker* tracker) {
41 IconGroupID group = GetGroupIDFromFilepath(file_name); 56 IconGroupID group = GetGroupIDFromFilepath(file_name);
42 IconRequest* request = new IconRequest(callback);
43 AddRequest(request, consumer);
44 57
45 IconLoader* loader = new IconLoader(group, size, this); 58 IconLoader* loader = new IconLoader(group, size, this);
46 loader->AddRef(); 59 loader->AddRef();
47 loader->Start(); 60 loader->Start();
48 ClientRequest client_request = { request, group, size }; 61
62 CancelableTaskTracker::IsCanceledCallback is_canceled;
63 CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled);
64 IconRequestCallback callback_runner = base::Bind(
65 &RunCallbackIfNotCanceled, is_canceled, callback);
66
67 ClientRequest client_request = { callback_runner, group, size };
49 requests_[loader] = client_request; 68 requests_[loader] = client_request;
50 return request->handle(); 69 return id;
51 } 70 }
52 71
53 // IconLoader::Delegate implementation ----------------------------------------- 72 // IconLoader::Delegate implementation -----------------------------------------
54 73
55 bool IconManager::OnImageLoaded(IconLoader* source, gfx::Image* result) { 74 bool IconManager::OnImageLoaded(IconLoader* loader, gfx::Image* result) {
56 ClientRequests::iterator rit = requests_.find(source); 75 ClientRequests::iterator rit = requests_.find(loader);
76
57 // Balances the AddRef() in LoadIcon(). 77 // Balances the AddRef() in LoadIcon().
58 source->Release(); 78 loader->Release();
59 79
60 // Look up our client state. 80 // Look up our client state.
61 if (rit == requests_.end()) { 81 if (rit == requests_.end()) {
62 NOTREACHED(); 82 NOTREACHED();
63 return false; // Return false to indicate result should be deleted. 83 return false; // Return false to indicate result should be deleted.
64 } 84 }
65 85
66 ClientRequest client_request = rit->second; 86 const ClientRequest& client_request = rit->second;
67 if (client_request.request->canceled()) {
68 requests_.erase(rit);
69 return false; // Return false to indicate result should be deleted.
70 }
71 87
72 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to 88 // Cache the bitmap. Watch out: |result| or the cached bitmap may be NULL to
73 // indicate a current or past failure. 89 // indicate a current or past failure.
74 CacheKey key(client_request.group, client_request.size); 90 CacheKey key(client_request.group, client_request.size);
75 IconMap::iterator it = icon_cache_.find(key); 91 IconMap::iterator it = icon_cache_.find(key);
76 if (it != icon_cache_.end() && result && it->second) { 92 if (it != icon_cache_.end() && result && it->second) {
77 it->second->SwapRepresentations(result); 93 it->second->SwapRepresentations(result);
78 delete result; 94 delete result;
79 result = it->second; 95 result = it->second;
80 } else { 96 } else {
81 icon_cache_[key] = result; 97 icon_cache_[key] = result;
82 } 98 }
83 99
84 // Inform our client that the request has completed. 100 // Inform our client that the request has completed.
85 IconRequest* icon_request = client_request.request; 101 client_request.callback.Run(result);
86 icon_request->ForwardResult(icon_request->handle(), result);
87 requests_.erase(rit); 102 requests_.erase(rit);
88 103
89 return true; // Indicates we took ownership of result. 104 return true; // Indicates we took ownership of result.
90 } 105 }
91 106
92 IconManager::CacheKey::CacheKey(const IconGroupID& group, 107 IconManager::CacheKey::CacheKey(const IconGroupID& group,
93 IconLoader::IconSize size) 108 IconLoader::IconSize size)
94 : group(group), 109 : group(group),
95 size(size) { 110 size(size) {
96 } 111 }
97 112
98 bool IconManager::CacheKey::operator<(const CacheKey &other) const { 113 bool IconManager::CacheKey::operator<(const CacheKey &other) const {
99 if (group != other.group) 114 if (group != other.group)
100 return group < other.group; 115 return group < other.group;
101 return size < other.size; 116 return size < other.size;
102 } 117 }
OLDNEW
« no previous file with comments | « chrome/browser/icon_manager.h ('k') | chrome/browser/ui/cocoa/download/download_item_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698