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/core/image_fetcher.h" | 13 #include "components/image_fetcher/core/image_fetcher.h" |
| 14 #include "components/suggestions/image_encoder.h" | 14 #include "components/suggestions/image_encoder.h" |
| 15 #include "net/traffic_annotation/network_traffic_annotation.h" | |
| 15 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
| 16 | 17 |
| 17 using leveldb_proto::ProtoDatabase; | 18 using leveldb_proto::ProtoDatabase; |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // Statistics are logged to UMA with this string as part of histogram name. They | 22 // Statistics are logged to UMA with this string as part of histogram name. They |
| 22 // can all be found under LevelDB.*.ImageManager. Changing this needs to | 23 // can all be found under LevelDB.*.ImageManager. Changing this needs to |
| 23 // synchronize with histograms.xml, AND will also become incompatible with older | 24 // synchronize with histograms.xml, AND will also become incompatible with older |
| 24 // browsers still reporting the previous values. | 25 // browsers still reporting the previous values. |
| 25 const char kDatabaseUMAClientName[] = "ImageManager"; | 26 const char kDatabaseUMAClientName[] = "ImageManager"; |
| 26 | 27 |
| 27 std::unique_ptr<SkBitmap> DecodeImage( | 28 std::unique_ptr<SkBitmap> DecodeImage( |
| 28 scoped_refptr<base::RefCountedMemory> encoded_data) { | 29 scoped_refptr<base::RefCountedMemory> encoded_data) { |
| 29 return suggestions::DecodeJPEGToSkBitmap(encoded_data->front(), | 30 return suggestions::DecodeJPEGToSkBitmap(encoded_data->front(), |
| 30 encoded_data->size()); | 31 encoded_data->size()); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // Wraps an ImageManager callback so that it can be used with the ImageFetcher. | 34 // Wraps an ImageManager callback so that it can be used with the ImageFetcher. |
| 34 void WrapCallback( | 35 void WrapCallback( |
| 35 const suggestions::ImageManager::ImageCallback& wrapped_callback, | 36 const suggestions::ImageManager::ImageCallback& wrapped_callback, |
| 36 const std::string& url, | 37 const std::string& url, |
| 37 const gfx::Image& image, | 38 const gfx::Image& image, |
| 38 const image_fetcher::RequestMetadata& metadata) { | 39 const image_fetcher::RequestMetadata& metadata) { |
| 39 wrapped_callback.Run(GURL(url), image); | 40 wrapped_callback.Run(GURL(url), image); |
| 40 } | 41 } |
| 41 | 42 |
| 43 constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation = | |
| 44 net::DefineNetworkTrafficAnnotation("suggestions_image_manager", R"( | |
| 45 semantics { | |
| 46 sender: "Suggestions Service Thumbnail Fetch" | |
| 47 description: | |
| 48 "Retrieves thumbnails for site suggestions based on the user's " | |
| 49 "synced browsing history, for use e.g. on the New Tab page." | |
| 50 trigger: | |
| 51 "Triggered when a thumbnail for a suggestion is required, and no " | |
| 52 "local thumbnail is available." | |
| 53 data: "The URL for which to retrieve a thumbnail." | |
| 54 destination: GOOGLE_OWNED_SERVICE | |
| 55 } | |
| 56 policy { | |
| 57 cookies_allowed: false | |
| 58 setting: | |
| 59 "Users can disable this feature by signing out of Chrome, or " | |
|
msramek
2017/05/19 08:47:38
nit: Offset.
Ramin Halavati
2017/05/19 09:03:37
Done.
| |
| 60 "disabling Sync or History Sync in Chrome settings under 'Advanced " | |
| 61 "sync settings...'. The feature is enabled by default." | |
| 62 chrome_policy { | |
| 63 SyncDisabled { | |
| 64 policy_options {mode: MANDATORY} | |
| 65 SyncDisabled: true | |
| 66 } | |
| 67 } | |
| 68 chrome_policy { | |
| 69 SigninAllowed { | |
| 70 policy_options {mode: MANDATORY} | |
| 71 SigninAllowed: false | |
| 72 } | |
| 73 } | |
| 74 })"); | |
| 75 | |
| 42 } // namespace | 76 } // namespace |
| 43 | 77 |
| 44 namespace suggestions { | 78 namespace suggestions { |
| 45 | 79 |
| 46 ImageManager::ImageManager() : weak_ptr_factory_(this) {} | 80 ImageManager::ImageManager() : weak_ptr_factory_(this) {} |
| 47 | 81 |
| 48 ImageManager::ImageManager( | 82 ImageManager::ImageManager( |
| 49 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 83 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 50 std::unique_ptr<ProtoDatabase<ImageData>> database, | 84 std::unique_ptr<ProtoDatabase<ImageData>> database, |
| 51 const base::FilePath& database_dir, | 85 const base::FilePath& database_dir, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 173 |
| 140 void ImageManager::OnCacheImageDecoded( | 174 void ImageManager::OnCacheImageDecoded( |
| 141 const GURL& url, | 175 const GURL& url, |
| 142 const GURL& image_url, | 176 const GURL& image_url, |
| 143 const ImageCallback& callback, | 177 const ImageCallback& callback, |
| 144 std::unique_ptr<SkBitmap> bitmap) { | 178 std::unique_ptr<SkBitmap> bitmap) { |
| 145 if (bitmap.get()) { | 179 if (bitmap.get()) { |
| 146 callback.Run(url, gfx::Image::CreateFrom1xBitmap(*bitmap)); | 180 callback.Run(url, gfx::Image::CreateFrom1xBitmap(*bitmap)); |
| 147 } else { | 181 } else { |
| 148 image_fetcher_->StartOrQueueNetworkRequest( | 182 image_fetcher_->StartOrQueueNetworkRequest( |
| 149 url.spec(), image_url, base::Bind(&WrapCallback, callback)); | 183 url.spec(), image_url, base::Bind(&WrapCallback, callback), |
| 184 kTrafficAnnotation); | |
| 150 } | 185 } |
| 151 } | 186 } |
| 152 | 187 |
| 153 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache( | 188 scoped_refptr<base::RefCountedMemory> ImageManager::GetEncodedImageFromCache( |
| 154 const GURL& url) { | 189 const GURL& url) { |
| 155 ImageMap::iterator image_iter = image_map_.find(url.spec()); | 190 ImageMap::iterator image_iter = image_map_.find(url.spec()); |
| 156 if (image_iter != image_map_.end()) { | 191 if (image_iter != image_map_.end()) { |
| 157 return image_iter->second; | 192 return image_iter->second; |
| 158 } | 193 } |
| 159 return nullptr; | 194 return nullptr; |
| 160 } | 195 } |
| 161 | 196 |
| 162 void ImageManager::ServeFromCacheOrNetwork( | 197 void ImageManager::ServeFromCacheOrNetwork( |
| 163 const GURL& url, | 198 const GURL& url, |
| 164 const GURL& image_url, | 199 const GURL& image_url, |
| 165 ImageCallback callback) { | 200 ImageCallback callback) { |
| 166 scoped_refptr<base::RefCountedMemory> encoded_data = | 201 scoped_refptr<base::RefCountedMemory> encoded_data = |
| 167 GetEncodedImageFromCache(url); | 202 GetEncodedImageFromCache(url); |
| 168 if (encoded_data.get()) { | 203 if (encoded_data.get()) { |
| 169 base::PostTaskAndReplyWithResult( | 204 base::PostTaskAndReplyWithResult( |
| 170 background_task_runner_.get(), FROM_HERE, | 205 background_task_runner_.get(), FROM_HERE, |
| 171 base::Bind(&DecodeImage, encoded_data), | 206 base::Bind(&DecodeImage, encoded_data), |
| 172 base::Bind(&ImageManager::OnCacheImageDecoded, | 207 base::Bind(&ImageManager::OnCacheImageDecoded, |
| 173 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback)); | 208 weak_ptr_factory_.GetWeakPtr(), url, image_url, callback)); |
| 174 } else { | 209 } else { |
| 175 image_fetcher_->StartOrQueueNetworkRequest( | 210 image_fetcher_->StartOrQueueNetworkRequest( |
| 176 url.spec(), image_url, base::Bind(&WrapCallback, callback)); | 211 url.spec(), image_url, base::Bind(&WrapCallback, callback), |
| 212 kTrafficAnnotation); | |
| 177 } | 213 } |
| 178 } | 214 } |
| 179 | 215 |
| 180 void ImageManager::SaveImage(const std::string& url, const SkBitmap& bitmap) { | 216 void ImageManager::SaveImage(const std::string& url, const SkBitmap& bitmap) { |
| 181 scoped_refptr<base::RefCountedBytes> encoded_data( | 217 scoped_refptr<base::RefCountedBytes> encoded_data( |
| 182 new base::RefCountedBytes()); | 218 new base::RefCountedBytes()); |
| 183 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) { | 219 if (!EncodeSkBitmapToJPEG(bitmap, &encoded_data->data())) { |
| 184 return; | 220 return; |
| 185 } | 221 } |
| 186 | 222 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 it != pending_cache_requests_.end(); ++it) { | 290 it != pending_cache_requests_.end(); ++it) { |
| 255 const ImageCacheRequest& request = it->second; | 291 const ImageCacheRequest& request = it->second; |
| 256 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); | 292 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); |
| 257 callback_it != request.callbacks.end(); ++callback_it) { | 293 callback_it != request.callbacks.end(); ++callback_it) { |
| 258 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); | 294 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); |
| 259 } | 295 } |
| 260 } | 296 } |
| 261 } | 297 } |
| 262 | 298 |
| 263 } // namespace suggestions | 299 } // namespace suggestions |
| OLD | NEW |