Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SESSIONS_SESSION_RESTORE_STATS_COLLECTOR_H_ | 5 #ifndef CHROME_BROWSER_SESSIONS_SESSION_RESTORE_STATS_COLLECTOR_H_ |
| 6 #define CHROME_BROWSER_SESSIONS_SESSION_RESTORE_STATS_COLLECTOR_H_ | 6 #define CHROME_BROWSER_SESSIONS_SESSION_RESTORE_STATS_COLLECTOR_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/callback_list.h" | 10 #include "base/callback_list.h" |
| 11 #include "base/time/tick_clock.h" | |
| 11 #include "chrome/browser/sessions/session_restore.h" | 12 #include "chrome/browser/sessions/session_restore.h" |
| 12 #include "chrome/browser/sessions/session_restore_delegate.h" | 13 #include "chrome/browser/sessions/session_restore_delegate.h" |
| 13 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 14 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 15 #include "content/public/browser/render_widget_host.h" | 16 #include "content/public/browser/render_widget_host.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 class NavigationController; | 19 class NavigationController; |
| 19 } | 20 } |
| 20 | 21 |
| 21 // SessionRestoreStatsCollector observes SessionRestore events ands records UMA | 22 // SessionRestoreStatsCollector observes SessionRestore events ands records UMA |
| 22 // accordingly. | 23 // accordingly. |
| 24 // | |
| 25 // A SessionRestoreStatsCollector is tied to an instance of a session restore, | |
| 26 // currently being instantianted and owned by the TabLoader. It has two main | |
| 27 // phases to its life: | |
| 28 // | |
| 29 // 1. The session restore is active and ongoing (the TabLoader is still | |
| 30 // scheduling tabs for loading). This phases ends when there are no | |
| 31 // non-deferred tabs left to be loaded. During this phases statistics are | |
| 32 // gathered in a structure before being emitted as UMA metrics at the end of | |
| 33 // this phase. At this point the TabLoader ceases to exist and destroys it's | |
| 34 // reference to the SessionRestoreStatsCollector. | |
| 35 // 2. If any tabs have been deferred the SessionRestoreStatsCollector continues | |
| 36 // tracking deferred tabs. This continues to observe the tabs to see which | |
| 37 // (if any) of the deferred tabs are subsequently forced to be loaded by the | |
| 38 // user. Since such tabs may exist until the end of the browsers life the | |
| 39 // statistics are emitted immediately, or risk being lost entirely. When | |
| 40 // there are no longer deferred tabs to track the | |
| 41 // SessionRestoreStatsCollector will destroy itself. | |
| 42 // | |
| 43 // TODO(chrisha): Many of these metrics don't make sense to collect in the | |
| 44 // presence of an unavailable network, or when tabs are closed during loading. | |
| 45 // Rethink the collection in these cases. | |
| 23 class SessionRestoreStatsCollector | 46 class SessionRestoreStatsCollector |
| 24 : public content::NotificationObserver, | 47 : public content::NotificationObserver, |
| 25 public base::RefCounted<SessionRestoreStatsCollector> { | 48 public base::RefCounted<SessionRestoreStatsCollector> { |
| 26 public: | 49 public: |
| 27 // Called to start tracking tabs. If a restore is already occuring, the tabs | 50 // Houses all of the statistics gathered by the SessionRestoreStatsCollector |
| 28 // are added to the existing list of tracked tabs. | 51 // while the underlying TabLoader is active. These statistics are all reported |
| 29 static void TrackTabs( | 52 // at once via the reporting delegate. |
| 30 const std::vector<SessionRestoreDelegate::RestoredTab>& tabs, | 53 struct TabLoaderStats { |
| 31 const base::TimeTicks& restore_started); | 54 // Constructor that initializes everything to zero. |
| 32 | 55 TabLoaderStats(); |
| 33 // Called to start tracking only active tabs. If a restore is already | 56 |
| 34 // occuring, the tabs are added to the existing list of tracked tabs. | 57 // The number of tabs involved in all overlapping session restores being |
| 35 static void TrackActiveTabs( | 58 // tracked by this SessionRestoreStatsCollector. This corresponds to the |
| 36 const std::vector<SessionRestoreDelegate::RestoredTab>& tabs, | 59 // "SessionRestore.TabCount" metric and one bucket of the |
| 37 const base::TimeTicks& restore_started); | 60 // "SessionRestore.TabActions" histogram. |
| 61 size_t tab_count; | |
| 62 | |
| 63 // The number of tabs loaded automatically because they are active, and | |
| 64 // explicitly caused to be loaded by the TabLoader. This corresponds to one | |
| 65 // bucket of the "SessionRestore.TabActions" histogram. | |
| 66 size_t tabs_loaded; | |
| 67 | |
| 68 // The time elapsed between |restore_started| and reception of the first | |
| 69 // NOTIFICATION_LOAD_STOP event for any of the active tabs involved in the | |
| 70 // session restore. If this is zero it is because it has not been | |
| 71 // recorded (all visible tabs were closed before they finished loading, or | |
| 72 // the user switched to an already loaded tab before a visible session | |
| 73 // restore tab finished loading). Corresponds to | |
| 74 // "SessionRestore.ForegroundTabFirstLoaded" and its _XX variants. | |
| 75 base::TimeDelta foreground_tab_first_loaded; | |
| 76 | |
| 77 // The time elapsed between |restore_started| and reception of the first | |
| 78 // NOTIFICATION_RENDER_WIDGET_HOST_DID_UPDATE_BACKING_STORE event for any of | |
| 79 // the tabs involved in the session restore. If this is zero it is because | |
| 80 // it has not been recorded (all visible tabs were closed or switched away | |
| 81 // from before they were painted). Corresponds to | |
| 82 // "SessionRestore.ForegroundTabFirstPaint3" and its _XX variants. | |
| 83 base::TimeDelta foreground_tab_first_paint; | |
| 84 | |
| 85 // The time taken for all non-deferred tabs to be loaded. This corresponds | |
| 86 // to the "SessionRestore.AllTabsLoaded" metric and its _XX variants | |
| 87 // (vaguely named for historical reasons, as it predates the concept of | |
| 88 // deferred tabs). | |
| 89 base::TimeDelta non_deferred_tabs_loaded; | |
| 90 | |
| 91 // The maximum number of tabs loading in parallel. This corresponds to the | |
| 92 // "SessionRestore.ParallelTabLoads" metric. | |
| 93 size_t parallel_tab_loads; | |
| 94 }; | |
| 95 | |
| 96 // The StatsReportingDelegate is responsible for delivering statistics | |
| 97 // reported by the SessionRestoreStatsCollector. | |
| 98 class StatsReportingDelegate; | |
| 99 | |
| 100 // An implementation of StatsReportingDelegate for reporting via UMA. | |
| 101 class UmaStatsReportingDelegate; | |
| 102 | |
| 103 // Constructs a SessionRestoreStatsCollector. | |
| 104 SessionRestoreStatsCollector( | |
| 105 const base::TimeTicks& restore_started, | |
| 106 scoped_ptr<StatsReportingDelegate> reporting_delegate); | |
| 107 | |
| 108 // Adds new tabs to the list of tracked tabs. | |
| 109 void TrackTabs(const std::vector<SessionRestoreDelegate::RestoredTab>& tabs); | |
| 110 | |
| 111 // Called to indicate that the loading of a tab has been deferred by session | |
| 112 // restore. | |
| 113 void DeferTab(content::NavigationController* tab); | |
| 114 | |
| 115 // Exposed for unittesting. | |
| 116 const TabLoaderStats& tab_loader_stats() const { return tab_loader_stats_; } | |
| 38 | 117 |
| 39 private: | 118 private: |
| 119 friend class TestSessionRestoreStatsCollector; | |
| 40 friend class base::RefCounted<SessionRestoreStatsCollector>; | 120 friend class base::RefCounted<SessionRestoreStatsCollector>; |
| 41 | 121 |
| 42 using RenderWidgetHostSet = std::set<content::RenderWidgetHost*>; | 122 // Indicates whether the loading state of a tab. |
|
sky
2015/06/17 21:12:04
nit: fix this comment. Maybe you can just nuke it
chrisha
2015/06/18 12:46:23
Done.
| |
| 43 | 123 enum TabLoadingState { TAB_IS_NOT_LOADING, TAB_IS_LOADING, TAB_IS_LOADED }; |
| 44 explicit SessionRestoreStatsCollector(const base::TimeTicks& restore_started); | 124 |
| 45 ~SessionRestoreStatsCollector() override; | 125 // State that is tracked for a tab while it is being observed. |
| 46 | 126 struct TabState { |
| 47 // NotificationObserver method. | 127 explicit TabState(content::NavigationController* controller); |
| 128 | |
| 129 // The NavigationController associated with the tab. This is the primary | |
| 130 // index for it and is never null. | |
| 131 content::NavigationController* controller; | |
| 132 | |
| 133 // The RenderWidgetHost associated with the tab. This is the secondary | |
| 134 // index and starts out being null. If it is not null it is because the tab | |
| 135 // is actively loading or waiting to be painted. | |
| 136 content::RenderWidgetHost* render_widget_host; | |
| 137 | |
| 138 // Set to true if the tab has been deferred by the TabLoader. | |
| 139 bool is_deferred; | |
| 140 | |
| 141 // The current loading state of the tab. | |
| 142 TabLoadingState loading_state; | |
| 143 }; | |
| 144 | |
| 145 // Maps a NavigationController to its state. This is the primary map and | |
| 146 // physically houses the state. | |
| 147 using NavigationControllerMap = | |
| 148 std::map<content::NavigationController*, TabState>; | |
| 149 | |
| 150 virtual ~SessionRestoreStatsCollector(); | |
| 151 | |
| 152 // NotificationObserver method. This is the workhorse of the class and drives | |
| 153 // all state transitions. | |
| 48 void Observe(int type, | 154 void Observe(int type, |
| 49 const content::NotificationSource& source, | 155 const content::NotificationSource& source, |
| 50 const content::NotificationDetails& details) override; | 156 const content::NotificationDetails& details) override; |
| 51 | 157 |
| 52 // Adds new tabs to the list of tracked tabs. | 158 // Called when a tab is no longer tracked. This is called by the 'Observe' |
| 53 void AddTabs(const std::vector<SessionRestoreDelegate::RestoredTab>& tabs); | 159 // notification callback. Takes care of unregistering all observers and |
| 54 | 160 // removing the tab from all internal data structures. |
| 55 // Called when a tab is no longer tracked. | |
| 56 void RemoveTab(content::NavigationController* tab); | 161 void RemoveTab(content::NavigationController* tab); |
| 57 | 162 |
| 58 // Registers for relevant notifications for a tab. | 163 // Registers for relevant notifications for a tab and inserts the tab into |
| 59 void RegisterForNotifications(content::NavigationController* tab); | 164 // to tabs_tracked_ map. Return a pointer to the newly created TabState. |
| 165 TabState* RegisterForNotifications(content::NavigationController* tab); | |
| 60 | 166 |
| 61 // Returns the RenderWidgetHost of a tab. | 167 // Returns the RenderWidgetHost of a tab. |
| 62 content::RenderWidgetHost* GetRenderWidgetHost( | 168 content::RenderWidgetHost* GetRenderWidgetHost( |
| 63 content::NavigationController* tab); | 169 content::NavigationController* tab); |
| 64 | 170 |
| 65 // Have we recorded the times for a foreground tab load? | 171 // Returns the tab state, nullptr if not found. |
| 172 TabState* GetTabState(content::NavigationController* tab); | |
| 173 TabState* GetTabState(content::RenderWidgetHost* tab); | |
| 174 | |
| 175 // Marks a tab as loading. | |
| 176 void MarkTabAsLoading(TabState* tab_state); | |
| 177 | |
| 178 // Checks to see if the SessionRestoreStatsCollector has finished collecting, | |
| 179 // and cleans itself up if it has. | |
| 180 void CleanupIfDoneTracking(); | |
|
sky
2015/06/17 21:12:04
By cleanup isn't this a delete? Maybe DeleteIfDone
chrisha
2015/06/18 12:46:23
Done.
| |
| 181 | |
| 182 // Testing seam for configuring the tick clock in use. | |
| 183 void set_tick_clock(scoped_ptr<base::TickClock> tick_clock) { | |
| 184 tick_clock_ = tick_clock.Pass(); | |
| 185 } | |
| 186 | |
| 187 // Has CleanIfDoneTracking determined that there are no non-deferred tabs to | |
| 188 // track? | |
| 189 bool done_tracking_non_deferred_tabs_; | |
| 190 | |
| 191 // Has the time for foreground tab load been recorded? | |
| 66 bool got_first_foreground_load_; | 192 bool got_first_foreground_load_; |
| 67 | 193 |
| 68 // Have we recorded the times for a foreground tab paint? | 194 // Has the time for foreground tab paint been recorded? |
| 69 bool got_first_paint_; | 195 bool got_first_paint_; |
| 70 | 196 |
| 71 // The time the restore process started. | 197 // The time the restore process started. |
| 72 base::TimeTicks restore_started_; | 198 const base::TimeTicks restore_started_; |
| 73 | 199 |
| 74 // The renderers we have started loading into. | 200 // List of tracked tabs, mapped to their TabState. |
| 75 RenderWidgetHostSet render_widget_hosts_loading_; | 201 NavigationControllerMap tabs_tracked_; |
| 76 | 202 |
| 77 // The renderers we have loaded and are waiting on to paint. | 203 // Counts the number of non-deferred tabs that the |
| 78 RenderWidgetHostSet render_widget_hosts_to_paint_; | 204 // SessionRestoreStatsCollector is waiting to see load. |
| 79 | 205 size_t waiting_for_load_tab_count_; |
| 80 // List of tracked tabs. | 206 |
| 81 std::set<content::NavigationController*> tabs_tracked_; | 207 // Counts the current number of actively loading tabs. |
| 82 | 208 size_t loading_tab_count_; |
| 83 // The number of tabs that have been restored. | |
| 84 int tab_count_; | |
| 85 | |
| 86 // Max number of tabs that were loaded in parallel (for metrics). | |
| 87 size_t max_parallel_tab_loads_; | |
| 88 | 209 |
| 89 // Notification registrar. | 210 // Notification registrar. |
| 90 content::NotificationRegistrar registrar_; | 211 content::NotificationRegistrar registrar_; |
| 91 | 212 |
| 92 // To keep the collector alive as long as needed. | 213 // Statistics gathered regarding the TabLoader. |
| 214 TabLoaderStats tab_loader_stats_; | |
| 215 | |
| 216 // The source of ticks used for taking timing information. This is | |
| 217 // configurable as a testing seam. Defaults to using base::DefaultTickClock, | |
| 218 // which in turn uses base::TimeTicks. | |
| 219 scoped_ptr<base::TickClock> tick_clock_; | |
| 220 | |
| 221 // The reporting delegate used to report gathered statistics. | |
| 222 scoped_ptr<StatsReportingDelegate> reporting_delegate_; | |
| 223 | |
| 224 // For keeping SessionRestoreStatsCollector alive while it is still working | |
| 225 // even if no TabLoader references it. The object only lives on if it still | |
| 226 // has deferred tabs remaining from an interrupted session restore. | |
| 93 scoped_refptr<SessionRestoreStatsCollector> this_retainer_; | 227 scoped_refptr<SessionRestoreStatsCollector> this_retainer_; |
| 94 | 228 |
| 95 // The shared SessionRestoreNotifier instance for all SessionRestores running | |
| 96 // at this time. | |
| 97 static SessionRestoreStatsCollector* shared_collector_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(SessionRestoreStatsCollector); | 229 DISALLOW_COPY_AND_ASSIGN(SessionRestoreStatsCollector); |
| 100 }; | 230 }; |
| 101 | 231 |
| 232 // An abstract reporting delegate is used as a testing seam. | |
| 233 class SessionRestoreStatsCollector::StatsReportingDelegate { | |
| 234 public: | |
| 235 StatsReportingDelegate() {} | |
| 236 virtual ~StatsReportingDelegate() {} | |
| 237 | |
| 238 // Called when TabLoader has completed its work. | |
| 239 virtual void ReportTabLoaderStats(const TabLoaderStats& tab_loader_stats) = 0; | |
| 240 // Called when a tab has been deferred. | |
| 241 virtual void ReportTabDeferred() = 0; | |
| 242 // Called when a deferred tab has been loaded. | |
| 243 virtual void ReportDeferredTabLoaded() = 0; | |
| 244 | |
| 245 private: | |
| 246 DISALLOW_COPY_AND_ASSIGN(StatsReportingDelegate); | |
| 247 }; | |
| 248 | |
| 249 // The default reporting delegate, which reports statistics via UMA. | |
| 250 class SessionRestoreStatsCollector::UmaStatsReportingDelegate | |
| 251 : public StatsReportingDelegate { | |
| 252 public: | |
| 253 UmaStatsReportingDelegate(); | |
| 254 ~UmaStatsReportingDelegate() override {} | |
| 255 | |
| 256 // StatsReportingDelegate implementation. | |
| 257 void ReportTabLoaderStats(const TabLoaderStats& tab_loader_stats) override; | |
| 258 void ReportTabDeferred() override; | |
| 259 void ReportDeferredTabLoaded() override; | |
| 260 | |
| 261 private: | |
| 262 // Has ReportTabDeferred been called? | |
| 263 bool got_report_tab_deferred_; | |
| 264 | |
| 265 DISALLOW_COPY_AND_ASSIGN(UmaStatsReportingDelegate); | |
| 266 }; | |
| 267 | |
| 102 #endif // CHROME_BROWSER_SESSIONS_SESSION_RESTORE_STATS_COLLECTOR_H_ | 268 #endif // CHROME_BROWSER_SESSIONS_SESSION_RESTORE_STATS_COLLECTOR_H_ |
| OLD | NEW |