From 780724790539809bb7e1363b04839a794b756063 Mon Sep 17 00:00:00 2001 From: Saleel Salam Date: Sun, 28 May 2017 21:28:45 -0700 Subject: [PATCH] =?UTF-8?q?Add=20'thirdPartyCookiesEnabled'=20prop=20on=20?= =?UTF-8?q?WebView=20to=20enable=20third=20party=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: …cookies on Android Lollipop or later versions. Third party cookies in WebView are [disabled by default](https://developer.android.com/reference/android/webkit/CookieManager.html#setAcceptFileSchemeCookies(boolean)) on Android Lollipop or later versions. This prevented users from logging in by using _Login by Facebook_ method (in redirect mode) in Android Webview. This PR exposes a prop `thirdPartyCookiesEnabled` which will enable third party cookies in Android Webview. This setting is ignored on versions below Android Lollipop and on iOS as third party cookies are enabled in them by default. Appropriate documentation was added in code and they were reflected in the website. Closes https://github.com/facebook/react-native/pull/14013 Differential Revision: D5145059 Pulled By: shergin fbshipit-source-id: 67bcb3a497a9c6f6db1d12e8d16197d2facd157e --- Libraries/Components/WebView/WebView.android.js | 9 +++++++++ Libraries/Components/WebView/WebView.ios.js | 8 ++++++++ .../react/views/webview/ReactWebViewManager.java | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/Libraries/Components/WebView/WebView.android.js b/Libraries/Components/WebView/WebView.android.js index cd05617c8..1735adf63 100644 --- a/Libraries/Components/WebView/WebView.android.js +++ b/Libraries/Components/WebView/WebView.android.js @@ -120,6 +120,13 @@ class WebView extends React.Component { */ javaScriptEnabled: PropTypes.bool, + /** + * Used on Android Lollipop and above only, third party cookies are enabled + * by default for WebView on Android Kitkat and below and on iOS + * @platform android + */ + thirdPartyCookiesEnabled: PropTypes.bool, + /** * Used on Android only, controls whether DOM Storage is enabled or not * @platform android @@ -192,6 +199,7 @@ class WebView extends React.Component { static defaultProps = { javaScriptEnabled : true, + thirdPartyCookiesEnabled: true, scalesPageToFit: true, saveFormDataDisabled: false }; @@ -253,6 +261,7 @@ class WebView extends React.Component { injectedJavaScript={this.props.injectedJavaScript} userAgent={this.props.userAgent} javaScriptEnabled={this.props.javaScriptEnabled} + thirdPartyCookiesEnabled={this.props.thirdPartyCookiesEnabled} domStorageEnabled={this.props.domStorageEnabled} messagingEnabled={typeof this.props.onMessage === 'function'} onMessage={this.onMessage} diff --git a/Libraries/Components/WebView/WebView.ios.js b/Libraries/Components/WebView/WebView.ios.js index 21f4397c0..485af3956 100644 --- a/Libraries/Components/WebView/WebView.ios.js +++ b/Libraries/Components/WebView/WebView.ios.js @@ -285,6 +285,14 @@ class WebView extends React.Component { */ javaScriptEnabled: PropTypes.bool, + /** + * Boolean value to enable third party cookies in the `WebView`. Used on + * Android Lollipop and above only as third party cookies are enabled by + * default on Android Kitkat and below and on iOS. The default value is `true`. + * @platform android + */ + thirdPartyCookiesEnabled: PropTypes.bool, + /** * Boolean value to control whether DOM Storage is enabled. Used only in * Android. 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 a22824294..584751579 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 @@ -32,6 +32,7 @@ import android.webkit.WebViewClient; import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebSettings; +import android.webkit.CookieManager; import com.facebook.common.logging.FLog; import com.facebook.react.common.ReactConstants; @@ -370,6 +371,13 @@ public class ReactWebViewManager extends SimpleViewManager { view.getSettings().setJavaScriptEnabled(enabled); } + @ReactProp(name = "thirdPartyCookiesEnabled") + public void setThirdPartyCookiesEnabled(WebView view, boolean enabled) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + CookieManager.getInstance().setAcceptThirdPartyCookies(view, enabled); + } + } + @ReactProp(name = "scalesPageToFit") public void setScalesPageToFit(WebView view, boolean enabled) { view.getSettings().setUseWideViewPort(!enabled);