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