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 0f0621b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
45 changes: 23 additions & 22 deletions app/cmdLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const BrowserWindow = electron.BrowserWindow
const appActions = require('../js/actions/appActions')
const urlParse = require('url').parse
const {navigatableTypes} = require('../js/lib/appUrlUtil')
const sessionStore = require('./sessionStore')
const isDarwin = process.platform === 'darwin'
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 +45,26 @@ 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 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]
}
}
}
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 @@ -105,23 +90,39 @@ app.on('will-finish-launching', () => {
// User clicked a link when w were the default or via command line like:
// open -a Brave http://www.brave.com
app.on('open-url', (event, path) => {
console.log('open-url', path)
event.preventDefault()
const parsedUrl = urlParse(path)
if (isProtocolHandled(parsedUrl.protocol)) {
if (sessionStore.isProtocolHandled(parsedUrl.protocol)) {
if (!focusOrOpenWindow(path)) {
module.exports.newWindowURL = path
newWindowURL = path
}
}
})

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

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

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

module.exports.newWindowURL = () => {
// For macOS, there are events like open-url instead
if (!isDarwin) {
const openUrl = 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 0f0621b

Please sign in to comment.