Use SignalContext instead of SignalWindow

This commit is contained in:
Josh Perez 2021-10-07 19:28:47 -04:00 committed by GitHub
parent a70a8a88d6
commit 75dab30367
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 180 additions and 231 deletions

View file

@ -1,7 +1,9 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const { bytes } = window.SignalContext;
import { Bytes } from './context/Bytes';
const bytes = window.SignalContext?.bytes || new Bytes();
export function fromBase64(value: string): Uint8Array {
return bytes.fromBase64(value);

View file

@ -24,7 +24,7 @@ type SystemThemeType = 'dark' | 'light';
export type SystemThemeHolder = { systemTheme: SystemThemeType };
type NativeThemeType = {
export type NativeThemeType = {
getSystemTheme: () => SystemThemeType;
subscribe: (fn: Callback) => void;
update: () => SystemThemeType;

View file

@ -1,28 +0,0 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { Bytes } from './Bytes';
import { Crypto } from './Crypto';
import { Timers } from './Timers';
import {
createNativeThemeListener,
MinimalIPC,
} from './createNativeThemeListener';
export class Context {
public readonly bytes = new Bytes();
public readonly crypto = new Crypto();
public readonly timers = new Timers();
public readonly nativeThemeListener;
constructor(private readonly ipc: MinimalIPC) {
this.nativeThemeListener = createNativeThemeListener(ipc, window);
}
setIsCallActive(isCallActive: boolean): void {
this.ipc.send('set-is-call-active', isCallActive);
}
}

View file

@ -100,8 +100,8 @@ function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>): void {
log.setLogAtLevel(logAtLevel);
window.SignalWindow = window.SignalWindow || {};
window.SignalWindow.log = {
window.SignalContext = window.SignalContext || {};
window.SignalContext.log = {
fatal: log.fatal,
error: log.error,
warn: log.warn,

View file

@ -322,7 +322,7 @@ class NotificationService extends EventEmitter {
}
public clear(): void {
window.SignalWindow.log.info('Removing notification');
window.SignalContext.log.info('Removing notification');
this.notificationData = null;
this.update();
}

View file

@ -41,7 +41,7 @@ function buildAvatarUpdater({ field }: { field: 'avatar' | 'profileAvatar' }) {
const { hash, path } = avatar;
const exists = await doesAttachmentExist(path);
if (!exists) {
window.SignalWindow.log.warn(
window.SignalContext.log.warn(
`Conversation.buildAvatarUpdater: attachment ${path} did not exist`
);
}

View file

@ -14058,14 +14058,14 @@
{
"rule": "DOM-innerHTML",
"path": "ts/windows/loading/start.js",
"line": " message.innerHTML = window.SignalWindow.i18n('optimizingApplication');",
"line": " message.innerHTML = window.SignalContext.i18n('optimizingApplication');",
"reasonCategory": "usageTrusted",
"updated": "2021-09-17T21:02:59.414Z"
},
{
"rule": "DOM-innerHTML",
"path": "ts/windows/loading/start.ts",
"line": " message.innerHTML = window.SignalWindow.i18n('optimizingApplication');",
"line": " message.innerHTML = window.SignalContext.i18n('optimizingApplication');",
"reasonCategory": "usageTrusted",
"updated": "2021-09-17T21:02:59.414Z"
}

22
ts/window.d.ts vendored
View file

@ -10,6 +10,7 @@ import moment from 'moment';
import PQueue from 'p-queue/dist';
import { Ref } from 'react';
import { imageToBlurHash } from './util/imageToBlurHash';
import type { ParsedUrlQuery } from 'querystring';
import * as Util from './util';
import {
ConversationModelCollectionType,
@ -96,7 +97,6 @@ import { MIMEType } from './types/MIME';
import { DownloadedAttachmentType } from './types/Attachment';
import { ElectronLocaleType } from './util/mapToSupportLocale';
import { SignalProtocolStore } from './SignalProtocolStore';
import { Context as SignalContext } from './context';
import { StartupQueue } from './util/StartupQueue';
import { SocketStatus } from './types/SocketStatus';
import SyncRequest from './textsecure/SyncRequest';
@ -111,8 +111,7 @@ import { QualifiedAddress } from './types/QualifiedAddress';
import { CI } from './CI';
import { IPCEventsType, IPCEventsValuesType } from './util/createIPCEvents';
import { ConversationView } from './views/conversation_view';
import { LoggerType } from './types/Logging';
import { SettingType } from './util/preload';
import type { SignalContextType } from './windows/context';
export { Long } from 'long';
@ -462,7 +461,6 @@ declare global {
};
challengeHandler: ChallengeHandler;
};
SignalContext: SignalContext;
ConversationController: ConversationController;
Events: IPCEventsType;
@ -489,21 +487,7 @@ declare global {
RETRY_DELAY: boolean;
// Context Isolation
SignalWindow: {
Settings: {
themeSetting: SettingType<IPCEventsValuesType['themeSetting']>;
waitForChange: () => Promise<void>;
};
config: string;
context: SignalContext;
getAppInstance: () => string | undefined;
getEnvironment: () => string;
getNodeVersion: () => string;
getVersion: () => string;
i18n: LocalizerType;
log: LoggerType;
renderWindow: () => void;
};
SignalContext: SignalContextType;
}
// We want to extend `Error`, so we need an interface.

View file

@ -5,18 +5,15 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { contextBridge, ipcRenderer } from 'electron';
// It is important to call this as early as possible
import '../context';
import { SignalWindow } from '../configure';
import { SignalContext } from '../context';
import { About } from '../../components/About';
contextBridge.exposeInMainWorld('SignalWindow', {
...SignalWindow,
contextBridge.exposeInMainWorld('SignalContext', {
...SignalContext,
renderWindow: () => {
const environmentText: Array<string> = [SignalWindow.getEnvironment()];
const environmentText: Array<string> = [SignalContext.getEnvironment()];
const appInstance = SignalWindow.getAppInstance();
const appInstance = SignalContext.getAppInstance();
if (appInstance) {
environmentText.push(appInstance);
}
@ -25,8 +22,8 @@ contextBridge.exposeInMainWorld('SignalWindow', {
React.createElement(About, {
closeAbout: () => ipcRenderer.send('close-about'),
environment: environmentText.join(' - '),
i18n: SignalWindow.i18n,
version: SignalWindow.getVersion(),
i18n: SignalContext.i18n,
version: SignalContext.getVersion(),
}),
document.getElementById('app')
);

View file

@ -2,13 +2,13 @@
// SPDX-License-Identifier: AGPL-3.0-only
async function applyTheme() {
const theme = await window.SignalWindow.Settings.themeSetting.getValue();
const theme = await window.SignalContext.Settings.themeSetting.getValue();
document.body.classList.remove('light-theme');
document.body.classList.remove('dark-theme');
document.body.classList.add(
`${
theme === 'system'
? window.SignalWindow.context.nativeThemeListener.getSystemTheme()
? window.SignalContext.nativeThemeListener.getSystemTheme()
: theme
}-theme`
);
@ -18,7 +18,7 @@ async function applyThemeLoop() {
// eslint-disable-next-line no-constant-condition
while (true) {
// eslint-disable-next-line no-await-in-loop
await window.SignalWindow.Settings.waitForChange();
await window.SignalContext.Settings.waitForChange();
// eslint-disable-next-line no-await-in-loop
await applyTheme();
@ -28,6 +28,6 @@ async function applyThemeLoop() {
applyTheme();
applyThemeLoop();
window.SignalWindow.context.nativeThemeListener.subscribe(() => {
window.SignalContext.nativeThemeListener.subscribe(() => {
applyTheme();
});

View file

@ -1,44 +0,0 @@
// Copyright 2018-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import url from 'url';
import { ipcRenderer } from 'electron';
import { setupI18n } from '../util/setupI18n';
import {
getEnvironment,
parseEnvironment,
setEnvironment,
} from '../environment';
import { strictAssert } from '../util/assert';
import { createSetting } from '../util/preload';
import { initialize as initializeLogging } from '../logging/set_up_renderer_logging';
import { waitForSettingsChange } from './waitForSettingsChange';
const config = url.parse(window.location.toString(), true).query;
const { locale } = config;
strictAssert(locale, 'locale could not be parsed from config');
strictAssert(typeof locale === 'string', 'locale is not a string');
const localeMessages = ipcRenderer.sendSync('locale-data');
setEnvironment(parseEnvironment(config.environment));
strictAssert(Boolean(window.SignalContext), 'context must be defined');
initializeLogging();
export const SignalWindow = {
Settings: {
themeSetting: createSetting('themeSetting', { setter: false }),
waitForChange: waitForSettingsChange,
},
config,
context: window.SignalContext,
getAppInstance: (): string | undefined =>
config.appInstance ? String(config.appInstance) : undefined,
getEnvironment,
getNodeVersion: (): string => String(config.node_version),
getVersion: (): string => String(config.version),
i18n: setupI18n(locale, localeMessages),
log: window.SignalWindow.log,
};

View file

@ -1,8 +1,83 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { ipcRenderer as ipc } from 'electron';
import { ipcRenderer } from 'electron';
import url from 'url';
import type { ParsedUrlQuery } from 'querystring';
import type { IPCEventsValuesType } from '../util/createIPCEvents';
import type { LocalizerType } from '../types/Util';
import type { LoggerType } from '../types/Logging';
import type { NativeThemeType } from '../context/createNativeThemeListener';
import type { SettingType } from '../util/preload';
import { Bytes } from '../context/Bytes';
import { Crypto } from '../context/Crypto';
import { Timers } from '../context/Timers';
import { Context } from '../context';
import { setupI18n } from '../util/setupI18n';
import {
getEnvironment,
parseEnvironment,
setEnvironment,
} from '../environment';
import { strictAssert } from '../util/assert';
import { createSetting } from '../util/preload';
import { initialize as initializeLogging } from '../logging/set_up_renderer_logging';
import { waitForSettingsChange } from './waitForSettingsChange';
import { createNativeThemeListener } from '../context/createNativeThemeListener';
window.SignalContext = new Context(ipc);
const config = url.parse(window.location.toString(), true).query;
const { locale } = config;
strictAssert(locale, 'locale could not be parsed from config');
strictAssert(typeof locale === 'string', 'locale is not a string');
const localeMessages = ipcRenderer.sendSync('locale-data');
setEnvironment(parseEnvironment(config.environment));
strictAssert(Boolean(window.SignalContext), 'context must be defined');
initializeLogging();
export type SignalContextType = {
bytes: Bytes;
crypto: Crypto;
timers: Timers;
nativeThemeListener: NativeThemeType;
setIsCallActive: (isCallActive: boolean) => unknown;
Settings: {
themeSetting: SettingType<IPCEventsValuesType['themeSetting']>;
waitForChange: () => Promise<void>;
};
config: ParsedUrlQuery;
getAppInstance: () => string | undefined;
getEnvironment: () => string;
getNodeVersion: () => string;
getVersion: () => string;
i18n: LocalizerType;
log: LoggerType;
renderWindow?: () => void;
};
export const SignalContext: SignalContextType = {
Settings: {
themeSetting: createSetting('themeSetting', { setter: false }),
waitForChange: waitForSettingsChange,
},
bytes: new Bytes(),
config,
crypto: new Crypto(),
getAppInstance: (): string | undefined =>
config.appInstance ? String(config.appInstance) : undefined,
getEnvironment,
getNodeVersion: (): string => String(config.node_version),
getVersion: (): string => String(config.version),
i18n: setupI18n(locale, localeMessages),
log: window.SignalContext.log,
nativeThemeListener: createNativeThemeListener(ipcRenderer, window),
setIsCallActive(isCallActive: boolean): void {
ipcRenderer.send('set-is-call-active', isCallActive);
},
timers: new Timers(),
};
window.SignalContext = SignalContext;

View file

@ -5,19 +5,16 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { contextBridge, ipcRenderer } from 'electron';
// It is important to call this as early as possible
import '../context';
import { SignalWindow } from '../configure';
import { SignalContext } from '../context';
import { DebugLogWindow } from '../../components/DebugLogWindow';
import * as debugLog from '../../logging/debuglogs';
contextBridge.exposeInMainWorld('SignalWindow', {
...SignalWindow,
contextBridge.exposeInMainWorld('SignalContext', {
...SignalContext,
renderWindow: () => {
const environmentText: Array<string> = [SignalWindow.getEnvironment()];
const environmentText: Array<string> = [SignalContext.getEnvironment()];
const appInstance = SignalWindow.getAppInstance();
const appInstance = SignalContext.getAppInstance();
if (appInstance) {
environmentText.push(appInstance);
}
@ -27,15 +24,15 @@ contextBridge.exposeInMainWorld('SignalWindow', {
closeWindow: () => ipcRenderer.send('close-debug-log'),
downloadLog: (logText: string) =>
ipcRenderer.send('show-debug-log-save-dialog', logText),
i18n: SignalWindow.i18n,
i18n: SignalContext.i18n,
fetchLogs() {
return debugLog.fetch(
SignalWindow.getNodeVersion(),
SignalWindow.getVersion()
SignalContext.getNodeVersion(),
SignalContext.getVersion()
);
},
uploadLogs(logs: string) {
return debugLog.upload(logs, SignalWindow.getVersion());
return debugLog.upload(logs, SignalContext.getVersion());
},
}),
document.getElementById('app')

View file

@ -1,4 +1,8 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
window.SignalWindow.renderWindow();
if (window.SignalContext.renderWindow) {
window.SignalContext.renderWindow();
} else {
window.SignalContext.log.error('renderWindow is undefined!');
}

View file

@ -3,9 +3,6 @@
import { contextBridge } from 'electron';
// It is important to call this as early as possible
import '../context';
import { SignalContext } from '../context';
import { SignalWindow } from '../configure';
contextBridge.exposeInMainWorld('SignalWindow', SignalWindow);
contextBridge.exposeInMainWorld('SignalContext', SignalContext);

View file

@ -3,5 +3,5 @@
const message = document.getElementById('message');
if (message) {
message.innerHTML = window.SignalWindow.i18n('optimizingApplication');
message.innerHTML = window.SignalContext.i18n('optimizingApplication');
}

View file

@ -5,11 +5,9 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { contextBridge, ipcRenderer } from 'electron';
// It is important to call this as early as possible
import '../context';
import { SignalContext } from '../context';
import { createSetting } from '../../util/preload';
import { SignalWindow } from '../configure';
import { PermissionsPopup } from '../../components/PermissionsPopup';
const mediaCameraPermissions = createSetting('mediaCameraPermissions', {
@ -24,21 +22,21 @@ contextBridge.exposeInMainWorld(
window.SignalContext.nativeThemeListener
);
contextBridge.exposeInMainWorld('SignalWindow', {
...SignalWindow,
contextBridge.exposeInMainWorld('SignalContext', {
...SignalContext,
renderWindow: () => {
const forCalling = SignalWindow.config.forCalling === 'true';
const forCamera = SignalWindow.config.forCamera === 'true';
const forCalling = SignalContext.config.forCalling === 'true';
const forCamera = SignalContext.config.forCamera === 'true';
let message;
if (forCalling) {
if (forCamera) {
message = SignalWindow.i18n('videoCallingPermissionNeeded');
message = SignalContext.i18n('videoCallingPermissionNeeded');
} else {
message = SignalWindow.i18n('audioCallingPermissionNeeded');
message = SignalContext.i18n('audioCallingPermissionNeeded');
}
} else {
message = SignalWindow.i18n('audioPermissionNeeded');
message = SignalContext.i18n('audioPermissionNeeded');
}
function onClose() {
@ -47,7 +45,7 @@ contextBridge.exposeInMainWorld('SignalWindow', {
ReactDOM.render(
React.createElement(PermissionsPopup, {
i18n: SignalWindow.i18n,
i18n: SignalContext.i18n,
message,
onAccept: () => {
if (!forCamera) {

View file

@ -5,18 +5,15 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { contextBridge, ipcRenderer } from 'electron';
// It is important to call this as early as possible
import '../context';
import { SignalWindow } from '../configure';
import { SignalContext } from '../context';
import { CallingScreenSharingController } from '../../components/CallingScreenSharingController';
contextBridge.exposeInMainWorld('SignalWindow', SignalWindow);
contextBridge.exposeInMainWorld('SignalContext', SignalContext);
function renderScreenSharingController(presentedSourceName: string): void {
ReactDOM.render(
React.createElement(CallingScreenSharingController, {
i18n: SignalWindow.i18n,
i18n: SignalContext.i18n,
onCloseController: () =>
ipcRenderer.send('close-screen-share-controller'),
onStopSharing: () => ipcRenderer.send('stop-screen-share'),

View file

@ -5,10 +5,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { contextBridge, ipcRenderer } from 'electron';
// It is important to call this as early as possible
import '../context';
import { SignalWindow } from '../configure';
import { SignalContext } from '../context';
import * as Settings from '../../types/Settings';
import { Preferences } from '../../components/Preferences';
import {
@ -260,7 +257,7 @@ const renderPreferences = async () => {
editCustomColor: ipcEditCustomColor,
getConversationsWithCustomColor: ipcGetConversationsWithCustomColor,
initialSpellCheckSetting:
SignalWindow.config.appStartInitialSpellcheckSetting === 'true',
SignalContext.config.appStartInitialSpellcheckSetting === 'true',
makeSyncRequest: ipcMakeSyncRequest,
removeCustomColor: ipcRemoveCustomColor,
removeCustomColorOnConversations: ipcRemoveCustomColorOnConversations,
@ -277,7 +274,7 @@ const renderPreferences = async () => {
isPhoneNumberSharingSupported,
isSyncSupported: !isSyncNotSupported,
isSystemTraySupported: Settings.isSystemTraySupported(
SignalWindow.getVersion()
SignalContext.getVersion()
),
// Change handlers
@ -343,7 +340,7 @@ const renderPreferences = async () => {
// rerender.
onZoomFactorChange: settingZoomFactor.setValue,
i18n: SignalWindow.i18n,
i18n: SignalContext.i18n,
};
function reRender<Value>(f: (value: Value) => Promise<Value>) {
@ -361,7 +358,7 @@ const renderPreferences = async () => {
ipcRenderer.on('preferences-changed', () => renderPreferences());
contextBridge.exposeInMainWorld('SignalWindow', {
...SignalWindow,
contextBridge.exposeInMainWorld('SignalContext', {
...SignalContext,
renderWindow: renderPreferences,
});