| 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | 119 for (int i = 0; i < suggestions.suggestions_size(); ++i) { |
| 120 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | 120 const ChromeSuggestion& suggestion = suggestions.suggestions(i); |
| 121 GURL url(suggestion.url()); | 121 GURL url(suggestion.url()); |
| 122 if (url.is_valid()) | 122 if (url.is_valid()) |
| 123 hosts.insert(url.host()); | 123 hosts.insert(url.host()); |
| 124 } | 124 } |
| 125 return hosts; | 125 return hosts; |
| 126 } | 126 } |
| 127 | 127 |
| 128 std::unique_ptr<base::ListValue> SnippetsToListValue( | 128 std::unique_ptr<base::ListValue> SnippetsToListValue( |
| 129 const NTPSnippetsService::NTPSnippetStorage& snippets) { | 129 const NTPSnippet::PtrVector& snippets) { |
| 130 std::unique_ptr<base::ListValue> list(new base::ListValue); | 130 std::unique_ptr<base::ListValue> list(new base::ListValue); |
| 131 for (const auto& snippet : snippets) { | 131 for (const auto& snippet : snippets) { |
| 132 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary(); | 132 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary(); |
| 133 list->Append(std::move(dict)); | 133 list->Append(std::move(dict)); |
| 134 } | 134 } |
| 135 return list; | 135 return list; |
| 136 } | 136 } |
| 137 | 137 |
| 138 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack, | 138 bool ContainsSnippet(const NTPSnippet::PtrVector& haystack, |
| 139 const std::unique_ptr<NTPSnippet>& needle) { | 139 const std::unique_ptr<NTPSnippet>& needle) { |
| 140 const std::string& id = needle->id(); | 140 const std::string& id = needle->id(); |
| 141 return std::find_if(haystack.begin(), haystack.end(), | 141 return std::find_if(haystack.begin(), haystack.end(), |
| 142 [&id](const std::unique_ptr<NTPSnippet>& snippet) { | 142 [&id](const std::unique_ptr<NTPSnippet>& snippet) { |
| 143 return snippet->id() == id; | 143 return snippet->id() == id; |
| 144 }) != haystack.end(); | 144 }) != haystack.end(); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void WrapImageFetchedCallback( | 147 void WrapImageFetchedCallback( |
| 148 const NTPSnippetsService::ImageFetchedCallback& callback, | 148 const NTPSnippetsService::ImageFetchedCallback& callback, |
| 149 const GURL& snippet_id_url, | 149 const GURL& snippet_id_url, |
| 150 const SkBitmap* bitmap) { | 150 const SkBitmap* bitmap) { |
| 151 callback.Run(snippet_id_url.spec(), bitmap); | 151 callback.Run(snippet_id_url.spec(), bitmap); |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace | 154 } // namespace |
| 155 | 155 |
| 156 NTPSnippetsService::NTPSnippetsService( | 156 NTPSnippetsService::NTPSnippetsService( |
| 157 PrefService* pref_service, | 157 PrefService* pref_service, |
| 158 SuggestionsService* suggestions_service, | 158 SuggestionsService* suggestions_service, |
| 159 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 159 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 160 const std::string& application_language_code, | 160 const std::string& application_language_code, |
| 161 NTPSnippetsScheduler* scheduler, | 161 NTPSnippetsScheduler* scheduler, |
| 162 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 162 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 163 std::unique_ptr<ImageFetcher> image_fetcher) | 163 std::unique_ptr<ImageFetcher> image_fetcher) |
| 164 : enabled_(false), | 164 : state_(State::NOT_INITED), |
| 165 enabled_(false), |
| 165 pref_service_(pref_service), | 166 pref_service_(pref_service), |
| 166 suggestions_service_(suggestions_service), | 167 suggestions_service_(suggestions_service), |
| 167 file_task_runner_(file_task_runner), | 168 file_task_runner_(file_task_runner), |
| 168 application_language_code_(application_language_code), | 169 application_language_code_(application_language_code), |
| 169 scheduler_(scheduler), | 170 scheduler_(scheduler), |
| 170 snippets_fetcher_(std::move(snippets_fetcher)), | 171 snippets_fetcher_(std::move(snippets_fetcher)), |
| 171 image_fetcher_(std::move(image_fetcher)) { | 172 image_fetcher_(std::move(image_fetcher)) { |
| 172 snippets_fetcher_->SetCallback(base::Bind( | 173 snippets_fetcher_->SetCallback(base::Bind( |
| 173 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); | 174 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); |
| 174 } | 175 } |
| 175 | 176 |
| 176 NTPSnippetsService::~NTPSnippetsService() {} | 177 NTPSnippetsService::~NTPSnippetsService() { |
| 178 DCHECK(state_ == State::NOT_INITED || state_ == State::SHUT_DOWN); |
| 179 } |
| 177 | 180 |
| 178 // static | 181 // static |
| 179 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 182 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 180 registry->RegisterListPref(prefs::kSnippets); | 183 registry->RegisterListPref(prefs::kSnippets); |
| 181 registry->RegisterListPref(prefs::kDiscardedSnippets); | 184 registry->RegisterListPref(prefs::kDiscardedSnippets); |
| 182 registry->RegisterListPref(prefs::kSnippetHosts); | 185 registry->RegisterListPref(prefs::kSnippetHosts); |
| 183 } | 186 } |
| 184 | 187 |
| 185 void NTPSnippetsService::Init(bool enabled) { | 188 void NTPSnippetsService::Init(bool enabled) { |
| 189 DCHECK(state_ == State::NOT_INITED); |
| 190 state_ = State::INITED; |
| 191 |
| 186 enabled_ = enabled; | 192 enabled_ = enabled; |
| 187 if (enabled_) { | 193 if (enabled_) { |
| 188 // |suggestions_service_| can be null in tests. | 194 // |suggestions_service_| can be null in tests. |
| 189 if (suggestions_service_) { | 195 if (suggestions_service_) { |
| 190 suggestions_service_subscription_ = suggestions_service_->AddCallback( | 196 suggestions_service_subscription_ = suggestions_service_->AddCallback( |
| 191 base::Bind(&NTPSnippetsService::OnSuggestionsChanged, | 197 base::Bind(&NTPSnippetsService::OnSuggestionsChanged, |
| 192 base::Unretained(this))); | 198 base::Unretained(this))); |
| 193 } | 199 } |
| 194 | 200 |
| 195 // Get any existing snippets immediately from prefs. | 201 // Get any existing snippets immediately from prefs. |
| 196 LoadDiscardedSnippetsFromPrefs(); | 202 LoadDiscardedSnippetsFromPrefs(); |
| 197 LoadSnippetsFromPrefs(); | 203 LoadSnippetsFromPrefs(); |
| 198 | 204 |
| 199 // If we don't have any snippets yet, start a fetch. | 205 // If we don't have any snippets yet, start a fetch. |
| 200 if (snippets_.empty()) | 206 if (snippets_.empty()) |
| 201 FetchSnippets(); | 207 FetchSnippets(); |
| 202 } | 208 } |
| 203 | 209 |
| 204 RescheduleFetching(); | 210 RescheduleFetching(); |
| 205 } | 211 } |
| 206 | 212 |
| 207 void NTPSnippetsService::Shutdown() { | 213 void NTPSnippetsService::Shutdown() { |
| 214 DCHECK(state_ == State::INITED); |
| 215 state_ = State::SHUT_DOWN; |
| 216 |
| 208 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 217 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 209 NTPSnippetsServiceShutdown()); | 218 NTPSnippetsServiceShutdown()); |
| 219 suggestions_service_subscription_.reset(); |
| 210 enabled_ = false; | 220 enabled_ = false; |
| 211 } | 221 } |
| 212 | 222 |
| 213 void NTPSnippetsService::FetchSnippets() { | 223 void NTPSnippetsService::FetchSnippets() { |
| 214 FetchSnippetsFromHosts(GetSuggestionsHosts()); | 224 FetchSnippetsFromHosts(GetSuggestionsHosts()); |
| 215 } | 225 } |
| 216 | 226 |
| 217 void NTPSnippetsService::FetchSnippetsFromHosts( | 227 void NTPSnippetsService::FetchSnippetsFromHosts( |
| 218 const std::set<std::string>& hosts) { | 228 const std::set<std::string>& hosts) { |
| 219 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, | 229 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 // Sparse histogram used because the number of snippets is small (bound by | 358 // Sparse histogram used because the number of snippets is small (bound by |
| 349 // kMaxSnippetCount). | 359 // kMaxSnippetCount). |
| 350 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 360 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 351 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 361 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 352 snippets->size()); | 362 snippets->size()); |
| 353 MergeSnippets(std::move(*snippets)); | 363 MergeSnippets(std::move(*snippets)); |
| 354 } | 364 } |
| 355 LoadingSnippetsFinished(); | 365 LoadingSnippetsFinished(); |
| 356 } | 366 } |
| 357 | 367 |
| 358 void NTPSnippetsService::MergeSnippets(NTPSnippetStorage new_snippets) { | 368 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
| 359 // Remove new snippets that we already have, or that have been discarded. | 369 // Remove new snippets that we already have, or that have been discarded. |
| 360 new_snippets.erase( | 370 new_snippets.erase( |
| 361 std::remove_if(new_snippets.begin(), new_snippets.end(), | 371 std::remove_if(new_snippets.begin(), new_snippets.end(), |
| 362 [this](const std::unique_ptr<NTPSnippet>& snippet) { | 372 [this](const std::unique_ptr<NTPSnippet>& snippet) { |
| 363 return ContainsSnippet(discarded_snippets_, snippet) || | 373 return ContainsSnippet(discarded_snippets_, snippet) || |
| 364 ContainsSnippet(snippets_, snippet); | 374 ContainsSnippet(snippets_, snippet); |
| 365 }), | 375 }), |
| 366 new_snippets.end()); | 376 new_snippets.end()); |
| 367 | 377 |
| 368 // Fill in default publish/expiry dates where required. | 378 // Fill in default publish/expiry dates where required. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 398 } | 408 } |
| 399 } | 409 } |
| 400 | 410 |
| 401 // Insert the new snippets at the front. | 411 // Insert the new snippets at the front. |
| 402 snippets_.insert(snippets_.begin(), | 412 snippets_.insert(snippets_.begin(), |
| 403 std::make_move_iterator(new_snippets.begin()), | 413 std::make_move_iterator(new_snippets.begin()), |
| 404 std::make_move_iterator(new_snippets.end())); | 414 std::make_move_iterator(new_snippets.end())); |
| 405 } | 415 } |
| 406 | 416 |
| 407 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 417 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 408 NTPSnippetStorage prefs_snippets; | 418 NTPSnippet::PtrVector prefs_snippets; |
| 409 bool success = NTPSnippet::AddFromListValue( | 419 bool success = NTPSnippet::AddFromListValue( |
| 410 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); | 420 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); |
| 411 DCHECK(success) << "Failed to parse snippets from prefs"; | 421 DCHECK(success) << "Failed to parse snippets from prefs"; |
| 412 MergeSnippets(std::move(prefs_snippets)); | 422 MergeSnippets(std::move(prefs_snippets)); |
| 413 LoadingSnippetsFinished(); | 423 LoadingSnippetsFinished(); |
| 414 } | 424 } |
| 415 | 425 |
| 416 void NTPSnippetsService::StoreSnippetsToPrefs() { | 426 void NTPSnippetsService::StoreSnippetsToPrefs() { |
| 417 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 427 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
| 418 } | 428 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 if (snippet->expiry_date() < next_expiry) | 508 if (snippet->expiry_date() < next_expiry) |
| 499 next_expiry = snippet->expiry_date(); | 509 next_expiry = snippet->expiry_date(); |
| 500 } | 510 } |
| 501 DCHECK_GT(next_expiry, expiry); | 511 DCHECK_GT(next_expiry, expiry); |
| 502 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 512 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 503 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 513 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 504 base::Unretained(this))); | 514 base::Unretained(this))); |
| 505 } | 515 } |
| 506 | 516 |
| 507 } // namespace ntp_snippets | 517 } // namespace ntp_snippets |
| OLD | NEW |