分离主进程与渲染进程的功能,禁用remote模块

pull/96/head
lyswhut 2019-11-03 09:53:19 +08:00
parent fddfaf23ce
commit a2af321c5e
25 changed files with 130 additions and 75 deletions

View File

@ -1,19 +0,0 @@
const { ipcMain, ipcRenderer } = require('electron')
export const mainSend = (name, params) => {
ipcMain.send(name, params)
}
export const mainOn = (name, callback) => {
ipcMain.on(name, callback)
}
export const rendererSend = (name, params) => {
ipcRenderer.send(name, params)
}
export const rendererOn = (name, callback) => {
ipcRenderer.on(name, callback)
}

32
src/common/ipc.js Normal file
View File

@ -0,0 +1,32 @@
const { ipcMain, ipcRenderer } = require('electron')
export const mainOn = (event, callback) => {
ipcMain.on(event, callback)
}
export const mainOnce = (event, callback) => {
ipcMain.once(event, callback)
}
export const mainHandle = (name, callback) => {
console.log(name, callback)
ipcMain.handle(name, callback)
}
export const mainHandleOnce = (name, callback) => {
ipcMain.handleOnce(name, callback)
}
export const rendererSend = (name, params) => {
ipcRenderer.send(name, params)
}
export const rendererSendSync = (name, params) => ipcRenderer.sendSync(name, params)
export const rendererInvoke = (name, params) => ipcRenderer.invoke(name, params)
export const rendererOn = (name, callback) => {
ipcRenderer.on(name, callback)
}
export const rendererOnce = (name, callback) => {
ipcRenderer.once(name, callback)
}

View File

@ -1,4 +1,4 @@
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
const { app } = require('electron')
const { name: defaultName } = require('../../../package.json')

View File

@ -0,0 +1,8 @@
const { mainHandle } = require('../../common/ipc')
mainHandle('clearCache', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return global.mainWindow.webContents.session.clearCache()
})

View File

@ -0,0 +1,7 @@
const { mainHandle } = require('../../common/ipc')
mainHandle('getCacheSize', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return global.mainWindow.webContents.session.getCacheSize()
})

View File

@ -4,3 +4,8 @@ require('./request')
require('./progressBar')
require('./trafficLight')
require('./musicMeta')
require('./selectDir')
require('./setWindowSize')
require('./showSaveDialog')
require('./clearCache')
require('./getCacheSize')

View File

