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