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

Side by Side Diff: ios/chrome/browser/web/mailto_url_rewriter.mm

Issue 2852003002: Adds mailto: URL support to app launching. (Closed)
Patch Set: fixed lack of persistency of setDefaultHandlerID: 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
OLDNEW
(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 for testing to clear the default state.
32 + (void)resetDefaultHandlerID;
33
34 // Private method to add |handlerApp| to the list of know Mail client apps.
35 - (void)addMailtoApp:(MailtoHandler*)handlerApp;
36
37 // Custom logic to handle the migration from Google Native App Launcher options
38 // to this simplified mailto: URL only system. This must be called after
39 // -addMailtoApp: has been called to add all the known Mail client apps.
40 // TODO(crbug.com/718601): At some point in the future when almost all users
41 // have upgraded, this method can be removed.
42 - (void)convertLegacyOptions;
43
44 @end
45
46 @implementation MailtoURLRewriter
47 @synthesize handlers = _handlers;
48
49 + (NSString*)systemMailApp {
50 // This is the App Store ID for Apple Mail app.
51 // See https://itunes.apple.com/us/app/mail/id1108187098?mt=8
52 return @"1108187098";
53 }
54
55 - (instancetype)init {
56 self = [super init];
57 if (self) {
58 _handlers = [NSMutableDictionary dictionary];
59 }
60 return self;
61 }
62
63 - (instancetype)initWithStandardHandlers {
64 self = [self init];
65 if (self) {
66 [self addMailtoApp:[[MailtoHandlerSystemMail alloc] init]];
67 [self addMailtoApp:[[MailtoHandlerGmail alloc] init]];
68 [self convertLegacyOptions];
69 }
70 return self;
71 }
72
73 - (NSArray<MailtoHandler*>*)defaultHandlers {
74 return [_handlers allValues];
75 }
76
77 - (NSString*)defaultHandlerID {
78 NSString* value = [[NSUserDefaults standardUserDefaults]
79 stringForKey:kMailtoDefaultHandlerKey];
80 return value ? value : [[self class] systemMailApp];
81 }
82
83 - (void)setDefaultHandlerID:(NSString*)appStoreID {
84 DCHECK([appStoreID length]);
85 [[NSUserDefaults standardUserDefaults] setObject:appStoreID
86 forKey:kMailtoDefaultHandlerKey];
87 }
88
89 - (NSString*)rewriteMailtoURL:(const GURL&)gURL {
90 NSString* value = [self defaultHandlerID];
91 if ([value length]) {
92 MailtoHandler* handler = _handlers[value];
93 if (handler) {
94 return [handler rewriteMailtoURL:gURL];
95 }
96 }
97 return nil;
98 }
99
100 #pragma mark - Private
101
102 + (void)resetDefaultHandlerID {
103 [[NSUserDefaults standardUserDefaults]
104 removeObjectForKey:kMailtoDefaultHandlerKey];
105 }
106
107 - (void)addMailtoApp:(MailtoHandler*)handlerApp {
108 if ([handlerApp isAvailable])
109 [_handlers setObject:handlerApp forKey:[handlerApp appStoreID]];
110 }
111
112 - (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
113 // If a default handler for mailto: has already been registered, this is not
114 // an upgrade case that needs to take legacy options into account.
115 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
116 if ([defaults objectForKey:kMailtoDefaultHandlerKey])
117 return;
118
119 NSString* const kGmailAppStoreID = @"422689480";
120 MailtoHandler* gmailHandler = _handlers[kGmailAppStoreID];
121 if (!gmailHandler)
122 return;
123
124 // If Gmail app is not installed or otherwise unavailable, do not make it the
125 // default mailto: handler app.
126 if (![gmailHandler isAvailable])
127 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
128
129 // The key used in NSUserDefaults is from legacy code when Native App
130 // Launcher was still in use. The general format is a string prefix,
131 // underscore, then followed by the App Store ID of the application.
132 NSString* const kLegacyShouldAutoOpenKey =
133 [NSString stringWithFormat:@"ShouldAutoOpenLinks_%@", kGmailAppStoreID];
134 NSNumber* legacyValue = [defaults objectForKey:kLegacyShouldAutoOpenKey];
135 // User previously did not have a selection made for opening mailto: links
136 // 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
137 if (!legacyValue) {
138 [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
139 } else {
140 switch ([legacyValue intValue]) {
141 case 0: // kAutoOpenLinksNotSet
142 case 2: // kAutoOpenLinksYes
143 [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey];
144 break;
145 case 1: // kAutoOpenLinksNo
146 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
147 break;
148 default:
149 NOTREACHED();
150 break;
151 }
152 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
153 }
154 }
155
156 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698