Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/chrome/browser/web/mailto_url_rewriter.h" | |
| 6 | |
| 7 #import "base/logging.h" | |
| 8 #import "ios/chrome/browser/web/mailto_handler.h" | |
| 9 #import "ios/chrome/browser/web/mailto_handler_gmail.h" | |
| 10 #import "ios/chrome/browser/web/mailto_handler_system_mail.h" | |
| 11 | |
| 12 #import <UIKit/UIKit.h> | |
| 13 | |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 15 #error "This file requires ARC support." | |
| 16 #endif | |
| 17 | |
| 18 namespace { | |
| 19 // The key for NSUserDefaults to store the Mail client selected to handle | |
| 20 // mailto: URL scheme. | |
| 21 NSString* const kMailtoDefaultHandlerKey = @"MailtoHandlerDefault"; | |
| 22 } // namespace | |
| 23 | |
| 24 @interface MailtoURLRewriter () | |
| 25 | |
| 26 // Dictionary keyed by the App Store ID of the Mail client and the value is | |
| 27 // the MailtoHandler object that can rewrite a mailto: URL. | |
| 28 @property(nonatomic, strong) | |
| 29 NSMutableDictionary<NSString*, MailtoHandler*>* handlers; | |
| 30 | |
| 31 // Private method to add |handlerApp| to the list of know Mail client apps. | |
| 32 - (void)addMailtoApp:(MailtoHandler*)handlerApp; | |
| 33 | |
| 34 // Custom logic to handle the migration from Google Native App Launcher options | |
| 35 // to this simplified mailto: URL only system. This must be called after | |
| 36 // -addMailtoApp: has been called to add all the known Mail client apps. | |
| 37 - (void)convertLegacyOptions; | |
| 38 | |
| 39 @end | |
| 40 | |
| 41 @implementation MailtoURLRewriter | |
| 42 @synthesize handlers = _handlers; | |
| 43 | |
| 44 + (instancetype)sharedInstance { | |
|
jif
2017/05/03 13:01:35
Assuming you agree with making this class not a si
pkl (ping after 24h if needed)
2017/05/03 22:17:18
I'm using -init and initWithStandardHandlers.
| |
| 45 static MailtoURLRewriter* sharedInstance; | |
| 46 static dispatch_once_t onceToken; | |
| 47 dispatch_once(&onceToken, ^{ | |
| 48 sharedInstance = [[MailtoURLRewriter alloc] init]; | |
| 49 [sharedInstance addMailtoApp:[[MailtoHandlerSystemMail alloc] init]]; | |
| 50 [sharedInstance addMailtoApp:[[MailtoHandlerGmail alloc] init]]; | |
| 51 [sharedInstance convertLegacyOptions]; | |
| 52 }); | |
| 53 return sharedInstance; | |
| 54 } | |
| 55 | |
| 56 + (NSString*)systemMailApp { | |
| 57 // This is the App Store ID for Apple Mail app. | |
| 58 // See https://itunes.apple.com/us/app/mail/id1108187098?mt=8 | |
| 59 return @"1108187098"; | |
| 60 } | |
| 61 | |
| 62 - (instancetype)init { | |
| 63 self = [super init]; | |
| 64 if (self) { | |
| 65 _handlers = [NSMutableDictionary dictionary]; | |
| 66 } | |
| 67 return self; | |
| 68 } | |
| 69 | |
| 70 - (NSArray<MailtoHandler*>*)defaultHandlers { | |
| 71 return [_handlers allValues]; | |
| 72 } | |
| 73 | |
| 74 - (NSString*)defaultHandlerID { | |
| 75 NSString* value = [[NSUserDefaults standardUserDefaults] | |
| 76 stringForKey:kMailtoDefaultHandlerKey]; | |
| 77 return value ? value : [[self class] systemMailApp]; | |
| 78 } | |
| 79 | |
| 80 - (void)setDefaultHandlerID:(NSString*)appStoreID { | |
| 81 DCHECK([appStoreID length]); | |
| 82 [[NSUserDefaults standardUserDefaults] setObject:appStoreID | |
| 83 forKey:kMailtoDefaultHandlerKey]; | |
| 84 } | |
| 85 | |
| 86 - (NSString*)rewriteMailtoURL:(const GURL&)gURL { | |
| 87 NSString* value = [self defaultHandlerID]; | |
| 88 if ([value length]) { | |
| 89 MailtoHandler* handler = _handlers[value]; | |
| 90 if (handler) { | |
| 91 return [handler rewriteMailtoURL:gURL]; | |
| 92 } | |
| 93 } | |
| 94 return nil; | |
| 95 } | |
| 96 | |
| 97 #pragma mark - Private | |
| 98 | |
| 99 - (void)addMailtoApp:(MailtoHandler*)handlerApp { | |
| 100 if ([handlerApp isAvailable]) | |
| 101 [_handlers setObject:handlerApp forKey:[handlerApp appStoreID]]; | |
| 102 } | |
| 103 | |
| 104 - (void)convertLegacyOptions { | |
| 105 NSString* const kGmailAppStoreID = @"422689480"; | |
| 106 MailtoHandler* gmailHandler = _handlers[kGmailAppStoreID]; | |
| 107 if (!gmailHandler) | |
| 108 return; | |
| 109 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
| 110 | |
| 111 // If Gmail app is not installed or otherwise unavailable, do not make it the | |
| 112 // default mailto: handler app. | |
| 113 if (![gmailHandler isAvailable]) | |
| 114 [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; | |
| 115 | |
| 116 NSString* const kLegacyShouldAutoOpenKey = @"ShouldAutoOpenLinks_422689480"; | |
| 117 NSNumber* legacyValue = [defaults objectForKey:kLegacyShouldAutoOpenKey]; | |
| 118 // User previously did not have a selection made for opening mailto: links | |
| 119 // with Gmail, upgrade will set Gmail app to be the default mailto: handler. | |
| 120 if (!legacyValue) { | |
| 121 [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey]; | |
| 122 } else { | |
| 123 switch ([legacyValue intValue]) { | |
| 124 case 0: | |
| 125 case 2: | |
| 126 [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey]; | |
| 127 break; | |
| 128 case 1: | |
| 129 [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; | |
| 130 break; | |
| 131 default: | |
| 132 NOTREACHED(); | |
| 133 break; | |
| 134 } | |
| 135 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 @end | |
| OLD | NEW |