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

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

Issue 1919383005: Small cleanup of SiteEngagementService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ 5 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_
6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ 6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 10
11 #include "base/gtest_prod_util.h" 11 #include "base/gtest_prod_util.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "chrome/browser/engagement/site_engagement_metrics.h" 15 #include "chrome/browser/engagement/site_engagement_metrics.h"
16 #include "components/history/core/browser/history_service_observer.h" 16 #include "components/history/core/browser/history_service_observer.h"
17 #include "components/keyed_service/core/keyed_service.h" 17 #include "components/keyed_service/core/keyed_service.h"
18 #include "ui/base/page_transition_types.h" 18 #include "ui/base/page_transition_types.h"
19 19
20 namespace base { 20 namespace base {
21 class DictionaryValue; 21 class DictionaryValue;
22 class Clock; 22 class Clock;
23 } 23 }
24 24
25 namespace history { 25 namespace history {
26 class HistoryService; 26 class HistoryService;
27 } 27 }
28 28
29 class GURL; 29 class GURL;
30 class HostContentSettingsMap;
30 class Profile; 31 class Profile;
31 32
32 class SiteEngagementScore { 33 class SiteEngagementScore {
33 public: 34 public:
34 // The parameters which can be varied via field trial. All "points" values 35 // The parameters which can be varied via field trial. All "points" values
35 // should be appended to the end of the enum prior to MAX_VARIATION. 36 // should be appended to the end of the enum prior to MAX_VARIATION.
36 enum Variation { 37 enum Variation {
37 // The maximum number of points that can be accrued in one day. 38 // The maximum number of points that can be accrued in one day.
38 MAX_POINTS_PER_DAY = 0, 39 MAX_POINTS_PER_DAY = 0,
39 40
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 // The SiteEngagementService does not take ownership of |clock|. It is the 99 // The SiteEngagementService does not take ownership of |clock|. It is the
99 // responsibility of the caller to make sure |clock| outlives this 100 // responsibility of the caller to make sure |clock| outlives this
100 // SiteEngagementScore. 101 // SiteEngagementScore.
101 SiteEngagementScore(base::Clock* clock, 102 SiteEngagementScore(base::Clock* clock,
102 const base::DictionaryValue& score_dict); 103 const base::DictionaryValue& score_dict);
103 ~SiteEngagementScore(); 104 ~SiteEngagementScore();
104 105
105 double Score() const; 106 double Score() const;
106 void AddPoints(double points); 107 void AddPoints(double points);
107 108
108 // Resets the score to |points| and resets the daily point limit. If 109 // Resets the score to |points|, resets the daily point limit and sets the
109 // |updated_time| is non-null, sets the last engagement time and last 110 // last engagement time to |last_engagement_time|.
110 // shortcut launch time (if it is non-null) to |updated_time|. Otherwise, last 111 void Reset(double points, const base::Time last_engagement_time);
111 // engagement time is set to the current time and last shortcut launch time is
112 // left unchanged.
113 // TODO(calamity): Ideally, all SiteEngagementScore methods should take a
114 // base::Time argument like this one does rather than each Score hold a
115 // pointer to a base::Clock. Then SiteEngagementScore doesn't need to worry
116 // about clock vending. See crbug.com/604305
117 void Reset(double points, const base::Time* updated_time);
118 112
119 // Returns true if the maximum number of points today has been added. 113 // Returns true if the maximum number of points today has been added.
120 bool MaxPointsPerDayAdded() const; 114 bool MaxPointsPerDayAdded() const;
121 115
122 // Get/set the last time this origin was launched from an installed shortcut. 116 // Get/set the last time this origin was launched from an installed shortcut.
123 base::Time last_shortcut_launch_time() const { 117 base::Time last_shortcut_launch_time() const {
124 return last_shortcut_launch_time_; 118 return last_shortcut_launch_time_;
125 } 119 }
126 void set_last_shortcut_launch_time(const base::Time& time) { 120 void set_last_shortcut_launch_time(const base::Time& time) {
127 last_shortcut_launch_time_ = time; 121 last_shortcut_launch_time_ = time;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); 278 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints);
285 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); 279 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch);
286 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, 280 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest,
287 CleanupOriginsOnHistoryDeletion); 281 CleanupOriginsOnHistoryDeletion);
288 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); 282 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped);
289 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); 283 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel);
290 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); 284 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms);
291 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); 285 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger);
292 FRIEND_TEST_ALL_PREFIXES(ImportantSitesUtilTest, NotificationsThenEngagement); 286 FRIEND_TEST_ALL_PREFIXES(ImportantSitesUtilTest, NotificationsThenEngagement);
293 287
288 class ScopedEngagementScore {
289 public:
290 ScopedEngagementScore(HostContentSettingsMap* settings_map,
291 const GURL& url,
292 base::Clock* clock,
293 bool update);
294 ~ScopedEngagementScore();
295
296 SiteEngagementScore* get() { return &score_; }
297
298 private:
299 std::unique_ptr<base::DictionaryValue> score_dict_;
300 SiteEngagementScore score_;
301 bool update_;
302 GURL url_;
303
304 HostContentSettingsMap* settings_map_;
305
306 DISALLOW_COPY_AND_ASSIGN(ScopedEngagementScore);
307 };
308
294 // Only used in tests. 309 // Only used in tests.
295 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); 310 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock);
296 311
312 std::unique_ptr<ScopedEngagementScore> GetEngagementScore(const GURL& url,
313 bool update);
dominickn 2016/05/02 21:13:49 I don't really like this ScopedEngagementScore. -
314
315 std::unique_ptr<ScopedEngagementScore> GetEngagementScore(
316 const GURL& url) const;
317
297 // Adds the specified number of points to the given origin, respecting the 318 // Adds the specified number of points to the given origin, respecting the
298 // maximum limits for the day and overall. 319 // maximum limits for the day and overall.
299 void AddPoints(const GURL& url, double points); 320 void AddPoints(const GURL& url, double points);
300 321
301 // Post startup tasks: cleaning up origins which have decayed to 0, and 322 // Post startup tasks: cleaning up origins which have decayed to 0, and
302 // logging UMA statistics. 323 // logging UMA statistics.
303 void AfterStartupTask(); 324 void AfterStartupTask();
304 void CleanupEngagementScores(); 325 void CleanupEngagementScores();
305 void RecordMetrics(); 326 void RecordMetrics();
306 327
307 // Returns the median engagement score of all recorded origins. 328 // Returns the median engagement score of all recorded origins.
308 double GetMedianEngagement(const std::map<GURL, double>& score_map) const; 329 double GetMedianEngagement(const std::map<GURL, double>& score_map) const;
309 330
310 // Returns the number of origins with maximum daily and total engagement 331 // Returns the number of origins with maximum daily and total engagement
311 // respectively. 332 // respectively.
312 int OriginsWithMaxDailyEngagement() const; 333 int OriginsWithMaxDailyEngagement() const;
313 int OriginsWithMaxEngagement(const std::map<GURL, double>& score_map) const; 334 int OriginsWithMaxEngagement(const std::map<GURL, double>& score_map) const;
314 335
315 void GetCountsAndLastVisitForOriginsComplete( 336 void GetCountsAndLastVisitForOriginsComplete(
316 history::HistoryService* history_service, 337 history::HistoryService* history_service,
317 const std::multiset<GURL>& deleted_url_origins, 338 const std::multiset<GURL>& deleted_url_origins,
318 bool expired, 339 bool expired,
319 const history::OriginCountAndLastVisitMap& remaining_origin_counts); 340 const history::OriginCountAndLastVisitMap& remaining_origin_counts);
320 341
321 // Resets the engagement score for |url| to |score|, and sets the last 342 // Resets the engagement score for |url| to |score|, and sets the last
322 // engagement time and last shortcut launch time (if it is non-null) to 343 // engagement time and last shortcut launch time (if it is non-null). Clears
323 // |updated_time|. Clears daily limits. 344 // daily maximum point limits.
324 void ResetScoreAndAccessTimesForURL(const GURL& url, 345 void ResetScoreAndAccessTimesForURL(
325 double score, 346 const GURL& url,
326 const base::Time* updated_time); 347 double score,
348 const base::Time last_engagement_time,
349 const base::Time* last_shortcut_launch_time);
dominickn 2016/05/02 21:13:49 Having one argument be the raw time and a second a
327 350
328 Profile* profile_; 351 Profile* profile_;
329 352
330 // The clock used to vend times. 353 // The clock used to vend times.
331 std::unique_ptr<base::Clock> clock_; 354 std::unique_ptr<base::Clock> clock_;
332 355
333 // Metrics are recorded at non-incognito browser startup, and then 356 // Metrics are recorded at non-incognito browser startup, and then
334 // approximately once per hour thereafter. Store the local time at which 357 // approximately once per hour thereafter. Store the local time at which
335 // metrics were previously uploaded: the first event which affects any 358 // metrics were previously uploaded: the first event which affects any
336 // origin's engagement score after an hour has elapsed triggers the next 359 // origin's engagement score after an hour has elapsed triggers the next
337 // upload. 360 // upload.
338 base::Time last_metrics_time_; 361 base::Time last_metrics_time_;
339 362
340 base::WeakPtrFactory<SiteEngagementService> weak_factory_; 363 base::WeakPtrFactory<SiteEngagementService> weak_factory_;
341 364
342 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); 365 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService);
343 }; 366 };
344 367
345 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ 368 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698