Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Offline): Fix some aborted downloads continue to download #7842

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/offline/download_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ shaka.offline.DownloadManager = class {

/** @private {shaka.offline.DownloadProgressEstimator} */
this.estimator_ = new shaka.offline.DownloadProgressEstimator();

/** @private {boolean} */
this.aborted_ = false;
}

/** @override */
Expand All @@ -97,11 +100,19 @@ shaka.offline.DownloadManager = class {
* aborted.
*/
abortAll() {
this.aborted_ = true;
const promises = this.abortCallbacks_.map((callback) => callback());
this.abortCallbacks_ = [];
return Promise.all(promises);
}

/**
* @return {boolean}
*/
isAborted() {
return this.aborted_;
}

/**
* Adds a byte length to the download estimate.
*
Expand Down
26 changes: 18 additions & 8 deletions lib/offline/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,22 @@ shaka.offline.Storage = class {
let pendingManifestUpdates = {};
let pendingDataSize = 0;

const ensureNotAbortedOrDestroyed = () => {
if (this.destroyer_.destroyed() || downloader.isAborted()) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.STORAGE,
shaka.util.Error.Code.OPERATION_ABORTED);
}
};

/**
* @param {!Array.<!shaka.offline.DownloadInfo>} toDownload
* @param {boolean} updateDRM
*/
const download = async (toDownload, updateDRM) => {
for (const download of toDownload) {
ensureNotAbortedOrDestroyed();
const request = download.makeSegmentRequest(config);
const estimateId = download.estimateId;
const isInitSegment = download.isInitSegment;
Expand All @@ -541,7 +551,7 @@ shaka.offline.Storage = class {
}
// Store the data.
const dataKeys = await storage.addSegments([{data}]);
this.ensureNotDestroyed_();
ensureNotAbortedOrDestroyed();

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

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

// Copy these and reset them now, before calling await.
Expand All @@ -595,12 +605,12 @@ shaka.offline.Storage = class {
await shaka.offline.Storage.assignSegmentsToManifest(
storage, manifestId, manifestDB, manifestUpdates, dataSize,
() => this.ensureNotDestroyed_());
this.ensureNotDestroyed_();
ensureNotAbortedOrDestroyed();
}

if (!usingBgFetch) {
await download(toDownload, false);
this.ensureNotDestroyed_();
ensureNotAbortedOrDestroyed();

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

await shaka.offline.Storage.assignSegmentsToManifest(
storage, manifestId, manifestDB, manifestUpdates, dataSize,
() => this.ensureNotDestroyed_());
this.ensureNotDestroyed_();
() => ensureNotAbortedOrDestroyed());
ensureNotAbortedOrDestroyed();

goog.asserts.assert(
!manifestDB.isIncomplete, 'The manifest should be complete by now');
Expand Down
Loading