From 36eb69ecd0e87bc91cc14b572df39528d4def31f Mon Sep 17 00:00:00 2001 From: Prabakar Marimuthu Date: Wed, 8 Mar 2017 06:37:31 -0800 Subject: [PATCH] Added option to set mixed content mode in android webview Summary: PR for option to set mixed content mode in Webview(Android) for issue #8460 Closes https://github.com/facebook/react-native/pull/12314 Differential Revision: D4663084 Pulled By: lacker fbshipit-source-id: 0e40ea463739166311ddcb7887ff6d0289369637 --- Libraries/Components/WebView/WebView.android.js | 17 +++++++++++++++++ Libraries/Components/WebView/WebView.ios.js | 16 ++++++++++++++++ .../views/webview/ReactWebViewManager.java | 14 ++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Libraries/Components/WebView/WebView.android.js b/Libraries/Components/WebView/WebView.android.js index 170f14240..4de8be62d 100644 --- a/Libraries/Components/WebView/WebView.android.js +++ b/Libraries/Components/WebView/WebView.android.js @@ -166,6 +166,22 @@ class WebView extends React.Component { * executed immediately as JavaScript. */ injectJavaScript: PropTypes.func, + + /** + * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin. + * + * Possible values for `mixedContentMode` are: + * + * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin. + * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure. + * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content. + * @platform android + */ + mixedContentMode: PropTypes.oneOf([ + 'never', + 'always', + 'compatibility' + ]), }; static defaultProps = { @@ -242,6 +258,7 @@ class WebView extends React.Component { testID={this.props.testID} mediaPlaybackRequiresUserAction={this.props.mediaPlaybackRequiresUserAction} allowUniversalAccessFromFileURLs={this.props.allowUniversalAccessFromFileURLs} + mixedContentMode={this.props.mixedContentMode} />; return ( diff --git a/Libraries/Components/WebView/WebView.ios.js b/Libraries/Components/WebView/WebView.ios.js index a6bcc5949..96488b2c3 100644 --- a/Libraries/Components/WebView/WebView.ios.js +++ b/Libraries/Components/WebView/WebView.ios.js @@ -340,6 +340,22 @@ class WebView extends React.Component { * executed immediately as JavaScript. */ injectJavaScript: PropTypes.func, + + /** + * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin. + * + * Possible values for `mixedContentMode` are: + * + * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin. + * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure. + * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content. + * @platform android + */ + mixedContentMode: PropTypes.oneOf([ + 'never', + 'always', + 'compatibility' + ]), }; state = { diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java index bc18f86b0..ec3107f20 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java @@ -31,6 +31,7 @@ import android.webkit.WebView; import android.webkit.WebViewClient; import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; +import android.webkit.WebSettings; import com.facebook.common.logging.FLog; import com.facebook.react.common.ReactConstants; @@ -476,6 +477,19 @@ public class ReactWebViewManager extends SimpleViewManager { } } + @ReactProp(name = "mixedContentMode") + public void setMixedContentMode(WebView view, @Nullable String mixedContentMode) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + if (mixedContentMode == null || "never".equals(mixedContentMode)) { + view.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW); + } else if ("always".equals(mixedContentMode)) { + view.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); + } else if ("compatibility".equals(mixedContentMode)) { + view.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); + } + } + } + @Override protected void addEventEmitters(ThemedReactContext reactContext, WebView view) { // Do not register default touch emitter and let WebView implementation handle touches