Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 expecte | |
| 35 // gfx::Images. The ImageFetcher used to return NULL when the image could not be | |
| 36 // loaded. Now an empty image is returned which is mapped to NULL. | |
| 37 void WrapImageManagerCallback( | |
| 38 base::Callback<void(const GURL&, const SkBitmap*)> wrapped_callback, | |
|
noyau (Ping after 24h)
2016/05/13 16:10:09
Same here.
The rule of thumb is: If you are in co
markusheintz_
2016/05/17 13:08:23
Same as the other. This will be changed. Just in a
| |
| 39 const GURL& url, | |
| 40 const gfx::Image& image) { | |
| 41 const SkBitmap* bitmap = NULL; | |
| 42 if (!image.IsEmpty()) { | |
| 43 bitmap = image.ToSkBitmap(); | |
| 44 } | |
| 45 wrapped_callback.Run(url, bitmap); | |
| 46 } | |
| 47 | |
| 32 } // namespace | 48 } // namespace |
| 33 | 49 |
| 34 namespace suggestions { | 50 namespace suggestions { |
| 35 | 51 |
| 36 ImageManager::ImageManager() : weak_ptr_factory_(this) {} | 52 ImageManager::ImageManager() : weak_ptr_factory_(this) {} |
| 37 | 53 |
| 38 ImageManager::ImageManager( | 54 ImageManager::ImageManager( |
| 39 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 55 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 40 std::unique_ptr<ProtoDatabase<ImageData>> database, | 56 std::unique_ptr<ProtoDatabase<ImageData>> database, |
| 41 const base::FilePath& database_dir, | 57 const base::FilePath& database_dir, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 if (database_.get() && !database_ready_) { | 107 if (database_.get() && !database_ready_) { |
| 92 // Once database is initialized, it will serve pending requests from either | 108 // Once database is initialized, it will serve pending requests from either |
| 93 // cache or network. | 109 // cache or network. |
| 94 QueueCacheRequest(url, image_url, callback); | 110 QueueCacheRequest(url, image_url, callback); |
| 95 return; | 111 return; |
| 96 } | 112 } |
| 97 | 113 |
| 98 ServeFromCacheOrNetwork(url, image_url, callback); | 114 ServeFromCacheOrNetwork(url, image_url, callback); |
| 99 } | 115 } |
| 100 | 116 |
| 101 void ImageManager::OnImageFetched(const GURL& url, const SkBitmap* bitmap) { | 117 void ImageManager::OnImageFetched(const GURL& url, const gfx::Image& image) { |
| 102 if (bitmap) // |bitmap| can be nullptr if image fetch was unsuccessful. | 118 // |image| can be empty if image fetch was unsuccessful. |
| 103 SaveImage(url, *bitmap); | 119 if (!image.IsEmpty()) |
| 120 SaveImage(url, *(image.ToSkBitmap())); | |
| 104 } | 121 } |
| 105 | 122 |
| 106 bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) { | 123 bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) { |
| 107 DCHECK(image_url); | 124 DCHECK(image_url); |
| 108 std::map<GURL, GURL>::iterator it = image_url_map_.find(url); | 125 std::map<GURL, GURL>::iterator it = image_url_map_.find(url); |
| 109 if (it == image_url_map_.end()) return false; // Not found. | 126 if (it == image_url_map_.end()) return false; // Not found. |
| 110 *image_url = it->second; | 127 *image_url = it->second; |
| 111 return true; | 128 return true; |
| 112 } | 129 } |
| 113 | 130 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 129 } | 146 } |
| 130 | 147 |
| 131 void ImageManager::OnCacheImageDecoded( | 148 void ImageManager::OnCacheImageDecoded( |
| 132 const GURL& url, | 149 const GURL& url, |
| 133 const GURL& image_url, | 150 const GURL& image_url, |
| 134 base::Callback<void(const GURL&, const SkBitmap*)> callback, | 151 base::Callback<void(const GURL&, const SkBitmap*)> callback, |
| 135 std::unique_ptr<SkBitmap> bitmap) { | 152 std::unique_ptr<SkBitmap> bitmap) { |
| 136 if (bitmap.get()) { | 153 if (bitmap.get()) { |
| 137 callback.Run(url, bitmap.get()); | 154 callback.Run(url, bitmap.get()); |
| 138 } else { | 155 } else { |
| 139 image_fetcher_->StartOrQueueNetworkRequest(url, image_url, callback); | 156 image_fetcher_->StartOrQueueNetworkRequest( |
| 157 url, image_url, base::Bind(&WrapImageManagerCallback, callback)); | |
| 140 } | 158 } |
| 141 } | 159 } |
| 142 | 160 |
| 143 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache( | 161 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache( |
| 144 const GURL& url) { | 162 const GURL& url) { |
| 145 ImageMap::iterator image_iter = image_map_.find(url.spec()); | 163 ImageMap::iterator image_iter = image_map_.find(url.spec()); |
| 146 if (image_iter != image_map_.end()) { | 164 if (image_iter != image_map_.end()) { |
| 147 return image_iter->second; | 165 return image_iter->second; |
| 148 } | 166 } |
| 149 return nullptr; | 167 return nullptr; |
| 150 } | 168 } |
| 151 | 169 |
| 152 void ImageManager::ServeFromCacheOrNetwork( | 170 void ImageManager::ServeFromCacheOrNetwork( |
| 153 const GURL& url, | 171 const GURL& url, |
| 154 const GURL& image_url, | 172 const GURL& image_url, |
| 155 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | 173 base::Callback<void(const GURL&, const SkBitmap*)> callback) { |
| 156 scoped_refptr<base::RefCountedMemory> encoded_data = | 174 scoped_refptr<base::RefCountedMemory> encoded_data = |
| 157 GetEncodedImageFromCache(url); | 175 GetEncodedImageFromCache(url); |
| 158 if (encoded_data.get()) { | 176 if (encoded_data.get()) { |
| 159 base::PostTaskAndReplyWithResult( | 177 base::PostTaskAndReplyWithResult( |
| 160 background_task_runner_.get(), FROM_HERE, | 178 background_task_runner_.get(), FROM_HERE, |
| 161 base::Bind(&DecodeImage, encoded_data), | 179 base::Bind(&DecodeImage, encoded_data), |
| 162 base::Bind(&ImageManager::OnCacheImageDecoded, | 180 base::Bind(&ImageManager::OnCacheImageDecoded, |
| 163 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback)); | 181 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback)); |
| 164 } else { | 182 } else { |
| 165 image_fetcher_->StartOrQueueNetworkRequest(url, image_url, callback); | 183 image_fetcher_->StartOrQueueNetworkRequest( |
| 184 url, image_url, base::Bind(&WrapImageManagerCallback, callback)); | |
| 166 } | 185 } |
| 167 } | 186 } |
| 168 | 187 |
| 169 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { | 188 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { |
| 170 scoped_refptr<base::RefCountedBytes> encoded_data( | 189 scoped_refptr<base::RefCountedBytes> encoded_data( |
| 171 new base::RefCountedBytes()); | 190 new base::RefCountedBytes()); |
| 172 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) { | 191 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) { |
| 173 return; | 192 return; |
| 174 } | 193 } |
| 175 | 194 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 it != pending_cache_requests_.end(); ++it) { | 262 it != pending_cache_requests_.end(); ++it) { |
| 244 const ImageCacheRequest& request = it->second; | 263 const ImageCacheRequest& request = it->second; |
| 245 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); | 264 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); |
| 246 callback_it != request.callbacks.end(); ++callback_it) { | 265 callback_it != request.callbacks.end(); ++callback_it) { |
| 247 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); | 266 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); |
| 248 } | 267 } |
| 249 } | 268 } |
| 250 } | 269 } |
| 251 | 270 |
| 252 } // namespace suggestions | 271 } // namespace suggestions |
| OLD | NEW |