@capacitor/background-runner 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorBackgroundRunner.podspec +17 -0
- package/README.md +310 -0
- package/android/build.gradle +67 -0
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/BackgroundRunner.kt +166 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/BackgroundRunnerPlugin.kt +102 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/CoroutineHelper.kt +21 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/RunnerConfig.kt +33 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/RunnerWorker.kt +46 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Device.kt +99 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Geolocation.kt +55 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/KV.kt +29 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/NetworkStatus.kt +10 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Notification.kt +59 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Notifications.kt +158 -0
- package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/TimedNotificationPublisher.kt +40 -0
- package/android/src/main/libs/android-js-engine-release.aar +0 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +208 -0
- package/dist/esm/apis.d.ts +52 -0
- package/dist/esm/apis.js +2 -0
- package/dist/esm/apis.js.map +1 -0
- package/dist/esm/definitions.d.ts +82 -0
- package/dist/esm/definitions.js +3 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +8 -0
- package/dist/esm/web.js +16 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +32 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +35 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/BackgroundRunner.swift +223 -0
- package/ios/Plugin/BackgroundRunnerErrors.swift +8 -0
- package/ios/Plugin/BackgroundRunnerPlugin.h +10 -0
- package/ios/Plugin/BackgroundRunnerPlugin.m +11 -0
- package/ios/Plugin/BackgroundRunnerPlugin.swift +161 -0
- package/ios/Plugin/CapacitorAPI/Device.swift +116 -0
- package/ios/Plugin/CapacitorAPI/Geolocation.swift +190 -0
- package/ios/Plugin/CapacitorAPI/KV.swift +22 -0
- package/ios/Plugin/CapacitorAPI/Notifications.swift +165 -0
- package/ios/Plugin/CapacitorAPI/Reachability.swift +395 -0
- package/ios/Plugin/CapacitorAPI/Wearable.swift +48 -0
- package/ios/Plugin/Context+CapacitorAPI.swift +11 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/RunnerConfig.swift +36 -0
- package/ios/Plugin/RunnerEngine/Context.swift +151 -0
- package/ios/Plugin/RunnerEngine/Errors.swift +8 -0
- package/ios/Plugin/RunnerEngine/JSConsole.swift +38 -0
- package/ios/Plugin/RunnerEngine/JSCrypto.swift +38 -0
- package/ios/Plugin/RunnerEngine/JSError.swift +33 -0
- package/ios/Plugin/RunnerEngine/JSFetch.swift +50 -0
- package/ios/Plugin/RunnerEngine/JSResponse.swift +69 -0
- package/ios/Plugin/RunnerEngine/JSTextDecoder.swift +60 -0
- package/ios/Plugin/RunnerEngine/JSTextEncoder.swift +12 -0
- package/ios/Plugin/RunnerEngine/Runner.swift +23 -0
- package/ios/Plugin/WatchConnectivtySessionDelegate.swift +34 -0
- package/package.json +81 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2014, Ashley Mills
|
|
3
|
+
All rights reserved.
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
7
|
+
list of conditions and the following disclaimer.
|
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
9
|
+
this list of conditions and the following disclaimer in the documentation
|
|
10
|
+
and/or other materials provided with the distribution.
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
12
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
13
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
14
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
15
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
16
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
17
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
18
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
19
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
20
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
21
|
+
POSSIBILITY OF SUCH DAMAGE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import SystemConfiguration
|
|
25
|
+
import Foundation
|
|
26
|
+
|
|
27
|
+
public enum ReachabilityError: Error {
|
|
28
|
+
case failedToCreateWithAddress(sockaddr, Int32)
|
|
29
|
+
case failedToCreateWithHostname(String, Int32)
|
|
30
|
+
case unableToSetCallback(Int32)
|
|
31
|
+
case unableToSetDispatchQueue(Int32)
|
|
32
|
+
case unableToGetFlags(Int32)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@available(*, unavailable, renamed: "Notification.Name.reachabilityChanged")
|
|
36
|
+
public let reachabilityChangedNotification = NSNotification.Name("ReachabilityChangedNotification")
|
|
37
|
+
|
|
38
|
+
public extension Notification.Name {
|
|
39
|
+
static let reachabilityChanged = Notification.Name("reachabilityChanged")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public class Reachability {
|
|
43
|
+
|
|
44
|
+
public typealias NetworkReachable = (Reachability) -> Void
|
|
45
|
+
public typealias NetworkUnreachable = (Reachability) -> Void
|
|
46
|
+
|
|
47
|
+
@available(*, unavailable, renamed: "Connection")
|
|
48
|
+
public enum NetworkStatus: CustomStringConvertible {
|
|
49
|
+
case notReachable, reachableViaWiFi, reachableViaWWAN
|
|
50
|
+
public var description: String {
|
|
51
|
+
switch self {
|
|
52
|
+
case .reachableViaWWAN: return "Cellular"
|
|
53
|
+
case .reachableViaWiFi: return "WiFi"
|
|
54
|
+
case .notReachable: return "No Connection"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public enum Connection: CustomStringConvertible {
|
|
60
|
+
case unavailable, wifi, cellular
|
|
61
|
+
public var description: String {
|
|
62
|
+
switch self {
|
|
63
|
+
case .cellular: return "Cellular"
|
|
64
|
+
case .wifi: return "WiFi"
|
|
65
|
+
case .unavailable: return "No Connection"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@available(*, deprecated, renamed: "unavailable")
|
|
70
|
+
public static let none: Connection = .unavailable
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public var whenReachable: NetworkReachable?
|
|
74
|
+
public var whenUnreachable: NetworkUnreachable?
|
|
75
|
+
|
|
76
|
+
@available(*, deprecated, renamed: "allowsCellularConnection")
|
|
77
|
+
public let reachableOnWWAN: Bool = true
|
|
78
|
+
|
|
79
|
+
/// Set to `false` to force Reachability.connection to .none when on cellular connection (default value `true`)
|
|
80
|
+
public var allowsCellularConnection: Bool
|
|
81
|
+
|
|
82
|
+
// The notification center on which "reachability changed" events are being posted
|
|
83
|
+
public var notificationCenter: NotificationCenter = NotificationCenter.default
|
|
84
|
+
|
|
85
|
+
@available(*, deprecated, renamed: "connection.description")
|
|
86
|
+
public var currentReachabilityString: String {
|
|
87
|
+
return "\(connection)"
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@available(*, unavailable, renamed: "connection")
|
|
91
|
+
public var currentReachabilityStatus: Connection {
|
|
92
|
+
return connection
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public var connection: Connection {
|
|
96
|
+
if flags == nil {
|
|
97
|
+
try? setReachabilityFlags()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
switch flags?.connection {
|
|
101
|
+
case .unavailable?, nil: return .unavailable
|
|
102
|
+
case .cellular?: return allowsCellularConnection ? .cellular : .unavailable
|
|
103
|
+
case .wifi?: return .wifi
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fileprivate var isRunningOnDevice: Bool = {
|
|
108
|
+
#if targetEnvironment(simulator)
|
|
109
|
+
return false
|
|
110
|
+
#else
|
|
111
|
+
return true
|
|
112
|
+
#endif
|
|
113
|
+
}()
|
|
114
|
+
|
|
115
|
+
fileprivate(set) var notifierRunning = false
|
|
116
|
+
fileprivate let reachabilityRef: SCNetworkReachability
|
|
117
|
+
fileprivate let reachabilitySerialQueue: DispatchQueue
|
|
118
|
+
fileprivate let notificationQueue: DispatchQueue?
|
|
119
|
+
fileprivate(set) var flags: SCNetworkReachabilityFlags? {
|
|
120
|
+
didSet {
|
|
121
|
+
guard flags != oldValue else { return }
|
|
122
|
+
notifyReachabilityChanged()
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public required init(reachabilityRef: SCNetworkReachability,
|
|
127
|
+
queueQoS: DispatchQoS = .default,
|
|
128
|
+
targetQueue: DispatchQueue? = nil,
|
|
129
|
+
notificationQueue: DispatchQueue? = .main) {
|
|
130
|
+
self.allowsCellularConnection = true
|
|
131
|
+
self.reachabilityRef = reachabilityRef
|
|
132
|
+
self.reachabilitySerialQueue = DispatchQueue(label: "uk.co.ashleymills.reachability", qos: queueQoS, target: targetQueue)
|
|
133
|
+
self.notificationQueue = notificationQueue
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public convenience init(hostname: String,
|
|
137
|
+
queueQoS: DispatchQoS = .default,
|
|
138
|
+
targetQueue: DispatchQueue? = nil,
|
|
139
|
+
notificationQueue: DispatchQueue? = .main) throws {
|
|
140
|
+
guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else {
|
|
141
|
+
throw ReachabilityError.failedToCreateWithHostname(hostname, SCError())
|
|
142
|
+
}
|
|
143
|
+
self.init(reachabilityRef: ref, queueQoS: queueQoS, targetQueue: targetQueue, notificationQueue: notificationQueue)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public convenience init(queueQoS: DispatchQoS = .default,
|
|
147
|
+
targetQueue: DispatchQueue? = nil,
|
|
148
|
+
notificationQueue: DispatchQueue? = .main) throws {
|
|
149
|
+
var zeroAddress = sockaddr()
|
|
150
|
+
zeroAddress.sa_len = UInt8(MemoryLayout<sockaddr>.size)
|
|
151
|
+
zeroAddress.sa_family = sa_family_t(AF_INET)
|
|
152
|
+
|
|
153
|
+
guard let ref = SCNetworkReachabilityCreateWithAddress(nil, &zeroAddress) else {
|
|
154
|
+
throw ReachabilityError.failedToCreateWithAddress(zeroAddress, SCError())
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
self.init(reachabilityRef: ref, queueQoS: queueQoS, targetQueue: targetQueue, notificationQueue: notificationQueue)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
deinit {
|
|
161
|
+
stopNotifier()
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public extension Reachability {
|
|
166
|
+
|
|
167
|
+
// MARK: - *** Notifier methods ***
|
|
168
|
+
func startNotifier() throws {
|
|
169
|
+
guard !notifierRunning else { return }
|
|
170
|
+
|
|
171
|
+
let callback: SCNetworkReachabilityCallBack = { (_, flags, info) in
|
|
172
|
+
guard let info = info else { return }
|
|
173
|
+
|
|
174
|
+
// `weakifiedReachability` is guaranteed to exist by virtue of our
|
|
175
|
+
// retain/release callbacks which we provided to the `SCNetworkReachabilityContext`.
|
|
176
|
+
let weakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info).takeUnretainedValue()
|
|
177
|
+
|
|
178
|
+
// The weak `reachability` _may_ no longer exist if the `Reachability`
|
|
179
|
+
// object has since been deallocated but a callback was already in flight.
|
|
180
|
+
weakifiedReachability.reachability?.flags = flags
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let weakifiedReachability = ReachabilityWeakifier(reachability: self)
|
|
184
|
+
let opaqueWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.passUnretained(weakifiedReachability).toOpaque()
|
|
185
|
+
|
|
186
|
+
var context = SCNetworkReachabilityContext(
|
|
187
|
+
version: 0,
|
|
188
|
+
info: UnsafeMutableRawPointer(opaqueWeakifiedReachability),
|
|
189
|
+
retain: { (info: UnsafeRawPointer) -> UnsafeRawPointer in
|
|
190
|
+
let unmanagedWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info)
|
|
191
|
+
_ = unmanagedWeakifiedReachability.retain()
|
|
192
|
+
return UnsafeRawPointer(unmanagedWeakifiedReachability.toOpaque())
|
|
193
|
+
},
|
|
194
|
+
release: { (info: UnsafeRawPointer) -> Void in
|
|
195
|
+
let unmanagedWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info)
|
|
196
|
+
unmanagedWeakifiedReachability.release()
|
|
197
|
+
},
|
|
198
|
+
copyDescription: { (info: UnsafeRawPointer) -> Unmanaged<CFString> in
|
|
199
|
+
let unmanagedWeakifiedReachability = Unmanaged<ReachabilityWeakifier>.fromOpaque(info)
|
|
200
|
+
let weakifiedReachability = unmanagedWeakifiedReachability.takeUnretainedValue()
|
|
201
|
+
let description = weakifiedReachability.reachability?.description ?? "nil"
|
|
202
|
+
return Unmanaged.passRetained(description as CFString)
|
|
203
|
+
}
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
if !SCNetworkReachabilitySetCallback(reachabilityRef, callback, &context) {
|
|
207
|
+
stopNotifier()
|
|
208
|
+
throw ReachabilityError.unableToSetCallback(SCError())
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if !SCNetworkReachabilitySetDispatchQueue(reachabilityRef, reachabilitySerialQueue) {
|
|
212
|
+
stopNotifier()
|
|
213
|
+
throw ReachabilityError.unableToSetDispatchQueue(SCError())
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Perform an initial check
|
|
217
|
+
try setReachabilityFlags()
|
|
218
|
+
|
|
219
|
+
notifierRunning = true
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
func stopNotifier() {
|
|
223
|
+
defer { notifierRunning = false }
|
|
224
|
+
|
|
225
|
+
SCNetworkReachabilitySetCallback(reachabilityRef, nil, nil)
|
|
226
|
+
SCNetworkReachabilitySetDispatchQueue(reachabilityRef, nil)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// MARK: - *** Connection test methods ***
|
|
230
|
+
@available(*, deprecated, message: "Please use `connection != .none`")
|
|
231
|
+
var isReachable: Bool {
|
|
232
|
+
return connection != .unavailable
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
@available(*, deprecated, message: "Please use `connection == .cellular`")
|
|
236
|
+
var isReachableViaWWAN: Bool {
|
|
237
|
+
// Check we're not on the simulator, we're REACHABLE and check we're on WWAN
|
|
238
|
+
return connection == .cellular
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
@available(*, deprecated, message: "Please use `connection == .wifi`")
|
|
242
|
+
var isReachableViaWiFi: Bool {
|
|
243
|
+
return connection == .wifi
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
var description: String {
|
|
247
|
+
return flags?.description ?? "unavailable flags"
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
fileprivate extension Reachability {
|
|
252
|
+
|
|
253
|
+
func setReachabilityFlags() throws {
|
|
254
|
+
try reachabilitySerialQueue.sync { [unowned self] in
|
|
255
|
+
var flags = SCNetworkReachabilityFlags()
|
|
256
|
+
if !SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags) {
|
|
257
|
+
self.stopNotifier()
|
|
258
|
+
throw ReachabilityError.unableToGetFlags(SCError())
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
self.flags = flags
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
func notifyReachabilityChanged() {
|
|
266
|
+
let notify = { [weak self] in
|
|
267
|
+
guard let self = self else { return }
|
|
268
|
+
self.connection != .unavailable ? self.whenReachable?(self) : self.whenUnreachable?(self)
|
|
269
|
+
self.notificationCenter.post(name: .reachabilityChanged, object: self)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// notify on the configured `notificationQueue`, or the caller's (i.e. `reachabilitySerialQueue`)
|
|
273
|
+
notificationQueue?.async(execute: notify) ?? notify()
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
extension SCNetworkReachabilityFlags {
|
|
278
|
+
|
|
279
|
+
typealias Connection = Reachability.Connection
|
|
280
|
+
|
|
281
|
+
var connection: Connection {
|
|
282
|
+
guard isReachableFlagSet else { return .unavailable }
|
|
283
|
+
|
|
284
|
+
// If we're reachable, but not on an iOS device (i.e. simulator), we must be on WiFi
|
|
285
|
+
#if targetEnvironment(simulator)
|
|
286
|
+
return .wifi
|
|
287
|
+
#else
|
|
288
|
+
var connection = Connection.unavailable
|
|
289
|
+
|
|
290
|
+
if !isConnectionRequiredFlagSet {
|
|
291
|
+
connection = .wifi
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if isConnectionOnTrafficOrDemandFlagSet {
|
|
295
|
+
if !isInterventionRequiredFlagSet {
|
|
296
|
+
connection = .wifi
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if isOnWWANFlagSet {
|
|
301
|
+
connection = .cellular
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return connection
|
|
305
|
+
#endif
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
var isOnWWANFlagSet: Bool {
|
|
309
|
+
#if os(iOS)
|
|
310
|
+
return contains(.isWWAN)
|
|
311
|
+
#else
|
|
312
|
+
return false
|
|
313
|
+
#endif
|
|
314
|
+
}
|
|
315
|
+
var isReachableFlagSet: Bool {
|
|
316
|
+
return contains(.reachable)
|
|
317
|
+
}
|
|
318
|
+
var isConnectionRequiredFlagSet: Bool {
|
|
319
|
+
return contains(.connectionRequired)
|
|
320
|
+
}
|
|
321
|
+
var isInterventionRequiredFlagSet: Bool {
|
|
322
|
+
return contains(.interventionRequired)
|
|
323
|
+
}
|
|
324
|
+
var isConnectionOnTrafficFlagSet: Bool {
|
|
325
|
+
return contains(.connectionOnTraffic)
|
|
326
|
+
}
|
|
327
|
+
var isConnectionOnDemandFlagSet: Bool {
|
|
328
|
+
return contains(.connectionOnDemand)
|
|
329
|
+
}
|
|
330
|
+
var isConnectionOnTrafficOrDemandFlagSet: Bool {
|
|
331
|
+
return !intersection([.connectionOnTraffic, .connectionOnDemand]).isEmpty
|
|
332
|
+
}
|
|
333
|
+
var isTransientConnectionFlagSet: Bool {
|
|
334
|
+
return contains(.transientConnection)
|
|
335
|
+
}
|
|
336
|
+
var isLocalAddressFlagSet: Bool {
|
|
337
|
+
return contains(.isLocalAddress)
|
|
338
|
+
}
|
|
339
|
+
var isDirectFlagSet: Bool {
|
|
340
|
+
return contains(.isDirect)
|
|
341
|
+
}
|
|
342
|
+
var isConnectionRequiredAndTransientFlagSet: Bool {
|
|
343
|
+
return intersection([.connectionRequired, .transientConnection]) == [.connectionRequired, .transientConnection]
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// swiftlint:disable identifier_name
|
|
347
|
+
var description: String {
|
|
348
|
+
let W = isOnWWANFlagSet ? "W" : "-"
|
|
349
|
+
let R = isReachableFlagSet ? "R" : "-"
|
|
350
|
+
let c = isConnectionRequiredFlagSet ? "c" : "-"
|
|
351
|
+
let t = isTransientConnectionFlagSet ? "t" : "-"
|
|
352
|
+
let i = isInterventionRequiredFlagSet ? "i" : "-"
|
|
353
|
+
let C = isConnectionOnTrafficFlagSet ? "C" : "-"
|
|
354
|
+
let D = isConnectionOnDemandFlagSet ? "D" : "-"
|
|
355
|
+
let l = isLocalAddressFlagSet ? "l" : "-"
|
|
356
|
+
let d = isDirectFlagSet ? "d" : "-"
|
|
357
|
+
|
|
358
|
+
return "\(W)\(R) \(c)\(t)\(i)\(C)\(D)\(l)\(d)"
|
|
359
|
+
}
|
|
360
|
+
// swiftlint:enable identifier_name
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
`ReachabilityWeakifier` weakly wraps the `Reachability` class
|
|
365
|
+
in order to break retain cycles when interacting with CoreFoundation.
|
|
366
|
+
CoreFoundation callbacks expect a pair of retain/release whenever an
|
|
367
|
+
opaque `info` parameter is provided. These callbacks exist to guard
|
|
368
|
+
against memory management race conditions when invoking the callbacks.
|
|
369
|
+
#### Race Condition
|
|
370
|
+
If we passed `SCNetworkReachabilitySetCallback` a direct reference to our
|
|
371
|
+
`Reachability` class without also providing corresponding retain/release
|
|
372
|
+
callbacks, then a race condition can lead to crashes when:
|
|
373
|
+
- `Reachability` is deallocated on thread X
|
|
374
|
+
- A `SCNetworkReachability` callback(s) is already in flight on thread Y
|
|
375
|
+
#### Retain Cycle
|
|
376
|
+
If we pass `Reachability` to CoreFoundtion while also providing retain/
|
|
377
|
+
release callbacks, we would create a retain cycle once CoreFoundation
|
|
378
|
+
retains our `Reachability` class. This fixes the crashes and his how
|
|
379
|
+
CoreFoundation expects the API to be used, but doesn't play nicely with
|
|
380
|
+
Swift/ARC. This cycle would only be broken after manually calling
|
|
381
|
+
`stopNotifier()` — `deinit` would never be called.
|
|
382
|
+
#### ReachabilityWeakifier
|
|
383
|
+
By providing both retain/release callbacks and wrapping `Reachability` in
|
|
384
|
+
a weak wrapper, we:
|
|
385
|
+
- interact correctly with CoreFoundation, thereby avoiding a crash.
|
|
386
|
+
See "Memory Management Programming Guide for Core Foundation".
|
|
387
|
+
- don't alter the public API of `Reachability.swift` in any way
|
|
388
|
+
- still allow for automatic stopping of the notifier on `deinit`.
|
|
389
|
+
*/
|
|
390
|
+
private class ReachabilityWeakifier {
|
|
391
|
+
weak var reachability: Reachability?
|
|
392
|
+
init(reachability: Reachability) {
|
|
393
|
+
self.reachability = reachability
|
|
394
|
+
}
|
|
395
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import JavaScriptCore
|
|
3
|
+
import WatchConnectivity
|
|
4
|
+
|
|
5
|
+
@objc protocol CapacitorWearableExports: JSExport {
|
|
6
|
+
func send(_ dataDict: JSValue) -> JSValue
|
|
7
|
+
func transferUserInfo(_ userInfo: JSValue) -> JSValue
|
|
8
|
+
func isReachable() -> JSValue
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class CapacitorWearable: NSObject, CapacitorWearableExports {
|
|
12
|
+
func send(_ dataDict: JSValue) -> JSValue {
|
|
13
|
+
var dict: [String: Any] = [:]
|
|
14
|
+
|
|
15
|
+
if dataDict.isObject {
|
|
16
|
+
if let convertedDict = dataDict.toDictionary() as? [String: Any] {
|
|
17
|
+
dict = convertedDict
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
WCSession.default.sendMessage(dict, replyHandler: nil)
|
|
22
|
+
|
|
23
|
+
return JSValue(undefinedIn: JSContext.current())
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
func transferUserInfo(_ userInfo: JSValue) -> JSValue {
|
|
27
|
+
var dict: [String: Any] = [:]
|
|
28
|
+
|
|
29
|
+
if userInfo.isObject {
|
|
30
|
+
if let convertedDict = userInfo.toDictionary() as? [String: Any] {
|
|
31
|
+
dict = convertedDict
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_ = WCSession.default.transferUserInfo(dict)
|
|
36
|
+
|
|
37
|
+
return JSValue(undefinedIn: JSContext.current())
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func isReachable() -> JSValue {
|
|
41
|
+
if WCSession.default.isReachable {
|
|
42
|
+
return JSValue(bool: true, in: JSContext.current())
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return JSValue(bool: false, in: JSContext.current())
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
extension Context {
|
|
4
|
+
public func setupCapacitorAPI() {
|
|
5
|
+
ctx.setObject(CapacitorKVStore.self, forKeyedSubscript: "CapacitorKV" as NSString)
|
|
6
|
+
ctx.setObject(CapacitorGeolocation(context: self), forKeyedSubscript: "CapacitorGeolocation" as NSString)
|
|
7
|
+
ctx.setObject(CapacitorWearable(), forKeyedSubscript: "CapacitorWearable" as NSString)
|
|
8
|
+
ctx.setObject(CapacitorNotifications.self, forKeyedSubscript: "CapacitorNotifications" as NSString)
|
|
9
|
+
ctx.setObject(CapacitorDevice(), forKeyedSubscript: "CapacitorDevice" as NSString)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>CFBundleDevelopmentRegion</key>
|
|
6
|
+
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
|
7
|
+
<key>CFBundleExecutable</key>
|
|
8
|
+
<string>$(EXECUTABLE_NAME)</string>
|
|
9
|
+
<key>CFBundleIdentifier</key>
|
|
10
|
+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
11
|
+
<key>CFBundleInfoDictionaryVersion</key>
|
|
12
|
+
<string>6.0</string>
|
|
13
|
+
<key>CFBundleName</key>
|
|
14
|
+
<string>$(PRODUCT_NAME)</string>
|
|
15
|
+
<key>CFBundlePackageType</key>
|
|
16
|
+
<string>FMWK</string>
|
|
17
|
+
<key>CFBundleShortVersionString</key>
|
|
18
|
+
<string>1.0</string>
|
|
19
|
+
<key>CFBundleVersion</key>
|
|
20
|
+
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
21
|
+
<key>NSPrincipalClass</key>
|
|
22
|
+
<string></string>
|
|
23
|
+
</dict>
|
|
24
|
+
</plist>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
public struct RunnerConfig {
|
|
5
|
+
let label: String
|
|
6
|
+
let src: String
|
|
7
|
+
var autoSchedule: Bool
|
|
8
|
+
let event: String?
|
|
9
|
+
let repeats: Bool?
|
|
10
|
+
let enableWatchConnectivity: Bool
|
|
11
|
+
|
|
12
|
+
let interval: Int?
|
|
13
|
+
|
|
14
|
+
public init(from jsObject: JSObject) throws {
|
|
15
|
+
guard let label = jsObject["label"] as? String else {
|
|
16
|
+
throw BackgroundRunnerPluginError.invalidRunnerConfig(reason: "runner label is missing or invalid")
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
guard let src = jsObject["src"] as? String else {
|
|
20
|
+
throw BackgroundRunnerPluginError.invalidRunnerConfig(reason: "runner source file path is missing or invalid")
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let event = jsObject["event"] as? String
|
|
24
|
+
let repeats = jsObject["repeat"] as? Bool
|
|
25
|
+
let interval = jsObject["interval"] as? Int
|
|
26
|
+
let autoStart = jsObject["autoStart"] as? Bool
|
|
27
|
+
|
|
28
|
+
self.label = label
|
|
29
|
+
self.src = src
|
|
30
|
+
self.event = event
|
|
31
|
+
self.repeats = repeats
|
|
32
|
+
self.interval = interval
|
|
33
|
+
self.autoSchedule = autoStart ?? false
|
|
34
|
+
self.enableWatchConnectivity = jsObject["enableWatchConnectivity"] as? Bool ?? false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import JavaScriptCore
|
|
3
|
+
|
|
4
|
+
public class Context {
|
|
5
|
+
let name: String
|
|
6
|
+
let ctx: JSContext
|
|
7
|
+
|
|
8
|
+
private var timers = [Int: Timer]()
|
|
9
|
+
private var eventListeners = [String: [JSValue]]()
|
|
10
|
+
|
|
11
|
+
// swiftlint:disable:next identifier_name
|
|
12
|
+
public init(vm: JSVirtualMachine, ctxName: String) throws {
|
|
13
|
+
guard let newCtx = JSContext(virtualMachine: vm) else {
|
|
14
|
+
throw EngineError.jsCoreError
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
newCtx.name = ctxName
|
|
18
|
+
|
|
19
|
+
name = ctxName
|
|
20
|
+
ctx = newCtx
|
|
21
|
+
|
|
22
|
+
try setupWebAPI()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public func execute(code: String) throws -> JSValue? {
|
|
26
|
+
var thrownException: JSValue?
|
|
27
|
+
|
|
28
|
+
ctx.exceptionHandler = { _, exception in
|
|
29
|
+
thrownException = exception
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let value = ctx.evaluateScript(code)
|
|
33
|
+
|
|
34
|
+
if let exception = thrownException {
|
|
35
|
+
throw EngineError.jsException(details: String(describing: exception))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return value
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public func dispatchEvent(event: String, details: [String: Any]? = nil) throws {
|
|
42
|
+
if let callbacks = eventListeners[event] {
|
|
43
|
+
try callbacks.forEach { jsFunc in
|
|
44
|
+
var thrownException: JSValue?
|
|
45
|
+
|
|
46
|
+
self.ctx.exceptionHandler = { _, exception in
|
|
47
|
+
thrownException = exception
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if let detailsObj = details {
|
|
51
|
+
var callbackFunctions: [String: Any] = [:]
|
|
52
|
+
|
|
53
|
+
detailsObj.keys.forEach { key in
|
|
54
|
+
if key.hasPrefix("__ebr::") {
|
|
55
|
+
let funcName = key.replacingOccurrences(of: "__ebr::", with: "")
|
|
56
|
+
callbackFunctions[funcName] = detailsObj[key]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
guard let jsDetailsObj = JSValue(object: detailsObj, in: self.ctx) else {
|
|
61
|
+
throw EngineError.jsValueError
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
callbackFunctions.forEach { (funcName: String, jsFunc: Any) in
|
|
65
|
+
jsDetailsObj.setValue(jsFunc, forProperty: funcName)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
jsFunc.call(withArguments: [jsDetailsObj])
|
|
69
|
+
} else {
|
|
70
|
+
jsFunc.call(withArguments: [])
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if let exception = thrownException {
|
|
74
|
+
throw EngineError.jsException(details: String(describing: exception))
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
private func setupWebAPI() throws {
|
|
80
|
+
let consoleObj = JSConsole(name: name)
|
|
81
|
+
let addEventListenerFunc: @convention(block)(String, JSValue) -> Void = { type, listener in
|
|
82
|
+
return self.addEventListener(eventName: type, callback: listener)
|
|
83
|
+
}
|
|
84
|
+
let setTimeoutFunc: @convention(block) (JSValue, Int) -> Int = { callback, timeout in
|
|
85
|
+
return self.setTimeout(callback: callback, timeout: timeout)
|
|
86
|
+
}
|
|
87
|
+
let setIntervalFunc: @convention(block) (JSValue, Int) -> Int = { callback, timeout in
|
|
88
|
+
return self.setInterval(callback: callback, timeout: timeout)
|
|
89
|
+
}
|
|
90
|
+
let clearTimeoutFunc: @convention(block) (Int) -> Void = { id in
|
|
91
|
+
return self.clearTimeout(id: id)
|
|
92
|
+
}
|
|
93
|
+
let fetchFunc: @convention(block) (JSValue, JSValue) -> JSValue = { resource, options in
|
|
94
|
+
return fetch(resource: resource, options: options)
|
|
95
|
+
}
|
|
96
|
+
let newTextEncoderConst: @convention(block) () -> JSTextEncoder = JSTextEncoder.init
|
|
97
|
+
let newTextDecoderConst: @convention(block) (String?, [AnyHashable: Any]?) -> JSTextDecoder = JSTextDecoder.init
|
|
98
|
+
|
|
99
|
+
ctx.setObject(consoleObj, forKeyedSubscript: "console" as NSString)
|
|
100
|
+
ctx.setObject(fetchFunc, forKeyedSubscript: "fetch" as NSString)
|
|
101
|
+
ctx.setObject(JSCrypto.self, forKeyedSubscript: "crypto" as NSString)
|
|
102
|
+
ctx.setObject(addEventListenerFunc, forKeyedSubscript: "addEventListener" as NSString)
|
|
103
|
+
ctx.setObject(setTimeoutFunc, forKeyedSubscript: "setTimeout" as NSString)
|
|
104
|
+
ctx.setObject(setIntervalFunc, forKeyedSubscript: "setInterval" as NSString)
|
|
105
|
+
ctx.setObject(clearTimeoutFunc, forKeyedSubscript: "clearTimeout" as NSString)
|
|
106
|
+
ctx.setObject(clearTimeoutFunc, forKeyedSubscript: "clearInterval" as NSString)
|
|
107
|
+
ctx.setObject(newTextEncoderConst, forKeyedSubscript: "TextEncoder" as NSString)
|
|
108
|
+
ctx.setObject(newTextDecoderConst, forKeyedSubscript: "TextDecoder" as NSString)
|
|
109
|
+
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private func addEventListener(eventName: String, callback: JSValue) {
|
|
113
|
+
if eventListeners[eventName] == nil {
|
|
114
|
+
eventListeners[eventName] = [callback]
|
|
115
|
+
} else {
|
|
116
|
+
eventListeners[eventName]?.append(callback)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private func initTimer(callback: JSValue, timeout: Int, isInterval: Bool) -> Int {
|
|
121
|
+
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(timeout / 1000), repeats: isInterval) { t in
|
|
122
|
+
callback.call(withArguments: [])
|
|
123
|
+
if !isInterval {
|
|
124
|
+
let timerId = Int(Int32.init(truncatingIfNeeded: t.hashValue))
|
|
125
|
+
self.timers.removeValue(forKey: timerId)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let timerId = Int(Int32.init(truncatingIfNeeded: timer.hashValue))
|
|
130
|
+
|
|
131
|
+
RunLoop.current.add(timer, forMode: .common)
|
|
132
|
+
|
|
133
|
+
timers[timerId] = timer
|
|
134
|
+
|
|
135
|
+
return timerId
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private func setTimeout(callback: JSValue, timeout: Int) -> Int {
|
|
139
|
+
return initTimer(callback: callback, timeout: timeout, isInterval: false)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private func setInterval(callback: JSValue, timeout: Int) -> Int {
|
|
143
|
+
return initTimer(callback: callback, timeout: timeout, isInterval: true)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private func clearTimeout(id: Int) {
|
|
147
|
+
if let timer = timers.removeValue(forKey: id) {
|
|
148
|
+
timer.invalidate()
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|