OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), | 184 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), |
185 snippets->end()); | 185 snippets->end()); |
186 } | 186 } |
187 | 187 |
188 } // namespace | 188 } // namespace |
189 | 189 |
190 NTPSnippetsService::NTPSnippetsService( | 190 NTPSnippetsService::NTPSnippetsService( |
191 Observer* observer, | 191 Observer* observer, |
192 CategoryFactory* category_factory, | 192 CategoryFactory* category_factory, |
193 PrefService* pref_service, | 193 PrefService* pref_service, |
194 history::HistoryService* history_service, | |
195 SuggestionsService* suggestions_service, | 194 SuggestionsService* suggestions_service, |
196 const std::string& application_language_code, | 195 const std::string& application_language_code, |
197 NTPSnippetsScheduler* scheduler, | 196 NTPSnippetsScheduler* scheduler, |
198 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 197 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
199 std::unique_ptr<ImageFetcher> image_fetcher, | 198 std::unique_ptr<ImageFetcher> image_fetcher, |
200 std::unique_ptr<ImageDecoder> image_decoder, | 199 std::unique_ptr<ImageDecoder> image_decoder, |
201 std::unique_ptr<NTPSnippetsDatabase> database, | 200 std::unique_ptr<NTPSnippetsDatabase> database, |
202 std::unique_ptr<NTPSnippetsStatusService> status_service) | 201 std::unique_ptr<NTPSnippetsStatusService> status_service) |
203 : ContentSuggestionsProvider(observer, category_factory), | 202 : ContentSuggestionsProvider(observer, category_factory), |
204 state_(State::NOT_INITED), | 203 state_(State::NOT_INITED), |
205 category_status_(CategoryStatus::INITIALIZING), | 204 category_status_(CategoryStatus::INITIALIZING), |
206 pref_service_(pref_service), | 205 pref_service_(pref_service), |
207 suggestions_service_(suggestions_service), | 206 suggestions_service_(suggestions_service), |
208 application_language_code_(application_language_code), | 207 application_language_code_(application_language_code), |
209 scheduler_(scheduler), | 208 scheduler_(scheduler), |
210 history_service_observer_(this), | |
211 snippets_fetcher_(std::move(snippets_fetcher)), | 209 snippets_fetcher_(std::move(snippets_fetcher)), |
212 image_fetcher_(std::move(image_fetcher)), | 210 image_fetcher_(std::move(image_fetcher)), |
213 image_decoder_(std::move(image_decoder)), | 211 image_decoder_(std::move(image_decoder)), |
214 database_(std::move(database)), | 212 database_(std::move(database)), |
215 snippets_status_service_(std::move(status_service)), | 213 snippets_status_service_(std::move(status_service)), |
216 fetch_after_load_(false), | 214 fetch_after_load_(false), |
217 nuke_after_load_(false), | 215 nuke_after_load_(false), |
218 provided_category_( | 216 provided_category_( |
219 category_factory->FromKnownCategory(KnownCategories::ARTICLES)), | 217 category_factory->FromKnownCategory(KnownCategories::ARTICLES)), |
220 thumbnail_requests_throttler_( | 218 thumbnail_requests_throttler_( |
221 pref_service, | 219 pref_service, |
222 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { | 220 RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) { |
223 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); | 221 observer->OnCategoryStatusChanged(this, provided_category_, category_status_); |
224 if (database_->IsErrorState()) { | 222 if (database_->IsErrorState()) { |
225 EnterState(State::ERROR_OCCURRED, CategoryStatus::LOADING_ERROR); | 223 EnterState(State::ERROR_OCCURRED, CategoryStatus::LOADING_ERROR); |
226 return; | 224 return; |
227 } | 225 } |
228 | 226 |
229 // Can be null in tests. | |
230 if (history_service) | |
231 history_service_observer_.Add(history_service); | |
232 | |
233 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 227 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
234 base::Unretained(this))); | 228 base::Unretained(this))); |
235 | 229 |
236 // We transition to other states while finalizing the initialization, when the | 230 // We transition to other states while finalizing the initialization, when the |
237 // database is done loading. | 231 // database is done loading. |
238 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 232 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
239 base::Unretained(this))); | 233 base::Unretained(this))); |
240 } | 234 } |
241 | 235 |
242 NTPSnippetsService::~NTPSnippetsService() { | 236 NTPSnippetsService::~NTPSnippetsService() { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 } | 403 } |
410 | 404 |
411 // static | 405 // static |
412 int NTPSnippetsService::GetMaxSnippetCountForTesting() { | 406 int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
413 return kMaxSnippetCount; | 407 return kMaxSnippetCount; |
414 } | 408 } |
415 | 409 |
416 //////////////////////////////////////////////////////////////////////////////// | 410 //////////////////////////////////////////////////////////////////////////////// |
417 // Private methods | 411 // Private methods |
418 | 412 |
419 // history::HistoryServiceObserver implementation. | |
420 void NTPSnippetsService::OnURLsDeleted( | |
421 history::HistoryService* history_service, | |
422 bool all_history, | |
423 bool expired, | |
424 const history::URLRows& deleted_rows, | |
425 const std::set<GURL>& favicon_urls) { | |
426 // We don't care about expired entries. | |
427 if (expired) | |
428 return; | |
429 | |
430 if (!ready()) | |
431 nuke_after_load_ = true; | |
432 else | |
433 NukeAllSnippets(); | |
434 } | |
435 | |
436 void NTPSnippetsService::HistoryServiceBeingDeleted( | |
437 history::HistoryService* history_service) { | |
438 history_service_observer_.RemoveAll(); | |
439 } | |
440 | |
441 // image_fetcher::ImageFetcherDelegate implementation. | 413 // image_fetcher::ImageFetcherDelegate implementation. |
442 void NTPSnippetsService::OnImageDataFetched(const std::string& snippet_id, | 414 void NTPSnippetsService::OnImageDataFetched(const std::string& snippet_id, |
443 const std::string& image_data) { | 415 const std::string& image_data) { |
444 if (image_data.empty()) | 416 if (image_data.empty()) |
445 return; | 417 return; |
446 | 418 |
447 // Only save the image if the corresponding snippet still exists. | 419 // Only save the image if the corresponding snippet still exists. |
448 auto it = | 420 auto it = |
449 std::find_if(snippets_.begin(), snippets_.end(), | 421 std::find_if(snippets_.begin(), snippets_.end(), |
450 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 422 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
905 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { | 877 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { |
906 if (status == category_status_) | 878 if (status == category_status_) |
907 return; | 879 return; |
908 | 880 |
909 category_status_ = status; | 881 category_status_ = status; |
910 observer()->OnCategoryStatusChanged(this, provided_category_, | 882 observer()->OnCategoryStatusChanged(this, provided_category_, |
911 category_status_); | 883 category_status_); |
912 } | 884 } |
913 | 885 |
914 } // namespace ntp_snippets | 886 } // namespace ntp_snippets |
OLD | NEW |