Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 2187233002: Add ContentSuggestionsCategoryFactory; Store categories as ints (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }), 180 [](const std::unique_ptr<NTPSnippet>& snippet) { return !snippet; }),
181 snippets->end()); 181 snippets->end());
182 } 182 }
183 183
184 } // namespace 184 } // namespace
185 185
186 NTPSnippetsService::NTPSnippetsService( 186 NTPSnippetsService::NTPSnippetsService(
187 bool enabled, 187 bool enabled,
188 PrefService* pref_service, 188 PrefService* pref_service,
189 SuggestionsService* suggestions_service, 189 SuggestionsService* suggestions_service,
190 ContentSuggestionsCategoryFactory* category_factory,
190 const std::string& application_language_code, 191 const std::string& application_language_code,
191 NTPSnippetsScheduler* scheduler, 192 NTPSnippetsScheduler* scheduler,
192 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, 193 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
193 std::unique_ptr<ImageFetcher> image_fetcher, 194 std::unique_ptr<ImageFetcher> image_fetcher,
194 std::unique_ptr<ImageDecoder> image_decoder, 195 std::unique_ptr<ImageDecoder> image_decoder,
195 std::unique_ptr<NTPSnippetsDatabase> database, 196 std::unique_ptr<NTPSnippetsDatabase> database,
196 std::unique_ptr<NTPSnippetsStatusService> status_service) 197 std::unique_ptr<NTPSnippetsStatusService> status_service)
197 : ContentSuggestionsProvider({ContentSuggestionsCategory::ARTICLES}), 198 : ContentSuggestionsProvider(category_factory),
198 state_(State::NOT_INITED), 199 state_(State::NOT_INITED),
199 category_status_(ContentSuggestionsCategoryStatus::INITIALIZING), 200 category_status_(ContentSuggestionsCategoryStatus::INITIALIZING),
200 pref_service_(pref_service), 201 pref_service_(pref_service),
201 suggestions_service_(suggestions_service), 202 suggestions_service_(suggestions_service),
202 application_language_code_(application_language_code), 203 application_language_code_(application_language_code),
203 observer_(nullptr), 204 observer_(nullptr),
204 scheduler_(scheduler), 205 scheduler_(scheduler),
205 snippets_fetcher_(std::move(snippets_fetcher)), 206 snippets_fetcher_(std::move(snippets_fetcher)),
206 image_fetcher_(std::move(image_fetcher)), 207 image_fetcher_(std::move(image_fetcher)),
207 image_decoder_(std::move(image_decoder)), 208 image_decoder_(std::move(image_decoder)),
208 database_(std::move(database)), 209 database_(std::move(database)),
209 snippets_status_service_(std::move(status_service)), 210 snippets_status_service_(std::move(status_service)),
210 fetch_after_load_(false) { 211 fetch_after_load_(false),
212 provided_category_(category_factory->FromKnownCategory(
213 KnownSuggestionsCategories::ARTICLES)) {
211 // In some cases, don't even bother loading the database. 214 // In some cases, don't even bother loading the database.
212 if (!enabled) { 215 if (!enabled) {
213 EnterState(State::SHUT_DOWN, 216 EnterState(State::SHUT_DOWN,
214 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED); 217 ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED);
215 return; 218 return;
216 } 219 }
217 if (database_->IsErrorState()) { 220 if (database_->IsErrorState()) {
218 EnterState(State::SHUT_DOWN, 221 EnterState(State::SHUT_DOWN,
219 ContentSuggestionsCategoryStatus::LOADING_ERROR); 222 ContentSuggestionsCategoryStatus::LOADING_ERROR);
220 return; 223 return;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 if (ready()) { 274 if (ready()) {
272 base::Time now = base::Time::Now(); 275 base::Time now = base::Time::Now();
273 scheduler_->Schedule( 276 scheduler_->Schedule(
274 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now), 277 GetFetchingIntervalWifiCharging(), GetFetchingIntervalWifi(now),
275 GetFetchingIntervalFallback(), GetRescheduleTime(now)); 278 GetFetchingIntervalFallback(), GetRescheduleTime(now));
276 } else { 279 } else {
277 scheduler_->Unschedule(); 280 scheduler_->Unschedule();
278 } 281 }
279 } 282 }
280 283
284 std::vector<ContentSuggestionsCategory>
285 NTPSnippetsService::GetProvidedCategories() {
286 return std::vector<ContentSuggestionsCategory>({provided_category_});
287 }
288
281 void NTPSnippetsService::FetchSuggestionImage( 289 void NTPSnippetsService::FetchSuggestionImage(
282 const std::string& suggestion_id, 290 const std::string& suggestion_id,
283 const ImageFetchedCallback& callback) { 291 const ImageFetchedCallback& callback) {
284 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id); 292 std::string snippet_id = GetWithinCategoryIDFromUniqueID(suggestion_id);
285 database_->LoadImage( 293 database_->LoadImage(
286 snippet_id, 294 snippet_id,
287 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase, 295 base::Bind(&NTPSnippetsService::OnSnippetImageFetchedFromDatabase,
288 base::Unretained(this), snippet_id, callback)); 296 base::Unretained(this), snippet_id, callback));
289 } 297 }
290 298
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 database_->DeleteSnippets(dismissed_snippets_); 352 database_->DeleteSnippets(dismissed_snippets_);
345 dismissed_snippets_.clear(); 353 dismissed_snippets_.clear();
346 } 354 }
347 355
348 void NTPSnippetsService::SetObserver(Observer* observer) { 356 void NTPSnippetsService::SetObserver(Observer* observer) {
349 observer_ = observer; 357 observer_ = observer;
350 } 358 }
351 359
352 ContentSuggestionsCategoryStatus NTPSnippetsService::GetCategoryStatus( 360 ContentSuggestionsCategoryStatus NTPSnippetsService::GetCategoryStatus(
353 ContentSuggestionsCategory category) { 361 ContentSuggestionsCategory category) {
354 DCHECK_EQ(ContentSuggestionsCategory::ARTICLES, category); 362 DCHECK(category.IsKnownCategory(KnownSuggestionsCategories::ARTICLES));
355 return category_status_; 363 return category_status_;
356 } 364 }
357 365
358 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 366 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
359 observers_.AddObserver(observer); 367 observers_.AddObserver(observer);
360 } 368 }
361 369
362 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 370 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
363 observers_.RemoveObserver(observer); 371 observers_.RemoveObserver(observer);
364 } 372 }
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 625
618 // Fetching from the DB failed; start a network fetch. 626 // Fetching from the DB failed; start a network fetch.
619 FetchSnippetImageFromNetwork(snippet_id, callback); 627 FetchSnippetImageFromNetwork(snippet_id, callback);
620 } 628 }
621 629
622 void NTPSnippetsService::OnSnippetImageDecoded( 630 void NTPSnippetsService::OnSnippetImageDecoded(
623 const std::string& snippet_id, 631 const std::string& snippet_id,
624 const ImageFetchedCallback& callback, 632 const ImageFetchedCallback& callback,
625 const gfx::Image& image) { 633 const gfx::Image& image) {
626 if (!image.IsEmpty()) { 634 if (!image.IsEmpty()) {
627 callback.Run(MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet_id), 635 callback.Run(MakeUniqueID(provided_category_, snippet_id), image);
628 image);
629 return; 636 return;
630 } 637 }
631 638
632 // If decoding the image failed, delete the DB entry. 639 // If decoding the image failed, delete the DB entry.
633 database_->DeleteImage(snippet_id); 640 database_->DeleteImage(snippet_id);
634 641
635 FetchSnippetImageFromNetwork(snippet_id, callback); 642 FetchSnippetImageFromNetwork(snippet_id, callback);
636 } 643 }
637 644
638 void NTPSnippetsService::FetchSnippetImageFromNetwork( 645 void NTPSnippetsService::FetchSnippetImageFromNetwork(
639 const std::string& snippet_id, 646 const std::string& snippet_id,
640 const ImageFetchedCallback& callback) { 647 const ImageFetchedCallback& callback) {
641 auto it = 648 auto it =
642 std::find_if(snippets_.begin(), snippets_.end(), 649 std::find_if(snippets_.begin(), snippets_.end(),
643 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { 650 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) {
644 return snippet->id() == snippet_id; 651 return snippet->id() == snippet_id;
645 }); 652 });
646 if (it == snippets_.end()) { 653 if (it == snippets_.end()) {
647 callback.Run(MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet_id), 654 callback.Run(MakeUniqueID(provided_category_, snippet_id), gfx::Image());
648 gfx::Image());
649 return; 655 return;
650 } 656 }
651 657
652 const NTPSnippet& snippet = *it->get(); 658 const NTPSnippet& snippet = *it->get();
653 image_fetcher_->StartOrQueueNetworkRequest( 659 image_fetcher_->StartOrQueueNetworkRequest(
654 snippet.id(), snippet.salient_image_url(), callback); 660 snippet.id(), snippet.salient_image_url(), callback);
655 } 661 }
656 662
657 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) { 663 void NTPSnippetsService::EnterStateEnabled(bool fetch_snippets) {
658 if (fetch_snippets) 664 if (fetch_snippets)
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 NTPSnippetsServiceLoaded()); 785 NTPSnippetsServiceLoaded());
780 786
781 if (!observer_) 787 if (!observer_)
782 return; 788 return;
783 789
784 std::vector<ContentSuggestion> result; 790 std::vector<ContentSuggestion> result;
785 for (const std::unique_ptr<NTPSnippet>& snippet : snippets_) { 791 for (const std::unique_ptr<NTPSnippet>& snippet : snippets_) {
786 if (!snippet->is_complete()) 792 if (!snippet->is_complete())
787 continue; 793 continue;
788 ContentSuggestion suggestion( 794 ContentSuggestion suggestion(
789 MakeUniqueID(ContentSuggestionsCategory::ARTICLES, snippet->id()), 795 MakeUniqueID(provided_category_, snippet->id()),
790 snippet->best_source().url); 796 snippet->best_source().url);
791 suggestion.set_amp_url(snippet->best_source().amp_url); 797 suggestion.set_amp_url(snippet->best_source().amp_url);
792 suggestion.set_title(snippet->title()); 798 suggestion.set_title(snippet->title());
793 suggestion.set_snippet_text(snippet->snippet()); 799 suggestion.set_snippet_text(snippet->snippet());
794 suggestion.set_publish_date(snippet->publish_date()); 800 suggestion.set_publish_date(snippet->publish_date());
795 suggestion.set_publisher_name(snippet->best_source().publisher_name); 801 suggestion.set_publisher_name(snippet->best_source().publisher_name);
796 suggestion.set_score(snippet->score()); 802 suggestion.set_score(snippet->score());
797 result.emplace_back(std::move(suggestion)); 803 result.emplace_back(std::move(suggestion));
798 } 804 }
799 observer_->OnNewSuggestions(ContentSuggestionsCategory::ARTICLES, 805 observer_->OnNewSuggestions(provided_category_, std::move(result));
800 std::move(result));
801 } 806 }
802 807
803 void NTPSnippetsService::NotifyCategoryStatusChanged() { 808 void NTPSnippetsService::NotifyCategoryStatusChanged() {
804 if (observer_) { 809 if (observer_) {
805 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::ARTICLES, 810 observer_->OnCategoryStatusChanged(provided_category_, category_status_);
806 category_status_);
807 } 811 }
808 } 812 }
809 813
810 } // namespace ntp_snippets 814 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698