Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoland Grunberg2017-12-01 20:01:17 +0000
committerLeo Ufimtsev2017-12-12 19:23:53 +0000
commitd8fca1df5fd4712c60aca821d48bec6f9359f1a4 (patch)
treecd628bab93072665a9e8b4c2f496b9fe9623d621
parente4ea137643762dd3e38d38276d52c49d8a5b8aec (diff)
downloadeclipse.platform.swt-d8fca1df5fd4712c60aca821d48bec6f9359f1a4.tar.gz
eclipse.platform.swt-d8fca1df5fd4712c60aca821d48bec6f9359f1a4.tar.xz
eclipse.platform.swt-d8fca1df5fd4712c60aca821d48bec6f9359f1a4.zip
Bug 527738 [Webkit2] implement setUrl(...POST..) support. (snippet 330)
When setURL contains POST data, bypass the Webkit2 API and use the Java API (HttpURLConnection) to perform the request. Set the URL globally so that when calling setText with the response string, any relative paths can be resolved using the base URL. This implementation does not currently support Webkit configured proxies and there may be issues with detecting various signals/listeners that operate on request/response objects given that these are handled by the Java side. Tested against Snippet330. --- Leo's modifications: Patchest 9 notes: - Added g_bytes_unref() to prevent memory leakage. - Making g_bytes_* dynamic otherwise we'll get jvm crash on gtk2. (g_byte* introduced in glib 2.32, but gtk2 only has glib 2.26(ish)). - Move data type conversion logic into one place. (html converted where it's used) - Reduced size of if statment, it was scary. - Add scope to code for clarity. - Don't touch this.postData & this.headers, they're only used by webkit1. Webkit2 shouldn't touch what it doesn't use. - Added if (Webkit1) guards around variables only used by webkit1. - Refactoring. - Avoid passing null value to webkit_web_view_load_bytes(.) if connection is not HTML. - Instead of passing empty array to encoding type, pass array with only a single 0. - Modified Snippet330.java to include some more examples for better testing and learning. - Verified: - against (modified) Snippet330 - jUnit tests. (w1/w2) ... Patchset 15: - Added counter support to setText(..) as a long setURI() could override a setText() call. Change-Id: Ic93747083af6af9933af3ca52149a1fb6e894d2c Signed-off-by: Roland Grunberg <rgrunber@redhat.com> Signed-off-by: Leo Ufimtsev <lufimtse@redhat.com>
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c42
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_custom.h3
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.c2
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.h2
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java35
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk.c28
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.c1
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/library/webkitgtk_stats.h1
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java164
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/internal/webkit/WebKitGTK.java12
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet330.java110
11 files changed, 341 insertions, 59 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c
index 3fe853f20c..4ce6a8cc0d 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os.c
@@ -1914,6 +1914,48 @@ JNIEXPORT jboolean JNICALL OS_NATIVE(_1g_1app_1info_1supports_1uris)
}
#endif
+#ifndef NO__1g_1bytes_1new
+JNIEXPORT jintLong JNICALL OS_NATIVE(_1g_1bytes_1new)
+ (JNIEnv *env, jclass that, jbyteArray arg0, jintLong arg1)
+{
+ jbyte *lparg0=NULL;
+ jintLong rc = 0;
+ OS_NATIVE_ENTER(env, that, _1g_1bytes_1new_FUNC);
+ if (arg0) if ((lparg0 = (*env)->GetByteArrayElements(env, arg0, NULL)) == NULL) goto fail;
+/*
+ rc = (jintLong)g_bytes_new((gconstpointer)lparg0, (gsize)arg1);
+*/
+ {
+ OS_LOAD_FUNCTION(fp, g_bytes_new)
+ if (fp) {
+ rc = (jintLong)((jintLong (CALLING_CONVENTION*)(gconstpointer, gsize))fp)((gconstpointer)lparg0, (gsize)arg1);
+ }
+ }
+fail:
+ if (arg0 && lparg0) (*env)->ReleaseByteArrayElements(env, arg0, lparg0, 0);
+ OS_NATIVE_EXIT(env, that, _1g_1bytes_1new_FUNC);
+ return rc;
+}
+#endif
+
+#ifndef NO__1g_1bytes_1unref
+JNIEXPORT void JNICALL OS_NATIVE(_1g_1bytes_1unref)
+ (JNIEnv *env, jclass that, jintLong arg0)
+{
+ OS_NATIVE_ENTER(env, that, _1g_1bytes_1unref_FUNC);
+/*
+ g_bytes_unref((GBytes *)arg0);
+*/
+ {
+ OS_LOAD_FUNCTION(fp, g_bytes_unref)
+ if (fp) {
+ ((void (CALLING_CONVENTION*)(GBytes *))fp)((GBytes *)arg0);
+ }
+ }
+ OS_NATIVE_EXIT(env, that, _1g_1bytes_1unref_FUNC);
+}
+#endif
+
#ifndef NO__1g_1cclosure_1new
JNIEXPORT jintLong JNICALL OS_NATIVE(_1g_1cclosure_1new)
(JNIEnv *env, jclass that, jintLong arg0, jintLong arg1, jintLong arg2)
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_custom.h b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_custom.h
index 168a1abb96..b4e99759aa 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_custom.h
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_custom.h
@@ -302,6 +302,9 @@
#define gtk_drag_set_icon_surface_LIB LIB_GTK
#define gtk_accel_label_set_accel_LIB LIB_GTK
#define gtk_drag_begin_with_coordinates_LIB LIB_GTK
+#define g_bytes_new_LIB LIB_GTK
+#define g_bytes_unref_LIB LIB_GTK
+
#ifndef g_thread_supported
#define g_thread_supported() 0
#endif
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.c b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.c
index abba27976f..7f6fc0abaf 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.c
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.c
@@ -193,6 +193,8 @@ char * OS_nativeFunctionNames[] = {
"_1g_1app_1info_1launch_1default_1for_1uri",
"_1g_1app_1info_1should_1show",
"_1g_1app_1info_1supports_1uris",
+ "_1g_1bytes_1new",
+ "_1g_1bytes_1unref",
"_1g_1cclosure_1new",
"_1g_1closure_1ref",
"_1g_1closure_1sink",
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.h b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.h
index 609ba771a8..0ec4e3ad8a 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.h
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/library/os_stats.h
@@ -203,6 +203,8 @@ typedef enum {
_1g_1app_1info_1launch_1default_1for_1uri_FUNC,
_1g_1app_1info_1should_1show_FUNC,
_1g_1app_1info_1supports_1uris_FUNC,
+ _1g_1bytes_1new_FUNC,
+ _1g_1bytes_1unref_FUNC,
_1g_1cclosure_1new_FUNC,
_1g_1closure_1ref_FUNC,
_1g_1closure_1sink_FUNC,
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java
index 79cc73579d..9fca585520 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java
@@ -2719,6 +2719,41 @@ public static final void g_object_unref(long /*int*/ object) {
lock.unlock();
}
}
+
+
+/**
+ * @method flags=dynamic
+ * @param data cast=(gconstpointer)
+ * @param size cast=(gsize)
+ */
+public static final native long /*int*/ _g_bytes_new (byte [] data, long /*int*/ size);
+public static final long /*int*/ g_bytes_new (byte [] data, long /*int*/ size) {
+ assert GTK3;
+ // Since glib 2.32. (gtk2.24 = glib2.28)
+ lock.lock();
+ try {
+ return _g_bytes_new (data, size);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/**
+ * @method flags=dynamic
+ * @param gBytes cast=(GBytes *)
+ */
+public static final native void _g_bytes_unref (long /*int*/ gBytes);
+public static final void g_bytes_unref (long /*int*/ gBytes) {
+ // Since glib 2.32. (gtk2.24 = glib2.28)
+ assert GTK3;
+ lock.lock();
+ try {
+ _g_bytes_unref (gBytes);
+ } finally {
+ lock.unlock();
+ }
+}
+
/** @param string cast=(const gchar *),flags=no_out */
public static final native int _g_quark_from_string(byte[] string);
public static final int g_quark_from_string(byte[] string) {
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 124811f541..1f408c62dd 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
@@ -3494,6 +3494,34 @@ JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1web_1view_1go_1forward)
}
#endif
+#ifndef NO__1webkit_1web_1view_1load_1bytes
+JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1web_1view_1load_1bytes)
+ (JNIEnv *env, jclass that, jintLong arg0, jintLong arg1, jbyteArray arg2, jbyteArray arg3, jbyteArray arg4)
+{
+ jbyte *lparg2=NULL;
+ jbyte *lparg3=NULL;
+ jbyte *lparg4=NULL;
+ WebKitGTK_NATIVE_ENTER(env, that, _1webkit_1web_1view_1load_1bytes_FUNC);
+ if (arg2) if ((lparg2 = (*env)->GetByteArrayElements(env, arg2, NULL)) == NULL) goto fail;
+ if (arg3) if ((lparg3 = (*env)->GetByteArrayElements(env, arg3, NULL)) == NULL) goto fail;
+ if (arg4) if ((lparg4 = (*env)->GetByteArrayElements(env, arg4, NULL)) == NULL) goto fail;
+/*
+ webkit_web_view_load_bytes(arg0, arg1, lparg2, lparg3, lparg4);
+*/
+ {
+ WebKitGTK_LOAD_FUNCTION(fp, webkit_web_view_load_bytes)
+ if (fp) {
+ ((void (CALLING_CONVENTION*)(jintLong, jintLong, jbyte *, jbyte *, jbyte *))fp)(arg0, arg1, lparg2, lparg3, lparg4);
+ }
+ }
+fail:
+ if (arg4 && lparg4) (*env)->ReleaseByteArrayElements(env, arg4, lparg4, 0);
+ if (arg3 && lparg3) (*env)->ReleaseByteArrayElements(env, arg3, lparg3, 0);
+ if (arg2 && lparg2) (*env)->ReleaseByteArrayElements(env, arg2, lparg2, 0);
+ WebKitGTK_NATIVE_EXIT(env, that, _1webkit_1web_1view_1load_1bytes_FUNC);
+}
+#endif
+
#ifndef NO__1webkit_1web_1view_1load_1html
JNIEXPORT void JNICALL WebKitGTK_NATIVE(_1webkit_1web_1view_1load_1html)
(JNIEnv *env, jclass that, jintLong arg0, jbyteArray arg1, jbyteArray arg2)
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 6f083a9b02..f8eca540d2 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
@@ -194,6 +194,7 @@ char * WebKitGTK_nativeFunctionNames[] = {
"_1webkit_1web_1view_1get_1window_1properties",
"_1webkit_1web_1view_1go_1back",
"_1webkit_1web_1view_1go_1forward",
+ "_1webkit_1web_1view_1load_1bytes",
"_1webkit_1web_1view_1load_1html",
"_1webkit_1web_1view_1load_1request",
"_1webkit_1web_1view_1load_1string",
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 dae029f52c..fa6882df24 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
@@ -204,6 +204,7 @@ typedef enum {
_1webkit_1web_1view_1get_1window_1properties_FUNC,
_1webkit_1web_1view_1go_1back_FUNC,
_1webkit_1web_1view_1go_1forward_FUNC,
+ _1webkit_1web_1view_1load_1bytes_FUNC,
_1webkit_1web_1view_1load_1html_FUNC,
_1webkit_1web_1view_1load_1request_FUNC,
_1webkit_1web_1view_1load_1string_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 e7be2f2cd7..1f8c0f5602 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
@@ -21,6 +21,7 @@ import java.net.*;
import java.nio.charset.*;
import java.time.*;
import java.util.*;
+import java.util.concurrent.atomic.*;
import java.util.function.*;
import org.eclipse.swt.*;
@@ -39,12 +40,15 @@ class WebKit extends WebBrowser {
long /*int*/ webViewData;
int failureCount, lastKeyCode, lastCharCode;
- String postData;
- String[] headers;
- boolean ignoreDispose, loadingText, untrustedText;
- byte[] htmlBytes;
+
+ String postData; // Webkit1 only.
+ String[] headers; // Webkit1 only.
+ byte[] htmlBytes; // Webkit1 only.
+ boolean loadingText, untrustedText; // Webkit1 only.
BrowserFunction eventFunction; //Webkit1 only.
+ boolean ignoreDispose; // Webkit1 & Webkit2.
+
/**
* Timeout used for javascript execution / deadlock detection.
* Loosely based on the 10s limit commonly found in browsers.
@@ -66,6 +70,9 @@ class WebKit extends WebBrowser {
static boolean bug522733FirstInstanceCreated = false; //Webkit2 workaround for Bug 522733
+ /** Part of workaround in Bug 527738. Prevent old request overring newer request */
+ static AtomicInteger w2_bug527738LastRequestCounter = new AtomicInteger(); // Webkit 2 only (Bug 527738)
+
/**
* Webkit2: In a few situations, evaluate() should not wait for it's asynchronous callback to finish.
* This is to avoid deadlocks, see Bug 512001.<br>
@@ -2248,9 +2255,11 @@ void onDispose (Event e) {
C.free (webViewData);
}
- postData = null;
- headers = null;
- htmlBytes = null;
+ if (WEBKIT1) {
+ postData = null;
+ headers = null;
+ htmlBytes = null;
+ }
}
void onResize (Event e) {
@@ -2390,24 +2399,27 @@ public void refresh () {
@Override
public boolean setText (String html, boolean trusted) {
/* convert the String containing HTML to an array of bytes with UTF-8 data */
- byte[] bytes = (html + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
+ byte[] html_bytes = (html + '\0').getBytes (StandardCharsets.UTF_8); //$NON-NLS-1$
/*
* If this.htmlBytes is not null then the about:blank page is already being loaded,
* so no navigate is required. Just set the html that is to be shown.
*/
- boolean blankLoading = htmlBytes != null;
- htmlBytes = bytes;
- untrustedText = !trusted;
+ boolean blankLoading = htmlBytes != null; // Webkit1 only.
+ if (WEBKIT1) {
+ this.htmlBytes = html_bytes;
+ untrustedText = !trusted;
+ }
if (WEBKIT2) {
+ w2_bug527738LastRequestCounter.incrementAndGet();
byte[] uriBytes;
- if (untrustedText) {
+ if (!trusted) {
uriBytes = Converter.wcsToMbcs (ABOUT_BLANK, true);
} else {
uriBytes = Converter.wcsToMbcs (URI_FILEROOT, true);
}
- WebKitGTK.webkit_web_view_load_html (webView, htmlBytes, uriBytes);
+ WebKitGTK.webkit_web_view_load_html (webView, html_bytes, uriBytes);
} else {
if (blankLoading) return true;
@@ -2420,9 +2432,13 @@ public boolean setText (String html, boolean trusted) {
@Override
public boolean setUrl (String url, String postData, String[] headers) {
- this.postData = postData;
- this.headers = headers;
-
+ if (WEBKIT1) {
+ this.postData = postData;
+ this.headers = headers;
+ }
+ if (WEBKIT2) {
+ w2_bug527738LastRequestCounter.incrementAndGet();
+ }
/*
* WebKitGTK attempts to open the exact url string that is passed to it and
* will not infer a protocol if it's not specified. Detect the case of an
@@ -2475,19 +2491,127 @@ public boolean setUrl (String url, String postData, String[] headers) {
byte[] uriBytes = Converter.wcsToMbcs (url, true);
- if (WEBKIT2 && headers != null){
+ if (WEBKIT2 && postData==null && headers != null) {
long /*int*/ request = WebKitGTK.webkit_uri_request_new (uriBytes);
long /*int*/ requestHeaders = WebKitGTK.webkit_uri_request_get_http_headers (request);
if (requestHeaders != 0) {
addRequestHeaders(requestHeaders, headers);
}
WebKitGTK.webkit_web_view_load_request (webView, request);
-
+ OS.g_object_set (settings, WebKitGTK.user_agent, 0, 0);
return true;
}
- WebKitGTK.webkit_web_view_load_uri (webView, uriBytes);
- OS.g_object_set (settings, WebKitGTK.user_agent, 0, 0);
+ // Bug 527738
+ // Webkit2 doesn't have api to set url with data. (2.18). While we wait for them to implement,
+ // this workaround uses java to query a server and then manually populate webkit with content.
+ // This should be version guarded and replaced with proper functions once webkit2 has implemented api.
+ if (WEBKIT2 && postData != null) {
+ final String base_url = url;
+
+ // Use Webkit User-Agent
+ long /*int*/ [] user_agent_str_ptr = new long /*int*/ [1];
+ OS.g_object_get (settings, WebKitGTK.user_agent, user_agent_str_ptr, 0);
+ final String userAgent = Converter.cCharPtrToJavaString(user_agent_str_ptr[0], true);
+ final int lastRequest = w2_bug527738LastRequestCounter.incrementAndGet(); // Webkit 2 only
+ Thread send_request = new Thread(() -> {
+ String html = null;
+ String mime_type = null;
+ String encoding_type = null;
+ try {
+ URL base = new URL(base_url);
+ URLConnection url_conn = base.openConnection();
+ if (url_conn instanceof HttpURLConnection) {
+ HttpURLConnection conn = (HttpURLConnection) url_conn;
+
+ { // Configure connection.
+ conn.setRequestMethod("POST"); //$NON-NLS-1$
+
+ // Use Webkit Accept
+ conn.setRequestProperty( "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); //$NON-NLS-1$ $NON-NLS-2$
+
+ conn.setRequestProperty("User-Agent", userAgent); //$NON-NLS-1$
+ conn.setDoOutput(true); // because default value is false
+
+ // Set headers
+ if (headers != null) {
+ for (String header : headers) {
+ int index = header.indexOf(':');
+ if (index > 0) {
+ String key = header.substring(0, index).trim();
+ String value = header.substring(index + 1).trim();
+ conn.setRequestProperty(key, value);
+ }
+ }
+ }
+ }
+
+ { // Query server
+ try (OutputStream out = conn.getOutputStream()) {
+ out.write(postData.getBytes());
+ }
+
+ StringBuffer response = new StringBuffer();
+ try (BufferedReader buff = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
+ char [] cbuff = new char[4096];
+ while (buff.read(cbuff, 0, cbuff.length) > 0) {
+ response.append(new String(cbuff));
+ Arrays.fill(cbuff, '\0');
+ }
+ }
+ html = response.toString();
+ }
+
+ { // Extract result meta data
+ // Get Media Type from Content-Type
+ String content_type = conn.getContentType();
+ int paramaterSeparatorIndex = content_type.indexOf(';');
+ mime_type = paramaterSeparatorIndex > 0 ? content_type.substring(0, paramaterSeparatorIndex) : content_type;
+
+ // Get Encoding if defined
+ if (content_type.indexOf(';') > 0) {
+ String [] attrs = content_type.split(";");
+ for (String attr : attrs) {
+ int i = attr.indexOf('=');
+ if (i > 0) {
+ String key = attr.substring(0, i).trim();
+ String value = attr.substring(i + 1).trim();
+ if ("charset".equalsIgnoreCase(key)) { //$NON-NLS-1$
+ encoding_type = value;
+ }
+ }
+ }
+ }
+ }
+ }
+ } catch (IOException e) { // MalformedURLException is an IOException also.
+ html = e.getMessage();
+ } finally {
+ if (html != null && lastRequest == w2_bug527738LastRequestCounter.get()) {
+ final String final_html = html;
+ final String final_mime_type = mime_type;
+ final String final_encoding_type = encoding_type;
+ Display.getDefault().syncExec(() -> {
+ byte [] html_bytes = Converter.wcsToMbcs(final_html, false);
+ byte [] mime_type_bytes = final_mime_type != null ? Converter.javaStringToCString(final_mime_type) : Converter.javaStringToCString("text/plain");
+ byte [] encoding_bytes = final_encoding_type != null ? Converter.wcsToMbcs(final_encoding_type, true) : new byte [] {0};
+ long /*int*/ gByte = OS.g_bytes_new(html_bytes, html_bytes.length);
+ WebKitGTK.webkit_web_view_load_bytes (webView, gByte, mime_type_bytes, encoding_bytes, uriBytes);
+ OS.g_bytes_unref (gByte); // as per glib/tests/keyfile:test_bytes()..
+ OS.g_object_set (settings, WebKitGTK.user_agent, 0, 0);
+ });
+ }
+ }
+ });
+ send_request.start();
+ } else {
+ WebKitGTK.webkit_web_view_load_uri (webView, uriBytes);
+ }
+
+ // Handle when !(WEBKIT2 && postData != null)
+ if (WEBKIT1 || (WEBKIT2 && postData == null)) {
+ OS.g_object_set (settings, WebKitGTK.user_agent, 0, 0);
+ }
return true;
}
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 c2eb48549e..bf24d29ea0 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
@@ -1904,6 +1904,18 @@ public static final void webkit_web_view_load_html (long /*int*/ web_view, byte[
}
/** @method flags=dynamic */
+public static final native void _webkit_web_view_load_bytes (long /*int*/ web_view, long /*int*/ bytes, byte [] mime_type, byte [] encoding, byte [] base_uri);
+public static final void webkit_web_view_load_bytes (long /*int*/ web_view, long /*int*/ bytes, byte [] mime_type, byte [] encoding, byte [] base_uri) {
+ assert WEBKIT1 || WEBKIT2;
+ lock.lock();
+ try {
+ _webkit_web_view_load_bytes (web_view, bytes, mime_type, encoding, base_uri);
+ } finally {
+ lock.unlock();
+ }
+}
+
+/** @method flags=dynamic */
public static final native void _webkit_web_view_load_string (long /*int*/ web_view, byte[] content, byte[] mime_type, byte[] encoding, byte[] base_uri);
public static final void webkit_web_view_load_string (long /*int*/ web_view, byte[] content, byte[] mime_type, byte[] encoding, byte[] base_uri) {
assert WEBKIT1 : Webkit1AssertMsg;
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet330.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet330.java
index 0084cb602b..5cca50fef0 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet330.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet330.java
@@ -25,48 +25,80 @@ import org.eclipse.swt.widgets.*;
public class Snippet330 {
-public static void main(String [] args) {
- Display display = new Display();
- Shell shell = new Shell(display);
- shell.setLayout(new GridLayout(2, true));
+ static int BUTTONS_PER_ROW = 1;
- final Browser browser;
- try {
- browser = new Browser(shell, SWT.NONE);
- } catch (SWTError e) {
- System.out.println("Could not instantiate Browser: " + e.getMessage());
- display.dispose();
- return;
- }
- GridData data = new GridData(GridData.FILL_BOTH);
- data.horizontalSpan = 2;
- browser.setLayoutData(data);
+ public static void main(String[] args) {
+
+ Display display = new Display();
+ Shell shell = new Shell(display);
+ shell.setLayout(new GridLayout(BUTTONS_PER_ROW, true));
+
+ final Browser browser;
+ try {
+ browser = new Browser(shell, SWT.NONE);
+ } catch (SWTError e) {
+ System.out.println("Could not instantiate Browser: " + e.getMessage());
+ display.dispose();
+ return;
+ }
+ GridData data = new GridData(GridData.FILL_BOTH);
+ data.horizontalSpan = BUTTONS_PER_ROW;
+ browser.setLayoutData(data);
+
+ {
+ Button setHeaders = new Button(shell, SWT.PUSH);
+ setHeaders.setText("Send custom headers");
+ setHeaders.addListener(SWT.Selection,event ->
+ browser.setUrl("http://www.ericgiguere.com/tools/http-header-viewer.html", null,
+ new String[] { "User-agent: SWT Browser", "Custom-header: this is just a demo" }));
+ }
- Button headersButton = new Button(shell, SWT.PUSH);
- headersButton.setText("Send custom headers");
- data = new GridData();
- data.horizontalAlignment = GridData.FILL;
- headersButton.setLayoutData(data);
- headersButton.addListener(SWT.Selection, event -> browser.setUrl(
- "http://www.ericgiguere.com/tools/http-header-viewer.html",
- null,
- new String[] {"User-agent: SWT Browser","Custom-header: this is just a demo"}));
- Button postDataButton = new Button(shell, SWT.PUSH);
- postDataButton.setText("Send post data");
- data = new GridData();
- data.horizontalAlignment = GridData.FILL;
- postDataButton.setLayoutData(data);
- postDataButton.addListener(SWT.Selection, event -> browser.setUrl(
- "https://bugs.eclipse.org/bugs/buglist.cgi",
- "emailassigned_to1=1&bug_severity=enhancement&bug_status=NEW&email1=platform-swt-inbox&emailtype1=substring",
- null));
+ {
+ Button postTextPlain = new Button(shell, SWT.PUSH);
+ postTextPlain.setText("Post data as 'text/plain'");
+ postTextPlain.addListener(SWT.Selection, event -> browser.setUrl("http://httpbin.org/post",
+ "Plain text passed as postData", new String[] { "content-type: text/plain" }));
+ }
- shell.setBounds(10,10,600,600);
- shell.open();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch()) display.sleep();
+ {
+ Button postTextPlainUtf8 = new Button(shell, SWT.PUSH);
+ postTextPlainUtf8.setText("Post data as 'text/plain' and specify encoding.");
+ postTextPlainUtf8.addListener(SWT.Selection, event -> browser.setUrl("http://httpbin.org/post",
+ "Plain text passed as postData", new String[] { "content-type: text/plain; charset=UTF-8" }));
+ }
+
+ {
+ Button postUrlEncoded = new Button(shell, SWT.PUSH);
+ postUrlEncoded.setText("Post data with url encoding key=value");
+ postUrlEncoded.addListener(SWT.Selection, event -> browser.setUrl("http://httpbin.org/post", "MyKey1=MyValue1&MyKey2=MyValue2",
+ new String[] { "content-type: application/x-www-form-urlencoded" }));
+ }
+
+ {
+ Button postDataBugzilla = new Button(shell, SWT.PUSH);
+ postDataBugzilla.setText("Send post data to bugzilla. url encoding is used as default");
+ postDataBugzilla.addListener(SWT.Selection, event -> browser.setUrl(
+ "https://bugs.eclipse.org/bugs/buglist.cgi",
+ "emailassigned_to1=1&bug_severity=enhancement&bug_status=NEW&email1=platform-swt-inbox&emailtype1=substring",
+ null));
+ }
+
+ {
+ Button postDataBugzillaLongQuery = new Button(shell, SWT.PUSH);
+ postDataBugzillaLongQuery.setText("Send post data to bugzilla. Very slow response");
+ postDataBugzillaLongQuery.addListener(SWT.Selection, event -> browser.setUrl(
+ "https://bugs.eclipse.org/bugs/buglist.cgi",
+ "emailassigned_to1=1&bug_severity=enhancement&bug_status=NEW",
+ null));
+ }
+
+ shell.setSize(1000, 1000);
+ shell.open();
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch())
+ display.sleep();
+ }
+ display.dispose();
}
- display.dispose();
-}
} \ No newline at end of file

Back to the top