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

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

Issue 1986033002: Implement an observer interface for the site engagement service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@site-engagement-refactor
Patch Set: Address comments Created 4 years, 6 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 <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/time/time.h" 16 #include "base/time/time.h"
16 #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"
17 #include "components/history/core/browser/history_service_observer.h" 19 #include "components/history/core/browser/history_service_observer.h"
18 #include "components/keyed_service/core/keyed_service.h" 20 #include "components/keyed_service/core/keyed_service.h"
19 #include "ui/base/page_transition_types.h" 21 #include "ui/base/page_transition_types.h"
20 22
21 namespace base { 23 namespace base {
22 class DictionaryValue; 24 class DictionaryValue;
23 class Clock; 25 class Clock;
24 } 26 }
25 27
26 namespace history { 28 namespace history {
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 104
103 // Update the last time |url| was opened from an installed shortcut to be 105 // Update the last time |url| was opened from an installed shortcut to be
104 // clock_->Now(). 106 // clock_->Now().
105 void SetLastShortcutLaunchTime(const GURL& url); 107 void SetLastShortcutLaunchTime(const GURL& url);
106 108
107 // Overridden from SiteEngagementScoreProvider. 109 // Overridden from SiteEngagementScoreProvider.
108 double GetScore(const GURL& url) const override; 110 double GetScore(const GURL& url) const override;
109 double GetTotalEngagementPoints() const override; 111 double GetTotalEngagementPoints() const override;
110 112
111 private: 113 private:
114 friend class SiteEngagementObserver;
112 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); 115 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms);
113 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); 116 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores);
114 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs); 117 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs);
115 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); 118 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement);
116 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); 119 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints);
117 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); 120 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints);
118 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); 121 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch);
119 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, 122 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest,
120 CleanupOriginsOnHistoryDeletion); 123 CleanupOriginsOnHistoryDeletion);
121 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); 124 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped);
122 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); 125 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel);
126 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, Observers);
123 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); 127 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms);
124 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); 128 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger);
125 129
126 // Only used in tests. 130 // Only used in tests.
127 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); 131 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock);
128 132
129 // Adds the specified number of points to the given origin, respecting the 133 // Adds the specified number of points to the given origin, respecting the
130 // maximum limits for the day and overall. 134 // maximum limits for the day and overall.
131 void AddPoints(const GURL& url, double points); 135 void AddPoints(const GURL& url, double points);
132 136
133 // Post startup tasks: cleaning up origins which have decayed to 0, and 137 // Post startup tasks: cleaning up origins which have decayed to 0, and
134 // logging UMA statistics. 138 // logging UMA statistics.
135 void AfterStartupTask(); 139 void AfterStartupTask();
136 void CleanupEngagementScores(); 140 void CleanupEngagementScores();
137 void RecordMetrics(); 141 void RecordMetrics();
138 142
139 // Returns the median engagement score of all recorded origins. 143 // Returns the median engagement score of all recorded origins.
140 double GetMedianEngagement(const std::map<GURL, double>& score_map) const; 144 double GetMedianEngagement(const std::map<GURL, double>& score_map) const;
141 145
146 // Returns the minimum engagement score corresponding to |level|.
147 double GetScoreForEngagementLevel(EngagementLevel level) const;
benwells 2016/06/01 01:44:45 This doesn't appear to be used or tested. Are you
dominickn 2016/06/01 02:54:13 Acknowledged, I'll remove it.
148
142 // Update the engagement score of the origin matching |url| for media playing. 149 // Update the engagement score of the origin matching |url| for media playing.
143 // The points awarded are discounted if the media is being played in a non- 150 // The points awarded are discounted if the media is being played in a non-
144 // visible tab. 151 // visible tab.
145 void HandleMediaPlaying(const GURL& url, bool is_hidden); 152 void HandleMediaPlaying(const GURL& url, bool is_hidden);
146 153
147 // Update the engagement score of the origin matching |url| for navigation. 154 // Update the engagement score of the origin matching |url| for navigation.
148 void HandleNavigation(const GURL& url, ui::PageTransition transition); 155 void HandleNavigation(const GURL& url, ui::PageTransition transition);
149 156
150 // Update the engagement score of the origin matching |url| for time-on-site, 157 // Update the engagement score of the origin matching |url| for time-on-site,
151 // based on user input. 158 // based on user input.
(...skipping 13 matching lines...) Expand all
165 int OriginsWithMaxEngagement(const std::map<GURL, double>& score_map) const; 172 int OriginsWithMaxEngagement(const std::map<GURL, double>& score_map) const;
166 173
167 // Callback for the history service when it is asked for a map of origins to 174 // Callback for the history service when it is asked for a map of origins to
168 // how many URLs corresponding to that origin remain in history. 175 // how many URLs corresponding to that origin remain in history.
169 void GetCountsAndLastVisitForOriginsComplete( 176 void GetCountsAndLastVisitForOriginsComplete(
170 history::HistoryService* history_service, 177 history::HistoryService* history_service,
171 const std::multiset<GURL>& deleted_url_origins, 178 const std::multiset<GURL>& deleted_url_origins,
172 bool expired, 179 bool expired,
173 const history::OriginCountAndLastVisitMap& remaining_origin_counts); 180 const history::OriginCountAndLastVisitMap& remaining_origin_counts);
174 181
182 // Add and remove observers of this service.
183 void AddObserver(SiteEngagementObserver* observer);
184 void RemoveObserver(SiteEngagementObserver* observer);
185
175 // Resets the engagement score for |url| to |score|, and sets the last 186 // Resets the engagement score for |url| to |score|, and sets the last
176 // engagement time and last shortcut launch time (if it is non-null) to 187 // engagement time and last shortcut launch time (if it is non-null) to
177 // |updated_time|. Clears daily limits. 188 // |updated_time|. Clears daily limits.
178 void ResetScoreAndAccessTimesForURL(const GURL& url, 189 void ResetScoreAndAccessTimesForURL(const GURL& url,
179 double score, 190 double score,
180 const base::Time* updated_time); 191 const base::Time* updated_time);
181 192
182 Profile* profile_; 193 Profile* profile_;
183 194
184 // The clock used to vend times. 195 // The clock used to vend times.
185 std::unique_ptr<base::Clock> clock_; 196 std::unique_ptr<base::Clock> clock_;
186 197
187 // Metrics are recorded at non-incognito browser startup, and then 198 // Metrics are recorded at non-incognito browser startup, and then
188 // approximately once per hour thereafter. Store the local time at which 199 // approximately once per hour thereafter. Store the local time at which
189 // metrics were previously uploaded: the first event which affects any 200 // metrics were previously uploaded: the first event which affects any
190 // origin's engagement score after an hour has elapsed triggers the next 201 // origin's engagement score after an hour has elapsed triggers the next
191 // upload. 202 // upload.
192 base::Time last_metrics_time_; 203 base::Time last_metrics_time_;
193 204
205 // A list of observers. When any origin registers an engagement-increasing
206 // event, each observer's OnEngagementIncreased method will be called.
207 base::ObserverList<SiteEngagementObserver> observer_list_;
208
194 base::WeakPtrFactory<SiteEngagementService> weak_factory_; 209 base::WeakPtrFactory<SiteEngagementService> weak_factory_;
195 210
196 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); 211 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService);
197 }; 212 };
198 213
199 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ 214 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698