summaryrefslogtreecommitdiffstats
path: root/bundles
diff options
context:
space:
mode:
authorGrant Gayed <ggayed>2008-12-04 20:33:36 +0000
committerGrant Gayed <ggayed>2008-12-04 20:33:36 +0000
commit828bb598ffea029296304120bac0896dc4445d73 (patch)
treef0c8c51186b2c2c5b7e3630c22244fa4026df691 /bundles
parent754da3e12d3ae9fd2c85bf06f23ede42c6f49859 (diff)
downloadeclipse.platform.swt-828bb598ffea029296304120bac0896dc4445d73.tar.gz
eclipse.platform.swt-828bb598ffea029296304120bac0896dc4445d73.tar.xz
eclipse.platform.swt-828bb598ffea029296304120bac0896dc4445d73.zip
257614 - add API to evaluate JS expression
Diffstat (limited to 'bundles')
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java9
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/BrowserFunction.java8
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/WebBrowser.java147
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java20
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java11
5 files changed, 143 insertions, 52 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java
index b949251ab1..ff745fd1ff 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/Browser.java
@@ -408,6 +408,15 @@ public boolean execute (String script) {
}
/**
+ * @since 3.5
+ */
+public Object evaluate (String script) throws SWTException {
+ checkWidget();
+ if (script == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
+ return webBrowser.evaluate (script);
+}
+
+/**
* Navigate to the next session history item.
*
* @return <code>true</code> if the operation was successful and <code>false</code> otherwise
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/BrowserFunction.java b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/BrowserFunction.java
index 59a63532fd..3af46cddc7 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/BrowserFunction.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Browser/common/org/eclipse/swt/browser/BrowserFunction.java
@@ -25,13 +25,17 @@ public class BrowserFunction {
*
*/
public BrowserFunction (Browser browser, String name) {
+ this (browser, name, true);
+}
+
+BrowserFunction (Browser browser, String name, boolean create) {
super ();
if (browser == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
if (name == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
if (browser.isDisposed ()) SWT.error (SWT.ERROR_WIDGET_DISPOSED);
this.browser = browser;
this.name = name;
- browser.webBrowser.addFunction (this);
+ if (create) browser.webBrowser.createFunction (this);
}
/**
@@ -43,7 +47,7 @@ public void dispose () {
void dispose (boolean remove) {
if (index < 0) return;
- if (remove) browser.webBrowser.removeFunction (this);
+ if (remove) browser.webBrowser.destroyFunction (this);
browser = null;
name = functionString = null;
index = -1;
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 b9b488126c..4133253119 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
@@ -12,7 +12,7 @@ package org.eclipse.swt.browser;
import java.util.*;
-import org.eclipse.swt.SWT;
+import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
abstract class WebBrowser {
@@ -26,8 +26,10 @@ abstract class WebBrowser {
TitleListener[] titleListeners = new TitleListener[0];
VisibilityWindowListener[] visibilityWindowListeners = new VisibilityWindowListener[0];
int nextFunctionIndex = 1;
+ Object evaluateResult;
static final String ERROR_ID = "org.eclipse.swt.browser.error"; // $NON-NLS-1$
+ static final String EXECUTE_ID = "SWTExecuteTemporaryFunction"; // $NON-NLS-1$
static Runnable MozillaClearSessions;
static Runnable NativeClearSessions;
@@ -167,6 +169,24 @@ abstract class WebBrowser {
{189, '-'},
};
+public class EvaluateFunction extends BrowserFunction {
+ public EvaluateFunction (Browser browser, String name) {
+ super (browser, name, false);
+ }
+ public Object function (Object[] arguments) {
+ if (arguments[0] instanceof String) {
+ String string = (String)arguments[0];
+ if (string.startsWith (ERROR_ID)) {
+ String errorString = string.substring (ERROR_ID.length () + 1);
+ evaluateResult = new SWTException (SWT.ERROR_FAILED_EVALUATE, errorString);
+ return null;
+ }
+ }
+ evaluateResult = arguments[0];
+ return null;
+ }
+}
+
public void addCloseWindowListener (CloseWindowListener listener) {
CloseWindowListener[] newCloseWindowListeners = new CloseWindowListener[closeWindowListeners.length + 1];
System.arraycopy(closeWindowListeners, 0, newCloseWindowListeners, 0, closeWindowListeners.length);
@@ -174,40 +194,6 @@ public void addCloseWindowListener (CloseWindowListener listener) {
closeWindowListeners[closeWindowListeners.length - 1] = listener;
}
-public void addFunction (BrowserFunction function) {
- /*
- * If an existing function with the same name is found then
- * remove it so that it is not recreated on subsequent pages
- * (the new function overwrites the old one).
- */
- Enumeration keys = functions.keys ();
- while (keys.hasMoreElements ()) {
- Object key = keys.nextElement ();
- BrowserFunction current = (BrowserFunction)functions.get (key);
- if (current.name.equals (function.name)) {
- functions.remove (key);
- break;
- }
- }
-
- function.index = getNextFunctionIndex ();
- functions.put (new Integer (function.index), function);
-
- StringBuffer buffer = new StringBuffer ("window."); //$NON-NLS-1$
- buffer.append (function.name);
- buffer.append (" = function "); //$NON-NLS-1$
- buffer.append (function.name);
- buffer.append ("() {var result = window.external.callJava("); //$NON-NLS-1$
- buffer.append (function.index);
- buffer.append (",Array.prototype.slice.call(arguments)); if (typeof result == 'string' && result.indexOf('"); //$NON-NLS-1$
- buffer.append (ERROR_ID);
- buffer.append ("') == 0) {var error = new Error(result.substring("); //$NON-NLS-1$
- buffer.append (ERROR_ID.length () + 1);
- buffer.append (")); throw error;} return result;}"); //$NON-NLS-1$
- function.functionString = buffer.toString ();
- execute (function.functionString);
-}
-
public void addLocationListener (LocationListener listener) {
LocationListener[] newLocationListeners = new LocationListener[locationListeners.length + 1];
System.arraycopy(locationListeners, 0, newLocationListeners, 0, locationListeners.length);
@@ -259,8 +245,90 @@ public static void clearSessions () {
public abstract void create (Composite parent, int style);
+public void createFunction (BrowserFunction function) {
+ /*
+ * If an existing function with the same name is found then
+ * remove it so that it is not recreated on subsequent pages
+ * (the new function overwrites the old one).
+ */
+ Enumeration keys = functions.keys ();
+ while (keys.hasMoreElements ()) {
+ Object key = keys.nextElement ();
+ BrowserFunction current = (BrowserFunction)functions.get (key);
+ if (current.name.equals (function.name)) {
+ functions.remove (key);
+ break;
+ }
+ }
+
+ function.index = getNextFunctionIndex ();
+ registerFunction (function);
+
+ StringBuffer buffer = new StringBuffer ("window."); //$NON-NLS-1$
+ buffer.append (function.name);
+ buffer.append (" = function "); //$NON-NLS-1$
+ buffer.append (function.name);
+ buffer.append ("() {var result = window.external.callJava("); //$NON-NLS-1$
+ buffer.append (function.index);
+ buffer.append (",Array.prototype.slice.call(arguments)); if (typeof result == 'string' && result.indexOf('"); //$NON-NLS-1$
+ buffer.append (ERROR_ID);
+ buffer.append ("') == 0) {var error = new Error(result.substring("); //$NON-NLS-1$
+ buffer.append (ERROR_ID.length () + 1);
+ buffer.append (")); throw error;} return result;}"); //$NON-NLS-1$
+ function.functionString = buffer.toString ();
+ if (!execute (function.functionString)) {
+ deregisterFunction (function);
+ }
+}
+
+void deregisterFunction (BrowserFunction function) {
+ functions.remove (new Integer (function.index));
+}
+
+public void destroyFunction (BrowserFunction function) {
+ execute (getDeleteFunctionString (function.name));
+ deregisterFunction (function);
+}
+
public abstract boolean execute (String script);
+public Object evaluate (String script) throws SWTException {
+ BrowserFunction function = new EvaluateFunction (browser, ""); // $NON-NLS-1$
+ int index = getNextFunctionIndex ();
+ function.index = index;
+ registerFunction (function);
+ String functionName = EXECUTE_ID + index;
+
+ StringBuffer buffer = new StringBuffer ("window."); // $NON-NLS-1$
+ buffer.append (functionName);
+ buffer.append (" = function "); // $NON-NLS-1$
+ buffer.append (functionName);
+ buffer.append ("() {"); // $NON-NLS-1$
+ buffer.append (script);
+ buffer.append ("};"); // $NON-NLS-1$
+ if (!execute (buffer.toString ())) {
+ deregisterFunction (function);
+ return null;
+ }
+ buffer = new StringBuffer ("try {var result = "); // $NON-NLS-1$
+ buffer.append (functionName);
+ buffer.append ("(); window.external.callJava("); // $NON-NLS-1$
+ buffer.append (index);
+ buffer.append (", [result]);} catch (e) {window.external.callJava("); // $NON-NLS-1$
+ buffer.append (index);
+ buffer.append (", ['"); // $NON-NLS-1$
+ buffer.append (ERROR_ID);
+ buffer.append (":' + e.message]);}"); // $NON-NLS-1$
+ execute (buffer.toString ());
+ execute (getDeleteFunctionString (functionName));
+ deregisterFunction (function);
+
+ Object result = evaluateResult;
+ evaluateResult = null;
+ if (result instanceof SWTException) throw (SWTException)result;
+ return result;
+}
+
public abstract boolean forward ();
public abstract String getBrowserType ();
@@ -291,6 +359,10 @@ public abstract boolean isForwardEnabled ();
public abstract void refresh ();
+void registerFunction (BrowserFunction function) {
+ functions.put (new Integer (function.index), function);
+}
+
public void removeCloseWindowListener (CloseWindowListener listener) {
if (closeWindowListeners.length == 0) return;
int index = -1;
@@ -311,11 +383,6 @@ public void removeCloseWindowListener (CloseWindowListener listener) {
closeWindowListeners = newCloseWindowListeners;
}
-public void removeFunction (BrowserFunction function) {
- execute (getDeleteFunctionString (function.name));
- functions.remove (new Integer (function.index));
-}
-
public void removeLocationListener (LocationListener listener) {
if (locationListeners.length == 0) return;
int index = -1;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java b/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java
index d93f6f524c..7a4656dc93 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Mozilla/common/org/eclipse/swt/browser/Mozilla.java
@@ -1450,6 +1450,11 @@ void createCOMInterfaces () {
};
}
+void deregisterFunction (BrowserFunction function) {
+ super.deregisterFunction (function);
+ AllFunctions.remove (new Integer (function.index));
+}
+
void disposeCOMInterfaces () {
if (supports != null) {
supports.dispose ();
@@ -1826,11 +1831,6 @@ void Activate () {
webBrowserFocus.Release ();
}
-public void addFunction (BrowserFunction function) {
- super.addFunction (function);
- AllFunctions.put (new Integer (function.index), function);
-}
-
void Deactivate () {
int /*long*/[] result = new int /*long*/[1];
int rc = webBrowser.QueryInterface (nsIWebBrowserFocus.NS_IWEBBROWSERFOCUS_IID, result);
@@ -1885,6 +1885,11 @@ public void refresh () {
if (rc != XPCOM.NS_ERROR_INVALID_POINTER && rc != XPCOM.NS_ERROR_FILE_NOT_FOUND) error (rc);
}
+void registerFunction (BrowserFunction function) {
+ super.registerFunction (function);
+ AllFunctions.put (new Integer (function.index), function);
+}
+
public boolean setText (String html) {
/*
* Feature in Mozilla. The focus memory of Mozilla must be
@@ -2245,11 +2250,6 @@ int Release () {
return refCount;
}
-public void removeFunction (BrowserFunction function) {
- super.removeFunction (function);
- AllFunctions.remove (new Integer (function.index));
-}
-
/* nsIWeakReference */
int QueryReferent (int /*long*/ riid, int /*long*/ ppvObject) {
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java
index 7734eb06d6..b3fa3bcdeb 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java
@@ -3105,6 +3105,15 @@ public class SWT {
*/
public static final int ERROR_FUNCTION_DISPOSED = 49;
+ /**
+ * SWT error constant indicating that an exception happened
+ * when evaluating a javascript expression
+ * (value is 50).
+ *
+ * @since 3.5
+ */
+ public static final int ERROR_FAILED_EVALUATE = 50;
+
/**
* Constant indicating that an image or operation is of type bitmap (value is 0).
*/
@@ -3643,6 +3652,7 @@ static String findErrorText (int code) {
case ERROR_DEVICE_DISPOSED: return "Device is disposed"; //$NON-NLS-1$
case ERROR_FUNCTION_DISPOSED: return "BrowserFunction is disposed"; //$NON-NLS-1$
case ERROR_FAILED_EXEC: return "Failed to execute runnable"; //$NON-NLS-1$
+ case ERROR_FAILED_EVALUATE: return "Failed to evaluate javascript expression"; //$NON-NLS-1$
case ERROR_FAILED_LOAD_LIBRARY: return "Unable to load library"; //$NON-NLS-1$
case ERROR_CANNOT_INVERT_MATRIX: return "Cannot invert matrix"; //$NON-NLS-1$
case ERROR_NO_GRAPHICS_LIBRARY: return "Unable to load graphics library"; //$NON-NLS-1$
@@ -3800,6 +3810,7 @@ public static void error (int code, Throwable throwable, String detail) {
case ERROR_UNSUPPORTED_DEPTH:
case ERROR_UNSUPPORTED_FORMAT:
case ERROR_FAILED_EXEC:
+ case ERROR_FAILED_EVALUATE:
case ERROR_CANNOT_INVERT_MATRIX:
case ERROR_NO_GRAPHICS_LIBRARY:
case ERROR_IO: {