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

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

Issue 2852003002: Adds mailto: URL support to app launching. (Closed)
Patch Set: rebase 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 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 + (NSString*)systemMailApp {
45 // This is the App Store ID for Apple Mail app.
46 // See https://itunes.apple.com/us/app/mail/id1108187098?mt=8
47 return @"1108187098";
48 }
49
50 - (instancetype)init {
51 self = [super init];
52 if (self) {
53 _handlers = [NSMutableDictionary dictionary];
54 }
55 return self;
56 }
57
58 - (instancetype)initWithStandardHandlers {
59 self = [self init];
60 if (self) {
61 [self addMailtoApp:[[MailtoHandlerSystemMail alloc] init]];
62 [self addMailtoApp:[[MailtoHandlerGmail alloc] init]];
63 [self convertLegacyOptions];
64 }
65 return self;
66 }
67
68 - (NSArray<MailtoHandler*>*)defaultHandlers {
69 return [_handlers allValues];
70 }
71
72 - (NSString*)defaultHandlerID {
73 NSString* value = [[NSUserDefaults standardUserDefaults]
74 stringForKey:kMailtoDefaultHandlerKey];
75 return value ? value : [[self class] systemMailApp];
76 }
77
78 - (void)setDefaultHandlerID:(NSString*)appStoreID {
79 DCHECK([appStoreID length]);
80 [[NSUserDefaults standardUserDefaults] setObject:appStoreID
81 forKey:kMailtoDefaultHandlerKey];
82 }
83
84 - (NSString*)rewriteMailtoURL:(const GURL&)gURL {
85 NSString* value = [self defaultHandlerID];
86 if ([value length]) {
87 MailtoHandler* handler = _handlers[value];
88 if (handler) {
89 return [handler rewriteMailtoURL:gURL];
90 }
91 }
92 return nil;
93 }
94
95 #pragma mark - Private
96
97 - (void)addMailtoApp:(MailtoHandler*)handlerApp {
98 if ([handlerApp isAvailable])
99 [_handlers setObject:handlerApp forKey:[handlerApp appStoreID]];
100 }
101
102 - (void)convertLegacyOptions {
jif 2017/05/04 14:08:47 Add a TODO saying that we need to remove this meth
pkl (ping after 24h if needed) 2017/05/04 22:25:59 Done, but in the forward declaration earlier.
103 NSString* const kGmailAppStoreID = @"422689480";
104 MailtoHandler* gmailHandler = _handlers[kGmailAppStoreID];
105 if (!gmailHandler)
106 return;
107 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
108
109 // If Gmail app is not installed or otherwise unavailable, do not make it the
110 // default mailto: handler app.
111 if (![gmailHandler isAvailable])
112 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
113
114 NSString* const kLegacyShouldAutoOpenKey = @"ShouldAutoOpenLinks_422689480";
jif 2017/05/04 14:08:47 Add comment just in case somebody wonders where th
pkl (ping after 24h if needed) 2017/05/04 22:25:59 Done.
115 NSNumber* legacyValue = [defaults objectForKey:kLegacyShouldAutoOpenKey];
116 // User previously did not have a selection made for opening mailto: links
117 // with Gmail, upgrade will set Gmail app to be the default mailto: handler.
118 if (!legacyValue) {
119 [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey];
120 } else {
121 switch ([legacyValue intValue]) {
122 case 0:
jif 2017/05/04 14:08:47 add comment: // kAutoOpenLinksNotSet
pkl (ping after 24h if needed) 2017/05/04 22:25:59 Done.
123 case 2:
jif 2017/05/04 14:08:47 // kAutoOpenLinksYes
pkl (ping after 24h if needed) 2017/05/04 22:25:59 Done.
124 [defaults setObject:kGmailAppStoreID forKey:kMailtoDefaultHandlerKey];
125 break;
126 case 1:
jif 2017/05/04 14:08:47 // kAutoOpenLinksNo
pkl (ping after 24h if needed) 2017/05/04 22:25:59 Done.
127 [defaults removeObjectForKey:kMailtoDefaultHandlerKey];
128 break;
129 default:
130 NOTREACHED();
131 break;
132 }
133 [defaults removeObjectForKey:kLegacyShouldAutoOpenKey];
134 }
135 }
136
137 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698