Chromium Code Reviews| 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" | |
| 8 #include "base/mac/bind_objc_block.h" | 7 #include "base/mac/bind_objc_block.h" |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 10 #include "base/metrics/user_metrics.h" | 8 #include "base/metrics/user_metrics.h" |
| 11 #include "base/metrics/user_metrics_action.h" | 9 #include "base/metrics/user_metrics_action.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 13 #include "base/task/cancelable_task_tracker.h" | 11 #include "base/task/cancelable_task_tracker.h" |
| 14 #include "components/favicon/core/large_icon_service.h" | 12 #include "components/favicon/core/large_icon_service.h" |
| 15 #include "components/favicon_base/fallback_icon_style.h" | 13 #include "components/favicon_base/fallback_icon_style.h" |
| 16 #include "components/ntp_tiles/metrics.h" | 14 #include "components/ntp_tiles/metrics.h" |
| 17 #include "components/ntp_tiles/most_visited_sites.h" | 15 #include "components/ntp_tiles/most_visited_sites.h" |
| 18 #include "components/ntp_tiles/ntp_tile.h" | 16 #include "components/ntp_tiles/ntp_tile.h" |
| 19 #include "components/rappor/rappor_service_impl.h" | 17 #include "components/rappor/rappor_service_impl.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 37 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" | 35 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
| 38 #import "ios/chrome/browser/ui/url_loader.h" | 36 #import "ios/chrome/browser/ui/url_loader.h" |
| 39 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 37 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| 40 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" | 38 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
| 41 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 39 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 42 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" | 40 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
| 43 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" | 41 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
| 44 #include "ios/web/public/web_state/web_state.h" | 42 #include "ios/web/public/web_state/web_state.h" |
| 45 #include "skia/ext/skia_utils_ios.h" | 43 #include "skia/ext/skia_utils_ios.h" |
| 46 | 44 |
| 45 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 46 #error "This file requires ARC support." | |
| 47 #endif | |
| 48 | |
| 47 using base::UserMetricsAction; | 49 using base::UserMetricsAction; |
| 48 | 50 |
| 49 namespace { | 51 namespace { |
| 50 | 52 |
| 51 const CGFloat kFaviconMinSize = 32; | 53 const CGFloat kFaviconMinSize = 32; |
| 52 const NSInteger kMaxNumMostVisitedFavicons = 8; | 54 const NSInteger kMaxNumMostVisitedFavicons = 8; |
| 53 | 55 |
| 54 } // namespace | 56 } // namespace |
| 55 | 57 |
| 56 @interface GoogleLandingMediator (UsedBySearchEngineObserver) | 58 @interface GoogleLandingMediator (UsedBySearchEngineObserver) |
| 57 // Check to see if the logo visibility should change. | 59 // Check to see if the logo visibility should change. |
| 58 - (void)updateShowLogo; | 60 - (void)updateShowLogo; |
| 59 @end | 61 @end |
| 60 | 62 |
| 61 namespace google_landing { | 63 namespace google_landing { |
| 62 | 64 |
| 63 // Observer used to hide the Google logo and doodle if the TemplateURLService | 65 // Observer used to hide the Google logo and doodle if the TemplateURLService |
| 64 // changes. | 66 // changes. |
| 65 class SearchEngineObserver : public TemplateURLServiceObserver { | 67 class SearchEngineObserver : public TemplateURLServiceObserver { |
| 66 public: | 68 public: |
| 67 SearchEngineObserver(GoogleLandingMediator* owner, | 69 SearchEngineObserver(GoogleLandingMediator* owner, |
| 68 TemplateURLService* urlService); | 70 TemplateURLService* urlService); |
| 69 ~SearchEngineObserver() override; | 71 ~SearchEngineObserver() override; |
| 70 void OnTemplateURLServiceChanged() override; | 72 void OnTemplateURLServiceChanged() override; |
| 71 | 73 |
| 72 private: | 74 private: |
| 73 base::WeakNSObject<GoogleLandingMediator> _owner; | 75 __weak GoogleLandingMediator* _owner; |
| 74 TemplateURLService* _templateURLService; // weak | 76 TemplateURLService* _templateURLService; // weak |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 SearchEngineObserver::SearchEngineObserver(GoogleLandingMediator* owner, | 79 SearchEngineObserver::SearchEngineObserver(GoogleLandingMediator* owner, |
| 78 TemplateURLService* urlService) | 80 TemplateURLService* urlService) |
| 79 : _owner(owner), _templateURLService(urlService) { | 81 : _owner(owner), _templateURLService(urlService) { |
| 80 _templateURLService->AddObserver(this); | 82 _templateURLService->AddObserver(this); |
| 81 } | 83 } |
| 82 | 84 |
| 83 SearchEngineObserver::~SearchEngineObserver() { | 85 SearchEngineObserver::~SearchEngineObserver() { |
| 84 _templateURLService->RemoveObserver(this); | 86 _templateURLService->RemoveObserver(this); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void SearchEngineObserver::OnTemplateURLServiceChanged() { | 89 void SearchEngineObserver::OnTemplateURLServiceChanged() { |
| 88 [_owner updateShowLogo]; | 90 [_owner updateShowLogo]; |
| 89 } | 91 } |
| 90 | 92 |
| 91 } // namespace google_landing | 93 } // namespace google_landing |
| 92 | 94 |
| 93 @interface GoogleLandingMediator ()<GoogleLandingDataSource, | 95 @interface GoogleLandingMediator ()<GoogleLandingDataSource, |
| 94 MostVisitedSitesObserving, | 96 MostVisitedSitesObserving, |
| 95 WebStateListObserving> { | 97 WebStateListObserving> { |
| 96 // The ChromeBrowserState associated with this mediator. | 98 // The ChromeBrowserState associated with this mediator. |
| 97 ios::ChromeBrowserState* _browserState; // Weak. | 99 ios::ChromeBrowserState* _browserState; // Weak. |
| 98 | 100 |
| 99 // |YES| if impressions were logged already and shouldn't be logged again. | 101 // |YES| if impressions were logged already and shouldn't be logged again. |
| 100 BOOL _recordedPageImpression; | 102 BOOL _recordedPageImpression; |
| 101 | 103 |
| 102 // Controller to fetch and show doodles or a default Google logo. | 104 // Controller to fetch and show doodles or a default Google logo. |
| 103 base::scoped_nsprotocol<id<LogoVendor>> _doodleController; | 105 id<LogoVendor> _doodleController; |
| 104 | 106 |
| 105 // Listen for default search engine changes. | 107 // Listen for default search engine changes. |
| 106 std::unique_ptr<google_landing::SearchEngineObserver> _observer; | 108 std::unique_ptr<google_landing::SearchEngineObserver> _observer; |
| 107 TemplateURLService* _templateURLService; // weak | 109 TemplateURLService* _templateURLService; // weak |
| 108 | 110 |
| 109 // A MostVisitedSites::Observer bridge object to get notified of most visited | 111 // A MostVisitedSites::Observer bridge object to get notified of most visited |
| 110 // sites changes. | 112 // sites changes. |
| 111 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> | 113 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> |
| 112 _mostVisitedObserverBridge; | 114 _mostVisitedObserverBridge; |
| 113 | 115 |
| 114 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; | 116 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; |
| 115 | 117 |
| 116 // Most visited data from the MostVisitedSites service currently in use. | 118 // Most visited data from the MostVisitedSites service currently in use. |
| 117 ntp_tiles::NTPTilesVector _mostVisitedData; | 119 ntp_tiles::NTPTilesVector _mostVisitedData; |
| 118 | 120 |
| 119 // Observes the WebStateList so that this mediator can update the UI when the | 121 // Observes the WebStateList so that this mediator can update the UI when the |
| 120 // active WebState changes. | 122 // active WebState changes. |
| 121 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 123 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 122 | 124 |
| 123 // What's new promo. | 125 // What's new promo. |
| 124 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; | 126 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; |
| 125 | 127 |
| 126 // Used to cancel tasks for the LargeIconService. | 128 // Used to cancel tasks for the LargeIconService. |
| 127 base::CancelableTaskTracker _cancelable_task_tracker; | 129 base::CancelableTaskTracker _cancelable_task_tracker; |
| 128 } | 130 } |
| 129 | 131 |
| 130 // Consumer to handle google landing update notifications. | 132 // Consumer to handle google landing update notifications. |
| 131 @property(nonatomic) id<GoogleLandingConsumer> consumer; | 133 @property(weak, nonatomic) id<GoogleLandingConsumer> consumer; |
|
marq (ping after 24h)
2017/06/29 11:57:15
nit: nonatomic, weak
stkhapugin
2017/06/29 15:04:49
Done.
| |
| 132 | 134 |
| 133 // The WebStateList that is being observed by this mediator. | 135 // The WebStateList that is being observed by this mediator. |
| 134 @property(nonatomic, assign) WebStateList* webStateList; | 136 @property(nonatomic, assign) WebStateList* webStateList; |
| 135 | 137 |
| 136 // The dispatcher for this mediator. | 138 // The dispatcher for this mediator. |
| 137 @property(nonatomic, assign) id<ChromeExecuteCommand, UrlLoader> dispatcher; | 139 @property(nonatomic, weak) id<ChromeExecuteCommand, UrlLoader> dispatcher; |
| 138 | 140 |
| 139 // Most visited data from the MostVisitedSites service (copied upon receiving | 141 // Most visited data from the MostVisitedSites service (copied upon receiving |
| 140 // the callback), not yet used. | 142 // the callback), not yet used. |
| 141 @property(nonatomic, assign) ntp_tiles::NTPTilesVector freshMostVisitedData; | 143 @property(nonatomic, assign) ntp_tiles::NTPTilesVector freshMostVisitedData; |
| 142 | 144 |
| 143 // Perform initial setup. | 145 // Perform initial setup. |
| 144 - (void)setUp; | 146 - (void)setUp; |
| 145 | 147 |
| 146 // If there is some fresh most visited tiles, they become the current tiles and | 148 // If there is some fresh most visited tiles, they become the current tiles and |
| 147 // the consumer gets notified. | 149 // the consumer gets notified. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 [_consumer setCanGoForward:nav->CanGoForward()]; | 195 [_consumer setCanGoForward:nav->CanGoForward()]; |
| 194 [_consumer setCanGoBack:nav->CanGoBack()]; | 196 [_consumer setCanGoBack:nav->CanGoBack()]; |
| 195 } | 197 } |
| 196 | 198 |
| 197 // Set up template URL service to listen for default search engine changes. | 199 // Set up template URL service to listen for default search engine changes. |
| 198 _templateURLService = | 200 _templateURLService = |
| 199 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); | 201 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
| 200 _observer.reset( | 202 _observer.reset( |
| 201 new google_landing::SearchEngineObserver(self, _templateURLService)); | 203 new google_landing::SearchEngineObserver(self, _templateURLService)); |
| 202 _templateURLService->Load(); | 204 _templateURLService->Load(); |
| 203 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( | 205 _doodleController = ios::GetChromeBrowserProvider()->CreateLogoVendor( |
| 204 _browserState, self.dispatcher)); | 206 _browserState, self.dispatcher); |
| 205 [_consumer setLogoVendor:_doodleController]; | 207 [_consumer setLogoVendor:_doodleController]; |
| 206 [self updateShowLogo]; | 208 [self updateShowLogo]; |
| 207 | 209 |
| 208 // Set up most visited sites. This call may have the side effect of | 210 // Set up most visited sites. This call may have the side effect of |
| 209 // triggering -onMostVisitedURLsAvailable immediately, which can load the | 211 // triggering -onMostVisitedURLsAvailable immediately, which can load the |
| 210 // view before dataSource is set. | 212 // view before dataSource is set. |
| 211 _mostVisitedSites = | 213 _mostVisitedSites = |
| 212 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); | 214 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); |
| 213 _mostVisitedObserverBridge.reset( | 215 _mostVisitedObserverBridge.reset( |
| 214 new ntp_tiles::MostVisitedSitesObserverBridge(self)); | 216 new ntp_tiles::MostVisitedSitesObserverBridge(self)); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 } | 291 } |
| 290 } | 292 } |
| 291 | 293 |
| 292 - (void)getFaviconForURL:(GURL)URL | 294 - (void)getFaviconForURL:(GURL)URL |
| 293 size:(CGFloat)size | 295 size:(CGFloat)size |
| 294 useCache:(BOOL)useCache | 296 useCache:(BOOL)useCache |
| 295 imageCallback:(void (^)(UIImage* favicon))imageCallback | 297 imageCallback:(void (^)(UIImage* favicon))imageCallback |
| 296 fallbackCallback:(void (^)(UIColor* textColor, | 298 fallbackCallback:(void (^)(UIColor* textColor, |
| 297 UIColor* backgroundColor, | 299 UIColor* backgroundColor, |
| 298 BOOL isDefaultColor))fallbackCallback { | 300 BOOL isDefaultColor))fallbackCallback { |
| 299 base::WeakNSObject<GoogleLandingMediator> weakSelf(self); | 301 __weak GoogleLandingMediator* weakSelf = self; |
| 300 | 302 |
| 301 void (^faviconBlock)(const favicon_base::LargeIconResult&) = ^( | 303 void (^faviconBlock)(const favicon_base::LargeIconResult&) = ^( |
| 302 const favicon_base::LargeIconResult& result) { | 304 const favicon_base::LargeIconResult& result) { |
| 303 if (result.bitmap.is_valid()) { | 305 if (result.bitmap.is_valid()) { |
| 304 scoped_refptr<base::RefCountedMemory> data = | 306 scoped_refptr<base::RefCountedMemory> data = |
| 305 result.bitmap.bitmap_data.get(); | 307 result.bitmap.bitmap_data.get(); |
| 306 UIImage* favicon = [UIImage | 308 UIImage* favicon = [UIImage |
| 307 imageWithData:[NSData dataWithBytes:data->front() length:data->size()] | 309 imageWithData:[NSData dataWithBytes:data->front() length:data->size()] |
| 308 scale:[UIScreen mainScreen].scale]; | 310 scale:[UIScreen mainScreen].scale]; |
| 309 imageCallback(favicon); | 311 imageCallback(favicon); |
| 310 } else if (result.fallback_icon_style) { | 312 } else if (result.fallback_icon_style) { |
| 311 UIColor* backgroundColor = skia::UIColorFromSkColor( | 313 UIColor* backgroundColor = skia::UIColorFromSkColor( |
| 312 result.fallback_icon_style->background_color); | 314 result.fallback_icon_style->background_color); |
| 313 UIColor* textColor = | 315 UIColor* textColor = |
| 314 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); | 316 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); |
| 315 BOOL isDefaultColor = | 317 BOOL isDefaultColor = |
| 316 result.fallback_icon_style->is_default_background_color; | 318 result.fallback_icon_style->is_default_background_color; |
| 317 fallbackCallback(textColor, backgroundColor, isDefaultColor); | 319 fallbackCallback(textColor, backgroundColor, isDefaultColor); |
| 318 } | 320 } |
| 319 | 321 |
| 320 base::scoped_nsobject<GoogleLandingMediator> strongSelf([weakSelf retain]); | 322 GoogleLandingMediator* strongSelf = weakSelf; |
| 321 if (strongSelf && | 323 if (strongSelf && |
| 322 (result.bitmap.is_valid() || result.fallback_icon_style)) { | 324 (result.bitmap.is_valid() || result.fallback_icon_style)) { |
| 323 [strongSelf largeIconCache]->SetCachedResult(URL, result); | 325 [strongSelf largeIconCache]->SetCachedResult(URL, result); |
| 324 } | 326 } |
| 325 }; | 327 }; |
| 326 | 328 |
| 327 if (useCache) { | 329 if (useCache) { |
| 328 std::unique_ptr<favicon_base::LargeIconResult> cached_result = | 330 std::unique_ptr<favicon_base::LargeIconResult> cached_result = |
| 329 [self largeIconCache]->GetCachedResult(URL); | 331 [self largeIconCache]->GetCachedResult(URL); |
| 330 if (cached_result) { | 332 if (cached_result) { |
| 331 faviconBlock(*cached_result); | 333 faviconBlock(*cached_result); |
| 332 } | 334 } |
| 333 } | 335 } |
| 334 | 336 |
| 335 CGFloat faviconSize = [UIScreen mainScreen].scale * size; | 337 CGFloat faviconSize = [UIScreen mainScreen].scale * size; |
| 336 CGFloat faviconMinSize = [UIScreen mainScreen].scale * kFaviconMinSize; | 338 CGFloat faviconMinSize = [UIScreen mainScreen].scale * kFaviconMinSize; |
| 337 [self largeIconService]->GetLargeIconOrFallbackStyle( | 339 [self largeIconService]->GetLargeIconOrFallbackStyle( |
| 338 URL, faviconMinSize, faviconSize, base::BindBlock(faviconBlock), | 340 URL, faviconMinSize, faviconSize, base::BindBlockArc(faviconBlock), |
| 339 &_cancelable_task_tracker); | 341 &_cancelable_task_tracker); |
| 340 } | 342 } |
| 341 | 343 |
| 342 #pragma mark - WebStateListObserving | 344 #pragma mark - WebStateListObserving |
| 343 | 345 |
| 344 - (void)webStateList:(WebStateList*)webStateList | 346 - (void)webStateList:(WebStateList*)webStateList |
| 345 didInsertWebState:(web::WebState*)webState | 347 didInsertWebState:(web::WebState*)webState |
| 346 atIndex:(int)index { | 348 atIndex:(int)index { |
| 347 [self.consumer setTabCount:self.webStateList->count()]; | 349 [self.consumer setTabCount:self.webStateList->count()]; |
| 348 } | 350 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 | 425 |
| 424 if (_notification_promo->IsURLPromo()) { | 426 if (_notification_promo->IsURLPromo()) { |
| 425 [self.dispatcher webPageOrderedOpen:_notification_promo->url() | 427 [self.dispatcher webPageOrderedOpen:_notification_promo->url() |
| 426 referrer:web::Referrer() | 428 referrer:web::Referrer() |
| 427 inBackground:NO | 429 inBackground:NO |
| 428 appendTo:kCurrentTab]; | 430 appendTo:kCurrentTab]; |
| 429 return; | 431 return; |
| 430 } | 432 } |
| 431 | 433 |
| 432 if (_notification_promo->IsChromeCommand()) { | 434 if (_notification_promo->IsChromeCommand()) { |
| 433 base::scoped_nsobject<GenericChromeCommand> command( | 435 GenericChromeCommand* command = [[GenericChromeCommand alloc] |
| 434 [[GenericChromeCommand alloc] | 436 initWithTag:_notification_promo->command_id()]; |
| 435 initWithTag:_notification_promo->command_id()]); | |
| 436 [self.dispatcher chromeExecuteCommand:command]; | 437 [self.dispatcher chromeExecuteCommand:command]; |
| 437 return; | 438 return; |
| 438 } | 439 } |
| 439 NOTREACHED(); | 440 NOTREACHED(); |
| 440 } | 441 } |
| 441 | 442 |
| 442 #pragma mark - Private | 443 #pragma mark - Private |
| 443 | 444 |
| 444 - (void)useFreshData { | 445 - (void)useFreshData { |
| 445 _mostVisitedData = self.freshMostVisitedData; | 446 _mostVisitedData = self.freshMostVisitedData; |
| 446 [self.consumer mostVisitedDataUpdated]; | 447 [self.consumer mostVisitedDataUpdated]; |
| 447 } | 448 } |
| 448 | 449 |
| 449 @end | 450 @end |
| OLD | NEW |