|
| 1 | +// |
| 2 | +// SystemPermissionManager.swift |
| 3 | +// |
| 4 | +// Copyright © 2025 DuckDuckGo. All rights reserved. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +import Combine |
| 20 | +import CoreLocation |
| 21 | + |
| 22 | +/// Represents the authorization state for a system permission |
| 23 | +enum SystemPermissionAuthorizationState { |
| 24 | + /// Permission has not been requested yet |
| 25 | + case notDetermined |
| 26 | + /// Permission has been granted |
| 27 | + case authorized |
| 28 | + /// Permission has been denied by the user |
| 29 | + case denied |
| 30 | + /// Permission is restricted (parental controls, MDM, etc.) |
| 31 | + case restricted |
| 32 | + /// Services are disabled system-wide (e.g., Location Services off in System Settings) |
| 33 | + case systemDisabled |
| 34 | +} |
| 35 | + |
| 36 | +/// Protocol for managing system-level permissions required before website permissions can be granted |
| 37 | +protocol SystemPermissionManagerProtocol: AnyObject { |
| 38 | + |
| 39 | + // MARK: - Geolocation |
| 40 | + |
| 41 | + /// Returns the current geolocation authorization state |
| 42 | + var geolocationAuthorizationState: SystemPermissionAuthorizationState { get } |
| 43 | + |
| 44 | + /// Publisher that emits the current geolocation authorization state whenever it changes |
| 45 | + var geolocationAuthorizationStatePublisher: AnyPublisher<SystemPermissionAuthorizationState, Never> { get } |
| 46 | + |
| 47 | + /// Returns true if geolocation authorization has been granted |
| 48 | + var isGeolocationAuthorized: Bool { get } |
| 49 | + |
| 50 | + /// Returns true if geolocation authorization needs to be requested |
| 51 | + var isGeolocationAuthorizationRequired: Bool { get } |
| 52 | + |
| 53 | + /// Requests geolocation authorization from the system |
| 54 | + /// - Parameter completion: Called with the resulting authorization state after the user responds |
| 55 | + /// - Returns: A cancellable that can be used to cancel the observation |
| 56 | + @discardableResult |
| 57 | + func requestGeolocationAuthorization(completion: @escaping (SystemPermissionAuthorizationState) -> Void) -> AnyCancellable |
| 58 | + |
| 59 | + /// Requests geolocation authorization from the system using async/await |
| 60 | + /// - Returns: The authorization state after the user responds |
| 61 | + func requestGeolocationAuthorization() async -> SystemPermissionAuthorizationState |
| 62 | +} |
| 63 | + |
| 64 | +/// Manages system-level permissions required before website permissions can be granted |
| 65 | +final class SystemPermissionManager: SystemPermissionManagerProtocol { |
| 66 | + |
| 67 | + private let geolocationService: GeolocationServiceProtocol |
| 68 | + |
| 69 | + init(geolocationService: GeolocationServiceProtocol = GeolocationService.shared) { |
| 70 | + self.geolocationService = geolocationService |
| 71 | + } |
| 72 | + |
| 73 | + // MARK: - Geolocation Authorization |
| 74 | + |
| 75 | + /// Returns the current geolocation authorization state |
| 76 | + var geolocationAuthorizationState: SystemPermissionAuthorizationState { |
| 77 | + guard geolocationService.locationServicesEnabled() else { |
| 78 | + return .systemDisabled |
| 79 | + } |
| 80 | + |
| 81 | + switch geolocationService.authorizationStatus { |
| 82 | + case .notDetermined: |
| 83 | + return .notDetermined |
| 84 | + case .authorized, .authorizedAlways: |
| 85 | + return .authorized |
| 86 | + case .denied: |
| 87 | + return .denied |
| 88 | + case .restricted: |
| 89 | + return .restricted |
| 90 | + @unknown default: |
| 91 | + return .notDetermined |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// Publisher that emits the current geolocation authorization state whenever it changes |
| 96 | + var geolocationAuthorizationStatePublisher: AnyPublisher<SystemPermissionAuthorizationState, Never> { |
| 97 | + geolocationService.authorizationStatusPublisher |
| 98 | + .map { [weak self] _ in |
| 99 | + self?.geolocationAuthorizationState ?? .notDetermined |
| 100 | + } |
| 101 | + .eraseToAnyPublisher() |
| 102 | + } |
| 103 | + |
| 104 | + /// Returns true if geolocation authorization has been granted |
| 105 | + var isGeolocationAuthorized: Bool { |
| 106 | + geolocationAuthorizationState == .authorized |
| 107 | + } |
| 108 | + |
| 109 | + /// Returns true if geolocation authorization needs to be requested |
| 110 | + var isGeolocationAuthorizationRequired: Bool { |
| 111 | + switch geolocationAuthorizationState { |
| 112 | + case .notDetermined, .systemDisabled: |
| 113 | + return true |
| 114 | + case .authorized, .denied, .restricted: |
| 115 | + return false |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// Requests geolocation authorization from the system |
| 120 | + /// - Parameter completion: Called with the resulting authorization state after the user responds |
| 121 | + /// - Returns: A cancellable that can be used to cancel the observation |
| 122 | + @discardableResult |
| 123 | + func requestGeolocationAuthorization(completion: @escaping (SystemPermissionAuthorizationState) -> Void) -> AnyCancellable { |
| 124 | + // If already determined, return current state immediately |
| 125 | + guard geolocationAuthorizationState == .notDetermined else { |
| 126 | + completion(geolocationAuthorizationState) |
| 127 | + return AnyCancellable {} |
| 128 | + } |
| 129 | + |
| 130 | + var locationCancellable: AnyCancellable? |
| 131 | + |
| 132 | + // Subscribe to authorization status publisher to observe changes |
| 133 | + let authorizationCancellable = geolocationService.authorizationStatusPublisher |
| 134 | + .dropFirst() // Skip initial value, we want to observe changes |
| 135 | + .first() // Only need the first change |
| 136 | + .sink { [weak self] _ in |
| 137 | + let state = self?.geolocationAuthorizationState ?? .notDetermined |
| 138 | + // Cancel location subscription once we have the authorization result |
| 139 | + locationCancellable?.cancel() |
| 140 | + completion(state) |
| 141 | + } |
| 142 | + |
| 143 | + // Subscribe to location publisher to trigger authorization request |
| 144 | + // The GeolocationService calls requestWhenInUseAuthorization() when first subscribed |
| 145 | + // We keep this subscription alive until authorization is determined |
| 146 | + locationCancellable = geolocationService.locationPublisher |
| 147 | + .sink { _ in } |
| 148 | + |
| 149 | + return AnyCancellable { |
| 150 | + authorizationCancellable.cancel() |
| 151 | + locationCancellable?.cancel() |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// Requests geolocation authorization from the system using async/await |
| 156 | + /// - Returns: The authorization state after the user responds |
| 157 | + func requestGeolocationAuthorization() async -> SystemPermissionAuthorizationState { |
| 158 | + // If already determined, return current state immediately |
| 159 | + guard geolocationAuthorizationState == .notDetermined else { |
| 160 | + return geolocationAuthorizationState |
| 161 | + } |
| 162 | + |
| 163 | + return await withCheckedContinuation { continuation in |
| 164 | + var cancellable: AnyCancellable? |
| 165 | + cancellable = requestGeolocationAuthorization { state in |
| 166 | + continuation.resume(returning: state) |
| 167 | + cancellable?.cancel() |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments