File: media_engagement_score.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (150 lines) | stat: -rw-r--r-- 5,672 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SCORE_H_
#define CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SCORE_H_

#include "base/memory/raw_ptr.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/media/media_engagement_score_details.mojom.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "url/origin.h"

class HostContentSettingsMap;

// Calculates and stores the Media Engagement Index score for a certain origin.
class MediaEngagementScore final {
 public:
  // The dictionary keys to store individual metrics. kVisitsKey will store the
  // number of visits to an origin and kMediaPlaybacksKey will store the number
  // of media playbacks on an origin. kLastMediaPlaybackTimeKey will store the
  // timestamp of the last media playback on an origin. kHasHighScoreKey will
  // store whether the score is considered high.
  static const char kVisitsKey[];
  static const char kMediaPlaybacksKey[];
  static const char kLastMediaPlaybackTimeKey[];
  static const char kHasHighScoreKey[];

  // Origins with a number of visits less than this number will recieve
  // a score of zero.
  static int GetScoreMinVisits();

  // The upper and lower threshold of whether the total score is considered
  // to be high.
  static double GetHighScoreLowerThreshold();
  static double GetHighScoreUpperThreshold();

  MediaEngagementScore(base::Clock* clock,
                       const url::Origin& origin,
                       HostContentSettingsMap* settings);

  MediaEngagementScore(const MediaEngagementScore&) = delete;
  MediaEngagementScore& operator=(const MediaEngagementScore&) = delete;

  ~MediaEngagementScore();

  MediaEngagementScore(MediaEngagementScore&&);
  MediaEngagementScore& operator=(MediaEngagementScore&&);

  // Returns the total score, as per the formula.
  double actual_score() const { return actual_score_; }

  // Returns whether the total score is considered high.
  bool high_score() const { return is_high_; }

  // Returns the origin associated with this score.
  const url::Origin& origin() const { return origin_; }

  // Writes the values in this score into |settings_map_|. If there are multiple
  // instances of a score object for an origin, this could result in stale data
  // being stored. Takes in a boolean indicating whether to force an update
  // even if properties of the score are unchanged.
  void Commit(bool force_update = false);

  // Get/increment the number of visits this origin has had.
  int visits() const { return visits_; }
  void IncrementVisits() { SetVisits(visits() + 1); }

  // Get/increment the number of media playbacks this origin has had.
  int media_playbacks() const { return media_playbacks_; }
  void IncrementMediaPlaybacks();

  // Gets/sets the last time media was played on this origin.
  base::Time last_media_playback_time() const {
    return last_media_playback_time_;
  }
  void set_last_media_playback_time(base::Time new_time) {
    last_media_playback_time_ = new_time;
  }

  // Get a breakdown of the score that can be serialized by Mojo.
  media::mojom::MediaEngagementScoreDetailsPtr GetScoreDetails() const;

 protected:
  friend class MediaEngagementAutoplayBrowserTest;
  friend class MediaEngagementContentsObserverTest;
  friend class MediaEngagementContentsObserverMPArchBrowserTest;
  friend class MediaEngagementSessionTest;
  friend class MediaEngagementService;

  // Only used by the Media Engagement service when bulk loading data.
  MediaEngagementScore(base::Clock* clock,
                       const url::Origin& origin,
                       base::Value::Dict score_dict,
                       HostContentSettingsMap* settings);

  static const char kScoreMinVisitsParamName[];
  static const char kHighScoreLowerThresholdParamName[];
  static const char kHighScoreUpperThresholdParamName[];

  void SetVisits(int visits);
  void SetMediaPlaybacks(int media_playbacks);

 private:
  friend class MediaEngagementServiceTest;
  friend class MediaEngagementScoreTest;
  friend class MediaEngagementScoreWithOverrideFieldTrialsTest;

  // Update the dictionary continaing the latest score values and return whether
  // they have changed or not (since what was last retrieved from content
  // settings). Takes in a boolean indicating whether to force an update
  // even if properties of the score are unchanged.
  bool UpdateScoreDict(bool force_update = false);

  // If the number of playbacks or visits is updated then this will recalculate
  // the total score and whether the score is considered high.
  void Recalculate();

  // The number of media playbacks this origin has had.
  int media_playbacks_ = 0;

  // The number of visits this origin has had.
  int visits_ = 0;

  // If the current score is considered high.
  bool is_high_ = false;

  // The current engagement score.
  double actual_score_ = 0.0;

  // The last time media was played back on this origin.
  base::Time last_media_playback_time_;

  // The origin this score represents.
  url::Origin origin_;

  // A clock that can be used for testing, owned by the service.
  raw_ptr<base::Clock> clock_;

  // The dictionary that represents this engagement score.
  base::Value::Dict score_dict_;

  // The content settings map that will persist the score,
  // has a lifetime of the Profile like the service which owns |this|.
  raw_ptr<HostContentSettingsMap> settings_map_ = nullptr;
};

#endif  // CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SCORE_H_