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

Commit 6169150

Browse files
bridiverbbondy
authored andcommitted
use cr62 download api
fix #11326 requires brave/muon#342
1 parent 760a2b0 commit 6169150

File tree

6 files changed

+60
-121
lines changed

6 files changed

+60
-121
lines changed

app/filtering.js

+21-40
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 fs = require('fs')
3129
const getOrigin = require('../js/lib/urlutil').getOrigin
@@ -553,6 +551,10 @@ function updateDownloadState (win, downloadId, item, state) {
553551
}
554552

555553
function registerForDownloadListener (session) {
554+
session.on('default-download-directory-changed', (e, newPath) => {
555+
appActions.changeSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH, newPath)
556+
})
557+
556558
session.on('will-download', function (event, item, webContents) {
557559
if (webContents.isDestroyed()) {
558560
event.preventDefault()
@@ -568,51 +570,30 @@ function registerForDownloadListener (session) {
568570
webContents.forceClose()
569571
}
570572

571-
// special handling for data URLs where another 'will-download' event handler is trying to suggest a filename via item.setSavePath
572-
// see the IPC handler for RENDER_URL_TO_PDF in app/index.js for example
573-
let itemFilename
574-
if (item.getURL().match(/^data:/) && item.getSavePath()) {
575-
itemFilename = path.basename(item.getSavePath())
576-
} else {
577-
itemFilename = item.getFilename()
573+
if (getSetting(settings.DOWNLOAD_ALWAYS_ASK)) {
574+
item.setPrompt(true)
578575
}
579576

580-
const defaultDir = (getSetting(settings.DOWNLOAD_DEFAULT_PATH) || getSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH) || app.getPath('downloads'))
581-
let savePath
582-
if (process.env.SPECTRON || (!getSetting(settings.DOWNLOAD_ALWAYS_ASK) && !item.promptForSaveLocation())) {
583-
let willOverwrite = true
584-
let matchedFilenames = 0
585-
while (willOverwrite) {
586-
savePath = path.join(defaultDir, (matchedFilenames ? `${itemFilename.replace(new RegExp(`${path.extname(itemFilename)}$`), '')} (${matchedFilenames})${path.extname(itemFilename)}` : itemFilename))
587-
if (!fs.existsSync(savePath)) {
588-
willOverwrite = false
589-
} else {
590-
matchedFilenames++
591-
}
577+
const downloadId = item.getGuid()
578+
item.on('updated', function (e, st) {
579+
if (!item.getSavePath()) {
580+
return
592581
}
593-
} else {
594-
savePath = dialog.showSaveDialog(win, { defaultPath: path.join(defaultDir, itemFilename) })
595-
}
596-
597-
// User cancelled out of save dialog prompt
598-
if (!savePath) {
599-
event.preventDefault()
600-
return
601-
}
602-
603-
item.setSavePath(savePath)
604-
appActions.changeSetting(settings.DEFAULT_DOWNLOAD_SAVE_PATH, path.dirname(savePath))
605-
606-
const downloadId = uuid.v4()
607-
updateDownloadState(win, downloadId, item, downloadStates.PENDING)
608-
if (win) {
609-
win.webContents.send(messages.SHOW_DOWNLOADS_TOOLBAR)
610-
}
611-
item.on('updated', function () {
612582
const state = item.isPaused() ? downloadStates.PAUSED : downloadStates.IN_PROGRESS
613583
updateDownloadState(win, downloadId, item, state)
584+
if (win) {
585+
win.webContents.send(messages.SHOW_DOWNLOADS_TOOLBAR)
586+
}
587+
item.on('removed', function () {
588+
updateElectronDownloadItem(downloadId, item, downloadStates.CANCELLED)
589+
appActions.mergeDownloadDetail(downloadId)
590+
})
614591
})
592+
615593
item.on('done', function (e, state) {
594+
if (!item.getSavePath()) {
595+
return
596+
}
616597
updateDownloadState(win, downloadId, item, state)
617598
})
618599
})

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
@@ -341,12 +341,11 @@ const aboutActions = {
341341
},
342342

343343
/**
344-
* Dispatches a message to render a URL into a PDF file
344+
* Dispatches a message to render the tab into a PDF file
345345
*/
346-
renderUrlToPdf: function (url, savePath) {
346+
renderToPdf: function (savePath) {
347347
aboutActions.dispatchAction({
348-
actionType: appConstants.APP_RENDER_URL_TO_PDF,
349-
url: url,
348+
actionType: appConstants.APP_RENDER_TO_PDF,
350349
savePath: savePath
351350
})
352351
}

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
@@ -73,7 +73,7 @@ const appConstants = {
7373
APP_DEFAULT_BROWSER_UPDATED: _,
7474
APP_DEFAULT_BROWSER_CHECK_COMPLETE: _,
7575
APP_POPULATE_HISTORY: _,
76-
APP_RENDER_URL_TO_PDF: _,
76+
APP_RENDER_TO_PDF: _,
7777
APP_DATA_URL_COPIED: _,
7878
APP_DOWNLOAD_REVEALED: _,
7979
APP_DOWNLOAD_OPENED: _,

js/stores/appStore.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,9 @@ const handleAppAction = (action) => {
517517
calculateTopSites(false)
518518
}
519519
break
520-
case appConstants.APP_RENDER_URL_TO_PDF:
520+
case appConstants.APP_RENDER_TO_PDF:
521521
const pdf = require('../../app/pdf')
522-
appState = pdf.renderUrlToPdf(appState, action)
522+
appState = pdf.renderToPdf(appState, action)
523523
break
524524
case appConstants.APP_SET_OBJECT_ID:
525525
let obj = appState.getIn(action.objectPath)

0 commit comments

Comments
 (0)