Chromium Code Reviews| Index: ios/chrome/browser/web/mailto_handler.mm |
| diff --git a/ios/chrome/browser/web/mailto_handler.mm b/ios/chrome/browser/web/mailto_handler.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b998d7cc9fbc362fd79bb61a9a1949b94e99d139 |
| --- /dev/null |
| +++ b/ios/chrome/browser/web/mailto_handler.mm |
| @@ -0,0 +1,72 @@ |
| +// 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_handler.h" |
| + |
| +#import "base/strings/sys_string_conversions.h" |
| +#include "url/gurl.h" |
| +#include "url/url_constants.h" |
| + |
| +#import <UIKit/UIKit.h> |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +@implementation MailtoHandler |
| +@synthesize appName = _appName; |
| +@synthesize appStoreID = _appStoreID; |
| + |
| +- (instancetype)initWithName:(NSString*)appName |
| + appStoreID:(NSString*)appStoreID { |
| + self = [super init]; |
| + if (self) { |
| + _appName = [appName copy]; |
| + _appStoreID = [appStoreID copy]; |
| + } |
| + return self; |
| +} |
| + |
| +- (BOOL)isAvailable { |
| + // This should be implemented by subclasses. |
| + NOTREACHED(); |
| + return NO; |
| +} |
| + |
| +- (NSString*)beginningScheme { |
| + // Subclasses should override this method. |
| + return @"mailtohandler:/co?"; |
| +} |
| + |
| +- (NSSet*)supportedHeaders { |
| + return [NSSet setWithObjects:@"to", @"from", @"cc", @"subject", @"body", nil]; |
| +} |
| + |
| +- (NSString*)rewriteMailtoURL:(const GURL&)gURL { |
| + if (!gURL.SchemeIs(url::kMailToScheme)) |
| + return nil; |
| + NSMutableArray* outParams = [NSMutableArray array]; |
| + NSString* recipient = base::SysUTF8ToNSString(gURL.path()); |
| + if ([recipient length]) { |
| + [outParams addObject:[NSString stringWithFormat:@"to=%@", recipient]]; |
| + } |
| + NSString* query = base::SysUTF8ToNSString(gURL.query()); |
| + for (NSString* keyvalue : [query componentsSeparatedByString:@"&"]) { |
| + NSArray* pair = [keyvalue componentsSeparatedByString:@"="]; |
| + if ([pair count] == 0U || [pair count] > 2U) |
|
jif
2017/05/04 14:08:47
Do we really want to handle the case where [pair c
pkl (ping after 24h if needed)
2017/05/04 22:25:59
Good point. Checked some mailto query params docs
|
| + continue; |
| + if (![[self supportedHeaders] containsObject:pair[0]]) |
| + continue; |
| + if ([pair count] == 1) { |
| + [outParams addObject:pair[0]]; |
| + } else if ([pair count] == 2) { |
| + [outParams |
| + addObject:[NSString stringWithFormat:@"%@=%@", pair[0], pair[1]]]; |
| + } |
| + } |
| + return [NSString stringWithFormat:@"%@%@", [self beginningScheme], |
| + [outParams componentsJoinedByString:@"&"]]; |
| +} |
| + |
| +@end |