OLD | NEW |
---|---|
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/metrics/histogram_macros.h" | |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/time/default_clock.h" | 18 #include "base/time/default_clock.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "components/favicon/core/large_icon_service.h" | 20 #include "components/favicon/core/large_icon_service.h" |
20 #include "components/favicon_base/fallback_icon_style.h" | 21 #include "components/favicon_base/fallback_icon_style.h" |
21 #include "components/favicon_base/favicon_types.h" | 22 #include "components/favicon_base/favicon_types.h" |
22 #include "components/ntp_snippets/content_suggestions_metrics.h" | 23 #include "components/ntp_snippets/content_suggestions_metrics.h" |
23 #include "components/ntp_snippets/pref_names.h" | 24 #include "components/ntp_snippets/pref_names.h" |
24 #include "components/prefs/pref_registry_simple.h" | 25 #include "components/prefs/pref_registry_simple.h" |
25 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
26 #include "ui/gfx/image/image.h" | 27 #include "ui/gfx/image/image.h" |
27 | 28 |
28 namespace ntp_snippets { | 29 namespace ntp_snippets { |
29 | 30 |
31 namespace { | |
32 | |
33 // Enumeration listing all possible outcomes for fetch attempts of favicons for | |
34 // content suggestions. Used for UMA histograms, so do not change existing | |
35 // values. Insert new values at the end, and update the histogram definition. | |
36 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets | |
37 enum class FaviconFetchResult { | |
38 SUCCESS_CACHED = 0, | |
39 SUCCESS_FETCHED = 1, | |
40 FAILURE = 2, | |
41 RESULT_MAX = 3 | |
Ilya Sherman
2017/04/12 23:18:32
Optional nit: I'd name this "COUNT" rather than "M
jkrcal
2017/04/13 08:10:46
Good point, done.
| |
42 }; | |
43 | |
44 void RecordFaviconFetchResult(FaviconFetchResult result) { | |
45 UMA_HISTOGRAM_ENUMERATION( | |
46 "NewTabPage.ContentSuggestions.ArticleFaviconFetchResult", result, | |
47 FaviconFetchResult::RESULT_MAX); | |
48 } | |
49 | |
50 } // namespace | |
51 | |
30 ContentSuggestionsService::ContentSuggestionsService( | 52 ContentSuggestionsService::ContentSuggestionsService( |
31 State state, | 53 State state, |
32 SigninManagerBase* signin_manager, | 54 SigninManagerBase* signin_manager, |
33 history::HistoryService* history_service, | 55 history::HistoryService* history_service, |
34 favicon::LargeIconService* large_icon_service, | 56 favicon::LargeIconService* large_icon_service, |
35 PrefService* pref_service, | 57 PrefService* pref_service, |
36 std::unique_ptr<CategoryRanker> category_ranker, | 58 std::unique_ptr<CategoryRanker> category_ranker, |
37 std::unique_ptr<UserClassifier> user_classifier, | 59 std::unique_ptr<UserClassifier> user_classifier, |
38 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler) | 60 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler) |
39 : state_(state), | 61 : state_(state), |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 std::vector<ContentSuggestion>* suggestions = | 166 std::vector<ContentSuggestion>* suggestions = |
145 &suggestions_by_category_[suggestion_id.category()]; | 167 &suggestions_by_category_[suggestion_id.category()]; |
146 auto position = | 168 auto position = |
147 std::find_if(suggestions->begin(), suggestions->end(), | 169 std::find_if(suggestions->begin(), suggestions->end(), |
148 [&suggestion_id](const ContentSuggestion& suggestion) { | 170 [&suggestion_id](const ContentSuggestion& suggestion) { |
149 return suggestion_id == suggestion.id(); | 171 return suggestion_id == suggestion.id(); |
150 }); | 172 }); |
151 if (position == suggestions->end() || !large_icon_service_) { | 173 if (position == suggestions->end() || !large_icon_service_) { |
152 base::ThreadTaskRunnerHandle::Get()->PostTask( | 174 base::ThreadTaskRunnerHandle::Get()->PostTask( |
153 FROM_HERE, base::Bind(callback, gfx::Image())); | 175 FROM_HERE, base::Bind(callback, gfx::Image())); |
176 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | |
154 return; | 177 return; |
155 } | 178 } |
156 | 179 |
157 const GURL& publisher_url = position->url().GetWithEmptyPath(); | 180 const GURL& publisher_url = position->url().GetWithEmptyPath(); |
158 | 181 |
159 // TODO(jkrcal): Create a general wrapper function in LargeIconService that | 182 // TODO(jkrcal): Create a general wrapper function in LargeIconService that |
160 // does handle the get-from-cache-and-fallback-to-google-server functionality | 183 // does handle the get-from-cache-and-fallback-to-google-server functionality |
161 // in one shot (for all clients that do not need to react in between). | 184 // in one shot (for all clients that do not need to react in between). |
162 large_icon_service_->GetLargeIconImageOrFallbackStyle( | 185 large_icon_service_->GetLargeIconImageOrFallbackStyle( |
163 publisher_url, minimum_size_in_pixel, desired_size_in_pixel, | 186 publisher_url, minimum_size_in_pixel, desired_size_in_pixel, |
164 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | 187 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, |
165 base::Unretained(this), publisher_url, minimum_size_in_pixel, | 188 base::Unretained(this), publisher_url, minimum_size_in_pixel, |
166 desired_size_in_pixel, callback, | 189 desired_size_in_pixel, callback, |
167 /*continue_to_google_server=*/true), | 190 /*continue_to_google_server=*/true), |
168 &favicons_task_tracker_); | 191 &favicons_task_tracker_); |
169 } | 192 } |
170 | 193 |
171 void ContentSuggestionsService::OnGetFaviconFromCacheFinished( | 194 void ContentSuggestionsService::OnGetFaviconFromCacheFinished( |
172 const GURL& publisher_url, | 195 const GURL& publisher_url, |
173 int minimum_size_in_pixel, | 196 int minimum_size_in_pixel, |
174 int desired_size_in_pixel, | 197 int desired_size_in_pixel, |
175 const ImageFetchedCallback& callback, | 198 const ImageFetchedCallback& callback, |
176 bool continue_to_google_server, | 199 bool continue_to_google_server, |
177 const favicon_base::LargeIconImageResult& result) { | 200 const favicon_base::LargeIconImageResult& result) { |
178 if (!result.image.IsEmpty()) { | 201 if (!result.image.IsEmpty()) { |
Michael van Ouwerkerk
2017/04/12 13:20:31
Should there be an else clause to log FaviconFetch
jkrcal
2017/04/13 08:10:46
The failure is recorded on line 218 / line 240.
T
| |
179 callback.Run(result.image); | 202 callback.Run(result.image); |
203 // The icon is from cache if we haven't gone to Google server yet. The icon | |
204 // is freshly fetched, otherwise. | |
205 RecordFaviconFetchResult(continue_to_google_server | |
206 ? FaviconFetchResult::SUCCESS_CACHED | |
207 : FaviconFetchResult::SUCCESS_FETCHED); | |
180 return; | 208 return; |
181 } | 209 } |
182 | 210 |
183 if (!continue_to_google_server || | 211 if (!continue_to_google_server || |
184 (result.fallback_icon_style && | 212 (result.fallback_icon_style && |
185 !result.fallback_icon_style->is_default_background_color)) { | 213 !result.fallback_icon_style->is_default_background_color)) { |
186 // We cannot download from the server if there is some small icon in the | 214 // We cannot download from the server if there is some small icon in the |
187 // cache (resulting in non-default bakground color) or if we already did so. | 215 // cache (resulting in non-default background color) or if we already did |
216 // so. | |
188 callback.Run(gfx::Image()); | 217 callback.Run(gfx::Image()); |
218 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | |
189 return; | 219 return; |
190 } | 220 } |
191 | 221 |
192 // Try to fetch the favicon from a Google favicon server. | 222 // Try to fetch the favicon from a Google favicon server. |
193 large_icon_service_ | 223 large_icon_service_ |
194 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 224 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
195 publisher_url, minimum_size_in_pixel, | 225 publisher_url, minimum_size_in_pixel, |
196 base::Bind( | 226 base::Bind( |
197 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished, | 227 &ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished, |
198 base::Unretained(this), publisher_url, minimum_size_in_pixel, | 228 base::Unretained(this), publisher_url, minimum_size_in_pixel, |
199 desired_size_in_pixel, callback)); | 229 desired_size_in_pixel, callback)); |
200 } | 230 } |
201 | 231 |
202 void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished( | 232 void ContentSuggestionsService::OnGetFaviconFromGoogleServerFinished( |
203 const GURL& publisher_url, | 233 const GURL& publisher_url, |
204 int minimum_size_in_pixel, | 234 int minimum_size_in_pixel, |
205 int desired_size_in_pixel, | 235 int desired_size_in_pixel, |
206 const ImageFetchedCallback& callback, | 236 const ImageFetchedCallback& callback, |
207 bool success) { | 237 bool success) { |
208 if (!success) { | 238 if (!success) { |
209 callback.Run(gfx::Image()); | 239 callback.Run(gfx::Image()); |
240 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | |
210 return; | 241 return; |
211 } | 242 } |
212 | 243 |
213 // Get the freshly downloaded icon from the cache. | 244 // Get the freshly downloaded icon from the cache. |
214 large_icon_service_->GetLargeIconImageOrFallbackStyle( | 245 large_icon_service_->GetLargeIconImageOrFallbackStyle( |
215 publisher_url, minimum_size_in_pixel, desired_size_in_pixel, | 246 publisher_url, minimum_size_in_pixel, desired_size_in_pixel, |
216 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | 247 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, |
217 base::Unretained(this), publisher_url, minimum_size_in_pixel, | 248 base::Unretained(this), publisher_url, minimum_size_in_pixel, |
218 desired_size_in_pixel, callback, | 249 desired_size_in_pixel, callback, |
219 /*continue_to_google_server=*/false), | 250 /*continue_to_google_server=*/false), |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
621 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 652 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
622 base::ListValue list; | 653 base::ListValue list; |
623 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 654 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
624 list.AppendInteger(category_provider_pair.first.id()); | 655 list.AppendInteger(category_provider_pair.first.id()); |
625 } | 656 } |
626 | 657 |
627 pref_service_->Set(prefs::kDismissedCategories, list); | 658 pref_service_->Set(prefs::kDismissedCategories, list); |
628 } | 659 } |
629 | 660 |
630 } // namespace ntp_snippets | 661 } // namespace ntp_snippets |
OLD | NEW |