Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 89 additions & 113 deletions packages/selenium-ide/src/api/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,37 @@ const router = new Router()
const errors = {
cannotAccessInControlMode: {
errorCode: 'CannotAccessInControlMode',
error: 'Selenium IDE is controlled by a different extension.'
error: 'Selenium IDE is controlled by a different extension.',
},
missingPlugin: {
errorCode: 'MissingPlugin',
error: 'Plugin is not registered'
error: 'Plugin is not registered',
},
missingConnectionId: {
errorCode: 'MissingConnectionId',
error: 'No connection Id specified'
error: 'No connection Id specified',
},
missingProject: {
errorCode: 'MissingProject',
error: 'Plugin is not registered'
}
error: 'Plugin is not registered',
},
}

function checkControl(req) {
return Manager.controller && Manager.controller.connectionId != req.connectionId
return Manager.controller &&
Manager.controller.connectionId != req.connectionId
? Promise.reject()
: Promise.resolve()
}

function controlledOnly(req, res) {
return checkControl(req)
.catch(() => {
res(errors.cannotAccessInControlMode)
return errors.cannotAccessInControlMode
})
return checkControl(req).catch(() => {
res(errors.cannotAccessInControlMode)
return errors.cannotAccessInControlMode
})
}

function tryOverrideControl(req)
{
function tryOverrideControl(req) {
if (!req.connectionId) return
if (!ModalState.welcomeState.completed) {
ModalState.hideWelcome()
Expand All @@ -76,8 +75,7 @@ function tryOverrideControl(req)
description: `${req.name} is trying to control Selenium IDE`,
confirmLabel: 'Restart and Allow access',
cancelLabel: 'Deny access',
})
.then(r => {
}).then(r => {
if (r) {
const plugin = {
id: req.sender,
Expand All @@ -98,87 +96,74 @@ router.get('/health', (req, res) => {
res(Manager.hasPlugin(req.sender))
})

router.post(
'/register',
(req, res) => {
controlledOnly(req, res).then(() => {
const plugin = {
id: req.sender,
name: req.name,
version: req.version,
commands: req.commands,
dependencies: req.dependencies,
jest: req.jest,
exports: req.exports,
}
Manager.registerPlugin(plugin)
res(true)
});
}
)
router.post('/register', (req, res) => {
controlledOnly(req, res).then(() => {
const plugin = {
id: req.sender,
name: req.name,
version: req.version,
commands: req.commands,
dependencies: req.dependencies,
jest: req.jest,
exports: req.exports,
}
Manager.registerPlugin(plugin)
res(true)
})
})

router.post(
'/log',
(req, res) => {
controlledOnly(req, res).then(() => {
if (req.type === LogTypes.Error) {
logger.error(`${Manager.getPlugin(req.sender).name}: ${req.message}`)
} else if (req.type === LogTypes.Warning) {
logger.warn(`${Manager.getPlugin(req.sender).name}: ${req.message}`)
} else {
logger.log(`${Manager.getPlugin(req.sender).name}: ${req.message}`)
}
res(true)
})
}
)
router.post('/log', (req, res) => {
controlledOnly(req, res).then(() => {
if (req.type === LogTypes.Error) {
logger.error(`${Manager.getPlugin(req.sender).name}: ${req.message}`)
} else if (req.type === LogTypes.Warning) {
logger.warn(`${Manager.getPlugin(req.sender).name}: ${req.message}`)
} else {
logger.log(`${Manager.getPlugin(req.sender).name}: ${req.message}`)
}
res(true)
})
})

router.get(
'/project',
(_req, res) => {
controlledOnly(_req, res).then(() => {
res({ id: UiState.project.id, name: UiState.project.name })
})
}
)
router.get('/project', (_req, res) => {
controlledOnly(_req, res).then(() => {
res({ id: UiState.project.id, name: UiState.project.name })
})
})

router.post('/control', (req, res) => {
checkControl(req).then(() =>{
if(UiState.isControlled)
{
// Already in control mode with the same connection id.
res(true)
}
else
{
// Already in non-control mode.
checkControl(req)
.then(() => {
if (UiState.isControlled) {
// Already in control mode with the same connection id.
res(true)
} else {
// Already in non-control mode.
tryOverrideControl(req)
.then(() => res(true))
.catch(() => res(false))
}
})
.catch(() => {
// Already in control mode but another connection come.
tryOverrideControl(req)
.then(() => res(true))
.catch(() => res(false))
}
})
.catch(() => {
// Already in control mode but another connection come.
tryOverrideControl(req)
.then(() => res(true))
.catch(() => res(false))
})
})
})

router.post('/close', (req, res) => {
controlledOnly(req, res).then(() =>{
controlledOnly(req, res).then(() => {
// Not allow close if is not control mode.
if(!UiState.isControlled) {
if (!UiState.isControlled) {
return res(false)
}
const plugin = Manager.getPlugin(req.sender)
if (!plugin) return res(errors.missingPlugin)
if (!UiState.isSaved()) {
ModalState.showAlert({
title: 'Close project without saving',
description: `${
plugin.name
} is trying to close a project, are you sure you want to load this project and lose all unsaved changes?`,
description: `${plugin.name} is trying to close a project, are you sure you want to load this project and lose all unsaved changes?`,
confirmLabel: 'proceed',
cancelLabel: 'cancel',
}).then(result => {
Expand All @@ -189,9 +174,7 @@ router.post('/close', (req, res) => {
})

res(false)
}
else
{
} else {
window.close()
res(true)
}
Expand All @@ -214,42 +197,35 @@ router.post('/_connect', (req, res) => {
}
})

router.post(
'/project',
(req, res) => {
controlledOnly(req, res).then(() => {
const plugin = Manager.getPlugin(req.sender)
if (!plugin) return res(errors.missingPlugin)
if (!req.project) return res(errors.missingProject)
if (req.project) {
if (!UiState.isSaved()) {
WindowSession.focusIDEWindow()
ModalState.showAlert({
title: 'Open project without saving',
description: `${
plugin.name
} is trying to load a project, are you sure you want to load this project and lose all unsaved changes?`,
confirmLabel: 'proceed',
cancelLabel: 'cancel',
})
.then(result => {
if (result) {
loadJSProject(UiState.project, req.project)
ModalState.completeWelcome()
res(true)
}
})
} else {
WindowSession.focusIDEWindow()
router.post('/project', (req, res) => {
controlledOnly(req, res).then(() => {
const plugin = Manager.getPlugin(req.sender)
if (!plugin) return res(errors.missingPlugin)
if (!req.project) return res(errors.missingProject)

if (!UiState.isSaved()) {
WindowSession.focusIDEWindow()
ModalState.showAlert({
title: 'Open project without saving',
description: `${plugin.name} is trying to load a project, are you sure you want to load this project and lose all unsaved changes?`,
confirmLabel: 'proceed',
cancelLabel: 'cancel',
}).then(result => {
if (result) {
loadJSProject(UiState.project, req.project)
ModalState.completeWelcome()
res(true)
}
}
res(false)
})
}
)
})
} else {
WindowSession.focusIDEWindow()
loadJSProject(UiState.project, req.project)
ModalState.completeWelcome()
res(true)
}
res(false)
})
})

router.use('/playback', playbackRouter)
router.use('/record', recordRouter)
Expand Down
9 changes: 4 additions & 5 deletions packages/selenium-ide/src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ function handleInternalMessage(message) {
})
.then(() => {
openPanel({ windowId: 0 }).then(() => {
var payload = { ...message };
delete payload.restart;
var payload = { ...message }
delete payload.restart

const newMessage = {
uri: '/_connect',
Expand Down Expand Up @@ -234,9 +234,8 @@ browser.runtime.onMessageExternal.addListener(
let payload = message.payload

payload.sender = sender.id
if (message.uri === '/_connect' || message.uri === '/_close')
{
return sendResponse(false);
if (message.uri === '/_connect' || message.uri === '/_close') {
return sendResponse(false)
}
browser.runtime
.sendMessage(message)
Expand Down