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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 std::unique_ptr<base::ListValue> SnippetsToListValue( | 154 std::unique_ptr<base::ListValue> SnippetsToListValue( |
| 155 const NTPSnippet::PtrVector& snippets) { | 155 const NTPSnippet::PtrVector& snippets) { |
| 156 std::unique_ptr<base::ListValue> list(new base::ListValue); | 156 std::unique_ptr<base::ListValue> list(new base::ListValue); |
| 157 for (const auto& snippet : snippets) { | 157 for (const auto& snippet : snippets) { |
| 158 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary(); | 158 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary(); |
| 159 list->Append(std::move(dict)); | 159 list->Append(std::move(dict)); |
| 160 } | 160 } |
| 161 return list; | 161 return list; |
| 162 } | 162 } |
| 163 | 163 |
| 164 bool ContainsSnippet(const NTPSnippet::PtrVector& haystack, | 164 void InsertAllIDs(const NTPSnippet::PtrVector& snippets, |
| 165 const std::unique_ptr<NTPSnippet>& needle) { | 165 std::set<std::string>* ids) { |
| 166 const std::string& id = needle->id(); | 166 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 167 return std::find_if(haystack.begin(), haystack.end(), | 167 ids->insert(snippet->id()); |
| 168 [&id](const std::unique_ptr<NTPSnippet>& snippet) { | 168 for (const SnippetSource& source : snippet->sources()) { |
|
Marc Treib
2016/05/18 18:58:05
nit: no braces required (we usually leave them out
tschumann
2016/05/18 19:23:28
hmmm... Ok, done for consistency.
One day (in the
Marc Treib
2016/05/18 19:29:29
You'll have to fight Bernhard over it :D
Personall
| |
| 169 return snippet->id() == id; | 169 ids->insert(source.url.spec()); |
| 170 }) != haystack.end(); | 170 } |
| 171 } | |
| 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 SkBitmap* bitmap) { |
| 177 callback.Run(snippet_id_url.spec(), bitmap); | 178 callback.Run(snippet_id_url.spec(), bitmap); |
| 178 } | 179 } |
| 179 | 180 |
| 180 } // namespace | 181 } // namespace |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 387 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 387 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 388 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 388 snippets->size()); | 389 snippets->size()); |
| 389 MergeSnippets(std::move(*snippets)); | 390 MergeSnippets(std::move(*snippets)); |
| 390 } | 391 } |
| 391 LoadingSnippetsFinished(); | 392 LoadingSnippetsFinished(); |
| 392 } | 393 } |
| 393 | 394 |
| 394 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { | 395 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
| 395 // Remove new snippets that we already have, or that have been discarded. | 396 // Remove new snippets that we already have, or that have been discarded. |
| 397 std::set<std::string> old_snippet_ids; | |
| 398 InsertAllIDs(discarded_snippets_, &old_snippet_ids); | |
| 399 InsertAllIDs(snippets_, &old_snippet_ids); | |
| 396 new_snippets.erase( | 400 new_snippets.erase( |
| 397 std::remove_if(new_snippets.begin(), new_snippets.end(), | 401 std::remove_if( |
| 398 [this](const std::unique_ptr<NTPSnippet>& snippet) { | 402 new_snippets.begin(), new_snippets.end(), |
| 399 return ContainsSnippet(discarded_snippets_, snippet) || | 403 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { |
| 400 ContainsSnippet(snippets_, snippet); | 404 if (old_snippet_ids.count(snippet->id())) { |
|
Marc Treib
2016/05/18 18:58:05
Also here: no braces required
tschumann
2016/05/18 19:23:28
Done.
| |
| 401 }), | 405 return true; |
| 406 } | |
| 407 for (const SnippetSource& source : snippet->sources()) { | |
| 408 if (old_snippet_ids.count(source.url.spec())) { | |
|
Marc Treib
2016/05/18 18:58:05
And here. (The "for" should keep the braces though
tschumann
2016/05/18 19:23:28
Done.
| |
| 409 return true; | |
| 410 } | |
| 411 } | |
| 412 return false; | |
| 413 }), | |
| 402 new_snippets.end()); | 414 new_snippets.end()); |
| 403 | 415 |
| 404 // Fill in default publish/expiry dates where required. | 416 // Fill in default publish/expiry dates where required. |
| 405 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { | 417 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { |
| 406 if (snippet->publish_date().is_null()) | 418 if (snippet->publish_date().is_null()) |
| 407 snippet->set_publish_date(base::Time::Now()); | 419 snippet->set_publish_date(base::Time::Now()); |
| 408 if (snippet->expiry_date().is_null()) { | 420 if (snippet->expiry_date().is_null()) { |
| 409 snippet->set_expiry_date( | 421 snippet->set_expiry_date( |
| 410 snippet->publish_date() + | 422 snippet->publish_date() + |
| 411 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); | 423 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 426 }), | 438 }), |
| 427 new_snippets.end()); | 439 new_snippets.end()); |
| 428 int num_snippets_discarded = num_new_snippets - new_snippets.size(); | 440 int num_snippets_discarded = num_new_snippets - new_snippets.size(); |
| 429 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", | 441 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", |
| 430 num_snippets_discarded > 0); | 442 num_snippets_discarded > 0); |
| 431 if (num_snippets_discarded > 0) { | 443 if (num_snippets_discarded > 0) { |
| 432 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", | 444 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", |
| 433 num_snippets_discarded); | 445 num_snippets_discarded); |
| 434 } | 446 } |
| 435 } | 447 } |
| 436 | |
| 437 // Insert the new snippets at the front. | 448 // Insert the new snippets at the front. |
| 438 snippets_.insert(snippets_.begin(), | 449 snippets_.insert(snippets_.begin(), |
| 439 std::make_move_iterator(new_snippets.begin()), | 450 std::make_move_iterator(new_snippets.begin()), |
| 440 std::make_move_iterator(new_snippets.end())); | 451 std::make_move_iterator(new_snippets.end())); |
| 441 } | 452 } |
| 442 | 453 |
| 443 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 454 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 444 NTPSnippet::PtrVector prefs_snippets; | 455 NTPSnippet::PtrVector prefs_snippets; |
| 445 bool success = NTPSnippet::AddFromListValue( | 456 bool success = NTPSnippet::AddFromListValue( |
| 446 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); | 457 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 534 if (snippet->expiry_date() < next_expiry) | 545 if (snippet->expiry_date() < next_expiry) |
| 535 next_expiry = snippet->expiry_date(); | 546 next_expiry = snippet->expiry_date(); |
| 536 } | 547 } |
| 537 DCHECK_GT(next_expiry, expiry); | 548 DCHECK_GT(next_expiry, expiry); |
| 538 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 549 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 539 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 550 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 540 base::Unretained(this))); | 551 base::Unretained(this))); |
| 541 } | 552 } |
| 542 | 553 |
| 543 } // namespace ntp_snippets | 554 } // namespace ntp_snippets |
| OLD | NEW |