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

Commit

Permalink
fix PDFJS loading PDFs in new tabs
Browse files Browse the repository at this point in the history
fix #8364

also adds LOAD_URL_REQUESTED message handler, which is needed for brave/pdf.js#2

test plan:
1. go to https://letsencrypt.org/repository/
2. right click on any of the links to open in a new tab. it should work.
3. right click on any of the links to open in a new private tab. it should work.
  • Loading branch information
diracdeltas authored and bsclifton committed Apr 18, 2017
1 parent 9bb7570 commit fcf9a55
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tabState = require('../common/state/tabState')
const {app, BrowserWindow, extensions, session} = require('electron')
const {makeImmutable} = require('../common/state/immutableUtil')
const {getTargetAboutUrl, isSourceAboutUrl, newFrameUrl} = require('../../js/lib/appUrlUtil')
const {isURL, getUrlFromInput} = require('../../js/lib/urlutil')
const {isURL, getUrlFromInput, toPDFJSLocation} = require('../../js/lib/urlutil')
const {isSessionPartition} = require('../../js/state/frameStateUtil')
const {getOrigin} = require('../../js/state/siteUtil')
const {getSetting} = require('../../js/settings')
Expand All @@ -29,6 +29,9 @@ const normalizeUrl = function (url) {
if (isURL(url)) {
url = getUrlFromInput(url)
}
if (getSetting(settings.PDFJS_ENABLED)) {
url = toPDFJSLocation(url)
}
return url
}

Expand Down
6 changes: 6 additions & 0 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const contextMenus = require('./browser/extensions/contextMenus')
const extensionActions = require('./common/actions/extensionActions')
const config = require('../js/constants/config')
const appConfig = require('../js/constants/appConfig')
const messages = require('../js/constants/messages')
const {fileUrl} = require('../js/lib/appUrlUtil')
const {getExtensionsPath, getBraveExtUrl, getBraveExtIndexHTML} = require('../js/lib/appUrlUtil')
const {getSetting} = require('../js/settings')
Expand All @@ -16,6 +17,7 @@ const fs = require('fs')
const path = require('path')
const l10n = require('../js/l10n')
const {bravifyText} = require('./renderer/lib/extensionsUtil')
const ipcMain = require('electron').ipcMain

// Takes Content Security Policy flags, for example { 'default-src': '*' }
// Returns a CSP string, for example 'default-src: *;'
Expand Down Expand Up @@ -340,6 +342,10 @@ module.exports.init = () => {
}
})

ipcMain.on(messages.LOAD_URL_REQUESTED, (e, tabId, url) => {
appActions.loadURLRequested(tabId, url)
})

process.on('reload-sync-extension', () => {
console.log('reloading sync')
disableExtension(config.syncExtensionId)
Expand Down
2 changes: 2 additions & 0 deletions js/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ const messages = {
SAVE_INIT_DATA: _,
RELOAD_SYNC_EXTENSION: _,
RESET_SYNC: _,
// PDFJS
LOAD_URL_REQUESTED: _,
// Torrent
TORRENT_MESSAGE: _
}
Expand Down
13 changes: 13 additions & 0 deletions js/lib/urlutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ const UrlUtil = {
return url
},

/**
* Converts a potential PDF URL to the PDFJS URL.
* XXX: This only looks at the URL file extension, not MIME types.
* @param {string} url
* @return {string}
*/
toPDFJSLocation: function (url) {
if (url && UrlUtil.isHttpOrHttps(url) && UrlUtil.isFileType(url, 'pdf')) {
return `chrome-extension://${pdfjsExtensionId}/${url}`
}
return url
},

/**
* Gets location to display in the urlbar
* @param {string} url
Expand Down
16 changes: 16 additions & 0 deletions test/unit/lib/urlutilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ describe('urlutil', function () {
})
})

describe('toPDFJSLocation', function () {
const baseUrl = 'chrome-extension://jdbefljfgobbmcidnmpjamcbhnbphjnb/'
it('pdf', function () {
assert.equal(UrlUtil.toPDFJSLocation('http://abc.com/test.pdf'), baseUrl + 'http://abc.com/test.pdf')
})
it('non-pdf', function () {
assert.equal(UrlUtil.toPDFJSLocation('http://abc.com/test.pdf.txt'), 'http://abc.com/test.pdf.txt')
})
it('file url', function () {
assert.equal(UrlUtil.toPDFJSLocation('file://abc.com/test.pdf.txt'), 'file://abc.com/test.pdf.txt')
})
it('empty', function () {
assert.equal(UrlUtil.toPDFJSLocation(''), '')
})
})

describe('getHostname', function () {
it('returns undefined if the URL is invalid', function () {
assert.equal(UrlUtil.getHostname(null), undefined)
Expand Down

0 comments on commit fcf9a55

Please sign in to comment.