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