@capgo/capacitor-updater 8.54.2 → 9.0.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/CapgoCapacitorUpdater.podspec +2 -2
- package/Package.swift +3 -4
- package/README.md +162 -417
- package/android/build.gradle +7 -7
- package/android/consumer-rules.pro +48 -0
- package/android/proguard-rules.pro +1 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +1420 -404
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +1100 -308
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +102 -65
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +155 -18
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +22 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +82 -21
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +356 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +553 -0
- package/dist/docs.json +44 -566
- package/dist/esm/definitions.d.ts +97 -215
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +197 -84
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +402 -139
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +81 -55
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +24 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +2 -1
- package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +3905 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +151 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +249 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +528 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1095 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +192 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +454 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
- package/package.json +15 -13
|
@@ -34,6 +34,7 @@ import java.io.FileNotFoundException;
|
|
|
34
34
|
import java.io.FileOutputStream;
|
|
35
35
|
import java.io.FilenameFilter;
|
|
36
36
|
import java.io.IOException;
|
|
37
|
+
import java.math.BigInteger;
|
|
37
38
|
import java.nio.charset.StandardCharsets;
|
|
38
39
|
import java.nio.file.Files;
|
|
39
40
|
import java.security.SecureRandom;
|
|
@@ -50,6 +51,7 @@ import java.util.Set;
|
|
|
50
51
|
import java.util.concurrent.CompletableFuture;
|
|
51
52
|
import java.util.concurrent.ConcurrentHashMap;
|
|
52
53
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
54
|
+
import java.util.concurrent.CountDownLatch;
|
|
53
55
|
import java.util.concurrent.ExecutorService;
|
|
54
56
|
import java.util.concurrent.Executors;
|
|
55
57
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
@@ -58,6 +60,7 @@ import java.util.concurrent.ScheduledFuture;
|
|
|
58
60
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
59
61
|
import java.util.concurrent.TimeUnit;
|
|
60
62
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
63
|
+
import java.util.concurrent.atomic.AtomicReference;
|
|
61
64
|
import java.util.zip.ZipEntry;
|
|
62
65
|
import java.util.zip.ZipInputStream;
|
|
63
66
|
import okhttp3.*;
|
|
@@ -77,15 +80,43 @@ public class CapgoUpdater {
|
|
|
77
80
|
|
|
78
81
|
private static final String FALLBACK_VERSION = "pastVersion";
|
|
79
82
|
private static final String NEXT_VERSION = "nextVersion";
|
|
83
|
+
private static final Object nextBundleLock = new Object();
|
|
80
84
|
private static final String PREVIEW_FALLBACK_VERSION = "previewFallbackVersion";
|
|
81
85
|
private static final String PENDING_DELETE_IDS = "pendingDeleteIds";
|
|
86
|
+
private static final String PENDING_DELETE_NO_CANCEL_IDS = "pendingDeleteNoCancelIds";
|
|
87
|
+
private static final Object pendingDeleteLock = new Object();
|
|
82
88
|
private static final String bundleDirectory = "versions";
|
|
83
89
|
private static final String TEMP_UNZIP_PREFIX = "capgo_unzip_";
|
|
84
90
|
static final String MANIFEST_STAGING_PREFIX = "capgo_manifest_";
|
|
85
91
|
static final String MANIFEST_DEST_SUFFIX = "_manifest_dest";
|
|
86
92
|
private static final String LEGACY_MANIFEST_STAGING_FOLDER_PATTERN = "^[0-9A-Za-z]{10}$";
|
|
87
93
|
private static final long DELETE_PACE_MS = 75L;
|
|
88
|
-
private final Object deleteLock = new Object();
|
|
94
|
+
private static final Object deleteLock = new Object();
|
|
95
|
+
private static final Set<File> activeBundleDeletes = ConcurrentHashMap.newKeySet();
|
|
96
|
+
// Plugin and headless observers share storage, including work queued before handover.
|
|
97
|
+
private static final Map<File, Set<DownloadFinalization>> downloadFinalizations = new HashMap<>();
|
|
98
|
+
|
|
99
|
+
private enum DeletionDownloadPolicy {
|
|
100
|
+
CANCEL,
|
|
101
|
+
KEEP,
|
|
102
|
+
RESUME
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private static final class DownloadFinalization {
|
|
106
|
+
|
|
107
|
+
final File bundle;
|
|
108
|
+
final boolean headless;
|
|
109
|
+
final CountDownLatch finished = new CountDownLatch(1);
|
|
110
|
+
volatile boolean cancelled;
|
|
111
|
+
boolean automaticInstallAllowed = true;
|
|
112
|
+
Thread running;
|
|
113
|
+
|
|
114
|
+
DownloadFinalization(final File bundle, final boolean headless) {
|
|
115
|
+
this.bundle = bundle;
|
|
116
|
+
this.headless = headless;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
89
120
|
private static final String BACKGROUND_RUNNER_CONFIG_KEY = "BackgroundRunner";
|
|
90
121
|
private static final String BACKGROUND_RUNNER_WORKER_CLASS = "io.ionic.backgroundrunner.plugin.RunnerWorker";
|
|
91
122
|
|
|
@@ -118,8 +149,9 @@ public class CapgoUpdater {
|
|
|
118
149
|
public String publicKey = "";
|
|
119
150
|
public String deviceID = "";
|
|
120
151
|
public int timeout = 20000;
|
|
121
|
-
/** Refuse bundles whose version is below the native app version (versionBuild). Config builtinMinimum, default
|
|
122
|
-
public volatile boolean builtinMinimum =
|
|
152
|
+
/** Refuse bundles whose version is below the native app version (versionBuild). Config builtinMinimum, default true. */
|
|
153
|
+
public volatile boolean builtinMinimum = true;
|
|
154
|
+
private final AtomicReference<String> lastVersionFloorRejection = new AtomicReference<>();
|
|
123
155
|
/**
|
|
124
156
|
* Require https for every URL the updater downloads from. Config httpsOnly, default true. Mirrors
|
|
125
157
|
* DownloadService.httpsOnly (the OkHttp interceptor); this field gates URLs before any network IO.
|
|
@@ -196,6 +228,8 @@ public class CapgoUpdater {
|
|
|
196
228
|
// Versions whose WorkManager download is observed by this process. WorkManager persists work across a
|
|
197
229
|
// process kill, but the observer that finishes the download does not survive it.
|
|
198
230
|
private final Set<String> observedDownloadVersions = ConcurrentHashMap.newKeySet();
|
|
231
|
+
private final Map<String, BackgroundDownloadContext> backgroundDownloadContexts = new ConcurrentHashMap<>();
|
|
232
|
+
private final Object backgroundDownloadRegistrationLock = new Object();
|
|
199
233
|
private final ExecutorService io = createIoExecutor();
|
|
200
234
|
|
|
201
235
|
private static ExecutorService createIoExecutor() {
|
|
@@ -320,9 +354,94 @@ public class CapgoUpdater {
|
|
|
320
354
|
|
|
321
355
|
void directUpdateFinish(final BundleInfo latest) {}
|
|
322
356
|
|
|
357
|
+
/** One automatic check's install plan; its identity must survive download retries and later checks. */
|
|
358
|
+
static final class BackgroundDownloadContext {
|
|
359
|
+
|
|
360
|
+
final long timeoutGeneration;
|
|
361
|
+
final long checkSequence;
|
|
362
|
+
final boolean directUpdate;
|
|
363
|
+
volatile boolean deferredForRetry;
|
|
364
|
+
|
|
365
|
+
BackgroundDownloadContext(final long timeoutGeneration, final boolean directUpdate) {
|
|
366
|
+
this(timeoutGeneration, directUpdate, 0);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
BackgroundDownloadContext(final long timeoutGeneration, final boolean directUpdate, final long checkSequence) {
|
|
370
|
+
this.checkSequence = checkSequence;
|
|
371
|
+
this.timeoutGeneration = timeoutGeneration;
|
|
372
|
+
this.directUpdate = directUpdate;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static final class BackgroundDownloadResult {
|
|
377
|
+
|
|
378
|
+
enum Status {
|
|
379
|
+
STARTED,
|
|
380
|
+
REUSED,
|
|
381
|
+
NOT_STARTED
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
static final BackgroundDownloadResult REUSED = new BackgroundDownloadResult(Status.REUSED, null);
|
|
385
|
+
static final BackgroundDownloadResult NOT_STARTED = new BackgroundDownloadResult(Status.NOT_STARTED, null);
|
|
386
|
+
|
|
387
|
+
final Status status;
|
|
388
|
+
final String bundleId;
|
|
389
|
+
|
|
390
|
+
private BackgroundDownloadResult(final Status status, final String bundleId) {
|
|
391
|
+
this.status = status;
|
|
392
|
+
this.bundleId = bundleId;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
static BackgroundDownloadResult started(final String bundleId) {
|
|
396
|
+
return new BackgroundDownloadResult(Status.STARTED, bundleId);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
void directUpdateFinish(final BundleInfo latest, final BackgroundDownloadContext downloadContext) {
|
|
401
|
+
this.directUpdateFinish(latest);
|
|
402
|
+
}
|
|
403
|
+
|
|
323
404
|
/** Launch downloads have no waiter. The plugin emits appReady from here when WorkManager settles. */
|
|
324
405
|
void backgroundDownloadSettled(final BundleInfo bundle, final String status) {}
|
|
325
406
|
|
|
407
|
+
void backgroundDownloadSettled(final BundleInfo bundle, final String status, final BackgroundDownloadContext downloadContext) {
|
|
408
|
+
this.backgroundDownloadSettled(bundle, status);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
void backgroundDownloadStarted(final BackgroundDownloadContext downloadContext) {}
|
|
412
|
+
|
|
413
|
+
void backgroundDownloadReused(final BackgroundDownloadContext original, final BackgroundDownloadContext replacement) {}
|
|
414
|
+
|
|
415
|
+
boolean reuseBackgroundDownload(final String version, final BackgroundDownloadContext replacement) {
|
|
416
|
+
final BackgroundDownloadContext original = this.backgroundDownloadContexts.get(version);
|
|
417
|
+
if (original == null) {
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
// Serialize the ownership handoff with terminal dispatch, without holding the plugin monitor while waiting.
|
|
421
|
+
synchronized (original) {
|
|
422
|
+
if (this.backgroundDownloadContexts.get(version) != original) {
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
this.backgroundDownloadReused(original, replacement);
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private void completeBackgroundDownload(
|
|
431
|
+
final String version,
|
|
432
|
+
final BackgroundDownloadContext downloadContext,
|
|
433
|
+
final Runnable completion
|
|
434
|
+
) {
|
|
435
|
+
if (downloadContext == null) {
|
|
436
|
+
completion.run();
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
synchronized (downloadContext) {
|
|
440
|
+
this.backgroundDownloadContexts.remove(version, downloadContext);
|
|
441
|
+
completion.run();
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
326
445
|
void notifyListeners(final String id, final Map<String, Object> res) {}
|
|
327
446
|
|
|
328
447
|
/** A downloaded bundle was stored as next bundle and waits for its install moment. */
|
|
@@ -707,34 +826,86 @@ public class CapgoUpdater {
|
|
|
707
826
|
return 0;
|
|
708
827
|
}
|
|
709
828
|
|
|
710
|
-
|
|
711
|
-
|
|
829
|
+
/** Parses major, minor, patch as arbitrary-precision integers; returns null when the core is not numeric. */
|
|
830
|
+
static BigInteger[] parseCoreVersionComponents(final String version) {
|
|
831
|
+
final String core = stripBuildMetadata(version);
|
|
832
|
+
if (core == null || core.isEmpty() || !Character.isDigit(core.charAt(0))) {
|
|
833
|
+
return null;
|
|
834
|
+
}
|
|
835
|
+
final String[] tokens = core.replaceAll("\\s", "").split("\\.");
|
|
836
|
+
final BigInteger[] components = new BigInteger[3];
|
|
837
|
+
for (int i = 0; i < 3; i++) {
|
|
838
|
+
if (i < tokens.length) {
|
|
839
|
+
final String token = tokens[i];
|
|
840
|
+
int end = 0;
|
|
841
|
+
while (end < token.length() && Character.isDigit(token.charAt(end))) {
|
|
842
|
+
end++;
|
|
843
|
+
}
|
|
844
|
+
if (end == 0) {
|
|
845
|
+
components[i] = BigInteger.ZERO;
|
|
846
|
+
} else {
|
|
847
|
+
try {
|
|
848
|
+
components[i] = new BigInteger(token.substring(0, end));
|
|
849
|
+
} catch (final NumberFormatException e) {
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
} else {
|
|
854
|
+
components[i] = BigInteger.ZERO;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return components;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Compares major.minor.patch without {@link Long} limits. Returns {@code Integer.MIN_VALUE} when either
|
|
862
|
+
* version core is not parseable.
|
|
863
|
+
*/
|
|
864
|
+
static int compareCoreVersionStrings(final String left, final String right) {
|
|
865
|
+
final BigInteger[] a = parseCoreVersionComponents(left);
|
|
866
|
+
final BigInteger[] b = parseCoreVersionComponents(right);
|
|
867
|
+
if (a == null || b == null) {
|
|
868
|
+
return Integer.MIN_VALUE;
|
|
869
|
+
}
|
|
870
|
+
for (int i = 0; i < 3; i++) {
|
|
871
|
+
final int cmp = a[i].compareTo(b[i]);
|
|
872
|
+
if (cmp != 0) {
|
|
873
|
+
return cmp;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return 0;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
private void logBuiltinMinimumParseSkipped(final String version) {
|
|
880
|
+
logger.debug("builtinMinimum skipped: cannot parse bundle version '" + version + "' or native version '" + this.versionBuild + "'");
|
|
712
881
|
}
|
|
713
882
|
|
|
714
883
|
/**
|
|
715
884
|
* builtinMinimum gate: a bundle may never roll the app below its native version. Skipped (debug log) when
|
|
716
|
-
* either version string does not parse, so unversioned bundles keep working.
|
|
885
|
+
* either version string does not parse, so unversioned bundles keep working. Consecutive identical
|
|
886
|
+
* rejections report one stat per updater instance; every attempt still validates and throws.
|
|
717
887
|
*/
|
|
718
888
|
void requireVersionNotBelowBuiltin(final String version) throws IOException {
|
|
719
889
|
if (!this.builtinMinimum) {
|
|
890
|
+
this.lastVersionFloorRejection.set(null);
|
|
720
891
|
return;
|
|
721
892
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
logger.debug(
|
|
727
|
-
"builtinMinimum skipped: cannot parse bundle version '" + version + "' or native version '" + this.versionBuild + "'"
|
|
728
|
-
);
|
|
893
|
+
final int cmp = compareCoreVersionStrings(version, this.versionBuild);
|
|
894
|
+
if (cmp == Integer.MIN_VALUE) {
|
|
895
|
+
this.lastVersionFloorRejection.set(null);
|
|
896
|
+
logBuiltinMinimumParseSkipped(version);
|
|
729
897
|
return;
|
|
730
898
|
}
|
|
731
899
|
// Only major.minor.patch matter: prerelease suffixes and a fourth component are not a rollback.
|
|
732
|
-
if (
|
|
900
|
+
if (cmp < 0) {
|
|
733
901
|
final String message = "Bundle version " + version + " is below native version " + this.versionBuild + " (builtinMinimum)";
|
|
734
902
|
logger.error(message);
|
|
735
|
-
this.
|
|
903
|
+
if (!message.equals(this.lastVersionFloorRejection.getAndSet(message))) {
|
|
904
|
+
this.sendStats("version_below_native", version);
|
|
905
|
+
}
|
|
736
906
|
throw new IOException(message);
|
|
737
907
|
}
|
|
908
|
+
this.lastVersionFloorRejection.set(null);
|
|
738
909
|
}
|
|
739
910
|
|
|
740
911
|
/** Pre-network download gates shared by every entry point: httpsOnly and builtinMinimum. */
|
|
@@ -1151,13 +1322,14 @@ public class CapgoUpdater {
|
|
|
1151
1322
|
final boolean directInstall,
|
|
1152
1323
|
final boolean previewSession,
|
|
1153
1324
|
final boolean setNext,
|
|
1154
|
-
final BundleInfo bundle
|
|
1325
|
+
final BundleInfo bundle,
|
|
1326
|
+
final BackgroundDownloadContext downloadContext
|
|
1155
1327
|
) {
|
|
1156
1328
|
if (!shouldNotifyLaunchDownloadReady(awaitedByCaller, success, directInstall, previewSession)) {
|
|
1157
1329
|
return;
|
|
1158
1330
|
}
|
|
1159
1331
|
final BundleInfo readyBundle = bundle != null ? bundle : this.getCurrentBundle();
|
|
1160
|
-
this.backgroundDownloadSettled(readyBundle, launchDownloadReadyStatus(success, setNext));
|
|
1332
|
+
this.backgroundDownloadSettled(readyBundle, launchDownloadReadyStatus(success, setNext), downloadContext);
|
|
1161
1333
|
}
|
|
1162
1334
|
|
|
1163
1335
|
// Created on first use: plain JVM unit tests construct CapgoUpdater without a main Looper.
|
|
@@ -1170,19 +1342,173 @@ public class CapgoUpdater {
|
|
|
1170
1342
|
return handler;
|
|
1171
1343
|
}
|
|
1172
1344
|
|
|
1345
|
+
private DownloadFinalization registerDownloadFinalization(final String id, final boolean headless) {
|
|
1346
|
+
final File bundle;
|
|
1347
|
+
try {
|
|
1348
|
+
bundle = this.getBundleDirectory(id);
|
|
1349
|
+
} catch (IOException e) {
|
|
1350
|
+
throw new IllegalArgumentException("Invalid download bundle id", e);
|
|
1351
|
+
}
|
|
1352
|
+
synchronized (deleteLock) {
|
|
1353
|
+
final DownloadFinalization finalization = new DownloadFinalization(bundle, headless);
|
|
1354
|
+
final BundleInfo info = this.getBundleInfo(id);
|
|
1355
|
+
finalization.cancelled =
|
|
1356
|
+
activeBundleDeletes.contains(bundle) || !this.hasStoredBundleInfo(id) || info.isDeleting() || info.isDeleted();
|
|
1357
|
+
if (!finalization.cancelled) {
|
|
1358
|
+
downloadFinalizations.computeIfAbsent(bundle, (unused) -> new HashSet<>()).add(finalization);
|
|
1359
|
+
}
|
|
1360
|
+
return finalization;
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
private static void retireDownloadFinalization(final DownloadFinalization finalization) {
|
|
1365
|
+
synchronized (deleteLock) {
|
|
1366
|
+
final Set<DownloadFinalization> pending = downloadFinalizations.get(finalization.bundle);
|
|
1367
|
+
if (pending != null) {
|
|
1368
|
+
pending.remove(finalization);
|
|
1369
|
+
if (pending.isEmpty()) {
|
|
1370
|
+
downloadFinalizations.remove(finalization.bundle);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
finalization.running = null;
|
|
1374
|
+
finalization.finished.countDown();
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
boolean admitBuiltinInstall(final java.util.function.BooleanSupplier admitInstall) {
|
|
1379
|
+
final File bundles;
|
|
1380
|
+
try {
|
|
1381
|
+
bundles = this.documentsDir == null ? null : new File(this.documentsDir, bundleDirectory).getCanonicalFile();
|
|
1382
|
+
} catch (IOException e) {
|
|
1383
|
+
logger.error("Cannot resolve builtin install storage");
|
|
1384
|
+
return false;
|
|
1385
|
+
}
|
|
1386
|
+
synchronized (deleteLock) {
|
|
1387
|
+
synchronized (nextBundleLock) {
|
|
1388
|
+
if (!admitInstall.getAsBoolean()) {
|
|
1389
|
+
return false;
|
|
1390
|
+
}
|
|
1391
|
+
for (final Map.Entry<File, Set<DownloadFinalization>> entry : downloadFinalizations.entrySet()) {
|
|
1392
|
+
if (entry.getKey().getParentFile().equals(bundles)) {
|
|
1393
|
+
for (final DownloadFinalization finalization : entry.getValue()) {
|
|
1394
|
+
if (finalization.headless) {
|
|
1395
|
+
finalization.automaticInstallAllowed = false;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
return true;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
private void completeContextlessDownload(final DownloadFinalization finalization, final Runnable completion) {
|
|
1406
|
+
if (finalization == null || !finalization.headless) {
|
|
1407
|
+
completion.run();
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
1410
|
+
// Serialize queue publication and headless installation with builtin admission across instances.
|
|
1411
|
+
synchronized (deleteLock) {
|
|
1412
|
+
if (!finalization.cancelled && finalization.automaticInstallAllowed) {
|
|
1413
|
+
completion.run();
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
private void dispatchDownloadFinalization(
|
|
1419
|
+
final DownloadFinalization finalization,
|
|
1420
|
+
final Runnable completion,
|
|
1421
|
+
final Runnable cancelled
|
|
1422
|
+
) {
|
|
1423
|
+
io.execute(() -> {
|
|
1424
|
+
final boolean admitted;
|
|
1425
|
+
synchronized (deleteLock) {
|
|
1426
|
+
admitted = !finalization.cancelled;
|
|
1427
|
+
if (admitted) {
|
|
1428
|
+
finalization.running = Thread.currentThread();
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
try {
|
|
1432
|
+
if (admitted) {
|
|
1433
|
+
completion.run();
|
|
1434
|
+
}
|
|
1435
|
+
} finally {
|
|
1436
|
+
retireDownloadFinalization(finalization);
|
|
1437
|
+
if (finalization.cancelled) {
|
|
1438
|
+
cancelled.run();
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
private void settleCancelledDownload(
|
|
1445
|
+
final String id,
|
|
1446
|
+
final String version,
|
|
1447
|
+
final BackgroundDownloadContext downloadContext,
|
|
1448
|
+
final boolean awaitedByCaller,
|
|
1449
|
+
final boolean launchReleased
|
|
1450
|
+
) {
|
|
1451
|
+
final CompletableFuture<BundleInfo> future = this.downloadFutures.remove(id);
|
|
1452
|
+
if (future != null) {
|
|
1453
|
+
future.cancel(true);
|
|
1454
|
+
}
|
|
1455
|
+
this.completeBackgroundDownload(version, downloadContext, () -> {
|
|
1456
|
+
if (!launchReleased) {
|
|
1457
|
+
this.notifyLaunchDownloadReady(awaitedByCaller, false, false, false, false, null, downloadContext);
|
|
1458
|
+
}
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
private void cleanupIncompleteDownload(final String id, final String dest, final boolean manifest) {
|
|
1463
|
+
this.cleanupManifestStagingAfterDownloadFailed(id, dest, manifest);
|
|
1464
|
+
if (!manifest && dest != null) {
|
|
1465
|
+
try {
|
|
1466
|
+
final File archive = resolvePathInsideDirectory(this.documentsDir, dest);
|
|
1467
|
+
if (archive.isFile()) {
|
|
1468
|
+
safeDelete(archive);
|
|
1469
|
+
}
|
|
1470
|
+
} catch (IOException e) {
|
|
1471
|
+
logger.warn("Ignoring invalid incomplete download destination");
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1173
1476
|
@SuppressWarnings("unchecked")
|
|
1174
|
-
private
|
|
1477
|
+
private DownloadFinalization observeWorkProgress(Context context, String id, String observedVersion, boolean setNext) {
|
|
1478
|
+
return this.observeWorkProgress(context, id, observedVersion, setNext, null);
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
@SuppressWarnings("unchecked")
|
|
1482
|
+
private DownloadFinalization observeWorkProgress(
|
|
1483
|
+
Context context,
|
|
1484
|
+
String id,
|
|
1485
|
+
String observedVersion,
|
|
1486
|
+
boolean setNext,
|
|
1487
|
+
BackgroundDownloadContext downloadContext
|
|
1488
|
+
) {
|
|
1489
|
+
// A manual waiter can exit before WorkManager settles; its origin must survive the future entry.
|
|
1490
|
+
final boolean awaitedByCaller = downloadFutures.containsKey(id);
|
|
1175
1491
|
final AtomicBoolean terminalHandled = new AtomicBoolean(false);
|
|
1176
1492
|
final AtomicBoolean launchReleasedWhileRetrying = new AtomicBoolean(false);
|
|
1177
1493
|
final LiveData<List<WorkInfo>> workInfosLiveData = WorkManager.getInstance(
|
|
1178
1494
|
context.getApplicationContext()
|
|
1179
1495
|
).getWorkInfosByTagLiveData(id);
|
|
1496
|
+
final DownloadFinalization finalization = this.registerDownloadFinalization(
|
|
1497
|
+
id,
|
|
1498
|
+
setNext && downloadContext == null && !awaitedByCaller
|
|
1499
|
+
);
|
|
1500
|
+
final AtomicBoolean completionReported = new AtomicBoolean(false);
|
|
1501
|
+
final Runnable cancelled = () -> {
|
|
1502
|
+
if (completionReported.compareAndSet(false, true)) {
|
|
1503
|
+
this.settleCancelledDownload(id, observedVersion, downloadContext, awaitedByCaller, launchReleasedWhileRetrying.get());
|
|
1504
|
+
}
|
|
1505
|
+
};
|
|
1180
1506
|
// observeForever, not observe(activity): a stopped Activity pauses LiveData, so a download
|
|
1181
1507
|
// that completes while the app is in the background would only be finished once the user
|
|
1182
1508
|
// opens the app again. The observer removes itself on a terminal state.
|
|
1183
1509
|
final Observer<List<WorkInfo>>[] observerRef = new Observer[1];
|
|
1184
1510
|
final Runnable removeObserver = () -> mainHandler().post(() -> workInfosLiveData.removeObserver(observerRef[0]));
|
|
1185
|
-
|
|
1511
|
+
final Runnable attachObserver = () -> {
|
|
1186
1512
|
observerRef[0] = (workInfos) -> {
|
|
1187
1513
|
if (workInfos == null || workInfos.isEmpty()) return;
|
|
1188
1514
|
|
|
@@ -1197,17 +1523,25 @@ public class CapgoUpdater {
|
|
|
1197
1523
|
if (
|
|
1198
1524
|
workInfo.getRunAttemptCount() > 0 &&
|
|
1199
1525
|
shouldReleaseLaunchWhileRetrying(
|
|
1200
|
-
|
|
1526
|
+
awaitedByCaller,
|
|
1201
1527
|
setNext,
|
|
1202
|
-
|
|
1528
|
+
downloadContext != null
|
|
1529
|
+
? downloadContext.directUpdate
|
|
1530
|
+
: Boolean.TRUE.equals(CapgoUpdater.this.directUpdate),
|
|
1203
1531
|
CapgoUpdater.this.previewSession
|
|
1204
1532
|
) &&
|
|
1205
1533
|
launchReleasedWhileRetrying.compareAndSet(false, true)
|
|
1206
1534
|
) {
|
|
1207
1535
|
logger.warn("Direct update download is retrying, continuing launch on the current bundle");
|
|
1208
1536
|
// Same fallback as the autoSplashscreen timeout: a later success installs on next background.
|
|
1209
|
-
|
|
1210
|
-
|
|
1537
|
+
if (downloadContext != null) {
|
|
1538
|
+
downloadContext.deferredForRetry = true;
|
|
1539
|
+
} else {
|
|
1540
|
+
CapgoUpdater.this.directUpdate = false;
|
|
1541
|
+
}
|
|
1542
|
+
io.execute(() ->
|
|
1543
|
+
backgroundDownloadSettled(getCurrentBundle(), launchDownloadReadyStatus(false, false), downloadContext)
|
|
1544
|
+
);
|
|
1211
1545
|
}
|
|
1212
1546
|
break;
|
|
1213
1547
|
case RUNNING:
|
|
@@ -1226,41 +1560,81 @@ public class CapgoUpdater {
|
|
|
1226
1560
|
String checksum = outputData.getString(DownloadService.CHECKSUM);
|
|
1227
1561
|
boolean isManifest = outputData.getBoolean(DownloadService.IS_MANIFEST, false);
|
|
1228
1562
|
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1563
|
+
dispatchDownloadFinalization(
|
|
1564
|
+
finalization,
|
|
1565
|
+
() -> {
|
|
1566
|
+
// Context-backed completions report readiness from their owning plugin attempt.
|
|
1567
|
+
final boolean directInstall =
|
|
1568
|
+
setNext &&
|
|
1569
|
+
(downloadContext != null || Boolean.TRUE.equals(CapgoUpdater.this.directUpdate)) &&
|
|
1570
|
+
!CapgoUpdater.this.previewSession;
|
|
1571
|
+
final boolean previewSession = CapgoUpdater.this.previewSession;
|
|
1572
|
+
boolean success = finishDownload(
|
|
1573
|
+
id,
|
|
1574
|
+
dest,
|
|
1575
|
+
version,
|
|
1576
|
+
sessionKey,
|
|
1577
|
+
checksum,
|
|
1578
|
+
setNext,
|
|
1579
|
+
isManifest,
|
|
1580
|
+
downloadContext,
|
|
1581
|
+
finalization
|
|
1582
|
+
);
|
|
1583
|
+
if (finalization.cancelled) {
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
BundleInfo resultBundle;
|
|
1587
|
+
if (!success) {
|
|
1588
|
+
cleanupIncompleteDownload(id, dest, isManifest);
|
|
1589
|
+
logger.error("Finish download failed");
|
|
1590
|
+
logger.debug("Version: " + version);
|
|
1591
|
+
resultBundle = new BundleInfo(
|
|
1592
|
+
id,
|
|
1593
|
+
version,
|
|
1594
|
+
BundleStatus.ERROR,
|
|
1595
|
+
new Date(System.currentTimeMillis()),
|
|
1596
|
+
""
|
|
1597
|
+
);
|
|
1598
|
+
markDownloadErrorUnlessDeleted(id, version);
|
|
1599
|
+
// Cleanup download tracking
|
|
1600
|
+
DownloadWorkerManager.cancelBundleDownload(context, id, version);
|
|
1601
|
+
sendStats("finish_download_fail", version);
|
|
1602
|
+
// finishDownload already emitted downloadFailed for this failure.
|
|
1603
|
+
} else {
|
|
1604
|
+
// Successful download - cleanup tracking
|
|
1605
|
+
DownloadWorkerManager.cancelBundleDownload(context, id, version);
|
|
1606
|
+
resultBundle = getBundleInfo(id);
|
|
1607
|
+
}
|
|
1250
1608
|
|
|
1251
|
-
|
|
1609
|
+
CapgoUpdater.this.clearManifestStagingDest(id);
|
|
1252
1610
|
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1611
|
+
// Complete the future if it exists. download() waits on it.
|
|
1612
|
+
// downloadBackground does not, so the launch check must emit appReady here.
|
|
1613
|
+
CompletableFuture<BundleInfo> future = downloadFutures.remove(id);
|
|
1614
|
+
if (future != null) {
|
|
1615
|
+
future.complete(resultBundle);
|
|
1616
|
+
}
|
|
1617
|
+
final BundleInfo readyBundle = success && setNext ? resultBundle : null;
|
|
1618
|
+
completionReported.set(true);
|
|
1619
|
+
completeBackgroundDownload(observedVersion, downloadContext, () -> {
|
|
1620
|
+
if (!launchReleasedWhileRetrying.get()) {
|
|
1621
|
+
notifyLaunchDownloadReady(
|
|
1622
|
+
awaitedByCaller,
|
|
1623
|
+
success,
|
|
1624
|
+
directInstall,
|
|
1625
|
+
previewSession,
|
|
1626
|
+
setNext,
|
|
1627
|
+
readyBundle,
|
|
1628
|
+
downloadContext
|
|
1629
|
+
);
|
|
1630
|
+
}
|
|
1631
|
+
});
|
|
1632
|
+
},
|
|
1633
|
+
() -> {
|
|
1634
|
+
cleanupIncompleteDownload(id, dest, isManifest);
|
|
1635
|
+
cancelled.run();
|
|
1262
1636
|
}
|
|
1263
|
-
|
|
1637
|
+
);
|
|
1264
1638
|
break;
|
|
1265
1639
|
case FAILED:
|
|
1266
1640
|
if (!terminalHandled.compareAndSet(false, true)) break;
|
|
@@ -1274,55 +1648,73 @@ public class CapgoUpdater {
|
|
|
1274
1648
|
final String failedDest = failedData.getString(DownloadService.FILEDEST);
|
|
1275
1649
|
final boolean failedManifest = failedData.getBoolean(DownloadService.IS_MANIFEST, false);
|
|
1276
1650
|
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
id,
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
failedFuture.
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1651
|
+
dispatchDownloadFinalization(
|
|
1652
|
+
finalization,
|
|
1653
|
+
() -> {
|
|
1654
|
+
CapgoUpdater.this.cleanupManifestStagingAfterDownloadFailed(id, failedDest, failedManifest);
|
|
1655
|
+
BundleInfo failedBundle = new BundleInfo(
|
|
1656
|
+
id,
|
|
1657
|
+
failedVersion,
|
|
1658
|
+
BundleStatus.ERROR,
|
|
1659
|
+
new Date(System.currentTimeMillis()),
|
|
1660
|
+
""
|
|
1661
|
+
);
|
|
1662
|
+
saveBundleInfo(id, failedBundle);
|
|
1663
|
+
// Cleanup download tracking for failed downloads
|
|
1664
|
+
DownloadWorkerManager.cancelBundleDownload(context, id, failedVersion);
|
|
1665
|
+
Map<String, Object> ret = new HashMap<>();
|
|
1666
|
+
ret.put("version", failedVersion);
|
|
1667
|
+
if ("low_mem_fail".equals(error)) {
|
|
1668
|
+
sendStats("low_mem_fail", failedVersion);
|
|
1669
|
+
}
|
|
1670
|
+
if ("insufficient_disk_space".equals(error)) {
|
|
1671
|
+
sendStats("insufficient_disk_space", failedVersion);
|
|
1672
|
+
}
|
|
1673
|
+
ret.put("error", error != null ? error : "download_fail");
|
|
1674
|
+
sendStats("download_fail", failedVersion);
|
|
1675
|
+
notifyListeners("downloadFailed", ret);
|
|
1676
|
+
|
|
1677
|
+
// Complete the future with error status
|
|
1678
|
+
CompletableFuture<BundleInfo> failedFuture = downloadFutures.remove(id);
|
|
1679
|
+
if (failedFuture != null) {
|
|
1680
|
+
failedFuture.complete(failedBundle);
|
|
1681
|
+
}
|
|
1682
|
+
completionReported.set(true);
|
|
1683
|
+
completeBackgroundDownload(observedVersion, downloadContext, () -> {
|
|
1684
|
+
if (!launchReleasedWhileRetrying.get()) {
|
|
1685
|
+
notifyLaunchDownloadReady(awaitedByCaller, false, false, false, false, null, downloadContext);
|
|
1686
|
+
}
|
|
1687
|
+
});
|
|
1688
|
+
},
|
|
1689
|
+
() -> {
|
|
1690
|
+
cleanupIncompleteDownload(id, failedDest, failedManifest);
|
|
1691
|
+
cancelled.run();
|
|
1308
1692
|
}
|
|
1309
|
-
|
|
1693
|
+
);
|
|
1310
1694
|
break;
|
|
1311
1695
|
case CANCELLED:
|
|
1312
1696
|
if (!terminalHandled.compareAndSet(false, true)) break;
|
|
1313
1697
|
removeObserver.run();
|
|
1314
1698
|
observedDownloadVersions.remove(observedVersion);
|
|
1315
1699
|
DataManager.getInstance().clearManifest(id);
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
cancelledFuture.cancel(true);
|
|
1320
|
-
}
|
|
1700
|
+
retireDownloadFinalization(finalization);
|
|
1701
|
+
io.execute(() -> this.cleanupManifestStagingAfterDownloadCancelled(id));
|
|
1702
|
+
cancelled.run();
|
|
1321
1703
|
break;
|
|
1322
1704
|
}
|
|
1323
1705
|
};
|
|
1324
1706
|
workInfosLiveData.observeForever(observerRef[0]);
|
|
1325
|
-
}
|
|
1707
|
+
};
|
|
1708
|
+
try {
|
|
1709
|
+
if (!mainHandler().post(attachObserver)) {
|
|
1710
|
+
throw new IllegalStateException("Could not register download observer");
|
|
1711
|
+
}
|
|
1712
|
+
} catch (RuntimeException e) {
|
|
1713
|
+
finalization.cancelled = true;
|
|
1714
|
+
retireDownloadFinalization(finalization);
|
|
1715
|
+
throw e;
|
|
1716
|
+
}
|
|
1717
|
+
return finalization;
|
|
1326
1718
|
}
|
|
1327
1719
|
|
|
1328
1720
|
private void download(
|
|
@@ -1334,6 +1726,20 @@ public class CapgoUpdater {
|
|
|
1334
1726
|
final String checksum,
|
|
1335
1727
|
final JSONArray manifest,
|
|
1336
1728
|
final boolean setNext
|
|
1729
|
+
) {
|
|
1730
|
+
this.download(id, url, dest, version, sessionKey, checksum, manifest, setNext, null);
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
private void download(
|
|
1734
|
+
final String id,
|
|
1735
|
+
final String url,
|
|
1736
|
+
final String dest,
|
|
1737
|
+
final String version,
|
|
1738
|
+
final String sessionKey,
|
|
1739
|
+
final String checksum,
|
|
1740
|
+
final JSONArray manifest,
|
|
1741
|
+
final boolean setNext,
|
|
1742
|
+
final BackgroundDownloadContext downloadContext
|
|
1337
1743
|
) {
|
|
1338
1744
|
final Context context = this.context();
|
|
1339
1745
|
if (context == null) {
|
|
@@ -1341,37 +1747,58 @@ public class CapgoUpdater {
|
|
|
1341
1747
|
return;
|
|
1342
1748
|
}
|
|
1343
1749
|
observedDownloadVersions.add(version);
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1750
|
+
if (downloadContext != null) {
|
|
1751
|
+
this.backgroundDownloadContexts.put(version, downloadContext);
|
|
1752
|
+
}
|
|
1753
|
+
DownloadFinalization finalization = null;
|
|
1754
|
+
try {
|
|
1755
|
+
finalization = observeWorkProgress(context, id, version, setNext, downloadContext);
|
|
1756
|
+
|
|
1757
|
+
if (manifest != null) {
|
|
1758
|
+
DataManager.getInstance().setManifest(id, manifest);
|
|
1759
|
+
this.saveManifestStagingDest(id, dest);
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
DownloadWorkerManager.enqueueDownload(
|
|
1763
|
+
context,
|
|
1764
|
+
url,
|
|
1765
|
+
id,
|
|
1766
|
+
this.documentsDir.getAbsolutePath(),
|
|
1767
|
+
dest,
|
|
1768
|
+
version,
|
|
1769
|
+
sessionKey,
|
|
1770
|
+
checksum,
|
|
1771
|
+
this.publicKey,
|
|
1772
|
+
manifest != null,
|
|
1773
|
+
this.isEmulator(),
|
|
1774
|
+
this.appId,
|
|
1775
|
+
this.pluginVersion,
|
|
1776
|
+
this.isProd(),
|
|
1777
|
+
this.getInstallSource(),
|
|
1778
|
+
this.statsUrl,
|
|
1779
|
+
this.deviceID,
|
|
1780
|
+
this.versionBuild,
|
|
1781
|
+
this.versionCode,
|
|
1782
|
+
this.versionOs,
|
|
1783
|
+
this.customId,
|
|
1784
|
+
this.defaultChannel
|
|
1785
|
+
);
|
|
1786
|
+
if (finalization.cancelled) {
|
|
1787
|
+
WorkManager.getInstance(context).cancelAllWorkByTag(id);
|
|
1788
|
+
}
|
|
1789
|
+
} catch (final RuntimeException e) {
|
|
1790
|
+
if (finalization != null) {
|
|
1791
|
+
synchronized (deleteLock) {
|
|
1792
|
+
finalization.cancelled = true;
|
|
1793
|
+
if (finalization.running == null) {
|
|
1794
|
+
retireDownloadFinalization(finalization);
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
observedDownloadVersions.remove(version);
|
|
1799
|
+
completeBackgroundDownload(version, downloadContext, () -> {});
|
|
1800
|
+
throw e;
|
|
1349
1801
|
}
|
|
1350
|
-
|
|
1351
|
-
DownloadWorkerManager.enqueueDownload(
|
|
1352
|
-
context,
|
|
1353
|
-
url,
|
|
1354
|
-
id,
|
|
1355
|
-
this.documentsDir.getAbsolutePath(),
|
|
1356
|
-
dest,
|
|
1357
|
-
version,
|
|
1358
|
-
sessionKey,
|
|
1359
|
-
checksum,
|
|
1360
|
-
this.publicKey,
|
|
1361
|
-
manifest != null,
|
|
1362
|
-
this.isEmulator(),
|
|
1363
|
-
this.appId,
|
|
1364
|
-
this.pluginVersion,
|
|
1365
|
-
this.isProd(),
|
|
1366
|
-
this.getInstallSource(),
|
|
1367
|
-
this.statsUrl,
|
|
1368
|
-
this.deviceID,
|
|
1369
|
-
this.versionBuild,
|
|
1370
|
-
this.versionCode,
|
|
1371
|
-
this.versionOs,
|
|
1372
|
-
this.customId,
|
|
1373
|
-
this.defaultChannel
|
|
1374
|
-
);
|
|
1375
1802
|
}
|
|
1376
1803
|
|
|
1377
1804
|
public Boolean finishDownload(
|
|
@@ -1382,6 +1809,20 @@ public class CapgoUpdater {
|
|
|
1382
1809
|
String checksumRes,
|
|
1383
1810
|
Boolean setNext,
|
|
1384
1811
|
Boolean isManifest
|
|
1812
|
+
) {
|
|
1813
|
+
return this.finishDownload(id, dest, version, sessionKey, checksumRes, setNext, isManifest, null, null);
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
private Boolean finishDownload(
|
|
1817
|
+
String id,
|
|
1818
|
+
String dest,
|
|
1819
|
+
String version,
|
|
1820
|
+
String sessionKey,
|
|
1821
|
+
String checksumRes,
|
|
1822
|
+
Boolean setNext,
|
|
1823
|
+
Boolean isManifest,
|
|
1824
|
+
BackgroundDownloadContext downloadContext,
|
|
1825
|
+
DownloadFinalization finalization
|
|
1385
1826
|
) {
|
|
1386
1827
|
File downloaded = null;
|
|
1387
1828
|
File extractedDir = null;
|
|
@@ -1397,8 +1838,17 @@ public class CapgoUpdater {
|
|
|
1397
1838
|
this.requireBundleChecksum(expectedChecksum);
|
|
1398
1839
|
|
|
1399
1840
|
if (CryptoCipher.isValidSessionKey(sessionKey)) {
|
|
1400
|
-
CryptoCipher.
|
|
1401
|
-
|
|
1841
|
+
final CryptoCipher.BundleChecksum signed = CryptoCipher.decryptBundleChecksum(checksumRes, publicKey);
|
|
1842
|
+
try {
|
|
1843
|
+
// The encrypted zip is hashed and checked against the signed digest before decrypting.
|
|
1844
|
+
CryptoCipher.decryptFile(downloaded, publicKey, sessionKey, signed.ciphertext);
|
|
1845
|
+
} catch (IOException e) {
|
|
1846
|
+
if (CryptoCipher.CIPHERTEXT_CHECKSUM_MISMATCH.equals(e.getMessage())) {
|
|
1847
|
+
this.sendStats("checksum_fail");
|
|
1848
|
+
}
|
|
1849
|
+
throw e;
|
|
1850
|
+
}
|
|
1851
|
+
expectedChecksum = signed.plain;
|
|
1402
1852
|
}
|
|
1403
1853
|
checksum = CryptoCipher.calcChecksum(downloaded);
|
|
1404
1854
|
CryptoCipher.logChecksumInfo("Calculated checksum", checksum);
|
|
@@ -1428,6 +1878,9 @@ public class CapgoUpdater {
|
|
|
1428
1878
|
}
|
|
1429
1879
|
|
|
1430
1880
|
try {
|
|
1881
|
+
if (finalization != null && finalization.cancelled) {
|
|
1882
|
+
return false;
|
|
1883
|
+
}
|
|
1431
1884
|
if (!isManifest) {
|
|
1432
1885
|
extractedDir = this.unzip(id, downloaded, TEMP_UNZIP_PREFIX + this.randomString());
|
|
1433
1886
|
this.notifyDownload(id, 91);
|
|
@@ -1438,10 +1891,15 @@ public class CapgoUpdater {
|
|
|
1438
1891
|
this.flattenAssets(downloaded, this.getBundleDirectory(id));
|
|
1439
1892
|
downloaded.delete();
|
|
1440
1893
|
}
|
|
1441
|
-
// Remove old bundle info and set new one
|
|
1442
|
-
this.saveBundleInfo(id, null);
|
|
1443
1894
|
BundleInfo next = new BundleInfo(id, version, BundleStatus.PENDING, new Date(System.currentTimeMillis()), checksum);
|
|
1444
|
-
|
|
1895
|
+
synchronized (deleteLock) {
|
|
1896
|
+
if (finalization != null && finalization.cancelled) {
|
|
1897
|
+
return false;
|
|
1898
|
+
}
|
|
1899
|
+
// Remove old bundle info and set new one without overwriting a restored deletion marker.
|
|
1900
|
+
this.saveBundleInfo(id, null);
|
|
1901
|
+
this.saveBundleInfo(id, next);
|
|
1902
|
+
}
|
|
1445
1903
|
this.notifyDownload(id, 100);
|
|
1446
1904
|
|
|
1447
1905
|
final Map<String, Object> ret = new HashMap<>();
|
|
@@ -1450,18 +1908,27 @@ public class CapgoUpdater {
|
|
|
1450
1908
|
CapgoUpdater.this.notifyListeners("updateAvailable", ret);
|
|
1451
1909
|
logger.info("setNext: " + setNext);
|
|
1452
1910
|
if (setNext) {
|
|
1453
|
-
if (
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
CapgoUpdater.this.directUpdateFinish(next);
|
|
1459
|
-
this.directUpdate = false;
|
|
1911
|
+
if (finalization != null && finalization.cancelled) {
|
|
1912
|
+
return false;
|
|
1913
|
+
}
|
|
1914
|
+
if (downloadContext != null) {
|
|
1915
|
+
this.completeBackgroundDownload(version, downloadContext, () -> this.directUpdateFinish(next, downloadContext));
|
|
1460
1916
|
} else {
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1917
|
+
this.completeContextlessDownload(finalization, () -> {
|
|
1918
|
+
if (this.previewSession) {
|
|
1919
|
+
logger.info("Preview session is active, skipping automatic install of downloaded bundle");
|
|
1920
|
+
this.directUpdate = false;
|
|
1921
|
+
} else if (this.directUpdate) {
|
|
1922
|
+
logger.info("directUpdate: " + this.directUpdate);
|
|
1923
|
+
CapgoUpdater.this.directUpdateFinish(next);
|
|
1924
|
+
this.directUpdate = false;
|
|
1925
|
+
} else {
|
|
1926
|
+
logger.info("directUpdate: " + this.directUpdate);
|
|
1927
|
+
if (this.setNextBundle(next.getId())) {
|
|
1928
|
+
this.nextBundleReady(next);
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
});
|
|
1465
1932
|
}
|
|
1466
1933
|
}
|
|
1467
1934
|
} catch (IOException e) {
|
|
@@ -1480,7 +1947,7 @@ public class CapgoUpdater {
|
|
|
1480
1947
|
return true;
|
|
1481
1948
|
}
|
|
1482
1949
|
|
|
1483
|
-
|
|
1950
|
+
void deleteDirectory(final File file) throws IOException {
|
|
1484
1951
|
deleteDirectory(file, null);
|
|
1485
1952
|
}
|
|
1486
1953
|
|
|
@@ -1951,6 +2418,18 @@ public class CapgoUpdater {
|
|
|
1951
2418
|
final String checksum,
|
|
1952
2419
|
final JSONArray manifest,
|
|
1953
2420
|
final boolean setNext
|
|
2421
|
+
) {
|
|
2422
|
+
return this.downloadBackground(url, version, sessionKey, checksum, manifest, setNext, null).bundleId;
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
BackgroundDownloadResult downloadBackground(
|
|
2426
|
+
final String url,
|
|
2427
|
+
final String version,
|
|
2428
|
+
final String sessionKey,
|
|
2429
|
+
final String checksum,
|
|
2430
|
+
final JSONArray manifest,
|
|
2431
|
+
final boolean setNext,
|
|
2432
|
+
final BackgroundDownloadContext downloadContext
|
|
1954
2433
|
) {
|
|
1955
2434
|
try {
|
|
1956
2435
|
this.requireSessionKeyForEncryptedUpdate(sessionKey);
|
|
@@ -1960,10 +2439,30 @@ public class CapgoUpdater {
|
|
|
1960
2439
|
this.runPreDownloadSecurityGates(url, version, manifest);
|
|
1961
2440
|
} catch (final IOException e) {
|
|
1962
2441
|
logger.error("Download blocked: " + e.getMessage());
|
|
1963
|
-
return
|
|
2442
|
+
return BackgroundDownloadResult.NOT_STARTED;
|
|
1964
2443
|
}
|
|
1965
2444
|
if (!this.runDownloadGateQuiet()) {
|
|
1966
|
-
return
|
|
2445
|
+
return BackgroundDownloadResult.NOT_STARTED;
|
|
2446
|
+
}
|
|
2447
|
+
// A later check can miss reuse before the earlier worker registers. Recheck and register
|
|
2448
|
+
// together so simultaneous starts cannot overwrite the context attached to the observer.
|
|
2449
|
+
synchronized (this.backgroundDownloadRegistrationLock) {
|
|
2450
|
+
return this.startBackgroundDownload(url, version, sessionKey, checksum, manifest, setNext, downloadContext);
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
private BackgroundDownloadResult startBackgroundDownload(
|
|
2455
|
+
final String url,
|
|
2456
|
+
final String version,
|
|
2457
|
+
final String sessionKey,
|
|
2458
|
+
final String checksum,
|
|
2459
|
+
final JSONArray manifest,
|
|
2460
|
+
final boolean setNext,
|
|
2461
|
+
final BackgroundDownloadContext downloadContext
|
|
2462
|
+
) {
|
|
2463
|
+
if (downloadContext != null && this.reuseBackgroundDownload(version, downloadContext)) {
|
|
2464
|
+
logger.info("Reusing automatic download at registration for version: " + version);
|
|
2465
|
+
return BackgroundDownloadResult.REUSED;
|
|
1967
2466
|
}
|
|
1968
2467
|
final String id = this.randomString();
|
|
1969
2468
|
|
|
@@ -1975,14 +2474,14 @@ public class CapgoUpdater {
|
|
|
1975
2474
|
// Cancel the failed download and allow retry
|
|
1976
2475
|
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), version)) {
|
|
1977
2476
|
logger.error("Failed to cancel previous download before retry");
|
|
1978
|
-
return
|
|
2477
|
+
return BackgroundDownloadResult.NOT_STARTED;
|
|
1979
2478
|
}
|
|
1980
2479
|
logger.info("Retrying failed download for version: " + version);
|
|
1981
2480
|
} else if (shouldRestartOrphanedDownload(observedDownloadVersions.contains(version))) {
|
|
1982
2481
|
// Left over from a killed process: nothing would finish it or release the launch, so start over.
|
|
1983
2482
|
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), version)) {
|
|
1984
2483
|
logger.error("Failed to cancel orphaned download before restarting it");
|
|
1985
|
-
return
|
|
2484
|
+
return BackgroundDownloadResult.NOT_STARTED;
|
|
1986
2485
|
}
|
|
1987
2486
|
BundleInfo orphanedBundle = null;
|
|
1988
2487
|
for (final BundleInfo info : this.list(true)) {
|
|
@@ -1998,7 +2497,7 @@ public class CapgoUpdater {
|
|
|
1998
2497
|
logger.info("Restarting download orphaned by a previous process for version: " + version);
|
|
1999
2498
|
} else {
|
|
2000
2499
|
logger.info("Version already downloading: " + version);
|
|
2001
|
-
return
|
|
2500
|
+
return BackgroundDownloadResult.NOT_STARTED;
|
|
2002
2501
|
}
|
|
2003
2502
|
}
|
|
2004
2503
|
|
|
@@ -2007,8 +2506,16 @@ public class CapgoUpdater {
|
|
|
2007
2506
|
this.notifyDownload(id, 5);
|
|
2008
2507
|
|
|
2009
2508
|
final String dest = manifest != null ? this.manifestStagingDest() : this.randomString();
|
|
2010
|
-
|
|
2011
|
-
|
|
2509
|
+
if (downloadContext == null) {
|
|
2510
|
+
this.download(id, url, dest, version, sessionKey, checksum, manifest, setNext, null);
|
|
2511
|
+
} else {
|
|
2512
|
+
// Terminal delivery and same-version reuse cannot overtake successful registration.
|
|
2513
|
+
synchronized (downloadContext) {
|
|
2514
|
+
this.download(id, url, dest, version, sessionKey, checksum, manifest, setNext, downloadContext);
|
|
2515
|
+
this.backgroundDownloadStarted(downloadContext);
|
|
2516
|
+
}
|
|
2517
|
+
}
|
|
2518
|
+
return BackgroundDownloadResult.started(id);
|
|
2012
2519
|
}
|
|
2013
2520
|
|
|
2014
2521
|
public BundleInfo download(final String url, final String version, final String sessionKey, final String checksum) throws IOException {
|
|
@@ -2051,8 +2558,7 @@ public class CapgoUpdater {
|
|
|
2051
2558
|
downloadFutures.remove(id);
|
|
2052
2559
|
logger.error("Error waiting for download");
|
|
2053
2560
|
logger.debug("Error: " + e.getMessage());
|
|
2054
|
-
|
|
2055
|
-
saveBundleInfo(id, errorBundle);
|
|
2561
|
+
this.markDownloadErrorUnlessDeleted(id, version);
|
|
2056
2562
|
if (e instanceof IOException) {
|
|
2057
2563
|
throw (IOException) e;
|
|
2058
2564
|
}
|
|
@@ -2109,8 +2615,7 @@ public class CapgoUpdater {
|
|
|
2109
2615
|
downloadFutures.remove(id);
|
|
2110
2616
|
logger.error("Error waiting for download");
|
|
2111
2617
|
logger.debug("Error: " + e.getMessage());
|
|
2112
|
-
|
|
2113
|
-
saveBundleInfo(id, errorBundle);
|
|
2618
|
+
this.markDownloadErrorUnlessDeleted(id, version);
|
|
2114
2619
|
if (e instanceof IOException) {
|
|
2115
2620
|
throw (IOException) e;
|
|
2116
2621
|
}
|
|
@@ -2118,6 +2623,15 @@ public class CapgoUpdater {
|
|
|
2118
2623
|
}
|
|
2119
2624
|
}
|
|
2120
2625
|
|
|
2626
|
+
private void markDownloadErrorUnlessDeleted(final String id, final String version) {
|
|
2627
|
+
synchronized (deleteLock) {
|
|
2628
|
+
final BundleInfo info = this.getBundleInfo(id);
|
|
2629
|
+
if (this.hasStoredBundleInfo(id) && !this.isBundleBeingDeleted(id) && !info.isDeleting() && !info.isDeleted()) {
|
|
2630
|
+
this.saveBundleInfo(id, new BundleInfo(id, version, BundleStatus.ERROR, new Date(System.currentTimeMillis()), ""));
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
|
|
2121
2635
|
public List<BundleInfo> list(boolean rawList) {
|
|
2122
2636
|
if (!rawList) {
|
|
2123
2637
|
final List<BundleInfo> res = new ArrayList<>();
|
|
@@ -2149,9 +2663,26 @@ public class CapgoUpdater {
|
|
|
2149
2663
|
return this.delete(id, removeInfo, true);
|
|
2150
2664
|
}
|
|
2151
2665
|
|
|
2666
|
+
boolean deleteDeletedBundle(final String id) throws IOException {
|
|
2667
|
+
// Admit only this tombstone; version-wide cancellation could stop a newer download.
|
|
2668
|
+
return this.deleteBundle(id, true, DeletionDownloadPolicy.KEEP, true);
|
|
2669
|
+
}
|
|
2670
|
+
|
|
2152
2671
|
public Boolean delete(final String id, final Boolean removeInfo, final boolean cancelActiveDownload) throws IOException {
|
|
2153
|
-
|
|
2154
|
-
|
|
2672
|
+
return this.deleteBundle(id, removeInfo, cancelActiveDownload ? DeletionDownloadPolicy.CANCEL : DeletionDownloadPolicy.KEEP, false);
|
|
2673
|
+
}
|
|
2674
|
+
|
|
2675
|
+
private boolean deleteBundle(
|
|
2676
|
+
final String id,
|
|
2677
|
+
final Boolean removeInfo,
|
|
2678
|
+
final DeletionDownloadPolicy downloadPolicy,
|
|
2679
|
+
final boolean requireDeleted
|
|
2680
|
+
) throws IOException {
|
|
2681
|
+
final File bundle;
|
|
2682
|
+
final BundleInfo deleted;
|
|
2683
|
+
final boolean shouldCancelDownload;
|
|
2684
|
+
final List<DownloadFinalization> runningFinalizations = new ArrayList<>();
|
|
2685
|
+
synchronized (deleteLock) {
|
|
2155
2686
|
try {
|
|
2156
2687
|
bundle = this.getBundleDirectory(id);
|
|
2157
2688
|
} catch (IOException e) {
|
|
@@ -2159,7 +2690,10 @@ public class CapgoUpdater {
|
|
|
2159
2690
|
logger.debug("Bundle ID: " + id + ", Error: " + e.getMessage());
|
|
2160
2691
|
return false;
|
|
2161
2692
|
}
|
|
2162
|
-
|
|
2693
|
+
deleted = this.getBundleInfo(id);
|
|
2694
|
+
if (this.isBundleBeingDeleted(id) || (requireDeleted && !deleted.isDeleted())) {
|
|
2695
|
+
return false;
|
|
2696
|
+
}
|
|
2163
2697
|
if (deleted.isBuiltin() || this.getCurrentBundleId().equals(id)) {
|
|
2164
2698
|
logger.error("Cannot delete current or builtin bundle");
|
|
2165
2699
|
logger.debug("Bundle ID: " + id);
|
|
@@ -2177,38 +2711,92 @@ public class CapgoUpdater {
|
|
|
2177
2711
|
logger.debug("Bundle ID: " + id);
|
|
2178
2712
|
return false;
|
|
2179
2713
|
}
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
if (!hadRegistry && !hadFolder) {
|
|
2190
|
-
logger.error("Cannot delete unknown bundle");
|
|
2191
|
-
logger.debug("Bundle ID: " + id);
|
|
2192
|
-
return false;
|
|
2193
|
-
}
|
|
2714
|
+
// Keep the existing activation-to-queue lock order. Queue validation and deletion
|
|
2715
|
+
// admission must finish together; neither lock is held during cancellation or disk removal.
|
|
2716
|
+
synchronized (nextBundleLock) {
|
|
2717
|
+
final BundleInfo next = this.getNextBundle();
|
|
2718
|
+
if (next != null && !next.isDeleted() && !next.isErrorStatus() && !next.isDeleting() && next.getId().equals(id)) {
|
|
2719
|
+
logger.error("Cannot delete the next bundle");
|
|
2720
|
+
logger.debug("Bundle ID: " + id);
|
|
2721
|
+
return false;
|
|
2722
|
+
}
|
|
2194
2723
|
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
if (!
|
|
2198
|
-
|
|
2724
|
+
final boolean hadRegistry = this.hasStoredBundleInfo(id);
|
|
2725
|
+
final boolean hadFolder = bundle.exists();
|
|
2726
|
+
if (!hadRegistry && !hadFolder) {
|
|
2727
|
+
if (this.getPendingDeleteIds().contains(id)) {
|
|
2728
|
+
// Disk and registry cleanup survived, but retiring its retry record did not.
|
|
2729
|
+
return this.dequeuePendingDelete(id);
|
|
2730
|
+
}
|
|
2731
|
+
logger.error("Cannot delete unknown bundle");
|
|
2199
2732
|
logger.debug("Bundle ID: " + id);
|
|
2200
2733
|
return false;
|
|
2201
2734
|
}
|
|
2735
|
+
|
|
2736
|
+
// Persist the ID and cancellation policy before a marker can make this deletion recoverable.
|
|
2737
|
+
// Download callbacks can overwrite DELETING while cancellation or disk removal is in progress.
|
|
2738
|
+
if (!this.enqueuePendingDelete(id, downloadPolicy)) {
|
|
2739
|
+
logger.error("Failed to persist pending deletion, aborting disk delete");
|
|
2740
|
+
return false;
|
|
2741
|
+
}
|
|
2742
|
+
shouldCancelDownload = !this.getPendingDeleteIds(PENDING_DELETE_NO_CANCEL_IDS).contains(id);
|
|
2743
|
+
if (!deleted.isDeleting()) {
|
|
2744
|
+
if (!this.saveBundleInfo(id, deleted.setStatus(BundleStatus.DELETING))) {
|
|
2745
|
+
logger.error("Failed to persist DELETING marker, aborting disk delete");
|
|
2746
|
+
logger.debug("Bundle ID: " + id);
|
|
2747
|
+
return false;
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
|
|
2751
|
+
activeBundleDeletes.add(bundle);
|
|
2752
|
+
final Set<DownloadFinalization> pending = downloadFinalizations.get(bundle);
|
|
2753
|
+
if (pending != null) {
|
|
2754
|
+
final Iterator<DownloadFinalization> iterator = pending.iterator();
|
|
2755
|
+
while (iterator.hasNext()) {
|
|
2756
|
+
final DownloadFinalization finalization = iterator.next();
|
|
2757
|
+
if (finalization.running == Thread.currentThread()) {
|
|
2758
|
+
// Failed verification cleans up its own bundle before reporting the failure.
|
|
2759
|
+
continue;
|
|
2760
|
+
}
|
|
2761
|
+
finalization.cancelled = true;
|
|
2762
|
+
if (finalization.running == null) {
|
|
2763
|
+
iterator.remove();
|
|
2764
|
+
} else {
|
|
2765
|
+
runningFinalizations.add(finalization);
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
if (pending.isEmpty()) {
|
|
2769
|
+
downloadFinalizations.remove(bundle);
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2202
2772
|
}
|
|
2773
|
+
}
|
|
2203
2774
|
|
|
2775
|
+
boolean completed = false;
|
|
2776
|
+
try {
|
|
2204
2777
|
// Cancel download for this version if active
|
|
2205
|
-
if (
|
|
2778
|
+
if (shouldCancelDownload && this.context() != null) {
|
|
2206
2779
|
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), deleted.getVersionName())) {
|
|
2207
2780
|
logger.error("Failed to cancel active download before delete");
|
|
2208
2781
|
return false;
|
|
2209
2782
|
}
|
|
2210
2783
|
}
|
|
2211
2784
|
|
|
2785
|
+
// WorkManager reaching a terminal state does not settle observers' queued disk work.
|
|
2786
|
+
// Wait outside the admission lock so unrelated activation and lifecycle calls can proceed.
|
|
2787
|
+
final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
|
2788
|
+
for (final DownloadFinalization finalization : runningFinalizations) {
|
|
2789
|
+
try {
|
|
2790
|
+
if (!finalization.finished.await(Math.max(0, deadline - System.nanoTime()), TimeUnit.NANOSECONDS)) {
|
|
2791
|
+
logger.warn("Download finalization is still running; retaining pending deletion");
|
|
2792
|
+
return false;
|
|
2793
|
+
}
|
|
2794
|
+
} catch (InterruptedException e) {
|
|
2795
|
+
Thread.currentThread().interrupt();
|
|
2796
|
+
return false;
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
|
|
2212
2800
|
if (bundle.exists()) {
|
|
2213
2801
|
try {
|
|
2214
2802
|
this.deleteDirectory(bundle);
|
|
@@ -2226,22 +2814,40 @@ public class CapgoUpdater {
|
|
|
2226
2814
|
return false;
|
|
2227
2815
|
}
|
|
2228
2816
|
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2817
|
+
synchronized (deleteLock) {
|
|
2818
|
+
final boolean finalized;
|
|
2819
|
+
if (Boolean.FALSE.equals(removeInfo)) {
|
|
2820
|
+
finalized = this.saveBundleInfo(id, deleted.setStatus(BundleStatus.DELETED));
|
|
2821
|
+
} else {
|
|
2822
|
+
finalized = this.saveBundleInfo(id, null);
|
|
2823
|
+
}
|
|
2824
|
+
if (!finalized) {
|
|
2825
|
+
logger.error("Failed to finalize delete registry update, will retry later");
|
|
2826
|
+
logger.debug("Bundle ID: " + id);
|
|
2827
|
+
return false;
|
|
2828
|
+
}
|
|
2829
|
+
// Do not recreate registry metadata if only pending-record retirement needs retrying.
|
|
2830
|
+
completed = true;
|
|
2831
|
+
if (!this.dequeuePendingDelete(id)) {
|
|
2832
|
+
logger.error("Failed to retire pending deletion, will retry later");
|
|
2833
|
+
return false;
|
|
2834
|
+
}
|
|
2239
2835
|
}
|
|
2240
2836
|
this.sendStats("delete", deleted.getVersionName());
|
|
2241
|
-
this.dequeuePendingDelete(id);
|
|
2242
2837
|
logger.info("Bundle deleted and confirmed gone");
|
|
2243
2838
|
logger.debug("Bundle ID: " + id);
|
|
2244
2839
|
return true;
|
|
2840
|
+
} finally {
|
|
2841
|
+
synchronized (deleteLock) {
|
|
2842
|
+
try {
|
|
2843
|
+
if (!completed && !this.getBundleInfo(id).isDeleting()) {
|
|
2844
|
+
// A download callback may have updated its registry while cancellation waited.
|
|
2845
|
+
this.saveBundleInfo(id, deleted.setStatus(BundleStatus.DELETING));
|
|
2846
|
+
}
|
|
2847
|
+
} finally {
|
|
2848
|
+
activeBundleDeletes.remove(bundle);
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2245
2851
|
}
|
|
2246
2852
|
}
|
|
2247
2853
|
|
|
@@ -2270,9 +2876,7 @@ public class CapgoUpdater {
|
|
|
2270
2876
|
for (final String id : pendingIds) {
|
|
2271
2877
|
try {
|
|
2272
2878
|
logger.info("Resuming pending delete for bundle: " + id);
|
|
2273
|
-
|
|
2274
|
-
this.dequeuePendingDelete(id);
|
|
2275
|
-
}
|
|
2879
|
+
this.deleteBundle(id, true, DeletionDownloadPolicy.RESUME, false);
|
|
2276
2880
|
} catch (final Exception e) {
|
|
2277
2881
|
logger.error("Pending delete failed, will retry next launch");
|
|
2278
2882
|
logger.debug("Bundle ID: " + id + ", Error: " + e.getMessage());
|
|
@@ -2287,55 +2891,93 @@ public class CapgoUpdater {
|
|
|
2287
2891
|
}
|
|
2288
2892
|
|
|
2289
2893
|
private Set<String> getPendingDeleteIds() {
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2894
|
+
return this.getPendingDeleteIds(PENDING_DELETE_IDS);
|
|
2895
|
+
}
|
|
2896
|
+
|
|
2897
|
+
private Set<String> getPendingDeleteIds(final String key) {
|
|
2898
|
+
synchronized (pendingDeleteLock) {
|
|
2899
|
+
final Set<String> ids = new LinkedHashSet<>();
|
|
2900
|
+
if (this.prefs == null) {
|
|
2901
|
+
return ids;
|
|
2902
|
+
}
|
|
2903
|
+
final String raw = this.prefs.getString(key, "");
|
|
2904
|
+
if (raw == null || raw.isEmpty()) {
|
|
2905
|
+
return ids;
|
|
2906
|
+
}
|
|
2907
|
+
for (final String part : raw.split(",")) {
|
|
2908
|
+
if (!part.isEmpty()) {
|
|
2909
|
+
ids.add(part);
|
|
2910
|
+
}
|
|
2301
2911
|
}
|
|
2912
|
+
return ids;
|
|
2302
2913
|
}
|
|
2303
|
-
return ids;
|
|
2304
2914
|
}
|
|
2305
2915
|
|
|
2306
|
-
private
|
|
2916
|
+
private boolean enqueuePendingDelete(final String id) {
|
|
2917
|
+
return this.enqueuePendingDelete(id, DeletionDownloadPolicy.RESUME);
|
|
2918
|
+
}
|
|
2919
|
+
|
|
2920
|
+
private boolean enqueuePendingDelete(final String id, final DeletionDownloadPolicy downloadPolicy) {
|
|
2307
2921
|
if (id == null || id.isEmpty() || this.editor == null || this.prefs == null) {
|
|
2308
|
-
return;
|
|
2922
|
+
return false;
|
|
2309
2923
|
}
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2924
|
+
synchronized (pendingDeleteLock) {
|
|
2925
|
+
final Set<String> ids = this.getPendingDeleteIds();
|
|
2926
|
+
final Set<String> noCancelIds = this.getPendingDeleteIds(PENDING_DELETE_NO_CANCEL_IDS);
|
|
2927
|
+
ids.add(id);
|
|
2928
|
+
if (downloadPolicy == DeletionDownloadPolicy.CANCEL) {
|
|
2929
|
+
noCancelIds.remove(id);
|
|
2930
|
+
} else if (downloadPolicy == DeletionDownloadPolicy.KEEP) {
|
|
2931
|
+
noCancelIds.add(id);
|
|
2932
|
+
}
|
|
2933
|
+
// Recovery retains the recorded policy; a new request replaces it before disk work.
|
|
2934
|
+
final SharedPreferences.Editor pendingEditor = this.prefs.edit();
|
|
2935
|
+
this.writePendingDeleteIds(pendingEditor, PENDING_DELETE_IDS, ids);
|
|
2936
|
+
this.writePendingDeleteIds(pendingEditor, PENDING_DELETE_NO_CANCEL_IDS, noCancelIds);
|
|
2937
|
+
return pendingEditor.commit();
|
|
2313
2938
|
}
|
|
2314
|
-
this.editor.putString(PENDING_DELETE_IDS, String.join(",", ids));
|
|
2315
|
-
this.editor.commit();
|
|
2316
2939
|
}
|
|
2317
2940
|
|
|
2318
|
-
private
|
|
2941
|
+
private boolean dequeuePendingDelete(final String id) {
|
|
2319
2942
|
if (id == null || id.isEmpty() || this.editor == null || this.prefs == null) {
|
|
2320
|
-
return;
|
|
2943
|
+
return false;
|
|
2321
2944
|
}
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2945
|
+
synchronized (pendingDeleteLock) {
|
|
2946
|
+
final Set<String> ids = this.getPendingDeleteIds();
|
|
2947
|
+
final Set<String> noCancelIds = this.getPendingDeleteIds(PENDING_DELETE_NO_CANCEL_IDS);
|
|
2948
|
+
ids.remove(id);
|
|
2949
|
+
noCancelIds.remove(id);
|
|
2950
|
+
final SharedPreferences.Editor pendingEditor = this.prefs.edit();
|
|
2951
|
+
this.writePendingDeleteIds(pendingEditor, PENDING_DELETE_IDS, ids);
|
|
2952
|
+
this.writePendingDeleteIds(pendingEditor, PENDING_DELETE_NO_CANCEL_IDS, noCancelIds);
|
|
2953
|
+
return pendingEditor.commit();
|
|
2325
2954
|
}
|
|
2955
|
+
}
|
|
2956
|
+
|
|
2957
|
+
private void writePendingDeleteIds(final SharedPreferences.Editor pendingEditor, final String key, final Set<String> ids) {
|
|
2326
2958
|
if (ids.isEmpty()) {
|
|
2327
|
-
|
|
2959
|
+
pendingEditor.remove(key);
|
|
2328
2960
|
} else {
|
|
2329
|
-
|
|
2961
|
+
pendingEditor.putString(key, String.join(",", ids));
|
|
2330
2962
|
}
|
|
2331
|
-
this.editor.commit();
|
|
2332
2963
|
}
|
|
2333
2964
|
|
|
2334
2965
|
private File getBundleDirectory(final String id) throws IOException {
|
|
2335
2966
|
return resolveBundleDirectory(this.documentsDir, id);
|
|
2336
2967
|
}
|
|
2337
2968
|
|
|
2338
|
-
private boolean
|
|
2969
|
+
private boolean isBundleBeingDeleted(final String id) {
|
|
2970
|
+
try {
|
|
2971
|
+
return activeBundleDeletes.contains(this.getBundleDirectory(id));
|
|
2972
|
+
} catch (IOException e) {
|
|
2973
|
+
return false;
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
|
|
2977
|
+
boolean bundleExists(final String id) {
|
|
2978
|
+
if (id != null && this.isBundleBeingDeleted(id)) {
|
|
2979
|
+
return false;
|
|
2980
|
+
}
|
|
2339
2981
|
final File bundle;
|
|
2340
2982
|
try {
|
|
2341
2983
|
bundle = this.getBundleDirectory(id);
|
|
@@ -2383,17 +3025,34 @@ public class CapgoUpdater {
|
|
|
2383
3025
|
this.restoreResetStateRollbackTargets(state);
|
|
2384
3026
|
}
|
|
2385
3027
|
|
|
3028
|
+
void restoreResetStatePreservingNextBundle(final ResetState state) {
|
|
3029
|
+
synchronized (deleteLock) {
|
|
3030
|
+
final String previousPath = state.currentBundlePath;
|
|
3031
|
+
final boolean builtin = previousPath == null || previousPath.trim().isEmpty() || previousPath.equals("public");
|
|
3032
|
+
final boolean targetAvailable = builtin || this.bundleExists(previousPath.substring(previousPath.lastIndexOf('/') + 1));
|
|
3033
|
+
final ResetState target = targetAvailable ? state : new ResetState("public", BundleInfo.ID_BUILTIN, state.nextBundleId);
|
|
3034
|
+
synchronized (nextBundleLock) {
|
|
3035
|
+
final BundleInfo next = this.getNextBundle();
|
|
3036
|
+
this.restoreResetState(
|
|
3037
|
+
new ResetState(target.currentBundlePath, target.fallbackBundleId, next == null ? null : next.getId())
|
|
3038
|
+
);
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
}
|
|
3042
|
+
|
|
2386
3043
|
void restoreResetStateRollbackTargets(final ResetState state) {
|
|
2387
3044
|
final String fallbackBundleId =
|
|
2388
3045
|
state.fallbackBundleId == null || state.fallbackBundleId.isEmpty() ? BundleInfo.ID_BUILTIN : state.fallbackBundleId;
|
|
2389
3046
|
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
3047
|
+
synchronized (nextBundleLock) {
|
|
3048
|
+
this.editor.putString(FALLBACK_VERSION, fallbackBundleId);
|
|
3049
|
+
if (state.nextBundleId == null || state.nextBundleId.isEmpty()) {
|
|
3050
|
+
this.editor.remove(NEXT_VERSION);
|
|
3051
|
+
} else {
|
|
3052
|
+
this.editor.putString(NEXT_VERSION, state.nextBundleId);
|
|
3053
|
+
}
|
|
3054
|
+
this.editor.commit();
|
|
2395
3055
|
}
|
|
2396
|
-
this.editor.commit();
|
|
2397
3056
|
}
|
|
2398
3057
|
|
|
2399
3058
|
void preserveFallbackForWatchdog(final ResetState state) {
|
|
@@ -2404,9 +3063,13 @@ public class CapgoUpdater {
|
|
|
2404
3063
|
}
|
|
2405
3064
|
|
|
2406
3065
|
void prepareResetStateForTransition() {
|
|
3066
|
+
this.prepareBuiltinTransition();
|
|
3067
|
+
this.setNextBundle(null);
|
|
3068
|
+
}
|
|
3069
|
+
|
|
3070
|
+
void prepareBuiltinTransition() {
|
|
2407
3071
|
this.setCurrentBundle(new File("public"));
|
|
2408
3072
|
this.setFallbackBundle(null);
|
|
2409
|
-
this.setNextBundle(null);
|
|
2410
3073
|
}
|
|
2411
3074
|
|
|
2412
3075
|
void finalizeResetTransition(final String previousBundleName, final boolean internal) {
|
|
@@ -2414,75 +3077,107 @@ public class CapgoUpdater {
|
|
|
2414
3077
|
DownloadWorkerManager.cancelAllDownloads(this.context());
|
|
2415
3078
|
}
|
|
2416
3079
|
if (!internal) {
|
|
2417
|
-
this.
|
|
3080
|
+
this.finalizeBuiltinUpdate(previousBundleName);
|
|
2418
3081
|
}
|
|
2419
3082
|
}
|
|
2420
3083
|
|
|
3084
|
+
void finalizeBuiltinUpdate(final String previousBundleName) {
|
|
3085
|
+
// Automatic builtin updates may overlap newer downloads; only an explicit reset cancels all work.
|
|
3086
|
+
this.sendStats("reset", this.getCurrentBundle().getVersionName(), previousBundleName);
|
|
3087
|
+
}
|
|
3088
|
+
|
|
2421
3089
|
boolean canSet(final BundleInfo bundle) {
|
|
2422
3090
|
return bundle != null && !bundle.isErrorStatus() && (bundle.isBuiltin() || this.bundleExists(bundle.getId()));
|
|
2423
3091
|
}
|
|
2424
3092
|
|
|
3093
|
+
/** Activates a stored bundle through the same version-floor checks as activation by id. */
|
|
2425
3094
|
public Boolean set(final BundleInfo bundle) {
|
|
2426
3095
|
return this.set(bundle.getId());
|
|
2427
3096
|
}
|
|
2428
3097
|
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
this.reset();
|
|
2433
|
-
return true;
|
|
3098
|
+
boolean setQueuedBundle(final BundleInfo bundle) {
|
|
3099
|
+
if (!bundle.isBuiltin()) {
|
|
3100
|
+
return this.set(bundle);
|
|
2434
3101
|
}
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
3102
|
+
this.prepareBuiltinTransition();
|
|
3103
|
+
// The installing worker finalizes statistics and its own queue entry after reload succeeds.
|
|
3104
|
+
return true;
|
|
3105
|
+
}
|
|
3106
|
+
|
|
3107
|
+
/** Activates a stored bundle only when its version satisfies builtinMinimum; builtin reset remains available. */
|
|
3108
|
+
public Boolean set(final String id) {
|
|
3109
|
+
synchronized (deleteLock) {
|
|
3110
|
+
final BundleInfo newBundle = this.getBundleInfo(id);
|
|
3111
|
+
if ((id != null && this.isBundleBeingDeleted(id)) || newBundle.isDeleting()) {
|
|
3112
|
+
return false;
|
|
3113
|
+
}
|
|
3114
|
+
if (newBundle.isBuiltin()) {
|
|
3115
|
+
this.reset();
|
|
3116
|
+
return true;
|
|
3117
|
+
}
|
|
3118
|
+
final File bundle;
|
|
3119
|
+
try {
|
|
3120
|
+
bundle = this.getBundleDirectory(id);
|
|
3121
|
+
} catch (IOException e) {
|
|
3122
|
+
logger.error("Invalid bundle id");
|
|
3123
|
+
logger.debug("Bundle ID: " + id + ", Error: " + e.getMessage());
|
|
3124
|
+
this.setBundleStatus(id, BundleStatus.ERROR);
|
|
3125
|
+
this.sendStats("set_fail", newBundle.getVersionName());
|
|
3126
|
+
return false;
|
|
3127
|
+
}
|
|
3128
|
+
logger.info("Setting next active bundle: " + id);
|
|
3129
|
+
if (this.bundleExists(id)) {
|
|
3130
|
+
try {
|
|
3131
|
+
this.requireVersionNotBelowBuiltin(newBundle.getVersionName());
|
|
3132
|
+
} catch (IOException e) {
|
|
3133
|
+
return false;
|
|
3134
|
+
}
|
|
3135
|
+
var currentBundleName = this.getCurrentBundle().getVersionName();
|
|
3136
|
+
this.setCurrentBundle(bundle);
|
|
3137
|
+
this.setBundleStatus(id, BundleStatus.PENDING);
|
|
3138
|
+
this.sendStats("set", newBundle.getVersionName(), currentBundleName);
|
|
3139
|
+
return true;
|
|
3140
|
+
}
|
|
2441
3141
|
this.setBundleStatus(id, BundleStatus.ERROR);
|
|
2442
3142
|
this.sendStats("set_fail", newBundle.getVersionName());
|
|
2443
3143
|
return false;
|
|
2444
3144
|
}
|
|
2445
|
-
logger.info("Setting next active bundle: " + id);
|
|
2446
|
-
if (this.bundleExists(id)) {
|
|
2447
|
-
var currentBundleName = this.getCurrentBundle().getVersionName();
|
|
2448
|
-
this.setCurrentBundle(bundle);
|
|
2449
|
-
this.setBundleStatus(id, BundleStatus.PENDING);
|
|
2450
|
-
this.sendStats("set", newBundle.getVersionName(), currentBundleName);
|
|
2451
|
-
return true;
|
|
2452
|
-
}
|
|
2453
|
-
this.setBundleStatus(id, BundleStatus.ERROR);
|
|
2454
|
-
this.sendStats("set_fail", newBundle.getVersionName());
|
|
2455
|
-
return false;
|
|
2456
3145
|
}
|
|
2457
3146
|
|
|
3147
|
+
/** Stages an installable pending bundle without mutating state when the native version floor rejects it. */
|
|
2458
3148
|
boolean stagePendingReload(final BundleInfo bundle) {
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
3149
|
+
synchronized (deleteLock) {
|
|
3150
|
+
if (bundle == null || bundle.isBuiltin() || !this.bundleExists(bundle.getId())) {
|
|
3151
|
+
return false;
|
|
3152
|
+
}
|
|
3153
|
+
try {
|
|
3154
|
+
this.requireVersionNotBelowBuiltin(bundle.getVersionName());
|
|
3155
|
+
this.setCurrentBundle(this.getBundleDirectory(bundle.getId()));
|
|
3156
|
+
return true;
|
|
3157
|
+
} catch (IOException e) {
|
|
3158
|
+
return false;
|
|
3159
|
+
}
|
|
2467
3160
|
}
|
|
2468
3161
|
}
|
|
2469
3162
|
|
|
2470
3163
|
boolean stagePreviewFallbackReload(final BundleInfo bundle) {
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
3164
|
+
synchronized (deleteLock) {
|
|
3165
|
+
if (bundle == null || bundle.isErrorStatus()) {
|
|
3166
|
+
return false;
|
|
3167
|
+
}
|
|
3168
|
+
if (bundle.isBuiltin()) {
|
|
3169
|
+
this.setCurrentBundle(new File("public"));
|
|
3170
|
+
return true;
|
|
3171
|
+
}
|
|
3172
|
+
if (!this.bundleExists(bundle.getId())) {
|
|
3173
|
+
return false;
|
|
3174
|
+
}
|
|
3175
|
+
try {
|
|
3176
|
+
this.setCurrentBundle(this.getBundleDirectory(bundle.getId()));
|
|
3177
|
+
return true;
|
|
3178
|
+
} catch (IOException e) {
|
|
3179
|
+
return false;
|
|
3180
|
+
}
|
|
2486
3181
|
}
|
|
2487
3182
|
}
|
|
2488
3183
|
|
|
@@ -2493,15 +3188,24 @@ public class CapgoUpdater {
|
|
|
2493
3188
|
this.sendStats("set", bundle.getVersionName(), previousBundleName);
|
|
2494
3189
|
}
|
|
2495
3190
|
|
|
2496
|
-
@Deprecated
|
|
2497
|
-
public void autoReset() {
|
|
2498
|
-
this.autoReset(this.versionCode == null ? "" : this.versionCode);
|
|
2499
|
-
}
|
|
2500
|
-
|
|
2501
3191
|
public void autoReset(final String currentNativeBuildVersion) {
|
|
2502
3192
|
this.autoReset(currentNativeBuildVersion, true);
|
|
2503
3193
|
}
|
|
2504
3194
|
|
|
3195
|
+
/** Enforces the floor on the final restored startup bundle without deleting retained bundle files or metadata. */
|
|
3196
|
+
void enforceStartupVersionFloor() {
|
|
3197
|
+
final BundleInfo currentBundle = this.getCurrentBundle();
|
|
3198
|
+
if (currentBundle.isBuiltin()) {
|
|
3199
|
+
return;
|
|
3200
|
+
}
|
|
3201
|
+
try {
|
|
3202
|
+
this.requireVersionNotBelowBuiltin(currentBundle.getVersionName());
|
|
3203
|
+
} catch (IOException e) {
|
|
3204
|
+
logger.info("Stored startup bundle is below the native version. Falling back to builtin.");
|
|
3205
|
+
this.reset();
|
|
3206
|
+
}
|
|
3207
|
+
}
|
|
3208
|
+
|
|
2505
3209
|
public void autoReset(final String currentNativeBuildVersion, final boolean resetWhenNativeVersionChanged) {
|
|
2506
3210
|
final BundleInfo currentBundle = this.getCurrentBundle();
|
|
2507
3211
|
if (!currentBundle.isBuiltin() && !this.bundleExists(currentBundle.getId())) {
|
|
@@ -2990,6 +3694,18 @@ public class CapgoUpdater {
|
|
|
2990
3694
|
}
|
|
2991
3695
|
}
|
|
2992
3696
|
callback.callback(ret);
|
|
3697
|
+
} catch (IOException e) {
|
|
3698
|
+
if (response.isSuccessful()) {
|
|
3699
|
+
onFailure(call, e);
|
|
3700
|
+
return;
|
|
3701
|
+
}
|
|
3702
|
+
final RemoteBlockResult rateLimit = checkAndHandleRateLimitResponse(response, "");
|
|
3703
|
+
Map<String, Object> retError = new HashMap<>();
|
|
3704
|
+
retError.put("message", rateLimit.blocked ? rateLimit.message : "Server error: " + response.code());
|
|
3705
|
+
retError.put("error", rateLimit.blocked ? rateLimit.error : "response_error");
|
|
3706
|
+
retError.put("kind", "failed");
|
|
3707
|
+
retError.put("statusCode", response.code());
|
|
3708
|
+
callback.callback(retError);
|
|
2993
3709
|
} catch (JSONException e) {
|
|
2994
3710
|
Map<String, Object> retError = new HashMap<>();
|
|
2995
3711
|
retError.put("message", "JSON parse error: " + e.getMessage());
|
|
@@ -3058,6 +3774,7 @@ public class CapgoUpdater {
|
|
|
3058
3774
|
|
|
3059
3775
|
// Clear persisted defaultChannel and revert to config value
|
|
3060
3776
|
editor.remove(defaultChannelKey);
|
|
3777
|
+
editor.remove(CapacitorUpdaterPlugin.DEFAULT_CHANNEL_FROM_SET_CHANNEL_PREF_KEY);
|
|
3061
3778
|
editor.apply();
|
|
3062
3779
|
this.defaultChannel = configDefaultChannel;
|
|
3063
3780
|
logger.info("Persisted defaultChannel cleared, reverted to config value: " + configDefaultChannel);
|
|
@@ -3068,16 +3785,6 @@ public class CapgoUpdater {
|
|
|
3068
3785
|
callback.callback(ret);
|
|
3069
3786
|
}
|
|
3070
3787
|
|
|
3071
|
-
public void setChannel(
|
|
3072
|
-
final String channel,
|
|
3073
|
-
final SharedPreferences.Editor editor,
|
|
3074
|
-
final String defaultChannelKey,
|
|
3075
|
-
final boolean allowSetDefaultChannel,
|
|
3076
|
-
final Callback callback
|
|
3077
|
-
) {
|
|
3078
|
-
this.setChannel(channel, editor, defaultChannelKey, allowSetDefaultChannel, "", callback);
|
|
3079
|
-
}
|
|
3080
|
-
|
|
3081
3788
|
public void setChannel(
|
|
3082
3789
|
final String channel,
|
|
3083
3790
|
final SharedPreferences.Editor editor,
|
|
@@ -3136,6 +3843,7 @@ public class CapgoUpdater {
|
|
|
3136
3843
|
// Server requested to unset channel (public channel was requested)
|
|
3137
3844
|
// Clear persisted defaultChannel and revert to config value
|
|
3138
3845
|
editor.remove(defaultChannelKey);
|
|
3846
|
+
editor.remove(CapacitorUpdaterPlugin.DEFAULT_CHANNEL_FROM_SET_CHANNEL_PREF_KEY);
|
|
3139
3847
|
editor.apply();
|
|
3140
3848
|
this.defaultChannel = configDefaultChannel;
|
|
3141
3849
|
logger.info("Public channel requested, channel override removed");
|
|
@@ -3143,6 +3851,7 @@ public class CapgoUpdater {
|
|
|
3143
3851
|
} else {
|
|
3144
3852
|
this.defaultChannel = channel;
|
|
3145
3853
|
editor.putString(defaultChannelKey, channel);
|
|
3854
|
+
editor.putBoolean(CapacitorUpdaterPlugin.DEFAULT_CHANNEL_FROM_SET_CHANNEL_PREF_KEY, true);
|
|
3146
3855
|
editor.apply();
|
|
3147
3856
|
logger.info("defaultChannel persisted locally: " + channel);
|
|
3148
3857
|
callback.callback(res);
|
|
@@ -3150,11 +3859,13 @@ public class CapgoUpdater {
|
|
|
3150
3859
|
});
|
|
3151
3860
|
}
|
|
3152
3861
|
|
|
3153
|
-
public void getChannel(
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3862
|
+
public void getChannel(
|
|
3863
|
+
final Callback callback,
|
|
3864
|
+
final SharedPreferences.Editor editor,
|
|
3865
|
+
final String defaultChannelKey,
|
|
3866
|
+
final String defaultChannelFromSetChannelKey,
|
|
3867
|
+
final String configDefaultChannel
|
|
3868
|
+
) {
|
|
3158
3869
|
if (isRemoteBlocked()) {
|
|
3159
3870
|
final RemoteBlockResult blocked = remoteBlockedClientError();
|
|
3160
3871
|
logger.debug("Skipping getChannel due to remote block (" + blocked.error + ").");
|
|
@@ -3265,7 +3976,14 @@ public class CapgoUpdater {
|
|
|
3265
3976
|
ret.put(key, jsonResponse.get(key));
|
|
3266
3977
|
}
|
|
3267
3978
|
}
|
|
3268
|
-
persistDefaultChannelFromResponse(
|
|
3979
|
+
persistDefaultChannelFromResponse(
|
|
3980
|
+
ret.get("channel"),
|
|
3981
|
+
ret.get("status"),
|
|
3982
|
+
editor,
|
|
3983
|
+
defaultChannelKey,
|
|
3984
|
+
defaultChannelFromSetChannelKey,
|
|
3985
|
+
configDefaultChannel
|
|
3986
|
+
);
|
|
3269
3987
|
logger.info("Channel get to \"" + ret);
|
|
3270
3988
|
callback.callback(ret);
|
|
3271
3989
|
} catch (JSONException e) {
|
|
@@ -3279,7 +3997,39 @@ public class CapgoUpdater {
|
|
|
3279
3997
|
);
|
|
3280
3998
|
}
|
|
3281
3999
|
|
|
3282
|
-
void persistDefaultChannelFromResponse(
|
|
4000
|
+
void persistDefaultChannelFromResponse(
|
|
4001
|
+
final Object channel,
|
|
4002
|
+
final Object status,
|
|
4003
|
+
final SharedPreferences.Editor editor,
|
|
4004
|
+
final String defaultChannelKey,
|
|
4005
|
+
final String defaultChannelFromSetChannelKey,
|
|
4006
|
+
final String configDefaultChannel
|
|
4007
|
+
) {
|
|
4008
|
+
if (status instanceof String && "default".equals(status)) {
|
|
4009
|
+
if (channel instanceof String) {
|
|
4010
|
+
final String channelName = ((String) channel).trim();
|
|
4011
|
+
if (!channelName.isEmpty() && channelName.equals(this.defaultChannel)) {
|
|
4012
|
+
// Match iOS: compare in-memory defaultChannel; iOS also consults prefs when memory diverges.
|
|
4013
|
+
boolean fromSetChannel = false;
|
|
4014
|
+
if (this.prefs != null && defaultChannelFromSetChannelKey != null && !defaultChannelFromSetChannelKey.isEmpty()) {
|
|
4015
|
+
fromSetChannel = this.prefs.getBoolean(defaultChannelFromSetChannelKey, false);
|
|
4016
|
+
}
|
|
4017
|
+
if (!fromSetChannel) {
|
|
4018
|
+
this.defaultChannel = configDefaultChannel != null ? configDefaultChannel : "";
|
|
4019
|
+
if (editor != null && defaultChannelKey != null && !defaultChannelKey.isEmpty()) {
|
|
4020
|
+
editor.remove(defaultChannelKey);
|
|
4021
|
+
if (defaultChannelFromSetChannelKey != null && !defaultChannelFromSetChannelKey.isEmpty()) {
|
|
4022
|
+
editor.remove(defaultChannelFromSetChannelKey);
|
|
4023
|
+
}
|
|
4024
|
+
editor.apply();
|
|
4025
|
+
}
|
|
4026
|
+
logger.info("defaultChannel cleared stale getChannel() fallback: " + channelName);
|
|
4027
|
+
}
|
|
4028
|
+
}
|
|
4029
|
+
}
|
|
4030
|
+
return;
|
|
4031
|
+
}
|
|
4032
|
+
|
|
3283
4033
|
if (!(channel instanceof String)) {
|
|
3284
4034
|
return;
|
|
3285
4035
|
}
|
|
@@ -3289,9 +4039,19 @@ public class CapgoUpdater {
|
|
|
3289
4039
|
return;
|
|
3290
4040
|
}
|
|
3291
4041
|
|
|
4042
|
+
final boolean preserveSetChannelMarker =
|
|
4043
|
+
this.prefs != null &&
|
|
4044
|
+
defaultChannelFromSetChannelKey != null &&
|
|
4045
|
+
!defaultChannelFromSetChannelKey.isEmpty() &&
|
|
4046
|
+
this.prefs.getBoolean(defaultChannelFromSetChannelKey, false) &&
|
|
4047
|
+
channelName.equals(this.defaultChannel);
|
|
4048
|
+
|
|
3292
4049
|
this.defaultChannel = channelName;
|
|
3293
4050
|
if (editor != null && defaultChannelKey != null && !defaultChannelKey.isEmpty()) {
|
|
3294
4051
|
editor.putString(defaultChannelKey, channelName);
|
|
4052
|
+
if (defaultChannelFromSetChannelKey != null && !defaultChannelFromSetChannelKey.isEmpty() && !preserveSetChannelMarker) {
|
|
4053
|
+
editor.putBoolean(defaultChannelFromSetChannelKey, false);
|
|
4054
|
+
}
|
|
3295
4055
|
editor.apply();
|
|
3296
4056
|
}
|
|
3297
4057
|
logger.info("defaultChannel synchronized from getChannel(): " + channelName);
|
|
@@ -4252,13 +5012,17 @@ public class CapgoUpdater {
|
|
|
4252
5012
|
}
|
|
4253
5013
|
|
|
4254
5014
|
public boolean clearNextBundleIfCurrent() {
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
5015
|
+
return this.clearNextBundleIfMatches(this.getCurrentBundle().getId());
|
|
5016
|
+
}
|
|
5017
|
+
|
|
5018
|
+
boolean clearNextBundleIfMatches(final String installedBundleId) {
|
|
5019
|
+
synchronized (nextBundleLock) {
|
|
5020
|
+
final BundleInfo next = this.getNextBundle();
|
|
5021
|
+
if (next == null || !next.getId().equals(installedBundleId)) {
|
|
5022
|
+
return false;
|
|
5023
|
+
}
|
|
5024
|
+
return this.setNextBundle(null);
|
|
4258
5025
|
}
|
|
4259
|
-
logger.info("Next bundle " + next.getId() + " is already the current bundle. Clearing next.");
|
|
4260
|
-
this.setNextBundle(null);
|
|
4261
|
-
return true;
|
|
4262
5026
|
}
|
|
4263
5027
|
|
|
4264
5028
|
public BundleInfo getPreviewFallbackBundle() {
|
|
@@ -4287,23 +5051,51 @@ public class CapgoUpdater {
|
|
|
4287
5051
|
}
|
|
4288
5052
|
|
|
4289
5053
|
public boolean setNextBundle(final String next) {
|
|
5054
|
+
return this.setNextBundle(next, false);
|
|
5055
|
+
}
|
|
5056
|
+
|
|
5057
|
+
boolean clearNextBundleForDirectInstall(final BundleInfo candidate, final java.util.function.BooleanSupplier admitInstall) {
|
|
5058
|
+
synchronized (deleteLock) {
|
|
5059
|
+
if (candidate == null || (!candidate.isBuiltin() && !this.bundleExists(candidate.getId()))) {
|
|
5060
|
+
return false;
|
|
5061
|
+
}
|
|
5062
|
+
synchronized (nextBundleLock) {
|
|
5063
|
+
if (!admitInstall.getAsBoolean()) {
|
|
5064
|
+
return false;
|
|
5065
|
+
}
|
|
5066
|
+
// Supersede the old queue only after validating the candidate under deletion admission.
|
|
5067
|
+
return this.setNextBundle(null);
|
|
5068
|
+
}
|
|
5069
|
+
}
|
|
5070
|
+
}
|
|
5071
|
+
|
|
5072
|
+
boolean setNextBundleIfEmpty(final String next) {
|
|
5073
|
+
return this.setNextBundle(next, true);
|
|
5074
|
+
}
|
|
5075
|
+
|
|
5076
|
+
private boolean setNextBundle(final String next, final boolean onlyIfEmpty) {
|
|
4290
5077
|
BundleInfo bundleToNotify = null;
|
|
4291
|
-
|
|
4292
|
-
this.
|
|
4293
|
-
} else {
|
|
4294
|
-
final BundleInfo newBundle = this.getBundleInfo(next);
|
|
4295
|
-
if (!newBundle.isBuiltin() && !this.bundleExists(next)) {
|
|
5078
|
+
synchronized (nextBundleLock) {
|
|
5079
|
+
if (onlyIfEmpty && this.getNextBundle() != null) {
|
|
4296
5080
|
return false;
|
|
4297
5081
|
}
|
|
4298
|
-
if (next
|
|
4299
|
-
|
|
4300
|
-
|
|
5082
|
+
if (next == null) {
|
|
5083
|
+
this.editor.remove(NEXT_VERSION);
|
|
5084
|
+
} else {
|
|
5085
|
+
final BundleInfo newBundle = this.getBundleInfo(next);
|
|
5086
|
+
if (!newBundle.isBuiltin() && !this.bundleExists(next)) {
|
|
5087
|
+
return false;
|
|
5088
|
+
}
|
|
5089
|
+
if (next.equals(this.getCurrentBundleId()) && BundleStatus.SUCCESS == newBundle.getStatus()) {
|
|
5090
|
+
logger.info("Bundle " + next + " is already the current successful bundle. Skip next().");
|
|
5091
|
+
return true;
|
|
5092
|
+
}
|
|
5093
|
+
this.editor.putString(NEXT_VERSION, next);
|
|
5094
|
+
this.setBundleStatus(next, BundleStatus.PENDING);
|
|
5095
|
+
bundleToNotify = newBundle;
|
|
4301
5096
|
}
|
|
4302
|
-
this.editor.
|
|
4303
|
-
this.setBundleStatus(next, BundleStatus.PENDING);
|
|
4304
|
-
bundleToNotify = newBundle;
|
|
5097
|
+
this.editor.commit();
|
|
4305
5098
|
}
|
|
4306
|
-
this.editor.commit();
|
|
4307
5099
|
if (bundleToNotify != null) {
|
|
4308
5100
|
this.sendStats("set_next", bundleToNotify.getVersionName(), this.getCurrentBundle().getVersionName());
|
|
4309
5101
|
final Map<String, Object> payload = new HashMap<>();
|