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

Side by Side Diff: ios/clean/chrome/browser/ui/tab_collection/tab_collection_mediator.mm

Issue 2820493002: Use a scoped WebStateListObserverBridge. (Closed)
Patch Set: Feedback Created 3 years, 8 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
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
27 - (instancetype)init {
28 self = [super init];
29 if (self) {
30 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self);
31 _scopedWebStateListObserver = base::MakeUnique<
32 ScopedObserver<WebStateList, WebStateListObserverBridge>>(
33 _webStateListObserver.get());
34 }
35 return self;
36 }
37
24 - (void)dealloc { 38 - (void)dealloc {
25 [self disconnect]; 39 [self disconnect];
26 } 40 }
27 41
28 #pragma mark - Public 42 #pragma mark - Public
29 43
30 - (void)disconnect { 44 - (void)disconnect {
31 self.webStateList = nullptr; 45 self.webStateList = nullptr;
32 } 46 }
33 47
34 #pragma mark - Properties 48 #pragma mark - Properties
35 49
36 - (void)setWebStateList:(WebStateList*)webStateList { 50 - (void)setWebStateList:(WebStateList*)webStateList {
37 if (_webStateList) { 51 _scopedWebStateListObserver->RemoveAll();
38 _webStateList->RemoveObserver(_webStateListObserver.get());
39 _webStateListObserver.reset();
40 }
41 _webStateList = webStateList; 52 _webStateList = webStateList;
42 if (!_webStateList) { 53 if (!_webStateList) {
43 return; 54 return;
44 } 55 }
45 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); 56 _scopedWebStateListObserver->Add(_webStateList);
46 _webStateList->AddObserver(_webStateListObserver.get());
47 } 57 }
48 58
49 #pragma mark - TabCollectionDataSource 59 #pragma mark - TabCollectionDataSource
50 60
51 - (int)numberOfTabs { 61 - (int)numberOfTabs {
52 return self.webStateList->count(); 62 return self.webStateList->count();
53 } 63 }
54 64
55 - (NSString*)titleAtIndex:(int)index { 65 - (NSString*)titleAtIndex:(int)index {
56 return [self titleFromWebState:self.webStateList->GetWebStateAt(index)]; 66 return [self titleFromWebState:self.webStateList->GetWebStateAt(index)];
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 // PLACEHOLDER: Use real webstate title in the future. 124 // PLACEHOLDER: Use real webstate title in the future.
115 GURL url = webState->GetVisibleURL(); 125 GURL url = webState->GetVisibleURL();
116 NSString* urlText = @"<New Tab>"; 126 NSString* urlText = @"<New Tab>";
117 if (url.is_valid()) { 127 if (url.is_valid()) {
118 urlText = base::SysUTF8ToNSString(url.spec()); 128 urlText = base::SysUTF8ToNSString(url.spec());
119 } 129 }
120 return urlText; 130 return urlText;
121 } 131 }
122 132
123 @end 133 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698