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_TAB_RESTORE_SERVICE_H_
6 #define CHROME_BROWSER_SESSIONS_TAB_RESTORE_SERVICE_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/sessions/session_types.h"
15 #include "chrome/browser/ui/host_desktop.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/sessions/serialized_navigation_entry.h"
18 #include "components/sessions/session_id.h"
19 #include "content/public/browser/session_storage_namespace.h"
20 #include "ui/base/window_open_disposition.h"
22 class TabRestoreServiceDelegate
;
23 class TabRestoreServiceObserver
;
26 class SessionStorageNamespace
;
30 // TabRestoreService is responsible for maintaining the most recently closed
31 // tabs and windows. When a tab is closed
32 // TabRestoreService::CreateHistoricalTab is invoked and a Tab is created to
33 // represent the tab. Similarly, when a browser is closed, BrowserClosing is
34 // invoked and a Window is created to represent the window.
36 // To restore a tab/window from the TabRestoreService invoke RestoreEntryById
37 // or RestoreMostRecentEntry.
39 // To listen for changes to the set of entries managed by the TabRestoreService
41 class TabRestoreService
: public KeyedService
{
43 // Interface used to allow the test to provide a custom time.
46 virtual ~TimeFactory();
47 virtual base::Time
TimeNow() = 0;
58 explicit Entry(Type type
);
61 // Unique id for this entry. The id is guaranteed to be unique for a
63 SessionID::id_type id
;
65 // The type of the entry.
68 // The time when the window or tab was closed.
71 // Is this entry from the last session? This is set to true for entries that
72 // were closed during the last session, and false for entries that were
73 // closed during this session.
74 bool from_last_session
;
77 // Represents a previously open tab.
78 struct Tab
: public Entry
{
82 bool has_browser() const { return browser_id
> 0; }
85 std::vector
<sessions::SerializedNavigationEntry
> navigations
;
87 // Index of the selected navigation in navigations.
88 int current_navigation_index
;
90 // The ID of the browser to which this tab belonged, so it can be restored
91 // there. May be 0 (an invalid SessionID) when restoring an entire session.
92 SessionID::id_type browser_id
;
94 // Index within the tab strip. May be -1 for an unknown index.
97 // True if the tab was pinned.
100 // If non-empty gives the id of the extension for the tab.
101 std::string extension_app_id
;
103 // The associated session storage namespace (if any).
104 scoped_refptr
<content::SessionStorageNamespace
> session_storage_namespace
;
106 // The user agent override used for the tab's navigations (if applicable).
107 std::string user_agent_override
;
110 // Represents a previously open window.
111 struct Window
: public Entry
{
115 // The tabs that comprised the window, in order.
116 std::vector
<Tab
> tabs
;
118 // Index of the selected tab.
119 int selected_tab_index
;
121 // If an application window, the name of the app.
122 std::string app_name
;
125 typedef std::list
<Entry
*> Entries
;
127 virtual ~TabRestoreService();
129 // Adds/removes an observer. TabRestoreService does not take ownership of
131 virtual void AddObserver(TabRestoreServiceObserver
* observer
) = 0;
132 virtual void RemoveObserver(TabRestoreServiceObserver
* observer
) = 0;
134 // Creates a Tab to represent |contents| and notifies observers the list of
135 // entries has changed.
136 virtual void CreateHistoricalTab(content::WebContents
* contents
,
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|. If a new browser needs to be created for this entry,
158 // it will be created on the desktop specified by |host_desktop_type|. Returns
159 // the WebContents of the restored tab(s).
160 virtual std::vector
<content::WebContents
*> RestoreMostRecentEntry(
161 TabRestoreServiceDelegate
* delegate
,
162 chrome::HostDesktopType host_desktop_type
) = 0;
164 // Removes the Tab with id |id| from the list and returns it; ownership is
165 // passed to the caller.
166 virtual Tab
* RemoveTabEntryById(SessionID::id_type id
) = 0;
168 // Restores an entry by id. If there is no entry with an id matching |id|,
169 // this does nothing. If |delegate| is NULL, this creates a new window for the
170 // entry. |disposition| is respected, but the attributes (tabstrip index,
171 // browser window) of the tab when it was closed will be respected if
172 // disposition is UNKNOWN. If a new browser needs to be created for this
173 // entry, it will be created on the desktop specified by |host_desktop_type|.
174 // Returns the WebContents of the restored tab(s).
175 virtual std::vector
<content::WebContents
*> RestoreEntryById(
176 TabRestoreServiceDelegate
* delegate
,
177 SessionID::id_type id
,
178 chrome::HostDesktopType host_desktop_type
,
179 WindowOpenDisposition disposition
) = 0;
181 // Loads the tabs and previous session. This does nothing if the tabs
182 // from the previous session have already been loaded.
183 virtual void LoadTabsFromLastSession() = 0;
185 // Returns true if the tab entries have been loaded.
186 virtual bool IsLoaded() const = 0;
188 // Deletes the last session.
189 virtual void DeleteLastSession() = 0;
192 #endif // CHROME_BROWSER_SESSIONS_TAB_RESTORE_SERVICE_H_