| Index: chrome/browser/metrics/tab_usage_recorder.cc
|
| diff --git a/chrome/browser/metrics/tab_usage_recorder.cc b/chrome/browser/metrics/tab_usage_recorder.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9777c72405d05dd3eaedc552d6c1b1c24e571235
|
| --- /dev/null
|
| +++ b/chrome/browser/metrics/tab_usage_recorder.cc
|
| @@ -0,0 +1,155 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/metrics/tab_usage_recorder.h"
|
| +
|
| +#include "base/metrics/histogram_macros.h"
|
| +#include "chrome/browser/ui/tabs/tab_strip_model.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/web_contents_user_data.h"
|
| +
|
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(metrics::TabUsageRecorder::WebContentsData);
|
| +
|
| +namespace metrics {
|
| +
|
| +namespace {
|
| +
|
| +// This global is never freed.
|
| +TabUsageRecorder* g_tab_usage_recorder = nullptr;
|
| +
|
| +void RecordTabDeactivation(bool is_pinned) {
|
| + if (is_pinned)
|
| + UMA_HISTOGRAM_COUNTS("Tab.DeactivationCount_Pinned", 1);
|
| + else
|
| + UMA_HISTOGRAM_COUNTS("Tab.DeactivationCount_Regular", 1);
|
| +}
|
| +
|
| +void RecordTabReactivation(bool is_pinned) {
|
| + if (is_pinned)
|
| + UMA_HISTOGRAM_COUNTS("Tab.ReactivationCount_Pinned", 1);
|
| + else
|
| + UMA_HISTOGRAM_COUNTS("Tab.ReactivationCount_Regular", 1);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +// This class is used to track the activation/deactivation cycle per
|
| +// WebContents. It also keeps track of the pinned state of the tab.
|
| +class TabUsageRecorder::WebContentsData
|
| + : public content::WebContentsUserData<WebContentsData> {
|
| + public:
|
| + static void CreateForWebContents(content::WebContents* contents,
|
| + bool is_pinned);
|
| +
|
| + ~WebContentsData() override;
|
| +
|
| + void OnTabActivating();
|
| + void OnTabDeactivating();
|
| + void OnTabClosing();
|
| + void OnTabPinnedStateChanging(bool is_pinned);
|
| +
|
| + private:
|
| + friend class content::WebContentsUserData<WebContentsData>;
|
| +
|
| + explicit WebContentsData(content::WebContents* contents, bool is_pinned);
|
| +
|
| + // Indicates if the tab is pinned to the tab strip.
|
| + bool is_pinned_;
|
| +
|
| + // Indicates if the tab has been deactivated before as to only count
|
| + // reactivations.
|
| + bool was_deactivated_once_;
|
| +
|
| + // The deactivation metric is not recorded for closing tabs.
|
| + bool is_closing_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WebContentsData);
|
| +};
|
| +
|
| +// static
|
| +void TabUsageRecorder::WebContentsData::CreateForWebContents(
|
| + content::WebContents* contents,
|
| + bool is_pinned) {
|
| + DCHECK(contents);
|
| + if (FromWebContents(contents))
|
| + return;
|
| +
|
| + contents->SetUserData(UserDataKey(),
|
| + new WebContentsData(contents, is_pinned));
|
| +}
|
| +
|
| +TabUsageRecorder::WebContentsData::~WebContentsData() = default;
|
| +
|
| +void TabUsageRecorder::WebContentsData::OnTabActivating() {
|
| + if (was_deactivated_once_) {
|
| + RecordTabReactivation(is_pinned_);
|
| + }
|
| +}
|
| +
|
| +void TabUsageRecorder::WebContentsData::OnTabDeactivating() {
|
| + was_deactivated_once_ = true;
|
| + if (!is_closing_)
|
| + RecordTabDeactivation(is_pinned_);
|
| +}
|
| +
|
| +void TabUsageRecorder::WebContentsData::OnTabClosing() {
|
| + is_closing_ = true;
|
| +}
|
| +
|
| +void TabUsageRecorder::WebContentsData::OnTabPinnedStateChanging(
|
| + bool is_pinned) {
|
| + is_pinned_ = is_pinned;
|
| +}
|
| +
|
| +TabUsageRecorder::WebContentsData::WebContentsData(
|
| + content::WebContents* contents,
|
| + bool is_pinned)
|
| + : is_pinned_(is_pinned), was_deactivated_once_(false), is_closing_(false) {}
|
| +
|
| +// static
|
| +void TabUsageRecorder::Initialize() {
|
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
| + DCHECK(!g_tab_usage_recorder);
|
| + g_tab_usage_recorder = new TabUsageRecorder();
|
| +}
|
| +
|
| +void TabUsageRecorder::TabInsertedAt(TabStripModel* tab_strip_model,
|
| + content::WebContents* contents,
|
| + int index,
|
| + bool foreground) {
|
| + WebContentsData::CreateForWebContents(contents,
|
| + tab_strip_model->IsTabPinned(index));
|
| +}
|
| +
|
| +void TabUsageRecorder::TabClosingAt(TabStripModel* tab_strip_model,
|
| + content::WebContents* contents,
|
| + int index) {
|
| + WebContentsData::FromWebContents(contents)->OnTabClosing();
|
| +}
|
| +
|
| +void TabUsageRecorder::ActiveTabChanged(content::WebContents* old_contents,
|
| + content::WebContents* new_contents,
|
| + int index,
|
| + int reason) {
|
| + if (old_contents)
|
| + WebContentsData::FromWebContents(old_contents)->OnTabDeactivating();
|
| + WebContentsData::FromWebContents(new_contents)->OnTabActivating();
|
| +}
|
| +
|
| +void TabUsageRecorder::TabPinnedStateChanged(TabStripModel* tab_strip_model,
|
| + content::WebContents* contents,
|
| + int index) {
|
| + WebContentsData::FromWebContents(contents)->OnTabPinnedStateChanging(
|
| + tab_strip_model->IsTabPinned(index));
|
| +}
|
| +
|
| +TabUsageRecorder::TabUsageRecorder()
|
| + : browser_tab_strip_tracker_(this, nullptr, nullptr) {
|
| + browser_tab_strip_tracker_.Init(
|
| + BrowserTabStripTracker::InitWith::ALL_BROWERS);
|
| +}
|
| +
|
| +TabUsageRecorder::~TabUsageRecorder() = default;
|
| +
|
| +} // namespace metrics
|
|
|