forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 192
/
preload.js
285 lines (239 loc) · 8.58 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// eslint:disable: no-require-imports no-var-requires
const { clipboard, ipcRenderer: ipc, webFrame } = require('electron/main');
const { Storage } = require('./ts/util/storage');
const { isTestNet, isTestIntegration } = require('./ts/shared/env_vars');
const os = require('os');
const url = require('url');
const _ = require('lodash');
const { setupI18n } = require('./ts/util/i18n/i18n');
const { dictionary, crowdinLocale } = ipc.sendSync('locale-data');
const config = url.parse(window.location.toString(), true).query;
const configAny = config;
window.i18n = setupI18n({ crowdinLocale, translationDictionary: dictionary });
let title = config.name;
if (config.environment !== 'production') {
title += ` - ${config.environment}`;
}
if (config.appInstance) {
title += ` - ${config.appInstance}`;
}
window.platform = process.platform;
window.getTitle = () => title;
window.getEnvironment = () => configAny.environment;
window.getAppInstance = () => configAny.appInstance;
window.getVersion = () => configAny.version;
window.getCommitHash = () => configAny.commitHash;
window.getNodeVersion = () => configAny.node_version;
window.getOSRelease = () =>
`${os.type()} ${os.release()}, Node.js ${config.node_version} ${os.platform()} ${os.arch()}`;
window.saveLog = additionalText => ipc.send('save-debug-log', additionalText);
window.sessionFeatureFlags = {
useOnionRequests: true,
useTestNet: isTestNet() || isTestIntegration(),
useClosedGroupV3: false,
replaceLocalizedStringsWithKeys: false,
debug: {
debugLogging: !_.isEmpty(process.env.SESSION_DEBUG),
debugLibsessionDumps: !_.isEmpty(process.env.SESSION_DEBUG_LIBSESSION_DUMPS),
debugFileServerRequests: false,
debugNonSnodeRequests: false,
debugOnionRequests: false,
},
};
window.versionInfo = {
environment: window.getEnvironment(),
version: window.getVersion(),
commitHash: window.getCommitHash(),
appInstance: window.getAppInstance(),
};
window.updateZoomFactor = () => {
const zoomFactor = window.getSettingValue('zoom-factor-setting') || 100;
window.setZoomFactor(zoomFactor / 100);
};
window.setZoomFactor = number => {
webFrame.setZoomFactor(number);
};
// Set the password for the database
window.setPassword = async (passPhrase, oldPhrase) =>
new Promise((resolve, reject) => {
ipc.once('set-password-response', (_event, response) => {
if (!response) {
// We don't reject here, but return undefined and handle the result in the caller.
// The reason is because we sometimes want to reject, but sometimes not depending on what the caller is doing (set/change/remove)
// For instance, removing a password makes `!response` true, and so we would reject here even if we
// technically didn't have an reason to.
return resolve(undefined);
}
return resolve(response);
});
ipc.send('set-password', passPhrase, oldPhrase);
});
// called to verify that the password is correct when showing the recovery from seed modal
window.onTryPassword = async passPhrase =>
new Promise((resolve, reject) => {
ipc.once('password-recovery-phrase-response', (_event, error) => {
if (error) {
return reject(error);
}
return resolve();
});
ipc.send('password-recovery-phrase', passPhrase);
});
window.setStartInTray = async startInTray =>
new Promise((resolve, reject) => {
ipc.once('start-in-tray-on-start-response', (_event, error) => {
if (error) {
reject(error);
return;
}
resolve();
return;
});
ipc.send('start-in-tray-on-start', startInTray);
});
window.getStartInTray = async () => {
return new Promise(resolve => {
ipc.once('get-start-in-tray-response', (_event, value) => {
resolve(value);
});
ipc.send('get-start-in-tray');
});
};
window.getOpengroupPruning = async () => {
return new Promise(resolve => {
ipc.once('get-opengroup-pruning-response', (_event, value) => {
resolve(value);
});
ipc.send('get-opengroup-pruning');
});
};
window.setOpengroupPruning = async opengroupPruning =>
new Promise((resolve, reject) => {
ipc.once('set-opengroup-pruning-response', (_event, error) => {
if (error) {
reject(error);
return;
}
resolve();
return;
});
ipc.send('set-opengroup-pruning', opengroupPruning);
});
window._ = require('lodash');
// We never do these in our code, so we'll prevent it everywhere
window.open = () => null;
// eslint-disable-next-line no-eval, no-multi-assign
window.eval = global.eval = () => null;
window.drawAttention = () => {
// window.log.debug('draw attention');
ipc.send('draw-attention');
};
window.showWindow = () => {
window.log.info('show window');
ipc.send('show-window');
};
window.setAutoHideMenuBar = autoHide => {
ipc.send('set-auto-hide-menu-bar', autoHide);
};
window.setMenuBarVisibility = visibility => {
ipc.send('set-menu-bar-visibility', visibility);
};
window.restart = () => {
window.log.info('restart');
ipc.send('restart');
};
window.closeAbout = () => {
ipc.send('close-about');
};
window.readyForUpdates = () => {
ipc.send('ready-for-updates');
};
ipc.on('get-theme-setting', () => {
const theme = window.Events.getThemeSetting();
ipc.send('get-success-theme-setting', theme);
});
window.getSettingValue = (settingID, comparisonValue = null) => {
// Comparison value allows you to pull boolean values from any type.
// Eg. window.getSettingValue('theme', 'classic-dark')
// returns 'false' when the value is 'classic-light'.
// We need to get specific settings from the main process
if (settingID === 'media-permissions') {
return window.getMediaPermissions();
} else if (settingID === 'call-media-permissions') {
return window.getCallMediaPermissions();
} else if (settingID === 'auto-update') {
return window.getAutoUpdateEnabled();
}
const settingVal = Storage.get(settingID);
return comparisonValue ? !!settingVal === comparisonValue : settingVal;
};
window.setSettingValue = async (settingID, value) => {
// For auto updating we need to pass the value to the main process
if (settingID === 'auto-update') {
window.setAutoUpdateEnabled(value);
return;
}
await Storage.put(settingID, value);
};
window.getMediaPermissions = () => ipc.sendSync('get-media-permissions');
window.setMediaPermissions = value => {
ipc.send('set-media-permissions', !!value);
};
window.getCallMediaPermissions = () => ipc.sendSync('get-call-media-permissions');
window.setCallMediaPermissions = value => {
ipc.send('set-call-media-permissions', !!value);
};
window.askForMediaAccess = () => {
ipc.send('media-access');
};
// Auto update setting
window.getAutoUpdateEnabled = () => ipc.sendSync('get-auto-update-setting');
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) {
ipc.send('now-ready-for-shutdown', error && error.stack ? error.stack : error);
}
});
// We pull these dependencies in now, from here, because they have Node.js dependencies
require('./ts/util/logging');
if (config.proxyUrl) {
window.log.info('Using provided proxy url');
}
window.nodeSetImmediate = setImmediate;
const data = require('./ts/data/dataInit');
window.Signal = data.initData();
const { getConversationController } = require('./ts/session/conversations/ConversationController');
window.getConversationController = getConversationController;
// Linux seems to periodically let the event loop stop, so this is a global workaround
setInterval(() => {
window.nodeSetImmediate(() => {});
}, 1000);
window.clipboard = clipboard;
window.getSeedNodeList = () =>
window.sessionFeatureFlags.useTestNet
? ['http://seed2.getsession.org:38157']
: [
// Note: for each of the seed nodes, the cert pinned is the one provided on the port 4443 and not the 4433, because the 4443 is a 10year one
'https://seed1.getsession.org:4443/',
'https://seed2.getsession.org:4443/',
'https://seed3.getsession.org:4443/',
];
window.addEventListener('contextmenu', e => {
const editable = e && e.target.closest('textarea, input, [contenteditable="true"]');
const link = e && e.target.closest('a');
const selection = Boolean(window && window.getSelection() && window.getSelection().toString());
if (!editable && !selection && !link) {
e.preventDefault();
}
});