Chromium Code Reviews| 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. |mergedItems| is needs |
| 504 std::unique_ptr<web::NavigationItemImpl> sourceItemCopy = | 510 // to be large enough for all items in |source| preceding |
| 505 base::MakeUnique<web::NavigationItemImpl>(*sourceItems[i]); | 511 // |sourceCurrentIndex|, the |source|'s current item, and |self|'s current |
| 506 _items.insert(_items.begin() + i, std::move(sourceItemCopy)); | 512 // item, which comes out to |sourceCurrentIndex| + 2. |
| 513 DCHECK_GT(source.currentNavigationIndex, -1); | |
| 514 size_t sourceCurrentIndex = | |
| 515 static_cast<size_t>(source.currentNavigationIndex); | |
| 516 web::ScopedNavigationItemImplList mergedItems(sourceCurrentIndex + 2); | |
| 517 for (size_t index = 0; index <= sourceCurrentIndex; ++index) { | |
| 518 mergedItems[index] = | |
| 519 base::MakeUnique<web::NavigationItemImpl>(*sourceItems[index]); | |
| 507 } | 520 } |
| 521 mergedItems.back() = std::move(_items[self.currentNavigationIndex]); | |
|
Eugene But (OOO till 7-30)
2017/03/15 23:22:10
Should this be push_back?
kkhorimoto
2017/03/15 23:28:33
No, push_back() will add an extra pointer at the e
| |
| 522 | |
| 523 // Use |mergedItems| as the session history. | |
| 524 std::swap(mergedItems, _items); | |
| 508 | 525 |
| 509 // Update state to reflect inserted NavigationItems. | 526 // Update state to reflect inserted NavigationItems. |
| 510 _previousNavigationIndex = -1; | 527 _previousNavigationIndex = -1; |
| 511 _currentNavigationIndex += lastIndexToCopy + 1; | 528 _currentNavigationIndex = self.items.size() - 1; |
| 512 if (self.pendingItemIndex != -1) | |
| 513 self.pendingItemIndex += lastIndexToCopy + 1; | |
| 514 | 529 |
| 515 DCHECK_LT(static_cast<NSUInteger>(_currentNavigationIndex), | 530 DCHECK_LT(static_cast<NSUInteger>(_currentNavigationIndex), |
| 516 self.items.size()); | 531 self.items.size()); |
| 517 DCHECK(self.pendingItemIndex == -1 || self.pendingItem); | |
| 518 } | 532 } |
| 519 | 533 |
| 520 - (void)goToItemAtIndex:(NSInteger)index { | 534 - (void)goToItemAtIndex:(NSInteger)index { |
| 521 if (index < 0 || static_cast<NSUInteger>(index) >= self.items.size()) | 535 if (index < 0 || static_cast<NSUInteger>(index) >= self.items.size()) |
| 522 return; | 536 return; |
| 523 | 537 |
| 524 if (index < _currentNavigationIndex) { | 538 if (index < _currentNavigationIndex) { |
| 525 // Going back. | 539 // Going back. |
| 526 [self discardNonCommittedItems]; | 540 [self discardNonCommittedItems]; |
| 527 } else if (_currentNavigationIndex < index) { | 541 } else if (_currentNavigationIndex < index) { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 625 return item; | 639 return item; |
| 626 } | 640 } |
| 627 | 641 |
| 628 - (BOOL)isRedirectTransitionForItemAtIndex:(size_t)index { | 642 - (BOOL)isRedirectTransitionForItemAtIndex:(size_t)index { |
| 629 DCHECK_LT(index, self.items.size()); | 643 DCHECK_LT(index, self.items.size()); |
| 630 ui::PageTransition transition = self.items[index]->GetTransitionType(); | 644 ui::PageTransition transition = self.items[index]->GetTransitionType(); |
| 631 return (transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK) ? YES : NO; | 645 return (transition & ui::PAGE_TRANSITION_IS_REDIRECT_MASK) ? YES : NO; |
| 632 } | 646 } |
| 633 | 647 |
| 634 @end | 648 @end |
| OLD | NEW |