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> |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 int desired_size_in_pixel, | 163 int desired_size_in_pixel, |
| 164 const ImageFetchedCallback& callback) { | 164 const ImageFetchedCallback& callback) { |
| 165 const GURL& domain_with_favicon = GetFaviconDomain(suggestion_id); | 165 const GURL& domain_with_favicon = GetFaviconDomain(suggestion_id); |
| 166 if (!domain_with_favicon.is_valid() || !large_icon_service_) { | 166 if (!domain_with_favicon.is_valid() || !large_icon_service_) { |
| 167 base::ThreadTaskRunnerHandle::Get()->PostTask( | 167 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 168 FROM_HERE, base::Bind(callback, gfx::Image())); | 168 FROM_HERE, base::Bind(callback, gfx::Image())); |
| 169 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | 169 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); |
| 170 return; | 170 return; |
| 171 } | 171 } |
| 172 | 172 |
| 173 // TODO(jkrcal): Create a general wrapper function in LargeIconService that | 173 GetFaviconFromCache(domain_with_favicon, minimum_size_in_pixel, |
| 174 // does handle the get-from-cache-and-fallback-to-google-server functionality | 174 desired_size_in_pixel, callback, |
| 175 // in one shot (for all clients that do not need to react in between). | 175 /*continue_to_google_server=*/true); |
| 176 large_icon_service_->GetLargeIconImageOrFallbackStyle( | |
| 177 domain_with_favicon, minimum_size_in_pixel, desired_size_in_pixel, | |
| 178 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | |
| 179 base::Unretained(this), domain_with_favicon, | |
| 180 minimum_size_in_pixel, desired_size_in_pixel, callback, | |
| 181 /*continue_to_google_server=*/true), | |
| 182 &favicons_task_tracker_); | |
| 183 } | 176 } |
| 184 | 177 |
| 185 GURL ContentSuggestionsService::GetFaviconDomain( | 178 GURL ContentSuggestionsService::GetFaviconDomain( |
| 186 const ContentSuggestion::ID& suggestion_id) { | 179 const ContentSuggestion::ID& suggestion_id) { |
| 187 const std::vector<ContentSuggestion>& suggestions = | 180 const std::vector<ContentSuggestion>& suggestions = |
| 188 suggestions_by_category_[suggestion_id.category()]; | 181 suggestions_by_category_[suggestion_id.category()]; |
| 189 auto position = | 182 auto position = |
| 190 std::find_if(suggestions.begin(), suggestions.end(), | 183 std::find_if(suggestions.begin(), suggestions.end(), |
| 191 [&suggestion_id](const ContentSuggestion& suggestion) { | 184 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 192 return suggestion_id == suggestion.id(); | 185 return suggestion_id == suggestion.id(); |
| 193 }); | 186 }); |
| 194 if (position != suggestions.end()) { | 187 if (position != suggestions.end()) { |
| 195 return position->url_with_favicon(); | 188 return position->url_with_favicon(); |
| 196 } | 189 } |
| 197 | 190 |
| 198 // Look up the URL in the archive of |remote_suggestions_provider_|. | 191 // Look up the URL in the archive of |remote_suggestions_provider_|. |
| 199 // TODO(jkrcal): Fix how Fetch more works or find other ways to remove this | 192 // TODO(jkrcal): Fix how Fetch more works or find other ways to remove this |
| 200 // hack. crbug.com/714031 | 193 // hack. crbug.com/714031 |
| 201 if (providers_by_category_[suggestion_id.category()] == | 194 if (providers_by_category_[suggestion_id.category()] == |
| 202 remote_suggestions_provider_) { | 195 remote_suggestions_provider_) { |
| 203 return remote_suggestions_provider_->GetUrlWithFavicon(suggestion_id); | 196 return remote_suggestions_provider_->GetUrlWithFavicon(suggestion_id); |
| 204 } | 197 } |
| 205 return GURL(); | 198 return GURL(); |
| 206 } | 199 } |
| 207 | 200 |
| 201 void ContentSuggestionsService::GetFaviconFromCache( | |
| 202 const GURL& publisher_url, | |
| 203 int minimum_size_in_pixel, | |
| 204 int desired_size_in_pixel, | |
| 205 const ImageFetchedCallback& callback, | |
| 206 bool continue_to_google_server) { | |
| 207 // TODO(jkrcal): Create a general wrapper function in LargeIconService that | |
|
fhorschig
2017/05/19 08:17:45
Isn't that what you did? Why is this TODO still he
jkrcal
2017/05/19 11:36:39
No, this TODO means something different (basically
| |
| 208 // does handle the get-from-cache-and-fallback-to-google-server functionality | |
| 209 // in one shot (for all clients that do not need to react in between). | |
| 210 large_icon_service_->GetLargeIconImageOrFallbackStyle( | |
| 211 publisher_url, minimum_size_in_pixel, desired_size_in_pixel, | |
| 212 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | |
| 213 base::Unretained(this), publisher_url, minimum_size_in_pixel, | |
| 214 /*desired_size_in_pixel=*/0, callback, | |
| 215 continue_to_google_server), | |
| 216 &favicons_task_tracker_); | |
| 217 } | |
| 218 | |
| 208 void ContentSuggestionsService::OnGetFaviconFromCacheFinished( | 219 void ContentSuggestionsService::OnGetFaviconFromCacheFinished( |
| 209 const GURL& publisher_url, | 220 const GURL& publisher_url, |
| 210 int minimum_size_in_pixel, | 221 int minimum_size_in_pixel, |
| 211 int desired_size_in_pixel, | 222 int desired_size_in_pixel, |
| 212 const ImageFetchedCallback& callback, | 223 const ImageFetchedCallback& callback, |
| 213 bool continue_to_google_server, | 224 bool continue_to_google_server, |
| 214 const favicon_base::LargeIconImageResult& result) { | 225 const favicon_base::LargeIconImageResult& result) { |
| 215 if (!result.image.IsEmpty()) { | 226 if (!result.image.IsEmpty()) { |
| 216 callback.Run(result.image); | 227 callback.Run(result.image); |
| 217 // The icon is from cache if we haven't gone to Google server yet. The icon | 228 // The icon is from cache if we haven't gone to Google server yet. The icon |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 248 int minimum_size_in_pixel, | 259 int minimum_size_in_pixel, |
| 249 int desired_size_in_pixel, | 260 int desired_size_in_pixel, |
| 250 const ImageFetchedCallback& callback, | 261 const ImageFetchedCallback& callback, |
| 251 bool success) { | 262 bool success) { |
| 252 if (!success) { | 263 if (!success) { |
| 253 callback.Run(gfx::Image()); | 264 callback.Run(gfx::Image()); |
| 254 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); | 265 RecordFaviconFetchResult(FaviconFetchResult::FAILURE); |
| 255 return; | 266 return; |
| 256 } | 267 } |
| 257 | 268 |
| 258 // Get the freshly downloaded icon from the cache. | 269 GetFaviconFromCache(publisher_url, minimum_size_in_pixel, |
| 259 large_icon_service_->GetLargeIconImageOrFallbackStyle( | 270 desired_size_in_pixel, callback, |
| 260 publisher_url, minimum_size_in_pixel, desired_size_in_pixel, | 271 /*continue_to_google_server=*/false); |
| 261 base::Bind(&ContentSuggestionsService::OnGetFaviconFromCacheFinished, | |
| 262 base::Unretained(this), publisher_url, minimum_size_in_pixel, | |
| 263 desired_size_in_pixel, callback, | |
| 264 /*continue_to_google_server=*/false), | |
| 265 &favicons_task_tracker_); | |
| 266 } | 272 } |
| 267 | 273 |
| 268 void ContentSuggestionsService::ClearHistory( | 274 void ContentSuggestionsService::ClearHistory( |
| 269 base::Time begin, | 275 base::Time begin, |
| 270 base::Time end, | 276 base::Time end, |
| 271 const base::Callback<bool(const GURL& url)>& filter) { | 277 const base::Callback<bool(const GURL& url)>& filter) { |
| 272 for (const auto& provider : providers_) { | 278 for (const auto& provider : providers_) { |
| 273 provider->ClearHistory(begin, end, filter); | 279 provider->ClearHistory(begin, end, filter); |
| 274 } | 280 } |
| 275 category_ranker_->ClearHistory(begin, end); | 281 category_ranker_->ClearHistory(begin, end); |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 673 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 679 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 674 base::ListValue list; | 680 base::ListValue list; |
| 675 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 681 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 676 list.AppendInteger(category_provider_pair.first.id()); | 682 list.AppendInteger(category_provider_pair.first.id()); |
| 677 } | 683 } |
| 678 | 684 |
| 679 pref_service_->Set(prefs::kDismissedCategories, list); | 685 pref_service_->Set(prefs::kDismissedCategories, list); |
| 680 } | 686 } |
| 681 | 687 |
| 682 } // namespace ntp_snippets | 688 } // namespace ntp_snippets |
| OLD | NEW |