1 // Copyright 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 COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_H_
6 #define COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "components/sessions/core/tab_restore_service_client.h"
16 #include "components/sessions/serialized_navigation_entry.h"
17 #include "components/sessions/session_id.h"
18 #include "components/sessions/session_types.h"
19 #include "components/sessions/sessions_export.h"
20 #include "ui/base/window_open_disposition.h"
25 class TabRestoreServiceDelegate
;
26 class TabRestoreServiceObserver
;
28 // TabRestoreService is responsible for maintaining the most recently closed
29 // tabs and windows. When a tab is closed
30 // TabRestoreService::CreateHistoricalTab is invoked and a Tab is created to
31 // represent the tab. Similarly, when a browser is closed, BrowserClosing is
32 // invoked and a Window is created to represent the window.
34 // To restore a tab/window from the TabRestoreService invoke RestoreEntryById
35 // or RestoreMostRecentEntry.
37 // To listen for changes to the set of entries managed by the TabRestoreService
39 class SESSIONS_EXPORT TabRestoreService
: public KeyedService
{
41 // Interface used to allow the test to provide a custom time.
42 class SESSIONS_EXPORT TimeFactory
{
44 virtual ~TimeFactory();
45 virtual base::Time
TimeNow() = 0;
54 struct SESSIONS_EXPORT Entry
{
56 explicit Entry(Type type
);
59 // Unique id for this entry. The id is guaranteed to be unique for a
61 SessionID::id_type id
;
63 // The type of the entry.
66 // The time when the window or tab was closed.
69 // Is this entry from the last session? This is set to true for entries that
70 // were closed during the last session, and false for entries that were
71 // closed during this session.
72 bool from_last_session
;
75 // Represents a previously open tab.
76 struct SESSIONS_EXPORT Tab
: public Entry
{
81 Tab
& operator=(const Tab
& tab
);
83 bool has_browser() const { return browser_id
> 0; }
86 std::vector
<SerializedNavigationEntry
> navigations
;
88 // Index of the selected navigation in navigations.
89 int current_navigation_index
;
91 // The ID of the browser to which this tab belonged, so it can be restored
92 // there. May be 0 (an invalid SessionID) when restoring an entire session.
93 SessionID::id_type browser_id
;
95 // Index within the tab strip. May be -1 for an unknown index.
98 // True if the tab was pinned.
101 // If non-empty gives the id of the extension for the tab.
102 std::string extension_app_id
;
104 // The associated client data.
105 scoped_ptr
<TabClientData
> client_data
;
107 // The user agent override used for the tab's navigations (if applicable).
108 std::string user_agent_override
;
111 // Represents a previously open window.
112 struct SESSIONS_EXPORT Window
: public Entry
{
116 // The tabs that comprised the window, in order.
117 std::vector
<Tab
> tabs
;
119 // Index of the selected tab.
120 int selected_tab_index
;
122 // If an application window, the name of the app.
123 std::string app_name
;
126 typedef std::list
<Entry
*> Entries
;
128 ~TabRestoreService() override
;
130 // Adds/removes an observer. TabRestoreService does not take ownership of
132 virtual void AddObserver(TabRestoreServiceObserver
* observer
) = 0;
133 virtual void RemoveObserver(TabRestoreServiceObserver
* observer
) = 0;
135 // Creates a Tab to represent |live_tab| and notifies observers the list of
136 // entries has changed.
137 virtual void CreateHistoricalTab(LiveTab
* live_tab
, int index
) = 0;
139 // Invoked when a browser is closing. If |delegate| is a tabbed browser with
140 // at least one tab, a Window is created, added to entries and observers are
142 virtual void BrowserClosing(TabRestoreServiceDelegate
* delegate
) = 0;
144 // Invoked when the browser is done closing.
145 virtual void BrowserClosed(TabRestoreServiceDelegate
* delegate
) = 0;
147 // Removes all entries from the list and notifies observers the list
148 // of tabs has changed.
149 virtual void ClearEntries() = 0;
151 // Returns the entries, ordered with most recently closed entries at the
153 virtual const Entries
& entries() const = 0;
155 // Restores the most recently closed entry. Does nothing if there are no
156 // entries to restore. If the most recently restored entry is a tab, it is
157 // added to |delegate|. |host_desktop_type| is a value that is opaque to this
158 // class and will be used only to pass back to the embedder via
159 // TabRestoreServiceClient if necessary. Returns the LiveTab instances of the
161 virtual std::vector
<LiveTab
*> RestoreMostRecentEntry(
162 TabRestoreServiceDelegate
* delegate
,
163 int host_desktop_type
) = 0;
165 // Removes the Tab with id |id| from the list and returns it; ownership is
166 // passed to the caller.
167 virtual Tab
* RemoveTabEntryById(SessionID::id_type id
) = 0;
169 // Restores an entry by id. If there is no entry with an id matching |id|,
170 // this does nothing. If |delegate| is NULL, this creates a new window for the
171 // entry. |disposition| is respected, but the attributes (tabstrip index,
172 // browser window) of the tab when it was closed will be respected if
173 // disposition is UNKNOWN. |host_desktop_type| is a value that is opaque to
174 // this class and will be used only to pass back to the embedder via
175 // TabRestoreServiceClient if necessary. Returns the LiveTab instances of the
177 virtual std::vector
<LiveTab
*> RestoreEntryById(
178 TabRestoreServiceDelegate
* delegate
,
179 SessionID::id_type id
,
180 int host_desktop_type
,
181 WindowOpenDisposition disposition
) = 0;
183 // Loads the tabs and previous session. This does nothing if the tabs
184 // from the previous session have already been loaded.
185 virtual void LoadTabsFromLastSession() = 0;
187 // Returns true if the tab entries have been loaded.
188 virtual bool IsLoaded() const = 0;
190 // Deletes the last session.
191 virtual void DeleteLastSession() = 0;
194 } // namespace sessions
196 #endif // COMPONENTS_SESSIONS_CORE_TAB_RESTORE_SERVICE_H_