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

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: Tab discarding resistant 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
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/browser_thread.h"
12 #include "content/public/browser/web_contents_user_data.h"
13 #include "content/public/common/page_importance_signals.h"
14
15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::TabUsageRecorder::WebContentsData);
16
17 namespace metrics {
18
19 namespace {
20
21 // This global is never freed.
22 TabUsageRecorder* g_tab_usage_recorder = nullptr;
23
24 } // namespace
25
26 // This class is responsible for recording the metrics. It also keeps track of
27 // the pinned state of the tab.
28 class TabUsageRecorder::WebContentsData
29 : public content::WebContentsUserData<WebContentsData> {
30 public:
31 ~WebContentsData() override;
32
33 void RecordTabDeactivation();
34 void RecordTabReactivation();
35
36 void OnTabPinnedStateChanging(bool is_pinned);
37
38 private:
39 friend class content::WebContentsUserData<WebContentsData>;
40
41 explicit WebContentsData(content::WebContents* contents);
42
43 // Returns true if |contents_|'s URL is bookmarked.
44 bool IsBookmarked();
45
46 // The WebContents associated to this instance.
47 content::WebContents* contents_;
48
49 // Indicates if the tab is pinned to the tab strip.
50 bool is_pinned_;
51
52 DISALLOW_COPY_AND_ASSIGN(WebContentsData);
53 };
54
55 TabUsageRecorder::WebContentsData::~WebContentsData() = default;
56
57 void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging(
58 bool is_pinned) {
59 is_pinned_ = is_pinned;
60 }
61
62 TabUsageRecorder::WebContentsData::WebContentsData(
63 content::WebContents* contents)
64 : contents_(contents), is_pinned_(false) {}
65
66 bool TabUsageRecorder::WebContentsData::IsBookmarked() {
67 bookmarks::BookmarkModel* bookmark_model =
68 BookmarkModelFactory::GetForBrowserContextIfExists(
69 contents_->GetBrowserContext());
70
71 return bookmark_model &&
72 bookmark_model->IsBookmarked(contents_->GetLastCommittedURL());
73 }
74
75 void TabUsageRecorder::WebContentsData::RecordTabDeactivation() {
76 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Pinned", is_pinned_);
77 UMA_HISTOGRAM_BOOLEAN(
78 "Tab.Deactivation.HadFormInteraction",
79 contents_->GetPageImportanceSignals().had_form_interaction);
80 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Bookmarked", IsBookmarked());
81 }
82
83 void TabUsageRecorder::WebContentsData::RecordTabReactivation() {
84 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Pinned", is_pinned_);
85 UMA_HISTOGRAM_BOOLEAN(
86 "Tab.Reactivation.HadFormInteraction",
87 contents_->GetPageImportanceSignals().had_form_interaction);
88 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Bookmarked", IsBookmarked());
89 }
90
91 // static
92 void TabUsageRecorder::Initialize() {
93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
94 DCHECK(!g_tab_usage_recorder);
95 g_tab_usage_recorder = new TabUsageRecorder();
96 }
97
98 void TabUsageRecorder::OnTabDeactivated(content::WebContents* contents) {
99 GetWebContentsData(contents)->RecordTabDeactivation();
100 }
101
102 void TabUsageRecorder::OnTabReactivated(content::WebContents* contents) {
103 GetWebContentsData(contents)->RecordTabReactivation();
104 }
105
106 void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model,
107 content::WebContents* contents,
108 int index,
109 bool foreground) {
110 // Set the initial pinned value.
111 TabPinnedStateChanged(tab_strip_model, contents, index);
112 }
113
114 void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model,
115 content::WebContents* contents,
116 int index) {
117 GetWebContentsData(contents)->OnTabPinnedStateChanging(
118 tab_strip_model->IsTabPinned(index));
119 }
120
121 TabUsageRecorder::TabUsageRecorder()
122 : tab_reactivation_tracker_(this),
123 browser_tab_strip_tracker_(this, nullptr, nullptr) {
124 browser_tab_strip_tracker_.Init(
125 BrowserTabStripTracker::InitWith::ALL_BROWERS);
126 }
127
128 TabUsageRecorder::~TabUsageRecorder() = default;
129
130 TabUsageRecorder::WebContentsData* TabUsageRecorder::GetWebContentsData(
131 content::WebContents* contents) {
132 WebContentsData::CreateForWebContents(contents);
133 return WebContentsData::FromWebContents(contents);
134 }
135
136 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698