@ -1,4 +1,4 @@
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
const { setMeta } = require('../utils/musicMeta')
mainOn('setMusicMeta', (event, { filePath, meta }) => {

View File

@ -1,4 +1,4 @@
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
mainOn('progress', (event, params) => {

View File

@ -1,6 +1,6 @@
const request = require('request')
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
const tasks = []

View File

@ -1,4 +1,4 @@
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
mainOn('restartWindow', (event, name) => {

View File

@ -0,0 +1,8 @@
const { mainHandle } = require('../../common/ipc')
const { dialog } = require('electron')
mainHandle('selectDir', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return dialog.showOpenDialog(global.mainWindow, options)
})

View File

@ -1,11 +0,0 @@
const { mainOn } = require('../../common/icp')
const { dialog } = require('electron')
module.exports = win => {
mainOn('selectPath', (event, params) => {
let path = dialog.showOpenDialog(win, params.options)
if (path === undefined) return
event.sender.send(params.eventName, path)
})
}

View File

@ -0,0 +1,7 @@
const { mainOn } = require('../../common/ipc')
mainOn('setWindowSize', (event, options) => {
if (!global.mainWindow) return
global.mainWindow.setBounds(options)
})

View File

@ -0,0 +1,8 @@
const { mainHandle } = require('../../common/ipc')
const { dialog } = require('electron')
mainHandle('showSaveDialog', async(event, options) => {
if (!global.mainWindow) throw new Error('mainwindow is undefined')
return dialog.showSaveDialog(global.mainWindow, options)
})

View File

@ -1,5 +1,5 @@
// const { app } = require('electron')
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
mainOn('min', event => {
@ -7,11 +7,11 @@ mainOn('min', event => {
global.mainWindow.minimize()
}
})
// mainOn('max', event => {
// if (global.mainWindow) {
// global.mainWindow.maximize()
// }
// })
mainOn('max', event => {
if (global.mainWindow) {
global.mainWindow.maximize()
}
})
mainOn('close', event => {
if (global.mainWindow) {
// global.mainWindowdow.destroy()

View File

@ -50,6 +50,7 @@ function createWindow() {
width: windowSizeInfo.width,
frame: false,
transparent: !isLinux,
enableRemoteModule: false,
// icon: path.join(global.__static, isWin ? 'icons/256x256.ico' : 'icons/512x512.png'),
resizable: false,
maximizable: false,

View File

@ -1,6 +1,6 @@
const { log } = require('../../common/utils')
const { autoUpdater } = require('electron-updater')
const { mainOn } = require('../../common/icp')
const { mainOn } = require('../../common/ipc')
autoUpdater.logger = log
// autoUpdater.autoDownload = false
@ -21,7 +21,7 @@ log.info('App starting...')
function sendStatusToWindow(text) {
log.info(text)
// global.mainWindow.webContents.send('message', text)
// ipcMain.send('message', text)
}
@ -90,16 +90,20 @@ module.exports = isFirstCheckedUpdate => {
sendStatusToWindow('Update not available.')
handleSendEvent({ type: 'update-not-available' })
})
autoUpdater.on('error', () => {
autoUpdater.on('error', err => {
sendStatusToWindow('Error in auto-updater.')
handleSendEvent({ type: 'update-error' })
handleSendEvent({ type: 'update-error', info: err })
})
autoUpdater.on('download-progress', progressObj => {
sendStatusToWindow('Download progress...')
let log_message = 'Download speed: ' + progressObj.bytesPerSecond
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%'
log_message = log_message + ' (' + progressObj.transferred + '/' + progressObj.total + ')'
sendStatusToWindow(log_message)
handleSendEvent({ type: 'download-progress', info: progressObj })
})
autoUpdater.on('update-downloaded', info => {
sendStatusToWindow('Update downloaded.')
handleSendEvent({ type: 'update-downloaded' })
handleSendEvent({ type: 'update-downloaded', info })
})
mainOn('quit-update', () => {
setTimeout(() => {

View File

@ -20,7 +20,7 @@
<script>
import dnscache from 'dnscache'
import { mapMutations, mapGetters, mapActions } from 'vuex'
import { rendererOn } from '../common/icp'
import { ipcRenderer } from 'electron'
import { isLinux } from '../common/utils'
import music from './utils/music'
window.ELECTRON_DISABLE_SECURITY_WARNINGS = process.env.ELECTRON_DISABLE_SECURITY_WARNINGS
@ -97,7 +97,7 @@ export default {
},
methods: {
...mapActions(['getVersionInfo']),
...mapMutations(['setNewVersion', 'setVersionModalVisible']),
...mapMutations(['setNewVersion', 'setVersionModalVisible', 'setDownloadProgress']),
...mapMutations('list', ['initList']),
...mapMutations('download', ['updateDownloadList']),
...mapMutations(['setSetting']),
@ -106,13 +106,15 @@ export default {
body.addEventListener('mouseenter', this.dieableIgnoreMouseEvents)
body.addEventListener('mouseleave', this.enableIgnoreMouseEvents)
}
rendererOn('update-available', (e, info) => {
ipcRenderer.on('update-available', (e, info) => {
// this.showUpdateModal(true)
console.log(info)
this.setNewVersion({
version: info.version,
})
})
rendererOn('update-error', () => {
ipcRenderer.on('update-error', err => {
console.log(err)
if (!this.updateTimeout) return
this.setVersionModalVisible({ isError: true })
this.clearUpdateTimeout()
@ -120,12 +122,17 @@ export default {
this.showUpdateModal()
})
})
rendererOn('update-downloaded', () => {
ipcRenderer.on('update-progress', progress => {
console.log(progress)
this.setDownloadProgress(progress)
})
ipcRenderer.on('update-downloaded', info => {
console.log(info)
this.clearUpdateTimeout()
this.setVersionModalVisible({ isError: false })
this.showUpdateModal()
})
rendererOn('update-not-available', () => {
ipcRenderer.on('update-not-available', () => {
if (!this.updateTimeout) return
if (this.setting.ignoreVersion) this.setSetting(Object.assign({}, this.setting, { ignoreVersion: null }))
this.clearUpdateTimeout()

View File

@ -48,7 +48,7 @@ div(:class="$style.player")
<script>
import Lyric from 'lrc-file-parser'
import { rendererSend } from '../../../common/icp'
import { rendererSend } from '../../../common/ipc'
import { formatPlayTime2, getRandom, checkPath, setTitle, clipboardWriteText, debounce } from '../../utils'
import { mapGetters, mapActions, mapMutations } from 'vuex'
import { requestMsg } from '../../utils/message'

View File

@ -12,7 +12,7 @@
</template>
<script>
import { rendererSend } from 'common/icp'
import { rendererSend } from 'common/ipc'
import { mapGetters } from 'vuex'
import music from '../../utils/music'
import { debounce } from '../../utils'

View File

@ -45,7 +45,7 @@ material-modal(:show="version.showModal" @close="handleClose")
<script>
import { mapGetters, mapMutations } from 'vuex'
import { rendererSend } from '../../../common/icp'
import { rendererSend } from '../../../common/ipc'
import { checkVersion, openUrl, clipboardWriteText } from '../../utils'
export default {

View File

@ -22,12 +22,11 @@ export default {
state.setting.list.scroll.locations[id] = location
},
setNewVersion(state, val) {
// val.history.forEach(ver => {
// ver.desc = ver.desc.replace(/\n/g, '<br>')
// })
// val.desc = val.desc.replace(/\n/g, '<br>')
state.version.newVersion = val
},
setDownloadProgress(state, info) {
state.version.downloadProgress = info
},
setVersionModalVisible(state, { isShow, isError }) {
if (isShow !== undefined) state.version.showModal = isShow
if (isError !== undefined) state.version.isError = isError

View File

@ -62,6 +62,7 @@ export default {
newVersion: null,
showModal: false,
isError: false,
downloadProgress: null,
},
userInfo: null,
setting,

View File

@ -1,9 +1,9 @@
import fs from 'fs'
import { shell, remote, clipboard } from 'electron'
import { shell, clipboard } from 'electron'
import path from 'path'
import os from 'os'
import crypto from 'crypto'
import { rendererSend } from '../../common/icp'
import { rendererSend, rendererInvoke } from '../../common/ipc'
/**
* 获取两个数之间的随机整数大于等于min小于max
@ -86,13 +86,13 @@ export const checkPath = path => fs.existsSync(path)
* 在资源管理器中打开目录
* @param {*} 选项
*/
export const openSelectDir = options => remote.dialog.showOpenDialog(remote.getCurrentWindow(), options)
export const selectDir = options => rendererInvoke('selectDir', options)
/**
* 在资源管理器中打开目录
* @param {*} 选项
*/
export const openSaveDir = options => remote.dialog.showSaveDialog(remote.getCurrentWindow(), options)
export const openSaveDir = options => rendererInvoke('showSaveDialog', options)
/**
* 在资源管理器中打开目录
@ -361,19 +361,17 @@ export const asyncSetArray = (from, to, num = 100) => new Promise(resolve => {
/**
* 获取缓存大小
* @param {*} win
*/
export const getCacheSize = () => remote.getCurrentWindow().webContents.session.getCacheSize()
export const getCacheSize = () => rendererInvoke('getCacheSize')
/**
* 清除缓存
* @param {*} win
*/
export const clearCache = () => remote.getCurrentWindow().webContents.session.clearCache()
export const clearCache = () => rendererInvoke('clearCache')
/**
* 设置窗口大小
* @param {*} width
* @param {*} height
*/
export const setWindowSize = (width, height) => remote.getCurrentWindow().setBounds({ width, height })
export const setWindowSize = (width, height) => rendererSend('setWindowSize', { width, height })

View File

@ -174,7 +174,7 @@ div.scroll(:class="$style.setting")
import { mapGetters, mapMutations } from 'vuex'
import {
openDirInExplorer,
openSelectDir,
selectDir,
openSaveDir,
updateSetting,
openUrl,
@ -184,7 +184,7 @@ import {
sizeFormate,
setWindowSize,
} from '../utils'
import { rendererSend } from '../../common/icp'
import { rendererSend } from '../../common/ipc'
import fs from 'fs'
export default {
@ -328,7 +328,7 @@ export default {
this.getCacheSize()
},
handleChangeSavePath() {
openSelectDir({
selectDir({
title: '选择歌曲保存路径',
defaultPath: this.current_setting.download.savePath,
properties: ['openDirectory'],
@ -421,7 +421,7 @@ export default {
})
},
handleImportAllData() {
openSelectDir({
selectDir({
title: '选择备份文件',
properties: ['openFile'],
filters: [
@ -443,7 +443,7 @@ export default {
})
},
handleImportSetting() {
openSelectDir({
selectDir({
title: '选择配置文件',
properties: ['openFile'],
filters: [
@ -465,7 +465,7 @@ export default {
})
},
handleImportPlayList() {
openSelectDir({
selectDir({
title: '选择列表文件',
properties: ['openFile'],
filters: [