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";
|
|
|
|
import {StorageFactory} from "../../matrix/storage/idb/StorageFactory.js";
|
|
|
|
import {SessionInfoStorage} from "../../matrix/sessioninfo/localstorage/SessionInfoStorage.js";
|
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-02-12 18:05:39 +01:00
|
|
|
import {IDBLogger} from "../../logging/IDBLogger.js";
|
2021-03-01 15:01:43 +01:00
|
|
|
import {ConsoleLogger} from "../../logging/ConsoleLogger.js";
|
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";
|
|
|
|
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";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make path relative to basePath,
|
|
|
|
// assuming it and basePath are relative to document
|
|
|
|
function relPath(path, basePath) {
|
|
|
|
const idx = basePath.lastIndexOf("/");
|
|
|
|
const dir = idx === -1 ? "" : basePath.slice(0, idx);
|
|
|
|
const dirCount = dir.length ? dir.split("/").length : 0;
|
|
|
|
return "../".repeat(dirCount) + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadOlmWorker(paths) {
|
|
|
|
const workerPool = new WorkerPool(paths.worker, 4);
|
|
|
|
await workerPool.init();
|
|
|
|
const path = relPath(paths.olm.legacyBundle, paths.worker);
|
|
|
|
await workerPool.sendAll({type: "load_olm", path});
|
|
|
|
const olmWorker = new OlmWorker(workerPool);
|
|
|
|
return olmWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Platform {
|
2021-03-01 15:01:43 +01:00
|
|
|
constructor(container, paths, cryptoExtras = null, options = null) {
|
2020-10-26 15:44:11 +01:00
|
|
|
this._paths = paths;
|
|
|
|
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-03-01 15:01:43 +01:00
|
|
|
if (options?.development) {
|
|
|
|
this.logger = new ConsoleLogger({platform: this});
|
|
|
|
} else {
|
|
|
|
this.logger = new IDBLogger({name: "hydrogen_logs", platform: this});
|
|
|
|
}
|
2020-10-26 15:44:11 +01:00
|
|
|
this.history = new History();
|
|
|
|
this.onlineStatus = new OnlineStatus();
|
|
|
|
this._serviceWorkerHandler = null;
|
|
|
|
if (paths.serviceWorker && "serviceWorker" in navigator) {
|
|
|
|
this._serviceWorkerHandler = new ServiceWorkerHandler();
|
|
|
|
this._serviceWorkerHandler.registerAndStart(paths.serviceWorker);
|
|
|
|
}
|
|
|
|
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") {
|
|
|
|
this.request = createFetchRequest(this.clock.createTimeout);
|
|
|
|
} 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;
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get updateService() {
|
|
|
|
return this._serviceWorkerHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOlm() {
|
|
|
|
return loadOlm(this._paths.olm);
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadOlmWorker() {
|
|
|
|
if (!window.WebAssembly) {
|
|
|
|
return await loadOlmWorker(this._paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
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-04 21:51:29 +01:00
|
|
|
downloadInIframe(this._container, this._paths.downloadSandbox, blobHandle, filename);
|
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);
|
|
|
|
}
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-10-26 15:44:11 +01:00
|
|
|
}
|