Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/engagement/site_engagement_score.cc

Issue 1919383005: Small cleanup of SiteEngagementService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address nit + rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_score.h" 5 #include "chrome/browser/engagement/site_engagement_score.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/memory/ptr_util.h"
9 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
10 #include "base/time/clock.h" 11 #include "base/time/clock.h"
11 #include "base/time/time.h" 12 #include "base/time/time.h"
12 #include "base/values.h" 13 #include "base/values.h"
13 #include "chrome/browser/engagement/site_engagement_metrics.h" 14 #include "chrome/browser/engagement/site_engagement_metrics.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "components/variations/variations_associated_data.h" 16 #include "components/variations/variations_associated_data.h"
15 17
16 namespace { 18 namespace {
17 19
18 // Delta within which to consider scores equal. 20 // Delta within which to consider scores equal.
19 const double kScoreDelta = 0.001; 21 const double kScoreDelta = 0.001;
20 22
21 // Delta within which to consider internal time values equal. Internal time 23 // Delta within which to consider internal time values equal. Internal time
22 // values are in microseconds, so this delta comes out at one second. 24 // values are in microseconds, so this delta comes out at one second.
23 const double kTimeDelta = 1000000; 25 const double kTimeDelta = 1000000;
(...skipping 16 matching lines...) Expand all
40 "first_daily_engagement_points", 42 "first_daily_engagement_points",
41 "medium_engagement_boundary", 43 "medium_engagement_boundary",
42 "high_engagement_boundary", 44 "high_engagement_boundary",
43 }; 45 };
44 46
45 bool DoublesConsideredDifferent(double value1, double value2, double delta) { 47 bool DoublesConsideredDifferent(double value1, double value2, double delta) {
46 double abs_difference = fabs(value1 - value2); 48 double abs_difference = fabs(value1 - value2);
47 return abs_difference > delta; 49 return abs_difference > delta;
48 } 50 }
49 51
52 std::unique_ptr<base::DictionaryValue> GetScoreDictForOrigin(
53 HostContentSettingsMap* settings,
54 const GURL& origin_url) {
55 if (!settings)
56 return std::unique_ptr<base::DictionaryValue>();
57
58 std::unique_ptr<base::Value> value = settings->GetWebsiteSetting(
59 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
60 std::string(), NULL);
61 if (!value.get())
62 return base::WrapUnique(new base::DictionaryValue());
63
64 if (!value->IsType(base::Value::TYPE_DICTIONARY))
65 return base::WrapUnique(new base::DictionaryValue());
66
67 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release()));
68 }
69
50 } // namespace 70 } // namespace
51 71
52 const double SiteEngagementScore::kMaxPoints = 100; 72 const double SiteEngagementScore::kMaxPoints = 100;
53 double SiteEngagementScore::param_values[] = { 73 double SiteEngagementScore::param_values[] = {
54 5, // MAX_POINTS_PER_DAY 74 5, // MAX_POINTS_PER_DAY
55 7, // DECAY_PERIOD_IN_DAYS 75 7, // DECAY_PERIOD_IN_DAYS
56 5, // DECAY_POINTS 76 5, // DECAY_POINTS
57 0.5, // NAVIGATION_POINTS 77 0.5, // NAVIGATION_POINTS
58 0.2, // USER_INPUT_POINTS 78 0.2, // USER_INPUT_POINTS
59 0.02, // VISIBLE_MEDIA_POINTS 79 0.02, // VISIBLE_MEDIA_POINTS
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 } 156 }
137 } 157 }
138 158
139 // Once we're sure everything is valid, assign the variation to the param 159 // Once we're sure everything is valid, assign the variation to the param
140 // values array. 160 // values array.
141 for (int i = 0; i < MAX_VARIATION; ++i) 161 for (int i = 0; i < MAX_VARIATION; ++i)
142 SiteEngagementScore::param_values[i] = param_vals[i]; 162 SiteEngagementScore::param_values[i] = param_vals[i];
143 } 163 }
144 164
145 SiteEngagementScore::SiteEngagementScore(base::Clock* clock, 165 SiteEngagementScore::SiteEngagementScore(base::Clock* clock,
146 const base::DictionaryValue& score_dict) 166 const GURL& origin,
147 : SiteEngagementScore(clock) { 167 HostContentSettingsMap* settings_map)
148 score_dict.GetDouble(kRawScoreKey, &raw_score_); 168 : SiteEngagementScore(clock, GetScoreDictForOrigin(settings_map, origin)) {
149 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); 169 origin_ = origin;
150 170 settings_map_ = settings_map;
151 double internal_time;
152 if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time))
153 last_engagement_time_ = base::Time::FromInternalValue(internal_time);
154 if (score_dict.GetDouble(kLastShortcutLaunchTimeKey, &internal_time))
155 last_shortcut_launch_time_ = base::Time::FromInternalValue(internal_time);
156 } 171 }
157 172
173 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default;
174
158 SiteEngagementScore::~SiteEngagementScore() {} 175 SiteEngagementScore::~SiteEngagementScore() {}
159 176
177 SiteEngagementScore& SiteEngagementScore::operator=(
178 SiteEngagementScore&& other) = default;
179
160 void SiteEngagementScore::AddPoints(double points) { 180 void SiteEngagementScore::AddPoints(double points) {
161 DCHECK_NE(0, points); 181 DCHECK_NE(0, points);
162 double decayed_score = DecayedScore(); 182 double decayed_score = DecayedScore();
163 183
164 // Record the original and decayed scores after a decay event. 184 // Record the original and decayed scores after a decay event.
165 if (decayed_score < raw_score_) { 185 if (decayed_score < raw_score_) {
166 SiteEngagementMetrics::RecordScoreDecayedFrom(raw_score_); 186 SiteEngagementMetrics::RecordScoreDecayedFrom(raw_score_);
167 SiteEngagementMetrics::RecordScoreDecayedTo(decayed_score); 187 SiteEngagementMetrics::RecordScoreDecayedTo(decayed_score);
168 } 188 }
169 189
(...skipping 21 matching lines...) Expand all
191 points_added_today_ += to_add; 211 points_added_today_ += to_add;
192 raw_score_ += to_add; 212 raw_score_ += to_add;
193 213
194 last_engagement_time_ = now; 214 last_engagement_time_ = now;
195 } 215 }
196 216
197 double SiteEngagementScore::GetScore() const { 217 double SiteEngagementScore::GetScore() const {
198 return std::min(DecayedScore() + BonusScore(), kMaxPoints); 218 return std::min(DecayedScore() + BonusScore(), kMaxPoints);
199 } 219 }
200 220
221 void SiteEngagementScore::Commit() {
222 if (!UpdateScoreDict(score_dict_.get()))
223 return;
224
225 settings_map_->SetWebsiteSettingDefaultScope(
226 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(),
227 score_dict_.release());
228 }
229
201 bool SiteEngagementScore::MaxPointsPerDayAdded() const { 230 bool SiteEngagementScore::MaxPointsPerDayAdded() const {
202 if (!last_engagement_time_.is_null() && 231 if (!last_engagement_time_.is_null() &&
203 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { 232 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) {
204 return false; 233 return false;
205 } 234 }
206 235
207 return points_added_today_ == GetMaxPointsPerDay(); 236 return points_added_today_ == GetMaxPointsPerDay();
208 } 237 }
209 238
210 void SiteEngagementScore::Reset(double points, const base::Time* updated_time) { 239 void SiteEngagementScore::Reset(double points,
240 const base::Time last_engagement_time) {
211 raw_score_ = points; 241 raw_score_ = points;
212 points_added_today_ = 0; 242 points_added_today_ = 0;
213 243
214 // This must be set in order to prevent the score from decaying when read. 244 // This must be set in order to prevent the score from decaying when read.
215 if (updated_time) { 245 last_engagement_time_ = last_engagement_time;
216 last_engagement_time_ = *updated_time;
217 if (!last_shortcut_launch_time_.is_null())
218 last_shortcut_launch_time_ = *updated_time;
219 } else {
220 last_engagement_time_ = clock_->Now();
221 }
222 } 246 }
223 247
224 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { 248 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) {
225 double raw_score_orig = 0; 249 double raw_score_orig = 0;
226 double points_added_today_orig = 0; 250 double points_added_today_orig = 0;
227 double last_engagement_time_internal_orig = 0; 251 double last_engagement_time_internal_orig = 0;
228 double last_shortcut_launch_time_internal_orig = 0; 252 double last_shortcut_launch_time_internal_orig = 0;
229 253
230 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); 254 score_dict->GetDouble(kRawScoreKey, &raw_score_orig);
231 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); 255 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig);
(...skipping 18 matching lines...) Expand all
250 score_dict->SetDouble(kRawScoreKey, raw_score_); 274 score_dict->SetDouble(kRawScoreKey, raw_score_);
251 score_dict->SetDouble(kPointsAddedTodayKey, points_added_today_); 275 score_dict->SetDouble(kPointsAddedTodayKey, points_added_today_);
252 score_dict->SetDouble(kLastEngagementTimeKey, 276 score_dict->SetDouble(kLastEngagementTimeKey,
253 last_engagement_time_.ToInternalValue()); 277 last_engagement_time_.ToInternalValue());
254 score_dict->SetDouble(kLastShortcutLaunchTimeKey, 278 score_dict->SetDouble(kLastShortcutLaunchTimeKey,
255 last_shortcut_launch_time_.ToInternalValue()); 279 last_shortcut_launch_time_.ToInternalValue());
256 280
257 return true; 281 return true;
258 } 282 }
259 283
260 SiteEngagementScore::SiteEngagementScore(base::Clock* clock) 284 SiteEngagementScore::SiteEngagementScore(
285 base::Clock* clock,
286 std::unique_ptr<base::DictionaryValue> score_dict)
261 : clock_(clock), 287 : clock_(clock),
262 raw_score_(0), 288 raw_score_(0),
263 points_added_today_(0), 289 points_added_today_(0),
264 last_engagement_time_(), 290 last_engagement_time_(),
265 last_shortcut_launch_time_() {} 291 last_shortcut_launch_time_(),
292 score_dict_(score_dict.release()) {
293 if (!score_dict_)
294 return;
295
296 score_dict_->GetDouble(kRawScoreKey, &raw_score_);
297 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_);
298
299 double internal_time;
300 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time))
301 last_engagement_time_ = base::Time::FromInternalValue(internal_time);
302 if (score_dict_->GetDouble(kLastShortcutLaunchTimeKey, &internal_time))
303 last_shortcut_launch_time_ = base::Time::FromInternalValue(internal_time);
304 }
266 305
267 double SiteEngagementScore::DecayedScore() const { 306 double SiteEngagementScore::DecayedScore() const {
268 // Note that users can change their clock, so from this system's perspective 307 // Note that users can change their clock, so from this system's perspective
269 // time can go backwards. If that does happen and the system detects that the 308 // time can go backwards. If that does happen and the system detects that the
270 // current day is earlier than the last engagement, no decay (or growth) is 309 // current day is earlier than the last engagement, no decay (or growth) is
271 // applied. 310 // applied.
272 int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays(); 311 int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays();
273 if (days_since_engagement < 0) 312 if (days_since_engagement < 0)
274 return raw_score_; 313 return raw_score_;
275 314
(...skipping 20 matching lines...) Expand all
296 param_values[HIDDEN_MEDIA_POINTS] = 0.01; 335 param_values[HIDDEN_MEDIA_POINTS] = 0.01;
297 param_values[WEB_APP_INSTALLED_POINTS] = 5; 336 param_values[WEB_APP_INSTALLED_POINTS] = 5;
298 param_values[BOOTSTRAP_POINTS] = 8; 337 param_values[BOOTSTRAP_POINTS] = 8;
299 param_values[MEDIUM_ENGAGEMENT_BOUNDARY] = 5; 338 param_values[MEDIUM_ENGAGEMENT_BOUNDARY] = 5;
300 param_values[HIGH_ENGAGEMENT_BOUNDARY] = 50; 339 param_values[HIGH_ENGAGEMENT_BOUNDARY] = 50;
301 340
302 // This is set to zero to avoid interference with tests and is set when 341 // This is set to zero to avoid interference with tests and is set when
303 // testing this functionality. 342 // testing this functionality.
304 param_values[FIRST_DAILY_ENGAGEMENT] = 0; 343 param_values[FIRST_DAILY_ENGAGEMENT] = 0;
305 } 344 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_score.h ('k') | chrome/browser/engagement/site_engagement_score_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698