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 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #import "base/mac/foundation_util.h" | 13 #import "base/mac/foundation_util.h" |
14 #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_action.h" | 15 #include "base/metrics/user_metrics_action.h" |
17 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
18 #include "base/task/cancelable_task_tracker.h" | 17 #include "base/task/cancelable_task_tracker.h" |
19 #include "components/sessions/core/serialized_navigation_entry.h" | 18 #include "components/sessions/core/serialized_navigation_entry.h" |
20 #include "components/sessions/core/session_id.h" | 19 #include "components/sessions/core/session_id.h" |
21 #include "components/sessions/core/tab_restore_service.h" | 20 #include "components/sessions/core/tab_restore_service.h" |
22 #include "components/sessions/ios/ios_live_tab.h" | 21 #include "components/sessions/ios/ios_live_tab.h" |
23 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 22 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
24 #include "ios/chrome/browser/chrome_url_constants.h" | 23 #include "ios/chrome/browser/chrome_url_constants.h" |
(...skipping 28 matching lines...) Expand all Loading... |
53 #include "ios/web/public/certificate_policy_cache.h" | 52 #include "ios/web/public/certificate_policy_cache.h" |
54 #include "ios/web/public/navigation_item.h" | 53 #include "ios/web/public/navigation_item.h" |
55 #import "ios/web/public/navigation_manager.h" | 54 #import "ios/web/public/navigation_manager.h" |
56 #import "ios/web/public/serializable_user_data_manager.h" | 55 #import "ios/web/public/serializable_user_data_manager.h" |
57 #include "ios/web/public/web_state/session_certificate_policy_cache.h" | 56 #include "ios/web/public/web_state/session_certificate_policy_cache.h" |
58 #include "ios/web/public/web_thread.h" | 57 #include "ios/web/public/web_thread.h" |
59 #import "ios/web/web_state/ui/crw_web_controller.h" | 58 #import "ios/web/web_state/ui/crw_web_controller.h" |
60 #import "ios/web/web_state/web_state_impl.h" | 59 #import "ios/web/web_state/web_state_impl.h" |
61 #include "url/gurl.h" | 60 #include "url/gurl.h" |
62 | 61 |
| 62 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 63 #error "This file requires ARC support." |
| 64 #endif |
| 65 |
63 NSString* const kTabModelTabWillStartLoadingNotification = | 66 NSString* const kTabModelTabWillStartLoadingNotification = |
64 @"kTabModelTabWillStartLoadingNotification"; | 67 @"kTabModelTabWillStartLoadingNotification"; |
65 NSString* const kTabModelTabDidStartLoadingNotification = | 68 NSString* const kTabModelTabDidStartLoadingNotification = |
66 @"kTabModelTabDidStartLoadingNotification"; | 69 @"kTabModelTabDidStartLoadingNotification"; |
67 NSString* const kTabModelTabDidFinishLoadingNotification = | 70 NSString* const kTabModelTabDidFinishLoadingNotification = |
68 @"kTabModelTabDidFinishLoadingNotification"; | 71 @"kTabModelTabDidFinishLoadingNotification"; |
69 NSString* const kTabModelAllTabsDidCloseNotification = | 72 NSString* const kTabModelAllTabsDidCloseNotification = |
70 @"kTabModelAllTabsDidCloseNotification"; | 73 @"kTabModelAllTabsDidCloseNotification"; |
71 NSString* const kTabModelTabDeselectedNotification = | 74 NSString* const kTabModelTabDeselectedNotification = |
72 @"kTabModelTabDeselectedNotification"; | 75 @"kTabModelTabDeselectedNotification"; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 143 |
141 // Helper providing NSFastEnumeration implementation over the WebStateList. | 144 // Helper providing NSFastEnumeration implementation over the WebStateList. |
142 std::unique_ptr<WebStateListFastEnumerationHelper> _fastEnumerationHelper; | 145 std::unique_ptr<WebStateListFastEnumerationHelper> _fastEnumerationHelper; |
143 | 146 |
144 // WebStateListObservers reacting to modifications of the model (may send | 147 // WebStateListObservers reacting to modifications of the model (may send |
145 // notification, translate and forward events, update metrics, ...). | 148 // notification, translate and forward events, update metrics, ...). |
146 std::vector<std::unique_ptr<WebStateListObserver>> _webStateListObservers; | 149 std::vector<std::unique_ptr<WebStateListObserver>> _webStateListObservers; |
147 | 150 |
148 // Strong references to id<WebStateListObserving> wrapped by non-owning | 151 // Strong references to id<WebStateListObserving> wrapped by non-owning |
149 // WebStateListObserverBridges. | 152 // WebStateListObserverBridges. |
150 base::scoped_nsobject<NSArray<id<WebStateListObserving>>> | 153 NSArray<id<WebStateListObserving>>* _retainedWebStateListObservers; |
151 _retainedWebStateListObservers; | |
152 | 154 |
153 // The delegate for sync. | 155 // The delegate for sync. |
154 std::unique_ptr<TabModelSyncedWindowDelegate> _syncedWindowDelegate; | 156 std::unique_ptr<TabModelSyncedWindowDelegate> _syncedWindowDelegate; |
155 | 157 |
156 // Counters for metrics. | 158 // Counters for metrics. |
157 WebStateListMetricsObserver* _webStateListMetricsObserver; | 159 WebStateListMetricsObserver* _webStateListMetricsObserver; |
158 | 160 |
159 // Backs up property with the same name. | 161 // Backs up property with the same name. |
160 std::unique_ptr<TabUsageRecorder> _tabUsageRecorder; | 162 std::unique_ptr<TabUsageRecorder> _tabUsageRecorder; |
161 // Backs up property with the same name. | 163 // Backs up property with the same name. |
162 const SessionID _sessionID; | 164 const SessionID _sessionID; |
163 // Saves session's state. | 165 // Saves session's state. |
164 base::scoped_nsobject<SessionServiceIOS> _sessionService; | 166 SessionServiceIOS* _sessionService; |
165 // List of TabModelObservers. | 167 // List of TabModelObservers. |
166 base::scoped_nsobject<TabModelObservers> _observers; | 168 TabModelObservers* _observers; |
167 | 169 |
168 // Used to ensure thread-safety of the certificate policy management code. | 170 // Used to ensure thread-safety of the certificate policy management code. |
169 base::CancelableTaskTracker _clearPoliciesTaskTracker; | 171 base::CancelableTaskTracker _clearPoliciesTaskTracker; |
170 } | 172 } |
171 | 173 |
172 // Session window for the contents of the tab model. | 174 // Session window for the contents of the tab model. |
173 @property(nonatomic, readonly) SessionIOS* sessionForSaving; | 175 @property(nonatomic, readonly) SessionIOS* sessionForSaving; |
174 | 176 |
175 // Helper method that posts a notification with the given name with |tab| | |
176 // in the userInfo dictionary under the kTabModelTabKey. | |
177 - (void)postNotificationName:(NSString*)notificationName withTab:(Tab*)tab; | |
178 | |
179 // Helper method to restore a saved session and control if the state should | 177 // Helper method to restore a saved session and control if the state should |
180 // be persisted or not. Used to implement the public -restoreSessionWindow: | 178 // be persisted or not. Used to implement the public -restoreSessionWindow: |
181 // method and restoring session in the initialiser. | 179 // method and restoring session in the initialiser. |
182 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window | 180 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window |
183 persistState:(BOOL)persistState; | 181 persistState:(BOOL)persistState; |
184 | 182 |
185 @end | 183 @end |
186 | 184 |
187 @implementation TabModel | 185 @implementation TabModel |
188 | 186 |
(...skipping 24 matching lines...) Expand all Loading... |
213 // the WebStateList destructor is called. | 211 // the WebStateList destructor is called. |
214 @autoreleasepool { | 212 @autoreleasepool { |
215 [self closeAllTabs]; | 213 [self closeAllTabs]; |
216 } | 214 } |
217 | 215 |
218 // Unregister all observers after closing all the tabs as some of them are | 216 // Unregister all observers after closing all the tabs as some of them are |
219 // required to properly clean up the Tabs. | 217 // required to properly clean up the Tabs. |
220 for (const auto& webStateListObserver : _webStateListObservers) | 218 for (const auto& webStateListObserver : _webStateListObservers) |
221 _webStateList->RemoveObserver(webStateListObserver.get()); | 219 _webStateList->RemoveObserver(webStateListObserver.get()); |
222 _webStateListObservers.clear(); | 220 _webStateListObservers.clear(); |
223 _retainedWebStateListObservers.reset(); | 221 _retainedWebStateListObservers = nil; |
224 | 222 |
225 _clearPoliciesTaskTracker.TryCancelAll(); | 223 _clearPoliciesTaskTracker.TryCancelAll(); |
226 | |
227 [super dealloc]; | |
228 } | 224 } |
229 | 225 |
230 #pragma mark - Public methods | 226 #pragma mark - Public methods |
231 | 227 |
232 - (Tab*)currentTab { | 228 - (Tab*)currentTab { |
233 web::WebState* webState = _webStateList->GetActiveWebState(); | 229 web::WebState* webState = _webStateList->GetActiveWebState(); |
234 return webState ? LegacyTabHelper::GetTabForWebState(webState) : nil; | 230 return webState ? LegacyTabHelper::GetTabForWebState(webState) : nil; |
235 } | 231 } |
236 | 232 |
237 - (void)setCurrentTab:(Tab*)newTab { | 233 - (void)setCurrentTab:(Tab*)newTab { |
(...skipping 24 matching lines...) Expand all Loading... |
262 } | 258 } |
263 | 259 |
264 - (WebStateList*)webStateList { | 260 - (WebStateList*)webStateList { |
265 return _webStateList.get(); | 261 return _webStateList.get(); |
266 } | 262 } |
267 | 263 |
268 - (instancetype)initWithSessionWindow:(SessionWindowIOS*)window | 264 - (instancetype)initWithSessionWindow:(SessionWindowIOS*)window |
269 sessionService:(SessionServiceIOS*)service | 265 sessionService:(SessionServiceIOS*)service |
270 browserState:(ios::ChromeBrowserState*)browserState { | 266 browserState:(ios::ChromeBrowserState*)browserState { |
271 if ((self = [super init])) { | 267 if ((self = [super init])) { |
272 _observers.reset([[TabModelObservers observers] retain]); | 268 _observers = [TabModelObservers observers]; |
273 | 269 |
274 _webStateListDelegate = | 270 _webStateListDelegate = |
275 base::MakeUnique<TabModelWebStateListDelegate>(self); | 271 base::MakeUnique<TabModelWebStateListDelegate>(self); |
276 _webStateList = base::MakeUnique<WebStateList>(_webStateListDelegate.get()); | 272 _webStateList = base::MakeUnique<WebStateList>(_webStateListDelegate.get()); |
277 | 273 |
278 _fastEnumerationHelper = | 274 _fastEnumerationHelper = |
279 base::MakeUnique<WebStateListFastEnumerationHelper>( | 275 base::MakeUnique<WebStateListFastEnumerationHelper>( |
280 _webStateList.get(), [[TabModelWebStateProxyFactory alloc] init]); | 276 _webStateList.get(), [[TabModelWebStateProxyFactory alloc] init]); |
281 | 277 |
282 _browserState = browserState; | 278 _browserState = browserState; |
283 DCHECK(_browserState); | 279 DCHECK(_browserState); |
284 | 280 |
285 // Normal browser states are the only ones to get tab restore. Tab sync | 281 // Normal browser states are the only ones to get tab restore. Tab sync |
286 // handles incognito browser states by filtering on profile, so it's | 282 // handles incognito browser states by filtering on profile, so it's |
287 // important to the backend code to always have a sync window delegate. | 283 // important to the backend code to always have a sync window delegate. |
288 if (!_browserState->IsOffTheRecord()) { | 284 if (!_browserState->IsOffTheRecord()) { |
289 // Set up the usage recorder before tabs are created. | 285 // Set up the usage recorder before tabs are created. |
290 _tabUsageRecorder = base::MakeUnique<TabUsageRecorder>(self); | 286 _tabUsageRecorder = base::MakeUnique<TabUsageRecorder>(self); |
291 } | 287 } |
292 _syncedWindowDelegate = base::MakeUnique<TabModelSyncedWindowDelegate>( | 288 _syncedWindowDelegate = base::MakeUnique<TabModelSyncedWindowDelegate>( |
293 _webStateList.get(), _sessionID); | 289 _webStateList.get(), _sessionID); |
294 | 290 |
295 // There must be a valid session service defined to consume session windows. | 291 // There must be a valid session service defined to consume session windows. |
296 DCHECK(service); | 292 DCHECK(service); |
297 _sessionService.reset([service retain]); | 293 _sessionService = service; |
298 | 294 |
299 base::scoped_nsobject<NSMutableArray<id<WebStateListObserving>>> | 295 NSMutableArray<id<WebStateListObserving>>* retainedWebStateListObservers = |
300 retainedWebStateListObservers([[NSMutableArray alloc] init]); | 296 [[NSMutableArray alloc] init]; |
301 | 297 |
302 base::scoped_nsobject<TabModelClosingWebStateObserver> | 298 TabModelClosingWebStateObserver* tabModelClosingWebStateObserver = [ |
303 tabModelClosingWebStateObserver([[TabModelClosingWebStateObserver alloc] | 299 [TabModelClosingWebStateObserver alloc] |
304 initWithTabModel:self | 300 initWithTabModel:self |
305 restoreService:IOSChromeTabRestoreServiceFactory:: | 301 restoreService:IOSChromeTabRestoreServiceFactory::GetForBrowserState( |
306 GetForBrowserState(_browserState)]); | 302 _browserState)]; |
307 [retainedWebStateListObservers addObject:tabModelClosingWebStateObserver]; | 303 [retainedWebStateListObservers addObject:tabModelClosingWebStateObserver]; |
308 | 304 |
309 _webStateListObservers.push_back( | 305 _webStateListObservers.push_back( |
310 base::MakeUnique<WebStateListObserverBridge>( | 306 base::MakeUnique<WebStateListObserverBridge>( |
311 tabModelClosingWebStateObserver)); | 307 tabModelClosingWebStateObserver)); |
312 | 308 |
313 _webStateListObservers.push_back( | 309 _webStateListObservers.push_back( |
314 base::MakeUnique<SnapshotCacheWebStateListObserver>( | 310 base::MakeUnique<SnapshotCacheWebStateListObserver>( |
315 [SnapshotCache sharedInstance])); | 311 [SnapshotCache sharedInstance])); |
316 if (_tabUsageRecorder) { | 312 if (_tabUsageRecorder) { |
317 _webStateListObservers.push_back( | 313 _webStateListObservers.push_back( |
318 base::MakeUnique<TabUsageRecorderWebStateListObserver>( | 314 base::MakeUnique<TabUsageRecorderWebStateListObserver>( |
319 _tabUsageRecorder.get())); | 315 _tabUsageRecorder.get())); |
320 } | 316 } |
321 _webStateListObservers.push_back(base::MakeUnique<TabParentingObserver>()); | 317 _webStateListObservers.push_back(base::MakeUnique<TabParentingObserver>()); |
322 | 318 |
323 base::scoped_nsobject<TabModelSelectedTabObserver> | 319 TabModelSelectedTabObserver* tabModelSelectedTabObserver = |
324 tabModelSelectedTabObserver( | 320 [[TabModelSelectedTabObserver alloc] initWithTabModel:self]; |
325 [[TabModelSelectedTabObserver alloc] initWithTabModel:self]); | |
326 [retainedWebStateListObservers addObject:tabModelSelectedTabObserver]; | 321 [retainedWebStateListObservers addObject:tabModelSelectedTabObserver]; |
327 _webStateListObservers.push_back( | 322 _webStateListObservers.push_back( |
328 base::MakeUnique<WebStateListObserverBridge>( | 323 base::MakeUnique<WebStateListObserverBridge>( |
329 tabModelSelectedTabObserver)); | 324 tabModelSelectedTabObserver)); |
330 | 325 |
331 base::scoped_nsobject<TabModelObserversBridge> tabModelObserversBridge( | 326 TabModelObserversBridge* tabModelObserversBridge = |
332 [[TabModelObserversBridge alloc] initWithTabModel:self | 327 [[TabModelObserversBridge alloc] initWithTabModel:self |
333 tabModelObservers:_observers.get()]); | 328 tabModelObservers:_observers]; |
334 [retainedWebStateListObservers addObject:tabModelObserversBridge]; | 329 [retainedWebStateListObservers addObject:tabModelObserversBridge]; |
335 _webStateListObservers.push_back( | 330 _webStateListObservers.push_back( |
336 base::MakeUnique<WebStateListObserverBridge>(tabModelObserversBridge)); | 331 base::MakeUnique<WebStateListObserverBridge>(tabModelObserversBridge)); |
337 | 332 |
338 auto webStateListMetricsObserver = | 333 auto webStateListMetricsObserver = |
339 base::MakeUnique<WebStateListMetricsObserver>(); | 334 base::MakeUnique<WebStateListMetricsObserver>(); |
340 _webStateListMetricsObserver = webStateListMetricsObserver.get(); | 335 _webStateListMetricsObserver = webStateListMetricsObserver.get(); |
341 _webStateListObservers.push_back(std::move(webStateListMetricsObserver)); | 336 _webStateListObservers.push_back(std::move(webStateListMetricsObserver)); |
342 | 337 |
343 for (const auto& webStateListObserver : _webStateListObservers) | 338 for (const auto& webStateListObserver : _webStateListObservers) |
344 _webStateList->AddObserver(webStateListObserver.get()); | 339 _webStateList->AddObserver(webStateListObserver.get()); |
345 _retainedWebStateListObservers.reset([retainedWebStateListObservers copy]); | 340 _retainedWebStateListObservers = [retainedWebStateListObservers copy]; |
346 | 341 |
347 if (window) { | 342 if (window) { |
348 DCHECK([_observers empty]); | 343 DCHECK([_observers empty]); |
349 // Restore the session and reset the session metrics (as the event have | 344 // Restore the session and reset the session metrics (as the event have |
350 // not been generated by the user but by a cold start cycle). | 345 // not been generated by the user but by a cold start cycle). |
351 [self restoreSessionWindow:window persistState:NO]; | 346 [self restoreSessionWindow:window persistState:NO]; |
352 [self resetSessionMetrics]; | 347 [self resetSessionMetrics]; |
353 } | 348 } |
354 | 349 |
355 // Register for resign active notification. | 350 // Register for resign active notification. |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 } | 658 } |
664 } | 659 } |
665 | 660 |
666 int tabCount = static_cast<int>(self.count); | 661 int tabCount = static_cast<int>(self.count); |
667 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tabCount, 1, 200, 50); | 662 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tabCount, 1, 200, 50); |
668 } | 663 } |
669 | 664 |
670 #pragma mark - NSFastEnumeration | 665 #pragma mark - NSFastEnumeration |
671 | 666 |
672 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state | 667 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state |
673 objects:(id*)objects | 668 objects:(id __unsafe_unretained*)objects |
674 count:(NSUInteger)count { | 669 count:(NSUInteger)count { |
675 return [_fastEnumerationHelper->GetFastEnumeration() | 670 return [_fastEnumerationHelper->GetFastEnumeration() |
676 countByEnumeratingWithState:state | 671 countByEnumeratingWithState:state |
677 objects:objects | 672 objects:objects |
678 count:count]; | 673 count:count]; |
679 } | 674 } |
680 | 675 |
681 #pragma mark - TabUsageRecorderDelegate | 676 #pragma mark - TabUsageRecorderDelegate |
682 | 677 |
683 - (NSUInteger)liveTabsCount { | 678 - (NSUInteger)liveTabsCount { |
684 NSUInteger count = 0; | 679 NSUInteger count = 0; |
685 for (Tab* tab in self) { | 680 for (Tab* tab in self) { |
686 if ([tab.webController isViewAlive]) | 681 if ([tab.webController isViewAlive]) |
687 count++; | 682 count++; |
688 } | 683 } |
689 return count; | 684 return count; |
690 } | 685 } |
691 | 686 |
692 #pragma mark - Private methods | 687 #pragma mark - Private methods |
693 | 688 |
694 - (SessionIOS*)sessionForSaving { | 689 - (SessionIOS*)sessionForSaving { |
695 // Build the array of sessions. Copy the session objects as the saving will | 690 // Build the array of sessions. Copy the session objects as the saving will |
696 // be done on a separate thread. | 691 // be done on a separate thread. |
697 // TODO(crbug.com/661986): This could get expensive especially since this | 692 // TODO(crbug.com/661986): This could get expensive especially since this |
698 // window may never be saved (if another call comes in before the delay). | 693 // window may never be saved (if another call comes in before the delay). |
699 return [[SessionIOS alloc] | 694 return [[SessionIOS alloc] |
700 initWithWindows:@[ SerializeWebStateList(_webStateList.get()) ]]; | 695 initWithWindows:@[ SerializeWebStateList(_webStateList.get()) ]]; |
701 } | 696 } |
702 | 697 |
703 - (void)postNotificationName:(NSString*)notificationName withTab:(Tab*)tab { | |
704 // A scoped_nsobject is used rather than an NSDictionary with static | |
705 // initializer dictionaryWithObject, because that approach adds the dictionary | |
706 // to the autorelease pool, which in turn holds Tab alive longer than | |
707 // necessary. | |
708 base::scoped_nsobject<NSDictionary> userInfo( | |
709 [[NSDictionary alloc] initWithObjectsAndKeys:tab, kTabModelTabKey, nil]); | |
710 [[NSNotificationCenter defaultCenter] postNotificationName:notificationName | |
711 object:self | |
712 userInfo:userInfo]; | |
713 } | |
714 | |
715 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window | 698 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window |
716 persistState:(BOOL)persistState { | 699 persistState:(BOOL)persistState { |
717 DCHECK(_browserState); | 700 DCHECK(_browserState); |
718 DCHECK(window); | 701 DCHECK(window); |
719 | 702 |
720 if (!window.sessions.count) | 703 if (!window.sessions.count) |
721 return NO; | 704 return NO; |
722 | 705 |
723 int oldCount = _webStateList->count(); | 706 int oldCount = _webStateList->count(); |
724 DCHECK_GE(oldCount, 0); | 707 DCHECK_GE(oldCount, 0); |
725 | 708 |
726 web::WebState::CreateParams createParams(_browserState); | 709 web::WebState::CreateParams createParams(_browserState); |
727 DeserializeWebStateList( | 710 DeserializeWebStateList( |
728 _webStateList.get(), window, | 711 _webStateList.get(), window, |
729 base::BindRepeating(&web::WebState::CreateWithStorageSession, | 712 base::BindRepeating(&web::WebState::CreateWithStorageSession, |
730 createParams)); | 713 createParams)); |
731 | 714 |
732 DCHECK_GT(_webStateList->count(), oldCount); | 715 DCHECK_GT(_webStateList->count(), oldCount); |
733 int restoredCount = _webStateList->count() - oldCount; | 716 int restoredCount = _webStateList->count() - oldCount; |
734 DCHECK_EQ(window.sessions.count, static_cast<NSUInteger>(restoredCount)); | 717 DCHECK_EQ(window.sessions.count, static_cast<NSUInteger>(restoredCount)); |
735 | 718 |
736 scoped_refptr<web::CertificatePolicyCache> policyCache = | 719 scoped_refptr<web::CertificatePolicyCache> policyCache = |
737 web::BrowserState::GetCertificatePolicyCache(_browserState); | 720 web::BrowserState::GetCertificatePolicyCache(_browserState); |
738 | 721 |
739 base::scoped_nsobject<NSMutableArray<Tab*>> restoredTabs( | 722 NSMutableArray<Tab*>* restoredTabs = |
740 [[NSMutableArray alloc] initWithCapacity:window.sessions.count]); | 723 [[NSMutableArray alloc] initWithCapacity:window.sessions.count]; |
741 | 724 |
742 for (int index = oldCount; index < _webStateList->count(); ++index) { | 725 for (int index = oldCount; index < _webStateList->count(); ++index) { |
743 web::WebState* webState = _webStateList->GetWebStateAt(index); | 726 web::WebState* webState = _webStateList->GetWebStateAt(index); |
744 Tab* tab = LegacyTabHelper::GetTabForWebState(webState); | 727 Tab* tab = LegacyTabHelper::GetTabForWebState(webState); |
745 | 728 |
746 webState->SetWebUsageEnabled(webUsageEnabled_ ? true : false); | 729 webState->SetWebUsageEnabled(webUsageEnabled_ ? true : false); |
747 tab.webController.usePlaceholderOverlay = YES; | 730 tab.webController.usePlaceholderOverlay = YES; |
748 | 731 |
749 // Restore the CertificatePolicyCache (note that webState is invalid after | 732 // Restore the CertificatePolicyCache (note that webState is invalid after |
750 // passing it via move semantic to -initWithWebState:model:). | 733 // passing it via move semantic to -initWithWebState:model:). |
751 UpdateCertificatePolicyCacheFromWebState(policyCache, [tab webState]); | 734 UpdateCertificatePolicyCacheFromWebState(policyCache, [tab webState]); |
752 [restoredTabs addObject:tab]; | 735 [restoredTabs addObject:tab]; |
753 } | 736 } |
754 | 737 |
755 // If there was only one tab and it was the new tab page, clobber it. | 738 // If there was only one tab and it was the new tab page, clobber it. |
756 BOOL closedNTPTab = NO; | 739 BOOL closedNTPTab = NO; |
757 if (oldCount == 1) { | 740 if (oldCount == 1) { |
758 Tab* tab = [self tabAtIndex:0]; | 741 Tab* tab = [self tabAtIndex:0]; |
759 if (tab.url == GURL(kChromeUINewTabURL)) { | 742 if (tab.url == GURL(kChromeUINewTabURL)) { |
760 [self closeTab:tab]; | 743 [self closeTab:tab]; |
761 closedNTPTab = YES; | 744 closedNTPTab = YES; |
762 oldCount = 0; | 745 oldCount = 0; |
763 } | 746 } |
764 } | 747 } |
765 if (_tabUsageRecorder) | 748 if (_tabUsageRecorder) |
766 _tabUsageRecorder->InitialRestoredTabs(self.currentTab, restoredTabs.get()); | 749 _tabUsageRecorder->InitialRestoredTabs(self.currentTab, restoredTabs); |
767 return closedNTPTab; | 750 return closedNTPTab; |
768 } | 751 } |
769 | 752 |
770 #pragma mark - Notification Handlers | 753 #pragma mark - Notification Handlers |
771 | 754 |
772 // Called when UIApplicationWillResignActiveNotification is received. | 755 // Called when UIApplicationWillResignActiveNotification is received. |
773 - (void)willResignActive:(NSNotification*)notify { | 756 - (void)willResignActive:(NSNotification*)notify { |
774 if (webUsageEnabled_ && self.currentTab) { | 757 if (webUsageEnabled_ && self.currentTab) { |
775 [[SnapshotCache sharedInstance] | 758 [[SnapshotCache sharedInstance] |
776 willBeSavedGreyWhenBackgrounding:self.currentTab.tabId]; | 759 willBeSavedGreyWhenBackgrounding:self.currentTab.tabId]; |
(...skipping 28 matching lines...) Expand all Loading... |
805 } | 788 } |
806 | 789 |
807 // Called when UIApplicationWillEnterForegroundNotification is received. | 790 // Called when UIApplicationWillEnterForegroundNotification is received. |
808 - (void)applicationWillEnterForeground:(NSNotification*)notify { | 791 - (void)applicationWillEnterForeground:(NSNotification*)notify { |
809 if (_tabUsageRecorder) { | 792 if (_tabUsageRecorder) { |
810 _tabUsageRecorder->AppWillEnterForeground(); | 793 _tabUsageRecorder->AppWillEnterForeground(); |
811 } | 794 } |
812 } | 795 } |
813 | 796 |
814 @end | 797 @end |
OLD | NEW |