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

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

Issue 1131373003: [Session restore] Add MRU logic to loading of background pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 5 years, 7 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
« no previous file with comments | « no previous file | chrome/browser/sessions/session_restore_browsertest.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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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_restore.h" 5 #include "chrome/browser/sessions/session_restore.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <list> 8 #include <list>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 if (urls_to_open_.empty()) { 290 if (urls_to_open_.empty()) {
291 // No tab browsers were created and no URLs were supplied on the command 291 // No tab browsers were created and no URLs were supplied on the command
292 // line. Open the new tab page. 292 // line. Open the new tab page.
293 urls_to_open_.push_back(GURL(chrome::kChromeUINewTabURL)); 293 urls_to_open_.push_back(GURL(chrome::kChromeUINewTabURL));
294 } 294 }
295 AppendURLsToBrowser(browser, urls_to_open_); 295 AppendURLsToBrowser(browser, urls_to_open_);
296 browser->window()->Show(); 296 browser->window()->Show();
297 } 297 }
298 298
299 if (succeeded) { 299 if (succeeded) {
300 if (SessionRestore::GetSmartRestoreMode() !=
301 SessionRestore::SMART_RESTORE_MODE_OFF) {
302 std::stable_sort(contents_created->begin(), contents_created->end());
303 }
300 // Start Loading tabs. 304 // Start Loading tabs.
301 if (SessionRestore::GetSmartRestoreMode() !=
302 SessionRestore::SMART_RESTORE_MODE_OFF)
303 std::stable_sort(contents_created->begin(), contents_created->end());
304 SessionRestoreDelegate::RestoreTabs(*contents_created, restore_started_); 305 SessionRestoreDelegate::RestoreTabs(*contents_created, restore_started_);
305 } 306 }
306 307
307 if (!synchronous_) { 308 if (!synchronous_) {
308 // If we're not synchronous we need to delete ourself. 309 // If we're not synchronous we need to delete ourself.
309 // NOTE: we must use DeleteLater here as most likely we're in a callback 310 // NOTE: we must use DeleteLater here as most likely we're in a callback
310 // from the history service which doesn't deal well with deleting the 311 // from the history service which doesn't deal well with deleting the
311 // object it is notifying. 312 // object it is notifying.
312 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); 313 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
313 314
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 // tabs but pinned tabs will be pushed in front. 501 // tabs but pinned tabs will be pushed in front.
501 // If there are no existing tabs, the tab at |selected_tab_index| will be 502 // If there are no existing tabs, the tab at |selected_tab_index| will be
502 // selected. Otherwise, the tab selection will remain untouched. 503 // selected. Otherwise, the tab selection will remain untouched.
503 void RestoreTabsToBrowser(const sessions::SessionWindow& window, 504 void RestoreTabsToBrowser(const sessions::SessionWindow& window,
504 Browser* browser, 505 Browser* browser,
505 int initial_tab_count, 506 int initial_tab_count,
506 int selected_tab_index, 507 int selected_tab_index,
507 std::vector<RestoredTab>* created_contents) { 508 std::vector<RestoredTab>* created_contents) {
508 DVLOG(1) << "RestoreTabsToBrowser " << window.tabs.size(); 509 DVLOG(1) << "RestoreTabsToBrowser " << window.tabs.size();
509 DCHECK(!window.tabs.empty()); 510 DCHECK(!window.tabs.empty());
511 base::TimeTicks now = base::TimeTicks::Now();
512 base::TimeTicks highest_time = base::TimeTicks::UnixEpoch();
510 if (initial_tab_count == 0) { 513 if (initial_tab_count == 0) {
514 if (SessionRestore::GetSmartRestoreMode() ==
515 SessionRestore::SMART_RESTORE_MODE_MRU) {
516 // The last active time of a WebContents is initially set to the
517 // creation time of the tab, which is not necessarly the same as the
518 // loading time, so we have to restore the values. Also, since TimeTicks
519 // only make sense in their current session, these values have to be
520 // sanitized first. To do so, we need to first figure out the largest
521 // time. This will then be used to set the last active time of
522 // each tab where the most recent tab will have its time set to |now|
523 // and the rest of the tabs will have theirs set earlier by the same
524 // delta as they originally had.
525 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) {
526 const sessions::SessionTab& tab = *(window.tabs[i]);
527 if (tab.last_active_time > highest_time)
528 highest_time = tab.last_active_time;
529 }
530 }
511 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) { 531 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) {
512 const sessions::SessionTab& tab = *(window.tabs[i]); 532 const sessions::SessionTab& tab = *(window.tabs[i]);
513 533
514 // Loads are scheduled for each restored tab unless the tab is going to 534 // Loads are scheduled for each restored tab unless the tab is going to
515 // be selected as ShowBrowser() will load the selected tab. 535 // be selected as ShowBrowser() will load the selected tab.
516 bool is_selected_tab = (i == selected_tab_index); 536 bool is_selected_tab = (i == selected_tab_index);
517 WebContents* contents = RestoreTab(tab, i, browser, is_selected_tab); 537 WebContents* contents = RestoreTab(tab, i, browser, is_selected_tab);
518 538
519 // RestoreTab can return nullptr if |tab| doesn't have valid data. 539 // RestoreTab can return nullptr if |tab| doesn't have valid data.
520 if (!contents) 540 if (!contents)
521 continue; 541 continue;
522 542
543 // Sanitize the last active time.
544 if (SessionRestore::GetSmartRestoreMode() ==
545 SessionRestore::SMART_RESTORE_MODE_MRU) {
546 base::TimeDelta delta = highest_time - tab.last_active_time;
547 contents->SetLastActiveTime(now - delta);
548 }
549
523 RestoredTab restored_tab(contents, is_selected_tab, 550 RestoredTab restored_tab(contents, is_selected_tab,
524 tab.extension_app_id.empty(), tab.pinned); 551 tab.extension_app_id.empty(), tab.pinned);
525 created_contents->push_back(restored_tab); 552 created_contents->push_back(restored_tab);
526 553
527 // If this isn't the selected tab, there's nothing else to do. 554 // If this isn't the selected tab, there's nothing else to do.
528 if (!is_selected_tab) 555 if (!is_selected_tab)
529 continue; 556 continue;
530 557
531 ShowBrowser(browser, browser->tab_strip_model()->GetIndexOfWebContents( 558 ShowBrowser(browser, browser->tab_strip_model()->GetIndexOfWebContents(
532 contents)); 559 contents));
533 // TODO(sky): remove. For debugging 368236. 560 // TODO(sky): remove. For debugging 368236.
534 CHECK_EQ(browser->tab_strip_model()->GetActiveWebContents(), contents); 561 CHECK_EQ(browser->tab_strip_model()->GetActiveWebContents(), contents);
535 } 562 }
536 } else { 563 } else {
537 // If the browser already has tabs, we want to restore the new ones after 564 // If the browser already has tabs, we want to restore the new ones after
538 // the existing ones. E.g. this happens in Win8 Metro where we merge 565 // the existing ones. E.g. this happens in Win8 Metro where we merge
539 // windows or when launching a hosted app from the app launcher. 566 // windows or when launching a hosted app from the app launcher.
540 int tab_index_offset = initial_tab_count; 567 int tab_index_offset = initial_tab_count;
541 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) { 568 for (int i = 0; i < static_cast<int>(window.tabs.size()); ++i) {
542 const sessions::SessionTab& tab = *(window.tabs[i]); 569 const sessions::SessionTab& tab = *(window.tabs[i]);
543 // Always schedule loads as we will not be calling ShowBrowser(). 570 // Always schedule loads as we will not be calling ShowBrowser().
544 WebContents* contents = 571 WebContents* contents =
545 RestoreTab(tab, tab_index_offset + i, browser, false); 572 RestoreTab(tab, tab_index_offset + i, browser, false);
546 if (contents) { 573 if (contents) {
574 // Sanitize the last active time.
575 if (SessionRestore::GetSmartRestoreMode() ==
576 SessionRestore::SMART_RESTORE_MODE_MRU) {
577 base::TimeDelta delta = highest_time - tab.last_active_time;
578 contents->SetLastActiveTime(now - delta);
579 }
547 RestoredTab restored_tab(contents, false, 580 RestoredTab restored_tab(contents, false,
548 tab.extension_app_id.empty(), tab.pinned); 581 tab.extension_app_id.empty(), tab.pinned);
549 created_contents->push_back(restored_tab); 582 created_contents->push_back(restored_tab);
550 } 583 }
551 } 584 }
552 } 585 }
553 } 586 }
554 587
555 // |tab_index| is ignored for pinned tabs which will always be pushed behind 588 // |tab_index| is ignored for pinned tabs which will always be pushed behind
556 // the last existing pinned tab. 589 // the last existing pinned tab.
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 if (prioritize_tabs == "mru") 858 if (prioritize_tabs == "mru")
826 return SMART_RESTORE_MODE_MRU; 859 return SMART_RESTORE_MODE_MRU;
827 if (prioritize_tabs == "simple") 860 if (prioritize_tabs == "simple")
828 return SMART_RESTORE_MODE_SIMPLE; 861 return SMART_RESTORE_MODE_SIMPLE;
829 return SMART_RESTORE_MODE_OFF; 862 return SMART_RESTORE_MODE_OFF;
830 } 863 }
831 864
832 // static 865 // static
833 base::CallbackList<void(int)>* 866 base::CallbackList<void(int)>*
834 SessionRestore::on_session_restored_callbacks_ = nullptr; 867 SessionRestore::on_session_restored_callbacks_ = nullptr;
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/sessions/session_restore_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698