| 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 17 matching lines...) Expand all Loading... |
| 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" | 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" | 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" | 36 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 37 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" | 37 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
| 38 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
| 38 #include "ios/web/public/web_state/web_state.h" | 39 #include "ios/web/public/web_state/web_state.h" |
| 39 | 40 |
| 40 using base::UserMetricsAction; | 41 using base::UserMetricsAction; |
| 41 | 42 |
| 42 namespace { | 43 namespace { |
| 43 | 44 |
| 44 const NSInteger kMaxNumMostVisitedFavicons = 8; | 45 const NSInteger kMaxNumMostVisitedFavicons = 8; |
| 45 | 46 |
| 46 } // namespace | 47 } // namespace |
| 47 | 48 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 75 SearchEngineObserver::~SearchEngineObserver() { | 76 SearchEngineObserver::~SearchEngineObserver() { |
| 76 _templateURLService->RemoveObserver(this); | 77 _templateURLService->RemoveObserver(this); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void SearchEngineObserver::OnTemplateURLServiceChanged() { | 80 void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| 80 [_owner updateShowLogo]; | 81 [_owner updateShowLogo]; |
| 81 } | 82 } |
| 82 | 83 |
| 83 } // namespace google_landing | 84 } // namespace google_landing |
| 84 | 85 |
| 85 @interface GoogleLandingMediator ()<MostVisitedSitesObserving, | 86 @interface GoogleLandingMediator ()<GoogleLandingDataSource, |
| 87 MostVisitedSitesObserving, |
| 86 WebStateListObserving> { | 88 WebStateListObserving> { |
| 87 // The ChromeBrowserState associated with this mediator. | 89 // The ChromeBrowserState associated with this mediator. |
| 88 ios::ChromeBrowserState* _browserState; // Weak. | 90 ios::ChromeBrowserState* _browserState; // Weak. |
| 89 | 91 |
| 90 // |YES| if impressions were logged already and shouldn't be logged again. | 92 // |YES| if impressions were logged already and shouldn't be logged again. |
| 91 BOOL _recordedPageImpression; | 93 BOOL _recordedPageImpression; |
| 92 | 94 |
| 93 // The designated url loader. | |
| 94 id<UrlLoader> _loader; // Weak. | |
| 95 | |
| 96 // Delegate to focus and blur the omnibox. | |
| 97 base::WeakNSProtocol<id<OmniboxFocuser>> _focuser; | |
| 98 | |
| 99 // Controller to fetch and show doodles or a default Google logo. | 95 // Controller to fetch and show doodles or a default Google logo. |
| 100 base::scoped_nsprotocol<id<LogoVendor>> _doodleController; | 96 base::scoped_nsprotocol<id<LogoVendor>> _doodleController; |
| 101 | 97 |
| 102 // Listen for default search engine changes. | 98 // Listen for default search engine changes. |
| 103 std::unique_ptr<google_landing::SearchEngineObserver> _observer; | 99 std::unique_ptr<google_landing::SearchEngineObserver> _observer; |
| 104 TemplateURLService* _templateURLService; // weak | 100 TemplateURLService* _templateURLService; // weak |
| 105 | 101 |
| 106 // A MostVisitedSites::Observer bridge object to get notified of most visited | 102 // A MostVisitedSites::Observer bridge object to get notified of most visited |
| 107 // sites changes. | 103 // sites changes. |
| 108 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> | 104 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> |
| 109 _mostVisitedObserverBridge; | 105 _mostVisitedObserverBridge; |
| 110 | 106 |
| 111 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; | 107 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; |
| 112 | 108 |
| 113 // Most visited data from the MostVisitedSites service (copied upon receiving | 109 // Most visited data from the MostVisitedSites service (copied upon receiving |
| 114 // the callback). | 110 // the callback). |
| 115 ntp_tiles::NTPTilesVector _mostVisitedData; | 111 ntp_tiles::NTPTilesVector _mostVisitedData; |
| 116 | 112 |
| 117 base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate; | |
| 118 | |
| 119 // Observes the WebStateList so that this mediator can update the UI when the | 113 // Observes the WebStateList so that this mediator can update the UI when the |
| 120 // active WebState changes. | 114 // active WebState changes. |
| 121 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 115 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 122 | 116 |
| 123 // What's new promo. | 117 // What's new promo. |
| 124 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; | 118 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; |
| 125 } | 119 } |
| 126 | 120 |
| 127 // Consumer to handle google landing update notifications. | 121 // Consumer to handle google landing update notifications. |
| 128 @property(nonatomic) id<GoogleLandingConsumer> consumer; | 122 @property(nonatomic) id<GoogleLandingConsumer> consumer; |
| 129 | 123 |
| 130 // The WebStateList that is being observed by this mediator. | 124 // The WebStateList that is being observed by this mediator. |
| 131 @property(nonatomic, assign) WebStateList* webStateList; | 125 @property(nonatomic, assign) WebStateList* webStateList; |
| 132 | 126 |
| 127 // The dispatcher for this mediator. |
| 128 @property(nonatomic, assign) id<ChromeExecuteCommand, UrlLoader> dispatcher; |
| 129 |
| 133 // Perform initial setup. | 130 // Perform initial setup. |
| 134 - (void)setUp; | 131 - (void)setUp; |
| 135 | 132 |
| 136 @end | 133 @end |
| 137 | 134 |
| 138 @implementation GoogleLandingMediator | 135 @implementation GoogleLandingMediator |
| 139 | 136 |
| 140 @synthesize consumer = _consumer; | 137 @synthesize consumer = _consumer; |
| 138 @synthesize dispatcher = _dispatcher; |
| 141 @synthesize webStateList = _webStateList; | 139 @synthesize webStateList = _webStateList; |
| 142 | 140 |
| 143 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer | 141 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer |
| 144 browserState:(ios::ChromeBrowserState*)browserState | 142 browserState:(ios::ChromeBrowserState*)browserState |
| 145 loader:(id<UrlLoader>)loader | 143 dispatcher:(id<ChromeExecuteCommand, UrlLoader>)dispatcher |
| 146 focuser:(id<OmniboxFocuser>)focuser | |
| 147 webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate | |
| 148 webStateList:(WebStateList*)webStateList { | 144 webStateList:(WebStateList*)webStateList { |
| 149 self = [super init]; | 145 self = [super init]; |
| 150 if (self) { | 146 if (self) { |
| 151 _consumer = consumer; | 147 _consumer = consumer; |
| 152 _browserState = browserState; | 148 _browserState = browserState; |
| 153 _loader = loader; | 149 _dispatcher = dispatcher; |
| 154 _focuser.reset(focuser); | |
| 155 _webToolbarDelegate.reset(webToolbarDelegate); | |
| 156 _webStateList = webStateList; | 150 _webStateList = webStateList; |
| 157 | 151 |
| 158 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | 152 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 159 _webStateList->AddObserver(_webStateListObserver.get()); | 153 _webStateList->AddObserver(_webStateListObserver.get()); |
| 160 | 154 |
| 161 [self setUp]; | 155 [self setUp]; |
| 162 } | 156 } |
| 163 return self; | 157 return self; |
| 164 } | 158 } |
| 165 | 159 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 183 [_consumer setCanGoBack:nav->CanGoBack()]; | 177 [_consumer setCanGoBack:nav->CanGoBack()]; |
| 184 } | 178 } |
| 185 | 179 |
| 186 // 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. |
| 187 _templateURLService = | 181 _templateURLService = |
| 188 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); | 182 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
| 189 _observer.reset( | 183 _observer.reset( |
| 190 new google_landing::SearchEngineObserver(self, _templateURLService)); | 184 new google_landing::SearchEngineObserver(self, _templateURLService)); |
| 191 _templateURLService->Load(); | 185 _templateURLService->Load(); |
| 192 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( | 186 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( |
| 193 _browserState, _loader)); | 187 _browserState, self.dispatcher)); |
| 194 [_consumer setLogoVendor:_doodleController]; | 188 [_consumer setLogoVendor:_doodleController]; |
| 195 [self updateShowLogo]; | 189 [self updateShowLogo]; |
| 196 | 190 |
| 197 // Set up most visited sites. This call may have the side effect of | 191 // Set up most visited sites. This call may have the side effect of |
| 198 // triggering -onMostVisitedURLsAvailable immediately, which can load the | 192 // triggering -onMostVisitedURLsAvailable immediately, which can load the |
| 199 // view before dataSource is set. | 193 // view before dataSource is set. |
| 200 _mostVisitedSites = | 194 _mostVisitedSites = |
| 201 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); | 195 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); |
| 202 _mostVisitedObserverBridge.reset( | 196 _mostVisitedObserverBridge.reset( |
| 203 new ntp_tiles::MostVisitedSitesObserverBridge(self)); | 197 new ntp_tiles::MostVisitedSitesObserverBridge(self)); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 _notification_promo->HandleViewed(); | 339 _notification_promo->HandleViewed(); |
| 346 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; | 340 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; |
| 347 } | 341 } |
| 348 | 342 |
| 349 - (void)promoTapped { | 343 - (void)promoTapped { |
| 350 DCHECK(_notification_promo); | 344 DCHECK(_notification_promo); |
| 351 _notification_promo->HandleClosed(); | 345 _notification_promo->HandleClosed(); |
| 352 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; | 346 [self.consumer setPromoCanShow:_notification_promo->CanShow()]; |
| 353 | 347 |
| 354 if (_notification_promo->IsURLPromo()) { | 348 if (_notification_promo->IsURLPromo()) { |
| 355 [_loader webPageOrderedOpen:_notification_promo->url() | 349 [self.dispatcher webPageOrderedOpen:_notification_promo->url() |
| 356 referrer:web::Referrer() | 350 referrer:web::Referrer() |
| 357 inBackground:NO | 351 inBackground:NO |
| 358 appendTo:kCurrentTab]; | 352 appendTo:kCurrentTab]; |
| 359 return; | 353 return; |
| 360 } | 354 } |
| 361 | 355 |
| 362 if (_notification_promo->IsChromeCommand()) { | 356 if (_notification_promo->IsChromeCommand()) { |
| 363 base::scoped_nsobject<GenericChromeCommand> command( | 357 base::scoped_nsobject<GenericChromeCommand> command( |
| 364 [[GenericChromeCommand alloc] | 358 [[GenericChromeCommand alloc] |
| 365 initWithTag:_notification_promo->command_id()]); | 359 initWithTag:_notification_promo->command_id()]); |
| 366 [self.consumer chromeExecuteCommand:command]; | 360 [self.dispatcher chromeExecuteCommand:command]; |
| 367 return; | 361 return; |
| 368 } | 362 } |
| 369 NOTREACHED(); | 363 NOTREACHED(); |
| 370 } | 364 } |
| 371 | 365 |
| 372 - (void)prepareToEnterTabSwitcher:(id)sender { | |
| 373 [_webToolbarDelegate prepareToEnterTabSwitcher:sender]; | |
| 374 } | |
| 375 | |
| 376 #pragma mark - UrlLoader | |
| 377 | |
| 378 - (void)loadURL:(const GURL&)url | |
| 379 referrer:(const web::Referrer&)referrer | |
| 380 transition:(ui::PageTransition)transition | |
| 381 rendererInitiated:(BOOL)rendererInitiated { | |
| 382 [_loader loadURL:url | |
| 383 referrer:referrer | |
| 384 transition:transition | |
| 385 rendererInitiated:rendererInitiated]; | |
| 386 } | |
| 387 | |
| 388 - (void)webPageOrderedOpen:(const GURL&)url | |
| 389 referrer:(const web::Referrer&)referrer | |
| 390 inBackground:(BOOL)inBackground | |
| 391 appendTo:(OpenPosition)appendTo { | |
| 392 [_loader webPageOrderedOpen:url | |
| 393 referrer:referrer | |
| 394 inBackground:inBackground | |
| 395 appendTo:appendTo]; | |
| 396 } | |
| 397 | |
| 398 - (void)webPageOrderedOpen:(const GURL&)url | |
| 399 referrer:(const web::Referrer&)referrer | |
| 400 inIncognito:(BOOL)inIncognito | |
| 401 inBackground:(BOOL)inBackground | |
| 402 appendTo:(OpenPosition)appendTo { | |
| 403 [_loader webPageOrderedOpen:url | |
| 404 referrer:referrer | |
| 405 inIncognito:inIncognito | |
| 406 inBackground:inBackground | |
| 407 appendTo:appendTo]; | |
| 408 } | |
| 409 | |
| 410 - (void)loadSessionTab:(const sessions::SessionTab*)sessionTab { | |
| 411 NOTREACHED(); | |
| 412 } | |
| 413 | |
| 414 - (void)loadJavaScriptFromLocationBar:(NSString*)script { | |
| 415 NOTREACHED(); | |
| 416 } | |
| 417 | |
| 418 #pragma mark - OmniboxFocuser | |
| 419 | |
| 420 - (void)focusOmnibox { | |
| 421 [_focuser focusOmnibox]; | |
| 422 } | |
| 423 | |
| 424 - (void)cancelOmniboxEdit { | |
| 425 [_focuser cancelOmniboxEdit]; | |
| 426 } | |
| 427 | |
| 428 - (void)focusFakebox { | |
| 429 [_focuser focusFakebox]; | |
| 430 } | |
| 431 | |
| 432 - (void)onFakeboxBlur { | |
| 433 [_focuser onFakeboxBlur]; | |
| 434 } | |
| 435 | |
| 436 - (void)onFakeboxAnimationComplete { | |
| 437 [_focuser onFakeboxAnimationComplete]; | |
| 438 } | |
| 439 | |
| 440 @end | 366 @end |
| OLD | NEW |