2016-11-02 16:10:21 +01:00
|
|
|
/*
|
2016-11-03 12:47:57 +01:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-12-18 18:40:30 +01:00
|
|
|
Copyright 2018 New Vector Ltd
|
2020-03-02 15:59:54 +01:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2016-11-02 16:10:21 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
import {MatrixClient} from "matrix-js-sdk/src/client";
|
2020-12-10 00:40:31 +01:00
|
|
|
import {encodeUnpaddedBase64} from "matrix-js-sdk/src/crypto/olmlib";
|
2020-05-14 04:41:41 +02:00
|
|
|
import dis from './dispatcher/dispatcher';
|
2019-11-19 12:52:12 +01:00
|
|
|
import BaseEventIndexManager from './indexing/BaseEventIndexManager';
|
2020-05-21 19:06:36 +02:00
|
|
|
import {ActionPayload} from "./dispatcher/payloads";
|
2020-05-29 19:24:45 +02:00
|
|
|
import {CheckUpdatesPayload} from "./dispatcher/payloads/CheckUpdatesPayload";
|
|
|
|
import {Action} from "./dispatcher/actions";
|
2020-05-29 20:59:47 +02:00
|
|
|
import {hideToast as hideUpdateToast} from "./toasts/UpdateToast";
|
2020-11-02 18:25:48 +01:00
|
|
|
import {MatrixClientPeg} from "./MatrixClientPeg";
|
2020-12-11 03:52:18 +01:00
|
|
|
import {idbLoad, idbSave, idbDelete} from "./utils/StorageManager";
|
2020-05-29 19:24:45 +02:00
|
|
|
|
2020-06-25 22:59:46 +02:00
|
|
|
export const SSO_HOMESERVER_URL_KEY = "mx_sso_hs_url";
|
|
|
|
export const SSO_ID_SERVER_URL_KEY = "mx_sso_is_url";
|
2021-02-01 17:25:50 +01:00
|
|
|
export const SSO_IDP_ID_KEY = "mx_sso_idp_id";
|
2020-06-02 17:26:07 +02:00
|
|
|
|
2020-05-29 19:24:45 +02:00
|
|
|
export enum UpdateCheckStatus {
|
|
|
|
Checking = "CHECKING",
|
|
|
|
Error = "ERROR",
|
|
|
|
NotAvailable = "NOTAVAILABLE",
|
|
|
|
Downloading = "DOWNLOADING",
|
|
|
|
Ready = "READY",
|
|
|
|
}
|
2017-06-20 19:47:35 +02:00
|
|
|
|
2020-05-29 20:59:47 +02:00
|
|
|
const UPDATE_DEFER_KEY = "mx_defer_update";
|
|
|
|
|
2016-11-02 16:10:21 +01:00
|
|
|
/**
|
|
|
|
* Base class for classes that provide platform-specific functionality
|
|
|
|
* eg. Setting an application badge or displaying notifications
|
|
|
|
*
|
|
|
|
* Instances of this class are provided by the application.
|
|
|
|
*/
|
2020-05-21 19:06:36 +02:00
|
|
|
export default abstract class BasePlatform {
|
2020-05-21 19:11:21 +02:00
|
|
|
protected notificationCount = 0;
|
|
|
|
protected errorDidOccur = false;
|
2017-06-20 19:47:35 +02:00
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
constructor() {
|
|
|
|
dis.register(this.onAction);
|
2020-06-02 17:26:07 +02:00
|
|
|
this.startUpdateCheck = this.startUpdateCheck.bind(this);
|
2017-06-20 19:47:35 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 14:40:46 +01:00
|
|
|
abstract getConfig(): Promise<{}>;
|
2020-07-03 00:14:31 +02:00
|
|
|
|
|
|
|
abstract getDefaultDeviceDisplayName(): string;
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
protected onAction = (payload: ActionPayload) => {
|
2017-06-20 19:47:35 +02:00
|
|
|
switch (payload.action) {
|
2019-07-04 00:46:37 +02:00
|
|
|
case 'on_client_not_viable':
|
2017-06-20 19:47:35 +02:00
|
|
|
case 'on_logged_out':
|
|
|
|
this.setNotificationCount(0);
|
|
|
|
break;
|
|
|
|
}
|
2020-05-21 19:06:36 +02:00
|
|
|
};
|
2016-11-02 16:10:21 +01:00
|
|
|
|
2017-05-29 20:50:04 +02:00
|
|
|
// Used primarily for Analytics
|
2020-05-21 19:06:36 +02:00
|
|
|
abstract getHumanReadableName(): string;
|
2017-05-29 20:50:04 +02:00
|
|
|
|
2016-11-02 16:10:21 +01:00
|
|
|
setNotificationCount(count: number) {
|
|
|
|
this.notificationCount = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
setErrorStatus(errorDidOccur: boolean) {
|
|
|
|
this.errorDidOccur = errorDidOccur;
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:24:45 +02:00
|
|
|
/**
|
|
|
|
* Whether we can call checkForUpdate on this platform build
|
|
|
|
*/
|
|
|
|
async canSelfUpdate(): Promise<boolean> {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
startUpdateCheck() {
|
2020-05-29 20:59:47 +02:00
|
|
|
hideUpdateToast();
|
|
|
|
localStorage.removeItem(UPDATE_DEFER_KEY);
|
2020-05-29 19:24:45 +02:00
|
|
|
dis.dispatch<CheckUpdatesPayload>({
|
|
|
|
action: Action.CheckUpdates,
|
|
|
|
status: UpdateCheckStatus.Checking,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-29 20:59:47 +02:00
|
|
|
/**
|
|
|
|
* Update the currently running app to the latest available version
|
|
|
|
* and replace this instance of the app with the new version.
|
|
|
|
*/
|
|
|
|
installUpdate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the version update has been deferred and that deferment is still in effect
|
|
|
|
* @param newVersion the version string to check
|
|
|
|
*/
|
|
|
|
protected shouldShowUpdate(newVersion: string): boolean {
|
2020-11-02 18:25:48 +01:00
|
|
|
// If the user registered on this client in the last 24 hours then do not show them the update toast
|
|
|
|
if (MatrixClientPeg.userRegisteredWithinLastHours(24)) return false;
|
|
|
|
|
2020-05-29 20:59:47 +02:00
|
|
|
try {
|
|
|
|
const [version, deferUntil] = JSON.parse(localStorage.getItem(UPDATE_DEFER_KEY));
|
|
|
|
return newVersion !== version || Date.now() > deferUntil;
|
|
|
|
} catch (e) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignore the pending update and don't prompt about this version
|
|
|
|
* until the next morning (8am).
|
|
|
|
*/
|
|
|
|
deferUpdate(newVersion: string) {
|
|
|
|
const date = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
|
|
date.setHours(8, 0, 0, 0); // set to next 8am
|
|
|
|
localStorage.setItem(UPDATE_DEFER_KEY, JSON.stringify([newVersion, date.getTime()]));
|
|
|
|
hideUpdateToast();
|
|
|
|
}
|
|
|
|
|
2020-12-02 20:14:58 +01:00
|
|
|
/**
|
|
|
|
* Return true if platform supports multi-language
|
|
|
|
* spell-checking, otherwise false.
|
|
|
|
*/
|
|
|
|
supportsMultiLanguageSpellCheck(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:35:31 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the platform supports displaying
|
|
|
|
* notifications, otherwise false.
|
2017-07-01 15:15:26 +02:00
|
|
|
* @returns {boolean} whether the platform supports displaying notifications
|
2016-11-02 18:35:31 +01:00
|
|
|
*/
|
2017-01-20 15:22:27 +01:00
|
|
|
supportsNotifications(): boolean {
|
2016-11-02 18:35:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the application currently has permission
|
|
|
|
* to display notifications. Otherwise false.
|
2017-07-01 15:15:26 +02:00
|
|
|
* @returns {boolean} whether the application has permission to display notifications
|
2016-11-02 18:35:31 +01:00
|
|
|
*/
|
2017-01-20 15:22:27 +01:00
|
|
|
maySendNotifications(): boolean {
|
2016-11-02 18:35:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests permission to send notifications. Returns
|
|
|
|
* a promise that is resolved when the user has responded
|
|
|
|
* to the request. The promise has a single string argument
|
|
|
|
* that is 'granted' if the user allowed the request or
|
|
|
|
* 'denied' otherwise.
|
|
|
|
*/
|
2020-05-21 19:06:36 +02:00
|
|
|
abstract requestNotificationPermission(): Promise<string>;
|
2016-11-02 18:35:31 +01:00
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
abstract displayNotification(title: string, msg: string, avatarUrl: string, room: Object);
|
2016-11-08 11:45:19 +01:00
|
|
|
|
2017-06-01 00:49:55 +02:00
|
|
|
loudNotification(ev: Event, room: Object) {
|
2020-06-18 15:32:43 +02:00
|
|
|
}
|
2020-08-12 13:16:28 +02:00
|
|
|
|
2020-08-05 12:07:10 +02:00
|
|
|
clearNotification(notif: Notification) {
|
2020-08-12 13:16:28 +02:00
|
|
|
// Some browsers don't support this, e.g Safari on iOS
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Notification/close
|
|
|
|
if (notif.close) {
|
|
|
|
notif.close();
|
|
|
|
}
|
2020-08-05 12:07:10 +02:00
|
|
|
}
|
2017-06-01 00:49:55 +02:00
|
|
|
|
2016-11-08 11:45:19 +01:00
|
|
|
/**
|
2020-05-21 19:06:36 +02:00
|
|
|
* Returns a promise that resolves to a string representing the current version of the application.
|
2016-11-08 11:45:19 +01:00
|
|
|
*/
|
2020-05-21 19:06:36 +02:00
|
|
|
abstract getAppVersion(): Promise<string>;
|
2017-01-10 19:37:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's not expected that capturing the screen will work
|
|
|
|
* with getUserMedia, return a string explaining why not.
|
|
|
|
* Otherwise, return null.
|
|
|
|
*/
|
2017-05-24 16:40:50 +02:00
|
|
|
screenCaptureErrorString(): string {
|
2017-01-10 19:37:57 +01:00
|
|
|
return "Not implemented";
|
|
|
|
}
|
2017-04-10 18:39:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restarts the application, without neccessarily reloading
|
|
|
|
* any application code
|
|
|
|
*/
|
2020-05-21 19:06:36 +02:00
|
|
|
abstract reload();
|
2019-02-24 02:06:53 +01:00
|
|
|
|
2019-02-24 02:20:49 +01:00
|
|
|
supportsAutoLaunch(): boolean {
|
2019-02-24 02:06:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Surely this should be a setting like any other?
|
2020-05-21 19:06:36 +02:00
|
|
|
async getAutoLaunchEnabled(): Promise<boolean> {
|
2019-02-24 02:06:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
async setAutoLaunchEnabled(enabled: boolean): Promise<void> {
|
2019-02-24 02:06:53 +01:00
|
|
|
throw new Error("Unimplemented");
|
|
|
|
}
|
|
|
|
|
2019-08-05 12:58:53 +02:00
|
|
|
supportsAutoHideMenuBar(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
async getAutoHideMenuBarEnabled(): Promise<boolean> {
|
2019-08-05 12:58:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
async setAutoHideMenuBarEnabled(enabled: boolean): Promise<void> {
|
2019-08-05 12:58:53 +02:00
|
|
|
throw new Error("Unimplemented");
|
|
|
|
}
|
|
|
|
|
2019-02-24 02:20:49 +01:00
|
|
|
supportsMinimizeToTray(): boolean {
|
2019-02-24 02:06:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
async getMinimizeToTrayEnabled(): Promise<boolean> {
|
2019-02-24 02:06:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-21 19:06:36 +02:00
|
|
|
async setMinimizeToTrayEnabled(enabled: boolean): Promise<void> {
|
2019-02-24 02:06:53 +01:00
|
|
|
throw new Error("Unimplemented");
|
|
|
|
}
|
2019-10-11 16:07:59 +02:00
|
|
|
|
2019-11-13 16:21:26 +01:00
|
|
|
/**
|
|
|
|
* Get our platform specific EventIndexManager.
|
|
|
|
*
|
|
|
|
* @return {BaseEventIndexManager} The EventIndex manager for our platform,
|
|
|
|
* can be null if the platform doesn't support event indexing.
|
|
|
|
*/
|
2019-11-13 12:25:16 +01:00
|
|
|
getEventIndexingManager(): BaseEventIndexManager | null {
|
|
|
|
return null;
|
2019-10-11 16:07:59 +02:00
|
|
|
}
|
2020-02-24 18:11:08 +01:00
|
|
|
|
2020-02-25 11:57:40 +01:00
|
|
|
setLanguage(preferredLangs: string[]) {}
|
2020-03-02 15:59:54 +01:00
|
|
|
|
2020-12-01 20:17:24 +01:00
|
|
|
setSpellCheckLanguages(preferredLangs: string[]) {}
|
|
|
|
|
2021-02-18 20:12:48 +01:00
|
|
|
getSpellCheckLanguages(): Promise<string[]> | null {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-29 20:46:47 +01:00
|
|
|
getAvailableSpellCheckLanguages(): Promise<string[]> | null {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-25 22:59:46 +02:00
|
|
|
protected getSSOCallbackUrl(fragmentAfterLogin: string): URL {
|
2020-03-02 15:59:54 +01:00
|
|
|
const url = new URL(window.location.href);
|
2020-05-13 07:24:04 +02:00
|
|
|
url.hash = fragmentAfterLogin || "";
|
2020-03-02 15:59:54 +01:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin Single Sign On flows.
|
2020-03-02 16:05:56 +01:00
|
|
|
* @param {MatrixClient} mxClient the matrix client using which we should start the flow
|
|
|
|
* @param {"sso"|"cas"} loginType the type of SSO it is, CAS/SSO.
|
2020-05-13 07:24:04 +02:00
|
|
|
* @param {string} fragmentAfterLogin the hash to pass to the app during sso callback.
|
2020-11-19 12:07:44 +01:00
|
|
|
* @param {string} idpId The ID of the Identity Provider being targeted, optional.
|
2020-03-02 15:59:54 +01:00
|
|
|
*/
|
2020-11-19 12:07:44 +01:00
|
|
|
startSingleSignOn(mxClient: MatrixClient, loginType: "sso" | "cas", fragmentAfterLogin: string, idpId?: string) {
|
2020-06-25 22:59:46 +02:00
|
|
|
// persist hs url and is url for when the user is returned to the app with the login token
|
|
|
|
localStorage.setItem(SSO_HOMESERVER_URL_KEY, mxClient.getHomeserverUrl());
|
|
|
|
if (mxClient.getIdentityServerUrl()) {
|
|
|
|
localStorage.setItem(SSO_ID_SERVER_URL_KEY, mxClient.getIdentityServerUrl());
|
|
|
|
}
|
2021-02-01 17:25:50 +01:00
|
|
|
if (idpId) {
|
|
|
|
localStorage.setItem(SSO_IDP_ID_KEY, idpId);
|
|
|
|
}
|
2020-06-02 17:26:07 +02:00
|
|
|
const callbackUrl = this.getSSOCallbackUrl(fragmentAfterLogin);
|
2020-11-19 12:07:44 +01:00
|
|
|
window.location.href = mxClient.getSsoLoginUrl(callbackUrl.toString(), loginType, idpId); // redirect to SSO
|
2020-03-02 15:59:54 +01:00
|
|
|
}
|
2020-04-11 19:57:59 +02:00
|
|
|
|
|
|
|
onKeyDown(ev: KeyboardEvent): boolean {
|
|
|
|
return false; // no shortcuts implemented
|
|
|
|
}
|
2020-05-28 06:05:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a previously stored pickle key. The pickle key is used for
|
|
|
|
* encrypting libolm objects.
|
|
|
|
* @param {string} userId the user ID for the user that the pickle key is for.
|
|
|
|
* @param {string} userId the device ID that the pickle key is for.
|
|
|
|
* @returns {string|null} the previously stored pickle key, or null if no
|
|
|
|
* pickle key has been stored.
|
|
|
|
*/
|
2020-05-28 17:56:48 +02:00
|
|
|
async getPickleKey(userId: string, deviceId: string): Promise<string | null> {
|
2020-12-10 00:40:31 +01:00
|
|
|
if (!window.crypto || !window.crypto.subtle) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await idbLoad("pickleKey", [userId, deviceId]);
|
|
|
|
} catch (e) {}
|
|
|
|
if (!data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!data.encrypted || !data.iv || !data.cryptoKey) {
|
|
|
|
console.error("Badly formatted pickle key");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const additionalData = new Uint8Array(userId.length + deviceId.length + 1);
|
|
|
|
for (let i = 0; i < userId.length; i++) {
|
|
|
|
additionalData[i] = userId.charCodeAt(i);
|
|
|
|
}
|
|
|
|
additionalData[userId.length] = 124; // "|"
|
|
|
|
for (let i = 0; i < deviceId.length; i++) {
|
|
|
|
additionalData[userId.length + 1 + i] = deviceId.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const key = await crypto.subtle.decrypt(
|
|
|
|
{name: "AES-GCM", iv: data.iv, additionalData}, data.cryptoKey,
|
|
|
|
data.encrypted,
|
|
|
|
);
|
|
|
|
return encodeUnpaddedBase64(key);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error decrypting pickle key");
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-28 06:05:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and store a pickle key for encrypting libolm objects.
|
|
|
|
* @param {string} userId the user ID for the user that the pickle key is for.
|
|
|
|
* @param {string} userId the device ID that the pickle key is for.
|
|
|
|
* @returns {string|null} the pickle key, or null if the platform does not
|
|
|
|
* support storing pickle keys.
|
|
|
|
*/
|
2020-05-28 17:56:48 +02:00
|
|
|
async createPickleKey(userId: string, deviceId: string): Promise<string | null> {
|
2020-12-10 00:40:31 +01:00
|
|
|
if (!window.crypto || !window.crypto.subtle) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const crypto = window.crypto;
|
|
|
|
const randomArray = new Uint8Array(32);
|
|
|
|
crypto.getRandomValues(randomArray);
|
|
|
|
const cryptoKey = await crypto.subtle.generateKey(
|
|
|
|
{name: "AES-GCM", length: 256}, false, ["encrypt", "decrypt"],
|
|
|
|
);
|
|
|
|
const iv = new Uint8Array(32);
|
|
|
|
crypto.getRandomValues(iv);
|
|
|
|
|
|
|
|
const additionalData = new Uint8Array(userId.length + deviceId.length + 1);
|
|
|
|
for (let i = 0; i < userId.length; i++) {
|
|
|
|
additionalData[i] = userId.charCodeAt(i);
|
|
|
|
}
|
|
|
|
additionalData[userId.length] = 124; // "|"
|
|
|
|
for (let i = 0; i < deviceId.length; i++) {
|
|
|
|
additionalData[userId.length + 1 + i] = deviceId.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
const encrypted = await crypto.subtle.encrypt(
|
|
|
|
{name: "AES-GCM", iv, additionalData}, cryptoKey, randomArray,
|
|
|
|
);
|
|
|
|
|
2020-12-11 03:52:18 +01:00
|
|
|
try {
|
|
|
|
await idbSave("pickleKey", [userId, deviceId], {encrypted, iv, cryptoKey});
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-10 00:40:31 +01:00
|
|
|
return encodeUnpaddedBase64(randomArray);
|
2020-05-28 06:05:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a previously stored pickle key from storage.
|
|
|
|
* @param {string} userId the user ID for the user that the pickle key is for.
|
|
|
|
* @param {string} userId the device ID that the pickle key is for.
|
|
|
|
*/
|
2020-05-28 17:56:48 +02:00
|
|
|
async destroyPickleKey(userId: string, deviceId: string): Promise<void> {
|
2020-12-10 00:40:31 +01:00
|
|
|
try {
|
|
|
|
await idbDelete("pickleKey", [userId, deviceId]);
|
|
|
|
} catch (e) {}
|
2020-05-28 06:05:45 +02:00
|
|
|
}
|
2016-11-02 16:10:21 +01:00
|
|
|
}
|