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.

App.native.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // @flow
  2. import React from 'react';
  3. import '../../analytics';
  4. import '../../authentication';
  5. import { setColorScheme } from '../../base/color-scheme';
  6. import { DialogContainer } from '../../base/dialog';
  7. import { CALL_INTEGRATION_ENABLED, updateFlags } from '../../base/flags';
  8. import '../../base/jwt';
  9. import { Platform } from '../../base/react';
  10. import '../../base/responsive-ui';
  11. import { updateSettings } from '../../base/settings';
  12. import '../../google-api';
  13. import '../../mobile/audio-mode';
  14. import '../../mobile/back-button';
  15. import '../../mobile/background';
  16. import '../../mobile/call-integration';
  17. import '../../mobile/external-api';
  18. import '../../mobile/full-screen';
  19. import '../../mobile/permissions';
  20. import '../../mobile/picture-in-picture';
  21. import '../../mobile/proximity';
  22. import '../../mobile/wake-lock';
  23. import '../../mobile/watchos';
  24. import logger from '../logger';
  25. import { AbstractApp } from './AbstractApp';
  26. import type { Props as AbstractAppProps } from './AbstractApp';
  27. declare var __DEV__;
  28. /**
  29. * The type of React {@code Component} props of {@link App}.
  30. */
  31. type Props = AbstractAppProps & {
  32. /**
  33. * An object of colors that override the default colors of the app/sdk.
  34. */
  35. colorScheme: ?Object,
  36. /**
  37. * Identifier for this app on the native side.
  38. */
  39. externalAPIScope: string,
  40. /**
  41. * An object with the feature flags.
  42. */
  43. flags: Object,
  44. /**
  45. * An object with user information (display name, email, avatar URL).
  46. */
  47. userInfo: ?Object
  48. };
  49. /**
  50. * Root app {@code Component} on mobile/React Native.
  51. *
  52. * @extends AbstractApp
  53. */
  54. export class App extends AbstractApp {
  55. _init: Promise<*>;
  56. /**
  57. * Initializes a new {@code App} instance.
  58. *
  59. * @param {Props} props - The read-only React {@code Component} props with
  60. * which the new instance is to be initialized.
  61. */
  62. constructor(props: Props) {
  63. super(props);
  64. // In the Release configuration, React Native will (intentionally) throw
  65. // an unhandled JavascriptException for an unhandled JavaScript error.
  66. // This will effectively kill the app. In accord with the Web, do not
  67. // kill the app.
  68. this._maybeDisableExceptionsManager();
  69. }
  70. /**
  71. * Initializes the color scheme.
  72. *
  73. * @inheritdoc
  74. *
  75. * @returns {void}
  76. */
  77. componentDidMount() {
  78. super.componentDidMount();
  79. this._init.then(() => {
  80. // We set these early enough so then we avoid any unnecessary re-renders.
  81. const { dispatch } = this.state.store;
  82. dispatch(setColorScheme(this.props.colorScheme));
  83. dispatch(updateFlags(this.props.flags));
  84. dispatch(updateSettings(this.props.userInfo || {}));
  85. // Update settings with feature-flag.
  86. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  87. if (typeof callIntegrationEnabled !== 'undefined') {
  88. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  89. }
  90. });
  91. }
  92. /**
  93. * Attempts to disable the use of React Native
  94. * {@link ExceptionsManager#handleException} on platforms and in
  95. * configurations on/in which the use of the method in questions has been
  96. * determined to be undesirable. For example, React Native will
  97. * (intentionally) throw an unhandled {@code JavascriptException} for an
  98. * unhandled JavaScript error in the Release configuration. This will
  99. * effectively kill the app. In accord with the Web, do not kill the app.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. _maybeDisableExceptionsManager() {
  105. if (__DEV__) {
  106. // As mentioned above, only the Release configuration was observed
  107. // to suffer.
  108. return;
  109. }
  110. if (Platform.OS !== 'android') {
  111. // A solution based on RTCSetFatalHandler was implemented on iOS and
  112. // it is preferred because it is at a later step of the
  113. // error/exception handling and it is specific to fatal
  114. // errors/exceptions which were observed to kill the app. The
  115. // solution implemented bellow was tested on Android only so it is
  116. // considered safest to use it there only.
  117. return;
  118. }
  119. const oldHandler = global.ErrorUtils.getGlobalHandler();
  120. const newHandler = _handleException;
  121. if (!oldHandler || oldHandler !== newHandler) {
  122. newHandler.next = oldHandler;
  123. global.ErrorUtils.setGlobalHandler(newHandler);
  124. }
  125. }
  126. /**
  127. * Renders the platform specific dialog container.
  128. *
  129. * @returns {React$Element}
  130. */
  131. _renderDialogContainer() {
  132. return (
  133. <DialogContainer />
  134. );
  135. }
  136. }
  137. /**
  138. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  139. * from converting a fatal error into an unhandled native exception which will
  140. * kill the app.
  141. *
  142. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  143. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  144. * otherwise, {@code false}.
  145. * @private
  146. * @returns {void}
  147. */
  148. function _handleException(error, fatal) {
  149. if (fatal) {
  150. // In the Release configuration, React Native will (intentionally) throw
  151. // an unhandled JavascriptException for an unhandled JavaScript error.
  152. // This will effectively kill the app. In accord with the Web, do not
  153. // kill the app.
  154. logger.error(error);
  155. } else {
  156. // Forward to the next globalHandler of ErrorUtils.
  157. const { next } = _handleException;
  158. typeof next === 'function' && next(error, fatal);
  159. }
  160. }