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

Side by Side Diff: chrome/browser/engagement/site_engagement_score.cc

Issue 2285553002: [HBD] Gate the advertising of Flash on Site Engagement. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tidying Created 4 years, 3 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_score.h" 5 #include "chrome/browser/engagement/site_engagement_score.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/time/clock.h" 11 #include "base/time/clock.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
15 #include "chrome/browser/engagement/site_engagement_metrics.h" 15 #include "chrome/browser/engagement/site_engagement_metrics.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h" 16 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/variations/variations_associated_data.h" 17 #include "components/variations/variations_associated_data.h"
19 18
20 namespace { 19 namespace {
21 20
22 // Delta within which to consider scores equal. 21 // Delta within which to consider scores equal.
23 const double kScoreDelta = 0.001; 22 const double kScoreDelta = 0.001;
24 23
25 // Delta within which to consider internal time values equal. Internal time 24 // Delta within which to consider internal time values equal. Internal time
26 // values are in microseconds, so this delta comes out at one second. 25 // values are in microseconds, so this delta comes out at one second.
27 const double kTimeDelta = 1000000; 26 const double kTimeDelta = 1000000;
28 27
29 // Number of days after the last launch of an origin from an installed shortcut 28 // Number of days after the last launch of an origin from an installed shortcut
30 // for which WEB_APP_INSTALLED_POINTS will be added to the engagement score. 29 // for which WEB_APP_INSTALLED_POINTS will be added to the engagement score.
31 const int kMaxDaysSinceShortcutLaunch = 10; 30 const int kMaxDaysSinceShortcutLaunch = 10;
32 31
33 bool DoublesConsideredDifferent(double value1, double value2, double delta) { 32 bool DoublesConsideredDifferent(double value1, double value2, double delta) {
34 double abs_difference = fabs(value1 - value2); 33 double abs_difference = fabs(value1 - value2);
35 return abs_difference > delta; 34 return abs_difference > delta;
36 } 35 }
37 36
38 std::unique_ptr<base::DictionaryValue> GetScoreDictForOrigin( 37 std::unique_ptr<base::DictionaryValue> GetScoreDictForSettings(
39 Profile* profile, 38 const HostContentSettingsMap* settings,
40 const GURL& origin_url) { 39 const GURL& origin_url) {
41 HostContentSettingsMap* settings =
42 HostContentSettingsMapFactory::GetForProfile(profile);
43 HostContentSettingsMap* fallback_settings =
44 profile->IsOffTheRecord() ? HostContentSettingsMapFactory::GetForProfile(
45 profile->GetOriginalProfile())
46 : nullptr;
47
48 if (!settings) 40 if (!settings)
49 return std::unique_ptr<base::DictionaryValue>(); 41 return base::MakeUnique<base::DictionaryValue>();
50 42
51 std::unique_ptr<base::Value> value = settings->GetWebsiteSetting( 43 std::unique_ptr<base::Value> value = settings->GetWebsiteSetting(
52 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, 44 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
53 std::string(), NULL); 45 std::string(), NULL);
54 if (!value.get() && fallback_settings) {
55 value = fallback_settings->GetWebsiteSetting(
56 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
57 std::string(), NULL);
58 }
59 46
60 if (!value.get()) 47 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY))
61 return base::WrapUnique(new base::DictionaryValue()); 48 return base::MakeUnique<base::DictionaryValue>();
62
63 if (!value->IsType(base::Value::TYPE_DICTIONARY))
64 return base::WrapUnique(new base::DictionaryValue());
65 49
66 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); 50 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release()));
67 } 51 }
68 52
69 } // namespace 53 } // namespace
70 54
71 const double SiteEngagementScore::kMaxPoints = 100; 55 const double SiteEngagementScore::kMaxPoints = 100;
72 56
73 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; 57 const char* SiteEngagementScore::kRawScoreKey = "rawScore";
74 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; 58 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday";
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 return; 169 return;
186 } 170 }
187 } 171 }
188 172
189 // Once we're sure everything is valid, assign the variation to the param 173 // Once we're sure everything is valid, assign the variation to the param
190 // values array. 174 // values array.
191 for (int i = 0; i < MAX_VARIATION; ++i) 175 for (int i = 0; i < MAX_VARIATION; ++i)
192 SiteEngagementScore::GetParamValues()[i].second = param_vals[i]; 176 SiteEngagementScore::GetParamValues()[i].second = param_vals[i];
193 } 177 }
194 178
195 SiteEngagementScore::SiteEngagementScore(base::Clock* clock, 179 SiteEngagementScore::SiteEngagementScore(
196 const GURL& origin, 180 base::Clock* clock,
197 Profile* profile) 181 const GURL& origin,
198 : SiteEngagementScore(clock, GetScoreDictForOrigin(profile, origin)) { 182 HostContentSettingsMap* settings)
199 origin_ = origin; 183 : SiteEngagementScore(
200 profile_ = profile; 184 clock,
185 origin,
186 GetScoreDictForSettings(settings, origin)) {
187 settings_map_ = settings;
201 } 188 }
202 189
203 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default; 190 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default;
204 191
205 SiteEngagementScore::~SiteEngagementScore() {} 192 SiteEngagementScore::~SiteEngagementScore() {}
206 193
207 SiteEngagementScore& SiteEngagementScore::operator=( 194 SiteEngagementScore& SiteEngagementScore::operator=(
208 SiteEngagementScore&& other) = default; 195 SiteEngagementScore&& other) = default;
209 196
210 void SiteEngagementScore::AddPoints(double points) { 197 void SiteEngagementScore::AddPoints(double points) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 raw_score_ += to_add; 229 raw_score_ += to_add;
243 230
244 last_engagement_time_ = now; 231 last_engagement_time_ = now;
245 } 232 }
246 233
247 double SiteEngagementScore::GetScore() const { 234 double SiteEngagementScore::GetScore() const {
248 return std::min(DecayedScore() + BonusScore(), kMaxPoints); 235 return std::min(DecayedScore() + BonusScore(), kMaxPoints);
249 } 236 }
250 237
251 void SiteEngagementScore::Commit() { 238 void SiteEngagementScore::Commit() {
252 if (!UpdateScoreDict(score_dict_.get())) 239 if (!UpdateScoreDict(score_dict_.get()) || !settings_map_)
calamity 2016/09/13 04:14:37 Is the settings_map_ ever null? We may want to DCH
dominickn 2016/09/13 04:42:14 It could be nullptr in tests (where it uses the pr
calamity 2016/09/13 06:33:34 To me, the condition the DCHECK here enforces is '
dominickn 2016/09/14 01:06:55 Done.
253 return; 240 return;
254 241
255 HostContentSettingsMapFactory::GetForProfile(profile_) 242 settings_map_->SetWebsiteSettingDefaultScope(
256 ->SetWebsiteSettingDefaultScope(origin_, GURL(), 243 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(),
257 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, 244 std::move(score_dict_));
258 std::string(), std::move(score_dict_));
259 } 245 }
260 246
261 bool SiteEngagementScore::MaxPointsPerDayAdded() const { 247 bool SiteEngagementScore::MaxPointsPerDayAdded() const {
262 if (!last_engagement_time_.is_null() && 248 if (!last_engagement_time_.is_null() &&
263 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { 249 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) {
264 return false; 250 return false;
265 } 251 }
266 252
267 return points_added_today_ == GetMaxPointsPerDay(); 253 return points_added_today_ == GetMaxPointsPerDay();
268 } 254 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 score_dict->SetDouble(kLastEngagementTimeKey, 293 score_dict->SetDouble(kLastEngagementTimeKey,
308 last_engagement_time_.ToInternalValue()); 294 last_engagement_time_.ToInternalValue());
309 score_dict->SetDouble(kLastShortcutLaunchTimeKey, 295 score_dict->SetDouble(kLastShortcutLaunchTimeKey,
310 last_shortcut_launch_time_.ToInternalValue()); 296 last_shortcut_launch_time_.ToInternalValue());
311 297
312 return true; 298 return true;
313 } 299 }
314 300
315 SiteEngagementScore::SiteEngagementScore( 301 SiteEngagementScore::SiteEngagementScore(
316 base::Clock* clock, 302 base::Clock* clock,
303 const GURL& origin,
317 std::unique_ptr<base::DictionaryValue> score_dict) 304 std::unique_ptr<base::DictionaryValue> score_dict)
318 : clock_(clock), 305 : clock_(clock),
319 raw_score_(0), 306 raw_score_(0),
320 points_added_today_(0), 307 points_added_today_(0),
321 last_engagement_time_(), 308 last_engagement_time_(),
322 last_shortcut_launch_time_(), 309 last_shortcut_launch_time_(),
323 score_dict_(score_dict.release()) { 310 score_dict_(score_dict.release()),
311 origin_(origin) {
324 if (!score_dict_) 312 if (!score_dict_)
325 return; 313 return;
326 314
327 score_dict_->GetDouble(kRawScoreKey, &raw_score_); 315 score_dict_->GetDouble(kRawScoreKey, &raw_score_);
328 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_); 316 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_);
329 317
330 double internal_time; 318 double internal_time;
331 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time)) 319 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time))
332 last_engagement_time_ = base::Time::FromInternalValue(internal_time); 320 last_engagement_time_ = base::Time::FromInternalValue(internal_time);
333 if (score_dict_->GetDouble(kLastShortcutLaunchTimeKey, &internal_time)) 321 if (score_dict_->GetDouble(kLastShortcutLaunchTimeKey, &internal_time))
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second = 50; 360 GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second = 50;
373 GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1; 361 GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1;
374 GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72; 362 GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72;
375 363
376 // This is set to values that avoid interference with tests and are set when 364 // This is set to values that avoid interference with tests and are set when
377 // testing these features. 365 // testing these features.
378 GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0; 366 GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0;
379 GetParamValues()[DECAY_PROPORTION].second = 1; 367 GetParamValues()[DECAY_PROPORTION].second = 1;
380 GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0; 368 GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0;
381 } 369 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698