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

Add strategy for handling download error #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 36 additions & 3 deletions background/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var ApplySessionDuration = true;
var DebugLogs = false;
var RoleArns = {};
var LF = '\n';
var downloadFileId;

// When this background process starts, load variables from chrome storage
// from saved Extension Options
Expand Down Expand Up @@ -252,7 +253,38 @@ function assumeAdditionalRole(profileList, index, AccessKeyId, SecretAccessKey,
});
}

// Triggers download of the generated file
// Updated the downloadFileId value
function triggerDownload(conflictAction = 'overwrite') {
chrome.downloads.download({ url: doc, filename: FileName, conflictAction, saveAs: false }, (id) => {
if(!id) return;

downloadFileId = id;
});
}

// Check for errors during the download
function handleDownloadsChanged({id, state}) {
if (!state || id !== downloadFileId) return;
let retry = false;

if (state.current === 'interrupted') {
// If download was interrupted, ask for retry preference
retry = window.confirm(`An error occurred while downloading "${FileName}". Would you like to retry?`);

if (retry) {
// Triggers download attempt with conflictAction set as 'prompt'
// This might give the user a chance to check for possible causes of the error
triggerDownload('prompt');
return;
}
}

// If download was successfull or retry was cancelled, remove the event listener
if (state.current === 'complete' || !retry) {
chrome.downloads.onChanged.removeListener(handleDownloadsChanged);
}
}

// Called from either extractPrincipalPlusRoleAndAssumeRole (if RoleArns dict is empty)
// Otherwise called from assumeAdditionalRole as soon as all roles from RoleArns have been assumed
Expand All @@ -265,12 +297,13 @@ function outputDocAsDownload(docContent) {
if (DebugLogs) {
console.log('DEBUG: Blob URL:' + doc);
}
// Triggers download of the generated file
chrome.downloads.download({ url: doc, filename: FileName, conflictAction: 'overwrite', saveAs: false });
// Listen to download changes
chrome.downloads.onChanged.addListener(handleDownloadsChanged);
// Triggers the first download attempt with conflictAction set as 'overwrite'
triggerDownload();
}



// This Listener receives messages from options.js and popup.js
// Received messages are meant to affect the background process.
chrome.runtime.onMessage.addListener(
Expand Down