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"
17 #include "net/url_request/url_request.h"
19 #include "url/url_constants.h"
21 bool UrlIsExternalFileReference(const GURL& url) {
22 return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme()) &&
23 base::LowerCaseEqualsASCII(url.host(), kChromeUIExternalFileHost);
26 NSURL* UrlToLaunchChrome() {
27 // Determines the target URL scheme that will launch Chrome.
28 NSString* scheme = [[ChromeAppConstants sharedInstance] getBundleURLScheme];
29 return [NSURL URLWithString:[NSString stringWithFormat:@"%@://", scheme]];
32 NSURL* UrlOfChromeAppIcon(int width, int height) {
34 [[ChromeAppConstants sharedInstance] getChromeAppIconURLOfWidth:width
36 return [NSURL URLWithString:url];
39 bool UrlHasChromeScheme(const GURL& url) {
40 return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme());
43 bool UrlHasChromeScheme(NSURL* url) {
44 return net::UrlSchemeIs(
45 url, base::SysUTF8ToNSString(
46 ios::GetChromeBrowserProvider()->GetChromeUIScheme()));
49 bool IsHandledProtocol(const std::string& scheme) {
50 DCHECK_EQ(scheme, base::StringToLowerASCII(scheme));
51 if (scheme == url::kAboutScheme)
53 if (scheme == url::kDataScheme)
55 if (scheme == ios::GetChromeBrowserProvider()->GetChromeUIScheme())
57 return net::URLRequest::IsHandledProtocol(scheme);
60 @implementation ChromeAppConstants {
61 base::scoped_nsobject<NSString> _callbackScheme;
62 base::mac::ScopedBlock<AppIconURLProvider> _appIconURLProvider;
65 + (ChromeAppConstants*)sharedInstance {
66 static ChromeAppConstants* g_instance = [[ChromeAppConstants alloc] init];
70 - (NSString*)getBundleURLScheme {
71 if (!_callbackScheme) {
72 NSSet* allowableSchemes =
73 [NSSet setWithObjects:@"googlechrome", @"chromium", nil];
74 NSDictionary* info = [[NSBundle mainBundle] infoDictionary];
75 NSArray* urlTypes = [info objectForKey:@"CFBundleURLTypes"];
76 for (NSDictionary* urlType in urlTypes) {
77 DCHECK([urlType isKindOfClass:[NSDictionary class]]);
78 NSArray* schemes = [urlType objectForKey:@"CFBundleURLSchemes"];
81 DCHECK([schemes isKindOfClass:[NSArray class]]);
82 for (NSString* scheme in schemes) {
83 if ([allowableSchemes containsObject:scheme])
84 _callbackScheme.reset([scheme copy]);
88 DCHECK([_callbackScheme length]);
89 return _callbackScheme;
92 - (NSString*)getChromeAppIconURLOfWidth:(int)width height:(int)height {
93 if (!_appIconURLProvider) {
94 // TODO(ios): iOS code should default to "Chromium" branding.
95 // http://crbug.com/489270
96 _appIconURLProvider.reset(^(int width, int height) {
98 stringWithFormat:@"https://%@/Icon-%dx%d-%dx.png",
99 @"ssl.gstatic.com/ios-apps/com.google.chrome.ios",
101 [[UIScreen mainScreen] scale] == 1.0 ? 1 : 2];
102 }, base::scoped_policy::RETAIN);
104 return _appIconURLProvider.get()(width, height);
107 - (void)setCallbackSchemeForTesting:(NSString*)scheme {
108 _callbackScheme.reset([scheme copy]);
111 - (void)setAppIconURLProviderForTesting:(AppIconURLProvider)block {
112 _appIconURLProvider.reset(block, base::scoped_policy::RETAIN);