OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/sessions/session_service.h" | 7 #include "chrome/browser/sessions/session_service.h" |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 SessionID::id_type tab_id; | 112 SessionID::id_type tab_id; |
113 bool pinned_state; | 113 bool pinned_state; |
114 }; | 114 }; |
115 | 115 |
116 } // namespace | 116 } // namespace |
117 | 117 |
118 // SessionService ------------------------------------------------------------- | 118 // SessionService ------------------------------------------------------------- |
119 | 119 |
120 SessionService::SessionService(Profile* profile) | 120 SessionService::SessionService(Profile* profile) |
121 : BaseSessionService(SESSION_RESTORE, profile, FilePath()), | 121 : BaseSessionService(SESSION_RESTORE, profile, FilePath()), |
122 has_open_tabbed_browsers_(false), | 122 has_open_trackable_browsers_(false), |
123 move_on_new_browser_(false) { | 123 move_on_new_browser_(false) { |
124 Init(); | 124 Init(); |
125 } | 125 } |
126 | 126 |
127 SessionService::SessionService(const FilePath& save_path) | 127 SessionService::SessionService(const FilePath& save_path) |
128 : BaseSessionService(SESSION_RESTORE, NULL, save_path), | 128 : BaseSessionService(SESSION_RESTORE, NULL, save_path), |
129 has_open_tabbed_browsers_(false), | 129 has_open_trackable_browsers_(false), |
130 move_on_new_browser_(false) { | 130 move_on_new_browser_(false) { |
131 Init(); | 131 Init(); |
132 } | 132 } |
133 | 133 |
134 SessionService::~SessionService() { | 134 SessionService::~SessionService() { |
135 Save(); | 135 Save(); |
136 } | 136 } |
137 | 137 |
138 void SessionService::ResetFromCurrentBrowsers() { | 138 void SessionService::ResetFromCurrentBrowsers() { |
139 ScheduleReset(); | 139 ScheduleReset(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 window_id.id()) != window_closing_ids_.end() || | 209 window_id.id()) != window_closing_ids_.end() || |
210 !IsOnlyOneTabLeft()) { | 210 !IsOnlyOneTabLeft()) { |
211 // Tab closure is the result of a window close (and it isn't the last | 211 // Tab closure is the result of a window close (and it isn't the last |
212 // window), or closing a tab and there are other windows/tabs open. Mark the | 212 // window), or closing a tab and there are other windows/tabs open. Mark the |
213 // tab as closed. | 213 // tab as closed. |
214 ScheduleCommand(CreateTabClosedCommand(tab_id.id())); | 214 ScheduleCommand(CreateTabClosedCommand(tab_id.id())); |
215 } else { | 215 } else { |
216 // User closed the last tab in the last tabbed browser. Don't mark the | 216 // User closed the last tab in the last tabbed browser. Don't mark the |
217 // tab closed. | 217 // tab closed. |
218 pending_tab_close_ids_.insert(tab_id.id()); | 218 pending_tab_close_ids_.insert(tab_id.id()); |
219 has_open_tabbed_browsers_ = false; | 219 has_open_trackable_browsers_ = false; |
220 } | 220 } |
221 } | 221 } |
222 | 222 |
223 void SessionService::WindowClosing(const SessionID& window_id) { | 223 void SessionService::WindowClosing(const SessionID& window_id) { |
224 if (!ShouldTrackChangesToWindow(window_id)) | 224 if (!ShouldTrackChangesToWindow(window_id)) |
225 return; | 225 return; |
226 | 226 |
227 // The window is about to close. If there are other tabbed browsers with the | 227 // The window is about to close. If there are other tabbed browsers with the |
228 // same original profile commit the close immediately. | 228 // same original profile commit the close immediately. |
229 // | 229 // |
230 // NOTE: if the user chooses the exit menu item session service is destroyed | 230 // NOTE: if the user chooses the exit menu item session service is destroyed |
231 // and this code isn't hit. | 231 // and this code isn't hit. |
232 if (has_open_tabbed_browsers_) { | 232 if (has_open_trackable_browsers_) { |
233 // Closing a window can never make has_open_tabbed_browsers_ go from false | 233 // Closing a window can never make has_open_trackable_browsers_ go from |
234 // to true, so only update it if already true. | 234 // false to true, so only update it if already true. |
235 has_open_tabbed_browsers_ = HasOpenTabbedBrowsers(window_id); | 235 has_open_trackable_browsers_ = HasOpenTrackableBrowsers(window_id); |
236 } | 236 } |
237 if (!has_open_tabbed_browsers_) | 237 if (!has_open_trackable_browsers_) |
238 pending_window_close_ids_.insert(window_id.id()); | 238 pending_window_close_ids_.insert(window_id.id()); |
239 else | 239 else |
240 window_closing_ids_.insert(window_id.id()); | 240 window_closing_ids_.insert(window_id.id()); |
241 } | 241 } |
242 | 242 |
243 void SessionService::WindowClosed(const SessionID& window_id) { | 243 void SessionService::WindowClosed(const SessionID& window_id) { |
244 if (!ShouldTrackChangesToWindow(window_id)) | 244 if (!ShouldTrackChangesToWindow(window_id)) |
245 return; | 245 return; |
246 | 246 |
247 windows_tracking_.erase(window_id.id()); | 247 windows_tracking_.erase(window_id.id()); |
248 | 248 |
249 if (window_closing_ids_.find(window_id.id()) != window_closing_ids_.end()) { | 249 if (window_closing_ids_.find(window_id.id()) != window_closing_ids_.end()) { |
250 window_closing_ids_.erase(window_id.id()); | 250 window_closing_ids_.erase(window_id.id()); |
251 ScheduleCommand(CreateWindowClosedCommand(window_id.id())); | 251 ScheduleCommand(CreateWindowClosedCommand(window_id.id())); |
252 } else if (pending_window_close_ids_.find(window_id.id()) == | 252 } else if (pending_window_close_ids_.find(window_id.id()) == |
253 pending_window_close_ids_.end()) { | 253 pending_window_close_ids_.end()) { |
254 // We'll hit this if user closed the last tab in a window. | 254 // We'll hit this if user closed the last tab in a window. |
255 has_open_tabbed_browsers_ = HasOpenTabbedBrowsers(window_id); | 255 has_open_trackable_browsers_ = HasOpenTrackableBrowsers(window_id); |
256 if (!has_open_tabbed_browsers_) | 256 if (!has_open_trackable_browsers_) |
257 pending_window_close_ids_.insert(window_id.id()); | 257 pending_window_close_ids_.insert(window_id.id()); |
258 else | 258 else |
259 ScheduleCommand(CreateWindowClosedCommand(window_id.id())); | 259 ScheduleCommand(CreateWindowClosedCommand(window_id.id())); |
260 } | 260 } |
261 } | 261 } |
262 | 262 |
263 void SessionService::SetWindowType(const SessionID& window_id, | 263 void SessionService::SetWindowType(const SessionID& window_id, |
264 Browser::Type type) { | 264 Browser::Type type) { |
265 if (!should_track_changes_for_browser_type(type)) | 265 if (!should_track_changes_for_browser_type(type)) |
266 return; | 266 return; |
267 | 267 |
268 windows_tracking_.insert(window_id.id()); | 268 windows_tracking_.insert(window_id.id()); |
269 | 269 |
270 // The user created a new tabbed browser with our profile. Commit any | 270 // The user created a new tabbed browser with our profile. Commit any |
271 // pending closes. | 271 // pending closes. |
272 CommitPendingCloses(); | 272 CommitPendingCloses(); |
273 | 273 |
274 has_open_tabbed_browsers_ = true; | 274 has_open_trackable_browsers_ = true; |
275 move_on_new_browser_ = true; | 275 move_on_new_browser_ = true; |
276 | 276 |
277 ScheduleCommand(CreateSetWindowTypeCommand(window_id, type)); | 277 ScheduleCommand(CreateSetWindowTypeCommand(window_id, type)); |
278 } | 278 } |
279 | 279 |
280 void SessionService::TabNavigationPathPrunedFromBack(const SessionID& window_id, | 280 void SessionService::TabNavigationPathPrunedFromBack(const SessionID& window_id, |
281 const SessionID& tab_id, | 281 const SessionID& tab_id, |
282 int count) { | 282 int count) { |
283 if (!ShouldTrackChangesToWindow(window_id)) | 283 if (!ShouldTrackChangesToWindow(window_id)) |
284 return; | 284 return; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 const NotificationDetails& details) { | 402 const NotificationDetails& details) { |
403 // All of our messages have the NavigationController as the source. | 403 // All of our messages have the NavigationController as the source. |
404 switch (type.value) { | 404 switch (type.value) { |
405 case NotificationType::BROWSER_OPENED: { | 405 case NotificationType::BROWSER_OPENED: { |
406 Browser* browser = Source<Browser>(source).ptr(); | 406 Browser* browser = Source<Browser>(source).ptr(); |
407 if (browser->profile() != profile() || | 407 if (browser->profile() != profile() || |
408 !should_track_changes_for_browser_type(browser->type())) { | 408 !should_track_changes_for_browser_type(browser->type())) { |
409 return; | 409 return; |
410 } | 410 } |
411 | 411 |
412 if (!has_open_tabbed_browsers_ && !BrowserInit::InProcessStartup()) { | 412 if (!has_open_trackable_browsers_ && !BrowserInit::InProcessStartup()) { |
413 // We're going from no tabbed browsers to a tabbed browser (and not in | 413 // We're going from no tabbed browsers to a tabbed browser (and not in |
414 // process startup), restore the last session. | 414 // process startup), restore the last session. |
415 if (move_on_new_browser_) { | 415 if (move_on_new_browser_) { |
416 // Make the current session the last. | 416 // Make the current session the last. |
417 MoveCurrentSessionToLastSession(); | 417 MoveCurrentSessionToLastSession(); |
418 move_on_new_browser_ = false; | 418 move_on_new_browser_ = false; |
419 } | 419 } |
420 SessionStartupPref pref = SessionStartupPref::GetStartupPref(profile()); | 420 SessionStartupPref pref = SessionStartupPref::GetStartupPref(profile()); |
421 if (pref.type == SessionStartupPref::LAST) { | 421 if (pref.type == SessionStartupPref::LAST) { |
422 SessionRestore::RestoreSession( | 422 SessionRestore::RestoreSession( |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 void SessionService::ScheduleReset() { | 1012 void SessionService::ScheduleReset() { |
1013 set_pending_reset(true); | 1013 set_pending_reset(true); |
1014 STLDeleteElements(&pending_commands()); | 1014 STLDeleteElements(&pending_commands()); |
1015 tab_to_available_range_.clear(); | 1015 tab_to_available_range_.clear(); |
1016 windows_tracking_.clear(); | 1016 windows_tracking_.clear(); |
1017 BuildCommandsFromBrowsers(&pending_commands(), &tab_to_available_range_, | 1017 BuildCommandsFromBrowsers(&pending_commands(), &tab_to_available_range_, |
1018 &windows_tracking_); | 1018 &windows_tracking_); |
1019 if (!windows_tracking_.empty()) { | 1019 if (!windows_tracking_.empty()) { |
1020 // We're lazily created on startup and won't get an initial batch of | 1020 // We're lazily created on startup and won't get an initial batch of |
1021 // SetWindowType messages. Set these here to make sure our state is correct. | 1021 // SetWindowType messages. Set these here to make sure our state is correct. |
1022 has_open_tabbed_browsers_ = true; | 1022 has_open_trackable_browsers_ = true; |
1023 move_on_new_browser_ = true; | 1023 move_on_new_browser_ = true; |
1024 } | 1024 } |
1025 StartSaveTimer(); | 1025 StartSaveTimer(); |
1026 } | 1026 } |
1027 | 1027 |
1028 bool SessionService::ReplacePendingCommand(SessionCommand* command) { | 1028 bool SessionService::ReplacePendingCommand(SessionCommand* command) { |
1029 // We only optimize page navigations, which can happen quite frequently and | 1029 // We only optimize page navigations, which can happen quite frequently and |
1030 // are expensive. If necessary, other commands could be searched for as | 1030 // are expensive. If necessary, other commands could be searched for as |
1031 // well. | 1031 // well. |
1032 if (command->id() != kCommandUpdateTabNavigation) | 1032 if (command->id() != kCommandUpdateTabNavigation) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1101 pending_window_close_ids_.clear(); | 1101 pending_window_close_ids_.clear(); |
1102 } | 1102 } |
1103 | 1103 |
1104 bool SessionService::IsOnlyOneTabLeft() { | 1104 bool SessionService::IsOnlyOneTabLeft() { |
1105 if (!profile()) { | 1105 if (!profile()) { |
1106 // We're testing, always return false. | 1106 // We're testing, always return false. |
1107 return false; | 1107 return false; |
1108 } | 1108 } |
1109 | 1109 |
1110 // NOTE: This uses the original profile so that closing the last non-off the | 1110 // NOTE: This uses the original profile so that closing the last non-off the |
1111 // record window while there are open off the record window resets state). | 1111 // record window while there are open off the record windows resets state). |
1112 int window_count = 0; | 1112 int window_count = 0; |
1113 for (BrowserList::const_iterator i = BrowserList::begin(); | 1113 for (BrowserList::const_iterator i = BrowserList::begin(); |
1114 i != BrowserList::end(); ++i) { | 1114 i != BrowserList::end(); ++i) { |
1115 const SessionID::id_type window_id = (*i)->session_id().id(); | 1115 const SessionID::id_type window_id = (*i)->session_id().id(); |
1116 if (should_track_changes_for_browser_type((*i)->type()) && | 1116 if (should_track_changes_for_browser_type((*i)->type()) && |
1117 (*i)->profile()->GetOriginalProfile() == profile() && | 1117 (*i)->profile()->GetOriginalProfile() == profile() && |
1118 window_closing_ids_.find(window_id) == window_closing_ids_.end()) { | 1118 window_closing_ids_.find(window_id) == window_closing_ids_.end()) { |
1119 if (++window_count > 1) | 1119 if (++window_count > 1) |
1120 return false; | 1120 return false; |
1121 // By the time this is invoked the tab has been removed. As such, we use | 1121 // By the time this is invoked the tab has been removed. As such, we use |
1122 // > 0 here rather than > 1. | 1122 // > 0 here rather than > 1. |
1123 if ((*i)->tab_count() > 0) | 1123 if ((*i)->tab_count() > 0) |
1124 return false; | 1124 return false; |
1125 } | 1125 } |
1126 } | 1126 } |
1127 return true; | 1127 return true; |
1128 } | 1128 } |
1129 | 1129 |
1130 bool SessionService::HasOpenTabbedBrowsers(const SessionID& window_id) { | 1130 bool SessionService::HasOpenTrackableBrowsers(const SessionID& window_id) { |
1131 if (!profile()) { | 1131 if (!profile()) { |
1132 // We're testing, always return false. | 1132 // We're testing, always return false. |
1133 return true; | 1133 return true; |
1134 } | 1134 } |
1135 | 1135 |
1136 // NOTE: This uses the original profile so that closing the last non-off the | 1136 // NOTE: This uses the original profile so that closing the last non-off the |
1137 // record window while there are open off the record window resets state). | 1137 // record window while there are open off the record window resets state). |
1138 for (BrowserList::const_iterator i = BrowserList::begin(); | 1138 for (BrowserList::const_iterator i = BrowserList::begin(); |
1139 i != BrowserList::end(); ++i) { | 1139 i != BrowserList::end(); ++i) { |
1140 Browser* browser = *i; | 1140 Browser* browser = *i; |
1141 const SessionID::id_type browser_id = browser->session_id().id(); | 1141 const SessionID::id_type browser_id = browser->session_id().id(); |
1142 if (browser_id != window_id.id() && | 1142 if (browser_id != window_id.id() && |
1143 window_closing_ids_.find(browser_id) == window_closing_ids_.end() && | 1143 window_closing_ids_.find(browser_id) == window_closing_ids_.end() && |
1144 should_track_changes_for_browser_type(browser->type()) && | 1144 should_track_changes_for_browser_type(browser->type()) && |
1145 browser->profile()->GetOriginalProfile() == profile()) { | 1145 browser->profile()->GetOriginalProfile() == profile()) { |
1146 return true; | 1146 return true; |
1147 } | 1147 } |
1148 } | 1148 } |
1149 return false; | 1149 return false; |
1150 } | 1150 } |
1151 | 1151 |
1152 bool SessionService::ShouldTrackChangesToWindow(const SessionID& window_id) { | 1152 bool SessionService::ShouldTrackChangesToWindow(const SessionID& window_id) { |
1153 return windows_tracking_.find(window_id.id()) != windows_tracking_.end(); | 1153 return windows_tracking_.find(window_id.id()) != windows_tracking_.end(); |
1154 } | 1154 } |
OLD | NEW |