Chromium Code Reviews| Index: chrome/browser/engagement/site_engagement_service.h |
| diff --git a/chrome/browser/engagement/site_engagement_service.h b/chrome/browser/engagement/site_engagement_service.h |
| index 1d733322e887cced1264f0a6ac062984c06b8a99..66bd8e5da86aa81f1ba387d7d36f7f2a9a8d6829 100644 |
| --- a/chrome/browser/engagement/site_engagement_service.h |
| +++ b/chrome/browser/engagement/site_engagement_service.h |
| @@ -9,6 +9,7 @@ |
| #include <memory> |
| #include <set> |
| +#include "base/callback_forward.h" |
| #include "base/gtest_prod_util.h" |
| #include "base/macros.h" |
| #include "base/memory/weak_ptr.h" |
| @@ -58,6 +59,14 @@ class SiteEngagementService : public KeyedService, |
| // the service of them. |
| class Helper; |
| + // Callback used by the site engagement service to signal that a given origin |
| + // has reached a given engagement score. When invoked, the callback will be |
| + // passed the score for |url|'s origin at the time of invocation, and the URL |
| + // which led to the engagement reaching that score (or the origin if the score |
| + // was already at the requested level). |
| + using EngagementCallback = base::Callback<void(const GURL&, double)>; |
| + using EngagementCallbacks = std::vector<EngagementCallback>; |
| + |
| enum EngagementLevel { |
| ENGAGEMENT_LEVEL_NONE, |
| ENGAGEMENT_LEVEL_LOW, |
| @@ -97,6 +106,18 @@ class SiteEngagementService : public KeyedService, |
| // Returns whether |url| has at least the given |level| of engagement. |
| bool IsEngagementAtLeast(const GURL& url, EngagementLevel level) const; |
| + // Register |callback| to be run on the UI thread when the engagement for |
| + // |url|'s origin reaches |level| or |score|. Callbacks for origins which have |
|
calamity
2016/05/19 03:28:22
It's not obvious that this only happens when engag
dominickn
2016/05/19 04:36:15
I've made this comment more explicit - it's equal
|
| + // already reached the requested score will be dispatched as soon as they are |
| + // registered. Callers are responsible for ensuring that |callback| is safe to |
| + // run at any time. |
| + void RegisterCallback(const EngagementCallback& callback, |
| + const GURL& url, |
| + EngagementLevel level); |
| + void RegisterCallback(const EngagementCallback& callback, |
| + const GURL& url, |
| + double score); |
| + |
| // Resets the engagement score |url| to |score|, clearing daily limits. |
| void ResetScoreForURL(const GURL& url, double score); |
| @@ -109,6 +130,13 @@ class SiteEngagementService : public KeyedService, |
| double GetTotalEngagementPoints() const override; |
| private: |
| + // Wrapping struct for callbacks. |
| + struct EngagementCallbackWrapper { |
| + EngagementCallback* callback; |
| + GURL origin; |
| + double score; |
| + }; |
| + |
| FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); |
| FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
| FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs); |
| @@ -120,6 +148,7 @@ class SiteEngagementService : public KeyedService, |
| CleanupOriginsOnHistoryDeletion); |
| FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); |
| FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); |
| + FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, RegisterCallbacks); |
| FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); |
| FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); |
| @@ -139,6 +168,9 @@ class SiteEngagementService : public KeyedService, |
| // Returns the median engagement score of all recorded origins. |
| double GetMedianEngagement(const std::map<GURL, double>& score_map) const; |
| + // Returns the minimum engagement score corresponding to |level|. |
| + double GetScoreForEngagementLevel(EngagementLevel level) const; |
| + |
| // Update the engagement score of the origin matching |url| for media playing. |
| // The points awarded are discounted if the media is being played in a non- |
| // visible tab. |
| @@ -172,6 +204,16 @@ class SiteEngagementService : public KeyedService, |
| bool expired, |
| const history::OriginCountAndLastVisitMap& remaining_origin_counts); |
| + // Runs any callbacks in |engagement_callbacks_| which have been registered to |
| + // listen to |url|'s origin reaching at least |score| engagement. |
| + void ProcessCallbacks(const GURL& url, double score); |
| + |
| + // Dispatches |callback| to be run on the UI thread, and deletes it once it |
| + // has finished running. |
| + void PostCallback(const EngagementCallback* callback, |
| + const GURL& url, |
| + double score); |
| + |
| // Resets the engagement score for |url| to |score|, and sets the last |
| // engagement time and last shortcut launch time (if it is non-null) to |
| // |updated_time|. Clears daily limits. |
| @@ -179,6 +221,11 @@ class SiteEngagementService : public KeyedService, |
| double score, |
| const base::Time* updated_time); |
| + // Runs |callback| and deletes it once it has finished execution. |
| + void RunAndDeleteCallback(const EngagementCallback* callback, |
| + const GURL& origin, |
| + double score) const; |
| + |
| Profile* profile_; |
| // The clock used to vend times. |
| @@ -191,6 +238,10 @@ class SiteEngagementService : public KeyedService, |
| // upload. |
| base::Time last_metrics_time_; |
| + // A list of callbacks, registered with an origin and an engagement score. |
| + // Once a callback is dispatched, its wrapper will be removed from the list. |
| + std::vector<EngagementCallbackWrapper> engagement_callbacks_; |
| + |
| base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
| DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |