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

Commit c1b8e87

Browse files
authored
Merge pull request #8645 from brave/fix/local-pdf-2
allow PDFJS to load local files
2 parents d3ff9d9 + ba8e3f5 commit c1b8e87

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

app/extensions.js

+3
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ module.exports.init = () => {
408408
}
409409

410410
let loadExtension = (extensionId, extensionPath, manifest = {}, manifestLocation = 'unpacked') => {
411+
if (extensionId === config.PDFJSExtensionId) {
412+
manifestLocation = 'component'
413+
}
411414
if (!extensionInfo.isLoaded(extensionId) && !extensionInfo.isLoading(extensionId)) {
412415
extensionInfo.setState(extensionId, extensionStates.LOADING)
413416
if (extensionId === config.braveExtensionId || extensionId === config.torrentExtensionId || extensionId === config.syncExtensionId) {

app/filtering.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ const initPartition = (partition) => {
592592
}
593593
module.exports.initPartition = initPartition
594594

595-
const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:']
595+
const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:', 'file:']
596596

597597
function shouldIgnoreUrl (details) {
598598
// internal requests
@@ -676,6 +676,9 @@ module.exports.isResourceEnabled = (resourceName, url, isPrivate) => {
676676
return true
677677
}
678678

679+
if (resourceName === 'pdfjs') {
680+
return getSetting(settings.PDFJS_ENABLED)
681+
}
679682
if (resourceName === 'webtorrent') {
680683
return getSetting(settings.TORRENT_VIEWER_ENABLED)
681684
}

app/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const TrackingProtection = require('./trackingProtection')
6565
const AdBlock = require('./adBlock')
6666
const AdInsertion = require('./browser/ads/adInsertion')
6767
const HttpsEverywhere = require('./httpsEverywhere')
68+
const PDFJS = require('./pdfJS')
6869
const SiteHacks = require('./siteHacks')
6970
const CmdLine = require('./cmdLine')
7071
const UpdateStatus = require('../js/constants/updateStatus')
@@ -338,6 +339,7 @@ app.on('ready', () => {
338339
TrackingProtection.init()
339340
AdBlock.init()
340341
AdInsertion.init()
342+
PDFJS.init()
341343

342344
if (!loadedPerWindowState || loadedPerWindowState.length === 0) {
343345
if (!CmdLine.newWindowURL()) {

app/pdfJS.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
'use strict'
5+
6+
const UrlUtil = require('../js/lib/urlutil')
7+
const Filtering = require('./filtering')
8+
const config = require('../js/constants/config')
9+
const appActions = require('../js/actions/appActions')
10+
const getSetting = require('../js/settings').getSetting
11+
const settings = require('../js/constants/settings')
12+
13+
const pdfjsBaseUrl = `chrome-extension://${config.PDFJSExtensionId}/`
14+
const viewerBaseUrl = `${pdfjsBaseUrl}content/web/viewer.html`
15+
16+
const onBeforeRequest = (details) => {
17+
const result = { resourceName: 'pdfjs' }
18+
if (!(details.resourceType === 'mainFrame' &&
19+
UrlUtil.isFileScheme(details.url) &&
20+
UrlUtil.isFileType(details.url, 'pdf'))) {
21+
return result
22+
}
23+
appActions.loadURLRequested(details.tabId, `${viewerBaseUrl}?file=${encodeURIComponent(details.url)}`)
24+
result.cancel = true
25+
return result
26+
}
27+
28+
/**
29+
* Load PDF.JS
30+
*/
31+
module.exports.init = () => {
32+
if (getSetting(settings.PDFJS_ENABLED)) {
33+
Filtering.registerBeforeRequestFilteringCB(onBeforeRequest)
34+
}
35+
}

test/unit/lib/urlutilTest.js

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ describe('urlutil', function () {
254254
assert.equal(UrlUtil.getLocationIfPDF('chrome-extension://blank'), 'chrome-extension://blank')
255255
assert.equal(UrlUtil.getLocationIfPDF(null), null)
256256
})
257+
it('gets location for file: PDF URL', function () {
258+
let url = 'chrome-extension://jdbefljfgobbmcidnmpjamcbhnbphjnb/file:///Users/yan/Downloads/test.pdf'
259+
assert.equal(UrlUtil.getLocationIfPDF(url), 'file:///Users/yan/Downloads/test.pdf')
260+
})
257261
})
258262

259263
describe('getDisplayLocation', function () {

0 commit comments

Comments
 (0)