| OLD | NEW |
| 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 #import "ios/chrome/browser/tabs/tab_model.h" | 5 #import "ios/chrome/browser/tabs/tab_model.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 | 693 |
| 694 - (SessionIOS*)sessionForSaving { | 694 - (SessionIOS*)sessionForSaving { |
| 695 // Background tabs will already have their state preserved, but not the | 695 // Background tabs will already have their state preserved, but not the |
| 696 // fg tab. Do it now. | 696 // fg tab. Do it now. |
| 697 [self.currentTab recordStateInHistory]; | 697 [self.currentTab recordStateInHistory]; |
| 698 | 698 |
| 699 // Build the array of sessions. Copy the session objects as the saving will | 699 // Build the array of sessions. Copy the session objects as the saving will |
| 700 // be done on a separate thread. | 700 // be done on a separate thread. |
| 701 // TODO(crbug.com/661986): This could get expensive especially since this | 701 // TODO(crbug.com/661986): This could get expensive especially since this |
| 702 // window may never be saved (if another call comes in before the delay). | 702 // window may never be saved (if another call comes in before the delay). |
| 703 SessionWindowIOS* sessionWindow = [[[SessionWindowIOS alloc] | 703 return [[SessionIOS alloc] |
| 704 initWithSessions:SerializeWebStateList(_webStateList.get()) | 704 initWithWindows:@[ SerializeWebStateList(_webStateList.get()) ]]; |
| 705 selectedIndex:[self indexOfTab:self.currentTab]] autorelease]; | |
| 706 return [[SessionIOS alloc] initWithWindows:@[ sessionWindow ]]; | |
| 707 } | 705 } |
| 708 | 706 |
| 709 - (void)postNotificationName:(NSString*)notificationName withTab:(Tab*)tab { | 707 - (void)postNotificationName:(NSString*)notificationName withTab:(Tab*)tab { |
| 710 // A scoped_nsobject is used rather than an NSDictionary with static | 708 // A scoped_nsobject is used rather than an NSDictionary with static |
| 711 // initializer dictionaryWithObject, because that approach adds the dictionary | 709 // initializer dictionaryWithObject, because that approach adds the dictionary |
| 712 // to the autorelease pool, which in turn holds Tab alive longer than | 710 // to the autorelease pool, which in turn holds Tab alive longer than |
| 713 // necessary. | 711 // necessary. |
| 714 base::scoped_nsobject<NSDictionary> userInfo( | 712 base::scoped_nsobject<NSDictionary> userInfo( |
| 715 [[NSDictionary alloc] initWithObjectsAndKeys:tab, kTabModelTabKey, nil]); | 713 [[NSDictionary alloc] initWithObjectsAndKeys:tab, kTabModelTabKey, nil]); |
| 716 [[NSNotificationCenter defaultCenter] postNotificationName:notificationName | 714 [[NSNotificationCenter defaultCenter] postNotificationName:notificationName |
| 717 object:self | 715 object:self |
| 718 userInfo:userInfo]; | 716 userInfo:userInfo]; |
| 719 } | 717 } |
| 720 | 718 |
| 721 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window | 719 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window |
| 722 persistState:(BOOL)persistState { | 720 persistState:(BOOL)persistState { |
| 723 DCHECK(_browserState); | 721 DCHECK(_browserState); |
| 724 DCHECK(window); | 722 DCHECK(window); |
| 725 | 723 |
| 726 NSArray* sessions = window.sessions; | 724 if (!window.sessions.count) |
| 727 if (!sessions.count) | |
| 728 return NO; | 725 return NO; |
| 729 | 726 |
| 730 int oldCount = _webStateList->count(); | 727 int oldCount = _webStateList->count(); |
| 731 DCHECK_GE(oldCount, 0); | 728 DCHECK_GE(oldCount, 0); |
| 732 | 729 |
| 730 if (persistState && self.currentTab) |
| 731 [self.currentTab recordStateInHistory]; |
| 732 |
| 733 web::WebState::CreateParams createParams(_browserState); | 733 web::WebState::CreateParams createParams(_browserState); |
| 734 DeserializeWebStateList( | 734 DeserializeWebStateList( |
| 735 _webStateList.get(), sessions, | 735 _webStateList.get(), window, |
| 736 base::BindRepeating(&web::WebState::CreateWithStorageSession, | 736 base::BindRepeating(&web::WebState::CreateWithStorageSession, |
| 737 createParams)); | 737 createParams)); |
| 738 | 738 |
| 739 DCHECK_GT(_webStateList->count(), oldCount); | 739 DCHECK_GT(_webStateList->count(), oldCount); |
| 740 int restoredCount = _webStateList->count() - oldCount; | 740 int restoredCount = _webStateList->count() - oldCount; |
| 741 DCHECK_EQ(sessions.count, static_cast<NSUInteger>(restoredCount)); | 741 DCHECK_EQ(window.sessions.count, static_cast<NSUInteger>(restoredCount)); |
| 742 | 742 |
| 743 scoped_refptr<web::CertificatePolicyCache> policyCache = | 743 scoped_refptr<web::CertificatePolicyCache> policyCache = |
| 744 web::BrowserState::GetCertificatePolicyCache(_browserState); | 744 web::BrowserState::GetCertificatePolicyCache(_browserState); |
| 745 | 745 |
| 746 base::scoped_nsobject<NSMutableArray<Tab*>> restoredTabs( | 746 base::scoped_nsobject<NSMutableArray<Tab*>> restoredTabs( |
| 747 [[NSMutableArray alloc] initWithCapacity:sessions.count]); | 747 [[NSMutableArray alloc] initWithCapacity:window.sessions.count]); |
| 748 | 748 |
| 749 for (int index = oldCount; index < _webStateList->count(); ++index) { | 749 for (int index = oldCount; index < _webStateList->count(); ++index) { |
| 750 web::WebState* webState = _webStateList->GetWebStateAt(index); | 750 web::WebState* webState = _webStateList->GetWebStateAt(index); |
| 751 Tab* tab = LegacyTabHelper::GetTabForWebState(webState); | 751 Tab* tab = LegacyTabHelper::GetTabForWebState(webState); |
| 752 | 752 |
| 753 webState->SetWebUsageEnabled(webUsageEnabled_ ? true : false); | 753 webState->SetWebUsageEnabled(webUsageEnabled_ ? true : false); |
| 754 tab.webController.usePlaceholderOverlay = YES; | 754 tab.webController.usePlaceholderOverlay = YES; |
| 755 | 755 |
| 756 // Restore the CertificatePolicyCache (note that webState is invalid after | 756 // Restore the CertificatePolicyCache (note that webState is invalid after |
| 757 // passing it via move semantic to -initWithWebState:model:). | 757 // passing it via move semantic to -initWithWebState:model:). |
| 758 UpdateCertificatePolicyCacheFromWebState(policyCache, [tab webState]); | 758 UpdateCertificatePolicyCacheFromWebState(policyCache, [tab webState]); |
| 759 [restoredTabs addObject:tab]; | 759 [restoredTabs addObject:tab]; |
| 760 } | 760 } |
| 761 | 761 |
| 762 // Update the selected tab if there was a selected Tab in the saved session. | |
| 763 if (window.selectedIndex != NSNotFound) { | |
| 764 NSUInteger selectedIndex = window.selectedIndex + oldCount; | |
| 765 DCHECK_LT(selectedIndex, self.count); | |
| 766 DCHECK([self tabAtIndex:selectedIndex]); | |
| 767 | |
| 768 if (persistState && self.currentTab) | |
| 769 [self.currentTab recordStateInHistory]; | |
| 770 _webStateList->ActivateWebStateAt(static_cast<int>(selectedIndex)); | |
| 771 } | |
| 772 | |
| 773 // If there was only one tab and it was the new tab page, clobber it. | 762 // If there was only one tab and it was the new tab page, clobber it. |
| 774 BOOL closedNTPTab = NO; | 763 BOOL closedNTPTab = NO; |
| 775 if (oldCount == 1) { | 764 if (oldCount == 1) { |
| 776 Tab* tab = [self tabAtIndex:0]; | 765 Tab* tab = [self tabAtIndex:0]; |
| 777 if (tab.url == GURL(kChromeUINewTabURL)) { | 766 if (tab.url == GURL(kChromeUINewTabURL)) { |
| 778 [self closeTab:tab]; | 767 [self closeTab:tab]; |
| 779 closedNTPTab = YES; | 768 closedNTPTab = YES; |
| 780 oldCount = 0; | 769 oldCount = 0; |
| 781 } | 770 } |
| 782 } | 771 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 } | 812 } |
| 824 | 813 |
| 825 // Called when UIApplicationWillEnterForegroundNotification is received. | 814 // Called when UIApplicationWillEnterForegroundNotification is received. |
| 826 - (void)applicationWillEnterForeground:(NSNotification*)notify { | 815 - (void)applicationWillEnterForeground:(NSNotification*)notify { |
| 827 if (_tabUsageRecorder) { | 816 if (_tabUsageRecorder) { |
| 828 _tabUsageRecorder->AppWillEnterForeground(); | 817 _tabUsageRecorder->AppWillEnterForeground(); |
| 829 } | 818 } |
| 830 } | 819 } |
| 831 | 820 |
| 832 @end | 821 @end |
| OLD | NEW |