| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/tabs/tab_model_closing_web_state_observer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string_piece.h" |
| 9 #include "components/sessions/core/tab_restore_service.h" |
| 10 #include "components/sessions/ios/ios_live_tab.h" |
| 11 #include "ios/chrome/browser/chrome_url_constants.h" |
| 12 #import "ios/chrome/browser/tabs/legacy_tab_helper.h" |
| 13 #import "ios/chrome/browser/tabs/tab.h" |
| 14 #import "ios/chrome/browser/tabs/tab_model.h" |
| 15 #import "ios/chrome/browser/tabs/tab_private.h" |
| 16 #import "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 17 #import "ios/web/public/navigation_item.h" |
| 18 #import "ios/web/public/navigation_manager.h" |
| 19 #import "ios/web/public/web_state/web_state.h" |
| 20 #include "url/gurl.h" |
| 21 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." |
| 24 #endif |
| 25 |
| 26 @implementation TabModelClosingWebStateObserver { |
| 27 __weak TabModel* _tabModel; |
| 28 sessions::TabRestoreService* _restoreService; |
| 29 |
| 30 // Records whether the last detached WebState was the active WebState. |
| 31 // If YES, then the session is saved if the WebState is closed. |
| 32 BOOL _lastDetachedWebStateWasActive; |
| 33 } |
| 34 |
| 35 - (instancetype)initWithTabModel:(TabModel*)tabModel |
| 36 restoreService:(sessions::TabRestoreService*)restoreService { |
| 37 DCHECK(tabModel); |
| 38 self = [super init]; |
| 39 if (self) { |
| 40 _tabModel = tabModel; |
| 41 _restoreService = restoreService; |
| 42 _lastDetachedWebStateWasActive = NO; |
| 43 } |
| 44 return self; |
| 45 } |
| 46 |
| 47 #pragma mark - WebStateListObserving |
| 48 |
| 49 - (void)webStateList:(WebStateList*)webStateList |
| 50 willDetachWebState:(web::WebState*)webState |
| 51 atIndex:(int)atIndex { |
| 52 _lastDetachedWebStateWasActive = webStateList->active_index() == atIndex; |
| 53 [self recordHistoryForWebState:webState atIndex:atIndex]; |
| 54 } |
| 55 |
| 56 - (void)webStateList:(WebStateList*)webStateList |
| 57 willCloseWebState:(web::WebState*)webState |
| 58 atIndex:(int)atIndex { |
| 59 if (_lastDetachedWebStateWasActive) { |
| 60 _lastDetachedWebStateWasActive = NO; |
| 61 [_tabModel saveSessionImmediately:NO]; |
| 62 } |
| 63 } |
| 64 |
| 65 #pragma mark - Private |
| 66 |
| 67 - (void)recordHistoryForWebState:(web::WebState*)webState atIndex:(int)atIndex { |
| 68 // The RestoreService will be null if navigation is off the record. |
| 69 if (!_restoreService) |
| 70 return; |
| 71 |
| 72 // No need to record history if the tab has no navigation or has only |
| 73 // presented the NTP or the bookmark UI. |
| 74 web::NavigationManager* navigationManager = webState->GetNavigationManager(); |
| 75 if (navigationManager->GetItemCount() <= 1) { |
| 76 web::NavigationItem* item = navigationManager->GetLastCommittedItem(); |
| 77 if (!item) |
| 78 return; |
| 79 |
| 80 const base::StringPiece host = item->GetVirtualURL().host_piece(); |
| 81 if (host == kChromeUINewTabHost || host == kChromeUIBookmarksHost) |
| 82 return; |
| 83 } |
| 84 |
| 85 _restoreService->CreateHistoricalTab( |
| 86 sessions::IOSLiveTab::GetForWebState(webState), atIndex); |
| 87 } |
| 88 |
| 89 @end |
| OLD | NEW |