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 392ee8cb5fe956d3ef7a8e3945f708ff0b588d30..d3e7dfe072e65a3654aaf1661afa60f22312a058 100644 |
| --- a/chrome/browser/engagement/site_engagement_service.cc |
| +++ b/chrome/browser/engagement/site_engagement_service.cc |
| @@ -9,6 +9,7 @@ |
| #include <vector> |
| #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" |
| @@ -18,11 +19,23 @@ |
| #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" |
| namespace { |
| +// Global bool to ensure we only update the parameters from variations once. |
| +bool gUpdatedFromVariations = false; |
| + |
| +// Keys used in the variations params. |
| +const char kEngagementParams[] = "SiteEngagementParams"; |
|
benwells
2015/10/06 02:55:50
Nit: can we just call this SiteEngagement?
dominickn
2015/10/06 05:00:39
Done.
|
| +const char kMaxPointsPerDayParam[] = "max_points_per_day"; |
| +const char kNavigationPointsParam[] = "navigation_points"; |
| +const char kUserInputPointsParam[] = "user_input_points"; |
| +const char kDecayPeriodInDaysParam[] = "decay_period_in_days"; |
| +const char kDecayPointsParam[] = "decay_points"; |
| + |
| // Length of time between metrics logging. |
| const base::TimeDelta metrics_interval = base::TimeDelta::FromMinutes(60); |
| @@ -67,16 +80,55 @@ 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; |
| +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, |
| @@ -108,7 +160,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; |
| @@ -123,7 +175,7 @@ bool SiteEngagementScore::MaxPointsPerDayAdded() { |
| return false; |
| } |
| - return points_added_today_ == kMaxPointsPerDay; |
| + return points_added_today_ == gMaxPointsPerDay; |
| } |
| bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
| @@ -169,8 +221,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); |
| } |
| @@ -192,6 +244,11 @@ SiteEngagementService::SiteEngagementService(Profile* profile) |
| content::BrowserThread::UI), |
| base::Bind(&SiteEngagementService::AfterStartupTask, |
| weak_factory_.GetWeakPtr())); |
| + |
| + if (!gUpdatedFromVariations) { |
| + SiteEngagementScore::UpdateFromVariations(); |
| + gUpdatedFromVariations = true; |
| + } |
| } |
| SiteEngagementService::~SiteEngagementService() { |
| @@ -201,7 +258,7 @@ void SiteEngagementService::HandleNavigation(const GURL& url, |
| ui::PageTransition transition) { |
| SiteEngagementMetrics::RecordEngagement( |
| SiteEngagementMetrics::ENGAGEMENT_NAVIGATION); |
| - AddPoints(url, SiteEngagementScore::kNavigationPoints); |
| + AddPoints(url, SiteEngagementScore::gNavigationPoints); |
| RecordMetrics(); |
| } |
| @@ -209,7 +266,7 @@ void SiteEngagementService::HandleUserInput( |
| const GURL& url, |
| SiteEngagementMetrics::EngagementType type) { |
| SiteEngagementMetrics::RecordEngagement(type); |
| - AddPoints(url, SiteEngagementScore::kUserInputPoints); |
| + AddPoints(url, SiteEngagementScore::gUserInputPoints); |
| RecordMetrics(); |
| } |