Chromium Code Reviews| 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/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 used to track the activation/deactivation cycle per | |
| 27 // WebContents. It also keeps track of the pinned state of the tab. | |
| 28 class TabUsageRecorder::WebContentsData | |
| 29 : public content::WebContentsUserData<WebContentsData> { | |
| 30 public: | |
| 31 ~WebContentsData() override; | |
| 32 | |
| 33 void OnTabActivating(); | |
| 34 void OnTabDeactivating(); | |
| 35 void OnTabClosing(); | |
| 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 void RecordTabDeactivation(); | |
| 47 void RecordTabReactivation(); | |
| 48 | |
| 49 // The WebContents associated to this instance. | |
| 50 content::WebContents* contents_; | |
| 51 | |
| 52 // Indicates if the tab is pinned to the tab strip. | |
| 53 bool is_pinned_; | |
| 54 | |
| 55 // Indicates if the tab has been deactivated before as to only count | |
| 56 // reactivations. | |
| 57 bool was_deactivated_once_; | |
| 58 | |
| 59 // The deactivation metric is not recorded for closing tabs. | |
| 60 bool is_closing_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(WebContentsData); | |
| 63 }; | |
| 64 | |
| 65 TabUsageRecorder::WebContentsData::~WebContentsData() = default; | |
| 66 | |
| 67 void TabUsageRecorder::WebContentsData::OnTabActivating() { | |
| 68 if (was_deactivated_once_) | |
| 69 RecordTabReactivation(); | |
| 70 } | |
| 71 | |
| 72 void TabUsageRecorder::WebContentsData::OnTabDeactivating() { | |
| 73 was_deactivated_once_ = true; | |
| 74 if (!is_closing_) | |
| 75 RecordTabDeactivation(); | |
| 76 } | |
| 77 | |
| 78 void TabUsageRecorder::WebContentsData::OnTabClosing() { | |
| 79 is_closing_ = true; | |
| 80 } | |
| 81 | |
| 82 void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging( | |
| 83 bool is_pinned) { | |
| 84 is_pinned_ = is_pinned; | |
| 85 } | |
| 86 | |
| 87 TabUsageRecorder::WebContentsData::WebContentsData( | |
| 88 content::WebContents* contents) | |
| 89 : contents_(contents), | |
| 90 is_pinned_(false), | |
| 91 was_deactivated_once_(false), | |
| 92 is_closing_(false) {} | |
| 93 | |
| 94 bool TabUsageRecorder::WebContentsData::IsBookmarked() { | |
| 95 bookmarks::BookmarkModel* bookmark_model = | |
| 96 BookmarkModelFactory::GetForBrowserContextIfExists( | |
| 97 contents_->GetBrowserContext()); | |
| 98 | |
| 99 return bookmark_model && | |
| 100 bookmark_model->IsBookmarked(contents_->GetLastCommittedURL()); | |
| 101 } | |
| 102 | |
| 103 void TabUsageRecorder::WebContentsData::RecordTabDeactivation() { | |
| 104 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Pinned", is_pinned_); | |
| 105 UMA_HISTOGRAM_BOOLEAN( | |
| 106 "Tab.Deactivation.HadFormInteraction", | |
| 107 contents_->GetPageImportanceSignals().had_form_interaction); | |
| 108 UMA_HISTOGRAM_BOOLEAN("Tab.Deactivation.Bookmarked", IsBookmarked()); | |
| 109 } | |
| 110 | |
| 111 void TabUsageRecorder::WebContentsData::RecordTabReactivation() { | |
| 112 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Pinned", is_pinned_); | |
| 113 UMA_HISTOGRAM_BOOLEAN( | |
| 114 "Tab.Reactivation.HadFormInteraction", | |
| 115 contents_->GetPageImportanceSignals().had_form_interaction); | |
| 116 UMA_HISTOGRAM_BOOLEAN("Tab.Reactivation.Bookmarked", IsBookmarked()); | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 void TabUsageRecorder::Initialize() { | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 DCHECK(!g_tab_usage_recorder); | |
| 123 g_tab_usage_recorder = new TabUsageRecorder(); | |
| 124 } | |
| 125 | |
| 126 void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model, | |
| 127 content::WebContents* contents, | |
| 128 int index, | |
| 129 bool foreground) { | |
| 130 WebContentsData::CreateForWebContents(contents); | |
|
Georges Khalil
2016/09/20 21:36:31
I think we can have news WebContentses that don't
Patrick Monette
2016/09/21 21:03:18
Done.
| |
| 131 // Set the initial pinned value. | |
| 132 TabPinnedStateChanged(tab_strip_model, contents, index); | |
| 133 } | |
| 134 | |
| 135 void TabUsageRecorder::TabClosingAt(TabStripModel* tab_strip_model, | |
| 136 content::WebContents* contents, | |
| 137 int index) { | |
| 138 WebContentsData::FromWebContents(contents)->OnTabClosing(); | |
| 139 } | |
| 140 | |
| 141 void TabUsageRecorder::ActiveTabChanged(content::WebContents* old_contents, | |
| 142 content::WebContents* new_contents, | |
| 143 int index, | |
| 144 int reason) { | |
| 145 if (old_contents) | |
| 146 WebContentsData::FromWebContents(old_contents)->OnTabDeactivating(); | |
| 147 WebContentsData::FromWebContents(new_contents)->OnTabActivating(); | |
| 148 } | |
| 149 | |
| 150 void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model, | |
| 151 content::WebContents* contents, | |
| 152 int index) { | |
| 153 WebContentsData::FromWebContents(contents)->OnTabPinnedStateChanging( | |
| 154 tab_strip_model->IsTabPinned(index)); | |
| 155 } | |
| 156 | |
| 157 TabUsageRecorder::TabUsageRecorder() | |
| 158 : browser_tab_strip_tracker_(this, nullptr, nullptr) { | |
| 159 browser_tab_strip_tracker_.Init( | |
| 160 BrowserTabStripTracker::InitWith::ALL_BROWERS); | |
| 161 } | |
| 162 | |
| 163 TabUsageRecorder::~TabUsageRecorder() = default; | |
| 164 | |
| 165 } // namespace metrics | |
| OLD | NEW |