Index: ios/chrome/browser/tabs/tab_model_selected_tab_observer.mm |
diff --git a/ios/chrome/browser/tabs/tab_model_selected_tab_observer.mm b/ios/chrome/browser/tabs/tab_model_selected_tab_observer.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fb9846a13ae208ed03bd5eff0a4104d569ffc4c0 |
--- /dev/null |
+++ b/ios/chrome/browser/tabs/tab_model_selected_tab_observer.mm |
@@ -0,0 +1,62 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ios/chrome/browser/tabs/tab_model_selected_tab_observer.h" |
+ |
+#include "base/logging.h" |
+#import "ios/chrome/browser/tabs/legacy_tab_helper.h" |
+#import "ios/chrome/browser/tabs/tab.h" |
+#import "ios/chrome/browser/tabs/tab_model.h" |
+ |
+#if !defined(__has_feature) || !__has_feature(objc_arc) |
+#error "This file requires ARC support." |
+#endif |
+ |
+@implementation TabModelSelectedTabObserver { |
+ __weak TabModel* _tabModel; |
+} |
+ |
+- (instancetype)initWithTabModel:(TabModel*)tabModel { |
+ DCHECK(tabModel); |
+ if ((self = [super init])) { |
+ _tabModel = tabModel; |
+ } |
+ return self; |
+} |
+ |
+#pragma mark WebStateListObserving |
+ |
+- (void)webStateList:(WebStateList*)webStateList |
+ didChangeActiveWebState:(web::WebState*)newWebState |
+ oldWebState:(web::WebState*)oldWebState |
+ atIndex:(int)atIndex |
+ userAction:(BOOL)userAction { |
+ Tab* oldTab = nil; |
+ Tab* newTab = nil; |
+ if (oldWebState) { |
+ // Save state, such as scroll position, ... of the old selected Tab. |
+ oldTab = LegacyTabHelper::GetTabForWebState(oldWebState); |
+ if (userAction) |
+ [oldTab recordStateInHistory]; |
+ |
+ // Avoid artificially extending the lifetime of oldTab until the global |
+ // autoreleasepool is purged. |
+ @autoreleasepool { |
+ [[NSNotificationCenter defaultCenter] |
+ postNotificationName:kTabModelTabDeselectedNotification |
+ object:@{kTabModelTabKey : oldTab}]; |
+ } |
+ } |
+ |
+ if (newWebState) { |
+ newTab = LegacyTabHelper::GetTabForWebState(newWebState); |
+ [newTab updateLastVisitedTimestamp]; |
+ |
+ // Persist the session state. |
+ if (newTab.loadFinished) |
+ [_tabModel saveSessionImmediately:NO]; |
+ } |
+} |
+ |
+@end |