2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-12-16 15:02:22 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
import type { ComponentProps } from 'react';
|
|
|
|
import React, { memo, useCallback, useEffect, useRef, useState } from 'react';
|
2021-12-16 15:02:22 +00:00
|
|
|
import { useSelector } from 'react-redux';
|
2023-05-10 21:36:45 +00:00
|
|
|
import pTimeout, { TimeoutError } from 'p-timeout';
|
2021-12-16 15:02:22 +00:00
|
|
|
import { getIntl } from '../selectors/user';
|
2023-03-20 20:42:00 +00:00
|
|
|
import { getUpdatesState } from '../selectors/updates';
|
|
|
|
import { useUpdatesActions } from '../ducks/updates';
|
|
|
|
import { hasExpired as hasExpiredSelector } from '../selectors/expiration';
|
2021-12-16 15:02:22 +00:00
|
|
|
import * as log from '../../logging/log';
|
|
|
|
import type { Loadable } from '../../util/loadable';
|
|
|
|
import { LoadingState } from '../../util/loadable';
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from '../../util/assert';
|
2021-12-16 15:02:22 +00:00
|
|
|
import { explodePromise } from '../../util/explodePromise';
|
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
2023-04-11 03:54:43 +00:00
|
|
|
import * as Registration from '../../util/registration';
|
2021-12-16 15:02:22 +00:00
|
|
|
import {
|
|
|
|
InstallScreen,
|
|
|
|
InstallScreenStep,
|
|
|
|
} from '../../components/InstallScreen';
|
|
|
|
import { InstallError } from '../../components/installScreen/InstallScreenErrorStep';
|
|
|
|
import { MAX_DEVICE_NAME_LENGTH } from '../../components/installScreen/InstallScreenChoosingDeviceNameStep';
|
2024-01-29 20:09:54 +00:00
|
|
|
import { WidthBreakpoint } from '../../components/_util';
|
2021-12-16 15:02:22 +00:00
|
|
|
import { HTTPError } from '../../textsecure/Errors';
|
|
|
|
import { isRecord } from '../../util/isRecord';
|
2024-03-15 14:20:33 +00:00
|
|
|
import type { ConfirmNumberResultType } from '../../textsecure/AccountManager';
|
2022-03-01 23:01:21 +00:00
|
|
|
import * as Errors from '../../types/errors';
|
2021-12-16 15:02:22 +00:00
|
|
|
import { normalizeDeviceName } from '../../util/normalizeDeviceName';
|
2023-04-20 21:23:19 +00:00
|
|
|
import OS from '../../util/os/osMain';
|
2023-05-10 21:36:45 +00:00
|
|
|
import { SECOND } from '../../util/durations';
|
|
|
|
import { BackOff } from '../../util/BackOff';
|
|
|
|
import { drop } from '../../util/drop';
|
2024-01-29 20:09:54 +00:00
|
|
|
import { SmartToastManager } from './ToastManager';
|
2024-03-15 14:20:33 +00:00
|
|
|
import { fileToBytes } from '../../util/fileToBytes';
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
type PropsType = ComponentProps<typeof InstallScreen>;
|
|
|
|
|
|
|
|
type StateType =
|
|
|
|
| {
|
|
|
|
step: InstallScreenStep.Error;
|
|
|
|
error: InstallError;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
step: InstallScreenStep.QrCodeNotScanned;
|
|
|
|
provisioningUrl: Loadable<string>;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
step: InstallScreenStep.ChoosingDeviceName;
|
|
|
|
deviceName: string;
|
2024-03-15 14:20:33 +00:00
|
|
|
backupFile?: File;
|
2021-12-16 15:02:22 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
step: InstallScreenStep.LinkInProgress;
|
|
|
|
};
|
|
|
|
|
|
|
|
const INITIAL_STATE: StateType = {
|
|
|
|
step: InstallScreenStep.QrCodeNotScanned,
|
|
|
|
provisioningUrl: { loadingState: LoadingState.Loading },
|
|
|
|
};
|
|
|
|
|
2023-05-10 21:36:45 +00:00
|
|
|
const qrCodeBackOff = new BackOff([
|
|
|
|
10 * SECOND,
|
|
|
|
20 * SECOND,
|
|
|
|
30 * SECOND,
|
|
|
|
60 * SECOND,
|
|
|
|
]);
|
|
|
|
|
2021-12-16 15:02:22 +00:00
|
|
|
function getInstallError(err: unknown): InstallError {
|
|
|
|
if (err instanceof HTTPError) {
|
|
|
|
switch (err.code) {
|
|
|
|
case -1:
|
|
|
|
return InstallError.ConnectionFailed;
|
|
|
|
case 409:
|
|
|
|
return InstallError.TooOld;
|
|
|
|
case 411:
|
|
|
|
return InstallError.TooManyDevices;
|
|
|
|
default:
|
|
|
|
return InstallError.UnknownError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// AccountManager.registerSecondDevice uses this specific "websocket closed" error
|
|
|
|
// message.
|
|
|
|
if (isRecord(err) && err.message === 'websocket closed') {
|
|
|
|
return InstallError.ConnectionFailed;
|
|
|
|
}
|
|
|
|
return InstallError.UnknownError;
|
|
|
|
}
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const SmartInstallScreen = memo(function SmartInstallScreen() {
|
2021-12-16 15:02:22 +00:00
|
|
|
const i18n = useSelector(getIntl);
|
2023-03-20 20:42:00 +00:00
|
|
|
const updates = useSelector(getUpdatesState);
|
|
|
|
const { startUpdate } = useUpdatesActions();
|
|
|
|
const hasExpired = useSelector(hasExpiredSelector);
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
const chooseDeviceNamePromiseWrapperRef = useRef(explodePromise<string>());
|
2024-03-15 14:20:33 +00:00
|
|
|
const chooseBackupFilePromiseWrapperRef = useRef(
|
|
|
|
explodePromise<File | undefined>()
|
|
|
|
);
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
const [state, setState] = useState<StateType>(INITIAL_STATE);
|
2023-05-10 21:36:45 +00:00
|
|
|
const [retryCounter, setRetryCounter] = useState(0);
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
const setProvisioningUrl = useCallback(
|
|
|
|
(value: string) => {
|
|
|
|
setState(currentState => {
|
|
|
|
if (currentState.step !== InstallScreenStep.QrCodeNotScanned) {
|
|
|
|
return currentState;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...currentState,
|
|
|
|
provisioningUrl: {
|
|
|
|
loadingState: LoadingState.Loaded,
|
|
|
|
value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[setState]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onQrCodeScanned = useCallback(() => {
|
|
|
|
setState(currentState => {
|
|
|
|
if (currentState.step !== InstallScreenStep.QrCodeNotScanned) {
|
|
|
|
return currentState;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
step: InstallScreenStep.ChoosingDeviceName,
|
|
|
|
deviceName: normalizeDeviceName(
|
|
|
|
window.textsecure.storage.user.getDeviceName() ||
|
|
|
|
window.getHostName() ||
|
|
|
|
''
|
|
|
|
).slice(0, MAX_DEVICE_NAME_LENGTH),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}, [setState]);
|
|
|
|
|
|
|
|
const setDeviceName = useCallback(
|
|
|
|
(deviceName: string) => {
|
|
|
|
setState(currentState => {
|
|
|
|
if (currentState.step !== InstallScreenStep.ChoosingDeviceName) {
|
|
|
|
return currentState;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...currentState,
|
|
|
|
deviceName,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[setState]
|
|
|
|
);
|
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
const setBackupFile = useCallback(
|
|
|
|
(backupFile: File) => {
|
|
|
|
setState(currentState => {
|
|
|
|
if (currentState.step !== InstallScreenStep.ChoosingDeviceName) {
|
|
|
|
return currentState;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...currentState,
|
|
|
|
backupFile,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[setState]
|
|
|
|
);
|
|
|
|
|
2021-12-16 15:02:22 +00:00
|
|
|
const onSubmitDeviceName = useCallback(() => {
|
|
|
|
if (state.step !== InstallScreenStep.ChoosingDeviceName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let deviceName: string = normalizeDeviceName(state.deviceName);
|
|
|
|
if (!deviceName.length) {
|
|
|
|
// This should be impossible, but we have it here just in case.
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-12-16 15:02:22 +00:00
|
|
|
false,
|
|
|
|
'Unexpected empty device name. Falling back to placeholder value'
|
|
|
|
);
|
2023-03-30 00:03:25 +00:00
|
|
|
deviceName = i18n('icu:Install__choose-device-name__placeholder');
|
2021-12-16 15:02:22 +00:00
|
|
|
}
|
|
|
|
chooseDeviceNamePromiseWrapperRef.current.resolve(deviceName);
|
2024-03-15 14:20:33 +00:00
|
|
|
chooseBackupFilePromiseWrapperRef.current.resolve(state.backupFile);
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
setState({ step: InstallScreenStep.LinkInProgress });
|
|
|
|
}, [state, i18n]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let hasCleanedUp = false;
|
2023-05-15 15:08:15 +00:00
|
|
|
const qrCodeResolution = explodePromise<void>();
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
const accountManager = window.getAccountManager();
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(accountManager, 'Expected an account manager');
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
const updateProvisioningUrl = (value: string): void => {
|
|
|
|
if (hasCleanedUp) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-15 15:08:15 +00:00
|
|
|
qrCodeResolution.resolve();
|
2021-12-16 15:02:22 +00:00
|
|
|
setProvisioningUrl(value);
|
|
|
|
};
|
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
const confirmNumber = async (): Promise<ConfirmNumberResultType> => {
|
2021-12-16 15:02:22 +00:00
|
|
|
if (hasCleanedUp) {
|
|
|
|
throw new Error('Cannot confirm number; the component was unmounted');
|
|
|
|
}
|
|
|
|
onQrCodeScanned();
|
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
let deviceName: string;
|
|
|
|
let backupFileData: Uint8Array | undefined;
|
2023-01-19 00:02:03 +00:00
|
|
|
if (window.SignalCI) {
|
2024-03-15 14:20:33 +00:00
|
|
|
({ deviceName, backupData: backupFileData } = window.SignalCI);
|
|
|
|
} else {
|
|
|
|
deviceName = await chooseDeviceNamePromiseWrapperRef.current.promise;
|
|
|
|
const backupFile = await chooseBackupFilePromiseWrapperRef.current
|
|
|
|
.promise;
|
2021-12-16 15:02:22 +00:00
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
backupFileData = backupFile ? await fileToBytes(backupFile) : undefined;
|
|
|
|
}
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
if (hasCleanedUp) {
|
|
|
|
throw new Error('Cannot confirm number; the component was unmounted');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all data from the database unless we're in the middle of a re-link.
|
|
|
|
// Without this, the app restarts at certain times and can cause weird things to
|
|
|
|
// happen, like data from a previous light import showing up after a new install.
|
2023-04-11 03:54:43 +00:00
|
|
|
const shouldRetainData = Registration.everDone();
|
2021-12-16 15:02:22 +00:00
|
|
|
if (!shouldRetainData) {
|
|
|
|
try {
|
|
|
|
await window.textsecure.storage.protocol.removeAllData();
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'confirmNumber: error clearing database',
|
2022-03-01 23:01:21 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-12-16 15:02:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasCleanedUp) {
|
|
|
|
throw new Error('Cannot confirm number; the component was unmounted');
|
|
|
|
}
|
|
|
|
|
2024-03-15 14:20:33 +00:00
|
|
|
return { deviceName, backupFile: backupFileData };
|
2021-12-16 15:02:22 +00:00
|
|
|
};
|
|
|
|
|
2023-05-10 21:36:45 +00:00
|
|
|
async function getQRCode(): Promise<void> {
|
|
|
|
const sleepError = new TimeoutError();
|
2021-12-16 15:02:22 +00:00
|
|
|
try {
|
2023-05-10 21:36:45 +00:00
|
|
|
const qrCodePromise = accountManager.registerSecondDevice(
|
2021-12-16 15:02:22 +00:00
|
|
|
updateProvisioningUrl,
|
|
|
|
confirmNumber
|
|
|
|
);
|
2023-05-10 21:36:45 +00:00
|
|
|
const sleepMs = qrCodeBackOff.getAndIncrement();
|
|
|
|
log.info(`InstallScreen/getQRCode: race to ${sleepMs}ms`);
|
2023-05-15 15:08:15 +00:00
|
|
|
await pTimeout(qrCodeResolution.promise, sleepMs, sleepError);
|
|
|
|
await qrCodePromise;
|
2022-02-23 01:35:34 +00:00
|
|
|
|
2023-01-13 00:24:59 +00:00
|
|
|
window.IPC.removeSetupMenuItems();
|
2022-03-01 23:01:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'account.registerSecondDevice: got an error',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
2021-12-16 15:02:22 +00:00
|
|
|
if (hasCleanedUp) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-10 21:36:45 +00:00
|
|
|
|
|
|
|
if (qrCodeBackOff.isFull()) {
|
|
|
|
log.error('InstallScreen/getQRCode: too many tries');
|
|
|
|
setState({
|
|
|
|
step: InstallScreenStep.Error,
|
|
|
|
error: InstallError.QRCodeFailed,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error === sleepError) {
|
|
|
|
setState({
|
|
|
|
step: InstallScreenStep.QrCodeNotScanned,
|
|
|
|
provisioningUrl: { loadingState: LoadingState.LoadFailed, error },
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setState({
|
|
|
|
step: InstallScreenStep.Error,
|
|
|
|
error: getInstallError(error),
|
|
|
|
});
|
|
|
|
}
|
2021-12-16 15:02:22 +00:00
|
|
|
}
|
2023-05-10 21:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drop(getQRCode());
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
hasCleanedUp = true;
|
|
|
|
};
|
2023-05-10 21:36:45 +00:00
|
|
|
}, [setProvisioningUrl, retryCounter, onQrCodeScanned]);
|
2021-12-16 15:02:22 +00:00
|
|
|
|
|
|
|
let props: PropsType;
|
|
|
|
|
|
|
|
switch (state.step) {
|
|
|
|
case InstallScreenStep.Error:
|
|
|
|
props = {
|
|
|
|
step: InstallScreenStep.Error,
|
|
|
|
screenSpecificProps: {
|
|
|
|
i18n,
|
|
|
|
error: state.error,
|
2023-01-13 00:24:59 +00:00
|
|
|
quit: () => window.IPC.shutdown(),
|
2024-01-27 00:56:58 +00:00
|
|
|
tryAgain: () => {
|
|
|
|
setRetryCounter(count => count + 1);
|
|
|
|
setState(INITIAL_STATE);
|
|
|
|
},
|
2021-12-16 15:02:22 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case InstallScreenStep.QrCodeNotScanned:
|
|
|
|
props = {
|
|
|
|
step: InstallScreenStep.QrCodeNotScanned,
|
|
|
|
screenSpecificProps: {
|
|
|
|
i18n,
|
|
|
|
provisioningUrl: state.provisioningUrl,
|
2023-03-20 20:42:00 +00:00
|
|
|
hasExpired,
|
|
|
|
updates,
|
|
|
|
currentVersion: window.getVersion(),
|
|
|
|
startUpdate,
|
2023-05-10 21:36:45 +00:00
|
|
|
retryGetQrCode: () => {
|
|
|
|
setRetryCounter(count => count + 1);
|
|
|
|
setState(INITIAL_STATE);
|
|
|
|
},
|
2023-04-20 21:23:19 +00:00
|
|
|
OS: OS.getName(),
|
2021-12-16 15:02:22 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case InstallScreenStep.ChoosingDeviceName:
|
|
|
|
props = {
|
|
|
|
step: InstallScreenStep.ChoosingDeviceName,
|
|
|
|
screenSpecificProps: {
|
|
|
|
i18n,
|
|
|
|
deviceName: state.deviceName,
|
|
|
|
setDeviceName,
|
2024-03-15 14:20:33 +00:00
|
|
|
setBackupFile,
|
2021-12-16 15:02:22 +00:00
|
|
|
onSubmit: onSubmitDeviceName,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case InstallScreenStep.LinkInProgress:
|
|
|
|
props = {
|
|
|
|
step: InstallScreenStep.LinkInProgress,
|
|
|
|
screenSpecificProps: { i18n },
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw missingCaseError(state);
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:09:54 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<InstallScreen {...props} />
|
|
|
|
<SmartToastManager
|
|
|
|
disableMegaphone
|
|
|
|
containerWidthBreakpoint={WidthBreakpoint.Narrow}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
2024-03-13 20:44:13 +00:00
|
|
|
});
|