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

Unified Diff: components/ntp_snippets/content_suggestions_service.cc

Issue 2790743003: [Content suggestions] Implement fetching publishers' favicons (Closed)
Patch Set: Fix iOS compilation Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/content_suggestions_service.cc
diff --git a/components/ntp_snippets/content_suggestions_service.cc b/components/ntp_snippets/content_suggestions_service.cc
index 1bf49e9a43c761fbb43aa5c24a79276dd4aca670..7b7ee9e871484f6aaf424b60ff425ff7e4a5ae98 100644
--- a/components/ntp_snippets/content_suggestions_service.cc
+++ b/components/ntp_snippets/content_suggestions_service.cc
@@ -16,6 +16,9 @@
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/default_clock.h"
#include "base/values.h"
+#include "components/favicon/core/large_icon_service.h"
+#include "components/favicon_base/fallback_icon_style.h"
+#include "components/favicon_base/favicon_types.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
@@ -27,6 +30,7 @@ ContentSuggestionsService::ContentSuggestionsService(
State state,
SigninManagerBase* signin_manager,
history::HistoryService* history_service,
+ favicon::LargeIconService* large_icon_service,
PrefService* pref_service,
std::unique_ptr<CategoryRanker> category_ranker,
std::unique_ptr<UserClassifier> user_classifier,
@@ -35,6 +39,7 @@ ContentSuggestionsService::ContentSuggestionsService(
signin_observer_(this),
history_service_observer_(this),
remote_suggestions_provider_(nullptr),
+ large_icon_service_(large_icon_service),
pref_service_(pref_service),
remote_suggestions_scheduler_(std::move(remote_suggestions_scheduler)),
user_classifier_(std::move(user_classifier)),
@@ -132,7 +137,81 @@ void ContentSuggestionsService::FetchSuggestionFavicon(
int minimum_size_in_pixel,
int desired_size_in_pixel,
const ImageFetchedCallback& callback) {
- // TODO(jkrcal): Implement.
+ // TODO(jkrcal): Involve the provider in providing the URL for looking up the
+ // favicon (maybe voluntarily).
Bernhard Bauer 2017/03/31 16:45:16 Nit: I _think_ I know what you mean by this commen
jkrcal 2017/04/03 12:37:36 :) Done.
+ std::vector<ContentSuggestion>* suggestions =
+ &suggestions_by_category_[suggestion_id.category()];
+ auto position =
+ std::find_if(suggestions->begin(), suggestions->end(),
+ [&suggestion_id](const ContentSuggestion& suggestion) {
+ return suggestion_id == suggestion.id();
+ });
+ if (position == suggestions->end() || !large_icon_service_) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(callback, gfx::Image()));
+ return;
+ }
+
+ const GURL& publisher_url = (*position).url().GetWithEmptyPath();
Bernhard Bauer 2017/03/31 16:45:16 This can be `position->`.
jkrcal 2017/04/03 12:37:36 Done.
+
+ large_icon_service_->GetLargeIconImageOrFallbackStyle(
gambard 2017/03/31 14:30:44 It would make sense to move this to a separate fil
jkrcal 2017/04/03 12:37:36 I agree that some wrapper fetch-from-cache-and-ser
gambard 2017/04/03 12:38:50 Acknowledged.
+ publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
+ base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
+ base::Unretained(this), publisher_url, minimum_size_in_pixel,
+ desired_size_in_pixel, callback,
+ /*continue_to_google_server=*/true),
+ &favicons_task_tracker_);
+}
+
+void ContentSuggestionsService::OnGetFaviconFromCacheFinished(
+ const GURL& publisher_url,
+ int minimum_size_in_pixel,
+ int desired_size_in_pixel,
+ const ImageFetchedCallback& callback,
+ bool continue_to_google_server,
+ const favicon_base::LargeIconImageResult& result) {
+ if (!result.image.IsEmpty()) {
+ callback.Run(result.image);
+ return;
+ }
+
+ if (!result.fallback_icon_style->is_default_background_color ||
+ !continue_to_google_server) {
+ // We cannot download from the server if there is some small icon in the
+ // cache (resulting in non-default bakground color) or if we already did so.
+ callback.Run(gfx::Image());
+ return;
+ }
+
+ // Try to fetch the favicon from a Google favicon server.
+ large_icon_service_
+ ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
+ publisher_url, minimum_size_in_pixel,
+ base::Bind(
+ &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished,
+ base::Unretained(this), publisher_url, minimum_size_in_pixel,
+ desired_size_in_pixel, callback));
+}
+
+void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished(
+ const GURL& publisher_url,
+ int minimum_size_in_pixel,
+ int desired_size_in_pixel,
+ const ImageFetchedCallback& callback,
+ bool success) {
+ if (!success) {
+ callback.Run(gfx::Image());
+ return;
+ }
+
+ // Get the freshly downloaded icon from the cache.
+ large_icon_service_->GetLargeIconImageOrFallbackStyle(
+ publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
+ base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
+ base::Unretained(this), publisher_url, minimum_size_in_pixel,
+ desired_size_in_pixel, callback,
+ /*continue_to_google_server=*/false),
+ &favicons_task_tracker_);
}
void ContentSuggestionsService::ClearHistory(

Powered by Google App Engine
This is Rietveld 408576698