2020-10-26 15:44:11 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {createFetchRequest} from "./dom/request/fetch.js";
|
|
|
|
import {xhrRequest} from "./dom/request/xhr.js";
|
2021-08-13 19:13:40 +02:00
|
|
|
import {StorageFactory} from "../../matrix/storage/idb/StorageFactory";
|
2021-11-25 10:48:03 +01:00
|
|
|
import {SessionInfoStorage} from "../../matrix/sessioninfo/localstorage/SessionInfoStorage";
|
2020-11-20 15:51:16 +01:00
|
|
|
import {SettingsStorage} from "./dom/SettingsStorage.js";
|
2021-02-12 16:01:54 +01:00
|
|
|
import {Encoding} from "./utils/Encoding.js";
|
2020-10-26 15:44:11 +01:00
|
|
|
import {OlmWorker} from "../../matrix/e2ee/OlmWorker.js";
|
2021-11-12 10:36:21 +01:00
|
|
|
import {IDBLogger} from "../../logging/IDBLogger";
|
2021-11-14 15:12:18 +01:00
|
|
|
import {ConsoleLogger} from "../../logging/ConsoleLogger";
|
2020-10-26 15:44:11 +01:00
|
|
|
import {RootView} from "./ui/RootView.js";
|
|
|
|
import {Clock} from "./dom/Clock.js";
|
|
|
|
import {ServiceWorkerHandler} from "./dom/ServiceWorkerHandler.js";
|
2021-03-18 20:43:40 +01:00
|
|
|
import {NotificationService} from "./dom/NotificationService.js";
|
2020-10-26 15:44:11 +01:00
|
|
|
import {History} from "./dom/History.js";
|
|
|
|
import {OnlineStatus} from "./dom/OnlineStatus.js";
|
|
|
|
import {Crypto} from "./dom/Crypto.js";
|
|
|
|
import {estimateStorageUsage} from "./dom/StorageEstimate.js";
|
|
|
|
import {WorkerPool} from "./dom/WorkerPool.js";
|
2020-11-10 22:36:26 +01:00
|
|
|
import {BlobHandle} from "./dom/BlobHandle.js";
|
2021-03-09 19:35:25 +01:00
|
|
|
import {hasReadPixelPermission, ImageHandle, VideoHandle} from "./dom/ImageHandle.js";
|
2020-11-10 17:23:23 +01:00
|
|
|
import {downloadInIframe} from "./dom/download.js";
|
2021-11-16 09:43:35 +01:00
|
|
|
import {Disposables} from "../../utils/Disposables";
|
2021-07-12 23:46:42 +02:00
|
|
|
import {parseHTML} from "./parsehtml.js";
|
2021-06-30 20:00:44 +02:00
|
|
|
import {handleAvatarError} from "./ui/avatar.js";
|
2020-10-26 15:44:11 +01:00
|
|
|
|
|
|
|
function addScript(src) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var s = document.createElement("script");
|
|
|
|
s.setAttribute("src", src );
|
|
|
|
s.onload=resolve;
|
|
|
|
s.onerror=reject;
|
|
|
|
document.body.appendChild(s);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadOlm(olmPaths) {
|
|
|
|
// make crypto.getRandomValues available without
|
|
|
|
// a prefix on IE11, needed by olm to work
|
|
|
|
if (window.msCrypto && !window.crypto) {
|
|
|
|
window.crypto = window.msCrypto;
|
|
|
|
}
|
|
|
|
if (olmPaths) {
|
|
|
|
if (window.WebAssembly) {
|
|
|
|
await addScript(olmPaths.wasmBundle);
|
|
|
|
await window.Olm.init({locateFile: () => olmPaths.wasm});
|
|
|
|
} else {
|
|
|
|
await addScript(olmPaths.legacyBundle);
|
|
|
|
await window.Olm.init();
|
|
|
|
}
|
|
|
|
return window.Olm;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-06 15:38:01 +01:00
|
|
|
// turn asset path to absolute path if it isn't already
|
|
|
|
// so it can be loaded independent of base
|
|
|
|
function assetAbsPath(assetPath) {
|
|
|
|
if (!assetPath.startsWith("/")) {
|
|
|
|
return new URL(assetPath, document.location.href).pathname;
|
|
|
|
}
|
|
|
|
return assetPath;
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 20:43:40 +01:00
|
|
|
async function loadOlmWorker(config) {
|
|
|
|
const workerPool = new WorkerPool(config.worker, 4);
|
2020-10-26 15:44:11 +01:00
|
|
|
await workerPool.init();
|
2021-12-06 13:49:14 +01:00
|
|
|
await workerPool.sendAll({
|
|
|
|
type: "load_olm",
|
2021-12-06 15:38:01 +01:00
|
|
|
path: assetAbsPath(config.olm.legacyBundle)
|
2021-12-06 13:49:14 +01:00
|
|
|
});
|
2020-10-26 15:44:11 +01:00
|
|
|
const olmWorker = new OlmWorker(workerPool);
|
|
|
|
return olmWorker;
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:34:34 +01:00
|
|
|
// needed for mobile Safari which shifts the layout viewport up without resizing it
|
|
|
|
// when the keyboard shows (see https://bugs.webkit.org/show_bug.cgi?id=141832)
|
|
|
|
function adaptUIOnVisualViewportResize(container) {
|
|
|
|
if (!window.visualViewport) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const handler = () => {
|
|
|
|
const sessionView = container.querySelector('.SessionView');
|
|
|
|
if (!sessionView) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scrollable = container.querySelector('.bottom-aligned-scroll');
|
|
|
|
let scrollTopBefore, heightBefore, heightAfter;
|
|
|
|
|
|
|
|
if (scrollable) {
|
|
|
|
scrollTopBefore = scrollable.scrollTop;
|
|
|
|
heightBefore = scrollable.offsetHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally we'd use window.visualViewport.offsetTop but that seems to occasionally lag
|
|
|
|
// behind (last tested on iOS 14.4 simulator) so we have to compute the offset manually
|
|
|
|
const offsetTop = sessionView.offsetTop + sessionView.offsetHeight - window.visualViewport.height;
|
|
|
|
|
|
|
|
container.style.setProperty('--ios-viewport-height', window.visualViewport.height.toString() + 'px');
|
|
|
|
container.style.setProperty('--ios-viewport-top', offsetTop.toString() + 'px');
|
|
|
|
|
|
|
|
if (scrollable) {
|
|
|
|
heightAfter = scrollable.offsetHeight;
|
|
|
|
scrollable.scrollTop = scrollTopBefore + heightBefore - heightAfter;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.visualViewport.addEventListener('resize', handler);
|
|
|
|
return () => {
|
|
|
|
window.visualViewport.removeEventListener('resize', handler);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:44:11 +01:00
|
|
|
export class Platform {
|
2021-03-18 20:43:40 +01:00
|
|
|
constructor(container, config, cryptoExtras = null, options = null) {
|
|
|
|
this._config = config;
|
2020-10-26 15:44:11 +01:00
|
|
|
this._container = container;
|
2021-02-12 18:05:39 +01:00
|
|
|
this.settingsStorage = new SettingsStorage("hydrogen_setting_v1_");
|
2020-10-26 15:44:11 +01:00
|
|
|
this.clock = new Clock();
|
2021-02-12 18:05:39 +01:00
|
|
|
this.encoding = new Encoding();
|
|
|
|
this.random = Math.random;
|
2021-11-30 08:58:28 +01:00
|
|
|
this._createLogger(options?.development);
|
2020-10-26 15:44:11 +01:00
|
|
|
this.history = new History();
|
|
|
|
this.onlineStatus = new OnlineStatus();
|
|
|
|
this._serviceWorkerHandler = null;
|
2021-03-18 20:43:40 +01:00
|
|
|
if (config.serviceWorker && "serviceWorker" in navigator) {
|
2020-10-26 15:44:11 +01:00
|
|
|
this._serviceWorkerHandler = new ServiceWorkerHandler();
|
2021-03-18 20:43:40 +01:00
|
|
|
this._serviceWorkerHandler.registerAndStart(config.serviceWorker);
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
2021-03-18 20:43:40 +01:00
|
|
|
this.notificationService = new NotificationService(this._serviceWorkerHandler, config.push);
|
2020-10-26 15:44:11 +01:00
|
|
|
this.crypto = new Crypto(cryptoExtras);
|
|
|
|
this.storageFactory = new StorageFactory(this._serviceWorkerHandler);
|
|
|
|
this.sessionInfoStorage = new SessionInfoStorage("hydrogen_sessions_v1");
|
|
|
|
this.estimateStorageUsage = estimateStorageUsage;
|
|
|
|
if (typeof fetch === "function") {
|
2021-03-18 19:34:41 +01:00
|
|
|
this.request = createFetchRequest(this.clock.createTimeout, this._serviceWorkerHandler);
|
2020-10-26 15:44:11 +01:00
|
|
|
} else {
|
|
|
|
this.request = xhrRequest;
|
|
|
|
}
|
2020-11-02 11:16:19 +01:00
|
|
|
const isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
|
2020-11-10 17:51:39 +01:00
|
|
|
this.isIE11 = isIE11;
|
2021-03-16 19:34:34 +01:00
|
|
|
// From https://stackoverflow.com/questions/9038625/detect-if-device-is-ios/9039885
|
|
|
|
const isIOS = /iPad|iPhone|iPod/.test(navigator.platform) || (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) && !window.MSStream;
|
|
|
|
this.isIOS = isIOS;
|
|
|
|
this._disposables = new Disposables();
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:58:28 +01:00
|
|
|
_createLogger(isDevelopment) {
|
|
|
|
// Make sure that loginToken does not end up in the logs
|
|
|
|
const transformer = (item) => {
|
|
|
|
if (item.e?.stack) {
|
2021-12-08 18:05:57 +01:00
|
|
|
item.e.stack = item.e.stack.replace(/\/\?loginToken=(.+)/, "?loginToken=<snip>");
|
2021-11-30 08:58:28 +01:00
|
|
|
}
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
if (isDevelopment) {
|
|
|
|
this.logger = new ConsoleLogger({platform: this});
|
|
|
|
} else {
|
|
|
|
this.logger = new IDBLogger({name: "hydrogen_logs", platform: this, serializedTransformer: transformer});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:44:11 +01:00
|
|
|
get updateService() {
|
|
|
|
return this._serviceWorkerHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOlm() {
|
2021-03-18 20:43:40 +01:00
|
|
|
return loadOlm(this._config.olm);
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:43:03 +02:00
|
|
|
get config() {
|
|
|
|
return this._config;
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:44:11 +01:00
|
|
|
async loadOlmWorker() {
|
|
|
|
if (!window.WebAssembly) {
|
2021-03-18 20:43:40 +01:00
|
|
|
return await loadOlmWorker(this._config);
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createAndMountRootView(vm) {
|
2020-11-02 11:16:19 +01:00
|
|
|
if (this.isIE11) {
|
2020-10-26 15:44:11 +01:00
|
|
|
this._container.className += " legacy";
|
|
|
|
}
|
2021-03-16 19:34:34 +01:00
|
|
|
if (this.isIOS) {
|
|
|
|
this._container.className += " ios";
|
|
|
|
const disposable = adaptUIOnVisualViewportResize(this._container);
|
|
|
|
if (disposable) {
|
|
|
|
this._disposables.track(disposable);
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 20:00:44 +02:00
|
|
|
this._container.addEventListener("error", handleAvatarError, true);
|
2021-07-01 12:12:07 +02:00
|
|
|
this._disposables.track(() => this._container.removeEventListener("error", handleAvatarError, true));
|
2020-10-26 15:44:11 +01:00
|
|
|
window.__hydrogenViewModel = vm;
|
|
|
|
const view = new RootView(vm);
|
|
|
|
this._container.appendChild(view.mount());
|
|
|
|
}
|
|
|
|
|
|
|
|
setNavigation(navigation) {
|
|
|
|
this._serviceWorkerHandler?.setNavigation(navigation);
|
|
|
|
}
|
2020-10-26 17:18:17 +01:00
|
|
|
|
2020-11-10 22:36:26 +01:00
|
|
|
createBlob(buffer, mimetype) {
|
|
|
|
return BlobHandle.fromBuffer(buffer, mimetype);
|
2020-10-26 17:18:17 +01:00
|
|
|
}
|
2020-11-10 17:23:23 +01:00
|
|
|
|
2020-11-10 22:36:26 +01:00
|
|
|
saveFileAs(blobHandle, filename) {
|
2020-11-10 17:23:23 +01:00
|
|
|
if (navigator.msSaveBlob) {
|
2020-11-11 10:44:44 +01:00
|
|
|
navigator.msSaveBlob(blobHandle.nativeBlob, filename);
|
2020-11-10 17:23:23 +01:00
|
|
|
} else {
|
2021-03-16 19:34:34 +01:00
|
|
|
downloadInIframe(this._container, this._config.downloadSandbox, blobHandle, filename, this.isIOS);
|
2020-11-10 17:23:23 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 22:36:26 +01:00
|
|
|
|
|
|
|
openFile(mimeType = null) {
|
|
|
|
const input = document.createElement("input");
|
|
|
|
input.setAttribute("type", "file");
|
2020-11-11 12:47:26 +01:00
|
|
|
input.className = "hidden";
|
2020-11-10 22:36:26 +01:00
|
|
|
if (mimeType) {
|
|
|
|
input.setAttribute("accept", mimeType);
|
|
|
|
}
|
2021-08-24 15:31:18 +02:00
|
|
|
const promise = new Promise(resolve => {
|
2020-11-10 22:36:26 +01:00
|
|
|
const checkFile = () => {
|
|
|
|
input.removeEventListener("change", checkFile, true);
|
|
|
|
const file = input.files[0];
|
2020-11-11 12:47:26 +01:00
|
|
|
this._container.removeChild(input);
|
2020-11-10 22:36:26 +01:00
|
|
|
if (file) {
|
2020-11-12 13:50:06 +01:00
|
|
|
resolve({name: file.name, blob: BlobHandle.fromBlob(file)});
|
2020-11-10 22:36:26 +01:00
|
|
|
} else {
|
2020-11-18 20:07:31 +01:00
|
|
|
resolve();
|
2020-11-10 22:36:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
input.addEventListener("change", checkFile, true);
|
|
|
|
});
|
2020-11-11 12:47:26 +01:00
|
|
|
// IE11 needs the input to be attached to the document
|
|
|
|
this._container.appendChild(input);
|
2020-11-10 22:36:26 +01:00
|
|
|
input.click();
|
|
|
|
return promise;
|
|
|
|
}
|
2020-11-12 13:50:06 +01:00
|
|
|
|
2021-07-30 19:33:18 +02:00
|
|
|
openUrl(url) {
|
|
|
|
location.href = url;
|
|
|
|
}
|
|
|
|
|
2021-07-03 00:05:50 +02:00
|
|
|
parseHTML(html) {
|
2021-07-12 23:46:42 +02:00
|
|
|
return parseHTML(html);
|
2021-07-03 00:05:50 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 13:50:06 +01:00
|
|
|
async loadImage(blob) {
|
|
|
|
return ImageHandle.fromBlob(blob);
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:35:25 +01:00
|
|
|
async loadVideo(blob) {
|
|
|
|
return VideoHandle.fromBlob(blob);
|
|
|
|
}
|
|
|
|
|
2020-11-12 13:50:06 +01:00
|
|
|
hasReadPixelPermission() {
|
|
|
|
return hasReadPixelPermission();
|
|
|
|
}
|
2020-11-12 13:50:32 +01:00
|
|
|
|
|
|
|
get devicePixelRatio() {
|
|
|
|
return window.devicePixelRatio || 1;
|
|
|
|
}
|
2021-03-15 16:55:14 +01:00
|
|
|
|
|
|
|
get version() {
|
2021-12-09 16:37:31 +01:00
|
|
|
return DEFINE_VERSION;
|
2021-03-15 16:55:14 +01:00
|
|
|
}
|
2021-03-16 19:34:34 +01:00
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this._disposables.dispose();
|
|
|
|
}
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
2021-11-30 08:58:28 +01:00
|
|
|
|
|
|
|
import {LogItem} from "../../logging/LogItem";
|
|
|
|
export function tests() {
|
|
|
|
return {
|
|
|
|
"loginToken should not be in logs": (assert) => {
|
|
|
|
const transformer = (item) => {
|
|
|
|
if (item.e?.stack) {
|
|
|
|
item.e.stack = item.e.stack.replace(/(?<=\/\?loginToken=).+/, "<snip>");
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
};
|
|
|
|
const logger = {
|
|
|
|
_queuedItems: [],
|
|
|
|
_serializedTransformer: transformer,
|
|
|
|
_now: () => {}
|
|
|
|
};
|
|
|
|
logger.persist = IDBLogger.prototype._persistItem.bind(logger);
|
|
|
|
const logItem = new LogItem("test", 1, logger);
|
|
|
|
logItem.error = new Error();
|
|
|
|
logItem.error.stack = "main http://localhost:3000/src/main.js:55\n<anonymous> http://localhost:3000/?loginToken=secret:26"
|
|
|
|
logger.persist(logItem, null, false);
|
|
|
|
const item = logger._queuedItems.pop();
|
|
|
|
console.log(item);
|
|
|
|
assert.strictEqual(item.json.search("secret"), -1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|