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..bd50049e6b3a6be61d7a78a3665276be458d4c23 |
--- /dev/null |
+++ b/ios/chrome/browser/web/mailto_url_rewriter.mm |
@@ -0,0 +1,139 @@ |
+// 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 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. |
+- (void)convertLegacyOptions; |
+ |
+@end |
+ |
+@implementation MailtoURLRewriter |
+@synthesize handlers = _handlers; |
+ |
++ (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.
|
+ static MailtoURLRewriter* sharedInstance; |
+ static dispatch_once_t onceToken; |
+ dispatch_once(&onceToken, ^{ |
+ sharedInstance = [[MailtoURLRewriter alloc] init]; |
+ [sharedInstance addMailtoApp:[[MailtoHandlerSystemMail alloc] init]]; |
+ [sharedInstance addMailtoApp:[[MailtoHandlerGmail alloc] init]]; |
+ [sharedInstance convertLegacyOptions]; |
+ }); |
+ return sharedInstance; |
+} |
+ |
++ (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; |
+} |
+ |
+- (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)addMailtoApp:(MailtoHandler*)handlerApp { |
+ if ([handlerApp isAvailable]) |
+ [_handlers setObject:handlerApp forKey:[handlerApp appStoreID]]; |
+} |
+ |
+- (void)convertLegacyOptions { |
+ NSString* const kGmailAppStoreID = @"422689480"; |
+ MailtoHandler* gmailHandler = _handlers[kGmailAppStoreID]; |
+ if (!gmailHandler) |
+ return; |
+ NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
+ |
+ // If Gmail app is not installed or otherwise unavailable, do not make it the |
+ // default mailto: handler app. |
+ if (![gmailHandler isAvailable]) |
+ [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
+ |
+ NSString* const kLegacyShouldAutoOpenKey = @"ShouldAutoOpenLinks_422689480"; |
+ 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. |
+ if (!legacyValue) { |
+ [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey]; |
+ } else { |
+ switch ([legacyValue intValue]) { |
+ case 0: |
+ case 2: |
+ [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey]; |
+ break; |
+ case 1: |
+ [defaults removeObjectForKey:kMailtoDefaultHandlerKey]; |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ [defaults removeObjectForKey:kLegacyShouldAutoOpenKey]; |
+ } |
+} |
+ |
+@end |