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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_
6 #define CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/defaults.h"
17 #include "chrome/browser/sessions/base_session_service.h"
18 #include "chrome/browser/sessions/session_id.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/browser_list_observer.h"
22 #include "components/keyed_service/core/keyed_service.h"
23 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "ui/base/ui_base_types.h"
33 class NavigationEntry
;
37 // SessionService ------------------------------------------------------------
39 // SessionService is responsible for maintaining the state of open windows
40 // and tabs so that they can be restored at a later date. The state of the
41 // currently open browsers is referred to as the current session.
43 // SessionService supports restoring from the last session. The last session
44 // typically corresponds to the last run of the browser, but not always. For
45 // example, if the user has a tabbed browser and app window running, closes the
46 // tabbed browser, then creates a new tabbed browser the current session is made
47 // the last session and the current session reset. This is done to provide the
48 // illusion that app windows run in separate processes. Similar behavior occurs
49 // with incognito windows.
51 // SessionService itself maintains a set of SessionCommands that allow
52 // SessionService to rebuild the open state of the browser (as SessionWindow,
53 // SessionTab and SerializedNavigationEntry). The commands are periodically
54 // flushed to SessionBackend and written to a file. Every so often
55 // SessionService rebuilds the contents of the file from the open state of the
57 class SessionService
: public BaseSessionService
,
59 public content::NotificationObserver
,
60 public chrome::BrowserListObserver
{
61 friend class SessionServiceTestHelper
;
63 // Used to distinguish an application window from a normal one.
69 // Creates a SessionService for the specified profile.
70 explicit SessionService(Profile
* profile
);
72 explicit SessionService(const base::FilePath
& save_path
);
74 virtual ~SessionService();
76 // Returns true if a new window opening should really be treated like the
77 // start of a session (with potential session restore, startup URLs, etc.).
78 // In particular, this is true if there are no tabbed browsers running
79 // currently (eg. because only background or other app pages are running).
80 bool ShouldNewWindowStartSession();
82 // Invoke at a point when you think session restore might occur. For example,
83 // during startup and window creation this is invoked to see if a session
84 // needs to be restored. If a session needs to be restored it is done so
85 // asynchronously and true is returned. If false is returned the session was
86 // not restored and the caller needs to create a new window.
87 bool RestoreIfNecessary(const std::vector
<GURL
>& urls_to_open
);
89 // Resets the contents of the file from the current state of all open
90 // browsers whose profile matches our profile.
91 void ResetFromCurrentBrowsers();
93 // Moves the current session to the last session. This is useful when a
94 // checkpoint occurs, such as when the user launches the app and no tabbed
95 // browsers are running.
96 void MoveCurrentSessionToLastSession();
98 // Associates a tab with a window.
99 void SetTabWindow(const SessionID
& window_id
,
100 const SessionID
& tab_id
);
102 // Sets the bounds of a window.
103 void SetWindowBounds(const SessionID
& window_id
,
104 const gfx::Rect
& bounds
,
105 ui::WindowShowState show_state
);
107 // Sets the visual index of the tab in its parent window.
108 void SetTabIndexInWindow(const SessionID
& window_id
,
109 const SessionID
& tab_id
,
112 // Sets the pinned state of the tab.
113 void SetPinnedState(const SessionID
& window_id
,
114 const SessionID
& tab_id
,
117 // Notification that a tab has been closed. |closed_by_user_gesture| comes
118 // from |WebContents::closed_by_user_gesture|; see it for details.
120 // Note: this is invoked from the NavigationController's destructor, which is
121 // after the actual tab has been removed.
122 void TabClosed(const SessionID
& window_id
,
123 const SessionID
& tab_id
,
124 bool closed_by_user_gesture
);
126 // Notification a window has opened.
127 void WindowOpened(Browser
* browser
);
129 // Notification the window is about to close.
130 void WindowClosing(const SessionID
& window_id
);
132 // Notification a window has finished closing.
133 void WindowClosed(const SessionID
& window_id
);
135 // Called when a tab is inserted.
136 void TabInserted(content::WebContents
* contents
);
138 // Called when a tab is closing.
139 void TabClosing(content::WebContents
* contents
);
141 // Sets the type of window. In order for the contents of a window to be
142 // tracked SetWindowType must be invoked with a type we track
143 // (should_track_changes_for_browser_type returns true).
144 void SetWindowType(const SessionID
& window_id
,
148 // Sets the application name of the specified window.
149 void SetWindowAppName(const SessionID
& window_id
,
150 const std::string
& app_name
);
152 // Invoked when the NavigationController has removed entries from the back of
153 // the list. |count| gives the number of entries in the navigation controller.
154 void TabNavigationPathPrunedFromBack(const SessionID
& window_id
,
155 const SessionID
& tab_id
,
158 // Invoked when the NavigationController has removed entries from the front of
159 // the list. |count| gives the number of entries that were removed.
160 void TabNavigationPathPrunedFromFront(const SessionID
& window_id
,
161 const SessionID
& tab_id
,
164 // Updates the navigation entry for the specified tab.
165 void UpdateTabNavigation(
166 const SessionID
& window_id
,
167 const SessionID
& tab_id
,
168 const sessions::SerializedNavigationEntry
& navigation
);
170 // Notification that a tab has restored its entries or a closed tab is being
172 void TabRestored(content::WebContents
* tab
, bool pinned
);
174 // Sets the index of the selected entry in the navigation controller for the
176 void SetSelectedNavigationIndex(const SessionID
& window_id
,
177 const SessionID
& tab_id
,
180 // Sets the index of the selected tab in the specified window.
181 void SetSelectedTabInWindow(const SessionID
& window_id
, int index
);
183 // Sets the user agent override of the specified tab.
184 void SetTabUserAgentOverride(const SessionID
& window_id
,
185 const SessionID
& tab_id
,
186 const std::string
& user_agent_override
);
188 // Callback from GetLastSession.
189 // The second parameter is the id of the window that was last active.
190 typedef base::Callback
<void(ScopedVector
<SessionWindow
>, SessionID::id_type
)>
193 // Fetches the contents of the last session, notifying the callback when
194 // done. If the callback is supplied an empty vector of SessionWindows
195 // it means the session could not be restored.
196 base::CancelableTaskTracker::TaskId
GetLastSession(
197 const SessionCallback
& callback
,
198 base::CancelableTaskTracker
* tracker
);
200 // Overridden from BaseSessionService because we want some UMA reporting on
201 // session update activities.
202 virtual void Save() OVERRIDE
;
205 // Allow tests to access our innards for testing purposes.
206 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest
, RestoreActivation1
);
207 FRIEND_TEST_ALL_PREFIXES(SessionServiceTest
, RestoreActivation2
);
208 FRIEND_TEST_ALL_PREFIXES(NoStartupWindowTest
, DontInitSessionServiceForApps
);
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
;
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.
218 // Note that we only store browsers of type TYPE_TABBED and TYPE_POPUP.
226 // Returns true if we have scheduled any commands, or any scheduled commands
228 bool processed_any_commands();
230 // Implementation of RestoreIfNecessary. If |browser| is non-null and we need
231 // to restore, the tabs are added to it, otherwise a new browser is created.
232 bool RestoreIfNecessary(const std::vector
<GURL
>& urls_to_open
,
235 virtual void Observe(int type
,
236 const content::NotificationSource
& source
,
237 const content::NotificationDetails
& details
) OVERRIDE
;
239 // chrome::BrowserListObserver
240 virtual void OnBrowserAdded(Browser
* browser
) OVERRIDE
{}
241 virtual void OnBrowserRemoved(Browser
* browser
) OVERRIDE
{}
242 virtual void OnBrowserSetLastActive(Browser
* browser
) OVERRIDE
;
244 // Sets the application extension id of the specified tab.
245 void SetTabExtensionAppID(const SessionID
& window_id
,
246 const SessionID
& tab_id
,
247 const std::string
& extension_app_id
);
249 // Methods to create the various commands. It is up to the caller to delete
250 // the returned the SessionCommand* object.
251 SessionCommand
* CreateSetSelectedTabInWindow(const SessionID
& window_id
,
254 SessionCommand
* CreateSetTabWindowCommand(const SessionID
& window_id
,
255 const SessionID
& tab_id
);
257 SessionCommand
* CreateSetWindowBoundsCommand(const SessionID
& window_id
,
258 const gfx::Rect
& bounds
,
259 ui::WindowShowState show_state
);
261 SessionCommand
* CreateSetTabIndexInWindowCommand(const SessionID
& tab_id
,
264 SessionCommand
* CreateTabClosedCommand(SessionID::id_type tab_id
);
266 SessionCommand
* CreateWindowClosedCommand(SessionID::id_type tab_id
);
268 SessionCommand
* CreateSetSelectedNavigationIndexCommand(
269 const SessionID
& tab_id
,
272 SessionCommand
* CreateSetWindowTypeCommand(const SessionID
& window_id
,
275 SessionCommand
* CreatePinnedStateCommand(const SessionID
& tab_id
,
278 SessionCommand
* CreateSessionStorageAssociatedCommand(
279 const SessionID
& tab_id
,
280 const std::string
& session_storage_persistent_id
);
282 SessionCommand
* CreateSetActiveWindowCommand(const SessionID
& window_id
);
284 // Converts |commands| to SessionWindows and notifies the callback.
285 void OnGotSessionCommands(const SessionCallback
& callback
,
286 ScopedVector
<SessionCommand
> commands
);
288 // Converts the commands into SessionWindows. On return any valid
289 // windows are added to valid_windows. It is up to the caller to delete
290 // the windows added to valid_windows. |active_window_id| will be set with the
291 // id of the last active window, but it's only valid when this id corresponds
292 // to the id of one of the windows in valid_windows.
293 void RestoreSessionFromCommands(const std::vector
<SessionCommand
*>& commands
,
294 std::vector
<SessionWindow
*>* valid_windows
,
295 SessionID::id_type
* active_window_id
);
297 // Iterates through the vector updating the selected_tab_index of each
298 // SessionWindow based on the actual tabs that were restored.
299 void UpdateSelectedTabIndex(std::vector
<SessionWindow
*>* windows
);
301 // Returns the window in windows with the specified id. If a window does
302 // not exist, one is created.
303 SessionWindow
* GetWindow(SessionID::id_type window_id
,
304 IdToSessionWindow
* windows
);
306 // Returns the tab with the specified id in tabs. If a tab does not exist,
308 SessionTab
* GetTab(SessionID::id_type tab_id
,
309 IdToSessionTab
* tabs
);
311 // Returns an iterator into navigations pointing to the navigation whose
312 // index matches |index|. If no navigation index matches |index|, the first
313 // navigation with an index > |index| is returned.
315 // This assumes the navigations are ordered by index in ascending order.
316 std::vector
<sessions::SerializedNavigationEntry
>::iterator
317 FindClosestNavigationWithIndex(
318 std::vector
<sessions::SerializedNavigationEntry
>* navigations
,
321 // Does the following:
322 // . Deletes and removes any windows with no tabs or windows with types other
323 // than tabbed_browser or browser. NOTE: constrained windows that have
324 // been dragged out are of type browser. As such, this preserves any dragged
325 // out constrained windows (aka popups that have been dragged out).
326 // . Sorts the tabs in windows with valid tabs based on the tabs
327 // visual order, and adds the valid windows to windows.
328 void SortTabsBasedOnVisualOrderAndPrune(
329 std::map
<int, SessionWindow
*>* windows
,
330 std::vector
<SessionWindow
*>* valid_windows
);
332 // Adds tabs to their parent window based on the tab's window_id. This
333 // ignores tabs with no navigations.
334 void AddTabsToWindows(std::map
<int, SessionTab
*>* tabs
,
335 std::map
<int, SessionWindow
*>* windows
);
337 // Creates tabs and windows from the commands specified in |data|. The created
338 // tabs and windows are added to |tabs| and |windows| respectively, with the
339 // id of the active window set in |active_window_id|. It is up to the caller
340 // to delete the tabs and windows added to |tabs| and |windows|.
342 // This does NOT add any created SessionTabs to SessionWindow.tabs, that is
343 // done by AddTabsToWindows.
344 bool CreateTabsAndWindows(const std::vector
<SessionCommand
*>& data
,
345 std::map
<int, SessionTab
*>* tabs
,
346 std::map
<int, SessionWindow
*>* windows
,
347 SessionID::id_type
* active_window_id
);
349 // Adds commands to commands that will recreate the state of the specified
350 // tab. This adds at most kMaxNavigationCountToPersist navigations (in each
351 // direction from the current navigation index).
352 // A pair is added to tab_to_available_range indicating the range of
353 // indices that were written.
354 void BuildCommandsForTab(
355 const SessionID
& window_id
,
356 content::WebContents
* tab
,
359 std::vector
<SessionCommand
*>* commands
,
360 IdToRange
* tab_to_available_range
);
362 // Adds commands to create the specified browser, and invokes
363 // BuildCommandsForTab for each of the tabs in the browser. This ignores
364 // any tabs not in the profile we were created with.
365 void BuildCommandsForBrowser(
367 std::vector
<SessionCommand
*>* commands
,
368 IdToRange
* tab_to_available_range
,
369 std::set
<SessionID::id_type
>* windows_to_track
);
371 // Iterates over all the known browsers invoking BuildCommandsForBrowser.
372 // This only adds browsers that should be tracked
373 // (should_track_changes_for_browser_type returns true). All browsers that
374 // are tracked are added to windows_to_track (as long as it is non-null).
375 void BuildCommandsFromBrowsers(
376 std::vector
<SessionCommand
*>* commands
,
377 IdToRange
* tab_to_available_range
,
378 std::set
<SessionID::id_type
>* windows_to_track
);
380 // Schedules a reset. A reset means the contents of the file are recreated
381 // from the state of the browser.
382 void ScheduleReset();
384 // Searches for a pending command that can be replaced with command.
385 // If one is found, pending command is removed, command is added to
386 // the pending commands and true is returned.
387 bool ReplacePendingCommand(SessionCommand
* command
);
389 // Schedules the specified command. This method takes ownership of the
391 virtual void ScheduleCommand(SessionCommand
* command
) OVERRIDE
;
393 // Converts all pending tab/window closes to commands and schedules them.
394 void CommitPendingCloses();
396 // Returns true if there is only one window open with a single tab that shares
398 bool IsOnlyOneTabLeft() const;
400 // Returns true if there are open trackable browser windows whose ids do
401 // match |window_id| with our profile. A trackable window is a window from
402 // which |should_track_changes_for_browser_type| returns true. See
403 // |should_track_changes_for_browser_type| for details.
404 bool HasOpenTrackableBrowsers(const SessionID
& window_id
) const;
406 // Returns true if changes to tabs in the specified window should be tracked.
407 bool ShouldTrackChangesToWindow(const SessionID
& window_id
) const;
409 // Returns true if we track changes to the specified browser.
410 bool ShouldTrackBrowser(Browser
* browser
) const;
412 // Returns true if we track changes to the specified browser type.
413 static bool should_track_changes_for_browser_type(
417 // Call when certain session relevant notifications
418 // (tab_closed, nav_list_pruned) occur. In addition, this is
419 // currently called when Save() is called to compare how often the
420 // session data is currently saved verses when we may want to save it.
421 // It records the data in UMA stats.
422 void RecordSessionUpdateHistogramData(int type
,
423 base::TimeTicks
* last_updated_time
);
425 // Helper methods to record the histogram data
426 void RecordUpdatedTabClosed(base::TimeDelta delta
, bool use_long_period
);
427 void RecordUpdatedNavListPruned(base::TimeDelta delta
, bool use_long_period
);
428 void RecordUpdatedNavEntryCommit(base::TimeDelta delta
, bool use_long_period
);
429 void RecordUpdatedSaveTime(base::TimeDelta delta
, bool use_long_period
);
430 void RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta
,
431 bool use_long_period
);
433 // Deletes session data if no windows are open for the current profile.
434 void MaybeDeleteSessionOnlyData();
436 // Convert back/forward between the Browser and SessionService DB window
438 static WindowType
WindowTypeForBrowserType(Browser::Type type
);
439 static Browser::Type
BrowserTypeForWindowType(WindowType type
);
441 content::NotificationRegistrar registrar_
;
443 // Maps from session tab id to the range of navigation entries that has
444 // been written to disk.
446 // This is only used if not all the navigation entries have been
448 IdToRange tab_to_available_range_
;
450 // When the user closes the last window, where the last window is the
451 // last tabbed browser and no more tabbed browsers are open with the same
452 // profile, the window ID is added here. These IDs are only committed (which
453 // marks them as closed) if the user creates a new tabbed browser.
454 typedef std::set
<SessionID::id_type
> PendingWindowCloseIDs
;
455 PendingWindowCloseIDs pending_window_close_ids_
;
457 // Set of tabs that have been closed by way of the last window or last tab
458 // closing, but not yet committed.
459 typedef std::set
<SessionID::id_type
> PendingTabCloseIDs
;
460 PendingTabCloseIDs pending_tab_close_ids_
;
462 // When a window other than the last window (see description of
463 // pending_window_close_ids) is closed, the id is added to this set.
464 typedef std::set
<SessionID::id_type
> WindowClosingIDs
;
465 WindowClosingIDs window_closing_ids_
;
467 // Set of windows we're tracking changes to. This is only browsers that
468 // return true from should_track_changes_for_browser_type.
469 typedef std::set
<SessionID::id_type
> WindowsTracking
;
470 WindowsTracking windows_tracking_
;
472 // Are there any open trackable browsers?
473 bool has_open_trackable_browsers_
;
475 // If true and a new tabbed browser is created and there are no opened tabbed
476 // browser (has_open_trackable_browsers_ is false), then the current session
477 // is made the last session. See description above class for details on
478 // current/last session.
479 bool move_on_new_browser_
;
481 // Used for reporting frequency of session altering operations.
482 base::TimeTicks last_updated_tab_closed_time_
;
483 base::TimeTicks last_updated_nav_list_pruned_time_
;
484 base::TimeTicks last_updated_nav_entry_commit_time_
;
485 base::TimeTicks last_updated_save_time_
;
487 // Constants used in calculating histogram data.
488 const base::TimeDelta save_delay_in_millis_
;
489 const base::TimeDelta save_delay_in_mins_
;
490 const base::TimeDelta save_delay_in_hrs_
;
492 // For browser_tests, since we want to simulate the browser shutting down
494 bool force_browser_not_alive_with_no_windows_
;
496 DISALLOW_COPY_AND_ASSIGN(SessionService
);
499 #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_