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

allow PDFJS to load local files #8645

Merged
merged 1 commit into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ module.exports.init = () => {
}

let loadExtension = (extensionId, extensionPath, manifest = {}, manifestLocation = 'unpacked') => {
if (extensionId === config.PDFJSExtensionId) {
manifestLocation = 'component'
Copy link
Member Author

@diracdeltas diracdeltas May 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: 'component' type extensions are able to access cross-origin 'file:' URLs, but somehow they are not able to see 'file:' URLs in the chrome.webRequest listener; instead the 'file:' URL webrequest listener must be added using session.webRequest.

}
if (!extensionInfo.isLoaded(extensionId) && !extensionInfo.isLoading(extensionId)) {
extensionInfo.setState(extensionId, extensionStates.LOADING)
if (extensionId === config.braveExtensionId || extensionId === config.torrentExtensionId || extensionId === config.syncExtensionId) {
Expand Down
5 changes: 4 additions & 1 deletion app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ const initPartition = (partition) => {
}
module.exports.initPartition = initPartition

const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:']
const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:', 'file:']

function shouldIgnoreUrl (details) {
// internal requests
Expand Down Expand Up @@ -676,6 +676,9 @@ module.exports.isResourceEnabled = (resourceName, url, isPrivate) => {
return true
}

if (resourceName === 'pdfjs') {
return getSetting(settings.PDFJS_ENABLED)
}
if (resourceName === 'webtorrent') {
return getSetting(settings.TORRENT_VIEWER_ENABLED)
}
Expand Down
2 changes: 2 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const TrackingProtection = require('./trackingProtection')
const AdBlock = require('./adBlock')
const AdInsertion = require('./browser/ads/adInsertion')
const HttpsEverywhere = require('./httpsEverywhere')
const PDFJS = require('./pdfJS')
const SiteHacks = require('./siteHacks')
const CmdLine = require('./cmdLine')
const UpdateStatus = require('../js/constants/updateStatus')
Expand Down Expand Up @@ -333,6 +334,7 @@ app.on('ready', () => {
TrackingProtection.init()
AdBlock.init()
AdInsertion.init()
PDFJS.init()

if (!loadedPerWindowState || loadedPerWindowState.length === 0) {
if (!CmdLine.newWindowURL()) {
Expand Down
35 changes: 35 additions & 0 deletions app/pdfJS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* 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 UrlUtil = require('../js/lib/urlutil')
const Filtering = require('./filtering')
const config = require('../js/constants/config')
const appActions = require('../js/actions/appActions')
const getSetting = require('../js/settings').getSetting
const settings = require('../js/constants/settings')

const pdfjsBaseUrl = `chrome-extension://${config.PDFJSExtensionId}/`
const viewerBaseUrl = `${pdfjsBaseUrl}content/web/viewer.html`

const onBeforeRequest = (details) => {
const result = { resourceName: 'pdfjs' }
if (!(details.resourceType === 'mainFrame' &&
UrlUtil.isFileScheme(details.url) &&
UrlUtil.isFileType(details.url, 'pdf'))) {
return result
}
appActions.loadURLRequested(details.tabId, `${viewerBaseUrl}?file=${encodeURIComponent(details.url)}`)
result.cancel = true
return result
}

/**
* Load PDF.JS
*/
module.exports.init = () => {
if (getSetting(settings.PDFJS_ENABLED)) {
Filtering.registerBeforeRequestFilteringCB(onBeforeRequest)
}
}
4 changes: 4 additions & 0 deletions test/unit/lib/urlutilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ describe('urlutil', function () {
assert.equal(UrlUtil.getLocationIfPDF('chrome-extension://blank'), 'chrome-extension://blank')
assert.equal(UrlUtil.getLocationIfPDF(null), null)
})
it('gets location for file: PDF URL', function () {
let url = 'chrome-extension://jdbefljfgobbmcidnmpjamcbhnbphjnb/file:///Users/yan/Downloads/test.pdf'
assert.equal(UrlUtil.getLocationIfPDF(url), 'file:///Users/yan/Downloads/test.pdf')
})
})

describe('getDisplayLocation', function () {
Expand Down