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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * Handles changes of the fake local storage.
  11. *
  12. * @returns {void}
  13. */
  14. function onFakeLocalStorageChanged() {
  15. APP.API.notifyLocalStorageChanged(jitsiLocalStorage.serialize());
  16. }
  17. /**
  18. * Checks if the local storage of the host page needs to be used instead jitsi-meet's local storage.
  19. *
  20. * @param {Object} urlParams - Object with parsed URL params.
  21. * @returns {boolean} - True if the local storage of the host page needs to be used instead jitsi-meet's local storage
  22. * and false otherwise.
  23. */
  24. function shouldUseHostPageLocalStorage(urlParams) {
  25. // NOTE: normally the url params and the config will be merged into the redux store. But we want to setup the local
  26. // storage as soon as possible, the store is not created yet and the merging of the URL params and the config
  27. // haven't been executed yet. That's why we need to manually parse the URL params and also access the config through
  28. // the global variable.
  29. if (urlParams['config.useHostPageLocalStorage'] === true
  30. || (typeof config === 'object' && config.useHostPageLocalStorage)) {
  31. return true;
  32. }
  33. if (jitsiLocalStorage.isLocalStorageDisabled()) { // We have detected that ou own local storage is not working.
  34. return true;
  35. }
  36. if (browser.isWebKitBased()) { // Webkit browsers don't persist local storage for third-party iframes.
  37. return true;
  38. }
  39. return false;
  40. }
  41. /**
  42. * Performs initial setup of the jitsiLocalStorage.
  43. *
  44. * @returns {void}
  45. */
  46. function setupJitsiLocalStorage() {
  47. const urlParams = parseURLParams(window.location);
  48. if (shouldUseHostPageLocalStorage(urlParams)) {
  49. try {
  50. const localStorageContent = Bourne.parse(urlParams['appData.localStorageContent']);
  51. if (typeof localStorageContent === 'object') {
  52. Object.keys(localStorageContent).forEach(key => {
  53. jitsiLocalStorage.setItem(key, localStorageContent[key]);
  54. });
  55. }
  56. } catch (error) {
  57. logger.error('Can\'t parse localStorageContent.', error);
  58. }
  59. jitsiLocalStorage.on('changed', onFakeLocalStorageChanged);
  60. }
  61. }
  62. setupJitsiLocalStorage();