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