summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/geolocation/omnibox_geolocation_config.mm
blob: 6a56ae4f6a9ceb9616710baaaabb1f72642292d1 (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
// Copyright 2013 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/geolocation/omnibox_geolocation_config.h"

#include <set>
#include <string>

#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#include "url/gurl.h"

namespace {

NSString* const kEligibleDomainsKey = @"EligibleDomains";

}  // namespace

@interface OmniboxGeolocationConfig () {
  // Whitelist of domains eligible for Omnibox geolocation.
  std::set<std::string> _eligibleDomains;
}

// Loads |eligibleDomains_| from config file.
- (void)loadEligibleDomains;

@end

@implementation OmniboxGeolocationConfig

+ (OmniboxGeolocationConfig*)sharedInstance {
  static OmniboxGeolocationConfig* instance =
      [[OmniboxGeolocationConfig alloc] init];
  return instance;
}

- (id)init {
  self =
      [super initWithAppId:nil version:nil plist:@"OmniboxGeolocation.plist"];
  if (self) {
    [self loadEligibleDomains];
  }
  return self;
}

- (BOOL)URLHasEligibleDomain:(const GURL&)url {
  // Here we iterate through the eligible domains and check url.DomainIs() for
  // each domain rather than extracting url.host() and checking the domain for
  // membership in |eligibleDomains_|, because GURL::DomainIs() is more robust
  // and contains logic that we want to reuse.
  for (const auto& eligibleDomain : _eligibleDomains) {
    if (url.DomainIs(eligibleDomain.c_str()))
      return YES;
  }
  return NO;
}

#pragma mark - Private

- (void)loadEligibleDomains {
  _eligibleDomains.clear();

  NSDictionary* configData = [self dictionaryFromConfig];
  NSArray* eligibleDomains = [configData objectForKey:kEligibleDomainsKey];
  if (eligibleDomains) {
    DCHECK([eligibleDomains isKindOfClass:[NSArray class]]);

    for (NSString* domain in eligibleDomains) {
      DCHECK([domain isKindOfClass:[NSString class]]);
      if ([domain length]) {
        _eligibleDomains.insert(
            base::SysNSStringToUTF8([domain lowercaseString]));
      }
    }
  }

  DCHECK(!_eligibleDomains.empty());
}

@end