| 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()) |
| 169 return snippet->id() == id; | 169 ids->insert(source.url.spec()); |
| 170 }) != haystack.end(); | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 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 |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 386 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 387 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 387 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 388 snippets->size()); | 388 snippets->size()); |
| 389 MergeSnippets(std::move(*snippets)); | 389 MergeSnippets(std::move(*snippets)); |
| 390 } | 390 } |
| 391 LoadingSnippetsFinished(); | 391 LoadingSnippetsFinished(); |
| 392 } | 392 } |
| 393 | 393 |
| 394 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { | 394 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) { |
| 395 // Remove new snippets that we already have, or that have been discarded. | 395 // Remove new snippets that we already have, or that have been discarded. |
| 396 std::set<std::string> old_snippet_ids; |
| 397 InsertAllIDs(discarded_snippets_, &old_snippet_ids); |
| 398 InsertAllIDs(snippets_, &old_snippet_ids); |
| 396 new_snippets.erase( | 399 new_snippets.erase( |
| 397 std::remove_if(new_snippets.begin(), new_snippets.end(), | 400 std::remove_if( |
| 398 [this](const std::unique_ptr<NTPSnippet>& snippet) { | 401 new_snippets.begin(), new_snippets.end(), |
| 399 return ContainsSnippet(discarded_snippets_, snippet) || | 402 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { |
| 400 ContainsSnippet(snippets_, snippet); | 403 if (old_snippet_ids.count(snippet->id())) |
| 401 }), | 404 return true; |
| 405 for (const SnippetSource& source : snippet->sources()) { |
| 406 if (old_snippet_ids.count(source.url.spec())) |
| 407 return true; |
| 408 } |
| 409 return false; |
| 410 }), |
| 402 new_snippets.end()); | 411 new_snippets.end()); |
| 403 | 412 |
| 404 // Fill in default publish/expiry dates where required. | 413 // Fill in default publish/expiry dates where required. |
| 405 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { | 414 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { |
| 406 if (snippet->publish_date().is_null()) | 415 if (snippet->publish_date().is_null()) |
| 407 snippet->set_publish_date(base::Time::Now()); | 416 snippet->set_publish_date(base::Time::Now()); |
| 408 if (snippet->expiry_date().is_null()) { | 417 if (snippet->expiry_date().is_null()) { |
| 409 snippet->set_expiry_date( | 418 snippet->set_expiry_date( |
| 410 snippet->publish_date() + | 419 snippet->publish_date() + |
| 411 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); | 420 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 426 }), | 435 }), |
| 427 new_snippets.end()); | 436 new_snippets.end()); |
| 428 int num_snippets_discarded = num_new_snippets - new_snippets.size(); | 437 int num_snippets_discarded = num_new_snippets - new_snippets.size(); |
| 429 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", | 438 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", |
| 430 num_snippets_discarded > 0); | 439 num_snippets_discarded > 0); |
| 431 if (num_snippets_discarded > 0) { | 440 if (num_snippets_discarded > 0) { |
| 432 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", | 441 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", |
| 433 num_snippets_discarded); | 442 num_snippets_discarded); |
| 434 } | 443 } |
| 435 } | 444 } |
| 436 | |
| 437 // Insert the new snippets at the front. | 445 // Insert the new snippets at the front. |
| 438 snippets_.insert(snippets_.begin(), | 446 snippets_.insert(snippets_.begin(), |
| 439 std::make_move_iterator(new_snippets.begin()), | 447 std::make_move_iterator(new_snippets.begin()), |
| 440 std::make_move_iterator(new_snippets.end())); | 448 std::make_move_iterator(new_snippets.end())); |
| 441 } | 449 } |
| 442 | 450 |
| 443 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 451 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 444 NTPSnippet::PtrVector prefs_snippets; | 452 NTPSnippet::PtrVector prefs_snippets; |
| 445 bool success = NTPSnippet::AddFromListValue( | 453 bool success = NTPSnippet::AddFromListValue( |
| 446 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); | 454 *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) | 542 if (snippet->expiry_date() < next_expiry) |
| 535 next_expiry = snippet->expiry_date(); | 543 next_expiry = snippet->expiry_date(); |
| 536 } | 544 } |
| 537 DCHECK_GT(next_expiry, expiry); | 545 DCHECK_GT(next_expiry, expiry); |
| 538 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 546 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 539 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 547 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 540 base::Unretained(this))); | 548 base::Unretained(this))); |
| 541 } | 549 } |
| 542 | 550 |
| 543 } // namespace ntp_snippets | 551 } // namespace ntp_snippets |
| OLD | NEW |