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.
5 #import "ios/chrome/browser/chrome_url_util.h"
7 #import <UIKit/UIKit.h>
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"
19 bool UrlIsExternalFileReference(const GURL& url) {
20 return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme()) &&
21 base::LowerCaseEqualsASCII(url.host(), kChromeUIExternalFileHost);
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]];
30 NSURL* UrlOfChromeAppIcon(int width, int height) {
32 [[ChromeAppConstants sharedInstance] getChromeAppIconURLOfWidth:width
34 return [NSURL URLWithString:url];
37 bool UrlHasChromeScheme(const GURL& url) {
38 return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme());
41 bool UrlHasChromeScheme(NSURL* url) {
42 return net::UrlSchemeIs(
43 url, base::SysUTF8ToNSString(
44 ios::GetChromeBrowserProvider()->GetChromeUIScheme()));
47 @implementation ChromeAppConstants {
48 base::scoped_nsobject<NSString> _callbackScheme;
49 base::mac::ScopedBlock<AppIconURLProvider> _appIconURLProvider;
52 + (ChromeAppConstants*)sharedInstance {
53 static ChromeAppConstants* g_instance = [[ChromeAppConstants alloc] init];
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"];
68 DCHECK([schemes isKindOfClass:[NSArray class]]);
69 for (NSString* scheme in schemes) {
70 if ([allowableSchemes containsObject:scheme])
71 _callbackScheme.reset([scheme copy]);
75 DCHECK([_callbackScheme length]);
76 return _callbackScheme;
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) {
85 stringWithFormat:@"https://%@/Icon-%dx%d-%dx.png",
86 @"ssl.gstatic.com/ios-apps/com.google.chrome.ios",
88 [[UIScreen mainScreen] scale] == 1.0 ? 1 : 2];
89 }, base::scoped_policy::RETAIN);
91 return _appIconURLProvider.get()(width, height);
94 - (void)setCallbackSchemeForTesting:(NSString*)scheme {
95 _callbackScheme.reset([scheme copy]);
98 - (void)setAppIconURLProviderForTesting:(AppIconURLProvider)block {
99 _appIconURLProvider.reset(block, base::scoped_policy::RETAIN);