OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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/chrome_url_util.h" |
| 6 |
| 7 #import <UIKit/UIKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/mac/scoped_block.h" |
| 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "ios/chrome/browser/chrome_url_constants.h" |
| 15 #import "ios/net/url_scheme_util.h" |
| 16 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 bool UrlIsExternalFileReference(const GURL& url) { |
| 20 return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme()) && |
| 21 LowerCaseEqualsASCII(url.host(), kChromeUIExternalFileHost); |
| 22 } |
| 23 |
| 24 NSURL* UrlToLaunchChrome() { |
| 25 // Determines the target URL scheme that will launch Chrome. |
| 26 NSString* scheme = [[ChromeAppConstants sharedInstance] getBundleURLScheme]; |
| 27 return [NSURL URLWithString:[NSString stringWithFormat:@"%@://", scheme]]; |
| 28 } |
| 29 |
| 30 NSURL* UrlOfChromeAppIcon(int width, int height) { |
| 31 NSString* url = |
| 32 [[ChromeAppConstants sharedInstance] getChromeAppIconURLOfWidth:width |
| 33 height:height]; |
| 34 return [NSURL URLWithString:url]; |
| 35 } |
| 36 |
| 37 bool UrlHasChromeScheme(const GURL& url) { |
| 38 return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme()); |
| 39 } |
| 40 |
| 41 bool UrlHasChromeScheme(NSURL* url) { |
| 42 return net::UrlSchemeIs( |
| 43 url, base::SysUTF8ToNSString( |
| 44 ios::GetChromeBrowserProvider()->GetChromeUIScheme())); |
| 45 } |
| 46 |
| 47 @implementation ChromeAppConstants { |
| 48 base::scoped_nsobject<NSString> _callbackScheme; |
| 49 base::mac::ScopedBlock<AppIconURLProvider> _appIconURLProvider; |
| 50 } |
| 51 |
| 52 + (ChromeAppConstants*)sharedInstance { |
| 53 static ChromeAppConstants* g_instance = [[ChromeAppConstants alloc] init]; |
| 54 return g_instance; |
| 55 } |
| 56 |
| 57 - (NSString*)getBundleURLScheme { |
| 58 if (!_callbackScheme) { |
| 59 NSSet* allowableSchemes = |
| 60 [NSSet setWithObjects:@"googlechrome", @"chromium", nil]; |
| 61 NSDictionary* info = [[NSBundle mainBundle] infoDictionary]; |
| 62 NSArray* urlTypes = [info objectForKey:@"CFBundleURLTypes"]; |
| 63 for (NSDictionary* urlType in urlTypes) { |
| 64 DCHECK([urlType isKindOfClass:[NSDictionary class]]); |
| 65 NSArray* schemes = [urlType objectForKey:@"CFBundleURLSchemes"]; |
| 66 if (!schemes) |
| 67 continue; |
| 68 DCHECK([schemes isKindOfClass:[NSArray class]]); |
| 69 for (NSString* scheme in schemes) { |
| 70 if ([allowableSchemes containsObject:scheme]) |
| 71 _callbackScheme.reset([scheme copy]); |
| 72 } |
| 73 } |
| 74 } |
| 75 DCHECK([_callbackScheme length]); |
| 76 return _callbackScheme; |
| 77 } |
| 78 |
| 79 - (NSString*)getChromeAppIconURLOfWidth:(int)width height:(int)height { |
| 80 if (!_appIconURLProvider) { |
| 81 // TODO(ios): iOS code should default to "Chromium" branding. |
| 82 // http://crbug.com/489270 |
| 83 _appIconURLProvider.reset(^(int width, int height) { |
| 84 return [NSString |
| 85 stringWithFormat:@"https://%@/Icon-%dx%d-%dx.png", |
| 86 @"ssl.gstatic.com/ios-apps/com.google.chrome.ios", |
| 87 width, height, |
| 88 [[UIScreen mainScreen] scale] == 1.0 ? 1 : 2]; |
| 89 }, base::scoped_policy::RETAIN); |
| 90 } |
| 91 return _appIconURLProvider.get()(width, height); |
| 92 } |
| 93 |
| 94 - (void)setCallbackSchemeForTesting:(NSString*)scheme { |
| 95 _callbackScheme.reset([scheme copy]); |
| 96 } |
| 97 |
| 98 - (void)setAppIconURLProviderForTesting:(AppIconURLProvider)block { |
| 99 _appIconURLProvider.reset(block, base::scoped_policy::RETAIN); |
| 100 } |
| 101 |
| 102 @end |
OLD | NEW |