File: media_engagement_service.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 (146 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download | duplicates (6)
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
// 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_SERVICE_H_
#define CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SERVICE_H_

#include <map>
#include <set>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/media/media_engagement_score.h"
#include "chrome/browser/media/media_engagement_score_details.mojom.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/pref_registry/pref_registry_syncable.h"

class MediaEngagementContentsObserver;
class MediaEngagementScore;
class Profile;

namespace base {
class Clock;
}

namespace content {
class WebContents;
}  // namespace content

namespace url {
class Origin;
}  // namespace url

class MediaEngagementService : public KeyedService,
                               public history::HistoryServiceObserver {
 public:
  // Returns the instance attached to the given |profile|.
  static MediaEngagementService* Get(Profile* profile);

  // Returns whether the feature is enabled.
  static bool IsEnabled();

  // Observe the given |web_contents| by creating an internal
  // WebContentsObserver.
  static void CreateWebContentsObserver(content::WebContents* web_contents);

  // Register profile prefs in the pref registry.
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  explicit MediaEngagementService(Profile* profile);

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

  ~MediaEngagementService() override;

  // Returns the engagement score of |origin|.
  double GetEngagementScore(const url::Origin& origin) const;

  // Returns true if `origin` has an engagement score considered high.
  // Otherwise, check the global data (`MediaEngagementPreloadedList`) if the
  // `kPreloadMediaEngagementData` feature flag is enabled and the number of
  // visits is less than the number of visits required to have an MEI score.
  virtual bool HasHighEngagement(const url::Origin& origin) const;

  // Returns a map of all stored origins and their engagement levels.
  std::map<url::Origin, double> GetScoreMapForTesting() const;

  // Record a visit of a |origin|.
  void RecordVisit(const url::Origin& origin);

  // Returns an array of engagement score details for all origins which
  // have a score.
  std::vector<media::mojom::MediaEngagementScoreDetailsPtr> GetAllScoreDetails()
      const;

  // Overridden from history::HistoryServiceObserver:
  void OnHistoryDeletions(history::HistoryService* history_service,
                          const history::DeletionInfo& deletion_info) override;

  // KeyedService support:
  void Shutdown() override;

  // Clear data if the last playback time is between these two time points.
  void ClearDataBetweenTime(const base::Time& delete_begin,
                            const base::Time& delete_end);

  // Retrieves the MediaEngagementScore for |origin|.
  MediaEngagementScore CreateEngagementScore(const url::Origin& origin) const;

  MediaEngagementContentsObserver* GetContentsObserverFor(
      content::WebContents* web_contents) const;

  // Sets the |history| service to observe.
  void SetHistoryServiceForTesting(history::HistoryService* history);

  Profile* profile() const;

  const base::Clock* clock() const { return clock_; }

 private:
  friend class MediaEngagementBrowserTest;
  friend class MediaEngagementContentsObserverTest;
  friend class MediaEngagementServiceTest;
  friend class MediaEngagementSessionTest;
  friend class MediaEngagementContentsObserver;

  MediaEngagementService(Profile* profile, base::Clock* clock);

  // Returns true if we should record engagement for this url. Currently,
  // engagement is only earned for HTTP and HTTPS.
  bool ShouldRecordEngagement(const url::Origin& origin) const;

  base::flat_map<content::WebContents*,
                 raw_ptr<MediaEngagementContentsObserver, CtnExperimental>>
      contents_observers_;

  raw_ptr<Profile, DanglingUntriaged> profile_;

  // Clear any data for a specific origin.
  void Clear(const url::Origin& origin);

  // An internal clock for testing.
  raw_ptr<base::Clock> clock_;

  std::vector<MediaEngagementScore> GetAllStoredScores() const;

  int GetSchemaVersion() const;
  void SetSchemaVersion(int);

  // Remove origins from `deleted_origins` that have no more visits in the
  // history service, represented as `origin_data`. This is meant to be used
  // when the service receives a notification of history expiration.
  void RemoveOriginsWithNoVisits(
      const std::set<url::Origin>& deleted_origins,
      const history::OriginCountAndLastVisitMap& origin_data);

  base::ScopedObservation<history::HistoryService,
                          history::HistoryServiceObserver>
      history_service_observation_{this};
};

#endif  // CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SERVICE_H_