session-desktop/preload.js

250 lines
7.3 KiB
JavaScript
Raw Normal View History

Integration tests (#975) * add first integration test Session Checking window title Checking window count Can restore from seed * FIXME torevert once found why this crash on app close * [test] add join valid open group test * [test] validate cannot join two times the same open group * [test] move common things to common.js * [test] move tests to separate files * [test] clean * [test] add send message to open group test * [test] lint * [test] rename hooks -> common * [test] add 15s delay before considering test as slow * upgrade electron 8.0.3 and spectron 10.0.0 * [test] signin from seed: validate pubkey * Replace spellchecker in favor of typo-js * [test] refactor common calls to common.js * [test] add two different pubkey, mnemonic and displayname * [test] FIXME unsafe eval needed for now * [test] add: add friends test * [test] working multi instance tests * [test] FIXME disable snodeproxy * [test] update yarn.lock * [test] make tests more robust with restart from scratch each test * [test] add link of two devices test and hard rm of apps before start (rm -r) * remove unused file * [test] lint * [test] add registration from generated pubkey test * [test] add beginning of network stub * [test] stub "token" endpoint * [test] add test of one message on pub group pull * [test] add starting port randomize. looks to help for some bad start with multi instance * [test] add stub for one to one chats (sessions) * [test] clean code * [test] finish add friend test and stub snode server * [test] stub calls during link device test * [test] add a flag to show some logs on stubbed snode * [test] finish link of two device test. check both pubkey matches * [test] add and use function to wrap erase+start+login+stub app * [test] add method to login as friend and closed group test&messages * Revert "[test] FIXME unsafe eval needed for now" This reverts commit de5322fdae6cdab8e3b9bd9a52b7d172c9bc2d26. * [test] apply review * [test] fix lint * [test] fix existing test with new spectron version * [test] fix lint * [test] refactor page objects * [test] add delete account test * [test] add unlink of two device test * [test] make tiny waitForExists -> isExisting * [test] add checks of link new device buttons * upgrade fs-extra@9.0.0 * address pr review * [test] fix spell_check test Co-authored-by: Josh Perez <60019601+josh-signal@users.noreply.github.com>
2020-03-25 01:42:53 +01:00
/* eslint-disable global-require */
2018-04-03 21:03:57 +02:00
/* global Whisper: false */
/* global window: false */
const path = require('path');
2021-10-04 01:56:54 +02:00
const { webFrame, remote, clipboard, ipcRenderer } = require('electron');
2018-04-03 21:03:57 +02:00
2021-10-04 01:56:54 +02:00
const { app } = remote;
const url = require('url');
2018-04-03 21:03:57 +02:00
const config = url.parse(window.location.toString(), true).query;
let title = config.name;
if (config.environment !== 'production') {
title += ` - ${config.environment}`;
}
if (config.appInstance) {
title += ` - ${config.appInstance}`;
}
2020-06-18 02:46:52 +02:00
global.dcodeIO = global.dcodeIO || {};
global.dcodeIO.ByteBuffer = require('bytebuffer');
window.platform = process.platform;
window.getTitle = () => title;
window.getEnvironment = () => config.environment;
window.getAppInstance = () => config.appInstance;
window.getVersion = () => config.version;
window.isDev = () => config.environment === 'development';
window.getCommitHash = () => config.commitHash;
window.getNodeVersion = () => config.node_version;
2022-02-18 03:03:47 +01:00
window.sessionFeatureFlags = {
2021-01-29 01:29:24 +01:00
useOnionRequests: true,
2022-03-22 01:18:01 +01:00
useCallMessage: true,
};
2020-01-22 05:57:58 +01:00
window.versionInfo = {
environment: window.getEnvironment(),
version: window.getVersion(),
commitHash: window.getCommitHash(),
appInstance: window.getAppInstance(),
};
2021-10-04 01:56:54 +02:00
const ipc = ipcRenderer;
const localeMessages = ipc.sendSync('locale-data');
2018-04-03 21:03:57 +02:00
2020-02-26 03:30:56 +01:00
window.updateZoomFactor = () => {
const zoomFactor = window.getSettingValue('zoom-factor-setting') || 100;
window.setZoomFactor(zoomFactor / 100);
};
2020-02-20 07:15:39 +01:00
2020-02-26 03:30:56 +01:00
window.setZoomFactor = number => {
webFrame.setZoomFactor(number);
};
// Set the password for the database
2019-01-16 05:44:13 +01:00
window.setPassword = (passPhrase, oldPhrase) =>
new Promise((resolve, reject) => {
ipc.once('set-password-response', (event, error) => {
if (error) {
return reject(error);
}
return resolve();
});
ipc.send('set-password', passPhrase, oldPhrase);
});
window.setStartInTray = startInTray =>
new Promise((resolve, reject) => {
ipc.once('start-in-tray-on-start-response', (_event, error) => {
if (error) {
return reject(error);
}
return resolve();
});
ipc.send('start-in-tray-on-start', startInTray);
});
window.getStartInTray = () =>
new Promise(resolve => {
ipc.once('get-start-in-tray-response', (event, value) => resolve(value));
ipc.send('get-start-in-tray');
});
window.libsession = require('./ts/session');
2022-01-18 05:21:36 +01:00
window._ = require('lodash');
window.getConversationController = window.libsession.Conversations.getConversationController;
2018-05-24 18:32:18 +02:00
// We never do these in our code, so we'll prevent it everywhere
window.open = () => null;
2018-05-24 18:32:18 +02:00
// eslint-disable-next-line no-eval, no-multi-assign
window.eval = global.eval = () => null;
2018-04-03 21:03:57 +02:00
window.drawAttention = () => {
// window.log.debug('draw attention');
2018-04-03 21:03:57 +02:00
ipc.send('draw-attention');
};
window.showWindow = () => {
window.log.info('show window');
2018-04-03 21:03:57 +02:00
ipc.send('show-window');
};
2021-04-22 10:03:58 +02:00
window.setAutoHideMenuBar = autoHide => ipc.send('set-auto-hide-menu-bar', autoHide);
2018-04-03 21:03:57 +02:00
2021-04-22 10:03:58 +02:00
window.setMenuBarVisibility = visibility => ipc.send('set-menu-bar-visibility', visibility);
2018-04-03 21:03:57 +02:00
window.restart = () => {
window.log.info('restart');
2018-04-03 21:03:57 +02:00
ipc.send('restart');
};
2018-04-27 23:25:04 +02:00
window.closeAbout = () => ipc.send('close-about');
window.readyForUpdates = () => ipc.send('ready-for-updates');
2018-04-03 21:03:57 +02:00
ipc.on('get-theme-setting', () => {
const theme = window.Events.getThemeSetting();
ipc.send('get-success-theme-setting', theme);
});
2020-02-27 23:49:03 +01:00
window.getSettingValue = (settingID, comparisonValue = null) => {
// Comparison value allows you to pull boolean values from any type.
// Eg. window.getSettingValue('theme', 'light')
// returns 'false' when the value is 'dark'.
// We need to get specific settings from the main process
2020-02-27 23:49:03 +01:00
if (settingID === 'media-permissions') {
2020-03-10 00:49:07 +01:00
return window.getMediaPermissions();
} else if (settingID === 'call-media-permissions') {
return window.getCallMediaPermissions();
} else if (settingID === 'auto-update') {
return window.getAutoUpdateEnabled();
2020-02-27 23:49:03 +01:00
}
const settingVal = window.storage.get(settingID);
return comparisonValue ? !!settingVal === comparisonValue : settingVal;
};
window.setSettingValue = (settingID, value) => {
// For auto updating we need to pass the value to the main process
if (settingID === 'auto-update') {
window.setAutoUpdateEnabled(value);
return;
}
2020-02-27 23:49:03 +01:00
window.storage.put(settingID, value);
2020-01-15 03:49:07 +01:00
};
2020-01-14 03:28:31 +01:00
2020-03-10 01:05:26 +01:00
window.getMediaPermissions = () => ipc.sendSync('get-media-permissions');
2021-04-22 10:03:58 +02:00
window.setMediaPermissions = value => ipc.send('set-media-permissions', !!value);
2018-04-03 21:03:57 +02:00
window.getCallMediaPermissions = () => ipc.sendSync('get-call-media-permissions');
window.setCallMediaPermissions = value => ipc.send('set-call-media-permissions', !!value);
2021-07-30 08:44:01 +02:00
window.askForMediaAccess = () => ipc.send('media-access');
2021-07-30 08:31:35 +02:00
// Auto update setting
2020-03-10 04:58:44 +01:00
window.getAutoUpdateEnabled = () => ipc.sendSync('get-auto-update-setting');
2021-04-22 10:03:58 +02:00
window.setAutoUpdateEnabled = value => ipc.send('set-auto-update-setting', !!value);
ipc.on('get-ready-for-shutdown', async () => {
const { shutdown } = window.Events || {};
if (!shutdown) {
window.log.error('preload shutdown handler: shutdown method not found');
ipc.send('now-ready-for-shutdown');
return;
}
try {
await shutdown();
ipc.send('now-ready-for-shutdown');
} catch (error) {
2021-04-22 10:03:58 +02:00
ipc.send('now-ready-for-shutdown', error && error.stack ? error.stack : error);
}
});
2018-04-03 21:03:57 +02:00
// We pull these dependencies in now, from here, because they have Node.js dependencies
2022-03-23 06:35:29 +01:00
require('./ts/util/logging');
2018-04-03 21:03:57 +02:00
if (config.proxyUrl) {
window.log.info('Using provided proxy url');
2018-04-03 21:03:57 +02:00
}
window.nodeSetImmediate = setImmediate;
2021-01-29 01:29:24 +01:00
const Signal = require('./js/modules/signal');
const i18n = require('./js/modules/i18n');
window.Signal = Signal.setup();
2022-03-23 00:24:05 +01:00
window.getSwarmPollingInstance = require('./ts/session/apis/snode_api').getSwarmPollingInstance;
2019-09-19 08:36:19 +02:00
const WorkerInterface = require('./js/modules/util_worker_interface');
// A Worker with a 3 minute timeout
2018-12-14 00:03:15 +01:00
const utilWorkerPath = path.join(app.getAppPath(), 'js', 'util_worker.js');
const utilWorker = new WorkerInterface(utilWorkerPath, 3 * 60 * 1000);
2021-01-29 01:29:24 +01:00
2018-12-14 00:03:15 +01:00
window.callWorker = (fnName, ...args) => utilWorker.callWorker(fnName, ...args);
2018-04-03 21:03:57 +02:00
// Linux seems to periodically let the event loop stop, so this is a global workaround
setInterval(() => {
2021-03-01 02:05:39 +01:00
window.nodeSetImmediate(() => {});
2018-04-03 21:03:57 +02:00
}, 1000);
window.loadImage = require('blueimp-load-image');
2018-04-24 17:19:39 +02:00
window.filesize = require('filesize');
2018-04-03 21:03:57 +02:00
window.React = require('react');
window.ReactDOM = require('react-dom');
2018-04-03 21:03:57 +02:00
2018-12-03 01:19:45 +01:00
window.clipboard = clipboard;
window.getSeedNodeList = () => [
{
url: 'https://storage.seed1.loki.network:4433/',
},
{
url: 'https://storage.seed3.loki.network:4433/',
},
{
url: 'https://public.loki.foundation:4433/',
},
];
2021-03-19 03:51:03 +01:00
const { locale: localFromEnv } = config;
window.i18n = i18n.setup(localFromEnv, localeMessages);
2021-02-08 06:18:36 +01:00
window.moment = require('moment');
2021-01-29 01:29:24 +01:00
window.libsession = require('./ts/session');
2021-02-15 05:16:38 +01:00
window.Signal.Data = require('./ts/data/data');
2018-04-03 21:03:57 +02:00
window.Signal.Logs = require('./js/modules/logs');
2020-03-27 06:43:19 +01:00
window.addEventListener('contextmenu', e => {
2021-04-22 10:03:58 +02:00
const editable = e.target.closest('textarea, input, [contenteditable="true"]');
2020-03-27 06:43:19 +01:00
const link = e.target.closest('a');
const selection = Boolean(window.getSelection().toString());
if (!editable && !selection && !link) {
e.preventDefault();
}
});
window.NewReceiver = require('./ts/receiver/receiver');
2020-07-07 01:13:55 +02:00
// Blocking
2021-04-22 10:03:58 +02:00
const { BlockedNumberController } = require('./ts/util/blockedNumberController');
2020-07-07 01:13:55 +02:00
window.BlockedNumberController = BlockedNumberController;