OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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/web_state_list/web_state_list_fast_enumeration_helpe r.h" | 5 #import "ios/chrome/browser/web_state_list/web_state_list_fast_enumeration_helpe r.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdint> | 8 #include <cstdint> |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #import "base/mac/foundation_util.h" | 12 #import "base/mac/foundation_util.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/scoped_observer.h" | |
14 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 15 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
15 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" | 16 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
16 | 17 |
17 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
18 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
19 #endif | 20 #endif |
20 | 21 |
21 @interface WebStateListFastEnumeration | 22 @interface WebStateListFastEnumeration |
22 : NSObject<NSFastEnumeration, WebStateListObserving> | 23 : NSObject<NSFastEnumeration, WebStateListObserving> |
23 | 24 |
24 - (instancetype)initWithWebStateList:(WebStateList*)webStateList | 25 - (instancetype)initWithWebStateList:(WebStateList*)webStateList |
25 proxyFactory:(id<WebStateProxyFactory>)proxyFactory; | 26 proxyFactory:(id<WebStateProxyFactory>)proxyFactory; |
26 | 27 |
27 - (void)shutdown; | 28 - (void)shutdown; |
28 | 29 |
29 @end | 30 @end |
30 | 31 |
31 @implementation WebStateListFastEnumeration { | 32 @implementation WebStateListFastEnumeration { |
32 // The wrapped WebStateList. | 33 // The wrapped WebStateList. |
33 WebStateList* _webStateList; | 34 WebStateList* _webStateList; |
34 | 35 |
35 // Helper that returns Objective-C proxies for WebState objects. | 36 // Helper that returns Objective-C proxies for WebState objects. |
36 id<WebStateProxyFactory> _proxyFactory; | 37 id<WebStateProxyFactory> _proxyFactory; |
37 | 38 |
38 // WebStateListObserverBridge forwarding the events of WebStateList to self. | 39 // WebStateListObserverBridge forwarding the events of WebStateList to self. |
39 std::unique_ptr<WebStateListObserverBridge> _observerBridge; | 40 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
41 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> | |
42 _scopedWebStateListObserver; | |
40 | 43 |
41 // Counter incremented each time the WebStateList is mutated. | 44 // Counter incremented each time the WebStateList is mutated. |
42 unsigned long _mutationCounter; | 45 unsigned long _mutationCounter; |
43 } | 46 } |
44 | 47 |
45 - (instancetype)initWithWebStateList:(WebStateList*)webStateList | 48 - (instancetype)initWithWebStateList:(WebStateList*)webStateList |
46 proxyFactory:(id<WebStateProxyFactory>)proxyFactory { | 49 proxyFactory:(id<WebStateProxyFactory>)proxyFactory { |
47 DCHECK(webStateList); | 50 DCHECK(webStateList); |
48 DCHECK(proxyFactory); | 51 DCHECK(proxyFactory); |
49 if ((self = [super init])) { | 52 if ((self = [super init])) { |
50 _webStateList = webStateList; | 53 _webStateList = webStateList; |
51 _proxyFactory = proxyFactory; | 54 _proxyFactory = proxyFactory; |
52 _observerBridge = base::MakeUnique<WebStateListObserverBridge>(self); | 55 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
53 _webStateList->AddObserver(_observerBridge.get()); | 56 _scopedWebStateListObserver = base::MakeUnique< |
57 ScopedObserver<WebStateList, WebStateListObserverBridge>>( | |
58 _webStateListObserver.get()); | |
59 _scopedWebStateListObserver->Add(_webStateList); | |
54 } | 60 } |
55 return self; | 61 return self; |
56 } | 62 } |
57 | 63 |
58 - (void)shutdown { | 64 - (void)shutdown { |
59 _webStateList->RemoveObserver(_observerBridge.get()); | |
sdefresne
2017/04/20 10:02:07
This is incorrect, the registration needs to happe
lpromero
2017/04/20 16:25:13
Done.
| |
60 _webStateList = nullptr; | 65 _webStateList = nullptr; |
61 ++_mutationCounter; | 66 ++_mutationCounter; |
62 } | 67 } |
63 | 68 |
64 - (void)dealloc { | 69 - (void)dealloc { |
65 DCHECK(!_webStateList) << "-shutdown must be called before -dealloc"; | 70 DCHECK(!_webStateList) << "-shutdown must be called before -dealloc"; |
66 } | 71 } |
67 | 72 |
68 #pragma mark NSFastEnumeration | 73 #pragma mark NSFastEnumeration |
69 | 74 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 WebStateListFastEnumeration* fast_enumeration = | 146 WebStateListFastEnumeration* fast_enumeration = |
142 base::mac::ObjCCastStrict<WebStateListFastEnumeration>( | 147 base::mac::ObjCCastStrict<WebStateListFastEnumeration>( |
143 fast_enumeration_.get()); | 148 fast_enumeration_.get()); |
144 [fast_enumeration shutdown]; | 149 [fast_enumeration shutdown]; |
145 fast_enumeration_.reset(); | 150 fast_enumeration_.reset(); |
146 } | 151 } |
147 | 152 |
148 id<NSFastEnumeration> WebStateListFastEnumerationHelper::GetFastEnumeration() { | 153 id<NSFastEnumeration> WebStateListFastEnumerationHelper::GetFastEnumeration() { |
149 return fast_enumeration_.get(); | 154 return fast_enumeration_.get(); |
150 } | 155 } |
OLD | NEW |