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

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

Issue 2674293002: Make certificate policy restoration thread-safe. (Closed)
Patch Set: Rebase. Created 3 years, 10 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 | no next file » | 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 <list>
8 #include <utility> 7 #include <utility>
9 #include <vector> 8 #include <vector>
10 9
11 #include "base/bind.h" 10 #include "base/bind.h"
12 #import "base/ios/crb_protocol_observers.h" 11 #import "base/ios/crb_protocol_observers.h"
13 #include "base/logging.h" 12 #include "base/logging.h"
14 #import "base/mac/scoped_nsobject.h" 13 #import "base/mac/scoped_nsobject.h"
15 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
16 #include "base/metrics/user_metrics.h" 15 #include "base/metrics/user_metrics.h"
17 #include "base/metrics/user_metrics_action.h" 16 #include "base/metrics/user_metrics_action.h"
18 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
18 #include "base/task/cancelable_task_tracker.h"
19 #include "components/sessions/core/serialized_navigation_entry.h" 19 #include "components/sessions/core/serialized_navigation_entry.h"
20 #include "components/sessions/core/session_id.h" 20 #include "components/sessions/core/session_id.h"
21 #include "components/sessions/core/tab_restore_service.h" 21 #include "components/sessions/core/tab_restore_service.h"
22 #include "components/sessions/ios/ios_live_tab.h" 22 #include "components/sessions/ios/ios_live_tab.h"
23 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" 23 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
24 #include "ios/chrome/browser/chrome_url_constants.h" 24 #include "ios/chrome/browser/chrome_url_constants.h"
25 #import "ios/chrome/browser/chrome_url_util.h" 25 #import "ios/chrome/browser/chrome_url_util.h"
26 #import "ios/chrome/browser/metrics/tab_usage_recorder.h" 26 #import "ios/chrome/browser/metrics/tab_usage_recorder.h"
27 #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h" 27 #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h"
28 #import "ios/chrome/browser/sessions/session_service.h" 28 #import "ios/chrome/browser/sessions/session_service.h"
(...skipping 30 matching lines...) Expand all
59 @"kTabModelTabDeselectedNotification"; 59 @"kTabModelTabDeselectedNotification";
60 NSString* const kTabModelNewTabWillOpenNotification = 60 NSString* const kTabModelNewTabWillOpenNotification =
61 @"kTabModelNewTabWillOpenNotification"; 61 @"kTabModelNewTabWillOpenNotification";
62 NSString* const kTabModelTabKey = @"tab"; 62 NSString* const kTabModelTabKey = @"tab";
63 NSString* const kTabModelPageLoadSuccess = @"pageLoadSuccess"; 63 NSString* const kTabModelPageLoadSuccess = @"pageLoadSuccess";
64 NSString* const kTabModelOpenInBackgroundKey = @"shouldOpenInBackground"; 64 NSString* const kTabModelOpenInBackgroundKey = @"shouldOpenInBackground";
65 65
66 namespace { 66 namespace {
67 67
68 // Updates CRWSessionCertificatePolicyManager's certificate policy cache. 68 // Updates CRWSessionCertificatePolicyManager's certificate policy cache.
69 void UpdateCertificatePolicyCacheFromWebState(web::WebState* web_state) { 69 void UpdateCertificatePolicyCacheFromWebState(
70 DCHECK([NSThread isMainThread]); 70 const scoped_refptr<web::CertificatePolicyCache>& policy_cache,
71 web::WebState* web_state) {
71 DCHECK(web_state); 72 DCHECK(web_state);
72 scoped_refptr<web::CertificatePolicyCache> policy_cache = 73 DCHECK_CURRENTLY_ON(web::WebThread::UI);
73 web::BrowserState::GetCertificatePolicyCache(
74 web_state->GetBrowserState());
75 // TODO(crbug.com/454984): Remove CRWSessionController usage once certificate 74 // TODO(crbug.com/454984): Remove CRWSessionController usage once certificate
76 // policy manager is moved to NavigationManager. 75 // policy manager is moved to NavigationManager.
77 CRWSessionController* controller = static_cast<web::WebStateImpl*>(web_state) 76 CRWSessionController* controller = static_cast<web::WebStateImpl*>(web_state)
78 ->GetNavigationManagerImpl() 77 ->GetNavigationManagerImpl()
79 .GetSessionController(); 78 .GetSessionController();
80 [[controller sessionCertificatePolicyManager] 79 [[controller sessionCertificatePolicyManager]
81 updateCertificatePolicyCache:policy_cache]; 80 updateCertificatePolicyCache:policy_cache];
82 } 81 }
83 82
84 // Populates the certificate policy cache based on the current entries of the 83 // Populates the certificate policy cache based on the WebStates of |tab_model|.
85 // given tabs. 84 void RestoreCertificatePolicyCacheFromModel(
86 void RestoreCertificatePolicyCacheFromTabs(NSArray* tabs) { 85 const scoped_refptr<web::CertificatePolicyCache>& policy_cache,
87 DCHECK([NSThread isMainThread]); 86 TabModel* tab_model) {
88 for (Tab* tab in tabs) { 87 DCHECK_CURRENTLY_ON(web::WebThread::UI);
89 UpdateCertificatePolicyCacheFromWebState(tab.webState); 88 for (Tab* tab in tab_model)
90 } 89 UpdateCertificatePolicyCacheFromWebState(policy_cache, tab.webState);
91 } 90 }
92 91
93 // Scrubs the certificate policy cache of all the certificate policies except 92 // Scrubs the certificate policy cache of all certificates policies except
94 // those for the current entries of the given tabs. 93 // those for the current entries in |tab_model|.
95 void CleanCertificatePolicyCache( 94 void CleanCertificatePolicyCache(
96 scoped_refptr<web::CertificatePolicyCache> policy_cache, 95 base::CancelableTaskTracker* task_tracker,
97 NSArray* tabs) { 96 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
98 DCHECK_CURRENTLY_ON(web::WebThread::IO); 97 const scoped_refptr<web::CertificatePolicyCache>& policy_cache,
98 TabModel* tab_model) {
99 DCHECK(tab_model);
99 DCHECK(policy_cache); 100 DCHECK(policy_cache);
100 policy_cache->ClearCertificatePolicies(); 101 DCHECK_CURRENTLY_ON(web::WebThread::UI);
101 web::WebThread::PostTask( 102 task_tracker->PostTaskAndReply(
102 web::WebThread::UI, FROM_HERE, 103 task_runner.get(), FROM_HERE,
103 base::Bind(&RestoreCertificatePolicyCacheFromTabs, tabs)); 104 base::Bind(&web::CertificatePolicyCache::ClearCertificatePolicies,
105 policy_cache),
106 base::Bind(&RestoreCertificatePolicyCacheFromModel, policy_cache,
107 base::Unretained(tab_model)));
104 } 108 }
105 109
106 } // anonymous namespace 110 } // anonymous namespace
107 111
108 @interface TabModelObservers : CRBProtocolObservers<TabModelObserver> 112 @interface TabModelObservers : CRBProtocolObservers<TabModelObserver>
109 @end 113 @end
110 @implementation TabModelObservers 114 @implementation TabModelObservers
111 @end 115 @end
112 116
113 @interface TabModel ()<TabUsageRecorderDelegate> { 117 @interface TabModel ()<TabUsageRecorderDelegate> {
(...skipping 13 matching lines...) Expand all
127 int _newTabCount; 131 int _newTabCount;
128 132
129 // Backs up property with the same name. 133 // Backs up property with the same name.
130 std::unique_ptr<TabUsageRecorder> _tabUsageRecorder; 134 std::unique_ptr<TabUsageRecorder> _tabUsageRecorder;
131 // Backs up property with the same name. 135 // Backs up property with the same name.
132 const SessionID _sessionID; 136 const SessionID _sessionID;
133 // Saves session's state. 137 // Saves session's state.
134 base::scoped_nsobject<SessionServiceIOS> _sessionService; 138 base::scoped_nsobject<SessionServiceIOS> _sessionService;
135 // List of TabModelObservers. 139 // List of TabModelObservers.
136 base::scoped_nsobject<TabModelObservers> _observers; 140 base::scoped_nsobject<TabModelObservers> _observers;
141
142 // Used to ensure thread-safety of the certificate policy management code.
143 base::CancelableTaskTracker _clearPoliciesTaskTracker;
137 } 144 }
138 145
139 // Session window for the contents of the tab model. 146 // Session window for the contents of the tab model.
140 @property(nonatomic, readonly) SessionWindowIOS* windowForSavingSession; 147 @property(nonatomic, readonly) SessionWindowIOS* windowForSavingSession;
141 148
142 // Returns YES if tab URL host indicates that tab is an NTP tab. 149 // Returns YES if tab URL host indicates that tab is an NTP tab.
143 - (BOOL)isNTPTab:(Tab*)tab; 150 - (BOOL)isNTPTab:(Tab*)tab;
144 151
145 // Opens a tab at the specified URL and registers its JS-supplied window name if 152 // Opens a tab at the specified URL and registers its JS-supplied window name if
146 // appropriate. For certain transition types, will consult the order controller 153 // appropriate. For certain transition types, will consult the order controller
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 DCHECK([_observers empty]); 201 DCHECK([_observers empty]);
195 // browserStateDestroyed should always have been called before destruction. 202 // browserStateDestroyed should always have been called before destruction.
196 DCHECK(!_browserState); 203 DCHECK(!_browserState);
197 204
198 [[NSNotificationCenter defaultCenter] removeObserver:self]; 205 [[NSNotificationCenter defaultCenter] removeObserver:self];
199 // Make sure the tabs do clean after themselves. It is important for 206 // Make sure the tabs do clean after themselves. It is important for
200 // removeObserver: to be called first otherwise a lot of unecessary work will 207 // removeObserver: to be called first otherwise a lot of unecessary work will
201 // happen on -closeAllTabs. 208 // happen on -closeAllTabs.
202 [self closeAllTabs]; 209 [self closeAllTabs];
203 210
211 _clearPoliciesTaskTracker.TryCancelAll();
212
204 [super dealloc]; 213 [super dealloc];
205 } 214 }
206 215
207 #pragma mark - Public methods 216 #pragma mark - Public methods
208 217
209 - (Tab*)currentTab { 218 - (Tab*)currentTab {
210 return _currentTab.get(); 219 return _currentTab.get();
211 } 220 }
212 221
213 - (void)setCurrentTab:(Tab*)newTab { 222 - (void)setCurrentTab:(Tab*)newTab {
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 persistState:(BOOL)persistState { 956 persistState:(BOOL)persistState {
948 DCHECK(_browserState); 957 DCHECK(_browserState);
949 DCHECK(window); 958 DCHECK(window);
950 959
951 NSArray* sessions = window.sessions; 960 NSArray* sessions = window.sessions;
952 if (!sessions.count) 961 if (!sessions.count)
953 return NO; 962 return NO;
954 963
955 size_t oldCount = [_tabs count]; 964 size_t oldCount = [_tabs count];
956 web::WebState::CreateParams params(_browserState); 965 web::WebState::CreateParams params(_browserState);
966 scoped_refptr<web::CertificatePolicyCache> policyCache =
967 web::BrowserState::GetCertificatePolicyCache(_browserState);
957 968
958 for (CRWNavigationManagerStorage* session in sessions) { 969 for (CRWNavigationManagerStorage* session in sessions) {
959 std::unique_ptr<web::WebState> webState = 970 std::unique_ptr<web::WebState> webState =
960 web::WebState::Create(params, session); 971 web::WebState::Create(params, session);
961 DCHECK_EQ(webState->GetBrowserState(), _browserState); 972 DCHECK_EQ(webState->GetBrowserState(), _browserState);
962 Tab* tab = 973 Tab* tab =
963 [self insertTabWithWebState:std::move(webState) atIndex:[_tabs count]]; 974 [self insertTabWithWebState:std::move(webState) atIndex:[_tabs count]];
964 tab.webController.usePlaceholderOverlay = YES; 975 tab.webController.usePlaceholderOverlay = YES;
965 976
966 // Restore the CertificatePolicyCache (note that webState is invalid after 977 // Restore the CertificatePolicyCache (note that webState is invalid after
967 // passing it via move semantic to -insertTabWithWebState:atIndex:). 978 // passing it via move semantic to -insertTabWithWebState:atIndex:).
968 UpdateCertificatePolicyCacheFromWebState(tab.webState); 979 UpdateCertificatePolicyCacheFromWebState(policyCache, tab.webState);
969 } 980 }
970 DCHECK_GT([_tabs count], oldCount); 981 DCHECK_GT([_tabs count], oldCount);
971 982
972 // Update the selected tab if there was a selected Tab in the saved session. 983 // Update the selected tab if there was a selected Tab in the saved session.
973 if (window.selectedIndex != NSNotFound) { 984 if (window.selectedIndex != NSNotFound) {
974 NSUInteger selectedIndex = window.selectedIndex + oldCount; 985 NSUInteger selectedIndex = window.selectedIndex + oldCount;
975 DCHECK_LT(selectedIndex, [_tabs count]); 986 DCHECK_LT(selectedIndex, [_tabs count]);
976 DCHECK([self tabAtIndex:selectedIndex]); 987 DCHECK([self tabAtIndex:selectedIndex]);
977 [self changeSelectedTabFrom:_currentTab 988 [self changeSelectedTabFrom:_currentTab
978 to:[self tabAtIndex:selectedIndex] 989 to:[self tabAtIndex:selectedIndex]
(...skipping 26 matching lines...) Expand all
1005 if (webUsageEnabled_ && _currentTab) { 1016 if (webUsageEnabled_ && _currentTab) {
1006 [[SnapshotCache sharedInstance] 1017 [[SnapshotCache sharedInstance]
1007 willBeSavedGreyWhenBackgrounding:_currentTab.get().tabId]; 1018 willBeSavedGreyWhenBackgrounding:_currentTab.get().tabId];
1008 } 1019 }
1009 } 1020 }
1010 1021
1011 // Called when UIApplicationDidEnterBackgroundNotification is received. 1022 // Called when UIApplicationDidEnterBackgroundNotification is received.
1012 - (void)applicationDidEnterBackground:(NSNotification*)notify { 1023 - (void)applicationDidEnterBackground:(NSNotification*)notify {
1013 if (!_browserState) 1024 if (!_browserState)
1014 return; 1025 return;
1026
1015 // Evict all the certificate policies except for the current entries of the 1027 // Evict all the certificate policies except for the current entries of the
1016 // active sessions. 1028 // active sessions.
1017 scoped_refptr<web::CertificatePolicyCache> policy_cache = 1029 CleanCertificatePolicyCache(
1018 web::BrowserState::GetCertificatePolicyCache(_browserState); 1030 &_clearPoliciesTaskTracker,
1019 DCHECK(policy_cache); 1031 web::WebThread::GetTaskRunnerForThread(web::WebThread::IO),
1020 web::WebThread::PostTask( 1032 web::BrowserState::GetCertificatePolicyCache(_browserState), self);
1021 web::WebThread::IO, FROM_HERE,
1022 base::Bind(&CleanCertificatePolicyCache, policy_cache, _tabs));
1023 1033
1024 if (_tabUsageRecorder) 1034 if (_tabUsageRecorder)
1025 _tabUsageRecorder->AppDidEnterBackground(); 1035 _tabUsageRecorder->AppDidEnterBackground();
1026 1036
1027 // Normally, the session is saved after some timer expires but since the app 1037 // Normally, the session is saved after some timer expires but since the app
1028 // is about to enter the background send YES to save the session immediately. 1038 // is about to enter the background send YES to save the session immediately.
1029 [self saveSessionImmediately:YES]; 1039 [self saveSessionImmediately:YES];
1030 1040
1031 // Write out a grey version of the current website to disk. 1041 // Write out a grey version of the current website to disk.
1032 if (webUsageEnabled_ && _currentTab) { 1042 if (webUsageEnabled_ && _currentTab) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 web::NavigationManager::WebLoadParams params(URL); 1081 web::NavigationManager::WebLoadParams params(URL);
1072 params.referrer = referrer; 1082 params.referrer = referrer;
1073 params.transition_type = ui::PAGE_TRANSITION_TYPED; 1083 params.transition_type = ui::PAGE_TRANSITION_TYPED;
1074 [[tab webController] loadWithParams:params]; 1084 [[tab webController] loadWithParams:params];
1075 [tab webController].webUsageEnabled = webUsageEnabled_; 1085 [tab webController].webUsageEnabled = webUsageEnabled_;
1076 [self insertTab:tab atIndex:index]; 1086 [self insertTab:tab atIndex:index];
1077 return tab; 1087 return tab;
1078 } 1088 }
1079 1089
1080 @end 1090 @end
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698