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 #include "chrome/browser/sessions/session_service.h" | 5 #include "chrome/browser/sessions/session_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "chrome/browser/defaults.h" | 25 #include "chrome/browser/defaults.h" |
26 #include "chrome/browser/extensions/tab_helper.h" | 26 #include "chrome/browser/extensions/tab_helper.h" |
27 #include "chrome/browser/prefs/session_startup_pref.h" | 27 #include "chrome/browser/prefs/session_startup_pref.h" |
28 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
29 #include "chrome/browser/profiles/profile_manager.h" | 29 #include "chrome/browser/profiles/profile_manager.h" |
30 #include "chrome/browser/sessions/session_common_utils.h" | 30 #include "chrome/browser/sessions/session_common_utils.h" |
31 #include "chrome/browser/sessions/session_data_deleter.h" | 31 #include "chrome/browser/sessions/session_data_deleter.h" |
32 #include "chrome/browser/sessions/session_restore.h" | 32 #include "chrome/browser/sessions/session_restore.h" |
33 #include "chrome/browser/sessions/session_service_utils.h" | 33 #include "chrome/browser/sessions/session_service_utils.h" |
34 #include "chrome/browser/sessions/session_tab_helper.h" | 34 #include "chrome/browser/sessions/session_tab_helper.h" |
35 #include "chrome/browser/ui/browser_iterator.h" | |
36 #include "chrome/browser/ui/browser_list.h" | 35 #include "chrome/browser/ui/browser_list.h" |
37 #include "chrome/browser/ui/browser_tabstrip.h" | 36 #include "chrome/browser/ui/browser_tabstrip.h" |
38 #include "chrome/browser/ui/browser_window.h" | 37 #include "chrome/browser/ui/browser_window.h" |
39 #include "chrome/browser/ui/host_desktop.h" | 38 #include "chrome/browser/ui/host_desktop.h" |
40 #include "chrome/browser/ui/startup/startup_browser_creator.h" | 39 #include "chrome/browser/ui/startup/startup_browser_creator.h" |
41 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 40 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
42 #include "components/sessions/content/content_serialized_navigation_builder.h" | 41 #include "components/sessions/content/content_serialized_navigation_builder.h" |
43 #include "components/sessions/core/session_command.h" | 42 #include "components/sessions/core/session_command.h" |
44 #include "components/sessions/core/session_constants.h" | 43 #include "components/sessions/core/session_constants.h" |
45 #include "components/sessions/core/session_types.h" | 44 #include "components/sessions/core/session_types.h" |
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 | 831 |
833 base_session_service_->AppendRebuildCommand( | 832 base_session_service_->AppendRebuildCommand( |
834 sessions::CreateSetSelectedTabInWindowCommand( | 833 sessions::CreateSetSelectedTabInWindowCommand( |
835 browser->session_id(), | 834 browser->session_id(), |
836 browser->tab_strip_model()->active_index())); | 835 browser->tab_strip_model()->active_index())); |
837 } | 836 } |
838 | 837 |
839 void SessionService::BuildCommandsFromBrowsers( | 838 void SessionService::BuildCommandsFromBrowsers( |
840 IdToRange* tab_to_available_range, | 839 IdToRange* tab_to_available_range, |
841 std::set<SessionID::id_type>* windows_to_track) { | 840 std::set<SessionID::id_type>* windows_to_track) { |
842 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 841 for (auto* browser : *BrowserList::GetInstance()) { |
843 Browser* browser = *it; | |
844 // Make sure the browser has tabs and a window. Browser's destructor | 842 // Make sure the browser has tabs and a window. Browser's destructor |
845 // removes itself from the BrowserList. When a browser is closed the | 843 // removes itself from the BrowserList. When a browser is closed the |
846 // destructor is not necessarily run immediately. This means it's possible | 844 // destructor is not necessarily run immediately. This means it's possible |
847 // for us to get a handle to a browser that is about to be removed. If | 845 // for us to get a handle to a browser that is about to be removed. If |
848 // the tab count is 0 or the window is NULL, the browser is about to be | 846 // the tab count is 0 or the window is NULL, the browser is about to be |
849 // deleted, so we ignore it. | 847 // deleted, so we ignore it. |
850 if (ShouldTrackBrowser(browser) && browser->tab_strip_model()->count() && | 848 if (ShouldTrackBrowser(browser) && browser->tab_strip_model()->count() && |
851 browser->window()) { | 849 browser->window()) { |
852 BuildCommandsForBrowser(browser, | 850 BuildCommandsForBrowser(browser, |
853 tab_to_available_range, | 851 tab_to_available_range, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 pending_window_close_ids_.clear(); | 901 pending_window_close_ids_.clear(); |
904 } | 902 } |
905 | 903 |
906 bool SessionService::IsOnlyOneTabLeft() const { | 904 bool SessionService::IsOnlyOneTabLeft() const { |
907 if (!profile() || profile()->AsTestingProfile()) { | 905 if (!profile() || profile()->AsTestingProfile()) { |
908 // We're testing, always return false. | 906 // We're testing, always return false. |
909 return false; | 907 return false; |
910 } | 908 } |
911 | 909 |
912 int window_count = 0; | 910 int window_count = 0; |
913 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 911 for (auto* browser : *BrowserList::GetInstance()) { |
914 Browser* browser = *it; | |
915 const SessionID::id_type window_id = browser->session_id().id(); | 912 const SessionID::id_type window_id = browser->session_id().id(); |
916 if (ShouldTrackBrowser(browser) && | 913 if (ShouldTrackBrowser(browser) && |
917 window_closing_ids_.find(window_id) == window_closing_ids_.end()) { | 914 window_closing_ids_.find(window_id) == window_closing_ids_.end()) { |
918 if (++window_count > 1) | 915 if (++window_count > 1) |
919 return false; | 916 return false; |
920 // By the time this is invoked the tab has been removed. As such, we use | 917 // By the time this is invoked the tab has been removed. As such, we use |
921 // > 0 here rather than > 1. | 918 // > 0 here rather than > 1. |
922 if (browser->tab_strip_model()->count() > 0) | 919 if (browser->tab_strip_model()->count() > 0) |
923 return false; | 920 return false; |
924 } | 921 } |
925 } | 922 } |
926 return true; | 923 return true; |
927 } | 924 } |
928 | 925 |
929 bool SessionService::HasOpenTrackableBrowsers( | 926 bool SessionService::HasOpenTrackableBrowsers( |
930 const SessionID& window_id) const { | 927 const SessionID& window_id) const { |
931 if (!profile() || profile()->AsTestingProfile()) { | 928 if (!profile() || profile()->AsTestingProfile()) { |
932 // We're testing, always return true. | 929 // We're testing, always return true. |
933 return true; | 930 return true; |
934 } | 931 } |
935 | 932 |
936 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 933 for (auto* browser : *BrowserList::GetInstance()) { |
937 Browser* browser = *it; | |
938 const SessionID::id_type browser_id = browser->session_id().id(); | 934 const SessionID::id_type browser_id = browser->session_id().id(); |
939 if (browser_id != window_id.id() && | 935 if (browser_id != window_id.id() && |
940 window_closing_ids_.find(browser_id) == window_closing_ids_.end() && | 936 window_closing_ids_.find(browser_id) == window_closing_ids_.end() && |
941 ShouldTrackBrowser(browser)) { | 937 ShouldTrackBrowser(browser)) { |
942 return true; | 938 return true; |
943 } | 939 } |
944 } | 940 } |
945 return false; | 941 return false; |
946 } | 942 } |
947 | 943 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1103 // Clear session data if the last window for a profile has been closed and | 1099 // Clear session data if the last window for a profile has been closed and |
1104 // closing the last window would normally close Chrome, unless background mode | 1100 // closing the last window would normally close Chrome, unless background mode |
1105 // is active. Tests don't have a background_mode_manager. | 1101 // is active. Tests don't have a background_mode_manager. |
1106 if (has_open_trackable_browsers_ || | 1102 if (has_open_trackable_browsers_ || |
1107 browser_defaults::kBrowserAliveWithNoWindows || | 1103 browser_defaults::kBrowserAliveWithNoWindows || |
1108 g_browser_process->background_mode_manager()->IsBackgroundModeActive()) { | 1104 g_browser_process->background_mode_manager()->IsBackgroundModeActive()) { |
1109 return; | 1105 return; |
1110 } | 1106 } |
1111 | 1107 |
1112 // Check for any open windows for the current profile that we aren't tracking. | 1108 // Check for any open windows for the current profile that we aren't tracking. |
1113 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 1109 for (auto* browser : *BrowserList::GetInstance()) { |
1114 if ((*it)->profile() == profile()) | 1110 if (browser->profile() == profile()) |
1115 return; | 1111 return; |
1116 } | 1112 } |
1117 DeleteSessionOnlyData(profile()); | 1113 DeleteSessionOnlyData(profile()); |
1118 } | 1114 } |
1119 | 1115 |
1120 sessions::BaseSessionService* SessionService::GetBaseSessionServiceForTest() { | 1116 sessions::BaseSessionService* SessionService::GetBaseSessionServiceForTest() { |
1121 return base_session_service_.get(); | 1117 return base_session_service_.get(); |
1122 } | 1118 } |
OLD | NEW |