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> | |
| 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); |
| 57 } | 63 } |
| 58 } | 64 } |
| 59 | 65 |
| 60 - (void)setConsumer:(id<TabCollectionConsumer>)consumer { | 66 - (void)setConsumer:(id<TabCollectionConsumer>)consumer { |
| 61 _consumer = consumer; | 67 _consumer = consumer; |
| 62 [self populateConsumerItems]; | 68 [self populateConsumerItems]; |
| 63 } | 69 } |
| 64 | 70 |
| 65 #pragma mark - WebStateListObserving | 71 #pragma mark - WebStateListObserving |
| 66 | 72 |
| 67 - (void)webStateList:(WebStateList*)webStateList | 73 - (void)webStateList:(WebStateList*)webStateList |
| 68 didInsertWebState:(web::WebState*)webState | 74 didInsertWebState:(web::WebState*)webState |
| 69 atIndex:(int)index { | 75 atIndex:(int)index { |
| 70 DCHECK(self.consumer); | 76 DCHECK(self.consumer); |
| 71 [self.consumer insertItem:[self tabCollectionItemFromWebState:webState] | 77 [self.consumer insertItem:[self tabCollectionItemFromWebState:webState] |
| 72 atIndex:index]; | 78 atIndex:index |
| 79 selectedIndex:webStateList->active_index()]; | |
| 73 } | 80 } |
| 74 | 81 |
| 75 - (void)webStateList:(WebStateList*)webStateList | 82 - (void)webStateList:(WebStateList*)webStateList |
| 76 didMoveWebState:(web::WebState*)webState | 83 didMoveWebState:(web::WebState*)webState |
| 77 fromIndex:(int)fromIndex | 84 fromIndex:(int)fromIndex |
| 78 toIndex:(int)toIndex { | 85 toIndex:(int)toIndex { |
| 79 DCHECK(self.consumer); | 86 DCHECK(self.consumer); |
| 80 [self.consumer moveItemFromIndex:fromIndex toIndex:toIndex]; | 87 [self.consumer moveItemFromIndex:fromIndex |
| 88 toIndex:toIndex | |
| 89 selectedIndex:webStateList->active_index()]; | |
| 81 } | 90 } |
| 82 | 91 |
| 83 - (void)webStateList:(WebStateList*)webStateList | 92 - (void)webStateList:(WebStateList*)webStateList |
| 84 didReplaceWebState:(web::WebState*)oldWebState | 93 didReplaceWebState:(web::WebState*)oldWebState |
| 85 withWebState:(web::WebState*)newWebState | 94 withWebState:(web::WebState*)newWebState |
| 86 atIndex:(int)index { | 95 atIndex:(int)index { |
| 87 DCHECK(self.consumer); | 96 DCHECK(self.consumer); |
| 88 [self.consumer | 97 [self.consumer |
| 89 replaceItemAtIndex:index | 98 replaceItemAtIndex:index |
| 90 withItem:[self tabCollectionItemFromWebState:newWebState]]; | 99 withItem:[self tabCollectionItemFromWebState:newWebState]]; |
| 91 } | 100 } |
| 92 | 101 |
| 93 - (void)webStateList:(WebStateList*)webStateList | 102 - (void)webStateList:(WebStateList*)webStateList |
| 94 didDetachWebState:(web::WebState*)webState | 103 didDetachWebState:(web::WebState*)webState |
| 95 atIndex:(int)index { | 104 atIndex:(int)index { |
| 96 DCHECK(self.consumer); | 105 DCHECK(self.consumer); |
| 97 [self.consumer deleteItemAtIndex:index]; | 106 [self.consumer deleteItemAtIndex:index |
| 107 selectedIndex:webStateList->active_index()]; | |
| 98 } | 108 } |
| 99 | 109 |
| 100 - (void)webStateList:(WebStateList*)webStateList | 110 - (void)webStateList:(WebStateList*)webStateList |
| 101 didChangeActiveWebState:(web::WebState*)newWebState | 111 didChangeActiveWebState:(web::WebState*)newWebState |
| 102 oldWebState:(web::WebState*)oldWebState | 112 oldWebState:(web::WebState*)oldWebState |
| 103 atIndex:(int)atIndex | 113 atIndex:(int)atIndex |
| 104 userAction:(BOOL)userAction { | 114 userAction:(BOOL)userAction { |
| 105 DCHECK(self.consumer); | 115 DCHECK(self.consumer); |
| 106 [self.consumer selectItemAtIndex:atIndex]; | 116 [self.consumer selectItemAtIndex:atIndex]; |
| 117 _webStateObserver = | |
| 118 base::MakeUnique<web::WebStateObserverBridge>(newWebState, self); | |
| 119 } | |
| 120 | |
| 121 #pragma mark - CRWWebStateObserver | |
| 122 | |
| 123 // Navigational changes to the web state update the tab collection, such as | |
| 124 // the title and snapshot. | |
| 125 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | |
| 126 int index = self.webStateList->GetIndexOfWebState(webState); | |
| 127 [self.consumer | |
| 128 replaceItemAtIndex:index | |
| 129 withItem:[self tabCollectionItemFromWebState:webState]]; | |
| 107 } | 130 } |
| 108 | 131 |
| 109 #pragma mark - Private | 132 #pragma mark - Private |
|
marq (ping after 24h)
2017/05/30 11:09:43
Each private method needs a comment.
| |
| 110 | 133 |
| 111 - (TabCollectionItem*)tabCollectionItemFromWebState: | 134 - (TabCollectionItem*)tabCollectionItemFromWebState: |
| 112 (const web::WebState*)webState { | 135 (const web::WebState*)webState { |
| 113 // PLACEHOLDER: Use real webstate title in the future. | 136 // PLACEHOLDER: Use real webstate title in the future. |
| 114 DCHECK(webState); | 137 DCHECK(webState); |
| 115 GURL url = webState->GetVisibleURL(); | 138 GURL url = webState->GetVisibleURL(); |
| 116 NSString* urlText = @"<New Tab>"; | 139 NSString* urlText = @"<New Tab>"; |
| 117 if (url.is_valid()) { | 140 if (url.is_valid()) { |
| 118 urlText = base::SysUTF8ToNSString(url.spec()); | 141 urlText = base::SysUTF8ToNSString(url.spec()); |
| 119 } | 142 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 130 [items | 153 [items |
| 131 addObject:[self | 154 addObject:[self |
| 132 tabCollectionItemFromWebState:webStateList->GetWebStateAt( | 155 tabCollectionItemFromWebState:webStateList->GetWebStateAt( |
| 133 i)]]; | 156 i)]]; |
| 134 } | 157 } |
| 135 return [items copy]; | 158 return [items copy]; |
| 136 } | 159 } |
| 137 | 160 |
| 138 - (void)populateConsumerItems { | 161 - (void)populateConsumerItems { |
| 139 if (self.consumer && self.webStateList) { | 162 if (self.consumer && self.webStateList) { |
| 140 [self.consumer populateItems:[self tabCollectionItemsFromWebStateList: | 163 [self.consumer |
| 141 self.webStateList]]; | 164 populateItems:[self |
| 142 [self.consumer selectItemAtIndex:self.webStateList->active_index()]; | 165 tabCollectionItemsFromWebStateList:self.webStateList] |
| 166 selectedIndex:self.webStateList->active_index()]; | |
| 167 web::WebState* webState = self.webStateList->GetActiveWebState(); | |
| 168 _webStateObserver = | |
|
marq (ping after 24h)
2017/05/30 11:09:43
Setting the WSO seems disconnected from populating
edchin
2017/06/01 23:52:28
Moved to a more appropriate place. It should be se
| |
| 169 base::MakeUnique<web::WebStateObserverBridge>(webState, self); | |
| 143 } | 170 } |
| 144 } | 171 } |
| 145 | 172 |
| 146 @end | 173 @end |
| OLD | NEW |