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

Unified Diff: ios/chrome/browser/web/mailto_url_rewriter.mm

Issue 2852003002: Adds mailto: URL support to app launching. (Closed)
Patch Set: clean up convertLegacyOptions logic some more 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 side-by-side diff with in-line comments
Download patch
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..d6dc6a7acceb6174f6ee370d300cbdcabf1b273c
--- /dev/null
+++ b/ios/chrome/browser/web/mailto_url_rewriter.mm
@@ -0,0 +1,184 @@
+// 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]];
+}
+
+// Implements the migration logic for users of previous versions of Google
+// Chrome which supports Google Native App Launcher. The goal is to preserve
+// the previous behavior and support user choice of non-system provided Mail
+// client apps. System-provided Mail client app will be referred to as
+// "System Mail". The migration goals are:
+// - If a user has not made a choice for preferred mailto: handler in the past,
+// the behavior after upgrading to this version should not change.
+// - If a user had disabled Gmail app as the preferred mailto: handler in the
+// past, System Mail should be selected as the default mailto: handler.
+// - If a user installs Gmail app after the installation of Chrome, Gmail app
+// will be chosen as the default mailto: handler, preserving the previous
+// behavior.
+// - If a user installs another 3rd party mail client app, assuming that the
+// 3rd party mail client app is explicitly supported in Chrome, the new 3rd
+// party mail client app can be set as the default mailto: handler through
+// Tools > Settings > Content Settings.
+- (void)convertLegacyOptions {
+ // 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;
+
+ // 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 kGmailAppStoreID = @"422689480";
+ NSString* const kLegacyShouldAutoOpenKey =
+ [NSString stringWithFormat:@"ShouldAutoOpenLinks_%@", kGmailAppStoreID];
+
+ // If Gmail app is not installed, there is nothing to migrate.
+ MailtoHandler* gmailHandler = _handlers[kGmailAppStoreID];
+ if (!gmailHandler) {
+ // There is a stale legacy key around. This may mean that the user was using
+ // Gmail some time in the past but later uninstalled Gmail app. This clears
+ // the stale entry to start user from a clean slate again.
+ [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
+ 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];
+
+ // User previously had a selection made for opening mailto: links with Gmail,
+ // upgrade will set Gmail app to be the default mailto: handler. If user had
+ // opted out to using Gmail app (even when it was installed), migrate user
+ // to use system-provided Mail client.
+ NSNumber* legacyValue = [defaults objectForKey:kLegacyShouldAutoOpenKey];
+ if (legacyValue) {
+ 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];
+ } else {
+ // This is an unlikely (if not impossible) case of user not having the
+ // legacy key set at all. In this case, set the default to use Gmail app
+ // for mailto: handling.
+ [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey];
+ }
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698