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 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* @flow */
  2. import _ from 'lodash';
  3. import { _CONFIG_STORE_PREFIX } from './constants';
  4. import parseURLParams from './parseURLParams';
  5. declare var $: Object;
  6. /**
  7. * The config keys to whitelist, the keys that can be overridden.
  8. * Currently we can only whitelist the first part of the properties, like
  9. * 'p2p.useStunTurn' and 'p2p.enabled' we whitelist all p2p options.
  10. * The whitelist is used only for config.js.
  11. *
  12. * @private
  13. * @type Array
  14. */
  15. const WHITELISTED_KEYS = [
  16. '_peerConnStatusOutOfLastNTimeout',
  17. '_peerConnStatusRtcMuteTimeout',
  18. 'abTesting',
  19. 'alwaysVisibleToolbar',
  20. 'autoRecord',
  21. 'autoRecordToken',
  22. 'avgRtpStatsN',
  23. 'callStatsConfIDNamespace',
  24. 'callStatsID',
  25. 'callStatsSecret',
  26. 'callUUID',
  27. 'channelLastN',
  28. 'constraints',
  29. 'debug',
  30. 'debugAudioLevels',
  31. 'defaultLanguage',
  32. 'desktopSharingChromeDisabled',
  33. 'desktopSharingChromeExtId',
  34. 'desktopSharingChromeMinExtVersion',
  35. 'desktopSharingChromeSources',
  36. 'desktopSharingFrameRate',
  37. 'desktopSharingFirefoxDisabled',
  38. 'desktopSharingSources',
  39. 'disable1On1Mode',
  40. 'disableAEC',
  41. 'disableAGC',
  42. 'disableAP',
  43. 'disableAudioLevels',
  44. 'disableDesktopSharing',
  45. 'disableDesktopSharing',
  46. 'disableH264',
  47. 'disableHPF',
  48. 'disableNS',
  49. 'disableRemoteControl',
  50. 'disableRtx',
  51. 'disableSuspendVideo',
  52. 'displayJids',
  53. 'enableDisplayNameInStats',
  54. 'enableLipSync',
  55. 'enableLocalVideoFlip',
  56. 'enableRecording',
  57. 'enableRemb',
  58. 'enableStatsID',
  59. 'enableTalkWhileMuted',
  60. 'enableTcc',
  61. 'enableUserRolesBasedOnToken',
  62. 'etherpad_base',
  63. 'failICE',
  64. 'firefox_fake_device',
  65. 'forceJVB121Ratio',
  66. 'gatherStats',
  67. 'googleApiApplicationClientID',
  68. 'hiddenDomain',
  69. 'hosts',
  70. 'iAmRecorder',
  71. 'iAmSipGateway',
  72. 'iceTransportPolicy',
  73. 'ignoreStartMuted',
  74. 'minParticipants',
  75. 'nick',
  76. 'openBridgeChannel',
  77. 'p2p',
  78. 'preferH264',
  79. 'recordingType',
  80. 'requireDisplayName',
  81. 'resolution',
  82. 'startAudioMuted',
  83. 'startAudioOnly',
  84. 'startBitrate',
  85. 'startScreenSharing',
  86. 'startVideoMuted',
  87. 'startWithAudioMuted',
  88. 'startWithVideoMuted',
  89. 'testing',
  90. 'useIPv6',
  91. 'useNicks',
  92. 'useStunTurn',
  93. 'webrtcIceTcpDisable',
  94. 'webrtcIceUdpDisable'
  95. ];
  96. const logger = require('jitsi-meet-logger').getLogger(__filename);
  97. // XXX The functions getRoomName and parseURLParams are split out of
  98. // functions.js because they are bundled in both app.bundle and
  99. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  100. // want all functions to be bundled in do_external_connect.
  101. export { default as getRoomName } from './getRoomName';
  102. export { parseURLParams };
  103. /**
  104. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  105. * of the room is included in JSON format:
  106. * {
  107. * "rooomName": "someroom12345"
  108. * }.
  109. *
  110. * @param {string} endpoint - The name of HTTP endpoint to which to send
  111. * the HTTP POST request.
  112. * @param {string} roomName - The name of the conference room for which config
  113. * is requested.
  114. * @param {Function} complete - The callback to invoke upon success or failure.
  115. * @returns {void}
  116. */
  117. export function obtainConfig(
  118. endpoint: string,
  119. roomName: string,
  120. complete: Function) {
  121. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  122. $.ajax(
  123. endpoint,
  124. {
  125. contentType: 'application/json',
  126. data: JSON.stringify({ roomName }),
  127. dataType: 'json',
  128. method: 'POST',
  129. error(jqXHR, textStatus, errorThrown) {
  130. logger.error('Get config error: ', jqXHR, errorThrown);
  131. complete(false, `Get config response status: ${textStatus}`);
  132. },
  133. success(data) {
  134. const { config, interfaceConfig, loggingConfig } = window;
  135. try {
  136. overrideConfigJSON(
  137. config, interfaceConfig, loggingConfig,
  138. data);
  139. complete(true);
  140. } catch (e) {
  141. logger.error('Parse config error: ', e);
  142. complete(false, e);
  143. }
  144. }
  145. }
  146. );
  147. }
  148. /* eslint-disable max-params, no-shadow */
  149. /**
  150. * Overrides JSON properties in {@code config} and
  151. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  152. * Overrides only the whitelisted keys.
  153. *
  154. * @param {Object} config - The config Object in which we'll be overriding
  155. * properties.
  156. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  157. * be overriding properties.
  158. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  159. * overriding properties.
  160. * @param {Object} json - Object containing configuration properties.
  161. * Destination object is selected based on root property name:
  162. * {
  163. * config: {
  164. * // config.js properties here
  165. * },
  166. * interfaceConfig: {
  167. * // interface_config.js properties here
  168. * },
  169. * loggingConfig: {
  170. * // logging_config.js properties here
  171. * }
  172. * }.
  173. * @returns {void}
  174. */
  175. export function overrideConfigJSON(
  176. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  177. json: Object) {
  178. for (const configName of Object.keys(json)) {
  179. let configObj;
  180. if (configName === 'config') {
  181. configObj = config;
  182. } else if (configName === 'interfaceConfig') {
  183. configObj = interfaceConfig;
  184. } else if (configName === 'loggingConfig') {
  185. configObj = loggingConfig;
  186. }
  187. if (configObj) {
  188. const configJSON
  189. = _getWhitelistedJSON(configName, json[configName]);
  190. if (!_.isEmpty(configJSON)) {
  191. logger.info(
  192. `Extending ${configName} with: ${
  193. JSON.stringify(configJSON)}`);
  194. // eslint-disable-next-line arrow-body-style
  195. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  196. // XXX We don't want to merge the arrays, we want to
  197. // overwrite them.
  198. return Array.isArray(oldValue) ? newValue : undefined;
  199. });
  200. }
  201. }
  202. }
  203. }
  204. /* eslint-enable max-params, no-shadow */
  205. /**
  206. * Whitelist only config.js, skips this for others configs
  207. * (interfaceConfig, loggingConfig).
  208. * Only extracts overridden values for keys we allow to be overridden.
  209. *
  210. * @param {string} configName - The config name, one of config,
  211. * interfaceConfig, loggingConfig.
  212. * @param {Object} configJSON - The object with keys and values to override.
  213. * @private
  214. * @returns {Object} - The result object only with the keys
  215. * that are whitelisted.
  216. */
  217. function _getWhitelistedJSON(configName, configJSON) {
  218. if (configName !== 'config') {
  219. return configJSON;
  220. }
  221. return _.pick(configJSON, WHITELISTED_KEYS);
  222. }
  223. /**
  224. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  225. * previously downloaded from a specific {@code baseURL} and stored with
  226. * {@link storeConfig}.
  227. *
  228. * @param {string} baseURL - The base URL from which the config.js was
  229. * previously downloaded and stored with {@code storeConfig}.
  230. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  231. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  232. * otherwise, {@code undefined}.
  233. */
  234. export function restoreConfig(baseURL: string): ?Object {
  235. let storage;
  236. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  237. try {
  238. // XXX Even reading the property localStorage of window may throw an
  239. // error (which is user agent-specific behavior).
  240. storage = window.localStorage;
  241. const config = storage.getItem(key);
  242. if (config) {
  243. return JSON.parse(config) || undefined;
  244. }
  245. } catch (e) {
  246. // Somehow incorrect data ended up in the storage. Clean it up.
  247. storage && storage.removeItem(key);
  248. }
  249. return undefined;
  250. }
  251. /* eslint-disable max-params */
  252. /**
  253. * Inspects the hash part of the location URI and overrides values specified
  254. * there in the corresponding config objects given as the arguments. The syntax
  255. * is: {@code https://server.com/room#config.debug=true
  256. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  257. *
  258. * In the hash part each parameter will be parsed to JSON and then the root
  259. * object will be matched with the corresponding config object given as the
  260. * argument to this function.
  261. *
  262. * @param {Object} config - This is the general config.
  263. * @param {Object} interfaceConfig - This is the interface config.
  264. * @param {Object} loggingConfig - The logging config.
  265. * @param {URI} location - The new location to which the app is navigating to.
  266. * @returns {void}
  267. */
  268. export function setConfigFromURLParams(
  269. config: ?Object,
  270. interfaceConfig: ?Object,
  271. loggingConfig: ?Object,
  272. location: Object) {
  273. const params = parseURLParams(location);
  274. const json = {};
  275. // At this point we have:
  276. // params = {
  277. // "config.disableAudioLevels": false,
  278. // "config.channelLastN": -1,
  279. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  280. // }
  281. // We want to have:
  282. // json = {
  283. // config: {
  284. // "disableAudioLevels": false,
  285. // "channelLastN": -1
  286. // },
  287. // interfaceConfig: {
  288. // "APP_NAME": "Jitsi Meet"
  289. // }
  290. // }
  291. config && (json.config = {});
  292. interfaceConfig && (json.interfaceConfig = {});
  293. loggingConfig && (json.loggingConfig = {});
  294. for (const param of Object.keys(params)) {
  295. let base = json;
  296. const names = param.split('.');
  297. const last = names.pop();
  298. for (const name of names) {
  299. base = base[name] = base[name] || {};
  300. }
  301. base[last] = params[param];
  302. }
  303. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  304. }
  305. /* eslint-enable max-params */