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 12a9879123b851530a5a751d305e1a055cb77824..30fff80092c54dffcf76499a6b06be844e9602cd 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/values.h" |
| #include "chrome/browser/content_settings/host_content_settings_map_factory.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 "url/gurl.h" |
| namespace { |
| @@ -57,12 +59,60 @@ const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
| const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
| const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
| +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"; |
| + |
| 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; |
| +double SiteEngagementScore::gMaxPointsPerDay = 5; |
| +double SiteEngagementScore::gNavigationPoints = 0.5; |
| +double SiteEngagementScore::gUserInputPoints = 0.05; |
| +int SiteEngagementScore::gDecayPeriodInDays = 7; |
| +double SiteEngagementScore::gDecayPoints = 5; |
| + |
| +// static |
| +void SiteEngagementScore::UpdateFromVariations() { |
|
benwells
2015/09/29 00:40:31
Should something be calling this?
dominickn
2015/09/29 00:52:30
It will once calamity's clean up CL lands. Then I
benwells
2015/09/29 01:10:44
OK. Functions overwriting global variables feel we
|
| + 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, |
| @@ -94,7 +144,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; |
| @@ -109,7 +159,7 @@ bool SiteEngagementScore::MaxPointsPerDayAdded() { |
| points_added_today_ = 0; |
| } |
| - return points_added_today_ == kMaxPointsPerDay; |
| + return points_added_today_ == gMaxPointsPerDay; |
| } |
| bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
| @@ -155,8 +205,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); |
| } |
| @@ -181,11 +231,11 @@ SiteEngagementService::~SiteEngagementService() { |
| void SiteEngagementService::HandleNavigation(const GURL& url, |
| ui::PageTransition transition) { |
| - AddPoints(url, SiteEngagementScore::kNavigationPoints); |
| + AddPoints(url, SiteEngagementScore::gNavigationPoints); |
| } |
| void SiteEngagementService::HandleUserInput(const GURL& url) { |
| - AddPoints(url, SiteEngagementScore::kUserInputPoints); |
| + AddPoints(url, SiteEngagementScore::gUserInputPoints); |
| } |
| double SiteEngagementService::GetScore(const GURL& url) { |