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" | 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 std::unique_ptr<google_landing::SearchEngineObserver> _observer; | 99 std::unique_ptr<google_landing::SearchEngineObserver> _observer; |
| 100 TemplateURLService* _templateURLService; // weak | 100 TemplateURLService* _templateURLService; // weak |
| 101 | 101 |
| 102 // A MostVisitedSites::Observer bridge object to get notified of most visited | 102 // A MostVisitedSites::Observer bridge object to get notified of most visited |
| 103 // sites changes. | 103 // sites changes. |
| 104 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> | 104 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> |
| 105 _mostVisitedObserverBridge; | 105 _mostVisitedObserverBridge; |
| 106 | 106 |
| 107 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; | 107 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; |
| 108 | 108 |
| 109 // Most visited data from the MostVisitedSites service (copied upon receiving | 109 // Most visited data from the MostVisitedSites service currently in use. |
| 110 // the callback). | |
| 111 ntp_tiles::NTPTilesVector _mostVisitedData; | 110 ntp_tiles::NTPTilesVector _mostVisitedData; |
| 112 | 111 |
| 113 // Observes the WebStateList so that this mediator can update the UI when the | 112 // Observes the WebStateList so that this mediator can update the UI when the |
| 114 // active WebState changes. | 113 // active WebState changes. |
| 115 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 114 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 116 | 115 |
| 117 // What's new promo. | 116 // What's new promo. |
| 118 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; | 117 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; |
| 119 } | 118 } |
| 120 | 119 |
| 121 // Consumer to handle google landing update notifications. | 120 // Consumer to handle google landing update notifications. |
| 122 @property(nonatomic) id<GoogleLandingConsumer> consumer; | 121 @property(nonatomic) id<GoogleLandingConsumer> consumer; |
| 123 | 122 |
| 124 // The WebStateList that is being observed by this mediator. | 123 // The WebStateList that is being observed by this mediator. |
| 125 @property(nonatomic, assign) WebStateList* webStateList; | 124 @property(nonatomic, assign) WebStateList* webStateList; |
| 126 | 125 |
| 127 // The dispatcher for this mediator. | 126 // The dispatcher for this mediator. |
| 128 @property(nonatomic, assign) id<ChromeExecuteCommand, UrlLoader> dispatcher; | 127 @property(nonatomic, assign) id<ChromeExecuteCommand, UrlLoader> dispatcher; |
| 129 | 128 |
| 129 // Most visited data from the MostVisitedSites service (copied upon receiving | |
| 130 // the callback), not yet used. | |
| 131 @property(nonatomic, assign) ntp_tiles::NTPTilesVector freshMostVisitedData; | |
| 132 | |
| 130 // Perform initial setup. | 133 // Perform initial setup. |
| 131 - (void)setUp; | 134 - (void)setUp; |
| 132 | 135 |
| 136 // If there is some fresh most visited tiles, they become the current tiles and | |
| 137 // the consumer gets notified. | |
| 138 - (void)useFreshData; | |
| 139 | |
| 133 @end | 140 @end |
| 134 | 141 |
| 135 @implementation GoogleLandingMediator | 142 @implementation GoogleLandingMediator |
| 136 | 143 |
| 137 @synthesize consumer = _consumer; | 144 @synthesize consumer = _consumer; |
| 138 @synthesize dispatcher = _dispatcher; | 145 @synthesize dispatcher = _dispatcher; |
| 139 @synthesize webStateList = _webStateList; | 146 @synthesize webStateList = _webStateList; |
| 147 @synthesize freshMostVisitedData = _freshMostVisitedData; | |
| 140 | 148 |
| 141 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer | 149 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer |
| 142 browserState:(ios::ChromeBrowserState*)browserState | 150 browserState:(ios::ChromeBrowserState*)browserState |
| 143 dispatcher:(id<ChromeExecuteCommand, UrlLoader>)dispatcher | 151 dispatcher:(id<ChromeExecuteCommand, UrlLoader>)dispatcher |
| 144 webStateList:(WebStateList*)webStateList { | 152 webStateList:(WebStateList*)webStateList { |
| 145 self = [super init]; | 153 self = [super init]; |
| 146 if (self) { | 154 if (self) { |
| 147 _consumer = consumer; | 155 _consumer = consumer; |
| 148 _browserState = browserState; | 156 _browserState = browserState; |
| 149 _dispatcher = dispatcher; | 157 _dispatcher = dispatcher; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 [self.consumer setLogoIsShowing:showLogo]; | 241 [self.consumer setLogoIsShowing:showLogo]; |
| 234 } | 242 } |
| 235 | 243 |
| 236 + (NSUInteger)maxSitesShown { | 244 + (NSUInteger)maxSitesShown { |
| 237 return kMaxNumMostVisitedFavicons; | 245 return kMaxNumMostVisitedFavicons; |
| 238 } | 246 } |
| 239 | 247 |
| 240 #pragma mark - MostVisitedSitesObserving | 248 #pragma mark - MostVisitedSitesObserving |
| 241 | 249 |
| 242 - (void)onMostVisitedURLsAvailable:(const ntp_tiles::NTPTilesVector&)data { | 250 - (void)onMostVisitedURLsAvailable:(const ntp_tiles::NTPTilesVector&)data { |
| 251 if (_mostVisitedData.size() > 0) { | |
| 252 // If some content is already displayed to the user, do not update it to | |
| 253 // prevent updating the all the tiles without any action from the user. | |
| 254 self.freshMostVisitedData = data; | |
| 255 return; | |
| 256 } | |
| 257 | |
| 243 _mostVisitedData = data; | 258 _mostVisitedData = data; |
| 244 [self.consumer mostVisitedDataUpdated]; | 259 [self.consumer mostVisitedDataUpdated]; |
| 245 | 260 |
| 246 if (data.size() && !_recordedPageImpression) { | 261 if (data.size() && !_recordedPageImpression) { |
| 247 _recordedPageImpression = YES; | 262 _recordedPageImpression = YES; |
| 248 std::vector<ntp_tiles::metrics::TileImpression> tiles; | 263 std::vector<ntp_tiles::metrics::TileImpression> tiles; |
| 249 for (const ntp_tiles::NTPTile& ntpTile : data) { | 264 for (const ntp_tiles::NTPTile& ntpTile : data) { |
| 250 tiles.emplace_back(ntpTile.source, ntp_tiles::UNKNOWN_TILE_TYPE, | 265 tiles.emplace_back(ntpTile.source, ntp_tiles::UNKNOWN_TILE_TYPE, |
| 251 ntpTile.url); | 266 ntpTile.url); |
| 252 } | 267 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 292 web::NavigationManager* nav = newWebState->GetNavigationManager(); | 307 web::NavigationManager* nav = newWebState->GetNavigationManager(); |
| 293 [self.consumer setCanGoForward:nav->CanGoForward()]; | 308 [self.consumer setCanGoForward:nav->CanGoForward()]; |
| 294 [self.consumer setCanGoBack:nav->CanGoBack()]; | 309 [self.consumer setCanGoBack:nav->CanGoBack()]; |
| 295 } | 310 } |
| 296 } | 311 } |
| 297 | 312 |
| 298 #pragma mark - GoogleLandingDataSource | 313 #pragma mark - GoogleLandingDataSource |
| 299 | 314 |
| 300 - (void)addBlacklistedURL:(const GURL&)url { | 315 - (void)addBlacklistedURL:(const GURL&)url { |
| 301 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, true); | 316 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, true); |
| 317 [self useFreshData]; | |
| 302 } | 318 } |
| 303 | 319 |
| 304 - (void)removeBlacklistedURL:(const GURL&)url { | 320 - (void)removeBlacklistedURL:(const GURL&)url { |
| 305 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, false); | 321 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, false); |
| 322 [self useFreshData]; | |
| 306 } | 323 } |
| 307 | 324 |
| 308 - (ntp_tiles::NTPTile)mostVisitedAtIndex:(NSUInteger)index { | 325 - (ntp_tiles::NTPTile)mostVisitedAtIndex:(NSUInteger)index { |
| 309 return _mostVisitedData[index]; | 326 return _mostVisitedData[index]; |
| 310 } | 327 } |
| 311 | 328 |
| 312 - (NSUInteger)mostVisitedSize { | 329 - (NSUInteger)mostVisitedSize { |
| 313 return _mostVisitedData.size(); | 330 return _mostVisitedData.size(); |
| 314 } | 331 } |
| 315 | 332 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 if (_notification_promo->IsChromeCommand()) { | 373 if (_notification_promo->IsChromeCommand()) { |
| 357 base::scoped_nsobject<GenericChromeCommand> command( | 374 base::scoped_nsobject<GenericChromeCommand> command( |
| 358 [[GenericChromeCommand alloc] | 375 [[GenericChromeCommand alloc] |
| 359 initWithTag:_notification_promo->command_id()]); | 376 initWithTag:_notification_promo->command_id()]); |
| 360 [self.dispatcher chromeExecuteCommand:command]; | 377 [self.dispatcher chromeExecuteCommand:command]; |
| 361 return; | 378 return; |
| 362 } | 379 } |
| 363 NOTREACHED(); | 380 NOTREACHED(); |
| 364 } | 381 } |
| 365 | 382 |
| 383 #pragma mark - Private | |
| 384 | |
| 385 - (void)useFreshData { | |
| 386 if (self.freshMostVisitedData.size() == 0) { | |
| 387 return; | |
|
justincohen
2017/05/24 15:58:21
should this NOTREACHED?
gambard
2017/06/02 12:47:04
No. If you remove a tile, all the tiles are not ne
justincohen
2017/06/05 15:18:59
When would removing a tile not call -onMostVisited
gambard
2017/06/06 15:34:54
Actually this code do not remove the last tile. I
| |
| 388 } | |
| 389 _mostVisitedData = self.freshMostVisitedData; | |
| 390 self.freshMostVisitedData.clear(); | |
| 391 [self.consumer mostVisitedDataUpdated]; | |
| 392 } | |
| 393 | |
| 366 @end | 394 @end |
| OLD | NEW |