| 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 _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 // TODO(crbug.com/727427):Add support for DCHECK(webStateList). | 58 DCHECK(webStateList); |
| 53 _scopedWebStateListObserver->RemoveAll(); | 59 _scopedWebStateListObserver->RemoveAll(); |
| 54 _webStateList = webStateList; | 60 _webStateList = webStateList; |
| 61 _scopedWebStateListObserver->Add(_webStateList); |
| 62 _webStateObserver = base::MakeUnique<web::WebStateObserverBridge>( |
| 63 self.webStateList->GetActiveWebState(), self); |
| 55 [self populateConsumerItems]; | 64 [self populateConsumerItems]; |
| 56 if (_webStateList) { | |
| 57 _scopedWebStateListObserver->Add(_webStateList); | |
| 58 } | |
| 59 } | 65 } |
| 60 | 66 |
| 61 - (void)setConsumer:(id<TabCollectionConsumer>)consumer { | 67 - (void)setConsumer:(id<TabCollectionConsumer>)consumer { |
| 68 DCHECK(consumer); |
| 62 _consumer = consumer; | 69 _consumer = consumer; |
| 63 [self populateConsumerItems]; | 70 [self populateConsumerItems]; |
| 64 } | 71 } |
| 65 | 72 |
| 66 #pragma mark - WebStateListObserving | 73 #pragma mark - WebStateListObserving |
| 67 | 74 |
| 68 - (void)webStateList:(WebStateList*)webStateList | 75 - (void)webStateList:(WebStateList*)webStateList |
| 69 didInsertWebState:(web::WebState*)webState | 76 didInsertWebState:(web::WebState*)webState |
| 70 atIndex:(int)index { | 77 atIndex:(int)index { |
| 71 DCHECK(self.consumer); | 78 DCHECK(self.consumer); |
| 72 [self.consumer insertItem:[self tabCollectionItemFromWebState:webState] | 79 [self.consumer insertItem:[self tabCollectionItemFromWebState:webState] |
| 73 atIndex:index]; | 80 atIndex:index |
| 81 selectedIndex:webStateList->active_index()]; |
| 74 } | 82 } |
| 75 | 83 |
| 76 - (void)webStateList:(WebStateList*)webStateList | 84 - (void)webStateList:(WebStateList*)webStateList |
| 77 didMoveWebState:(web::WebState*)webState | 85 didMoveWebState:(web::WebState*)webState |
| 78 fromIndex:(int)fromIndex | 86 fromIndex:(int)fromIndex |
| 79 toIndex:(int)toIndex { | 87 toIndex:(int)toIndex { |
| 80 DCHECK(self.consumer); | 88 DCHECK(self.consumer); |
| 81 [self.consumer moveItemFromIndex:fromIndex toIndex:toIndex]; | 89 [self.consumer moveItemFromIndex:fromIndex |
| 90 toIndex:toIndex |
| 91 selectedIndex:webStateList->active_index()]; |
| 82 } | 92 } |
| 83 | 93 |
| 84 - (void)webStateList:(WebStateList*)webStateList | 94 - (void)webStateList:(WebStateList*)webStateList |
| 85 didReplaceWebState:(web::WebState*)oldWebState | 95 didReplaceWebState:(web::WebState*)oldWebState |
| 86 withWebState:(web::WebState*)newWebState | 96 withWebState:(web::WebState*)newWebState |
| 87 atIndex:(int)index { | 97 atIndex:(int)index { |
| 88 DCHECK(self.consumer); | 98 DCHECK(self.consumer); |
| 89 [self.consumer | 99 [self.consumer |
| 90 replaceItemAtIndex:index | 100 replaceItemAtIndex:index |
| 91 withItem:[self tabCollectionItemFromWebState:newWebState]]; | 101 withItem:[self tabCollectionItemFromWebState:newWebState]]; |
| 92 } | 102 } |
| 93 | 103 |
| 94 - (void)webStateList:(WebStateList*)webStateList | 104 - (void)webStateList:(WebStateList*)webStateList |
| 95 didDetachWebState:(web::WebState*)webState | 105 didDetachWebState:(web::WebState*)webState |
| 96 atIndex:(int)index { | 106 atIndex:(int)index { |
| 97 DCHECK(self.consumer); | 107 DCHECK(self.consumer); |
| 98 [self.consumer deleteItemAtIndex:index]; | 108 [self.consumer deleteItemAtIndex:index |
| 109 selectedIndex:webStateList->active_index()]; |
| 99 } | 110 } |
| 100 | 111 |
| 101 - (void)webStateList:(WebStateList*)webStateList | 112 - (void)webStateList:(WebStateList*)webStateList |
| 102 didChangeActiveWebState:(web::WebState*)newWebState | 113 didChangeActiveWebState:(web::WebState*)newWebState |
| 103 oldWebState:(web::WebState*)oldWebState | 114 oldWebState:(web::WebState*)oldWebState |
| 104 atIndex:(int)atIndex | 115 atIndex:(int)atIndex |
| 105 userAction:(BOOL)userAction { | 116 userAction:(BOOL)userAction { |
| 106 DCHECK(self.consumer); | 117 DCHECK(self.consumer); |
| 107 [self.consumer selectItemAtIndex:atIndex]; | 118 [self.consumer setSelectedIndex:atIndex]; |
| 119 _webStateObserver = |
| 120 base::MakeUnique<web::WebStateObserverBridge>(newWebState, self); |
| 121 } |
| 122 |
| 123 #pragma mark - CRWWebStateObserver |
| 124 |
| 125 // Navigational changes to the web state update the tab collection, such as |
| 126 // the title and snapshot. |
| 127 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 128 DCHECK(self.webStateList); |
| 129 DCHECK(self.consumer); |
| 130 int index = self.webStateList->GetIndexOfWebState(webState); |
| 131 [self.consumer |
| 132 replaceItemAtIndex:index |
| 133 withItem:[self tabCollectionItemFromWebState:webState]]; |
| 108 } | 134 } |
| 109 | 135 |
| 110 #pragma mark - Private | 136 #pragma mark - Private |
| 111 | 137 |
| 138 // Constructs a TabCollectionItem from a |webState|. |
| 112 - (TabCollectionItem*)tabCollectionItemFromWebState: | 139 - (TabCollectionItem*)tabCollectionItemFromWebState: |
| 113 (const web::WebState*)webState { | 140 (const web::WebState*)webState { |
| 114 // PLACEHOLDER: Use real webstate title in the future. | 141 // PLACEHOLDER: Use real webstate title in the future. |
| 115 DCHECK(webState); | 142 DCHECK(webState); |
| 116 GURL url = webState->GetVisibleURL(); | 143 GURL url = webState->GetVisibleURL(); |
| 117 NSString* urlText = @"<New Tab>"; | 144 NSString* urlText = @"<New Tab>"; |
| 118 if (url.is_valid()) { | 145 if (url.is_valid()) { |
| 119 urlText = base::SysUTF8ToNSString(url.spec()); | 146 urlText = base::SysUTF8ToNSString(url.spec()); |
| 120 } | 147 } |
| 121 TabCollectionItem* item = [[TabCollectionItem alloc] init]; | 148 TabCollectionItem* item = [[TabCollectionItem alloc] init]; |
| 122 item.title = urlText; | 149 item.title = urlText; |
| 123 return item; | 150 return item; |
| 124 } | 151 } |
| 125 | 152 |
| 153 // Constructs an array of TabCollectionItems from a |webStateList|. |
| 126 - (NSArray<TabCollectionItem*>*)tabCollectionItemsFromWebStateList: | 154 - (NSArray<TabCollectionItem*>*)tabCollectionItemsFromWebStateList: |
| 127 (const WebStateList*)webStateList { | 155 (const WebStateList*)webStateList { |
| 128 DCHECK(webStateList); | 156 DCHECK(webStateList); |
| 129 NSMutableArray<TabCollectionItem*>* items = [[NSMutableArray alloc] init]; | 157 NSMutableArray<TabCollectionItem*>* items = [[NSMutableArray alloc] init]; |
| 130 for (int i = 0; i < webStateList->count(); i++) { | 158 for (int i = 0; i < webStateList->count(); i++) { |
| 131 [items | 159 web::WebState* webState = webStateList->GetWebStateAt(i); |
| 132 addObject:[self | 160 [items addObject:[self tabCollectionItemFromWebState:webState]]; |
| 133 tabCollectionItemFromWebState:webStateList->GetWebStateAt( | |
| 134 i)]]; | |
| 135 } | 161 } |
| 136 return [items copy]; | 162 return [items copy]; |
| 137 } | 163 } |
| 138 | 164 |
| 165 // Constructs an array of TabCollectionItems from the current webStateList |
| 166 // and pushes them to the consumer. |
| 139 - (void)populateConsumerItems { | 167 - (void)populateConsumerItems { |
| 140 if (self.consumer && self.webStateList) { | 168 if (self.consumer && self.webStateList) { |
| 141 [self.consumer populateItems:[self tabCollectionItemsFromWebStateList: | 169 [self.consumer |
| 142 self.webStateList]]; | 170 populateItems:[self |
| 143 [self.consumer selectItemAtIndex:self.webStateList->active_index()]; | 171 tabCollectionItemsFromWebStateList:self.webStateList] |
| 172 selectedIndex:self.webStateList->active_index()]; |
| 144 } | 173 } |
| 145 } | 174 } |
| 146 | 175 |
| 147 @end | 176 @end |
| OLD | NEW |