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/scoped_nsobject.h" | |
9 #include "base/metrics/user_metrics.h" | 7 #include "base/metrics/user_metrics.h" |
10 #include "base/metrics/user_metrics_action.h" | 8 #include "base/metrics/user_metrics_action.h" |
11 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
12 #include "components/ntp_tiles/metrics.h" | 10 #include "components/ntp_tiles/metrics.h" |
13 #include "components/ntp_tiles/most_visited_sites.h" | 11 #include "components/ntp_tiles/most_visited_sites.h" |
14 #include "components/ntp_tiles/ntp_tile.h" | 12 #include "components/ntp_tiles/ntp_tile.h" |
15 #include "components/rappor/rappor_service_impl.h" | 13 #include "components/rappor/rappor_service_impl.h" |
16 #include "components/search_engines/template_url_service.h" | 14 #include "components/search_engines/template_url_service.h" |
17 #include "components/search_engines/template_url_service_observer.h" | 15 #include "components/search_engines/template_url_service_observer.h" |
18 #include "ios/chrome/browser/application_context.h" | 16 #include "ios/chrome/browser/application_context.h" |
(...skipping 12 matching lines...) Expand all Loading... |
31 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" | 29 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" |
32 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" | 30 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
33 #import "ios/chrome/browser/ui/url_loader.h" | 31 #import "ios/chrome/browser/ui/url_loader.h" |
34 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 32 #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" | 33 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
36 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 34 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
37 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" | 35 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
38 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" | 36 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
39 #include "ios/web/public/web_state/web_state.h" | 37 #include "ios/web/public/web_state/web_state.h" |
40 | 38 |
| 39 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 40 #error "This file requires ARC support." |
| 41 #endif |
| 42 |
41 using base::UserMetricsAction; | 43 using base::UserMetricsAction; |
42 | 44 |
43 namespace { | 45 namespace { |
44 | 46 |
45 const NSInteger kMaxNumMostVisitedFavicons = 8; | 47 const NSInteger kMaxNumMostVisitedFavicons = 8; |
46 | 48 |
47 } // namespace | 49 } // namespace |
48 | 50 |
49 @interface GoogleLandingMediator (UsedBySearchEngineObserver) | 51 @interface GoogleLandingMediator (UsedBySearchEngineObserver) |
50 // Check to see if the logo visibility should change. | 52 // Check to see if the logo visibility should change. |
51 - (void)updateShowLogo; | 53 - (void)updateShowLogo; |
52 @end | 54 @end |
53 | 55 |
54 namespace google_landing { | 56 namespace google_landing { |
55 | 57 |
56 // Observer used to hide the Google logo and doodle if the TemplateURLService | 58 // Observer used to hide the Google logo and doodle if the TemplateURLService |
57 // changes. | 59 // changes. |
58 class SearchEngineObserver : public TemplateURLServiceObserver { | 60 class SearchEngineObserver : public TemplateURLServiceObserver { |
59 public: | 61 public: |
60 SearchEngineObserver(GoogleLandingMediator* owner, | 62 SearchEngineObserver(GoogleLandingMediator* owner, |
61 TemplateURLService* urlService); | 63 TemplateURLService* urlService); |
62 ~SearchEngineObserver() override; | 64 ~SearchEngineObserver() override; |
63 void OnTemplateURLServiceChanged() override; | 65 void OnTemplateURLServiceChanged() override; |
64 | 66 |
65 private: | 67 private: |
66 base::WeakNSObject<GoogleLandingMediator> _owner; | 68 __weak GoogleLandingMediator* _owner; |
67 TemplateURLService* _templateURLService; // weak | 69 TemplateURLService* _templateURLService; // weak |
68 }; | 70 }; |
69 | 71 |
70 SearchEngineObserver::SearchEngineObserver(GoogleLandingMediator* owner, | 72 SearchEngineObserver::SearchEngineObserver(GoogleLandingMediator* owner, |
71 TemplateURLService* urlService) | 73 TemplateURLService* urlService) |
72 : _owner(owner), _templateURLService(urlService) { | 74 : _owner(owner), _templateURLService(urlService) { |
73 _templateURLService->AddObserver(this); | 75 _templateURLService->AddObserver(this); |
74 } | 76 } |
75 | 77 |
76 SearchEngineObserver::~SearchEngineObserver() { | 78 SearchEngineObserver::~SearchEngineObserver() { |
77 _templateURLService->RemoveObserver(this); | 79 _templateURLService->RemoveObserver(this); |
78 } | 80 } |
79 | 81 |
80 void SearchEngineObserver::OnTemplateURLServiceChanged() { | 82 void SearchEngineObserver::OnTemplateURLServiceChanged() { |
81 [_owner updateShowLogo]; | 83 [_owner updateShowLogo]; |
82 } | 84 } |
83 | 85 |
84 } // namespace google_landing | 86 } // namespace google_landing |
85 | 87 |
86 @interface GoogleLandingMediator ()<GoogleLandingDataSource, | 88 @interface GoogleLandingMediator ()<GoogleLandingDataSource, |
87 MostVisitedSitesObserving, | 89 MostVisitedSitesObserving, |
88 WebStateListObserving> { | 90 WebStateListObserving> { |
89 // The ChromeBrowserState associated with this mediator. | 91 // The ChromeBrowserState associated with this mediator. |
90 ios::ChromeBrowserState* _browserState; // Weak. | 92 ios::ChromeBrowserState* _browserState; // Weak. |
91 | 93 |
92 // |YES| if impressions were logged already and shouldn't be logged again. | 94 // |YES| if impressions were logged already and shouldn't be logged again. |
93 BOOL _recordedPageImpression; | 95 BOOL _recordedPageImpression; |
94 | 96 |
95 // Controller to fetch and show doodles or a default Google logo. | 97 // Controller to fetch and show doodles or a default Google logo. |
96 base::scoped_nsprotocol<id<LogoVendor>> _doodleController; | 98 id<LogoVendor> _doodleController; |
97 | 99 |
98 // Listen for default search engine changes. | 100 // Listen for default search engine changes. |
99 std::unique_ptr<google_landing::SearchEngineObserver> _observer; | 101 std::unique_ptr<google_landing::SearchEngineObserver> _observer; |
100 TemplateURLService* _templateURLService; // weak | 102 TemplateURLService* _templateURLService; // weak |
101 | 103 |
102 // A MostVisitedSites::Observer bridge object to get notified of most visited | 104 // A MostVisitedSites::Observer bridge object to get notified of most visited |
103 // sites changes. | 105 // sites changes. |
104 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> | 106 std::unique_ptr<ntp_tiles::MostVisitedSitesObserverBridge> |
105 _mostVisitedObserverBridge; | 107 _mostVisitedObserverBridge; |
106 | 108 |
107 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; | 109 std::unique_ptr<ntp_tiles::MostVisitedSites> _mostVisitedSites; |
108 | 110 |
109 // Most visited data from the MostVisitedSites service currently in use. | 111 // Most visited data from the MostVisitedSites service currently in use. |
110 ntp_tiles::NTPTilesVector _mostVisitedData; | 112 ntp_tiles::NTPTilesVector _mostVisitedData; |
111 | 113 |
112 // Observes the WebStateList so that this mediator can update the UI when the | 114 // Observes the WebStateList so that this mediator can update the UI when the |
113 // active WebState changes. | 115 // active WebState changes. |
114 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 116 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
115 | 117 |
116 // What's new promo. | 118 // What's new promo. |
117 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; | 119 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; |
118 } | 120 } |
119 | 121 |
120 // Consumer to handle google landing update notifications. | 122 // Consumer to handle google landing update notifications. |
121 @property(nonatomic) id<GoogleLandingConsumer> consumer; | 123 @property(weak, nonatomic) id<GoogleLandingConsumer> consumer; |
122 | 124 |
123 // The WebStateList that is being observed by this mediator. | 125 // The WebStateList that is being observed by this mediator. |
124 @property(nonatomic, assign) WebStateList* webStateList; | 126 @property(nonatomic, assign) WebStateList* webStateList; |
125 | 127 |
126 // The dispatcher for this mediator. | 128 // The dispatcher for this mediator. |
127 @property(nonatomic, assign) id<ChromeExecuteCommand, UrlLoader> dispatcher; | 129 @property(nonatomic, weak) id<ChromeExecuteCommand, UrlLoader> dispatcher; |
128 | 130 |
129 // Most visited data from the MostVisitedSites service (copied upon receiving | 131 // Most visited data from the MostVisitedSites service (copied upon receiving |
130 // the callback), not yet used. | 132 // the callback), not yet used. |
131 @property(nonatomic, assign) ntp_tiles::NTPTilesVector freshMostVisitedData; | 133 @property(nonatomic, assign) ntp_tiles::NTPTilesVector freshMostVisitedData; |
132 | 134 |
133 // Perform initial setup. | 135 // Perform initial setup. |
134 - (void)setUp; | 136 - (void)setUp; |
135 | 137 |
136 // If there is some fresh most visited tiles, they become the current tiles and | 138 // If there is some fresh most visited tiles, they become the current tiles and |
137 // the consumer gets notified. | 139 // the consumer gets notified. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 [_consumer setCanGoForward:nav->CanGoForward()]; | 186 [_consumer setCanGoForward:nav->CanGoForward()]; |
185 [_consumer setCanGoBack:nav->CanGoBack()]; | 187 [_consumer setCanGoBack:nav->CanGoBack()]; |
186 } | 188 } |
187 | 189 |
188 // Set up template URL service to listen for default search engine changes. | 190 // Set up template URL service to listen for default search engine changes. |
189 _templateURLService = | 191 _templateURLService = |
190 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); | 192 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); |
191 _observer.reset( | 193 _observer.reset( |
192 new google_landing::SearchEngineObserver(self, _templateURLService)); | 194 new google_landing::SearchEngineObserver(self, _templateURLService)); |
193 _templateURLService->Load(); | 195 _templateURLService->Load(); |
194 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( | 196 _doodleController = ios::GetChromeBrowserProvider()->CreateLogoVendor( |
195 _browserState, self.dispatcher)); | 197 _browserState, self.dispatcher); |
196 [_consumer setLogoVendor:_doodleController]; | 198 [_consumer setLogoVendor:_doodleController]; |
197 [self updateShowLogo]; | 199 [self updateShowLogo]; |
198 | 200 |
199 // Set up most visited sites. This call may have the side effect of | 201 // Set up most visited sites. This call may have the side effect of |
200 // triggering -onMostVisitedURLsAvailable immediately, which can load the | 202 // triggering -onMostVisitedURLsAvailable immediately, which can load the |
201 // view before dataSource is set. | 203 // view before dataSource is set. |
202 _mostVisitedSites = | 204 _mostVisitedSites = |
203 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); | 205 IOSMostVisitedSitesFactory::NewForBrowserState(_browserState); |
204 _mostVisitedObserverBridge.reset( | 206 _mostVisitedObserverBridge.reset( |
205 new ntp_tiles::MostVisitedSitesObserverBridge(self)); | 207 new ntp_tiles::MostVisitedSitesObserverBridge(self)); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 366 |
365 if (_notification_promo->IsURLPromo()) { | 367 if (_notification_promo->IsURLPromo()) { |
366 [self.dispatcher webPageOrderedOpen:_notification_promo->url() | 368 [self.dispatcher webPageOrderedOpen:_notification_promo->url() |
367 referrer:web::Referrer() | 369 referrer:web::Referrer() |
368 inBackground:NO | 370 inBackground:NO |
369 appendTo:kCurrentTab]; | 371 appendTo:kCurrentTab]; |
370 return; | 372 return; |
371 } | 373 } |
372 | 374 |
373 if (_notification_promo->IsChromeCommand()) { | 375 if (_notification_promo->IsChromeCommand()) { |
374 base::scoped_nsobject<GenericChromeCommand> command( | 376 GenericChromeCommand* command = [[GenericChromeCommand alloc] |
375 [[GenericChromeCommand alloc] | 377 initWithTag:_notification_promo->command_id()]; |
376 initWithTag:_notification_promo->command_id()]); | |
377 [self.dispatcher chromeExecuteCommand:command]; | 378 [self.dispatcher chromeExecuteCommand:command]; |
378 return; | 379 return; |
379 } | 380 } |
380 NOTREACHED(); | 381 NOTREACHED(); |
381 } | 382 } |
382 | 383 |
383 #pragma mark - Private | 384 #pragma mark - Private |
384 | 385 |
385 - (void)useFreshData { | 386 - (void)useFreshData { |
386 if (self.freshMostVisitedData.size() == 0) { | 387 if (self.freshMostVisitedData.size() == 0) { |
387 return; | 388 return; |
388 } | 389 } |
389 _mostVisitedData = self.freshMostVisitedData; | 390 _mostVisitedData = self.freshMostVisitedData; |
390 self.freshMostVisitedData.clear(); | 391 self.freshMostVisitedData.clear(); |
391 [self.consumer mostVisitedDataUpdated]; | 392 [self.consumer mostVisitedDataUpdated]; |
392 } | 393 } |
393 | 394 |
394 @end | 395 @end |
OLD | NEW |