<commit_before>/* 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 Immutable = require('immutable')
const electron = require('electron')
const app = electron.app
const messages = require('../js/constants/messages')
const BrowserWindow = electron.BrowserWindow
const appActions = require('../js/actions/appActions')
const isDarwin = process.platform === 'darwin'
let appInitialized = false

const focusOrOpenWindow = function (url) {
  // don't try to do anything if the app hasn't been initialized
  if (!appInitialized) {
    return false
  }

  let win = BrowserWindow.getFocusedWindow()
  if (!win) {
    win = BrowserWindow.getAllWindows()[0]
    if (win) {
      if (win.isMinimized()) {
        win.restore()
      }
      win.focus()
    }
  }

  if (!win) {
    appActions.newWindow(Immutable.fromJS({
      location: url
    }))
  } else if (url) {
    win.webContents.send(messages.SHORTCUT_NEW_FRAME, url)
  }

  return true
}

// Checks an array of arguments if it can find a url
const getUrlFromCommandLine = (argv) => {
  if (argv) {
    if (argv.length === 2 && !argv[1].startsWith('-')) {
      return argv[1]
    }
    const index = argv.indexOf('--')
    if (index !== -1 && index + 1 < argv.length && !argv[index + 1].startsWith('-')) {
      return argv[index + 1]
    }
  }
  return undefined
}

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

if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test') {
  const appAlreadyStartedShouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
    // Someone tried to run a second instance, we should focus our window.
    if (isDarwin) {
      focusOrOpenWindow()
    } else {
      focusOrOpenWindow(getUrlFromCommandLine(argv))
    }
  })
  if (appAlreadyStartedShouldQuit) {
    app.exit(0)
  }
}

app.on('will-finish-launching', () => {
  app.on('activate', () => {
    // (OS X) open a new window when the user clicks on the app icon if there aren't any open
    focusOrOpenWindow()
  })

  // 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) => {
    event.preventDefault()

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

process.on(messages.APP_INITIALIZED, () => { appInitialized = true })<commit_msg>Open html files when Brave is default on OS X<commit_after>/* 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 Immutable = require('immutable')
const electron = require('electron')
const app = electron.app
const messages = require('../js/constants/messages')
const BrowserWindow = electron.BrowserWindow
const appActions = require('../js/actions/appActions')
const isDarwin = process.platform === 'darwin'
let appInitialized = false

const focusOrOpenWindow = function (url) {
  // don't try to do anything if the app hasn't been initialized
  if (!appInitialized) {
    return false
  }

  let win = BrowserWindow.getFocusedWindow()
  if (!win) {
    win = BrowserWindow.getAllWindows()[0]
    if (win) {
      if (win.isMinimized()) {
        win.restore()
      }
      win.focus()
    }
  }

  if (!win) {
    appActions.newWindow(Immutable.fromJS({
      location: url
    }))
  } else if (url) {
    win.webContents.send(messages.SHORTCUT_NEW_FRAME, url)
  }

  return true
}

// Checks an array of arguments if it can find a url
const getUrlFromCommandLine = (argv) => {
  if (argv) {
    if (argv.length === 2 && !argv[1].startsWith('-')) {
      return argv[1]
    }
    const index = argv.indexOf('--')
    if (index !== -1 && index + 1 < argv.length && !argv[index + 1].startsWith('-')) {
      return argv[index + 1]
    }
  }
  return undefined
}

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

if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test') {
  const appAlreadyStartedShouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
    // Someone tried to run a second instance, we should focus our window.
    if (isDarwin) {
      focusOrOpenWindow()
    } else {
      focusOrOpenWindow(getUrlFromCommandLine(argv))
    }
  })
  if (appAlreadyStartedShouldQuit) {
    app.exit(0)
  }
}

app.on('will-finish-launching', () => {
  app.on('activate', () => {
    // (OS X) open a new window when the user clicks on the app icon if there aren't any open
    focusOrOpenWindow()
  })

  // 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) => {
    event.preventDefault()

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

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

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

process.on(messages.APP_INITIALIZED, () => { appInitialized = true })
{'fraction_non_alphanumeric': 0.1358091137423753, 'fraction_numerical': 0.0046645138141370645, 'mean_word_length': 3.0842490842490844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6835738', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1783, 'n_words': 516}