| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/search/suggestions/thumbnail_manager.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted_memory.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 #include "net/base/load_flags.h" | |
| 10 #include "net/url_request/url_request_context_getter.h" | |
| 11 #include "ui/gfx/codec/jpeg_codec.h" | |
| 12 | |
| 13 using leveldb_proto::ProtoDatabase; | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // From JPEG-encoded bytes to SkBitmap. | |
| 18 SkBitmap* DecodeThumbnail(const std::vector<unsigned char>& encoded_data) { | |
| 19 return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); | |
| 20 } | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 namespace suggestions { | |
| 25 | |
| 26 ThumbnailManager::ThumbnailManager() : weak_ptr_factory_(this) {} | |
| 27 | |
| 28 ThumbnailManager::ThumbnailManager( | |
| 29 net::URLRequestContextGetter* url_request_context, | |
| 30 scoped_ptr<ProtoDatabase<ThumbnailData> > database, | |
| 31 const base::FilePath& database_dir) | |
| 32 : url_request_context_(url_request_context), | |
| 33 database_(database.Pass()), | |
| 34 database_ready_(false), | |
| 35 weak_ptr_factory_(this) { | |
| 36 database_->Init(database_dir, base::Bind(&ThumbnailManager::OnDatabaseInit, | |
| 37 weak_ptr_factory_.GetWeakPtr())); | |
| 38 } | |
| 39 | |
| 40 ThumbnailManager::~ThumbnailManager() {} | |
| 41 | |
| 42 ThumbnailManager::ThumbnailRequest::ThumbnailRequest() : fetcher(NULL) {} | |
| 43 | |
| 44 ThumbnailManager::ThumbnailRequest::ThumbnailRequest(chrome::BitmapFetcher* f) | |
| 45 : fetcher(f) {} | |
| 46 | |
| 47 ThumbnailManager::ThumbnailRequest::~ThumbnailRequest() { delete fetcher; } | |
| 48 | |
| 49 void ThumbnailManager::Initialize(const SuggestionsProfile& suggestions) { | |
| 50 thumbnail_url_map_.clear(); | |
| 51 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | |
| 52 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | |
| 53 if (suggestion.has_thumbnail()) { | |
| 54 thumbnail_url_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); | |
| 55 } | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void ThumbnailManager::GetImageForURL( | |
| 60 const GURL& url, | |
| 61 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | |
| 62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 63 // If |url| is not found in |thumbnail_url_map_|, then invoke |callback| with | |
| 64 // NULL since there is no associated thumbnail for this |url|. | |
| 65 GURL thumbnail_url; | |
| 66 if (!GetThumbnailURL(url, &thumbnail_url)) { | |
| 67 callback.Run(url, NULL); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 // |database_| can be NULL if something went wrong in initialization. | |
| 72 if (database_.get() && !database_ready_) { | |
| 73 // Once database is initialized, it will serve pending requests from either | |
| 74 // cache or network. | |
| 75 QueueCacheRequest(url, thumbnail_url, callback); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 ServeFromCacheOrNetwork(url, thumbnail_url, callback); | |
| 80 } | |
| 81 | |
| 82 bool ThumbnailManager::GetThumbnailURL(const GURL& url, GURL* thumbnail_url) { | |
| 83 std::map<GURL, GURL>::iterator it = thumbnail_url_map_.find(url); | |
| 84 if (it == thumbnail_url_map_.end()) return false; // Not found. | |
| 85 *thumbnail_url = it->second; | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 void ThumbnailManager::QueueCacheRequest( | |
| 90 const GURL& url, const GURL& thumbnail_url, | |
| 91 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | |
| 92 // To be served when the database has loaded. | |
| 93 ThumbnailRequestMap::iterator it = pending_cache_requests_.find(url); | |
| 94 if (it == pending_cache_requests_.end()) { | |
| 95 ThumbnailRequest request(NULL); | |
| 96 request.url = url; | |
| 97 request.thumbnail_url = thumbnail_url; | |
| 98 request.callbacks.push_back(callback); | |
| 99 pending_cache_requests_[url].swap(&request); | |
| 100 } else { | |
| 101 // Request already queued for this url. | |
| 102 it->second.callbacks.push_back(callback); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void ThumbnailManager::ServeFromCacheOrNetwork( | |
| 107 const GURL& url, const GURL& thumbnail_url, | |
| 108 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | |
| 109 // If there is a thumbnail available in memory, return it. | |
| 110 if (!ServeFromCache(url, callback)) { | |
| 111 StartOrQueueNetworkRequest(url, thumbnail_url, callback); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 bool ThumbnailManager::ServeFromCache( | |
| 116 const GURL& url, | |
| 117 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | |
| 118 SkBitmap* bitmap = GetBitmapFromCache(url); | |
| 119 if (bitmap) { | |
| 120 callback.Run(url, bitmap); | |
| 121 return true; | |
| 122 } | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 SkBitmap* ThumbnailManager::GetBitmapFromCache(const GURL& url) { | |
| 127 ThumbnailMap::iterator thumb_iter = thumbnail_map_.find(url.spec()); | |
| 128 if (thumb_iter != thumbnail_map_.end()) { | |
| 129 return &thumb_iter->second; | |
| 130 } | |
| 131 return NULL; | |
| 132 } | |
| 133 | |
| 134 void ThumbnailManager::StartOrQueueNetworkRequest( | |
| 135 const GURL& url, const GURL& thumbnail_url, | |
| 136 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | |
| 137 // Before starting to fetch the thumbnail. Look for a request in progress for | |
| 138 // |thumbnail_url|, and queue if appropriate. | |
| 139 ThumbnailRequestMap::iterator it = pending_net_requests_.find(thumbnail_url); | |
| 140 if (it == pending_net_requests_.end()) { | |
| 141 // |thumbnail_url| is not being fetched, so create a request and initiate | |
| 142 // the fetch. | |
| 143 ThumbnailRequest request(new chrome::BitmapFetcher(thumbnail_url, this)); | |
| 144 request.url = url; | |
| 145 request.callbacks.push_back(callback); | |
| 146 request.fetcher->Start( | |
| 147 url_request_context_, std::string(), | |
| 148 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | |
| 149 net::LOAD_NORMAL); | |
| 150 pending_net_requests_[thumbnail_url].swap(&request); | |
| 151 } else { | |
| 152 // Request in progress. Register as an interested callback. | |
| 153 it->second.callbacks.push_back(callback); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void ThumbnailManager::OnFetchComplete(const GURL thumbnail_url, | |
| 158 const SkBitmap* bitmap) { | |
| 159 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 160 | |
| 161 ThumbnailRequestMap::iterator thumb_iter = | |
| 162 pending_net_requests_.find(thumbnail_url); | |
| 163 DCHECK(thumb_iter != pending_net_requests_.end()); | |
| 164 | |
| 165 ThumbnailRequest* request = &thumb_iter->second; | |
| 166 | |
| 167 // Here |bitmap| could be NULL or a pointer to a bitmap which is owned by the | |
| 168 // BitmapFetcher and which ceases to exist after this function. Pass the | |
| 169 // un-owned pointer to the registered callbacks. | |
| 170 for (CallbackVector::iterator callback_iter = request->callbacks.begin(); | |
| 171 callback_iter != request->callbacks.end(); ++callback_iter) { | |
| 172 callback_iter->Run(request->url, bitmap); | |
| 173 } | |
| 174 | |
| 175 // Save the bitmap to the in-memory model as well as the database, because it | |
| 176 // is now the freshest representation of the thumbnail. | |
| 177 // TODO(mathp): Handle null (no thumbnail found), possible deletion in DB. | |
| 178 if (bitmap) SaveThumbnail(request->url, *bitmap); | |
| 179 | |
| 180 // Erase the completed ThumbnailRequest. | |
| 181 pending_net_requests_.erase(thumb_iter); | |
| 182 } | |
| 183 | |
| 184 void ThumbnailManager::SaveThumbnail(const GURL& url, const SkBitmap& bitmap) { | |
| 185 // Update the thumbnail map. | |
| 186 thumbnail_map_.insert(std::make_pair(url.spec(), bitmap)); | |
| 187 | |
| 188 if (!database_ready_) return; | |
| 189 | |
| 190 // Attempt to save a JPEG representation to the database. If not successful, | |
| 191 // the fetched bitmap will still be inserted in the cache, above. | |
| 192 std::vector<unsigned char> encoded_data; | |
| 193 if (EncodeThumbnail(bitmap, &encoded_data)) { | |
| 194 // Save the resulting bitmap to the database. | |
| 195 ThumbnailData data; | |
| 196 data.set_url(url.spec()); | |
| 197 data.set_data(std::string(encoded_data.begin(), encoded_data.end())); | |
| 198 scoped_ptr<ProtoDatabase<ThumbnailData>::KeyEntryVector> entries_to_save( | |
| 199 new ProtoDatabase<ThumbnailData>::KeyEntryVector()); | |
| 200 scoped_ptr<std::vector<std::string> > keys_to_remove( | |
| 201 new std::vector<std::string>()); | |
| 202 entries_to_save->push_back(std::make_pair(data.url(), data)); | |
| 203 database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), | |
| 204 base::Bind(&ThumbnailManager::OnDatabaseSave, | |
| 205 weak_ptr_factory_.GetWeakPtr())); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void ThumbnailManager::OnDatabaseInit(bool success) { | |
| 210 if (!success) { | |
| 211 DVLOG(1) << "Thumbnail database init failed."; | |
| 212 database_.reset(); | |
| 213 ServePendingCacheRequests(); | |
| 214 return; | |
| 215 } | |
| 216 database_->LoadEntries(base::Bind(&ThumbnailManager::OnDatabaseLoad, | |
| 217 weak_ptr_factory_.GetWeakPtr())); | |
| 218 } | |
| 219 | |
| 220 void ThumbnailManager::OnDatabaseLoad(bool success, | |
| 221 scoped_ptr<ThumbnailVector> entries) { | |
| 222 if (!success) { | |
| 223 DVLOG(1) << "Thumbnail database load failed."; | |
| 224 database_.reset(); | |
| 225 ServePendingCacheRequests(); | |
| 226 return; | |
| 227 } | |
| 228 database_ready_ = true; | |
| 229 | |
| 230 LoadEntriesInCache(entries.Pass()); | |
| 231 ServePendingCacheRequests(); | |
| 232 } | |
| 233 | |
| 234 void ThumbnailManager::OnDatabaseSave(bool success) { | |
| 235 if (!success) { | |
| 236 DVLOG(1) << "Thumbnail database save failed."; | |
| 237 database_.reset(); | |
| 238 database_ready_ = false; | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 void ThumbnailManager::LoadEntriesInCache( | |
| 243 scoped_ptr<ThumbnailVector> entries) { | |
| 244 for (ThumbnailVector::iterator it = entries->begin(); it != entries->end(); | |
| 245 ++it) { | |
| 246 std::vector<unsigned char> encoded_data(it->data().begin(), | |
| 247 it->data().end()); | |
| 248 | |
| 249 scoped_ptr<SkBitmap> bitmap(DecodeThumbnail(encoded_data)); | |
| 250 if (bitmap.get()) { | |
| 251 thumbnail_map_.insert(std::make_pair(it->url(), *bitmap)); | |
| 252 } | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 void ThumbnailManager::ServePendingCacheRequests() { | |
| 257 for (ThumbnailRequestMap::iterator it = pending_cache_requests_.begin(); | |
| 258 it != pending_cache_requests_.end(); ++it) { | |
| 259 const ThumbnailRequest& request = it->second; | |
| 260 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); | |
| 261 callback_it != request.callbacks.end(); ++callback_it) { | |
| 262 ServeFromCacheOrNetwork(request.url, request.thumbnail_url, *callback_it); | |
| 263 } | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 // static | |
| 268 bool ThumbnailManager::EncodeThumbnail(const SkBitmap& bitmap, | |
| 269 std::vector<unsigned char>* dest) { | |
| 270 SkAutoLockPixels bitmap_lock(bitmap); | |
| 271 if (!bitmap.readyToDraw() || bitmap.isNull()) { | |
| 272 return false; | |
| 273 } | |
| 274 return gfx::JPEGCodec::Encode( | |
| 275 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
| 276 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | |
| 277 bitmap.width() * bitmap.bytesPerPixel(), 100, dest); | |
| 278 } | |
| 279 | |
| 280 } // namespace suggestions | |
| OLD | NEW |