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

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

Issue 2427343002: Make the site engagement service more robust to clock changes and time inconsistencies. (Closed)
Patch Set: Created 4 years, 2 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 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 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <utility> 10 #include <utility>
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 // This method should not be called with |update_last_engagement_time| = true 292 // This method should not be called with |update_last_engagement_time| = true
293 // if the last engagement time isn't stale. 293 // if the last engagement time isn't stale.
294 DCHECK(!update_last_engagement_time || IsLastEngagementStale()); 294 DCHECK(!update_last_engagement_time || IsLastEngagementStale());
295 295
296 HostContentSettingsMap* settings_map = 296 HostContentSettingsMap* settings_map =
297 HostContentSettingsMapFactory::GetForProfile(profile_); 297 HostContentSettingsMapFactory::GetForProfile(profile_);
298 std::unique_ptr<ContentSettingsForOneType> engagement_settings = 298 std::unique_ptr<ContentSettingsForOneType> engagement_settings =
299 GetEngagementContentSettings(settings_map); 299 GetEngagementContentSettings(settings_map);
300 300
301 // We want to rebase last engagement times relative to MaxDecaysPerScore 301 // We want to rebase last engagement times relative to MaxDecaysPerScore
302 // periods of decay in the past. 302 // periods of decay in the past. If |update_last_engagement_time| = true, we
303 // should have last_engagement_time < rebase_time < now.
303 base::Time now = clock_->Now(); 304 base::Time now = clock_->Now();
304 base::Time last_engagement_time = GetLastEngagementTime(); 305 base::Time last_engagement_time = GetLastEngagementTime();
305 base::Time rebase_time = now - GetMaxDecayPeriod(); 306 base::Time rebase_time = now - GetMaxDecayPeriod();
306 base::Time new_last_engagement_time; 307 base::Time new_last_engagement_time;
308 DCHECK(!update_last_engagement_time ||
309 (last_engagement_time < rebase_time && rebase_time < now));
310
307 for (const auto& site : *engagement_settings) { 311 for (const auto& site : *engagement_settings) {
308 GURL origin(site.primary_pattern.ToString()); 312 GURL origin(site.primary_pattern.ToString());
309 313
310 if (origin.is_valid()) { 314 if (origin.is_valid()) {
311 SiteEngagementScore score = CreateEngagementScore(origin); 315 SiteEngagementScore score = CreateEngagementScore(origin);
312 if (update_last_engagement_time) { 316 if (update_last_engagement_time) {
313 // Work out the offset between this score's last engagement time and the 317 // Catch cases of users moving their clocks, or a potential race where
314 // last time the service recorded any engagement. Set the score's last 318 // a score content setting is written out to prefs, but the updated
315 // engagement time to rebase_time - offset to preserve its state, 319 // |last_engagement_time| was not written, as both are lossy
316 // relative to the rebase date. This ensures that the score will decay 320 // preferences.
317 // the next time it is used, but will not decay too much. 321 if (score.last_engagement_time() > rebase_time) {
318 DCHECK_LE(score.last_engagement_time(), rebase_time); 322 score.set_last_engagement_time(now);
319 base::TimeDelta offset = 323 } else if (score.last_engagement_time() > last_engagement_time) {
320 last_engagement_time - score.last_engagement_time(); 324 // This score is newer than |last_engagement_time|, but older than
321 base::Time rebase_score_time = rebase_time - offset; 325 // |rebase_time|. It should still be rebased with no offset as we
322 score.set_last_engagement_time(rebase_score_time); 326 // don't accurately know what the offset should be.
323 if (rebase_score_time > new_last_engagement_time) 327 score.set_last_engagement_time(rebase_time);
324 new_last_engagement_time = rebase_score_time; 328 } else {
329 // Work out the offset between this score's last engagement time and
330 // the last time the service recorded any engagement. Set the score's
331 // last engagement time to rebase_time - offset to preserve its state,
332 // relative to the rebase date. This ensures that the score will decay
333 // the next time it is used, but will not decay too much.
334 base::TimeDelta offset =
335 last_engagement_time - score.last_engagement_time();
336 base::Time rebase_score_time = rebase_time - offset;
337 score.set_last_engagement_time(rebase_score_time);
338 }
325 339
340 if (score.last_engagement_time() > new_last_engagement_time)
341 new_last_engagement_time = score.last_engagement_time();
326 score.Commit(); 342 score.Commit();
327 } 343 }
328 344
329 if (score.GetScore() > SiteEngagementScore::GetScoreCleanupThreshold()) 345 if (score.GetScore() > SiteEngagementScore::GetScoreCleanupThreshold())
330 continue; 346 continue;
331 } 347 }
332 348
333 // This origin has a score of 0. Wipe it from content settings. 349 // This origin has a score of 0. Wipe it from content settings.
334 settings_map->SetWebsiteSettingDefaultScope( 350 settings_map->SetWebsiteSettingDefaultScope(
335 origin, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), 351 origin, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(),
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 if (!engagement_score.last_shortcut_launch_time().is_null() && 613 if (!engagement_score.last_shortcut_launch_time().is_null() &&
598 engagement_score.last_shortcut_launch_time() > last_visit) { 614 engagement_score.last_shortcut_launch_time() > last_visit) {
599 engagement_score.set_last_shortcut_launch_time(last_visit); 615 engagement_score.set_last_shortcut_launch_time(last_visit);
600 } 616 }
601 617
602 engagement_score.Commit(); 618 engagement_score.Commit();
603 } 619 }
604 620
605 SetLastEngagementTime(now); 621 SetLastEngagementTime(now);
606 } 622 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698