[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> |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 10 | #include <utility> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
[email protected] | f9ffb89 | 2011-11-28 18:57:31 | [diff] [blame] | 12 | #include "base/callback.h" |
thestig | a0e18cd | 2015-09-25 04:58:36 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 14 | #include "base/macros.h" |
sky | 1a14f49 | 2014-08-26 21:44:26 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 16 | #include "base/optional.h" |
Collin Baker | d61e2fe | 2019-08-07 19:19:53 | [diff] [blame] | 17 | #include "base/strings/string16.h" |
[email protected] | e95b717f | 2014-02-06 13:47:13 | [diff] [blame] | 18 | #include "base/task/cancelable_task_tracker.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 19 | #include "base/time/time.h" |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 20 | #include "base/token.h" |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 21 | #include "chrome/browser/defaults.h" |
blundell | 6d34c34 | 2015-09-11 09:09:10 | [diff] [blame] | 22 | #include "chrome/browser/sessions/session_common_utils.h" |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 23 | #include "chrome/browser/sessions/session_service_utils.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser.h" |
[email protected] | 0665ebe | 2013-02-13 09:53:19 | [diff] [blame] | 25 | #include "chrome/browser/ui/browser_finder.h" |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 26 | #include "chrome/browser/ui/browser_list_observer.h" |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 27 | #include "components/keyed_service/core/keyed_service.h" |
Scott Violet | 13d44fd | 2020-01-14 20:38:33 | [diff] [blame] | 28 | #include "components/sessions/content/session_tab_helper_delegate.h" |
Scott Violet | cae997b5 | 2020-01-17 16:47:44 | [diff] [blame] | 29 | #include "components/sessions/core/command_storage_manager_delegate.h" |
blundell | 47c6d8a | 2015-09-24 11:06:40 | [diff] [blame] | 30 | #include "components/sessions/core/session_service_commands.h" |
blundell | 56789fe | 2015-09-10 05:11:16 | [diff] [blame] | 31 | #include "components/sessions/core/tab_restore_service_client.h" |
Connie Wan | 866179b | 2019-12-18 21:39:33 | [diff] [blame] | 32 | #include "components/tab_groups/tab_group_id.h" |
| 33 | #include "components/tab_groups/tab_group_visual_data.h" |
Collin Baker | d61e2fe | 2019-08-07 19:19:53 | [diff] [blame] | 34 | #include "third_party/skia/include/core/SkColor.h" |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 35 | #include "ui/base/ui_base_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | class Profile; |
[email protected] | ad23a09 | 2011-12-28 07:02:04 | [diff] [blame] | 38 | |
| 39 | namespace content { |
[email protected] | 4060015 | 2012-09-26 10:04:31 | [diff] [blame] | 40 | class WebContents; |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 41 | } // namespace content |
| 42 | |
| 43 | namespace sessions { |
| 44 | class SessionCommand; |
| 45 | struct SessionTab; |
| 46 | struct SessionWindow; |
Scott Violet | 8e3a335d | 2020-01-17 20:37:38 | [diff] [blame] | 47 | class SnapshottingCommandStorageManager; |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 48 | } // namespace sessions |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | |
| 50 | // SessionService ------------------------------------------------------------ |
| 51 | |
| 52 | // SessionService is responsible for maintaining the state of open windows |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 53 | // and tabs so that they can be restored at a later date. The state of the |
| 54 | // currently open browsers is referred to as the current session. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | // |
[email protected] | 668cd42 | 2011-04-06 21:00:01 | [diff] [blame] | 56 | // SessionService supports restoring from the last session. The last session |
| 57 | // typically corresponds to the last run of the browser, but not always. For |
| 58 | // example, if the user has a tabbed browser and app window running, closes the |
| 59 | // tabbed browser, then creates a new tabbed browser the current session is made |
| 60 | // the last session and the current session reset. This is done to provide the |
| 61 | // illusion that app windows run in separate processes. Similar behavior occurs |
| 62 | // with incognito windows. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | // |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 64 | // SessionService itself uses functions from session_service_commands to store |
| 65 | // commands which can rebuild the open state of the browser (as |SessionWindow|, |
| 66 | // |SessionTab| and |SerializedNavigationEntry|). The commands are periodically |
Scott Violet | ebfa63c | 2020-01-17 21:17:28 | [diff] [blame] | 67 | // flushed to |CommandStorageBackend| and written to a file. Every so often |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 68 | // |SessionService| rebuilds the contents of the file from the open state of the |
[email protected] | 40a7e41 | 2013-04-29 18:13:01 | [diff] [blame] | 69 | // browser. |
Scott Violet | cae997b5 | 2020-01-17 16:47:44 | [diff] [blame] | 70 | class SessionService : public sessions::CommandStorageManagerDelegate, |
Scott Violet | 13d44fd | 2020-01-14 20:38:33 | [diff] [blame] | 71 | public sessions::SessionTabHelperDelegate, |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 72 | public KeyedService, |
cm.sanchi | a0d6add | 2017-12-13 04:59:33 | [diff] [blame] | 73 | public BrowserListObserver { |
[email protected] | 86ef6a39 | 2012-05-11 22:03:11 | [diff] [blame] | 74 | friend class SessionServiceTestHelper; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | // Creates a SessionService for the specified profile. |
| 77 | explicit SessionService(Profile* profile); |
| 78 | // For testing. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 79 | explicit SessionService(const base::FilePath& save_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 81 | ~SessionService() override; |
[email protected] | f0a58a38 | 2011-04-26 16:09:29 | [diff] [blame] | 82 | |
skuhne | d58ab12 | 2014-10-22 20:23:02 | [diff] [blame] | 83 | // This may be NULL during testing. |
| 84 | Profile* profile() const { return profile_; } |
| 85 | |
[email protected] | 8ddbb64 | 2011-09-01 02:52:20 | [diff] [blame] | 86 | // Returns true if a new window opening should really be treated like the |
| 87 | // start of a session (with potential session restore, startup URLs, etc.). |
| 88 | // In particular, this is true if there are no tabbed browsers running |
| 89 | // currently (eg. because only background or other app pages are running). |
| 90 | bool ShouldNewWindowStartSession(); |
| 91 | |
[email protected] | c9b1994 | 2010-03-26 15:58:08 | [diff] [blame] | 92 | // Invoke at a point when you think session restore might occur. For example, |
| 93 | // during startup and window creation this is invoked to see if a session |
| 94 | // needs to be restored. If a session needs to be restored it is done so |
| 95 | // asynchronously and true is returned. If false is returned the session was |
Findit | 04679572 | 2018-10-03 11:44:48 | [diff] [blame] | 96 | // not restored and the caller needs to create a new window. |
| 97 | bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open); |
[email protected] | c9b1994 | 2010-03-26 15:58:08 | [diff] [blame] | 98 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // Resets the contents of the file from the current state of all open |
| 100 | // browsers whose profile matches our profile. |
| 101 | void ResetFromCurrentBrowsers(); |
| 102 | |
| 103 | // Moves the current session to the last session. This is useful when a |
| 104 | // checkpoint occurs, such as when the user launches the app and no tabbed |
| 105 | // browsers are running. |
| 106 | void MoveCurrentSessionToLastSession(); |
| 107 | |
skuhne | d1e250b | 2014-11-05 15:26:50 | [diff] [blame] | 108 | // Deletes the last session. |
| 109 | void DeleteLastSession(); |
| 110 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | // Associates a tab with a window. |
| 112 | void SetTabWindow(const SessionID& window_id, |
| 113 | const SessionID& tab_id); |
| 114 | |
| 115 | // Sets the bounds of a window. |
| 116 | void SetWindowBounds(const SessionID& window_id, |
| 117 | const gfx::Rect& bounds, |
[email protected] | 400eaf8 | 2011-08-22 15:47:39 | [diff] [blame] | 118 | ui::WindowShowState show_state); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | |
thomasanderson | 06405c5 | 2016-05-03 22:52:22 | [diff] [blame] | 120 | // Sets the workspace the window resides in. |
| 121 | void SetWindowWorkspace(const SessionID& window_id, |
| 122 | const std::string& workspace); |
| 123 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | // Sets the visual index of the tab in its parent window. |
| 125 | void SetTabIndexInWindow(const SessionID& window_id, |
| 126 | const SessionID& tab_id, |
| 127 | int new_index); |
| 128 | |
Collin Baker | d61e2fe | 2019-08-07 19:19:53 | [diff] [blame] | 129 | // Sets a tab's group ID, if any. Note that a group can't be split between |
| 130 | // multiple windows. |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 131 | void SetTabGroup(const SessionID& window_id, |
| 132 | const SessionID& tab_id, |
Connie Wan | 866179b | 2019-12-18 21:39:33 | [diff] [blame] | 133 | base::Optional<tab_groups::TabGroupId> group); |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 134 | |
Collin Baker | d61e2fe | 2019-08-07 19:19:53 | [diff] [blame] | 135 | // Updates the metadata associated with a tab group. |window_id| should be the |
| 136 | // window where the group currently resides. Note that a group can't be split |
| 137 | // between multiple windows. |
| 138 | void SetTabGroupMetadata(const SessionID& window_id, |
Connie Wan | 866179b | 2019-12-18 21:39:33 | [diff] [blame] | 139 | const tab_groups::TabGroupId& group_id, |
| 140 | const tab_groups::TabGroupVisualData* visual_data); |
Collin Baker | d61e2fe | 2019-08-07 19:19:53 | [diff] [blame] | 141 | |
[email protected] | 5c0e648 | 2009-07-14 20:20:09 | [diff] [blame] | 142 | // Sets the pinned state of the tab. |
| 143 | void SetPinnedState(const SessionID& window_id, |
| 144 | const SessionID& tab_id, |
| 145 | bool is_pinned); |
| 146 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | // Note: this is invoked from the NavigationController's destructor, which is |
| 148 | // after the actual tab has been removed. |
Cliff Smolinsky | b4806261 | 2020-04-14 20:07:34 | [diff] [blame] | 149 | void TabClosed(const SessionID& window_id, const SessionID& tab_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
[email protected] | 7d52ea4c6 | 2013-11-13 03:52:14 | [diff] [blame] | 151 | // Notification a window has opened. |
| 152 | void WindowOpened(Browser* browser); |
| 153 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | // Notification the window is about to close. |
| 155 | void WindowClosing(const SessionID& window_id); |
| 156 | |
| 157 | // Notification a window has finished closing. |
| 158 | void WindowClosed(const SessionID& window_id); |
| 159 | |
[email protected] | 4060015 | 2012-09-26 10:04:31 | [diff] [blame] | 160 | // Called when a tab is inserted. |
| 161 | void TabInserted(content::WebContents* contents); |
| 162 | |
| 163 | // Called when a tab is closing. |
| 164 | void TabClosing(content::WebContents* contents); |
| 165 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | // Sets the type of window. In order for the contents of a window to be |
| 167 | // tracked SetWindowType must be invoked with a type we track |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 168 | // (ShouldRestoreOfWindowType returns true). |
Joel Hockey | 7132db6 | 2019-08-20 23:03:31 | [diff] [blame] | 169 | void SetWindowType(const SessionID& window_id, Browser::Type type); |
[email protected] | a636d8e5 | 2012-02-28 15:40:41 | [diff] [blame] | 170 | |
[email protected] | 18ab515 | 2013-03-28 23:14:05 | [diff] [blame] | 171 | // Sets the application name of the specified window. |
| 172 | void SetWindowAppName(const SessionID& window_id, |
| 173 | const std::string& app_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | // Notification that a tab has restored its entries or a closed tab is being |
| 176 | // reused. |
[email protected] | b624ddc | 2012-11-15 18:04:13 | [diff] [blame] | 177 | void TabRestored(content::WebContents* tab, bool pinned); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | // Sets the index of the selected tab in the specified window. |
| 180 | void SetSelectedTabInWindow(const SessionID& window_id, int index); |
| 181 | |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 182 | // Sets the application extension id of the specified tab. |
| 183 | void SetTabExtensionAppID(const SessionID& window_id, |
| 184 | const SessionID& tab_id, |
| 185 | const std::string& extension_app_id); |
| 186 | |
georgesak | 5582cbe | 2015-05-22 22:08:07 | [diff] [blame] | 187 | // Sets the last active time of the tab. |
| 188 | void SetLastActiveTime(const SessionID& window_id, |
| 189 | const SessionID& tab_id, |
| 190 | base::TimeTicks last_active_time); |
| 191 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | // Fetches the contents of the last session, notifying the callback when |
| 193 | // done. If the callback is supplied an empty vector of SessionWindows |
| 194 | // it means the session could not be restored. |
[email protected] | e95b717f | 2014-02-06 13:47:13 | [diff] [blame] | 195 | base::CancelableTaskTracker::TaskId GetLastSession( |
Yuzu Saijo | 543f8e04 | 2020-01-08 07:00:14 | [diff] [blame] | 196 | sessions::GetLastSessionCallback callback, |
[email protected] | e95b717f | 2014-02-06 13:47:13 | [diff] [blame] | 197 | base::CancelableTaskTracker* tracker); |
[email protected] | 3acb70ef | 2010-03-01 18:44:38 | [diff] [blame] | 198 | |
Scott Violet | cae997b5 | 2020-01-17 16:47:44 | [diff] [blame] | 199 | // CommandStorageManagerDelegate: |
blundell | 6d34c34 | 2015-09-11 09:09:10 | [diff] [blame] | 200 | bool ShouldUseDelayedSave() override; |
Christian Dullweber | b13ffda | 2018-02-23 11:42:14 | [diff] [blame] | 201 | void OnWillSaveCommands() override; |
[email protected] | 70eb8ed9 | 2010-06-10 18:13:12 | [diff] [blame] | 202 | |
Scott Violet | 13d44fd | 2020-01-14 20:38:33 | [diff] [blame] | 203 | // sessions::SessionTabHelperDelegate: |
| 204 | void SetTabUserAgentOverride(const SessionID& window_id, |
| 205 | const SessionID& tab_id, |
Maks Orlovich | 30312696 | 2020-04-06 21:34:25 | [diff] [blame] | 206 | const sessions::SerializedUserAgentOverride& |
| 207 | user_agent_override) override; |
Scott Violet | 13d44fd | 2020-01-14 20:38:33 | [diff] [blame] | 208 | void SetSelectedNavigationIndex(const SessionID& window_id, |
| 209 | const SessionID& tab_id, |
| 210 | int index) override; |
| 211 | void UpdateTabNavigation( |
| 212 | const SessionID& window_id, |
| 213 | const SessionID& tab_id, |
| 214 | const sessions::SerializedNavigationEntry& navigation) override; |
| 215 | void TabNavigationPathPruned(const SessionID& window_id, |
| 216 | const SessionID& tab_id, |
| 217 | int index, |
| 218 | int count) override; |
| 219 | void TabNavigationPathEntriesDeleted(const SessionID& window_id, |
| 220 | const SessionID& tab_id) override; |
| 221 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | private: |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 223 | // Allow tests to access our innards for testing purposes. |
skuhne | d1e250b | 2014-11-05 15:26:50 | [diff] [blame] | 224 | FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, SavedSessionNotification); |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 225 | FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation1); |
| 226 | FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RestoreActivation2); |
aseren | 748ab62e | 2015-01-12 13:33:02 | [diff] [blame] | 227 | FRIEND_TEST_ALL_PREFIXES(SessionServiceTest, RemoveUnusedRestoreWindowsTest); |
[email protected] | 51e5bdc | 2012-12-06 06:12:56 | [diff] [blame] | 228 | FRIEND_TEST_ALL_PREFIXES(NoStartupWindowTest, DontInitSessionServiceForApps); |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 229 | |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 230 | typedef std::map<SessionID, std::pair<int, int>> IdToRange; |
[email protected] | 97d2c1e | 2009-11-05 03:55:05 | [diff] [blame] | 231 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 232 | void Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | |
Joel Hockey | 7132db6 | 2019-08-20 23:03:31 | [diff] [blame] | 234 | // Returns true if a window of given |window_type| should get |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 235 | // restored upon session restore. |
Joel Hockey | 7132db6 | 2019-08-20 23:03:31 | [diff] [blame] | 236 | bool ShouldRestoreWindowOfType( |
| 237 | sessions::SessionWindow::WindowType type) const; |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 238 | |
| 239 | // Removes unrestorable windows from the previous windows list. |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 240 | void RemoveUnusedRestoreWindows( |
avi | 498cabca | 2016-09-21 19:03:50 | [diff] [blame] | 241 | std::vector<std::unique_ptr<sessions::SessionWindow>>* window_list); |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 242 | |
[email protected] | c9b1994 | 2010-03-26 15:58:08 | [diff] [blame] | 243 | // Implementation of RestoreIfNecessary. If |browser| is non-null and we need |
| 244 | // to restore, the tabs are added to it, otherwise a new browser is created. |
Findit | 04679572 | 2018-10-03 11:44:48 | [diff] [blame] | 245 | bool RestoreIfNecessary(const std::vector<GURL>& urls_to_open, |
[email protected] | c9b1994 | 2010-03-26 15:58:08 | [diff] [blame] | 246 | Browser* browser); |
| 247 | |
cm.sanchi | a0d6add | 2017-12-13 04:59:33 | [diff] [blame] | 248 | // BrowserListObserver |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 249 | void OnBrowserAdded(Browser* browser) override {} |
| 250 | void OnBrowserRemoved(Browser* browser) override {} |
| 251 | void OnBrowserSetLastActive(Browser* browser) override; |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 252 | |
[email protected] | 4c7f9c5 | 2012-12-11 21:51:30 | [diff] [blame] | 253 | // Converts |commands| to SessionWindows and notifies the callback. |
avi | 51f1cd9 | 2017-01-03 18:23:38 | [diff] [blame] | 254 | void OnGotSessionCommands( |
Yuzu Saijo | 543f8e04 | 2020-01-08 07:00:14 | [diff] [blame] | 255 | sessions::GetLastSessionCallback callback, |
avi | 51f1cd9 | 2017-01-03 18:23:38 | [diff] [blame] | 256 | std::vector<std::unique_ptr<sessions::SessionCommand>> commands); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | // Adds commands to commands that will recreate the state of the specified |
[email protected] | 8189899 | 2011-06-14 22:15:00 | [diff] [blame] | 259 | // tab. This adds at most kMaxNavigationCountToPersist navigations (in each |
| 260 | // direction from the current navigation index). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | // A pair is added to tab_to_available_range indicating the range of |
| 262 | // indices that were written. |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 263 | void BuildCommandsForTab(const SessionID& window_id, |
| 264 | content::WebContents* tab, |
| 265 | int index_in_window, |
Connie Wan | 866179b | 2019-12-18 21:39:33 | [diff] [blame] | 266 | base::Optional<tab_groups::TabGroupId> group, |
Collin Baker | 81999fd | 2019-06-05 23:12:27 | [diff] [blame] | 267 | bool is_pinned, |
| 268 | IdToRange* tab_to_available_range); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | |
| 270 | // Adds commands to create the specified browser, and invokes |
| 271 | // BuildCommandsForTab for each of the tabs in the browser. This ignores |
| 272 | // any tabs not in the profile we were created with. |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 273 | void BuildCommandsForBrowser(Browser* browser, |
| 274 | IdToRange* tab_to_available_range, |
| 275 | std::set<SessionID>* windows_to_track); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | |
| 277 | // Iterates over all the known browsers invoking BuildCommandsForBrowser. |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 278 | // This only adds browsers that should be tracked (|ShouldRestoreWindowOfType| |
| 279 | // returns true). All browsers that are tracked are added to windows_to_track |
| 280 | // (as long as it is non-null). |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 281 | void BuildCommandsFromBrowsers(IdToRange* tab_to_available_range, |
| 282 | std::set<SessionID>* windows_to_track); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 284 | // Schedules a reset of the existing commands. A reset means the contents |
| 285 | // of the file are recreated from the state of the browser. |
| 286 | void ScheduleResetCommands(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 287 | |
skuhne | 57b5b03f | 2014-10-31 03:59:55 | [diff] [blame] | 288 | // Schedules the specified command. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 289 | void ScheduleCommand(std::unique_ptr<sessions::SessionCommand> command); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | |
| 291 | // Converts all pending tab/window closes to commands and schedules them. |
| 292 | void CommitPendingCloses(); |
| 293 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | // Returns true if there is only one window open with a single tab that shares |
| 295 | // our profile. |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 296 | bool IsOnlyOneTabLeft() const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 298 | // Returns true if there are open trackable browser windows whose ids do |
| 299 | // match |window_id| with our profile. A trackable window is a window from |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 300 | // which |ShouldRestoreWindowOfType| returns true. See |
| 301 | // |ShouldRestoreWindowOfType| for details. |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 302 | bool HasOpenTrackableBrowsers(const SessionID& window_id) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | |
| 304 | // Returns true if changes to tabs in the specified window should be tracked. |
[email protected] | bf219e9eb | 2012-10-10 15:20:38 | [diff] [blame] | 305 | bool ShouldTrackChangesToWindow(const SessionID& window_id) const; |
| 306 | |
| 307 | // Returns true if we track changes to the specified browser. |
| 308 | bool ShouldTrackBrowser(Browser* browser) const; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 309 | |
Christian Dullweber | b13ffda | 2018-02-23 11:42:14 | [diff] [blame] | 310 | // Will rebuild session commands if rebuild_on_next_save_ is true. |
| 311 | void RebuildCommandsIfRequired(); |
| 312 | |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 313 | // Call when certain session relevant notifications |
| 314 | // (tab_closed, nav_list_pruned) occur. In addition, this is |
| 315 | // currently called when Save() is called to compare how often the |
| 316 | // session data is currently saved verses when we may want to save it. |
| 317 | // It records the data in UMA stats. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 318 | void RecordSessionUpdateHistogramData(int type, |
[email protected] | 23da84e15 | 2010-07-14 01:29:05 | [diff] [blame] | 319 | base::TimeTicks* last_updated_time); |
| 320 | |
[email protected] | 3efee417 | 2014-01-23 13:53:36 | [diff] [blame] | 321 | // Deletes session data if no windows are open for the current profile. |
| 322 | void MaybeDeleteSessionOnlyData(); |
| 323 | |
skuhne | d1e250b | 2014-11-05 15:26:50 | [diff] [blame] | 324 | // Unit test accessors. |
Scott Violet | cae997b5 | 2020-01-17 16:47:44 | [diff] [blame] | 325 | sessions::CommandStorageManager* GetCommandStorageManagerForTest(); |
skuhne | d1e250b | 2014-11-05 15:26:50 | [diff] [blame] | 326 | |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 327 | void SetAvailableRangeForTest(const SessionID& tab_id, |
Shivani Sharma | 0707459 | 2019-02-12 16:36:28 | [diff] [blame] | 328 | const std::pair<int, int>& range); |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 329 | bool GetAvailableRangeForTest(const SessionID& tab_id, |
Shivani Sharma | 0707459 | 2019-02-12 16:36:28 | [diff] [blame] | 330 | std::pair<int, int>* range); |
Shivani Sharma | b9c46de8 | 2019-02-08 16:54:50 | [diff] [blame] | 331 | |
skuhne | d58ab12 | 2014-10-22 20:23:02 | [diff] [blame] | 332 | // The profile. This may be null during testing. |
| 333 | Profile* profile_; |
| 334 | |
blundell | 6d34c34 | 2015-09-11 09:09:10 | [diff] [blame] | 335 | // Whether to use delayed save. Set to false when constructed with a FilePath |
| 336 | // (which should only be used for testing). |
| 337 | bool should_use_delayed_save_; |
| 338 | |
Scott Violet | 8e3a335d | 2020-01-17 20:37:38 | [diff] [blame] | 339 | std::unique_ptr<sessions::SnapshottingCommandStorageManager> |
| 340 | command_storage_manager_; |
skuhne | d1e250b | 2014-11-05 15:26:50 | [diff] [blame] | 341 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 342 | // Maps from session tab id to the range of navigation entries that has |
| 343 | // been written to disk. |
| 344 | // |
| 345 | // This is only used if not all the navigation entries have been |
| 346 | // written. |
| 347 | IdToRange tab_to_available_range_; |
| 348 | |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 349 | // When the user closes the last window, where the last window is the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | // last tabbed browser and no more tabbed browsers are open with the same |
| 351 | // profile, the window ID is added here. These IDs are only committed (which |
| 352 | // marks them as closed) if the user creates a new tabbed browser. |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 353 | typedef std::set<SessionID> PendingWindowCloseIDs; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | PendingWindowCloseIDs pending_window_close_ids_; |
| 355 | |
| 356 | // Set of tabs that have been closed by way of the last window or last tab |
| 357 | // closing, but not yet committed. |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 358 | typedef std::set<SessionID> PendingTabCloseIDs; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | PendingTabCloseIDs pending_tab_close_ids_; |
| 360 | |
| 361 | // When a window other than the last window (see description of |
| 362 | // pending_window_close_ids) is closed, the id is added to this set. |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 363 | typedef std::set<SessionID> WindowClosingIDs; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | WindowClosingIDs window_closing_ids_; |
| 365 | |
| 366 | // Set of windows we're tracking changes to. This is only browsers that |
skuhne | c3aa8cf | 2014-10-30 06:02:45 | [diff] [blame] | 367 | // return true from |ShouldRestoreWindowOfType|. |
Mikel Astiz | 69a7588 | 2018-03-29 19:00:09 | [diff] [blame] | 368 | typedef std::set<SessionID> WindowsTracking; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 369 | WindowsTracking windows_tracking_; |
| 370 | |
[email protected] | 982921f1 | 2009-10-27 21:43:53 | [diff] [blame] | 371 | // Are there any open trackable browsers? |
| 372 | bool has_open_trackable_browsers_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | |
Cliff Smolinsky | b4806261 | 2020-04-14 20:07:34 | [diff] [blame] | 374 | // Used to override HasOpenTrackableBrowsers() |
| 375 | bool has_open_trackable_browser_for_test_ = true; |
| 376 | |
| 377 | // Use to override IsOnlyOneTableft() |
| 378 | bool is_only_one_tab_left_for_test_ = false; |
| 379 | |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 380 | // 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] | 381 | // browser (has_open_trackable_browsers_ is false), then the current session |
[email protected] | 668cd42 | 2011-04-06 21:00:01 | [diff] [blame] | 382 | // is made the last session. See description above class for details on |
| 383 | // current/last session. |
[email protected] | 6ea265a | 2008-10-30 02:58:36 | [diff] [blame] | 384 | bool move_on_new_browser_; |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 385 | |
[email protected] | c4340bc0 | 2012-04-25 02:49:38 | [diff] [blame] | 386 | // For browser_tests, since we want to simulate the browser shutting down |
| 387 | // without quitting. |
| 388 | bool force_browser_not_alive_with_no_windows_; |
| 389 | |
Christian Dullweber | b13ffda | 2018-02-23 11:42:14 | [diff] [blame] | 390 | // Force session commands to be rebuild before next save event. |
| 391 | bool rebuild_on_next_save_; |
| 392 | |
erikchen | f64c33d6 | 2018-05-14 23:39:33 | [diff] [blame] | 393 | // Don't send duplicate SetSelectedTabInWindow commands when the selected |
| 394 | // tab's index hasn't changed. |
| 395 | std::map<SessionID, int> last_selected_tab_in_window_; |
| 396 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 397 | base::WeakPtrFactory<SessionService> weak_factory_{this}; |
sky | 1a14f49 | 2014-08-26 21:44:26 | [diff] [blame] | 398 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 399 | DISALLOW_COPY_AND_ASSIGN(SessionService); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | }; |
| 401 | |
[email protected] | 169627b | 2008-12-06 19:30:19 | [diff] [blame] | 402 | #endif // CHROME_BROWSER_SESSIONS_SESSION_SERVICE_H_ |