Chromium Code Reviews| 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) { |
| 164 GURL domain_with_favicon; | |
| 163 std::vector<ContentSuggestion>* suggestions = | 165 std::vector<ContentSuggestion>* suggestions = |
| 164 &suggestions_by_category_[suggestion_id.category()]; | 166 &suggestions_by_category_[suggestion_id.category()]; |
| 165 auto position = | 167 auto position = |
| 166 std::find_if(suggestions->begin(), suggestions->end(), | 168 std::find_if(suggestions->begin(), suggestions->end(), |
| 167 [&suggestion_id](const ContentSuggestion& suggestion) { | 169 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 168 return suggestion_id == suggestion.id(); | 170 return suggestion_id == suggestion.id(); |
| 169 }); | 171 }); |
| 170 if (position == suggestions->end() || !large_icon_service_) { | 172 if (position != suggestions->end()) { |
|
tschumann
2017/04/21 08:27:55
nit: this is a bit tricky to follow (especially wi
jkrcal
2017/04/21 10:57:01
Done.
| |
| 173 domain_with_favicon = position->url_with_favicon(); | |
| 174 } else if (remote_suggestions_provider_ && | |
| 175 suggestion_id.category().IsKnownCategory( | |
| 176 KnownCategories::ARTICLES)) { | |
|
tschumann
2017/04/21 08:27:54
unknown remote suggestions should also be tackled
jkrcal
2017/04/21 10:57:00
Due to privacy, we call the new code path from jav
tschumann
2017/04/21 11:23:58
I would prefer we only have a single place where w
jkrcal
2017/04/21 13:30:54
Okay, done.
| |
| 177 // TODO(jkrcal): Fix how Fetch more works or find other ways to remove this | |
| 178 // hack. crbug.com/714031 | |
| 179 domain_with_favicon = | |
| 180 remote_suggestions_provider_->GetUrlWithFavicon(suggestion_id); | |
| 181 } | |
| 182 | |
| 183 if (!domain_with_favicon.is_valid() || !large_icon_service_) { | |
| 171 base::ThreadTaskRunnerHandle::Get()->PostTask( | 184 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 172 FROM_HERE, base::Bind(callback, gfx::Image())); | 185 FROM_HERE, base::Bind(callback, gfx::Image())); |
| 173 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | 186 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); |
| 174 return; | 187 return; |
| 175 } | 188 } |
| 176 | 189 |
| 177 const GURL& domain_with_favicon = position->url_with_favicon(); | |
| 178 | |
| 179 // TODO(jkrcal): Create a general wrapper function in LargeIconService that | 190 // TODO(jkrcal): Create a general wrapper function in LargeIconService that |
| 180 // does handle the get-from-cache-and-fallback-to-google-server functionality | 191 // does handle the get-from-cache-and-fallback-to-google-server functionality |
| 181 // in one shot (for all clients that do not need to react in between). | 192 // in one shot (for all clients that do not need to react in between). |
| 182 large_icon_service_->GetLargeIconImageOrFallbackStyle( | 193 large_icon_service_->GetLargeIconImageOrFallbackStyle( |
| 183 domain_with_favicon, minimum_size_in_pixel, desired_size_in_pixel, | 194 domain_with_favicon, minimum_size_in_pixel, desired_size_in_pixel, |
| 184 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | 195 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, |
| 185 base::Unretained(this), domain_with_favicon, | 196 base::Unretained(this), domain_with_favicon, |
| 186 minimum_size_in_pixel, desired_size_in_pixel, callback, | 197 minimum_size_in_pixel, desired_size_in_pixel, callback, |
| 187 /*continue_to_google_server=*/true), | 198 /*continue_to_google_server=*/true), |
| 188 &favicons_task_tracker_); | 199 &favicons_task_tracker_); |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 649 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 660 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 650 base::ListValue list; | 661 base::ListValue list; |
| 651 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 662 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 652 list.AppendInteger(category_provider_pair.first.id()); | 663 list.AppendInteger(category_provider_pair.first.id()); |
| 653 } | 664 } |
| 654 | 665 |
| 655 pref_service_->Set(prefs::kDismissedCategories, list); | 666 pref_service_->Set(prefs::kDismissedCategories, list); |
| 656 } | 667 } |
| 657 | 668 |
| 658 } // namespace ntp_snippets | 669 } // namespace ntp_snippets |
| OLD | NEW |