From 9b9501b0fef312da72586299e22d329f00ac5e20 Mon Sep 17 00:00:00 2001 From: Leo Ufimtsev Date: Tue, 24 Jan 2017 12:23:26 -0500 Subject: Bug 508217 Implement webkit2 support for browser function (Part 1: JavaScript to call Java) This implements Browser.function() on webkit2. Current implementation allows javascript to reach java, but java doesn't provide a return vaulue in the callback. For detailed architecture changes, see: Webkit2JavaCallback() inside WebKit.java. In short, in webkit1 an 'external' object was registered and callJava was a property of this object. In webkit2, 'external' object is no longer used and instead a callback/signal is registered/connected. With this patch, the following jUnits pass: test_BrowserFunction_callback test_BrowserFunction_callback_with_integer test_BrowserFunction_callback_with_boolean test_BrowserFunction_callback_with_String test_BrowserFunction_callback_with_multipleValues testBrowser4. The following does not: test_BrowserFunction_callback_with_javaReturningInt Patch set 4: - I noticed that after a page re-load, webkit2 custom functions did not work. This is because the page-reload mechanism has changed (Bg:510972) but in the ported webkit2 mechanism functions were not re-registered). - Added code to re-register custom functions after page reload. Patch set 7: - Removed 'protected' modifier. - Moved comments around. Change-Id: I94549ed306e0095c735d18389b55a36923c5ec08 Signed-off-by: Leo Ufimtsev --- .../common/org/eclipse/swt/browser/WebBrowser.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'bundles/org.eclipse.swt/Eclipse SWT Browser') diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java index 5f79e68379..df7f31745e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java @@ -346,8 +346,10 @@ public void createFunction (BrowserFunction function) { functionBuffer.append (ERROR_ID.length ()); functionBuffer.append (")); throw error;} return result;};"); //$NON-NLS-1$ - StringBuffer buffer = new StringBuffer ("if (!window.callJava) {window.callJava = function callJava(index, token, args) {"); //$NON-NLS-1$ - buffer.append ("return external.callJava(index,token,args);}};"); //$NON-NLS-1$ + String javaCallDeclaration = getJavaCallDeclaration(); + + StringBuffer buffer = new StringBuffer (); //$NON-NLS-1$ + buffer.append (javaCallDeclaration); //$NON-NLS-1$ if (function.top) { buffer.append (functionBuffer.toString ()); } @@ -375,6 +377,18 @@ public void createFunction (BrowserFunction function) { execute (function.functionString); } +/** + * Designed to be overriden. + * @return javaScrit code that defines the 'callJava' syntax for javascript. + */ +String getJavaCallDeclaration() { + return "if (!window.callJava) {\n" + + " window.callJava = function callJava(index, token, args) {\n" + + " return external.callJava(index,token,args);\n" + + " }\n" + + "};\n"; +} + void deregisterFunction (BrowserFunction function) { functions.remove (function.index); } @@ -396,6 +410,9 @@ public Object evaluate (String script, boolean trusted) throws SWTException { } public Object evaluate (String script) throws SWTException { + // Developer note: + // Webkit1 & Mozilla use this mechanism. + // Webkit2 uses a different mechanism. See WebKit:evaluate(); BrowserFunction function = new EvaluateFunction (browser, ""); // $NON-NLS-1$ int index = getNextFunctionIndex (); function.index = index; -- cgit v1.2.3