Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 44d164e

Browse files
committed
use cr62 download api
fix #11326 requires brave/muon#342
1 parent 5cee273 commit 44d164e

File tree

6 files changed

+62
-109
lines changed

6 files changed

+62
-109
lines changed

app/filtering.js

+22-27
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ const config = require('../js/constants/config')
2323
const locale = require('./locale')
2424
const {isSessionPartition} = require('../js/state/frameStateUtil')
2525
const ipcMain = electron.ipcMain
26-
const dialog = electron.dialog
2726
const app = electron.app
28-
const uuid = require('uuid')
2927
const path = require('path')
3028
const getOrigin = require('../js/state/siteUtil').getOrigin
3129
const {adBlockResourceName} = require('./adBlock')
@@ -552,6 +550,10 @@ function updateDownloadState (downloadId, item, state) {
552550
}
553551

554552
function registerForDownloadListener (session) {
553+
session.on('default-download-directory-changed', (e, newPath) => {
554+
appActions.changeSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH, newPath)
555+
})
556+
555557
session.on('will-download', function (event, item, webContents) {
556558
if (webContents.isDestroyed()) {
557559
event.preventDefault()
@@ -567,37 +569,30 @@ function registerForDownloadListener (session) {
567569
webContents.forceClose()
568570
}
569571

570-
// special handling for data URLs where another 'will-download' event handler is trying to suggest a filename via item.setSavePath
571-
// see the IPC handler for RENDER_URL_TO_PDF in app/index.js for example
572-
let itemFilename
573-
if (item.getURL().match(/^data:/) && item.getSavePath()) {
574-
itemFilename = path.basename(item.getSavePath())
575-
} else {
576-
itemFilename = item.getFilename()
577-
}
578-
579-
const defaultPath = path.join(getSetting(settings.DOWNLOAD_DEFAULT_PATH) || getSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH) || app.getPath('downloads'), itemFilename)
580-
const savePath = ((process.env.SPECTRON || (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation())) ? defaultPath : dialog.showSaveDialog(win, { defaultPath }))
581-
582-
// User cancelled out of save dialog prompt
583-
if (!savePath) {
584-
event.preventDefault()
585-
return
572+
if (getSetting(settings.DOWNLOAD_ALWAYS_ASK)) {
573+
item.setPrompt(true)
586574
}
587575

588-
item.setSavePath(savePath)
589-
appActions.changeSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH, path.dirname(savePath))
590-
591-
const downloadId = uuid.v4()
592-
updateDownloadState(downloadId, item, downloadStates.PENDING)
593-
if (win) {
594-
win.webContents.send(messages.SHOW_DOWNLOADS_TOOLBAR)
595-
}
596-
item.on('updated', function () {
576+
const downloadId = item.getGuid()
577+
item.on('updated', function (e, st) {
578+
if (!item.getSavePath()) {
579+
return
580+
}
597581
const state = item.isPaused() ? downloadStates.PAUSED : downloadStates.IN_PROGRESS
598582
updateDownloadState(downloadId, item, state)
583+
if (win) {
584+
win.webContents.send(messages.SHOW_DOWNLOADS_TOOLBAR)
585+
}
586+
item.on('removed', function () {
587+
updateElectronDownloadItem(downloadId, item, downloadStates.CANCELLED)
588+
appActions.mergeDownloadDetail(downloadId)
589+
})
599590
})
591+
600592
item.on('done', function (e, state) {
593+
if (!item.getSavePath()) {
594+
return
595+
}
601596
updateDownloadState(downloadId, item, state)
602597
})
603598
})

app/pdf.js

+32-65
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,50 @@
44

55
'use strict'
66

7-
const electron = require('electron')
8-
const config = require('../js/constants/config')
9-
const BrowserWindow = electron.BrowserWindow
10-
11-
const renderUrlToPdf = (appState, action) => {
12-
let url = action.url
13-
let savePath = action.savePath
14-
let openAfterwards = action.openAfterwards
7+
const tabs = require('./browser/tabs')
8+
const {fileUrl} = require('../../js/lib/appUrlUtil')
9+
const {getWebContents} = require('./browser/webContentsCache')
10+
11+
const renderToPdf = (appState, action) => {
12+
const tab = getWebContents(action.tabId)
13+
if (!tab || tab.isDestroyed()) {
14+
return
15+
}
1516

16-
let currentBw = BrowserWindow.getFocusedWindow()
17+
const savePath = action.savePath
18+
const openAfterwards = action.openAfterwards
1719

18-
let bw = new BrowserWindow({
19-
show: false,
20-
width: 0,
21-
height: 0,
22-
focusable: false,
23-
backgroundColor: '#ffffff',
24-
webPreferences: {
25-
partition: 'default'
20+
tab.printToPDF({}, function (err, data) {
21+
if (err) {
22+
throw err
2623
}
27-
})
28-
29-
let wv = bw.webContents
30-
31-
let whenReadyToGeneratePDF = () => {
32-
wv.printToPDF({}, function (err, data) {
33-
if (err) {
34-
throw err
35-
}
3624

37-
let pdfDataURI = 'data:application/pdf;base64,' + data.toString('base64')
38-
39-
// need to put our event handler first so we can set filename
40-
// specifically, needs to execute ahead of app/filtering.js:registerForDownloadListener (which opens the dialog box)
41-
let listeners = wv.session.listeners('will-download')
42-
wv.session.removeAllListeners('will-download')
43-
44-
wv.downloadURL(pdfDataURI, true)
45-
wv.session.once('will-download', function (event, item) {
46-
if (savePath) {
47-
item.setSavePath(savePath)
48-
}
25+
if (tab.isDestroyed()) {
26+
return
27+
}
4928

50-
item.once('done', function (event, state) {
51-
if (state === 'completed') {
52-
let finalSavePath = item && item.getSavePath()
29+
let pdfDataURI = 'data:application/pdf;base64,' + data.toString('base64')
30+
tab.downloadURL(pdfDataURI, true, savePath, (downloadItem) => {
31+
downloadItem.once('done', function (event, state) {
32+
if (state === 'completed') {
33+
let finalSavePath = downloadItem.getSavePath()
5334

54-
if (openAfterwards && savePath) {
55-
currentBw.webContents.loadURL('file://' + finalSavePath)
35+
if (openAfterwards && finalSavePath) {
36+
let createProperties = {
37+
url: fileUrl(finalSavePath)
5638
}
39+
if (tab && !tab.isDestoyed()) {
40+
createProperties.openerTabId = tab.getId()
41+
}
42+
tabs.create(createProperties)
5743
}
58-
59-
if (bw) {
60-
try {
61-
bw.close()
62-
} catch (exc) {}
63-
}
64-
})
65-
})
66-
// add back other event handlers (esp. add/filtering.js:registerForDownloadListener which opens the dialog box)
67-
listeners.forEach(function (listener) {
68-
wv.session.on('will-download', listener)
44+
}
6945
})
7046
})
71-
}
72-
73-
let afterLoaded = () => {
74-
let removeCharEncodingArtifactJS = 'document.body.outerHTML = document.body.outerHTML.replace(/Â/g, "")'
75-
wv.executeScriptInTab(config.braveExtensionId, removeCharEncodingArtifactJS, {}, whenReadyToGeneratePDF)
76-
}
77-
78-
bw.loadURL(url)
79-
wv.on('did-finish-load', afterLoaded)
80-
47+
})
8148
return appState
8249
}
8350

8451
module.exports = {
85-
renderUrlToPdf
52+
renderToPdf
8653
}

js/about/aboutActions.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,11 @@ const aboutActions = {
351351
},
352352

353353
/**
354-
* Dispatches a message to render a URL into a PDF file
354+
* Dispatches a message to render the tab into a PDF file
355355
*/
356-
renderUrlToPdf: function (url, savePath) {
356+
renderToPdf: function (savePath) {
357357
aboutActions.dispatchAction({
358-
actionType: appConstants.APP_RENDER_URL_TO_PDF,
359-
url: url,
358+
actionType: appConstants.APP_RENDER_TO_PDF,
360359
savePath: savePath
361360
})
362361
}

js/about/contributionStatement.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
const React = require('react')
6-
const ReactDOM = require('react-dom')
76
const Immutable = require('immutable')
87
const {makeImmutable} = require('../../app/common/state/immutableUtil')
98
const {getBase64FromImageUrl} = require('../lib/imageUtil')
@@ -129,20 +128,13 @@ class ContributionStatement extends React.Component {
129128
getBase64FromImageUrl(verifiedIcon).then(src => this.setState({verifiedIcon: src}))
130129
}
131130

132-
get htmlDataURL () {
133-
let generatedStylesheet = document.head.querySelector('style').outerHTML
134-
let dataURL = 'data:text/html,' + encodeURIComponent('<html><head><meta charset="utf-8">' + generatedStylesheet + '</head><body>' + ReactDOM.findDOMNode(this).outerHTML + '</body></html>')
135-
136-
return dataURL
137-
}
138-
139131
receiptFileName (transaction) {
140132
transaction = makeImmutable(transaction || this.transaction)
141133
return `${transaction.get('exportFilenamePrefix')}.pdf`
142134
}
143135

144136
renderPdf () {
145-
aboutActions.renderUrlToPdf(this.htmlDataURL, this.receiptFileName())
137+
aboutActions.renderToPdf(this.receiptFileName())
146138
}
147139

148140
get transaction () {

js/constants/appConstants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const appConstants = {
7777
APP_DEFAULT_BROWSER_UPDATED: _,
7878
APP_DEFAULT_BROWSER_CHECK_COMPLETE: _,
7979
APP_POPULATE_HISTORY: _,
80-
APP_RENDER_URL_TO_PDF: _,
80+
APP_RENDER_TO_PDF: _,
8181
APP_DATA_URL_COPIED: _,
8282
APP_DOWNLOAD_REVEALED: _,
8383
APP_DOWNLOAD_OPENED: _,

js/stores/appStore.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ const handleAppAction = (action) => {
531531
break
532532
case appConstants.APP_CHANGE_SETTING:
533533
appState = appState.setIn(['settings', action.key], action.value)
534-
handleChangeSettingAction(action.key, action.value)
534+
setImmediate(handleChangeSettingAction.bind(null, action.key, action.value))
535535
break
536536
case appConstants.APP_SET_SKIP_SYNC:
537537
{
@@ -749,9 +749,9 @@ const handleAppAction = (action) => {
749749
calculateTopSites(false)
750750
}
751751
break
752-
case appConstants.APP_RENDER_URL_TO_PDF:
752+
case appConstants.APP_RENDER_TO_PDF:
753753
const pdf = require('../../app/pdf')
754-
appState = pdf.renderUrlToPdf(appState, action)
754+
appState = pdf.renderToPdf(appState, action)
755755
break
756756
case appConstants.APP_SET_OBJECT_ID:
757757
let obj = appState.getIn(action.objectPath)

0 commit comments

Comments
 (0)