summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRustRuntime/MullvadAddressCacheKeychainStore.swift
blob: 4835165af29b455c4840327878d4b5fbbb72ef9f (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
//
//  MullvadAddressCacheKeychainStore.swift
//  MullvadVPN
//
//  Created by Andrew Bulhak on 2025-11-20.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings

/// Whether the settings store is available. It requires `ApplicationSecurityGroupIdentifier`
/// to be present in the main bundle's Info.plist, which is not the case in e.g. UI test runners.
private let isSettingsStoreAvailable: Bool =
    Bundle.main
    .object(forInfoDictionaryKey: "ApplicationSecurityGroupIdentifier") != nil

/// Store the address cache, given to us by the Rust code,  to the keychain
@_cdecl("swift_store_address_cache")
func storeAddressCache(_ pointer: UnsafeRawPointer, dataSize: UInt64) {
    guard isSettingsStoreAvailable else { return }
    let data = Data(bytes: pointer, count: Int(dataSize))
    // if writing to the Keychain fails, it will do so silently.
    try? SettingsManager.writeAddressCache(data)
}

@_cdecl("swift_read_address_cache")
func readAddressCache() -> SwiftData {
    guard isSettingsStoreAvailable else { return SwiftData(data: NSData()) }
    let data = (try? SettingsManager.readAddressCache()) ?? Data()
    return SwiftData(data: data as NSData)
}