You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.web.ts 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // @ts-ignore
  2. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  3. // eslint-disable-next-line lines-around-comment
  4. // @ts-ignore
  5. import { safeJsonParse } from '@jitsi/js-utils/json';
  6. import { browser } from '../lib-jitsi-meet';
  7. import { inIframe } from '../util/iframeUtils';
  8. import { parseURLParams } from '../util/parseURLParams';
  9. import logger from './logger';
  10. /**
  11. * Handles changes of the fake local storage.
  12. *
  13. * @returns {void}
  14. */
  15. function onFakeLocalStorageChanged() {
  16. APP.API.notifyLocalStorageChanged(jitsiLocalStorage.serialize([ 'jitsiLocalStorage' ]));
  17. }
  18. /**
  19. * Checks if the local storage of the host page needs to be used instead jitsi-meet's local storage.
  20. *
  21. * @param {Object} urlParams - Object with parsed URL params.
  22. * @returns {boolean} - True if the local storage of the host page needs to be used instead jitsi-meet's local storage
  23. * and false otherwise.
  24. */
  25. function shouldUseHostPageLocalStorage(urlParams: { 'config.useHostPageLocalStorage'?: boolean; }) {
  26. // NOTE: normally the url params and the config will be merged into the redux store. But we want to setup the local
  27. // storage as soon as possible, the store is not created yet and the merging of the URL params and the config
  28. // haven't been executed yet. That's why we need to manually parse the URL params and also access the config through
  29. // the global variable.
  30. if (urlParams['config.useHostPageLocalStorage'] === true
  31. || (typeof config === 'object' && config.useHostPageLocalStorage)) {
  32. return true;
  33. }
  34. if (jitsiLocalStorage.isLocalStorageDisabled()) { // We have detected that ou own local storage is not working.
  35. return true;
  36. }
  37. if (browser.isWebKitBased() && inIframe()) {
  38. // WebKit browsers don't persist local storage for third-party iframes.
  39. return true;
  40. }
  41. return false;
  42. }
  43. /**
  44. * Performs initial setup of the jitsiLocalStorage.
  45. *
  46. * @returns {void}
  47. */
  48. function setupJitsiLocalStorage() {
  49. // @ts-ignore
  50. const urlParams = parseURLParams(window.location);
  51. if (shouldUseHostPageLocalStorage(urlParams)) {
  52. try {
  53. const localStorageContent = safeJsonParse(urlParams['appData.localStorageContent']);
  54. // We need to disable the local storage before setting the data in case the browser local storage doesn't
  55. // throw exception (in some cases when this happens the local storage may be cleared for every session.
  56. // Example: when loading meet from cross-domain with the IFrame API with Brave with the default
  57. // configuration). Otherwise we will set the data in the browser local storage and then switch to the dummy
  58. // local storage from jitsiLocalStorage and we will loose the data.
  59. jitsiLocalStorage.setLocalStorageDisabled(true);
  60. if (typeof localStorageContent === 'object') {
  61. Object.keys(localStorageContent).forEach(key => {
  62. jitsiLocalStorage.setItem(key, localStorageContent[key]);
  63. });
  64. }
  65. } catch (error) {
  66. logger.error('Can\'t parse localStorageContent.', error);
  67. }
  68. jitsiLocalStorage.on('changed', onFakeLocalStorageChanged);
  69. }
  70. }
  71. setupJitsiLocalStorage();