[email protected] | a636d8e5 | 2012-02-28 15:40:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <map> |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 9 | #include <string> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | f9ffb89 | 2011-11-28 18:57:31 | [diff] [blame] | 12 | #include "base/callback.h" |
[email protected] | 70eb8ed9 | 2010-06-10 18:13:12 | [diff] [blame] | 13 | #include "base/time.h" |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 14 | #include "chrome/browser/defaults.h" |
[email protected] | 926bfeb | 2009-02-14 23:19:47 | [diff] [blame] | 15 | #include "chrome/browser/sessions/base_session_service.h" |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 16 | #include "chrome/browser/sessions/session_id.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser.h" |
| 18 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 19 | #include "chrome/browser/ui/browser_list_observer.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 20 | #include "content/public/browser/notification_observer.h" |
| 21 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 22 | #include "ui/base/ui_base_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | class Profile; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | class SessionCommand; |
[email protected] | c36a9e1d | 2012-06-05 14:31:02 | [diff] [blame] | 26 | class TabContents; |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 27 | struct SessionTab; |
| 28 | struct SessionWindow; |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 29 | |
| 30 | namespace content { |
| 31 | class NavigationEntry; |
[email protected] | 4060015 | 2012-09-26 10:04:31 | [diff] [blame] | 32 | class WebContents; |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 33 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
| 35 | // SessionService ------------------------------------------------------------ |
| 36 | |
| 37 | // SessionService is responsible for maintaining the state of open windows |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 38 | // and tabs so that they can be restored at a later date. The state of the |
| 39 | // currently open browsers is referred to as the current session. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | // |
[email protected] | 668cd42 | 2011-04-06 21:00:01 | [diff] [blame] | 41 | // SessionService supports restoring from the last session. The last session |
| 42 | // typically corresponds to the last run of the browser, but not always. For |
| 43 | // example, if the user has a tabbed browser and app window running, closes the |
| 44 | // tabbed browser, then creates a new tabbed browser the current session is made |
| 45 | // the last session and the current session reset. This is done to provide the |
| 46 | // illusion that app windows run in separate processes. Similar behavior occurs |
| 47 | // with incognito windows. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | // |
| 49 | // SessionService itself maintains a set of SessionCommands that allow |
| 50 | // SessionService to rebuild the open state of the browser (as |
| 51 | // SessionWindow, SessionTab and TabNavigation). The commands are periodically |
| 52 | // flushed to SessionBackend and written to a file. Every so often |
| 53 | // SessionService rebuilds the contents of the file from the open state |
| 54 | // of the browser. |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 55 | class SessionService : public BaseSessionService, |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 56 | public content::NotificationObserver, |
| 57 | public chrome::BrowserListObserver { |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 58 | friend class SessionServiceTestHelper; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | public: |
[email protected] | a636d8e5 | 2012-02-28 15:40:41 | [diff] [blame] | 60 | // Used to distinguish an application window from a normal one. |
| 61 | enum AppType { |
| 62 | TYPE_APP, |
| 63 | TYPE_NORMAL |
| 64 | }; |
| 65 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | // Creates a SessionService for the specified profile. |
| 67 | explicit SessionService(Profile* profile); |
| 68 | // For testing. |
[email protected] | 5a82010a | 2009-02-14 01:33:02 | [diff] [blame] | 69 | explicit SessionService(const FilePath& save_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | f0a58a38 | 2011-04-26 16:09:29 | [diff] [blame] | 71 | virtual ~SessionService(); |
| 72 | |
[email protected] | 8ddbb64 | 2011-09-01 02:52:20 | [diff] [blame] | 73 | // Returns true if a new window opening should really be treated like the |
| 74 | // start of a session (with potential session restore, startup URLs, etc.). |
| 75 | // In particular, this is true if there are no tabbed browsers running |
| 76 | // currently (eg. because only background or other app pages are running). |
| 77 | bool ShouldNewWindowStartSession(); |
| 78 | |
[email protected] | c9b1994 | 2010-03-26 15:58:08 | [diff] [blame] | 79 | // Invoke at a point when you think session restore might occur. For example, |
| 80 | // during startup and window creation this is invoked to see if a session |
| 81 | // needs to be restored. If a session needs to be restored it is done so |
| 82 | // asynchronously and true is returned. If false is returned the session was |
| 83 | // not restored and the caller needs to create a new window. |
| 84 | bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open); |
| 85 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | // Resets the contents of the file from the current state of all open |
| 87 | // browsers whose profile matches our profile. |
| 88 | void ResetFromCurrentBrowsers(); |
| 89 | |
| 90 | // Moves the current session to the last session. This is useful when a |
| 91 | // checkpoint occurs, such as when the user launches the app and no tabbed |
| 92 | // browsers are running. |
| 93 | void MoveCurrentSessionToLastSession(); |
| 94 | |
| 95 | // Associates a tab with a window. |
| 96 | void SetTabWindow(const SessionID& window_id, |
| 97 | const SessionID& tab_id); |
| 98 | |
| 99 | // Sets the bounds of a window. |
| 100 | void SetWindowBounds(const SessionID& window_id, |
| 101 | const gfx::Rect& bounds, |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 102 | ui::WindowShowState show_state); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | |
| 104 | // Sets the visual index of the tab in its parent window. |
| 105 | void SetTabIndexInWindow(const SessionID& window_id, |
| 106 | const SessionID& tab_id, |
| 107 | int new_index); |
| 108 | |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 109 | // Sets the pinned state of the tab. |
| 110 | void SetPinnedState(const SessionID& window_id, |
| 111 | const SessionID& tab_id, |
| 112 | bool is_pinned); |
| 113 | |
[email protected] | c0e3ee4 | 2010-05-26 22:11:07 | [diff] [blame] | 114 | // Notification that a tab has been closed. |closed_by_user_gesture| comes |
[email protected] | e6ff1d5 | 2012-04-17 20:00:40 | [diff] [blame] | 115 | // from |WebContents::closed_by_user_gesture|; see it for details. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | // |
| 117 | // Note: this is invoked from the NavigationController's destructor, which is |
| 118 | // after the actual tab has been removed. |
[email protected] | c0e3ee4 | 2010-05-26 22:11:07 | [diff] [blame] | 119 | void TabClosed(const SessionID& window_id, |
| 120 | const SessionID& tab_id, |
| 121 | bool closed_by_user_gesture); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
| 123 | // Notification the window is about to close. |
| 124 | void WindowClosing(const SessionID& window_id); |
| 125 | |
| 126 | // Notification a window has finished closing. |
| 127 | void WindowClosed(const SessionID& window_id); |
| 128 | |
[email protected] | 4060015 | 2012-09-26 10:04:31 | [diff] [blame] | 129 | // Called when a tab is inserted. |
| 130 | void TabInserted(content::WebContents* contents); |
| 131 | |
| 132 | // Called when a tab is closing. |
| 133 | void TabClosing(content::WebContents* contents); |
| 134 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | // Sets the type of window. In order for the contents of a window to be |
| 136 | // tracked SetWindowType must be invoked with a type we track |
| 137 | // (should_track_changes_for_browser_type returns true). |
[email protected] | a636d8e5 | 2012-02-28 15:40:41 | [diff] [blame] | 138 | void SetWindowType(const SessionID& window_id, |
| 139 | Browser::Type type, |
| 140 | AppType app_type); |
| 141 | |
| 142 | // Sets the application name of the specified window. |
| 143 | void SetWindowAppName(const SessionID& window_id, |
| 144 | const std::string& app_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | |
[email protected] | c12bf1a1 | 2008-09-17 16:28:49 | [diff] [blame] | 146 | // Invoked when the NavigationController has removed entries from the back of |
| 147 | // the list. |count| gives the number of entries in the navigation controller. |
| 148 | void TabNavigationPathPrunedFromBack(const SessionID& window_id, |
| 149 | const SessionID& tab_id, |
| 150 | int count); |
| 151 | |
| 152 | // Invoked when the NavigationController has removed entries from the front of |
| 153 | // the list. |count| gives the number of entries that were removed. |
| 154 | void TabNavigationPathPrunedFromFront(const SessionID& window_id, |
| 155 | const SessionID& tab_id, |
| 156 | int count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | |
| 158 | // Updates the navigation entry for the specified tab. |
| 159 | void UpdateTabNavigation(const SessionID& window_id, |
| 160 | const SessionID& tab_id, |
[email protected] | 9dd1d58 | 2012-09-19 04:04:16 | [diff] [blame] | 161 | const TabNavigation& navigation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
| 163 | // Notification that a tab has restored its entries or a closed tab is being |
| 164 | // reused. |
[email protected] | 337a286e | 2012-06-09 00:01:45 | [diff] [blame] | 165 | void TabRestored(TabContents* tab, bool pinned); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | |
| 167 | // Sets the index of the selected entry in the navigation controller for the |
| 168 | // specified tab. |
| 169 | void SetSelectedNavigationIndex(const SessionID& window_id, |
| 170 | const SessionID& tab_id, |
| 171 | int index); |
| 172 | |
| 173 | // Sets the index of the selected tab in the specified window. |
| 174 | void SetSelectedTabInWindow(const SessionID& window_id, int index); |
| 175 | |
[email protected] | 8d0f331 | 2012-08-18 01:47:53 | [diff] [blame] | 176 | // Sets the user agent override of the specified tab. |
| 177 | void SetTabUserAgentOverride(const SessionID& window_id, |
| 178 | const SessionID& tab_id, |
| 179 | const std::string& user_agent_override); |
| 180 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | // Callback from GetSavedSession of GetLastSession. |
| 182 | // |
| 183 | // The contents of the supplied vector are deleted after the callback is |
| 184 | // notified. To take ownership of the vector clear it before returning. |
| 185 | // |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 186 | // The session ID is the id of the window that was last active. |
| 187 | typedef base::Callback<void(Handle, |
| 188 | std::vector<SessionWindow*>*, |
| 189 | SessionID::id_type)> SessionCallback; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | |
| 191 | // Fetches the contents of the last session, notifying the callback when |
| 192 | // done. If the callback is supplied an empty vector of SessionWindows |
| 193 | // it means the session could not be restored. |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 194 | // |
| 195 | // The created request does NOT directly invoke the callback, rather the |
| 196 | // callback invokes OnGotSessionCommands from which we map the |
| 197 | // SessionCommands to browser state, then notify the callback. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 198 | Handle GetLastSession(CancelableRequestConsumerBase* consumer, |
[email protected] | f9ffb89 | 2011-11-28 18:57:31 | [diff] [blame] | 199 | const SessionCallback& callback); |
[email protected] | 3acb70ef | 2010-03-01 18:44:38 | [diff] [blame] | 200 | |
[email protected] | 70eb8ed9 | 2010-06-10 18:13:12 | [diff] [blame] | 201 | // Overridden from BaseSessionService because we want some UMA reporting on |
| 202 | // session update activities. |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 203 | virtual void Save() OVERRIDE; |
[email protected] | 70eb8ed9 | 2010-06-10 18:13:12 | [diff] [blame] | 204 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | private: |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 206 | // Allow tests to access our innards for testing purposes. |
| 207 | FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation1); |
| 208 | FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation2); |
| 209 | |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 210 | typedef std::map<SessionID::id_type, std::pair<int, int> > IdToRange; |
| 211 | typedef std::map<SessionID::id_type, SessionTab*> IdToSessionTab; |
| 212 | typedef std::map<SessionID::id_type, SessionWindow*> IdToSessionWindow; |
| 213 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | |
[email protected] | 97d2c1e | 2009-11-05 03:55:05 | [diff] [blame] | 215 | // These types mirror Browser::Type, but are re-defined here because these |
| 216 | // specific enumeration _values_ are written into the session database and |
| 217 | // are needed to maintain forward compatibility. |
[email protected] | b35b26b3 | 2011-05-05 20:35:14 | [diff] [blame] | 218 | // Note that we only store browsers of type TYPE_TABBED and TYPE_POPUP. |
[email protected] | 97d2c1e | 2009-11-05 03:55:05 | [diff] [blame] | 219 | enum WindowType { |
[email protected] | b35b26b3 | 2011-05-05 20:35:14 | [diff] [blame] | 220 | TYPE_TABBED = 0, |
| 221 | TYPE_POPUP = 1 |
[email protected] | 97d2c1e | 2009-11-05 03:55:05 | [diff] [blame] | 222 | }; |
| 223 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 224 | void Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | |
[email protected] | c9b1994 | 2010-03-26 15:58:08 | [diff] [blame] | 226 | // Implementation of RestoreIfNecessary. If |browser| is non-null and we need |
| 227 | // to restore, the tabs are added to it, otherwise a new browser is created. |
| 228 | bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open, |
| 229 | Browser* browser); |
| 230 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 231 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 232 | const content::NotificationSource& source, |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 233 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 234 | |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 235 | // chrome::BrowserListObserver |
| 236 | virtual void OnBrowserAdded(Browser* browser) OVERRIDE {} |
| 237 | virtual void OnBrowserRemoved(Browser* browser) OVERRIDE {} |
| 238 | virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE; |
| 239 | |
[email protected] | fca656c | 2010-02-10 20:30:10 | [diff] [blame] | 240 | // Sets the application extension id of the specified tab. |
[email protected] | 98aa0b5 | 2010-05-06 17:03:08 | [diff] [blame] | 241 | void SetTabExtensionAppID(const SessionID& window_id, |
[email protected] | fca656c | 2010-02-10 20:30:10 | [diff] [blame] | 242 | const SessionID& tab_id, |
[email protected] | 98aa0b5 | 2010-05-06 17:03:08 | [diff] [blame] | 243 | const std::string& extension_app_id); |
[email protected] | fca656c | 2010-02-10 20:30:10 | [diff] [blame] | 244 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | // Methods to create the various commands. It is up to the caller to delete |
| 246 | // the returned the SessionCommand* object. |
| 247 | SessionCommand* CreateSetSelectedTabInWindow(const SessionID& window_id, |
| 248 | int index); |
| 249 | |
| 250 | SessionCommand* CreateSetTabWindowCommand(const SessionID& window_id, |
| 251 | const SessionID& tab_id); |
| 252 | |
| 253 | SessionCommand* CreateSetWindowBoundsCommand(const SessionID& window_id, |
| 254 | const gfx::Rect& bounds, |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 255 | ui::WindowShowState show_state); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | |
| 257 | SessionCommand* CreateSetTabIndexInWindowCommand(const SessionID& tab_id, |
| 258 | int new_index); |
| 259 | |
| 260 | SessionCommand* CreateTabClosedCommand(SessionID::id_type tab_id); |
| 261 | |
| 262 | SessionCommand* CreateWindowClosedCommand(SessionID::id_type tab_id); |
| 263 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | SessionCommand* CreateSetSelectedNavigationIndexCommand( |
| 265 | const SessionID& tab_id, |
| 266 | int index); |
| 267 | |
| 268 | SessionCommand* CreateSetWindowTypeCommand(const SessionID& window_id, |
[email protected] | 97d2c1e | 2009-11-05 03:55:05 | [diff] [blame] | 269 | WindowType type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 271 | SessionCommand* CreatePinnedStateCommand(const SessionID& tab_id, |
| 272 | bool is_pinned); |
| 273 | |
[email protected] | 9cc8fd9 | 2012-06-22 21:20:55 | [diff] [blame] | 274 | SessionCommand* CreateSessionStorageAssociatedCommand( |
| 275 | const SessionID& tab_id, |
| 276 | const std::string& session_storage_persistent_id); |
| 277 | |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 278 | SessionCommand* CreateSetActiveWindowCommand(const SessionID& window_id); |
| 279 | |
[email protected] | 668cd42 | 2011-04-06 21:00:01 | [diff] [blame] | 280 | // Callback from the backend for getting the commands from the save file. |
| 281 | // Converts the commands in SessionWindows and notifies the real callback. |
[email protected] | 3acb70ef | 2010-03-01 18:44:38 | [diff] [blame] | 282 | void OnGotSessionCommands( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | Handle handle, |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 284 | scoped_refptr<InternalGetCommandsRequest> request); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | |
| 286 | // Converts the commands into SessionWindows. On return any valid |
| 287 | // windows are added to valid_windows. It is up to the caller to delete |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 288 | // the windows added to valid_windows. |active_window_id| will be set with the |
| 289 | // id of the last active window, but it's only valid when this id corresponds |
| 290 | // to the id of one of the windows in valid_windows. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 291 | void RestoreSessionFromCommands(const std::vector<SessionCommand*>& commands, |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 292 | std::vector<SessionWindow*>* valid_windows, |
| 293 | SessionID::id_type* active_window_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | |
| 295 | // Iterates through the vector updating the selected_tab_index of each |
| 296 | // SessionWindow based on the actual tabs that were restored. |
| 297 | void UpdateSelectedTabIndex(std::vector<SessionWindow*>* windows); |
| 298 | |
| 299 | // Returns the window in windows with the specified id. If a window does |
| 300 | // not exist, one is created. |
| 301 | SessionWindow* GetWindow(SessionID::id_type window_id, |
| 302 | IdToSessionWindow* windows); |
| 303 | |
| 304 | // Returns the tab with the specified id in tabs. If a tab does not exist, |
| 305 | // it is created. |
| 306 | SessionTab* GetTab(SessionID::id_type tab_id, |
| 307 | IdToSessionTab* tabs); |
| 308 | |
| 309 | // Returns an iterator into navigations pointing to the navigation whose |
| 310 | // index matches |index|. If no navigation index matches |index|, the first |
| 311 | // navigation with an index > |index| is returned. |
| 312 | // |
| 313 | // This assumes the navigations are ordered by index in ascending order. |
| 314 | std::vector<TabNavigation>::iterator FindClosestNavigationWithIndex( |
| 315 | std::vector<TabNavigation>* navigations, |
| 316 | int index); |
| 317 | |
| 318 | // Does the following: |
| 319 | // . Deletes and removes any windows with no tabs or windows with types other |
| 320 | // than tabbed_browser or browser. NOTE: constrained windows that have |
| 321 | // been dragged out are of type browser. As such, this preserves any dragged |
| 322 | // out constrained windows (aka popups that have been dragged out). |
| 323 | // . Sorts the tabs in windows with valid tabs based on the tabs |
| 324 | // visual order, and adds the valid windows to windows. |
| 325 | void SortTabsBasedOnVisualOrderAndPrune( |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 326 | std::map<int, SessionWindow*>* windows, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | std::vector<SessionWindow*>* valid_windows); |
| 328 | |
| 329 | // Adds tabs to their parent window based on the tab's window_id. This |
| 330 | // ignores tabs with no navigations. |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 331 | void AddTabsToWindows(std::map<int, SessionTab*>* tabs, |
| 332 | std::map<int, SessionWindow*>* windows); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 334 | // Creates tabs and windows from the commands specified in |data|. The created |
| 335 | // tabs and windows are added to |tabs| and |windows| respectively, with the |
| 336 | // id of the active window set in |active_window_id|. It is up to the caller |
| 337 | // to delete the tabs and windows added to |tabs| and |windows|. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | // |
| 339 | // This does NOT add any created SessionTabs to SessionWindow.tabs, that is |
| 340 | // done by AddTabsToWindows. |
| 341 | bool CreateTabsAndWindows(const std::vector<SessionCommand*>& data, |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 342 | std::map<int, SessionTab*>* tabs, |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 343 | std::map<int, SessionWindow*>* windows, |
| 344 | SessionID::id_type* active_window_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | |
| 346 | // Adds commands to commands that will recreate the state of the specified |
[email protected] | 8189899 | 2011-06-14 22:15:00 | [diff] [blame] | 347 | // tab. This adds at most kMaxNavigationCountToPersist navigations (in each |
| 348 | // direction from the current navigation index). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | // A pair is added to tab_to_available_range indicating the range of |
| 350 | // indices that were written. |
| 351 | void BuildCommandsForTab( |
| 352 | const SessionID& window_id, |
[email protected] | 337a286e | 2012-06-09 00:01:45 | [diff] [blame] | 353 | TabContents* tab, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | int index_in_window, |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 355 | bool is_pinned, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | std::vector<SessionCommand*>* commands, |
| 357 | IdToRange* tab_to_available_range); |
| 358 | |
| 359 | // Adds commands to create the specified browser, and invokes |
| 360 | // BuildCommandsForTab for each of the tabs in the browser. This ignores |
| 361 | // any tabs not in the profile we were created with. |
| 362 | void BuildCommandsForBrowser( |
| 363 | Browser* browser, |
| 364 | std::vector<SessionCommand*>* commands, |
| 365 | IdToRange* tab_to_available_range, |
| 366 | std::set<SessionID::id_type>* windows_to_track); |
| 367 | |
| 368 | // Iterates over all the known browsers invoking BuildCommandsForBrowser. |
| 369 | // This only adds browsers that should be tracked |
| 370 | // (should_track_changes_for_browser_type returns true). All browsers that |
| 371 | // are tracked are added to windows_to_track (as long as it is non-null). |
| 372 | void BuildCommandsFromBrowsers( |
| 373 | std::vector<SessionCommand*>* commands, |
| 374 | IdToRange* tab_to_available_range, |
| 375 | std::set<SessionID::id_type>* windows_to_track); |
| 376 | |
| 377 | // Schedules a reset. A reset means the contents of the file are recreated |
| 378 | // from the state of the browser. |
| 379 | void ScheduleReset(); |
| 380 | |
| 381 | // Searches for a pending command that can be replaced with command. |
| 382 | // If one is found, pending command is removed, command is added to |
| 383 | // the pending commands and true is returned. |
| 384 | bool ReplacePendingCommand(SessionCommand* command); |
| 385 | |
| 386 | // Schedules the specified command. This method takes ownership of the |
| 387 | // command. |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 388 | virtual void ScheduleCommand(SessionCommand* command) OVERRIDE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | |
| 390 | // Converts all pending tab/window closes to commands and schedules them. |
| 391 | void CommitPendingCloses(); |
| 392 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 393 | // Returns true if there is only one window open with a single tab that shares |
| 394 | // our profile. |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 395 | bool IsOnlyOneTabLeft() const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 397 | // Returns true if there are open trackable browser windows whose ids do |
| 398 | // match |window_id| with our profile. A trackable window is a window from |
| 399 | // which |should_track_changes_for_browser_type| returns true. See |
| 400 | // |should_track_changes_for_browser_type| for details. |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 401 | bool HasOpenTrackableBrowsers(const SessionID& window_id) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | |
| 403 | // Returns true if changes to tabs in the specified window should be tracked. |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame^] | 404 | bool ShouldTrackChangesToWindow(const SessionID& window_id) const; |
| 405 | |
| 406 | // Returns true if we track changes to the specified browser. |
| 407 | bool ShouldTrackBrowser(Browser* browser) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | |
| 409 | // Returns true if we track changes to the specified browser type. |
[email protected] | a636d8e5 | 2012-02-28 15:40:41 | [diff] [blame] | 410 | static bool should_track_changes_for_browser_type( |
| 411 | Browser::Type type, |
| 412 | AppType app_type); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
[email protected] | 4070a6b | 2009-11-05 23:33:55 | [diff] [blame] | 414 | // Returns true if we should record a window close as pending. |
| 415 | // |has_open_trackable_browsers_| must be up-to-date before calling this. |
| 416 | bool should_record_close_as_pending() const { |
| 417 | // When this is called, the browser window being closed is still open, hence |
| 418 | // still in the browser list. If there is a browser window other than the |
| 419 | // one being closed but no trackable windows, then the others must be App |
| 420 | // windows or similar. In this case, we record the close as pending. |
| 421 | return !has_open_trackable_browsers_ && |
| 422 | (!browser_defaults::kBrowserAliveWithNoWindows || |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 423 | force_browser_not_alive_with_no_windows_ || |
[email protected] | 4070a6b | 2009-11-05 23:33:55 | [diff] [blame] | 424 | BrowserList::size() > 1); |
[email protected] | 70eb8ed9 | 2010-06-10 18:13:12 | [diff] [blame] | 425 | } |
| 426 | |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 427 | // Call when certain session relevant notifications |
| 428 | // (tab_closed, nav_list_pruned) occur. In addition, this is |
| 429 | // currently called when Save() is called to compare how often the |
| 430 | // session data is currently saved verses when we may want to save it. |
| 431 | // It records the data in UMA stats. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 432 | void RecordSessionUpdateHistogramData(int type, |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 433 | base::TimeTicks* last_updated_time); |
| 434 | |
| 435 | // Helper methods to record the histogram data |
| 436 | void RecordUpdatedTabClosed(base::TimeDelta delta, bool use_long_period); |
| 437 | void RecordUpdatedNavListPruned(base::TimeDelta delta, bool use_long_period); |
| 438 | void RecordUpdatedNavEntryCommit(base::TimeDelta delta, bool use_long_period); |
| 439 | void RecordUpdatedSaveTime(base::TimeDelta delta, bool use_long_period); |
| 440 | void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta, |
| 441 | bool use_long_period); |
[email protected] | 4070a6b | 2009-11-05 23:33:55 | [diff] [blame] | 442 | |
[email protected] | 97d2c1e | 2009-11-05 03:55:05 | [diff] [blame] | 443 | // Convert back/forward between the Browser and SessionService DB window |
| 444 | // types. |
| 445 | static WindowType WindowTypeForBrowserType(Browser::Type type); |
| 446 | static Browser::Type BrowserTypeForWindowType(WindowType type); |
| 447 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 448 | content::NotificationRegistrar registrar_; |
[email protected] | 6a02963e | 2009-01-06 16:58:03 | [diff] [blame] | 449 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | // Maps from session tab id to the range of navigation entries that has |
| 451 | // been written to disk. |
| 452 | // |
| 453 | // This is only used if not all the navigation entries have been |
| 454 | // written. |
| 455 | IdToRange tab_to_available_range_; |
| 456 | |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 457 | // When the user closes the last window, where the last window is the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | // last tabbed browser and no more tabbed browsers are open with the same |
| 459 | // profile, the window ID is added here. These IDs are only committed (which |
| 460 | // marks them as closed) if the user creates a new tabbed browser. |
| 461 | typedef std::set<SessionID::id_type> PendingWindowCloseIDs; |
| 462 | PendingWindowCloseIDs pending_window_close_ids_; |
| 463 | |
| 464 | // Set of tabs that have been closed by way of the last window or last tab |
| 465 | // closing, but not yet committed. |
| 466 | typedef std::set<SessionID::id_type> PendingTabCloseIDs; |
| 467 | PendingTabCloseIDs pending_tab_close_ids_; |
| 468 | |
| 469 | // When a window other than the last window (see description of |
| 470 | // pending_window_close_ids) is closed, the id is added to this set. |
| 471 | typedef std::set<SessionID::id_type> WindowClosingIDs; |
| 472 | WindowClosingIDs window_closing_ids_; |
| 473 | |
| 474 | // Set of windows we're tracking changes to. This is only browsers that |
| 475 | // return true from should_track_changes_for_browser_type. |
| 476 | typedef std::set<SessionID::id_type> WindowsTracking; |
| 477 | WindowsTracking windows_tracking_; |
| 478 | |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 479 | // Are there any open trackable browsers? |
| 480 | bool has_open_trackable_browsers_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 481 | |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 482 | // If true and a new tabbed browser is created and there are no opened tabbed |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 483 | // browser (has_open_trackable_browsers_ is false), then the current session |
[email protected] | 668cd42 | 2011-04-06 21:00:01 | [diff] [blame] | 484 | // is made the last session. See description above class for details on |
| 485 | // current/last session. |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 486 | bool move_on_new_browser_; |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 487 | |
[email protected] | 47db9a9 | 2011-01-24 20:10:13 | [diff] [blame] | 488 | // Used for reporting frequency of session altering operations. |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 489 | base::TimeTicks last_updated_tab_closed_time_; |
| 490 | base::TimeTicks last_updated_nav_list_pruned_time_; |
| 491 | base::TimeTicks last_updated_nav_entry_commit_time_; |
| 492 | base::TimeTicks last_updated_save_time_; |
| 493 | |
| 494 | // Constants used in calculating histogram data. |
| 495 | const base::TimeDelta save_delay_in_millis_; |
| 496 | const base::TimeDelta save_delay_in_mins_; |
| 497 | const base::TimeDelta save_delay_in_hrs_; |
[email protected] | 70eb8ed9 | 2010-06-10 18:13:12 | [diff] [blame] | 498 | |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 499 | // For browser_tests, since we want to simulate the browser shutting down |
| 500 | // without quitting. |
| 501 | bool force_browser_not_alive_with_no_windows_; |
| 502 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 503 | DISALLOW_COPY_AND_ASSIGN(SessionService); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | }; |
| 505 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 506 | #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |