1 // Copyright 2013 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.
5 #import "ios/chrome/browser/geolocation/omnibox_geolocation_config.h"
10 #include "base/logging.h"
11 #include "base/strings/sys_string_conversions.h"
16 NSString* const kEligibleDomainsKey = @"EligibleDomains";
20 @interface OmniboxGeolocationConfig () {
21 // Whitelist of domains eligible for Omnibox geolocation.
22 std::set<std::string> _eligibleDomains;
25 // Loads |eligibleDomains_| from config file.
26 - (void)loadEligibleDomains;
30 @implementation OmniboxGeolocationConfig
32 + (OmniboxGeolocationConfig*)sharedInstance {
33 static OmniboxGeolocationConfig* instance =
34 [[OmniboxGeolocationConfig alloc] init];
40 [super initWithAppId:nil version:nil plist:@"OmniboxGeolocation.plist"];
42 [self loadEligibleDomains];
47 - (BOOL)URLHasEligibleDomain:(const GURL&)url {
48 // Here we iterate through the eligible domains and check url.DomainIs() for
49 // each domain rather than extracting url.host() and checking the domain for
50 // membership in |eligibleDomains_|, because GURL::DomainIs() is more robust
51 // and contains logic that we want to reuse.
52 for (const auto& eligibleDomain : _eligibleDomains) {
53 if (url.DomainIs(eligibleDomain.c_str()))
59 #pragma mark - Private
61 - (void)loadEligibleDomains {
62 _eligibleDomains.clear();
64 NSDictionary* configData = [self dictionaryFromConfig];
65 NSArray* eligibleDomains = [configData objectForKey:kEligibleDomainsKey];
66 if (eligibleDomains) {
67 DCHECK([eligibleDomains isKindOfClass:[NSArray class]]);
69 for (NSString* domain in eligibleDomains) {
70 DCHECK([domain isKindOfClass:[NSString class]]);
71 if ([domain length]) {
72 _eligibleDomains.insert(
73 base::SysNSStringToUTF8([domain lowercaseString]));
78 DCHECK(!_eligibleDomains.empty());