Chromium Code Reviews| 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/scoped_observer.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| 11 #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" |
| 12 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" | 12 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" |
| 13 #include "ios/web/public/web_state/web_state.h" | 13 #include "ios/web/public/web_state/web_state.h" |
| 14 #import "ios/web/public/web_state/web_state_observer_bridge.h" | |
| 14 | 15 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 20 @interface TabCollectionMediator ()<CRWWebStateObserver> | |
|
sczs
2017/05/26 16:12:52
Could we also move the WebstateListObserverving to
edchin
2017/05/26 18:09:00
WSLO is needed by the subclasses so it is exposed.
sczs
2017/05/30 02:32:02
Acknowledged.
| |
| 21 @end | |
| 22 | |
| 19 @implementation TabCollectionMediator { | 23 @implementation TabCollectionMediator { |
| 20 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 24 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 21 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> | 25 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> |
| 22 _scopedWebStateListObserver; | 26 _scopedWebStateListObserver; |
| 27 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; | |
| 23 } | 28 } |
| 24 | 29 |
| 25 @synthesize webStateList = _webStateList; | 30 @synthesize webStateList = _webStateList; |
| 26 @synthesize consumer = _consumer; | 31 @synthesize consumer = _consumer; |
| 27 | 32 |
| 28 - (instancetype)init { | 33 - (instancetype)init { |
| 29 self = [super init]; | 34 self = [super init]; |
| 30 if (self) { | 35 if (self) { |
| 31 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | 36 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 32 _scopedWebStateListObserver = base::MakeUnique< | 37 _scopedWebStateListObserver = base::MakeUnique< |
| 33 ScopedObserver<WebStateList, WebStateListObserverBridge>>( | 38 ScopedObserver<WebStateList, WebStateListObserverBridge>>( |
| 34 _webStateListObserver.get()); | 39 _webStateListObserver.get()); |
| 35 } | 40 } |
| 36 return self; | 41 return self; |
| 37 } | 42 } |
| 38 | 43 |
| 39 - (void)dealloc { | 44 - (void)dealloc { |
| 40 [self disconnect]; | 45 [self disconnect]; |
| 41 } | 46 } |
| 42 | 47 |
| 43 #pragma mark - Public | 48 #pragma mark - Public |
| 44 | 49 |
| 45 - (void)disconnect { | 50 - (void)disconnect { |
| 46 self.webStateList = nullptr; | 51 self.webStateList = nullptr; |
| 52 _webStateObserver.reset(); | |
| 47 } | 53 } |
| 48 | 54 |
| 49 #pragma mark - Properties | 55 #pragma mark - Properties |
| 50 | 56 |
| 51 - (void)setWebStateList:(WebStateList*)webStateList { | 57 - (void)setWebStateList:(WebStateList*)webStateList { |
| 52 _scopedWebStateListObserver->RemoveAll(); | 58 _scopedWebStateListObserver->RemoveAll(); |
| 53 _webStateList = webStateList; | 59 _webStateList = webStateList; |
| 54 [self populateConsumerItems]; | 60 [self populateConsumerItems]; |
| 55 if (_webStateList) { | 61 if (_webStateList) { |
| 56 _scopedWebStateListObserver->Add(_webStateList); | 62 _scopedWebStateListObserver->Add(_webStateList); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 [self.consumer deleteItemAtIndex:index]; | 103 [self.consumer deleteItemAtIndex:index]; |
| 98 } | 104 } |
| 99 | 105 |
| 100 - (void)webStateList:(WebStateList*)webStateList | 106 - (void)webStateList:(WebStateList*)webStateList |
| 101 didChangeActiveWebState:(web::WebState*)newWebState | 107 didChangeActiveWebState:(web::WebState*)newWebState |
| 102 oldWebState:(web::WebState*)oldWebState | 108 oldWebState:(web::WebState*)oldWebState |
| 103 atIndex:(int)atIndex | 109 atIndex:(int)atIndex |
| 104 userAction:(BOOL)userAction { | 110 userAction:(BOOL)userAction { |
| 105 DCHECK(self.consumer); | 111 DCHECK(self.consumer); |
| 106 [self.consumer selectItemAtIndex:atIndex]; | 112 [self.consumer selectItemAtIndex:atIndex]; |
| 113 _webStateObserver = | |
| 114 base::MakeUnique<web::WebStateObserverBridge>(newWebState, self); | |
| 115 } | |
| 116 | |
| 117 #pragma mark - CRWWebStateObserver | |
| 118 | |
| 119 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | |
|
rohitrao (ping after 24h)
2017/05/26 12:44:01
Please add a comment explaining why we listen for
edchin
2017/05/26 18:09:00
Done.
| |
| 120 int index = self.webStateList->GetIndexOfWebState(webState); | |
| 121 [self.consumer | |
| 122 replaceItemAtIndex:index | |
| 123 withItem:[self tabCollectionItemFromWebState:webState]]; | |
| 107 } | 124 } |
| 108 | 125 |
| 109 #pragma mark - Private | 126 #pragma mark - Private |
| 110 | 127 |
| 111 - (TabCollectionItem*)tabCollectionItemFromWebState: | 128 - (TabCollectionItem*)tabCollectionItemFromWebState: |
| 112 (const web::WebState*)webState { | 129 (const web::WebState*)webState { |
| 113 // PLACEHOLDER: Use real webstate title in the future. | 130 // PLACEHOLDER: Use real webstate title in the future. |
| 114 DCHECK(webState); | 131 DCHECK(webState); |
| 115 GURL url = webState->GetVisibleURL(); | 132 GURL url = webState->GetVisibleURL(); |
| 116 NSString* urlText = @"<New Tab>"; | 133 NSString* urlText = @"<New Tab>"; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 133 i)]]; | 150 i)]]; |
| 134 } | 151 } |
| 135 return [items copy]; | 152 return [items copy]; |
| 136 } | 153 } |
| 137 | 154 |
| 138 - (void)populateConsumerItems { | 155 - (void)populateConsumerItems { |
| 139 if (self.consumer && self.webStateList) { | 156 if (self.consumer && self.webStateList) { |
| 140 [self.consumer populateItems:[self tabCollectionItemsFromWebStateList: | 157 [self.consumer populateItems:[self tabCollectionItemsFromWebStateList: |
| 141 self.webStateList]]; | 158 self.webStateList]]; |
| 142 [self.consumer selectItemAtIndex:self.webStateList->active_index()]; | 159 [self.consumer selectItemAtIndex:self.webStateList->active_index()]; |
| 160 web::WebState* webState = self.webStateList->GetActiveWebState(); | |
| 161 _webStateObserver = | |
| 162 base::MakeUnique<web::WebStateObserverBridge>(webState, self); | |
| 143 } | 163 } |
| 144 } | 164 } |
| 145 | 165 |
| 146 @end | 166 @end |
| OLD | NEW |