| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_SESSIONS_SESSION_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ | 6 #define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/task/cancelable_task_tracker.h" | 15 #include "base/task/cancelable_task_tracker.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/defaults.h" | 17 #include "chrome/browser/defaults.h" |
| 18 #include "chrome/browser/sessions/base_session_service.h" | 18 #include "chrome/browser/sessions/base_session_service_delegate_impl.h" |
| 19 #include "chrome/browser/sessions/session_service_commands.h" |
| 19 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_finder.h" | 21 #include "chrome/browser/ui/browser_finder.h" |
| 21 #include "chrome/browser/ui/browser_list_observer.h" | 22 #include "chrome/browser/ui/browser_list_observer.h" |
| 22 #include "components/keyed_service/core/keyed_service.h" | 23 #include "components/keyed_service/core/keyed_service.h" |
| 23 #include "components/sessions/session_id.h" | |
| 24 #include "content/public/browser/notification_observer.h" | 24 #include "content/public/browser/notification_observer.h" |
| 25 #include "content/public/browser/notification_registrar.h" | 25 #include "content/public/browser/notification_registrar.h" |
| 26 #include "ui/base/ui_base_types.h" | 26 #include "ui/base/ui_base_types.h" |
| 27 | 27 |
| 28 class Profile; | 28 class Profile; |
| 29 class SessionCommand; | 29 class SessionCommand; |
| 30 struct SessionTab; | 30 struct SessionTab; |
| 31 struct SessionWindow; | 31 struct SessionWindow; |
| 32 | 32 |
| 33 namespace content { | 33 namespace content { |
| 34 class NavigationEntry; | 34 class NavigationEntry; |
| 35 class WebContents; | 35 class WebContents; |
| 36 } | 36 } |
| 37 | 37 |
| 38 // SessionService ------------------------------------------------------------ | 38 // SessionService ------------------------------------------------------------ |
| 39 | 39 |
| 40 // SessionService is responsible for maintaining the state of open windows | 40 // SessionService is responsible for maintaining the state of open windows |
| 41 // and tabs so that they can be restored at a later date. The state of the | 41 // and tabs so that they can be restored at a later date. The state of the |
| 42 // currently open browsers is referred to as the current session. | 42 // currently open browsers is referred to as the current session. |
| 43 // | 43 // |
| 44 // SessionService supports restoring from the last session. The last session | 44 // SessionService supports restoring from the last session. The last session |
| 45 // typically corresponds to the last run of the browser, but not always. For | 45 // typically corresponds to the last run of the browser, but not always. For |
| 46 // example, if the user has a tabbed browser and app window running, closes the | 46 // example, if the user has a tabbed browser and app window running, closes the |
| 47 // tabbed browser, then creates a new tabbed browser the current session is made | 47 // tabbed browser, then creates a new tabbed browser the current session is made |
| 48 // the last session and the current session reset. This is done to provide the | 48 // the last session and the current session reset. This is done to provide the |
| 49 // illusion that app windows run in separate processes. Similar behavior occurs | 49 // illusion that app windows run in separate processes. Similar behavior occurs |
| 50 // with incognito windows. | 50 // with incognito windows. |
| 51 // | 51 // |
| 52 // SessionService itself maintains a set of SessionCommands that allow | 52 // SessionService itself uses |SessionServiceCommands| to store commands which |
| 53 // SessionService to rebuild the open state of the browser (as SessionWindow, | 53 // can rebuild the open state of the browser (as |SessionWindow|, |SessionTab| |
| 54 // SessionTab and SerializedNavigationEntry). The commands are periodically | 54 // and |SerializedNavigationEntry|). The commands are periodically flushed to |
| 55 // flushed to SessionBackend and written to a file. Every so often | 55 // |SessionBackend| and written to a file. Every so often |SessionService| |
| 56 // SessionService rebuilds the contents of the file from the open state of the | 56 // rebuilds the contents of the file from the open state of the browser. |
| 57 // browser. | 57 class SessionService : public BaseSessionServiceDelegateImpl, |
| 58 class SessionService : public BaseSessionService, | 58 public SessionServiceCommandsDelegate, |
| 59 public KeyedService, | 59 public KeyedService, |
| 60 public content::NotificationObserver, | 60 public content::NotificationObserver, |
| 61 public chrome::BrowserListObserver { | 61 public chrome::BrowserListObserver { |
| 62 friend class SessionServiceTestHelper; | 62 friend class SessionServiceTestHelper; |
| 63 public: | 63 public: |
| 64 // Used to distinguish an application window from a normal one. | |
| 65 enum AppType { | |
| 66 TYPE_APP, | |
| 67 TYPE_NORMAL | |
| 68 }; | |
| 69 | |
| 70 // Creates a SessionService for the specified profile. | 64 // Creates a SessionService for the specified profile. |
| 71 explicit SessionService(Profile* profile); | 65 explicit SessionService(Profile* profile); |
| 72 // For testing. | 66 // For testing. |
| 73 explicit SessionService(const base::FilePath& save_path); | 67 explicit SessionService(const base::FilePath& save_path); |
| 74 | 68 |
| 75 ~SessionService() override; | 69 ~SessionService() override; |
| 76 | 70 |
| 71 // Callback from GetLastSession. |
| 72 // The second parameter is the id of the window that was last active. |
| 73 typedef base::Callback<void(ScopedVector<SessionWindow>, SessionID::id_type)> |
| 74 SessionCallback; |
| 75 |
| 76 // Convert back/forward between the Browser and SessionService DB window |
| 77 // types. |
| 78 static SessionWindow::WindowType WindowTypeForBrowserType(Browser::Type type); |
| 79 static Browser::Type BrowserTypeForWindowType(SessionWindow::WindowType type); |
| 80 |
| 77 // This may be NULL during testing. | 81 // This may be NULL during testing. |
| 78 Profile* profile() const { return profile_; } | 82 Profile* profile() const { return profile_; } |
| 79 | 83 |
| 80 // Returns true if a new window opening should really be treated like the | 84 // Returns true if a new window opening should really be treated like the |
| 81 // start of a session (with potential session restore, startup URLs, etc.). | 85 // start of a session (with potential session restore, startup URLs, etc.). |
| 82 // In particular, this is true if there are no tabbed browsers running | 86 // In particular, this is true if there are no tabbed browsers running |
| 83 // currently (eg. because only background or other app pages are running). | 87 // currently (eg. because only background or other app pages are running). |
| 84 bool ShouldNewWindowStartSession(); | 88 bool ShouldNewWindowStartSession(); |
| 85 | 89 |
| 86 // Invoke at a point when you think session restore might occur. For example, | 90 // Invoke at a point when you think session restore might occur. For example, |
| 87 // during startup and window creation this is invoked to see if a session | 91 // during startup and window creation this is invoked to see if a session |
| 88 // needs to be restored. If a session needs to be restored it is done so | 92 // needs to be restored. If a session needs to be restored it is done so |
| 89 // asynchronously and true is returned. If false is returned the session was | 93 // asynchronously and true is returned. If false is returned the session was |
| 90 // not restored and the caller needs to create a new window. | 94 // not restored and the caller needs to create a new window. |
| 91 bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open); | 95 bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open); |
| 92 | 96 |
| 93 // Resets the contents of the file from the current state of all open | 97 // Resets the contents of the file from the current state of all open |
| 94 // browsers whose profile matches our profile. | 98 // browsers whose profile matches our profile. |
| 95 void ResetFromCurrentBrowsers(); | 99 void ResetFromCurrentBrowsers(); |
| 96 | 100 |
| 97 // Moves the current session to the last session. This is useful when a | 101 // Moves the current session to the last session. This is useful when a |
| 98 // checkpoint occurs, such as when the user launches the app and no tabbed | 102 // checkpoint occurs, such as when the user launches the app and no tabbed |
| 99 // browsers are running. | 103 // browsers are running. |
| 100 void MoveCurrentSessionToLastSession(); | 104 void MoveCurrentSessionToLastSession(); |
| 101 | 105 |
| 106 // Deletes the last session. |
| 107 void DeleteLastSession(); |
| 108 |
| 102 // Associates a tab with a window. | 109 // Associates a tab with a window. |
| 103 void SetTabWindow(const SessionID& window_id, | 110 void SetTabWindow(const SessionID& window_id, |
| 104 const SessionID& tab_id); | 111 const SessionID& tab_id); |
| 105 | 112 |
| 106 // Sets the bounds of a window. | 113 // Sets the bounds of a window. |
| 107 void SetWindowBounds(const SessionID& window_id, | 114 void SetWindowBounds(const SessionID& window_id, |
| 108 const gfx::Rect& bounds, | 115 const gfx::Rect& bounds, |
| 109 ui::WindowShowState show_state); | 116 ui::WindowShowState show_state); |
| 110 | 117 |
| 111 // Sets the visual index of the tab in its parent window. | 118 // Sets the visual index of the tab in its parent window. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 137 void WindowClosed(const SessionID& window_id); | 144 void WindowClosed(const SessionID& window_id); |
| 138 | 145 |
| 139 // Called when a tab is inserted. | 146 // Called when a tab is inserted. |
| 140 void TabInserted(content::WebContents* contents); | 147 void TabInserted(content::WebContents* contents); |
| 141 | 148 |
| 142 // Called when a tab is closing. | 149 // Called when a tab is closing. |
| 143 void TabClosing(content::WebContents* contents); | 150 void TabClosing(content::WebContents* contents); |
| 144 | 151 |
| 145 // Sets the type of window. In order for the contents of a window to be | 152 // Sets the type of window. In order for the contents of a window to be |
| 146 // tracked SetWindowType must be invoked with a type we track | 153 // tracked SetWindowType must be invoked with a type we track |
| 147 // (should_track_changes_for_browser_type returns true). | 154 // (ShouldRestoreOfWindowType returns true). |
| 148 void SetWindowType(const SessionID& window_id, | 155 void SetWindowType(const SessionID& window_id, |
| 149 Browser::Type type, | 156 Browser::Type type, |
| 150 AppType app_type); | 157 SessionServiceCommands::AppType app_type); |
| 151 | 158 |
| 152 // Sets the application name of the specified window. | 159 // Sets the application name of the specified window. |
| 153 void SetWindowAppName(const SessionID& window_id, | 160 void SetWindowAppName(const SessionID& window_id, |
| 154 const std::string& app_name); | 161 const std::string& app_name); |
| 155 | 162 |
| 156 // Invoked when the NavigationController has removed entries from the back of | 163 // Invoked when the NavigationController has removed entries from the back of |
| 157 // the list. |count| gives the number of entries in the navigation controller. | 164 // the list. |count| gives the number of entries in the navigation controller. |
| 158 void TabNavigationPathPrunedFromBack(const SessionID& window_id, | 165 void TabNavigationPathPrunedFromBack(const SessionID& window_id, |
| 159 const SessionID& tab_id, | 166 const SessionID& tab_id, |
| 160 int count); | 167 int count); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 182 int index); | 189 int index); |
| 183 | 190 |
| 184 // Sets the index of the selected tab in the specified window. | 191 // Sets the index of the selected tab in the specified window. |
| 185 void SetSelectedTabInWindow(const SessionID& window_id, int index); | 192 void SetSelectedTabInWindow(const SessionID& window_id, int index); |
| 186 | 193 |
| 187 // Sets the user agent override of the specified tab. | 194 // Sets the user agent override of the specified tab. |
| 188 void SetTabUserAgentOverride(const SessionID& window_id, | 195 void SetTabUserAgentOverride(const SessionID& window_id, |
| 189 const SessionID& tab_id, | 196 const SessionID& tab_id, |
| 190 const std::string& user_agent_override); | 197 const std::string& user_agent_override); |
| 191 | 198 |
| 192 // Callback from GetLastSession. | 199 // Sets the application extension id of the specified tab. |
| 193 // The second parameter is the id of the window that was last active. | 200 void SetTabExtensionAppID(const SessionID& window_id, |
| 194 typedef base::Callback<void(ScopedVector<SessionWindow>, SessionID::id_type)> | 201 const SessionID& tab_id, |
| 195 SessionCallback; | 202 const std::string& extension_app_id); |
| 196 | 203 |
| 197 // Fetches the contents of the last session, notifying the callback when | 204 // Fetches the contents of the last session, notifying the callback when |
| 198 // done. If the callback is supplied an empty vector of SessionWindows | 205 // done. If the callback is supplied an empty vector of SessionWindows |
| 199 // it means the session could not be restored. | 206 // it means the session could not be restored. |
| 200 base::CancelableTaskTracker::TaskId GetLastSession( | 207 base::CancelableTaskTracker::TaskId GetLastSession( |
| 201 const SessionCallback& callback, | 208 const SessionCallback& callback, |
| 202 base::CancelableTaskTracker* tracker); | 209 base::CancelableTaskTracker* tracker); |
| 203 | 210 |
| 204 // Overridden from BaseSessionService because we want some UMA reporting on | 211 // SessionServiceCommandsDelegate: |
| 205 // session update activities. | 212 bool ShouldRestoreWindowOfType( |
| 206 void Save() override; | 213 SessionWindow::WindowType type, |
| 214 SessionServiceCommands::AppType app_type) const override; |
| 215 |
| 216 // BaseSessionServiceDelegateImpl: |
| 217 void OnWillSaveCommands() override; |
| 218 void OnSavedCommands() override; |
| 219 |
| 220 // Unit test accessors. |
| 221 SessionServiceCommands* GetSessionCommandsForTest(); |
| 222 BaseSessionService* GetBaseSessionServiceForTest(); |
| 207 | 223 |
| 208 private: | 224 private: |
| 209 // Allow tests to access our innards for testing purposes. | 225 // Allow tests to access our innards for testing purposes. |
| 210 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation1); | 226 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation1); |
| 211 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation2); | 227 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation2); |
| 212 FRIEND_TEST_ALL_PREFIXES(NoStartupWindowTest, DontInitSessionServiceForApps); | 228 FRIEND_TEST_ALL_PREFIXES(NoStartupWindowTest, DontInitSessionServiceForApps); |
| 213 | 229 |
| 214 typedef std::map<SessionID::id_type, std::pair<int, int> > IdToRange; | 230 typedef std::map<SessionID::id_type, std::pair<int, int> > IdToRange; |
| 215 typedef std::map<SessionID::id_type, SessionTab*> IdToSessionTab; | |
| 216 typedef std::map<SessionID::id_type, SessionWindow*> IdToSessionWindow; | |
| 217 | |
| 218 | |
| 219 // These types mirror Browser::Type, but are re-defined here because these | |
| 220 // specific enumeration _values_ are written into the session database and | |
| 221 // are needed to maintain forward compatibility. | |
| 222 // Note that we only store browsers of type TYPE_TABBED and TYPE_POPUP. | |
| 223 enum WindowType { | |
| 224 TYPE_TABBED = 0, | |
| 225 TYPE_POPUP = 1 | |
| 226 }; | |
| 227 | 231 |
| 228 void Init(); | 232 void Init(); |
| 229 | 233 |
| 230 // Returns true if we have scheduled any commands, or any scheduled commands | |
| 231 // have been saved. | |
| 232 bool processed_any_commands(); | |
| 233 | |
| 234 // Implementation of RestoreIfNecessary. If |browser| is non-null and we need | 234 // Implementation of RestoreIfNecessary. If |browser| is non-null and we need |
| 235 // to restore, the tabs are added to it, otherwise a new browser is created. | 235 // to restore, the tabs are added to it, otherwise a new browser is created. |
| 236 bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open, | 236 bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open, |
| 237 Browser* browser); | 237 Browser* browser); |
| 238 | 238 |
| 239 void Observe(int type, | 239 void Observe(int type, |
| 240 const content::NotificationSource& source, | 240 const content::NotificationSource& source, |
| 241 const content::NotificationDetails& details) override; | 241 const content::NotificationDetails& details) override; |
| 242 | 242 |
| 243 // chrome::BrowserListObserver | 243 // chrome::BrowserListObserver |
| 244 void OnBrowserAdded(Browser* browser) override {} | 244 void OnBrowserAdded(Browser* browser) override {} |
| 245 void OnBrowserRemoved(Browser* browser) override {} | 245 void OnBrowserRemoved(Browser* browser) override {} |
| 246 void OnBrowserSetLastActive(Browser* browser) override; | 246 void OnBrowserSetLastActive(Browser* browser) override; |
| 247 | 247 |
| 248 // Sets the application extension id of the specified tab. | |
| 249 void SetTabExtensionAppID(const SessionID& window_id, | |
| 250 const SessionID& tab_id, | |
| 251 const std::string& extension_app_id); | |
| 252 | |
| 253 // Methods to create the various commands. It is up to the caller to delete | |
| 254 // the returned the SessionCommand* object. | |
| 255 SessionCommand* CreateSetSelectedTabInWindow(const SessionID& window_id, | |
| 256 int index); | |
| 257 | |
| 258 SessionCommand* CreateSetTabWindowCommand(const SessionID& window_id, | |
| 259 const SessionID& tab_id); | |
| 260 | |
| 261 SessionCommand* CreateSetWindowBoundsCommand(const SessionID& window_id, | |
| 262 const gfx::Rect& bounds, | |
| 263 ui::WindowShowState show_state); | |
| 264 | |
| 265 SessionCommand* CreateSetTabIndexInWindowCommand(const SessionID& tab_id, | |
| 266 int new_index); | |
| 267 | |
| 268 SessionCommand* CreateTabClosedCommand(SessionID::id_type tab_id); | |
| 269 | |
| 270 SessionCommand* CreateWindowClosedCommand(SessionID::id_type tab_id); | |
| 271 | |
| 272 SessionCommand* CreateSetSelectedNavigationIndexCommand( | |
| 273 const SessionID& tab_id, | |
| 274 int index); | |
| 275 | |
| 276 SessionCommand* CreateSetWindowTypeCommand(const SessionID& window_id, | |
| 277 WindowType type); | |
| 278 | |
| 279 SessionCommand* CreatePinnedStateCommand(const SessionID& tab_id, | |
| 280 bool is_pinned); | |
| 281 | |
| 282 SessionCommand* CreateSessionStorageAssociatedCommand( | |
| 283 const SessionID& tab_id, | |
| 284 const std::string& session_storage_persistent_id); | |
| 285 | |
| 286 SessionCommand* CreateSetActiveWindowCommand(const SessionID& window_id); | |
| 287 | |
| 288 // Converts |commands| to SessionWindows and notifies the callback. | 248 // Converts |commands| to SessionWindows and notifies the callback. |
| 289 void OnGotSessionCommands(const SessionCallback& callback, | 249 void OnGotSessionCommands(const SessionCallback& callback, |
| 290 ScopedVector<SessionCommand> commands); | 250 ScopedVector<SessionCommand> commands); |
| 291 | 251 |
| 292 // Converts the commands into SessionWindows. On return any valid | |
| 293 // windows are added to valid_windows. It is up to the caller to delete | |
| 294 // the windows added to valid_windows. |active_window_id| will be set with the | |
| 295 // id of the last active window, but it's only valid when this id corresponds | |
| 296 // to the id of one of the windows in valid_windows. | |
| 297 void RestoreSessionFromCommands(const std::vector<SessionCommand*>& commands, | |
| 298 std::vector<SessionWindow*>* valid_windows, | |
| 299 SessionID::id_type* active_window_id); | |
| 300 | |
| 301 // Iterates through the vector updating the selected_tab_index of each | |
| 302 // SessionWindow based on the actual tabs that were restored. | |
| 303 void UpdateSelectedTabIndex(std::vector<SessionWindow*>* windows); | |
| 304 | |
| 305 // Returns the window in windows with the specified id. If a window does | |
| 306 // not exist, one is created. | |
| 307 SessionWindow* GetWindow(SessionID::id_type window_id, | |
| 308 IdToSessionWindow* windows); | |
| 309 | |
| 310 // Returns the tab with the specified id in tabs. If a tab does not exist, | |
| 311 // it is created. | |
| 312 SessionTab* GetTab(SessionID::id_type tab_id, | |
| 313 IdToSessionTab* tabs); | |
| 314 | |
| 315 // Returns an iterator into navigations pointing to the navigation whose | |
| 316 // index matches |index|. If no navigation index matches |index|, the first | |
| 317 // navigation with an index > |index| is returned. | |
| 318 // | |
| 319 // This assumes the navigations are ordered by index in ascending order. | |
| 320 std::vector<sessions::SerializedNavigationEntry>::iterator | |
| 321 FindClosestNavigationWithIndex( | |
| 322 std::vector<sessions::SerializedNavigationEntry>* navigations, | |
| 323 int index); | |
| 324 | |
| 325 // Does the following: | |
| 326 // . Deletes and removes any windows with no tabs or windows with types other | |
| 327 // than tabbed_browser or browser. NOTE: constrained windows that have | |
| 328 // been dragged out are of type browser. As such, this preserves any dragged | |
| 329 // out constrained windows (aka popups that have been dragged out). | |
| 330 // . Sorts the tabs in windows with valid tabs based on the tabs | |
| 331 // visual order, and adds the valid windows to windows. | |
| 332 void SortTabsBasedOnVisualOrderAndPrune( | |
| 333 std::map<int, SessionWindow*>* windows, | |
| 334 std::vector<SessionWindow*>* valid_windows); | |
| 335 | |
| 336 // Adds tabs to their parent window based on the tab's window_id. This | |
| 337 // ignores tabs with no navigations. | |
| 338 void AddTabsToWindows(std::map<int, SessionTab*>* tabs, | |
| 339 std::map<int, SessionWindow*>* windows); | |
| 340 | |
| 341 // Creates tabs and windows from the commands specified in |data|. The created | |
| 342 // tabs and windows are added to |tabs| and |windows| respectively, with the | |
| 343 // id of the active window set in |active_window_id|. It is up to the caller | |
| 344 // to delete the tabs and windows added to |tabs| and |windows|. | |
| 345 // | |
| 346 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is | |
| 347 // done by AddTabsToWindows. | |
| 348 bool CreateTabsAndWindows(const std::vector<SessionCommand*>& data, | |
| 349 std::map<int, SessionTab*>* tabs, | |
| 350 std::map<int, SessionWindow*>* windows, | |
| 351 SessionID::id_type* active_window_id); | |
| 352 | |
| 353 // Adds commands to commands that will recreate the state of the specified | 252 // Adds commands to commands that will recreate the state of the specified |
| 354 // tab. This adds at most kMaxNavigationCountToPersist navigations (in each | 253 // tab. This adds at most kMaxNavigationCountToPersist navigations (in each |
| 355 // direction from the current navigation index). | 254 // direction from the current navigation index). |
| 356 // A pair is added to tab_to_available_range indicating the range of | 255 // A pair is added to tab_to_available_range indicating the range of |
| 357 // indices that were written. | 256 // indices that were written. |
| 358 void BuildCommandsForTab( | 257 void BuildCommandsForTab( |
| 359 const SessionID& window_id, | 258 const SessionID& window_id, |
| 360 content::WebContents* tab, | 259 content::WebContents* tab, |
| 361 int index_in_window, | 260 int index_in_window, |
| 362 bool is_pinned, | 261 bool is_pinned, |
| 363 std::vector<SessionCommand*>* commands, | 262 ScopedVector<SessionCommand>* commands, |
| 364 IdToRange* tab_to_available_range); | 263 IdToRange* tab_to_available_range); |
| 365 | 264 |
| 366 // Adds commands to create the specified browser, and invokes | 265 // Adds commands to create the specified browser, and invokes |
| 367 // BuildCommandsForTab for each of the tabs in the browser. This ignores | 266 // BuildCommandsForTab for each of the tabs in the browser. This ignores |
| 368 // any tabs not in the profile we were created with. | 267 // any tabs not in the profile we were created with. |
| 369 void BuildCommandsForBrowser( | 268 void BuildCommandsForBrowser( |
| 370 Browser* browser, | 269 Browser* browser, |
| 371 std::vector<SessionCommand*>* commands, | 270 ScopedVector<SessionCommand>* commands, |
| 372 IdToRange* tab_to_available_range, | 271 IdToRange* tab_to_available_range, |
| 373 std::set<SessionID::id_type>* windows_to_track); | 272 std::set<SessionID::id_type>* windows_to_track); |
| 374 | 273 |
| 375 // Iterates over all the known browsers invoking BuildCommandsForBrowser. | 274 // Iterates over all the known browsers invoking BuildCommandsForBrowser. |
| 376 // This only adds browsers that should be tracked | 275 // This only adds browsers that should be tracked (|ShouldRestoreWindowOfType| |
| 377 // (should_track_changes_for_browser_type returns true). All browsers that | 276 // returns true). All browsers that are tracked are added to windows_to_track |
| 378 // are tracked are added to windows_to_track (as long as it is non-null). | 277 // (as long as it is non-null). |
| 379 void BuildCommandsFromBrowsers( | 278 void BuildCommandsFromBrowsers( |
| 380 std::vector<SessionCommand*>* commands, | 279 ScopedVector<SessionCommand>* commands, |
| 381 IdToRange* tab_to_available_range, | 280 IdToRange* tab_to_available_range, |
| 382 std::set<SessionID::id_type>* windows_to_track); | 281 std::set<SessionID::id_type>* windows_to_track); |
| 383 | 282 |
| 384 // Schedules a reset. A reset means the contents of the file are recreated | 283 // Schedules a reset of the existing commands. A reset means the contents |
| 385 // from the state of the browser. | 284 // of the file are recreated from the state of the browser. |
| 386 void ScheduleReset(); | 285 void ScheduleResetCommands(); |
| 387 | 286 |
| 388 // Searches for a pending command that can be replaced with command. | 287 // Schedules the specified command. |
| 389 // If one is found, pending command is removed, command is added to | 288 void ScheduleCommand(scoped_ptr<SessionCommand> command); |
| 390 // the pending commands and true is returned. | |
| 391 bool ReplacePendingCommand(SessionCommand* command); | |
| 392 | |
| 393 // Schedules the specified command. This method takes ownership of the | |
| 394 // command. | |
| 395 void ScheduleCommand(SessionCommand* command) override; | |
| 396 | 289 |
| 397 // Converts all pending tab/window closes to commands and schedules them. | 290 // Converts all pending tab/window closes to commands and schedules them. |
| 398 void CommitPendingCloses(); | 291 void CommitPendingCloses(); |
| 399 | 292 |
| 400 // Returns true if there is only one window open with a single tab that shares | 293 // Returns true if there is only one window open with a single tab that shares |
| 401 // our profile. | 294 // our profile. |
| 402 bool IsOnlyOneTabLeft() const; | 295 bool IsOnlyOneTabLeft() const; |
| 403 | 296 |
| 404 // Returns true if there are open trackable browser windows whose ids do | 297 // Returns true if there are open trackable browser windows whose ids do |
| 405 // match |window_id| with our profile. A trackable window is a window from | 298 // match |window_id| with our profile. A trackable window is a window from |
| 406 // which |should_track_changes_for_browser_type| returns true. See | 299 // which |ShouldRestoreWindowOfType| returns true. See |
| 407 // |should_track_changes_for_browser_type| for details. | 300 // |ShouldRestoreWindowOfType| for details. |
| 408 bool HasOpenTrackableBrowsers(const SessionID& window_id) const; | 301 bool HasOpenTrackableBrowsers(const SessionID& window_id) const; |
| 409 | 302 |
| 410 // Returns true if changes to tabs in the specified window should be tracked. | 303 // Returns true if changes to tabs in the specified window should be tracked. |
| 411 bool ShouldTrackChangesToWindow(const SessionID& window_id) const; | 304 bool ShouldTrackChangesToWindow(const SessionID& window_id) const; |
| 412 | 305 |
| 413 // Returns true if we track changes to the specified browser. | 306 // Returns true if we track changes to the specified browser. |
| 414 bool ShouldTrackBrowser(Browser* browser) const; | 307 bool ShouldTrackBrowser(Browser* browser) const; |
| 415 | 308 |
| 416 // Returns true if we track changes to the specified browser type. | |
| 417 static bool should_track_changes_for_browser_type( | |
| 418 Browser::Type type, | |
| 419 AppType app_type); | |
| 420 | |
| 421 // Call when certain session relevant notifications | 309 // Call when certain session relevant notifications |
| 422 // (tab_closed, nav_list_pruned) occur. In addition, this is | 310 // (tab_closed, nav_list_pruned) occur. In addition, this is |
| 423 // currently called when Save() is called to compare how often the | 311 // currently called when Save() is called to compare how often the |
| 424 // session data is currently saved verses when we may want to save it. | 312 // session data is currently saved verses when we may want to save it. |
| 425 // It records the data in UMA stats. | 313 // It records the data in UMA stats. |
| 426 void RecordSessionUpdateHistogramData(int type, | 314 void RecordSessionUpdateHistogramData(int type, |
| 427 base::TimeTicks* last_updated_time); | 315 base::TimeTicks* last_updated_time); |
| 428 | 316 |
| 429 // Helper methods to record the histogram data | 317 // Helper methods to record the histogram data |
| 430 void RecordUpdatedTabClosed(base::TimeDelta delta, bool use_long_period); | 318 void RecordUpdatedTabClosed(base::TimeDelta delta, bool use_long_period); |
| 431 void RecordUpdatedNavListPruned(base::TimeDelta delta, bool use_long_period); | 319 void RecordUpdatedNavListPruned(base::TimeDelta delta, bool use_long_period); |
| 432 void RecordUpdatedNavEntryCommit(base::TimeDelta delta, bool use_long_period); | 320 void RecordUpdatedNavEntryCommit(base::TimeDelta delta, bool use_long_period); |
| 433 void RecordUpdatedSaveTime(base::TimeDelta delta, bool use_long_period); | 321 void RecordUpdatedSaveTime(base::TimeDelta delta, bool use_long_period); |
| 434 void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta, | 322 void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta, |
| 435 bool use_long_period); | 323 bool use_long_period); |
| 436 | 324 |
| 437 // Deletes session data if no windows are open for the current profile. | 325 // Deletes session data if no windows are open for the current profile. |
| 438 void MaybeDeleteSessionOnlyData(); | 326 void MaybeDeleteSessionOnlyData(); |
| 439 | 327 |
| 440 // Convert back/forward between the Browser and SessionService DB window | |
| 441 // types. | |
| 442 static WindowType WindowTypeForBrowserType(Browser::Type type); | |
| 443 static Browser::Type BrowserTypeForWindowType(WindowType type); | |
| 444 | |
| 445 // The profile. This may be null during testing. | 328 // The profile. This may be null during testing. |
| 446 Profile* profile_; | 329 Profile* profile_; |
| 447 | 330 |
| 331 // The interface to create session commands and restore session information |
| 332 // from them. |
| 333 scoped_ptr<SessionServiceCommands> session_commands_; |
| 334 |
| 335 // The owned BaseSessionService. |
| 336 scoped_ptr<BaseSessionService> base_session_service_; |
| 337 |
| 448 content::NotificationRegistrar registrar_; | 338 content::NotificationRegistrar registrar_; |
| 449 | 339 |
| 450 // Maps from session tab id to the range of navigation entries that has | 340 // Maps from session tab id to the range of navigation entries that has |
| 451 // been written to disk. | 341 // been written to disk. |
| 452 // | 342 // |
| 453 // This is only used if not all the navigation entries have been | 343 // This is only used if not all the navigation entries have been |
| 454 // written. | 344 // written. |
| 455 IdToRange tab_to_available_range_; | 345 IdToRange tab_to_available_range_; |
| 456 | 346 |
| 457 // When the user closes the last window, where the last window is the | 347 // When the user closes the last window, where the last window is the |
| 458 // last tabbed browser and no more tabbed browsers are open with the same | 348 // 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 | 349 // 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. | 350 // marks them as closed) if the user creates a new tabbed browser. |
| 461 typedef std::set<SessionID::id_type> PendingWindowCloseIDs; | 351 typedef std::set<SessionID::id_type> PendingWindowCloseIDs; |
| 462 PendingWindowCloseIDs pending_window_close_ids_; | 352 PendingWindowCloseIDs pending_window_close_ids_; |
| 463 | 353 |
| 464 // Set of tabs that have been closed by way of the last window or last tab | 354 // Set of tabs that have been closed by way of the last window or last tab |
| 465 // closing, but not yet committed. | 355 // closing, but not yet committed. |
| 466 typedef std::set<SessionID::id_type> PendingTabCloseIDs; | 356 typedef std::set<SessionID::id_type> PendingTabCloseIDs; |
| 467 PendingTabCloseIDs pending_tab_close_ids_; | 357 PendingTabCloseIDs pending_tab_close_ids_; |
| 468 | 358 |
| 469 // When a window other than the last window (see description of | 359 // 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. | 360 // pending_window_close_ids) is closed, the id is added to this set. |
| 471 typedef std::set<SessionID::id_type> WindowClosingIDs; | 361 typedef std::set<SessionID::id_type> WindowClosingIDs; |
| 472 WindowClosingIDs window_closing_ids_; | 362 WindowClosingIDs window_closing_ids_; |
| 473 | 363 |
| 474 // Set of windows we're tracking changes to. This is only browsers that | 364 // Set of windows we're tracking changes to. This is only browsers that |
| 475 // return true from should_track_changes_for_browser_type. | 365 // return true from |ShouldRestoreWindowOfType|. |
| 476 typedef std::set<SessionID::id_type> WindowsTracking; | 366 typedef std::set<SessionID::id_type> WindowsTracking; |
| 477 WindowsTracking windows_tracking_; | 367 WindowsTracking windows_tracking_; |
| 478 | 368 |
| 479 // Are there any open trackable browsers? | 369 // Are there any open trackable browsers? |
| 480 bool has_open_trackable_browsers_; | 370 bool has_open_trackable_browsers_; |
| 481 | 371 |
| 482 // If true and a new tabbed browser is created and there are no opened tabbed | 372 // If true and a new tabbed browser is created and there are no opened tabbed |
| 483 // browser (has_open_trackable_browsers_ is false), then the current session | 373 // browser (has_open_trackable_browsers_ is false), then the current session |
| 484 // is made the last session. See description above class for details on | 374 // is made the last session. See description above class for details on |
| 485 // current/last session. | 375 // current/last session. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 499 // For browser_tests, since we want to simulate the browser shutting down | 389 // For browser_tests, since we want to simulate the browser shutting down |
| 500 // without quitting. | 390 // without quitting. |
| 501 bool force_browser_not_alive_with_no_windows_; | 391 bool force_browser_not_alive_with_no_windows_; |
| 502 | 392 |
| 503 base::WeakPtrFactory<SessionService> weak_factory_; | 393 base::WeakPtrFactory<SessionService> weak_factory_; |
| 504 | 394 |
| 505 DISALLOW_COPY_AND_ASSIGN(SessionService); | 395 DISALLOW_COPY_AND_ASSIGN(SessionService); |
| 506 }; | 396 }; |
| 507 | 397 |
| 508 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ | 398 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |
| OLD | NEW |