Нет описания

WebView.android.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow
  9. */
  10. import React from 'react';
  11. import ReactNative, {
  12. ActivityIndicator,
  13. Image,
  14. requireNativeComponent,
  15. StyleSheet,
  16. UIManager,
  17. View,
  18. NativeModules
  19. } from 'react-native';
  20. import invariant from 'fbjs/lib/invariant';
  21. import keyMirror from 'fbjs/lib/keyMirror';
  22. import {
  23. defaultOriginWhitelist,
  24. createOnShouldStartLoadWithRequest,
  25. } from './WebViewShared';
  26. import type {
  27. WebViewError,
  28. WebViewErrorEvent,
  29. WebViewMessageEvent,
  30. WebViewNavigationEvent,
  31. WebViewProgressEvent,
  32. WebViewSharedProps,
  33. WebViewSource,
  34. } from './WebViewTypes';
  35. const resolveAssetSource = Image.resolveAssetSource;
  36. const WebViewState = keyMirror({
  37. IDLE: null,
  38. LOADING: null,
  39. ERROR: null,
  40. });
  41. const defaultRenderLoading = () => (
  42. <View style={styles.loadingView}>
  43. <ActivityIndicator style={styles.loadingProgressBar} />
  44. </View>
  45. );
  46. type State = {|
  47. viewState: WebViewState,
  48. lastErrorEvent: ?WebViewError,
  49. |};
  50. /**
  51. * Renders a native WebView.
  52. */
  53. class WebView extends React.Component<WebViewSharedProps, State> {
  54. static defaultProps = {
  55. overScrollMode: 'always',
  56. javaScriptEnabled: true,
  57. thirdPartyCookiesEnabled: true,
  58. scalesPageToFit: true,
  59. allowFileAccess: false,
  60. saveFormDataDisabled: false,
  61. enableCache: true,
  62. originWhitelist: defaultOriginWhitelist,
  63. };
  64. static isFileUploadSupported = async () => {
  65. // native implementation should return "true" only for Android 5+
  66. return NativeModules.RNCWebView.isFileUploadSupported();
  67. }
  68. state = {
  69. viewState: this.props.startInLoadingState
  70. ? WebViewState.LOADING
  71. : WebViewState.IDLE,
  72. lastErrorEvent: null,
  73. };
  74. webViewRef = React.createRef();
  75. render() {
  76. let otherView = null;
  77. if (this.state.viewState === WebViewState.LOADING) {
  78. otherView = (this.props.renderLoading || defaultRenderLoading)();
  79. } else if (this.state.viewState === WebViewState.ERROR) {
  80. const errorEvent = this.state.lastErrorEvent;
  81. invariant(errorEvent != null, 'lastErrorEvent expected to be non-null');
  82. otherView =
  83. this.props.renderError &&
  84. this.props.renderError(
  85. errorEvent.domain,
  86. errorEvent.code,
  87. errorEvent.description,
  88. );
  89. } else if (this.state.viewState !== WebViewState.IDLE) {
  90. console.error(
  91. 'RNCWebView invalid state encountered: ' + this.state.viewState,
  92. );
  93. }
  94. const webViewStyles = [styles.container, this.props.style];
  95. if (
  96. this.state.viewState === WebViewState.LOADING ||
  97. this.state.viewState === WebViewState.ERROR
  98. ) {
  99. // if we're in either LOADING or ERROR states, don't show the webView
  100. webViewStyles.push(styles.hidden);
  101. }
  102. let source: WebViewSource = this.props.source || {};
  103. if (!this.props.source && this.props.html) {
  104. source = { html: this.props.html };
  105. } else if (!this.props.source && this.props.url) {
  106. source = { uri: this.props.url };
  107. }
  108. if (source.method === 'POST' && source.headers) {
  109. console.warn(
  110. 'WebView: `source.headers` is not supported when using POST.',
  111. );
  112. } else if (source.method === 'GET' && source.body) {
  113. console.warn('WebView: `source.body` is not supported when using GET.');
  114. }
  115. const nativeConfig = this.props.nativeConfig || {};
  116. let NativeWebView = nativeConfig.component || RNCWebView;
  117. const onShouldStartLoadWithRequest = createOnShouldStartLoadWithRequest(
  118. this.onShouldStartLoadWithRequestCallback,
  119. this.props.originWhitelist,
  120. this.props.onShouldStartLoadWithRequest,
  121. );
  122. const webView = (
  123. <NativeWebView
  124. ref={this.webViewRef}
  125. key="webViewKey"
  126. style={webViewStyles}
  127. source={resolveAssetSource(source)}
  128. scalesPageToFit={this.props.scalesPageToFit}
  129. allowFileAccess={this.props.allowFileAccess}
  130. injectedJavaScript={this.props.injectedJavaScript}
  131. userAgent={this.props.userAgent}
  132. javaScriptEnabled={this.props.javaScriptEnabled}
  133. thirdPartyCookiesEnabled={this.props.thirdPartyCookiesEnabled}
  134. domStorageEnabled={this.props.domStorageEnabled}
  135. messagingEnabled={typeof this.props.onMessage === 'function'}
  136. onMessage={this.onMessage}
  137. overScrollMode={this.props.overScrollMode}
  138. contentInset={this.props.contentInset}
  139. automaticallyAdjustContentInsets={
  140. this.props.automaticallyAdjustContentInsets
  141. }
  142. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  143. onContentSizeChange={this.props.onContentSizeChange}
  144. onLoadingStart={this.onLoadingStart}
  145. onLoadingFinish={this.onLoadingFinish}
  146. onLoadingError={this.onLoadingError}
  147. onLoadingProgress={this.onLoadingProgress}
  148. testID={this.props.testID}
  149. geolocationEnabled={this.props.geolocationEnabled}
  150. mediaPlaybackRequiresUserAction={
  151. this.props.mediaPlaybackRequiresUserAction
  152. }
  153. allowUniversalAccessFromFileURLs={
  154. this.props.allowUniversalAccessFromFileURLs
  155. }
  156. mixedContentMode={this.props.mixedContentMode}
  157. saveFormDataDisabled={this.props.saveFormDataDisabled}
  158. urlPrefixesForDefaultIntent={this.props.urlPrefixesForDefaultIntent}
  159. {...nativeConfig.props}
  160. />
  161. );
  162. return (
  163. <View style={styles.container}>
  164. {webView}
  165. {otherView}
  166. </View>
  167. );
  168. }
  169. goForward = () => {
  170. UIManager.dispatchViewManagerCommand(
  171. this.getWebViewHandle(),
  172. UIManager.RNCWebView.Commands.goForward,
  173. null,
  174. );
  175. };
  176. goBack = () => {
  177. UIManager.dispatchViewManagerCommand(
  178. this.getWebViewHandle(),
  179. UIManager.RNCWebView.Commands.goBack,
  180. null,
  181. );
  182. };
  183. reload = () => {
  184. this.setState({
  185. viewState: WebViewState.LOADING,
  186. });
  187. UIManager.dispatchViewManagerCommand(
  188. this.getWebViewHandle(),
  189. UIManager.RNCWebView.Commands.reload,
  190. null,
  191. );
  192. };
  193. stopLoading = () => {
  194. UIManager.dispatchViewManagerCommand(
  195. this.getWebViewHandle(),
  196. UIManager.RNCWebView.Commands.stopLoading,
  197. null,
  198. );
  199. };
  200. postMessage = (data: string) => {
  201. UIManager.dispatchViewManagerCommand(
  202. this.getWebViewHandle(),
  203. UIManager.RNCWebView.Commands.postMessage,
  204. [String(data)],
  205. );
  206. };
  207. /**
  208. * Injects a javascript string into the referenced WebView. Deliberately does not
  209. * return a response because using eval() to return a response breaks this method
  210. * on pages with a Content Security Policy that disallows eval(). If you need that
  211. * functionality, look into postMessage/onMessage.
  212. */
  213. injectJavaScript = (data: string) => {
  214. UIManager.dispatchViewManagerCommand(
  215. this.getWebViewHandle(),
  216. UIManager.RNCWebView.Commands.injectJavaScript,
  217. [data],
  218. );
  219. };
  220. /**
  221. * We return an event with a bunch of fields including:
  222. * url, title, loading, canGoBack, canGoForward
  223. */
  224. updateNavigationState = (event: WebViewNavigationEvent) => {
  225. if (this.props.onNavigationStateChange) {
  226. this.props.onNavigationStateChange(event.nativeEvent);
  227. }
  228. };
  229. getWebViewHandle = () => {
  230. return ReactNative.findNodeHandle(this.webViewRef.current);
  231. };
  232. onLoadingStart = (event: WebViewNavigationEvent) => {
  233. const onLoadStart = this.props.onLoadStart;
  234. onLoadStart && onLoadStart(event);
  235. this.updateNavigationState(event);
  236. };
  237. onLoadingError = (event: WebViewErrorEvent) => {
  238. event.persist(); // persist this event because we need to store it
  239. const { onError, onLoadEnd } = this.props;
  240. onError && onError(event);
  241. onLoadEnd && onLoadEnd(event);
  242. console.warn('Encountered an error loading page', event.nativeEvent);
  243. this.setState({
  244. lastErrorEvent: event.nativeEvent,
  245. viewState: WebViewState.ERROR,
  246. });
  247. };
  248. onLoadingFinish = (event: WebViewNavigationEvent) => {
  249. const { onLoad, onLoadEnd } = this.props;
  250. onLoad && onLoad(event);
  251. onLoadEnd && onLoadEnd(event);
  252. this.setState({
  253. viewState: WebViewState.IDLE,
  254. });
  255. this.updateNavigationState(event);
  256. };
  257. onMessage = (event: WebViewMessageEvent) => {
  258. const { onMessage } = this.props;
  259. onMessage && onMessage(event);
  260. };
  261. onLoadingProgress = (event: WebViewProgressEvent) => {
  262. const { onLoadProgress } = this.props;
  263. onLoadProgress && onLoadProgress(event);
  264. };
  265. onShouldStartLoadWithRequestCallback = (
  266. shouldStart: boolean,
  267. url: string,
  268. ) => {
  269. if (shouldStart) {
  270. UIManager.dispatchViewManagerCommand(
  271. this.getWebViewHandle(),
  272. UIManager.RNCWebView.Commands.loadUrl,
  273. [String(url)],
  274. );
  275. }
  276. };
  277. }
  278. const RNCWebView = requireNativeComponent('RNCWebView');
  279. const styles = StyleSheet.create({
  280. container: {
  281. flex: 1,
  282. },
  283. hidden: {
  284. height: 0,
  285. flex: 0, // disable 'flex:1' when hiding a View
  286. },
  287. loadingView: {
  288. flex: 1,
  289. justifyContent: 'center',
  290. alignItems: 'center',
  291. },
  292. loadingProgressBar: {
  293. height: 20,
  294. },
  295. });
  296. module.exports = WebView;