Chromium Code Reviews| Index: ios/chrome/browser/web/mailto_url_rewriter.mm |
| diff --git a/ios/chrome/browser/web/mailto_url_rewriter.mm b/ios/chrome/browser/web/mailto_url_rewriter.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2fbc833531ea6dfe24dfc2ca74f1de9c1e3cd9b0 |
| --- /dev/null |
| +++ b/ios/chrome/browser/web/mailto_url_rewriter.mm |
| @@ -0,0 +1,156 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/web/mailto_url_rewriter.h" |
| + |
| +#import "base/logging.h" |
| +#import "ios/chrome/browser/web/mailto_handler.h" |
| +#import "ios/chrome/browser/web/mailto_handler_gmail.h" |
| +#import "ios/chrome/browser/web/mailto_handler_system_mail.h" |
| + |
| +#import <UIKit/UIKit.h> |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +// The key for NSUserDefaults to store the Mail client selected to handle |
| +// mailto: URL scheme. |
| +NSString* const kMailtoDefaultHandlerKey = @"MailtoHandlerDefault"; |
| +} // namespace |
| + |
| +@interface MailtoURLRewriter () |
| + |
| +// Dictionary keyed by the App Store ID of the Mail client and the value is |
| +// the MailtoHandler object that can rewrite a mailto: URL. |
| +@property(nonatomic, strong) |
| + NSMutableDictionary<NSString*, MailtoHandler*>* handlers; |
| + |
| +// Private method for testing to clear the default state. |
| ++ (void)resetDefaultHandlerID; |
| + |
| +// Private method to add |handlerApp| to the list of know Mail client apps. |
| +- (void)addMailtoApp:(MailtoHandler*)handlerApp; |
| + |
| +// Custom logic to handle the migration from Google Native App Launcher options |
| +// to this simplified mailto: URL only system. This must be called after |
| +// -addMailtoApp: has been called to add all the known Mail client apps. |
| +// TODO(crbug.com/718601): At some point in the future when almost all users |
| +// have upgraded, this method can be removed. |
| +- (void)convertLegacyOptions; |
| + |
| +@end |
| + |
| +@implementation MailtoURLRewriter |
| +@synthesize handlers = _handlers; |
| + |
| ++ (NSString*)systemMailApp { |
| + // This is the App Store ID for Apple Mail app. |
| + // See https://itunes.apple.com/us/app/mail/id1108187098?mt=8 |
| + return @"1108187098"; |
| +} |
| + |
| +- (instancetype)init { |
| + self = [super init]; |
| + if (self) { |
| + _handlers = [NSMutableDictionary dictionary]; |
| + } |
| + return self; |
| +} |
| + |
| +- (instancetype)initWithStandardHandlers { |
| + self = [self init]; |
| + if (self) { |
| + [self addMailtoApp:[[MailtoHandlerSystemMail alloc] init]]; |
| + [self addMailtoApp:[[MailtoHandlerGmail alloc] init]]; |
| + [self convertLegacyOptions]; |
| + } |
| + return self; |
| +} |
| + |
| +- (NSArray<MailtoHandler*>*)defaultHandlers { |
| + return [_handlers allValues]; |
| +} |
| + |
| +- (NSString*)defaultHandlerID { |
| + NSString* value = [[NSUserDefaults standardUserDefaults] |
| + stringForKey:kMailtoDefaultHandlerKey]; |
| + return value ? value : [[self class] systemMailApp]; |
| +} |
| + |
| +- (void)setDefaultHandlerID:(NSString*)appStoreID { |
| + DCHECK([appStoreID length]); |
| + [[NSUserDefaults standardUserDefaults] setObject:appStoreID |
| + forKey:kMailtoDefaultHandlerKey]; |
| +} |
| + |
| +- (NSString*)rewriteMailtoURL:(const GURL&)gURL { |
| + NSString* value = [self defaultHandlerID]; |
| + if ([value length]) { |
| + MailtoHandler* handler = _handlers[value]; |
| + if (handler) { |
| + return [handler rewriteMailtoURL:gURL]; |
| + } |
| + } |
| + return nil; |
| +} |
| + |
| +#pragma mark - Private |
| + |
| ++ (void)resetDefaultHandlerID { |
| + [[NSUserDefaults standardUserDefaults] |
| + removeObjectForKey:kMailtoDefaultHandlerKey]; |
| +} |
| + |
| +- (void)addMailtoApp:(MailtoHandler*)handlerApp { |
| + if ([handlerApp isAvailable]) |
| + [_handlers setObject:handlerApp forKey:[handlerApp appStoreID]]; |
| +} |
| + |
| +- (void)convertLegacyOptions { |
|
rohitrao (ping after 24h)
2017/05/06 00:09:41
I don't fully follow the logic in this method. It
pkl (ping after 24h if needed)
2017/05/08 17:07:56
The migration path should be executed just once. I
|
| + // If a default handler for mailto: has already been registered, this is not |
| + // an upgrade case that needs to take legacy options into account. |
| + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| + if ([defaults objectForKey:kMailtoDefaultHandlerKey]) |
| + return; |
| + |
| + NSString* const kGmailAppStoreID = @"422689480"; |
| + MailtoHandler* gmailHandler = _handlers[kGmailAppStoreID]; |
| + if (!gmailHandler) |
| + return; |
| + |
| + // If Gmail app is not installed or otherwise unavailable, do not make it the |
| + // default mailto: handler app. |
| + if (![gmailHandler isAvailable]) |
| + [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
| + |
| + // The key used in NSUserDefaults is from legacy code when Native App |
| + // Launcher was still in use. The general format is a string prefix, |
| + // underscore, then followed by the App Store ID of the application. |
| + NSString* const kLegacyShouldAutoOpenKey = |
| + [NSString stringWithFormat:@"ShouldAutoOpenLinks_%@", kGmailAppStoreID]; |
| + NSNumber* legacyValue = [defaults objectForKey:kLegacyShouldAutoOpenKey]; |
| + // User previously did not have a selection made for opening mailto: links |
| + // with Gmail, upgrade will set Gmail app to be the default mailto: handler. |
|
rohitrao (ping after 24h)
2017/05/06 00:09:41
What's the rationale for choosing gmail to be the
pkl (ping after 24h if needed)
2017/05/08 17:07:56
This preserves the behavior in Google App Launcher
|
| + if (!legacyValue) { |
| + [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey]; |
|
rohitrao (ping after 24h)
2017/05/06 00:09:41
If the user has migrated successfully in the past
pkl (ping after 24h if needed)
2017/05/08 17:07:56
If user deletes Gmail app after a successful migra
|
| + } else { |
| + switch ([legacyValue intValue]) { |
| + case 0: // kAutoOpenLinksNotSet |
| + case 2: // kAutoOpenLinksYes |
| + [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey]; |
| + break; |
| + case 1: // kAutoOpenLinksNo |
| + [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
| + break; |
| + default: |
| + NOTREACHED(); |
| + break; |
| + } |
| + [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; |
| + } |
| +} |
| + |
| +@end |