blob: 39278c9ab5d8051b8e99fedd620c42062e6da235 [file] [log] [blame]
// 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 <memory>
#include <unordered_map>
#include "base/macros.h"
#include "chrome/browser/ui/browser_tab_strip_tracker.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
namespace metrics {
// This class is used to track tab deactivation/reactivation cycle. A
// reactivation is defined as a tab that was hidden and then reshown.
// Note: A closing tab will not trigger a tab deactivation. Also worth noting
// that tab discarding disrupts the tracking and the discarded tab will be
// treated as a new tab.
class TabReactivationTracker : public TabStripModelObserver {
class Delegate {
virtual void OnTabDeactivated(content::WebContents* contents) = 0;
virtual void OnTabReactivated(content::WebContents* contents) = 0;
explicit TabReactivationTracker(Delegate* delegate);
~TabReactivationTracker() override;
// TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;
void NotifyTabDeactivating(content::WebContents* contents);
void NotifyTabReactivating(content::WebContents* contents);
class WebContentsHelper;
// Returns the helper for the |contents|, creating it if it doesn't exist.
WebContentsHelper* GetHelper(content::WebContents* contents);
// Called by the helper when |contents| is being destroyed.
void OnWebContentsDestroyed(content::WebContents* contents);
// The delegate must outlive this class.
Delegate* delegate_;
// A helper instance is managed per WebContents.
std::unordered_map<content::WebContents*, std::unique_ptr<WebContentsHelper>>
BrowserTabStripTracker browser_tab_strip_tracker_;
} // namespace metrics