| 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/sessions/session_service_commands.h" |
| 20 #include "chrome/browser/sessions/session_service_utils.h" | 20 #include "chrome/browser/sessions/session_service_utils.h" |
| 21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" | 22 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/browser/ui/browser_list_observer.h" | 23 #include "chrome/browser/ui/browser_list_observer.h" |
| 24 #include "components/keyed_service/core/keyed_service.h" | 24 #include "components/keyed_service/core/keyed_service.h" |
| 25 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
| 26 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
| 27 #include "ui/base/ui_base_types.h" | 27 #include "ui/base/ui_base_types.h" |
| 28 | 28 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 49 // the last session and the current session reset. This is done to provide the | 49 // the last session and the current session reset. This is done to provide the |
| 50 // illusion that app windows run in separate processes. Similar behavior occurs | 50 // illusion that app windows run in separate processes. Similar behavior occurs |
| 51 // with incognito windows. | 51 // with incognito windows. |
| 52 // | 52 // |
| 53 // SessionService itself uses functions from session_service_commands to store | 53 // SessionService itself uses functions from session_service_commands to store |
| 54 // commands which can rebuild the open state of the browser (as |SessionWindow|, | 54 // commands which can rebuild the open state of the browser (as |SessionWindow|, |
| 55 // |SessionTab| and |SerializedNavigationEntry|). The commands are periodically | 55 // |SessionTab| and |SerializedNavigationEntry|). The commands are periodically |
| 56 // flushed to |SessionBackend| and written to a file. Every so often | 56 // flushed to |SessionBackend| and written to a file. Every so often |
| 57 // |SessionService| rebuilds the contents of the file from the open state of the | 57 // |SessionService| rebuilds the contents of the file from the open state of the |
| 58 // browser. | 58 // browser. |
| 59 class SessionService : public BaseSessionService, | 59 class SessionService : public BaseSessionServiceDelegateImpl, |
| 60 public KeyedService, | 60 public KeyedService, |
| 61 public content::NotificationObserver, | 61 public content::NotificationObserver, |
| 62 public chrome::BrowserListObserver { | 62 public chrome::BrowserListObserver { |
| 63 friend class SessionServiceTestHelper; | 63 friend class SessionServiceTestHelper; |
| 64 public: | 64 public: |
| 65 // Used to distinguish an application from a ordinary content window. | 65 // Used to distinguish an application from a ordinary content window. |
| 66 enum AppType { | 66 enum AppType { |
| 67 TYPE_APP, | 67 TYPE_APP, |
| 68 TYPE_NORMAL | 68 TYPE_NORMAL |
| 69 }; | 69 }; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 93 | 93 |
| 94 // Resets the contents of the file from the current state of all open | 94 // Resets the contents of the file from the current state of all open |
| 95 // browsers whose profile matches our profile. | 95 // browsers whose profile matches our profile. |
| 96 void ResetFromCurrentBrowsers(); | 96 void ResetFromCurrentBrowsers(); |
| 97 | 97 |
| 98 // Moves the current session to the last session. This is useful when a | 98 // Moves the current session to the last session. This is useful when a |
| 99 // checkpoint occurs, such as when the user launches the app and no tabbed | 99 // checkpoint occurs, such as when the user launches the app and no tabbed |
| 100 // browsers are running. | 100 // browsers are running. |
| 101 void MoveCurrentSessionToLastSession(); | 101 void MoveCurrentSessionToLastSession(); |
| 102 | 102 |
| 103 // Deletes the last session. |
| 104 void DeleteLastSession(); |
| 105 |
| 103 // Associates a tab with a window. | 106 // Associates a tab with a window. |
| 104 void SetTabWindow(const SessionID& window_id, | 107 void SetTabWindow(const SessionID& window_id, |
| 105 const SessionID& tab_id); | 108 const SessionID& tab_id); |
| 106 | 109 |
| 107 // Sets the bounds of a window. | 110 // Sets the bounds of a window. |
| 108 void SetWindowBounds(const SessionID& window_id, | 111 void SetWindowBounds(const SessionID& window_id, |
| 109 const gfx::Rect& bounds, | 112 const gfx::Rect& bounds, |
| 110 ui::WindowShowState show_state); | 113 ui::WindowShowState show_state); |
| 111 | 114 |
| 112 // Sets the visual index of the tab in its parent window. | 115 // Sets the visual index of the tab in its parent window. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 typedef base::Callback<void(ScopedVector<SessionWindow>, SessionID::id_type)> | 203 typedef base::Callback<void(ScopedVector<SessionWindow>, SessionID::id_type)> |
| 201 SessionCallback; | 204 SessionCallback; |
| 202 | 205 |
| 203 // Fetches the contents of the last session, notifying the callback when | 206 // Fetches the contents of the last session, notifying the callback when |
| 204 // done. If the callback is supplied an empty vector of SessionWindows | 207 // done. If the callback is supplied an empty vector of SessionWindows |
| 205 // it means the session could not be restored. | 208 // it means the session could not be restored. |
| 206 base::CancelableTaskTracker::TaskId GetLastSession( | 209 base::CancelableTaskTracker::TaskId GetLastSession( |
| 207 const SessionCallback& callback, | 210 const SessionCallback& callback, |
| 208 base::CancelableTaskTracker* tracker); | 211 base::CancelableTaskTracker* tracker); |
| 209 | 212 |
| 210 // Overridden from BaseSessionService because we want some UMA reporting on | 213 // BaseSessionServiceDelegateImpl: |
| 211 // session update activities. | 214 void OnSavedCommands() override; |
| 212 void Save() override; | |
| 213 | 215 |
| 214 private: | 216 private: |
| 215 // Allow tests to access our innards for testing purposes. | 217 // Allow tests to access our innards for testing purposes. |
| 218 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, SavedSessionNotification); |
| 216 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation1); | 219 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation1); |
| 217 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation2); | 220 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation2); |
| 218 FRIEND_TEST_ALL_PREFIXES(NoStartupWindowTest, DontInitSessionServiceForApps); | 221 FRIEND_TEST_ALL_PREFIXES(NoStartupWindowTest, DontInitSessionServiceForApps); |
| 219 | 222 |
| 220 typedef std::map<SessionID::id_type, std::pair<int, int> > IdToRange; | 223 typedef std::map<SessionID::id_type, std::pair<int, int> > IdToRange; |
| 221 | 224 |
| 222 void Init(); | 225 void Init(); |
| 223 | 226 |
| 224 // Returns true if a window of given |window_type| and |app_type| should get | 227 // Returns true if a window of given |window_type| and |app_type| should get |
| 225 // restored upon session restore. | 228 // restored upon session restore. |
| 226 bool ShouldRestoreWindowOfType(SessionWindow::WindowType type, | 229 bool ShouldRestoreWindowOfType(SessionWindow::WindowType type, |
| 227 AppType app_type) const; | 230 AppType app_type) const; |
| 228 | 231 |
| 229 // Removes unrestorable windows from the previous windows list. | 232 // Removes unrestorable windows from the previous windows list. |
| 230 void RemoveUnusedRestoreWindows(std::vector<SessionWindow*>* window_list); | 233 void RemoveUnusedRestoreWindows(std::vector<SessionWindow*>* window_list); |
| 231 | 234 |
| 232 // Returns true if we have scheduled any commands, or any scheduled commands | |
| 233 // have been saved. | |
| 234 bool processed_any_commands(); | |
| 235 | |
| 236 // Implementation of RestoreIfNecessary. If |browser| is non-null and we need | 235 // Implementation of RestoreIfNecessary. If |browser| is non-null and we need |
| 237 // to restore, the tabs are added to it, otherwise a new browser is created. | 236 // to restore, the tabs are added to it, otherwise a new browser is created. |
| 238 bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open, | 237 bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open, |
| 239 Browser* browser); | 238 Browser* browser); |
| 240 | 239 |
| 241 void Observe(int type, | 240 void Observe(int type, |
| 242 const content::NotificationSource& source, | 241 const content::NotificationSource& source, |
| 243 const content::NotificationDetails& details) override; | 242 const content::NotificationDetails& details) override; |
| 244 | 243 |
| 245 // chrome::BrowserListObserver | 244 // chrome::BrowserListObserver |
| 246 void OnBrowserAdded(Browser* browser) override {} | 245 void OnBrowserAdded(Browser* browser) override {} |
| 247 void OnBrowserRemoved(Browser* browser) override {} | 246 void OnBrowserRemoved(Browser* browser) override {} |
| 248 void OnBrowserSetLastActive(Browser* browser) override; | 247 void OnBrowserSetLastActive(Browser* browser) override; |
| 249 | 248 |
| 250 // Converts |commands| to SessionWindows and notifies the callback. | 249 // Converts |commands| to SessionWindows and notifies the callback. |
| 251 void OnGotSessionCommands(const SessionCallback& callback, | 250 void OnGotSessionCommands(const SessionCallback& callback, |
| 252 ScopedVector<SessionCommand> commands); | 251 ScopedVector<SessionCommand> commands); |
| 253 | 252 |
| 254 // Adds commands to commands that will recreate the state of the specified | 253 // Adds commands to commands that will recreate the state of the specified |
| 255 // tab. This adds at most kMaxNavigationCountToPersist navigations (in each | 254 // tab. This adds at most kMaxNavigationCountToPersist navigations (in each |
| 256 // direction from the current navigation index). | 255 // direction from the current navigation index). |
| 257 // A pair is added to tab_to_available_range indicating the range of | 256 // A pair is added to tab_to_available_range indicating the range of |
| 258 // indices that were written. | 257 // indices that were written. |
| 259 void BuildCommandsForTab( | 258 void BuildCommandsForTab( |
| 260 const SessionID& window_id, | 259 const SessionID& window_id, |
| 261 content::WebContents* tab, | 260 content::WebContents* tab, |
| 262 int index_in_window, | 261 int index_in_window, |
| 263 bool is_pinned, | 262 bool is_pinned, |
| 264 ScopedVector<SessionCommand>* commands, | |
| 265 IdToRange* tab_to_available_range); | 263 IdToRange* tab_to_available_range); |
| 266 | 264 |
| 267 // Adds commands to create the specified browser, and invokes | 265 // Adds commands to create the specified browser, and invokes |
| 268 // BuildCommandsForTab for each of the tabs in the browser. This ignores | 266 // BuildCommandsForTab for each of the tabs in the browser. This ignores |
| 269 // any tabs not in the profile we were created with. | 267 // any tabs not in the profile we were created with. |
| 270 void BuildCommandsForBrowser( | 268 void BuildCommandsForBrowser( |
| 271 Browser* browser, | 269 Browser* browser, |
| 272 ScopedVector<SessionCommand>* commands, | |
| 273 IdToRange* tab_to_available_range, | 270 IdToRange* tab_to_available_range, |
| 274 std::set<SessionID::id_type>* windows_to_track); | 271 std::set<SessionID::id_type>* windows_to_track); |
| 275 | 272 |
| 276 // Iterates over all the known browsers invoking BuildCommandsForBrowser. | 273 // Iterates over all the known browsers invoking BuildCommandsForBrowser. |
| 277 // This only adds browsers that should be tracked (|ShouldRestoreWindowOfType| | 274 // This only adds browsers that should be tracked (|ShouldRestoreWindowOfType| |
| 278 // returns true). All browsers that are tracked are added to windows_to_track | 275 // returns true). All browsers that are tracked are added to windows_to_track |
| 279 // (as long as it is non-null). | 276 // (as long as it is non-null). |
| 280 void BuildCommandsFromBrowsers( | 277 void BuildCommandsFromBrowsers( |
| 281 ScopedVector<SessionCommand>* commands, | |
| 282 IdToRange* tab_to_available_range, | 278 IdToRange* tab_to_available_range, |
| 283 std::set<SessionID::id_type>* windows_to_track); | 279 std::set<SessionID::id_type>* windows_to_track); |
| 284 | 280 |
| 285 // Schedules a reset of the existing commands. A reset means the contents | 281 // Schedules a reset of the existing commands. A reset means the contents |
| 286 // of the file are recreated from the state of the browser. | 282 // of the file are recreated from the state of the browser. |
| 287 void ScheduleResetCommands(); | 283 void ScheduleResetCommands(); |
| 288 | 284 |
| 289 // Schedules the specified command. | 285 // Schedules the specified command. |
| 290 void ScheduleCommand(scoped_ptr<SessionCommand> command) override; | 286 void ScheduleCommand(scoped_ptr<SessionCommand> command); |
| 291 | 287 |
| 292 // Converts all pending tab/window closes to commands and schedules them. | 288 // Converts all pending tab/window closes to commands and schedules them. |
| 293 void CommitPendingCloses(); | 289 void CommitPendingCloses(); |
| 294 | 290 |
| 295 // Returns true if there is only one window open with a single tab that shares | 291 // Returns true if there is only one window open with a single tab that shares |
| 296 // our profile. | 292 // our profile. |
| 297 bool IsOnlyOneTabLeft() const; | 293 bool IsOnlyOneTabLeft() const; |
| 298 | 294 |
| 299 // Returns true if there are open trackable browser windows whose ids do | 295 // Returns true if there are open trackable browser windows whose ids do |
| 300 // match |window_id| with our profile. A trackable window is a window from | 296 // match |window_id| with our profile. A trackable window is a window from |
| (...skipping 19 matching lines...) Expand all Loading... |
| 320 void RecordUpdatedTabClosed(base::TimeDelta delta, bool use_long_period); | 316 void RecordUpdatedTabClosed(base::TimeDelta delta, bool use_long_period); |
| 321 void RecordUpdatedNavListPruned(base::TimeDelta delta, bool use_long_period); | 317 void RecordUpdatedNavListPruned(base::TimeDelta delta, bool use_long_period); |
| 322 void RecordUpdatedNavEntryCommit(base::TimeDelta delta, bool use_long_period); | 318 void RecordUpdatedNavEntryCommit(base::TimeDelta delta, bool use_long_period); |
| 323 void RecordUpdatedSaveTime(base::TimeDelta delta, bool use_long_period); | 319 void RecordUpdatedSaveTime(base::TimeDelta delta, bool use_long_period); |
| 324 void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta, | 320 void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta, |
| 325 bool use_long_period); | 321 bool use_long_period); |
| 326 | 322 |
| 327 // Deletes session data if no windows are open for the current profile. | 323 // Deletes session data if no windows are open for the current profile. |
| 328 void MaybeDeleteSessionOnlyData(); | 324 void MaybeDeleteSessionOnlyData(); |
| 329 | 325 |
| 326 // Unit test accessors. |
| 327 BaseSessionService* GetBaseSessionServiceForTest(); |
| 328 |
| 330 // The profile. This may be null during testing. | 329 // The profile. This may be null during testing. |
| 331 Profile* profile_; | 330 Profile* profile_; |
| 332 | 331 |
| 332 // The owned BaseSessionService. |
| 333 scoped_ptr<BaseSessionService> base_session_service_; |
| 334 |
| 333 content::NotificationRegistrar registrar_; | 335 content::NotificationRegistrar registrar_; |
| 334 | 336 |
| 335 // Maps from session tab id to the range of navigation entries that has | 337 // Maps from session tab id to the range of navigation entries that has |
| 336 // been written to disk. | 338 // been written to disk. |
| 337 // | 339 // |
| 338 // This is only used if not all the navigation entries have been | 340 // This is only used if not all the navigation entries have been |
| 339 // written. | 341 // written. |
| 340 IdToRange tab_to_available_range_; | 342 IdToRange tab_to_available_range_; |
| 341 | 343 |
| 342 // When the user closes the last window, where the last window is the | 344 // When the user closes the last window, where the last window is the |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 // For browser_tests, since we want to simulate the browser shutting down | 386 // For browser_tests, since we want to simulate the browser shutting down |
| 385 // without quitting. | 387 // without quitting. |
| 386 bool force_browser_not_alive_with_no_windows_; | 388 bool force_browser_not_alive_with_no_windows_; |
| 387 | 389 |
| 388 base::WeakPtrFactory<SessionService> weak_factory_; | 390 base::WeakPtrFactory<SessionService> weak_factory_; |
| 389 | 391 |
| 390 DISALLOW_COPY_AND_ASSIGN(SessionService); | 392 DISALLOW_COPY_AND_ASSIGN(SessionService); |
| 391 }; | 393 }; |
| 392 | 394 |
| 393 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ | 395 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |
| OLD | NEW |