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

Side by Side Diff: components/suggestions/image_manager.cc

Issue 2000653002: Replace the usage of SkBitmap with gfx::Image in the suggestion service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replace SkBitmap with gfx::Image in the suggestions_service interface Created 4 years, 7 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/suggestions/image_manager.h" 5 #include "components/suggestions/image_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 12 matching lines...) Expand all
23 // synchronize with histograms.xml, AND will also become incompatible with older 23 // synchronize with histograms.xml, AND will also become incompatible with older
24 // browsers still reporting the previous values. 24 // browsers still reporting the previous values.
25 const char kDatabaseUMAClientName[] = "ImageManager"; 25 const char kDatabaseUMAClientName[] = "ImageManager";
26 26
27 std::unique_ptr<SkBitmap> DecodeImage( 27 std::unique_ptr<SkBitmap> DecodeImage(
28 scoped_refptr<base::RefCountedMemory> encoded_data) { 28 scoped_refptr<base::RefCountedMemory> encoded_data) {
29 return suggestions::DecodeJPEGToSkBitmap(encoded_data->front(), 29 return suggestions::DecodeJPEGToSkBitmap(encoded_data->front(),
30 encoded_data->size()); 30 encoded_data->size());
31 } 31 }
32 32
33 // Wraps an ImageManager callback so that it can be used with the ImageFetcher.
34 // ImageManager callbacks expect SkBitmaps while ImageFetcher callbacks expect
35 // gfx::Images. The image can be empty. In this case it is mapped to nullptr.
36 void WrapImageManagerCallback(
37 const base::Callback<void(const GURL&, const SkBitmap*)>& wrapped_callback,
38 const GURL& url,
39 const gfx::Image& image) {
40 const SkBitmap* bitmap = nullptr;
41 if (!image.IsEmpty())
42 bitmap = image.ToSkBitmap();
43 wrapped_callback.Run(url, bitmap);
44 }
45
46 } // namespace 33 } // namespace
47 34
48 namespace suggestions { 35 namespace suggestions {
49 36
50 ImageManager::ImageManager() : weak_ptr_factory_(this) {} 37 ImageManager::ImageManager() : weak_ptr_factory_(this) {}
51 38
52 ImageManager::ImageManager( 39 ImageManager::ImageManager(
53 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, 40 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
54 std::unique_ptr<ProtoDatabase<ImageData>> database, 41 std::unique_ptr<ProtoDatabase<ImageData>> database,
55 const base::FilePath& database_dir, 42 const base::FilePath& database_dir,
(...skipping 28 matching lines...) Expand all
84 } 71 }
85 } 72 }
86 73
87 void ImageManager::AddImageURL(const GURL& url, const GURL& image_url) { 74 void ImageManager::AddImageURL(const GURL& url, const GURL& image_url) {
88 DCHECK(thread_checker_.CalledOnValidThread()); 75 DCHECK(thread_checker_.CalledOnValidThread());
89 image_url_map_[url] = image_url; 76 image_url_map_[url] = image_url;
90 } 77 }
91 78
92 void ImageManager::GetImageForURL( 79 void ImageManager::GetImageForURL(
93 const GURL& url, 80 const GURL& url,
94 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 81 base::Callback<void(const GURL&, const gfx::Image&)> callback) {
95 DCHECK(thread_checker_.CalledOnValidThread()); 82 DCHECK(thread_checker_.CalledOnValidThread());
96 // If |url| is not found in |image_url_map_|, then invoke |callback| with 83 // If |url| is not found in |image_url_map_|, then invoke |callback| with
97 // NULL since there is no associated image for this |url|. 84 // NULL since there is no associated image for this |url|.
98 GURL image_url; 85 GURL image_url;
99 if (!GetImageURL(url, &image_url)) { 86 if (!GetImageURL(url, &image_url)) {
100 callback.Run(url, nullptr); 87 const gfx::Image empty_image;
88 callback.Run(url, empty_image);
Marc Treib 2016/05/20 14:40:04 Any reason for declaring an empty_image variable,
markusheintz_ 2016/05/23 13:20:46 Done.
101 return; 89 return;
102 } 90 }
103 91
104 // |database_| can be NULL if something went wrong in initialization. 92 // |database_| can be NULL if something went wrong in initialization.
105 if (database_.get() && !database_ready_) { 93 if (database_.get() && !database_ready_) {
106 // Once database is initialized, it will serve pending requests from either 94 // Once database is initialized, it will serve pending requests from either
107 // cache or network. 95 // cache or network.
108 QueueCacheRequest(url, image_url, callback); 96 QueueCacheRequest(url, image_url, callback);
109 return; 97 return;
110 } 98 }
(...skipping 10 matching lines...) Expand all
121 bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) { 109 bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) {
122 DCHECK(image_url); 110 DCHECK(image_url);
123 std::map<GURL, GURL>::iterator it = image_url_map_.find(url); 111 std::map<GURL, GURL>::iterator it = image_url_map_.find(url);
124 if (it == image_url_map_.end()) return false; // Not found. 112 if (it == image_url_map_.end()) return false; // Not found.
125 *image_url = it->second; 113 *image_url = it->second;
126 return true; 114 return true;
127 } 115 }
128 116
129 void ImageManager::QueueCacheRequest( 117 void ImageManager::QueueCacheRequest(
130 const GURL& url, const GURL& image_url, 118 const GURL& url, const GURL& image_url,
131 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 119 base::Callback<void(const GURL&, const gfx::Image&)> callback) {
132 // To be served when the database has loaded. 120 // To be served when the database has loaded.
133 ImageCacheRequestMap::iterator it = pending_cache_requests_.find(url); 121 ImageCacheRequestMap::iterator it = pending_cache_requests_.find(url);
134 if (it == pending_cache_requests_.end()) { 122 if (it == pending_cache_requests_.end()) {
135 ImageCacheRequest request; 123 ImageCacheRequest request;
136 request.url = url; 124 request.url = url;
137 request.image_url = image_url; 125 request.image_url = image_url;
138 request.callbacks.push_back(callback); 126 request.callbacks.push_back(callback);
139 pending_cache_requests_[url] = request; 127 pending_cache_requests_[url] = request;
140 } else { 128 } else {
141 // Request already queued for this url. 129 // Request already queued for this url.
142 it->second.callbacks.push_back(callback); 130 it->second.callbacks.push_back(callback);
143 } 131 }
144 } 132 }
145 133
146 void ImageManager::OnCacheImageDecoded( 134 void ImageManager::OnCacheImageDecoded(
147 const GURL& url, 135 const GURL& url,
148 const GURL& image_url, 136 const GURL& image_url,
149 base::Callback<void(const GURL&, const SkBitmap*)> callback, 137 const base::Callback<void(const GURL&, const gfx::Image&)>& callback,
150 std::unique_ptr<SkBitmap> bitmap) { 138 std::unique_ptr<SkBitmap> bitmap) {
151 if (bitmap.get()) { 139 if (bitmap.get()) {
152 callback.Run(url, bitmap.get()); 140 const gfx::Image image(gfx::Image::CreateFrom1xBitmap(*bitmap.get()));
Marc Treib 2016/05/20 14:40:04 I think the .get() isn't needed, you can directly
markusheintz_ 2016/05/23 13:20:46 Uups sorry forgot to remove done.
141 callback.Run(url, image);
Marc Treib 2016/05/20 14:40:03 Also here: I'd just inline the gfx::Image::CreateF
markusheintz_ 2016/05/23 13:20:46 Done.
153 } else { 142 } else {
154 image_fetcher_->StartOrQueueNetworkRequest( 143 image_fetcher_->StartOrQueueNetworkRequest(
155 url, image_url, base::Bind(&WrapImageManagerCallback, callback)); 144 url, image_url, callback);
156 } 145 }
157 } 146 }
158 147
159 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache( 148 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache(
160 const GURL& url) { 149 const GURL& url) {
161 ImageMap::iterator image_iter = image_map_.find(url.spec()); 150 ImageMap::iterator image_iter = image_map_.find(url.spec());
162 if (image_iter != image_map_.end()) { 151 if (image_iter != image_map_.end()) {
163 return image_iter->second; 152 return image_iter->second;
164 } 153 }
165 return nullptr; 154 return nullptr;
166 } 155 }
167 156
168 void ImageManager::ServeFromCacheOrNetwork( 157 void ImageManager::ServeFromCacheOrNetwork(
169 const GURL& url, 158 const GURL& url,
170 const GURL& image_url, 159 const GURL& image_url,
171 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 160 base::Callback<void(const GURL&, const gfx::Image&)> callback) {
172 scoped_refptr<base::RefCountedMemory> encoded_data = 161 scoped_refptr<base::RefCountedMemory> encoded_data =
173 GetEncodedImageFromCache(url); 162 GetEncodedImageFromCache(url);
174 if (encoded_data.get()) { 163 if (encoded_data.get()) {
175 base::PostTaskAndReplyWithResult( 164 base::PostTaskAndReplyWithResult(
176 background_task_runner_.get(), FROM_HERE, 165 background_task_runner_.get(), FROM_HERE,
177 base::Bind(&DecodeImage, encoded_data), 166 base::Bind(&DecodeImage, encoded_data),
178 base::Bind(&ImageManager::OnCacheImageDecoded, 167 base::Bind(&ImageManager::OnCacheImageDecoded,
179 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback)); 168 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback));
180 } else { 169 } else {
181 image_fetcher_->StartOrQueueNetworkRequest( 170 image_fetcher_->StartOrQueueNetworkRequest(
182 url, image_url, base::Bind(&WrapImageManagerCallback, callback)); 171 url, image_url, callback);
Marc Treib 2016/05/20 14:40:03 This should fit on the previous line now :)
markusheintz_ 2016/05/23 13:20:46 Done.
183 } 172 }
184 } 173 }
185 174
186 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { 175 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) {
187 scoped_refptr<base::RefCountedBytes> encoded_data( 176 scoped_refptr<base::RefCountedBytes> encoded_data(
188 new base::RefCountedBytes()); 177 new base::RefCountedBytes());
189 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) { 178 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) {
190 return; 179 return;
191 } 180 }
192 181
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 it != pending_cache_requests_.end(); ++it) { 249 it != pending_cache_requests_.end(); ++it) {
261 const ImageCacheRequest& request = it->second; 250 const ImageCacheRequest& request = it->second;
262 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); 251 for (CallbackVector::const_iterator callback_it = request.callbacks.begin();
263 callback_it != request.callbacks.end(); ++callback_it) { 252 callback_it != request.callbacks.end(); ++callback_it) {
264 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); 253 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it);
265 } 254 }
266 } 255 }
267 } 256 }
268 257
269 } // namespace suggestions 258 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698