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/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/scoped_observer.h" | |
8 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
9 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
10 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_consumer.h" | 11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_consumer.h" |
11 #include "ios/web/public/web_state/web_state.h" | 12 #include "ios/web/public/web_state/web_state.h" |
12 | 13 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
14 #error "This file requires ARC support." | 15 #error "This file requires ARC support." |
15 #endif | 16 #endif |
16 | 17 |
17 @implementation TabCollectionMediator { | 18 @implementation TabCollectionMediator { |
18 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 19 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
20 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> | |
21 _scopedWebStateListObserver; | |
19 } | 22 } |
20 | 23 |
21 @synthesize webStateList = _webStateList; | 24 @synthesize webStateList = _webStateList; |
22 @synthesize consumer = _consumer; | 25 @synthesize consumer = _consumer; |
23 | 26 |
24 - (void)dealloc { | 27 - (void)dealloc { |
25 [self disconnect]; | 28 [self disconnect]; |
26 } | 29 } |
27 | 30 |
28 #pragma mark - Public | 31 #pragma mark - Public |
29 | 32 |
30 - (void)disconnect { | 33 - (void)disconnect { |
31 self.webStateList = nullptr; | 34 self.webStateList = nullptr; |
32 } | 35 } |
33 | 36 |
34 #pragma mark - Properties | 37 #pragma mark - Properties |
35 | 38 |
36 - (void)setWebStateList:(WebStateList*)webStateList { | 39 - (void)setWebStateList:(WebStateList*)webStateList { |
37 if (_webStateList) { | 40 _scopedWebStateListObserver.reset(); |
sdefresne
2017/04/20 10:02:07
You can use RemoveAll/Add to avoid destroying the
lpromero
2017/04/20 16:25:13
Done, although could we imagine a time when Remove
| |
38 _webStateList->RemoveObserver(_webStateListObserver.get()); | 41 _webStateListObserver.reset(); |
39 _webStateListObserver.reset(); | |
40 } | |
41 _webStateList = webStateList; | 42 _webStateList = webStateList; |
42 if (!_webStateList) { | 43 if (!_webStateList) { |
43 return; | 44 return; |
44 } | 45 } |
45 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | 46 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
46 _webStateList->AddObserver(_webStateListObserver.get()); | 47 _scopedWebStateListObserver = base::MakeUnique< |
48 ScopedObserver<WebStateList, WebStateListObserverBridge>>( | |
49 _webStateListObserver.get()); | |
50 _scopedWebStateListObserver->Add(_webStateList); | |
47 } | 51 } |
48 | 52 |
49 #pragma mark - TabCollectionDataSource | 53 #pragma mark - TabCollectionDataSource |
50 | 54 |
51 - (int)numberOfTabs { | 55 - (int)numberOfTabs { |
52 return self.webStateList->count(); | 56 return self.webStateList->count(); |
53 } | 57 } |
54 | 58 |
55 - (NSString*)titleAtIndex:(int)index { | 59 - (NSString*)titleAtIndex:(int)index { |
56 return [self titleFromWebState:self.webStateList->GetWebStateAt(index)]; | 60 return [self titleFromWebState:self.webStateList->GetWebStateAt(index)]; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 // PLACEHOLDER: Use real webstate title in the future. | 118 // PLACEHOLDER: Use real webstate title in the future. |
115 GURL url = webState->GetVisibleURL(); | 119 GURL url = webState->GetVisibleURL(); |
116 NSString* urlText = @"<New Tab>"; | 120 NSString* urlText = @"<New Tab>"; |
117 if (url.is_valid()) { | 121 if (url.is_valid()) { |
118 urlText = base::SysUTF8ToNSString(url.spec()); | 122 urlText = base::SysUTF8ToNSString(url.spec()); |
119 } | 123 } |
120 return urlText; | 124 return urlText; |
121 } | 125 } |
122 | 126 |
123 @end | 127 @end |
OLD | NEW |