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

Side by Side Diff: ios/chrome/browser/tabs/tab_model.mm

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

Powered by Google App Engine
This is Rietveld 408576698