| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "components/image_fetcher/image_fetcher.h" | 22 #include "components/image_fetcher/image_fetcher.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_constants.h" | 23 #include "components/ntp_snippets/ntp_snippets_constants.h" |
| 24 #include "components/ntp_snippets/pref_names.h" | 24 #include "components/ntp_snippets/pref_names.h" |
| 25 #include "components/ntp_snippets/switches.h" | 25 #include "components/ntp_snippets/switches.h" |
| 26 #include "components/prefs/pref_registry_simple.h" | 26 #include "components/prefs/pref_registry_simple.h" |
| 27 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 28 #include "components/suggestions/proto/suggestions.pb.h" | 28 #include "components/suggestions/proto/suggestions.pb.h" |
| 29 #include "components/variations/variations_associated_data.h" | 29 #include "components/variations/variations_associated_data.h" |
| 30 #include "ui/gfx/image/image.h" |
| 30 | 31 |
| 31 using image_fetcher::ImageFetcher; | 32 using image_fetcher::ImageFetcher; |
| 32 using suggestions::ChromeSuggestion; | 33 using suggestions::ChromeSuggestion; |
| 33 using suggestions::SuggestionsProfile; | 34 using suggestions::SuggestionsProfile; |
| 34 using suggestions::SuggestionsService; | 35 using suggestions::SuggestionsService; |
| 35 | 36 |
| 36 namespace ntp_snippets { | 37 namespace ntp_snippets { |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 const std::string& id = needle->id(); | 167 const std::string& id = needle->id(); |
| 167 return std::find_if(haystack.begin(), haystack.end(), | 168 return std::find_if(haystack.begin(), haystack.end(), |
| 168 [&id](const std::unique_ptr<NTPSnippet>& snippet) { | 169 [&id](const std::unique_ptr<NTPSnippet>& snippet) { |
| 169 return snippet->id() == id; | 170 return snippet->id() == id; |
| 170 }) != haystack.end(); | 171 }) != haystack.end(); |
| 171 } | 172 } |
| 172 | 173 |
| 173 void WrapImageFetchedCallback( | 174 void WrapImageFetchedCallback( |
| 174 const NTPSnippetsService::ImageFetchedCallback& callback, | 175 const NTPSnippetsService::ImageFetchedCallback& callback, |
| 175 const GURL& snippet_id_url, | 176 const GURL& snippet_id_url, |
| 176 const SkBitmap* bitmap) { | 177 const gfx::Image& image) { |
| 177 callback.Run(snippet_id_url.spec(), bitmap); | 178 callback.Run(snippet_id_url.spec(), image); |
| 178 } | 179 } |
| 179 | 180 |
| 180 } // namespace | 181 } // namespace |
| 181 | 182 |
| 182 NTPSnippetsService::NTPSnippetsService( | 183 NTPSnippetsService::NTPSnippetsService( |
| 183 PrefService* pref_service, | 184 PrefService* pref_service, |
| 184 SuggestionsService* suggestions_service, | 185 SuggestionsService* suggestions_service, |
| 185 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 186 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 186 const std::string& application_language_code, | 187 const std::string& application_language_code, |
| 187 NTPSnippetsScheduler* scheduler, | 188 NTPSnippetsScheduler* scheduler, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 274 |
| 274 void NTPSnippetsService::FetchSnippetImage( | 275 void NTPSnippetsService::FetchSnippetImage( |
| 275 const std::string& snippet_id, | 276 const std::string& snippet_id, |
| 276 const ImageFetchedCallback& callback) { | 277 const ImageFetchedCallback& callback) { |
| 277 auto it = | 278 auto it = |
| 278 std::find_if(snippets_.begin(), snippets_.end(), | 279 std::find_if(snippets_.begin(), snippets_.end(), |
| 279 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { | 280 [&snippet_id](const std::unique_ptr<NTPSnippet>& snippet) { |
| 280 return snippet->id() == snippet_id; | 281 return snippet->id() == snippet_id; |
| 281 }); | 282 }); |
| 282 if (it == snippets_.end()) { | 283 if (it == snippets_.end()) { |
| 283 callback.Run(snippet_id, nullptr); | 284 gfx::Image empty_image; |
| 285 callback.Run(snippet_id, empty_image); |
| 284 return; | 286 return; |
| 285 } | 287 } |
| 286 | 288 |
| 287 const NTPSnippet& snippet = *it->get(); | 289 const NTPSnippet& snippet = *it->get(); |
| 288 // TODO(treib): Make ImageFetcher take a string instead of a GURL as an | 290 // TODO(treib): Make ImageFetcher take a string instead of a GURL as an |
| 289 // identifier. | 291 // identifier. |
| 290 image_fetcher_->StartOrQueueNetworkRequest( | 292 image_fetcher_->StartOrQueueNetworkRequest( |
| 291 GURL(snippet.id()), snippet.salient_image_url(), | 293 GURL(snippet.id()), snippet.salient_image_url(), |
| 292 base::Bind(WrapImageFetchedCallback, callback)); | 294 base::Bind(WrapImageFetchedCallback, callback)); |
| 293 // TODO(treib): Cache/persist the snippet image. | 295 // TODO(treib): Cache/persist the snippet image. |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 if (snippet->expiry_date() < next_expiry) | 536 if (snippet->expiry_date() < next_expiry) |
| 535 next_expiry = snippet->expiry_date(); | 537 next_expiry = snippet->expiry_date(); |
| 536 } | 538 } |
| 537 DCHECK_GT(next_expiry, expiry); | 539 DCHECK_GT(next_expiry, expiry); |
| 538 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 540 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 539 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 541 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 540 base::Unretained(this))); | 542 base::Unretained(this))); |
| 541 } | 543 } |
| 542 | 544 |
| 543 } // namespace ntp_snippets | 545 } // namespace ntp_snippets |
| OLD | NEW |