| 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/toolbar/toolbar_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_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" |
| 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| 11 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
| 9 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h" | 12 #import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h" |
| 10 #import "ios/web/public/navigation_manager.h" | 13 #import "ios/web/public/navigation_manager.h" |
| 11 #include "ios/web/public/web_state/web_state.h" | 14 #include "ios/web/public/web_state/web_state.h" |
| 12 #import "ios/web/public/web_state/web_state_observer_bridge.h" | 15 #import "ios/web/public/web_state/web_state_observer_bridge.h" |
| 13 | 16 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." | 18 #error "This file requires ARC support." |
| 16 #endif | 19 #endif |
| 17 | 20 |
| 18 @interface ToolbarMediator ()<CRWWebStateObserver> | 21 @interface ToolbarMediator ()<CRWWebStateObserver, WebStateListObserving> |
| 19 @end | 22 @end |
| 20 | 23 |
| 21 @implementation ToolbarMediator { | 24 @implementation ToolbarMediator { |
| 22 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; | 25 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver; |
| 26 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 27 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> |
| 28 _scopedWebStateListObserver; |
| 23 } | 29 } |
| 24 | 30 |
| 25 @synthesize consumer = _consumer; | 31 @synthesize consumer = _consumer; |
| 26 @synthesize webState = _webState; | 32 @synthesize webState = _webState; |
| 33 @synthesize webStateList = _webStateList; |
| 27 | 34 |
| 28 - (void)dealloc { | 35 - (void)dealloc { |
| 36 [self disconnect]; |
| 37 } |
| 38 |
| 39 #pragma mark - Public |
| 40 |
| 41 - (void)disconnect { |
| 42 self.webStateList = nullptr; |
| 29 _webStateObserver.reset(); | 43 _webStateObserver.reset(); |
| 30 _webState = nullptr; | |
| 31 } | 44 } |
| 32 | 45 |
| 33 #pragma mark - CRWWebStateObserver | 46 #pragma mark - CRWWebStateObserver |
| 34 | 47 |
| 35 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { | 48 - (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success { |
| 36 [self.consumer | 49 [self.consumer |
| 37 setCanGoBack:self.webState->GetNavigationManager()->CanGoBack()]; | 50 setCanGoBack:self.webState->GetNavigationManager()->CanGoBack()]; |
| 38 [self.consumer | 51 [self.consumer |
| 39 setCanGoForward:self.webState->GetNavigationManager()->CanGoForward()]; | 52 setCanGoForward:self.webState->GetNavigationManager()->CanGoForward()]; |
| 40 } | 53 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 51 didChangeLoadingProgress:(double)progress { | 64 didChangeLoadingProgress:(double)progress { |
| 52 [self.consumer setLoadingProgress:progress]; | 65 [self.consumer setLoadingProgress:progress]; |
| 53 } | 66 } |
| 54 | 67 |
| 55 #pragma mark - ChromeBroadcastObserver | 68 #pragma mark - ChromeBroadcastObserver |
| 56 | 69 |
| 57 - (void)broadcastTabStripVisible:(BOOL)visible { | 70 - (void)broadcastTabStripVisible:(BOOL)visible { |
| 58 [self.consumer setTabStripVisible:visible]; | 71 [self.consumer setTabStripVisible:visible]; |
| 59 } | 72 } |
| 60 | 73 |
| 74 #pragma mark - WebStateListObserver |
| 75 |
| 76 - (void)webStateList:(WebStateList*)webStateList |
| 77 didInsertWebState:(web::WebState*)webState |
| 78 atIndex:(int)index { |
| 79 [self.consumer setTabCount:_webStateList->count()]; |
| 80 } |
| 81 |
| 82 - (void)webStateList:(WebStateList*)webStateList |
| 83 didDetachWebState:(web::WebState*)webState |
| 84 atIndex:(int)index { |
| 85 [self.consumer setTabCount:_webStateList->count()]; |
| 86 } |
| 87 |
| 61 #pragma mark - Setters | 88 #pragma mark - Setters |
| 62 | 89 |
| 63 - (void)setWebState:(web::WebState*)webState { | 90 - (void)setWebState:(web::WebState*)webState { |
| 64 _webState = webState; | 91 _webState = webState; |
| 65 _webStateObserver = | 92 _webStateObserver = |
| 66 base::MakeUnique<web::WebStateObserverBridge>(_webState, self); | 93 base::MakeUnique<web::WebStateObserverBridge>(_webState, self); |
| 67 if (self.consumer) { | 94 if (self.consumer) { |
| 68 [self updateConsumer]; | 95 [self updateConsumer]; |
| 69 } | 96 } |
| 70 } | 97 } |
| 71 | 98 |
| 72 - (void)setConsumer:(id<ToolbarConsumer>)consumer { | 99 - (void)setConsumer:(id<ToolbarConsumer>)consumer { |
| 73 _consumer = consumer; | 100 _consumer = consumer; |
| 74 if (self.webState) { | 101 if (self.webState) { |
| 75 [self updateConsumer]; | 102 [self updateConsumer]; |
| 76 } | 103 } |
| 104 if (self.webStateList) { |
| 105 [self.consumer setTabCount:_webStateList->count()]; |
| 106 } |
| 107 } |
| 108 |
| 109 - (void)setWebStateList:(WebStateList*)webStateList { |
| 110 // TODO(crbug.com/727427):Add support for DCHECK(webStateList). |
| 111 _webStateList = webStateList; |
| 112 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 113 _scopedWebStateListObserver = base::MakeUnique< |
| 114 ScopedObserver<WebStateList, WebStateListObserverBridge>>( |
| 115 _webStateListObserver.get()); |
| 116 if (_webStateList) { |
| 117 _scopedWebStateListObserver->Add(_webStateList); |
| 118 if (self.consumer) { |
| 119 [self.consumer setTabCount:_webStateList->count()]; |
| 120 } |
| 121 } |
| 77 } | 122 } |
| 78 | 123 |
| 79 #pragma mark - Helper methods | 124 #pragma mark - Helper methods |
| 80 | 125 |
| 81 // Updates the consumer to match the current WebState. | 126 // Updates the consumer to match the current WebState. |
| 82 - (void)updateConsumer { | 127 - (void)updateConsumer { |
| 83 DCHECK(self.webState); | 128 DCHECK(self.webState); |
| 84 DCHECK(self.consumer); | 129 DCHECK(self.consumer); |
| 85 [self.consumer | 130 [self.consumer |
| 86 setCanGoForward:self.webState->GetNavigationManager()->CanGoForward()]; | 131 setCanGoForward:self.webState->GetNavigationManager()->CanGoForward()]; |
| 87 [self.consumer | 132 [self.consumer |
| 88 setCanGoBack:self.webState->GetNavigationManager()->CanGoBack()]; | 133 setCanGoBack:self.webState->GetNavigationManager()->CanGoBack()]; |
| 89 [self.consumer setIsLoading:self.webState->IsLoading()]; | 134 [self.consumer setIsLoading:self.webState->IsLoading()]; |
| 90 } | 135 } |
| 91 | 136 |
| 92 @end | 137 @end |
| OLD | NEW |