Chromium Code Reviews| 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/tab_model.h" | |
| 13 #import "ios/shared/chrome/browser/tabs/web_state_list.h" | |
| 14 #import "ios/web/public/navigation_item.h" | |
| 15 #import "ios/web/public/navigation_manager.h" | |
| 16 #import "ios/web/public/web_state/web_state.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 20 #error "This file requires ARC support." | |
| 21 #endif | |
| 22 | |
| 23 @implementation TabModelClosingWebStateObserver { | |
| 24 __weak TabModel* _tabModel; | |
| 25 sessions::TabRestoreService* _restoreService; | |
| 26 | |
| 27 // Records whether the last detached WebState was the active WebState. | |
| 28 // If YES, then the session is saved if the WebState is closed. | |
|
rohitrao (ping after 24h)
2017/03/26 23:21:22
We only save sessions when we close the active tab
sdefresne
2017/03/28 15:15:05
We also save the session if the current Tab change
| |
| 29 BOOL _lastDetachedWebStateWasActive; | |
| 30 } | |
| 31 | |
| 32 - (instancetype)initWithTabModel:(TabModel*)tabModel | |
| 33 restoreService:(sessions::TabRestoreService*)restoreService { | |
| 34 DCHECK(tabModel); | |
| 35 self = [super init]; | |
| 36 if (self) { | |
| 37 _tabModel = tabModel; | |
| 38 _restoreService = restoreService; | |
| 39 _lastDetachedWebStateWasActive = NO; | |
| 40 } | |
| 41 return self; | |
| 42 } | |
| 43 | |
| 44 #pragma mark - WebStateListObserving | |
| 45 | |
| 46 - (void)webStateList:(WebStateList*)webStateList | |
| 47 willDetachWebState:(web::WebState*)webState | |
| 48 atIndex:(int)atIndex { | |
| 49 _lastDetachedWebStateWasActive = webStateList->active_index() == atIndex; | |
| 50 [self recordHistoryForWebState:webState atIndex:atIndex]; | |
| 51 } | |
| 52 | |
| 53 - (void)webStateList:(WebStateList*)webStateList | |
| 54 willCloseWebState:(web::WebState*)webState | |
| 55 atIndex:(int)atIndex { | |
| 56 if (_lastDetachedWebStateWasActive) { | |
| 57 _lastDetachedWebStateWasActive = NO; | |
| 58 [_tabModel saveSessionImmediately:NO]; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 #pragma mark - Private | |
| 63 | |
| 64 - (void)recordHistoryForWebState:(web::WebState*)webState atIndex:(int)atIndex { | |
| 65 // The RestoreService will be null if navigation is off the record. | |
| 66 if (!_restoreService) | |
| 67 return; | |
| 68 | |
| 69 // No need to record history if the tab has no navigation or has only | |
| 70 // presented the NTP or the bookmark UI. | |
| 71 web::NavigationManager* navigationManager = webState->GetNavigationManager(); | |
| 72 if (navigationManager->GetItemCount() <= 1) { | |
| 73 web::NavigationItem* item = navigationManager->GetLastCommittedItem(); | |
| 74 if (!item) | |
| 75 return; | |
| 76 | |
| 77 const base::StringPiece host = item->GetVirtualURL().host_piece(); | |
| 78 if (host == kChromeUINewTabHost || host == kChromeUIBookmarksHost) | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 _restoreService->CreateHistoricalTab( | |
| 83 sessions::IOSLiveTab::GetForWebState(webState), atIndex); | |
| 84 } | |
| 85 | |
| 86 @end | |
| OLD | NEW |