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

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

Issue 641513003: [Suggestions] Introduce a different image encoder/decoder for iOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Latest Created 6 years, 2 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 "base/bind.h" 7 #include "base/bind.h"
8 #include "components/suggestions/image_fetcher.h" 8 #include "components/suggestions/image_fetcher.h"
9 #include "ui/gfx/codec/jpeg_codec.h" 9
10 #if defined(OS_IOS)
11 #include "components/suggestions/image_encoder_ios.h"
12 #else
13 #include "components/suggestions/image_encoder.h"
14 #endif
10 15
11 using leveldb_proto::ProtoDatabase; 16 using leveldb_proto::ProtoDatabase;
12 17
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 { 18 namespace suggestions {
23 19
24 ImageManager::ImageManager() : weak_ptr_factory_(this) {} 20 ImageManager::ImageManager() : weak_ptr_factory_(this) {}
25 21
26 ImageManager::ImageManager(scoped_ptr<ImageFetcher> image_fetcher, 22 ImageManager::ImageManager(scoped_ptr<ImageFetcher> image_fetcher,
27 scoped_ptr<ProtoDatabase<ImageData> > database, 23 scoped_ptr<ProtoDatabase<ImageData> > database,
28 const base::FilePath& database_dir) 24 const base::FilePath& database_dir)
29 : image_fetcher_(image_fetcher.Pass()), 25 : image_fetcher_(image_fetcher.Pass()),
30 database_(database.Pass()), 26 database_(database.Pass()),
31 database_ready_(false), 27 database_ready_(false),
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 128
133 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { 129 void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) {
134 // Update the image map. 130 // Update the image map.
135 image_map_.insert(std::make_pair(url.spec(), bitmap)); 131 image_map_.insert(std::make_pair(url.spec(), bitmap));
136 132
137 if (!database_ready_) return; 133 if (!database_ready_) return;
138 134
139 // Attempt to save a JPEG representation to the database. If not successful, 135 // Attempt to save a JPEG representation to the database. If not successful,
140 // the fetched bitmap will still be inserted in the cache, above. 136 // the fetched bitmap will still be inserted in the cache, above.
141 std::vector<unsigned char> encoded_data; 137 std::vector<unsigned char> encoded_data;
142 if (EncodeImage(bitmap, &encoded_data)) { 138 if (EncodeSkBitmapToJPEG(bitmap, &encoded_data)) {
143 // Save the resulting bitmap to the database. 139 // Save the resulting bitmap to the database.
144 ImageData data; 140 ImageData data;
145 data.set_url(url.spec()); 141 data.set_url(url.spec());
146 data.set_data(std::string(encoded_data.begin(), encoded_data.end())); 142 data.set_data(std::string(encoded_data.begin(), encoded_data.end()));
147 scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save( 143 scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save(
148 new ProtoDatabase<ImageData>::KeyEntryVector()); 144 new ProtoDatabase<ImageData>::KeyEntryVector());
149 scoped_ptr<std::vector<std::string> > keys_to_remove( 145 scoped_ptr<std::vector<std::string> > keys_to_remove(
150 new std::vector<std::string>()); 146 new std::vector<std::string>());
151 entries_to_save->push_back(std::make_pair(data.url(), data)); 147 entries_to_save->push_back(std::make_pair(data.url(), data));
152 database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), 148 database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 database_ready_ = false; 183 database_ready_ = false;
188 } 184 }
189 } 185 }
190 186
191 void ImageManager::LoadEntriesInCache(scoped_ptr<ImageDataVector> entries) { 187 void ImageManager::LoadEntriesInCache(scoped_ptr<ImageDataVector> entries) {
192 for (ImageDataVector::iterator it = entries->begin(); it != entries->end(); 188 for (ImageDataVector::iterator it = entries->begin(); it != entries->end();
193 ++it) { 189 ++it) {
194 std::vector<unsigned char> encoded_data(it->data().begin(), 190 std::vector<unsigned char> encoded_data(it->data().begin(),
195 it->data().end()); 191 it->data().end());
196 192
197 scoped_ptr<SkBitmap> bitmap(DecodeImage(encoded_data)); 193 scoped_ptr<SkBitmap> bitmap(DecodeJPEGToSkBitmap(encoded_data));
198 if (bitmap.get()) { 194 if (bitmap.get()) {
199 image_map_.insert(std::make_pair(it->url(), *bitmap)); 195 image_map_.insert(std::make_pair(it->url(), *bitmap));
200 } 196 }
201 } 197 }
202 } 198 }
203 199
204 void ImageManager::ServePendingCacheRequests() { 200 void ImageManager::ServePendingCacheRequests() {
205 for (ImageCacheRequestMap::iterator it = pending_cache_requests_.begin(); 201 for (ImageCacheRequestMap::iterator it = pending_cache_requests_.begin();
206 it != pending_cache_requests_.end(); ++it) { 202 it != pending_cache_requests_.end(); ++it) {
207 const ImageCacheRequest& request = it->second; 203 const ImageCacheRequest& request = it->second;
208 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); 204 for (CallbackVector::const_iterator callback_it = request.callbacks.begin();
209 callback_it != request.callbacks.end(); ++callback_it) { 205 callback_it != request.callbacks.end(); ++callback_it) {
210 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); 206 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it);
211 } 207 }
212 } 208 }
213 } 209 }
214 210
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 211 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698