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/shared/chrome/browser/tabs/web_state_list_fast_enumeration_helper.h
" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <memory> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #import "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 13 #import "ios/shared/chrome/browser/tabs/web_state_list_observer_bridge.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 19 #pragma mark - WebStateListFastEnumerationHelperObserver |
| 20 |
| 21 // Observer for WebStateListFastEnumerationHelper that will increment the |
| 22 // mutation counter provided in the constructor every time the WebStateList |
| 23 // it is tracking is modified. |
| 24 @interface WebStateListFastEnumerationHelperObserver |
| 25 : NSObject<WebStateListObserver> |
| 26 |
| 27 // Initializes the observer with a pointer to the mutation counter to increment |
| 28 // when the WebStateList is mutated. |
| 29 - (instancetype)initWithMutationCounter:(unsigned long*)mutationCounter |
| 30 NS_DESIGNATED_INITIALIZER; |
| 31 |
| 32 - (instancetype)init NS_UNAVAILABLE; |
| 33 |
| 34 @end |
| 35 |
| 36 @implementation WebStateListFastEnumerationHelperObserver { |
| 37 // Pointer to the mutation counter to increment when the WebStateList is |
| 38 // mutated. |
| 39 unsigned long* _mutationCounter; |
| 40 } |
| 41 |
| 42 - (instancetype)initWithMutationCounter:(unsigned long*)mutationCounter { |
| 43 DCHECK(mutationCounter); |
| 44 if ((self = [super init])) |
| 45 _mutationCounter = mutationCounter; |
| 46 return self; |
| 47 } |
| 48 |
| 49 #pragma mark WebStateListObserver |
| 50 |
| 51 - (void)webStateList:(WebStateList*)webStateList |
| 52 webStateInserted:(web::WebState*)webState |
| 53 atIndex:(NSUInteger)index { |
| 54 ++*_mutationCounter; |
| 55 } |
| 56 |
| 57 - (void)webStateList:(WebStateList*)webStateList |
| 58 webStateMoved:(web::WebState*)webState |
| 59 fromIndex:(NSUInteger)fromIndex |
| 60 toIndex:(NSUInteger)toIndex { |
| 61 ++*_mutationCounter; |
| 62 } |
| 63 |
| 64 - (void)webStateList:(WebStateList*)webStateList |
| 65 webStateReplaced:(web::WebState*)oldWebState |
| 66 byWebState:(web::WebState*)newWebState |
| 67 atIndex:(NSUInteger)index { |
| 68 ++*_mutationCounter; |
| 69 } |
| 70 |
| 71 - (void)webStateList:(WebStateList*)webStateList |
| 72 webStateRemoved:(web::WebState*)webState |
| 73 atIndex:(NSUInteger)index { |
| 74 ++*_mutationCounter; |
| 75 } |
| 76 |
| 77 @end |
| 78 |
| 79 #pragma mark - WebStateListFastEnumerationHelper |
| 80 |
| 81 @implementation WebStateListFastEnumerationHelper { |
| 82 // The wrapped WebStateList. |
| 83 WebStateList* _webStateList; |
| 84 |
| 85 // Helper that returns Objective-C proxies for WebState objects. |
| 86 __weak id<WebStateProxyFactory> _proxyFactory; |
| 87 |
| 88 // WebStateListObserverBridge forwarding the events of WebStateList to self. |
| 89 std::unique_ptr<WebStateListObserverBridge> _observerBridge; |
| 90 |
| 91 // Counter incremented each time the WebStateList is mutated. |
| 92 unsigned long _mutationCounter; |
| 93 } |
| 94 |
| 95 - (instancetype)initWithWebStateList:(WebStateList*)webStateList |
| 96 proxyFactory:(id<WebStateProxyFactory>)proxyFactory { |
| 97 DCHECK(webStateList); |
| 98 DCHECK(proxyFactory); |
| 99 if ((self = [super init])) { |
| 100 _webStateList = webStateList; |
| 101 _proxyFactory = proxyFactory; |
| 102 _observerBridge = base::MakeUnique<WebStateListObserverBridge>( |
| 103 [[WebStateListFastEnumerationHelperObserver alloc] |
| 104 initWithMutationCounter:&_mutationCounter]); |
| 105 _webStateList->AddObserver(_observerBridge.get()); |
| 106 } |
| 107 return self; |
| 108 } |
| 109 |
| 110 - (void)dealloc { |
| 111 _webStateList->RemoveObserver(_observerBridge.get()); |
| 112 } |
| 113 |
| 114 #pragma mark NSFastEnumeration |
| 115 |
| 116 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state |
| 117 objects:(id __unsafe_unretained*)buffer |
| 118 count:(NSUInteger)len { |
| 119 // The number of objects already returned is stored in |state->state|. For |
| 120 // the first iteration, the counter will be zero and the rest of the state |
| 121 // structure need to be initialised. |
| 122 const size_t offset = state->state; |
| 123 if (offset == 0) |
| 124 state->mutationsPtr = &_mutationCounter; |
| 125 |
| 126 DCHECK_LE(offset, _webStateList->count()); |
| 127 const size_t count = std::min<size_t>(len, _webStateList->count() - offset); |
| 128 |
| 129 for (size_t index = 0; index < count; ++index) { |
| 130 web::WebState* webState = _webStateList->GetWebStateAt(offset + index); |
| 131 buffer[index] = [_proxyFactory proxyForWebState:webState]; |
| 132 } |
| 133 |
| 134 state->state += count; |
| 135 state->itemsPtr = buffer; |
| 136 return count; |
| 137 } |
| 138 |
| 139 @end |
OLD | NEW |