1 // Copyright 2017 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 #ifndef CHROME_BROWSER_RESOURCE_COORDINATOR_TAB_ACTIVITY_WATCHER_H_ 6 #define CHROME_BROWSER_RESOURCE_COORDINATOR_TAB_ACTIVITY_WATCHER_H_ 7 8 #include <memory> 9 10 #include "base/containers/flat_set.h" 11 #include "base/macros.h" 12 #include "base/optional.h" 13 #include "chrome/browser/resource_coordinator/tab_ranker/tab_score_predictor.h" 14 #include "chrome/browser/ui/browser_list_observer.h" 15 #include "chrome/browser/ui/browser_tab_strip_tracker.h" 16 #include "chrome/browser/ui/browser_tab_strip_tracker_delegate.h" 17 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 18 19 class TabMetricsLogger; 20 21 namespace resource_coordinator { 22 23 class LifecycleUnit; 24 25 // Observes background tab activity in order to log UKMs for tabs and score tabs 26 // using the Tab Ranker. Metrics will be compared against tab reactivation/close 27 // events to determine the end state of each background tab. 28 class TabActivityWatcher : public BrowserListObserver, 29 public TabStripModelObserver, 30 public BrowserTabStripTrackerDelegate { 31 public: 32 TabActivityWatcher(); 33 ~TabActivityWatcher() override; 34 35 // Uses the Tab Ranker model to predict a score for the tab, where a higher 36 // value indicates a higher likelihood of being reactivated. 37 // Returns the score if the tab could be scored. 38 // This is only used in chrome://discards and unit tests. 39 base::Optional<float> CalculateReactivationScore( 40 content::WebContents* web_contents); 41 42 // Logs TabMetrics of all |tabs|; and sorts them by descending importance, 43 // so that the last tab is the first candidate that will be discarded. 44 void LogAndMaybeSortLifecycleUnitWithTabRanker( 45 std::vector<LifecycleUnit*>* tabs); 46 47 // Returns the single instance, creating it if necessary. 48 static TabActivityWatcher* GetInstance(); 49 50 private: 51 friend class TabActivityWatcherTest; 52 53 // Helper class to observe WebContents. 54 // TODO(michaelpg): Merge this into TabLifecycleUnit. 55 class WebContentsData; 56 57 // Called When A Tab is closed, log necessary metrics and erase the 58 // |web_contents_data| pointer in |all_closing_tabs_|. 59 void OnTabClosed(WebContentsData* web_contents_data); 60 61 // BrowserListObserver: 62 void OnBrowserSetLastActive(Browser* browser) override; 63 64 // TabStripModelObserver: 65 void OnTabStripModelChanged( 66 TabStripModel* tab_strip_model, 67 const TabStripModelChange& change, 68 const TabStripSelectionChange& selection) override; 69 void TabPinnedStateChanged(TabStripModel* tab_strip_model, 70 content::WebContents* contents, 71 int index) override; 72 73 // BrowserTabStripTrackerDelegate: 74 bool ShouldTrackBrowser(Browser* browser) override; 75 76 // Resets internal state. 77 void ResetForTesting(); 78 79 std::unique_ptr<TabMetricsLogger> tab_metrics_logger_; 80 81 // Manages registration of this class as an observer of all TabStripModels as 82 // browsers are created and destroyed. 83 BrowserTabStripTracker browser_tab_strip_tracker_; 84 85 // Loads the Tab Ranker model on first use and calculates tab scores. 86 std::unique_ptr<tab_ranker::TabScorePredictor> predictor_; 87 88 DISALLOW_COPY_AND_ASSIGN(TabActivityWatcher); 89 }; 90 91 } // namespace resource_coordinator 92 93 #endif // CHROME_BROWSER_RESOURCE_COORDINATOR_TAB_ACTIVITY_WATCHER_H_ 94