1 // Copyright (c) 2012 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_SESSION_RESTORE_H_
6 #define CHROME_BROWSER_SESSIONS_SESSION_RESTORE_H_
10 #include "base/basictypes.h"
11 #include "base/callback_list.h"
12 #include "chrome/browser/history/history_service.h"
13 #include "chrome/browser/ui/host_desktop.h"
14 #include "components/sessions/session_types.h"
15 #include "ui/base/window_open_disposition.h"
24 // SessionRestore handles restoring either the last or saved session. Session
25 // restore come in two variants, asynchronous or synchronous. The synchronous
26 // variety is meant for startup and blocks until restore is complete.
27 class SessionRestore
{
30 // Indicates the active tab of the supplied browser should be closed.
31 CLOBBER_CURRENT_TAB
= 1 << 0,
33 // Indicates that if there is a problem restoring the last session then a
34 // new tabbed browser should be created.
35 ALWAYS_CREATE_TABBED_BROWSER
= 1 << 1,
37 // Restore blocks until complete. This is intended for use during startup
38 // when we want to block until restore is complete.
42 // Notification callback list.
43 using CallbackList
= base::CallbackList
<void(int)>;
45 // Used by objects calling RegisterOnSessionRestoredCallback() to de-register
46 // themselves when they are destroyed.
47 using CallbackSubscription
=
48 scoped_ptr
<base::CallbackList
<void(int)>::Subscription
>;
50 // Restores the last session. |behavior| is a bitmask of Behaviors, see it
51 // for details. If |browser| is non-null the tabs for the first window are
52 // added to it. Returns the last active browser.
53 // Every additional browser created will be created on the desktop specified
54 // by |host_desktop_type|, if |browser| is non-null it should have the same
57 // If |urls_to_open| is non-empty, a tab is added for each of the URLs.
58 static Browser
* RestoreSession(Profile
* profile
,
60 chrome::HostDesktopType host_desktop_type
,
62 const std::vector
<GURL
>& urls_to_open
);
64 // Restores the last session when the last session crashed. It's a wrapper
65 // of function RestoreSession.
66 static void RestoreSessionAfterCrash(Browser
* browser
);
68 // Specifically used in the restoration of a foreign session. This function
69 // restores the given session windows to multiple browsers all of which
70 // will be created on the desktop specified by |host_desktop_type|. Returns
71 // the created Browsers.
72 static std::vector
<Browser
*> RestoreForeignSessionWindows(
74 chrome::HostDesktopType host_desktop_type
,
75 std::vector
<const sessions::SessionWindow
*>::const_iterator begin
,
76 std::vector
<const sessions::SessionWindow
*>::const_iterator end
);
78 // Specifically used in the restoration of a foreign session. This method
79 // restores the given session tab to the browser of |source_web_contents| if
80 // the disposition is not NEW_WINDOW. Returns the WebContents corresponding
81 // to the restored tab. If |disposition| is CURRENT_TAB, |source_web_contents|
83 static content::WebContents
* RestoreForeignSessionTab(
84 content::WebContents
* source_web_contents
,
85 const sessions::SessionTab
& tab
,
86 WindowOpenDisposition disposition
);
88 // Returns true if we're in the process of restoring |profile|.
89 static bool IsRestoring(const Profile
* profile
);
91 // Returns true if synchronously restoring a session.
92 static bool IsRestoringSynchronously();
94 // Register callbacks for session restore events. These callbacks are stored
95 // in |on_session_restored_callbacks_|.
96 // The callback is supplied an integer arg representing a tab count. The exact
97 // meaning and timing depend upon the restore type:
98 // - SessionRestore::SYNCHRONOUS: the parameter is the number of tabs that
99 // were created. Additionally the callback is invoked immediately after the
100 // tabs have been created. That is, the tabs are not necessarily loading.
101 // - For all other restore types the parameter is the number of tabs that were
102 // restored and is sent after all tabs have started loading. Additionally if a
103 // request to restore tabs comes in while a previous request to restore tabs
104 // has not yet completed (loading tabs is throttled), then the callback is
105 // only notified once both sets of tabs have started loading and with the
106 // total number of tabs for both restores.
107 static CallbackSubscription
RegisterOnSessionRestoredCallback(
108 const base::Callback
<void(int)>& callback
);
110 // The max number of non-selected tabs SessionRestore loads when restoring
111 // a session. A value of 0 indicates all tabs are loaded at once.
112 static size_t num_tabs_to_load_
;
117 // Accessor for |*on_session_restored_callbacks_|. Creates a new object the
118 // first time so that it always returns a valid object.
119 static CallbackList
* on_session_restored_callbacks() {
120 if (!on_session_restored_callbacks_
)
121 on_session_restored_callbacks_
= new CallbackList();
122 return on_session_restored_callbacks_
;
125 // Contains all registered callbacks for session restore notifications.
126 static CallbackList
* on_session_restored_callbacks_
;
128 DISALLOW_COPY_AND_ASSIGN(SessionRestore
);
131 #endif // CHROME_BROWSER_SESSIONS_SESSION_RESTORE_H_