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.SnippetsDiscardedAfterFetch", | |
Marc Treib
2016/05/04 07:42:43
I wouldn't use "Discarded" here, since that term i
May
2016/05/04 09:00:06
Recommended by jwd@ to just have a separate boolea
Marc Treib
2016/05/04 09:01:36
Couldn't we do that anyway by comparing the number
Bernhard Bauer
2016/05/04 09:06:54
Suggestion for consistency: Use "dismissed" for sn
Marc Treib
2016/05/04 09:08:29
Ah indeed, I mixed up "dismissed" and "discarded".
May
2016/05/04 09:26:34
Stop painting my bike shed!! I don't even use a bi
| |
437 num_snippets_discarded > 0); | |
438 if (num_snippets_discarded > 0) | |
Marc Treib
2016/05/04 07:42:43
nit: braces if the body doesn't fit on one line
May
2016/05/04 09:00:06
Done.
| |
439 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.IncompleteSnippets", | |
440 num_snippets_discarded); | |
441 } | |
442 | |
423 // Insert the new snippets at the front. | 443 // Insert the new snippets at the front. |
424 snippets_.insert(snippets_.begin(), | 444 snippets_.insert(snippets_.begin(), |
425 std::make_move_iterator(new_snippets.begin()), | 445 std::make_move_iterator(new_snippets.begin()), |
426 std::make_move_iterator(new_snippets.end())); | 446 std::make_move_iterator(new_snippets.end())); |
427 | 447 |
428 return true; | 448 return true; |
429 } | 449 } |
430 | 450 |
431 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 451 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
432 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); | 452 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); |
433 DCHECK(success) << "Failed to parse snippets from prefs"; | 453 DCHECK(success) << "Failed to parse snippets from prefs"; |
434 | 454 |
435 LoadingSnippetsFinished(); | 455 LoadingSnippetsFinished(); |
436 } | 456 } |
437 | 457 |
438 void NTPSnippetsService::StoreSnippetsToPrefs() { | 458 void NTPSnippetsService::StoreSnippetsToPrefs() { |
439 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); | 459 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); |
440 } | 460 } |
441 | 461 |
442 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { | 462 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { |
443 discarded_snippets_.clear(); | 463 discarded_snippets_.clear(); |
444 bool success = AddSnippetsFromListValue( | 464 bool success = AddSnippetsFromListValue( |
445 *pref_service_->GetList(prefs::kDiscardedSnippets), | 465 *pref_service_->GetList(prefs::kDiscardedSnippets), &discarded_snippets_); |
446 &discarded_snippets_); | |
447 DCHECK(success) << "Failed to parse discarded snippets from prefs"; | 466 DCHECK(success) << "Failed to parse discarded snippets from prefs"; |
448 } | 467 } |
449 | 468 |
450 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { | 469 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { |
451 pref_service_->Set(prefs::kDiscardedSnippets, | 470 pref_service_->Set(prefs::kDiscardedSnippets, |
452 *SnippetsToListValue(discarded_snippets_)); | 471 *SnippetsToListValue(discarded_snippets_)); |
453 } | 472 } |
454 | 473 |
455 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { | 474 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { |
456 std::set<std::string> hosts; | 475 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) | 536 if (snippet->expiry_date() < next_expiry) |
518 next_expiry = snippet->expiry_date(); | 537 next_expiry = snippet->expiry_date(); |
519 } | 538 } |
520 DCHECK_GT(next_expiry, expiry); | 539 DCHECK_GT(next_expiry, expiry); |
521 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 540 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
522 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, | 541 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, |
523 base::Unretained(this))); | 542 base::Unretained(this))); |
524 } | 543 } |
525 | 544 |
526 } // namespace ntp_snippets | 545 } // namespace ntp_snippets |
OLD | NEW |