fix lifetime of downloadItem class when defaultdownload canceled

This commit is contained in:
Robo 2016-02-02 15:54:51 +05:30
parent 045e42a10c
commit 2819af9586
6 changed files with 100 additions and 27 deletions

View file

@ -102,23 +102,35 @@ app.on('ready', function() {
// For session's download test, listen 'will-download' event in browser, and
// reply the result to renderer for verifying
var downloadFilePath = path.join(__dirname, '..', 'fixtures', 'mock.pdf');
ipcMain.on('set-download-option', function(event, need_cancel) {
window.webContents.session.once('will-download',
function(e, item, webContents) {
item.setSavePath(downloadFilePath);
item.on('done', function(e, state) {
window.webContents.send('download-done',
state,
item.getURL(),
item.getMimeType(),
item.getReceivedBytes(),
item.getTotalBytes(),
item.getContentDisposition(),
item.getFilename());
});
if (need_cancel)
item.cancel();
ipcMain.on('set-download-option', function(event, need_cancel, prevent_default) {
window.webContents.session.once('will-download', function(e, item, webContents) {
if (prevent_default) {
e.preventDefault();
const url = item.getURL();
const filename = item.getFilename();
setImmediate(function() {
try {
item.getURL();
} catch(err) {
window.webContents.send('download-error', url, filename, err.message);
}
});
} else {
item.setSavePath(downloadFilePath);
item.on('done', function(e, state) {
window.webContents.send('download-done',
state,
item.getURL(),
item.getMimeType(),
item.getReceivedBytes(),
item.getTotalBytes(),
item.getContentDisposition(),
item.getFilename());
});
if (need_cancel)
item.cancel();
}
});
event.returnValue = "done";
});
});