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 #include "chrome/browser/engagement/site_engagement_service.h" | 5 #include "chrome/browser/engagement/site_engagement_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 base::FieldTrialList::FindFullName(kEngagementParams); | 97 base::FieldTrialList::FindFullName(kEngagementParams); |
98 return !base::StartsWith(group_name, "Disabled", | 98 return !base::StartsWith(group_name, "Disabled", |
99 base::CompareCase::SENSITIVE); | 99 base::CompareCase::SENSITIVE); |
100 } | 100 } |
101 | 101 |
102 // static | 102 // static |
103 double SiteEngagementService::GetScoreFromSettings( | 103 double SiteEngagementService::GetScoreFromSettings( |
104 HostContentSettingsMap* settings, | 104 HostContentSettingsMap* settings, |
105 const GURL& origin) { | 105 const GURL& origin) { |
106 auto clock = base::MakeUnique<base::DefaultClock>(); | 106 auto clock = base::MakeUnique<base::DefaultClock>(); |
107 return SiteEngagementScore(clock.get(), origin, settings) | 107 return SiteEngagementScore(clock.get(), origin, settings).GetTotal(); |
108 .GetScore(); | |
109 } | 108 } |
110 | 109 |
111 SiteEngagementService::SiteEngagementService(Profile* profile) | 110 SiteEngagementService::SiteEngagementService(Profile* profile) |
112 : SiteEngagementService(profile, base::MakeUnique<base::DefaultClock>()) { | 111 : SiteEngagementService(profile, base::MakeUnique<base::DefaultClock>()) { |
113 content::BrowserThread::PostAfterStartupTask( | 112 content::BrowserThread::PostAfterStartupTask( |
114 FROM_HERE, content::BrowserThread::GetTaskRunnerForThread( | 113 FROM_HERE, content::BrowserThread::GetTaskRunnerForThread( |
115 content::BrowserThread::UI), | 114 content::BrowserThread::UI), |
116 base::Bind(&SiteEngagementService::AfterStartupTask, | 115 base::Bind(&SiteEngagementService::AfterStartupTask, |
117 weak_factory_.GetWeakPtr())); | 116 weak_factory_.GetWeakPtr())); |
118 | 117 |
(...skipping 13 matching lines...) Expand all Loading... | |
132 } | 131 } |
133 | 132 |
134 blink::mojom::EngagementLevel | 133 blink::mojom::EngagementLevel |
135 SiteEngagementService::GetEngagementLevel(const GURL& url) const { | 134 SiteEngagementService::GetEngagementLevel(const GURL& url) const { |
136 if (IsLastEngagementStale()) | 135 if (IsLastEngagementStale()) |
137 CleanupEngagementScores(true); | 136 CleanupEngagementScores(true); |
138 | 137 |
139 return CreateEngagementScore(url).GetEngagementLevel(); | 138 return CreateEngagementScore(url).GetEngagementLevel(); |
140 } | 139 } |
141 | 140 |
142 std::map<GURL, double> SiteEngagementService::GetScoreMap() const { | 141 std::vector<mojom::SiteEngagementDetails> |
142 SiteEngagementService::GetScoreDetails() const { | |
143 HostContentSettingsMap* settings_map = | 143 HostContentSettingsMap* settings_map = |
144 HostContentSettingsMapFactory::GetForProfile(profile_); | 144 HostContentSettingsMapFactory::GetForProfile(profile_); |
145 std::unique_ptr<ContentSettingsForOneType> engagement_settings = | 145 std::unique_ptr<ContentSettingsForOneType> engagement_settings = |
146 GetEngagementContentSettings(settings_map); | 146 GetEngagementContentSettings(settings_map); |
147 | 147 |
148 std::map<GURL, double> score_map; | 148 std::vector<mojom::SiteEngagementDetails> scores; |
149 scores.reserve(engagement_settings->size()); | |
149 for (const auto& site : *engagement_settings) { | 150 for (const auto& site : *engagement_settings) { |
150 GURL origin(site.primary_pattern.ToString()); | 151 GURL origin(site.primary_pattern.ToString()); |
151 if (!origin.is_valid()) | 152 if (!origin.is_valid()) |
152 continue; | 153 continue; |
154 scores.push_back(GetScoreDetails(origin)); | |
155 } | |
153 | 156 |
154 score_map[origin] = GetScore(origin); | 157 return scores; |
158 } | |
159 | |
160 std::map<GURL, double> SiteEngagementService::GetScoreMap() const { | |
161 std::vector<mojom::SiteEngagementDetails> scores = GetScoreDetails(); | |
162 | |
163 std::map<GURL, double> score_map; | |
164 for (const auto& info : scores) { | |
165 score_map[info.origin] = info.total_score; | |
155 } | 166 } |
156 | 167 |
157 return score_map; | 168 return score_map; |
158 } | 169 } |
159 | 170 |
160 void SiteEngagementService::HandleNotificationInteraction(const GURL& url) { | 171 void SiteEngagementService::HandleNotificationInteraction(const GURL& url) { |
161 if (!ShouldRecordEngagement(url)) | 172 if (!ShouldRecordEngagement(url)) |
162 return; | 173 return; |
163 | 174 |
164 SiteEngagementMetrics::RecordEngagement( | 175 SiteEngagementMetrics::RecordEngagement( |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 SiteEngagementService::Helper* helper) { | 248 SiteEngagementService::Helper* helper) { |
238 helpers_.insert(helper); | 249 helpers_.insert(helper); |
239 } | 250 } |
240 | 251 |
241 void SiteEngagementService::HelperDeleted( | 252 void SiteEngagementService::HelperDeleted( |
242 SiteEngagementService::Helper* helper) { | 253 SiteEngagementService::Helper* helper) { |
243 helpers_.erase(helper); | 254 helpers_.erase(helper); |
244 } | 255 } |
245 | 256 |
246 double SiteEngagementService::GetScore(const GURL& url) const { | 257 double SiteEngagementService::GetScore(const GURL& url) const { |
258 return GetScoreDetails(url).total_score; | |
259 } | |
260 | |
261 mojom::SiteEngagementDetails SiteEngagementService::GetScoreDetails( | |
262 const GURL& url) const { | |
247 // Ensure that if engagement is stale, we clean things up before fetching the | 263 // Ensure that if engagement is stale, we clean things up before fetching the |
248 // score. | 264 // score. |
249 if (IsLastEngagementStale()) | 265 if (IsLastEngagementStale()) |
250 CleanupEngagementScores(true); | 266 CleanupEngagementScores(true); |
251 | 267 |
252 return CreateEngagementScore(url).GetScore(); | 268 return CreateEngagementScore(url).GetDetails(); |
253 } | 269 } |
254 | 270 |
255 double SiteEngagementService::GetTotalEngagementPoints() const { | 271 double SiteEngagementService::GetTotalEngagementPoints() const { |
256 std::map<GURL, double> score_map = GetScoreMap(); | 272 std::map<GURL, double> score_map = GetScoreMap(); |
257 | 273 |
258 double total_score = 0; | 274 double total_score = 0; |
259 for (const auto& value : score_map) | 275 for (const auto& value : score_map) |
260 total_score += value.second; | 276 total_score += value.second; |
261 | 277 |
262 return total_score; | 278 return total_score; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 last_engagement_time - score.last_engagement_time(); | 385 last_engagement_time - score.last_engagement_time(); |
370 base::Time rebase_score_time = rebase_time - offset; | 386 base::Time rebase_score_time = rebase_time - offset; |
371 score.set_last_engagement_time(rebase_score_time); | 387 score.set_last_engagement_time(rebase_score_time); |
372 } | 388 } |
373 | 389 |
374 if (score.last_engagement_time() > new_last_engagement_time) | 390 if (score.last_engagement_time() > new_last_engagement_time) |
375 new_last_engagement_time = score.last_engagement_time(); | 391 new_last_engagement_time = score.last_engagement_time(); |
376 score.Commit(); | 392 score.Commit(); |
377 } | 393 } |
378 | 394 |
379 if (score.GetScore() > SiteEngagementScore::GetScoreCleanupThreshold()) | 395 if (score.GetTotal() > SiteEngagementScore::GetScoreCleanupThreshold()) |
380 continue; | 396 continue; |
381 } | 397 } |
382 | 398 |
383 // This origin has a score of 0. Wipe it from content settings. | 399 // This origin has a score of 0. Wipe it from content settings. |
384 settings_map->SetWebsiteSettingDefaultScope( | 400 settings_map->SetWebsiteSettingDefaultScope( |
385 origin, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | 401 origin, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
386 nullptr); | 402 nullptr); |
387 } | 403 } |
388 | 404 |
389 // Set the last engagement time to be consistent with the scores. This will | 405 // Set the last engagement time to be consistent with the scores. This will |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
648 // under the origin in history. If this new last visit date is long enough | 664 // under the origin in history. If this new last visit date is long enough |
649 // in the past, the next time the origin's engagement is accessed the | 665 // in the past, the next time the origin's engagement is accessed the |
650 // automatic decay will kick in - i.e. a double decay will have occurred. | 666 // automatic decay will kick in - i.e. a double decay will have occurred. |
651 // To prevent this, compute the decay that would have taken place since the | 667 // To prevent this, compute the decay that would have taken place since the |
652 // new last visit and add it to the engagement at this point. When the | 668 // new last visit and add it to the engagement at this point. When the |
653 // engagement is next accessed, it will decay back to the proportionally | 669 // engagement is next accessed, it will decay back to the proportionally |
654 // reduced value rather than being decayed once here, and then once again | 670 // reduced value rather than being decayed once here, and then once again |
655 // when it is next accessed. | 671 // when it is next accessed. |
656 SiteEngagementScore engagement_score = CreateEngagementScore(origin); | 672 SiteEngagementScore engagement_score = CreateEngagementScore(origin); |
657 | 673 |
658 double new_score = proportion_remaining * engagement_score.GetScore(); | 674 double new_score = proportion_remaining * engagement_score.GetTotal(); |
Wez
2017/04/08 01:52:53
This logic looks wrong; we're grabbing the *total*
dominickn
2017/04/10 00:58:11
You're right, this should be the underlying raw_sc
Wez
2017/04/10 03:45:28
That sounds great, but reading through the comment
dominickn
2017/04/10 04:41:26
Privacy required us to explicitly decay the engage
Wez
2017/04/10 21:18:09
The double-decay stuff was cofusing; I realised it
| |
659 int hours_since_engagement = (now - last_visit).InHours(); | 675 int hours_since_engagement = (now - last_visit).InHours(); |
660 int periods = | 676 int periods = |
661 hours_since_engagement / SiteEngagementScore::GetDecayPeriodInHours(); | 677 hours_since_engagement / SiteEngagementScore::GetDecayPeriodInHours(); |
662 new_score += periods * SiteEngagementScore::GetDecayPoints(); | 678 new_score += periods * SiteEngagementScore::GetDecayPoints(); |
663 new_score *= pow(1.0 / SiteEngagementScore::GetDecayProportion(), periods); | 679 new_score *= pow(1.0 / SiteEngagementScore::GetDecayProportion(), periods); |
664 | 680 |
665 double score = std::min(SiteEngagementScore::kMaxPoints, new_score); | 681 double score = std::min(SiteEngagementScore::kMaxPoints, new_score); |
666 engagement_score.Reset(score, last_visit); | 682 engagement_score.Reset(score, last_visit); |
667 if (!engagement_score.last_shortcut_launch_time().is_null() && | 683 if (!engagement_score.last_shortcut_launch_time().is_null() && |
668 engagement_score.last_shortcut_launch_time() > last_visit) { | 684 engagement_score.last_shortcut_launch_time() > last_visit) { |
669 engagement_score.set_last_shortcut_launch_time(last_visit); | 685 engagement_score.set_last_shortcut_launch_time(last_visit); |
670 } | 686 } |
671 | 687 |
672 engagement_score.Commit(); | 688 engagement_score.Commit(); |
673 } | 689 } |
674 | 690 |
675 SetLastEngagementTime(now); | 691 SetLastEngagementTime(now); |
676 } | 692 } |
OLD | NEW |