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