summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/chrome_url_util.mm
blob: 13477a6b63c083e400d7d5b3e5538e32da4e36c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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_block.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()) &&
         base::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::mac::ScopedBlock<AppIconURLProvider> _appIconURLProvider;
}

+ (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) {
    // TODO(ios): iOS code should default to "Chromium" branding.
    // http://crbug.com/489270
    _appIconURLProvider.reset(^(int width, int height) {
      return [NSString
          stringWithFormat:@"https://%@/Icon-%dx%d-%dx.png",
                           @"ssl.gstatic.com/ios-apps/com.google.chrome.ios",
                           width, height,
                           [[UIScreen mainScreen] scale] == 1.0 ? 1 : 2];
    }, base::scoped_policy::RETAIN);
  }
  return _appIconURLProvider.get()(width, height);
}

- (void)setCallbackSchemeForTesting:(NSString*)scheme {
  _callbackScheme.reset([scheme copy]);
}

- (void)setAppIconURLProviderForTesting:(AppIconURLProvider)block {
  _appIconURLProvider.reset(block, base::scoped_policy::RETAIN);
}

@end