OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/metrics/tab_usage_recorder.h" | 5 #include "chrome/browser/metrics/tab_usage_recorder.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 8 #include "base/time/time.h" |
9 #include "chrome/browser/ui/tabs/tab_strip_model.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" | 10 #include "content/public/browser/web_contents_user_data.h" |
12 #include "content/public/common/page_importance_signals.h" | 11 #include "content/public/common/page_importance_signals.h" |
13 | 12 |
14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::TabUsageRecorder::WebContentsData); | 13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::TabUsageRecorder::WebContentsData); |
15 | 14 |
16 namespace metrics { | 15 namespace metrics { |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 // This global is never freed. | 19 // The recorder is never stopped, thus it is ok to leak. |
21 TabUsageRecorder* g_tab_usage_recorder = nullptr; | 20 TabUsageRecorder* g_tab_usage_recorder = nullptr; |
22 | 21 |
23 } // namespace | 22 } // namespace |
24 | 23 |
25 // This class is responsible for recording the metrics. It also keeps track of | 24 // This class is responsible for recording the metrics. It also keeps track of |
26 // the pinned state of the tab. | 25 // the pinned state of the tab. |
27 class TabUsageRecorder::WebContentsData | 26 class TabUsageRecorder::WebContentsData |
28 : public content::WebContentsUserData<WebContentsData> { | 27 : public content::WebContentsUserData<WebContentsData> { |
29 public: | 28 public: |
30 ~WebContentsData() override; | 29 ~WebContentsData() override; |
31 | 30 |
32 void RecordTabDeactivation(); | 31 void RecordTabDeactivation(); |
33 void RecordTabReactivation(); | 32 void RecordTabReactivation(); |
34 | 33 |
35 void OnTabPinnedStateChanging(bool is_pinned); | 34 void OnTabPinnedStateChanging(bool is_pinned); |
36 | 35 |
37 private: | 36 private: |
38 friend class content::WebContentsUserData<WebContentsData>; | 37 friend class content::WebContentsUserData<WebContentsData>; |
39 | 38 |
40 explicit WebContentsData(content::WebContents* contents); | 39 explicit WebContentsData(content::WebContents* contents); |
41 | 40 |
42 // Returns true if |contents_|'s URL is bookmarked. | |
43 bool IsBookmarked(); | |
44 | |
45 // The WebContents associated to this instance. | 41 // The WebContents associated to this instance. |
46 content::WebContents* contents_; | 42 content::WebContents* contents_; |
47 | 43 |
48 // Indicates if the tab is pinned to the tab strip. | 44 // Indicates if the tab is pinned to the tab strip. |
49 bool is_pinned_; | 45 bool is_pinned_; |
50 | 46 |
| 47 base::TimeTicks last_inactive_time_; |
| 48 |
51 DISALLOW_COPY_AND_ASSIGN(WebContentsData); | 49 DISALLOW_COPY_AND_ASSIGN(WebContentsData); |
52 }; | 50 }; |
53 | 51 |
54 TabUsageRecorder::WebContentsData::~WebContentsData() = default; | 52 TabUsageRecorder::WebContentsData::~WebContentsData() = default; |
55 | 53 |
56 void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging( | 54 void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging( |
57 bool is_pinned) { | 55 bool is_pinned) { |
58 is_pinned_ = is_pinned; | 56 is_pinned_ = is_pinned; |
59 } | 57 } |
60 | 58 |
61 TabUsageRecorder::WebContentsData::WebContentsData( | 59 TabUsageRecorder::WebContentsData::WebContentsData( |
62 content::WebContents* contents) | 60 content::WebContents* contents) |
63 : contents_(contents), is_pinned_(false) {} | 61 : contents_(contents), is_pinned_(false) {} |
64 | 62 |
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() { | 63 void TabUsageRecorder::WebContentsData::RecordTabDeactivation() { |
| 64 last_inactive_time_ = base::TimeTicks::Now(); |
75 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Pinned", is_pinned_); | 65 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Pinned", is_pinned_); |
76 UMA_HISTOGRAM_BOOLEAN( | 66 UMA_HISTOGRAM_BOOLEAN( |
77 "Tab.Deactivation.HadFormInteraction", | 67 "Tab.Deactivation.HadFormInteraction", |
78 contents_->GetPageImportanceSignals().had_form_interaction); | 68 contents_->GetPageImportanceSignals().had_form_interaction); |
79 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Bookmarked", IsBookmarked()); | |
80 } | 69 } |
81 | 70 |
82 void TabUsageRecorder::WebContentsData::RecordTabReactivation() { | 71 void TabUsageRecorder::WebContentsData::RecordTabReactivation() { |
| 72 bool had_form_interaction = |
| 73 contents_->GetPageImportanceSignals().had_form_interaction; |
| 74 |
83 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Pinned", is_pinned_); | 75 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Pinned", is_pinned_); |
84 UMA_HISTOGRAM_BOOLEAN( | 76 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.HadFormInteraction", |
85 "Tab.Reactivation.HadFormInteraction", | 77 had_form_interaction); |
86 contents_->GetPageImportanceSignals().had_form_interaction); | 78 |
87 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Bookmarked", IsBookmarked()); | 79 base::TimeDelta time_to_reactivation = |
| 80 base::TimeTicks::Now() - last_inactive_time_; |
| 81 if (is_pinned_ || had_form_interaction) { |
| 82 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 83 "Tab.TimeToReactivation.Important", time_to_reactivation, |
| 84 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromHours(2), 100); |
| 85 } else { |
| 86 UMA_HISTOGRAM_CUSTOM_TIMES( |
| 87 "Tab.TimeToReactivation.Normal", time_to_reactivation, |
| 88 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromHours(2), 100); |
| 89 } |
88 } | 90 } |
89 | 91 |
90 // static | 92 // static |
91 void TabUsageRecorder::Initialize() { | 93 void TabUsageRecorder::Initialize() { |
92 DCHECK(!g_tab_usage_recorder); | 94 DCHECK(!g_tab_usage_recorder); |
93 g_tab_usage_recorder = new TabUsageRecorder(); | 95 g_tab_usage_recorder = new TabUsageRecorder(); |
94 } | 96 } |
95 | 97 |
96 void TabUsageRecorder::OnTabDeactivated(content::WebContents* contents) { | 98 void TabUsageRecorder::OnTabDeactivated(content::WebContents* contents) { |
97 GetWebContentsData(contents)->RecordTabDeactivation(); | 99 GetWebContentsData(contents)->RecordTabDeactivation(); |
98 } | 100 } |
99 | 101 |
100 void TabUsageRecorder::OnTabReactivated(content::WebContents* contents) { | 102 void TabUsageRecorder::OnTabReactivated(content::WebContents* contents) { |
101 GetWebContentsData(contents)->RecordTabReactivation(); | 103 GetWebContentsData(contents)->RecordTabReactivation(); |
102 } | 104 } |
103 | 105 |
104 void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model, | 106 void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model, |
105 content::WebContents* contents, | 107 content::WebContents* contents, |
106 int index, | 108 int index, |
107 bool foreground) { | 109 bool foreground) { |
108 // Set the initial pinned value. | 110 // Set the initial pin state. |
109 TabPinnedStateChanged(tab_strip_model, contents, index); | 111 TabPinnedStateChanged(tab_strip_model, contents, index); |
110 } | 112 } |
111 | 113 |
112 void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model, | 114 void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model, |
113 content::WebContents* contents, | 115 content::WebContents* contents, |
114 int index) { | 116 int index) { |
115 GetWebContentsData(contents)->OnTabPinnedStateChanging( | 117 GetWebContentsData(contents)->OnTabPinnedStateChanging( |
116 tab_strip_model->IsTabPinned(index)); | 118 tab_strip_model->IsTabPinned(index)); |
117 } | 119 } |
118 | 120 |
119 TabUsageRecorder::TabUsageRecorder() | 121 TabUsageRecorder::TabUsageRecorder() |
120 : tab_reactivation_tracker_(this), | 122 : tab_reactivation_tracker_(this), |
121 browser_tab_strip_tracker_(this, nullptr, nullptr) { | 123 browser_tab_strip_tracker_(this, nullptr, nullptr) { |
122 browser_tab_strip_tracker_.Init( | 124 browser_tab_strip_tracker_.Init( |
123 BrowserTabStripTracker::InitWith::ALL_BROWERS); | 125 BrowserTabStripTracker::InitWith::ALL_BROWERS); |
124 } | 126 } |
125 | 127 |
126 TabUsageRecorder::~TabUsageRecorder() = default; | 128 TabUsageRecorder::~TabUsageRecorder() = default; |
127 | 129 |
128 TabUsageRecorder::WebContentsData* TabUsageRecorder::GetWebContentsData( | 130 TabUsageRecorder::WebContentsData* TabUsageRecorder::GetWebContentsData( |
129 content::WebContents* contents) { | 131 content::WebContents* contents) { |
130 WebContentsData::CreateForWebContents(contents); | 132 WebContentsData::CreateForWebContents(contents); |
131 return WebContentsData::FromWebContents(contents); | 133 return WebContentsData::FromWebContents(contents); |
132 } | 134 } |
133 | 135 |
134 } // namespace metrics | 136 } // namespace metrics |
OLD | NEW |