From c6fefd73d846ae72eab8261a91ba1223d8148c34 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 30 Jul 2021 19:23:48 +0530 Subject: [PATCH] fix sheet conversion in Chrome by instantiating SharedArrayBuffers in a more portable way --- www/common/onlyoffice/inner.js | 25 ++++++++++++++++++++++++- www/common/onlyoffice/x2t/x2t.js | 15 ++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index 4a0a56be0..5c57fbecc 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -72,8 +72,31 @@ define([ return JSONSortify(obj); }; + /* Chrome 92 dropped support for SharedArrayBuffer in cross-origin contexts + where window.crossOriginIsolated is false. + + Their blog (https://blog.chromium.org/2021/02/restriction-on-sharedarraybuffers.html) + isn't clear about why they're doing this, but since it's related to site-isolation + it seems they're trying to do vague security things. + + In any case, there seems to be a workaround where you can still create them + by using `new WebAssembly.Memory({shared: true, ...})` instead of `new SharedArrayBuffer`. + + This seems unreliable, but it's better than not being able to export, since + we actively rely on postMessage between iframes and therefore can't afford + to opt for full isolation. + */ + var supportsSharedArrayBuffers = function () { + try { + return Object.prototype.toString.call(new WebAssembly.Memory({shared: true, initial: 0, maximum: 0}).buffer) === '[object SharedArrayBuffer]'; + } catch (err) { + console.error(err); + } + return false; + }; + var supportsXLSX = function () { - return !(typeof(Atomics) === "undefined" || typeof (SharedArrayBuffer) === "undefined" || typeof(WebAssembly) === 'undefined'); + return !(typeof(Atomics) === "undefined" || !supportsSharedArrayBuffers() /* || typeof (SharedArrayBuffer) === "undefined" */ || typeof(WebAssembly) === 'undefined'); }; diff --git a/www/common/onlyoffice/x2t/x2t.js b/www/common/onlyoffice/x2t/x2t.js index 181b2db8f..9c1289c56 100644 --- a/www/common/onlyoffice/x2t/x2t.js +++ b/www/common/onlyoffice/x2t/x2t.js @@ -1,3 +1,8 @@ +function SUPPORTS_SHARED_MEMORY() { + return typeof(SharedArrayBuffer) !== 'undefined'; +} + + // Support for growable heap + pthreads, where the buffer may change, so JS views // must be updated. function GROWABLE_HEAP_STORE_I8(ptr, value) { @@ -1030,7 +1035,7 @@ if (ENVIRONMENT_IS_PTHREAD) { "maximum": 1073741824 / WASM_PAGE_SIZE, "shared": true }); - if (!(wasmMemory.buffer instanceof SharedArrayBuffer)) { + if (Object.prototype.toString.call(wasmMemory.buffer) !== '[object SharedArrayBuffer]') { err("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"); if (ENVIRONMENT_HAS_NODE) { console.log("(on node you may need: --experimental-wasm-threads --experimental-wasm-bulk-memory and also use a recent version)"); @@ -2161,7 +2166,7 @@ var PThread = { }), receiveObjectTransfer: (function(data) {}), allocateUnusedWorkers: (function(numWorkers, onFinishedLoading) { - if (typeof SharedArrayBuffer === "undefined") return; + if (!SUPPORTS_SHARED_MEMORY()) return; var workers = []; var numWorkersToCreate = numWorkers; if (PThread.preallocatedWorkers.length > 0) { @@ -2276,7 +2281,7 @@ var PThread = { } }), createNewWorkers: (function(numWorkers) { - if (typeof SharedArrayBuffer === "undefined") return []; + if (!SUPPORTS_SHARED_MEMORY()) return []; var pthreadMainJs = "x2t.worker.js"; pthreadMainJs = locateFile(pthreadMainJs); var newWorkers = []; @@ -5683,7 +5688,7 @@ function _emscripten_get_sbrk_ptr() { } Module["_emscripten_get_sbrk_ptr"] = _emscripten_get_sbrk_ptr; function _emscripten_has_threading_support() { - return typeof SharedArrayBuffer !== "undefined"; + return SUPPORTS_SHARED_MEMORY(); } Module["_emscripten_has_threading_support"] = _emscripten_has_threading_support; function _emscripten_is_main_browser_thread() { @@ -6761,7 +6766,7 @@ function _pthread_self() { } Module["_pthread_self"] = _pthread_self; function _pthread_create(pthread_ptr, attr, start_routine, arg) { - if (typeof SharedArrayBuffer === "undefined") { + if (!SUPPORTS_SHARED_MEMORY()) { err("Current environment does not support SharedArrayBuffer, pthreads are not available!"); return 6; }