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 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "chrome/browser/engagement/site_engagement_metrics.h" | 17 #include "chrome/browser/engagement/site_engagement_metrics.h" |
18 #include "chrome/browser/engagement/site_engagement_observer.h" | 18 #include "chrome/browser/engagement/site_engagement_observer.h" |
19 #include "components/history/core/browser/history_service_observer.h" | 19 #include "components/history/core/browser/history_service_observer.h" |
20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
21 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" | |
21 #include "ui/base/page_transition_types.h" | 22 #include "ui/base/page_transition_types.h" |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 class Clock; | 25 class Clock; |
25 } | 26 } |
26 | 27 |
27 namespace content { | 28 namespace content { |
28 class WebContents; | 29 class WebContents; |
29 } | 30 } |
30 | 31 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 // threads using SiteEngagementService::GetScoreFromSettings, but use of this | 64 // threads using SiteEngagementService::GetScoreFromSettings, but use of this |
64 // method is discouraged unless it is not possible to use the UI thread. | 65 // method is discouraged unless it is not possible to use the UI thread. |
65 class SiteEngagementService : public KeyedService, | 66 class SiteEngagementService : public KeyedService, |
66 public history::HistoryServiceObserver, | 67 public history::HistoryServiceObserver, |
67 public SiteEngagementScoreProvider { | 68 public SiteEngagementScoreProvider { |
68 public: | 69 public: |
69 // WebContentsObserver that detects engagement triggering events and notifies | 70 // WebContentsObserver that detects engagement triggering events and notifies |
70 // the service of them. | 71 // the service of them. |
71 class Helper; | 72 class Helper; |
72 | 73 |
73 enum EngagementLevel { | |
74 ENGAGEMENT_LEVEL_NONE, | |
75 ENGAGEMENT_LEVEL_LOW, | |
76 ENGAGEMENT_LEVEL_MEDIUM, | |
77 ENGAGEMENT_LEVEL_HIGH, | |
78 ENGAGEMENT_LEVEL_MAX, | |
79 }; | |
80 | |
81 // The name of the site engagement variation field trial. | 74 // The name of the site engagement variation field trial. |
82 static const char kEngagementParams[]; | 75 static const char kEngagementParams[]; |
83 | 76 |
84 // Returns the site engagement service attached to this profile. The service | 77 // Returns the site engagement service attached to this profile. The service |
85 // exists in incognito mode; scores will be initialised using the score from | 78 // exists in incognito mode; scores will be initialised using the score from |
86 // the profile that the incognito session was created from, and will increase | 79 // the profile that the incognito session was created from, and will increase |
87 // and decrease as usual. Engagement earned or decayed in incognito will not | 80 // and decrease as usual. Engagement earned or decayed in incognito will not |
88 // be persisted or reflected in the original profile. | 81 // be persisted or reflected in the original profile. |
89 // | 82 // |
90 // This method must be called on the UI thread. | 83 // This method must be called on the UI thread. |
(...skipping 10 matching lines...) Expand all Loading... | |
101 // | 94 // |
102 // Should only be used if you cannot create a SiteEngagementService (i.e. you | 95 // Should only be used if you cannot create a SiteEngagementService (i.e. you |
103 // cannot run on the UI thread). | 96 // cannot run on the UI thread). |
104 static double GetScoreFromSettings(HostContentSettingsMap* settings, | 97 static double GetScoreFromSettings(HostContentSettingsMap* settings, |
105 const GURL& origin); | 98 const GURL& origin); |
106 | 99 |
107 explicit SiteEngagementService(Profile* profile); | 100 explicit SiteEngagementService(Profile* profile); |
108 ~SiteEngagementService() override; | 101 ~SiteEngagementService() override; |
109 | 102 |
110 // Returns the engagement level of |url|. | 103 // Returns the engagement level of |url|. |
111 EngagementLevel GetEngagementLevel(const GURL& url) const; | 104 blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url) const; |
112 | 105 |
113 // Returns a map of all stored origins and their engagement scores. | 106 // Returns a map of all stored origins and their engagement scores. |
114 std::map<GURL, double> GetScoreMap() const; | 107 std::map<GURL, double> GetScoreMap() const; |
115 | 108 |
116 // Returns whether the engagement service has enough data to make meaningful | 109 // Returns whether the engagement service has enough data to make meaningful |
117 // decisions. Clients should avoid using engagement in their heuristic until | 110 // decisions. Clients should avoid using engagement in their heuristic until |
118 // this is true. | 111 // this is true. |
119 bool IsBootstrapped() const; | 112 bool IsBootstrapped() const; |
120 | 113 |
121 // Returns whether |url| has at least the given |level| of engagement. | 114 // Returns whether |url| has at least the given |level| of engagement. |
122 bool IsEngagementAtLeast(const GURL& url, EngagementLevel level) const; | 115 bool IsEngagementAtLeast(const GURL& url, |
116 blink::mojom::EngagementLevel level) const; | |
123 | 117 |
124 // Resets the engagement score |url| to |score|, clearing daily limits. | 118 // Resets the engagement score |url| to |score|, clearing daily limits. |
125 void ResetScoreForURL(const GURL& url, double score); | 119 void ResetScoreForURL(const GURL& url, double score); |
126 | 120 |
127 // Update the last time |url| was opened from an installed shortcut to be | 121 // Update the last time |url| was opened from an installed shortcut to be |
128 // clock_->Now(). | 122 // clock_->Now(). |
129 void SetLastShortcutLaunchTime(const GURL& url); | 123 void SetLastShortcutLaunchTime(const GURL& url); |
130 | 124 |
125 void HelperCreated(SiteEngagementService::Helper* helper); | |
benwells
2016/12/09 06:11:24
This seems like an observer now. Apart from this,
dominickn
2016/12/13 06:27:37
I've revamped the Helper to make it more of a priv
benwells
2016/12/14 06:17:53
Hrmph. I think it is now more obscure. Not only is
dominickn
2016/12/15 01:57:08
Done.
I think the main thing here is that we have
| |
126 void HelperDeleted(SiteEngagementService::Helper* helper); | |
127 | |
131 // Overridden from SiteEngagementScoreProvider. | 128 // Overridden from SiteEngagementScoreProvider. |
132 double GetScore(const GURL& url) const override; | 129 double GetScore(const GURL& url) const override; |
133 double GetTotalEngagementPoints() const override; | 130 double GetTotalEngagementPoints() const override; |
134 | 131 |
135 private: | 132 private: |
136 friend class SiteEngagementObserver; | 133 friend class SiteEngagementObserver; |
137 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); | 134 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); |
138 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 135 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
139 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 136 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
140 CleanupMovesScoreBackToNow); | 137 CleanupMovesScoreBackToNow); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 // Update the engagement score of the origin loaded in |web_contents| for | 208 // Update the engagement score of the origin loaded in |web_contents| for |
212 // navigation. | 209 // navigation. |
213 void HandleNavigation(content::WebContents* web_contents, | 210 void HandleNavigation(content::WebContents* web_contents, |
214 ui::PageTransition transition); | 211 ui::PageTransition transition); |
215 | 212 |
216 // Update the engagement score of the origin loaded in |web_contents| for | 213 // Update the engagement score of the origin loaded in |web_contents| for |
217 // time-on-site, based on user input. | 214 // time-on-site, based on user input. |
218 void HandleUserInput(content::WebContents* web_contents, | 215 void HandleUserInput(content::WebContents* web_contents, |
219 SiteEngagementMetrics::EngagementType type); | 216 SiteEngagementMetrics::EngagementType type); |
220 | 217 |
218 // Called if |url| changes to |level| engagement, and informs every Helper of | |
219 // the change. | |
220 void SendLevelChangeToHelpers(const GURL& url, | |
221 blink::mojom::EngagementLevel level); | |
222 | |
221 // Returns true if the last engagement increasing event seen by the site | 223 // Returns true if the last engagement increasing event seen by the site |
222 // engagement service was sufficiently long ago that we need to reset all | 224 // engagement service was sufficiently long ago that we need to reset all |
223 // scores to be relative to now. This ensures that users who do not use the | 225 // scores to be relative to now. This ensures that users who do not use the |
224 // browser for an extended period of time do not have their engagement decay. | 226 // browser for an extended period of time do not have their engagement decay. |
225 bool IsLastEngagementStale() const; | 227 bool IsLastEngagementStale() const; |
226 | 228 |
227 // Overridden from history::HistoryServiceObserver: | 229 // Overridden from history::HistoryServiceObserver: |
228 void OnURLsDeleted(history::HistoryService* history_service, | 230 void OnURLsDeleted(history::HistoryService* history_service, |
229 bool all_history, | 231 bool all_history, |
230 bool expired, | 232 bool expired, |
(...skipping 22 matching lines...) Expand all Loading... | |
253 // The clock used to vend times. | 255 // The clock used to vend times. |
254 std::unique_ptr<base::Clock> clock_; | 256 std::unique_ptr<base::Clock> clock_; |
255 | 257 |
256 // Metrics are recorded at non-incognito browser startup, and then | 258 // Metrics are recorded at non-incognito browser startup, and then |
257 // approximately once per hour thereafter. Store the local time at which | 259 // approximately once per hour thereafter. Store the local time at which |
258 // metrics were previously uploaded: the first event which affects any | 260 // metrics were previously uploaded: the first event which affects any |
259 // origin's engagement score after an hour has elapsed triggers the next | 261 // origin's engagement score after an hour has elapsed triggers the next |
260 // upload. | 262 // upload. |
261 base::Time last_metrics_time_; | 263 base::Time last_metrics_time_; |
262 | 264 |
265 // All helpers currently attached to a WebContents. | |
266 std::set<SiteEngagementService::Helper*> helpers_; | |
267 | |
263 // A list of observers. When any origin registers an engagement-increasing | 268 // A list of observers. When any origin registers an engagement-increasing |
264 // event, each observer's OnEngagementIncreased method will be called. | 269 // event, each observer's OnEngagementIncreased method will be called. |
265 base::ObserverList<SiteEngagementObserver> observer_list_; | 270 base::ObserverList<SiteEngagementObserver> observer_list_; |
266 | 271 |
267 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 272 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
268 | 273 |
269 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 274 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
270 }; | 275 }; |
271 | 276 |
272 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 277 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |