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

Commit

Permalink
Fix opening URLs and files on Windows
Browse files Browse the repository at this point in the history
Fix #5352
Fix #4795
  • Loading branch information
bbondy committed Nov 4, 2016
1 parent 30a9674 commit 286ff4b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
59 changes: 33 additions & 26 deletions app/cmdLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ const messages = require('../js/constants/messages')
const BrowserWindow = electron.BrowserWindow
const appActions = require('../js/actions/appActions')
const urlParse = require('url').parse
const {navigatableTypes} = require('../js/lib/appUrlUtil')
const {fileUrl} = require('../js/lib/appUrlUtil')
const sessionStore = require('./sessionStore')
const isDarwin = process.platform === 'darwin'
const fs = require('fs')
const path = require('path')
let appInitialized = false
let newWindowURL

const focusOrOpenWindow = function (url) {
// don't try to do anything if the app hasn't been initialized
Expand Down Expand Up @@ -43,43 +47,34 @@ const focusOrOpenWindow = function (url) {
return true
}

const isProtocolHandled = (protocol) => {
protocol = (protocol || '').split(':')[0]
return navigatableTypes.includes(`${protocol}:`) ||
electron.session.defaultSession.protocol.isNavigatorProtocolHandled(protocol)
}

// Checks an array of arguments if it can find a url
const getUrlFromCommandLine = (argv) => {
if (argv) {
if (argv.length === 2 && !argv[1].startsWith('-')) {
const parsedUrl = urlParse(argv[1])
if (isProtocolHandled(parsedUrl.protocol)) {
if (sessionStore.isProtocolHandled(parsedUrl.protocol)) {
return argv[1]
}
const filePath = path.resolve(argv[1])
if (fs.existsSync(filePath)) {
return fileUrl(filePath)
}
}
const index = argv.indexOf('--')
if (index !== -1 && index + 1 < argv.length && !argv[index + 1].startsWith('-')) {
const parsedUrl = urlParse(argv[index + 1])
if (isProtocolHandled(parsedUrl.protocol)) {
if (sessionStore.isProtocolHandled(parsedUrl.protocol)) {
return argv[index + 1]
}
const filePath = path.resolve(argv[index + 1])
if (fs.existsSync(filePath)) {
return fileUrl(filePath)
}
}
}
return undefined
}

// For macOS, there are events like open-url instead
if (!isDarwin) {
const openUrl = getUrlFromCommandLine(process.argv)
if (openUrl) {
const parsedUrl = urlParse(openUrl)
if (isProtocolHandled(parsedUrl.protocol)) {
module.exports.newWindowURL = openUrl
}
}
}

app.on('ready', () => {
if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test') {
const appAlreadyStartedShouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
Expand All @@ -106,22 +101,34 @@ app.on('will-finish-launching', () => {
// open -a Brave http://www.brave.com
app.on('open-url', (event, path) => {
event.preventDefault()
const parsedUrl = urlParse(path)
if (isProtocolHandled(parsedUrl.protocol)) {
if (!focusOrOpenWindow(path)) {
module.exports.newWindowURL = path
if (!appInitialized) {
newWindowURL = path
} else {
const parsedUrl = urlParse(path)
if (sessionStore.isProtocolHandled(parsedUrl.protocol)) {
focusOrOpenWindow(path)
}
}
})

// User clicked on a file or dragged a file to the dock on macOS
app.on('open-file', (event, path) => {
event.preventDefault()

if (!focusOrOpenWindow(path)) {
module.exports.newWindowURL = path
newWindowURL = path
}
})
})

process.on(messages.APP_INITIALIZED, () => { appInitialized = true })

module.exports.newWindowURL = () => {
const openUrl = newWindowURL || getUrlFromCommandLine(process.argv)
if (openUrl) {
const parsedUrl = urlParse(openUrl)
if (sessionStore.isProtocolHandled(parsedUrl.protocol)) {
newWindowURL = openUrl
}
}
return newWindowURL
}
6 changes: 3 additions & 3 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ app.on('ready', () => {
AdInsertion.init()

if (!loadedPerWindowState || loadedPerWindowState.length === 0) {
if (!CmdLine.newWindowURL) {
if (!CmdLine.newWindowURL()) {
appActions.newWindow()
}
} else {
Expand All @@ -457,9 +457,9 @@ app.on('ready', () => {
appActions.changeSetting(settings.IS_DEFAULT_BROWSER, isDefaultBrowser)
}

if (CmdLine.newWindowURL) {
if (CmdLine.newWindowURL()) {
appActions.newWindow(Immutable.fromJS({
location: CmdLine.newWindowURL
location: CmdLine.newWindowURL()
}))
}

Expand Down
11 changes: 11 additions & 0 deletions app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const siteUtil = require('../js/state/siteUtil')
const sessionStorageVersion = 1
const filtering = require('./filtering')
const autofill = require('./autofill')
const {navigatableTypes} = require('../js/lib/appUrlUtil')
// const tabState = require('./common/state/tabState')

const getSetting = require('../js/settings').getSetting
Expand Down Expand Up @@ -479,3 +480,13 @@ module.exports.defaultAppState = () => {
defaultWindowParams: {}
}
}

/**
* Determines if a protocol is handled.
* app.on('ready') must have been fired before this is called.
*/
module.exports.isProtocolHandled = (protocol) => {
protocol = (protocol || '').split(':')[0]
return navigatableTypes.includes(`${protocol}:`) ||
electron.session.defaultSession.protocol.isNavigatorProtocolHandled(protocol)
}

0 comments on commit 286ff4b

Please sign in to comment.