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

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

Issue 1974013002: Replace SkBitmap with gfx::Image in the ImageFetcher API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync again 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"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/task_runner_util.h" 12 #include "base/task_runner_util.h"
13 #include "components/image_fetcher/image_fetcher.h" 13 #include "components/image_fetcher/image_fetcher.h"
14 #include "components/suggestions/image_encoder.h" 14 #include "components/suggestions/image_encoder.h"
15 #include "ui/gfx/image/image.h"
15 16
16 using leveldb_proto::ProtoDatabase; 17 using leveldb_proto::ProtoDatabase;
17 18
18 namespace { 19 namespace {
19 20
20 // Statistics are logged to UMA with this string as part of histogram name. They 21 // Statistics are logged to UMA with this string as part of histogram name. They
21 // can all be found under LevelDB.*.ImageManager. Changing this needs to 22 // can all be found under LevelDB.*.ImageManager. Changing this needs to
22 // synchronize with histograms.xml, AND will also become incompatible with older 23 // synchronize with histograms.xml, AND will also become incompatible with older
23 // browsers still reporting the previous values. 24 // browsers still reporting the previous values.
24 const char kDatabaseUMAClientName[] = "ImageManager"; 25 const char kDatabaseUMAClientName[] = "ImageManager";
25 26
26 std::unique_ptr<SkBitmap> DecodeImage( 27 std::unique_ptr<SkBitmap> DecodeImage(
27 scoped_refptr<base::RefCountedMemory> encoded_data) { 28 scoped_refptr<base::RefCountedMemory> encoded_data) {
28 return suggestions::DecodeJPEGToSkBitmap(encoded_data->front(), 29 return suggestions::DecodeJPEGToSkBitmap(encoded_data->front(),
29 encoded_data->size()); 30 encoded_data->size());
30 } 31 }
31 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
32 } // namespace 46 } // namespace
33 47
34 namespace suggestions { 48 namespace suggestions {
35 49
36 ImageManager::ImageManager() : weak_ptr_factory_(this) {} 50 ImageManager::ImageManager() : weak_ptr_factory_(this) {}
37 51
38 ImageManager::ImageManager( 52 ImageManager::ImageManager(
39 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, 53 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
40 std::unique_ptr<ProtoDatabase<ImageData>> database, 54 std::unique_ptr<ProtoDatabase<ImageData>> database,
41 const base::FilePath& database_dir, 55 const base::FilePath& database_dir,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 } 90 }
77 91
78 void ImageManager::GetImageForURL( 92 void ImageManager::GetImageForURL(
79 const GURL& url, 93 const GURL& url,
80 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 94 base::Callback<void(const GURL&, const SkBitmap*)> callback) {
81 DCHECK(thread_checker_.CalledOnValidThread()); 95 DCHECK(thread_checker_.CalledOnValidThread());
82 // If |url| is not found in |image_url_map_|, then invoke |callback| with 96 // If |url| is not found in |image_url_map_|, then invoke |callback| with
83 // NULL since there is no associated image for this |url|. 97 // NULL since there is no associated image for this |url|.
84 GURL image_url; 98 GURL image_url;
85 if (!GetImageURL(url, &image_url)) { 99 if (!GetImageURL(url, &image_url)) {
86 callback.Run(url, NULL); 100 callback.Run(url, nullptr);
87 return; 101 return;
88 } 102 }
89 103
90 // |database_| can be NULL if something went wrong in initialization. 104 // |database_| can be NULL if something went wrong in initialization.
91 if (database_.get() && !database_ready_) { 105 if (database_.get() && !database_ready_) {
92 // Once database is initialized, it will serve pending requests from either 106 // Once database is initialized, it will serve pending requests from either
93 // cache or network. 107 // cache or network.
94 QueueCacheRequest(url, image_url, callback); 108 QueueCacheRequest(url, image_url, callback);
95 return; 109 return;
96 } 110 }
97 111
98 ServeFromCacheOrNetwork(url, image_url, callback); 112 ServeFromCacheOrNetwork(url, image_url, callback);
99 } 113 }
100 114
101 void ImageManager::OnImageFetched(const GURL& url, const SkBitmap* bitmap) { 115 void ImageManager::OnImageFetched(const GURL& url, const gfx::Image& image) {
102 if (bitmap) // |bitmap| can be nullptr if image fetch was unsuccessful. 116 // |image| can be empty if image fetch was unsuccessful.
103 SaveImage(url, *bitmap); 117 if (!image.IsEmpty())
118 SaveImage(url, *image.ToSkBitmap());
104 } 119 }
105 120
106 bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) { 121 bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) {
107 DCHECK(image_url); 122 DCHECK(image_url);
108 std::map<GURL, GURL>::iterator it = image_url_map_.find(url); 123 std::map<GURL, GURL>::iterator it = image_url_map_.find(url);
109 if (it == image_url_map_.end()) return false; // Not found. 124 if (it == image_url_map_.end()) return false; // Not found.
110 *image_url = it->second; 125 *image_url = it->second;
111 return true; 126 return true;
112 } 127 }
113 128
(...skipping 15 matching lines...) Expand all
129 } 144 }
130 145
131 void ImageManager::OnCacheImageDecoded( 146 void ImageManager::OnCacheImageDecoded(
132 const GURL& url, 147 const GURL& url,
133 const GURL& image_url, 148 const GURL& image_url,
134 base::Callback<void(const GURL&, const SkBitmap*)> callback, 149 base::Callback<void(const GURL&, const SkBitmap*)> callback,
135 std::unique_ptr<SkBitmap> bitmap) { 150 std::unique_ptr<SkBitmap> bitmap) {
136 if (bitmap.get()) { 151 if (bitmap.get()) {
137 callback.Run(url, bitmap.get()); 152 callback.Run(url, bitmap.get());
138 } else { 153 } else {
139 image_fetcher_->StartOrQueueNetworkRequest(url, image_url, callback); 154 image_fetcher_->StartOrQueueNetworkRequest(
155 url, image_url, base::Bind(&WrapImageManagerCallback, callback));
140 } 156 }
141 } 157 }
142 158
143 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache( 159 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache(
144 const GURL& url) { 160 const GURL& url) {
145 ImageMap::iterator image_iter = image_map_.find(url.spec()); 161 ImageMap::iterator image_iter = image_map_.find(url.spec());
146 if (image_iter != image_map_.end()) { 162 if (image_iter != image_map_.end()) {
147 return image_iter->second; 163 return image_iter->second;
148 } 164 }
149 return nullptr; 165 return nullptr;
150 } 166 }
151 167
152 void ImageManager::ServeFromCacheOrNetwork( 168 void ImageManager::ServeFromCacheOrNetwork(
153 const GURL& url, 169 const GURL& url,
154 const GURL& image_url, 170 const GURL& image_url,
155 base::Callback<void(const GURL&, const SkBitmap*)> callback) { 171 base::Callback<void(const GURL&, const SkBitmap*)> callback) {
156 scoped_refptr<base::RefCountedMemory> encoded_data = 172 scoped_refptr<base::RefCountedMemory> encoded_data =
157 GetEncodedImageFromCache(url); 173 GetEncodedImageFromCache(url);
158 if (encoded_data.get()) { 174 if (encoded_data.get()) {
159 base::PostTaskAndReplyWithResult( 175 base::PostTaskAndReplyWithResult(
160 background_task_runner_.get(), FROM_HERE, 176 background_task_runner_.get(), FROM_HERE,
161 base::Bind(&DecodeImage, encoded_data), 177 base::Bind(&DecodeImage, encoded_data),
162 base::Bind(&ImageManager::OnCacheImageDecoded, 178 base::Bind(&ImageManager::OnCacheImageDecoded,
163 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback)); 179 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback));
164 } else { 180 } else {
165 image_fetcher_->StartOrQueueNetworkRequest(url, image_url, callback); 181 image_fetcher_->StartOrQueueNetworkRequest(
182 url, image_url, base::Bind(&WrapImageManagerCallback, callback));
166 } 183 }
167 } 184 }
168 185
169 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { 186 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) {
170 scoped_refptr<base::RefCountedBytes> encoded_data( 187 scoped_refptr<base::RefCountedBytes> encoded_data(
171 new base::RefCountedBytes()); 188 new base::RefCountedBytes());
172 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) { 189 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) {
173 return; 190 return;
174 } 191 }
175 192
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 it != pending_cache_requests_.end(); ++it) { 260 it != pending_cache_requests_.end(); ++it) {
244 const ImageCacheRequest& request = it->second; 261 const ImageCacheRequest& request = it->second;
245 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); 262 for (CallbackVector::const_iterator callback_it = request.callbacks.begin();
246 callback_it != request.callbacks.end(); ++callback_it) { 263 callback_it != request.callbacks.end(); ++callback_it) {
247 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); 264 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it);
248 } 265 }
249 } 266 }
250 } 267 }
251 268
252 } // namespace suggestions 269 } // namespace suggestions
OLDNEW
« no previous file with comments | « components/suggestions/image_manager.h ('k') | components/suggestions/image_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698