-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOSResilientStorage.swift
More file actions
164 lines (139 loc) · 6.82 KB
/
Copy pathOSResilientStorage.swift
File metadata and controls
164 lines (139 loc) · 6.82 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Modified MIT License
Copyright 2026 OneSignal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import Foundation
import OneSignalCore
/// File-backed mirror of OneSignal SDK identifiers, written with `NSFileProtectionNone`
/// so it's readable before first unlock, when shared `UserDefaults` reads silently return nil.
///
/// Stored in the App Group container, so it's shared across any targets (main app, NSE, etc.)
/// configured with the same App Group entitlement. Opaque identifiers only: no PII or credentials.
@objc(OSResilientStorage)
public final class OSResilientStorage: NSObject {
// MARK: - Public key constants
@objc public static let keyAppId = "app_id"
@objc public static let keySubscriptionId = "subscription_id"
/// Needed because the NSE reads this flag from shared UserDefaults while the device may be locked
/// and the read silently returns the default (NO). Stored as "1" / "0".
@objc public static let keyReceiveReceiptsEnabled = "receive_receipts_enabled"
/// Set to `"1"` once `OneSignalUserManagerImpl.start()` has completed on this device at least once.
/// Used by the main app's protected-data seed to distinguish "fresh install" from
/// "prior session exists but UserDefaults isn't readable yet (iOS prewarm before first
/// unlock)". Cleared on app-id change so a new app's first launch behaves like a fresh install.
@objc public static let keyDidStart = "did_start"
// MARK: - Internal
private static let fileName = "onesignal_identity.json"
/// Serial queue used to serialize all file reads/writes.
private static let queue = DispatchQueue(label: "com.onesignal.resilient-storage")
/// Resolve a writable container URL. App Group container is preferred so
/// the NSE can read the same file. Falls back to the app's private
/// Application Support directory when no App Group is entitled.
private static func fileURL() -> URL? {
let fm = FileManager.default
let groupName = OneSignalUserDefaults.appGroupName()
if let container = fm.containerURL(forSecurityApplicationGroupIdentifier: groupName) {
return container.appendingPathComponent(fileName)
}
do {
let support = try fm.url(
for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
return support.appendingPathComponent(fileName)
} catch {
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSResilientStorage could not resolve a container URL: \(error)")
return nil
}
}
/// Reads the cache file. Caller is responsible for queue-serialization.
/// Returns an empty dict if the file is missing or unreadable.
private static func loadUnsafe() -> [String: String] {
guard let url = fileURL() else { return [:] }
guard FileManager.default.fileExists(atPath: url.path) else { return [:] }
do {
let data = try Data(contentsOf: url)
if data.isEmpty { return [:] }
let object = try JSONSerialization.jsonObject(with: data, options: [])
return (object as? [String: String]) ?? [:]
} catch {
OneSignalLog.onesignalLog(.LL_WARN, message: "OSResilientStorage could not read file: \(error)")
return [:]
}
}
/// Writes the cache file atomically with `.none` file protection.
/// Caller is responsible for queue-serialization.
private static func writeUnsafe(_ contents: [String: String]) {
guard let url = fileURL() else { return }
do {
let data = try JSONSerialization.data(withJSONObject: contents, options: [])
try data.write(to: url, options: [.atomic, .noFileProtection])
// Explicitly re-apply protection class. The atomic write performs a rename which
// has been observed to reset attributes on some iOS versions.
try FileManager.default.setAttributes(
[.protectionKey: FileProtectionType.none],
ofItemAtPath: url.path
)
} catch {
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSResilientStorage write failed: \(error)")
}
}
// MARK: - Public API
/// Returns the full current contents of the cache. Empty dict if absent.
@objc public static func snapshot() -> [String: String] {
return queue.sync { loadUnsafe() }
}
/// Reads a single value. Returns nil when missing or unreadable.
@objc public static func string(forKey key: String) -> String? {
let dict = snapshot()
guard let value = dict[key], !value.isEmpty else { return nil }
return value
}
/// Atomically updates a single value. Passing nil or an empty string removes the key.
@objc public static func setString(_ value: String?, forKey key: String) {
queue.async {
var current = loadUnsafe()
if let value = value, !value.isEmpty {
current[key] = value
} else {
current.removeValue(forKey: key)
}
writeUnsafe(current)
}
}
/// Atomically updates multiple values, preserving keys not in `values`.
/// An empty-string value removes the corresponding key.
@objc public static func setStrings(_ values: [String: String]) {
guard !values.isEmpty else { return }
queue.async {
var current = loadUnsafe()
for (key, value) in values {
if value.isEmpty {
current.removeValue(forKey: key)
} else {
current[key] = value
}
}
writeUnsafe(current)
}
}
}