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.

ChromeExtensionBanner.web.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import { connect } from '../../base/redux';
  4. import { Icon, IconClose } from '../../base/icons';
  5. import { translate } from '../../base/i18n';
  6. import { getCurrentConference } from '../../base/conference/functions';
  7. import { browser } from '../../base/lib-jitsi-meet';
  8. import { isMobileBrowser } from '../../base/environment/utils';
  9. declare var interfaceConfig: Object;
  10. /**
  11. * Local storage key name for flag telling if user checked 'Don't show again' checkbox on the banner
  12. * If the user checks this before closing the banner, next time he will access a jitsi domain
  13. * the banner will not be shown regardless of extensions being installed or not.
  14. */
  15. const DONT_SHOW_AGAIN_CHECKED = 'hide_chrome_extension_banner';
  16. /**
  17. * The type of the React {@code PureComponent} props of {@link ChromeExtensionBanner}.
  18. */
  19. type Props = {
  20. /**
  21. * Conference data, if any
  22. */
  23. conference: Object,
  24. /**
  25. * The url of the chrome extension
  26. */
  27. chromeExtensionUrl: string,
  28. /**
  29. * An array containing info for identifying a chrome extension
  30. */
  31. chromeExtensionsInfo: Array<Object>,
  32. /**
  33. * Whether I am the current recorder.
  34. */
  35. iAmRecorder: boolean,
  36. /**
  37. * Invoked to obtain translated strings.
  38. */
  39. t: Function,
  40. };
  41. /**
  42. * The type of the React {@link PureComponent} state of {@link ChromeExtensionBanner}.
  43. */
  44. type State = {
  45. /**
  46. * Keeps the current value of dont show again checkbox
  47. */
  48. dontShowAgainChecked: boolean,
  49. /**
  50. * Tells whether user pressed install extension or close button.
  51. */
  52. closePressed: boolean,
  53. /**
  54. * Tells whether should show the banner or not based on extension being installed or not.
  55. */
  56. shouldShow: boolean,
  57. };
  58. /**
  59. * Implements a React {@link PureComponent} which displays a banner having a link to the chrome extension.
  60. * @class ChromeExtensionBanner
  61. * @extends PureComponent
  62. */
  63. class ChromeExtensionBanner extends PureComponent<Props, State> {
  64. /**
  65. * Initializes a new {@code ChromeExtensionBanner} instance.
  66. *
  67. * @param {Object} props - The read-only React {@code PureComponent} props with
  68. * which the new instance is to be initialized.
  69. */
  70. constructor(props: Props) {
  71. super(props);
  72. this.state = {
  73. dontShowAgainChecked: false,
  74. closePressed: false,
  75. shouldShow: false
  76. };
  77. this._onClosePressed = this._onClosePressed.bind(this);
  78. this._onInstallExtensionClick = this._onInstallExtensionClick.bind(this);
  79. this._checkExtensionsInstalled = this._checkExtensionsInstalled.bind(this);
  80. this._shouldNotRender = this._shouldNotRender.bind(this);
  81. this._onDontShowAgainChange = this._onDontShowAgainChange.bind(this);
  82. }
  83. /**
  84. * Executed on component update.
  85. * Checks whether any chrome extension from the config is installed.
  86. *
  87. * @inheritdoc
  88. */
  89. async componentDidUpdate() {
  90. const hasExtensions = await this._checkExtensionsInstalled();
  91. if (
  92. hasExtensions
  93. && hasExtensions.length
  94. && hasExtensions.every(ext => !ext)
  95. && !this.state.shouldShow
  96. ) {
  97. this.setState({ shouldShow: true }); // eslint-disable-line
  98. }
  99. }
  100. _onClosePressed: () => void;
  101. /**
  102. * Closes the banner for the current session.
  103. *
  104. * @returns {void}
  105. */
  106. _onClosePressed() {
  107. this.setState({ closePressed: true });
  108. }
  109. _onInstallExtensionClick: () => void;
  110. /**
  111. * Opens the chrome extension page.
  112. *
  113. * @returns {void}
  114. */
  115. _onInstallExtensionClick() {
  116. window.open(this.props.chromeExtensionUrl);
  117. this.setState({ closePressed: true });
  118. }
  119. _checkExtensionsInstalled: () => Promise<*>;
  120. /**
  121. * Checks whether the chrome extensions defined in the config file are installed or not.
  122. *
  123. * @returns {Promise[]}
  124. */
  125. _checkExtensionsInstalled() {
  126. const isExtensionInstalled = info => new Promise(resolve => {
  127. const img = new Image();
  128. img.src = `chrome-extension://${info.id}/${info.path}`;
  129. img.onload = function() {
  130. resolve(true);
  131. };
  132. img.onerror = function() {
  133. resolve(false);
  134. };
  135. });
  136. const extensionInstalledFunction = info => isExtensionInstalled(info);
  137. if (!this.props.chromeExtensionsInfo.length) {
  138. console.warn('Further configuration needed, missing chrome extension(s) info');
  139. }
  140. return Promise.all(
  141. this.props.chromeExtensionsInfo.map(info => extensionInstalledFunction(info))
  142. );
  143. }
  144. _shouldNotRender: () => boolean;
  145. /**
  146. * Checks whether the banner should not be rendered.
  147. *
  148. * @returns {boolean} Whether to show the banner or not.
  149. */
  150. _shouldNotRender() {
  151. if (!this.props.chromeExtensionUrl) {
  152. console.warn('Further configuration needed, missing chrome extension URL');
  153. return true;
  154. }
  155. const dontShowAgain = localStorage.getItem(DONT_SHOW_AGAIN_CHECKED) === 'true';
  156. return !interfaceConfig.SHOW_CHROME_EXTENSION_BANNER
  157. || !browser.isChrome()
  158. || isMobileBrowser()
  159. || dontShowAgain
  160. || this.state.closePressed
  161. || !this.state.shouldShow
  162. || this.props.iAmRecorder;
  163. }
  164. _onDontShowAgainChange: (object: Object) => void;
  165. /**
  166. * Handles the current `don't show again` checkbox state.
  167. *
  168. * @param {Object} event - Input change event.
  169. * @returns {void}
  170. */
  171. _onDontShowAgainChange(event) {
  172. this.setState({ dontShowAgainChecked: event.target.checked });
  173. }
  174. /**
  175. * Implements React's {@link PureComponent#render()}.
  176. *
  177. * @inheritdoc
  178. * @returns {ReactElement}
  179. */
  180. render() {
  181. if (this._shouldNotRender()) {
  182. if (this.state.dontShowAgainChecked) {
  183. localStorage.setItem(DONT_SHOW_AGAIN_CHECKED, 'true');
  184. }
  185. return null;
  186. }
  187. const { t } = this.props;
  188. const mainClassNames = this.props.conference
  189. ? 'chrome-extension-banner chrome-extension-banner__pos_in_meeting'
  190. : 'chrome-extension-banner';
  191. return (
  192. <div className = { mainClassNames }>
  193. <div className = 'chrome-extension-banner__container'>
  194. <div
  195. className = 'chrome-extension-banner__icon-container' />
  196. <div
  197. className = 'chrome-extension-banner__text-container'>
  198. { t('chromeExtensionBanner.installExtensionText') }
  199. </div>
  200. <div
  201. className = 'chrome-extension-banner__close-container'
  202. onClick = { this._onClosePressed }>
  203. <Icon
  204. className = 'gray'
  205. size = { 12 }
  206. src = { IconClose } />
  207. </div>
  208. </div>
  209. <div
  210. className = 'chrome-extension-banner__button-container'>
  211. <div
  212. className = 'chrome-extension-banner__button-open-url'
  213. onClick = { this._onInstallExtensionClick }>
  214. <div
  215. className = 'chrome-extension-banner__button-text'>
  216. { t('chromeExtensionBanner.buttonText') }
  217. </div>
  218. </div>
  219. </div>
  220. <div className = 'chrome-extension-banner__checkbox-container'>
  221. <label className = 'chrome-extension-banner__checkbox-label'>
  222. <input
  223. checked = { this.state.dontShowAgainChecked }
  224. onChange = { this._onDontShowAgainChange }
  225. type = 'checkbox' />
  226. &nbsp;{ t('chromeExtensionBanner.dontShowAgain') }
  227. </label>
  228. </div>
  229. </div>
  230. );
  231. }
  232. }
  233. /**
  234. * Function that maps parts of Redux state tree into component props.
  235. *
  236. * @param {Object} state - Redux state.
  237. * @returns {Object}
  238. */
  239. const _mapStateToProps = state => {
  240. const bannerCfg = state['features/base/config'].chromeExtensionBanner || {};
  241. return {
  242. chromeExtensionUrl: bannerCfg.url,
  243. chromeExtensionsInfo: bannerCfg.chromeExtensionsInfo || [],
  244. conference: getCurrentConference(state),
  245. iAmRecorder: state['features/base/config'].iAmRecorder
  246. };
  247. };
  248. export default translate(connect(_mapStateToProps)(ChromeExtensionBanner));