-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
settingsChannel.ts
231 lines (197 loc) · 7.3 KB
/
settingsChannel.ts
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
// Copyright 2017-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { ipcMain as ipc, BrowserWindow, session } from 'electron';
import { userConfig } from '../../app/user_config';
import { ephemeralConfig } from '../../app/ephemeral_config';
import { installPermissionsHandler } from '../../app/permissions';
import { strictAssert } from '../util/assert';
import {
IPCEventsValuesType,
IPCEventsCallbacksType,
} from '../util/createIPCEvents';
const EPHEMERAL_NAME_MAP = new Map([
['spellCheck', 'spell-check'],
['systemTraySetting', 'system-tray-setting'],
]);
export class SettingsChannel {
private mainWindow?: BrowserWindow;
public setMainWindow(mainWindow: BrowserWindow | undefined): void {
this.mainWindow = mainWindow;
}
public install(): void {
this.installSetting('deviceName', { setter: false });
// ChatColorPicker redux hookups
this.installCallback('getCustomColors');
this.installCallback('getConversationsWithCustomColor');
this.installCallback('resetAllChatColors');
this.installCallback('resetDefaultChatColor');
this.installCallback('addCustomColor');
this.installCallback('editCustomColor');
this.installCallback('removeCustomColor');
this.installCallback('removeCustomColorOnConversations');
this.installCallback('setGlobalDefaultConversationColor');
this.installCallback('getDefaultConversationColor');
// Various callbacks
this.installCallback('getAvailableIODevices');
this.installCallback('isPrimary');
this.installCallback('syncRequest');
this.installCallback('isPhoneNumberSharingEnabled');
// Getters only. These are set by the primary device
this.installSetting('blockedCount', { setter: false });
this.installSetting('linkPreviewSetting', { setter: false });
this.installSetting('phoneNumberDiscoverabilitySetting', { setter: false });
this.installSetting('phoneNumberSharingSetting', { setter: false });
this.installSetting('readReceiptSetting', { setter: false });
this.installSetting('typingIndicatorSetting', { setter: false });
this.installSetting('themeSetting');
this.installSetting('hideMenuBar');
this.installSetting('systemTraySetting', {
isEphemeral: true,
});
this.installSetting('notificationSetting');
this.installSetting('notificationDrawAttention');
this.installSetting('audioNotification');
this.installSetting('countMutedConversations');
this.installSetting('spellCheck', {
isEphemeral: true,
});
this.installSetting('autoDownloadUpdate');
this.installSetting('autoLaunch');
this.installSetting('alwaysRelayCalls');
this.installSetting('callRingtoneNotification');
this.installSetting('callSystemNotification');
this.installSetting('incomingCallNotification');
// Media settings
this.installSetting('preferredAudioInputDevice');
this.installSetting('preferredAudioOutputDevice');
this.installSetting('preferredVideoInputDevice');
this.installSetting('lastSyncTime');
this.installSetting('universalExpireTimer');
this.installSetting('zoomFactor');
installPermissionsHandler({ session, userConfig });
// These ones are different because its single source of truth is userConfig,
// not IndexedDB
ipc.on('settings:get:mediaPermissions', event => {
event.sender.send(
'settings:get-success:mediaPermissions',
null,
userConfig.get('mediaPermissions') || false
);
});
ipc.on('settings:get:mediaCameraPermissions', event => {
event.sender.send(
'settings:get-success:mediaCameraPermissions',
null,
userConfig.get('mediaCameraPermissions') || false
);
});
ipc.on('settings:set:mediaPermissions', (event, value) => {
userConfig.set('mediaPermissions', value);
// We reinstall permissions handler to ensure that a revoked permission takes effect
installPermissionsHandler({ session, userConfig });
event.sender.send('settings:set-success:mediaPermissions', null, value);
});
ipc.on('settings:set:mediaCameraPermissions', (event, value) => {
userConfig.set('mediaCameraPermissions', value);
// We reinstall permissions handler to ensure that a revoked permission takes effect
installPermissionsHandler({ session, userConfig });
event.sender.send(
'settings:set-success:mediaCameraPermissions',
null,
value
);
});
}
public getSettingFromMainWindow<Name extends keyof IPCEventsValuesType>(
name: Name
): Promise<IPCEventsValuesType[Name]> {
const { mainWindow } = this;
return new Promise((resolve, reject) => {
ipc.once(`settings:get-success:${name}`, (_event, error, value) => {
if (error) {
reject(error);
} else {
resolve(value);
}
});
if (!mainWindow || !mainWindow.webContents) {
reject(new Error('No main window available'));
return;
}
mainWindow.webContents.send(`settings:get:${name}`);
});
}
private installCallback<Name extends keyof IPCEventsCallbacksType>(
name: Name
): void {
ipc.on(`callbacks:call:${name}`, async (event, args) => {
const { mainWindow } = this;
const contents = event.sender;
if (!mainWindow || !mainWindow.webContents) {
return contents.send(
`callbacks:call-success:${name}`,
'Main window not found'
);
}
mainWindow.webContents.send(`callbacks:call:${name}`, args);
ipc.once(`callbacks:call-success:${name}`, (_event, error, value) => {
if (contents.isDestroyed()) {
return;
}
contents.send(`callbacks:call-success:${name}`, error, value);
});
});
}
private installSetting<Name extends keyof IPCEventsValuesType>(
name: Name,
{
getter = true,
setter = true,
isEphemeral = false,
}: { getter?: boolean; setter?: boolean; isEphemeral?: boolean } = {}
): void {
if (getter) {
ipc.on(`settings:get:${name}`, async event => {
const { mainWindow } = this;
if (mainWindow && mainWindow.webContents) {
let error: Error | undefined;
let value: unknown;
try {
value = await this.getSettingFromMainWindow(name);
} catch (caughtError) {
error = caughtError;
}
const contents = event.sender;
if (contents.isDestroyed()) {
return;
}
contents.send(`settings:get-success:${name}`, error, value);
}
});
}
if (!setter) {
return;
}
ipc.on(`settings:set:${name}`, (event, value) => {
if (isEphemeral) {
const ephemeralName = EPHEMERAL_NAME_MAP.get(name);
strictAssert(
ephemeralName !== undefined,
`${name} is not an ephemeral setting`
);
ephemeralConfig.set(ephemeralName, value);
}
const { mainWindow } = this;
if (mainWindow && mainWindow.webContents) {
ipc.once(`settings:set-success:${name}`, (_event, error) => {
const contents = event.sender;
if (contents.isDestroyed()) {
return;
}
contents.send(`settings:set-success:${name}`, error);
});
mainWindow.webContents.send(`settings:set:${name}`, value);
}
});
}
}