@capgo/capacitor-updater 5.50.1 → 5.51.15
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/CapgoCapacitorUpdater.podspec +1 -1
- package/Package.swift +3 -2
- package/README.md +53 -48
- package/android/build.gradle +1 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +29 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +7 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +1 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +476 -151
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +1354 -412
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +102 -31
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +23 -7
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +2 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +540 -224
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +103 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +1 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +131 -145
- package/dist/docs.json +32 -8
- package/dist/esm/definitions.d.ts +41 -17
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +124 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +9 -1
- package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +3 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +787 -92
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +1014 -268
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +49 -31
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +44 -20
- package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +28 -0
- package/package.json +12 -7
|
@@ -16,6 +16,13 @@ import Version
|
|
|
16
16
|
*/
|
|
17
17
|
@objc(CapacitorUpdaterPlugin)
|
|
18
18
|
public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
19
|
+
enum DefaultChannelPreviewSnapshot: Equatable {
|
|
20
|
+
case missing
|
|
21
|
+
case invalidated
|
|
22
|
+
case snapshot(String?)
|
|
23
|
+
case unreadable
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
lazy var logger: Logger = {
|
|
20
27
|
// Default to true for OS logging. In test environments without a bridge,
|
|
21
28
|
// this will default to true. In production, it reads from config.
|
|
@@ -85,7 +92,12 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
85
92
|
CAPPluginMethod(name: "completeFlexibleUpdate", returnType: CAPPluginReturnPromise)
|
|
86
93
|
]
|
|
87
94
|
public var implementation = CapgoUpdater()
|
|
88
|
-
|
|
95
|
+
|
|
96
|
+
deinit {
|
|
97
|
+
implementation.shutdown()
|
|
98
|
+
}
|
|
99
|
+
private let pluginVersion: String = "5.51.15"
|
|
100
|
+
private let launchStartedAtMs = Int64(Date().timeIntervalSince1970 * 1000)
|
|
89
101
|
static let updateUrlDefault = "https://plugin.capgo.app/updates"
|
|
90
102
|
static let statsUrlDefault = "https://plugin.capgo.app/stats"
|
|
91
103
|
static let channelUrlDefault = "https://plugin.capgo.app/channel_self"
|
|
@@ -123,6 +135,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
123
135
|
private let previewSourceDefaultsKey = "CapacitorUpdater.previewSource"
|
|
124
136
|
private let previewSessionsDefaultsKey = "CapacitorUpdater.previewSessions"
|
|
125
137
|
private let previewSessionAlertPendingDefaultsKey = "CapacitorUpdater.previewSessionAlertPending"
|
|
138
|
+
private let defaultChannelInstallMarkerDefaultsKey = "CapacitorUpdater.defaultChannelInstallMarkerCreated"
|
|
139
|
+
private let defaultChannelInstallMarkerFilename = "CapacitorUpdater.defaultChannelInstallMarker"
|
|
140
|
+
private let defaultChannelStateFilename = "CapacitorUpdater.defaultChannelState"
|
|
141
|
+
private let defaultChannelPreviewSnapshotFilename = "CapacitorUpdater.defaultChannelPreviewSnapshot"
|
|
126
142
|
private let previewDeepLinkScheme = "capgo"
|
|
127
143
|
private let previewDeepLinkRootComponent = "preview"
|
|
128
144
|
private let previewDeepLinkChannelComponent = "channel"
|
|
@@ -135,6 +151,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
135
151
|
private var currentBuildVersion: String = "0"
|
|
136
152
|
private var autoUpdate = false
|
|
137
153
|
private var autoUpdateMode = CapacitorUpdaterPlugin.autoUpdateModeOff
|
|
154
|
+
private var launchStartReported = false
|
|
155
|
+
private var launchReadyReported = false
|
|
138
156
|
private var appReadyTimeout = 10000
|
|
139
157
|
private var appReadyCheck: DispatchWorkItem?
|
|
140
158
|
private var resetWhenUpdate = true
|
|
@@ -160,6 +178,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
160
178
|
private var autoDeleteFailed = false
|
|
161
179
|
private var autoDeletePrevious = false
|
|
162
180
|
var allowSetDefaultChannel = true
|
|
181
|
+
var persistDefaultChannelOnReinstall = true
|
|
163
182
|
private var keepUrlPathAfterReload = false
|
|
164
183
|
private var backgroundWork: DispatchWorkItem?
|
|
165
184
|
private var taskRunning = false
|
|
@@ -172,8 +191,14 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
172
191
|
|
|
173
192
|
// Lock to ensure cleanup completes before downloads start
|
|
174
193
|
private let cleanupLock = NSLock()
|
|
194
|
+
// Short lock for cleanupComplete/cleanupTimedOut only. Never hold across cleanupGroup.wait.
|
|
195
|
+
private let cleanupStateLock = NSLock()
|
|
196
|
+
private let defaultChannelStateLock = NSLock()
|
|
197
|
+
private let cleanupGroup = DispatchGroup()
|
|
175
198
|
private var cleanupComplete = false
|
|
199
|
+
private var cleanupTimedOut = false
|
|
176
200
|
private var cleanupThread: Thread?
|
|
201
|
+
private var defaultChannelCleanupMustRetry = false
|
|
177
202
|
private var persistCustomId = false
|
|
178
203
|
private var persistModifyUrl = false
|
|
179
204
|
private var allowManualBundleError = false
|
|
@@ -191,6 +216,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
191
216
|
private var isLeavingPreviewForIncomingLink = false
|
|
192
217
|
private var previewTransitionClearWorkItem: DispatchWorkItem?
|
|
193
218
|
let semaphoreReady = DispatchSemaphore(value: 0)
|
|
219
|
+
// Best-effort flag: set before we expect notifyAppReady (load/reload).
|
|
220
|
+
// No lock — never held across waits; a rare race only mis-times one wait.
|
|
221
|
+
private var pendingNotifyAppReady = false
|
|
194
222
|
|
|
195
223
|
private var delayUpdateUtils: DelayUpdateUtils!
|
|
196
224
|
|
|
@@ -216,6 +244,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
216
244
|
self.implementation.deviceID = DeviceIdHelper.getOrCreateDeviceId()
|
|
217
245
|
persistCustomId = getConfig().getBoolean("persistCustomId", false)
|
|
218
246
|
allowSetDefaultChannel = getConfig().getBoolean("allowSetDefaultChannel", true)
|
|
247
|
+
persistDefaultChannelOnReinstall = getConfig().getBoolean("persistDefaultChannelOnReinstall", true)
|
|
219
248
|
if persistCustomId {
|
|
220
249
|
let storedCustomId = UserDefaults.standard.string(forKey: customIdDefaultsKey) ?? ""
|
|
221
250
|
if !storedCustomId.isEmpty {
|
|
@@ -331,14 +360,61 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
331
360
|
logger.info("Loaded persisted channelUrl")
|
|
332
361
|
}
|
|
333
362
|
}
|
|
363
|
+
implementation.restorePendingStats()
|
|
334
364
|
|
|
335
|
-
|
|
336
|
-
|
|
365
|
+
let nativeBuildVersionChanged = self.hasNativeBuildVersionChanged()
|
|
366
|
+
let defaultChannelPersistenceDisabled = !persistDefaultChannelOnReinstall
|
|
367
|
+
let restoredReinstall = defaultChannelPersistenceDisabled && self.isRestoredReinstall()
|
|
368
|
+
var installMarkerCanBePrepared = true
|
|
369
|
+
if shouldClearPersistedDefaultChannel(
|
|
370
|
+
nativeBuildVersionChanged: nativeBuildVersionChanged,
|
|
371
|
+
resetWhenUpdate: resetWhenUpdate,
|
|
372
|
+
restoredReinstall: restoredReinstall
|
|
373
|
+
) {
|
|
374
|
+
installMarkerCanBePrepared = clearPersistedDefaultChannel()
|
|
375
|
+
if installMarkerCanBePrepared {
|
|
376
|
+
logger.info("Cleared persisted defaultChannel because reinstall persistence is disabled")
|
|
377
|
+
} else {
|
|
378
|
+
logger.warn("Cannot durably clear persisted defaultChannel")
|
|
379
|
+
self.defaultChannelCleanupMustRetry = true
|
|
380
|
+
self.invalidateDefaultChannelInstallMarker()
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if defaultChannelPersistenceDisabled && installMarkerCanBePrepared {
|
|
384
|
+
self.prepareDefaultChannelInstallMarker()
|
|
385
|
+
}
|
|
386
|
+
if !previewSessionEnabled,
|
|
387
|
+
!defaultChannelCleanupMustRetry,
|
|
388
|
+
self.hasPendingDefaultChannelPreviewSnapshot(),
|
|
389
|
+
!self.restorePreviewPreviousDefaultChannel() {
|
|
390
|
+
logger.warn("Default channel preview restore remains pending")
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
let configDefaultChannel = getConfig().getString("defaultChannel", "")!
|
|
394
|
+
let storedDefaultChannel = UserDefaults.standard.string(forKey: defaultChannelDefaultsKey)
|
|
395
|
+
let stateFile = self.defaultChannelStateFile()
|
|
396
|
+
let state = stateFile.map { self.defaultChannelState(file: $0) }
|
|
397
|
+
if defaultChannelCleanupMustRetry {
|
|
398
|
+
implementation.defaultChannel = configDefaultChannel
|
|
399
|
+
logger.warn("Using configured defaultChannel until persisted cleanup can retry")
|
|
400
|
+
} else if let state = state, state.exists, state.isReadable {
|
|
401
|
+
self.reconcileDefaultChannelDefaults(with: state.channel)
|
|
402
|
+
implementation.defaultChannel = state.channel ?? configDefaultChannel
|
|
403
|
+
logger.info("Loaded persisted defaultChannel from local state")
|
|
404
|
+
} else if defaultChannelPersistenceDisabled, state?.exists == true {
|
|
405
|
+
implementation.defaultChannel = configDefaultChannel
|
|
406
|
+
logger.warn("Ignoring unreadable persisted defaultChannel while reinstall persistence is disabled")
|
|
407
|
+
} else if let storedDefaultChannel = storedDefaultChannel {
|
|
337
408
|
implementation.defaultChannel = storedDefaultChannel
|
|
338
409
|
logger.info("Loaded persisted defaultChannel from setChannel()")
|
|
339
410
|
} else {
|
|
340
|
-
implementation.defaultChannel =
|
|
411
|
+
implementation.defaultChannel = configDefaultChannel
|
|
341
412
|
}
|
|
413
|
+
if !defaultChannelCleanupMustRetry,
|
|
414
|
+
state?.exists != true || (!defaultChannelPersistenceDisabled && state?.isReadable != true) {
|
|
415
|
+
_ = self.persistDefaultChannelStateFromDefaults()
|
|
416
|
+
}
|
|
417
|
+
self.reportAppLaunchStart()
|
|
342
418
|
self.implementation.autoReset()
|
|
343
419
|
let appHealthTracker = AppHealthTracker(implementation: self.implementation)
|
|
344
420
|
self.appHealthTracker = appHealthTracker
|
|
@@ -347,16 +423,26 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
347
423
|
|
|
348
424
|
// Check if app was recently installed/updated BEFORE cleanup updates the stored native build version.
|
|
349
425
|
self.wasRecentlyInstalledOrUpdated = self.checkIfRecentlyInstalledOrUpdated()
|
|
350
|
-
let nativeBuildVersionChanged = self.hasNativeBuildVersionChanged()
|
|
351
426
|
if nativeBuildVersionChanged {
|
|
352
427
|
self.clearPreviewSessionForNativeBuildChange()
|
|
353
428
|
}
|
|
354
429
|
self.leavePreviewSessionForLaunchURLIfNeeded()
|
|
355
430
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
self
|
|
431
|
+
// Downloads (including shake-menu / CapgoUpdater entry points) wait on this gate.
|
|
432
|
+
self.implementation.beforeDownload = { [weak self] in
|
|
433
|
+
try self?.waitForCleanupIfNeeded()
|
|
359
434
|
}
|
|
435
|
+
// Always run async cleanup: delete obsolete bundles on native update (when enabled)
|
|
436
|
+
// and sweep orphan directories every launch. Must not block app startup.
|
|
437
|
+
if !resetWhenUpdate {
|
|
438
|
+
UserDefaults.standard.set(self.currentBuildVersion, forKey: "LatestNativeBuildVersion")
|
|
439
|
+
UserDefaults.standard.synchronize()
|
|
440
|
+
}
|
|
441
|
+
let didResetCurrentBundle = resetWhenUpdate ? self.resetCurrentBundleForNativeBuildChangeIfNeeded() : false
|
|
442
|
+
self.cleanupObsoleteVersions(
|
|
443
|
+
resetWhenUpdate: resetWhenUpdate,
|
|
444
|
+
didResetCurrentBundle: didResetCurrentBundle
|
|
445
|
+
)
|
|
360
446
|
self.reportNativeVersionStatsIfChanged()
|
|
361
447
|
|
|
362
448
|
// Load the server
|
|
@@ -518,6 +604,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
518
604
|
}
|
|
519
605
|
|
|
520
606
|
private func semaphoreUp() {
|
|
607
|
+
pendingNotifyAppReady = true
|
|
521
608
|
DispatchQueue.global().async {
|
|
522
609
|
self.semaphoreWait(waitTime: 0)
|
|
523
610
|
}
|
|
@@ -531,6 +618,327 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
531
618
|
UserDefaults.standard.string(forKey: "LatestNativeBuildVersion") ?? UserDefaults.standard.string(forKey: "LatestVersionNative") ?? "0"
|
|
532
619
|
}
|
|
533
620
|
|
|
621
|
+
func shouldClearPersistedDefaultChannel(
|
|
622
|
+
nativeBuildVersionChanged: Bool,
|
|
623
|
+
resetWhenUpdate: Bool,
|
|
624
|
+
restoredReinstall: Bool
|
|
625
|
+
) -> Bool {
|
|
626
|
+
!persistDefaultChannelOnReinstall && (restoredReinstall || (resetWhenUpdate && nativeBuildVersionChanged))
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
func clearPersistedDefaultChannel() -> Bool {
|
|
630
|
+
guard let stateFile = self.defaultChannelStateFile(),
|
|
631
|
+
let previewSnapshotFile = self.defaultChannelPreviewSnapshotFile() else {
|
|
632
|
+
return false
|
|
633
|
+
}
|
|
634
|
+
return self.clearPersistedDefaultChannel(
|
|
635
|
+
stateFile: stateFile,
|
|
636
|
+
previewSnapshotFile: previewSnapshotFile
|
|
637
|
+
)
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
func clearPersistedDefaultChannel(stateFile: URL, previewSnapshotFile: URL) -> Bool {
|
|
641
|
+
defaultChannelStateLock.lock()
|
|
642
|
+
defer { defaultChannelStateLock.unlock() }
|
|
643
|
+
do {
|
|
644
|
+
try self.persistDefaultChannelStateWithoutLock(channel: nil, file: stateFile)
|
|
645
|
+
try self.persistDefaultChannelPreviewSnapshotWithoutLock(
|
|
646
|
+
channel: nil,
|
|
647
|
+
isValid: false,
|
|
648
|
+
file: previewSnapshotFile
|
|
649
|
+
)
|
|
650
|
+
UserDefaults.standard.removeObject(forKey: defaultChannelDefaultsKey)
|
|
651
|
+
UserDefaults.standard.removeObject(forKey: previewPreviousDefaultChannelDefaultsKey)
|
|
652
|
+
UserDefaults.standard.removeObject(forKey: previewPreviousDefaultChannelWasSetDefaultsKey)
|
|
653
|
+
return true
|
|
654
|
+
} catch {
|
|
655
|
+
logger.warn("Cannot persist cleared default channel state: \(error.localizedDescription)")
|
|
656
|
+
return false
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
private func defaultChannelStateFile() -> URL? {
|
|
661
|
+
FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?
|
|
662
|
+
.appendingPathComponent(defaultChannelStateFilename)
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
private func defaultChannelPreviewSnapshotFile() -> URL? {
|
|
666
|
+
FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?
|
|
667
|
+
.appendingPathComponent(defaultChannelPreviewSnapshotFilename)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
func defaultChannelState(file: URL) -> (exists: Bool, channel: String?, isReadable: Bool) {
|
|
671
|
+
defaultChannelStateLock.lock()
|
|
672
|
+
defer { defaultChannelStateLock.unlock() }
|
|
673
|
+
return self.defaultChannelStateWithoutLock(file: file)
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
private func defaultChannelStateWithoutLock(file: URL) -> (exists: Bool, channel: String?, isReadable: Bool) {
|
|
677
|
+
let fileManager = FileManager.default
|
|
678
|
+
guard fileManager.fileExists(atPath: file.path) else {
|
|
679
|
+
return (false, nil, true)
|
|
680
|
+
}
|
|
681
|
+
do {
|
|
682
|
+
let data = try Data(contentsOf: file)
|
|
683
|
+
guard !data.isEmpty else {
|
|
684
|
+
return (true, nil, true)
|
|
685
|
+
}
|
|
686
|
+
guard let channel = String(data: data, encoding: .utf8) else {
|
|
687
|
+
logger.warn("Cannot decode persisted default channel state")
|
|
688
|
+
return (true, nil, false)
|
|
689
|
+
}
|
|
690
|
+
return (true, channel, true)
|
|
691
|
+
} catch {
|
|
692
|
+
logger.warn("Cannot read persisted default channel state: \(error.localizedDescription)")
|
|
693
|
+
return (true, nil, false)
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
func persistDefaultChannelState(channel: String?, file: URL) throws {
|
|
698
|
+
defaultChannelStateLock.lock()
|
|
699
|
+
defer { defaultChannelStateLock.unlock() }
|
|
700
|
+
try self.persistDefaultChannelStateWithoutLock(channel: channel, file: file)
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
private func persistDefaultChannelStateWithoutLock(channel: String?, file: URL) throws {
|
|
704
|
+
let data = channel.map { Data($0.utf8) } ?? Data()
|
|
705
|
+
try self.persistBackupExcludedFile(file: file, data: data, overwrite: true)
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
func defaultChannelPreviewSnapshot(file: URL) -> DefaultChannelPreviewSnapshot {
|
|
709
|
+
defaultChannelStateLock.lock()
|
|
710
|
+
defer { defaultChannelStateLock.unlock() }
|
|
711
|
+
return self.defaultChannelPreviewSnapshotWithoutLock(file: file)
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
private func defaultChannelPreviewSnapshotWithoutLock(file: URL) -> DefaultChannelPreviewSnapshot {
|
|
715
|
+
guard FileManager.default.fileExists(atPath: file.path) else {
|
|
716
|
+
return .missing
|
|
717
|
+
}
|
|
718
|
+
do {
|
|
719
|
+
let data = try Data(contentsOf: file)
|
|
720
|
+
guard let kind = data.first else {
|
|
721
|
+
return .unreadable
|
|
722
|
+
}
|
|
723
|
+
switch kind {
|
|
724
|
+
case 0 where data.count == 1:
|
|
725
|
+
return .invalidated
|
|
726
|
+
case 1 where data.count == 1:
|
|
727
|
+
return .snapshot(nil)
|
|
728
|
+
case 2:
|
|
729
|
+
guard let channel = String(data: Data(data.dropFirst()), encoding: .utf8) else {
|
|
730
|
+
return .unreadable
|
|
731
|
+
}
|
|
732
|
+
return .snapshot(channel)
|
|
733
|
+
default:
|
|
734
|
+
return .unreadable
|
|
735
|
+
}
|
|
736
|
+
} catch {
|
|
737
|
+
logger.warn("Cannot read default channel preview snapshot: \(error.localizedDescription)")
|
|
738
|
+
return .unreadable
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
func persistDefaultChannelPreviewSnapshot(channel: String?, file: URL) throws {
|
|
743
|
+
defaultChannelStateLock.lock()
|
|
744
|
+
defer { defaultChannelStateLock.unlock() }
|
|
745
|
+
try self.persistDefaultChannelPreviewSnapshotWithoutLock(
|
|
746
|
+
channel: channel,
|
|
747
|
+
isValid: true,
|
|
748
|
+
file: file
|
|
749
|
+
)
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
func invalidateDefaultChannelPreviewSnapshot(file: URL) throws {
|
|
753
|
+
defaultChannelStateLock.lock()
|
|
754
|
+
defer { defaultChannelStateLock.unlock() }
|
|
755
|
+
try self.persistDefaultChannelPreviewSnapshotWithoutLock(
|
|
756
|
+
channel: nil,
|
|
757
|
+
isValid: false,
|
|
758
|
+
file: file
|
|
759
|
+
)
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
private func persistDefaultChannelPreviewSnapshotWithoutLock(
|
|
763
|
+
channel: String?,
|
|
764
|
+
isValid: Bool,
|
|
765
|
+
file: URL
|
|
766
|
+
) throws {
|
|
767
|
+
var data = Data()
|
|
768
|
+
if !isValid {
|
|
769
|
+
data.append(0)
|
|
770
|
+
} else if let channel = channel {
|
|
771
|
+
data.append(2)
|
|
772
|
+
data.append(contentsOf: channel.utf8)
|
|
773
|
+
} else {
|
|
774
|
+
data.append(1)
|
|
775
|
+
}
|
|
776
|
+
try self.persistBackupExcludedFile(file: file, data: data, overwrite: true)
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
private func persistDefaultChannelPreviewSnapshot(channel: String?) -> Bool {
|
|
780
|
+
guard let file = self.defaultChannelPreviewSnapshotFile() else {
|
|
781
|
+
return false
|
|
782
|
+
}
|
|
783
|
+
do {
|
|
784
|
+
try self.persistDefaultChannelPreviewSnapshot(channel: channel, file: file)
|
|
785
|
+
return true
|
|
786
|
+
} catch {
|
|
787
|
+
logger.warn("Cannot persist default channel preview snapshot: \(error.localizedDescription)")
|
|
788
|
+
return false
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
private func hasPendingDefaultChannelPreviewSnapshot() -> Bool {
|
|
793
|
+
guard let file = self.defaultChannelPreviewSnapshotFile() else {
|
|
794
|
+
return false
|
|
795
|
+
}
|
|
796
|
+
switch self.defaultChannelPreviewSnapshot(file: file) {
|
|
797
|
+
case .snapshot, .unreadable:
|
|
798
|
+
return true
|
|
799
|
+
case .missing, .invalidated:
|
|
800
|
+
return false
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
@discardableResult
|
|
805
|
+
func persistDefaultChannelStateFromDefaults() -> Bool {
|
|
806
|
+
guard let file = self.defaultChannelStateFile() else {
|
|
807
|
+
logger.warn("Cannot locate default channel state file")
|
|
808
|
+
return false
|
|
809
|
+
}
|
|
810
|
+
return self.persistDefaultChannelStateFromDefaults(stateFile: file)
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
@discardableResult
|
|
814
|
+
func persistDefaultChannelStateFromDefaults(stateFile: URL) -> Bool {
|
|
815
|
+
defaultChannelStateLock.lock()
|
|
816
|
+
defer { defaultChannelStateLock.unlock() }
|
|
817
|
+
do {
|
|
818
|
+
try self.persistDefaultChannelStateWithoutLock(
|
|
819
|
+
channel: UserDefaults.standard.string(forKey: defaultChannelDefaultsKey),
|
|
820
|
+
file: stateFile
|
|
821
|
+
)
|
|
822
|
+
return true
|
|
823
|
+
} catch {
|
|
824
|
+
do {
|
|
825
|
+
if FileManager.default.fileExists(atPath: stateFile.path) {
|
|
826
|
+
try FileManager.default.removeItem(at: stateFile)
|
|
827
|
+
}
|
|
828
|
+
logger.warn("Cannot persist default channel state; falling back to UserDefaults: \(error.localizedDescription)")
|
|
829
|
+
return true
|
|
830
|
+
} catch let invalidationError {
|
|
831
|
+
logger.warn("Cannot persist or invalidate default channel state: \(invalidationError.localizedDescription)")
|
|
832
|
+
return false
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
private func persistedDefaultChannel() -> String? {
|
|
838
|
+
guard let file = self.defaultChannelStateFile() else {
|
|
839
|
+
return UserDefaults.standard.string(forKey: defaultChannelDefaultsKey)
|
|
840
|
+
}
|
|
841
|
+
return self.persistedDefaultChannel(stateFile: file)
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
func persistedDefaultChannel(stateFile: URL) -> String? {
|
|
845
|
+
let state = self.defaultChannelState(file: stateFile)
|
|
846
|
+
if state.exists {
|
|
847
|
+
if state.isReadable {
|
|
848
|
+
return state.channel
|
|
849
|
+
}
|
|
850
|
+
if !persistDefaultChannelOnReinstall {
|
|
851
|
+
return nil
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
return UserDefaults.standard.string(forKey: defaultChannelDefaultsKey)
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
private func reconcileDefaultChannelDefaults(with channel: String?) {
|
|
858
|
+
if let channel = channel {
|
|
859
|
+
UserDefaults.standard.set(channel, forKey: defaultChannelDefaultsKey)
|
|
860
|
+
} else {
|
|
861
|
+
UserDefaults.standard.removeObject(forKey: defaultChannelDefaultsKey)
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
private func defaultChannelInstallMarker() -> URL? {
|
|
866
|
+
FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?
|
|
867
|
+
.appendingPathComponent(defaultChannelInstallMarkerFilename)
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
private func invalidateDefaultChannelInstallMarker() {
|
|
871
|
+
guard let marker = self.defaultChannelInstallMarker() else {
|
|
872
|
+
return
|
|
873
|
+
}
|
|
874
|
+
do {
|
|
875
|
+
try self.invalidateDefaultChannelInstallMarker(marker: marker)
|
|
876
|
+
} catch {
|
|
877
|
+
logger.warn("Cannot invalidate default channel install marker for cleanup retry: \(error.localizedDescription)")
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
func invalidateDefaultChannelInstallMarker(marker: URL) throws {
|
|
882
|
+
let fileManager = FileManager.default
|
|
883
|
+
if fileManager.fileExists(atPath: marker.path) {
|
|
884
|
+
try fileManager.removeItem(at: marker)
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
func isRestoredReinstall() -> Bool {
|
|
889
|
+
guard let marker = self.defaultChannelInstallMarker() else {
|
|
890
|
+
return false
|
|
891
|
+
}
|
|
892
|
+
return self.isRestoredReinstall(
|
|
893
|
+
marker: marker,
|
|
894
|
+
markerWasCreated: UserDefaults.standard.bool(forKey: defaultChannelInstallMarkerDefaultsKey)
|
|
895
|
+
)
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
func isRestoredReinstall(marker: URL, markerWasCreated: Bool) -> Bool {
|
|
899
|
+
markerWasCreated && !FileManager.default.fileExists(atPath: marker.path)
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
private func prepareDefaultChannelInstallMarker() {
|
|
903
|
+
guard let marker = self.defaultChannelInstallMarker() else {
|
|
904
|
+
return
|
|
905
|
+
}
|
|
906
|
+
self.prepareDefaultChannelInstallMarker(
|
|
907
|
+
marker: marker,
|
|
908
|
+
markerWasCreated: UserDefaults.standard.bool(forKey: defaultChannelInstallMarkerDefaultsKey)
|
|
909
|
+
)
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
func prepareDefaultChannelInstallMarker(marker: URL, markerWasCreated: Bool) {
|
|
913
|
+
do {
|
|
914
|
+
try self.persistBackupExcludedFile(file: marker, data: Data())
|
|
915
|
+
if !markerWasCreated {
|
|
916
|
+
UserDefaults.standard.set(true, forKey: defaultChannelInstallMarkerDefaultsKey)
|
|
917
|
+
UserDefaults.standard.synchronize()
|
|
918
|
+
}
|
|
919
|
+
} catch {
|
|
920
|
+
try? FileManager.default.removeItem(at: marker)
|
|
921
|
+
UserDefaults.standard.set(false, forKey: defaultChannelInstallMarkerDefaultsKey)
|
|
922
|
+
UserDefaults.standard.synchronize()
|
|
923
|
+
logger.warn("Cannot prepare default channel install marker: \(error.localizedDescription)")
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
private func persistBackupExcludedFile(file: URL, data: Data, overwrite: Bool = false) throws {
|
|
928
|
+
let fileManager = FileManager.default
|
|
929
|
+
if overwrite || !fileManager.fileExists(atPath: file.path) {
|
|
930
|
+
try fileManager.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
|
|
931
|
+
try data.write(to: file, options: .atomic)
|
|
932
|
+
}
|
|
933
|
+
let currentResourceValues = try file.resourceValues(forKeys: [.isExcludedFromBackupKey])
|
|
934
|
+
if currentResourceValues.isExcludedFromBackup != true {
|
|
935
|
+
var resourceValues = URLResourceValues()
|
|
936
|
+
resourceValues.isExcludedFromBackup = true
|
|
937
|
+
var mutableFile = file
|
|
938
|
+
try mutableFile.setResourceValues(resourceValues)
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
534
942
|
func hasNativeBuildVersionChanged() -> Bool {
|
|
535
943
|
let previous = self.storedNativeBuildVersion()
|
|
536
944
|
return previous != "0" && self.currentBuildVersion != previous
|
|
@@ -629,12 +1037,35 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
629
1037
|
return true
|
|
630
1038
|
}
|
|
631
1039
|
|
|
632
|
-
private func cleanupObsoleteVersions(didResetCurrentBundle: Bool = false) {
|
|
1040
|
+
private func cleanupObsoleteVersions(resetWhenUpdate: Bool = true, didResetCurrentBundle: Bool = false) {
|
|
1041
|
+
// Enter before publishing incomplete state so waiters cannot hit an empty group.
|
|
1042
|
+
self.cleanupStateLock.lock()
|
|
1043
|
+
self.cleanupComplete = false
|
|
1044
|
+
self.cleanupTimedOut = false
|
|
1045
|
+
self.cleanupGroup.enter()
|
|
1046
|
+
self.cleanupStateLock.unlock()
|
|
633
1047
|
cleanupThread = Thread {
|
|
1048
|
+
let bgTaskLock = NSLock()
|
|
1049
|
+
var cleanupBackgroundTask = UIBackgroundTaskIdentifier.invalid
|
|
1050
|
+
let endCleanupBackgroundTask = {
|
|
1051
|
+
bgTaskLock.lock()
|
|
1052
|
+
defer { bgTaskLock.unlock() }
|
|
1053
|
+
if cleanupBackgroundTask != .invalid {
|
|
1054
|
+
UIApplication.shared.endBackgroundTask(cleanupBackgroundTask)
|
|
1055
|
+
cleanupBackgroundTask = .invalid
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
cleanupBackgroundTask = UIApplication.shared.beginBackgroundTask(withName: "CapgoBundleCleanup") {
|
|
1059
|
+
endCleanupBackgroundTask()
|
|
1060
|
+
}
|
|
634
1061
|
self.cleanupLock.lock()
|
|
635
1062
|
defer {
|
|
1063
|
+
self.cleanupStateLock.lock()
|
|
636
1064
|
self.cleanupComplete = true
|
|
1065
|
+
self.cleanupStateLock.unlock()
|
|
637
1066
|
self.cleanupLock.unlock()
|
|
1067
|
+
self.cleanupGroup.leave()
|
|
1068
|
+
endCleanupBackgroundTask()
|
|
638
1069
|
self.logger.info("Cleanup complete")
|
|
639
1070
|
}
|
|
640
1071
|
|
|
@@ -665,67 +1096,92 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
665
1096
|
// 2. Compare both keys. If any is not equal to "currentBuildVersion", then revert to builtin version. This fixes the part 2 of this bug
|
|
666
1097
|
|
|
667
1098
|
let previous = self.storedNativeBuildVersion()
|
|
668
|
-
|
|
1099
|
+
let nativeVersionChanged = previous != "0" && self.currentBuildVersion != previous
|
|
1100
|
+
if resetWhenUpdate && nativeVersionChanged {
|
|
669
1101
|
if !didResetCurrentBundle {
|
|
670
1102
|
self.logger.info("Native build version changed from \(previous) to \(self.currentBuildVersion). Resetting current bundle to builtin.")
|
|
671
1103
|
self.implementation.reset(isInternal: true)
|
|
672
1104
|
}
|
|
673
1105
|
let res = self.implementation.list()
|
|
674
1106
|
for version in res {
|
|
675
|
-
// Check if thread was cancelled
|
|
676
1107
|
if Thread.current.isCancelled {
|
|
677
|
-
self.logger.warn("Cleanup was cancelled, stopping")
|
|
678
1108
|
return
|
|
679
1109
|
}
|
|
680
1110
|
self.logger.info("Deleting obsolete bundle: \(version.getId())")
|
|
681
|
-
let
|
|
682
|
-
if !
|
|
1111
|
+
let deleted = self.implementation.delete(id: version.getId())
|
|
1112
|
+
if !deleted {
|
|
683
1113
|
self.logger.error("Delete failed, id \(version.getId()) doesn't exist")
|
|
684
1114
|
}
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
let storedBundles = self.implementation.list(raw: true)
|
|
688
|
-
let allowedIds = Set(storedBundles.compactMap { info -> String? in
|
|
689
|
-
let id = info.getId()
|
|
690
|
-
return id.isEmpty ? nil : id
|
|
691
|
-
})
|
|
692
|
-
self.implementation.cleanupDownloadDirectories(allowedIds: allowedIds, threadToCheck: Thread.current)
|
|
693
|
-
self.implementation.cleanupOrphanedTempFolders(threadToCheck: Thread.current)
|
|
694
|
-
|
|
695
|
-
// Check again before the expensive delta cache cleanup
|
|
696
|
-
if Thread.current.isCancelled {
|
|
697
|
-
self.logger.warn("Cleanup was cancelled before delta cache cleanup")
|
|
698
|
-
return
|
|
1115
|
+
Thread.sleep(forTimeInterval: 0.075)
|
|
699
1116
|
}
|
|
700
1117
|
self.implementation.cleanupDeltaCache(threadToCheck: Thread.current)
|
|
701
1118
|
}
|
|
702
|
-
UserDefaults.standard.set(self.currentBuildVersion, forKey: "LatestNativeBuildVersion")
|
|
703
|
-
UserDefaults.standard.synchronize()
|
|
704
|
-
}
|
|
705
|
-
cleanupThread?.start()
|
|
706
1119
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
1120
|
+
if Thread.current.isCancelled {
|
|
1121
|
+
return
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
// Resume any DELETING leftovers from prior kills, one-by-one.
|
|
1125
|
+
self.implementation.drainPendingDeletes()
|
|
1126
|
+
|
|
1127
|
+
if Thread.current.isCancelled {
|
|
1128
|
+
return
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// Always sweep orphan directories so incomplete prior cleanups (or failed deletes)
|
|
1132
|
+
// cannot leave hundreds of MB behind across launches.
|
|
1133
|
+
let allowedIds = self.implementation.allowedBundleIdsForCleanup()
|
|
1134
|
+
self.implementation.cleanupDownloadDirectories(allowedIds: allowedIds, threadToCheck: Thread.current)
|
|
1135
|
+
if Thread.current.isCancelled {
|
|
1136
|
+
return
|
|
1137
|
+
}
|
|
1138
|
+
self.implementation.cleanupOrphanedTempFolders(threadToCheck: Thread.current)
|
|
1139
|
+
|
|
1140
|
+
if self.defaultChannelCleanupMustRetry {
|
|
1141
|
+
self.logger.warn("Keeping the previous native build version so default channel cleanup retries")
|
|
1142
|
+
} else {
|
|
1143
|
+
UserDefaults.standard.set(self.currentBuildVersion, forKey: "LatestNativeBuildVersion")
|
|
1144
|
+
UserDefaults.standard.synchronize()
|
|
714
1145
|
}
|
|
715
1146
|
}
|
|
1147
|
+
cleanupThread?.start()
|
|
716
1148
|
}
|
|
717
1149
|
|
|
718
|
-
private func
|
|
719
|
-
|
|
1150
|
+
private func cleanupTimeoutError() -> NSError {
|
|
1151
|
+
NSError(
|
|
1152
|
+
domain: "CapacitorUpdater",
|
|
1153
|
+
code: 1,
|
|
1154
|
+
userInfo: [NSLocalizedDescriptionKey: "Cleanup did not finish before download"]
|
|
1155
|
+
)
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
private func waitForCleanupIfNeeded() throws {
|
|
1159
|
+
cleanupStateLock.lock()
|
|
1160
|
+
let alreadyDone = cleanupComplete
|
|
1161
|
+
let alreadyTimedOut = cleanupTimedOut
|
|
1162
|
+
cleanupStateLock.unlock()
|
|
1163
|
+
if alreadyDone {
|
|
720
1164
|
return // Already done, no need to wait
|
|
721
1165
|
}
|
|
1166
|
+
if alreadyTimedOut {
|
|
1167
|
+
throw cleanupTimeoutError()
|
|
1168
|
+
}
|
|
722
1169
|
|
|
723
1170
|
logger.info("Waiting for cleanup to complete before starting download...")
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
1171
|
+
let result = cleanupGroup.wait(timeout: .now() + .seconds(60))
|
|
1172
|
+
if result == .timedOut {
|
|
1173
|
+
logger.warn("Cleanup wait timed out after 60s, cancelling leftover cleanup")
|
|
1174
|
+
cleanupThread?.cancel()
|
|
1175
|
+
let cancelled = cleanupGroup.wait(timeout: .now() + .seconds(60))
|
|
1176
|
+
if cancelled == .timedOut {
|
|
1177
|
+
cleanupStateLock.lock()
|
|
1178
|
+
cleanupTimedOut = true
|
|
1179
|
+
cleanupStateLock.unlock()
|
|
1180
|
+
logger.error("Cleanup did not finish after cancel, aborting download")
|
|
1181
|
+
throw cleanupTimeoutError()
|
|
1182
|
+
}
|
|
1183
|
+
return
|
|
1184
|
+
}
|
|
729
1185
|
logger.info("Cleanup finished, proceeding with download")
|
|
730
1186
|
}
|
|
731
1187
|
|
|
@@ -1293,6 +1749,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1293
1749
|
|
|
1294
1750
|
let performReload: () -> Bool = {
|
|
1295
1751
|
guard self.applyCurrentBundleToBridge(bridge) else {
|
|
1752
|
+
self.clearPendingNotifyAppReady()
|
|
1296
1753
|
return false
|
|
1297
1754
|
}
|
|
1298
1755
|
self.checkAppReady()
|
|
@@ -1380,6 +1837,14 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1380
1837
|
}
|
|
1381
1838
|
}
|
|
1382
1839
|
|
|
1840
|
+
private func queueBundleForNextBackgroundInstall(_ next: BundleInfo) -> Bool {
|
|
1841
|
+
guard self.implementation.setNextBundle(next: next.getId()) else {
|
|
1842
|
+
self.logger.error("Failed to queue downloaded bundle as next: \(next.toString())")
|
|
1843
|
+
return false
|
|
1844
|
+
}
|
|
1845
|
+
return true
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1383
1848
|
private func applyDownloadedBundleForDirectUpdate(_ next: BundleInfo) -> Bool {
|
|
1384
1849
|
let previousState = self.implementation.captureResetState()
|
|
1385
1850
|
let previousBundleName = self.implementation.getCurrentBundle().getVersionName()
|
|
@@ -1497,8 +1962,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1497
1962
|
UserDefaults.standard.removeObject(forKey: self.previewPreviousNextBundleDefaultsKey)
|
|
1498
1963
|
}
|
|
1499
1964
|
|
|
1965
|
+
let previousDefaultChannel = self.persistedDefaultChannel()
|
|
1966
|
+
guard self.persistDefaultChannelPreviewSnapshot(channel: previousDefaultChannel) else {
|
|
1967
|
+
logger.error("Could not durably save the default channel preview snapshot")
|
|
1968
|
+
return false
|
|
1969
|
+
}
|
|
1500
1970
|
UserDefaults.standard.set(self.implementation.appId, forKey: self.previewPreviousAppIdDefaultsKey)
|
|
1501
|
-
if let previousDefaultChannel =
|
|
1971
|
+
if let previousDefaultChannel = previousDefaultChannel {
|
|
1502
1972
|
UserDefaults.standard.set(previousDefaultChannel, forKey: self.previewPreviousDefaultChannelDefaultsKey)
|
|
1503
1973
|
UserDefaults.standard.set(true, forKey: self.previewPreviousDefaultChannelWasSetDefaultsKey)
|
|
1504
1974
|
} else {
|
|
@@ -1946,7 +2416,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1946
2416
|
?? self.getBooleanConfig("allowShakeChannelSelector", defaultValue: false)
|
|
1947
2417
|
self.restorePreviewPreviousNextBundle()
|
|
1948
2418
|
self.restorePreviewPreviousAppId()
|
|
1949
|
-
self.restorePreviewPreviousDefaultChannel()
|
|
2419
|
+
if !self.restorePreviewPreviousDefaultChannel() {
|
|
2420
|
+
logger.warn("Default channel preview restore will retry on next launch")
|
|
2421
|
+
}
|
|
1950
2422
|
|
|
1951
2423
|
self.previewSessionEnabled = false
|
|
1952
2424
|
self.previewSessionAlertPending = false
|
|
@@ -1973,7 +2445,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1973
2445
|
|
|
1974
2446
|
self.restorePreviewPreviousNextBundle()
|
|
1975
2447
|
self.restorePreviewPreviousAppId()
|
|
1976
|
-
self.restorePreviewPreviousDefaultChannel()
|
|
2448
|
+
if !self.restorePreviewPreviousDefaultChannel() {
|
|
2449
|
+
logger.warn("Default channel preview restore will retry on next launch")
|
|
2450
|
+
}
|
|
1977
2451
|
self.previewSessionEnabled = false
|
|
1978
2452
|
self.previewSessionAlertPending = false
|
|
1979
2453
|
self.isLeavingPreviewForIncomingLink = false
|
|
@@ -2001,14 +2475,17 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2001
2475
|
}
|
|
2002
2476
|
|
|
2003
2477
|
private func clearPreviewSessionPreferences() {
|
|
2478
|
+
let keepDefaultChannelSnapshotFallback = self.hasPendingDefaultChannelPreviewSnapshot()
|
|
2004
2479
|
_ = self.implementation.setPreviewFallbackBundle(fallback: nil)
|
|
2005
2480
|
UserDefaults.standard.removeObject(forKey: self.previewSessionDefaultsKey)
|
|
2006
2481
|
UserDefaults.standard.removeObject(forKey: self.previewPreviousShakeMenuDefaultsKey)
|
|
2007
2482
|
UserDefaults.standard.removeObject(forKey: self.previewPreviousShakeChannelSelectorDefaultsKey)
|
|
2008
2483
|
UserDefaults.standard.removeObject(forKey: self.previewPreviousNextBundleDefaultsKey)
|
|
2009
2484
|
UserDefaults.standard.removeObject(forKey: self.previewPreviousAppIdDefaultsKey)
|
|
2010
|
-
|
|
2011
|
-
|
|
2485
|
+
if !keepDefaultChannelSnapshotFallback {
|
|
2486
|
+
UserDefaults.standard.removeObject(forKey: self.previewPreviousDefaultChannelDefaultsKey)
|
|
2487
|
+
UserDefaults.standard.removeObject(forKey: self.previewPreviousDefaultChannelWasSetDefaultsKey)
|
|
2488
|
+
}
|
|
2012
2489
|
UserDefaults.standard.removeObject(forKey: self.previewAppIdDefaultsKey)
|
|
2013
2490
|
UserDefaults.standard.removeObject(forKey: self.previewPayloadUrlDefaultsKey)
|
|
2014
2491
|
UserDefaults.standard.removeObject(forKey: self.previewNameDefaultsKey)
|
|
@@ -2026,21 +2503,95 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2026
2503
|
logger.info("Restored appId after preview: \(previousAppId)")
|
|
2027
2504
|
}
|
|
2028
2505
|
|
|
2029
|
-
private func restorePreviewPreviousDefaultChannel() {
|
|
2030
|
-
|
|
2031
|
-
|
|
2506
|
+
private func restorePreviewPreviousDefaultChannel() -> Bool {
|
|
2507
|
+
guard !defaultChannelCleanupMustRetry else {
|
|
2508
|
+
logger.warn("Skipping default channel preview restore until cleanup retries")
|
|
2509
|
+
return false
|
|
2510
|
+
}
|
|
2511
|
+
guard let stateFile = self.defaultChannelStateFile(),
|
|
2512
|
+
let previewSnapshotFile = self.defaultChannelPreviewSnapshotFile() else {
|
|
2513
|
+
return false
|
|
2514
|
+
}
|
|
2032
2515
|
|
|
2033
|
-
|
|
2034
|
-
|
|
2516
|
+
let previousDefaultChannel: String?
|
|
2517
|
+
switch self.defaultChannelPreviewSnapshot(file: previewSnapshotFile) {
|
|
2518
|
+
case .invalidated:
|
|
2519
|
+
return true
|
|
2520
|
+
case let .snapshot(channel):
|
|
2521
|
+
previousDefaultChannel = channel
|
|
2522
|
+
case .missing:
|
|
2523
|
+
let hadPreviousDefaultChannel = UserDefaults.standard.object(
|
|
2524
|
+
forKey: self.previewPreviousDefaultChannelWasSetDefaultsKey
|
|
2525
|
+
) as? Bool ?? false
|
|
2526
|
+
previousDefaultChannel = hadPreviousDefaultChannel
|
|
2527
|
+
? UserDefaults.standard.string(forKey: self.previewPreviousDefaultChannelDefaultsKey)
|
|
2528
|
+
: nil
|
|
2529
|
+
do {
|
|
2530
|
+
try self.persistDefaultChannelPreviewSnapshot(
|
|
2531
|
+
channel: previousDefaultChannel,
|
|
2532
|
+
file: previewSnapshotFile
|
|
2533
|
+
)
|
|
2534
|
+
} catch {
|
|
2535
|
+
logger.warn("Cannot migrate default channel preview snapshot: \(error.localizedDescription)")
|
|
2536
|
+
return false
|
|
2537
|
+
}
|
|
2538
|
+
case .unreadable:
|
|
2539
|
+
logger.warn("Cannot restore default channel from an unreadable preview snapshot")
|
|
2540
|
+
return false
|
|
2541
|
+
}
|
|
2542
|
+
|
|
2543
|
+
return self.persistRestoredDefaultChannelFromPreview(
|
|
2544
|
+
channel: previousDefaultChannel,
|
|
2545
|
+
stateFile: stateFile,
|
|
2546
|
+
previewSnapshotFile: previewSnapshotFile
|
|
2547
|
+
)
|
|
2548
|
+
}
|
|
2549
|
+
|
|
2550
|
+
private func applyRestoredDefaultChannelFromPreview(_ channel: String?) {
|
|
2551
|
+
let configDefaultChannel = self.getStringConfig("defaultChannel", defaultValue: "")
|
|
2552
|
+
if let channel = channel {
|
|
2553
|
+
UserDefaults.standard.set(channel, forKey: self.defaultChannelDefaultsKey)
|
|
2554
|
+
self.implementation.defaultChannel = channel
|
|
2555
|
+
logger.info("Restored defaultChannel after preview")
|
|
2556
|
+
} else {
|
|
2035
2557
|
UserDefaults.standard.removeObject(forKey: self.defaultChannelDefaultsKey)
|
|
2036
2558
|
self.implementation.defaultChannel = configDefaultChannel
|
|
2037
2559
|
logger.info("Restored defaultChannel after preview to config value")
|
|
2038
|
-
return
|
|
2039
2560
|
}
|
|
2561
|
+
}
|
|
2040
2562
|
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2563
|
+
private func persistRestoredDefaultChannelFromPreview(
|
|
2564
|
+
channel: String?,
|
|
2565
|
+
stateFile: URL,
|
|
2566
|
+
previewSnapshotFile: URL
|
|
2567
|
+
) -> Bool {
|
|
2568
|
+
defaultChannelStateLock.lock()
|
|
2569
|
+
defer { defaultChannelStateLock.unlock() }
|
|
2570
|
+
let didPersist: Bool
|
|
2571
|
+
do {
|
|
2572
|
+
try self.persistDefaultChannelStateWithoutLock(channel: channel, file: stateFile)
|
|
2573
|
+
try self.persistDefaultChannelPreviewSnapshotWithoutLock(
|
|
2574
|
+
channel: nil,
|
|
2575
|
+
isValid: false,
|
|
2576
|
+
file: previewSnapshotFile
|
|
2577
|
+
)
|
|
2578
|
+
didPersist = true
|
|
2579
|
+
} catch {
|
|
2580
|
+
let persistedState = self.defaultChannelStateWithoutLock(file: stateFile)
|
|
2581
|
+
let persistedSnapshot = self.defaultChannelPreviewSnapshotWithoutLock(file: previewSnapshotFile)
|
|
2582
|
+
if persistedState.exists,
|
|
2583
|
+
persistedState.isReadable,
|
|
2584
|
+
persistedState.channel == channel,
|
|
2585
|
+
persistedSnapshot == .invalidated {
|
|
2586
|
+
logger.warn("Default channel preview restore committed before metadata update failed")
|
|
2587
|
+
didPersist = true
|
|
2588
|
+
} else {
|
|
2589
|
+
logger.warn("Cannot durably restore default channel after preview: \(error.localizedDescription)")
|
|
2590
|
+
didPersist = false
|
|
2591
|
+
}
|
|
2592
|
+
}
|
|
2593
|
+
self.applyRestoredDefaultChannelFromPreview(channel)
|
|
2594
|
+
return didPersist
|
|
2044
2595
|
}
|
|
2045
2596
|
|
|
2046
2597
|
private func normalizedPreviewAppId(_ rawAppId: String?) -> String? {
|
|
@@ -2220,7 +2771,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2220
2771
|
self.shakeMenuGesture = Self.normalizedShakeMenuGesture(self.getStringConfig("shakeMenuGesture", defaultValue: Self.shakeMenuGestureShake))
|
|
2221
2772
|
self.syncShakeMenuGestureRecognizer()
|
|
2222
2773
|
self.restorePreviewPreviousAppId()
|
|
2223
|
-
self.restorePreviewPreviousDefaultChannel()
|
|
2774
|
+
if !self.restorePreviewPreviousDefaultChannel() {
|
|
2775
|
+
logger.warn("Default channel preview restore will retry on next launch")
|
|
2776
|
+
}
|
|
2224
2777
|
_ = self.implementation.setPreviewFallbackBundle(fallback: nil)
|
|
2225
2778
|
_ = self.implementation.setNextBundle(next: Optional<String>.none)
|
|
2226
2779
|
self.clearPreviewSessionPreferences()
|
|
@@ -2252,7 +2805,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2252
2805
|
guard self.previewSessionEnabled else {
|
|
2253
2806
|
return
|
|
2254
2807
|
}
|
|
2255
|
-
if let topVC = UIApplication.topViewController(),
|
|
2808
|
+
if let topVC = UIApplication.topViewController(self.bridge?.viewController),
|
|
2256
2809
|
topVC.isKind(of: UIAlertController.self) {
|
|
2257
2810
|
self.previewSessionAlertPending = true
|
|
2258
2811
|
UserDefaults.standard.set(true, forKey: self.previewSessionAlertPendingDefaultsKey)
|
|
@@ -2262,11 +2815,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2262
2815
|
|
|
2263
2816
|
let alert = UIAlertController(
|
|
2264
2817
|
title: "Preview started",
|
|
2265
|
-
message:
|
|
2818
|
+
message: self.shakeMenuGesture == Self.shakeMenuGestureThreeFingerPinch ? "Three-finger pinch to open menu." : "Shake to open menu.",
|
|
2266
2819
|
preferredStyle: .alert
|
|
2267
2820
|
)
|
|
2268
2821
|
alert.addAction(UIAlertAction(title: "Got it", style: .default))
|
|
2269
|
-
if let topVC = UIApplication.topViewController() {
|
|
2822
|
+
if let topVC = UIApplication.topViewController(self.bridge?.viewController) {
|
|
2270
2823
|
topVC.present(alert, animated: true)
|
|
2271
2824
|
} else {
|
|
2272
2825
|
self.previewSessionAlertPending = true
|
|
@@ -2465,6 +3018,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2465
3018
|
"error": res.error.contains("Channel URL") ? "missing_config" : "request_failed"
|
|
2466
3019
|
])
|
|
2467
3020
|
} else {
|
|
3021
|
+
guard self.persistDefaultChannelStateFromDefaults() else {
|
|
3022
|
+
self.rejectCall(call, message: "Channel override removed but local persistence failed", code: "UNSETCHANNEL_PERSISTENCE_FAILED")
|
|
3023
|
+
return
|
|
3024
|
+
}
|
|
2468
3025
|
if self._isAutoUpdateEnabled() && triggerAutoUpdate {
|
|
2469
3026
|
self.logger.info("Calling autoupdater after channel change!")
|
|
2470
3027
|
// Check if download is already in progress (with timeout protection)
|
|
@@ -2511,6 +3068,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2511
3068
|
"error": res.error.contains("Channel URL") ? "missing_config" : (res.error.contains("cannot_update_via_private_channel") || res.error.contains("channel_self_set_not_allowed")) ? "channel_private" : "request_failed"
|
|
2512
3069
|
])
|
|
2513
3070
|
} else {
|
|
3071
|
+
guard self.persistDefaultChannelStateFromDefaults() else {
|
|
3072
|
+
self.rejectCall(call, message: "Channel changed but local persistence failed", code: "SETCHANNEL_PERSISTENCE_FAILED")
|
|
3073
|
+
return
|
|
3074
|
+
}
|
|
2514
3075
|
if self._isAutoUpdateEnabled() && triggerAutoUpdate {
|
|
2515
3076
|
self.logger.info("Calling autoupdater after channel change!")
|
|
2516
3077
|
// Check if download is already in progress (with timeout protection)
|
|
@@ -2535,6 +3096,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2535
3096
|
"error": res.error.contains("Channel URL") ? "missing_config" : "request_failed"
|
|
2536
3097
|
])
|
|
2537
3098
|
} else {
|
|
3099
|
+
guard self.persistDefaultChannelStateFromDefaults() else {
|
|
3100
|
+
self.rejectCall(call, message: "Channel synchronized but local persistence failed", code: "GETCHANNEL_PERSISTENCE_FAILED")
|
|
3101
|
+
return
|
|
3102
|
+
}
|
|
2538
3103
|
self.resolveCall(call, data: res.toDict())
|
|
2539
3104
|
}
|
|
2540
3105
|
}
|
|
@@ -2674,10 +3239,67 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2674
3239
|
])
|
|
2675
3240
|
}
|
|
2676
3241
|
|
|
3242
|
+
private func reportAppLaunchStart() {
|
|
3243
|
+
guard !self.implementation.statsUrl.isEmpty, !launchStartReported else {
|
|
3244
|
+
return
|
|
3245
|
+
}
|
|
3246
|
+
|
|
3247
|
+
launchStartReported = true
|
|
3248
|
+
let current = self.implementation.getCurrentBundle()
|
|
3249
|
+
self.implementation.sendStats(
|
|
3250
|
+
action: "app_launch_start",
|
|
3251
|
+
versionName: current.getVersionName(),
|
|
3252
|
+
oldVersionName: "",
|
|
3253
|
+
metadata: [
|
|
3254
|
+
"launch_started_at": String(launchStartedAtMs),
|
|
3255
|
+
"source": "plugin_load"
|
|
3256
|
+
]
|
|
3257
|
+
)
|
|
3258
|
+
}
|
|
3259
|
+
|
|
3260
|
+
private func reportAppLaunchReady(_ bundle: BundleInfo) {
|
|
3261
|
+
guard !self.implementation.statsUrl.isEmpty, !launchReadyReported else {
|
|
3262
|
+
return
|
|
3263
|
+
}
|
|
3264
|
+
|
|
3265
|
+
launchReadyReported = true
|
|
3266
|
+
let duration = max(0, Int64(Date().timeIntervalSince1970 * 1000) - launchStartedAtMs)
|
|
3267
|
+
self.implementation.sendStats(
|
|
3268
|
+
action: "app_launch_ready",
|
|
3269
|
+
versionName: bundle.getVersionName(),
|
|
3270
|
+
oldVersionName: "",
|
|
3271
|
+
metadata: [
|
|
3272
|
+
"duration_ms": String(duration),
|
|
3273
|
+
"launch_started_at": String(launchStartedAtMs),
|
|
3274
|
+
"source": "notify_app_ready"
|
|
3275
|
+
]
|
|
3276
|
+
)
|
|
3277
|
+
}
|
|
3278
|
+
|
|
3279
|
+
private func reportAppLaunchTimeout(_ bundle: BundleInfo) {
|
|
3280
|
+
guard !self.implementation.statsUrl.isEmpty else {
|
|
3281
|
+
return
|
|
3282
|
+
}
|
|
3283
|
+
|
|
3284
|
+
let duration = max(0, Int64(Date().timeIntervalSince1970 * 1000) - launchStartedAtMs)
|
|
3285
|
+
self.implementation.sendStats(
|
|
3286
|
+
action: "app_launch_timeout",
|
|
3287
|
+
versionName: bundle.getVersionName(),
|
|
3288
|
+
oldVersionName: "",
|
|
3289
|
+
metadata: [
|
|
3290
|
+
"duration_ms": String(duration),
|
|
3291
|
+
"launch_started_at": String(launchStartedAtMs),
|
|
3292
|
+
"timeout_ms": String(appReadyTimeout),
|
|
3293
|
+
"source": "app_ready_timeout"
|
|
3294
|
+
]
|
|
3295
|
+
)
|
|
3296
|
+
}
|
|
3297
|
+
|
|
2677
3298
|
@objc func notifyAppReady(_ call: CAPPluginCall) {
|
|
2678
3299
|
self.semaphoreDown()
|
|
2679
3300
|
let bundle = self.implementation.getCurrentBundle()
|
|
2680
3301
|
self.implementation.setSuccess(bundle: bundle, autoDeletePrevious: self.autoDeletePrevious)
|
|
3302
|
+
self.reportAppLaunchReady(bundle)
|
|
2681
3303
|
logger.info("Current bundle loaded successfully. [notifyAppReady was called] \(bundle.toString())")
|
|
2682
3304
|
self.clearIncomingPreviewTransition()
|
|
2683
3305
|
self.hidePreviewTransitionLoader(reason: "notify-app-ready")
|
|
@@ -2785,16 +3407,26 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2785
3407
|
"bundle": current.toJSON()
|
|
2786
3408
|
])
|
|
2787
3409
|
self.persistLastFailedBundle(current)
|
|
3410
|
+
self.reportAppLaunchTimeout(current)
|
|
2788
3411
|
self.implementation.sendStats(action: "update_fail", versionName: current.getVersionName())
|
|
2789
3412
|
self.implementation.setError(bundle: current)
|
|
2790
3413
|
_ = self.performReset(toLastSuccessful: true, usePendingBundle: false, isInternal: true)
|
|
2791
3414
|
if self.autoDeleteFailed && !current.isBuiltin() {
|
|
3415
|
+
let failedId = current.getId()
|
|
2792
3416
|
logger.info("Deleting failing bundle: \(current.toString())")
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
3417
|
+
// Mark before async work so kill/OOM still resumes via drainPendingDeletes.
|
|
3418
|
+
self.implementation.saveBundleInfo(
|
|
3419
|
+
id: failedId,
|
|
3420
|
+
bundle: current.setStatus(status: BundleStatus.DELETING.storedValue)
|
|
3421
|
+
)
|
|
3422
|
+
UserDefaults.standard.synchronize()
|
|
3423
|
+
DispatchQueue.global(qos: .utility).async {
|
|
3424
|
+
let res = self.implementation.delete(id: failedId, removeInfo: false)
|
|
3425
|
+
if res {
|
|
3426
|
+
self.logger.info("Failed bundle deleted: \(failedId)")
|
|
3427
|
+
} else {
|
|
3428
|
+
self.logger.error("Failed to delete failed bundle: \(failedId)")
|
|
3429
|
+
}
|
|
2798
3430
|
}
|
|
2799
3431
|
}
|
|
2800
3432
|
} else {
|
|
@@ -2819,7 +3451,12 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2819
3451
|
func sendReadyToJs(current: BundleInfo, msg: String) {
|
|
2820
3452
|
logger.info("sendReadyToJs")
|
|
2821
3453
|
DispatchQueue.global().async {
|
|
2822
|
-
|
|
3454
|
+
// Only wait after load()/reload() armed the semaphore. Foreground resumes
|
|
3455
|
+
// with autoUpdate disabled call sendReadyToJs again without a fresh
|
|
3456
|
+
// notifyAppReady(), so waiting there always timed out after appReadyTimeout.
|
|
3457
|
+
if self.consumePendingNotifyAppReady() {
|
|
3458
|
+
self.semaphoreWait(waitTime: self.appReadyTimeout)
|
|
3459
|
+
}
|
|
2823
3460
|
self.notifyListeners("appReady", data: ["bundle": current.toJSON(), "status": msg], retainUntilConsumed: true)
|
|
2824
3461
|
|
|
2825
3462
|
// Auto hide splashscreen if enabled
|
|
@@ -2831,6 +3468,16 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
2831
3468
|
}
|
|
2832
3469
|
}
|
|
2833
3470
|
|
|
3471
|
+
private func consumePendingNotifyAppReady() -> Bool {
|
|
3472
|
+
let shouldWait = pendingNotifyAppReady
|
|
3473
|
+
pendingNotifyAppReady = false
|
|
3474
|
+
return shouldWait
|
|
3475
|
+
}
|
|
3476
|
+
|
|
3477
|
+
private func clearPendingNotifyAppReady() {
|
|
3478
|
+
pendingNotifyAppReady = false
|
|
3479
|
+
}
|
|
3480
|
+
|
|
2834
3481
|
private func hideSplashscreen() {
|
|
2835
3482
|
if Thread.isMainThread {
|
|
2836
3483
|
self.performHideSplashscreen()
|
|
@@ -3382,6 +4029,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3382
4029
|
isAutoUpdateModeEnabled(autoUpdateMode) && autoUpdateMode != autoUpdateModeOnlyDownload
|
|
3383
4030
|
}
|
|
3384
4031
|
|
|
4032
|
+
/// Incomplete leftovers must be downloaded again. Android already skips
|
|
4033
|
+
/// `DOWNLOADING` here; iOS used to treat them as ready, then `setNextBundle`
|
|
4034
|
+
/// failed quietly and the update never applied.
|
|
4035
|
+
static func shouldRetryDownloadForExistingBundle(_ bundle: BundleInfo) -> Bool {
|
|
4036
|
+
bundle.isDeleted() || bundle.isDeleting() || bundle.isDownloading()
|
|
4037
|
+
}
|
|
4038
|
+
|
|
3385
4039
|
static func isDirectUpdateMode(_ directUpdateMode: String) -> Bool {
|
|
3386
4040
|
directUpdateMode == autoUpdateModeInstall || directUpdateMode == autoUpdateModeLaunch || directUpdateMode == autoUpdateModeAlways
|
|
3387
4041
|
}
|
|
@@ -3444,6 +4098,22 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3444
4098
|
self.currentBuildVersion = currentBuildVersion
|
|
3445
4099
|
}
|
|
3446
4100
|
|
|
4101
|
+
func setAppReadyTimeoutForTesting(_ timeout: Int) {
|
|
4102
|
+
self.appReadyTimeout = timeout
|
|
4103
|
+
}
|
|
4104
|
+
|
|
4105
|
+
func armPendingNotifyAppReadyForTesting() {
|
|
4106
|
+
pendingNotifyAppReady = true
|
|
4107
|
+
}
|
|
4108
|
+
|
|
4109
|
+
func clearPendingNotifyAppReadyForTesting() {
|
|
4110
|
+
clearPendingNotifyAppReady()
|
|
4111
|
+
}
|
|
4112
|
+
|
|
4113
|
+
var isPendingNotifyAppReadyForTesting: Bool {
|
|
4114
|
+
pendingNotifyAppReady
|
|
4115
|
+
}
|
|
4116
|
+
|
|
3447
4117
|
func shouldUseDirectUpdateForTesting() -> Bool {
|
|
3448
4118
|
self.shouldUseDirectUpdate()
|
|
3449
4119
|
}
|
|
@@ -3593,7 +4263,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3593
4263
|
func runBackgroundDownloadWork(_ work: @escaping () -> Void) {
|
|
3594
4264
|
// Live update checks/downloads are user-visible work. Using `.background`
|
|
3595
4265
|
// lets the scheduler starve them for minutes while the app is active.
|
|
3596
|
-
DispatchQueue.global(qos: .
|
|
4266
|
+
DispatchQueue.global(qos: .userInitiated).async(execute: work)
|
|
3597
4267
|
}
|
|
3598
4268
|
|
|
3599
4269
|
private func beginDownloadBackgroundTask() {
|
|
@@ -3644,8 +4314,6 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3644
4314
|
}
|
|
3645
4315
|
|
|
3646
4316
|
self.runBackgroundDownloadWork {
|
|
3647
|
-
// Wait for cleanup to complete before starting download
|
|
3648
|
-
self.waitForCleanupIfNeeded()
|
|
3649
4317
|
if self.shouldBlockAutoUpdateForPreviewSession() {
|
|
3650
4318
|
self.clearDownloadInProgressState()
|
|
3651
4319
|
return
|
|
@@ -3653,7 +4321,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3653
4321
|
self.beginDownloadBackgroundTask()
|
|
3654
4322
|
self.logger.info("Check for update via \(self.updateUrl)")
|
|
3655
4323
|
let res = self.implementation.getLatest(url: url, channel: nil)
|
|
3656
|
-
|
|
4324
|
+
var current = self.implementation.getCurrentBundle()
|
|
3657
4325
|
if self.shouldBlockAutoUpdateForPreviewSession() {
|
|
3658
4326
|
self.clearDownloadInProgressState()
|
|
3659
4327
|
self.endBackGroundTask()
|
|
@@ -3672,6 +4340,16 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3672
4340
|
)
|
|
3673
4341
|
return
|
|
3674
4342
|
}
|
|
4343
|
+
// File mutations wait here. getLatest already ran in parallel with cleanup.
|
|
4344
|
+
do {
|
|
4345
|
+
try self.waitForCleanupIfNeeded()
|
|
4346
|
+
} catch {
|
|
4347
|
+
self.logger.error("Cleanup still running, skipping download")
|
|
4348
|
+
self.clearDownloadInProgressState()
|
|
4349
|
+
self.endBackGroundTask()
|
|
4350
|
+
return
|
|
4351
|
+
}
|
|
4352
|
+
current = self.implementation.getCurrentBundle()
|
|
3675
4353
|
if res.version == "builtin" {
|
|
3676
4354
|
self.logger.info("Latest version is builtin")
|
|
3677
4355
|
let directUpdateAllowed = plannedDirectUpdate && !self.autoSplashscreenTimedOut
|
|
@@ -3733,14 +4411,16 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3733
4411
|
do {
|
|
3734
4412
|
self.logger.info("New bundle: \(latestVersionName) found. Current is: \(current.getVersionName()). \(messageUpdate)")
|
|
3735
4413
|
var nextImpl = self.implementation.getBundleInfoByVersionName(version: latestVersionName)
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
4414
|
+
let needsDownload = nextImpl.map(Self.shouldRetryDownloadForExistingBundle) ?? true
|
|
4415
|
+
if needsDownload {
|
|
4416
|
+
if let existing = nextImpl {
|
|
4417
|
+
self.logger.info("Latest bundle already exists in incomplete state (\(existing.getStatus())) and will be deleted, download will overwrite it.")
|
|
4418
|
+
_ = self.implementation.setNextBundle(next: Optional<String>.none)
|
|
4419
|
+
let deleted = self.implementation.delete(id: existing.getId(), removeInfo: true)
|
|
4420
|
+
if deleted {
|
|
4421
|
+
self.logger.info("Incomplete bundle deleted: \(existing.toString())")
|
|
3742
4422
|
} else {
|
|
3743
|
-
self.logger.error("Failed to delete
|
|
4423
|
+
self.logger.error("Failed to delete incomplete bundle: \(existing.toString())")
|
|
3744
4424
|
}
|
|
3745
4425
|
}
|
|
3746
4426
|
self.consumeOnLaunchDirectUpdateAttempt(plannedDirectUpdate: plannedDirectUpdate)
|
|
@@ -3827,8 +4507,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3827
4507
|
error: false,
|
|
3828
4508
|
plannedDirectUpdate: plannedDirectUpdate
|
|
3829
4509
|
)
|
|
3830
|
-
} else {
|
|
3831
|
-
_ = self.implementation.setNextBundle(next: next.getId())
|
|
4510
|
+
} else if self.queueBundleForNextBackgroundInstall(next) {
|
|
3832
4511
|
self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()])
|
|
3833
4512
|
self.endBackGroundTaskWithNotif(
|
|
3834
4513
|
msg: "Direct update reload failed, update will install next background",
|
|
@@ -3837,20 +4516,35 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
3837
4516
|
error: false,
|
|
3838
4517
|
plannedDirectUpdate: plannedDirectUpdate
|
|
3839
4518
|
)
|
|
4519
|
+
} else {
|
|
4520
|
+
self.endBackGroundTaskWithNotif(
|
|
4521
|
+
msg: "Direct update reload failed, and next bundle could not be queued",
|
|
4522
|
+
latestVersionName: latestVersionName,
|
|
4523
|
+
current: current,
|
|
4524
|
+
plannedDirectUpdate: plannedDirectUpdate
|
|
4525
|
+
)
|
|
3840
4526
|
}
|
|
3841
4527
|
} else if self.shouldAutoSetNextBundle() {
|
|
3842
4528
|
if plannedDirectUpdate && !directUpdateAllowed {
|
|
3843
4529
|
self.logger.info("Direct update skipped because splashscreen timeout occurred. Update will install on next app background.")
|
|
3844
4530
|
}
|
|
3845
|
-
self.
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
4531
|
+
if self.queueBundleForNextBackgroundInstall(next) {
|
|
4532
|
+
self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()])
|
|
4533
|
+
self.endBackGroundTaskWithNotif(
|
|
4534
|
+
msg: "update downloaded, will install next background",
|
|
4535
|
+
latestVersionName: latestVersionName,
|
|
4536
|
+
current: current,
|
|
4537
|
+
error: false,
|
|
4538
|
+
plannedDirectUpdate: plannedDirectUpdate
|
|
4539
|
+
)
|
|
4540
|
+
} else {
|
|
4541
|
+
self.endBackGroundTaskWithNotif(
|
|
4542
|
+
msg: "Update downloaded, but next bundle could not be queued",
|
|
4543
|
+
latestVersionName: latestVersionName,
|
|
4544
|
+
current: current,
|
|
4545
|
+
plannedDirectUpdate: plannedDirectUpdate
|
|
4546
|
+
)
|
|
4547
|
+
}
|
|
3854
4548
|
} else {
|
|
3855
4549
|
self.logger.info("autoUpdate is set to onlyDownload, downloaded update will not be set as next bundle")
|
|
3856
4550
|
self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()], retainUntilConsumed: true)
|
|
@@ -4014,6 +4708,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
4014
4708
|
|
|
4015
4709
|
let current: BundleInfo = self.implementation.getCurrentBundle()
|
|
4016
4710
|
self.implementation.sendStats(action: "app_moved_to_background", versionName: current.getVersionName())
|
|
4711
|
+
self.implementation.persistPendingStats()
|
|
4017
4712
|
logger.info("Check for pending update")
|
|
4018
4713
|
|
|
4019
4714
|
// Show splashscreen only if autoSplashscreen is enabled AND autoUpdate is enabled AND directUpdate would be used
|