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/check_op.h"
10#include "base/mac/foundation_util.h"
11#include "base/strings/string_util.h"
12#include "base/strings/sys_string_conversions.h"
13#include "ios/chrome/browser/chrome_url_constants.h"
14#include "ios/components/webui/web_ui_url_constants.h"
15#import "ios/net/url_scheme_util.h"
16#include "url/gurl.h"
17#include "url/url_constants.h"
18
19#if !defined(__has_feature) || !__has_feature(objc_arc)
20#error "This file requires ARC support."
21#endif
22
23bool UrlIsExternalFileReference(const GURL& url) {
24  return url.SchemeIs(kChromeUIScheme) &&
25         base::LowerCaseEqualsASCII(url.host(), kChromeUIExternalFileHost);
26}
27
28bool UrlHasChromeScheme(const GURL& url) {
29  return url.SchemeIs(kChromeUIScheme);
30}
31
32bool UrlHasChromeScheme(NSURL* url) {
33  return net::UrlSchemeIs(url, base::SysUTF8ToNSString(kChromeUIScheme));
34}
35
36bool IsURLNtp(const GURL& url) {
37  return UrlHasChromeScheme(url) && url.host() == kChromeUINewTabHost;
38}
39
40bool IsHandledProtocol(const std::string& scheme) {
41  DCHECK_EQ(scheme, base::ToLowerASCII(scheme));
42  return (scheme == url::kHttpScheme || scheme == url::kHttpsScheme ||
43          scheme == url::kAboutScheme || scheme == url::kDataScheme ||
44          scheme == kChromeUIScheme);
45}
46
47@implementation ChromeAppConstants {
48  NSString* _callbackScheme;
49  NSArray* _schemes;
50}
51
52+ (ChromeAppConstants*)sharedInstance {
53  static ChromeAppConstants* g_instance = [[ChromeAppConstants alloc] init];
54  return g_instance;
55}
56
57- (NSString*)bundleURLScheme {
58  if (!_callbackScheme) {
59    NSSet* allowableSchemes =
60        [NSSet setWithObjects:@"googlechrome", @"chromium",
61                              @"ios-chrome-unittests.http", nil];
62    NSArray* schemes = [self allBundleURLSchemes];
63    for (NSString* scheme in schemes) {
64      if ([allowableSchemes containsObject:scheme])
65        _callbackScheme = [scheme copy];
66    }
67  }
68  DCHECK([_callbackScheme length]);
69  return _callbackScheme;
70}
71
72- (NSArray*)allBundleURLSchemes {
73  if (!_schemes) {
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      _schemes = [base::mac::ObjCCastStrict<NSArray>(
79          urlType[@"CFBundleURLSchemes"]) copy];
80    }
81  }
82  return _schemes;
83}
84
85- (void)setCallbackSchemeForTesting:(NSString*)scheme {
86  _callbackScheme = [scheme copy];
87}
88
89@end
90