OLD | NEW |
(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/ui/tabs/tab_strip_model.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/web_contents_user_data.h" |
| 11 |
| 12 DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::TabUsageRecorder::WebContentsData); |
| 13 |
| 14 namespace metrics { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // This global is never freed. |
| 19 TabUsageRecorder* g_tab_usage_recorder = nullptr; |
| 20 |
| 21 void RecordTabDeactivation(bool is_pinned) { |
| 22 if (is_pinned) |
| 23 UMA_HISTOGRAM_COUNTS("Tab.DeactivationCount_Pinned", 1); |
| 24 else |
| 25 UMA_HISTOGRAM_COUNTS("Tab.DeactivationCount_Regular", 1); |
| 26 } |
| 27 |
| 28 void RecordTabReactivation(bool is_pinned) { |
| 29 if (is_pinned) |
| 30 UMA_HISTOGRAM_COUNTS("Tab.ReactivationCount_Pinned", 1); |
| 31 else |
| 32 UMA_HISTOGRAM_COUNTS("Tab.ReactivationCount_Regular", 1); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 // This class is used to track the activation/deactivation cycle per |
| 38 // WebContents. It also keeps track of the pinned state of the tab. |
| 39 class TabUsageRecorder::WebContentsData |
| 40 : public content::WebContentsUserData<WebContentsData> { |
| 41 public: |
| 42 static void CreateForWebContents(content::WebContents* contents, |
| 43 bool is_pinned); |
| 44 |
| 45 ~WebContentsData() override; |
| 46 |
| 47 void OnTabActivating(); |
| 48 void OnTabDeactivating(); |
| 49 void OnTabClosing(); |
| 50 void OnTabPinnedStateChanging(bool is_pinned); |
| 51 |
| 52 private: |
| 53 friend class content::WebContentsUserData<WebContentsData>; |
| 54 |
| 55 explicit WebContentsData(content::WebContents* contents, bool is_pinned); |
| 56 |
| 57 // Indicates if the tab is pinned to the tab strip. |
| 58 bool is_pinned_; |
| 59 |
| 60 // Indicates if the tab has been deactivated before as to only count |
| 61 // reactivations. |
| 62 bool was_deactivated_once_; |
| 63 |
| 64 // The deactivation metric is not recorded for closing tabs. |
| 65 bool is_closing_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(WebContentsData); |
| 68 }; |
| 69 |
| 70 // static |
| 71 void TabUsageRecorder::WebContentsData::CreateForWebContents( |
| 72 content::WebContents* contents, |
| 73 bool is_pinned) { |
| 74 DCHECK(contents); |
| 75 if (FromWebContents(contents)) |
| 76 return; |
| 77 |
| 78 contents->SetUserData(UserDataKey(), |
| 79 new WebContentsData(contents, is_pinned)); |
| 80 } |
| 81 |
| 82 TabUsageRecorder::WebContentsData::~WebContentsData() = default; |
| 83 |
| 84 void TabUsageRecorder::WebContentsData::OnTabActivating() { |
| 85 if (was_deactivated_once_) { |
| 86 RecordTabReactivation(is_pinned_); |
| 87 } |
| 88 } |
| 89 |
| 90 void TabUsageRecorder::WebContentsData::OnTabDeactivating() { |
| 91 was_deactivated_once_ = true; |
| 92 if (!is_closing_) |
| 93 RecordTabDeactivation(is_pinned_); |
| 94 } |
| 95 |
| 96 void TabUsageRecorder::WebContentsData::OnTabClosing() { |
| 97 is_closing_ = true; |
| 98 } |
| 99 |
| 100 void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging( |
| 101 bool is_pinned) { |
| 102 is_pinned_ = is_pinned; |
| 103 } |
| 104 |
| 105 TabUsageRecorder::WebContentsData::WebContentsData( |
| 106 content::WebContents* contents, |
| 107 bool is_pinned) |
| 108 : is_pinned_(is_pinned), was_deactivated_once_(false), is_closing_(false) {} |
| 109 |
| 110 // static |
| 111 void TabUsageRecorder::Initialize() { |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 113 DCHECK(!g_tab_usage_recorder); |
| 114 g_tab_usage_recorder = new TabUsageRecorder(); |
| 115 } |
| 116 |
| 117 void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model, |
| 118 content::WebContents* contents, |
| 119 int index, |
| 120 bool foreground) { |
| 121 WebContentsData::CreateForWebContents(contents, |
| 122 tab_strip_model->IsTabPinned(index)); |
| 123 } |
| 124 |
| 125 void TabUsageRecorder::TabClosingAt(TabStripModel* tab_strip_model, |
| 126 content::WebContents* contents, |
| 127 int index) { |
| 128 WebContentsData::FromWebContents(contents)->OnTabClosing(); |
| 129 } |
| 130 |
| 131 void TabUsageRecorder::ActiveTabChanged(content::WebContents* old_contents, |
| 132 content::WebContents* new_contents, |
| 133 int index, |
| 134 int reason) { |
| 135 if (old_contents) |
| 136 WebContentsData::FromWebContents(old_contents)->OnTabDeactivating(); |
| 137 WebContentsData::FromWebContents(new_contents)->OnTabActivating(); |
| 138 } |
| 139 |
| 140 void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model, |
| 141 content::WebContents* contents, |
| 142 int index) { |
| 143 WebContentsData::FromWebContents(contents)->OnTabPinnedStateChanging( |
| 144 tab_strip_model->IsTabPinned(index)); |
| 145 } |
| 146 |
| 147 TabUsageRecorder::TabUsageRecorder() |
| 148 : browser_tab_strip_tracker_(this, nullptr, nullptr) { |
| 149 browser_tab_strip_tracker_.Init( |
| 150 BrowserTabStripTracker::InitWith::ALL_BROWERS); |
| 151 } |
| 152 |
| 153 TabUsageRecorder::~TabUsageRecorder() = default; |
| 154 |
| 155 } // namespace metrics |
OLD | NEW |