OLD | NEW |
(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_handler.h" |
| 6 |
| 7 #import "base/strings/sys_string_conversions.h" |
| 8 #include "url/gurl.h" |
| 9 #include "url/url_constants.h" |
| 10 |
| 11 #import <UIKit/UIKit.h> |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 @implementation MailtoHandler |
| 18 @synthesize appName = _appName; |
| 19 @synthesize appStoreID = _appStoreID; |
| 20 |
| 21 - (instancetype)initWithName:(NSString*)appName |
| 22 appStoreID:(NSString*)appStoreID { |
| 23 self = [super init]; |
| 24 if (self) { |
| 25 _appName = [appName copy]; |
| 26 _appStoreID = [appStoreID copy]; |
| 27 } |
| 28 return self; |
| 29 } |
| 30 |
| 31 - (BOOL)isAvailable { |
| 32 NSURL* baseURL = [NSURL URLWithString:[self beginningScheme]]; |
| 33 NSURL* testURL = [NSURL |
| 34 URLWithString:[NSString stringWithFormat:@"%@://", [baseURL scheme]]]; |
| 35 return [[UIApplication sharedApplication] canOpenURL:testURL]; |
| 36 } |
| 37 |
| 38 - (NSString*)beginningScheme { |
| 39 // Subclasses should override this method. |
| 40 return @"mailtohandler:/co?"; |
| 41 } |
| 42 |
| 43 - (NSSet<NSString*>*)supportedHeaders { |
| 44 return [NSSet<NSString*> |
| 45 setWithObjects:@"to", @"cc", @"bcc", @"subject", @"body", nil]; |
| 46 } |
| 47 |
| 48 - (NSString*)rewriteMailtoURL:(const GURL&)gURL { |
| 49 if (!gURL.SchemeIs(url::kMailToScheme)) |
| 50 return nil; |
| 51 NSMutableArray* outParams = [NSMutableArray array]; |
| 52 NSString* recipient = base::SysUTF8ToNSString(gURL.path()); |
| 53 if ([recipient length]) { |
| 54 [outParams addObject:[NSString stringWithFormat:@"to=%@", recipient]]; |
| 55 } |
| 56 NSString* query = base::SysUTF8ToNSString(gURL.query()); |
| 57 for (NSString* keyvalue : [query componentsSeparatedByString:@"&"]) { |
| 58 NSArray* pair = [keyvalue componentsSeparatedByString:@"="]; |
| 59 if ([pair count] != 2U || ![[self supportedHeaders] containsObject:pair[0]]) |
| 60 continue; |
| 61 [outParams |
| 62 addObject:[NSString stringWithFormat:@"%@=%@", pair[0], pair[1]]]; |
| 63 } |
| 64 return [NSString stringWithFormat:@"%@%@", [self beginningScheme], |
| 65 [outParams componentsJoinedByString:@"&"]]; |
| 66 } |
| 67 |
| 68 @end |
OLD | NEW |