| 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/metrics/histogram_macros.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "base/time/default_clock.h" | 18 #include "base/time/default_clock.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "components/favicon/core/large_icon_service.h" | 20 #include "components/favicon/core/large_icon_service.h" |
| 21 #include "components/favicon_base/fallback_icon_style.h" | 21 #include "components/favicon_base/fallback_icon_style.h" |
| 22 #include "components/favicon_base/favicon_types.h" | 22 #include "components/favicon_base/favicon_types.h" |
| 23 #include "components/ntp_snippets/pref_names.h" | 23 #include "components/ntp_snippets/pref_names.h" |
| 24 #include "components/ntp_snippets/remote/remote_suggestions_provider.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 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Enumeration listing all possible outcomes for fetch attempts of favicons for | 33 // Enumeration listing all possible outcomes for fetch attempts of favicons for |
| 33 // content suggestions. Used for UMA histograms, so do not change existing | 34 // content suggestions. Used for UMA histograms, so do not change existing |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage( | 154 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage( |
| 154 suggestion_id, callback); | 155 suggestion_id, callback); |
| 155 } | 156 } |
| 156 | 157 |
| 157 // TODO(jkrcal): Split the favicon fetching into a separate class. | 158 // TODO(jkrcal): Split the favicon fetching into a separate class. |
| 158 void ContentSuggestionsService::FetchSuggestionFavicon( | 159 void ContentSuggestionsService::FetchSuggestionFavicon( |
| 159 const ContentSuggestion::ID& suggestion_id, | 160 const ContentSuggestion::ID& suggestion_id, |
| 160 int minimum_size_in_pixel, | 161 int minimum_size_in_pixel, |
| 161 int desired_size_in_pixel, | 162 int desired_size_in_pixel, |
| 162 const ImageFetchedCallback& callback) { | 163 const ImageFetchedCallback& callback) { |
| 163 std::vector<ContentSuggestion>* suggestions = | 164 const GURL& domain_with_favicon = GetFaviconDomain(suggestion_id); |
| 164 &suggestions_by_category_[suggestion_id.category()]; | 165 if (!domain_with_favicon.is_valid() || !large_icon_service_) { |
| 165 auto position = | |
| 166 std::find_if(suggestions->begin(), suggestions->end(), | |
| 167 [&suggestion_id](const ContentSuggestion& suggestion) { | |
| 168 return suggestion_id == suggestion.id(); | |
| 169 }); | |
| 170 if (position == suggestions->end() || !large_icon_service_) { | |
| 171 base::ThreadTaskRunnerHandle::Get()->PostTask( | 166 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 172 FROM_HERE, base::Bind(callback, gfx::Image())); | 167 FROM_HERE, base::Bind(callback, gfx::Image())); |
| 173 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | 168 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); |
| 174 return; | 169 return; |
| 175 } | 170 } |
| 176 | 171 |
| 177 const GURL& domain_with_favicon = | |
| 178 position->url_with_favicon().GetWithEmptyPath(); | |
| 179 | |
| 180 // TODO(jkrcal): Create a general wrapper function in LargeIconService that | 172 // TODO(jkrcal): Create a general wrapper function in LargeIconService that |
| 181 // does handle the get-from-cache-and-fallback-to-google-server functionality | 173 // does handle the get-from-cache-and-fallback-to-google-server functionality |
| 182 // in one shot (for all clients that do not need to react in between). | 174 // in one shot (for all clients that do not need to react in between). |
| 183 large_icon_service_->GetLargeIconImageOrFallbackStyle( | 175 large_icon_service_->GetLargeIconImageOrFallbackStyle( |
| 184 domain_with_favicon, minimum_size_in_pixel, desired_size_in_pixel, | 176 domain_with_favicon, minimum_size_in_pixel, desired_size_in_pixel, |
| 185 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | 177 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, |
| 186 base::Unretained(this), domain_with_favicon, | 178 base::Unretained(this), domain_with_favicon, |
| 187 minimum_size_in_pixel, desired_size_in_pixel, callback, | 179 minimum_size_in_pixel, desired_size_in_pixel, callback, |
| 188 /*continue_to_google_server=*/true), | 180 /*continue_to_google_server=*/true), |
| 189 &favicons_task_tracker_); | 181 &favicons_task_tracker_); |
| 190 } | 182 } |
| 191 | 183 |
| 184 GURL ContentSuggestionsService::GetFaviconDomain( |
| 185 const ContentSuggestion::ID& suggestion_id) { |
| 186 const std::vector<ContentSuggestion>& suggestions = |
| 187 suggestions_by_category_[suggestion_id.category()]; |
| 188 auto position = |
| 189 std::find_if(suggestions.begin(), suggestions.end(), |
| 190 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 191 return suggestion_id == suggestion.id(); |
| 192 }); |
| 193 if (position != suggestions.end()) { |
| 194 return position->url_with_favicon(); |
| 195 } |
| 196 |
| 197 // Look up the URL in the archive of |remote_suggestions_provider_|. |
| 198 // TODO(jkrcal): Fix how Fetch more works or find other ways to remove this |
| 199 // hack. crbug.com/714031 |
| 200 if (providers_by_category_[suggestion_id.category()] == |
| 201 remote_suggestions_provider_) { |
| 202 return remote_suggestions_provider_->GetUrlWithFavicon(suggestion_id); |
| 203 } |
| 204 return GURL(); |
| 205 } |
| 206 |
| 192 void ContentSuggestionsService::OnGetFaviconFromCacheFinished( | 207 void ContentSuggestionsService::OnGetFaviconFromCacheFinished( |
| 193 const GURL& publisher_url, | 208 const GURL& publisher_url, |
| 194 int minimum_size_in_pixel, | 209 int minimum_size_in_pixel, |
| 195 int desired_size_in_pixel, | 210 int desired_size_in_pixel, |
| 196 const ImageFetchedCallback& callback, | 211 const ImageFetchedCallback& callback, |
| 197 bool continue_to_google_server, | 212 bool continue_to_google_server, |
| 198 const favicon_base::LargeIconImageResult& result) { | 213 const favicon_base::LargeIconImageResult& result) { |
| 199 if (!result.image.IsEmpty()) { | 214 if (!result.image.IsEmpty()) { |
| 200 callback.Run(result.image); | 215 callback.Run(result.image); |
| 201 // The icon is from cache if we haven't gone to Google server yet. The icon | 216 // The icon is from cache if we haven't gone to Google server yet. The icon |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 665 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 651 base::ListValue list; | 666 base::ListValue list; |
| 652 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 667 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 653 list.AppendInteger(category_provider_pair.first.id()); | 668 list.AppendInteger(category_provider_pair.first.id()); |
| 654 } | 669 } |
| 655 | 670 |
| 656 pref_service_->Set(prefs::kDismissedCategories, list); | 671 pref_service_->Set(prefs::kDismissedCategories, list); |
| 657 } | 672 } |
| 658 | 673 |
| 659 } // namespace ntp_snippets | 674 } // namespace ntp_snippets |
| OLD | NEW |