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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 DCHECK(web_state_list); | 114 DCHECK(web_state_list); |
115 DCHECK_CURRENTLY_ON(web::WebThread::UI); | 115 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
116 task_tracker->PostTaskAndReply( | 116 task_tracker->PostTaskAndReply( |
117 task_runner.get(), FROM_HERE, | 117 task_runner.get(), FROM_HERE, |
118 base::Bind(&web::CertificatePolicyCache::ClearCertificatePolicies, | 118 base::Bind(&web::CertificatePolicyCache::ClearCertificatePolicies, |
119 policy_cache), | 119 policy_cache), |
120 base::Bind(&RestoreCertificatePolicyCacheFromModel, policy_cache, | 120 base::Bind(&RestoreCertificatePolicyCacheFromModel, policy_cache, |
121 base::Unretained(web_state_list))); | 121 base::Unretained(web_state_list))); |
122 } | 122 } |
123 | 123 |
| 124 // Factory of WebState for DeserializeWebStateList that wraps the method |
| 125 // web::WebState::CreateWithStorageSession and sets the WebState usage |
| 126 // enabled flag from |web_usage_enabled|. |
| 127 std::unique_ptr<web::WebState> CreateWebState( |
| 128 BOOL web_usage_enabled, |
| 129 web::WebState::CreateParams create_params, |
| 130 CRWSessionStorage* session_storage) { |
| 131 std::unique_ptr<web::WebState> web_state = |
| 132 web::WebState::CreateWithStorageSession(create_params, session_storage); |
| 133 web_state->SetWebUsageEnabled(web_usage_enabled); |
| 134 return web_state; |
| 135 } |
| 136 |
124 } // anonymous namespace | 137 } // anonymous namespace |
125 | 138 |
126 @interface TabModelWebStateProxyFactory : NSObject<WebStateProxyFactory> | 139 @interface TabModelWebStateProxyFactory : NSObject<WebStateProxyFactory> |
127 @end | 140 @end |
128 | 141 |
129 @implementation TabModelWebStateProxyFactory | 142 @implementation TabModelWebStateProxyFactory |
130 | 143 |
131 - (id)proxyForWebState:(web::WebState*)webState { | 144 - (id)proxyForWebState:(web::WebState*)webState { |
132 return LegacyTabHelper::GetTabForWebState(webState); | 145 return LegacyTabHelper::GetTabForWebState(webState); |
133 } | 146 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // method and restoring session in the initialiser. | 192 // method and restoring session in the initialiser. |
180 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window | 193 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window |
181 persistState:(BOOL)persistState; | 194 persistState:(BOOL)persistState; |
182 | 195 |
183 @end | 196 @end |
184 | 197 |
185 @implementation TabModel | 198 @implementation TabModel |
186 | 199 |
187 @synthesize browserState = _browserState; | 200 @synthesize browserState = _browserState; |
188 @synthesize sessionID = _sessionID; | 201 @synthesize sessionID = _sessionID; |
189 @synthesize webUsageEnabled = webUsageEnabled_; | 202 @synthesize webUsageEnabled = _webUsageEnabled; |
190 | 203 |
191 #pragma mark - Overriden | 204 #pragma mark - Overriden |
192 | 205 |
193 - (void)dealloc { | 206 - (void)dealloc { |
194 // Make sure the tabs do clean after themselves. It is important for | 207 // Make sure the tabs do clean after themselves. It is important for |
195 // removeObserver: to be called first otherwise a lot of unecessary work will | 208 // removeObserver: to be called first otherwise a lot of unecessary work will |
196 // happen on -closeAllTabs. | 209 // happen on -closeAllTabs. |
197 DCHECK([_observers empty]); | 210 DCHECK([_observers empty]); |
198 | 211 |
199 // browserStateDestroyed should always have been called before destruction. | 212 // browserStateDestroyed should always have been called before destruction. |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 _webStateList->InsertWebState(insertion_index, std::move(webState)); | 502 _webStateList->InsertWebState(insertion_index, std::move(webState)); |
490 if (parentTab.webState) { | 503 if (parentTab.webState) { |
491 _webStateList->SetOpenerOfWebStateAt(insertion_index, | 504 _webStateList->SetOpenerOfWebStateAt(insertion_index, |
492 WebStateOpener(parentTab.webState)); | 505 WebStateOpener(parentTab.webState)); |
493 } | 506 } |
494 } | 507 } |
495 | 508 |
496 Tab* tab = LegacyTabHelper::GetTabForWebState(webStatePtr); | 509 Tab* tab = LegacyTabHelper::GetTabForWebState(webStatePtr); |
497 DCHECK(tab); | 510 DCHECK(tab); |
498 | 511 |
499 webStatePtr->SetWebUsageEnabled(webUsageEnabled_ ? true : false); | 512 webStatePtr->SetWebUsageEnabled(_webUsageEnabled ? true : false); |
500 | 513 |
501 if (!inBackground && _tabUsageRecorder) | 514 if (!inBackground && _tabUsageRecorder) |
502 _tabUsageRecorder->TabCreatedForSelection(tab); | 515 _tabUsageRecorder->TabCreatedForSelection(tab); |
503 | 516 |
504 webStatePtr->GetNavigationManager()->LoadURLWithParams(loadParams); | 517 webStatePtr->GetNavigationManager()->LoadURLWithParams(loadParams); |
505 | 518 |
506 // Force the page to start loading even if it's in the background. | 519 // Force the page to start loading even if it's in the background. |
507 // TODO(crbug.com/705819): Remove this call. | 520 // TODO(crbug.com/705819): Remove this call. |
508 if (webUsageEnabled_) | 521 if (_webUsageEnabled) |
509 webStatePtr->GetView(); | 522 webStatePtr->GetView(); |
510 | 523 |
511 NSDictionary* userInfo = @{ | 524 NSDictionary* userInfo = @{ |
512 kTabModelTabKey : tab, | 525 kTabModelTabKey : tab, |
513 kTabModelOpenInBackgroundKey : @(inBackground), | 526 kTabModelOpenInBackgroundKey : @(inBackground), |
514 }; | 527 }; |
515 [[NSNotificationCenter defaultCenter] | 528 [[NSNotificationCenter defaultCenter] |
516 postNotificationName:kTabModelNewTabWillOpenNotification | 529 postNotificationName:kTabModelNewTabWillOpenNotification |
517 object:self | 530 object:self |
518 userInfo:userInfo]; | 531 userInfo:userInfo]; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 [_observers tabModel:self didChangeTabSnapshot:tab withImage:image]; | 591 [_observers tabModel:self didChangeTabSnapshot:tab withImage:image]; |
579 } | 592 } |
580 | 593 |
581 - (void)resetAllWebViews { | 594 - (void)resetAllWebViews { |
582 for (Tab* tab in self) { | 595 for (Tab* tab in self) { |
583 [tab.webController reinitializeWebViewAndReload:(tab == self.currentTab)]; | 596 [tab.webController reinitializeWebViewAndReload:(tab == self.currentTab)]; |
584 } | 597 } |
585 } | 598 } |
586 | 599 |
587 - (void)setWebUsageEnabled:(BOOL)webUsageEnabled { | 600 - (void)setWebUsageEnabled:(BOOL)webUsageEnabled { |
588 if (webUsageEnabled_ == webUsageEnabled) | 601 if (_webUsageEnabled == webUsageEnabled) |
589 return; | 602 return; |
590 webUsageEnabled_ = webUsageEnabled; | 603 _webUsageEnabled = webUsageEnabled; |
591 for (int index = 0; index < _webStateList->count(); ++index) { | 604 for (int index = 0; index < _webStateList->count(); ++index) { |
592 web::WebState* webState = _webStateList->GetWebStateAt(index); | 605 web::WebState* webState = _webStateList->GetWebStateAt(index); |
593 webState->SetWebUsageEnabled(webUsageEnabled_ ? true : false); | 606 webState->SetWebUsageEnabled(_webUsageEnabled ? true : false); |
594 } | 607 } |
595 } | 608 } |
596 | 609 |
597 - (void)setPrimary:(BOOL)primary { | 610 - (void)setPrimary:(BOOL)primary { |
598 if (_tabUsageRecorder) | 611 if (_tabUsageRecorder) |
599 _tabUsageRecorder->RecordPrimaryTabModelChange(primary, self.currentTab); | 612 _tabUsageRecorder->RecordPrimaryTabModelChange(primary, self.currentTab); |
600 } | 613 } |
601 | 614 |
602 - (NSSet*)currentlyReferencedExternalFiles { | 615 - (NSSet*)currentlyReferencedExternalFiles { |
603 NSMutableSet* referencedFiles = [NSMutableSet set]; | 616 NSMutableSet* referencedFiles = [NSMutableSet set]; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 DCHECK(window); | 714 DCHECK(window); |
702 | 715 |
703 if (!window.sessions.count) | 716 if (!window.sessions.count) |
704 return NO; | 717 return NO; |
705 | 718 |
706 int oldCount = _webStateList->count(); | 719 int oldCount = _webStateList->count(); |
707 DCHECK_GE(oldCount, 0); | 720 DCHECK_GE(oldCount, 0); |
708 | 721 |
709 web::WebState::CreateParams createParams(_browserState); | 722 web::WebState::CreateParams createParams(_browserState); |
710 DeserializeWebStateList( | 723 DeserializeWebStateList( |
711 _webStateList.get(), window, webUsageEnabled_, | 724 _webStateList.get(), window, |
712 base::BindRepeating(&web::WebState::CreateWithStorageSession, | 725 base::BindRepeating(&CreateWebState, _webUsageEnabled, createParams)); |
713 createParams)); | |
714 | 726 |
715 DCHECK_GT(_webStateList->count(), oldCount); | 727 DCHECK_GT(_webStateList->count(), oldCount); |
716 int restoredCount = _webStateList->count() - oldCount; | 728 int restoredCount = _webStateList->count() - oldCount; |
717 DCHECK_EQ(window.sessions.count, static_cast<NSUInteger>(restoredCount)); | 729 DCHECK_EQ(window.sessions.count, static_cast<NSUInteger>(restoredCount)); |
718 | 730 |
719 scoped_refptr<web::CertificatePolicyCache> policyCache = | 731 scoped_refptr<web::CertificatePolicyCache> policyCache = |
720 web::BrowserState::GetCertificatePolicyCache(_browserState); | 732 web::BrowserState::GetCertificatePolicyCache(_browserState); |
721 | 733 |
722 NSMutableArray<Tab*>* restoredTabs = | 734 NSMutableArray<Tab*>* restoredTabs = |
723 [[NSMutableArray alloc] initWithCapacity:window.sessions.count]; | 735 [[NSMutableArray alloc] initWithCapacity:window.sessions.count]; |
724 | 736 |
725 for (int index = oldCount; index < _webStateList->count(); ++index) { | 737 for (int index = oldCount; index < _webStateList->count(); ++index) { |
726 web::WebState* webState = _webStateList->GetWebStateAt(index); | 738 web::WebState* webState = _webStateList->GetWebStateAt(index); |
727 Tab* tab = LegacyTabHelper::GetTabForWebState(webState); | 739 Tab* tab = LegacyTabHelper::GetTabForWebState(webState); |
728 | 740 |
729 webState->SetWebUsageEnabled(webUsageEnabled_ ? true : false); | 741 webState->SetWebUsageEnabled(_webUsageEnabled ? true : false); |
730 tab.webController.usePlaceholderOverlay = YES; | 742 tab.webController.usePlaceholderOverlay = YES; |
731 | 743 |
732 // Restore the CertificatePolicyCache (note that webState is invalid after | 744 // Restore the CertificatePolicyCache (note that webState is invalid after |
733 // passing it via move semantic to -initWithWebState:model:). | 745 // passing it via move semantic to -initWithWebState:model:). |
734 UpdateCertificatePolicyCacheFromWebState(policyCache, [tab webState]); | 746 UpdateCertificatePolicyCacheFromWebState(policyCache, [tab webState]); |
735 [restoredTabs addObject:tab]; | 747 [restoredTabs addObject:tab]; |
736 } | 748 } |
737 | 749 |
738 // If there was only one tab and it was the new tab page, clobber it. | 750 // If there was only one tab and it was the new tab page, clobber it. |
739 BOOL closedNTPTab = NO; | 751 BOOL closedNTPTab = NO; |
740 if (oldCount == 1) { | 752 if (oldCount == 1) { |
741 Tab* tab = [self tabAtIndex:0]; | 753 Tab* tab = [self tabAtIndex:0]; |
742 if (tab.url == GURL(kChromeUINewTabURL)) { | 754 if (tab.url == GURL(kChromeUINewTabURL)) { |
743 [self closeTab:tab]; | 755 [self closeTab:tab]; |
744 closedNTPTab = YES; | 756 closedNTPTab = YES; |
745 oldCount = 0; | 757 oldCount = 0; |
746 } | 758 } |
747 } | 759 } |
748 if (_tabUsageRecorder) | 760 if (_tabUsageRecorder) |
749 _tabUsageRecorder->InitialRestoredTabs(self.currentTab, restoredTabs); | 761 _tabUsageRecorder->InitialRestoredTabs(self.currentTab, restoredTabs); |
750 return closedNTPTab; | 762 return closedNTPTab; |
751 } | 763 } |
752 | 764 |
753 #pragma mark - Notification Handlers | 765 #pragma mark - Notification Handlers |
754 | 766 |
755 // Called when UIApplicationWillResignActiveNotification is received. | 767 // Called when UIApplicationWillResignActiveNotification is received. |
756 - (void)willResignActive:(NSNotification*)notify { | 768 - (void)willResignActive:(NSNotification*)notify { |
757 if (webUsageEnabled_ && self.currentTab) { | 769 if (_webUsageEnabled && self.currentTab) { |
758 [[SnapshotCache sharedInstance] | 770 [[SnapshotCache sharedInstance] |
759 willBeSavedGreyWhenBackgrounding:self.currentTab.tabId]; | 771 willBeSavedGreyWhenBackgrounding:self.currentTab.tabId]; |
760 } | 772 } |
761 } | 773 } |
762 | 774 |
763 // Called when UIApplicationDidEnterBackgroundNotification is received. | 775 // Called when UIApplicationDidEnterBackgroundNotification is received. |
764 - (void)applicationDidEnterBackground:(NSNotification*)notify { | 776 - (void)applicationDidEnterBackground:(NSNotification*)notify { |
765 if (!_browserState) | 777 if (!_browserState) |
766 return; | 778 return; |
767 | 779 |
768 // Evict all the certificate policies except for the current entries of the | 780 // Evict all the certificate policies except for the current entries of the |
769 // active sessions. | 781 // active sessions. |
770 CleanCertificatePolicyCache( | 782 CleanCertificatePolicyCache( |
771 &_clearPoliciesTaskTracker, | 783 &_clearPoliciesTaskTracker, |
772 web::WebThread::GetTaskRunnerForThread(web::WebThread::IO), | 784 web::WebThread::GetTaskRunnerForThread(web::WebThread::IO), |
773 web::BrowserState::GetCertificatePolicyCache(_browserState), | 785 web::BrowserState::GetCertificatePolicyCache(_browserState), |
774 _webStateList.get()); | 786 _webStateList.get()); |
775 | 787 |
776 if (_tabUsageRecorder) | 788 if (_tabUsageRecorder) |
777 _tabUsageRecorder->AppDidEnterBackground(); | 789 _tabUsageRecorder->AppDidEnterBackground(); |
778 | 790 |
779 // Normally, the session is saved after some timer expires but since the app | 791 // Normally, the session is saved after some timer expires but since the app |
780 // is about to enter the background send YES to save the session immediately. | 792 // is about to enter the background send YES to save the session immediately. |
781 [self saveSessionImmediately:YES]; | 793 [self saveSessionImmediately:YES]; |
782 | 794 |
783 // Write out a grey version of the current website to disk. | 795 // Write out a grey version of the current website to disk. |
784 if (webUsageEnabled_ && self.currentTab) { | 796 if (_webUsageEnabled && self.currentTab) { |
785 [[SnapshotCache sharedInstance] | 797 [[SnapshotCache sharedInstance] |
786 saveGreyInBackgroundForSessionID:self.currentTab.tabId]; | 798 saveGreyInBackgroundForSessionID:self.currentTab.tabId]; |
787 } | 799 } |
788 } | 800 } |
789 | 801 |
790 // Called when UIApplicationWillEnterForegroundNotification is received. | 802 // Called when UIApplicationWillEnterForegroundNotification is received. |
791 - (void)applicationWillEnterForeground:(NSNotification*)notify { | 803 - (void)applicationWillEnterForeground:(NSNotification*)notify { |
792 if (_tabUsageRecorder) { | 804 if (_tabUsageRecorder) { |
793 _tabUsageRecorder->AppWillEnterForeground(); | 805 _tabUsageRecorder->AppWillEnterForeground(); |
794 } | 806 } |
795 } | 807 } |
796 | 808 |
797 @end | 809 @end |
OLD | NEW |