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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 void Compact(NTPSnippet::PtrVector* snippets) { | 175 void Compact(NTPSnippet::PtrVector* snippets) { |
176 snippets->erase( | 176 snippets->erase( |
177 std::remove_if( | 177 std::remove_if( |
178 snippets->begin(), snippets->end(), | 178 snippets->begin(), snippets->end(), |
179 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), | 179 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), |
180 snippets->end()); | 180 snippets->end()); |
181 } | 181 } |
182 | 182 |
183 } // namespace | 183 } // namespace |
184 | 184 |
185 // TODO(pke): Rename this service to ArticleSuggestionsService and move to | |
186 // a subdirectory. | |
185 NTPSnippetsService::NTPSnippetsService( | 187 NTPSnippetsService::NTPSnippetsService( |
186 bool enabled, | 188 bool enabled, |
187 PrefService* pref_service, | 189 PrefService* pref_service, |
188 SuggestionsService* suggestions_service, | 190 SuggestionsService* suggestions_service, |
189 const std::string& application_language_code, | 191 const std::string& application_language_code, |
190 NTPSnippetsScheduler* scheduler, | 192 NTPSnippetsScheduler* scheduler, |
191 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 193 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
192 std::unique_ptr<ImageFetcher> image_fetcher, | 194 std::unique_ptr<ImageFetcher> image_fetcher, |
193 std::unique_ptr<ImageDecoder> image_decoder, | 195 std::unique_ptr<ImageDecoder> image_decoder, |
194 std::unique_ptr<NTPSnippetsDatabase> database, | 196 std::unique_ptr<NTPSnippetsDatabase> database, |
195 std::unique_ptr<NTPSnippetsStatusService> status_service) | 197 std::unique_ptr<NTPSnippetsStatusService> status_service) |
196 : state_(State::NOT_INITED), | 198 : ContentSuggestionsProvider({ContentSuggestionsCategory::ARTICLES}), |
199 state_(State::NOT_INITED), | |
197 pref_service_(pref_service), | 200 pref_service_(pref_service), |
198 suggestions_service_(suggestions_service), | 201 suggestions_service_(suggestions_service), |
199 application_language_code_(application_language_code), | 202 application_language_code_(application_language_code), |
200 scheduler_(scheduler), | 203 scheduler_(scheduler), |
201 snippets_fetcher_(std::move(snippets_fetcher)), | 204 snippets_fetcher_(std::move(snippets_fetcher)), |
202 image_fetcher_(std::move(image_fetcher)), | 205 image_fetcher_(std::move(image_fetcher)), |
203 image_decoder_(std::move(image_decoder)), | 206 image_decoder_(std::move(image_decoder)), |
204 database_(std::move(database)), | 207 database_(std::move(database)), |
205 snippets_status_service_(std::move(status_service)), | 208 snippets_status_service_(std::move(status_service)), |
206 fetch_after_load_(false) { | 209 fetch_after_load_(false), |
210 category_status_(ContentSuggestionsCategoryStatus::LOADING), | |
211 observer_(nullptr) { | |
207 // TODO(dgn) should be removed after branch point (https://crbug.com/617585). | 212 // TODO(dgn) should be removed after branch point (https://crbug.com/617585). |
208 ClearDeprecatedPrefs(); | 213 ClearDeprecatedPrefs(); |
209 | 214 |
210 if (!enabled || database_->IsErrorState()) { | 215 // In some cases, don't even bother loading the database. |
211 // Don't even bother loading the database. | 216 if (!enabled) { |
217 category_status_ = | |
218 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED; | |
212 EnterState(State::SHUT_DOWN); | 219 EnterState(State::SHUT_DOWN); |
220 NotifyCategoryStatusChanged(); | |
221 return; | |
222 } | |
223 if (database_->IsErrorState()) { | |
224 category_status_ = ContentSuggestionsCategoryStatus::ERROR; | |
225 EnterState(State::SHUT_DOWN); | |
226 NotifyCategoryStatusChanged(); | |
213 return; | 227 return; |
214 } | 228 } |
215 | 229 |
216 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, | 230 database_->SetErrorCallback(base::Bind(&NTPSnippetsService::OnDatabaseError, |
217 base::Unretained(this))); | 231 base::Unretained(this))); |
218 | 232 |
219 // We transition to other states while finalizing the initialization, when the | 233 // We transition to other states while finalizing the initialization, when the |
220 // database is done loading. | 234 // database is done loading. |
221 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, | 235 database_->LoadSnippets(base::Bind(&NTPSnippetsService::OnDatabaseLoaded, |
222 base::Unretained(this))); | 236 base::Unretained(this))); |
223 } | 237 } |
224 | 238 |
225 NTPSnippetsService::~NTPSnippetsService() { | 239 NTPSnippetsService::~NTPSnippetsService() { |
226 DCHECK(state_ == State::SHUT_DOWN); | 240 DCHECK(state_ == State::SHUT_DOWN); |
227 } | 241 } |
228 | 242 |
229 // static | 243 // static |
230 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 244 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
231 registry->RegisterListPref(prefs::kDeprecatedSnippets); | 245 registry->RegisterListPref(prefs::kDeprecatedSnippets); |
232 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets); | 246 registry->RegisterListPref(prefs::kDeprecatedDiscardedSnippets); |
233 registry->RegisterListPref(prefs::kSnippetHosts); | 247 registry->RegisterListPref(prefs::kSnippetHosts); |
234 } | 248 } |
235 | 249 |
236 // Inherited from KeyedService. | 250 // Inherited from KeyedService. |
237 void NTPSnippetsService::Shutdown() { | 251 void NTPSnippetsService::Shutdown() { |
252 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED; | |
238 EnterState(State::SHUT_DOWN); | 253 EnterState(State::SHUT_DOWN); |
254 NotifyCategoryStatusChanged(); | |
239 } | 255 } |
240 | 256 |
241 void NTPSnippetsService::FetchSnippets() { | 257 void NTPSnippetsService::FetchSnippets() { |
242 if (ready()) | 258 if (ready()) |
243 FetchSnippetsFromHosts(GetSuggestionsHosts()); | 259 FetchSnippetsFromHosts(GetSuggestionsHosts()); |
244 else | 260 else |
245 fetch_after_load_ = true; | 261 fetch_after_load_ = true; |
246 } | 262 } |
247 | 263 |
248 void NTPSnippetsService::FetchSnippetsFromHosts( | 264 void NTPSnippetsService::FetchSnippetsFromHosts( |
(...skipping 12 matching lines...) Expand all Loading... | |
261 if (ready()) { | 277 if (ready()) { |
262 base::Time now = base::Time::Now(); | 278 base::Time now = base::Time::Now(); |
263 scheduler_->Schedule( | 279 scheduler_->Schedule( |
264 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), | 280 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), |
265 GetFetchingIntervalFallback(), GetRescheduleTime(now)); | 281 GetFetchingIntervalFallback(), GetRescheduleTime(now)); |
266 } else { | 282 } else { |
267 scheduler_->Unschedule(); | 283 scheduler_->Unschedule(); |
268 } | 284 } |
269 } | 285 } |
270 | 286 |
271 void NTPSnippetsService::FetchSnippetImage( | 287 void NTPSnippetsService::FetchSuggestionImage( |
272 const std::string& snippet_id, | 288 const std::string& suggestion_id, |
273 const ImageFetchedCallback& callback) { | 289 const ImageFetchedCallback& callback) { |
274 database_->LoadImage( | 290 database_->LoadImage( |
275 snippet_id, | 291 suggestion_id, |
276 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, | 292 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, |
277 base::Unretained(this), snippet_id, callback)); | 293 base::Unretained(this), suggestion_id, callback)); |
278 } | 294 } |
279 | 295 |
280 void NTPSnippetsService::ClearSnippets() { | 296 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() { |
281 if (!initialized()) | 297 if (!initialized()) |
282 return; | 298 return; |
283 | 299 |
284 if (snippets_.empty()) | 300 if (snippets_.empty()) |
285 return; | 301 return; |
286 | 302 |
287 database_->DeleteSnippets(snippets_); | 303 database_->DeleteSnippets(snippets_); |
288 snippets_.clear(); | 304 snippets_.clear(); |
289 | 305 |
290 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 306 NotifyNewSuggestions(); |
291 NTPSnippetsServiceLoaded()); | |
292 } | 307 } |
293 | 308 |
294 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { | 309 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { |
295 // |suggestions_service_| can be null in tests. | 310 // |suggestions_service_| can be null in tests. |
296 if (!suggestions_service_) | 311 if (!suggestions_service_) |
297 return std::set<std::string>(); | 312 return std::set<std::string>(); |
298 | 313 |
299 // TODO(treib): This should just call GetSnippetHostsFromPrefs. | 314 // TODO(treib): This should just call GetSnippetHostsFromPrefs. |
300 return GetSuggestionsHostsImpl( | 315 return GetSuggestionsHostsImpl( |
301 suggestions_service_->GetSuggestionsDataFromCache()); | 316 suggestions_service_->GetSuggestionsDataFromCache()); |
302 } | 317 } |
303 | 318 |
304 bool NTPSnippetsService::DiscardSnippet(const std::string& snippet_id) { | 319 void NTPSnippetsService::DiscardSuggestion(const std::string& suggestion_id) { |
305 if (!ready()) | 320 if (!ready()) |
306 return false; | 321 return; |
307 | 322 |
308 auto it = | 323 auto it = std::find_if( |
309 std::find_if(snippets_.begin(), snippets_.end(), | 324 snippets_.begin(), snippets_.end(), |
310 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 325 [&suggestion_id](const std::unique_ptr<NTPSnippet>& snippet) { |
311 return snippet->id() == snippet_id; | 326 return snippet->id() == suggestion_id; |
312 }); | 327 }); |
313 if (it == snippets_.end()) | 328 if (it == snippets_.end()) |
314 return false; | 329 return; |
315 | 330 |
316 (*it)->set_discarded(true); | 331 (*it)->set_discarded(true); |
317 | 332 |
318 database_->SaveSnippet(**it); | 333 database_->SaveSnippet(**it); |
319 database_->DeleteImage((*it)->id()); | 334 database_->DeleteImage((*it)->id()); |
320 | 335 |
321 discarded_snippets_.push_back(std::move(*it)); | 336 discarded_snippets_.push_back(std::move(*it)); |
322 snippets_.erase(it); | 337 snippets_.erase(it); |
323 | 338 |
324 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 339 NotifyNewSuggestions(); |
325 NTPSnippetsServiceLoaded()); | |
326 return true; | |
327 } | 340 } |
328 | 341 |
329 void NTPSnippetsService::ClearDiscardedSnippets() { | 342 void NTPSnippetsService::ClearDiscardedSuggestionsForDebugging() { |
330 if (!initialized()) | 343 if (!initialized()) |
331 return; | 344 return; |
332 | 345 |
333 if (discarded_snippets_.empty()) | 346 if (discarded_snippets_.empty()) |
334 return; | 347 return; |
335 | 348 |
336 database_->DeleteSnippets(discarded_snippets_); | 349 database_->DeleteSnippets(discarded_snippets_); |
337 discarded_snippets_.clear(); | 350 discarded_snippets_.clear(); |
338 } | 351 } |
339 | 352 |
353 void NTPSnippetsService::SetObserver(Observer* observer) { | |
354 observer_ = observer; | |
355 } | |
356 | |
357 ContentSuggestionsCategoryStatus NTPSnippetsService::GetCategoryStatus( | |
358 ContentSuggestionsCategory category) { | |
359 return category_status_; | |
Marc Treib
2016/07/11 08:41:33
nit: Maybe DCHECK that |category| is ARTICLES?
Philipp Keck
2016/07/11 09:41:49
Done.
| |
360 } | |
361 | |
340 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 362 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
341 observers_.AddObserver(observer); | 363 observers_.AddObserver(observer); |
342 } | 364 } |
343 | 365 |
344 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 366 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
345 observers_.RemoveObserver(observer); | 367 observers_.RemoveObserver(observer); |
346 } | 368 } |
347 | 369 |
348 // static | 370 // static |
349 int NTPSnippetsService::GetMaxSnippetCountForTesting() { | 371 int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 [](const std::unique_ptr<NTPSnippet>& lhs, | 410 [](const std::unique_ptr<NTPSnippet>& lhs, |
389 const std::unique_ptr<NTPSnippet>& rhs) { | 411 const std::unique_ptr<NTPSnippet>& rhs) { |
390 return lhs->score() > rhs->score(); | 412 return lhs->score() > rhs->score(); |
391 }); | 413 }); |
392 | 414 |
393 ClearExpiredSnippets(); | 415 ClearExpiredSnippets(); |
394 FinishInitialization(); | 416 FinishInitialization(); |
395 } | 417 } |
396 | 418 |
397 void NTPSnippetsService::OnDatabaseError() { | 419 void NTPSnippetsService::OnDatabaseError() { |
420 category_status_ = ContentSuggestionsCategoryStatus::ERROR; | |
398 EnterState(State::SHUT_DOWN); | 421 EnterState(State::SHUT_DOWN); |
422 NotifyCategoryStatusChanged(); | |
399 } | 423 } |
400 | 424 |
401 void NTPSnippetsService::OnSuggestionsChanged( | 425 void NTPSnippetsService::OnSuggestionsChanged( |
402 const SuggestionsProfile& suggestions) { | 426 const SuggestionsProfile& suggestions) { |
403 DCHECK(initialized()); | 427 DCHECK(initialized()); |
404 | 428 |
405 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); | 429 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
406 if (hosts == GetSnippetHostsFromPrefs()) | 430 if (hosts == GetSnippetHostsFromPrefs()) |
407 return; | 431 return; |
408 | 432 |
409 // Remove existing snippets that aren't in the suggestions anymore. | 433 // Remove existing snippets that aren't in the suggestions anymore. |
410 // TODO(treib,maybelle): If there is another source with an allowed host, | 434 // TODO(treib,maybelle): If there is another source with an allowed host, |
411 // then we should fall back to that. | 435 // then we should fall back to that. |
412 // First, move them over into |to_delete|. | 436 // First, move them over into |to_delete|. |
413 NTPSnippet::PtrVector to_delete; | 437 NTPSnippet::PtrVector to_delete; |
414 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { | 438 for (std::unique_ptr<NTPSnippet>& snippet : snippets_) { |
415 if (!hosts.count(snippet->best_source().url.host())) | 439 if (!hosts.count(snippet->best_source().url.host())) |
416 to_delete.emplace_back(std::move(snippet)); | 440 to_delete.emplace_back(std::move(snippet)); |
417 } | 441 } |
418 Compact(&snippets_); | 442 Compact(&snippets_); |
419 // Then delete the removed snippets from the database. | 443 // Then delete the removed snippets from the database. |
420 database_->DeleteSnippets(to_delete); | 444 database_->DeleteSnippets(to_delete); |
421 | 445 |
422 StoreSnippetHostsToPrefs(hosts); | 446 StoreSnippetHostsToPrefs(hosts); |
423 | 447 |
424 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 448 NotifyNewSuggestions(); |
425 NTPSnippetsServiceLoaded()); | |
426 | 449 |
427 FetchSnippetsFromHosts(hosts); | 450 FetchSnippetsFromHosts(hosts); |
428 } | 451 } |
429 | 452 |
430 void NTPSnippetsService::OnFetchFinished( | 453 void NTPSnippetsService::OnFetchFinished( |
431 NTPSnippetsFetcher::OptionalSnippets snippets) { | 454 NTPSnippetsFetcher::OptionalSnippets snippets) { |
432 if (!ready()) | 455 if (!ready()) |
433 return; | 456 return; |
434 | 457 |
435 if (snippets) { | 458 if (snippets) { |
(...skipping 16 matching lines...) Expand all Loading... | |
452 database_->DeleteSnippets(to_delete); | 475 database_->DeleteSnippets(to_delete); |
453 } | 476 } |
454 | 477 |
455 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", | 478 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticles", |
456 snippets_.size()); | 479 snippets_.size()); |
457 if (snippets_.empty() && !discarded_snippets_.empty()) { | 480 if (snippets_.empty() && !discarded_snippets_.empty()) { |
458 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", | 481 UMA_HISTOGRAM_COUNTS("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded", |
459 discarded_snippets_.size()); | 482 discarded_snippets_.size()); |
460 } | 483 } |
461 | 484 |
462 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 485 NotifyNewSuggestions(); |
463 NTPSnippetsServiceLoaded()); | |
464 } | 486 } |
465 | 487 |
466 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { | 488 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
467 DCHECK(ready()); | 489 DCHECK(ready()); |
468 | 490 |
469 // Remove new snippets that we already have, or that have been discarded. | 491 // Remove new snippets that we already have, or that have been discarded. |
470 std::set<std::string> old_snippet_ids; | 492 std::set<std::string> old_snippet_ids; |
471 InsertAllIDs(discarded_snippets_, &old_snippet_ids); | 493 InsertAllIDs(discarded_snippets_, &old_snippet_ids); |
472 InsertAllIDs(snippets_, &old_snippet_ids); | 494 InsertAllIDs(snippets_, &old_snippet_ids); |
473 new_snippets.erase( | 495 new_snippets.erase( |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
645 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { | 667 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { |
646 suggestions_service_subscription_ = | 668 suggestions_service_subscription_ = |
647 suggestions_service_->AddCallback(base::Bind( | 669 suggestions_service_->AddCallback(base::Bind( |
648 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); | 670 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); |
649 } | 671 } |
650 | 672 |
651 RescheduleFetching(); | 673 RescheduleFetching(); |
652 } | 674 } |
653 | 675 |
654 void NTPSnippetsService::EnterStateDisabled() { | 676 void NTPSnippetsService::EnterStateDisabled() { |
655 ClearSnippets(); | 677 ClearCachedSuggestionsForDebugging(); |
656 ClearDiscardedSnippets(); | 678 ClearDiscardedSuggestionsForDebugging(); |
657 | 679 |
658 expiry_timer_.Stop(); | 680 expiry_timer_.Stop(); |
659 suggestions_service_subscription_.reset(); | 681 suggestions_service_subscription_.reset(); |
660 RescheduleFetching(); | 682 RescheduleFetching(); |
661 } | 683 } |
662 | 684 |
663 void NTPSnippetsService::EnterStateShutdown() { | 685 void NTPSnippetsService::EnterStateShutdown() { |
664 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 686 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
665 NTPSnippetsServiceShutdown()); | 687 NTPSnippetsServiceShutdown()); |
666 | 688 |
(...skipping 10 matching lines...) Expand all Loading... | |
677 | 699 |
678 // |image_fetcher_| can be null in tests. | 700 // |image_fetcher_| can be null in tests. |
679 if (image_fetcher_) | 701 if (image_fetcher_) |
680 image_fetcher_->SetImageFetcherDelegate(this); | 702 image_fetcher_->SetImageFetcherDelegate(this); |
681 | 703 |
682 // Note: Initializing the status service will run the callback right away with | 704 // Note: Initializing the status service will run the callback right away with |
683 // the current state. | 705 // the current state. |
684 snippets_status_service_->Init(base::Bind( | 706 snippets_status_service_->Init(base::Bind( |
685 &NTPSnippetsService::UpdateStateForStatus, base::Unretained(this))); | 707 &NTPSnippetsService::UpdateStateForStatus, base::Unretained(this))); |
686 | 708 |
687 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 709 NotifyNewSuggestions(); |
688 NTPSnippetsServiceLoaded()); | |
689 } | 710 } |
690 | 711 |
691 void NTPSnippetsService::UpdateStateForStatus(DisabledReason disabled_reason) { | 712 void NTPSnippetsService::UpdateStateForStatus(DisabledReason disabled_reason) { |
692 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 713 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
693 NTPSnippetsServiceDisabledReasonChanged(disabled_reason)); | 714 NTPSnippetsServiceDisabledReasonChanged(disabled_reason)); |
694 | 715 |
695 State new_state; | 716 State new_state; |
696 switch (disabled_reason) { | 717 switch (disabled_reason) { |
697 case DisabledReason::NONE: | 718 case DisabledReason::NONE: |
698 new_state = State::READY; | 719 new_state = State::READY; |
720 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE; | |
699 break; | 721 break; |
700 | 722 |
701 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN: | 723 case DisabledReason::HISTORY_SYNC_STATE_UNKNOWN: |
702 // HistorySync is not initialized yet, so we don't know what the actual | 724 // HistorySync is not initialized yet, so we don't know what the actual |
703 // state is and we just return the current one. If things change, | 725 // state is and we just return the current one. If things change, |
704 // |OnStateChanged| will call this function again to update the state. | 726 // |OnStateChanged| will call this function again to update the state. |
705 DVLOG(1) << "Sync configuration incomplete, continuing based on the " | 727 DVLOG(1) << "Sync configuration incomplete, continuing based on the " |
706 "current state."; | 728 "current state."; |
707 new_state = state_; | 729 new_state = state_; |
730 category_status_ = ContentSuggestionsCategoryStatus::LOADING; | |
708 break; | 731 break; |
709 | 732 |
710 case DisabledReason::EXPLICITLY_DISABLED: | 733 case DisabledReason::EXPLICITLY_DISABLED: |
711 case DisabledReason::SIGNED_OUT: | 734 category_status_ = |
712 case DisabledReason::SYNC_DISABLED: | 735 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED; |
713 case DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED: | |
714 case DisabledReason::HISTORY_SYNC_DISABLED: | |
715 new_state = State::DISABLED; | 736 new_state = State::DISABLED; |
716 break; | 737 break; |
717 | 738 |
739 case DisabledReason::SIGNED_OUT: | |
740 category_status_ = ContentSuggestionsCategoryStatus::SIGNED_OUT; | |
741 new_state = State::DISABLED; | |
742 break; | |
743 | |
744 case DisabledReason::SYNC_DISABLED: | |
745 category_status_ = ContentSuggestionsCategoryStatus::SYNC_DISABLED; | |
746 new_state = State::DISABLED; | |
747 break; | |
748 | |
749 case DisabledReason::PASSPHRASE_ENCRYPTION_ENABLED: | |
750 category_status_ = | |
751 ContentSuggestionsCategoryStatus::PASSPHRASE_ENCRYPTION_ENABLED; | |
752 new_state = State::DISABLED; | |
753 break; | |
754 | |
755 case DisabledReason::HISTORY_SYNC_DISABLED: | |
756 category_status_ = | |
757 ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED; | |
758 new_state = State::DISABLED; | |
759 break; | |
760 | |
718 default: | 761 default: |
719 // All cases should be handled by the above switch | 762 // All cases should be handled by the above switch |
720 NOTREACHED(); | 763 NOTREACHED(); |
721 new_state = State::DISABLED; | 764 new_state = State::DISABLED; |
722 break; | 765 break; |
723 } | 766 } |
724 | 767 |
725 EnterState(new_state); | 768 EnterState(new_state); |
769 NotifyCategoryStatusChanged(); | |
726 } | 770 } |
727 | 771 |
728 void NTPSnippetsService::EnterState(State state) { | 772 void NTPSnippetsService::EnterState(State state) { |
729 if (state == state_) | 773 if (state == state_) |
730 return; | 774 return; |
731 | 775 |
732 switch (state) { | 776 switch (state) { |
733 case State::NOT_INITED: | 777 case State::NOT_INITED: |
734 // Initial state, it should not be possible to get back there. | 778 // Initial state, it should not be possible to get back there. |
735 NOTREACHED(); | 779 NOTREACHED(); |
(...skipping 24 matching lines...) Expand all Loading... | |
760 EnterStateShutdown(); | 804 EnterStateShutdown(); |
761 return; | 805 return; |
762 } | 806 } |
763 } | 807 } |
764 | 808 |
765 void NTPSnippetsService::ClearDeprecatedPrefs() { | 809 void NTPSnippetsService::ClearDeprecatedPrefs() { |
766 pref_service_->ClearPref(prefs::kDeprecatedSnippets); | 810 pref_service_->ClearPref(prefs::kDeprecatedSnippets); |
767 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); | 811 pref_service_->ClearPref(prefs::kDeprecatedDiscardedSnippets); |
768 } | 812 } |
769 | 813 |
814 void NTPSnippetsService::NotifyNewSuggestions() { | |
815 // TODO(pke): Remove this as soon as this becomes a pure provider. | |
816 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | |
817 NTPSnippetsServiceLoaded()); | |
818 | |
819 if (!observer_) | |
820 return; | |
821 | |
822 std::vector<ContentSuggestion> result; | |
823 for (const std::unique_ptr<NTPSnippet>& snippet : snippets_) { | |
824 if (!snippet->is_complete()) | |
825 continue; | |
826 ContentSuggestion suggestion( | |
827 MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet->id()), | |
828 snippet->best_source().url); | |
829 suggestion.set_amp_url(snippet->best_source().amp_url); | |
830 suggestion.set_title(snippet->title()); | |
831 suggestion.set_snippet_text(snippet->snippet()); | |
832 suggestion.set_publish_date(snippet->publish_date()); | |
833 suggestion.set_publisher_name(snippet->best_source().publisher_name); | |
834 suggestion.set_score(snippet->score()); | |
835 result.emplace_back(std::move(suggestion)); | |
836 } | |
837 observer_->OnNewSuggestions(ContentSuggestionsCategory::ARTICLES, | |
838 std::move(result)); | |
839 } | |
840 | |
841 void NTPSnippetsService::NotifyCategoryStatusChanged() { | |
842 if (observer_) { | |
843 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, | |
844 category_status_); | |
845 } | |
846 } | |
847 | |
770 } // namespace ntp_snippets | 848 } // namespace ntp_snippets |
OLD | NEW |