<commit_before>/** Base implementation for all windows.
 *
 * @module gui/js/window_manager
 */

const { BrowserWindow, ipcMain, shell } = require('electron')
const _ = require('lodash')
const path = require('path')

const ELMSTARTUP = 400

const log = require('../../core/app').logger({
  component: 'windows'
})

module.exports = class WindowManager {
  constructor(app, desktop) {
    this.win = null
    this.app = app
    this.desktop = desktop
    this.log = require('../../core/app').logger({
      component: 'GUI/' + this.windowOptions().title
    })

    let handlers = this.ipcEvents()
    Object.keys(handlers).forEach(name => {
      if (!handlers[name]) {
        throw new Error('undefined handler for event ' + name)
      }
      ipcMain.on(name, handlers[name].bind(this))
    })
    ipcMain.on('renderer-error', (event, err) => {
      // Sender can be a WebContents instance not yet attached to this.win, so
      // we compare the title from browserWindowOptions:
      if (
        _.get(event, 'sender.browserWindowOptions.title') ===
        this.windowOptions().title
      ) {
        this.log.error({ err, sentry: true }, err.message)
      }
    })
  }

  /* abtract */
  windowOptions() {
    throw new Error('extend WindowManager before using')
  }

  /* abtract */
  ipcEvents() {
    throw new Error('extend WindowManager before using')
  }

  makesAppVisible() {
    return true
  }

  show() {
    if (!this.win) return this.create()
    this.log.debug('show')
    this.win.show()
    return Promise.resolve(this.win)
  }

  hide() {
    if (this.win) {
      this.log.debug('hide')
      this.win.close()
    }
    this.win = null
  }

  shown() {
    return this.win != null
  }

  focus() {
    return this.win && this.win.focus()
  }

  reload() {
    if (this.win) {
      this.log.debug('reload')
      this.win.reload()
    }
  }

  send(...args) {
    this.win && this.win.webContents && this.win.webContents.send(...args)
  }

  hash() {
    return ''
  }

  centerOnScreen(wantedWidth, wantedHeight) {
    try {
      this.win.setSize(wantedWidth, wantedHeight, true)
      this.win.center()
    } catch (err) {
      log.warn({ err, wantedWidth, wantedHeight }, 'Failed to centerOnScreen')
    }
  }

  create() {
    this.log.debug('create')
    const opts = {
      indexPath: path.resolve(__dirname, '..', 'index.html'),
      ...this.windowOptions()
    }
    opts.webPreferences = {
      ...opts.webPreferences,
      nodeIntegration: true,
      enableRemoteModule: true
    }
    // https://github.com/AppImage/AppImageKit/wiki/Bundling-Electron-apps
    if (process.platform === 'linux') {
      opts.icon = path.join(__dirname, '../images/icon.png')
    }
    this.win = new BrowserWindow({
      autoHideMenuBar: true,
      show: false,
      ...opts
    })
    this.win.on('unresponsive', () => {
      this.log.warn('Web page becomes unresponsive')
    })
    this.win.on('responsive', () => {
      this.log.warn('Web page becomes responsive again')
    })
    this.win.webContents.on(
      'did-fail-load',
      (event, errorCode, errorDescription, url, isMainFrame) => {
        const err = new Error(errorDescription)
        err.code = errorCode
        this.log.error(
          { err, url, isMainFrame, sentry: true },
          'failed loading window content'
        )<commit_msg>gui: Disable context isolation in renderer<commit_after>/** Base implementation for all windows.
 *
 * @module gui/js/window_manager
 */

const { BrowserWindow, ipcMain, shell } = require('electron')
const _ = require('lodash')
const path = require('path')

const ELMSTARTUP = 400

const log = require('../../core/app').logger({
  component: 'windows'
})

module.exports = class WindowManager {
  constructor(app, desktop) {
    this.win = null
    this.app = app
    this.desktop = desktop
    this.log = require('../../core/app').logger({
      component: 'GUI/' + this.windowOptions().title
    })

    let handlers = this.ipcEvents()
    Object.keys(handlers).forEach(name => {
      if (!handlers[name]) {
        throw new Error('undefined handler for event ' + name)
      }
      ipcMain.on(name, handlers[name].bind(this))
    })
    ipcMain.on('renderer-error', (event, err) => {
      // Sender can be a WebContents instance not yet attached to this.win, so
      // we compare the title from browserWindowOptions:
      if (
        _.get(event, 'sender.browserWindowOptions.title') ===
        this.windowOptions().title
      ) {
        this.log.error({ err, sentry: true }, err.message)
      }
    })
  }

  /* abtract */
  windowOptions() {
    throw new Error('extend WindowManager before using')
  }

  /* abtract */
  ipcEvents() {
    throw new Error('extend WindowManager before using')
  }

  makesAppVisible() {
    return true
  }

  show() {
    if (!this.win) return this.create()
    this.log.debug('show')
    this.win.show()
    return Promise.resolve(this.win)
  }

  hide() {
    if (this.win) {
      this.log.debug('hide')
      this.win.close()
    }
    this.win = null
  }

  shown() {
    return this.win != null
  }

  focus() {
    return this.win && this.win.focus()
  }

  reload() {
    if (this.win) {
      this.log.debug('reload')
      this.win.reload()
    }
  }

  send(...args) {
    this.win && this.win.webContents && this.win.webContents.send(...args)
  }

  hash() {
    return ''
  }

  centerOnScreen(wantedWidth, wantedHeight) {
    try {
      this.win.setSize(wantedWidth, wantedHeight, true)
      this.win.center()
    } catch (err) {
      log.warn({ err, wantedWidth, wantedHeight }, 'Failed to centerOnScreen')
    }
  }

  create() {
    this.log.debug('create')
    const opts = {
      indexPath: path.resolve(__dirname, '..', 'index.html'),
      ...this.windowOptions()
    }
    opts.webPreferences = {
      ...opts.webPreferences,
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true
    }
    // https://github.com/AppImage/AppImageKit/wiki/Bundling-Electron-apps
    if (process.platform === 'linux') {
      opts.icon = path.join(__dirname, '../images/icon.png')
    }
    this.win = new BrowserWindow({
      autoHideMenuBar: true,
      show: false,
      ...opts
    })
    this.win.on('unresponsive', () => {
      this.log.warn('Web page becomes unresponsive')
    })
    this.win.on('responsive', () => {
      this.log.warn('Web page becomes responsive again')
    })
    this.win.webContents.on(
      'did-fail-load',
      (event, errorCode, errorDescription, url, isMainFrame) => {
        const err = new Error(errorDescription)
        err.code = errorCode
        this.log.error(
          { err, url, isMainFrame, sentry: true },
          'failed loading window content'
        )
{'fraction_non_alphanumeric': 0.15310650887573965, 'fraction_numerical': 0.0008875739644970414, 'mean_word_length': 2.3974874371859296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '481482', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2117, 'n_words': 447}