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/histogram_macros.h" |
16 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
17 #include "base/path_service.h" | 18 #include "base/path_service.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 #include "base/values.h" | 23 #include "base/values.h" |
23 #include "components/ntp_snippets/pref_names.h" | 24 #include "components/ntp_snippets/pref_names.h" |
24 #include "components/ntp_snippets/switches.h" | 25 #include "components/ntp_snippets/switches.h" |
25 #include "components/prefs/pref_registry_simple.h" | 26 #include "components/prefs/pref_registry_simple.h" |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 return std::set<std::string>(); | 281 return std::set<std::string>(); |
281 | 282 |
282 // TODO(treib) this should just call GetSnippetHostsFromPrefs | 283 // TODO(treib) this should just call GetSnippetHostsFromPrefs |
283 return GetSuggestionsHostsImpl( | 284 return GetSuggestionsHostsImpl( |
284 suggestions_service_->GetSuggestionsDataFromCache()); | 285 suggestions_service_->GetSuggestionsDataFromCache()); |
285 } | 286 } |
286 | 287 |
287 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { | 288 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { |
288 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 289 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
289 [&url](const std::unique_ptr<NTPSnippet>& snippet) { | 290 [&url](const std::unique_ptr<NTPSnippet>& snippet) { |
290 return snippet->url() == url; | 291 return snippet->url() == url || |
| 292 snippet->best_source().url == url; |
291 }); | 293 }); |
292 if (it == snippets_.end()) | 294 if (it == snippets_.end()) |
293 return false; | 295 return false; |
294 discarded_snippets_.push_back(std::move(*it)); | 296 discarded_snippets_.push_back(std::move(*it)); |
295 snippets_.erase(it); | 297 snippets_.erase(it); |
296 StoreDiscardedSnippetsToPrefs(); | 298 StoreDiscardedSnippetsToPrefs(); |
297 StoreSnippetsToPrefs(); | 299 StoreSnippetsToPrefs(); |
298 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 300 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
299 NTPSnippetsServiceLoaded()); | 301 NTPSnippetsServiceLoaded()); |
300 return true; | 302 return true; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 StoreSnippetHostsToPrefs(hosts); | 339 StoreSnippetHostsToPrefs(hosts); |
338 | 340 |
339 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 341 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
340 NTPSnippetsServiceLoaded()); | 342 NTPSnippetsServiceLoaded()); |
341 | 343 |
342 FetchSnippetsFromHosts(hosts); | 344 FetchSnippetsFromHosts(hosts); |
343 } | 345 } |
344 | 346 |
345 void NTPSnippetsService::OnSnippetsDownloaded( | 347 void NTPSnippetsService::OnSnippetsDownloaded( |
346 const std::string& snippets_json, const std::string& status) { | 348 const std::string& snippets_json, const std::string& status) { |
347 | |
348 if (!snippets_json.empty()) { | 349 if (!snippets_json.empty()) { |
349 DCHECK(status.empty()); | 350 DCHECK(status.empty()); |
350 | 351 |
351 last_fetch_json_ = snippets_json; | 352 last_fetch_json_ = snippets_json; |
352 | 353 |
353 parse_json_callback_.Run( | 354 parse_json_callback_.Run( |
354 snippets_json, | 355 snippets_json, |
355 base::Bind(&NTPSnippetsService::OnJsonParsed, | 356 base::Bind(&NTPSnippetsService::OnJsonParsed, |
356 weak_ptr_factory_.GetWeakPtr(), snippets_json), | 357 weak_ptr_factory_.GetWeakPtr(), snippets_json), |
357 base::Bind(&NTPSnippetsService::OnJsonError, | 358 base::Bind(&NTPSnippetsService::OnJsonError, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { | 414 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { |
414 if (snippet->publish_date().is_null()) | 415 if (snippet->publish_date().is_null()) |
415 snippet->set_publish_date(base::Time::Now()); | 416 snippet->set_publish_date(base::Time::Now()); |
416 if (snippet->expiry_date().is_null()) { | 417 if (snippet->expiry_date().is_null()) { |
417 snippet->set_expiry_date( | 418 snippet->set_expiry_date( |
418 snippet->publish_date() + | 419 snippet->publish_date() + |
419 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); | 420 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); |
420 } | 421 } |
421 } | 422 } |
422 | 423 |
| 424 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 425 switches::kAddIncompleteSnippets)) { |
| 426 int num_new_snippets = new_snippets.size(); |
| 427 // Remove snippets that do not have all the info we need to display it to |
| 428 // the user. |
| 429 new_snippets.erase( |
| 430 std::remove_if(new_snippets.begin(), new_snippets.end(), |
| 431 [](const std::unique_ptr<NTPSnippet>& snippet) { |
| 432 return !snippet->is_complete(); |
| 433 }), |
| 434 new_snippets.end()); |
| 435 int num_snippets_discarded = num_new_snippets - new_snippets.size(); |
| 436 UMA_HISTOGRAM_BOOLEAN("NewTabPage.Snippets.IncompleteSnippetsAfterFetch", |
| 437 num_snippets_discarded > 0); |
| 438 if (num_snippets_discarded > 0) { |
| 439 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumIncompleteSnippets", |
| 440 num_snippets_discarded); |
| 441 } |
| 442 } |
| 443 |
423 // Insert the new snippets at the front. | 444 // Insert the new snippets at the front. |
424 snippets_.insert(snippets_.begin(), | 445 snippets_.insert(snippets_.begin(), |
425 std::make_move_iterator(new_snippets.begin()), | 446 std::make_move_iterator(new_snippets.begin()), |
426 std::make_move_iterator(new_snippets.end())); | 447 std::make_move_iterator(new_snippets.end())); |
427 | 448 |
428 return true; | 449 return true; |
429 } | 450 } |
430 | 451 |
431 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 452 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
432 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); | 453 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); |
433 DCHECK(success) << "Failed to parse snippets from prefs"; | 454 DCHECK(success) << "Failed to parse snippets from prefs"; |
434 | 455 |
435 LoadingSnippetsFinished(); | 456 LoadingSnippetsFinished(); |
436 } | 457 } |
437 | 458 |
438 void NTPSnippetsService::StoreSnippetsToPrefs() { | 459 void NTPSnippetsService::StoreSnippetsToPrefs() { |
439 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 460 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
440 } | 461 } |
441 | 462 |
442 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { | 463 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { |
443 discarded_snippets_.clear(); | 464 discarded_snippets_.clear(); |
444 bool success = AddSnippetsFromListValue( | 465 bool success = AddSnippetsFromListValue( |
445 *pref_service_->GetList(prefs::kDiscardedSnippets), | 466 *pref_service_->GetList(prefs::kDiscardedSnippets), &discarded_snippets_); |
446 &discarded_snippets_); | |
447 DCHECK(success) << "Failed to parse discarded snippets from prefs"; | 467 DCHECK(success) << "Failed to parse discarded snippets from prefs"; |
448 } | 468 } |
449 | 469 |
450 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { | 470 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { |
451 pref_service_->Set(prefs::kDiscardedSnippets, | 471 pref_service_->Set(prefs::kDiscardedSnippets, |
452 *SnippetsToListValue(discarded_snippets_)); | 472 *SnippetsToListValue(discarded_snippets_)); |
453 } | 473 } |
454 | 474 |
455 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { | 475 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { |
456 std::set<std::string> hosts; | 476 std::set<std::string> hosts; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 if (snippet->expiry_date() < next_expiry) | 537 if (snippet->expiry_date() < next_expiry) |
518 next_expiry = snippet->expiry_date(); | 538 next_expiry = snippet->expiry_date(); |
519 } | 539 } |
520 DCHECK_GT(next_expiry, expiry); | 540 DCHECK_GT(next_expiry, expiry); |
521 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 541 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
522 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 542 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
523 base::Unretained(this))); | 543 base::Unretained(this))); |
524 } | 544 } |
525 | 545 |
526 } // namespace ntp_snippets | 546 } // namespace ntp_snippets |
OLD | NEW |