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

Side by Side 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, 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): Involve the provider in providing the URL for looking up the
141 // 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.
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();
Bernhard Bauer 2017/03/31 16:45:16 This can be `position->`.
jkrcal 2017/04/03 12:37:36 Done.
156
157 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.
158 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
159 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
160 base::Unretained(this), publisher_url, minimum_size_in_pixel,
161 desired_size_in_pixel, callback,
162 /*continue_to_google_server=*/true),
163 &favicons_task_tracker_);
164 }
165
166 void ContentSuggestionsService::OnGetFaviconFromCacheFinished(
167 const GURL& publisher_url,
168 int minimum_size_in_pixel,
169 int desired_size_in_pixel,
170 const ImageFetchedCallback& callback,
171 bool continue_to_google_server,
172 const favicon_base::LargeIconImageResult& result) {
173 if (!result.image.IsEmpty()) {
174 callback.Run(result.image);
175 return;
176 }
177
178 if (!result.fallback_icon_style->is_default_background_color ||
179 !continue_to_google_server) {
180 // We cannot download from the server if there is some small icon in the
181 // cache (resulting in non-default bakground color) or if we already did so.
182 callback.Run(gfx::Image());
183 return;
184 }
185
186 // Try to fetch the favicon from a Google favicon server.
187 large_icon_service_
188 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
189 publisher_url, minimum_size_in_pixel,
190 base::Bind(
191 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished,
192 base::Unretained(this), publisher_url, minimum_size_in_pixel,
193 desired_size_in_pixel, callback));
194 }
195
196 void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished(
197 const GURL& publisher_url,
198 int minimum_size_in_pixel,
199 int desired_size_in_pixel,
200 const ImageFetchedCallback& callback,
201 bool success) {
202 if (!success) {
203 callback.Run(gfx::Image());
204 return;
205 }
206
207 // Get the freshly downloaded icon from the cache.
208 large_icon_service_->GetLargeIconImageOrFallbackStyle(
209 publisher_url, minimum_size_in_pixel, desired_size_in_pixel,
210 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished,
211 base::Unretained(this), publisher_url, minimum_size_in_pixel,
212 desired_size_in_pixel, callback,
213 /*continue_to_google_server=*/false),
214 &favicons_task_tracker_);
136 } 215 }
137 216
138 void ContentSuggestionsService::ClearHistory( 217 void ContentSuggestionsService::ClearHistory(
139 base::Time begin, 218 base::Time begin,
140 base::Time end, 219 base::Time end,
141 const base::Callback<bool(const GURL& url)>& filter) { 220 const base::Callback<bool(const GURL& url)>& filter) {
142 for (const auto& provider : providers_) { 221 for (const auto& provider : providers_) {
143 provider->ClearHistory(begin, end, filter); 222 provider->ClearHistory(begin, end, filter);
144 } 223 }
145 category_ranker_->ClearHistory(begin, end); 224 category_ranker_->ClearHistory(begin, end);
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { 599 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() {
521 base::ListValue list; 600 base::ListValue list;
522 for (const auto& category_provider_pair : dismissed_providers_by_category_) { 601 for (const auto& category_provider_pair : dismissed_providers_by_category_) {
523 list.AppendInteger(category_provider_pair.first.id()); 602 list.AppendInteger(category_provider_pair.first.id());
524 } 603 }
525 604
526 pref_service_->Set(prefs::kDismissedCategories, list); 605 pref_service_->Set(prefs::kDismissedCategories, list);
527 } 606 }
528 607
529 } // namespace ntp_snippets 608 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698