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