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_local_state.h"
7 #import <CoreLocation/CoreLocation.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "ios/chrome/browser/application_context.h"
14 #import "ios/chrome/browser/geolocation/location_manager.h"
15 #import "ios/chrome/browser/pref_names.h"
17 @interface OmniboxGeolocationLocalState () {
18 base::scoped_nsobject<LocationManager> locationManager_;
21 - (int)intForPath:(const char*)path;
22 - (void)setInt:(int)value forPath:(const char*)path;
23 - (std::string)stringForPath:(const char*)path;
24 - (void)setString:(const std::string&)value forPath:(const char*)path;
28 @implementation OmniboxGeolocationLocalState
30 + (void)registerLocalState:(PrefRegistrySimple*)registry {
31 registry->RegisterIntegerPref(
32 prefs::kOmniboxGeolocationAuthorizationState,
33 geolocation::kAuthorizationStateNotDeterminedWaiting);
34 registry->RegisterStringPref(
35 prefs::kOmniboxGeolocationLastAuthorizationAlertVersion, "");
38 - (instancetype)initWithLocationManager:(LocationManager*)locationManager {
39 DCHECK(locationManager);
42 locationManager_.reset([locationManager retain]);
47 - (instancetype)init {
52 - (geolocation::AuthorizationState)authorizationState {
53 int authorizationState =
54 [self intForPath:prefs::kOmniboxGeolocationAuthorizationState];
55 // Sanitize the stored value: if the value is corrupt, then treat it as
56 // kAuthorizationStateNotDeterminedWaiting.
57 switch (authorizationState) {
58 case geolocation::kAuthorizationStateNotDeterminedWaiting:
59 case geolocation::kAuthorizationStateNotDeterminedSystemPrompt:
60 case geolocation::kAuthorizationStateDenied:
61 case geolocation::kAuthorizationStateAuthorized:
64 authorizationState = geolocation::kAuthorizationStateNotDeterminedWaiting;
68 switch ([locationManager_ authorizationStatus]) {
69 case kCLAuthorizationStatusNotDetermined:
70 // If the user previously authorized or denied geolocation but reset the
71 // system settings, then start over.
72 if (authorizationState == geolocation::kAuthorizationStateAuthorized ||
73 authorizationState == geolocation::kAuthorizationStateDenied) {
75 geolocation::kAuthorizationStateNotDeterminedWaiting;
79 case kCLAuthorizationStatusRestricted:
80 case kCLAuthorizationStatusDenied:
81 authorizationState = geolocation::kAuthorizationStateDenied;
84 case kCLAuthorizationStatusAuthorized:
85 case kCLAuthorizationStatusAuthorizedWhenInUse:
89 return static_cast<geolocation::AuthorizationState>(authorizationState);
92 - (void)setAuthorizationState:
93 (geolocation::AuthorizationState)authorizationState {
94 [self setInt:authorizationState
95 forPath:prefs::kOmniboxGeolocationAuthorizationState];
98 - (std::string)lastAuthorizationAlertVersion {
100 stringForPath:prefs::kOmniboxGeolocationLastAuthorizationAlertVersion];
103 - (void)setLastAuthorizationAlertVersion:(std::string)value {
104 [self setString:value
105 forPath:prefs::kOmniboxGeolocationLastAuthorizationAlertVersion];
108 #pragma mark - Private
110 - (int)intForPath:(const char*)path {
111 return GetApplicationContext()->GetLocalState()->GetInteger(path);
114 - (void)setInt:(int)value forPath:(const char*)path {
115 GetApplicationContext()->GetLocalState()->SetInteger(path, value);
118 - (std::string)stringForPath:(const char*)path {
119 return GetApplicationContext()->GetLocalState()->GetString(path);
122 - (void)setString:(const std::string&)value forPath:(const char*)path {
123 GetApplicationContext()->GetLocalState()->SetString(path, value);