Chromium Code Reviews| Index: ios/chrome/browser/ui/ntp/google_landing_mediator.mm |
| diff --git a/ios/chrome/browser/ui/ntp/google_landing_mediator.mm b/ios/chrome/browser/ui/ntp/google_landing_mediator.mm |
| index f475f05e2e2cb11c3ddfe9c2707b1090268cd193..e9e8f77494ff8defb05394344c16a1b01dfb146d 100644 |
| --- a/ios/chrome/browser/ui/ntp/google_landing_mediator.mm |
| +++ b/ios/chrome/browser/ui/ntp/google_landing_mediator.mm |
| @@ -31,6 +31,8 @@ |
| #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" |
| #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
| #import "ios/chrome/browser/ui/url_loader.h" |
| +#import "ios/chrome/browser/web_state_list/web_state_list.h" |
| +#import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
| #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
| @@ -79,7 +81,8 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| } // namespace google_landing |
| -@interface GoogleLandingMediator ()<MostVisitedSitesObserving> { |
| +@interface GoogleLandingMediator ()<MostVisitedSitesObserving, |
| + WebStateListObserving> { |
| // The ChromeBrowserState associated with this mediator. |
| ios::ChromeBrowserState* _browserState; // Weak. |
| @@ -112,7 +115,9 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate; |
| - base::scoped_nsobject<TabModel> _tabModel; |
| + // Observes the WebStateList so that this mediator can update the UI when the |
| + // active WebState changes. |
| + std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| // What's new promo. |
| std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; |
| @@ -121,6 +126,9 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| // Consumer to handle google landing update notifications. |
| @property(nonatomic) id<GoogleLandingConsumer> consumer; |
| +// The WebStateList that is being observed by this mediator. |
| +@property(nonatomic, assign) WebStateList* webStateList; |
| + |
| // Perform initial setup. |
| - (void)setUp; |
| @@ -129,13 +137,14 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| @implementation GoogleLandingMediator |
| @synthesize consumer = _consumer; |
| +@synthesize webStateList = _webStateList; |
| - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer |
| browserState:(ios::ChromeBrowserState*)browserState |
| loader:(id<UrlLoader>)loader |
| focuser:(id<OmniboxFocuser>)focuser |
| webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate |
| - tabModel:(TabModel*)tabModel { |
| + webStateList:(WebStateList*)webStateList { |
| self = [super init]; |
| if (self) { |
| self.consumer = consumer; |
| @@ -143,13 +152,18 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| _loader = loader; |
| _focuser.reset(focuser); |
| _webToolbarDelegate.reset(webToolbarDelegate); |
| - _tabModel.reset([tabModel retain]); |
| + _webStateList = webStateList; |
| + |
| + _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
|
rohitrao (ping after 24h)
2017/04/19 19:26:33
Louis has an example CL that uses a ScopedObserver
justincohen
2017/04/19 21:12:23
Acknowledged.
|
| + _webStateList->AddObserver(_webStateListObserver.get()); |
| + |
| [self setUp]; |
| } |
| return self; |
| } |
| - (void)dealloc { |
| + _webStateList->RemoveObserver(_webStateListObserver.get()); |
|
rohitrao (ping after 24h)
2017/04/19 19:26:33
dealloc might be too late, if this object has its
justincohen
2017/04/19 21:12:23
I think the view's life can be extended, but not t
|
| [[NSNotificationCenter defaultCenter] removeObserver:self.consumer]; |
| [super dealloc]; |
| } |
| @@ -159,6 +173,7 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| self.consumer.voiceSearchEnabled = ios::GetChromeBrowserProvider() |
| ->GetVoiceSearchProvider() |
| ->IsVoiceSearchEnabled(); |
| + self.consumer.tabCount = self.webStateList->count(); |
| // Set up template URL service to listen for default search engine changes. |
| _templateURLService = |
| @@ -244,6 +259,20 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| } |
| } |
| +#pragma mark - WebStateListObserving |
| + |
| +- (void)webStateList:(WebStateList*)webStateList |
| + didInsertWebState:(web::WebState*)webState |
| + atIndex:(int)index { |
| + self.consumer.tabCount = self.webStateList->count(); |
| +} |
| + |
| +- (void)webStateList:(WebStateList*)webStateList |
| + didDetachWebState:(web::WebState*)webState |
| + atIndex:(int)atIndex { |
| + self.consumer.tabCount = self.webStateList->count(); |
| +} |
| + |
| #pragma mark - GoogleLandingDataSource |
| - (NSUInteger)maxSitesShown { |
| @@ -291,10 +320,6 @@ void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| return _webToolbarDelegate; |
| } |
| -- (TabModel*)tabModel { |
| - return _tabModel; |
| -} |
| - |
| - (BOOL)promoCanShow { |
| return _notification_promo && _notification_promo->CanShow(); |
| } |