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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 std::unique_ptr<base::ListValue> SnippetsToListValue( | 128 std::unique_ptr<base::ListValue> SnippetsToListValue( |
| 129 const NTPSnippetsService::NTPSnippetStorage& snippets) { | 129 const NTPSnippetsService::NTPSnippetStorage& 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 void InsertAllIDs(const NTPSnippetsService::NTPSnippetStorage& snippets, |
|
Marc Treib
2016/05/18 15:19:33
Heads-up: This will require a rebase onto https://
| |
| 139 const std::unique_ptr<NTPSnippet>& needle) { | 139 std::set<std::string>* ids) { |
| 140 const std::string& id = needle->id(); | 140 for (const std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 141 return std::find_if(haystack.begin(), haystack.end(), | 141 ids->insert(snippet->id()); |
| 142 [&id](const std::unique_ptr<NTPSnippet>& snippet) { | 142 for (const SnippetSource& source : snippet->sources()) { |
| 143 return snippet->id() == id; | 143 if (source.url.is_valid()) { |
|
Marc Treib
2016/05/18 15:19:33
We check that the source URL is valid when buildin
tschumann
2016/05/18 17:09:16
i thought I saw some code where we'd fall back to
| |
| 144 }) != haystack.end(); | 144 ids->insert(source.url.spec()); |
| 145 } | |
| 146 } | |
| 147 } | |
| 145 } | 148 } |
| 146 | 149 |
| 147 void WrapImageFetchedCallback( | 150 void WrapImageFetchedCallback( |
| 148 const NTPSnippetsService::ImageFetchedCallback& callback, | 151 const NTPSnippetsService::ImageFetchedCallback& callback, |
| 149 const GURL& snippet_id_url, | 152 const GURL& snippet_id_url, |
| 150 const SkBitmap* bitmap) { | 153 const SkBitmap* bitmap) { |
| 151 callback.Run(snippet_id_url.spec(), bitmap); | 154 callback.Run(snippet_id_url.spec(), bitmap); |
| 152 } | 155 } |
| 153 | 156 |
| 154 } // namespace | 157 } // namespace |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); | 348 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); |
| 346 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", | 349 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", |
| 347 snippets->size()); | 350 snippets->size()); |
| 348 MergeSnippets(std::move(*snippets)); | 351 MergeSnippets(std::move(*snippets)); |
| 349 } | 352 } |
| 350 LoadingSnippetsFinished(); | 353 LoadingSnippetsFinished(); |
| 351 } | 354 } |
| 352 | 355 |
| 353 void NTPSnippetsService::MergeSnippets(NTPSnippetStorage new_snippets) { | 356 void NTPSnippetsService::MergeSnippets(NTPSnippetStorage new_snippets) { |
| 354 // Remove new snippets that we already have, or that have been discarded. | 357 // Remove new snippets that we already have, or that have been discarded. |
| 358 std::set<std::string> old_snippet_ids; | |
| 359 InsertAllIDs(discarded_snippets_, &old_snippet_ids); | |
| 360 InsertAllIDs(snippets_, &old_snippet_ids); | |
| 355 new_snippets.erase( | 361 new_snippets.erase( |
| 356 std::remove_if(new_snippets.begin(), new_snippets.end(), | 362 std::remove_if( |
| 357 [this](const std::unique_ptr<NTPSnippet>& snippet) { | 363 new_snippets.begin(), new_snippets.end(), |
| 358 return ContainsSnippet(discarded_snippets_, snippet) || | 364 [&old_snippet_ids](const std::unique_ptr<NTPSnippet>& snippet) { |
| 359 ContainsSnippet(snippets_, snippet); | 365 return old_snippet_ids.find(snippet->id()) != old_snippet_ids.end(); |
|
Marc Treib
2016/05/18 15:19:33
We should probably check if *any* of the new snipp
tschumann
2016/05/18 17:09:16
Good catch!
| |
| 360 }), | 366 }), |
| 361 new_snippets.end()); | 367 new_snippets.end()); |
| 362 | 368 |
| 363 // Fill in default publish/expiry dates where required. | 369 // Fill in default publish/expiry dates where required. |
| 364 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { | 370 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { |
| 365 if (snippet->publish_date().is_null()) | 371 if (snippet->publish_date().is_null()) |
| 366 snippet->set_publish_date(base::Time::Now()); | 372 snippet->set_publish_date(base::Time::Now()); |
| 367 if (snippet->expiry_date().is_null()) { | 373 if (snippet->expiry_date().is_null()) { |
| 368 snippet->set_expiry_date( | 374 snippet->set_expiry_date( |
| 369 snippet->publish_date() + | 375 snippet->publish_date() + |
| 370 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); | 376 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 385 }), | 391 }), |
| 386 new_snippets.end()); | 392 new_snippets.end()); |
| 387 int num_snippets_discarded = num_new_snippets - new_snippets.size(); | 393 int num_snippets_discarded = num_new_snippets - new_snippets.size(); |
| 388 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", | 394 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", |
| 389 num_snippets_discarded > 0); | 395 num_snippets_discarded > 0); |
| 390 if (num_snippets_discarded > 0) { | 396 if (num_snippets_discarded > 0) { |
| 391 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", | 397 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", |
| 392 num_snippets_discarded); | 398 num_snippets_discarded); |
| 393 } | 399 } |
| 394 } | 400 } |
| 395 | |
|
Marc Treib
2016/05/18 15:19:33
nitty nit: I'd keep the empty line here between "u
| |
| 396 // Insert the new snippets at the front. | 401 // Insert the new snippets at the front. |
| 397 snippets_.insert(snippets_.begin(), | 402 snippets_.insert(snippets_.begin(), |
| 398 std::make_move_iterator(new_snippets.begin()), | 403 std::make_move_iterator(new_snippets.begin()), |
| 399 std::make_move_iterator(new_snippets.end())); | 404 std::make_move_iterator(new_snippets.end())); |
| 400 } | 405 } |
| 401 | 406 |
| 402 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 407 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 403 NTPSnippetStorage prefs_snippets; | 408 NTPSnippetStorage prefs_snippets; |
| 404 bool success = NTPSnippet::AddFromListValue( | 409 bool success = NTPSnippet::AddFromListValue( |
| 405 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); | 410 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 493 if (snippet->expiry_date() < next_expiry) | 498 if (snippet->expiry_date() < next_expiry) |
| 494 next_expiry = snippet->expiry_date(); | 499 next_expiry = snippet->expiry_date(); |
| 495 } | 500 } |
| 496 DCHECK_GT(next_expiry, expiry); | 501 DCHECK_GT(next_expiry, expiry); |
| 497 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 502 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 498 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 503 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 499 base::Unretained(this))); | 504 base::Unretained(this))); |
| 500 } | 505 } |
| 501 | 506 |
| 502 } // namespace ntp_snippets | 507 } // namespace ntp_snippets |
| OLD | NEW |