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 |
(...skipping 14 matching lines...) Expand all Loading... |
128 | 130 |
129 // Observes the WebStateList so that this mediator can update the UI when the | 131 // Observes the WebStateList so that this mediator can update the UI when the |
130 // active WebState changes. | 132 // active WebState changes. |
131 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 133 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
132 | 134 |
133 // What's new promo. | 135 // What's new promo. |
134 std::unique_ptr<NotificationPromoWhatsNew> _notificationPromo; | 136 std::unique_ptr<NotificationPromoWhatsNew> _notificationPromo; |
135 | 137 |
136 // Used to cancel tasks for the LargeIconService. | 138 // Used to cancel tasks for the LargeIconService. |
137 base::CancelableTaskTracker _cancelable_task_tracker; | 139 base::CancelableTaskTracker _cancelable_task_tracker; |
138 | |
139 // Consumer to handle google landing update notifications. | |
140 base::WeakNSProtocol<id<GoogleLandingConsumer>> _consumer; | |
141 | |
142 // Dispatcher for this mediator. | |
143 base::WeakNSProtocol<id<ChromeExecuteCommand, UrlLoader>> _dispatcher; | |
144 } | 140 } |
145 | 141 |
146 // Consumer to handle google landing update notifications. | 142 // Consumer to handle google landing update notifications. |
147 @property(nonatomic, assign, readonly) id<GoogleLandingConsumer> consumer; | 143 @property(nonatomic, weak) id<GoogleLandingConsumer> consumer; |
148 | 144 |
149 // The WebStateList that is being observed by this mediator. | 145 // The WebStateList that is being observed by this mediator. |
150 @property(nonatomic, assign, readonly) WebStateList* webStateList; | 146 @property(nonatomic, assign, readonly) WebStateList* webStateList; |
151 | 147 |
152 // The dispatcher for this mediator. | 148 // The dispatcher for this mediator. |
153 @property(nonatomic, assign, readonly) id<ChromeExecuteCommand, UrlLoader> | 149 @property(nonatomic, weak) id<ChromeExecuteCommand, UrlLoader> dispatcher; |
154 dispatcher; | |
155 | 150 |
156 // Perform initial setup. | 151 // Perform initial setup. |
157 - (void)setUp; | 152 - (void)setUp; |
158 | 153 |
159 @end | 154 @end |
160 | 155 |
161 @implementation GoogleLandingMediator | 156 @implementation GoogleLandingMediator |
162 | 157 |
163 @synthesize webStateList = _webStateList; | 158 @synthesize webStateList = _webStateList; |
| 159 @synthesize consumer = _consumer; |
| 160 @synthesize dispatcher = _dispatcher; |
164 | 161 |
165 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer | 162 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer |
166 browserState:(ios::ChromeBrowserState*)browserState | 163 browserState:(ios::ChromeBrowserState*)browserState |
167 dispatcher:(id<ChromeExecuteCommand, UrlLoader>)dispatcher | 164 dispatcher:(id<ChromeExecuteCommand, UrlLoader>)dispatcher |
168 webStateList:(WebStateList*)webStateList { | 165 webStateList:(WebStateList*)webStateList { |
169 self = [super init]; | 166 self = [super init]; |
170 if (self) { | 167 if (self) { |
171 _consumer.reset(consumer); | 168 _consumer = consumer; |
172 _browserState = browserState; | 169 _browserState = browserState; |
173 _dispatcher.reset(dispatcher); | 170 _dispatcher = dispatcher; |
174 _webStateList = webStateList; | 171 _webStateList = webStateList; |
175 | 172 |
176 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | 173 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
177 _webStateList->AddObserver(_webStateListObserver.get()); | 174 _webStateList->AddObserver(_webStateListObserver.get()); |
178 | 175 |
179 [self setUp]; | 176 [self setUp]; |
180 } | 177 } |
181 return self; | 178 return self; |
182 } | 179 } |
183 | 180 |
(...skipping 16 matching lines...) Expand all Loading... |
200 [self.consumer setCanGoForward:nav->CanGoForward()]; | 197 [self.consumer setCanGoForward:nav->CanGoForward()]; |
201 [self.consumer setCanGoBack:nav->CanGoBack()]; | 198 [self.consumer setCanGoBack:nav->CanGoBack()]; |
202 } | 199 } |
203 | 200 |
204 // Set up template URL service to listen for default search engine changes. | 201 // Set up template URL service to listen for default search engine changes. |
205 _templateURLService = | 202 _templateURLService = |
206 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); | 203 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
207 _observer.reset( | 204 _observer.reset( |
208 new google_landing::SearchEngineObserver(self, _templateURLService)); | 205 new google_landing::SearchEngineObserver(self, _templateURLService)); |
209 _templateURLService->Load(); | 206 _templateURLService->Load(); |
210 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( | 207 _doodleController = ios::GetChromeBrowserProvider()->CreateLogoVendor( |
211 _browserState, self.dispatcher)); | 208 _browserState, self.dispatcher); |
212 [self.consumer setLogoVendor:_doodleController]; | 209 [_consumer setLogoVendor:_doodleController]; |
213 [self updateShowLogo]; | 210 [self updateShowLogo]; |
214 | 211 |
215 // Set up most visited sites. This call may have the side effect of | 212 // Set up most visited sites. This call may have the side effect of |
216 // triggering -onMostVisitedURLsAvailable immediately, which can load the | 213 // triggering -onMostVisitedURLsAvailable immediately, which can load the |
217 // view before dataSource is set. | 214 // view before dataSource is set. |
218 _mostVisitedSites = | 215 _mostVisitedSites = |
219 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); | 216 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); |
220 _mostVisitedObserverBridge.reset( | 217 _mostVisitedObserverBridge.reset( |
221 new ntp_tiles::MostVisitedSitesObserverBridge(self)); | 218 new ntp_tiles::MostVisitedSitesObserverBridge(self)); |
222 _mostVisitedSites->SetMostVisitedURLsObserver( | 219 _mostVisitedSites->SetMostVisitedURLsObserver( |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 } | 288 } |
292 } | 289 } |
293 | 290 |
294 - (void)getFaviconForURL:(GURL)URL | 291 - (void)getFaviconForURL:(GURL)URL |
295 size:(CGFloat)size | 292 size:(CGFloat)size |
296 useCache:(BOOL)useCache | 293 useCache:(BOOL)useCache |
297 imageCallback:(void (^)(UIImage* favicon))imageCallback | 294 imageCallback:(void (^)(UIImage* favicon))imageCallback |
298 fallbackCallback:(void (^)(UIColor* textColor, | 295 fallbackCallback:(void (^)(UIColor* textColor, |
299 UIColor* backgroundColor, | 296 UIColor* backgroundColor, |
300 BOOL isDefaultColor))fallbackCallback { | 297 BOOL isDefaultColor))fallbackCallback { |
301 base::WeakNSObject<GoogleLandingMediator> weakSelf(self); | 298 __weak GoogleLandingMediator* weakSelf = self; |
302 | 299 |
303 void (^faviconBlock)(const favicon_base::LargeIconResult&) = ^( | 300 void (^faviconBlock)(const favicon_base::LargeIconResult&) = ^( |
304 const favicon_base::LargeIconResult& result) { | 301 const favicon_base::LargeIconResult& result) { |
305 ntp_tiles::TileVisualType tileType; | 302 ntp_tiles::TileVisualType tileType; |
306 | 303 |
307 if (result.bitmap.is_valid()) { | 304 if (result.bitmap.is_valid()) { |
308 scoped_refptr<base::RefCountedMemory> data = | 305 scoped_refptr<base::RefCountedMemory> data = |
309 result.bitmap.bitmap_data.get(); | 306 result.bitmap.bitmap_data.get(); |
310 UIImage* favicon = [UIImage | 307 UIImage* favicon = [UIImage |
311 imageWithData:[NSData dataWithBytes:data->front() length:data->size()] | 308 imageWithData:[NSData dataWithBytes:data->front() length:data->size()] |
312 scale:[UIScreen mainScreen].scale]; | 309 scale:[UIScreen mainScreen].scale]; |
313 imageCallback(favicon); | 310 imageCallback(favicon); |
314 tileType = ntp_tiles::TileVisualType::ICON_REAL; | 311 tileType = ntp_tiles::TileVisualType::ICON_REAL; |
315 } else if (result.fallback_icon_style) { | 312 } else if (result.fallback_icon_style) { |
316 UIColor* backgroundColor = skia::UIColorFromSkColor( | 313 UIColor* backgroundColor = skia::UIColorFromSkColor( |
317 result.fallback_icon_style->background_color); | 314 result.fallback_icon_style->background_color); |
318 UIColor* textColor = | 315 UIColor* textColor = |
319 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); | 316 skia::UIColorFromSkColor(result.fallback_icon_style->text_color); |
320 BOOL isDefaultColor = | 317 BOOL isDefaultColor = |
321 result.fallback_icon_style->is_default_background_color; | 318 result.fallback_icon_style->is_default_background_color; |
322 fallbackCallback(textColor, backgroundColor, isDefaultColor); | 319 fallbackCallback(textColor, backgroundColor, isDefaultColor); |
323 fallbackCallback(backgroundColor, textColor, isDefaultColor); | 320 fallbackCallback(backgroundColor, textColor, isDefaultColor); |
324 tileType = isDefaultColor ? ntp_tiles::TileVisualType::ICON_DEFAULT | 321 tileType = isDefaultColor ? ntp_tiles::TileVisualType::ICON_DEFAULT |
325 : ntp_tiles::TileVisualType::ICON_COLOR; | 322 : ntp_tiles::TileVisualType::ICON_COLOR; |
326 } | 323 } |
327 | 324 |
328 base::scoped_nsobject<GoogleLandingMediator> strongSelf([weakSelf retain]); | 325 GoogleLandingMediator* strongSelf = weakSelf; |
329 if (strongSelf) { | 326 if (strongSelf && |
330 if ((result.bitmap.is_valid() || result.fallback_icon_style)) | 327 (result.bitmap.is_valid() || result.fallback_icon_style)) { |
331 [strongSelf largeIconCache]->SetCachedResult(URL, result); | 328 [strongSelf largeIconCache]->SetCachedResult(URL, result); |
332 [strongSelf faviconOfType:tileType fetchedForURL:URL]; | |
333 } | 329 } |
334 }; | 330 }; |
335 | 331 |
336 if (useCache) { | 332 if (useCache) { |
337 std::unique_ptr<favicon_base::LargeIconResult> cached_result = | 333 std::unique_ptr<favicon_base::LargeIconResult> cached_result = |
338 [self largeIconCache]->GetCachedResult(URL); | 334 [self largeIconCache]->GetCachedResult(URL); |
339 if (cached_result) { | 335 if (cached_result) { |
340 faviconBlock(*cached_result); | 336 faviconBlock(*cached_result); |
341 } | 337 } |
342 } | 338 } |
343 | 339 |
344 CGFloat faviconSize = [UIScreen mainScreen].scale * size; | 340 CGFloat faviconSize = [UIScreen mainScreen].scale * size; |
345 CGFloat faviconMinSize = [UIScreen mainScreen].scale * kFaviconMinSize; | 341 CGFloat faviconMinSize = [UIScreen mainScreen].scale * kFaviconMinSize; |
346 [self largeIconService]->GetLargeIconOrFallbackStyle( | 342 [self largeIconService]->GetLargeIconOrFallbackStyle( |
347 URL, faviconMinSize, faviconSize, base::BindBlock(faviconBlock), | 343 URL, faviconMinSize, faviconSize, base::BindBlockArc(faviconBlock), |
348 &_cancelable_task_tracker); | 344 &_cancelable_task_tracker); |
349 } | 345 } |
350 | 346 |
351 #pragma mark - WebStateListObserving | 347 #pragma mark - WebStateListObserving |
352 | 348 |
353 - (void)webStateList:(WebStateList*)webStateList | 349 - (void)webStateList:(WebStateList*)webStateList |
354 didInsertWebState:(web::WebState*)webState | 350 didInsertWebState:(web::WebState*)webState |
355 atIndex:(int)index { | 351 atIndex:(int)index { |
356 [self.consumer setTabCount:self.webStateList->count()]; | 352 [self.consumer setTabCount:self.webStateList->count()]; |
357 } | 353 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 | 428 |
433 if (_notificationPromo->IsURLPromo()) { | 429 if (_notificationPromo->IsURLPromo()) { |
434 [self.dispatcher webPageOrderedOpen:_notificationPromo->url() | 430 [self.dispatcher webPageOrderedOpen:_notificationPromo->url() |
435 referrer:web::Referrer() | 431 referrer:web::Referrer() |
436 inBackground:NO | 432 inBackground:NO |
437 appendTo:kCurrentTab]; | 433 appendTo:kCurrentTab]; |
438 return; | 434 return; |
439 } | 435 } |
440 | 436 |
441 if (_notificationPromo->IsChromeCommand()) { | 437 if (_notificationPromo->IsChromeCommand()) { |
442 base::scoped_nsobject<GenericChromeCommand> command( | 438 GenericChromeCommand* command = [[GenericChromeCommand alloc] |
443 [[GenericChromeCommand alloc] | 439 initWithTag:_notificationPromo->command_id()]; |
444 initWithTag:_notificationPromo->command_id()]); | |
445 [self.dispatcher chromeExecuteCommand:command]; | 440 [self.dispatcher chromeExecuteCommand:command]; |
446 return; | 441 return; |
447 } | 442 } |
448 NOTREACHED(); | 443 NOTREACHED(); |
449 } | 444 } |
450 | 445 |
451 #pragma mark - Private | 446 #pragma mark - Private |
452 | 447 |
453 // If there is some fresh most visited tiles, they become the current tiles and | 448 // If there is some fresh most visited tiles, they become the current tiles and |
454 // the consumer gets notified. | 449 // the consumer gets notified. |
(...skipping 12 matching lines...) Expand all Loading... |
467 ntp_tiles::metrics::RecordTileImpression( | 462 ntp_tiles::metrics::RecordTileImpression( |
468 i, ntpTile.source, tileType, URL, | 463 i, ntpTile.source, tileType, URL, |
469 GetApplicationContext()->GetRapporServiceImpl()); | 464 GetApplicationContext()->GetRapporServiceImpl()); |
470 // Reset the URL to be sure to log the impression only once. | 465 // Reset the URL to be sure to log the impression only once. |
471 ntpTile.url = GURL(); | 466 ntpTile.url = GURL(); |
472 break; | 467 break; |
473 } | 468 } |
474 } | 469 } |
475 } | 470 } |
476 | 471 |
477 #pragma mark - Properties | |
478 | |
479 - (id<GoogleLandingConsumer>)consumer { | |
480 return _consumer.get(); | |
481 } | |
482 | |
483 - (id<ChromeExecuteCommand, UrlLoader>)dispatcher { | |
484 return _dispatcher.get(); | |
485 } | |
486 | |
487 @end | 472 @end |
OLD | NEW |