Index: chrome/browser/tab_restore_service.h |
=================================================================== |
--- chrome/browser/tab_restore_service.h (revision 5928) |
+++ chrome/browser/tab_restore_service.h (working copy) |
@@ -2,8 +2,8 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#ifndef CHROME_BROWSER_TAB_RESTORE_SERVICE_H__ |
-#define CHROME_BROWSER_TAB_RESTORE_SERVICE_H__ |
+#ifndef CHROME_BROWSER_TAB_RESTORE_SERVICE_H_ |
+#define CHROME_BROWSER_TAB_RESTORE_SERVICE_H_ |
#include <list> |
@@ -15,27 +15,23 @@ |
class Profile; |
// TabRestoreService is responsible for maintaining the most recently closed |
-// tabs. When the user closes a tab TabRestoreService::CreateHistoricalTab is |
-// invoked and a HistoricalTab is created to represent the tab. |
+// tabs and windows. When a tab is closed |
+// TabRestoreService::CreateHistoricalTab is invoked and a Tab is created to |
+// represent the tab. Similarly, when a browser is closed, BrowserClosing is |
+// invoked and a Window is created to represent the window. |
// |
-// TabRestoreService can recreate tabs from the previous session as well. |
-// LoadPreviousSessionTabs loads (asynchronously) the tabs from the previous |
-// session. When done, the observer is notified. |
+// To restore a tab/window from the TabRestoreService invoke RestoreEntryById |
+// or RestoreMostRecentEntry. |
// |
-// To restore a tab from the TabRestoreService invoke AddRestoredTab on the |
-// Browser you want to restore the tab to, followed by RemoveHistoricalTabById |
-// to remove the tab from the restore service. |
-// |
-// To listen for changes to the set of tabs managed by the TabRestoreService |
+// To listen for changes to the set of entries managed by the TabRestoreService |
// add an observer. |
- |
class TabRestoreService { |
public: |
- // Observer is notified when the set of tabs managed by TabRestoreService |
+ // Observer is notified when the set of entries managed by TabRestoreService |
// changes in some way. |
class Observer { |
public: |
- // Sent when the internal tab state changed |
+ // Sent when the set of entries changes in some way. |
virtual void TabRestoreServiceChanged(TabRestoreService* service) = 0; |
// Sent to all remaining Observers when TabRestoreService's |
@@ -43,32 +39,51 @@ |
virtual void TabRestoreServiceDestroyed(TabRestoreService* service) = 0; |
}; |
- // Represents a previously open tab. |
- struct HistoricalTab { |
- HistoricalTab(); |
+ // The type of entry. |
+ enum Type { |
+ TAB, |
+ WINDOW |
+ }; |
- // Time the tab was closed. |
- base::Time close_time; |
+ struct Entry { |
+ Entry(); |
+ explicit Entry(Type type); |
+ virtual ~Entry() {} |
- // If true, this is a historical session and not a closed tab. |
- bool from_last_session; |
+ // Unique id for this entry. The id is guaranteed to be unique for a |
+ // session. |
+ int id; |
+ // The type of the entry. |
+ Type type; |
+ }; |
+ |
+ // Represents a previously open tab. |
+ struct Tab : public Entry { |
+ Tab() : Entry(TAB), current_navigation_index(-1) {} |
+ |
// The navigations. |
// WARNING: navigations may be empty. |
std::vector<TabNavigation> navigations; |
// Index of the selected navigation in navigations. |
int current_navigation_index; |
+ }; |
- // Unique id for the closed tab. This is guaranteed to be unique for |
- // a session. |
- const int id; |
+ // Represents a previously open window. |
+ struct Window : public Entry { |
+ Window() : Entry(WINDOW), selected_tab_index(-1) {} |
+ |
+ // The tabs that comprised the window, in order. |
+ std::vector<Tab> tabs; |
+ |
+ // Index of the selected tab. |
+ int selected_tab_index; |
}; |
- typedef std::list<HistoricalTab> Tabs; |
+ typedef std::list<Entry*> Entries; |
- // Creates a new TabRestoreService. This does not load tabs from the last |
- // session, you must explicitly invoke LoadPreviousSessionTabs to do that. |
+ // Creates a new TabRestoreService. |
explicit TabRestoreService(Profile* profile); |
~TabRestoreService(); |
@@ -77,70 +92,70 @@ |
void AddObserver(Observer* observer); |
void RemoveObserver(Observer* observer); |
- // If the previous session has not been loaded, it is loaded and the tabs |
- // from it are placed at the end of the queue. |
- void LoadPreviousSessionTabs(); |
+ // Creates a Tab to represent |tab| and notifies observers the list of |
+ // entries has changed. |
+ void CreateHistoricalTab(NavigationController* tab); |
- // Returns true if loading the previous sessions tabs. |
- bool IsLoadingPreviousSessionTabs(); |
+ // Invoked when a browser is closing. If |browser| is a tabbed browser with |
+ // at least one tab, a Window is created, added to entries and observers are |
+ // notified. |
+ void BrowserClosing(Browser* browser); |
- // Returns true if LoadPreviousSessionTabs will attempt to load any tabs. |
- bool WillLoadPreviousSessionTabs(); |
+ // Invoked when the browser is done closing. |
+ void BrowserClosed(Browser* browser); |
- // Creates a HistoricalTab to represent the tab and notifies observers the |
- // list of tabs has changed. |
- void CreateHistoricalTab(NavigationController* tab); |
+ // Removes all entries from the list and notifies observers the list |
+ // of tabs has changed. |
+ void ClearEntries(); |
- // Removes the HistoricalTab with the specified id and notifies observers. |
- // Does nothing if id does not identify a valid historical tab id. |
- void RemoveHistoricalTabById(int id); |
+ // Returns the entries, ordered with most recently closed entries at the |
+ // front. |
+ const Entries& entries() const { return entries_; } |
- // Removes all HistoricalTabs from the list and notifies observers the list |
- // of tabs has changed. |
- void ClearHistoricalTabs(); |
+ // Restores the most recently closed entry. Does nothing if there are no |
+ // entries to restore. If the most recently restored entry is a tab, it is |
+ // added to |browser|. |
+ void RestoreMostRecentEntry(Browser* browser); |
- // Returns the tabs, ordered with most recently closed tabs at the front. |
- const Tabs& tabs() const { return tabs_; } |
+ // Restores an entry by id. If there is no entry with an id matching |id|, |
+ // this does nothing. If |replace_existing_tab| is true and id identifies a |
+ // tab, the newly created tab replaces the selected tab in |browser|. |
+ void RestoreEntryById(Browser* browser, int id, bool replace_existing_tab); |
private: |
- // Callback from loading the last session. As necessary adds the tabs to |
- // tabs_. |
- void OnGotLastSession(SessionService::Handle handle, |
- std::vector<SessionWindow*>* windows); |
- |
- // Invoked from OnGotLastSession to add the necessary tabs from windows |
- // to tabs_. |
- void AddHistoricalTabs(std::vector<SessionWindow*>* windows); |
- |
- // Creates a HistoricalTab from the tab. |
- void AppendHistoricalTabFromSessionTab(SessionTab* tab); |
- |
// Populates tabs->navigations from the NavigationController. |
void PopulateTabFromController(NavigationController* controller, |
- HistoricalTab* tab); |
+ Tab* tab); |
- // Populates tab->navigations from a previous sessions navigations. |
- void PopulateTabFromSessionTab(SessionTab* session_tab, |
- HistoricalTab* tab); |
- |
// Notifies observers the tabs have changed. |
void NotifyTabsChanged(); |
+ // Prunes entries_ to contain only kMaxEntries and invokes NotifyTabsChanged. |
+ void PruneAndNotify(); |
+ |
+ // Returns an iterator into entries_ whose id matches |id|. |
+ Entries::iterator GetEntryIteratorById(int id); |
+ |
Profile* profile_; |
// Whether we've loaded the last session. |
bool loaded_last_session_; |
- // Set of tabs. We own the NavigationControllers in this list. |
- Tabs tabs_; |
+ // Set of entries. |
+ Entries entries_; |
- // Used in requesting the last session. |
- CancelableRequestConsumer cancelable_consumer_; |
+ // Are we restoring a tab? If this is true we ignore requests to create a |
+ // historical tab. |
+ bool restoring_; |
ObserverList<Observer> observer_list_; |
- DISALLOW_EVIL_CONSTRUCTORS(TabRestoreService); |
+ // Set of tabs that we've received a BrowserClosing method for but no |
+ // corresponding BrowserClosed. We cache the set of browsers closing to |
+ // avoid creating historical tabs for them. |
+ std::set<Browser*> closing_browsers_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TabRestoreService); |
}; |
-#endif // CHROME_BROWSER_TAB_RESTORE_SERVICE_H__ |
- |
+#endif // CHROME_BROWSER_TAB_RESTORE_SERVICE_H_ |