Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1921553004: Add favicon and publisher name to snippet cards (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 return std::set<std::string>(); 280 return std::set<std::string>();
280 281
281 // TODO(treib) this should just call GetSnippetHostsFromPrefs 282 // TODO(treib) this should just call GetSnippetHostsFromPrefs
282 return GetSuggestionsHostsImpl( 283 return GetSuggestionsHostsImpl(
283 suggestions_service_->GetSuggestionsDataFromCache()); 284 suggestions_service_->GetSuggestionsDataFromCache());
284 } 285 }
285 286
286 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { 287 bool NTPSnippetsService::DiscardSnippet(const GURL& url) {
287 auto it = std::find_if(snippets_.begin(), snippets_.end(), 288 auto it = std::find_if(snippets_.begin(), snippets_.end(),
288 [&url](const std::unique_ptr<NTPSnippet>& snippet) { 289 [&url](const std::unique_ptr<NTPSnippet>& snippet) {
289 return snippet->url() == url; 290 return snippet->url() == url ||
291 snippet->best_source().url == url;
290 }); 292 });
291 if (it == snippets_.end()) 293 if (it == snippets_.end())
292 return false; 294 return false;
293 discarded_snippets_.push_back(std::move(*it)); 295 discarded_snippets_.push_back(std::move(*it));
294 snippets_.erase(it); 296 snippets_.erase(it);
295 StoreDiscardedSnippetsToPrefs(); 297 StoreDiscardedSnippetsToPrefs();
296 StoreSnippetsToPrefs(); 298 StoreSnippetsToPrefs();
297 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 299 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
298 NTPSnippetsServiceLoaded()); 300 NTPSnippetsServiceLoaded());
299 return true; 301 return true;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 StoreSnippetHostsToPrefs(hosts); 338 StoreSnippetHostsToPrefs(hosts);
337 339
338 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 340 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
339 NTPSnippetsServiceLoaded()); 341 NTPSnippetsServiceLoaded());
340 342
341 FetchSnippetsFromHosts(hosts); 343 FetchSnippetsFromHosts(hosts);
342 } 344 }
343 345
344 void NTPSnippetsService::OnSnippetsDownloaded( 346 void NTPSnippetsService::OnSnippetsDownloaded(
345 const std::string& snippets_json, const std::string& status) { 347 const std::string& snippets_json, const std::string& status) {
346
347 if (!snippets_json.empty()) { 348 if (!snippets_json.empty()) {
348 DCHECK(status.empty()); 349 DCHECK(status.empty());
349 350
350 parse_json_callback_.Run( 351 parse_json_callback_.Run(
351 snippets_json, 352 snippets_json,
352 base::Bind(&NTPSnippetsService::OnJsonParsed, 353 base::Bind(&NTPSnippetsService::OnJsonParsed,
353 weak_ptr_factory_.GetWeakPtr(), snippets_json), 354 weak_ptr_factory_.GetWeakPtr(), snippets_json),
354 base::Bind(&NTPSnippetsService::OnJsonError, 355 base::Bind(&NTPSnippetsService::OnJsonError,
355 weak_ptr_factory_.GetWeakPtr(), snippets_json)); 356 weak_ptr_factory_.GetWeakPtr(), snippets_json));
356 } else { 357 } else {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) { 411 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) {
411 if (snippet->publish_date().is_null()) 412 if (snippet->publish_date().is_null())
412 snippet->set_publish_date(base::Time::Now()); 413 snippet->set_publish_date(base::Time::Now());
413 if (snippet->expiry_date().is_null()) { 414 if (snippet->expiry_date().is_null()) {
414 snippet->set_expiry_date( 415 snippet->set_expiry_date(
415 snippet->publish_date() + 416 snippet->publish_date() +
416 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); 417 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins));
417 } 418 }
418 } 419 }
419 420
421 int num_new_snippets = new_snippets.size();
422
423 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
424 switches::kAddIncompleteSnippets)) {
425 // Remove snippets that do not have all the info we need to display it to
426 // the user.
427 new_snippets.erase(
428 std::remove_if(new_snippets.begin(), new_snippets.end(),
429 [this](const std::unique_ptr<NTPSnippet>& snippet) {
Marc Treib 2016/04/30 13:51:19 No need to capture |this| here.
May 2016/05/03 17:11:00 Done.
430 return !snippet->is_complete();
431 }),
432 new_snippets.end());
433 int num_snippets_discarded = num_new_snippets - new_snippets.size();
434 if (num_snippets_discarded > 0)
Marc Treib 2016/04/30 13:51:19 I think we want to record this even if it's zero,
jwd 2016/05/02 16:00:28 I'd suggest keeping this histogram as it is, and a
May 2016/05/03 17:11:00 Done.
435 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.IncompleteSnippets",
jwd 2016/05/02 16:00:28 How many unique num_snippets_discarded do you expe
May 2016/05/03 17:11:00 Up to kMaxSnippetCount (=10 at the moment) can be
jwd 2016/05/03 19:36:46 Sounds fine, as long as you're ok with the overhea
436 num_snippets_discarded);
437 }
438
420 // Insert the new snippets at the front. 439 // Insert the new snippets at the front.
421 snippets_.insert(snippets_.begin(), 440 snippets_.insert(snippets_.begin(),
422 std::make_move_iterator(new_snippets.begin()), 441 std::make_move_iterator(new_snippets.begin()),
423 std::make_move_iterator(new_snippets.end())); 442 std::make_move_iterator(new_snippets.end()));
424 443
425 // If there are more snippets now than we want to show, drop the extra ones 444 // If there are more snippets now than we want to show, drop the extra ones
426 // from the end of the list. 445 // from the end of the list.
427 if (snippets_.size() > kMaxSnippetCount) 446 if (snippets_.size() > kMaxSnippetCount)
428 snippets_.resize(kMaxSnippetCount); 447 snippets_.resize(kMaxSnippetCount);
429 448
430 return true; 449 return true;
431 } 450 }
432 451
433 void NTPSnippetsService::LoadSnippetsFromPrefs() { 452 void NTPSnippetsService::LoadSnippetsFromPrefs() {
434 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); 453 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets));
435 DCHECK(success) << "Failed to parse snippets from prefs"; 454 DCHECK(success) << "Failed to parse snippets from prefs";
436 455
437 LoadingSnippetsFinished(); 456 LoadingSnippetsFinished();
438 } 457 }
439 458
440 void NTPSnippetsService::StoreSnippetsToPrefs() { 459 void NTPSnippetsService::StoreSnippetsToPrefs() {
441 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); 460 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_));
442 } 461 }
443 462
444 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() { 463 void NTPSnippetsService::LoadDiscardedSnippetsFromPrefs() {
445 discarded_snippets_.clear(); 464 discarded_snippets_.clear();
446 bool success = AddSnippetsFromListValue( 465 bool success = AddSnippetsFromListValue(
447 *pref_service_->GetList(prefs::kDiscardedSnippets), 466 *pref_service_->GetList(prefs::kDiscardedSnippets), &discarded_snippets_);
448 &discarded_snippets_);
449 DCHECK(success) << "Failed to parse discarded snippets from prefs"; 467 DCHECK(success) << "Failed to parse discarded snippets from prefs";
450 } 468 }
451 469
452 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() { 470 void NTPSnippetsService::StoreDiscardedSnippetsToPrefs() {
453 pref_service_->Set(prefs::kDiscardedSnippets, 471 pref_service_->Set(prefs::kDiscardedSnippets,
454 *SnippetsToListValue(discarded_snippets_)); 472 *SnippetsToListValue(discarded_snippets_));
455 } 473 }
456 474
457 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const { 475 std::set<std::string> NTPSnippetsService::GetSnippetHostsFromPrefs() const {
458 std::set<std::string> hosts; 476 std::set<std::string> hosts;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 if (snippet->expiry_date() < next_expiry) 528 if (snippet->expiry_date() < next_expiry)
511 next_expiry = snippet->expiry_date(); 529 next_expiry = snippet->expiry_date();
512 } 530 }
513 DCHECK_GT(next_expiry, expiry); 531 DCHECK_GT(next_expiry, expiry);
514 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 532 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
515 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 533 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
516 base::Unretained(this))); 534 base::Unretained(this)));
517 } 535 }
518 536
519 } // namespace ntp_snippets 537 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698