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

Unified Diff: chrome/browser/engagement/site_engagement_service.cc

Issue 1975723002: Reduce the site engagement service public interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Push messaging now uses engagement Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/engagement/site_engagement_service.cc
diff --git a/chrome/browser/engagement/site_engagement_service.cc b/chrome/browser/engagement/site_engagement_service.cc
index df709189e6bfac5542aad42bf67fcacb6e6c1e24..fa8f699fa73d44699dceb25ff8fd40be2f399503 100644
--- a/chrome/browser/engagement/site_engagement_service.cc
+++ b/chrome/browser/engagement/site_engagement_service.cc
@@ -7,14 +7,12 @@
#include <stddef.h>
#include <algorithm>
-#include <cmath>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
-#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
@@ -23,14 +21,14 @@
#include "chrome/browser/banners/app_banner_settings_helper.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/engagement/site_engagement_eviction_policy.h"
-#include "chrome/browser/engagement/site_engagement_helper.h"
+#include "chrome/browser/engagement/site_engagement_metrics.h"
+#include "chrome/browser/engagement/site_engagement_score.h"
#include "chrome/browser/engagement/site_engagement_service_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/history/core/browser/history_service.h"
-#include "components/variations/variations_associated_data.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
@@ -41,36 +39,9 @@ const int FOUR_WEEKS_IN_DAYS = 28;
// Global bool to ensure we only update the parameters from variations once.
bool g_updated_from_variations = false;
-// Keys used in the variations params. Order matches
-// SiteEngagementScore::Variation enum.
-const char* kVariationNames[] = {
- "max_points_per_day",
- "decay_period_in_days",
- "decay_points",
- "navigation_points",
- "user_input_points",
- "visible_media_playing_points",
- "hidden_media_playing_points",
- "web_app_installed_points",
- "first_daily_engagement_points",
- "medium_engagement_boundary",
- "high_engagement_boundary",
-};
-
// Length of time between metrics logging.
const int kMetricsIntervalInMinutes = 60;
-// Delta within which to consider scores equal.
-const double kScoreDelta = 0.001;
-
-// Delta within which to consider internal time values equal. Internal time
-// values are in microseconds, so this delta comes out at one second.
-const double kTimeDelta = 1000000;
-
-// Number of days after the last launch of an origin from an installed shortcut
-// for which WEB_APP_INSTALLED_POINTS will be added to the engagement score.
-const int kMaxDaysSinceShortcutLaunch = 10;
-
std::unique_ptr<ContentSettingsForOneType> GetEngagementContentSettings(
HostContentSettingsMap* settings_map) {
std::unique_ptr<ContentSettingsForOneType> engagement_settings(
@@ -80,27 +51,6 @@ std::unique_ptr<ContentSettingsForOneType> GetEngagementContentSettings(
return engagement_settings;
}
-bool DoublesConsideredDifferent(double value1, double value2, double delta) {
- double abs_difference = fabs(value1 - value2);
- return abs_difference > delta;
-}
-
-// Only accept a navigation event for engagement if it is one of:
-// a. direct typed navigation
-// b. clicking on an omnibox suggestion brought up by typing a keyword
-// c. clicking on a bookmark or opening a bookmark app
-// d. a custom search engine keyword search (e.g. Wikipedia search box added as
-// search engine).
-bool IsEngagementNavigation(ui::PageTransition transition) {
- return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_TYPED) ||
- ui::PageTransitionCoreTypeIs(transition,
- ui::PAGE_TRANSITION_GENERATED) ||
- ui::PageTransitionCoreTypeIs(transition,
- ui::PAGE_TRANSITION_AUTO_BOOKMARK) ||
- ui::PageTransitionCoreTypeIs(transition,
- ui::PAGE_TRANSITION_KEYWORD_GENERATED);
-}
-
std::unique_ptr<base::DictionaryValue> GetScoreDictForOrigin(
HostContentSettingsMap* settings,
const GURL& origin_url) {
@@ -119,263 +69,23 @@ std::unique_ptr<base::DictionaryValue> GetScoreDictForOrigin(
return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release()));
}
-} // namespace
-
-const double SiteEngagementScore::kMaxPoints = 100;
-double SiteEngagementScore::param_values[] = {
- 5, // MAX_POINTS_PER_DAY
- 7, // DECAY_PERIOD_IN_DAYS
- 5, // DECAY_POINTS
- 0.5, // NAVIGATION_POINTS
- 0.2, // USER_INPUT_POINTS
- 0.02, // VISIBLE_MEDIA_POINTS
- 0.01, // HIDDEN_MEDIA_POINTS
- 5, // WEB_APP_INSTALLED_POINTS
- 0.5, // FIRST_DAILY_ENGAGEMENT
- 8, // BOOTSTRAP_POINTS
- 5, // MEDIUM_ENGAGEMENT_BOUNDARY
- 50, // HIGH_ENGAGEMENT_BOUNDARY
-};
-
-const char* SiteEngagementScore::kRawScoreKey = "rawScore";
-const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday";
-const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime";
-const char* SiteEngagementScore::kLastShortcutLaunchTimeKey =
- "lastShortcutLaunchTime";
-
-double SiteEngagementScore::GetMaxPointsPerDay() {
- return param_values[MAX_POINTS_PER_DAY];
-}
-
-double SiteEngagementScore::GetDecayPeriodInDays() {
- return param_values[DECAY_PERIOD_IN_DAYS];
-}
-
-double SiteEngagementScore::GetDecayPoints() {
- return param_values[DECAY_POINTS];
-}
-
-double SiteEngagementScore::GetNavigationPoints() {
- return param_values[NAVIGATION_POINTS];
-}
-
-double SiteEngagementScore::GetUserInputPoints() {
- return param_values[USER_INPUT_POINTS];
-}
-
-double SiteEngagementScore::GetVisibleMediaPoints() {
- return param_values[VISIBLE_MEDIA_POINTS];
-}
-
-double SiteEngagementScore::GetHiddenMediaPoints() {
- return param_values[HIDDEN_MEDIA_POINTS];
-}
-
-double SiteEngagementScore::GetWebAppInstalledPoints() {
- return param_values[WEB_APP_INSTALLED_POINTS];
-}
-
-double SiteEngagementScore::GetFirstDailyEngagementPoints() {
- return param_values[FIRST_DAILY_ENGAGEMENT];
-}
-
-double SiteEngagementScore::GetBootstrapPoints() {
- return param_values[BOOTSTRAP_POINTS];
-}
-
-double SiteEngagementScore::GetMediumEngagementBoundary() {
- return param_values[MEDIUM_ENGAGEMENT_BOUNDARY];
-}
-
-double SiteEngagementScore::GetHighEngagementBoundary() {
- return param_values[HIGH_ENGAGEMENT_BOUNDARY];
-}
-
-void SiteEngagementScore::UpdateFromVariations() {
- double param_vals[MAX_VARIATION];
-
- for (int i = 0; i < MAX_VARIATION; ++i) {
- std::string param_string = variations::GetVariationParamValue(
- SiteEngagementService::kEngagementParams, kVariationNames[i]);
-
- // Bail out if we didn't get a param string for the key, or if we couldn't
- // convert the param string to a double, or if we get a negative value.
- if (param_string.empty() ||
- !base::StringToDouble(param_string, &param_vals[i]) ||
- param_vals[i] < 0) {
- return;
- }
- }
-
- // Once we're sure everything is valid, assign the variation to the param
- // values array.
- for (int i = 0; i < MAX_VARIATION; ++i)
- SiteEngagementScore::param_values[i] = param_vals[i];
-}
-
-SiteEngagementScore::SiteEngagementScore(
- base::Clock* clock,
- const base::DictionaryValue& score_dict)
- : SiteEngagementScore(clock) {
- score_dict.GetDouble(kRawScoreKey, &raw_score_);
- score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_);
-
- double internal_time;
- if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time))
- last_engagement_time_ = base::Time::FromInternalValue(internal_time);
- if (score_dict.GetDouble(kLastShortcutLaunchTimeKey, &internal_time))
- last_shortcut_launch_time_ = base::Time::FromInternalValue(internal_time);
-}
-
-SiteEngagementScore::~SiteEngagementScore() {
-}
-
-double SiteEngagementScore::Score() const {
- return std::min(DecayedScore() + BonusScore(), kMaxPoints);
-}
-
-void SiteEngagementScore::AddPoints(double points) {
- DCHECK_NE(0, points);
- double decayed_score = DecayedScore();
-
- // Record the original and decayed scores after a decay event.
- if (decayed_score < raw_score_) {
- SiteEngagementMetrics::RecordScoreDecayedFrom(raw_score_);
- SiteEngagementMetrics::RecordScoreDecayedTo(decayed_score);
- }
-
- // As the score is about to be updated, commit any decay that has happened
- // since the last update.
- raw_score_ = decayed_score;
-
- base::Time now = clock_->Now();
- if (!last_engagement_time_.is_null() &&
- now.LocalMidnight() != last_engagement_time_.LocalMidnight()) {
- points_added_today_ = 0;
- }
-
- if (points_added_today_ == 0) {
- // Award bonus engagement for the first engagement of the day for a site.
- points += GetFirstDailyEngagementPoints();
- SiteEngagementMetrics::RecordEngagement(
- SiteEngagementMetrics::ENGAGEMENT_FIRST_DAILY_ENGAGEMENT);
- }
-
- double to_add = std::min(kMaxPoints - raw_score_,
- GetMaxPointsPerDay() - points_added_today_);
- to_add = std::min(to_add, points);
-
- points_added_today_ += to_add;
- raw_score_ += to_add;
-
- last_engagement_time_ = now;
-}
-
-void SiteEngagementScore::Reset(double points, const base::Time* updated_time) {
- raw_score_ = points;
- points_added_today_ = 0;
-
- // This must be set in order to prevent the score from decaying when read.
- if (updated_time) {
- last_engagement_time_ = *updated_time;
- if (!last_shortcut_launch_time_.is_null())
- last_shortcut_launch_time_ = *updated_time;
- } else {
- last_engagement_time_ = clock_->Now();
- }
-}
-
-bool SiteEngagementScore::MaxPointsPerDayAdded() const {
- if (!last_engagement_time_.is_null() &&
- clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) {
- return false;
- }
-
- return points_added_today_ == GetMaxPointsPerDay();
-}
-
-bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) {
- double raw_score_orig = 0;
- double points_added_today_orig = 0;
- double last_engagement_time_internal_orig = 0;
- double last_shortcut_launch_time_internal_orig = 0;
-
- score_dict->GetDouble(kRawScoreKey, &raw_score_orig);
- score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig);
- score_dict->GetDouble(kLastEngagementTimeKey,
- &last_engagement_time_internal_orig);
- score_dict->GetDouble(kLastShortcutLaunchTimeKey,
- &last_shortcut_launch_time_internal_orig);
- bool changed =
- DoublesConsideredDifferent(raw_score_orig, raw_score_, kScoreDelta) ||
- DoublesConsideredDifferent(points_added_today_orig, points_added_today_,
- kScoreDelta) ||
- DoublesConsideredDifferent(last_engagement_time_internal_orig,
- last_engagement_time_.ToInternalValue(),
- kTimeDelta) ||
- DoublesConsideredDifferent(last_shortcut_launch_time_internal_orig,
- last_shortcut_launch_time_.ToInternalValue(),
- kTimeDelta);
-
- if (!changed)
- return false;
-
- score_dict->SetDouble(kRawScoreKey, raw_score_);
- score_dict->SetDouble(kPointsAddedTodayKey, points_added_today_);
- score_dict->SetDouble(kLastEngagementTimeKey,
- last_engagement_time_.ToInternalValue());
- score_dict->SetDouble(kLastShortcutLaunchTimeKey,
- last_shortcut_launch_time_.ToInternalValue());
-
- return true;
-}
-
-SiteEngagementScore::SiteEngagementScore(base::Clock* clock)
- : clock_(clock),
- raw_score_(0),
- points_added_today_(0),
- last_engagement_time_(),
- last_shortcut_launch_time_() {}
-
-double SiteEngagementScore::DecayedScore() const {
- // Note that users can change their clock, so from this system's perspective
- // time can go backwards. If that does happen and the system detects that the
- // current day is earlier than the last engagement, no decay (or growth) is
- // applied.
- int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays();
- if (days_since_engagement < 0)
- return raw_score_;
-
- int periods = days_since_engagement / GetDecayPeriodInDays();
- return std::max(0.0, raw_score_ - periods * GetDecayPoints());
-}
-
-double SiteEngagementScore::BonusScore() const {
- int days_since_shortcut_launch =
- (clock_->Now() - last_shortcut_launch_time_).InDays();
- if (days_since_shortcut_launch <= kMaxDaysSinceShortcutLaunch)
- return GetWebAppInstalledPoints();
-
- return 0;
+// Only accept a navigation event for engagement if it is one of:
+// a. direct typed navigation
+// b. clicking on an omnibox suggestion brought up by typing a keyword
+// c. clicking on a bookmark or opening a bookmark app
+// d. a custom search engine keyword search (e.g. Wikipedia search box added as
+// search engine).
+bool IsEngagementNavigation(ui::PageTransition transition) {
+ return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_TYPED) ||
+ ui::PageTransitionCoreTypeIs(transition,
+ ui::PAGE_TRANSITION_GENERATED) ||
+ ui::PageTransitionCoreTypeIs(transition,
+ ui::PAGE_TRANSITION_AUTO_BOOKMARK) ||
+ ui::PageTransitionCoreTypeIs(transition,
+ ui::PAGE_TRANSITION_KEYWORD_GENERATED);
}
-void SiteEngagementScore::SetParamValuesForTesting() {
- param_values[MAX_POINTS_PER_DAY] = 5;
- param_values[DECAY_PERIOD_IN_DAYS] = 7;
- param_values[DECAY_POINTS] = 5;
- param_values[NAVIGATION_POINTS] = 0.5;
- param_values[USER_INPUT_POINTS] = 0.05;
- param_values[VISIBLE_MEDIA_POINTS] = 0.02;
- param_values[HIDDEN_MEDIA_POINTS] = 0.01;
- param_values[WEB_APP_INSTALLED_POINTS] = 5;
- param_values[BOOTSTRAP_POINTS] = 8;
- param_values[MEDIUM_ENGAGEMENT_BOUNDARY] = 5;
- param_values[HIGH_ENGAGEMENT_BOUNDARY] = 50;
-
- // This is set to zero to avoid interference with tests and is set when
- // testing this functionality.
- param_values[FIRST_DAILY_ENGAGEMENT] = 0;
-}
+} // namespace
const char SiteEngagementService::kEngagementParams[] = "SiteEngagement";
@@ -385,6 +95,11 @@ SiteEngagementService* SiteEngagementService::Get(Profile* profile) {
}
// static
+double SiteEngagementService::GetMaxPoints() {
+ return SiteEngagementScore::kMaxPoints;
+}
+
+// static
bool SiteEngagementService::IsEnabled() {
// If the engagement service or any of its dependencies are force-enabled,
// return true immediately.
@@ -414,7 +129,7 @@ SiteEngagementService::SiteEngagementService(Profile* profile)
weak_factory_.GetWeakPtr()));
if (!g_updated_from_variations) {
- SiteEngagementScore::UpdateFromVariations();
+ SiteEngagementScore::UpdateFromVariations(kEngagementParams);
g_updated_from_variations = true;
}
}
@@ -426,57 +141,78 @@ SiteEngagementService::~SiteEngagementService() {
history->RemoveObserver(this);
}
-void SiteEngagementService::HandleNavigation(const GURL& url,
- ui::PageTransition transition) {
- if (IsEngagementNavigation(transition)) {
- SiteEngagementMetrics::RecordEngagement(
- SiteEngagementMetrics::ENGAGEMENT_NAVIGATION);
- AddPoints(url, SiteEngagementScore::GetNavigationPoints());
- RecordMetrics();
+SiteEngagementService::EngagementLevel
+SiteEngagementService::GetEngagementLevel(const GURL& url) const {
+ DCHECK_LT(SiteEngagementScore::GetMediumEngagementBoundary(),
+ SiteEngagementScore::GetHighEngagementBoundary());
+ double score = GetScore(url);
+ if (score == 0)
+ return ENGAGEMENT_LEVEL_NONE;
+
+ if (score < SiteEngagementScore::GetMediumEngagementBoundary())
+ return ENGAGEMENT_LEVEL_LOW;
+
+ if (score < SiteEngagementScore::GetHighEngagementBoundary())
+ return ENGAGEMENT_LEVEL_MEDIUM;
+
+ if (score < SiteEngagementScore::kMaxPoints)
+ return ENGAGEMENT_LEVEL_HIGH;
+
+ return ENGAGEMENT_LEVEL_MAX;
+}
+
+std::map<GURL, double> SiteEngagementService::GetScoreMap() const {
+ HostContentSettingsMap* settings_map =
+ HostContentSettingsMapFactory::GetForProfile(profile_);
+ std::unique_ptr<ContentSettingsForOneType> engagement_settings =
+ GetEngagementContentSettings(settings_map);
+
+ std::map<GURL, double> score_map;
+ for (const auto& site : *engagement_settings) {
+ GURL origin(site.primary_pattern.ToString());
+ if (!origin.is_valid())
+ continue;
+
+ std::unique_ptr<base::DictionaryValue> score_dict =
+ GetScoreDictForOrigin(settings_map, origin);
+ SiteEngagementScore score(clock_.get(), *score_dict);
+ score_map[origin] = score.GetScore();
}
+
+ return score_map;
}
-void SiteEngagementService::HandleUserInput(
- const GURL& url,
- SiteEngagementMetrics::EngagementType type) {
- SiteEngagementMetrics::RecordEngagement(type);
- AddPoints(url, SiteEngagementScore::GetUserInputPoints());
- RecordMetrics();
+bool SiteEngagementService::IsBootstrapped() {
+ return GetTotalEngagementPoints() >=
+ SiteEngagementScore::GetBootstrapPoints();
}
-void SiteEngagementService::HandleMediaPlaying(const GURL& url,
- bool is_hidden) {
- SiteEngagementMetrics::RecordEngagement(
- is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN
- : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE);
- AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints()
- : SiteEngagementScore::GetVisibleMediaPoints());
- RecordMetrics();
+bool SiteEngagementService::IsEngagementAtLeast(
+ const GURL& url,
+ EngagementLevel level) const {
+ DCHECK_LT(SiteEngagementScore::GetMediumEngagementBoundary(),
+ SiteEngagementScore::GetHighEngagementBoundary());
+ double score = GetScore(url);
+ switch (level) {
+ case ENGAGEMENT_LEVEL_NONE:
+ return true;
+ case ENGAGEMENT_LEVEL_LOW:
+ return score > 0;
+ case ENGAGEMENT_LEVEL_MEDIUM:
+ return score >= SiteEngagementScore::GetMediumEngagementBoundary();
+ case ENGAGEMENT_LEVEL_HIGH:
+ return score >= SiteEngagementScore::GetHighEngagementBoundary();
+ case ENGAGEMENT_LEVEL_MAX:
+ return score == SiteEngagementScore::kMaxPoints;
+ }
+ NOTREACHED();
+ return false;
}
void SiteEngagementService::ResetScoreForURL(const GURL& url, double score) {
ResetScoreAndAccessTimesForURL(url, score, nullptr);
}
-void SiteEngagementService::OnURLsDeleted(
- history::HistoryService* history_service,
- bool all_history,
- bool expired,
- const history::URLRows& deleted_rows,
- const std::set<GURL>& favicon_urls) {
- std::multiset<GURL> origins;
- for (const history::URLRow& row : deleted_rows)
- origins.insert(row.url().GetOrigin());
-
- history::HistoryService* hs = HistoryServiceFactory::GetForProfile(
- profile_, ServiceAccessType::EXPLICIT_ACCESS);
- hs->GetCountsAndLastVisitForOrigins(
- std::set<GURL>(origins.begin(), origins.end()),
- base::Bind(
- &SiteEngagementService::GetCountsAndLastVisitForOriginsComplete,
- weak_factory_.GetWeakPtr(), hs, origins, expired));
-}
-
void SiteEngagementService::SetLastShortcutLaunchTime(const GURL& url) {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
@@ -510,7 +246,7 @@ double SiteEngagementService::GetScore(const GURL& url) const {
GetScoreDictForOrigin(settings_map, url);
SiteEngagementScore score(clock_.get(), *score_dict);
- return score.Score();
+ return score.GetScore();
}
double SiteEngagementService::GetTotalEngagementPoints() const {
@@ -523,74 +259,6 @@ double SiteEngagementService::GetTotalEngagementPoints() const {
return total_score;
}
-std::map<GURL, double> SiteEngagementService::GetScoreMap() const {
- HostContentSettingsMap* settings_map =
- HostContentSettingsMapFactory::GetForProfile(profile_);
- std::unique_ptr<ContentSettingsForOneType> engagement_settings =
- GetEngagementContentSettings(settings_map);
-
- std::map<GURL, double> score_map;
- for (const auto& site : *engagement_settings) {
- GURL origin(site.primary_pattern.ToString());
- if (!origin.is_valid())
- continue;
-
- std::unique_ptr<base::DictionaryValue> score_dict =
- GetScoreDictForOrigin(settings_map, origin);
- SiteEngagementScore score(clock_.get(), *score_dict);
- score_map[origin] = score.Score();
- }
-
- return score_map;
-}
-
-bool SiteEngagementService::IsBootstrapped() {
- return GetTotalEngagementPoints() >=
- SiteEngagementScore::GetBootstrapPoints();
-}
-
-SiteEngagementService::EngagementLevel
-SiteEngagementService::GetEngagementLevel(const GURL& url) const {
- DCHECK_LT(SiteEngagementScore::GetMediumEngagementBoundary(),
- SiteEngagementScore::GetHighEngagementBoundary());
- double score = GetScore(url);
- if (score == 0)
- return ENGAGEMENT_LEVEL_NONE;
-
- if (score < SiteEngagementScore::GetMediumEngagementBoundary())
- return ENGAGEMENT_LEVEL_LOW;
-
- if (score < SiteEngagementScore::GetHighEngagementBoundary())
- return ENGAGEMENT_LEVEL_MEDIUM;
-
- if (score < SiteEngagementScore::kMaxPoints)
- return ENGAGEMENT_LEVEL_HIGH;
-
- return ENGAGEMENT_LEVEL_MAX;
-}
-
-bool SiteEngagementService::IsEngagementAtLeast(
- const GURL& url,
- EngagementLevel level) const {
- DCHECK_LT(SiteEngagementScore::GetMediumEngagementBoundary(),
- SiteEngagementScore::GetHighEngagementBoundary());
- double score = GetScore(url);
- switch (level) {
- case ENGAGEMENT_LEVEL_NONE:
- return true;
- case ENGAGEMENT_LEVEL_LOW:
- return score > 0;
- case ENGAGEMENT_LEVEL_MEDIUM:
- return score >= SiteEngagementScore::GetMediumEngagementBoundary();
- case ENGAGEMENT_LEVEL_HIGH:
- return score >= SiteEngagementScore::GetHighEngagementBoundary();
- case ENGAGEMENT_LEVEL_MAX:
- return score == SiteEngagementScore::kMaxPoints;
- }
- NOTREACHED();
- return false;
-}
-
SiteEngagementService::SiteEngagementService(Profile* profile,
std::unique_ptr<base::Clock> clock)
: profile_(profile), clock_(std::move(clock)), weak_factory_(this) {
@@ -633,7 +301,7 @@ void SiteEngagementService::CleanupEngagementScores() {
std::unique_ptr<base::DictionaryValue> score_dict =
GetScoreDictForOrigin(settings_map, origin);
SiteEngagementScore score(clock_.get(), *score_dict);
- if (score.Score() != 0)
+ if (score.GetScore() != 0)
continue;
}
@@ -697,6 +365,53 @@ double SiteEngagementService::GetMedianEngagement(
return (scores[mid - 1] + scores[mid]) / 2;
}
+void SiteEngagementService::HandleMediaPlaying(const GURL& url,
+ bool is_hidden) {
+ SiteEngagementMetrics::RecordEngagement(
+ is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN
+ : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE);
+ AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints()
+ : SiteEngagementScore::GetVisibleMediaPoints());
+ RecordMetrics();
+}
+
+void SiteEngagementService::HandleNavigation(const GURL& url,
+ ui::PageTransition transition) {
+ if (IsEngagementNavigation(transition)) {
+ SiteEngagementMetrics::RecordEngagement(
+ SiteEngagementMetrics::ENGAGEMENT_NAVIGATION);
+ AddPoints(url, SiteEngagementScore::GetNavigationPoints());
+ RecordMetrics();
+ }
+}
+
+void SiteEngagementService::HandleUserInput(
+ const GURL& url,
+ SiteEngagementMetrics::EngagementType type) {
+ SiteEngagementMetrics::RecordEngagement(type);
+ AddPoints(url, SiteEngagementScore::GetUserInputPoints());
+ RecordMetrics();
+}
+
+void SiteEngagementService::OnURLsDeleted(
+ history::HistoryService* history_service,
+ bool all_history,
+ bool expired,
+ const history::URLRows& deleted_rows,
+ const std::set<GURL>& favicon_urls) {
+ std::multiset<GURL> origins;
+ for (const history::URLRow& row : deleted_rows)
+ origins.insert(row.url().GetOrigin());
+
+ history::HistoryService* hs = HistoryServiceFactory::GetForProfile(
+ profile_, ServiceAccessType::EXPLICIT_ACCESS);
+ hs->GetCountsAndLastVisitForOrigins(
+ std::set<GURL>(origins.begin(), origins.end()),
+ base::Bind(
+ &SiteEngagementService::GetCountsAndLastVisitForOriginsComplete,
+ weak_factory_.GetWeakPtr(), hs, origins, expired));
+}
+
int SiteEngagementService::OriginsWithMaxDailyEngagement() const {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.h ('k') | chrome/browser/engagement/site_engagement_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698