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 "chrome/browser/engagement/site_engagement_service.h" | 5 #include "chrome/browser/engagement/site_engagement_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 double to_add = std::min(kMaxPoints - raw_score_, | 264 double to_add = std::min(kMaxPoints - raw_score_, |
265 GetMaxPointsPerDay() - points_added_today_); | 265 GetMaxPointsPerDay() - points_added_today_); |
266 to_add = std::min(to_add, points); | 266 to_add = std::min(to_add, points); |
267 | 267 |
268 points_added_today_ += to_add; | 268 points_added_today_ += to_add; |
269 raw_score_ += to_add; | 269 raw_score_ += to_add; |
270 | 270 |
271 last_engagement_time_ = now; | 271 last_engagement_time_ = now; |
272 } | 272 } |
273 | 273 |
274 void SiteEngagementScore::Reset(double points, const base::Time* updated_time) { | 274 void SiteEngagementScore::Reset(double points, |
275 const base::Time last_engagement_time) { | |
275 raw_score_ = points; | 276 raw_score_ = points; |
276 points_added_today_ = 0; | 277 points_added_today_ = 0; |
277 | 278 |
278 // This must be set in order to prevent the score from decaying when read. | 279 last_engagement_time_ = last_engagement_time; |
279 if (updated_time) { | |
280 last_engagement_time_ = *updated_time; | |
281 if (!last_shortcut_launch_time_.is_null()) | |
282 last_shortcut_launch_time_ = *updated_time; | |
283 } else { | |
284 last_engagement_time_ = clock_->Now(); | |
285 } | |
286 } | 280 } |
287 | 281 |
288 bool SiteEngagementScore::MaxPointsPerDayAdded() const { | 282 bool SiteEngagementScore::MaxPointsPerDayAdded() const { |
289 if (!last_engagement_time_.is_null() && | 283 if (!last_engagement_time_.is_null() && |
290 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 284 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
291 return false; | 285 return false; |
292 } | 286 } |
293 | 287 |
294 return points_added_today_ == GetMaxPointsPerDay(); | 288 return points_added_today_ == GetMaxPointsPerDay(); |
295 } | 289 } |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 bool is_hidden) { | 442 bool is_hidden) { |
449 SiteEngagementMetrics::RecordEngagement( | 443 SiteEngagementMetrics::RecordEngagement( |
450 is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN | 444 is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN |
451 : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE); | 445 : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE); |
452 AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints() | 446 AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints() |
453 : SiteEngagementScore::GetVisibleMediaPoints()); | 447 : SiteEngagementScore::GetVisibleMediaPoints()); |
454 RecordMetrics(); | 448 RecordMetrics(); |
455 } | 449 } |
456 | 450 |
457 void SiteEngagementService::ResetScoreForURL(const GURL& url, double score) { | 451 void SiteEngagementService::ResetScoreForURL(const GURL& url, double score) { |
458 ResetScoreAndAccessTimesForURL(url, score, nullptr); | 452 // Set the last engagement time to Now in order to prevent the newly set |
453 // score from decaying when read. | |
454 ResetScoreAndAccessTimesForURL(url, score, clock_->Now(), nullptr); | |
459 } | 455 } |
460 | 456 |
461 void SiteEngagementService::OnURLsDeleted( | 457 void SiteEngagementService::OnURLsDeleted( |
462 history::HistoryService* history_service, | 458 history::HistoryService* history_service, |
463 bool all_history, | 459 bool all_history, |
464 bool expired, | 460 bool expired, |
465 const history::URLRows& deleted_rows, | 461 const history::URLRows& deleted_rows, |
466 const std::set<GURL>& favicon_urls) { | 462 const std::set<GURL>& favicon_urls) { |
467 std::multiset<GURL> origins; | 463 std::multiset<GURL> origins; |
468 for (const history::URLRow& row : deleted_rows) | 464 for (const history::URLRow& row : deleted_rows) |
469 origins.insert(row.url().GetOrigin()); | 465 origins.insert(row.url().GetOrigin()); |
470 | 466 |
471 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 467 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
472 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 468 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
473 hs->GetCountsAndLastVisitForOrigins( | 469 hs->GetCountsAndLastVisitForOrigins( |
474 std::set<GURL>(origins.begin(), origins.end()), | 470 std::set<GURL>(origins.begin(), origins.end()), |
475 base::Bind( | 471 base::Bind( |
476 &SiteEngagementService::GetCountsAndLastVisitForOriginsComplete, | 472 &SiteEngagementService::GetCountsAndLastVisitForOriginsComplete, |
477 weak_factory_.GetWeakPtr(), hs, origins, expired)); | 473 weak_factory_.GetWeakPtr(), hs, origins, expired)); |
478 } | 474 } |
479 | 475 |
480 void SiteEngagementService::SetLastShortcutLaunchTime(const GURL& url) { | 476 void SiteEngagementService::SetLastShortcutLaunchTime(const GURL& url) { |
481 HostContentSettingsMap* settings_map = | 477 std::unique_ptr<ScopedEngagementScore> score = GetEngagementScore(url, true); |
482 HostContentSettingsMapFactory::GetForProfile(profile_); | |
483 std::unique_ptr<base::DictionaryValue> score_dict = | |
484 GetScoreDictForOrigin(settings_map, url); | |
485 SiteEngagementScore score(clock_.get(), *score_dict); | |
486 | 478 |
487 // Record the number of days since the last launch in UMA. If the user's clock | 479 // Record the number of days since the last launch in UMA. If the user's clock |
488 // has changed back in time, set this to 0. | 480 // has changed back in time, set this to 0. |
489 base::Time now = clock_->Now(); | 481 base::Time now = clock_->Now(); |
490 base::Time last_launch = score.last_shortcut_launch_time(); | 482 base::Time last_launch = score->get()->last_shortcut_launch_time(); |
491 if (!last_launch.is_null()) { | 483 if (!last_launch.is_null()) { |
492 SiteEngagementMetrics::RecordDaysSinceLastShortcutLaunch( | 484 SiteEngagementMetrics::RecordDaysSinceLastShortcutLaunch( |
493 std::max(0, (now - last_launch).InDays())); | 485 std::max(0, (now - last_launch).InDays())); |
494 } | 486 } |
495 SiteEngagementMetrics::RecordEngagement( | 487 SiteEngagementMetrics::RecordEngagement( |
496 SiteEngagementMetrics::ENGAGEMENT_WEBAPP_SHORTCUT_LAUNCH); | 488 SiteEngagementMetrics::ENGAGEMENT_WEBAPP_SHORTCUT_LAUNCH); |
497 | 489 |
498 score.set_last_shortcut_launch_time(now); | 490 score->get()->set_last_shortcut_launch_time(now); |
499 if (score.UpdateScoreDict(score_dict.get())) { | |
500 settings_map->SetWebsiteSettingDefaultScope( | |
501 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
502 score_dict.release()); | |
503 } | |
504 } | 491 } |
505 | 492 |
506 double SiteEngagementService::GetScore(const GURL& url) const { | 493 double SiteEngagementService::GetScore(const GURL& url) const { |
507 HostContentSettingsMap* settings_map = | 494 return GetEngagementScore(url)->get()->Score(); |
508 HostContentSettingsMapFactory::GetForProfile(profile_); | |
509 std::unique_ptr<base::DictionaryValue> score_dict = | |
510 GetScoreDictForOrigin(settings_map, url); | |
511 SiteEngagementScore score(clock_.get(), *score_dict); | |
512 | |
513 return score.Score(); | |
514 } | 495 } |
515 | 496 |
516 double SiteEngagementService::GetTotalEngagementPoints() const { | 497 double SiteEngagementService::GetTotalEngagementPoints() const { |
517 std::map<GURL, double> score_map = GetScoreMap(); | 498 std::map<GURL, double> score_map = GetScoreMap(); |
518 | 499 |
519 double total_score = 0; | 500 double total_score = 0; |
520 for (const auto& value : score_map) | 501 for (const auto& value : score_map) |
521 total_score += value.second; | 502 total_score += value.second; |
522 | 503 |
523 return total_score; | 504 return total_score; |
524 } | 505 } |
525 | 506 |
526 std::map<GURL, double> SiteEngagementService::GetScoreMap() const { | 507 std::map<GURL, double> SiteEngagementService::GetScoreMap() const { |
527 HostContentSettingsMap* settings_map = | 508 HostContentSettingsMap* settings_map = |
528 HostContentSettingsMapFactory::GetForProfile(profile_); | 509 HostContentSettingsMapFactory::GetForProfile(profile_); |
529 std::unique_ptr<ContentSettingsForOneType> engagement_settings = | 510 std::unique_ptr<ContentSettingsForOneType> engagement_settings = |
530 GetEngagementContentSettings(settings_map); | 511 GetEngagementContentSettings(settings_map); |
531 | 512 |
532 std::map<GURL, double> score_map; | 513 std::map<GURL, double> score_map; |
533 for (const auto& site : *engagement_settings) { | 514 for (const auto& site : *engagement_settings) { |
534 GURL origin(site.primary_pattern.ToString()); | 515 GURL origin(site.primary_pattern.ToString()); |
535 if (!origin.is_valid()) | 516 if (!origin.is_valid()) |
536 continue; | 517 continue; |
537 | 518 |
538 std::unique_ptr<base::DictionaryValue> score_dict = | 519 score_map[origin] = GetEngagementScore(origin)->get()->Score(); |
539 GetScoreDictForOrigin(settings_map, origin); | |
540 SiteEngagementScore score(clock_.get(), *score_dict); | |
541 score_map[origin] = score.Score(); | |
542 } | 520 } |
543 | 521 |
544 return score_map; | 522 return score_map; |
545 } | 523 } |
546 | 524 |
547 bool SiteEngagementService::IsBootstrapped() { | 525 bool SiteEngagementService::IsBootstrapped() { |
548 return GetTotalEngagementPoints() >= | 526 return GetTotalEngagementPoints() >= |
549 SiteEngagementScore::GetBootstrapPoints(); | 527 SiteEngagementScore::GetBootstrapPoints(); |
550 } | 528 } |
551 | 529 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 return score >= SiteEngagementScore::GetMediumEngagementBoundary(); | 562 return score >= SiteEngagementScore::GetMediumEngagementBoundary(); |
585 case ENGAGEMENT_LEVEL_HIGH: | 563 case ENGAGEMENT_LEVEL_HIGH: |
586 return score >= SiteEngagementScore::GetHighEngagementBoundary(); | 564 return score >= SiteEngagementScore::GetHighEngagementBoundary(); |
587 case ENGAGEMENT_LEVEL_MAX: | 565 case ENGAGEMENT_LEVEL_MAX: |
588 return score == SiteEngagementScore::kMaxPoints; | 566 return score == SiteEngagementScore::kMaxPoints; |
589 } | 567 } |
590 NOTREACHED(); | 568 NOTREACHED(); |
591 return false; | 569 return false; |
592 } | 570 } |
593 | 571 |
572 SiteEngagementService::ScopedEngagementScore::ScopedEngagementScore( | |
573 HostContentSettingsMap* settings_map, | |
574 const GURL& url, | |
575 base::Clock* clock, | |
576 bool update) | |
577 : score_dict_(GetScoreDictForOrigin(settings_map, url)), | |
578 score_(clock, *score_dict_), | |
579 update_(update), | |
580 url_(url), | |
581 settings_map_(settings_map) {} | |
582 | |
583 SiteEngagementService::ScopedEngagementScore::~ScopedEngagementScore() { | |
584 if (update_) { | |
585 if (score_.Score() == 0) { | |
586 settings_map_->SetWebsiteSettingDefaultScope( | |
587 url_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
588 nullptr); | |
589 return; | |
590 } | |
591 | |
592 if (score_.UpdateScoreDict(score_dict_.get())) { | |
593 settings_map_->SetWebsiteSettingDefaultScope( | |
594 url_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
595 score_dict_.release()); | |
596 } | |
597 } | |
598 } | |
599 | |
594 SiteEngagementService::SiteEngagementService(Profile* profile, | 600 SiteEngagementService::SiteEngagementService(Profile* profile, |
595 std::unique_ptr<base::Clock> clock) | 601 std::unique_ptr<base::Clock> clock) |
596 : profile_(profile), clock_(std::move(clock)), weak_factory_(this) { | 602 : profile_(profile), clock_(std::move(clock)), weak_factory_(this) { |
597 // May be null in tests. | 603 // May be null in tests. |
598 history::HistoryService* history = HistoryServiceFactory::GetForProfile( | 604 history::HistoryService* history = HistoryServiceFactory::GetForProfile( |
599 profile, ServiceAccessType::IMPLICIT_ACCESS); | 605 profile, ServiceAccessType::IMPLICIT_ACCESS); |
600 if (history) | 606 if (history) |
601 history->AddObserver(this); | 607 history->AddObserver(this); |
602 } | 608 } |
603 | 609 |
610 std::unique_ptr<SiteEngagementService::ScopedEngagementScore> | |
611 SiteEngagementService::GetEngagementScore(const GURL& url, bool update) { | |
612 return base::WrapUnique(new ScopedEngagementScore( | |
613 HostContentSettingsMapFactory::GetForProfile(profile_), url, clock_.get(), | |
614 update)); | |
615 } | |
616 | |
617 std::unique_ptr<SiteEngagementService::ScopedEngagementScore> | |
618 SiteEngagementService::GetEngagementScore(const GURL& url) const { | |
619 return base::WrapUnique(new ScopedEngagementScore( | |
620 HostContentSettingsMapFactory::GetForProfile(profile_), url, clock_.get(), | |
621 false)); | |
622 } | |
623 | |
604 void SiteEngagementService::AddPoints(const GURL& url, double points) { | 624 void SiteEngagementService::AddPoints(const GURL& url, double points) { |
605 HostContentSettingsMap* settings_map = | 625 std::unique_ptr<ScopedEngagementScore> score = GetEngagementScore(url, true); |
606 HostContentSettingsMapFactory::GetForProfile(profile_); | 626 score->get()->AddPoints(points); |
dominickn
2016/05/02 21:13:49
Nit: if you're not going to null check, then GetEn
| |
607 std::unique_ptr<base::DictionaryValue> score_dict = | |
608 GetScoreDictForOrigin(settings_map, url); | |
609 SiteEngagementScore score(clock_.get(), *score_dict); | |
610 | |
611 score.AddPoints(points); | |
612 if (score.UpdateScoreDict(score_dict.get())) { | |
613 settings_map->SetWebsiteSettingDefaultScope( | |
614 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
615 score_dict.release()); | |
616 } | |
617 } | 627 } |
618 | 628 |
619 void SiteEngagementService::AfterStartupTask() { | 629 void SiteEngagementService::AfterStartupTask() { |
620 CleanupEngagementScores(); | 630 CleanupEngagementScores(); |
621 RecordMetrics(); | 631 RecordMetrics(); |
622 } | 632 } |
623 | 633 |
624 void SiteEngagementService::CleanupEngagementScores() { | 634 void SiteEngagementService::CleanupEngagementScores() { |
625 HostContentSettingsMap* settings_map = | 635 HostContentSettingsMap* settings_map = |
626 HostContentSettingsMapFactory::GetForProfile(profile_); | 636 HostContentSettingsMapFactory::GetForProfile(profile_); |
627 std::unique_ptr<ContentSettingsForOneType> engagement_settings = | 637 std::unique_ptr<ContentSettingsForOneType> engagement_settings = |
628 GetEngagementContentSettings(settings_map); | 638 GetEngagementContentSettings(settings_map); |
629 | 639 |
630 for (const auto& site : *engagement_settings) { | 640 for (const auto& site : *engagement_settings) { |
631 GURL origin(site.primary_pattern.ToString()); | 641 GURL origin(site.primary_pattern.ToString()); |
632 if (origin.is_valid()) { | 642 if (origin.is_valid()) { |
633 std::unique_ptr<base::DictionaryValue> score_dict = | 643 // When this goes out of scope, the update will trigger a delete. |
634 GetScoreDictForOrigin(settings_map, origin); | 644 GetEngagementScore(origin, true); |
635 SiteEngagementScore score(clock_.get(), *score_dict); | |
636 if (score.Score() != 0) | |
637 continue; | |
638 } | 645 } |
639 | |
640 settings_map->SetWebsiteSettingDefaultScope( | |
641 origin, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
642 nullptr); | |
643 } | 646 } |
644 } | 647 } |
645 | 648 |
646 void SiteEngagementService::RecordMetrics() { | 649 void SiteEngagementService::RecordMetrics() { |
647 base::Time now = clock_->Now(); | 650 base::Time now = clock_->Now(); |
648 if (last_metrics_time_.is_null() || | 651 if (last_metrics_time_.is_null() || |
649 (now - last_metrics_time_).InMinutes() >= kMetricsIntervalInMinutes) { | 652 (now - last_metrics_time_).InMinutes() >= kMetricsIntervalInMinutes) { |
650 last_metrics_time_ = now; | 653 last_metrics_time_ = now; |
651 std::map<GURL, double> score_map = GetScoreMap(); | 654 std::map<GURL, double> score_map = GetScoreMap(); |
652 | 655 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
704 GetEngagementContentSettings(settings_map); | 707 GetEngagementContentSettings(settings_map); |
705 | 708 |
706 int total_origins = 0; | 709 int total_origins = 0; |
707 | 710 |
708 // We cannot call GetScoreMap as we need the score objects, not raw scores. | 711 // We cannot call GetScoreMap as we need the score objects, not raw scores. |
709 for (const auto& site : *engagement_settings) { | 712 for (const auto& site : *engagement_settings) { |
710 GURL origin(site.primary_pattern.ToString()); | 713 GURL origin(site.primary_pattern.ToString()); |
711 if (!origin.is_valid()) | 714 if (!origin.is_valid()) |
712 continue; | 715 continue; |
713 | 716 |
714 std::unique_ptr<base::DictionaryValue> score_dict = | 717 if (GetEngagementScore(origin)->get()->MaxPointsPerDayAdded()) |
715 GetScoreDictForOrigin(settings_map, origin); | |
716 SiteEngagementScore score(clock_.get(), *score_dict); | |
717 if (score.MaxPointsPerDayAdded()) | |
718 ++total_origins; | 718 ++total_origins; |
719 } | 719 } |
720 | 720 |
721 return total_origins; | 721 return total_origins; |
722 } | 722 } |
723 | 723 |
724 int SiteEngagementService::OriginsWithMaxEngagement( | 724 int SiteEngagementService::OriginsWithMaxEngagement( |
725 const std::map<GURL, double>& score_map) const { | 725 const std::map<GURL, double>& score_map) const { |
726 int total_origins = 0; | 726 int total_origins = 0; |
727 | 727 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
777 int days_since_engagement = (now - last_visit).InDays(); | 777 int days_since_engagement = (now - last_visit).InDays(); |
778 if (days_since_engagement > 0) { | 778 if (days_since_engagement > 0) { |
779 int periods = days_since_engagement / | 779 int periods = days_since_engagement / |
780 SiteEngagementScore::GetDecayPeriodInDays(); | 780 SiteEngagementScore::GetDecayPeriodInDays(); |
781 undecay = periods * SiteEngagementScore::GetDecayPoints(); | 781 undecay = periods * SiteEngagementScore::GetDecayPoints(); |
782 } | 782 } |
783 | 783 |
784 double score = | 784 double score = |
785 std::min(SiteEngagementScore::kMaxPoints, | 785 std::min(SiteEngagementScore::kMaxPoints, |
786 (proportion_remaining * GetScore(origin)) + undecay); | 786 (proportion_remaining * GetScore(origin)) + undecay); |
787 ResetScoreAndAccessTimesForURL(origin, score, &last_visit); | 787 base::Time last_shortcut_launch_time = |
788 GetEngagementScore(origin, false)->get()->last_shortcut_launch_time(); | |
789 std::unique_ptr<base::Time> new_last_shortcut_time; | |
790 if (last_shortcut_launch_time > last_visit) | |
791 new_last_shortcut_time.reset(new base::Time(last_visit)); | |
792 ResetScoreAndAccessTimesForURL(origin, score, last_visit, | |
793 new_last_shortcut_time.get()); | |
788 } | 794 } |
789 } | 795 } |
790 | 796 |
791 void SiteEngagementService::ResetScoreAndAccessTimesForURL( | 797 void SiteEngagementService::ResetScoreAndAccessTimesForURL( |
792 const GURL& url, double score, const base::Time* updated_time) { | 798 const GURL& url, |
799 double score, | |
800 const base::Time last_engagement_time, | |
801 const base::Time* last_shortcut_launch_time) { | |
793 DCHECK(url.is_valid()); | 802 DCHECK(url.is_valid()); |
794 DCHECK_GE(score, 0); | 803 DCHECK_GE(score, 0); |
795 DCHECK_LE(score, SiteEngagementScore::kMaxPoints); | 804 DCHECK_LE(score, SiteEngagementScore::kMaxPoints); |
796 | 805 |
797 HostContentSettingsMap* settings_map = | 806 std::unique_ptr<ScopedEngagementScore> engagement_score = |
798 HostContentSettingsMapFactory::GetForProfile(profile_); | 807 GetEngagementScore(url, true); |
799 std::unique_ptr<base::DictionaryValue> score_dict = | 808 engagement_score->get()->Reset(score, last_engagement_time); |
800 GetScoreDictForOrigin(settings_map, url); | 809 if (last_shortcut_launch_time) |
801 SiteEngagementScore engagement_score(clock_.get(), *score_dict); | 810 engagement_score->get()->set_last_shortcut_launch_time( |
802 | 811 *last_shortcut_launch_time); |
803 engagement_score.Reset(score, updated_time); | |
804 if (score == 0) { | |
805 settings_map->SetWebsiteSettingDefaultScope( | |
806 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
807 nullptr); | |
808 return; | |
809 } | |
810 | |
811 if (engagement_score.UpdateScoreDict(score_dict.get())) { | |
812 settings_map->SetWebsiteSettingDefaultScope( | |
813 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
814 score_dict.release()); | |
815 } | |
816 } | 812 } |
OLD | NEW |