Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: ios/chrome/browser/ui/ntp/google_landing_mediator.mm

Issue 2833513002: Replace TabModel with WebStateList in GoogleLandingController. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 13 matching lines...) Expand all
24 #import "ios/chrome/browser/ntp_tiles/most_visited_sites_observer_bridge.h" 24 #import "ios/chrome/browser/ntp_tiles/most_visited_sites_observer_bridge.h"
25 #include "ios/chrome/browser/reading_list/reading_list_model_factory.h" 25 #include "ios/chrome/browser/reading_list/reading_list_model_factory.h"
26 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" 26 #include "ios/chrome/browser/search_engines/template_url_service_factory.h"
27 #import "ios/chrome/browser/ui/browser_view_controller.h" 27 #import "ios/chrome/browser/ui/browser_view_controller.h"
28 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" 28 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h"
29 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" 29 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h"
30 #import "ios/chrome/browser/ui/ntp/google_landing_consumer.h" 30 #import "ios/chrome/browser/ui/ntp/google_landing_consumer.h"
31 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h" 31 #import "ios/chrome/browser/ui/ntp/notification_promo_whats_new.h"
32 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" 32 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h"
33 #import "ios/chrome/browser/ui/url_loader.h" 33 #import "ios/chrome/browser/ui/url_loader.h"
34 #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"
34 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" 36 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
35 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" 37 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h"
36 38
37 using base::UserMetricsAction; 39 using base::UserMetricsAction;
38 40
39 namespace { 41 namespace {
40 42
41 const NSInteger kMaxNumMostVisitedFavicons = 8; 43 const NSInteger kMaxNumMostVisitedFavicons = 8;
42 44
43 } // namespace 45 } // namespace
(...skipping 28 matching lines...) Expand all
72 SearchEngineObserver::~SearchEngineObserver() { 74 SearchEngineObserver::~SearchEngineObserver() {
73 _templateURLService->RemoveObserver(this); 75 _templateURLService->RemoveObserver(this);
74 } 76 }
75 77
76 void SearchEngineObserver::OnTemplateURLServiceChanged() { 78 void SearchEngineObserver::OnTemplateURLServiceChanged() {
77 [_owner updateShowLogo]; 79 [_owner updateShowLogo];
78 } 80 }
79 81
80 } // namespace google_landing 82 } // namespace google_landing
81 83
82 @interface GoogleLandingMediator ()<MostVisitedSitesObserving> { 84 @interface GoogleLandingMediator ()<MostVisitedSitesObserving,
85 WebStateListObserving> {
83 // The ChromeBrowserState associated with this mediator. 86 // The ChromeBrowserState associated with this mediator.
84 ios::ChromeBrowserState* _browserState; // Weak. 87 ios::ChromeBrowserState* _browserState; // Weak.
85 88
86 // |YES| if impressions were logged already and shouldn't be logged again. 89 // |YES| if impressions were logged already and shouldn't be logged again.
87 BOOL _recordedPageImpression; 90 BOOL _recordedPageImpression;
88 91
89 // The designated url loader. 92 // The designated url loader.
90 id<UrlLoader> _loader; // Weak. 93 id<UrlLoader> _loader; // Weak.
91 94
92 // Delegate to focus and blur the omnibox. 95 // Delegate to focus and blur the omnibox.
(...skipping 12 matching lines...) Expand all
105 _most_visited_observer_bridge; 108 _most_visited_observer_bridge;
106 109
107 std::unique_ptr<ntp_tiles::MostVisitedSites> _most_visited_sites; 110 std::unique_ptr<ntp_tiles::MostVisitedSites> _most_visited_sites;
108 111
109 // Most visited data from the MostVisitedSites service (copied upon receiving 112 // Most visited data from the MostVisitedSites service (copied upon receiving
110 // the callback). 113 // the callback).
111 ntp_tiles::NTPTilesVector _mostVisitedData; 114 ntp_tiles::NTPTilesVector _mostVisitedData;
112 115
113 base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate; 116 base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate;
114 117
115 base::scoped_nsobject<TabModel> _tabModel; 118 // Observes the WebStateList so that this mediator can update the UI when the
119 // active WebState changes.
120 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver;
116 121
117 // What's new promo. 122 // What's new promo.
118 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo; 123 std::unique_ptr<NotificationPromoWhatsNew> _notification_promo;
119 } 124 }
120 125
121 // Consumer to handle google landing update notifications. 126 // Consumer to handle google landing update notifications.
122 @property(nonatomic) id<GoogleLandingConsumer> consumer; 127 @property(nonatomic) id<GoogleLandingConsumer> consumer;
123 128
129 // The WebStateList that is being observed by this mediator.
130 @property(nonatomic, assign) WebStateList* webStateList;
131
124 // Perform initial setup. 132 // Perform initial setup.
125 - (void)setUp; 133 - (void)setUp;
126 134
127 @end 135 @end
128 136
129 @implementation GoogleLandingMediator 137 @implementation GoogleLandingMediator
130 138
131 @synthesize consumer = _consumer; 139 @synthesize consumer = _consumer;
140 @synthesize webStateList = _webStateList;
132 141
133 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer 142 - (instancetype)initWithConsumer:(id<GoogleLandingConsumer>)consumer
134 browserState:(ios::ChromeBrowserState*)browserState 143 browserState:(ios::ChromeBrowserState*)browserState
135 loader:(id<UrlLoader>)loader 144 loader:(id<UrlLoader>)loader
136 focuser:(id<OmniboxFocuser>)focuser 145 focuser:(id<OmniboxFocuser>)focuser
137 webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate 146 webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate
138 tabModel:(TabModel*)tabModel { 147 webStateList:(WebStateList*)webStateList {
139 self = [super init]; 148 self = [super init];
140 if (self) { 149 if (self) {
141 self.consumer = consumer; 150 self.consumer = consumer;
142 _browserState = browserState; 151 _browserState = browserState;
143 _loader = loader; 152 _loader = loader;
144 _focuser.reset(focuser); 153 _focuser.reset(focuser);
145 _webToolbarDelegate.reset(webToolbarDelegate); 154 _webToolbarDelegate.reset(webToolbarDelegate);
146 _tabModel.reset([tabModel retain]); 155 _webStateList = webStateList;
156
157 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self);
rohitrao (ping after 24h) 2017/04/19 19:26:33 Louis has an example CL that uses a ScopedObserver
justincohen 2017/04/19 21:12:23 Acknowledged.
158 _webStateList->AddObserver(_webStateListObserver.get());
159
147 [self setUp]; 160 [self setUp];
148 } 161 }
149 return self; 162 return self;
150 } 163 }
151 164
152 - (void)dealloc { 165 - (void)dealloc {
166 _webStateList->RemoveObserver(_webStateListObserver.get());
rohitrao (ping after 24h) 2017/04/19 19:26:33 dealloc might be too late, if this object has its
justincohen 2017/04/19 21:12:23 I think the view's life can be extended, but not t
153 [[NSNotificationCenter defaultCenter] removeObserver:self.consumer]; 167 [[NSNotificationCenter defaultCenter] removeObserver:self.consumer];
154 [super dealloc]; 168 [super dealloc];
155 } 169 }
156 170
157 - (void)setUp { 171 - (void)setUp {
158 self.consumer.offTheRecord = _browserState->IsOffTheRecord(); 172 self.consumer.offTheRecord = _browserState->IsOffTheRecord();
159 self.consumer.voiceSearchEnabled = ios::GetChromeBrowserProvider() 173 self.consumer.voiceSearchEnabled = ios::GetChromeBrowserProvider()
160 ->GetVoiceSearchProvider() 174 ->GetVoiceSearchProvider()
161 ->IsVoiceSearchEnabled(); 175 ->IsVoiceSearchEnabled();
176 self.consumer.tabCount = self.webStateList->count();
162 177
163 // Set up template URL service to listen for default search engine changes. 178 // Set up template URL service to listen for default search engine changes.
164 _templateURLService = 179 _templateURLService =
165 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState); 180 ios::TemplateURLServiceFactory::GetForBrowserState(_browserState);
166 _observer.reset( 181 _observer.reset(
167 new google_landing::SearchEngineObserver(self, _templateURLService)); 182 new google_landing::SearchEngineObserver(self, _templateURLService));
168 _templateURLService->Load(); 183 _templateURLService->Load();
169 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor( 184 _doodleController.reset(ios::GetChromeBrowserProvider()->CreateLogoVendor(
170 _browserState, _loader)); 185 _browserState, _loader));
171 self.consumer.logoVendor = _doodleController; 186 self.consumer.logoVendor = _doodleController;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 - (void)onIconMadeAvailable:(const GURL&)siteUrl { 252 - (void)onIconMadeAvailable:(const GURL&)siteUrl {
238 for (size_t i = 0; i < _mostVisitedData.size(); ++i) { 253 for (size_t i = 0; i < _mostVisitedData.size(); ++i) {
239 const ntp_tiles::NTPTile& ntpTile = _mostVisitedData[i]; 254 const ntp_tiles::NTPTile& ntpTile = _mostVisitedData[i];
240 if (ntpTile.url == siteUrl) { 255 if (ntpTile.url == siteUrl) {
241 [self.consumer mostVisitedIconMadeAvailableAtIndex:i]; 256 [self.consumer mostVisitedIconMadeAvailableAtIndex:i];
242 break; 257 break;
243 } 258 }
244 } 259 }
245 } 260 }
246 261
262 #pragma mark - WebStateListObserving
263
264 - (void)webStateList:(WebStateList*)webStateList
265 didInsertWebState:(web::WebState*)webState
266 atIndex:(int)index {
267 self.consumer.tabCount = self.webStateList->count();
268 }
269
270 - (void)webStateList:(WebStateList*)webStateList
271 didDetachWebState:(web::WebState*)webState
272 atIndex:(int)atIndex {
273 self.consumer.tabCount = self.webStateList->count();
274 }
275
247 #pragma mark - GoogleLandingDataSource 276 #pragma mark - GoogleLandingDataSource
248 277
249 - (NSUInteger)maxSitesShown { 278 - (NSUInteger)maxSitesShown {
250 return kMaxNumMostVisitedFavicons; 279 return kMaxNumMostVisitedFavicons;
251 } 280 }
252 281
253 - (void)addBlacklistedURL:(const GURL&)url { 282 - (void)addBlacklistedURL:(const GURL&)url {
254 _most_visited_sites->AddOrRemoveBlacklistedUrl(url, true); 283 _most_visited_sites->AddOrRemoveBlacklistedUrl(url, true);
255 } 284 }
256 285
(...skipping 27 matching lines...) Expand all
284 } 313 }
285 314
286 - (favicon::LargeIconService*)largeIconService { 315 - (favicon::LargeIconService*)largeIconService {
287 return IOSChromeLargeIconServiceFactory::GetForBrowserState(_browserState); 316 return IOSChromeLargeIconServiceFactory::GetForBrowserState(_browserState);
288 } 317 }
289 318
290 - (id<WebToolbarDelegate>)toolbarDelegate { 319 - (id<WebToolbarDelegate>)toolbarDelegate {
291 return _webToolbarDelegate; 320 return _webToolbarDelegate;
292 } 321 }
293 322
294 - (TabModel*)tabModel {
295 return _tabModel;
296 }
297
298 - (BOOL)promoCanShow { 323 - (BOOL)promoCanShow {
299 return _notification_promo && _notification_promo->CanShow(); 324 return _notification_promo && _notification_promo->CanShow();
300 } 325 }
301 326
302 - (NSString*)promoText { 327 - (NSString*)promoText {
303 DCHECK(_notification_promo); 328 DCHECK(_notification_promo);
304 return base::SysUTF8ToNSString(_notification_promo->promo_text()); 329 return base::SysUTF8ToNSString(_notification_promo->promo_text());
305 } 330 }
306 331
307 - (WhatsNewIcon)promoIcon { 332 - (WhatsNewIcon)promoIcon {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 420
396 - (void)onFakeboxBlur { 421 - (void)onFakeboxBlur {
397 [_focuser onFakeboxBlur]; 422 [_focuser onFakeboxBlur];
398 } 423 }
399 424
400 - (void)onFakeboxAnimationComplete { 425 - (void)onFakeboxAnimationComplete {
401 [_focuser onFakeboxAnimationComplete]; 426 [_focuser onFakeboxAnimationComplete];
402 } 427 }
403 428
404 @end 429 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698