summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/geolocation/omnibox_geolocation_local_state.mm
blob: c195f46f55e75b3076259809f08b4069af0f5230 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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_local_state.h"

#import <CoreLocation/CoreLocation.h>

#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ios/chrome/browser/application_context.h"
#import "ios/chrome/browser/geolocation/location_manager.h"
#import "ios/chrome/browser/pref_names.h"

@interface OmniboxGeolocationLocalState () {
  base::scoped_nsobject<LocationManager> locationManager_;
}

- (int)intForPath:(const char*)path;
- (void)setInt:(int)value forPath:(const char*)path;
- (std::string)stringForPath:(const char*)path;
- (void)setString:(const std::string&)value forPath:(const char*)path;

@end

@implementation OmniboxGeolocationLocalState

+ (void)registerLocalState:(PrefRegistrySimple*)registry {
  registry->RegisterIntegerPref(
      prefs::kOmniboxGeolocationAuthorizationState,
      geolocation::kAuthorizationStateNotDeterminedWaiting);
  registry->RegisterStringPref(
      prefs::kOmniboxGeolocationLastAuthorizationAlertVersion, "");
}

- (instancetype)initWithLocationManager:(LocationManager*)locationManager {
  DCHECK(locationManager);
  self = [super init];
  if (self) {
    locationManager_.reset([locationManager retain]);
  }
  return self;
}

- (instancetype)init {
  NOTREACHED();
  return nil;
}

- (geolocation::AuthorizationState)authorizationState {
  int authorizationState =
      [self intForPath:prefs::kOmniboxGeolocationAuthorizationState];
  // Sanitize the stored value: if the value is corrupt, then treat it as
  // kAuthorizationStateNotDeterminedWaiting.
  switch (authorizationState) {
    case geolocation::kAuthorizationStateNotDeterminedWaiting:
    case geolocation::kAuthorizationStateNotDeterminedSystemPrompt:
    case geolocation::kAuthorizationStateDenied:
    case geolocation::kAuthorizationStateAuthorized:
      break;
    default:
      authorizationState = geolocation::kAuthorizationStateNotDeterminedWaiting;
      break;
  }

  switch ([locationManager_ authorizationStatus]) {
    case kCLAuthorizationStatusNotDetermined:
      // If the user previously authorized or denied geolocation but reset the
      // system settings, then start over.
      if (authorizationState == geolocation::kAuthorizationStateAuthorized ||
          authorizationState == geolocation::kAuthorizationStateDenied) {
        authorizationState =
            geolocation::kAuthorizationStateNotDeterminedWaiting;
      }
      break;

    case kCLAuthorizationStatusRestricted:
    case kCLAuthorizationStatusDenied:
      authorizationState = geolocation::kAuthorizationStateDenied;
      break;

    case kCLAuthorizationStatusAuthorizedAlways:
    case kCLAuthorizationStatusAuthorizedWhenInUse:
      break;
  }

  return static_cast<geolocation::AuthorizationState>(authorizationState);
}

- (void)setAuthorizationState:
        (geolocation::AuthorizationState)authorizationState {
  [self setInt:authorizationState
       forPath:prefs::kOmniboxGeolocationAuthorizationState];
}

- (std::string)lastAuthorizationAlertVersion {
  return [self
      stringForPath:prefs::kOmniboxGeolocationLastAuthorizationAlertVersion];
}

- (void)setLastAuthorizationAlertVersion:(std::string)value {
  [self setString:value
          forPath:prefs::kOmniboxGeolocationLastAuthorizationAlertVersion];
}

#pragma mark - Private

- (int)intForPath:(const char*)path {
  return GetApplicationContext()->GetLocalState()->GetInteger(path);
}

- (void)setInt:(int)value forPath:(const char*)path {
  GetApplicationContext()->GetLocalState()->SetInteger(path, value);
}

- (std::string)stringForPath:(const char*)path {
  return GetApplicationContext()->GetLocalState()->GetString(path);
}

- (void)setString:(const std::string&)value forPath:(const char*)path {
  GetApplicationContext()->GetLocalState()->SetString(path, value);
}

@end