| 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/chrome/browser/ui/ntp/google_landing_mediator.h" | 5 #import "ios/chrome/browser/ui/ntp/google_landing_mediator.h" |
| 6 | 6 |
| 7 #import "base/ios/weak_nsobject.h" | 7 #import "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/metrics/user_metrics.h" | 9 #include "base/metrics/user_metrics.h" |
| 10 #include "base/metrics/user_metrics_action.h" | 10 #include "base/metrics/user_metrics_action.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #import "ios/chrome/browser/ntp_tiles/most_visited_sites_observer_bridge.h" | 24 #import "ios/chrome/browser/ntp_tiles/most_visited_sites_observer_bridge.h" |
| 25 #include "ios/chrome/browser/reading_list/reading_list_model_factory.h" | 25 #include "ios/chrome/browser/reading_list/reading_list_model_factory.h" |
| 26 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" | 26 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 27 #import "ios/chrome/browser/ui/browser_view_controller.h" | 27 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 28 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" | 28 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 29 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" | 29 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 30 #import "ios/chrome/browser/ui/ntp/google_landing_consumer.h" | 30 #import "ios/chrome/browser/ui/ntp/google_landing_consumer.h" |
| 31 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" | 31 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" |
| 32 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" | 32 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
| 33 #import "ios/chrome/browser/ui/url_loader.h" | 33 #import "ios/chrome/browser/ui/url_loader.h" |
| 34 #import "ios/chrome/browser/web_state_list/web_state_list.h" | |
| 35 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" | |
| 36 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 34 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 37 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" | 35 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
| 38 | 36 |
| 39 using base::UserMetricsAction; | 37 using base::UserMetricsAction; |
| 40 | 38 |
| 41 namespace { | 39 namespace { |
| 42 | 40 |
| 43 const NSInteger kMaxNumMostVisitedFavicons = 8; | 41 const NSInteger kMaxNumMostVisitedFavicons = 8; |
| 44 | 42 |
| 45 } // namespace | 43 } // namespace |
| (...skipping 28 matching lines...) Expand all Loading... |
| 74 SearchEngineObserver::~SearchEngineObserver() { | 72 SearchEngineObserver::~SearchEngineObserver() { |
| 75 _templateURLService->RemoveObserver(this); | 73 _templateURLService->RemoveObserver(this); |
| 76 } | 74 } |
| 77 | 75 |
| 78 void SearchEngineObserver::OnTemplateURLServiceChanged() { | 76 void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| 79 [_owner updateShowLogo]; | 77 [_owner updateShowLogo]; |
| 80 } | 78 } |
| 81 | 79 |
| 82 } // namespace google_landing | 80 } // namespace google_landing |
| 83 | 81 |
| 84 @interface GoogleLandingMediator ()<MostVisitedSitesObserving, | 82 @interface GoogleLandingMediator ()<MostVisitedSitesObserving> { |
| 85 WebStateListObserving> { | |
| 86 // The ChromeBrowserState associated with this mediator. | 83 // The ChromeBrowserState associated with this mediator. |
| 87 ios::ChromeBrowserState* _browserState; // Weak. | 84 ios::ChromeBrowserState* _browserState; // Weak. |
| 88 | 85 |
| 89 // |YES| if impressions were logged already and shouldn't be logged again. | 86 // |YES| if impressions were logged already and shouldn't be logged again. |
| 90 BOOL _recordedPageImpression; | 87 BOOL _recordedPageImpression; |
| 91 | 88 |
| 92 // The designated url loader. | 89 // The designated url loader. |
| 93 id<UrlLoader> _loader; // Weak. | 90 id<UrlLoader> _loader; // Weak. |
| 94 | 91 |
| 95 // Delegate to focus and blur the omnibox. | 92 // Delegate to focus and blur the omnibox. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 _mostVisitedObserverBridge; | 105 _mostVisitedObserverBridge; |
| 109 | 106 |
| 110 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; | 107 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; |
| 111 | 108 |
| 112 // Most visited data from the MostVisitedSites service (copied upon receiving | 109 // Most visited data from the MostVisitedSites service (copied upon receiving |
| 113 // the callback). | 110 // the callback). |
| 114 ntp_tiles::NTPTilesVector _mostVisitedData; | 111 ntp_tiles::NTPTilesVector _mostVisitedData; |
| 115 | 112 |
| 116 base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate; | 113 base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate; |
| 117 | 114 |
| 118 // Observes the WebStateList so that this mediator can update the UI when the | 115 base::scoped_nsobject<TabModel> _tabModel; |
| 119 // active WebState changes. | |
| 120 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | |
| 121 | 116 |
| 122 // What's new promo. | 117 // What's new promo. |
| 123 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; | 118 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; |
| 124 } | 119 } |
| 125 | 120 |
| 126 // Consumer to handle google landing update notifications. | 121 // Consumer to handle google landing update notifications. |
| 127 @property(nonatomic) id<GoogleLandingConsumer> consumer; | 122 @property(nonatomic) id<GoogleLandingConsumer> consumer; |
| 128 | 123 |
| 129 // The WebStateList that is being observed by this mediator. | |
| 130 @property(nonatomic, assign) WebStateList* webStateList; | |
| 131 | |
| 132 // Perform initial setup. | 124 // Perform initial setup. |
| 133 - (void)setUp; | 125 - (void)setUp; |
| 134 | 126 |
| 135 @end | 127 @end |
| 136 | 128 |
| 137 @implementation GoogleLandingMediator | 129 @implementation GoogleLandingMediator |
| 138 | 130 |
| 139 @synthesize consumer = _consumer; | 131 @synthesize consumer = _consumer; |
| 140 @synthesize webStateList = _webStateList; | |
| 141 | 132 |
| 142 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer | 133 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer |
| 143 browserState:(ios::ChromeBrowserState*)browserState | 134 browserState:(ios::ChromeBrowserState*)browserState |
| 144 loader:(id<UrlLoader>)loader | 135 loader:(id<UrlLoader>)loader |
| 145 focuser:(id<OmniboxFocuser>)focuser | 136 focuser:(id<OmniboxFocuser>)focuser |
| 146 webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate | 137 webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate |
| 147 webStateList:(WebStateList*)webStateList { | 138 tabModel:(TabModel*)tabModel { |
| 148 self = [super init]; | 139 self = [super init]; |
| 149 if (self) { | 140 if (self) { |
| 150 _consumer = consumer; | 141 _consumer = consumer; |
| 151 _browserState = browserState; | 142 _browserState = browserState; |
| 152 _loader = loader; | 143 _loader = loader; |
| 153 _focuser.reset(focuser); | 144 _focuser.reset(focuser); |
| 154 _webToolbarDelegate.reset(webToolbarDelegate); | 145 _webToolbarDelegate.reset(webToolbarDelegate); |
| 155 _webStateList = webStateList; | 146 _tabModel.reset([tabModel retain]); |
| 156 | |
| 157 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | |
| 158 _webStateList->AddObserver(_webStateListObserver.get()); | |
| 159 | |
| 160 [self setUp]; | 147 [self setUp]; |
| 161 } | 148 } |
| 162 return self; | 149 return self; |
| 163 } | 150 } |
| 164 | 151 |
| 165 - (void)dealloc { | 152 - (void)dealloc { |
| 166 _webStateList->RemoveObserver(_webStateListObserver.get()); | |
| 167 [[NSNotificationCenter defaultCenter] removeObserver:self.consumer]; | 153 [[NSNotificationCenter defaultCenter] removeObserver:self.consumer]; |
| 168 [super dealloc]; | 154 [super dealloc]; |
| 169 } | 155 } |
| 170 | 156 |
| 171 - (void)setUp { | 157 - (void)setUp { |
| 172 [_consumer setIsOffTheRecord:_browserState->IsOffTheRecord()]; | 158 [_consumer setIsOffTheRecord:_browserState->IsOffTheRecord()]; |
| 173 [_consumer setVoiceSearchIsEnabled:ios::GetChromeBrowserProvider() | 159 [_consumer setVoiceSearchIsEnabled:ios::GetChromeBrowserProvider() |
| 174 ->GetVoiceSearchProvider() | 160 ->GetVoiceSearchProvider() |
| 175 ->IsVoiceSearchEnabled()]; | 161 ->IsVoiceSearchEnabled()]; |
| 176 [_consumer | 162 [_consumer |
| 177 setMaximumMostVisitedSitesShown:[GoogleLandingMediator maxSitesShown]]; | 163 setMaximumMostVisitedSitesShown:[GoogleLandingMediator maxSitesShown]]; |
| 178 [_consumer setTabCount:self.webStateList->count()]; | |
| 179 | 164 |
| 180 // Set up template URL service to listen for default search engine changes. | 165 // Set up template URL service to listen for default search engine changes. |
| 181 _templateURLService = | 166 _templateURLService = |
| 182 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); | 167 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
| 183 _observer.reset( | 168 _observer.reset( |
| 184 new google_landing::SearchEngineObserver(self, _templateURLService)); | 169 new google_landing::SearchEngineObserver(self, _templateURLService)); |
| 185 _templateURLService->Load(); | 170 _templateURLService->Load(); |
| 186 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( | 171 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( |
| 187 _browserState, _loader)); | 172 _browserState, _loader)); |
| 188 [_consumer setLogoVendor:_doodleController]; | 173 [_consumer setLogoVendor:_doodleController]; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 - (void)onIconMadeAvailable:(const GURL&)siteUrl { | 242 - (void)onIconMadeAvailable:(const GURL&)siteUrl { |
| 258 for (size_t i = 0; i < _mostVisitedData.size(); ++i) { | 243 for (size_t i = 0; i < _mostVisitedData.size(); ++i) { |
| 259 const ntp_tiles::NTPTile& ntpTile = _mostVisitedData[i]; | 244 const ntp_tiles::NTPTile& ntpTile = _mostVisitedData[i]; |
| 260 if (ntpTile.url == siteUrl) { | 245 if (ntpTile.url == siteUrl) { |
| 261 [self.consumer mostVisitedIconMadeAvailableAtIndex:i]; | 246 [self.consumer mostVisitedIconMadeAvailableAtIndex:i]; |
| 262 break; | 247 break; |
| 263 } | 248 } |
| 264 } | 249 } |
| 265 } | 250 } |
| 266 | 251 |
| 267 #pragma mark - WebStateListObserving | |
| 268 | |
| 269 - (void)webStateList:(WebStateList*)webStateList | |
| 270 didInsertWebState:(web::WebState*)webState | |
| 271 atIndex:(int)index { | |
| 272 [self.consumer setTabCount:self.webStateList->count()]; | |
| 273 } | |
| 274 | |
| 275 - (void)webStateList:(WebStateList*)webStateList | |
| 276 didDetachWebState:(web::WebState*)webState | |
| 277 atIndex:(int)atIndex { | |
| 278 [self.consumer setTabCount:self.webStateList->count()]; | |
| 279 } | |
| 280 | |
| 281 #pragma mark - GoogleLandingDataSource | 252 #pragma mark - GoogleLandingDataSource |
| 282 | 253 |
| 283 - (void)addBlacklistedURL:(const GURL&)url { | 254 - (void)addBlacklistedURL:(const GURL&)url { |
| 284 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, true); | 255 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, true); |
| 285 } | 256 } |
| 286 | 257 |
| 287 - (void)removeBlacklistedURL:(const GURL&)url { | 258 - (void)removeBlacklistedURL:(const GURL&)url { |
| 288 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, false); | 259 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, false); |
| 289 } | 260 } |
| 290 | 261 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 314 } | 285 } |
| 315 | 286 |
| 316 - (favicon::LargeIconService*)largeIconService { | 287 - (favicon::LargeIconService*)largeIconService { |
| 317 return IOSChromeLargeIconServiceFactory::GetForBrowserState(_browserState); | 288 return IOSChromeLargeIconServiceFactory::GetForBrowserState(_browserState); |
| 318 } | 289 } |
| 319 | 290 |
| 320 - (id<WebToolbarDelegate>)toolbarDelegate { | 291 - (id<WebToolbarDelegate>)toolbarDelegate { |
| 321 return _webToolbarDelegate; | 292 return _webToolbarDelegate; |
| 322 } | 293 } |
| 323 | 294 |
| 295 - (TabModel*)tabModel { |
| 296 return _tabModel; |
| 297 } |
| 298 |
| 324 - (void)promoViewed { | 299 - (void)promoViewed { |
| 325 DCHECK(_notification_promo); | 300 DCHECK(_notification_promo); |
| 326 _notification_promo->HandleViewed(); | 301 _notification_promo->HandleViewed(); |
| 327 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; | 302 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; |
| 328 } | 303 } |
| 329 | 304 |
| 330 - (void)promoTapped { | 305 - (void)promoTapped { |
| 331 DCHECK(_notification_promo); | 306 DCHECK(_notification_promo); |
| 332 _notification_promo->HandleClosed(); | 307 _notification_promo->HandleClosed(); |
| 333 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; | 308 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 383 |
| 409 - (void)onFakeboxBlur { | 384 - (void)onFakeboxBlur { |
| 410 [_focuser onFakeboxBlur]; | 385 [_focuser onFakeboxBlur]; |
| 411 } | 386 } |
| 412 | 387 |
| 413 - (void)onFakeboxAnimationComplete { | 388 - (void)onFakeboxAnimationComplete { |
| 414 [_focuser onFakeboxAnimationComplete]; | 389 [_focuser onFakeboxAnimationComplete]; |
| 415 } | 390 } |
| 416 | 391 |
| 417 @end | 392 @end |
| OLD | NEW |