summaryrefslogtreecommitdiffstats
path: root/src/jar
diff options
context:
space:
mode:
authorChristian Strømme <christian.stromme@qt.io>2022-04-22 15:32:11 +0200
committerChristian Strømme <christian.stromme@qt.io>2022-05-09 12:38:03 +0200
commit3702c4ccc19155840b3058631acf5cc015561df0 (patch)
treea692b6cc686a7c13d11363909033d94ee6d13dcd /src/jar
parenta9a707de9279622d2db170ce76d44603e6674cf2 (diff)
Fix crash due to missing native functions for the cookie APIs
The cookie API that was added in 317e13cb2d97 missed the registration and implementation for the native java functions needed for notifying when a cookie got added/removed. Fixes: QTBUG-102801 Pick-to: 6.3 Change-Id: I7594f75abba17b6ff1843d5349af085f90aae9b1 Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'src/jar')
-rw-r--r--src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java85
1 files changed, 66 insertions, 19 deletions
diff --git a/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java b/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java
index 11aa686..63f6bb8 100644
--- a/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java
+++ b/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java
@@ -61,6 +61,7 @@ import java.util.concurrent.Semaphore;
import java.lang.reflect.Method;
import android.os.Build;
import java.util.concurrent.TimeUnit;
+import java.time.format.DateTimeFormatter;
public class QtAndroidWebViewController
{
@@ -95,7 +96,7 @@ public class QtAndroidWebViewController
private native void c_onRunJavaScriptResult(long id, long callbackId, String result);
private native void c_onReceivedError(long id, int errorCode, String description, String url);
private native void c_onCookieAdded(long id, boolean result, String domain, String name);
- private native void c_onCookiesRemoved(long id, boolean result);
+ private native void c_onCookieRemoved(long id, boolean result, String domain, String name);
// We need to block the UI thread in some cases, if it takes to long we should timeout before
// ANR kicks in... Usually the hard limit is set to 10s and if exceed that then we're in trouble.
@@ -528,41 +529,87 @@ public class QtAndroidWebViewController
});
}
- public void setCookie(final String url, final String cookieString)
+ private void setCookieImp(final String url, final String cookieString, ValueCallback<Boolean> callback)
{
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
try {
- cookieManager.setCookie(url, cookieString, new ValueCallback<Boolean>() {
- @Override
- public void onReceiveValue(Boolean value) {
- try {
- c_onCookieAdded(m_id, value, url, cookieString.split("=")[0]);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
+ cookieManager.setCookie(url, cookieString, callback);
} catch (Exception e) {
e.printStackTrace();
}
}
- public void removeCookies() {
- try {
- CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
+ public void setCookie(final String url, final String cookieString)
+ {
+ setCookieImp(url, cookieString, new ValueCallback<Boolean>() {
+ @Override
+ public void onReceiveValue(Boolean value) {
+ try {
+ c_onCookieAdded(m_id, value, url, cookieString.split("=")[0]);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+
+ private boolean hasValidCookie(final String url, final String cookieString)
+ {
+ CookieManager cookieManager = CookieManager.getInstance();
+ cookieManager.removeExpiredCookie();
+ boolean cookieFound = false;
+
+ final String domainCookie = cookieManager.getCookie(url);
+
+ String found = null;
+ if (domainCookie != null) {
+ String cookies[] = domainCookie.split(";");
+ for (final String cookie : cookies) {
+ if (cookie.startsWith(cookieString)) {
+ found = cookie;
+ // Cookie is "cleared" so not considered valid.
+ cookieFound = !cookie.endsWith("=");
+ break;
+ }
+ }
+ }
+
+ return cookieFound;
+ }
+
+ private String getExpireString()
+ {
+ return "expires=\"Thu, 1 Jan 1970 00:00:00 GMT\"";
+ }
+
+ public void removeCookie(final String url, final String cookieString)
+ {
+ // We need to work with what we have
+ // 1. Check if there's cookies for the url
+ final boolean hadCookie = hasValidCookie(url, cookieString);
+ if (hadCookie) {
+ // 2. Tag the string with an expire tag so it will be purged
+ final String removeCookieString = cookieString + ";" + getExpireString();
+ setCookieImp(url, removeCookieString, new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean value) {
try {
- c_onCookiesRemoved(m_id, value);
- }
- catch (Exception e) {
+ // 3. Verify that the cookie was indeed removed
+ final boolean removed = (hadCookie && !hasValidCookie(url, cookieString));
+ c_onCookieRemoved(m_id, removed, url, cookieString.split("=")[0]);
+ } catch (Exception e) {
e.printStackTrace();
}
}
});
+ }
+ }
+
+ public void removeCookies() {
+ try {
+ CookieManager.getInstance().removeAllCookies(null);
} catch (Exception e) {
e.printStackTrace();
}