| 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/web/navigation/crw_session_controller.h" | 5 #import "ios/web/navigation/crw_session_controller.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 - (void)setPendingItemIndex:(NSInteger)pendingItemIndex { | 148 - (void)setPendingItemIndex:(NSInteger)pendingItemIndex { |
| 149 DCHECK_GE(pendingItemIndex, -1); | 149 DCHECK_GE(pendingItemIndex, -1); |
| 150 DCHECK_LT(pendingItemIndex, static_cast<NSInteger>(self.items.size())); | 150 DCHECK_LT(pendingItemIndex, static_cast<NSInteger>(self.items.size())); |
| 151 _pendingItemIndex = pendingItemIndex; | 151 _pendingItemIndex = pendingItemIndex; |
| 152 DCHECK(_pendingItemIndex == -1 || self.pendingItem); | 152 DCHECK(_pendingItemIndex == -1 || self.pendingItem); |
| 153 } | 153 } |
| 154 | 154 |
| 155 - (BOOL)canPruneAllButLastCommittedItem { |
| 156 return self.currentNavigationIndex != -1 && self.pendingItemIndex == -1 && |
| 157 !self.transientItem; |
| 158 } |
| 159 |
| 155 - (const web::ScopedNavigationItemImplList&)items { | 160 - (const web::ScopedNavigationItemImplList&)items { |
| 156 return _items; | 161 return _items; |
| 157 } | 162 } |
| 158 | 163 |
| 159 - (web::NavigationItemImpl*)currentItem { | 164 - (web::NavigationItemImpl*)currentItem { |
| 160 if (self.transientItem) | 165 if (self.transientItem) |
| 161 return self.transientItem; | 166 return self.transientItem; |
| 162 if (self.pendingItem) | 167 if (self.pendingItem) |
| 163 return self.pendingItem; | 168 return self.pendingItem; |
| 164 return self.lastCommittedItem; | 169 return self.lastCommittedItem; |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 - (void)discardNonCommittedItems { | 484 - (void)discardNonCommittedItems { |
| 480 [self discardTransientItem]; | 485 [self discardTransientItem]; |
| 481 _pendingItem.reset(); | 486 _pendingItem.reset(); |
| 482 self.pendingItemIndex = -1; | 487 self.pendingItemIndex = -1; |
| 483 } | 488 } |
| 484 | 489 |
| 485 - (void)discardTransientItem { | 490 - (void)discardTransientItem { |
| 486 _transientItem.reset(); | 491 _transientItem.reset(); |
| 487 } | 492 } |
| 488 | 493 |
| 489 - (void)insertStateFromSessionController:(CRWSessionController*)sourceSession { | 494 - (void)copyStateFromSessionControllerAndPrune:(CRWSessionController*)source { |
| 490 DCHECK(sourceSession); | 495 DCHECK(source); |
| 496 if (!self.canPruneAllButLastCommittedItem) |
| 497 return; |
| 491 | 498 |
| 492 // The other session may not have any items, in which case there is nothing | 499 // The other session may not have any items, in which case there is nothing |
| 493 // to insert. The other session's currentItem will be bogus in such cases, so | 500 // to insert. |
| 494 // ignore it and return early. | 501 const web::ScopedNavigationItemImplList& sourceItems = source->_items; |
| 495 web::ScopedNavigationItemImplList& sourceItems = sourceSession->_items; | |
| 496 if (sourceItems.empty()) | 502 if (sourceItems.empty()) |
| 497 return; | 503 return; |
| 498 | 504 |
| 499 // Cycle through the items from the other session and insert them before any | 505 // Early return if there's no committed source item. |
| 500 // items from this session. Do not copy anything that comes after the other | 506 if (!source.lastCommittedItem) |
| 501 // session's current item. | 507 return; |
| 502 NSInteger lastIndexToCopy = sourceSession.currentNavigationIndex; | 508 |
| 503 for (NSInteger i = 0; i <= lastIndexToCopy; ++i) { | 509 // Copy |sourceItems| into a new NavigationItemList. |
| 504 std::unique_ptr<web::NavigationItemImpl> sourceItemCopy = | 510 DCHECK_GT(source.currentNavigationIndex, -1); |
| 505 base::MakeUnique<web::NavigationItemImpl>(*sourceItems[i]); | 511 size_t sourceCurrentIndex = |
| 506 _items.insert(_items.begin() + i, std::move(sourceItemCopy)); | 512 static_cast<size_t>(source.currentNavigationIndex); |
| 513 web::ScopedNavigationItemImplList mergedItems(sourceCurrentIndex + 2); |
| 514 for (size_t index = 0; index <= sourceCurrentIndex; ++index) { |
| 515 mergedItems[index] = |
| 516 base::MakeUnique<web::NavigationItemImpl>(*sourceItems[index]); |
| 507 } | 517 } |
| 518 mergedItems.back() = std::move(_items[self.currentNavigationIndex]); |
| 519 |
| 520 // Use |mergedItems| as the session history. |
| 521 std::swap(mergedItems, _items); |
| 508 | 522 |
| 509 // Update state to reflect inserted NavigationItems. | 523 // Update state to reflect inserted NavigationItems. |
| 510 _previousNavigationIndex = -1; | 524 _previousNavigationIndex = -1; |
| 511 _currentNavigationIndex += lastIndexToCopy + 1; | 525 _currentNavigationIndex = self.items.size() - 1; |
| 512 if (self.pendingItemIndex != -1) | |
| 513 self.pendingItemIndex += lastIndexToCopy + 1; | |
| 514 | 526 |
| 515 DCHECK_LT(static_cast<NSUInteger>(_currentNavigationIndex), | 527 DCHECK_LT(static_cast<NSUInteger>(_currentNavigationIndex), |
| 516 self.items.size()); | 528 self.items.size()); |
| 517 DCHECK(self.pendingItemIndex == -1 || self.pendingItem); | |
| 518 } | 529 } |
| 519 | 530 |
| 520 - (void)goToItemAtIndex:(NSInteger)index { | 531 - (void)goToItemAtIndex:(NSInteger)index { |
| 521 if (index < 0 || static_cast<NSUInteger>(index) >= self.items.size()) | 532 if (index < 0 || static_cast<NSUInteger>(index) >= self.items.size()) |
| 522 return; | 533 return; |
| 523 | 534 |
| 524 if (index < _currentNavigationIndex) { | 535 if (index < _currentNavigationIndex) { |
| 525 // Going back. | 536 // Going back. |
| 526 [self discardNonCommittedItems]; | 537 [self discardNonCommittedItems]; |
| 527 } else if (_currentNavigationIndex < index) { | 538 } else if (_currentNavigationIndex < index) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 return item; | 636 return item; |
| 626 } | 637 } |
| 627 | 638 |
| 628 - (BOOL)isRedirectTransitionForItemAtIndex:(size_t)index { | 639 - (BOOL)isRedirectTransitionForItemAtIndex:(size_t)index { |
| 629 DCHECK_LT(index, self.items.size()); | 640 DCHECK_LT(index, self.items.size()); |
| 630 ui::PageTransition transition = self.items[index]->GetTransitionType(); | 641 ui::PageTransition transition = self.items[index]->GetTransitionType(); |
| 631 return (transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK) ? YES : NO; | 642 return (transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK) ? YES : NO; |
| 632 } | 643 } |
| 633 | 644 |
| 634 @end | 645 @end |
| OLD | NEW |