OLD | NEW |
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 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "chrome/browser/engagement/site_engagement_metrics.h" | 14 #include "chrome/browser/engagement/site_engagement_metrics.h" |
15 #include "components/keyed_service/core/keyed_service.h" | 15 #include "components/keyed_service/core/keyed_service.h" |
16 #include "ui/base/page_transition_types.h" | 16 #include "ui/base/page_transition_types.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class DictionaryValue; | 19 class DictionaryValue; |
20 class Clock; | 20 class Clock; |
21 } | 21 } |
22 | 22 |
23 class GURL; | 23 class GURL; |
24 class Profile; | 24 class Profile; |
25 | 25 |
26 class SiteEngagementScore { | 26 class SiteEngagementScore { |
27 public: | 27 public: |
28 // Keys used in the content settings dictionary. | |
29 static const char* kRawScoreKey; | |
30 static const char* kPointsAddedTodayKey; | |
31 static const char* kLastEngagementTimeKey; | |
32 | |
33 // The maximum number of points that are allowed. | 28 // The maximum number of points that are allowed. |
34 static const double kMaxPoints; | 29 static const double kMaxPoints; |
35 | 30 |
36 // The maximum number of points that can be accrued in one day. | 31 // The maximum number of points that can be accrued in one day. |
37 static const double kMaxPointsPerDay; | 32 static double g_max_points_per_day; |
38 | 33 |
39 // The number of points given for navigations. | 34 // The number of points given for navigations. |
40 static const double kNavigationPoints; | 35 static double g_navigation_points; |
41 | 36 |
42 // The number of points given for user input (indicating time-on-site). | 37 // The number of points given for user input (indicating time-on-site). |
43 static const double kUserInputPoints; | 38 static double g_user_input_points; |
44 | 39 |
45 // Decaying works by removing a portion of the score periodically. This | 40 // Decaying works by removing a portion of the score periodically. This |
46 // constant determines how often that happens. | 41 // constant determines how often that happens. |
47 static const int kDecayPeriodInDays; | 42 static int g_decay_period_in_days; |
48 | 43 |
49 // How much the score decays after every kDecayPeriodInDays. | 44 // How much the score decays after every kDecayPeriodInDays. |
50 static const double kDecayPoints; | 45 static double g_decay_points; |
| 46 |
| 47 // Update the default engagement settings via variations. |
| 48 static void UpdateFromVariations(); |
51 | 49 |
52 // The SiteEngagementService does not take ownership of |clock|. It is the | 50 // The SiteEngagementService does not take ownership of |clock|. It is the |
53 // responsibility of the caller to make sure |clock| outlives this | 51 // responsibility of the caller to make sure |clock| outlives this |
54 // SiteEngagementScore. | 52 // SiteEngagementScore. |
55 SiteEngagementScore(base::Clock* clock, | 53 SiteEngagementScore(base::Clock* clock, |
56 const base::DictionaryValue& score_dict); | 54 const base::DictionaryValue& score_dict); |
57 ~SiteEngagementScore(); | 55 ~SiteEngagementScore(); |
58 | 56 |
59 double Score() const; | 57 double Score() const; |
60 void AddPoints(double points); | 58 void AddPoints(double points); |
61 | 59 |
62 // Returns true if the maximum number of points today has been added. | 60 // Returns true if the maximum number of points today has been added. |
63 bool MaxPointsPerDayAdded(); | 61 bool MaxPointsPerDayAdded(); |
64 | 62 |
65 // Updates the content settings dictionary |score_dict| with the current score | 63 // Updates the content settings dictionary |score_dict| with the current score |
66 // fields. Returns true if |score_dict| changed, otherwise return false. | 64 // fields. Returns true if |score_dict| changed, otherwise return false. |
67 bool UpdateScoreDict(base::DictionaryValue* score_dict); | 65 bool UpdateScoreDict(base::DictionaryValue* score_dict); |
68 | 66 |
69 private: | 67 private: |
| 68 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PartiallyEmptyDictionary); |
| 69 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PopulatedDictionary); |
70 friend class SiteEngagementScoreTest; | 70 friend class SiteEngagementScoreTest; |
71 | 71 |
| 72 // Keys used in the content settings dictionary. |
| 73 static const char* kRawScoreKey; |
| 74 static const char* kPointsAddedTodayKey; |
| 75 static const char* kLastEngagementTimeKey; |
| 76 |
72 // This version of the constructor is used in unit tests. | 77 // This version of the constructor is used in unit tests. |
73 explicit SiteEngagementScore(base::Clock* clock); | 78 explicit SiteEngagementScore(base::Clock* clock); |
74 | 79 |
75 // Determine the score, accounting for any decay. | 80 // Determine the score, accounting for any decay. |
76 double DecayedScore() const; | 81 double DecayedScore() const; |
77 | 82 |
78 // The clock used to vend times. Enables time travelling in tests. Owned by | 83 // The clock used to vend times. Enables time travelling in tests. Owned by |
79 // the SiteEngagementService. | 84 // the SiteEngagementService. |
80 base::Clock* clock_; | 85 base::Clock* clock_; |
81 | 86 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // origin's engagement score after an hour has elapsed triggers the next | 184 // origin's engagement score after an hour has elapsed triggers the next |
180 // upload. | 185 // upload. |
181 base::Time last_metrics_time_; | 186 base::Time last_metrics_time_; |
182 | 187 |
183 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 188 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
184 | 189 |
185 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 190 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
186 }; | 191 }; |
187 | 192 |
188 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 193 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |