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

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

Issue 2790743003: [Content suggestions] Implement fetching publishers' favicons (Closed)
Patch Set: Minor fix Created 3 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/content_suggestions_service.h" 5 #include "components/ntp_snippets/content_suggestions_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "base/time/default_clock.h" 17 #include "base/time/default_clock.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "components/favicon/core/large_icon_service.h"
20 #include "components/favicon_base/fallback_icon_style.h"
21 #include "components/favicon_base/favicon_types.h"
19 #include "components/ntp_snippets/pref_names.h" 22 #include "components/ntp_snippets/pref_names.h"
20 #include "components/prefs/pref_registry_simple.h" 23 #include "components/prefs/pref_registry_simple.h"
21 #include "components/prefs/pref_service.h" 24 #include "components/prefs/pref_service.h"
22 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
23 26
24 namespace ntp_snippets { 27 namespace ntp_snippets {
25 28
26 ContentSuggestionsService::ContentSuggestionsService( 29 ContentSuggestionsService::ContentSuggestionsService(
27 State state, 30 State state,
28 SigninManagerBase* signin_manager, 31 SigninManagerBase* signin_manager,
29 history::HistoryService* history_service, 32 history::HistoryService* history_service,
33 favicon::LargeIconService* large_icon_service,
30 PrefService* pref_service, 34 PrefService* pref_service,
31 std::unique_ptr<CategoryRanker> category_ranker, 35 std::unique_ptr<CategoryRanker> category_ranker,
32 std::unique_ptr<UserClassifier> user_classifier, 36 std::unique_ptr<UserClassifier> user_classifier,
33 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler) 37 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler)
34 : state_(state), 38 : state_(state),
35 signin_observer_(this), 39 signin_observer_(this),
36 history_service_observer_(this), 40 history_service_observer_(this),
37 remote_suggestions_provider_(nullptr), 41 remote_suggestions_provider_(nullptr),
42 large_icon_service_(large_icon_service),
38 pref_service_(pref_service), 43 pref_service_(pref_service),
39 remote_suggestions_scheduler_(std::move(remote_suggestions_scheduler)), 44 remote_suggestions_scheduler_(std::move(remote_suggestions_scheduler)),
40 user_classifier_(std::move(user_classifier)), 45 user_classifier_(std::move(user_classifier)),
41 category_ranker_(std::move(category_ranker)) { 46 category_ranker_(std::move(category_ranker)) {
42 // Can be null in tests. 47 // Can be null in tests.
43 if (signin_manager) { 48 if (signin_manager) {
44 signin_observer_.Add(signin_manager); 49 signin_observer_.Add(signin_manager);
45 } 50 }
46 51
47 if (history_service) { 52 if (history_service) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 130 }
126 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage( 131 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage(
127 suggestion_id, callback); 132 suggestion_id, callback);
128 } 133 }
129 134
130 void ContentSuggestionsService::FetchSuggestionFavicon( 135 void ContentSuggestionsService::FetchSuggestionFavicon(
131 const ContentSuggestion::ID& suggestion_id, 136 const ContentSuggestion::ID& suggestion_id,
132 int minimum_size_in_pixel, 137 int minimum_size_in_pixel,
133 int desired_size_in_pixel, 138 int desired_size_in_pixel,
134 const ImageFetchedCallback& callback) { 139 const ImageFetchedCallback& callback) {
135 // TODO(jkrcal): Implement. 140 // TODO(jkrcal): Allow the provider to provide (or possibly override) the URL
141 // for looking up the favicon.
142 std::vector<ContentSuggestion>* suggestions =
143 &suggestions_by_category_[suggestion_id.category()];
144 auto position =
145 std::find_if(suggestions->begin(), suggestions->end(),
146 [&suggestion_id](const ContentSuggestion& suggestion) {
147 return suggestion_id == suggestion.id();
148 });
149 if (position == suggestions->end() || !large_icon_service_) {
150 base::ThreadTaskRunnerHandle::Get()->PostTask(
151 FROM_HERE, base::Bind(callback, gfx::Image()));
152 return;
153 }
154
155 const GURL& publisher_url = position->url().GetWithEmptyPath();
156
157 // TODO(jkrcal): Create a general wrapper function in LargeIconService that
158 // does handle the get-from-cache-and-fallback-to-google-server functionality
159 // in one shot (for all clients that do not need to react in between).
160 large_icon_service_->GetLargeIconImageOrFallbackStyle(
161 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
162 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
163 base::Unretained(this), publisher_url, minimum_size_in_pixel,
164 desired_size_in_pixel, callback,
165 /*continue_to_google_server=*/true),
166 &favicons_task_tracker_);
167 }
168
169 void ContentSuggestionsService::OnGetFaviconFromCacheFinished(
170 const GURL& publisher_url,
171 int minimum_size_in_pixel,
172 int desired_size_in_pixel,
173 const ImageFetchedCallback& callback,
174 bool continue_to_google_server,
175 const favicon_base::LargeIconImageResult& result) {
176 if (!result.image.IsEmpty()) {
177 callback.Run(result.image);
178 return;
179 }
180
181 if (!continue_to_google_server ||
182 (result.fallback_icon_style &&
183 !result.fallback_icon_style->is_default_background_color)) {
184 // We cannot download from the server if there is some small icon in the
185 // cache (resulting in non-default bakground color) or if we already did so.
186 callback.Run(gfx::Image());
187 return;
188 }
189
190 // Try to fetch the favicon from a Google favicon server.
191 large_icon_service_
192 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
193 publisher_url, minimum_size_in_pixel,
194 base::Bind(
195 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished,
196 base::Unretained(this), publisher_url, minimum_size_in_pixel,
197 desired_size_in_pixel, callback));
198 }
199
200 void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished(
201 const GURL& publisher_url,
202 int minimum_size_in_pixel,
203 int desired_size_in_pixel,
204 const ImageFetchedCallback& callback,
205 bool success) {
206 if (!success) {
207 callback.Run(gfx::Image());
208 return;
209 }
210
211 // Get the freshly downloaded icon from the cache.
212 large_icon_service_->GetLargeIconImageOrFallbackStyle(
213 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
214 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
215 base::Unretained(this), publisher_url, minimum_size_in_pixel,
216 desired_size_in_pixel, callback,
217 /*continue_to_google_server=*/false),
218 &favicons_task_tracker_);
136 } 219 }
137 220
138 void ContentSuggestionsService::ClearHistory( 221 void ContentSuggestionsService::ClearHistory(
139 base::Time begin, 222 base::Time begin,
140 base::Time end, 223 base::Time end,
141 const base::Callback<bool(const GURL& url)>& filter) { 224 const base::Callback<bool(const GURL& url)>& filter) {
142 for (const auto& provider : providers_) { 225 for (const auto& provider : providers_) {
143 provider->ClearHistory(begin, end, filter); 226 provider->ClearHistory(begin, end, filter);
144 } 227 }
145 category_ranker_->ClearHistory(begin, end); 228 category_ranker_->ClearHistory(begin, end);
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { 621 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() {
539 base::ListValue list; 622 base::ListValue list;
540 for (const auto& category_provider_pair : dismissed_providers_by_category_) { 623 for (const auto& category_provider_pair : dismissed_providers_by_category_) {
541 list.AppendInteger(category_provider_pair.first.id()); 624 list.AppendInteger(category_provider_pair.first.id());
542 } 625 }
543 626
544 pref_service_->Set(prefs::kDismissedCategories, list); 627 pref_service_->Set(prefs::kDismissedCategories, list);
545 } 628 }
546 629
547 } // namespace ntp_snippets 630 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/content_suggestions_service.h ('k') | components/ntp_snippets/content_suggestions_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698