Commit 30d90aac authored by Olivier Li's avatar Olivier Li Committed by Chromium LUCI CQ

Create an interface that TabStatsDataStore conforms to.

This is the first step needed to enable defining a similar class that
will detect different chromium usage scenarios to be recorded alongside
power use.

Bug: 1153193
Change-Id: I29e59a8cffae7cc5962882f78c8fbbf29073a170
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2597013
Commit-Queue: Oliver Li <olivierli@chromium.org>
Reviewed-by: default avatarCaitlin Fischer <caitlinfischer@google.com>
Reviewed-by: default avatarRobert Kaplow <rkaplow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844802}
parent bcc8d251
......@@ -3740,6 +3740,7 @@ static_library("browser") {
"metrics/incognito_observer_desktop.cc",
"metrics/tab_stats_data_store.cc",
"metrics/tab_stats_data_store.h",
"metrics/tab_stats_handler.h",
"metrics/tab_stats_tracker.cc",
"metrics/tab_stats_tracker.h",
"metrics/tab_stats_tracker_delegate.h",
......
......@@ -102,6 +102,24 @@ void TabStatsDataStore::OnTabReplaced(content::WebContents* old_contents,
existing_tabs_[new_contents] = old_contents_id;
}
void TabStatsDataStore::OnTabInteraction(content::WebContents* web_contents) {
DCHECK(base::Contains(existing_tabs_, web_contents));
TabID web_contents_id = GetTabID(web_contents);
// Mark the tab as interacted with in all the intervals.
for (auto& interval_map : interval_maps_) {
DCHECK(base::Contains(*interval_map, web_contents_id));
(*interval_map)[web_contents_id].interacted_during_interval = true;
}
}
void TabStatsDataStore::OnTabAudible(content::WebContents* web_contents) {
OnTabAudibleOrVisible(web_contents);
}
void TabStatsDataStore::OnTabVisible(content::WebContents* web_contents) {
OnTabAudibleOrVisible(web_contents);
}
void TabStatsDataStore::UpdateMaxTabsPerWindowIfNeeded(size_t value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (value <= tab_stats_.max_tab_per_window)
......@@ -129,24 +147,6 @@ void TabStatsDataStore::ResetMaximumsToCurrentState() {
}
}
void TabStatsDataStore::OnTabInteraction(content::WebContents* web_contents) {
DCHECK(base::Contains(existing_tabs_, web_contents));
TabID web_contents_id = GetTabID(web_contents);
// Mark the tab as interacted with in all the intervals.
for (auto& interval_map : interval_maps_) {
DCHECK(base::Contains(*interval_map, web_contents_id));
(*interval_map)[web_contents_id].interacted_during_interval = true;
}
}
void TabStatsDataStore::OnTabAudible(content::WebContents* web_contents) {
OnTabAudibleOrVisible(web_contents);
}
void TabStatsDataStore::OnTabVisible(content::WebContents* web_contents) {
OnTabAudibleOrVisible(web_contents);
}
TabStatsDataStore::TabsStateDuringIntervalMap*
TabStatsDataStore::AddInterval() {
// Creates the interval and initialize its data.
......
......@@ -12,6 +12,7 @@
#include "base/gtest_prod_util.h"
#include "base/optional.h"
#include "base/sequence_checker.h"
#include "chrome/browser/metrics/tab_stats_handler.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_state.mojom.h"
using mojom::LifecycleUnitDiscardReason;
......@@ -27,9 +28,9 @@ namespace metrics {
FORWARD_DECLARE_TEST(TabStatsTrackerBrowserTest,
TabDeletionGetsHandledProperly);
// The data store that keeps track of all the information that the
// TabStatsTracker wants to track.
class TabStatsDataStore {
// Keeps track of all the information needed by TabStatsTracker. Stats are
// stored internally to be retrieved at a later point.
class TabStatsDataStore : public TabStatsHandler {
public:
// Houses all of the statistics gathered by the TabStatsTracker.
struct TabsStats {
......@@ -82,14 +83,16 @@ class TabStatsDataStore {
explicit TabStatsDataStore(PrefService* pref_service);
virtual ~TabStatsDataStore();
// Functions used to update the window/tab count.
void OnWindowAdded();
void OnWindowRemoved();
// Virtual for unittesting.
virtual void OnTabAdded(content::WebContents* web_contents);
virtual void OnTabRemoved(content::WebContents* web_contents);
// TabStatsHandler:
void OnWindowAdded() override;
void OnWindowRemoved() override;
void OnTabAdded(content::WebContents* web_contents) override;
void OnTabRemoved(content::WebContents* web_contents) override;
void OnTabReplaced(content::WebContents* old_contents,
content::WebContents* new_contents);
content::WebContents* new_contents) override;
void OnTabInteraction(content::WebContents* web_contents) override;
void OnTabAudible(content::WebContents* web_contents) override;
void OnTabVisible(content::WebContents* web_contents) override;
// Update the maximum number of tabs in a single window if |value| exceeds
// this.
......@@ -101,18 +104,6 @@ class TabStatsDataStore {
// metrics have been reported.
void ResetMaximumsToCurrentState();
// Records that there's been a direct user interaction with a tab, see the
// comment for |DidGetUserInteraction| in
// content/public/browser/web_contents_observer.h for a list of the possible
// type of interactions.
void OnTabInteraction(content::WebContents* web_contents);
// Records that a tab became audible.
void OnTabAudible(content::WebContents* web_contents);
// Records that a tab became visible.
void OnTabVisible(content::WebContents* web_contents);
// Creates a new interval map. The returned pointer is owned by |this|.
TabsStateDuringIntervalMap* AddInterval();
......@@ -120,9 +111,7 @@ class TabStatsDataStore {
void ResetIntervalData(TabsStateDuringIntervalMap* interval_map);
const TabsStats& tab_stats() const { return tab_stats_; }
base::Optional<TabID> GetTabIDForTesting(content::WebContents* web_contents);
base::flat_map<content::WebContents*, TabID>* existing_tabs_for_testing() {
return &existing_tabs_;
}
......
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_METRICS_TAB_STATS_HANDLER_H_
#define CHROME_BROWSER_METRICS_TAB_STATS_HANDLER_H_
namespace content {
class WebContents;
} // namespace content
namespace metrics {
// Defines an interface for a class used to handle tab and window related stats.
// Handling the events can be either storing them for logging purposes,
// forwarding them to another class or taking reactive measures when certain
// criteria are met.
class TabStatsHandler {
public:
// Functions used to update the window/tab count.
virtual void OnWindowAdded() = 0;
virtual void OnWindowRemoved() = 0;
virtual void OnTabAdded(content::WebContents* web_contents) = 0;
virtual void OnTabRemoved(content::WebContents* web_contents) = 0;
virtual void OnTabReplaced(content::WebContents* old_contents,
content::WebContents* new_contents) = 0;
// Records that there's been a direct user interaction with a tab, see the
// comment for |DidGetUserInteraction| in
// content/public/browser/web_contents_observer.h for a list of the possible
// type of interactions.
virtual void OnTabInteraction(content::WebContents* web_contents) = 0;
// Records that a tab became audible.
virtual void OnTabAudible(content::WebContents* web_contents) = 0;
// Records that a tab became visible.
virtual void OnTabVisible(content::WebContents* web_contents) = 0;
};
} // namespace metrics
#endif // CHROME_BROWSER_METRICS_TAB_STATS_HANDLER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment