Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Williams2019-12-06 18:44:04 +0000
committerEric Williams2019-12-11 17:11:45 +0000
commit823775bc9bc4982a48eb57242ce078c55550c85a (patch)
tree130a7985b4f5e8d1e4e9749dc0efb6182469b66a
parent2c83b4a71138033a6f3077c833fc4a02fc2039c8 (diff)
downloadeclipse.platform.swt-823775bc9bc4982a48eb57242ce078c55550c85a.tar.gz
eclipse.platform.swt-823775bc9bc4982a48eb57242ce078c55550c85a.tar.xz
eclipse.platform.swt-823775bc9bc4982a48eb57242ce078c55550c85a.zip
Bug 522181: [GTK][WebKit2] Port clearSessions(), getCookie() and setCookie() to WebKit2
Implement set/get cookie on WebKit2. clearSessions() has already been ported so no work is left to be done there. The API introduced in WebKit2.20 allows for asynchronous setting/getting of cookies. Because SWT expects a synchronous return, I've adapted the same approach to cookies as Leo Ufimtsev did for getText() -- we spin the display loop and wait for the callback to be triggered. This patch also allows for test_get_set_Cookies to finally be enabled on WebKit2. Tested on GTK3.24 and Fedora 31/30. No AllBrowserTests fail. Change-Id: I1136e0c4de332b276b256bd7a57a1fb4ba019da7 Signed-off-by: Eric Williams <ericwill@redhat.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk.c148
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.c9
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.h9
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java275
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebkitGDBus.java4
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java86
-rw-r--r--tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java6
7 files changed, 468 insertions, 69 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk.c b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk.c
index da0f52bf1e..dbfc187f84 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk.c
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk.c
@@ -696,6 +696,46 @@ JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1soup_1cookie_1free)
}
#endif
+#ifndef NO__1soup_1cookie_1get_1name
+JNIEXPORT jlong JNICALL WebKitGTK_NATIVE(_1soup_1cookie_1get_1name)
+ (JNIEnv *env, jclass that, jlong arg0)
+{
+ jlong rc = 0;
+ WebKitGTK_NATIVE_ENTER(env, that, _1soup_1cookie_1get_1name_FUNC);
+/*
+ rc = (jlong)soup_cookie_get_name(arg0);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, soup_cookie_get_name)
+ if (fp) {
+ rc = (jlong)((jlong (CALLING_CONVENTION*)(jlong))fp)(arg0);
+ }
+ }
+ WebKitGTK_NATIVE_EXIT(env, that, _1soup_1cookie_1get_1name_FUNC);
+ return rc;
+}
+#endif
+
+#ifndef NO__1soup_1cookie_1get_1value
+JNIEXPORT jlong JNICALL WebKitGTK_NATIVE(_1soup_1cookie_1get_1value)
+ (JNIEnv *env, jclass that, jlong arg0)
+{
+ jlong rc = 0;
+ WebKitGTK_NATIVE_ENTER(env, that, _1soup_1cookie_1get_1value_FUNC);
+/*
+ rc = (jlong)soup_cookie_get_value(arg0);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, soup_cookie_get_value)
+ if (fp) {
+ rc = (jlong)((jlong (CALLING_CONVENTION*)(jlong))fp)(arg0);
+ }
+ }
+ WebKitGTK_NATIVE_EXIT(env, that, _1soup_1cookie_1get_1value_FUNC);
+ return rc;
+}
+#endif
+
#ifndef NO__1soup_1cookie_1jar_1add_1cookie
JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1soup_1cookie_1jar_1add_1cookie)
(JNIEnv *env, jclass that, jlong arg0, jlong arg1)
@@ -1109,6 +1149,94 @@ JNIEXPORT jboolean JNICALL WebKitGTK_NATIVE(_1webkit_1authentication_1request_1i
}
#endif
+#ifndef NO__1webkit_1cookie_1manager_1add_1cookie
+JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1cookie_1manager_1add_1cookie)
+ (JNIEnv *env, jclass that, jlong arg0, jlong arg1, jlong arg2, jlong arg3, jlong arg4)
+{
+ WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1cookie_1manager_1add_1cookie_FUNC);
+/*
+ webkit_cookie_manager_add_cookie(arg0, arg1, arg2, arg3, arg4);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, webkit_cookie_manager_add_cookie)
+ if (fp) {
+ ((void (CALLING_CONVENTION*)(jlong, jlong, jlong, jlong, jlong))fp)(arg0, arg1, arg2, arg3, arg4);
+ }
+ }
+ WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1cookie_1manager_1add_1cookie_FUNC);
+}
+#endif
+
+#ifndef NO__1webkit_1cookie_1manager_1add_1cookie_1finish
+JNIEXPORT jboolean JNICALL WebKitGTK_NATIVE(_1webkit_1cookie_1manager_1add_1cookie_1finish)
+ (JNIEnv *env, jclass that, jlong arg0, jlong arg1, jlongArray arg2)
+{
+ jlong *lparg2=NULL;
+ jboolean rc = 0;
+ WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1cookie_1manager_1add_1cookie_1finish_FUNC);
+ if (arg2) if ((lparg2 = (*env)->GetLongArrayElements(env, arg2, NULL)) == NULL) goto fail;
+/*
+ rc = (jboolean)webkit_cookie_manager_add_cookie_finish(arg0, arg1, lparg2);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, webkit_cookie_manager_add_cookie_finish)
+ if (fp) {
+ rc = (jboolean)((jboolean (CALLING_CONVENTION*)(jlong, jlong, jlong *))fp)(arg0, arg1, lparg2);
+ }
+ }
+fail:
+ if (arg2 && lparg2) (*env)->ReleaseLongArrayElements(env, arg2, lparg2, 0);
+ WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1cookie_1manager_1add_1cookie_1finish_FUNC);
+ return rc;
+}
+#endif
+
+#ifndef NO__1webkit_1cookie_1manager_1get_1cookies
+JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1cookie_1manager_1get_1cookies)
+ (JNIEnv *env, jclass that, jlong arg0, jbyteArray arg1, jlong arg2, jlong arg3, jlong arg4)
+{
+ jbyte *lparg1=NULL;
+ WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1cookie_1manager_1get_1cookies_FUNC);
+ if (arg1) if ((lparg1 = (*env)->GetByteArrayElements(env, arg1, NULL)) == NULL) goto fail;
+/*
+ webkit_cookie_manager_get_cookies(arg0, lparg1, arg2, arg3, arg4);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, webkit_cookie_manager_get_cookies)
+ if (fp) {
+ ((void (CALLING_CONVENTION*)(jlong, jbyte *, jlong, jlong, jlong))fp)(arg0, lparg1, arg2, arg3, arg4);
+ }
+ }
+fail:
+ if (arg1 && lparg1) (*env)->ReleaseByteArrayElements(env, arg1, lparg1, 0);
+ WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1cookie_1manager_1get_1cookies_FUNC);
+}
+#endif
+
+#ifndef NO__1webkit_1cookie_1manager_1get_1cookies_1finish
+JNIEXPORT jlong JNICALL WebKitGTK_NATIVE(_1webkit_1cookie_1manager_1get_1cookies_1finish)
+ (JNIEnv *env, jclass that, jlong arg0, jlong arg1, jlongArray arg2)
+{
+ jlong *lparg2=NULL;
+ jlong rc = 0;
+ WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1cookie_1manager_1get_1cookies_1finish_FUNC);
+ if (arg2) if ((lparg2 = (*env)->GetLongArrayElements(env, arg2, NULL)) == NULL) goto fail;
+/*
+ rc = (jlong)webkit_cookie_manager_get_cookies_finish(arg0, arg1, lparg2);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, webkit_cookie_manager_get_cookies_finish)
+ if (fp) {
+ rc = (jlong)((jlong (CALLING_CONVENTION*)(jlong, jlong, jlong *))fp)(arg0, arg1, lparg2);
+ }
+ }
+fail:
+ if (arg2 && lparg2) (*env)->ReleaseLongArrayElements(env, arg2, lparg2, 0);
+ WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1cookie_1manager_1get_1cookies_1finish_FUNC);
+ return rc;
+}
+#endif
+
#ifndef NO__1webkit_1credential_1free
JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1credential_1free)
(JNIEnv *env, jclass that, jlong arg0)
@@ -2320,6 +2448,26 @@ fail:
}
#endif
+#ifndef NO__1webkit_1web_1context_1get_1cookie_1manager
+JNIEXPORT jlong JNICALL WebKitGTK_NATIVE(_1webkit_1web_1context_1get_1cookie_1manager)
+ (JNIEnv *env, jclass that, jlong arg0)
+{
+ jlong rc = 0;
+ WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1web_1context_1get_1cookie_1manager_FUNC);
+/*
+ rc = (jlong)webkit_web_context_get_cookie_manager(arg0);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, webkit_web_context_get_cookie_manager)
+ if (fp) {
+ rc = (jlong)((jlong (CALLING_CONVENTION*)(jlong))fp)(arg0);
+ }
+ }
+ WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1web_1context_1get_1cookie_1manager_FUNC);
+ return rc;
+}
+#endif
+
#ifndef NO__1webkit_1web_1context_1get_1default
JNIEXPORT jlong JNICALL WebKitGTK_NATIVE(_1webkit_1web_1context_1get_1default)
(JNIEnv *env, jclass that)
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.c b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.c
index 84ee7e587d..5989812d09 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.c
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.c
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2018 IBM Corporation and others. All rights reserved.
+ * Copyright (c) 2009, 2019 IBM Corporation and others. All rights reserved.
* The contents of this file are made available under the terms
* of the GNU Lesser General Public License (LGPL) Version 2.1 that
* accompanies this distribution (lgpl-v21.txt). The LGPL is also
@@ -56,6 +56,8 @@ char * WebKitGTK_nativeFunctionNames[] = {
"_1soup_1auth_1get_1host",
"_1soup_1auth_1get_1scheme_1name",
"_1soup_1cookie_1free",
+ "_1soup_1cookie_1get_1name",
+ "_1soup_1cookie_1get_1value",
"_1soup_1cookie_1jar_1add_1cookie",
"_1soup_1cookie_1jar_1all_1cookies",
"_1soup_1cookie_1jar_1delete_1cookie",
@@ -77,6 +79,10 @@ char * WebKitGTK_nativeFunctionNames[] = {
"_1webkit_1authentication_1request_1authenticate",
"_1webkit_1authentication_1request_1cancel",
"_1webkit_1authentication_1request_1is_1retry",
+ "_1webkit_1cookie_1manager_1add_1cookie",
+ "_1webkit_1cookie_1manager_1add_1cookie_1finish",
+ "_1webkit_1cookie_1manager_1get_1cookies",
+ "_1webkit_1cookie_1manager_1get_1cookies_1finish",
"_1webkit_1credential_1free",
"_1webkit_1credential_1new",
"_1webkit_1dom_1event_1target_1add_1event_1listener",
@@ -137,6 +143,7 @@ char * WebKitGTK_nativeFunctionNames[] = {
"_1webkit_1uri_1response_1get_1content_1length",
"_1webkit_1uri_1response_1get_1mime_1type",
"_1webkit_1web_1context_1allow_1tls_1certificate_1for_1host",
+ "_1webkit_1web_1context_1get_1cookie_1manager",
"_1webkit_1web_1context_1get_1default",
"_1webkit_1web_1context_1get_1type",
"_1webkit_1web_1context_1get_1website_1data_1manager",
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.h b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.h
index 22196ffafa..787ad71673 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.h
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.h
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2009, 2018 IBM Corporation and others. All rights reserved.
+ * Copyright (c) 2009, 2019 IBM Corporation and others. All rights reserved.
* The contents of this file are made available under the terms
* of the GNU Lesser General Public License (LGPL) Version 2.1 that
* accompanies this distribution (lgpl-v21.txt). The LGPL is also
@@ -66,6 +66,8 @@ typedef enum {
_1soup_1auth_1get_1host_FUNC,
_1soup_1auth_1get_1scheme_1name_FUNC,
_1soup_1cookie_1free_FUNC,
+ _1soup_1cookie_1get_1name_FUNC,
+ _1soup_1cookie_1get_1value_FUNC,
_1soup_1cookie_1jar_1add_1cookie_FUNC,
_1soup_1cookie_1jar_1all_1cookies_FUNC,
_1soup_1cookie_1jar_1delete_1cookie_FUNC,
@@ -87,6 +89,10 @@ typedef enum {
_1webkit_1authentication_1request_1authenticate_FUNC,
_1webkit_1authentication_1request_1cancel_FUNC,
_1webkit_1authentication_1request_1is_1retry_FUNC,
+ _1webkit_1cookie_1manager_1add_1cookie_FUNC,
+ _1webkit_1cookie_1manager_1add_1cookie_1finish_FUNC,
+ _1webkit_1cookie_1manager_1get_1cookies_FUNC,
+ _1webkit_1cookie_1manager_1get_1cookies_1finish_FUNC,
_1webkit_1credential_1free_FUNC,
_1webkit_1credential_1new_FUNC,
_1webkit_1dom_1event_1target_1add_1event_1listener_FUNC,
@@ -147,6 +153,7 @@ typedef enum {
_1webkit_1uri_1response_1get_1content_1length_FUNC,
_1webkit_1uri_1response_1get_1mime_1type_FUNC,
_1webkit_1web_1context_1allow_1tls_1certificate_1for_1host_FUNC,
+ _1webkit_1web_1context_1get_1cookie_1manager_FUNC,
_1webkit_1web_1context_1get_1default_FUNC,
_1webkit_1web_1context_1get_1type_FUNC,
_1webkit_1web_1context_1get_1website_1data_1manager_FUNC,
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
index 53b2a81f2f..0ccf9394b1 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java
@@ -307,16 +307,14 @@ class WebKit extends WebBrowser {
NativeClearSessions = () -> {
if (!WebKitGTK.LibraryLoaded) return;
-
if (WEBKIT2) {
if (WebKitGTK.webkit_get_minor_version() >= 16) {
- // TODO: webkit_website_data_manager_clear currently does not
- // support more fine grained removals. (I.e, session vs all cookies)
long context = WebKitGTK.webkit_web_context_get_default();
long manager = WebKitGTK.webkit_web_context_get_website_data_manager (context);
WebKitGTK.webkit_website_data_manager_clear(manager, WebKitGTK.WEBKIT_WEBSITE_DATA_COOKIES, 0, 0, 0, 0);
} else {
- System.err.println("SWT Webkit. Warning, clear cookies only supported on Webkitgtk version 2.16 and above. Your version is:" + internalGetWebKitVersionStr());
+ System.err.println("SWT WebKit: clear sessions only supported on WebKitGtk version 2.16 and above. "
+ + "Your version is: " + internalGetWebKitVersionStr());
}
} else {
long session = WebKitGTK.webkit_get_default_session ();
@@ -343,38 +341,39 @@ class WebKit extends WebBrowser {
NativeGetCookie = () -> {
if (!WebKitGTK.LibraryLoaded) return;
-
if (WEBKIT2) {
- // TODO - implement equivalent. Bug 522181
- // Currently 'webkit_get_default_session()' is a webkit1-only function.
- // If it's reached by webkit2, the whole JVM crashes. Better skip for now.
- return;
- }
-
- long session = WebKitGTK.webkit_get_default_session ();
- long type = WebKitGTK.soup_cookie_jar_get_type ();
- long jar = WebKitGTK.soup_session_get_feature (session, type);
- if (jar == 0) return;
- byte[] bytes = Converter.wcsToMbcs (CookieUrl, true);
- long uri = WebKitGTK.soup_uri_new (bytes);
- if (uri == 0) return;
- long cookies = WebKitGTK.soup_cookie_jar_get_cookies (jar, uri, 0);
- WebKitGTK.soup_uri_free (uri);
- if (cookies == 0) return;
- int length = C.strlen (cookies);
- bytes = new byte[length];
- C.memmove (bytes, cookies, length);
- OS.g_free (cookies);
- String allCookies = new String (Converter.mbcsToWcs (bytes));
- StringTokenizer tokenizer = new StringTokenizer (allCookies, ";"); //$NON-NLS-1$
- while (tokenizer.hasMoreTokens ()) {
- String cookie = tokenizer.nextToken ();
- int index = cookie.indexOf ('=');
- if (index != -1) {
- String name = cookie.substring (0, index).trim ();
- if (name.equals (CookieName)) {
- CookieValue = cookie.substring (index + 1).trim ();
- return;
+ if (WebKitGTK.webkit_get_minor_version() >= 20) {
+ CookieValue = Webkit2AsyncToSync.getCookie(CookieUrl, CookieName);
+ } else {
+ System.err.println("SWT WebKit: getCookie() only supported on WebKitGTK version 2.20 and above. "
+ + "Your version is: " + internalGetWebKitVersionStr());
+ }
+ } else {
+ long session = WebKitGTK.webkit_get_default_session ();
+ long type = WebKitGTK.soup_cookie_jar_get_type ();
+ long jar = WebKitGTK.soup_session_get_feature (session, type);
+ if (jar == 0) return;
+ byte[] bytes = Converter.wcsToMbcs (CookieUrl, true);
+ long uri = WebKitGTK.soup_uri_new (bytes);
+ if (uri == 0) return;
+ long cookies = WebKitGTK.soup_cookie_jar_get_cookies (jar, uri, 0);
+ WebKitGTK.soup_uri_free (uri);
+ if (cookies == 0) return;
+ int length = C.strlen (cookies);
+ bytes = new byte[length];
+ C.memmove (bytes, cookies, length);
+ OS.g_free (cookies);
+ String allCookies = new String (Converter.mbcsToWcs (bytes));
+ StringTokenizer tokenizer = new StringTokenizer (allCookies, ";"); //$NON-NLS-1$
+ while (tokenizer.hasMoreTokens ()) {
+ String cookie = tokenizer.nextToken ();
+ int index = cookie.indexOf ('=');
+ if (index != -1) {
+ String name = cookie.substring (0, index).trim ();
+ if (name.equals (CookieName)) {
+ CookieValue = cookie.substring (index + 1).trim ();
+ return;
+ }
}
}
}
@@ -382,35 +381,36 @@ class WebKit extends WebBrowser {
NativeSetCookie = () -> {
if (!WebKitGTK.LibraryLoaded) return;
-
if (WEBKIT2) {
- // TODO - implement equivalent. Bug 522181
- // Currently 'webkit_get_default_session()' is a webkit1-only function.
- // If it's reached by webkit2, the whole JVM crashes. Better skip for now.
- return;
- }
-
- long session = WebKitGTK.webkit_get_default_session ();
- long type = WebKitGTK.soup_cookie_jar_get_type ();
- long jar = WebKitGTK.soup_session_get_feature (session, type);
- if (jar == 0) {
- /* this happens if a navigation has not occurred yet */
- WebKitGTK.soup_session_add_feature_by_type (session, type);
- jar = WebKitGTK.soup_session_get_feature (session, type);
- }
- if (jar == 0) return;
- byte[] bytes = Converter.wcsToMbcs (CookieUrl, true);
- long uri = WebKitGTK.soup_uri_new (bytes);
- if (uri == 0) return;
- bytes = Converter.wcsToMbcs (CookieValue, true);
- long cookie = WebKitGTK.soup_cookie_parse (bytes, uri);
- if (cookie != 0) {
- WebKitGTK.soup_cookie_jar_add_cookie (jar, cookie);
- // the following line is intentionally commented
- // WebKitGTK.soup_cookie_free (cookie);
- CookieResult = true;
+ if (WebKitGTK.webkit_get_minor_version() >= 20) {
+ CookieResult = Webkit2AsyncToSync.setCookie(CookieUrl, CookieValue);
+ } else {
+ System.err.println("SWT WebKit: setCookie() only supported on WebKitGTK version 2.20 and above. "
+ + "Your version is: " + internalGetWebKitVersionStr());
+ }
+ } else {
+ long session = WebKitGTK.webkit_get_default_session ();
+ long type = WebKitGTK.soup_cookie_jar_get_type ();
+ long jar = WebKitGTK.soup_session_get_feature (session, type);
+ if (jar == 0) {
+ /* this happens if a navigation has not occurred yet */
+ WebKitGTK.soup_session_add_feature_by_type (session, type);
+ jar = WebKitGTK.soup_session_get_feature (session, type);
+ }
+ if (jar == 0) return;
+ byte[] bytes = Converter.wcsToMbcs (CookieUrl, true);
+ long uri = WebKitGTK.soup_uri_new (bytes);
+ if (uri == 0) return;
+ bytes = Converter.wcsToMbcs (CookieValue, true);
+ long cookie = WebKitGTK.soup_cookie_parse (bytes, uri);
+ if (cookie != 0) {
+ WebKitGTK.soup_cookie_jar_add_cookie (jar, cookie);
+ // the following line is intentionally commented
+ // WebKitGTK.soup_cookie_free (cookie);
+ CookieResult = true;
+ }
+ WebKitGTK.soup_uri_free (uri);
}
- WebKitGTK.soup_uri_free (uri);
};
if (NativePendingCookies != null) {
@@ -1117,6 +1117,12 @@ public void create (Composite parent, int style) {
WebKitGTK.memmove (classDefinitionPtr, jsClassDefinition, JSClassDefinition.sizeof);
ExternalClass = WebKitGTK.JSClassCreate (classDefinitionPtr);
+ } else {
+ /*
+ * Set this Browser instance to Webki2AsyncToSync in order for cookie
+ * functionality to work. See bug 522181.
+ */
+ Webkit2AsyncToSync.setCookieBrowser(browser);
}
byte [] bytes = Converter.wcsToMbcs ("POST", true); //$NON-NLS-1$
@@ -1663,15 +1669,25 @@ public boolean execute (String script) {
* The mechanism generates an ID for each callback and waits for that callback to complete.
*/
private static class Webkit2AsyncToSync {
-
+ /** We need a way to associate a Browser instance with this class for cookie functionality */
+ private static Browser cookieBrowser;
private static Callback runjavascript_callback;
private static Callback getText_callback;
+ private static Callback setCookie_callback;
+ private static Callback getCookie_callback;
+
static {
runjavascript_callback = new Callback(Webkit2AsyncToSync.class, "runjavascript_callback", void.class, new Type[] {long.class, long.class, long.class});
if (runjavascript_callback.getAddress() == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS);
getText_callback = new Callback(Webkit2AsyncToSync.class, "getText_callback", void.class, new Type[] {long.class, long.class, long.class});
if (getText_callback.getAddress() == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
+
+ setCookie_callback = new Callback(Webkit2AsyncToSync.class, "setCookie_callback", void.class, new Type[] {long.class, long.class, long.class});
+ if (setCookie_callback.getAddress() == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
+
+ getCookie_callback = new Callback(Webkit2AsyncToSync.class, "getCookie_callback", void.class, new Type[] {long.class, long.class, long.class});
+ if (getCookie_callback.getAddress() == 0) SWT.error(SWT.ERROR_NO_MORE_CALLBACKS);
}
/** Object used to return data from callback to original call */
@@ -1888,6 +1904,139 @@ private static class Webkit2AsyncToSync {
retObj.callbackFinished = true;
Display.getCurrent().wake();
}
+
+ /**
+ * Associates a Browser instance with this class, mainly so we can get its Display
+ * and check for disposal.
+ * @param toSet the Browser instance to set
+ */
+ static void setCookieBrowser (Browser toSet) {
+ if (toSet != null) cookieBrowser = toSet;
+ }
+
+ static boolean setCookie(String cookieUrl, String cookieValue) {
+ long context = WebKitGTK.webkit_web_context_get_default();
+ long cookieManager = WebKitGTK.webkit_web_context_get_cookie_manager(context);
+ byte[] bytes = Converter.wcsToMbcs (cookieUrl, true);
+ long uri = WebKitGTK.soup_uri_new (bytes);
+ if (uri == 0) {
+ System.err.println("SWT WebKit: SoupURI == 0 when setting cookie");
+ return false;
+ }
+ bytes = Converter.wcsToMbcs (cookieValue, true);
+ long soupCookie = WebKitGTK.soup_cookie_parse (bytes, uri);
+
+ if (nonBlockingEvaluate > 0) {
+ System.err.println("SWT Webkit: setCookie() called inside a synchronous callback, which can lead to a deadlock.\n"
+ + "Return value is false.");
+ return false;
+ }
+
+ Consumer<Integer> asyncFunc = (callbackID) -> WebKitGTK.webkit_cookie_manager_add_cookie(cookieManager, soupCookie, 0,
+ setCookie_callback.getAddress(), callbackID);
+ Webkit2AsyncReturnObj retObj = execAsyncAndWaitForReturn(cookieBrowser, asyncFunc, " setCookie() was called");
+
+ WebKitGTK.soup_uri_free (uri);
+
+ if (retObj.swtAsyncTimeout) {
+ return false;
+ } else {
+ return (Boolean) retObj.returnValue;
+ }
+ }
+
+ @SuppressWarnings("unused") // Callback only called only by C directly
+ private static void setCookie_callback(long cookieManager, long result, long user_data) {
+ int callbackID = (int) user_data;
+ Webkit2AsyncReturnObj retObj = CallBackMap.getObj(callbackID);
+
+ long [] error = new long [1];
+ retObj.returnValue = WebKitGTK.webkit_cookie_manager_add_cookie_finish(cookieManager, result, error);
+
+ if (error[0] != 0) {
+ long errorMessageC = OS.g_error_get_message(error[0]);
+ String errorMessageStr = Converter.cCharPtrToJavaString(errorMessageC, false);
+ System.err.println("SWT WebKit: error setting cookie: " + errorMessageStr);
+ OS.g_error_free(error[0]);
+ }
+
+ retObj.callbackFinished = true;
+ Display.getCurrent().wake();
+
+ }
+
+ static String getCookie(String cookieUrl, String cookieName) {
+ long context = WebKitGTK.webkit_web_context_get_default();
+ long cookieManager = WebKitGTK.webkit_web_context_get_cookie_manager(context);
+ byte[] uri = Converter.wcsToMbcs (cookieUrl, true);
+ if (nonBlockingEvaluate > 0) {
+ System.err.println("SWT Webkit: getCookie() called inside a synchronous callback, which can lead to a deadlock.\n"
+ + "Return value is an empty string '' instead of actual cookie value.");
+ return "";
+ }
+
+ /*
+ * We package the cookie name and callbackID into a GVariant which can be passed to the callback.
+ * The callbackID is necessary so we can find our way back to the correct Browser instance, and
+ * the cookie name is necessary as the field could have been modified by the time the callback
+ * triggers.
+ */
+ Consumer<Integer> asyncFunc = (callbackID) -> WebKitGTK.webkit_cookie_manager_get_cookies(cookieManager, uri, 0,
+ getCookie_callback.getAddress(), WebkitGDBus.convertJavaToGVariant(new Object [] {cookieName, callbackID}));
+ Webkit2AsyncReturnObj retObj = execAsyncAndWaitForReturn(cookieBrowser, asyncFunc, " getCookie() was called");
+
+ if (retObj.swtAsyncTimeout) {
+ return "SWT WEBKIT TIMEOUT ERROR";
+ } else {
+ return (String) retObj.returnValue;
+ }
+ }
+
+ @SuppressWarnings("unused") // Callback only called only by C directly
+ private static void getCookie_callback(long cookieManager, long result, long user_data) {
+ Object resultObject = WebkitGDBus.convertGVariantToJava(user_data);
+
+ // We are expecting a GVariant tuple, anything else means something went wrong
+ if (resultObject instanceof Object []) {
+ // Unpack callback ID and cookie name
+ Object [] nameAndId = (Object []) resultObject;
+ String cookieName = (String) nameAndId[0];
+ int callbackId = ((Number) nameAndId[1]).intValue();
+ Webkit2AsyncReturnObj retObj = CallBackMap.getObj(callbackId);
+
+ // Get GSList of cookies
+ long [] error = new long [1];
+ long cookieList = WebKitGTK.webkit_cookie_manager_get_cookies_finish(cookieManager, result, error);
+ if (error[0] != 0) {
+ long errorMessageC = OS.g_error_get_message(error[0]);
+ String errorMessageStr = Converter.cCharPtrToJavaString(errorMessageC, false);
+ System.err.println("SWT WebKit: error getting cookie: " + errorMessageStr);
+ OS.g_error_free(error[0]);
+ retObj.returnValue = (String) "";
+ }
+
+ int length = OS.g_slist_length (cookieList);
+ long current = cookieList;
+ for (int i = 0; i < length; i++) {
+ long soupCookie = OS.g_slist_data (current);
+ long soupName = WebKitGTK.soup_cookie_get_name(soupCookie);
+ String soupNameStr = Converter.cCharPtrToJavaString(soupName, false);
+ if (soupNameStr != null && soupNameStr.equals(cookieName)) {
+ long soupValue = WebKitGTK.soup_cookie_get_value(soupCookie);
+ retObj.returnValue = Converter.cCharPtrToJavaString(soupValue, false);
+ break;
+ }
+ current = OS.g_slist_next (current);
+ }
+ OS.g_slist_free (cookieList);
+
+ retObj.callbackFinished = true;
+ Display.getCurrent().wake();
+ } else {
+ System.err.println("SWT WebKit: something went wrong unpacking GVariant tuple for getCookie_callback");
+ }
+ }
+
/**
* You should check 'retObj.swtAsyncTimeout' after making a call to this.
*/
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebkitGDBus.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebkitGDBus.java
index b16d206756..3c39022fcd 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebkitGDBus.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebkitGDBus.java
@@ -512,7 +512,7 @@ class WebkitGDBus {
*
* @param gVariant a pointer to the native GVariant
*/
- private static Object convertGVariantToJava(long gVariant){
+ static Object convertGVariantToJava(long gVariant){
if (OS.g_variant_is_of_type(gVariant, OS.G_VARIANT_TYPE_BOOLEAN)){
return OS.g_variant_get_boolean(gVariant);
@@ -567,7 +567,7 @@ class WebkitGDBus {
*
* @return pointer GVariant *
*/
- private static long convertJavaToGVariant(Object javaObject) throws SWTException {
+ static long convertJavaToGVariant(Object javaObject) throws SWTException {
if (javaObject == null) {
return OS.g_variant_new_byte(WebkitGDBus.SWT_DBUS_MAGIC_NUMBER_NULL); // see: WebKitGTK.java 'TYPE NOTES'
diff --git a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java
index f1c624a8c1..58dc3af1bc 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java
@@ -570,6 +570,28 @@ public static final void soup_cookie_free (long cookie) {
}
/** @method flags=dynamic */
+public static final native long _soup_cookie_get_name (long cookie);
+public static final long soup_cookie_get_name (long cookie) {
+ lock.lock();
+ try {
+ return _soup_cookie_get_name (cookie);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/** @method flags=dynamic */
+public static final native long _soup_cookie_get_value (long cookie);
+public static final long soup_cookie_get_value (long cookie) {
+ lock.lock();
+ try {
+ return _soup_cookie_get_value (cookie);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/** @method flags=dynamic */
public static final native void _soup_cookie_jar_add_cookie (long jar, long cookie);
public static final void soup_cookie_jar_add_cookie (long jar, long cookie) {
lock.lock();
@@ -805,6 +827,58 @@ public static final boolean webkit_authentication_request_is_retry (long request
}
}
+/**
+ * @method flags=dynamic
+ */
+public static final native void _webkit_cookie_manager_add_cookie (long cookie_manager, long cookie, long cancellable, long cb, long user_data);
+public static final void webkit_cookie_manager_add_cookie (long cookie_manager, long cookie, long cancellable, long cb, long user_data) {
+ assert WEBKIT2 : Webkit2AssertMsg;
+ lock.lock();
+ try {
+ _webkit_cookie_manager_add_cookie (cookie_manager, cookie, cancellable, cb, user_data);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/** @method flags=dynamic */
+public static final native boolean _webkit_cookie_manager_add_cookie_finish (long cookie_manager, long result, long error []);
+public static final boolean webkit_cookie_manager_add_cookie_finish (long cookie_manager, long result, long error []) {
+ assert WEBKIT2 : Webkit2AssertMsg;
+ lock.lock();
+ try {
+ return _webkit_cookie_manager_add_cookie_finish (cookie_manager, result, error);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/**
+ * @method flags=dynamic
+ */
+public static final native void _webkit_cookie_manager_get_cookies (long cookie_manager, byte [] uri, long cancellable, long cb, long user_data);
+public static final void webkit_cookie_manager_get_cookies (long cookie_manager, byte [] uri, long cancellable, long cb, long user_data) {
+ assert WEBKIT2 : Webkit2AssertMsg;
+ lock.lock();
+ try {
+ _webkit_cookie_manager_get_cookies (cookie_manager, uri, cancellable, cb, user_data);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/** @method flags=dynamic */
+public static final native long _webkit_cookie_manager_get_cookies_finish (long cookie_manager, long result, long error []);
+public static final long webkit_cookie_manager_get_cookies_finish (long cookie_manager, long result, long error []) {
+ assert WEBKIT2 : Webkit2AssertMsg;
+ lock.lock();
+ try {
+ return _webkit_cookie_manager_get_cookies_finish (cookie_manager, result, error);
+ } finally {
+ lock.unlock();
+ }
+}
+
/** @method flags=dynamic */
public static final native void _webkit_credential_free (long credential);
public static final void webkit_credential_free (long credential) {
@@ -1434,6 +1508,18 @@ public static final long webkit_web_context_get_default () {
}
/** @method flags=dynamic */
+public static final native long _webkit_web_context_get_cookie_manager (long context);
+public static final long webkit_web_context_get_cookie_manager (long context) {
+ assert WEBKIT2 : Webkit2AssertMsg;
+ lock.lock();
+ try {
+ return _webkit_web_context_get_cookie_manager (context);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/** @method flags=dynamic */
public static final native long _webkit_web_context_get_website_data_manager (long context);
public static final long webkit_web_context_get_website_data_manager (long context) {
assert WEBKIT2 : Webkit2AssertMsg; // Since 2.10
diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java
index 0eb1205187..df3c83bb18 100644
--- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java
+++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java
@@ -246,8 +246,10 @@ private String toCookieEvalString (String key) {
@Test
public void test_get_set_Cookies() {
- assumeFalse("Not implemented on webkit2 yet. Bug 522181.", isWebkit2);
-
+ if (isWebkit2) {
+ // set/get cookies will only work for WebKit2.20+
+ assumeTrue(webkitGtkVersionInts[1] >= 20);
+ }
final AtomicBoolean loaded = new AtomicBoolean(false);
browser.addProgressListener(ProgressListener.completedAdapter(event -> loaded.set(true)));

Back to the top