| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "components/suggestions/image_encoder.h" | |
| 9 #include "components/suggestions/image_fetcher.h" | 8 #include "components/suggestions/image_fetcher.h" |
| 9 #include "ui/gfx/codec/jpeg_codec.h" |
| 10 | 10 |
| 11 using leveldb_proto::ProtoDatabase; | 11 using leveldb_proto::ProtoDatabase; |
| 12 | 12 |
| 13 namespace { |
| 14 |
| 15 // From JPEG-encoded bytes to SkBitmap. |
| 16 SkBitmap* DecodeImage(const std::vector<unsigned char>& encoded_data) { |
| 17 return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); |
| 18 } |
| 19 |
| 20 } // namespace |
| 21 |
| 13 namespace suggestions { | 22 namespace suggestions { |
| 14 | 23 |
| 15 ImageManager::ImageManager() : weak_ptr_factory_(this) {} | 24 ImageManager::ImageManager() : weak_ptr_factory_(this) {} |
| 16 | 25 |
| 17 ImageManager::ImageManager(scoped_ptr<ImageFetcher> image_fetcher, | 26 ImageManager::ImageManager(scoped_ptr<ImageFetcher> image_fetcher, |
| 18 scoped_ptr<ImageEncoder> image_encoder, | |
| 19 scoped_ptr<ProtoDatabase<ImageData> > database, | 27 scoped_ptr<ProtoDatabase<ImageData> > database, |
| 20 const base::FilePath& database_dir) | 28 const base::FilePath& database_dir) |
| 21 : image_fetcher_(image_fetcher.Pass()), | 29 : image_fetcher_(image_fetcher.Pass()), |
| 22 image_encoder_(image_encoder.Pass()), | |
| 23 database_(database.Pass()), | 30 database_(database.Pass()), |
| 24 database_ready_(false), | 31 database_ready_(false), |
| 25 weak_ptr_factory_(this) { | 32 weak_ptr_factory_(this) { |
| 26 image_fetcher_->SetImageFetcherDelegate(this); | 33 image_fetcher_->SetImageFetcherDelegate(this); |
| 27 database_->Init(database_dir, base::Bind(&ImageManager::OnDatabaseInit, | 34 database_->Init(database_dir, base::Bind(&ImageManager::OnDatabaseInit, |
| 28 weak_ptr_factory_.GetWeakPtr())); | 35 weak_ptr_factory_.GetWeakPtr())); |
| 29 } | 36 } |
| 30 | 37 |
| 31 ImageManager::~ImageManager() {} | 38 ImageManager::~ImageManager() {} |
| 32 | 39 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 132 |
| 126 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { | 133 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { |
| 127 // Update the image map. | 134 // Update the image map. |
| 128 image_map_.insert(std::make_pair(url.spec(), bitmap)); | 135 image_map_.insert(std::make_pair(url.spec(), bitmap)); |
| 129 | 136 |
| 130 if (!database_ready_) return; | 137 if (!database_ready_) return; |
| 131 | 138 |
| 132 // Attempt to save a JPEG representation to the database. If not successful, | 139 // Attempt to save a JPEG representation to the database. If not successful, |
| 133 // the fetched bitmap will still be inserted in the cache, above. | 140 // the fetched bitmap will still be inserted in the cache, above. |
| 134 std::vector<unsigned char> encoded_data; | 141 std::vector<unsigned char> encoded_data; |
| 135 if (image_encoder_->EncodeImage(bitmap, &encoded_data)) { | 142 if (EncodeImage(bitmap, &encoded_data)) { |
| 136 // Save the resulting bitmap to the database. | 143 // Save the resulting bitmap to the database. |
| 137 ImageData data; | 144 ImageData data; |
| 138 data.set_url(url.spec()); | 145 data.set_url(url.spec()); |
| 139 data.set_data(std::string(encoded_data.begin(), encoded_data.end())); | 146 data.set_data(std::string(encoded_data.begin(), encoded_data.end())); |
| 140 scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save( | 147 scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save( |
| 141 new ProtoDatabase<ImageData>::KeyEntryVector()); | 148 new ProtoDatabase<ImageData>::KeyEntryVector()); |
| 142 scoped_ptr<std::vector<std::string> > keys_to_remove( | 149 scoped_ptr<std::vector<std::string> > keys_to_remove( |
| 143 new std::vector<std::string>()); | 150 new std::vector<std::string>()); |
| 144 entries_to_save->push_back(std::make_pair(data.url(), data)); | 151 entries_to_save->push_back(std::make_pair(data.url(), data)); |
| 145 database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), | 152 database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 database_ready_ = false; | 187 database_ready_ = false; |
| 181 } | 188 } |
| 182 } | 189 } |
| 183 | 190 |
| 184 void ImageManager::LoadEntriesInCache(scoped_ptr<ImageDataVector> entries) { | 191 void ImageManager::LoadEntriesInCache(scoped_ptr<ImageDataVector> entries) { |
| 185 for (ImageDataVector::iterator it = entries->begin(); it != entries->end(); | 192 for (ImageDataVector::iterator it = entries->begin(); it != entries->end(); |
| 186 ++it) { | 193 ++it) { |
| 187 std::vector<unsigned char> encoded_data(it->data().begin(), | 194 std::vector<unsigned char> encoded_data(it->data().begin(), |
| 188 it->data().end()); | 195 it->data().end()); |
| 189 | 196 |
| 190 scoped_ptr<SkBitmap> bitmap(image_encoder_->DecodeImage(encoded_data)); | 197 scoped_ptr<SkBitmap> bitmap(DecodeImage(encoded_data)); |
| 191 if (bitmap.get()) { | 198 if (bitmap.get()) { |
| 192 image_map_.insert(std::make_pair(it->url(), *bitmap)); | 199 image_map_.insert(std::make_pair(it->url(), *bitmap)); |
| 193 } | 200 } |
| 194 } | 201 } |
| 195 } | 202 } |
| 196 | 203 |
| 197 void ImageManager::ServePendingCacheRequests() { | 204 void ImageManager::ServePendingCacheRequests() { |
| 198 for (ImageCacheRequestMap::iterator it = pending_cache_requests_.begin(); | 205 for (ImageCacheRequestMap::iterator it = pending_cache_requests_.begin(); |
| 199 it != pending_cache_requests_.end(); ++it) { | 206 it != pending_cache_requests_.end(); ++it) { |
| 200 const ImageCacheRequest& request = it->second; | 207 const ImageCacheRequest& request = it->second; |
| 201 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); | 208 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); |
| 202 callback_it != request.callbacks.end(); ++callback_it) { | 209 callback_it != request.callbacks.end(); ++callback_it) { |
| 203 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); | 210 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); |
| 204 } | 211 } |
| 205 } | 212 } |
| 206 } | 213 } |
| 207 | 214 |
| 215 // static |
| 216 bool ImageManager::EncodeImage(const SkBitmap& bitmap, |
| 217 std::vector<unsigned char>* dest) { |
| 218 SkAutoLockPixels bitmap_lock(bitmap); |
| 219 if (!bitmap.readyToDraw() || bitmap.isNull()) { |
| 220 return false; |
| 221 } |
| 222 return gfx::JPEGCodec::Encode( |
| 223 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 224 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), |
| 225 bitmap.rowBytes(), 100, dest); |
| 226 } |
| 227 |
| 208 } // namespace suggestions | 228 } // namespace suggestions |
| OLD | NEW |