| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 return false; | 134 return false; |
| 135 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content); | 135 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content); |
| 136 if (!snippet) | 136 if (!snippet) |
| 137 return false; | 137 return false; |
| 138 | 138 |
| 139 snippets->push_back(std::move(snippet)); | 139 snippets->push_back(std::move(snippet)); |
| 140 } | 140 } |
| 141 return true; | 141 return true; |
| 142 } | 142 } |
| 143 | 143 |
| 144 scoped_ptr<base::ListValue> SnippetsToListValue( | |
| 145 const NTPSnippetsService::NTPSnippetStorage& snippets) { | |
| 146 scoped_ptr<base::ListValue> list(new base::ListValue); | |
| 147 for (const auto& snippet : snippets) { | |
| 148 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
| 149 dict->Set(kContentInfo, snippet->ToDictionary()); | |
| 150 list->Append(std::move(dict)); | |
| 151 } | |
| 152 return list; | |
| 153 } | |
| 154 | 144 |
| 155 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack, | 145 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack, |
| 156 const scoped_ptr<NTPSnippet>& needle) { | 146 const scoped_ptr<NTPSnippet>& needle) { |
| 157 const GURL& url = needle->url(); | 147 const GURL& url = needle->url(); |
| 158 return std::find_if(haystack.begin(), haystack.end(), | 148 return std::find_if(haystack.begin(), haystack.end(), |
| 159 [&url](const scoped_ptr<NTPSnippet>& snippet) { | 149 [&url](const scoped_ptr<NTPSnippet>& snippet) { |
| 160 return snippet->url() == url; | 150 return snippet->url() == url; |
| 161 }) != haystack.end(); | 151 }) != haystack.end(); |
| 162 } | 152 } |
| 163 | 153 |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 std::make_move_iterator(new_snippets.begin()), | 375 std::make_move_iterator(new_snippets.begin()), |
| 386 std::make_move_iterator(new_snippets.end())); | 376 std::make_move_iterator(new_snippets.end())); |
| 387 | 377 |
| 388 // Immediately remove any already-expired snippets. This will also notify our | 378 // Immediately remove any already-expired snippets. This will also notify our |
| 389 // observers and schedule the expiry timer. | 379 // observers and schedule the expiry timer. |
| 390 RemoveExpiredSnippets(); | 380 RemoveExpiredSnippets(); |
| 391 | 381 |
| 392 return true; | 382 return true; |
| 393 } | 383 } |
| 394 | 384 |
| 385 scoped_ptr<base::ListValue> NTPSnippetsService::SnippetsToListValue( |
| 386 const NTPSnippetsService::NTPSnippetStorage& snippets) const { |
| 387 scoped_ptr<base::ListValue> list(new base::ListValue); |
| 388 for (const auto& snippet : snippets) { |
| 389 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 390 dict->Set(kContentInfo, snippet->ToDictionary()); |
| 391 list->Append(std::move(dict)); |
| 392 } |
| 393 return list; |
| 394 } |
| 395 |
| 395 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 396 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 396 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); | 397 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); |
| 397 DCHECK(success) << "Failed to parse snippets from prefs"; | 398 DCHECK(success) << "Failed to parse snippets from prefs"; |
| 398 } | 399 } |
| 399 | 400 |
| 400 void NTPSnippetsService::StoreSnippetsToPrefs() { | 401 void NTPSnippetsService::StoreSnippetsToPrefs() { |
| 401 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 402 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
| 402 } | 403 } |
| 403 | 404 |
| 404 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { | 405 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 if (snippet->expiry_date() < next_expiry) | 470 if (snippet->expiry_date() < next_expiry) |
| 470 next_expiry = snippet->expiry_date(); | 471 next_expiry = snippet->expiry_date(); |
| 471 } | 472 } |
| 472 DCHECK_GT(next_expiry, expiry); | 473 DCHECK_GT(next_expiry, expiry); |
| 473 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 474 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 474 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | 475 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, |
| 475 base::Unretained(this))); | 476 base::Unretained(this))); |
| 476 } | 477 } |
| 477 | 478 |
| 478 } // namespace ntp_snippets | 479 } // namespace ntp_snippets |
| OLD | NEW |