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