Chromium Code Reviews| Index: chrome/browser/engagement/site_engagement_service.cc |
| diff --git a/chrome/browser/engagement/site_engagement_service.cc b/chrome/browser/engagement/site_engagement_service.cc |
| index 087a82b301dbe19dfd41e56630dc619d9c4b7e43..0287a5ce2a9c418721f738bac3e0ed919a547ba4 100644 |
| --- a/chrome/browser/engagement/site_engagement_service.cc |
| +++ b/chrome/browser/engagement/site_engagement_service.cc |
| @@ -8,6 +8,7 @@ |
| #include <cmath> |
| #include "base/command_line.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/time/clock.h" |
| #include "base/time/default_clock.h" |
| #include "base/values.h" |
| @@ -17,6 +18,7 @@ |
| #include "chrome/common/chrome_switches.h" |
| #include "components/content_settings/core/browser/host_content_settings_map.h" |
| #include "components/content_settings/core/common/content_settings_pattern.h" |
| +#include "components/variations/variations_associated_data.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "url/gurl.h" |
| @@ -66,16 +68,64 @@ scoped_ptr<base::DictionaryValue> GetScoreDictForOrigin( |
| } // namespace |
| +const double SiteEngagementScore::kMaxPoints = 100; |
| +double SiteEngagementScore::gMaxPointsPerDay = 5; |
| +double SiteEngagementScore::gNavigationPoints = 0.5; |
| +double SiteEngagementScore::gUserInputPoints = 0.05; |
| +int SiteEngagementScore::gDecayPeriodInDays = 7; |
| +double SiteEngagementScore::gDecayPoints = 5; |
| + |
| const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
| const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
| const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
| -const double SiteEngagementScore::kMaxPoints = 100; |
| -const double SiteEngagementScore::kMaxPointsPerDay = 5; |
| -const double SiteEngagementScore::kNavigationPoints = 0.5; |
| -const double SiteEngagementScore::kUserInputPoints = 0.05; |
| -const int SiteEngagementScore::kDecayPeriodInDays = 7; |
| -const double SiteEngagementScore::kDecayPoints = 5; |
| +const char* SiteEngagementScore::kEngagementParams = "SiteEngagementParams"; |
| +const char* SiteEngagementScore::kMaxPointsPerDayParam = "max_points_per_day"; |
| +const char* SiteEngagementScore::kNavigationPointsParam = "navigation_points"; |
| +const char* SiteEngagementScore::kUserInputPointsParam = "user_input_points"; |
| +const char* SiteEngagementScore::kDecayPeriodInDaysParam = |
| + "decay_period_in_days"; |
| +const char* SiteEngagementScore::kDecayPointsParam = "decay_points"; |
| + |
| +// static |
| +void SiteEngagementScore::UpdateFromVariations() { |
| + std::string max_points_per_day_param = variations::GetVariationParamValue( |
| + kEngagementParams, kMaxPointsPerDayParam); |
| + std::string navigation_points_param = variations::GetVariationParamValue( |
| + kEngagementParams, kNavigationPointsParam); |
| + std::string user_input_points_param = variations::GetVariationParamValue( |
| + kEngagementParams, kUserInputPointsParam); |
| + std::string decay_period_in_days_param = variations::GetVariationParamValue( |
| + kEngagementParams, kDecayPeriodInDaysParam); |
| + std::string decay_points_param = variations::GetVariationParamValue( |
| + kEngagementParams, kDecayPointsParam); |
| + |
| + if (!max_points_per_day_param.empty() && !navigation_points_param.empty() && |
| + !user_input_points_param.empty() && !decay_period_in_days_param.empty() && |
| + !decay_points_param.empty()) { |
| + double max_points_per_day = 0; |
| + double navigation_points = 0; |
| + double user_input_points = 0; |
| + int decay_period_in_days = 0; |
| + double decay_points = 0; |
| + |
| + if (base::StringToDouble(max_points_per_day_param, &max_points_per_day) && |
| + base::StringToDouble(navigation_points_param, &navigation_points) && |
| + base::StringToDouble(user_input_points_param, &user_input_points) && |
| + base::StringToInt(decay_period_in_days_param, &decay_period_in_days) && |
| + base::StringToDouble(decay_points_param, &decay_points) && |
| + max_points_per_day >= navigation_points && |
| + max_points_per_day >= user_input_points && navigation_points >= 0 && |
| + user_input_points >= 0 && decay_period_in_days > 0 && |
| + decay_points >= 0) { |
| + gMaxPointsPerDay = max_points_per_day; |
| + gNavigationPoints = navigation_points; |
| + gUserInputPoints = user_input_points; |
| + gDecayPeriodInDays = decay_period_in_days; |
| + gDecayPoints = decay_points; |
| + } |
| + } |
| +} |
| SiteEngagementScore::SiteEngagementScore( |
| base::Clock* clock, |
| @@ -107,7 +157,7 @@ void SiteEngagementScore::AddPoints(double points) { |
| } |
| double to_add = |
| - std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); |
| + std::min(kMaxPoints - raw_score_, gMaxPointsPerDay - points_added_today_); |
| to_add = std::min(to_add, points); |
| points_added_today_ += to_add; |
| @@ -122,7 +172,7 @@ bool SiteEngagementScore::MaxPointsPerDayAdded() { |
| return false; |
| } |
| - return points_added_today_ == kMaxPointsPerDay; |
| + return points_added_today_ == gMaxPointsPerDay; |
| } |
| bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
| @@ -168,8 +218,8 @@ double SiteEngagementScore::DecayedScore() const { |
| if (days_since_engagement < 0) |
| return raw_score_; |
| - int periods = days_since_engagement / kDecayPeriodInDays; |
| - double decayed_score = raw_score_ - periods * kDecayPoints; |
| + int periods = days_since_engagement / gDecayPeriodInDays; |
| + double decayed_score = raw_score_ - periods * gDecayPoints; |
| return std::max(0.0, decayed_score); |
| } |
| @@ -200,7 +250,7 @@ void SiteEngagementService::HandleNavigation(const GURL& url, |
| ui::PageTransition transition) { |
| SiteEngagementMetrics::RecordEngagement( |
| SiteEngagementMetrics::ENGAGEMENT_NAVIGATION); |
| - AddPoints(url, SiteEngagementScore::kNavigationPoints); |
| + AddPoints(url, SiteEngagementScore::gNavigationPoints); |
| if (clock_->Now() - last_metrics_time_ >= metrics_interval) |
| RecordMetrics(); |
| @@ -210,7 +260,7 @@ void SiteEngagementService::HandleUserInput( |
| const GURL& url, |
| SiteEngagementMetrics::EngagementType type) { |
| SiteEngagementMetrics::RecordEngagement(type); |
| - AddPoints(url, SiteEngagementScore::kUserInputPoints); |
| + AddPoints(url, SiteEngagementScore::gUserInputPoints); |
| if (clock_->Now() - last_metrics_time_ >= metrics_interval) |
| RecordMetrics(); |
| @@ -283,6 +333,7 @@ void SiteEngagementService::AddPoints(const GURL& url, double points) { |
| void SiteEngagementService::AfterStartupTask() { |
| CleanupEngagementScores(); |
| + SiteEngagementScore::UpdateFromVariations(); |
|
benwells
2015/10/02 01:37:42
I think this is the wrong place for this, as there
dominickn
2015/10/02 06:29:05
Done.
|
| RecordMetrics(); |
| } |