説明なし

WebView.android.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. androidHardwareAccelerationDisabled: false,
  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. androidHardwareAccelerationDisabled={this.props.androidHardwareAccelerationDisabled}
  134. thirdPartyCookiesEnabled={this.props.thirdPartyCookiesEnabled}
  135. domStorageEnabled={this.props.domStorageEnabled}
  136. messagingEnabled={typeof this.props.onMessage === 'function'}
  137. onMessage={this.onMessage}
  138. overScrollMode={this.props.overScrollMode}
  139. contentInset={this.props.contentInset}
  140. automaticallyAdjustContentInsets={
  141. this.props.automaticallyAdjustContentInsets
  142. }
  143. onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
  144. onContentSizeChange={this.props.onContentSizeChange}
  145. onLoadingStart={this.onLoadingStart}
  146. onLoadingFinish={this.onLoadingFinish}
  147. onLoadingError={this.onLoadingError}
  148. onLoadingProgress={this.onLoadingProgress}
  149. testID={this.props.testID}
  150. geolocationEnabled={this.props.geolocationEnabled}
  151. mediaPlaybackRequiresUserAction={
  152. this.props.mediaPlaybackRequiresUserAction
  153. }
  154. allowUniversalAccessFromFileURLs={
  155. this.props.allowUniversalAccessFromFileURLs
  156. }
  157. mixedContentMode={this.props.mixedContentMode}
  158. saveFormDataDisabled={this.props.saveFormDataDisabled}
  159. urlPrefixesForDefaultIntent={this.props.urlPrefixesForDefaultIntent}
  160. {...nativeConfig.props}
  161. />
  162. );
  163. return (
  164. <View style={styles.container}>
  165. {webView}
  166. {otherView}
  167. </View>
  168. );
  169. }
  170. goForward = () => {
  171. UIManager.dispatchViewManagerCommand(
  172. this.getWebViewHandle(),
  173. UIManager.RNCWebView.Commands.goForward,
  174. null,
  175. );
  176. };
  177. goBack = () => {
  178. UIManager.dispatchViewManagerCommand(
  179. this.getWebViewHandle(),
  180. UIManager.RNCWebView.Commands.goBack,
  181. null,
  182. );
  183. };
  184. reload = () => {
  185. this.setState({
  186. viewState: WebViewState.LOADING,
  187. });
  188. UIManager.dispatchViewManagerCommand(
  189. this.getWebViewHandle(),
  190. UIManager.RNCWebView.Commands.reload,
  191. null,
  192. );
  193. };
  194. stopLoading = () => {
  195. UIManager.dispatchViewManagerCommand(
  196. this.getWebViewHandle(),
  197. UIManager.RNCWebView.Commands.stopLoading,
  198. null,
  199. );
  200. };
  201. postMessage = (data: string) => {
  202. UIManager.dispatchViewManagerCommand(
  203. this.getWebViewHandle(),
  204. UIManager.RNCWebView.Commands.postMessage,
  205. [String(data)],
  206. );
  207. };
  208. /**
  209. * Injects a javascript string into the referenced WebView. Deliberately does not
  210. * return a response because using eval() to return a response breaks this method
  211. * on pages with a Content Security Policy that disallows eval(). If you need that
  212. * functionality, look into postMessage/onMessage.
  213. */
  214. injectJavaScript = (data: string) => {
  215. UIManager.dispatchViewManagerCommand(
  216. this.getWebViewHandle(),
  217. UIManager.RNCWebView.Commands.injectJavaScript,
  218. [data],
  219. );
  220. };
  221. /**
  222. * We return an event with a bunch of fields including:
  223. * url, title, loading, canGoBack, canGoForward
  224. */
  225. updateNavigationState = (event: WebViewNavigationEvent) => {
  226. if (this.props.onNavigationStateChange) {
  227. this.props.onNavigationStateChange(event.nativeEvent);
  228. }
  229. };
  230. getWebViewHandle = () => {
  231. return ReactNative.findNodeHandle(this.webViewRef.current);
  232. };
  233. onLoadingStart = (event: WebViewNavigationEvent) => {
  234. const onLoadStart = this.props.onLoadStart;
  235. onLoadStart && onLoadStart(event);
  236. this.updateNavigationState(event);
  237. };
  238. onLoadingError = (event: WebViewErrorEvent) => {
  239. event.persist(); // persist this event because we need to store it
  240. const { onError, onLoadEnd } = this.props;
  241. onError && onError(event);
  242. onLoadEnd && onLoadEnd(event);
  243. console.warn('Encountered an error loading page', event.nativeEvent);
  244. this.setState({
  245. lastErrorEvent: event.nativeEvent,
  246. viewState: WebViewState.ERROR,
  247. });
  248. };
  249. onLoadingFinish = (event: WebViewNavigationEvent) => {
  250. const { onLoad, onLoadEnd } = this.props;
  251. onLoad && onLoad(event);
  252. onLoadEnd && onLoadEnd(event);
  253. this.setState({
  254. viewState: WebViewState.IDLE,
  255. });
  256. this.updateNavigationState(event);
  257. };
  258. onMessage = (event: WebViewMessageEvent) => {
  259. const { onMessage } = this.props;
  260. onMessage && onMessage(event);
  261. };
  262. onLoadingProgress = (event: WebViewProgressEvent) => {
  263. const { onLoadProgress } = this.props;
  264. onLoadProgress && onLoadProgress(event);
  265. };
  266. onShouldStartLoadWithRequestCallback = (
  267. shouldStart: boolean,
  268. url: string,
  269. ) => {
  270. if (shouldStart) {
  271. UIManager.dispatchViewManagerCommand(
  272. this.getWebViewHandle(),
  273. UIManager.RNCWebView.Commands.loadUrl,
  274. [String(url)],
  275. );
  276. }
  277. };
  278. }
  279. const RNCWebView = requireNativeComponent('RNCWebView');
  280. const styles = StyleSheet.create({
  281. container: {
  282. flex: 1,
  283. },
  284. hidden: {
  285. height: 0,
  286. flex: 0, // disable 'flex:1' when hiding a View
  287. },
  288. loadingView: {
  289. flex: 1,
  290. justifyContent: 'center',
  291. alignItems: 'center',
  292. },
  293. loadingProgressBar: {
  294. height: 20,
  295. },
  296. });
  297. module.exports = WebView;