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 <utility> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/location.h" | 10 #include "base/location.h" |
9 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
10 #include "components/suggestions/image_encoder.h" | 12 #include "components/suggestions/image_encoder.h" |
11 #include "components/suggestions/image_fetcher.h" | 13 #include "components/suggestions/image_fetcher.h" |
12 | 14 |
13 using leveldb_proto::ProtoDatabase; | 15 using leveldb_proto::ProtoDatabase; |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
(...skipping 13 matching lines...) Expand all Loading... |
30 | 32 |
31 namespace suggestions { | 33 namespace suggestions { |
32 | 34 |
33 ImageManager::ImageManager() : weak_ptr_factory_(this) {} | 35 ImageManager::ImageManager() : weak_ptr_factory_(this) {} |
34 | 36 |
35 ImageManager::ImageManager( | 37 ImageManager::ImageManager( |
36 scoped_ptr<ImageFetcher> image_fetcher, | 38 scoped_ptr<ImageFetcher> image_fetcher, |
37 scoped_ptr<ProtoDatabase<ImageData>> database, | 39 scoped_ptr<ProtoDatabase<ImageData>> database, |
38 const base::FilePath& database_dir, | 40 const base::FilePath& database_dir, |
39 scoped_refptr<base::TaskRunner> background_task_runner) | 41 scoped_refptr<base::TaskRunner> background_task_runner) |
40 : image_fetcher_(image_fetcher.Pass()), | 42 : image_fetcher_(std::move(image_fetcher)), |
41 database_(database.Pass()), | 43 database_(std::move(database)), |
42 background_task_runner_(background_task_runner), | 44 background_task_runner_(background_task_runner), |
43 database_ready_(false), | 45 database_ready_(false), |
44 weak_ptr_factory_(this) { | 46 weak_ptr_factory_(this) { |
45 image_fetcher_->SetImageFetcherDelegate(this); | 47 image_fetcher_->SetImageFetcherDelegate(this); |
46 database_->Init(kDatabaseUMAClientName, database_dir, | 48 database_->Init(kDatabaseUMAClientName, database_dir, |
47 base::Bind(&ImageManager::OnDatabaseInit, | 49 base::Bind(&ImageManager::OnDatabaseInit, |
48 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
49 } | 51 } |
50 | 52 |
51 ImageManager::~ImageManager() {} | 53 ImageManager::~ImageManager() {} |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 176 |
175 // Save the resulting bitmap to the database. | 177 // Save the resulting bitmap to the database. |
176 ImageData data; | 178 ImageData data; |
177 data.set_url(url.spec()); | 179 data.set_url(url.spec()); |
178 data.set_data(encoded_data->front(), encoded_data->size()); | 180 data.set_data(encoded_data->front(), encoded_data->size()); |
179 scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save( | 181 scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save( |
180 new ProtoDatabase<ImageData>::KeyEntryVector()); | 182 new ProtoDatabase<ImageData>::KeyEntryVector()); |
181 scoped_ptr<std::vector<std::string>> keys_to_remove( | 183 scoped_ptr<std::vector<std::string>> keys_to_remove( |
182 new std::vector<std::string>()); | 184 new std::vector<std::string>()); |
183 entries_to_save->push_back(std::make_pair(data.url(), data)); | 185 entries_to_save->push_back(std::make_pair(data.url(), data)); |
184 database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), | 186 database_->UpdateEntries(std::move(entries_to_save), |
| 187 std::move(keys_to_remove), |
185 base::Bind(&ImageManager::OnDatabaseSave, | 188 base::Bind(&ImageManager::OnDatabaseSave, |
186 weak_ptr_factory_.GetWeakPtr())); | 189 weak_ptr_factory_.GetWeakPtr())); |
187 } | 190 } |
188 | 191 |
189 void ImageManager::OnDatabaseInit(bool success) { | 192 void ImageManager::OnDatabaseInit(bool success) { |
190 if (!success) { | 193 if (!success) { |
191 DVLOG(1) << "Image database init failed."; | 194 DVLOG(1) << "Image database init failed."; |
192 database_.reset(); | 195 database_.reset(); |
193 ServePendingCacheRequests(); | 196 ServePendingCacheRequests(); |
194 return; | 197 return; |
195 } | 198 } |
196 database_->LoadEntries(base::Bind(&ImageManager::OnDatabaseLoad, | 199 database_->LoadEntries(base::Bind(&ImageManager::OnDatabaseLoad, |
197 weak_ptr_factory_.GetWeakPtr())); | 200 weak_ptr_factory_.GetWeakPtr())); |
198 } | 201 } |
199 | 202 |
200 void ImageManager::OnDatabaseLoad(bool success, | 203 void ImageManager::OnDatabaseLoad(bool success, |
201 scoped_ptr<ImageDataVector> entries) { | 204 scoped_ptr<ImageDataVector> entries) { |
202 if (!success) { | 205 if (!success) { |
203 DVLOG(1) << "Image database load failed."; | 206 DVLOG(1) << "Image database load failed."; |
204 database_.reset(); | 207 database_.reset(); |
205 ServePendingCacheRequests(); | 208 ServePendingCacheRequests(); |
206 return; | 209 return; |
207 } | 210 } |
208 database_ready_ = true; | 211 database_ready_ = true; |
209 | 212 |
210 LoadEntriesInCache(entries.Pass()); | 213 LoadEntriesInCache(std::move(entries)); |
211 ServePendingCacheRequests(); | 214 ServePendingCacheRequests(); |
212 } | 215 } |
213 | 216 |
214 void ImageManager::OnDatabaseSave(bool success) { | 217 void ImageManager::OnDatabaseSave(bool success) { |
215 if (!success) { | 218 if (!success) { |
216 DVLOG(1) << "Image database save failed."; | 219 DVLOG(1) << "Image database save failed."; |
217 database_.reset(); | 220 database_.reset(); |
218 database_ready_ = false; | 221 database_ready_ = false; |
219 } | 222 } |
220 } | 223 } |
(...skipping 14 matching lines...) Expand all Loading... |
235 it != pending_cache_requests_.end(); ++it) { | 238 it != pending_cache_requests_.end(); ++it) { |
236 const ImageCacheRequest& request = it->second; | 239 const ImageCacheRequest& request = it->second; |
237 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); | 240 for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); |
238 callback_it != request.callbacks.end(); ++callback_it) { | 241 callback_it != request.callbacks.end(); ++callback_it) { |
239 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); | 242 ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); |
240 } | 243 } |
241 } | 244 } |
242 } | 245 } |
243 | 246 |
244 } // namespace suggestions | 247 } // namespace suggestions |
OLD | NEW |