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