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 |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 // method and restoring session in the initialiser. | 233 // method and restoring session in the initialiser. |
234 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window | 234 - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window |
235 persistState:(BOOL)persistState; | 235 persistState:(BOOL)persistState; |
236 | 236 |
237 // Helper method to insert |tab| at the given |index| recording |parentTab| as | 237 // Helper method to insert |tab| at the given |index| recording |parentTab| as |
238 // the opener. Broadcasts the proper notifications about the change. The | 238 // the opener. Broadcasts the proper notifications about the change. The |
239 // receiver should be set as the parentTabModel for |tab|; this method doesn't | 239 // receiver should be set as the parentTabModel for |tab|; this method doesn't |
240 // check that. | 240 // check that. |
241 - (void)insertTab:(Tab*)tab atIndex:(NSUInteger)index opener:(Tab*)parentTab; | 241 - (void)insertTab:(Tab*)tab atIndex:(NSUInteger)index opener:(Tab*)parentTab; |
242 | 242 |
| 243 // Helper method to insert |tab| at the given |index| recording |parentTab| as |
| 244 // the opener. Broadcasts the proper notifications about the change. The |
| 245 // receiver should be set as the parentTabModel for |tab|; this method doesn't |
| 246 // check that. |
| 247 - (void)insertTab:(Tab*)tab |
| 248 atIndex:(NSUInteger)index |
| 249 opener:(Tab*)parentTab |
| 250 transition:(ui::PageTransition)transition; |
| 251 |
243 @end | 252 @end |
244 | 253 |
245 @implementation TabModel | 254 @implementation TabModel |
246 | 255 |
247 @synthesize browserState = _browserState; | 256 @synthesize browserState = _browserState; |
248 @synthesize sessionID = _sessionID; | 257 @synthesize sessionID = _sessionID; |
249 @synthesize webUsageEnabled = webUsageEnabled_; | 258 @synthesize webUsageEnabled = webUsageEnabled_; |
250 | 259 |
251 #pragma mark - Overriden | 260 #pragma mark - Overriden |
252 | 261 |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 atIndex:(NSUInteger)index { | 553 atIndex:(NSUInteger)index { |
545 DCHECK(_browserState); | 554 DCHECK(_browserState); |
546 DCHECK_EQ(webState->GetBrowserState(), _browserState); | 555 DCHECK_EQ(webState->GetBrowserState(), _browserState); |
547 base::scoped_nsobject<Tab> tab( | 556 base::scoped_nsobject<Tab> tab( |
548 [[Tab alloc] initWithWebState:std::move(webState) model:self]); | 557 [[Tab alloc] initWithWebState:std::move(webState) model:self]); |
549 [tab webController].webUsageEnabled = webUsageEnabled_; | 558 [tab webController].webUsageEnabled = webUsageEnabled_; |
550 [self insertTab:tab atIndex:index]; | 559 [self insertTab:tab atIndex:index]; |
551 return tab; | 560 return tab; |
552 } | 561 } |
553 | 562 |
554 - (void)insertTab:(Tab*)tab atIndex:(NSUInteger)index opener:(Tab*)parentTab { | 563 - (void)insertTab:(Tab*)tab |
| 564 atIndex:(NSUInteger)index |
| 565 opener:(Tab*)parentTab |
| 566 transition:(ui::PageTransition)transition { |
555 DCHECK(tab); | 567 DCHECK(tab); |
556 DCHECK(![_tabRetainer containsObject:tab]); | 568 DCHECK(![_tabRetainer containsObject:tab]); |
557 DCHECK_LE(index, static_cast<NSUInteger>(INT_MAX)); | |
558 | 569 |
559 [_tabRetainer addObject:tab]; | 570 [_tabRetainer addObject:tab]; |
560 _webStateList.InsertWebState(static_cast<int>(index), tab.webState, | 571 if (index == TabModelConstants::kTabPositionAutomatically) { |
561 parentTab.webState); | 572 _webStateList.AppendWebState(transition, tab.webState, parentTab.webState); |
| 573 } else { |
| 574 DCHECK_LE(index, static_cast<NSUInteger>(INT_MAX)); |
| 575 const int insertion_index = static_cast<int>(index); |
| 576 _webStateList.InsertWebState(insertion_index, tab.webState, |
| 577 parentTab.webState); |
| 578 } |
562 | 579 |
563 // Persist the session due to a new tab being inserted. If this is a | 580 // Persist the session due to a new tab being inserted. If this is a |
564 // background tab (will not become active), saving now will capture the | 581 // background tab (will not become active), saving now will capture the |
565 // state properly. If it does eventually become active, another save will | 582 // state properly. If it does eventually become active, another save will |
566 // be triggered to properly capture the end result. | 583 // be triggered to properly capture the end result. |
567 [self saveSessionImmediately:NO]; | 584 [self saveSessionImmediately:NO]; |
568 | 585 |
569 ++_newTabCount; | 586 ++_newTabCount; |
570 } | 587 } |
571 | 588 |
| 589 - (void)insertTab:(Tab*)tab atIndex:(NSUInteger)index opener:(Tab*)parentTab { |
| 590 DCHECK(tab); |
| 591 DCHECK(![_tabRetainer containsObject:tab]); |
| 592 DCHECK_LE(index, static_cast<NSUInteger>(INT_MAX)); |
| 593 |
| 594 [self insertTab:tab |
| 595 atIndex:index |
| 596 opener:parentTab |
| 597 transition:ui::PAGE_TRANSITION_GENERATED]; |
| 598 } |
| 599 |
572 - (void)insertTab:(Tab*)tab atIndex:(NSUInteger)index { | 600 - (void)insertTab:(Tab*)tab atIndex:(NSUInteger)index { |
| 601 DCHECK(tab); |
| 602 DCHECK(![_tabRetainer containsObject:tab]); |
| 603 DCHECK_LE(index, static_cast<NSUInteger>(INT_MAX)); |
| 604 |
573 [self insertTab:tab atIndex:index opener:GetOpenerForTab(self, tab)]; | 605 [self insertTab:tab atIndex:index opener:GetOpenerForTab(self, tab)]; |
574 } | 606 } |
575 | 607 |
576 - (void)moveTab:(Tab*)tab toIndex:(NSUInteger)toIndex { | 608 - (void)moveTab:(Tab*)tab toIndex:(NSUInteger)toIndex { |
577 DCHECK([_tabRetainer containsObject:tab]); | 609 DCHECK([_tabRetainer containsObject:tab]); |
578 DCHECK_LE(toIndex, static_cast<NSUInteger>(INT_MAX)); | 610 DCHECK_LE(toIndex, static_cast<NSUInteger>(INT_MAX)); |
579 int fromIndex = _webStateList.GetIndexOfWebState(tab.webState); | 611 int fromIndex = _webStateList.GetIndexOfWebState(tab.webState); |
580 _webStateList.MoveWebStateAt(fromIndex, static_cast<int>(toIndex)); | 612 _webStateList.MoveWebStateAt(fromIndex, static_cast<int>(toIndex)); |
581 } | 613 } |
582 | 614 |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 inBackground:(BOOL)inBackground { | 886 inBackground:(BOOL)inBackground { |
855 DCHECK(_browserState); | 887 DCHECK(_browserState); |
856 base::scoped_nsobject<Tab> tab([[Tab alloc] | 888 base::scoped_nsobject<Tab> tab([[Tab alloc] |
857 initWithWindowName:windowName | 889 initWithWindowName:windowName |
858 opener:parentTab | 890 opener:parentTab |
859 openedByDOM:openedByDOM | 891 openedByDOM:openedByDOM |
860 model:self | 892 model:self |
861 browserState:_browserState]); | 893 browserState:_browserState]); |
862 [tab webController].webUsageEnabled = webUsageEnabled_; | 894 [tab webController].webUsageEnabled = webUsageEnabled_; |
863 | 895 |
864 if ((PageTransitionCoreTypeIs(params.transition_type, | 896 [self insertTab:tab |
865 ui::PAGE_TRANSITION_LINK)) && | 897 atIndex:index |
866 (index == TabModelConstants::kTabPositionAutomatically)) { | 898 opener:parentTab |
867 DCHECK(!parentTab || [self indexOfTab:parentTab] != NSNotFound); | 899 transition:params.transition_type]; |
868 // Assume tabs opened via link clicks are part of the same "task" as their | |
869 // parent and are grouped together. | |
870 TabModelOrderConstants::InsertionAdjacency adjacency = | |
871 inBackground ? TabModelOrderConstants::kAdjacentAfter | |
872 : TabModelOrderConstants::kAdjacentBefore; | |
873 index = [_orderController insertionIndexForTab:tab | |
874 transition:params.transition_type | |
875 opener:parentTab | |
876 adjacency:adjacency]; | |
877 } else { | |
878 // For all other types, respect what was passed to us, normalizing values | |
879 // that are too large. | |
880 if (index >= self.count) | |
881 index = [_orderController insertionIndexForAppending]; | |
882 } | |
883 | |
884 if (PageTransitionCoreTypeIs(params.transition_type, | |
885 ui::PAGE_TRANSITION_TYPED) && | |
886 index == self.count) { | |
887 // Also, any tab opened at the end of the TabStrip with a "TYPED" | |
888 // transition inherit group as well. This covers the cases where the user | |
889 // creates a New Tab (e.g. Ctrl+T, or clicks the New Tab button), or types | |
890 // in the address bar and presses Alt+Enter. This allows for opening a new | |
891 // Tab to quickly look up something. When this Tab is closed, the old one | |
892 // is re-selected, not the next-adjacent. | |
893 // TODO(crbug.com/661988): Make this work. | |
894 } | |
895 | |
896 [self insertTab:tab atIndex:index opener:parentTab]; | |
897 | 900 |
898 if (!inBackground && _tabUsageRecorder) | 901 if (!inBackground && _tabUsageRecorder) |
899 _tabUsageRecorder->TabCreatedForSelection(tab); | 902 _tabUsageRecorder->TabCreatedForSelection(tab); |
900 | 903 |
901 [[tab webController] loadWithParams:params]; | 904 [[tab webController] loadWithParams:params]; |
902 // Force the page to start loading even if it's in the background. | 905 // Force the page to start loading even if it's in the background. |
903 if (webUsageEnabled_) | 906 if (webUsageEnabled_) |
904 [[tab webController] triggerPendingLoad]; | 907 [[tab webController] triggerPendingLoad]; |
905 NSDictionary* userInfo = @{ | 908 NSDictionary* userInfo = @{ |
906 kTabModelTabKey : tab, | 909 kTabModelTabKey : tab, |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1105 | 1108 |
1106 @implementation TabModel (PrivateForTestingOnly) | 1109 @implementation TabModel (PrivateForTestingOnly) |
1107 | 1110 |
1108 - (Tab*)addTabWithURL:(const GURL&)URL | 1111 - (Tab*)addTabWithURL:(const GURL&)URL |
1109 referrer:(const web::Referrer&)referrer | 1112 referrer:(const web::Referrer&)referrer |
1110 windowName:(NSString*)windowName { | 1113 windowName:(NSString*)windowName { |
1111 return [self insertTabWithURL:URL | 1114 return [self insertTabWithURL:URL |
1112 referrer:referrer | 1115 referrer:referrer |
1113 windowName:windowName | 1116 windowName:windowName |
1114 opener:nil | 1117 opener:nil |
1115 atIndex:[_orderController insertionIndexForAppending]]; | 1118 atIndex:self.count]; |
1116 } | 1119 } |
1117 | 1120 |
1118 - (Tab*)insertTabWithURL:(const GURL&)URL | 1121 - (Tab*)insertTabWithURL:(const GURL&)URL |
1119 referrer:(const web::Referrer&)referrer | 1122 referrer:(const web::Referrer&)referrer |
1120 windowName:(NSString*)windowName | 1123 windowName:(NSString*)windowName |
1121 opener:(Tab*)parentTab | 1124 opener:(Tab*)parentTab |
1122 atIndex:(NSUInteger)index { | 1125 atIndex:(NSUInteger)index { |
1123 DCHECK(_browserState); | 1126 DCHECK(_browserState); |
1124 base::scoped_nsobject<Tab> tab([[Tab alloc] | 1127 base::scoped_nsobject<Tab> tab([[Tab alloc] |
1125 initWithWindowName:windowName | 1128 initWithWindowName:windowName |
1126 opener:parentTab | 1129 opener:parentTab |
1127 openedByDOM:NO | 1130 openedByDOM:NO |
1128 model:self | 1131 model:self |
1129 browserState:_browserState]); | 1132 browserState:_browserState]); |
1130 web::NavigationManager::WebLoadParams params(URL); | 1133 web::NavigationManager::WebLoadParams params(URL); |
1131 params.referrer = referrer; | 1134 params.referrer = referrer; |
1132 params.transition_type = ui::PAGE_TRANSITION_TYPED; | 1135 params.transition_type = ui::PAGE_TRANSITION_TYPED; |
1133 [[tab webController] loadWithParams:params]; | 1136 [[tab webController] loadWithParams:params]; |
1134 [tab webController].webUsageEnabled = webUsageEnabled_; | 1137 [tab webController].webUsageEnabled = webUsageEnabled_; |
1135 [self insertTab:tab atIndex:index opener:parentTab]; | 1138 [self insertTab:tab atIndex:index opener:parentTab]; |
1136 return tab; | 1139 return tab; |
1137 } | 1140 } |
1138 | 1141 |
1139 @end | 1142 @end |
OLD | NEW |