| 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 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 GetFetchingIntervalFallback(), GetRescheduleTime(now)); | 267 GetFetchingIntervalFallback(), GetRescheduleTime(now)); |
| 268 } else { | 268 } else { |
| 269 scheduler_->Unschedule(); | 269 scheduler_->Unschedule(); |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 | 272 |
| 273 std::vector<Category> NTPSnippetsService::GetProvidedCategories() { | 273 std::vector<Category> NTPSnippetsService::GetProvidedCategories() { |
| 274 return std::vector<Category>({provided_category_}); | 274 return std::vector<Category>({provided_category_}); |
| 275 } | 275 } |
| 276 | 276 |
| 277 void NTPSnippetsService::FetchSuggestionImage( | 277 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { |
| 278 const std::string& suggestion_id, | 278 DCHECK(category.IsKnownCategory(KnownCategories::ARTICLES)); |
| 279 const ImageFetchedCallback& callback) { | 279 return category_status_; |
| 280 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | |
| 281 database_->LoadImage( | |
| 282 snippet_id, | |
| 283 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, | |
| 284 base::Unretained(this), snippet_id, callback)); | |
| 285 } | |
| 286 | |
| 287 void NTPSnippetsService::ClearCachedSuggestionsForDebugging() { | |
| 288 if (!initialized()) | |
| 289 return; | |
| 290 | |
| 291 if (snippets_.empty()) | |
| 292 return; | |
| 293 | |
| 294 database_->DeleteSnippets(snippets_); | |
| 295 snippets_.clear(); | |
| 296 | |
| 297 NotifyNewSuggestions(); | |
| 298 } | |
| 299 | |
| 300 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { | |
| 301 // |suggestions_service_| can be null in tests. | |
| 302 if (!suggestions_service_) | |
| 303 return std::set<std::string>(); | |
| 304 | |
| 305 // TODO(treib): This should just call GetSnippetHostsFromPrefs. | |
| 306 return GetSuggestionsHostsImpl( | |
| 307 suggestions_service_->GetSuggestionsDataFromCache()); | |
| 308 } | 280 } |
| 309 | 281 |
| 310 void NTPSnippetsService::DismissSuggestion(const std::string& suggestion_id) { | 282 void NTPSnippetsService::DismissSuggestion(const std::string& suggestion_id) { |
| 311 if (!ready()) | 283 if (!ready()) |
| 312 return; | 284 return; |
| 313 | 285 |
| 314 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); | 286 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); |
| 315 | 287 |
| 316 auto it = | 288 auto it = |
| 317 std::find_if(snippets_.begin(), snippets_.end(), | 289 std::find_if(snippets_.begin(), snippets_.end(), |
| 318 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 290 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { |
| 319 return snippet->id() == snippet_id; | 291 return snippet->id() == snippet_id; |
| 320 }); | 292 }); |
| 321 if (it == snippets_.end()) | 293 if (it == snippets_.end()) |
| 322 return; | 294 return; |
| 323 | 295 |
| 324 (*it)->set_dismissed(true); | 296 (*it)->set_dismissed(true); |
| 325 | 297 |
| 326 database_->SaveSnippet(**it); | 298 database_->SaveSnippet(**it); |
| 327 database_->DeleteImage((*it)->id()); | 299 database_->DeleteImage((*it)->id()); |
| 328 | 300 |
| 329 dismissed_snippets_.push_back(std::move(*it)); | 301 dismissed_snippets_.push_back(std::move(*it)); |
| 330 snippets_.erase(it); | 302 snippets_.erase(it); |
| 331 } | 303 } |
| 332 | 304 |
| 333 void NTPSnippetsService::ClearDismissedSuggestionsForDebugging() { | 305 void NTPSnippetsService::FetchSuggestionImage( |
| 306 const std::string& suggestion_id, |
| 307 const ImageFetchedCallback& callback) { |
| 308 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); |
| 309 database_->LoadImage( |
| 310 snippet_id, |
| 311 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, |
| 312 base::Unretained(this), snippet_id, callback)); |
| 313 } |
| 314 |
| 315 void NTPSnippetsService::ClearCachedSuggestionsForDebugging(Category category) { |
| 316 DCHECK_EQ(category, provided_category_); |
| 334 if (!initialized()) | 317 if (!initialized()) |
| 335 return; | 318 return; |
| 336 | 319 |
| 320 if (snippets_.empty()) |
| 321 return; |
| 322 |
| 323 database_->DeleteSnippets(snippets_); |
| 324 snippets_.clear(); |
| 325 |
| 326 NotifyNewSuggestions(); |
| 327 } |
| 328 |
| 329 std::vector<ContentSuggestion> |
| 330 NTPSnippetsService::GetDismissedSuggestionsForDebugging(Category category) { |
| 331 DCHECK_EQ(category, provided_category_); |
| 332 std::vector<ContentSuggestion> result; |
| 333 for (const std::unique_ptr<NTPSnippet>& snippet : dismissed_snippets_) { |
| 334 if (!snippet->is_complete()) |
| 335 continue; |
| 336 ContentSuggestion suggestion( |
| 337 MakeUniqueID(provided_category_, snippet->id()), |
| 338 snippet->best_source().url); |
| 339 suggestion.set_amp_url(snippet->best_source().amp_url); |
| 340 suggestion.set_title(base::UTF8ToUTF16(snippet->title())); |
| 341 suggestion.set_snippet_text(base::UTF8ToUTF16(snippet->snippet())); |
| 342 suggestion.set_publish_date(snippet->publish_date()); |
| 343 suggestion.set_publisher_name( |
| 344 base::UTF8ToUTF16(snippet->best_source().publisher_name)); |
| 345 suggestion.set_score(snippet->score()); |
| 346 result.emplace_back(std::move(suggestion)); |
| 347 } |
| 348 return result; |
| 349 } |
| 350 |
| 351 void NTPSnippetsService::ClearDismissedSuggestionsForDebugging( |
| 352 Category category) { |
| 353 DCHECK_EQ(category, provided_category_); |
| 354 if (!initialized()) |
| 355 return; |
| 356 |
| 337 if (dismissed_snippets_.empty()) | 357 if (dismissed_snippets_.empty()) |
| 338 return; | 358 return; |
| 339 | 359 |
| 340 database_->DeleteSnippets(dismissed_snippets_); | 360 database_->DeleteSnippets(dismissed_snippets_); |
| 341 dismissed_snippets_.clear(); | 361 dismissed_snippets_.clear(); |
| 342 } | 362 } |
| 343 | 363 |
| 344 CategoryStatus NTPSnippetsService::GetCategoryStatus(Category category) { | 364 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() const { |
| 345 DCHECK(category.IsKnownCategory(KnownCategories::ARTICLES)); | 365 // |suggestions_service_| can be null in tests. |
| 346 return category_status_; | 366 if (!suggestions_service_) |
| 367 return std::set<std::string>(); |
| 368 |
| 369 // TODO(treib): This should just call GetSnippetHostsFromPrefs. |
| 370 return GetSuggestionsHostsImpl( |
| 371 suggestions_service_->GetSuggestionsDataFromCache()); |
| 347 } | 372 } |
| 348 | 373 |
| 349 // static | 374 // static |
| 350 int NTPSnippetsService::GetMaxSnippetCountForTesting() { | 375 int NTPSnippetsService::GetMaxSnippetCountForTesting() { |
| 351 return kMaxSnippetCount; | 376 return kMaxSnippetCount; |
| 352 } | 377 } |
| 353 | 378 |
| 354 //////////////////////////////////////////////////////////////////////////////// | 379 //////////////////////////////////////////////////////////////////////////////// |
| 355 // Private methods | 380 // Private methods |
| 356 | 381 |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { | 682 if (snippets_fetcher_->UsesHostRestrictions() && suggestions_service_) { |
| 658 suggestions_service_subscription_ = | 683 suggestions_service_subscription_ = |
| 659 suggestions_service_->AddCallback(base::Bind( | 684 suggestions_service_->AddCallback(base::Bind( |
| 660 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); | 685 &NTPSnippetsService::OnSuggestionsChanged, base::Unretained(this))); |
| 661 } | 686 } |
| 662 | 687 |
| 663 RescheduleFetching(); | 688 RescheduleFetching(); |
| 664 } | 689 } |
| 665 | 690 |
| 666 void NTPSnippetsService::EnterStateDisabled() { | 691 void NTPSnippetsService::EnterStateDisabled() { |
| 667 ClearCachedSuggestionsForDebugging(); | 692 ClearCachedSuggestionsForDebugging(provided_category_); |
| 668 ClearDismissedSuggestionsForDebugging(); | 693 ClearDismissedSuggestionsForDebugging(provided_category_); |
| 669 | 694 |
| 670 expiry_timer_.Stop(); | 695 expiry_timer_.Stop(); |
| 671 suggestions_service_subscription_.reset(); | 696 suggestions_service_subscription_.reset(); |
| 672 RescheduleFetching(); | 697 RescheduleFetching(); |
| 673 } | 698 } |
| 674 | 699 |
| 675 void NTPSnippetsService::EnterStateError() { | 700 void NTPSnippetsService::EnterStateError() { |
| 676 expiry_timer_.Stop(); | 701 expiry_timer_.Stop(); |
| 677 suggestions_service_subscription_.reset(); | 702 suggestions_service_subscription_.reset(); |
| 678 RescheduleFetching(); | 703 RescheduleFetching(); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { | 805 void NTPSnippetsService::UpdateCategoryStatus(CategoryStatus status) { |
| 781 if (status == category_status_) | 806 if (status == category_status_) |
| 782 return; | 807 return; |
| 783 | 808 |
| 784 category_status_ = status; | 809 category_status_ = status; |
| 785 observer()->OnCategoryStatusChanged(this, provided_category_, | 810 observer()->OnCategoryStatusChanged(this, provided_category_, |
| 786 category_status_); | 811 category_status_); |
| 787 } | 812 } |
| 788 | 813 |
| 789 } // namespace ntp_snippets | 814 } // namespace ntp_snippets |
| OLD | NEW |