File: media_engagement_session.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 (128 lines) | stat: -rw-r--r-- 4,602 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
// 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_SESSION_H_
#define CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SESSION_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "url/origin.h"

class MediaEngagementService;

// Represents a session with regards to the Media Engagement. A session consists
// of a visit on a tab and all the tabs opened to the same origin originating
// from that tab or one of its children.
//
// Each tab will have a MediaEngagementContentsObserver that will hold a
// reference to a session. When all the references are released, the session
// will be destructed and record all the information. It guarantees that
// information are recorded only once.
class MediaEngagementSession : public base::RefCounted<MediaEngagementSession> {
 public:
  enum class RestoreType {
    kNotRestored = 0,
    kRestored,
  };

  MediaEngagementSession(MediaEngagementService* service,
                         const url::Origin& origin,
                         RestoreType restore_status,
                         ukm::SourceId ukm_source_id);

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

  // Returns whether the session's origin is same origin with |url|.
  bool IsSameOriginWith(const GURL& url) const;

  // Record that the session received a significant playback from a media
  // element.
  void RecordSignificantMediaElementPlayback();

  // Record that the session received a significant playback from an audio
  // context.
  void RecordSignificantAudioContextPlayback();

  // Record the length of an ignored media playback.
  void RecordShortPlaybackIgnored(int length_msec);

  // Register new audible/significant players. `significant_players` can't be
  // greater than `audible_players`.
  void RegisterAudiblePlayers(int32_t audible_players,
                              int32_t significant_players);

  // Whether any significant playback was recorded.
  bool WasSignificantPlaybackRecorded() const;

  bool significant_media_element_playback_recorded() const;
  bool significant_audio_context_playback_recorded() const;
  const url::Origin& origin() const;

 private:
  friend class base::RefCounted<MediaEngagementSession>;
  friend class MediaEngagementSessionTest;

  // Private because the class is RefCounted.
  ~MediaEngagementSession();

  // Records a significant playback that is either a media element or an audio
  // context playback.
  void RecordSignificantPlayback();

  // Record the score and change in score to UKM.
  void RecordUkmMetrics();

  // Returns whether the session has a pending playback that needs to be
  // committed into the database.
  bool HasPendingPlaybackToCommit() const;

  // Returns whether the session has data that needs to be committed into the
  // database.
  bool HasPendingDataToCommit() const;

  // Commits any pending data to website settings.
  void CommitPendingData();

  // Weak pointer because |this| has a lifetime shorter than it.
  raw_ptr<MediaEngagementService> service_;

  // Origin associated with the session.
  url::Origin origin_;

  // UKM source ID associated with the session. It will be used by all the
  // session clients.
  ukm::SourceId ukm_source_id_;

  // Delta counts are counts to be added to the score while total counts are
  // the sum of all the changes that happened during the session lifetime. The
  // total count is recorded as delta with regards to UKM.
  int32_t audible_players_delta_ = 0;
  int32_t significant_players_delta_ = 0;
  int32_t audible_players_total_ = 0;
  int32_t significant_players_total_ = 0;

  bool significant_media_element_playback_recorded_ = false;
  bool significant_audio_context_playback_recorded_ = false;
  struct PendingDataToCommit {
    bool visit = true;
    bool audio_context_playback = false;
    bool media_element_playback = false;
    bool players = false;
  };
  PendingDataToCommit pending_data_to_commit_;

  // The time the first significant playback occurred.
  base::Time first_significant_playback_time_;

  // Whether the session was restored.
  RestoreType restore_status_ = RestoreType::kNotRestored;

  // If the |is_high_| bit has changed since this object was created.
  bool high_score_changed_ = false;
};

#endif  // CHROME_BROWSER_MEDIA_MEDIA_ENGAGEMENT_SESSION_H_