Chromium Code Reviews| Index: ios/chrome/browser/chrome_url_util.mm |
| diff --git a/ios/chrome/browser/chrome_url_util.mm b/ios/chrome/browser/chrome_url_util.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0c1a92b761c14fc9b96a3e8fb95bdad45d644b38 |
| --- /dev/null |
| +++ b/ios/chrome/browser/chrome_url_util.mm |
| @@ -0,0 +1,99 @@ |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/chrome_url_util.h" |
| + |
| +#import <UIKit/UIKit.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/mac/scoped_nsobject.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "ios/chrome/browser/chrome_url_constants.h" |
| +#import "ios/net/url_scheme_util.h" |
| +#include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| +#include "url/gurl.h" |
| + |
| +bool UrlIsExternalFileReference(const GURL& url) { |
| + return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme()) && |
| + LowerCaseEqualsASCII(url.host(), kChromeUIExternalFileHost); |
| +} |
| + |
| +NSURL* UrlToLaunchChrome() { |
| + // Determines the target URL scheme that will launch Chrome. |
| + NSString* scheme = [[ChromeAppConstants sharedInstance] getBundleURLScheme]; |
| + return [NSURL URLWithString:[NSString stringWithFormat:@"%@://", scheme]]; |
| +} |
| + |
| +NSURL* UrlOfChromeAppIcon(int width, int height) { |
| + NSString* url = |
| + [[ChromeAppConstants sharedInstance] getChromeAppIconURLOfWidth:width |
| + height:height]; |
| + return [NSURL URLWithString:url]; |
| +} |
| + |
| +bool UrlHasChromeScheme(const GURL& url) { |
| + return url.SchemeIs(ios::GetChromeBrowserProvider()->GetChromeUIScheme()); |
| +} |
| + |
| +bool UrlHasChromeScheme(NSURL* url) { |
| + return net::UrlSchemeIs( |
| + url, base::SysUTF8ToNSString( |
| + ios::GetChromeBrowserProvider()->GetChromeUIScheme())); |
| +} |
| + |
| +@implementation ChromeAppConstants { |
| + base::scoped_nsobject<NSString> callbackScheme_; |
| + base::scoped_nsprotocol<AppIconURLProvider> appIconURLProvider_; |
|
rohitrao (ping after 24h)
2015/05/18 13:34:56
Want to switch to leading underscores for these va
sdefresne
2015/05/18 14:19:54
Done and changed the block to use base::mac::Scope
|
| +} |
| + |
| ++ (ChromeAppConstants*)sharedInstance { |
| + static ChromeAppConstants* g_instance = [[ChromeAppConstants alloc] init]; |
| + return g_instance; |
| +} |
| + |
| +- (NSString*)getBundleURLScheme { |
| + if (!callbackScheme_) { |
| + NSSet* allowableSchemes = |
| + [NSSet setWithObjects:@"googlechrome", @"chromium", nil]; |
| + NSDictionary* info = [[NSBundle mainBundle] infoDictionary]; |
| + NSArray* urlTypes = [info objectForKey:@"CFBundleURLTypes"]; |
| + for (NSDictionary* urlType in urlTypes) { |
| + DCHECK([urlType isKindOfClass:[NSDictionary class]]); |
| + NSArray* schemes = [urlType objectForKey:@"CFBundleURLSchemes"]; |
| + if (!schemes) |
| + continue; |
| + DCHECK([schemes isKindOfClass:[NSArray class]]); |
| + for (NSString* scheme in schemes) { |
| + if ([allowableSchemes containsObject:scheme]) |
| + callbackScheme_.reset([scheme copy]); |
| + } |
| + } |
| + } |
| + DCHECK([callbackScheme_ length]); |
| + return callbackScheme_; |
| +} |
| + |
| +- (NSString*)getChromeAppIconURLOfWidth:(int)width height:(int)height { |
| + if (!appIconURLProvider_) { |
| + appIconURLProvider_.reset([^(int width, int height) { |
| + return [NSString |
| + stringWithFormat:@"https://%@/Icon-%dx%d-%dx.png", |
| + @"ssl.gstatic.com/ios-apps/com.google.chrome.ios", |
|
rohitrao (ping after 24h)
2015/05/18 13:34:56
We should consider debranding this.
sdefresne
2015/05/18 14:19:54
Added a TODO.
|
| + width, height, |
| + [[UIScreen mainScreen] scale] == 1.0 ? 1 : 2]; |
| + } copy]); |
| + } |
| + return appIconURLProvider_.get()(width, height); |
| +} |
| + |
| +- (void)setCallbackSchemeForTesting:(NSString*)scheme { |
| + callbackScheme_.reset([scheme copy]); |
| +} |
| + |
| +- (void)setAppIconURLProviderForTesting:(AppIconURLProvider)block { |
| + appIconURLProvider_.reset([block copy]); |
| +} |
| + |
| +@end |