OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_TAB_RESTORE_SERVICE_H__ | 5 #ifndef CHROME_BROWSER_TAB_RESTORE_SERVICE_H_ |
6 #define CHROME_BROWSER_TAB_RESTORE_SERVICE_H__ | 6 #define CHROME_BROWSER_TAB_RESTORE_SERVICE_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "chrome/browser/session_service.h" | 12 #include "chrome/browser/session_service.h" |
13 | 13 |
14 class NavigationController; | 14 class NavigationController; |
15 class Profile; | 15 class Profile; |
16 | 16 |
17 // TabRestoreService is responsible for maintaining the most recently closed | 17 // TabRestoreService is responsible for maintaining the most recently closed |
18 // tabs. When the user closes a tab TabRestoreService::CreateHistoricalTab is | 18 // tabs and windows. When a tab is closed |
19 // invoked and a HistoricalTab is created to represent the tab. | 19 // TabRestoreService::CreateHistoricalTab is invoked and a Tab is created to |
| 20 // represent the tab. Similarly, when a browser is closed, BrowserClosing is |
| 21 // invoked and a Window is created to represent the window. |
20 // | 22 // |
21 // TabRestoreService can recreate tabs from the previous session as well. | 23 // To restore a tab/window from the TabRestoreService invoke RestoreEntryById |
22 // LoadPreviousSessionTabs loads (asynchronously) the tabs from the previous | 24 // or RestoreMostRecentEntry. |
23 // session. When done, the observer is notified. | |
24 // | 25 // |
25 // To restore a tab from the TabRestoreService invoke AddRestoredTab on the | 26 // To listen for changes to the set of entries managed by the TabRestoreService |
26 // Browser you want to restore the tab to, followed by RemoveHistoricalTabById | |
27 // to remove the tab from the restore service. | |
28 // | |
29 // To listen for changes to the set of tabs managed by the TabRestoreService | |
30 // add an observer. | 27 // add an observer. |
31 | |
32 class TabRestoreService { | 28 class TabRestoreService { |
33 public: | 29 public: |
34 // Observer is notified when the set of tabs managed by TabRestoreService | 30 // Observer is notified when the set of entries managed by TabRestoreService |
35 // changes in some way. | 31 // changes in some way. |
36 class Observer { | 32 class Observer { |
37 public: | 33 public: |
38 // Sent when the internal tab state changed | 34 // Sent when the set of entries changes in some way. |
39 virtual void TabRestoreServiceChanged(TabRestoreService* service) = 0; | 35 virtual void TabRestoreServiceChanged(TabRestoreService* service) = 0; |
40 | 36 |
41 // Sent to all remaining Observers when TabRestoreService's | 37 // Sent to all remaining Observers when TabRestoreService's |
42 // destructor is run. | 38 // destructor is run. |
43 virtual void TabRestoreServiceDestroyed(TabRestoreService* service) = 0; | 39 virtual void TabRestoreServiceDestroyed(TabRestoreService* service) = 0; |
44 }; | 40 }; |
45 | 41 |
| 42 // The type of entry. |
| 43 enum Type { |
| 44 TAB, |
| 45 WINDOW |
| 46 }; |
| 47 |
| 48 struct Entry { |
| 49 Entry(); |
| 50 explicit Entry(Type type); |
| 51 virtual ~Entry() {} |
| 52 |
| 53 // Unique id for this entry. The id is guaranteed to be unique for a |
| 54 // session. |
| 55 int id; |
| 56 |
| 57 // The type of the entry. |
| 58 Type type; |
| 59 }; |
| 60 |
46 // Represents a previously open tab. | 61 // Represents a previously open tab. |
47 struct HistoricalTab { | 62 struct Tab : public Entry { |
48 HistoricalTab(); | 63 Tab() : Entry(TAB), current_navigation_index(-1) {} |
49 | |
50 // Time the tab was closed. | |
51 base::Time close_time; | |
52 | |
53 // If true, this is a historical session and not a closed tab. | |
54 bool from_last_session; | |
55 | 64 |
56 // The navigations. | 65 // The navigations. |
57 // WARNING: navigations may be empty. | 66 // WARNING: navigations may be empty. |
58 std::vector<TabNavigation> navigations; | 67 std::vector<TabNavigation> navigations; |
59 | 68 |
60 // Index of the selected navigation in navigations. | 69 // Index of the selected navigation in navigations. |
61 int current_navigation_index; | 70 int current_navigation_index; |
62 | |
63 // Unique id for the closed tab. This is guaranteed to be unique for | |
64 // a session. | |
65 const int id; | |
66 }; | 71 }; |
67 | 72 |
68 typedef std::list<HistoricalTab> Tabs; | 73 // Represents a previously open window. |
| 74 struct Window : public Entry { |
| 75 Window() : Entry(WINDOW), selected_tab_index(-1) {} |
69 | 76 |
70 // Creates a new TabRestoreService. This does not load tabs from the last | 77 // The tabs that comprised the window, in order. |
71 // session, you must explicitly invoke LoadPreviousSessionTabs to do that. | 78 std::vector<Tab> tabs; |
| 79 |
| 80 // Index of the selected tab. |
| 81 int selected_tab_index; |
| 82 }; |
| 83 |
| 84 typedef std::list<Entry*> Entries; |
| 85 |
| 86 // Creates a new TabRestoreService. |
72 explicit TabRestoreService(Profile* profile); | 87 explicit TabRestoreService(Profile* profile); |
73 ~TabRestoreService(); | 88 ~TabRestoreService(); |
74 | 89 |
75 // Adds/removes an observer. TabRestoreService does not take ownership of | 90 // Adds/removes an observer. TabRestoreService does not take ownership of |
76 // the observer. | 91 // the observer. |
77 void AddObserver(Observer* observer); | 92 void AddObserver(Observer* observer); |
78 void RemoveObserver(Observer* observer); | 93 void RemoveObserver(Observer* observer); |
79 | 94 |
80 // If the previous session has not been loaded, it is loaded and the tabs | 95 // Creates a Tab to represent |tab| and notifies observers the list of |
81 // from it are placed at the end of the queue. | 96 // entries has changed. |
82 void LoadPreviousSessionTabs(); | |
83 | |
84 // Returns true if loading the previous sessions tabs. | |
85 bool IsLoadingPreviousSessionTabs(); | |
86 | |
87 // Returns true if LoadPreviousSessionTabs will attempt to load any tabs. | |
88 bool WillLoadPreviousSessionTabs(); | |
89 | |
90 // Creates a HistoricalTab to represent the tab and notifies observers the | |
91 // list of tabs has changed. | |
92 void CreateHistoricalTab(NavigationController* tab); | 97 void CreateHistoricalTab(NavigationController* tab); |
93 | 98 |
94 // Removes the HistoricalTab with the specified id and notifies observers. | 99 // Invoked when a browser is closing. If |browser| is a tabbed browser with |
95 // Does nothing if id does not identify a valid historical tab id. | 100 // at least one tab, a Window is created, added to entries and observers are |
96 void RemoveHistoricalTabById(int id); | 101 // notified. |
| 102 void BrowserClosing(Browser* browser); |
97 | 103 |
98 // Removes all HistoricalTabs from the list and notifies observers the list | 104 // Invoked when the browser is done closing. |
| 105 void BrowserClosed(Browser* browser); |
| 106 |
| 107 // Removes all entries from the list and notifies observers the list |
99 // of tabs has changed. | 108 // of tabs has changed. |
100 void ClearHistoricalTabs(); | 109 void ClearEntries(); |
101 | 110 |
102 // Returns the tabs, ordered with most recently closed tabs at the front. | 111 // Returns the entries, ordered with most recently closed entries at the |
103 const Tabs& tabs() const { return tabs_; } | 112 // front. |
| 113 const Entries& entries() const { return entries_; } |
| 114 |
| 115 // Restores the most recently closed entry. Does nothing if there are no |
| 116 // entries to restore. If the most recently restored entry is a tab, it is |
| 117 // added to |browser|. |
| 118 void RestoreMostRecentEntry(Browser* browser); |
| 119 |
| 120 // Restores an entry by id. If there is no entry with an id matching |id|, |
| 121 // this does nothing. If |replace_existing_tab| is true and id identifies a |
| 122 // tab, the newly created tab replaces the selected tab in |browser|. |
| 123 void RestoreEntryById(Browser* browser, int id, bool replace_existing_tab); |
104 | 124 |
105 private: | 125 private: |
106 // Callback from loading the last session. As necessary adds the tabs to | |
107 // tabs_. | |
108 void OnGotLastSession(SessionService::Handle handle, | |
109 std::vector<SessionWindow*>* windows); | |
110 | |
111 // Invoked from OnGotLastSession to add the necessary tabs from windows | |
112 // to tabs_. | |
113 void AddHistoricalTabs(std::vector<SessionWindow*>* windows); | |
114 | |
115 // Creates a HistoricalTab from the tab. | |
116 void AppendHistoricalTabFromSessionTab(SessionTab* tab); | |
117 | |
118 // Populates tabs->navigations from the NavigationController. | 126 // Populates tabs->navigations from the NavigationController. |
119 void PopulateTabFromController(NavigationController* controller, | 127 void PopulateTabFromController(NavigationController* controller, |
120 HistoricalTab* tab); | 128 Tab* tab); |
121 | |
122 // Populates tab->navigations from a previous sessions navigations. | |
123 void PopulateTabFromSessionTab(SessionTab* session_tab, | |
124 HistoricalTab* tab); | |
125 | 129 |
126 // Notifies observers the tabs have changed. | 130 // Notifies observers the tabs have changed. |
127 void NotifyTabsChanged(); | 131 void NotifyTabsChanged(); |
128 | 132 |
| 133 // Prunes entries_ to contain only kMaxEntries and invokes NotifyTabsChanged. |
| 134 void PruneAndNotify(); |
| 135 |
| 136 // Returns an iterator into entries_ whose id matches |id|. |
| 137 Entries::iterator GetEntryIteratorById(int id); |
| 138 |
129 Profile* profile_; | 139 Profile* profile_; |
130 | 140 |
131 // Whether we've loaded the last session. | 141 // Whether we've loaded the last session. |
132 bool loaded_last_session_; | 142 bool loaded_last_session_; |
133 | 143 |
134 // Set of tabs. We own the NavigationControllers in this list. | 144 // Set of entries. |
135 Tabs tabs_; | 145 Entries entries_; |
136 | 146 |
137 // Used in requesting the last session. | 147 // Are we restoring a tab? If this is true we ignore requests to create a |
138 CancelableRequestConsumer cancelable_consumer_; | 148 // historical tab. |
| 149 bool restoring_; |
139 | 150 |
140 ObserverList<Observer> observer_list_; | 151 ObserverList<Observer> observer_list_; |
141 | 152 |
142 DISALLOW_EVIL_CONSTRUCTORS(TabRestoreService); | 153 // Set of tabs that we've received a BrowserClosing method for but no |
| 154 // corresponding BrowserClosed. We cache the set of browsers closing to |
| 155 // avoid creating historical tabs for them. |
| 156 std::set<Browser*> closing_browsers_; |
| 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(TabRestoreService); |
143 }; | 159 }; |
144 | 160 |
145 #endif // CHROME_BROWSER_TAB_RESTORE_SERVICE_H__ | 161 #endif // CHROME_BROWSER_TAB_RESTORE_SERVICE_H_ |
146 | |
OLD | NEW |