Skip to content

Commit fb8086b

Browse files
authored
fix(Offline): Fix some aborted downloads continue to download (#7842)
Fixes #6311
1 parent bc0e1c6 commit fb8086b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/offline/download_manager.js

+11
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ shaka.offline.DownloadManager = class {
7575

7676
/** @private {shaka.offline.DownloadProgressEstimator} */
7777
this.estimator_ = new shaka.offline.DownloadProgressEstimator();
78+
79+
/** @private {boolean} */
80+
this.aborted_ = false;
7881
}
7982

8083
/** @override */
@@ -97,11 +100,19 @@ shaka.offline.DownloadManager = class {
97100
* aborted.
98101
*/
99102
abortAll() {
103+
this.aborted_ = true;
100104
const promises = this.abortCallbacks_.map((callback) => callback());
101105
this.abortCallbacks_ = [];
102106
return Promise.all(promises);
103107
}
104108

109+
/**
110+
* @return {boolean}
111+
*/
112+
isAborted() {
113+
return this.aborted_;
114+
}
115+
105116
/**
106117
* Adds a byte length to the download estimate.
107118
*

lib/offline/storage.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,22 @@ shaka.offline.Storage = class {
521521
let pendingManifestUpdates = {};
522522
let pendingDataSize = 0;
523523

524+
const ensureNotAbortedOrDestroyed = () => {
525+
if (this.destroyer_.destroyed() || downloader.isAborted()) {
526+
throw new shaka.util.Error(
527+
shaka.util.Error.Severity.CRITICAL,
528+
shaka.util.Error.Category.STORAGE,
529+
shaka.util.Error.Code.OPERATION_ABORTED);
530+
}
531+
};
532+
524533
/**
525534
* @param {!Array.<!shaka.offline.DownloadInfo>} toDownload
526535
* @param {boolean} updateDRM
527536
*/
528537
const download = async (toDownload, updateDRM) => {
529538
for (const download of toDownload) {
539+
ensureNotAbortedOrDestroyed();
530540
const request = download.makeSegmentRequest(config);
531541
const estimateId = download.estimateId;
532542
const isInitSegment = download.isInitSegment;
@@ -541,7 +551,7 @@ shaka.offline.Storage = class {
541551
}
542552
// Store the data.
543553
const dataKeys = await storage.addSegments([{data}]);
544-
this.ensureNotDestroyed_();
554+
ensureNotAbortedOrDestroyed();
545555

546556
// Store the necessary update to the manifest, to be processed later.
547557
pendingManifestUpdates[id] = dataKeys[0];
@@ -560,14 +570,14 @@ shaka.offline.Storage = class {
560570
}
561571
}
562572
await downloader.waitToFinish();
573+
ensureNotAbortedOrDestroyed();
563574

564-
if (updateDRM) {
575+
if (updateDRM && !downloader.isAborted()) {
565576
// Re-store the manifest, to attach session IDs.
566577
// These were (maybe) discovered inside the downloader; we can only add
567578
// them now, at the end, since the manifestDB is in flux during the
568579
// process of downloading and storing, and assignSegmentsToManifest
569580
// does not know about the DRM engine.
570-
this.ensureNotDestroyed_();
571581
this.setManifestDrmFields_(
572582
manifest, manifestDB, drmEngine, usePersistentLicense);
573583
await storage.updateManifest(manifestId, manifestDB);
@@ -583,7 +593,7 @@ shaka.offline.Storage = class {
583593
// init data from the init segments, download those first before
584594
// anything else.
585595
await download(toDownload.filter((info) => info.isInitSegment), true);
586-
this.ensureNotDestroyed_();
596+
ensureNotAbortedOrDestroyed();
587597
toDownload = toDownload.filter((info) => !info.isInitSegment);
588598

589599
// Copy these and reset them now, before calling await.
@@ -595,12 +605,12 @@ shaka.offline.Storage = class {
595605
await shaka.offline.Storage.assignSegmentsToManifest(
596606
storage, manifestId, manifestDB, manifestUpdates, dataSize,
597607
() => this.ensureNotDestroyed_());
598-
this.ensureNotDestroyed_();
608+
ensureNotAbortedOrDestroyed();
599609
}
600610

601611
if (!usingBgFetch) {
602612
await download(toDownload, false);
603-
this.ensureNotDestroyed_();
613+
ensureNotAbortedOrDestroyed();
604614

605615
// Copy these and reset them now, before calling await.
606616
const manifestUpdates = pendingManifestUpdates;
@@ -610,8 +620,8 @@ shaka.offline.Storage = class {
610620

611621
await shaka.offline.Storage.assignSegmentsToManifest(
612622
storage, manifestId, manifestDB, manifestUpdates, dataSize,
613-
() => this.ensureNotDestroyed_());
614-
this.ensureNotDestroyed_();
623+
() => ensureNotAbortedOrDestroyed());
624+
ensureNotAbortedOrDestroyed();
615625

616626
goog.asserts.assert(
617627
!manifestDB.isIncomplete, 'The manifest should be complete by now');

0 commit comments

Comments
 (0)