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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* global __DEV__ */
  2. import { Linking } from 'react-native';
  3. import '../../audio-mode';
  4. import '../../background';
  5. import { Platform } from '../../base/react';
  6. import '../../full-screen';
  7. import '../../wake-lock';
  8. import { AbstractApp } from './AbstractApp';
  9. /**
  10. * Root application component.
  11. *
  12. * @extends AbstractApp
  13. */
  14. export class App extends AbstractApp {
  15. /**
  16. * App component's property types.
  17. *
  18. * @static
  19. */
  20. static propTypes = AbstractApp.propTypes
  21. /**
  22. * Initializes a new App instance.
  23. *
  24. * @param {Object} props - The read-only React Component props with which
  25. * the new instance is to be initialized.
  26. */
  27. constructor(props) {
  28. super(props);
  29. // Bind event handlers so they are only bound once for every instance.
  30. this._onLinkingURL = this._onLinkingURL.bind(this);
  31. // In the Release configuration, React Native will (intentionally) throw
  32. // an unhandled JavascriptException for an unhandled JavaScript error.
  33. // This will effectively kill the application. In accord with the Web,
  34. // do not kill the application.
  35. this._maybeDisableExceptionsManager();
  36. }
  37. /**
  38. * Subscribe to notifications about activating URLs registered to be handled
  39. * by this app.
  40. *
  41. * @inheritdoc
  42. * @see https://facebook.github.io/react-native/docs/linking.html
  43. * @returns {void}
  44. */
  45. componentWillMount() {
  46. super.componentWillMount();
  47. Linking.addEventListener('url', this._onLinkingURL);
  48. }
  49. /**
  50. * Unsubscribe from notifications about activating URLs registered to be
  51. * handled by this app.
  52. *
  53. * @inheritdoc
  54. * @see https://facebook.github.io/react-native/docs/linking.html
  55. * @returns {void}
  56. */
  57. componentWillUnmount() {
  58. Linking.removeEventListener('url', this._onLinkingURL);
  59. super.componentWillUnmount();
  60. }
  61. /**
  62. * Attempts to disable the use of React Native
  63. * {@link ExceptionsManager#handleException} on platforms and in
  64. * configurations on/in which the use of the method in questions has been
  65. * determined to be undesirable. For example, React Native will
  66. * (intentionally) throw an unhandled JavascriptException for an
  67. * unhandled JavaScript error in the Release configuration. This will
  68. * effectively kill the application. In accord with the Web, do not kill the
  69. * application.
  70. *
  71. * @private
  72. * @returns {void}
  73. */
  74. _maybeDisableExceptionsManager() {
  75. if (__DEV__) {
  76. // As mentioned above, only the Release configuration was observed
  77. // to suffer.
  78. return;
  79. }
  80. if (Platform.OS !== 'android') {
  81. // A solution based on RTCSetFatalHandler was implemented on iOS and
  82. // it is preferred because it is at a later step of the
  83. // error/exception handling and it is specific to fatal
  84. // errors/exceptions which were observed to kill the application.
  85. // The solution implemented bellow was tested on Android only so it
  86. // is considered safest to use it there only.
  87. return;
  88. }
  89. const oldHandler = global.ErrorUtils.getGlobalHandler();
  90. const newHandler = _handleException;
  91. if (!oldHandler || oldHandler !== newHandler) {
  92. newHandler.next = oldHandler;
  93. global.ErrorUtils.setGlobalHandler(newHandler);
  94. }
  95. }
  96. /**
  97. * Notified by React's Linking API that a specific URL registered to be
  98. * handled by this App was activated.
  99. *
  100. * @param {Object} event - The details of the notification/event.
  101. * @param {string} event.url - The URL registered to be handled by this App
  102. * which was activated.
  103. * @private
  104. * @returns {void}
  105. */
  106. _onLinkingURL(event) {
  107. this._openURL(event.url);
  108. }
  109. }
  110. /**
  111. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  112. * from converting a fatal error into an unhandled native exception which will
  113. * kill the application.
  114. *
  115. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  116. * @param {boolean} fatal - True if the specified error is fatal; otherwise,
  117. * false.
  118. * @private
  119. * @returns {void}
  120. */
  121. function _handleException(error, fatal) {
  122. if (fatal) {
  123. // In the Release configuration, React Native will (intentionally) throw
  124. // an unhandled JavascriptException for an unhandled JavaScript error.
  125. // This will effectively kill the application. In accord with the Web,
  126. // do not kill the application.
  127. console.error(error);
  128. } else {
  129. // Forward to the next globalHandler of ErrorUtils.
  130. const next = _handleException.next;
  131. typeof next === 'function' && next(error, fatal);
  132. }
  133. }