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.

functions.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* global interfaceConfig */
  2. import { URI_PROTOCOL_PATTERN } from '../base/util';
  3. import { Platform } from '../base/react';
  4. import {
  5. DeepLinkingDesktopPage,
  6. DeepLinkingMobilePage,
  7. NoMobileApp
  8. } from './components';
  9. import { _shouldShowDeepLinkingDesktopPage }
  10. from './shouldShowDeepLinkingDesktopPage';
  11. /**
  12. * Indicates whether the window load event was already received.
  13. *
  14. * @type {boolean}
  15. */
  16. let windowIsLoaded = false;
  17. /**
  18. * Handler for the window load event.
  19. *
  20. * @returns {void}
  21. */
  22. function onWindowLoad() {
  23. windowIsLoaded = true;
  24. window.removeEventListener('load', onWindowLoad);
  25. }
  26. window.addEventListener('load', onWindowLoad);
  27. /**
  28. * Executes the passed function after the window load event was received.
  29. *
  30. * @param {Function} fn - The function that will be executed.
  31. * @returns {void}
  32. */
  33. function executeAfterWindowLoad(fn) {
  34. if (windowIsLoaded) {
  35. fn();
  36. } else {
  37. const loadHandler = () => {
  38. fn();
  39. window.removeEventListener('load', loadHandler);
  40. };
  41. window.addEventListener('load', loadHandler);
  42. }
  43. }
  44. /**
  45. * Generates a deep linking URL based on the current window URL.
  46. *
  47. * @returns {string} - The generated URL.
  48. */
  49. export function generateDeepLinkingURL() {
  50. // If the user installed the app while this Component was displayed
  51. // (e.g. the user clicked the Download the App button), then we would
  52. // like to open the current URL in the mobile app. The only way to do it
  53. // appears to be a link with an app-specific scheme, not a Universal
  54. // Link.
  55. const appScheme = interfaceConfig.APP_SCHEME || 'org.jitsi.meet';
  56. // Replace the protocol part with the app scheme.
  57. return window.location.href.replace(
  58. new RegExp(`^${URI_PROTOCOL_PATTERN}`), `${appScheme}:`);
  59. }
  60. /**
  61. * Resolves with the component that should be displayed if the deep linking page
  62. * should be shown and with <tt>undefined</tt> otherwise.
  63. *
  64. * @param {Object} state - Object containing current redux state.
  65. * @returns {Promise<Component>}
  66. */
  67. export function getDeepLinkingPage(state) {
  68. const { room } = state['features/base/conference'];
  69. // Show only if we are about to join a conference.
  70. if (!room) {
  71. return Promise.resolve();
  72. }
  73. const OS = Platform.OS;
  74. const isUsingMobileBrowser = OS === 'android' || OS === 'ios';
  75. if (isUsingMobileBrowser) { // mobile
  76. const mobileAppPromo
  77. = typeof interfaceConfig === 'object'
  78. && interfaceConfig.MOBILE_APP_PROMO;
  79. return Promise.resolve(
  80. typeof mobileAppPromo === 'undefined' || Boolean(mobileAppPromo)
  81. ? DeepLinkingMobilePage : NoMobileApp);
  82. }
  83. // desktop
  84. const { launchInWeb } = state['features/deep-linking'];
  85. if (launchInWeb) {
  86. return Promise.resolve();
  87. }
  88. return _shouldShowDeepLinkingDesktopPage().then(
  89. // eslint-disable-next-line no-confusing-arrow
  90. show => show ? DeepLinkingDesktopPage : undefined);
  91. }
  92. /**
  93. * Opens the desktop app.
  94. *
  95. * @returns {void}
  96. */
  97. export function openDesktopApp() {
  98. executeAfterWindowLoad(() => {
  99. // If the code for opening the deep link is executed before the window
  100. // load event, something with the internal chrome state goes wrong. The
  101. // result is that no window load event is received which is the cause
  102. // for some permission prompts to not be displayed. In our case the GUM
  103. // prompt wasn't displayed which causes the GUM call to never finish.
  104. window.location.href = generateDeepLinkingURL();
  105. });
  106. }