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.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @flow
  2. import Bourne from '@hapi/bourne';
  3. import { jitsiLocalStorage } from '@jitsi/js-utils/jitsi-local-storage';
  4. import { browser } from '../lib-jitsi-meet';
  5. import { parseURLParams } from '../util/parseURLParams';
  6. import logger from './logger';
  7. declare var APP: Object;
  8. declare var config: Object;
  9. /**
  10. * Checks whether we are loaded in an iframe.
  11. *
  12. * @returns {boolean} Returns {@code true} if loaded in iframe.
  13. * @private
  14. */
  15. function _inIframe() {
  16. try {
  17. return window.self !== window.top;
  18. } catch (e) {
  19. return true;
  20. }
  21. }
  22. /**
  23. * Handles changes of the fake local storage.
  24. *
  25. * @returns {void}
  26. */
  27. function onFakeLocalStorageChanged() {
  28. APP.API.notifyLocalStorageChanged(jitsiLocalStorage.serialize());
  29. }
  30. /**
  31. * Checks if the local storage of the host page needs to be used instead jitsi-meet's local storage.
  32. *
  33. * @param {Object} urlParams - Object with parsed URL params.
  34. * @returns {boolean} - True if the local storage of the host page needs to be used instead jitsi-meet's local storage
  35. * and false otherwise.
  36. */
  37. function shouldUseHostPageLocalStorage(urlParams) {
  38. // NOTE: normally the url params and the config will be merged into the redux store. But we want to setup the local
  39. // storage as soon as possible, the store is not created yet and the merging of the URL params and the config
  40. // haven't been executed yet. That's why we need to manually parse the URL params and also access the config through
  41. // the global variable.
  42. if (urlParams['config.useHostPageLocalStorage'] === true
  43. || (typeof config === 'object' && config.useHostPageLocalStorage)) {
  44. return true;
  45. }
  46. if (jitsiLocalStorage.isLocalStorageDisabled()) { // We have detected that ou own local storage is not working.
  47. return true;
  48. }
  49. if (browser.isWebKitBased() && _inIframe()) {
  50. // WebKit browsers don't persist local storage for third-party iframes.
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56. * Performs initial setup of the jitsiLocalStorage.
  57. *
  58. * @returns {void}
  59. */
  60. function setupJitsiLocalStorage() {
  61. const urlParams = parseURLParams(window.location);
  62. if (shouldUseHostPageLocalStorage(urlParams)) {
  63. try {
  64. const localStorageContent = Bourne.parse(urlParams['appData.localStorageContent']);
  65. if (typeof localStorageContent === 'object') {
  66. Object.keys(localStorageContent).forEach(key => {
  67. jitsiLocalStorage.setItem(key, localStorageContent[key]);
  68. });
  69. }
  70. } catch (error) {
  71. logger.error('Can\'t parse localStorageContent.', error);
  72. }
  73. jitsiLocalStorage.on('changed', onFakeLocalStorageChanged);
  74. }
  75. }
  76. setupJitsiLocalStorage();