This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor download handling so it works with node removal from window …
…renderer Auditors: @bsclifton Fix #5947
- Loading branch information
Showing
19 changed files
with
684 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const downloadStates = require('../../js/constants/downloadStates') | ||
|
||
/** | ||
* Maps downloadId to an electron download-item | ||
*/ | ||
const downloadMap = {} | ||
|
||
module.exports.updateElectronDownloadItem = (downloadId, item, state) => { | ||
if (state === downloadStates.INTERRUPTED || state === downloadStates.CANCELLED || state === downloadStates.COMPLETED) { | ||
delete downloadMap[downloadId] | ||
} else { | ||
downloadMap[downloadId] = item | ||
} | ||
} | ||
|
||
module.exports.cancelDownload = (downloadId) => | ||
downloadMap[downloadId] && downloadMap[downloadId].cancel() | ||
|
||
module.exports.pauseDownload = (downloadId) => | ||
downloadMap[downloadId] && downloadMap[downloadId].pause() | ||
|
||
module.exports.resumeDownload = (downloadId) => | ||
downloadMap[downloadId] && downloadMap[downloadId].resume() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const appConstants = require('../../../js/constants/appConstants') | ||
const downloadStates = require('../../../js/constants/downloadStates') | ||
const {clipboard, BrowserWindow, shell} = require('electron') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const {cancelDownload, pauseDownload, resumeDownload} = require('../electronDownloadItem') | ||
const {CANCEL, PAUSE, RESUME} = require('../../common/constants/electronDownloadItemActions') | ||
|
||
const downloadsReducer = (state, action) => { | ||
const download = action.downloadId ? state.getIn(['downloads', action.downloadId]) : undefined | ||
if (!download && | ||
![appConstants.APP_MERGE_DOWNLOAD_DETAIL, | ||
appConstants.APP_CLEAR_COMPLETED_DOWNLOADS].includes(action.actionType)) { | ||
return state | ||
} | ||
switch (action.actionType) { | ||
case appConstants.APP_DOWNLOAD_REVEALED: | ||
fs.exists(download.get('savePath'), (exists) => { | ||
if (exists) { | ||
shell.showItemInFolder(download.get('savePath')) | ||
} else { | ||
shell.openItem(path.dirname(download.get('savePath'))) | ||
} | ||
}) | ||
break | ||
case appConstants.APP_DOWNLOAD_OPENED: | ||
fs.exists(download.get('savePath'), (exists) => { | ||
if (exists) { | ||
shell.openItem(download.get('savePath')) | ||
} else { | ||
shell.beep() | ||
} | ||
}) | ||
break | ||
case appConstants.APP_DOWNLOAD_ACTION_PERFORMED: | ||
switch (action.downloadAction) { | ||
case CANCEL: | ||
// It's important to update state before the cancel since it'll remove the reference | ||
state = state.setIn(['downloads', action.downloadId, 'state'], downloadStates.CANCELLED) | ||
cancelDownload(action.downloadId) | ||
break | ||
case PAUSE: | ||
pauseDownload(action.downloadId) | ||
state = state.setIn(['downloads', action.downloadId, 'state'], downloadStates.PAUSED) | ||
break | ||
case RESUME: | ||
resumeDownload(action.downloadId) | ||
state = state.setIn(['downloads', action.downloadId, 'state'], downloadStates.IN_PROGRESS) | ||
break | ||
} | ||
break | ||
case appConstants.APP_DOWNLOAD_COPIED_TO_CLIPBOARD: | ||
clipboard.writeText(download.get('url')) | ||
break | ||
case appConstants.APP_DOWNLOAD_DELETED: | ||
shell.moveItemToTrash(download.get('savePath')) | ||
state = state.deleteIn(['downloads', action.downloadId]) | ||
break | ||
case appConstants.APP_DOWNLOAD_CLEARED: | ||
state = state.deleteIn(['downloads', action.downloadId]) | ||
break | ||
case appConstants.APP_DOWNLOAD_REDOWNLOADED: | ||
const win = BrowserWindow.getFocusedWindow() | ||
if (win) { | ||
win.webContents.downloadURL(download.get('url')) | ||
state = state.deleteIn(['downloads', action.downloadId]) | ||
} else { | ||
shell.beep() | ||
} | ||
break | ||
case appConstants.APP_MERGE_DOWNLOAD_DETAIL: | ||
if (action.downloadDetail) { | ||
state = state.mergeIn(['downloads', action.downloadId], action.downloadDetail) | ||
} else { | ||
state = state.deleteIn(['downloads', action.downloadId]) | ||
} | ||
break | ||
case appConstants.APP_CLEAR_COMPLETED_DOWNLOADS: | ||
if (state.get('downloads')) { | ||
const downloads = state.get('downloads') | ||
.filter((download) => | ||
![downloadStates.COMPLETED, downloadStates.INTERRUPTED, downloadStates.CANCELLED].includes(download.get('state'))) | ||
state = state.set('downloads', downloads) | ||
} | ||
break | ||
} | ||
return state | ||
} | ||
|
||
module.exports = downloadsReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.