Chromium Code Reviews| 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); | |
|
Marc Treib
2016/05/17 11:21:18
DCHECK_EQ etc don't work with enum class :(
(I gue
Bernhard Bauer
2016/05/17 12:38:13
Yuck. TBH, I'm less and less convinced that enum c
Marc Treib
2016/05/17 12:46:34
Yup, the missing operator<< is the reason DCHECK_E
| |
| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 // Sparse histogram used because the number of snippets is small (bound by | 353 // Sparse histogram used because the number of snippets is small (bound by |
| 344 // kMaxSnippetCount). | 354 // kMaxSnippetCount). |
| 345 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 355 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 346 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 356 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 347 snippets->size()); | 357 snippets->size()); |
| 348 MergeSnippets(std::move(*snippets)); | 358 MergeSnippets(std::move(*snippets)); |
| 349 } | 359 } |
| 350 LoadingSnippetsFinished(); | 360 LoadingSnippetsFinished(); |
| 351 } | 361 } |
| 352 | 362 |
| 353 void NTPSnippetsService::MergeSnippets(NTPSnippetStorage new_snippets) { | 363 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
| 354 // Remove new snippets that we already have, or that have been discarded. | 364 // Remove new snippets that we already have, or that have been discarded. |
| 355 new_snippets.erase( | 365 new_snippets.erase( |
| 356 std::remove_if(new_snippets.begin(), new_snippets.end(), | 366 std::remove_if(new_snippets.begin(), new_snippets.end(), |
| 357 [this](const std::unique_ptr<NTPSnippet>& snippet) { | 367 [this](const std::unique_ptr<NTPSnippet>& snippet) { |
| 358 return ContainsSnippet(discarded_snippets_, snippet) || | 368 return ContainsSnippet(discarded_snippets_, snippet) || |
| 359 ContainsSnippet(snippets_, snippet); | 369 ContainsSnippet(snippets_, snippet); |
| 360 }), | 370 }), |
| 361 new_snippets.end()); | 371 new_snippets.end()); |
| 362 | 372 |
| 363 // Fill in default publish/expiry dates where required. | 373 // Fill in default publish/expiry dates where required. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 393 } | 403 } |
| 394 } | 404 } |
| 395 | 405 |
| 396 // Insert the new snippets at the front. | 406 // Insert the new snippets at the front. |
| 397 snippets_.insert(snippets_.begin(), | 407 snippets_.insert(snippets_.begin(), |
| 398 std::make_move_iterator(new_snippets.begin()), | 408 std::make_move_iterator(new_snippets.begin()), |
| 399 std::make_move_iterator(new_snippets.end())); | 409 std::make_move_iterator(new_snippets.end())); |
| 400 } | 410 } |
| 401 | 411 |
| 402 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 412 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 403 NTPSnippetStorage prefs_snippets; | 413 NTPSnippet::PtrVector prefs_snippets; |
| 404 bool success = NTPSnippet::AddFromListValue( | 414 bool success = NTPSnippet::AddFromListValue( |
| 405 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); | 415 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); |
| 406 DCHECK(success) << "Failed to parse snippets from prefs"; | 416 DCHECK(success) << "Failed to parse snippets from prefs"; |
| 407 MergeSnippets(std::move(prefs_snippets)); | 417 MergeSnippets(std::move(prefs_snippets)); |
| 408 LoadingSnippetsFinished(); | 418 LoadingSnippetsFinished(); |
| 409 } | 419 } |
| 410 | 420 |
| 411 void NTPSnippetsService::StoreSnippetsToPrefs() { | 421 void NTPSnippetsService::StoreSnippetsToPrefs() { |
| 412 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 422 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
| 413 } | 423 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 493 if (snippet->expiry_date() < next_expiry) | 503 if (snippet->expiry_date() < next_expiry) |
| 494 next_expiry = snippet->expiry_date(); | 504 next_expiry = snippet->expiry_date(); |
| 495 } | 505 } |
| 496 DCHECK_GT(next_expiry, expiry); | 506 DCHECK_GT(next_expiry, expiry); |
| 497 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 507 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 498 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 508 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 499 base::Unretained(this))); | 509 base::Unretained(this))); |
| 500 } | 510 } |
| 501 | 511 |
| 502 } // namespace ntp_snippets | 512 } // namespace ntp_snippets |
| OLD | NEW |