Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: ios/shared/chrome/browser/tabs/web_state_list_fast_enumeration_helper.mm

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

Powered by Google App Engine
This is Rietveld 408576698