2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2017 Signal Messenger, LLC
|
2021-08-18 20:08:14 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BrowserWindow } from 'electron';
|
|
|
|
import { ipcMain as ipc, session } from 'electron';
|
2022-06-08 22:00:32 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2021-08-18 20:08:14 +00:00
|
|
|
|
|
|
|
import { userConfig } from '../../app/user_config';
|
|
|
|
import { ephemeralConfig } from '../../app/ephemeral_config';
|
|
|
|
import { installPermissionsHandler } from '../../app/permissions';
|
|
|
|
import { strictAssert } from '../util/assert';
|
2021-11-10 00:56:56 +00:00
|
|
|
import { explodePromise } from '../util/explodePromise';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-08-18 20:08:14 +00:00
|
|
|
IPCEventsValuesType,
|
|
|
|
IPCEventsCallbacksType,
|
|
|
|
} from '../util/createIPCEvents';
|
|
|
|
|
2021-10-20 19:56:49 +00:00
|
|
|
const EPHEMERAL_NAME_MAP = new Map([
|
|
|
|
['spellCheck', 'spell-check'],
|
|
|
|
['systemTraySetting', 'system-tray-setting'],
|
2022-05-11 22:58:14 +00:00
|
|
|
['themeSetting', 'theme-setting'],
|
2021-10-20 19:56:49 +00:00
|
|
|
]);
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
type ResponseQueueEntry = Readonly<{
|
|
|
|
resolve(value: unknown): void;
|
|
|
|
reject(error: Error): void;
|
|
|
|
}>;
|
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
export class SettingsChannel extends EventEmitter {
|
2021-08-18 20:08:14 +00:00
|
|
|
private mainWindow?: BrowserWindow;
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
private readonly responseQueue = new Map<number, ResponseQueueEntry>();
|
|
|
|
|
|
|
|
private responseSeq = 0;
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
public setMainWindow(mainWindow: BrowserWindow | undefined): void {
|
2021-08-18 20:08:14 +00:00
|
|
|
this.mainWindow = mainWindow;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
public getMainWindow(): BrowserWindow | undefined {
|
|
|
|
return this.mainWindow;
|
|
|
|
}
|
|
|
|
|
2021-08-18 20:08:14 +00:00
|
|
|
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
|
2022-10-19 16:35:56 +00:00
|
|
|
this.installCallback('deleteAllMyStories');
|
2021-08-18 20:08:14 +00:00
|
|
|
this.installCallback('getAvailableIODevices');
|
|
|
|
this.installCallback('isPrimary');
|
|
|
|
this.installCallback('syncRequest');
|
2021-08-19 01:04:38 +00:00
|
|
|
this.installCallback('isPhoneNumberSharingEnabled');
|
2023-04-14 18:16:28 +00:00
|
|
|
this.installCallback('isFormattingFlagEnabled');
|
2022-07-20 00:47:05 +00:00
|
|
|
this.installCallback('shouldShowStoriesSettings');
|
2021-08-18 20:08:14 +00:00
|
|
|
|
|
|
|
// Getters only. These are set by the primary device
|
|
|
|
this.installSetting('blockedCount', { setter: false });
|
|
|
|
this.installSetting('linkPreviewSetting', { setter: false });
|
|
|
|
this.installSetting('readReceiptSetting', { setter: false });
|
|
|
|
this.installSetting('typingIndicatorSetting', { setter: false });
|
|
|
|
|
2022-05-11 22:58:14 +00:00
|
|
|
this.installSetting('themeSetting', {
|
|
|
|
isEphemeral: true,
|
|
|
|
});
|
2021-08-18 20:08:14 +00:00
|
|
|
this.installSetting('hideMenuBar');
|
2021-10-20 19:56:49 +00:00
|
|
|
this.installSetting('systemTraySetting', {
|
|
|
|
isEphemeral: true,
|
|
|
|
});
|
2021-08-18 20:08:14 +00:00
|
|
|
|
|
|
|
this.installSetting('notificationSetting');
|
|
|
|
this.installSetting('notificationDrawAttention');
|
2023-05-08 19:59:36 +00:00
|
|
|
this.installSetting('audioMessage');
|
2021-08-18 20:08:14 +00:00
|
|
|
this.installSetting('audioNotification');
|
|
|
|
this.installSetting('countMutedConversations');
|
|
|
|
|
2022-12-02 23:54:37 +00:00
|
|
|
this.installSetting('sentMediaQualitySetting');
|
2021-08-18 20:08:14 +00:00
|
|
|
this.installSetting('spellCheck', {
|
|
|
|
isEphemeral: true,
|
|
|
|
});
|
2023-04-14 18:16:28 +00:00
|
|
|
this.installSetting('textFormatting');
|
2021-08-18 20:08:14 +00:00
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
this.installSetting('autoDownloadUpdate');
|
2021-08-18 20:08:14 +00:00
|
|
|
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');
|
|
|
|
|
2022-10-04 17:53:36 +00:00
|
|
|
this.installSetting('hasStoriesDisabled');
|
2021-08-18 20:08:14 +00:00
|
|
|
this.installSetting('zoomFactor');
|
|
|
|
|
2023-02-23 21:32:19 +00:00
|
|
|
this.installSetting('phoneNumberDiscoverabilitySetting');
|
|
|
|
this.installSetting('phoneNumberSharingSetting');
|
|
|
|
|
2023-04-20 15:59:17 +00:00
|
|
|
installPermissionsHandler({ session: session.defaultSession, userConfig });
|
2021-08-18 20:08:14 +00:00
|
|
|
|
|
|
|
// These ones are different because its single source of truth is userConfig,
|
|
|
|
// not IndexedDB
|
2021-11-10 00:56:56 +00:00
|
|
|
ipc.handle('settings:get:mediaPermissions', () => {
|
|
|
|
return userConfig.get('mediaPermissions') || false;
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
2021-11-10 00:56:56 +00:00
|
|
|
ipc.handle('settings:get:mediaCameraPermissions', () => {
|
|
|
|
return userConfig.get('mediaCameraPermissions') || false;
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
2021-11-10 00:56:56 +00:00
|
|
|
ipc.handle('settings:set:mediaPermissions', (_event, value) => {
|
2021-08-18 20:08:14 +00:00
|
|
|
userConfig.set('mediaPermissions', value);
|
|
|
|
|
|
|
|
// We reinstall permissions handler to ensure that a revoked permission takes effect
|
2023-04-20 15:59:17 +00:00
|
|
|
installPermissionsHandler({
|
|
|
|
session: session.defaultSession,
|
|
|
|
userConfig,
|
|
|
|
});
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
2021-11-10 00:56:56 +00:00
|
|
|
ipc.handle('settings:set:mediaCameraPermissions', (_event, value) => {
|
2021-08-18 20:08:14 +00:00
|
|
|
userConfig.set('mediaCameraPermissions', value);
|
|
|
|
|
|
|
|
// We reinstall permissions handler to ensure that a revoked permission takes effect
|
2023-04-20 15:59:17 +00:00
|
|
|
installPermissionsHandler({
|
|
|
|
session: session.defaultSession,
|
|
|
|
userConfig,
|
|
|
|
});
|
2021-11-10 00:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ipc.on('settings:response', (_event, seq, error, value) => {
|
|
|
|
const entry = this.responseQueue.get(seq);
|
|
|
|
this.responseQueue.delete(seq);
|
|
|
|
if (!entry) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-18 20:08:14 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const { resolve, reject } = entry;
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(value);
|
|
|
|
}
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
private waitForResponse<Value>(): { promise: Promise<Value>; seq: number } {
|
|
|
|
const seq = this.responseSeq;
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-bitwise
|
|
|
|
this.responseSeq = (this.responseSeq + 1) & 0x7fffffff;
|
|
|
|
|
|
|
|
const { promise, resolve, reject } = explodePromise<Value>();
|
|
|
|
|
|
|
|
this.responseQueue.set(seq, { resolve, reject });
|
|
|
|
|
|
|
|
return { seq, promise };
|
|
|
|
}
|
|
|
|
|
2021-08-18 20:08:14 +00:00
|
|
|
public getSettingFromMainWindow<Name extends keyof IPCEventsValuesType>(
|
|
|
|
name: Name
|
|
|
|
): Promise<IPCEventsValuesType[Name]> {
|
|
|
|
const { mainWindow } = this;
|
2021-11-10 00:56:56 +00:00
|
|
|
if (!mainWindow || !mainWindow.webContents) {
|
|
|
|
throw new Error('No main window');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { seq, promise } = this.waitForResponse<IPCEventsValuesType[Name]>();
|
|
|
|
|
|
|
|
mainWindow.webContents.send(`settings:get:${name}`, { seq });
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setSettingInMainWindow<Name extends keyof IPCEventsValuesType>(
|
|
|
|
name: Name,
|
|
|
|
value: IPCEventsValuesType[Name]
|
|
|
|
): Promise<void> {
|
|
|
|
const { mainWindow } = this;
|
|
|
|
if (!mainWindow || !mainWindow.webContents) {
|
|
|
|
throw new Error('No main window');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { seq, promise } = this.waitForResponse<void>();
|
|
|
|
|
|
|
|
mainWindow.webContents.send(`settings:set:${name}`, { seq, value });
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public invokeCallbackInMainWindow<Name extends keyof IPCEventsCallbacksType>(
|
|
|
|
name: Name,
|
|
|
|
args: ReadonlyArray<unknown>
|
|
|
|
): Promise<unknown> {
|
|
|
|
const { mainWindow } = this;
|
|
|
|
if (!mainWindow || !mainWindow.webContents) {
|
|
|
|
throw new Error('Main window not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { seq, promise } = this.waitForResponse<unknown>();
|
|
|
|
|
|
|
|
mainWindow.webContents.send(`settings:call:${name}`, { seq, args });
|
|
|
|
|
|
|
|
return promise;
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private installCallback<Name extends keyof IPCEventsCallbacksType>(
|
|
|
|
name: Name
|
|
|
|
): void {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipc.handle(`settings:call:${name}`, async (_event, args) => {
|
|
|
|
return this.invokeCallbackInMainWindow(name, args);
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private installSetting<Name extends keyof IPCEventsValuesType>(
|
|
|
|
name: Name,
|
|
|
|
{
|
|
|
|
getter = true,
|
|
|
|
setter = true,
|
|
|
|
isEphemeral = false,
|
|
|
|
}: { getter?: boolean; setter?: boolean; isEphemeral?: boolean } = {}
|
|
|
|
): void {
|
|
|
|
if (getter) {
|
2021-11-10 00:56:56 +00:00
|
|
|
ipc.handle(`settings:get:${name}`, async () => {
|
|
|
|
return this.getSettingFromMainWindow(name);
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
ipc.handle(`settings:set:${name}`, async (_event, value) => {
|
2021-08-18 20:08:14 +00:00
|
|
|
if (isEphemeral) {
|
2021-10-20 19:56:49 +00:00
|
|
|
const ephemeralName = EPHEMERAL_NAME_MAP.get(name);
|
|
|
|
strictAssert(
|
|
|
|
ephemeralName !== undefined,
|
|
|
|
`${name} is not an ephemeral setting`
|
|
|
|
);
|
|
|
|
ephemeralConfig.set(ephemeralName, value);
|
2021-08-18 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
await this.setSettingInMainWindow(name, value);
|
|
|
|
|
|
|
|
this.emit(`change:${name}`, value);
|
2021-08-18 20:08:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|