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 gMaxPointsPerDay; |
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 gNavigationPoints; |
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 gUserInputPoints; |
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 gDecayPeriodInDays; |
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 gDecayPoints; |
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. Resets | 60 // Returns true if the maximum number of points today has been added. Resets |
63 // the number of points added today if the day has changed. | 61 // the number of points added today if the day has changed. |
64 bool MaxPointsPerDayAdded(); | 62 bool MaxPointsPerDayAdded(); |
65 | 63 |
66 // Updates the content settings dictionary |score_dict| with the current score | 64 // Updates the content settings dictionary |score_dict| with the current score |
67 // fields. Returns true if |score_dict| changed, otherwise return false. | 65 // fields. Returns true if |score_dict| changed, otherwise return false. |
68 bool UpdateScoreDict(base::DictionaryValue* score_dict); | 66 bool UpdateScoreDict(base::DictionaryValue* score_dict); |
69 | 67 |
70 private: | 68 private: |
69 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PartiallyEmptyDictionary); | |
70 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PopulatedDictionary); | |
71 friend class SiteEngagementScoreTest; | 71 friend class SiteEngagementScoreTest; |
72 | 72 |
73 // Keys used in the content settings dictionary. | |
74 static const char* kRawScoreKey; | |
75 static const char* kPointsAddedTodayKey; | |
76 static const char* kLastEngagementTimeKey; | |
77 | |
78 // Keys used in the variations params. | |
79 static const char* kEngagementParams; | |
benwells
2015/10/02 01:37:43
Nit: are these needed in tests? If not they should
dominickn
2015/10/02 06:29:05
Done.
| |
80 static const char* kMaxPointsPerDayParam; | |
81 static const char* kNavigationPointsParam; | |
82 static const char* kUserInputPointsParam; | |
83 static const char* kDecayPeriodInDaysParam; | |
84 static const char* kDecayPointsParam; | |
85 | |
73 // This version of the constructor is used in unit tests. | 86 // This version of the constructor is used in unit tests. |
74 explicit SiteEngagementScore(base::Clock* clock); | 87 explicit SiteEngagementScore(base::Clock* clock); |
75 | 88 |
76 // Determine the score, accounting for any decay. | 89 // Determine the score, accounting for any decay. |
77 double DecayedScore() const; | 90 double DecayedScore() const; |
78 | 91 |
79 // The clock used to vend times. Enables time travelling in tests. Owned by | 92 // The clock used to vend times. Enables time travelling in tests. Owned by |
80 // the SiteEngagementService. | 93 // the SiteEngagementService. |
81 base::Clock* clock_; | 94 base::Clock* clock_; |
82 | 95 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 // origin's engagement score after an hour has elapsed triggers the next | 189 // origin's engagement score after an hour has elapsed triggers the next |
177 // upload. | 190 // upload. |
178 base::Time last_metrics_time_; | 191 base::Time last_metrics_time_; |
179 | 192 |
180 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 193 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
181 | 194 |
182 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 195 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
183 }; | 196 }; |
184 | 197 |
185 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 198 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |