Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: chrome/browser/sessions/tab_restore_service.cc

Issue 92001: Restore closed tabs into new windows when necessary, and track the windows th... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/sessions/tab_restore_service.h ('k') | chrome/browser/tab_restore_uitest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <iterator> 6 #include <iterator>
7 7
8 #include "chrome/browser/sessions/tab_restore_service.h" 8 #include "chrome/browser/sessions/tab_restore_service.h"
9 9
10 #include "chrome/browser/browser_list.h" 10 #include "chrome/browser/browser_list.h"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 119
120 void TabRestoreService::CreateHistoricalTab(NavigationController* tab) { 120 void TabRestoreService::CreateHistoricalTab(NavigationController* tab) {
121 if (restoring_) 121 if (restoring_)
122 return; 122 return;
123 123
124 Browser* browser = Browser::GetBrowserForController(tab, NULL); 124 Browser* browser = Browser::GetBrowserForController(tab, NULL);
125 if (closing_browsers_.find(browser) != closing_browsers_.end()) 125 if (closing_browsers_.find(browser) != closing_browsers_.end())
126 return; 126 return;
127 127
128 scoped_ptr<Tab> local_tab(new Tab()); 128 scoped_ptr<Tab> local_tab(new Tab());
129 PopulateTabFromController(tab, local_tab.get()); 129 PopulateTab(local_tab.get(), browser, tab);
130 if (local_tab->navigations.empty()) 130 if (local_tab->navigations.empty())
131 return; 131 return;
132 132
133 // browser may be NULL when running unit tests.
134 if (browser) {
135 local_tab->browser_id = browser->session_id().id();
136 local_tab->tabstrip_index =
137 browser->tabstrip_model()->GetIndexOfController(tab);
138 }
139
140 AddEntry(local_tab.release(), true, true); 133 AddEntry(local_tab.release(), true, true);
141 } 134 }
142 135
143 void TabRestoreService::BrowserClosing(Browser* browser) { 136 void TabRestoreService::BrowserClosing(Browser* browser) {
144 if (browser->type() != Browser::TYPE_NORMAL || 137 if (browser->type() != Browser::TYPE_NORMAL ||
145 browser->tab_count() == 0) 138 browser->tab_count() == 0)
146 return; 139 return;
147 140
148 closing_browsers_.insert(browser); 141 closing_browsers_.insert(browser);
149 142
150 Window* window = new Window(); 143 Window* window = new Window();
151 window->selected_tab_index = browser->selected_index(); 144 window->selected_tab_index = browser->selected_index();
152 window->tabs.resize(browser->tab_count()); 145 window->tabs.resize(browser->tab_count());
153 size_t entry_index = 0; 146 size_t entry_index = 0;
154 for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) { 147 for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) {
155 PopulateTabFromController( 148 PopulateTab(&(window->tabs[entry_index]),
156 &browser->GetTabContentsAt(tab_index)->controller(), 149 browser,
157 &(window->tabs[entry_index])); 150 &browser->GetTabContentsAt(tab_index)->controller());
158 if (window->tabs[entry_index].navigations.empty()) 151 if (window->tabs[entry_index].navigations.empty()) {
159 window->tabs.erase(window->tabs.begin() + entry_index); 152 window->tabs.erase(window->tabs.begin() + entry_index);
160 else 153 } else {
154 window->tabs[entry_index].browser_id = browser->session_id().id();
161 entry_index++; 155 entry_index++;
156 }
162 } 157 }
163 if (window->tabs.empty()) { 158 if (window->tabs.empty()) {
164 delete window; 159 delete window;
165 window = NULL; 160 window = NULL;
166 } else { 161 } else {
167 window->selected_tab_index = 162 window->selected_tab_index =
168 std::min(static_cast<int>(window->tabs.size() - 1), 163 std::min(static_cast<int>(window->tabs.size() - 1),
169 window->selected_tab_index); 164 window->selected_tab_index);
170 AddEntry(window, true, true); 165 AddEntry(window, true, true);
171 } 166 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 Tab* tab = static_cast<Tab*>(entry); 221 Tab* tab = static_cast<Tab*>(entry);
227 if (replace_existing_tab) { 222 if (replace_existing_tab) {
228 browser->ReplaceRestoredTab(tab->navigations, 223 browser->ReplaceRestoredTab(tab->navigations,
229 tab->current_navigation_index); 224 tab->current_navigation_index);
230 } else { 225 } else {
231 // Use the tab's former browser and index, if available. 226 // Use the tab's former browser and index, if available.
232 Browser* tab_browser = NULL; 227 Browser* tab_browser = NULL;
233 int tab_index = -1; 228 int tab_index = -1;
234 if (tab->has_browser()) 229 if (tab->has_browser())
235 tab_browser = BrowserList::FindBrowserWithID(tab->browser_id); 230 tab_browser = BrowserList::FindBrowserWithID(tab->browser_id);
236 if (tab_browser) 231
232 if (tab_browser) {
237 tab_index = tab->tabstrip_index; 233 tab_index = tab->tabstrip_index;
238 else 234 } else {
239 tab_browser = browser; 235 tab_browser = Browser::Create(profile());
240 if (tab_index < 0 || tab_index > browser->tab_count()) 236 if (tab->has_browser()) {
241 tab_index = browser->tab_count(); 237 UpdateTabBrowserIDs(tab->browser_id,
238 tab_browser->session_id().id());
239 }
240 tab_browser->window()->Show();
241 }
242
243 if (tab_index < 0 || tab_index > tab_browser->tab_count())
244 tab_index = tab_browser->tab_count();
242 tab_browser->AddRestoredTab(tab->navigations, tab_index, 245 tab_browser->AddRestoredTab(tab->navigations, tab_index,
243 tab->current_navigation_index, true); 246 tab->current_navigation_index, true);
244 } 247 }
245 } else if (entry->type == WINDOW) { 248 } else if (entry->type == WINDOW) {
246 const Window* window = static_cast<Window*>(entry); 249 const Window* window = static_cast<Window*>(entry);
247 Browser* browser = Browser::Create(profile()); 250 Browser* browser = Browser::Create(profile());
248 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) { 251 for (size_t tab_i = 0; tab_i < window->tabs.size(); ++tab_i) {
249 const Tab& tab = window->tabs[tab_i]; 252 const Tab& tab = window->tabs[tab_i];
250 TabContents* restored_tab = 253 TabContents* restored_tab =
251 browser->AddRestoredTab(tab.navigations, browser->tab_count(), 254 browser->AddRestoredTab(tab.navigations, browser->tab_count(),
252 tab.current_navigation_index, 255 tab.current_navigation_index,
253 (static_cast<int>(tab_i) == 256 (static_cast<int>(tab_i) ==
254 window->selected_tab_index)); 257 window->selected_tab_index));
255 if (restored_tab) 258 if (restored_tab)
256 restored_tab->controller().LoadIfNecessary(); 259 restored_tab->controller().LoadIfNecessary();
257 } 260 }
261 // All the window's tabs had the same former browser_id.
262 if (window->tabs[0].has_browser()) {
263 UpdateTabBrowserIDs(window->tabs[0].browser_id,
264 browser->session_id().id());
265 }
258 browser->window()->Show(); 266 browser->window()->Show();
259 } else { 267 } else {
260 NOTREACHED(); 268 NOTREACHED();
261 } 269 }
262 } 270 }
263 delete entry; 271 delete entry;
264 restoring_ = false; 272 restoring_ = false;
265 NotifyTabsChanged(); 273 NotifyTabsChanged();
266 } 274 }
267 275
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 ScheduleCommandsForWindow(*static_cast<Window*>(entry)); 326 ScheduleCommandsForWindow(*static_cast<Window*>(entry));
319 } 327 }
320 entries_written_++; 328 entries_written_++;
321 } 329 }
322 } 330 }
323 if (pending_reset()) 331 if (pending_reset())
324 entries_written_ = 0; 332 entries_written_ = 0;
325 BaseSessionService::Save(); 333 BaseSessionService::Save();
326 } 334 }
327 335
328 void TabRestoreService::PopulateTabFromController( 336 void TabRestoreService::PopulateTab(Tab* tab,
329 NavigationController* controller, 337 Browser* browser,
330 Tab* tab) { 338 NavigationController* controller) {
331 const int pending_index = controller->pending_entry_index(); 339 const int pending_index = controller->pending_entry_index();
332 int entry_count = controller->entry_count(); 340 int entry_count = controller->entry_count();
333 if (entry_count == 0 && pending_index == 0) 341 if (entry_count == 0 && pending_index == 0)
334 entry_count++; 342 entry_count++;
335 tab->navigations.resize(static_cast<int>(entry_count)); 343 tab->navigations.resize(static_cast<int>(entry_count));
336 for (int i = 0; i < entry_count; ++i) { 344 for (int i = 0; i < entry_count; ++i) {
337 NavigationEntry* entry = (i == pending_index) ? 345 NavigationEntry* entry = (i == pending_index) ?
338 controller->pending_entry() : controller->GetEntryAtIndex(i); 346 controller->pending_entry() : controller->GetEntryAtIndex(i);
339 tab->navigations[i].SetFromNavigationEntry(*entry); 347 tab->navigations[i].SetFromNavigationEntry(*entry);
340 } 348 }
341 tab->current_navigation_index = controller->GetCurrentEntryIndex(); 349 tab->current_navigation_index = controller->GetCurrentEntryIndex();
342 if (tab->current_navigation_index == -1 && entry_count > 0) 350 if (tab->current_navigation_index == -1 && entry_count > 0)
343 tab->current_navigation_index = 0; 351 tab->current_navigation_index = 0;
352
353 // Browser may be NULL during unit tests.
354 if (browser) {
355 tab->browser_id = browser->session_id().id();
356 tab->tabstrip_index =
357 browser->tabstrip_model()->GetIndexOfController(controller);
358 }
344 } 359 }
345 360
346 void TabRestoreService::NotifyTabsChanged() { 361 void TabRestoreService::NotifyTabsChanged() {
347 FOR_EACH_OBSERVER(Observer, observer_list_, TabRestoreServiceChanged(this)); 362 FOR_EACH_OBSERVER(Observer, observer_list_, TabRestoreServiceChanged(this));
348 } 363 }
349 364
350 void TabRestoreService::AddEntry(Entry* entry, bool notify, bool to_front) { 365 void TabRestoreService::AddEntry(Entry* entry, bool notify, bool to_front) {
351 if (to_front) 366 if (to_front)
352 entries_.push_front(entry); 367 entries_.push_front(entry);
353 else 368 else
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 else 691 else
677 invalid_entries.push_back(*i); 692 invalid_entries.push_back(*i);
678 } 693 }
679 // NOTE: at this point the entries are ordered with newest at the front. 694 // NOTE: at this point the entries are ordered with newest at the front.
680 entries->swap(valid_entries); 695 entries->swap(valid_entries);
681 696
682 // Delete the remaining entries. 697 // Delete the remaining entries.
683 STLDeleteElements(&invalid_entries); 698 STLDeleteElements(&invalid_entries);
684 } 699 }
685 700
701 void TabRestoreService::UpdateTabBrowserIDs(SessionID::id_type old_id,
702 SessionID::id_type new_id) {
703 for (Entries::iterator i = entries_.begin(); i != entries_.end(); ++i) {
704 Entry* entry = *i;
705 if (entry->type == TAB) {
706 Tab* tab = static_cast<Tab*>(entry);
707 if (tab->browser_id == old_id)
708 tab->browser_id = new_id;
709 }
710 }
711 }
712
686 void TabRestoreService::OnGotPreviousSession( 713 void TabRestoreService::OnGotPreviousSession(
687 Handle handle, 714 Handle handle,
688 std::vector<SessionWindow*>* windows) { 715 std::vector<SessionWindow*>* windows) {
689 std::vector<Entry*> entries; 716 std::vector<Entry*> entries;
690 CreateEntriesFromWindows(windows, &entries); 717 CreateEntriesFromWindows(windows, &entries);
691 // Previous session tabs go first. 718 // Previous session tabs go first.
692 staging_entries_.insert(staging_entries_.begin(), entries.begin(), 719 staging_entries_.insert(staging_entries_.begin(), entries.begin(),
693 entries.end()); 720 entries.end());
694 load_state_ |= LOADED_LAST_SESSION; 721 load_state_ |= LOADED_LAST_SESSION;
695 LoadStateChanged(); 722 LoadStateChanged();
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 // it doesn't delete them. 789 // it doesn't delete them.
763 staging_entries_.clear(); 790 staging_entries_.clear();
764 791
765 // Make it so we rewrite all the tabs. We need to do this otherwise we won't 792 // Make it so we rewrite all the tabs. We need to do this otherwise we won't
766 // correctly write out the entries when Save is invoked (Save starts from 793 // correctly write out the entries when Save is invoked (Save starts from
767 // the front, not the end and we just added the entries to the end). 794 // the front, not the end and we just added the entries to the end).
768 entries_to_write_ = staging_entries_.size(); 795 entries_to_write_ = staging_entries_.size();
769 796
770 PruneAndNotify(); 797 PruneAndNotify();
771 } 798 }
OLDNEW
« no previous file with comments | « chrome/browser/sessions/tab_restore_service.h ('k') | chrome/browser/tab_restore_uitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698