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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 sync_service_(sync_service), | 196 sync_service_(sync_service), |
197 sync_service_observer_(this), | 197 sync_service_observer_(this), |
198 suggestions_service_(suggestions_service), | 198 suggestions_service_(suggestions_service), |
199 application_language_code_(application_language_code), | 199 application_language_code_(application_language_code), |
200 scheduler_(scheduler), | 200 scheduler_(scheduler), |
201 snippets_fetcher_(std::move(snippets_fetcher)), | 201 snippets_fetcher_(std::move(snippets_fetcher)), |
202 image_fetcher_(std::move(image_fetcher)), | 202 image_fetcher_(std::move(image_fetcher)), |
203 image_decoder_(std::move(image_decoder)), | 203 image_decoder_(std::move(image_decoder)), |
204 database_(std::move(database)), | 204 database_(std::move(database)), |
205 fetch_after_load_(false) { | 205 fetch_after_load_(false) { |
206 // TODO(dgn) should be removed after branch point (https:://crbug.com/617585). | 206 // TODO(dgn) should be removed after branch point (https://crbug.com/617585). |
207 ClearDeprecatedPrefs(); | 207 ClearDeprecatedPrefs(); |
208 | 208 |
209 if (explicitly_disabled_) { | 209 if (explicitly_disabled_) { |
210 EnterState(State::DISABLED); | 210 EnterState(State::DISABLED); |
211 return; | 211 return; |
212 } | 212 } |
213 | 213 |
| 214 if (database_->IsErrorState()) { |
| 215 EnterState(State::SHUT_DOWN); |
| 216 return; |
| 217 } |
| 218 |
| 219 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
| 220 base::Unretained(this))); |
| 221 |
214 // We transition to other states while finalizing the initialization, when the | 222 // We transition to other states while finalizing the initialization, when the |
215 // database is done loading. | 223 // database is done loading. |
216 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 224 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
217 base::Unretained(this))); | 225 base::Unretained(this))); |
218 } | 226 } |
219 | 227 |
220 NTPSnippetsService::~NTPSnippetsService() { | 228 NTPSnippetsService::~NTPSnippetsService() { |
221 DCHECK(state_ == State::SHUT_DOWN); | 229 DCHECK(state_ == State::SHUT_DOWN); |
222 } | 230 } |
223 | 231 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 326 |
319 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 327 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
320 NTPSnippetsServiceLoaded()); | 328 NTPSnippetsServiceLoaded()); |
321 return true; | 329 return true; |
322 } | 330 } |
323 | 331 |
324 void NTPSnippetsService::ClearDiscardedSnippets() { | 332 void NTPSnippetsService::ClearDiscardedSnippets() { |
325 if (!initialized()) | 333 if (!initialized()) |
326 return; | 334 return; |
327 | 335 |
| 336 if (discarded_snippets_.empty()) |
| 337 return; |
| 338 |
328 database_->DeleteSnippets(discarded_snippets_); | 339 database_->DeleteSnippets(discarded_snippets_); |
329 discarded_snippets_.clear(); | 340 discarded_snippets_.clear(); |
330 } | 341 } |
331 | 342 |
332 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 343 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
333 observers_.AddObserver(observer); | 344 observers_.AddObserver(observer); |
334 } | 345 } |
335 | 346 |
336 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 347 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
337 observers_.RemoveObserver(observer); | 348 observers_.RemoveObserver(observer); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 std::sort(snippets_.begin(), snippets_.end(), | 426 std::sort(snippets_.begin(), snippets_.end(), |
416 [](const std::unique_ptr<NTPSnippet>& lhs, | 427 [](const std::unique_ptr<NTPSnippet>& lhs, |
417 const std::unique_ptr<NTPSnippet>& rhs) { | 428 const std::unique_ptr<NTPSnippet>& rhs) { |
418 return lhs->score() > rhs->score(); | 429 return lhs->score() > rhs->score(); |
419 }); | 430 }); |
420 | 431 |
421 ClearExpiredSnippets(); | 432 ClearExpiredSnippets(); |
422 FinishInitialization(); | 433 FinishInitialization(); |
423 } | 434 } |
424 | 435 |
| 436 void NTPSnippetsService::OnDatabaseError() { |
| 437 EnterState(State::SHUT_DOWN); |
| 438 } |
| 439 |
425 void NTPSnippetsService::OnSuggestionsChanged( | 440 void NTPSnippetsService::OnSuggestionsChanged( |
426 const SuggestionsProfile& suggestions) { | 441 const SuggestionsProfile& suggestions) { |
427 DCHECK(initialized()); | 442 DCHECK(initialized()); |
428 | 443 |
429 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); | 444 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
430 if (hosts == GetSnippetHostsFromPrefs()) | 445 if (hosts == GetSnippetHostsFromPrefs()) |
431 return; | 446 return; |
432 | 447 |
433 // Remove existing snippets that aren't in the suggestions anymore. | 448 // Remove existing snippets that aren't in the suggestions anymore. |
434 // TODO(treib,maybelle): If there is another source with an allowed host, | 449 // TODO(treib,maybelle): If there is another source with an allowed host, |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 return; | 806 return; |
792 } | 807 } |
793 } | 808 } |
794 | 809 |
795 void NTPSnippetsService::ClearDeprecatedPrefs() { | 810 void NTPSnippetsService::ClearDeprecatedPrefs() { |
796 pref_service_->ClearPref(prefs::kDeprecatedSnippets); | 811 pref_service_->ClearPref(prefs::kDeprecatedSnippets); |
797 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); | 812 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); |
798 } | 813 } |
799 | 814 |
800 } // namespace ntp_snippets | 815 } // namespace ntp_snippets |
OLD | NEW |