| 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 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 void WrapImageFetchedCallback( | 173 void WrapImageFetchedCallback( |
| 174 const NTPSnippetsService::ImageFetchedCallback& callback, | 174 const NTPSnippetsService::ImageFetchedCallback& callback, |
| 175 const GURL& snippet_id_url, | 175 const GURL& snippet_id_url, |
| 176 const SkBitmap* bitmap) { | 176 const SkBitmap* bitmap) { |
| 177 callback.Run(snippet_id_url.spec(), bitmap); | 177 callback.Run(snippet_id_url.spec(), bitmap); |
| 178 } | 178 } |
| 179 | 179 |
| 180 } // namespace | 180 } // namespace |
| 181 | 181 |
| 182 NTPSnippetsService::NTPSnippetsService( | 182 NTPSnippetsService::NTPSnippetsService( |
| 183 bool enabled, |
| 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, |
| 188 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 189 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 189 std::unique_ptr<ImageFetcher> image_fetcher) | 190 std::unique_ptr<ImageFetcher> image_fetcher) |
| 190 : state_(State::NOT_INITED), | 191 : state_(State::NOT_INITED), |
| 191 enabled_(false), | 192 enabled_(enabled), |
| 192 pref_service_(pref_service), | 193 pref_service_(pref_service), |
| 193 suggestions_service_(suggestions_service), | 194 suggestions_service_(suggestions_service), |
| 194 file_task_runner_(file_task_runner), | 195 file_task_runner_(file_task_runner), |
| 195 application_language_code_(application_language_code), | 196 application_language_code_(application_language_code), |
| 196 scheduler_(scheduler), | 197 scheduler_(scheduler), |
| 197 snippets_fetcher_(std::move(snippets_fetcher)), | 198 snippets_fetcher_(std::move(snippets_fetcher)), |
| 198 image_fetcher_(std::move(image_fetcher)) { | 199 image_fetcher_(std::move(image_fetcher)) { |
| 199 snippets_fetcher_->SetCallback(base::Bind( | 200 snippets_fetcher_->SetCallback(base::Bind( |
| 200 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); | 201 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); |
| 201 } | 202 } |
| 202 | 203 |
| 203 NTPSnippetsService::~NTPSnippetsService() { | 204 NTPSnippetsService::~NTPSnippetsService() { |
| 204 DCHECK(state_ == State::NOT_INITED || state_ == State::SHUT_DOWN); | 205 DCHECK(state_ == State::NOT_INITED || state_ == State::SHUT_DOWN); |
| 205 } | 206 } |
| 206 | 207 |
| 207 // static | 208 // static |
| 208 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 209 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 209 registry->RegisterListPref(prefs::kSnippets); | 210 registry->RegisterListPref(prefs::kSnippets); |
| 210 registry->RegisterListPref(prefs::kDiscardedSnippets); | 211 registry->RegisterListPref(prefs::kDiscardedSnippets); |
| 211 registry->RegisterListPref(prefs::kSnippetHosts); | 212 registry->RegisterListPref(prefs::kSnippetHosts); |
| 212 } | 213 } |
| 213 | 214 |
| 214 void NTPSnippetsService::Init(bool enabled) { | 215 void NTPSnippetsService::Init() { |
| 215 DCHECK(state_ == State::NOT_INITED); | 216 DCHECK(state_ == State::NOT_INITED); |
| 216 state_ = State::INITED; | 217 state_ = State::INITED; |
| 217 | 218 |
| 218 enabled_ = enabled; | |
| 219 if (enabled_) { | 219 if (enabled_) { |
| 220 // |suggestions_service_| can be null in tests. | 220 // |suggestions_service_| can be null in tests. |
| 221 if (snippets_fetcher_->UseHostRestriction() && suggestions_service_) { | 221 if (snippets_fetcher_->UseHostRestriction() && suggestions_service_) { |
| 222 suggestions_service_subscription_ = suggestions_service_->AddCallback( | 222 suggestions_service_subscription_ = suggestions_service_->AddCallback( |
| 223 base::Bind(&NTPSnippetsService::OnSuggestionsChanged, | 223 base::Bind(&NTPSnippetsService::OnSuggestionsChanged, |
| 224 base::Unretained(this))); | 224 base::Unretained(this))); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Get any existing snippets immediately from prefs. | 227 // Get any existing snippets immediately from prefs. |
| 228 LoadDiscardedSnippetsFromPrefs(); | 228 LoadDiscardedSnippetsFromPrefs(); |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 if (snippet->expiry_date() < next_expiry) | 542 if (snippet->expiry_date() < next_expiry) |
| 543 next_expiry = snippet->expiry_date(); | 543 next_expiry = snippet->expiry_date(); |
| 544 } | 544 } |
| 545 DCHECK_GT(next_expiry, expiry); | 545 DCHECK_GT(next_expiry, expiry); |
| 546 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 546 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 547 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 547 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 548 base::Unretained(this))); | 548 base::Unretained(this))); |
| 549 } | 549 } |
| 550 | 550 |
| 551 } // namespace ntp_snippets | 551 } // namespace ntp_snippets |
| OLD | NEW |