Chromium Code Reviews| 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 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 double to_add = std::min(kMaxPoints - raw_score_, | 260 double to_add = std::min(kMaxPoints - raw_score_, |
| 261 GetMaxPointsPerDay() - points_added_today_); | 261 GetMaxPointsPerDay() - points_added_today_); |
| 262 to_add = std::min(to_add, points); | 262 to_add = std::min(to_add, points); |
| 263 | 263 |
| 264 points_added_today_ += to_add; | 264 points_added_today_ += to_add; |
| 265 raw_score_ += to_add; | 265 raw_score_ += to_add; |
| 266 | 266 |
| 267 last_engagement_time_ = now; | 267 last_engagement_time_ = now; |
| 268 } | 268 } |
| 269 | 269 |
| 270 void SiteEngagementScore::Reset(double points) { | 270 void SiteEngagementScore::Reset(double points, const base::Time* updated_time) { |
| 271 raw_score_ = points; | 271 raw_score_ = points; |
| 272 points_added_today_ = 0; | 272 points_added_today_ = 0; |
| 273 | 273 |
| 274 // This must be set in order to prevent the score from decaying when read. | 274 // This must be set in order to prevent the score from decaying when read. |
| 275 last_engagement_time_ = clock_->Now(); | 275 if (updated_time == nullptr) { |
|
benwells
2016/04/18 07:01:42
Nit: Can you rearrange to be
if (updated_time) {
dominickn
2016/04/18 23:27:52
Done.
| |
| 276 last_engagement_time_ = clock_->Now(); | |
| 277 } else { | |
| 278 last_engagement_time_ = *updated_time; | |
| 279 if (!last_shortcut_launch_time_.is_null()) | |
| 280 last_shortcut_launch_time_ = *updated_time; | |
| 281 } | |
| 276 } | 282 } |
| 277 | 283 |
| 278 bool SiteEngagementScore::MaxPointsPerDayAdded() const { | 284 bool SiteEngagementScore::MaxPointsPerDayAdded() const { |
| 279 if (!last_engagement_time_.is_null() && | 285 if (!last_engagement_time_.is_null() && |
| 280 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 286 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
| 281 return false; | 287 return false; |
| 282 } | 288 } |
| 283 | 289 |
| 284 return points_added_today_ == GetMaxPointsPerDay(); | 290 return points_added_today_ == GetMaxPointsPerDay(); |
| 285 } | 291 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 void SiteEngagementService::HandleMediaPlaying(const GURL& url, | 443 void SiteEngagementService::HandleMediaPlaying(const GURL& url, |
| 438 bool is_hidden) { | 444 bool is_hidden) { |
| 439 SiteEngagementMetrics::RecordEngagement( | 445 SiteEngagementMetrics::RecordEngagement( |
| 440 is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN | 446 is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN |
| 441 : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE); | 447 : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE); |
| 442 AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints() | 448 AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints() |
| 443 : SiteEngagementScore::GetVisibleMediaPoints()); | 449 : SiteEngagementScore::GetVisibleMediaPoints()); |
| 444 RecordMetrics(); | 450 RecordMetrics(); |
| 445 } | 451 } |
| 446 | 452 |
| 447 void SiteEngagementService::ResetScoreForURL(const GURL& url, double score) { | 453 void SiteEngagementService::ResetScoreForURL(const GURL& url, double score) { |
|
benwells
2016/04/18 07:01:42
Is this still used?
dominickn
2016/04/18 23:27:52
Yes, in the WebUI code for changing engagement sco
| |
| 448 DCHECK(url.is_valid()); | 454 ResetScoreAndAccessTimesForURL(url, score, nullptr); |
| 449 DCHECK_GE(score, 0); | |
| 450 DCHECK_LE(score, SiteEngagementScore::kMaxPoints); | |
| 451 | |
| 452 HostContentSettingsMap* settings_map = | |
| 453 HostContentSettingsMapFactory::GetForProfile(profile_); | |
| 454 scoped_ptr<base::DictionaryValue> score_dict = | |
| 455 GetScoreDictForOrigin(settings_map, url); | |
| 456 SiteEngagementScore engagement_score(clock_.get(), *score_dict); | |
| 457 | |
| 458 engagement_score.Reset(score); | |
| 459 if (score == 0) { | |
| 460 settings_map->SetWebsiteSettingDefaultScope( | |
| 461 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
| 462 nullptr); | |
| 463 return; | |
| 464 } | |
| 465 | |
| 466 if (engagement_score.UpdateScoreDict(score_dict.get())) { | |
| 467 settings_map->SetWebsiteSettingDefaultScope( | |
| 468 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
| 469 score_dict.release()); | |
| 470 } | |
| 471 } | 455 } |
| 472 | 456 |
| 473 void SiteEngagementService::OnURLsDeleted( | 457 void SiteEngagementService::OnURLsDeleted( |
| 474 history::HistoryService* history_service, | 458 history::HistoryService* history_service, |
| 475 bool all_history, | 459 bool all_history, |
| 476 bool expired, | 460 bool expired, |
| 477 const history::URLRows& deleted_rows, | 461 const history::URLRows& deleted_rows, |
| 478 const std::set<GURL>& favicon_urls) { | 462 const std::set<GURL>& favicon_urls) { |
| 479 std::multiset<GURL> origins; | 463 std::multiset<GURL> origins; |
| 480 for (const history::URLRow& row : deleted_rows) | 464 for (const history::URLRow& row : deleted_rows) |
| 481 origins.insert(row.url().GetOrigin()); | 465 origins.insert(row.url().GetOrigin()); |
| 482 | 466 |
| 483 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( | 467 history::HistoryService* hs = HistoryServiceFactory::GetForProfile( |
| 484 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 468 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
| 485 hs->GetCountsForOrigins( | 469 hs->GetCountsForOrigins( |
| 486 std::set<GURL>(origins.begin(), origins.end()), | 470 std::set<GURL>(origins.begin(), origins.end()), |
| 487 base::Bind(&SiteEngagementService::GetCountsForOriginsComplete, | 471 base::Bind(&SiteEngagementService::GetCountsForOriginsComplete, |
| 488 weak_factory_.GetWeakPtr(), origins, expired)); | 472 weak_factory_.GetWeakPtr(), hs, origins, expired)); |
| 489 } | 473 } |
| 490 | 474 |
| 491 void SiteEngagementService::SetLastShortcutLaunchTime(const GURL& url) { | 475 void SiteEngagementService::SetLastShortcutLaunchTime(const GURL& url) { |
| 492 HostContentSettingsMap* settings_map = | 476 HostContentSettingsMap* settings_map = |
| 493 HostContentSettingsMapFactory::GetForProfile(profile_); | 477 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 494 scoped_ptr<base::DictionaryValue> score_dict = | 478 scoped_ptr<base::DictionaryValue> score_dict = |
| 495 GetScoreDictForOrigin(settings_map, url); | 479 GetScoreDictForOrigin(settings_map, url); |
| 496 SiteEngagementScore score(clock_.get(), *score_dict); | 480 SiteEngagementScore score(clock_.get(), *score_dict); |
| 497 | 481 |
| 498 // Record the number of days since the last launch in UMA. If the user's clock | 482 // Record the number of days since the last launch in UMA. If the user's clock |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 int total_origins = 0; | 721 int total_origins = 0; |
| 738 | 722 |
| 739 for (const auto& value : score_map) | 723 for (const auto& value : score_map) |
| 740 if (value.second == SiteEngagementScore::kMaxPoints) | 724 if (value.second == SiteEngagementScore::kMaxPoints) |
| 741 ++total_origins; | 725 ++total_origins; |
| 742 | 726 |
| 743 return total_origins; | 727 return total_origins; |
| 744 } | 728 } |
| 745 | 729 |
| 746 void SiteEngagementService::GetCountsForOriginsComplete( | 730 void SiteEngagementService::GetCountsForOriginsComplete( |
| 731 history::HistoryService* history_service, | |
| 747 const std::multiset<GURL>& deleted_origins, | 732 const std::multiset<GURL>& deleted_origins, |
| 748 bool expired, | 733 bool expired, |
| 749 const history::OriginCountMap& remaining_origins) { | 734 const history::OriginCountMap& remaining_origins) { |
| 750 for (const auto& origin_to_count : remaining_origins) { | 735 for (const auto& origin_to_count : remaining_origins) { |
| 751 GURL origin = origin_to_count.first; | 736 GURL origin = origin_to_count.first; |
| 752 int remaining = origin_to_count.second; | 737 int remaining = origin_to_count.second; |
| 753 int deleted = deleted_origins.count(origin); | 738 int deleted = deleted_origins.count(origin); |
| 754 | 739 |
| 755 // Do not update engagement scores if the deletion was an expiry, but the | 740 // Do not update engagement scores if the deletion was an expiry, but the |
| 756 // URL still has entries in history. | 741 // URL still has entries in history. |
| 757 if ((expired && remaining != 0) || deleted == 0) | 742 if ((expired && remaining != 0) || deleted == 0) |
| 758 continue; | 743 continue; |
| 759 | 744 |
| 745 // Retrieve the last visit time for this origin, and use this for the last | |
| 746 // engagement time and last shortcut launch time (if that is non-null). | |
| 747 // TODO(calamity): ensure that we have sufficient data to lookup the last | |
| 748 // visit to any URL under the origin, rather than just the origin. | |
| 749 base::Time updated_time; | |
| 750 if (!history_service->GetLastVisitTimeForURL(origin, &updated_time)) | |
| 751 updated_time = clock_->Now(); | |
|
benwells
2016/04/18 07:01:42
IIUC this will be fairly common:
- usually people
dominickn
2016/04/18 23:27:52
Yes, I agree with you here.
As it turns out, His
benwells
2016/04/19 00:18:53
Ack.
Can you also move this code to below where p
dominickn
2016/04/19 07:01:58
Done.
| |
| 752 | |
| 760 // Remove engagement proportional to the urls expired from the origin's | 753 // Remove engagement proportional to the urls expired from the origin's |
| 761 // entire history. | 754 // entire history. |
| 762 double proportion_remaining = | 755 double proportion_remaining = |
| 763 static_cast<double>(remaining) / (remaining + deleted); | 756 static_cast<double>(remaining) / (remaining + deleted); |
| 764 ResetScoreForURL(origin, proportion_remaining * GetScore(origin)); | 757 ResetScoreAndAccessTimesForURL(origin, |
| 758 proportion_remaining * GetScore(origin), | |
| 759 &updated_time); | |
| 765 } | 760 } |
| 766 } | 761 } |
| 762 | |
| 763 void SiteEngagementService::ResetScoreAndAccessTimesForURL( | |
| 764 const GURL& url, double score, const base::Time* updated_time) { | |
|
benwells
2016/04/18 07:01:42
Should this score by undecayed? That is, what happ
dominickn
2016/04/18 23:27:52
Having thought about this more, it seems counter-i
benwells
2016/04/19 00:18:53
Undecaying should not lead to any increase in enga
dominickn
2016/04/19 07:01:57
I finally understand what's going on here. Done, w
| |
| 765 DCHECK(url.is_valid()); | |
| 766 DCHECK_GE(score, 0); | |
| 767 DCHECK_LE(score, SiteEngagementScore::kMaxPoints); | |
| 768 | |
| 769 HostContentSettingsMap* settings_map = | |
| 770 HostContentSettingsMapFactory::GetForProfile(profile_); | |
| 771 scoped_ptr<base::DictionaryValue> score_dict = | |
| 772 GetScoreDictForOrigin(settings_map, url); | |
| 773 SiteEngagementScore engagement_score(clock_.get(), *score_dict); | |
| 774 | |
| 775 engagement_score.Reset(score, updated_time); | |
| 776 if (score == 0) { | |
| 777 settings_map->SetWebsiteSettingDefaultScope( | |
| 778 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
| 779 nullptr); | |
| 780 return; | |
| 781 } | |
| 782 | |
| 783 if (engagement_score.UpdateScoreDict(score_dict.get())) { | |
| 784 settings_map->SetWebsiteSettingDefaultScope( | |
| 785 url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | |
| 786 score_dict.release()); | |
| 787 } | |
| 788 } | |
| OLD | NEW |