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 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/metrics/sparse_histogram.h" |
16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "base/values.h" | 22 #include "base/values.h" |
22 #include "components/ntp_snippets/pref_names.h" | 23 #include "components/ntp_snippets/pref_names.h" |
23 #include "components/ntp_snippets/switches.h" | 24 #include "components/ntp_snippets/switches.h" |
24 #include "components/prefs/pref_registry_simple.h" | 25 #include "components/prefs/pref_registry_simple.h" |
25 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | 122 const ChromeSuggestion& suggestion = suggestions.suggestions(i); |
122 GURL url(suggestion.url()); | 123 GURL url(suggestion.url()); |
123 if (url.is_valid()) | 124 if (url.is_valid()) |
124 hosts.insert(url.host()); | 125 hosts.insert(url.host()); |
125 } | 126 } |
126 return hosts; | 127 return hosts; |
127 } | 128 } |
128 | 129 |
129 const char kContentInfo[] = "contentInfo"; | 130 const char kContentInfo[] = "contentInfo"; |
130 | 131 |
131 // Parses snippets from |list| and adds them to |snippets|. Returns true on | 132 // Parses snippets from |list| and adds them to |snippets|. |downloaded| should |
132 // success, false if anything went wrong. | 133 // be true if we are adding snippets that have been freshly downloaded, and |
| 134 // false if we are loading from prefs. Returns true on success, false if |
| 135 // anything went wrong. |
133 bool AddSnippetsFromListValue(const base::ListValue& list, | 136 bool AddSnippetsFromListValue(const base::ListValue& list, |
134 NTPSnippetsService::NTPSnippetStorage* snippets) { | 137 NTPSnippetsService::NTPSnippetStorage* snippets, |
| 138 bool downloaded) { |
| 139 int num_incomplete_snippets = 0; |
135 for (const base::Value* const value : list) { | 140 for (const base::Value* const value : list) { |
136 const base::DictionaryValue* dict = nullptr; | 141 const base::DictionaryValue* dict = nullptr; |
137 if (!value->GetAsDictionary(&dict)) | 142 if (!value->GetAsDictionary(&dict)) |
138 return false; | 143 return false; |
139 | 144 |
140 const base::DictionaryValue* content = nullptr; | 145 const base::DictionaryValue* content = nullptr; |
141 if (!dict->GetDictionary(kContentInfo, &content)) | 146 if (!dict->GetDictionary(kContentInfo, &content)) |
142 return false; | 147 return false; |
143 std::unique_ptr<NTPSnippet> snippet = | 148 std::unique_ptr<NTPSnippet> snippet = |
144 NTPSnippet::CreateFromDictionary(*content); | 149 NTPSnippet::CreateFromDictionary(*content); |
145 if (!snippet) | 150 if (!snippet) |
146 return false; | 151 return false; |
147 | 152 |
148 snippets->push_back(std::move(snippet)); | 153 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 154 switches::kAddIncompleteSnippets)) { |
| 155 snippets->push_back(std::move(snippet)); |
| 156 } else if (snippet->is_complete()) { |
| 157 snippets->push_back(std::move(snippet)); |
| 158 } else { |
| 159 ++num_incomplete_snippets; |
| 160 } |
149 } | 161 } |
| 162 |
| 163 if (num_incomplete_snippets > 0) |
| 164 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.IncompleteSnippets", |
| 165 num_incomplete_snippets); |
| 166 |
150 return true; | 167 return true; |
151 } | 168 } |
152 | 169 |
153 std::unique_ptr<base::ListValue> SnippetsToListValue( | 170 std::unique_ptr<base::ListValue> SnippetsToListValue( |
154 const NTPSnippetsService::NTPSnippetStorage& snippets) { | 171 const NTPSnippetsService::NTPSnippetStorage& snippets) { |
155 std::unique_ptr<base::ListValue> list(new base::ListValue); | 172 std::unique_ptr<base::ListValue> list(new base::ListValue); |
156 for (const auto& snippet : snippets) { | 173 for (const auto& snippet : snippets) { |
157 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 174 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
158 dict->Set(kContentInfo, snippet->ToDictionary()); | 175 dict->Set(kContentInfo, snippet->ToDictionary()); |
159 list->Append(std::move(dict)); | 176 list->Append(std::move(dict)); |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 404 |
388 const base::ListValue* list = nullptr; | 405 const base::ListValue* list = nullptr; |
389 if (!top_dict->GetList("recos", &list)) | 406 if (!top_dict->GetList("recos", &list)) |
390 return false; | 407 return false; |
391 | 408 |
392 return LoadFromListValue(*list); | 409 return LoadFromListValue(*list); |
393 } | 410 } |
394 | 411 |
395 bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) { | 412 bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) { |
396 NTPSnippetStorage new_snippets; | 413 NTPSnippetStorage new_snippets; |
397 if (!AddSnippetsFromListValue(list, &new_snippets)) | 414 if (!AddSnippetsFromListValue(list, &new_snippets, true)) |
398 return false; | 415 return false; |
399 | 416 |
400 // Remove new snippets that we already have, or that have been discarded. | 417 // Remove new snippets that we already have, or that have been discarded. |
401 new_snippets.erase( | 418 new_snippets.erase( |
402 std::remove_if(new_snippets.begin(), new_snippets.end(), | 419 std::remove_if(new_snippets.begin(), new_snippets.end(), |
403 [this](const std::unique_ptr<NTPSnippet>& snippet) { | 420 [this](const std::unique_ptr<NTPSnippet>& snippet) { |
404 return ContainsSnippet(discarded_snippets_, snippet) || | 421 return ContainsSnippet(discarded_snippets_, snippet) || |
405 ContainsSnippet(snippets_, snippet); | 422 ContainsSnippet(snippets_, snippet); |
406 }), | 423 }), |
407 new_snippets.end()); | 424 new_snippets.end()); |
(...skipping 29 matching lines...) Expand all Loading... |
437 LoadingSnippetsFinished(); | 454 LoadingSnippetsFinished(); |
438 } | 455 } |
439 | 456 |
440 void NTPSnippetsService::StoreSnippetsToPrefs() { | 457 void NTPSnippetsService::StoreSnippetsToPrefs() { |
441 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 458 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
442 } | 459 } |
443 | 460 |
444 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { | 461 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { |
445 discarded_snippets_.clear(); | 462 discarded_snippets_.clear(); |
446 bool success = AddSnippetsFromListValue( | 463 bool success = AddSnippetsFromListValue( |
447 *pref_service_->GetList(prefs::kDiscardedSnippets), | 464 *pref_service_->GetList(prefs::kDiscardedSnippets), &discarded_snippets_, |
448 &discarded_snippets_); | 465 false); |
449 DCHECK(success) << "Failed to parse discarded snippets from prefs"; | 466 DCHECK(success) << "Failed to parse discarded snippets from prefs"; |
450 } | 467 } |
451 | 468 |
452 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { | 469 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { |
453 pref_service_->Set(prefs::kDiscardedSnippets, | 470 pref_service_->Set(prefs::kDiscardedSnippets, |
454 *SnippetsToListValue(discarded_snippets_)); | 471 *SnippetsToListValue(discarded_snippets_)); |
455 } | 472 } |
456 | 473 |
457 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { | 474 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { |
458 std::set<std::string> hosts; | 475 std::set<std::string> hosts; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 if (snippet->expiry_date() < next_expiry) | 527 if (snippet->expiry_date() < next_expiry) |
511 next_expiry = snippet->expiry_date(); | 528 next_expiry = snippet->expiry_date(); |
512 } | 529 } |
513 DCHECK_GT(next_expiry, expiry); | 530 DCHECK_GT(next_expiry, expiry); |
514 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 531 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
515 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 532 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
516 base::Unretained(this))); | 533 base::Unretained(this))); |
517 } | 534 } |
518 | 535 |
519 } // namespace ntp_snippets | 536 } // namespace ntp_snippets |
OLD | NEW |