File: tab_usage_scenario_tracker.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 (112 lines) | stat: -rw-r--r-- 4,798 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
// Copyright 2021 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_METRICS_USAGE_SCENARIO_TAB_USAGE_SCENARIO_TRACKER_H_
#define CHROME_BROWSER_METRICS_USAGE_SCENARIO_TAB_USAGE_SCENARIO_TRACKER_H_

#include <optional>

#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/metrics/tab_stats/tab_stats_observer.h"
#include "chrome/browser/metrics/usage_scenario/usage_scenario_data_store.h"
#include "content/public/browser/visibility.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/display/display_observer.h"
#include "url/origin.h"

namespace content {
class WebContents;
}  // namespace content

namespace metrics {

// Used to relay information gathered from TabStatsTracker to
// UsageScenarioDataStore. No information is stored in this class.
class TabUsageScenarioTracker : public TabStatsObserver,
                                public display::DisplayObserver {
 public:
  // This class will not own |usage_scenario_data_store| so it needs to be
  // outlived by it.
  explicit TabUsageScenarioTracker(
      UsageScenarioDataStoreImpl* usage_scenario_data_store);
  TabUsageScenarioTracker(const TabUsageScenarioTracker& rhs) = delete;
  TabUsageScenarioTracker& operator=(const TabUsageScenarioTracker& rhs) =
      delete;
  ~TabUsageScenarioTracker() override;

  // TabStatsObserver:
  void OnTabAdded(content::WebContents* web_contents) override;
  void OnTabRemoved(content::WebContents* web_contents) override;
  void OnTabReplaced(content::WebContents* old_contents,
                     content::WebContents* new_contents) override;
  void OnTabVisibilityChanged(content::WebContents* web_contents) override;
  void OnTabDiscarded(content::WebContents* web_contents) override;
  void OnTabInteraction(content::WebContents* web_contents) override;
  void OnTabIsAudibleChanged(content::WebContents* web_contents) override;
  void OnMediaEffectivelyFullscreenChanged(content::WebContents* web_contents,
                                           bool is_fullscreen) override;
  void OnMediaDestroyed(content::WebContents* web_contents) override;
  void OnPrimaryMainFrameNavigationCommitted(
      content::WebContents* web_contents) override;
  void OnVideoStartedPlaying(content::WebContents* web_contents) override;
  void OnVideoStoppedPlaying(content::WebContents* web_contents) override;

  // display::DisplayObserver:
  void OnDisplayAdded(const display::Display& new_display) override;
  void OnDisplaysRemoved(const display::Displays& removed_displays) override;

 protected:
  // Invoked when displays are added or removed.
  void OnNumDisplaysChanged();

 private:
  using VisibleTabsMap = base::flat_map<content::WebContents*,
                                        std::pair<ukm::SourceId, url::Origin>>;

  // Can be overridden in tests.
  virtual int GetNumDisplays();

  // Should be called when |visible_tab_iter| switch from being visible to non
  // visible. |visible_tab_iter| should be an iterator in |visible_contents_|.
  void OnTabBecameHidden(VisibleTabsMap::iterator* visible_tab_iter);

  // Should be called when a WebContents is being destroyed, there's 2 possible
  // causes for this:
  //   - The tab that contains it is being removed.
  //   - A tab's WebContents is being replaced.
  void OnWebContentsRemoved(content::WebContents* web_contents);

  void InsertContentsInMapOfVisibleTabs(content::WebContents* web_contents);

  // Non-owning. Needs to outlive this class.
  raw_ptr<UsageScenarioDataStoreImpl> usage_scenario_data_store_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Keep track of the WebContents currently playing video.
  base::flat_set<raw_ptr<content::WebContents, CtnExperimental>>
      contents_playing_video_ GUARDED_BY_CONTEXT(sequence_checker_);

  // The last reading of the number of displays.
  std::optional<int> last_num_displays_;

  // Keep track of the visible WebContents and the navigation data associated to
  // them. The associated sourceID for tabs that don't have committed a main
  // frame navigation is ukm::kInvalidSourceID and the origin is empty.
  VisibleTabsMap visible_tabs_ GUARDED_BY_CONTEXT(sequence_checker_);

  // WebContents currently playing video fullscreen.
  base::flat_set<raw_ptr<content::WebContents, DanglingUntriaged>>
      contents_playing_video_fullscreen_;

  display::ScopedDisplayObserver display_observer_{this};

  // Used to verify that all access to |usage_scenario_data_store_| goes through
  // the same sequence as the one that created this object.
  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace metrics

#endif  // CHROME_BROWSER_METRICS_USAGE_SCENARIO_TAB_USAGE_SCENARIO_TRACKER_H_