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.

browser.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { DOMParser } from '@xmldom/xmldom';
  2. import { Platform } from 'react-native';
  3. import BackgroundTimer from 'react-native-background-timer';
  4. import 'react-native-url-polyfill/auto'; // Complete URL polyfill.
  5. import Storage from './Storage';
  6. /**
  7. * Gets the first common prototype of two specified Objects (treating the
  8. * objects themselves as prototypes as well).
  9. *
  10. * @param {Object} a - The first prototype chain to climb in search of a common
  11. * prototype.
  12. * @param {Object} b - The second prototype chain to climb in search of a common
  13. * prototype.
  14. * @returns {Object|undefined} - The first common prototype of a and b.
  15. */
  16. function _getCommonPrototype(a, b) {
  17. // Allow the arguments to be prototypes themselves.
  18. if (a === b) {
  19. return a;
  20. }
  21. let p;
  22. if (((p = Object.getPrototypeOf(a)) && (p = _getCommonPrototype(b, p)))
  23. || ((p = Object.getPrototypeOf(b))
  24. && (p = _getCommonPrototype(a, p)))) {
  25. return p;
  26. }
  27. return undefined;
  28. }
  29. /**
  30. * Implements an absolute minimum of the common logic of
  31. * {@code Document.querySelector} and {@code Element.querySelector}. Implements
  32. * the most simple of selectors necessary to satisfy the call sites at the time
  33. * of this writing (i.e. Select by tagName).
  34. *
  35. * @param {Node} node - The Node which is the root of the tree to query.
  36. * @param {string} selectors - The group of CSS selectors to match on.
  37. * @returns {Element} - The first Element which is a descendant of the specified
  38. * node and matches the specified group of selectors.
  39. */
  40. function _querySelector(node, selectors) {
  41. let element = null;
  42. node && _visitNode(node, n => {
  43. if (n.nodeType === 1 /* ELEMENT_NODE */
  44. && n.nodeName === selectors) {
  45. element = n;
  46. return true;
  47. }
  48. return false;
  49. });
  50. return element;
  51. }
  52. /**
  53. * Visits each Node in the tree of a specific root Node (using depth-first
  54. * traversal) and invokes a specific callback until the callback returns true.
  55. *
  56. * @param {Node} node - The root Node which represents the tree of Nodes to
  57. * visit.
  58. * @param {Function} callback - The callback to invoke with each visited Node.
  59. * @returns {boolean} - True if the specified callback returned true for a Node
  60. * (at which point the visiting stopped); otherwise, false.
  61. */
  62. function _visitNode(node, callback) {
  63. if (callback(node)) {
  64. return true;
  65. }
  66. /* eslint-disable no-param-reassign, no-extra-parens */
  67. if ((node = node.firstChild)) {
  68. do {
  69. if (_visitNode(node, callback)) {
  70. return true;
  71. }
  72. } while ((node = node.nextSibling));
  73. }
  74. /* eslint-enable no-param-reassign, no-extra-parens */
  75. return false;
  76. }
  77. (global => {
  78. // DOMParser
  79. //
  80. // Required by:
  81. // - lib-jitsi-meet requires this if using WebSockets
  82. global.DOMParser = DOMParser;
  83. // addEventListener
  84. //
  85. // Required by:
  86. // - jQuery
  87. if (typeof global.addEventListener === 'undefined') {
  88. // eslint-disable-next-line no-empty-function
  89. global.addEventListener = () => {};
  90. }
  91. // Promise.allSettled is supported from RN 0.63 onwards, use a polyfill for that.
  92. // Invokes its shim method to shim Promise.allSettled if it is unavailable or noncompliant.
  93. //
  94. // Required by:
  95. // lib-jitsi-meet/JitsiConference.js
  96. require('promise.allsettled').shim();
  97. // removeEventListener
  98. //
  99. // Required by:
  100. // - features/base/conference/middleware
  101. if (typeof global.removeEventListener === 'undefined') {
  102. // eslint-disable-next-line no-empty-function
  103. global.removeEventListener = () => {};
  104. }
  105. // document
  106. //
  107. // Required by:
  108. // - jQuery
  109. // - Strophe
  110. if (typeof global.document === 'undefined') {
  111. const document
  112. = new DOMParser().parseFromString(
  113. '<html><head></head><body></body></html>',
  114. 'text/xml');
  115. // document.addEventListener
  116. //
  117. // Required by:
  118. // - jQuery
  119. if (typeof document.addEventListener === 'undefined') {
  120. // eslint-disable-next-line no-empty-function
  121. document.addEventListener = () => {};
  122. }
  123. // document.cookie
  124. //
  125. // Required by:
  126. // - herment
  127. if (typeof document.cookie === 'undefined') {
  128. document.cookie = '';
  129. }
  130. // document.implementation.createHTMLDocument
  131. //
  132. // Required by:
  133. // - jQuery
  134. if (typeof document.implementation.createHTMLDocument === 'undefined') {
  135. document.implementation.createHTMLDocument = function(title = '') {
  136. const htmlDocument
  137. = new DOMParser().parseFromString(
  138. `<html>
  139. <head><title>${title}</title></head>
  140. <body></body>
  141. </html>`,
  142. 'text/xml');
  143. Object.defineProperty(htmlDocument, 'body', {
  144. get() {
  145. return htmlDocument.getElementsByTagName('body')[0];
  146. }
  147. });
  148. return htmlDocument;
  149. };
  150. }
  151. // Element.querySelector
  152. //
  153. // Required by:
  154. // - lib-jitsi-meet/modules/xmpp
  155. const elementPrototype
  156. = Object.getPrototypeOf(document.documentElement);
  157. if (elementPrototype) {
  158. if (typeof elementPrototype.querySelector === 'undefined') {
  159. elementPrototype.querySelector = function(selectors) {
  160. return _querySelector(this, selectors);
  161. };
  162. }
  163. // Element.remove
  164. //
  165. // Required by:
  166. // - lib-jitsi-meet ChatRoom#onPresence parsing
  167. if (typeof elementPrototype.remove === 'undefined') {
  168. elementPrototype.remove = function() {
  169. if (this.parentNode !== null) {
  170. this.parentNode.removeChild(this);
  171. }
  172. };
  173. }
  174. // Element.innerHTML
  175. //
  176. // Required by:
  177. // - jQuery's .append method
  178. if (!elementPrototype.hasOwnProperty('innerHTML')) {
  179. Object.defineProperty(elementPrototype, 'innerHTML', {
  180. get() {
  181. return this.childNodes.toString();
  182. },
  183. set(innerHTML) {
  184. // MDN says: removes all of element's children, parses
  185. // the content string and assigns the resulting nodes as
  186. // children of the element.
  187. // Remove all of element's children.
  188. this.textContent = '';
  189. // Parse the content string.
  190. const d
  191. = new DOMParser().parseFromString(
  192. `<div>${innerHTML}</div>`,
  193. 'text/xml');
  194. // Assign the resulting nodes as children of the
  195. // element.
  196. const documentElement = d.documentElement;
  197. let child;
  198. // eslint-disable-next-line no-cond-assign
  199. while (child = documentElement.firstChild) {
  200. this.appendChild(child);
  201. }
  202. }
  203. });
  204. }
  205. // Element.children
  206. //
  207. // Required by:
  208. // - lib-jitsi-meet ChatRoom#onPresence parsing
  209. if (!elementPrototype.hasOwnProperty('children')) {
  210. Object.defineProperty(elementPrototype, 'children', {
  211. get() {
  212. const nodes = this.childNodes;
  213. const children = [];
  214. let i = 0;
  215. let node = nodes[i];
  216. while (node) {
  217. if (node.nodeType === 1) {
  218. children.push(node);
  219. }
  220. i += 1;
  221. node = nodes[i];
  222. }
  223. return children;
  224. }
  225. });
  226. }
  227. }
  228. // FIXME There is a weird infinite loop related to console.log and
  229. // Document and/or Element at the time of this writing. Work around it
  230. // by patching Node and/or overriding console.log.
  231. const documentPrototype = Object.getPrototypeOf(document);
  232. const nodePrototype
  233. = _getCommonPrototype(documentPrototype, elementPrototype);
  234. if (nodePrototype
  235. // XXX The intention was to find Node from which Document and
  236. // Element extend. If for whatever reason we've reached Object,
  237. // then it doesn't sound like what expected.
  238. && nodePrototype !== Object.getPrototypeOf({})) {
  239. // Override console.log.
  240. const { console } = global;
  241. if (console) {
  242. const loggerLevels = require('jitsi-meet-logger').levels;
  243. Object.keys(loggerLevels).forEach(key => {
  244. const level = loggerLevels[key];
  245. const consoleLog = console[level];
  246. /* eslint-disable prefer-rest-params */
  247. if (typeof consoleLog === 'function') {
  248. console[level] = function(...args) {
  249. // XXX If console's disableYellowBox is truthy, then
  250. // react-native will not automatically display the
  251. // yellow box for the warn level. However, it will
  252. // still display the red box for the error level.
  253. // But I disable the yellow box when I don't want to
  254. // have react-native automatically show me the
  255. // console's output just like in the Release build
  256. // configuration. Because I didn't find a way to
  257. // disable the red box, downgrade the error level to
  258. // warn. The red box will still be displayed but not
  259. // for the error level.
  260. if (console.disableYellowBox && level === 'error') {
  261. console.warn(...args);
  262. return;
  263. }
  264. const { length } = args;
  265. for (let i = 0; i < length; ++i) {
  266. let arg = args[i];
  267. if (arg
  268. && typeof arg !== 'string'
  269. // Limit the console.log override to
  270. // Node (instances).
  271. && nodePrototype.isPrototypeOf(arg)) {
  272. const toString = arg.toString;
  273. if (toString) {
  274. arg = toString.call(arg);
  275. }
  276. }
  277. args[i] = arg;
  278. }
  279. consoleLog.apply(this, args);
  280. };
  281. }
  282. /* eslint-enable prefer-rest-params */
  283. });
  284. }
  285. }
  286. global.document = document;
  287. }
  288. // location
  289. if (typeof global.location === 'undefined') {
  290. global.location = {
  291. href: '',
  292. // Required by:
  293. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  294. search: ''
  295. };
  296. }
  297. const { navigator } = global;
  298. if (navigator) {
  299. // userAgent
  300. //
  301. // Required by:
  302. // - lib-jitsi-meet/modules/browser/BrowserDetection.js
  303. let userAgent = navigator.userAgent || '';
  304. // react-native/version
  305. const { name, version } = require('react-native/package.json');
  306. let rn = name || 'react-native';
  307. version && (rn += `/${version}`);
  308. if (userAgent.indexOf(rn) === -1) {
  309. userAgent = userAgent ? `${rn} ${userAgent}` : rn;
  310. }
  311. // (OS version)
  312. const os = `(${Platform.OS} ${Platform.Version})`;
  313. if (userAgent.indexOf(os) === -1) {
  314. userAgent = userAgent ? `${userAgent} ${os}` : os;
  315. }
  316. navigator.userAgent = userAgent;
  317. }
  318. // WebRTC
  319. require('./webrtc');
  320. // Performance API
  321. // RN 0.61 does not provide performance.now(), and react-native-performance
  322. // requires it.
  323. const now = () => Date.now();
  324. if (!global.performance) {
  325. global.performance = {};
  326. }
  327. if (!global.performance.now) {
  328. global.performance.now = now;
  329. }
  330. const perf = require('react-native-performance');
  331. global.performance = perf.default;
  332. global.performance.now = now;
  333. global.PerformanceObserver = perf.PerformanceObserver;
  334. // CallStats
  335. //
  336. // Required by:
  337. // - lib-jitsi-meet
  338. require('react-native-callstats/csio-polyfill');
  339. global.callstats = require('react-native-callstats/callstats');
  340. // XMLHttpRequest
  341. if (global.XMLHttpRequest) {
  342. const { prototype } = global.XMLHttpRequest;
  343. // XMLHttpRequest.responseXML
  344. //
  345. // Required by:
  346. // - Strophe
  347. if (prototype && !prototype.hasOwnProperty('responseXML')) {
  348. Object.defineProperty(prototype, 'responseXML', {
  349. get() {
  350. const { responseText } = this;
  351. return (
  352. responseText
  353. && new DOMParser().parseFromString(
  354. responseText,
  355. 'text/xml'));
  356. }
  357. });
  358. }
  359. }
  360. // Timers
  361. //
  362. // React Native's timers won't run while the app is in the background, this
  363. // is a known limitation. Replace them with a background-friendly
  364. // alternative.
  365. //
  366. // Required by:
  367. // - lib-jitsi-meet
  368. // - Strophe
  369. if (Platform.OS === 'android') {
  370. global.clearTimeout = BackgroundTimer.clearTimeout.bind(BackgroundTimer);
  371. global.clearInterval = BackgroundTimer.clearInterval.bind(BackgroundTimer);
  372. global.setInterval = BackgroundTimer.setInterval.bind(BackgroundTimer);
  373. global.setTimeout = (fn, ms = 0) => BackgroundTimer.setTimeout(fn, ms);
  374. }
  375. // localStorage
  376. if (typeof global.localStorage === 'undefined') {
  377. global.localStorage = new Storage('@jitsi-meet/');
  378. }
  379. // sessionStorage
  380. //
  381. // Required by:
  382. // - herment
  383. // - Strophe
  384. if (typeof global.sessionStorage === 'undefined') {
  385. global.sessionStorage = new Storage();
  386. }
  387. })(global || window || this); // eslint-disable-line no-invalid-this