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

Side by Side Diff: ios/chrome/browser/upgrade/upgrade_center.mm

Issue 2889023002: [ObjC ARC] Converts ios/chrome/browser/upgrade:upgrade to ARC. (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « ios/chrome/browser/upgrade/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/upgrade/upgrade_center.h" 5 #import "ios/chrome/browser/upgrade/upgrade_center.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/mac/bundle_locations.h" 11 #include "base/mac/bundle_locations.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
14 #include "base/scoped_observer.h" 13 #include "base/scoped_observer.h"
15 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
16 #include "base/version.h" 15 #include "base/version.h"
17 #include "components/infobars/core/confirm_infobar_delegate.h" 16 #include "components/infobars/core/confirm_infobar_delegate.h"
18 #include "components/infobars/core/infobar.h" 17 #include "components/infobars/core/infobar.h"
19 #include "components/infobars/core/infobar_manager.h" 18 #include "components/infobars/core/infobar_manager.h"
20 #include "components/version_info/version_info.h" 19 #include "components/version_info/version_info.h"
21 #import "ios/chrome/browser/open_url_util.h" 20 #import "ios/chrome/browser/open_url_util.h"
22 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" 21 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h"
23 #import "ios/chrome/browser/ui/commands/open_url_command.h" 22 #import "ios/chrome/browser/ui/commands/open_url_command.h"
24 #include "ios/chrome/grit/ios_chromium_strings.h" 23 #include "ios/chrome/grit/ios_chromium_strings.h"
25 #include "ios/chrome/grit/ios_strings.h" 24 #include "ios/chrome/grit/ios_strings.h"
26 #import "ios/web/public/url_scheme_util.h" 25 #import "ios/web/public/url_scheme_util.h"
27 #import "net/base/mac/url_conversions.h" 26 #import "net/base/mac/url_conversions.h"
28 #include "ui/base/l10n/l10n_util.h" 27 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/gfx/image/image.h" 28 #include "ui/gfx/image/image.h"
30 #include "url/gurl.h" 29 #include "url/gurl.h"
31 30
31 #if !defined(__has_feature) || !__has_feature(objc_arc)
32 #error "This file requires ARC support."
33 #endif
34
32 @interface UpgradeCenter () 35 @interface UpgradeCenter ()
33 // Creates infobars on all tabs. 36 // Creates infobars on all tabs.
34 - (void)showUpgradeInfoBars; 37 - (void)showUpgradeInfoBars;
35 // Removes all the infobars. 38 // Removes all the infobars.
36 - (void)hideUpgradeInfoBars; 39 - (void)hideUpgradeInfoBars;
37 // Callback when an infobar is closed, for any reason. Perform upgrade is set to 40 // Callback when an infobar is closed, for any reason. Perform upgrade is set to
38 // YES if the user choose to upgrade. 41 // YES if the user choose to upgrade.
39 - (void)dismissedInfoBar:(NSString*)tabId performUpgrade:(BOOL)shouldUpgrade; 42 - (void)dismissedInfoBar:(NSString*)tabId performUpgrade:(BOOL)shouldUpgrade;
40 // Returns YES if the infobar should be shown. 43 // Returns YES if the infobar should be shown.
41 - (BOOL)shouldShowInfoBar; 44 - (BOOL)shouldShowInfoBar;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 137
135 ~UpgradeInfoBarDismissObserver() override {} 138 ~UpgradeInfoBarDismissObserver() override {}
136 139
137 void RegisterObserver(infobars::InfoBarManager* infobar_manager, 140 void RegisterObserver(infobars::InfoBarManager* infobar_manager,
138 UpgradeInfoBarDelegate* infobar_delegate, 141 UpgradeInfoBarDelegate* infobar_delegate,
139 NSString* tab_id, 142 NSString* tab_id,
140 UpgradeCenter* dismiss_delegate) { 143 UpgradeCenter* dismiss_delegate) {
141 scoped_observer_.Add(infobar_manager); 144 scoped_observer_.Add(infobar_manager);
142 infobar_delegate_ = infobar_delegate; 145 infobar_delegate_ = infobar_delegate;
143 dismiss_delegate_ = dismiss_delegate; 146 dismiss_delegate_ = dismiss_delegate;
144 tab_id_.reset([tab_id copy]); 147 tab_id_ = [tab_id copy];
145 } 148 }
146 149
147 UpgradeInfoBarDelegate* infobar_delegate() { return infobar_delegate_; } 150 UpgradeInfoBarDelegate* infobar_delegate() { return infobar_delegate_; }
148 151
149 private: 152 private:
150 // infobars::InfoBarManager::Observer implementation. 153 // infobars::InfoBarManager::Observer implementation.
151 void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override { 154 void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override {
152 if (infobar->delegate() == infobar_delegate_) { 155 if (infobar->delegate() == infobar_delegate_) {
153 [dismiss_delegate_ dismissedInfoBar:tab_id_.get() 156 [dismiss_delegate_ dismissedInfoBar:tab_id_
154 performUpgrade:infobar_delegate_->AcceptPressed()]; 157 performUpgrade:infobar_delegate_->AcceptPressed()];
155 } 158 }
156 } 159 }
157 160
158 void OnManagerShuttingDown( 161 void OnManagerShuttingDown(
159 infobars::InfoBarManager* infobar_manager) override { 162 infobars::InfoBarManager* infobar_manager) override {
160 scoped_observer_.Remove(infobar_manager); 163 scoped_observer_.Remove(infobar_manager);
161 } 164 }
162 165
163 UpgradeInfoBarDelegate* infobar_delegate_; 166 UpgradeInfoBarDelegate* infobar_delegate_;
164 UpgradeCenter* dismiss_delegate_; 167 UpgradeCenter* dismiss_delegate_;
sdefresne 2017/05/31 08:16:13 This is an Objective-C pointer. Since the original
liaoyuke 2017/06/13 18:17:31 Done.
165 base::scoped_nsobject<NSString> tab_id_; 168 NSString* tab_id_;
166 ScopedObserver<infobars::InfoBarManager, infobars::InfoBarManager::Observer> 169 ScopedObserver<infobars::InfoBarManager, infobars::InfoBarManager::Observer>
167 scoped_observer_; 170 scoped_observer_;
168 171
169 DISALLOW_COPY_AND_ASSIGN(UpgradeInfoBarDismissObserver); 172 DISALLOW_COPY_AND_ASSIGN(UpgradeInfoBarDismissObserver);
170 }; 173 };
171 174
172 } // namespace 175 } // namespace
173 176
174 // The delegateHolder is a simple wrapper to be able to store all the 177 // The delegateHolder is a simple wrapper to be able to store all the
175 // infoBarDelegate related information in an object that can be put in 178 // infoBarDelegate related information in an object that can be put in
(...skipping 27 matching lines...) Expand all
203 - (UpgradeInfoBarDelegate*)infoBarDelegate { 206 - (UpgradeInfoBarDelegate*)infoBarDelegate {
204 return observer_.infobar_delegate(); 207 return observer_.infobar_delegate();
205 } 208 }
206 209
207 @end 210 @end
208 211
209 @implementation UpgradeCenter { 212 @implementation UpgradeCenter {
210 // YES if the infobars are currently visible. 213 // YES if the infobars are currently visible.
211 BOOL upgradeInfoBarIsVisible_; 214 BOOL upgradeInfoBarIsVisible_;
212 // Used to store the visible upgrade infobars, indexed by tabId. 215 // Used to store the visible upgrade infobars, indexed by tabId.
213 base::scoped_nsobject<NSMutableDictionary> upgradeInfoBarDelegates_; 216 NSMutableDictionary* upgradeInfoBarDelegates_;
sdefresne 2017/05/31 08:16:13 NSMutableDictionary<NSString*, DelegateHolder*>* u
liaoyuke 2017/06/13 18:17:31 Done.
214 // Stores the clients of the upgrade center. These objectiveC objects are not 217 // Stores the clients of the upgrade center. These objectiveC objects are not
215 // retained. 218 // retained.
216 std::set<id<UpgradeCenterClientProtocol>> clients_; 219 std::set<id<UpgradeCenterClientProtocol>> clients_;
sdefresne 2017/05/31 08:16:13 The comments says that the Objective-C objects are
liaoyuke 2017/06/13 18:17:31 Thank you for explaining! Done.
217 #ifndef NDEBUG 220 #ifndef NDEBUG
sdefresne 2017/05/31 08:16:13 followup: all "#ifndef NDEBUG" should instead be "
liaoyuke 2017/06/13 18:17:31 Will Create a follow up CL to clean all of them up
218 bool inCallback_; 221 bool inCallback_;
sdefresne 2017/05/31 08:16:13 bool -> BOOL
liaoyuke 2017/06/13 18:17:31 Done.
219 #endif 222 #endif
220 } 223 }
221 224
222 + (UpgradeCenter*)sharedInstance { 225 + (UpgradeCenter*)sharedInstance {
223 static UpgradeCenter* obj; 226 static UpgradeCenter* obj;
224 static dispatch_once_t onceToken; 227 static dispatch_once_t onceToken;
225 dispatch_once(&onceToken, ^{ 228 dispatch_once(&onceToken, ^{
226 obj = [[self alloc] init]; 229 obj = [[self alloc] init];
227 }); 230 });
228 return obj; 231 return obj;
229 } 232 }
230 233
231 - (instancetype)init { 234 - (instancetype)init {
232 self = [super init]; 235 self = [super init];
233 if (self) { 236 if (self) {
234 upgradeInfoBarDelegates_.reset([[NSMutableDictionary alloc] init]); 237 upgradeInfoBarDelegates_ = [[NSMutableDictionary alloc] init];
235 238
236 // There is no dealloc and no unregister as this class is a never 239 // There is no dealloc and no unregister as this class is a never
237 // deallocated singleton. 240 // deallocated singleton.
238 [[NSNotificationCenter defaultCenter] 241 [[NSNotificationCenter defaultCenter]
239 addObserver:self 242 addObserver:self
240 selector:@selector(applicationWillEnterForeground:) 243 selector:@selector(applicationWillEnterForeground:)
241 name:UIApplicationWillEnterForegroundNotification 244 name:UIApplicationWillEnterForegroundNotification
242 object:nil]; 245 object:nil];
243 246
244 upgradeInfoBarIsVisible_ = [self shouldShowInfoBar]; 247 upgradeInfoBarIsVisible_ = [self shouldShowInfoBar];
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 306
304 // Nothing to do if the infobar are not visible at this point in time. 307 // Nothing to do if the infobar are not visible at this point in time.
305 if (!upgradeInfoBarIsVisible_) 308 if (!upgradeInfoBarIsVisible_)
306 return; 309 return;
307 310
308 // Nothing to do if the infobar is already there. 311 // Nothing to do if the infobar is already there.
309 if ([upgradeInfoBarDelegates_ objectForKey:tabId]) 312 if ([upgradeInfoBarDelegates_ objectForKey:tabId])
310 return; 313 return;
311 314
312 auto infobarDelegate = base::MakeUnique<UpgradeInfoBarDelegate>(); 315 auto infobarDelegate = base::MakeUnique<UpgradeInfoBarDelegate>();
313 base::scoped_nsobject<DelegateHolder> delegateHolder([[DelegateHolder alloc] 316 DelegateHolder* delegateHolder =
314 initWithInfoBarManager:infoBarManager 317 [[DelegateHolder alloc] initWithInfoBarManager:infoBarManager
315 infoBarDelegate:infobarDelegate.get() 318 infoBarDelegate:infobarDelegate.get()
316 upgradeCenter:self 319 upgradeCenter:self
317 tabId:tabId]); 320 tabId:tabId];
318 321
319 [upgradeInfoBarDelegates_ setObject:delegateHolder forKey:tabId]; 322 [upgradeInfoBarDelegates_ setObject:delegateHolder forKey:tabId];
320 infoBarManager->AddInfoBar( 323 infoBarManager->AddInfoBar(
321 infoBarManager->CreateConfirmInfoBar(std::move(infobarDelegate))); 324 infoBarManager->CreateConfirmInfoBar(std::move(infobarDelegate)));
322 } 325 }
323 326
324 - (void)tabWillClose:(NSString*)tabId { 327 - (void)tabWillClose:(NSString*)tabId {
325 [upgradeInfoBarDelegates_ removeObjectForKey:tabId]; 328 [upgradeInfoBarDelegates_ removeObjectForKey:tabId];
326 } 329 }
327 330
328 - (void)dismissedInfoBar:(NSString*)tabId performUpgrade:(BOOL)shouldUpgrade { 331 - (void)dismissedInfoBar:(NSString*)tabId performUpgrade:(BOOL)shouldUpgrade {
329 // If the tabId is not in the upgradeInfoBarDelegates_ just ignore the 332 // If the tabId is not in the upgradeInfoBarDelegates_ just ignore the
330 // notification. In all likelyhood it was trigerred by calling 333 // notification. In all likelyhood it was trigerred by calling
331 // -hideUpgradeInfoBars. Or because a tab was closed without dismissing the 334 // -hideUpgradeInfoBars. Or because a tab was closed without dismissing the
332 // infobar. 335 // infobar.
333 base::scoped_nsobject<DelegateHolder> delegateHolder( 336 DelegateHolder* delegateHolder =
334 [[upgradeInfoBarDelegates_ objectForKey:tabId] retain]); 337 [upgradeInfoBarDelegates_ objectForKey:tabId];
335 if (!delegateHolder.get()) 338 if (!delegateHolder)
336 return; 339 return;
337 340
338 // Forget about this dismissed infobar. 341 // Forget about this dismissed infobar.
339 [upgradeInfoBarDelegates_ removeObjectForKey:tabId]; 342 [upgradeInfoBarDelegates_ removeObjectForKey:tabId];
340 343
341 // Get rid of all the infobars on the other tabs. 344 // Get rid of all the infobars on the other tabs.
342 [self hideUpgradeInfoBars]; 345 [self hideUpgradeInfoBars];
343 346
344 if (shouldUpgrade) { 347 if (shouldUpgrade) {
345 NSString* urlString = 348 NSString* urlString =
346 [[NSUserDefaults standardUserDefaults] valueForKey:kUpgradeURLKey]; 349 [[NSUserDefaults standardUserDefaults] valueForKey:kUpgradeURLKey];
347 if (!urlString) 350 if (!urlString)
348 return; // Missing URL, no upgrade possible. 351 return; // Missing URL, no upgrade possible.
349 352
350 GURL url = GURL(base::SysNSStringToUTF8(urlString)); 353 GURL url = GURL(base::SysNSStringToUTF8(urlString));
351 if (!url.is_valid()) 354 if (!url.is_valid())
352 return; 355 return;
353 356
354 if (web::UrlHasWebScheme(url)) { 357 if (web::UrlHasWebScheme(url)) {
355 // This URL can be opened in the application, just open in a new tab. 358 // This URL can be opened in the application, just open in a new tab.
356 base::scoped_nsobject<OpenUrlCommand> command( 359 OpenUrlCommand* command =
357 [[OpenUrlCommand alloc] initWithURLFromChrome:url]); 360 [[OpenUrlCommand alloc] initWithURLFromChrome:url];
358 UIWindow* main_window = [[UIApplication sharedApplication] keyWindow]; 361 UIWindow* main_window = [[UIApplication sharedApplication] keyWindow];
359 DCHECK(main_window); 362 DCHECK(main_window);
360 [main_window chromeExecuteCommand:command]; 363 [main_window chromeExecuteCommand:command];
361 } else { 364 } else {
362 // This URL scheme is not understood, ask the system to open it. 365 // This URL scheme is not understood, ask the system to open it.
363 NSURL* nsurl = [NSURL URLWithString:urlString]; 366 NSURL* nsurl = [NSURL URLWithString:urlString];
364 if (nsurl) { 367 if (nsurl) {
365 OpenUrlWithCompletionHandler(nsurl, nil); 368 OpenUrlWithCompletionHandler(nsurl, nil);
366 } 369 }
367 } 370 }
(...skipping 17 matching lines...) Expand all
385 forKey:kLastInfobarDisplayTimeKey]; 388 forKey:kLastInfobarDisplayTimeKey];
386 } 389 }
387 390
388 - (void)hideUpgradeInfoBars { 391 - (void)hideUpgradeInfoBars {
389 upgradeInfoBarIsVisible_ = NO; 392 upgradeInfoBarIsVisible_ = NO;
390 // It is important to call -allKeys here and not using a fast iteration on the 393 // It is important to call -allKeys here and not using a fast iteration on the
391 // dictionary directly: the dictionary is modified as we go... 394 // dictionary directly: the dictionary is modified as we go...
392 for (NSString* tabId in [upgradeInfoBarDelegates_ allKeys]) { 395 for (NSString* tabId in [upgradeInfoBarDelegates_ allKeys]) {
393 // It is important to retain the delegateHolder as otherwise it is 396 // It is important to retain the delegateHolder as otherwise it is
394 // deallocated as soon as it is removed from the dictionary. 397 // deallocated as soon as it is removed from the dictionary.
395 base::scoped_nsobject<DelegateHolder> delegateHolder( 398 DelegateHolder* delegateHolder =
396 [[upgradeInfoBarDelegates_ objectForKey:tabId] retain]); 399 [upgradeInfoBarDelegates_ objectForKey:tabId];
397 if (delegateHolder.get()) { 400 if (delegateHolder) {
398 [upgradeInfoBarDelegates_ removeObjectForKey:tabId]; 401 [upgradeInfoBarDelegates_ removeObjectForKey:tabId];
399 UpgradeInfoBarDelegate* delegate = [delegateHolder infoBarDelegate]; 402 UpgradeInfoBarDelegate* delegate = [delegateHolder infoBarDelegate];
400 DCHECK(delegate); 403 DCHECK(delegate);
401 delegate->RemoveSelf(); 404 delegate->RemoveSelf();
402 } 405 }
403 } 406 }
404 } 407 }
405 408
406 - (void)upgradeNotificationDidOccur:(const UpgradeRecommendedDetails&)details { 409 - (void)upgradeNotificationDidOccur:(const UpgradeRecommendedDetails&)details {
407 const GURL& upgradeUrl = details.upgrade_url; 410 const GURL& upgradeUrl = details.upgrade_url;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 } 453 }
451 454
452 - (void)setLastDisplayToPast { 455 - (void)setLastDisplayToPast {
453 NSDate* pastDate = 456 NSDate* pastDate =
454 [NSDate dateWithTimeIntervalSinceNow:-(kInfobarDisplayInterval + 1)]; 457 [NSDate dateWithTimeIntervalSinceNow:-(kInfobarDisplayInterval + 1)];
455 [[NSUserDefaults standardUserDefaults] setObject:pastDate 458 [[NSUserDefaults standardUserDefaults] setObject:pastDate
456 forKey:kLastInfobarDisplayTimeKey]; 459 forKey:kLastInfobarDisplayTimeKey];
457 } 460 }
458 461
459 @end 462 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/upgrade/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698