Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(712)

Side by Side Diff: chrome/browser/metrics/tab_usage_recorder.cc

Issue 2335203003: Add metrics to keep track of the tab activate/deactivate cycle (Closed)
Patch Set: Removed a problematic dcheck Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/metrics/tab_usage_recorder.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/metrics/tab_usage_recorder.h"
6
7 #include "base/metrics/histogram_macros.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "components/bookmarks/browser/bookmark_model.h"
11 #include "content/public/browser/web_contents_user_data.h"
12 #include "content/public/common/page_importance_signals.h"
13
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::TabUsageRecorder::WebContentsData);
15
16 namespace metrics {
17
18 namespace {
19
20 // This global is never freed.
21 TabUsageRecorder* g_tab_usage_recorder = nullptr;
22
23 } // namespace
24
25 // This class is responsible for recording the metrics. It also keeps track of
26 // the pinned state of the tab.
27 class TabUsageRecorder::WebContentsData
28 : public content::WebContentsUserData<WebContentsData> {
29 public:
30 ~WebContentsData() override;
31
32 void RecordTabDeactivation();
33 void RecordTabReactivation();
34
35 void OnTabPinnedStateChanging(bool is_pinned);
36
37 private:
38 friend class content::WebContentsUserData<WebContentsData>;
39
40 explicit WebContentsData(content::WebContents* contents);
41
42 // Returns true if |contents_|'s URL is bookmarked.
43 bool IsBookmarked();
44
45 // The WebContents associated to this instance.
46 content::WebContents* contents_;
47
48 // Indicates if the tab is pinned to the tab strip.
49 bool is_pinned_;
50
51 DISALLOW_COPY_AND_ASSIGN(WebContentsData);
52 };
53
54 TabUsageRecorder::WebContentsData::~WebContentsData() = default;
55
56 void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging(
57 bool is_pinned) {
58 is_pinned_ = is_pinned;
59 }
60
61 TabUsageRecorder::WebContentsData::WebContentsData(
62 content::WebContents* contents)
63 : contents_(contents), is_pinned_(false) {}
64
65 bool TabUsageRecorder::WebContentsData::IsBookmarked() {
66 bookmarks::BookmarkModel* bookmark_model =
67 BookmarkModelFactory::GetForBrowserContextIfExists(
68 contents_->GetBrowserContext());
69
70 return bookmark_model &&
71 bookmark_model->IsBookmarked(contents_->GetLastCommittedURL());
72 }
73
74 void TabUsageRecorder::WebContentsData::RecordTabDeactivation() {
75 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Pinned", is_pinned_);
76 UMA_HISTOGRAM_BOOLEAN(
77 "Tab.Deactivation.HadFormInteraction",
78 contents_->GetPageImportanceSignals().had_form_interaction);
79 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Bookmarked", IsBookmarked());
80 }
81
82 void TabUsageRecorder::WebContentsData::RecordTabReactivation() {
83 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Pinned", is_pinned_);
84 UMA_HISTOGRAM_BOOLEAN(
85 "Tab.Reactivation.HadFormInteraction",
86 contents_->GetPageImportanceSignals().had_form_interaction);
87 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Bookmarked", IsBookmarked());
88 }
89
90 // static
91 void TabUsageRecorder::Initialize() {
92 DCHECK(!g_tab_usage_recorder);
93 g_tab_usage_recorder = new TabUsageRecorder();
94 }
95
96 void TabUsageRecorder::OnTabDeactivated(content::WebContents* contents) {
97 GetWebContentsData(contents)->RecordTabDeactivation();
98 }
99
100 void TabUsageRecorder::OnTabReactivated(content::WebContents* contents) {
101 GetWebContentsData(contents)->RecordTabReactivation();
102 }
103
104 void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model,
105 content::WebContents* contents,
106 int index,
107 bool foreground) {
108 // Set the initial pinned value.
109 TabPinnedStateChanged(tab_strip_model, contents, index);
110 }
111
112 void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model,
113 content::WebContents* contents,
114 int index) {
115 GetWebContentsData(contents)->OnTabPinnedStateChanging(
116 tab_strip_model->IsTabPinned(index));
117 }
118
119 TabUsageRecorder::TabUsageRecorder()
120 : tab_reactivation_tracker_(this),
121 browser_tab_strip_tracker_(this, nullptr, nullptr) {
122 browser_tab_strip_tracker_.Init(
123 BrowserTabStripTracker::InitWith::ALL_BROWERS);
124 }
125
126 TabUsageRecorder::~TabUsageRecorder() = default;
127
128 TabUsageRecorder::WebContentsData* TabUsageRecorder::GetWebContentsData(
129 content::WebContents* contents) {
130 WebContentsData::CreateForWebContents(contents);
131 return WebContentsData::FromWebContents(contents);
132 }
133
134 } // namespace metrics
OLDNEW
« no previous file with comments | « chrome/browser/metrics/tab_usage_recorder.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698