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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SESSIONS_TAB_LOADER_H_
6 #define CHROME_BROWSER_SESSIONS_TAB_LOADER_H_
11 #include "base/memory/memory_pressure_listener.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/timer/timer.h"
14 #include "chrome/browser/sessions/session_restore_delegate.h"
15 #include "chrome/browser/sessions/tab_loader_delegate.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
20 class NavigationController
;
21 class RenderWidgetHost
;
24 // TabLoader is responsible for loading tabs after session restore has finished
25 // creating all the tabs. Tabs are loaded after a previously tab finishes
26 // loading or a timeout is reached. If the timeout is reached before a tab
27 // finishes loading the timeout delay is doubled.
29 // TabLoader keeps a reference to itself when it's loading. When it has finished
30 // loading, it drops the reference. If another profile is restored while the
31 // TabLoader is loading, it will schedule its tabs to get loaded by the same
32 // TabLoader. When doing the scheduling, it holds a reference to the TabLoader.
34 // This is not part of SessionRestoreImpl so that synchronous destruction
35 // of SessionRestoreImpl doesn't have timing problems.
36 class TabLoader
: public content::NotificationObserver
,
37 public base::RefCounted
<TabLoader
>,
38 public TabLoaderCallback
{
40 using RestoredTab
= SessionRestoreDelegate::RestoredTab
;
42 // NotificationObserver method. Removes the specified tab and loads the next
44 void Observe(int type
,
45 const content::NotificationSource
& source
,
46 const content::NotificationDetails
& details
) override
;
49 void SetTabLoadingEnabled(bool enable_tab_loading
) override
;
51 // Called to start restoring tabs.
52 static void RestoreTabs(const std::vector
<RestoredTab
>& tabs_
,
53 const base::TimeTicks
& restore_started
);
56 friend class base::RefCounted
<TabLoader
>;
58 using TabsLoading
= std::set
<content::NavigationController
*>;
59 using TabsToLoad
= std::list
<content::NavigationController
*>;
61 explicit TabLoader(base::TimeTicks restore_started
);
62 ~TabLoader() override
;
64 // This is invoked once by RestoreTabs to start loading.
65 void StartLoading(const std::vector
<RestoredTab
>& tabs
);
67 // Loads the next tab. If there are no more tabs to load this deletes itself,
68 // otherwise |force_load_timer_| is restarted.
71 // Starts a timer to load load the next tab once expired before the current
72 // tab loading is finished.
75 // Removes the listeners from the specified tab and removes the tab from
76 // the set of tabs to load and list of tabs we're waiting to get a load
78 void RemoveTab(content::NavigationController
* controller
);
80 // Invoked from |force_load_timer_|. Doubles |force_load_delay_multiplier_|
81 // and invokes |LoadNextTab| to load the next tab
82 void ForceLoadTimerFired();
84 // Returns the RenderWidgetHost associated with a tab if there is one,
86 static content::RenderWidgetHost
* GetRenderWidgetHost(
87 content::NavigationController
* controller
);
89 // Register for necessary notifications on a tab navigation controller.
90 void RegisterForNotifications(content::NavigationController
* controller
);
92 // Called when a tab goes away or a load completes.
93 void HandleTabClosedOrLoaded(content::NavigationController
* controller
);
95 // React to memory pressure by stopping to load any more tabs.
96 void OnMemoryPressure(
97 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level
);
99 scoped_ptr
<TabLoaderDelegate
> delegate_
;
101 // Listens for system under memory pressure notifications and stops loading
102 // of tabs when we start running out of memory.
103 base::MemoryPressureListener memory_pressure_listener_
;
105 content::NotificationRegistrar registrar_
;
107 // The delay timer multiplier. See class description for details.
108 size_t force_load_delay_multiplier_
;
110 // True if the tab loading is enabled.
111 bool loading_enabled_
;
113 // The set of tabs we've initiated loading on. This does NOT include the
115 TabsLoading tabs_loading_
;
117 // The tabs we need to load.
118 TabsToLoad tabs_to_load_
;
120 base::OneShotTimer
<TabLoader
> force_load_timer_
;
122 // The time the restore process started.
123 base::TimeTicks restore_started_
;
125 // For keeping TabLoader alive while it's loading even if no
126 // SessionRestoreImpls reference it.
127 scoped_refptr
<TabLoader
> this_retainer_
;
129 static TabLoader
* shared_tab_loader_
;
131 DISALLOW_COPY_AND_ASSIGN(TabLoader
);
134 #endif // CHROME_BROWSER_SESSIONS_TAB_LOADER_H_