Index: ios/chrome/browser/tabs/tab_model.mm |
diff --git a/ios/chrome/browser/tabs/tab_model.mm b/ios/chrome/browser/tabs/tab_model.mm |
index f6d21cd57f9bf2b3a05480a80a23ed05b96dfe48..0a768fe2456f01e21323d0f9070d0dc0c3ffecbd 100644 |
--- a/ios/chrome/browser/tabs/tab_model.mm |
+++ b/ios/chrome/browser/tabs/tab_model.mm |
@@ -117,6 +117,27 @@ void CleanCertificatePolicyCache( |
base::Unretained(web_state_list))); |
} |
+// Internal helper function returning the opener for a given WebState by |
+// checking the associated Tab tabId (should be removed once the opener |
+// is passed to the insertTab:atIndex: and replaceTab:withTab: methods). |
+web::WebState* GetOpenerForWebState(const WebStateList& web_state_list, |
+ web::WebState* web_state) { |
+ Tab* tab = LegacyTabHelper::GetTabForWebState(web_state); |
+ NSString* opener_id = |
+ [tab navigationManager]->GetSessionController().openerId; |
+ if (!opener_id) |
+ return nullptr; |
+ |
+ for (int index = 0; index < web_state_list.count(); ++index) { |
+ web::WebState* web_state = web_state_list.GetWebStateAt(index); |
+ Tab* tab = LegacyTabHelper::GetTabForWebState(web_state); |
+ if ([opener_id isEqualToString:tab.tabId]) |
+ return web_state; |
+ } |
+ |
+ return nullptr; |
+} |
+ |
} // anonymous namespace |
@interface TabModelWebStateProxyFactory : NSObject<WebStateProxyFactory> |
@@ -405,68 +426,43 @@ void CleanCertificatePolicyCache( |
} |
- (Tab*)nextTabWithOpener:(Tab*)tab afterTab:(Tab*)afterTab { |
- NSUInteger startIndex = NSNotFound; |
- // Start looking after |afterTab|. If it's not found, start looking after |
- // |tab|. If it's not found either, bail. |
+ int startIndex = WebStateList::kInvalidIndex; |
if (afterTab) |
- startIndex = [self indexOfTab:afterTab]; |
- if (startIndex == NSNotFound) |
- startIndex = [self indexOfTab:tab]; |
- if (startIndex == NSNotFound) |
+ startIndex = _webStateList.GetIndexOfWebState(afterTab.webState); |
+ |
+ if (startIndex == WebStateList::kInvalidIndex) |
+ startIndex = _webStateList.GetIndexOfWebState(tab.webState); |
+ |
+ const int index = _webStateList.GetIndexOfNextWebStateOpenedBy( |
+ tab.webState, startIndex, false); |
+ if (index == WebStateList::kInvalidIndex) |
return nil; |
- NSString* parentID = tab.tabId; |
- for (NSUInteger i = startIndex + 1; i < self.count; ++i) { |
- Tab* current = [self tabAtIndex:i]; |
- DCHECK([current navigationManager]); |
- CRWSessionController* sessionController = |
- [current navigationManager]->GetSessionController(); |
- if ([sessionController.openerId isEqualToString:parentID]) |
- return current; |
- } |
- return nil; |
+ |
+ DCHECK_GE(index, 0); |
+ return [self tabAtIndex:static_cast<NSUInteger>(index)]; |
} |
- (Tab*)lastTabWithOpener:(Tab*)tab { |
- NSUInteger startIndex = [self indexOfTab:tab]; |
- if (startIndex == NSNotFound) |
+ int startIndex = _webStateList.GetIndexOfWebState(tab.webState); |
+ if (startIndex == WebStateList::kInvalidIndex) |
return nil; |
- // There is at least one tab in the model, because otherwise the above check |
- // would have returned. |
- NSString* parentID = tab.tabId; |
- DCHECK([tab navigationManager]); |
- NSInteger parentNavIndex = [tab navigationManager]->GetCurrentItemIndex(); |
- |
- Tab* match = nil; |
- // Find the last tab in the first matching 'group'. A 'group' is a set of |
- // tabs whose opener's id and opener's navigation index match. The navigation |
- // index is used in addition to the session id to detect navigations changes |
- // within the same session. |
- for (NSUInteger i = startIndex + 1; i < self.count; ++i) { |
- Tab* tabToCheck = [self tabAtIndex:i]; |
- DCHECK([tabToCheck navigationManager]); |
- CRWSessionController* sessionController = |
- [tabToCheck navigationManager]->GetSessionController(); |
- if ([sessionController.openerId isEqualToString:parentID] && |
- sessionController.openerNavigationIndex == parentNavIndex) { |
- match = tabToCheck; |
- } else if (match) { |
- break; |
- } |
- } |
- return match; |
+ |
+ const int index = _webStateList.GetIndexOfLastWebStateOpenedBy( |
+ tab.webState, startIndex, true); |
+ if (index == WebStateList::kInvalidIndex) |
+ return nil; |
+ |
+ DCHECK_GE(index, 0); |
+ return [self tabAtIndex:static_cast<NSUInteger>(index)]; |
} |
- (Tab*)openerOfTab:(Tab*)tab { |
- if (![tab navigationManager]) |
- return nil; |
- NSString* openerId = [tab navigationManager]->GetSessionController().openerId; |
- if (!openerId.length) // Short-circuit if opener is empty. |
+ int index = _webStateList.GetIndexOfWebState(tab.webState); |
+ if (index == WebStateList::kInvalidIndex) |
return nil; |
- for (Tab* iteratedTab in self) { |
- if ([iteratedTab.tabId isEqualToString:openerId]) |
- return iteratedTab; |
- } |
- return nil; |
+ |
+ web::WebState* opener = _webStateList.GetOpenerOfWebStateAt(index); |
+ return opener ? LegacyTabHelper::GetTabForWebState(opener) : nil; |
} |
- (Tab*)insertOrUpdateTabWithURL:(const GURL&)URL |
@@ -559,7 +555,9 @@ void CleanCertificatePolicyCache( |
DCHECK_LE(index, static_cast<NSUInteger>(INT_MAX)); |
[_tabRetainer addObject:tab]; |
- _webStateList.InsertWebState(static_cast<int>(index), tab.webState); |
+ _webStateList.InsertWebState( |
+ static_cast<int>(index), tab.webState, |
+ GetOpenerForWebState(_webStateList, tab.webState)); |
// Persist the session due to a new tab being inserted. If this is a |
// background tab (will not become active), saving now will capture the |
@@ -590,7 +588,9 @@ void CleanCertificatePolicyCache( |
[_tabRetainer addObject:newTab]; |
[newTab setParentTabModel:self]; |
- _webStateList.ReplaceWebStateAt(index, newTab.webState); |
+ _webStateList.ReplaceWebStateAt( |
+ index, newTab.webState, |
+ GetOpenerForWebState(_webStateList, newTab.webState)); |
if (self.currentTab == oldTab) |
[self changeSelectedTabFrom:nil to:newTab persistState:NO]; |