Skip to content

Commit

Permalink
Use IPC to invoke task scheduling from renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
sarathms committed Mar 11, 2021
1 parent 50babd4 commit 5325287
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Electron Main",
"program": "${workspaceFolder}/main/index.js",
"request": "launch",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand",
"--detectOpenHandles"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}

20 changes: 9 additions & 11 deletions main/ipcBindings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, ipcMain, dialog } = require('electron')
const { app } = require('electron')
const fs = require('fs-extra')
const { listResults } = require('./actions')
const { Runner } = require('./utils/ooni/run')
Expand Down Expand Up @@ -98,17 +98,15 @@ const ipcBindingsForMain = (ipcMain) => {
await onboard({ optout })
})

ipcMain.on('autorun.schedule', async (event) => {
const { response } = await dialog.showMessageBox(event.sender, {
type: "question",
title: "Do you want OONI Probe to run automatically every day?",
message: "OONI Probe can run tests everyday and ...",
buttons: ['No', 'Yes']
ipcMain.on('autorun.schedule', async (event, { dialogTitle, dialogMessage, btnYes, btnNo }) => {
const scheduleAutorun = require('./utils/autorun/schedule')
scheduleAutorun({
targetWindow: event.sender,
dialogTitle,
dialogMessage,
btnYes,
btnNo
})
if (response === 1) {
const task = require('./utils/scheduler/task')
task.create()
}
})
}

Expand Down
22 changes: 22 additions & 0 deletions main/utils/autorun/schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { dialog } = require('electron')

const scheduleAutorun = async ({
targetWindow,
dialogTitle = 'Do you want OONI Probe to run automatically every day?',
dialogMessage = 'OONI Probe can run tests everyday and ...',
btnYes = 'Yes',
btnNo = 'No'
}) => {
const { response } = await dialog.showMessageBox(targetWindow, {
type: 'question',
title: dialogTitle,
message: dialogMessage,
buttons: [btnNo, btnYes]
})
if (response === 1) {
const autorunTask = require('./task')
autorunTask.create()
}
}

module.exports = scheduleAutorun
File renamed without changes.
25 changes: 25 additions & 0 deletions renderer/components/settings/SetupAutorun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { useCallback } from 'react'
import { ipcRenderer } from 'electron'
import { useIntl } from 'react-intl'
import { Box, Button } from 'ooni-components'

const SetupAutorun = () => {
const intl = useIntl()
const onClick = useCallback(() => {
ipcRenderer.send('autorun.schedule', {
dialogTitle: intl.formatMessage({ id: 'Settings.Autorun.Title' }),
dialogMessage: intl.formatMessage({ id: 'Settings.Autorun.Message' }),
btnYes: intl.formatMessage({ id: 'Settings.Autorun.Button.Yes' }),
btnNo: intl.formatMessage({ id: 'Settings.Autorun.Button.No' }),
})
})
return (
<Box>
<Button onClick={onClick}>
{intl.formatMessage({ id: 'Settings.Autorun.Button.Label' })}
</Button>
</Box>
)
}

export default SetupAutorun
6 changes: 5 additions & 1 deletion renderer/pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { WebsiteCategoriesSelector } from '../components/settings/WebsiteCategor
import Layout from '../components/Layout'
import Sidebar from '../components/Sidebar'
import { default as pkgJson } from '../../package.json'
import SetupAutorun from '../components/settings/SetupAutorun'

const TopBar = styled.div`
background-color: ${props => props.theme.colors.blue5};
Expand Down Expand Up @@ -99,7 +100,10 @@ const Settings = () => {
optionKey='sharing.include_asn'
/>
</Section>

{/* Privacy */}
<Section title={<FormattedMessage id='Settings.Autorun.Label' />}>
<SetupAutorun />
</Section>
<Text my={3}>OONI Probe Desktop v{pkgJson.version}</Text>

</Container>
Expand Down

0 comments on commit 5325287

Please sign in to comment.