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) { | |
fhorschig
2017/05/19 13:09:57
Could you please add a comment why the fresh data
gambard
2017/05/19 13:41:20
Done.
| |
252 self.freshMostVisitedData = data; | |
253 return; | |
254 } | |
255 | |
243 _mostVisitedData = data; | 256 _mostVisitedData = data; |
244 [self.consumer mostVisitedDataUpdated]; | 257 [self.consumer mostVisitedDataUpdated]; |
245 | 258 |
246 if (data.size() && !_recordedPageImpression) { | 259 if (data.size() && !_recordedPageImpression) { |
247 _recordedPageImpression = YES; | 260 _recordedPageImpression = YES; |
248 std::vector<ntp_tiles::metrics::TileImpression> tiles; | 261 std::vector<ntp_tiles::metrics::TileImpression> tiles; |
249 for (const ntp_tiles::NTPTile& ntpTile : data) { | 262 for (const ntp_tiles::NTPTile& ntpTile : data) { |
250 tiles.emplace_back(ntpTile.source, ntp_tiles::UNKNOWN_TILE_TYPE, | 263 tiles.emplace_back(ntpTile.source, ntp_tiles::UNKNOWN_TILE_TYPE, |
251 ntpTile.url); | 264 ntpTile.url); |
252 } | 265 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 web::NavigationManager* nav = newWebState->GetNavigationManager(); | 305 web::NavigationManager* nav = newWebState->GetNavigationManager(); |
293 [self.consumer setCanGoForward:nav->CanGoForward()]; | 306 [self.consumer setCanGoForward:nav->CanGoForward()]; |
294 [self.consumer setCanGoBack:nav->CanGoBack()]; | 307 [self.consumer setCanGoBack:nav->CanGoBack()]; |
295 } | 308 } |
296 } | 309 } |
297 | 310 |
298 #pragma mark - GoogleLandingDataSource | 311 #pragma mark - GoogleLandingDataSource |
299 | 312 |
300 - (void)addBlacklistedURL:(const GURL&)url { | 313 - (void)addBlacklistedURL:(const GURL&)url { |
301 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, true); | 314 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, true); |
315 [self useFreshData]; | |
302 } | 316 } |
303 | 317 |
304 - (void)removeBlacklistedURL:(const GURL&)url { | 318 - (void)removeBlacklistedURL:(const GURL&)url { |
305 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, false); | 319 _mostVisitedSites->AddOrRemoveBlacklistedUrl(url, false); |
320 [self useFreshData]; | |
306 } | 321 } |
307 | 322 |
308 - (ntp_tiles::NTPTile)mostVisitedAtIndex:(NSUInteger)index { | 323 - (ntp_tiles::NTPTile)mostVisitedAtIndex:(NSUInteger)index { |
309 return _mostVisitedData[index]; | 324 return _mostVisitedData[index]; |
310 } | 325 } |
311 | 326 |
312 - (NSUInteger)mostVisitedSize { | 327 - (NSUInteger)mostVisitedSize { |
313 return _mostVisitedData.size(); | 328 return _mostVisitedData.size(); |
314 } | 329 } |
315 | 330 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 if (_notification_promo->IsChromeCommand()) { | 371 if (_notification_promo->IsChromeCommand()) { |
357 base::scoped_nsobject<GenericChromeCommand> command( | 372 base::scoped_nsobject<GenericChromeCommand> command( |
358 [[GenericChromeCommand alloc] | 373 [[GenericChromeCommand alloc] |
359 initWithTag:_notification_promo->command_id()]); | 374 initWithTag:_notification_promo->command_id()]); |
360 [self.dispatcher chromeExecuteCommand:command]; | 375 [self.dispatcher chromeExecuteCommand:command]; |
361 return; | 376 return; |
362 } | 377 } |
363 NOTREACHED(); | 378 NOTREACHED(); |
364 } | 379 } |
365 | 380 |
381 #pragma mark - Private | |
382 | |
383 - (void)useFreshData { | |
384 if (self.freshMostVisitedData.size() == 0) { | |
385 return; | |
386 } | |
387 _mostVisitedData = self.freshMostVisitedData; | |
388 self.freshMostVisitedData.clear(); | |
389 [self.consumer mostVisitedDataUpdated]; | |
390 } | |
391 | |
366 @end | 392 @end |
OLD | NEW |