-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathOSLiveActivitiesExecutor.swift
More file actions
240 lines (200 loc) · 9.89 KB
/
Copy pathOSLiveActivitiesExecutor.swift
File metadata and controls
240 lines (200 loc) · 9.89 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Modified MIT License
Copyright 2024 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 OneSignalCore
import OneSignalOSCore
import OneSignalUser
/**
A request cache keeps track of all the current update token or start token requests. There can only be one request
per OSLiveActivityRequest.key, and each request has either been successfully sent to OneSignal or hasn't. Requests
that have been sent to OneSignal remain in the cache to avoid sending redundant reequests, as the update/start
token updates are called frequently with the same information.
The cache is persisted to disk via the `cacheKey`, and items will remain in the cache until explicitely removed or
it has existed past the `ttl` provided.
WARNING: This cache is **not** thread safe, synchronization required!
*/
class RequestCache {
var items: [String: OSLiveActivityRequest]
private var cacheKey: String
private var ttl: TimeInterval
init(cacheKey: String, ttl: TimeInterval) {
self.cacheKey = cacheKey
self.ttl = ttl
let cached = OneSignalUserDefaults.initShared().getSavedCodeableData(forKey: cacheKey, defaultValue: nil)
// for safe-casting to the protocol, the intermediary cast to AnyObject is necessary
self.items = cached as? [String: AnyObject] as? [String: OSLiveActivityRequest] ?? [String: OSLiveActivityRequest]()
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities initialized token cache \(self): \(items)")
}
func add(_ request: OSLiveActivityRequest) {
self.items.updateValue(request, forKey: request.key)
self.save()
}
func remove(_ request: OSLiveActivityRequest) {
if self.items.contains(where: { $0.1 == request}) {
self.items.removeValue(forKey: request.key)
self.save()
}
}
func markAllUnsuccessful() {
for (_, request) in self.items {
request.requestSuccessful = false
}
self.save()
}
func markSuccessful(_ request: OSLiveActivityRequest) {
if self.items.contains(where: { $0.1 == request}) {
// Save the appropriate cache with the updated request for this request key.
if request.shouldForgetWhenSuccessful {
self.items.removeValue(forKey: request.key)
} else {
request.requestSuccessful = true
}
self.save()
}
}
private func save() {
// before saving, remove any stale requests from the cache.
for (_, request) in self.items where -request.timestamp.timeIntervalSinceNow > ttl {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities remove stale request from token cache \(self): \(request)")
self.items.removeValue(forKey: request.key)
}
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities saving token cache \(self): \(items)")
OneSignalUserDefaults.initShared().saveCodeableData(forKey: self.cacheKey, withValue: self.items)
}
}
class UpdateRequestCache: RequestCache {
// An update token should not last longer than 8 hours, we keep for 24 hours to be safe.
static let OneDayInSeconds = TimeInterval(60 * 60 * 24)
init() {
super.init(cacheKey: OS_LIVE_ACTIVITIES_EXECUTOR_UPDATE_TOKENS_KEY, ttl: UpdateRequestCache.OneDayInSeconds)
}
}
class StartRequestCache: RequestCache {
// A start token will exist for a year in the cache.
static let OneYearInSeconds = TimeInterval(60 * 60 * 24 * 365)
init() {
super.init(cacheKey: OS_LIVE_ACTIVITIES_EXECUTOR_START_TOKENS_KEY, ttl: StartRequestCache.OneYearInSeconds)
}
}
class ReceiveReceiptsRequestCache: RequestCache {
// Keep receive receipts requests for up to 30 days.
static let OneMonthInSeconds = TimeInterval(60 * 60 * 24 * 30)
init() {
super.init(cacheKey: OS_LIVE_ACTIVITIES_EXECUTOR_RECEIVE_RECEIPTS_KEY, ttl: ReceiveReceiptsRequestCache.OneMonthInSeconds)
}
}
class OSLiveActivitiesExecutor: OSPushSubscriptionObserver {
// The currently tracked update and start tokens (key) and their associated request (value). THESE ARE NOT THREAD SAFE
let updateTokens: UpdateRequestCache = UpdateRequestCache()
let startTokens: StartRequestCache = StartRequestCache()
let receiveReceipts: ReceiveReceiptsRequestCache = ReceiveReceiptsRequestCache()
// The live activities request dispatch queue, serial. This synchronizes access to `updateTokens` and `startTokens`.
private var requestDispatch: OSDispatchQueue
private var pollIntervalSeconds = 30
init(requestDispatch: OSDispatchQueue) {
self.requestDispatch = requestDispatch
}
func start() {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities starting executor")
OneSignalUserManagerImpl.sharedInstance.pushSubscriptionImpl.addObserver(self)
// drive a poll in case there are any outstanding requests in the cache.
self.pollPendingRequests()
}
func onPushSubscriptionDidChange(state: OneSignalUser.OSPushSubscriptionChangedState) {
if state.previous.id == state.current.id {
return
}
// when a push subscription id changes, we need to re-send up all update and start tokens with the new ID.
self.requestDispatch.async {
self.caches { _ in
self.startTokens.markAllUnsuccessful()
}
self.pollPendingRequests()
}
}
func append(_ request: OSLiveActivityRequest) {
self.requestDispatch.async {
let cache = self.getCache(request)
let existingRequest = cache.items[request.key]
if existingRequest == nil || request.supersedes(existingRequest!) {
cache.add(request)
self.executeRequest(cache, request: request)
} else {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities superseded request not saved/executed: \(request)")
}
}
}
private func pollPendingRequests() {
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities pollPendingRequests")
self.requestDispatch.asyncAfterTime(deadline: .now() + .seconds(pollIntervalSeconds)) { [weak self] in
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities executing outstanding requests")
// execute any request that hasn't been successfully executed.
self?.caches { cache in
for request in cache.items.values.filter({ !$0.requestSuccessful }) {
self?.executeRequest(cache, request: request)
}
}
}
}
private func caches(_ block: (RequestCache) -> Void) {
block(self.startTokens)
block(self.updateTokens)
block(self.receiveReceipts)
}
private func getCache(_ request: OSLiveActivityRequest) -> RequestCache {
if request is OSLiveActivityUpdateTokenRequest {
return self.updateTokens
} else if request is OSLiveActivityStartTokenRequest {
return self.startTokens
}
return self.receiveReceipts
}
private func executeRequest(_ cache: RequestCache, request: OSLiveActivityRequest) {
if OSPrivacyConsentController.requiresUserPrivacyConsent() {
OneSignalLog.onesignalLog(.LL_WARN, message: "Cannot send live activity request when the user has not granted privacy permission")
self.pollPendingRequests()
return
}
if !request.prepareForExecution() {
return
}
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities executing request: \(request)")
OneSignalCoreImpl.sharedClient().execute(request) { _ in
// NOTE: No longer running under `requestDispatch` DispatchQueue!
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities request succeeded: \(request)")
self.requestDispatch.async {
cache.markSuccessful(request)
}
} onFailure: { error in
// NOTE: No longer running under `requestDispatch` DispatchQueue!
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignal.LiveActivities request failed with error \(error.debugDescription)")
let responseType = OSNetworkingUtils.getResponseStatusType(error.code)
if responseType != .retryable {
self.requestDispatch.async {
// Failed, no retry. Remove the key from the cache entirely so we don't try again.
cache.remove(request)
}
return
}
// retryable failures will stay in the cache, and will retry the next time the app starts
}
}
}