| 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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 | 340 |
| 341 FetchSnippetsFromHosts(hosts); | 341 FetchSnippetsFromHosts(hosts); |
| 342 } | 342 } |
| 343 | 343 |
| 344 void NTPSnippetsService::OnSnippetsDownloaded( | 344 void NTPSnippetsService::OnSnippetsDownloaded( |
| 345 const std::string& snippets_json, const std::string& status) { | 345 const std::string& snippets_json, const std::string& status) { |
| 346 | 346 |
| 347 if (!snippets_json.empty()) { | 347 if (!snippets_json.empty()) { |
| 348 DCHECK(status.empty()); | 348 DCHECK(status.empty()); |
| 349 | 349 |
| 350 last_fetch_json_ = snippets_json; |
| 351 |
| 350 parse_json_callback_.Run( | 352 parse_json_callback_.Run( |
| 351 snippets_json, | 353 snippets_json, |
| 352 base::Bind(&NTPSnippetsService::OnJsonParsed, | 354 base::Bind(&NTPSnippetsService::OnJsonParsed, |
| 353 weak_ptr_factory_.GetWeakPtr(), snippets_json), | 355 weak_ptr_factory_.GetWeakPtr(), snippets_json), |
| 354 base::Bind(&NTPSnippetsService::OnJsonError, | 356 base::Bind(&NTPSnippetsService::OnJsonError, |
| 355 weak_ptr_factory_.GetWeakPtr(), snippets_json)); | 357 weak_ptr_factory_.GetWeakPtr(), snippets_json)); |
| 356 } else { | 358 } else { |
| 357 last_fetch_status_ = status; | 359 last_fetch_status_ = status; |
| 358 LoadingSnippetsFinished(); | 360 LoadingSnippetsFinished(); |
| 359 } | 361 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 snippet->publish_date() + | 417 snippet->publish_date() + |
| 416 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); | 418 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); |
| 417 } | 419 } |
| 418 } | 420 } |
| 419 | 421 |
| 420 // Insert the new snippets at the front. | 422 // Insert the new snippets at the front. |
| 421 snippets_.insert(snippets_.begin(), | 423 snippets_.insert(snippets_.begin(), |
| 422 std::make_move_iterator(new_snippets.begin()), | 424 std::make_move_iterator(new_snippets.begin()), |
| 423 std::make_move_iterator(new_snippets.end())); | 425 std::make_move_iterator(new_snippets.end())); |
| 424 | 426 |
| 425 // If there are more snippets now than we want to show, drop the extra ones | |
| 426 // from the end of the list. | |
| 427 if (snippets_.size() > kMaxSnippetCount) | |
| 428 snippets_.resize(kMaxSnippetCount); | |
| 429 | |
| 430 return true; | 427 return true; |
| 431 } | 428 } |
| 432 | 429 |
| 433 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 430 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 434 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); | 431 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); |
| 435 DCHECK(success) << "Failed to parse snippets from prefs"; | 432 DCHECK(success) << "Failed to parse snippets from prefs"; |
| 436 | 433 |
| 437 LoadingSnippetsFinished(); | 434 LoadingSnippetsFinished(); |
| 438 } | 435 } |
| 439 | 436 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 void NTPSnippetsService::LoadingSnippetsFinished() { | 474 void NTPSnippetsService::LoadingSnippetsFinished() { |
| 478 // Remove expired snippets. | 475 // Remove expired snippets. |
| 479 base::Time expiry = base::Time::Now(); | 476 base::Time expiry = base::Time::Now(); |
| 480 | 477 |
| 481 snippets_.erase( | 478 snippets_.erase( |
| 482 std::remove_if(snippets_.begin(), snippets_.end(), | 479 std::remove_if(snippets_.begin(), snippets_.end(), |
| 483 [&expiry](const std::unique_ptr<NTPSnippet>& snippet) { | 480 [&expiry](const std::unique_ptr<NTPSnippet>& snippet) { |
| 484 return snippet->expiry_date() <= expiry; | 481 return snippet->expiry_date() <= expiry; |
| 485 }), | 482 }), |
| 486 snippets_.end()); | 483 snippets_.end()); |
| 484 |
| 485 // If there are more snippets now than we want to show, drop the extra ones |
| 486 // from the end of the list. |
| 487 if (snippets_.size() > kMaxSnippetCount) |
| 488 snippets_.resize(kMaxSnippetCount); |
| 489 |
| 487 StoreSnippetsToPrefs(); | 490 StoreSnippetsToPrefs(); |
| 488 | 491 |
| 489 discarded_snippets_.erase( | 492 discarded_snippets_.erase( |
| 490 std::remove_if(discarded_snippets_.begin(), discarded_snippets_.end(), | 493 std::remove_if(discarded_snippets_.begin(), discarded_snippets_.end(), |
| 491 [&expiry](const std::unique_ptr<NTPSnippet>& snippet) { | 494 [&expiry](const std::unique_ptr<NTPSnippet>& snippet) { |
| 492 return snippet->expiry_date() <= expiry; | 495 return snippet->expiry_date() <= expiry; |
| 493 }), | 496 }), |
| 494 discarded_snippets_.end()); | 497 discarded_snippets_.end()); |
| 495 StoreDiscardedSnippetsToPrefs(); | 498 StoreDiscardedSnippetsToPrefs(); |
| 496 | 499 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 510 if (snippet->expiry_date() < next_expiry) | 513 if (snippet->expiry_date() < next_expiry) |
| 511 next_expiry = snippet->expiry_date(); | 514 next_expiry = snippet->expiry_date(); |
| 512 } | 515 } |
| 513 DCHECK_GT(next_expiry, expiry); | 516 DCHECK_GT(next_expiry, expiry); |
| 514 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 517 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 515 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 518 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
| 516 base::Unretained(this))); | 519 base::Unretained(this))); |
| 517 } | 520 } |
| 518 | 521 |
| 519 } // namespace ntp_snippets | 522 } // namespace ntp_snippets |
| OLD | NEW |