com.genuitec.blinki.webkit.swt
Class WebKitBrowser

java.lang.Object
  extended by org.eclipse.swt.widgets.Widget
      extended by org.eclipse.swt.widgets.Control
          extended by org.eclipse.swt.widgets.Scrollable
              extended by org.eclipse.swt.widgets.Composite
                  extended by com.genuitec.blinki.webkit.swt.WebKitBrowser
All Implemented Interfaces:
org.eclipse.swt.graphics.Drawable

public class WebKitBrowser
extends org.eclipse.swt.widgets.Composite

An SWT WebKit browser component that provides navigation, rendering, and event and JavaScript management services. The WebKit implementation is based on the Google Chromium framework. Unlike Google Chrome, WebKitBrowser components run in the application's process and share the SWT message thread. The event model enables asynchronous notification of browser events. Popup support is available as well as the ability to veto a popup's instantiation. See OpenWindowListener.open(WindowEvent) for details on popup support. This component must be executed with JavaSE 6 or greater.

The listeners and events are slightly modified versions of their original counterpart in org.eclipse.swt.browser. They were replicated since their current Eclipse 3.4 design is unnecessarily restrictive and prevents creation of events from outside of the SWT browser package. We have communicate these issues to the Eclipse SWT team in the hopes that the design constraints can be relaxed to eliminate these and several other workaround hacks.

The WebKitBrowser drives the Chromium WebKit engine through a custom JNI bridge. The JNI bridge uses the Chromium Embedded Framework developed by Marshall Greenblat with contributions from the Genuitec Mobile Web Team and the TeamDev Java Integration Team.

 public class HelloWorld {
   public static void main(String [] args) {
     Display display = new Display();
     Shell shell = new Shell(display); //create window
     shell.setLayout(new FillLayout());
     WebKitBrowser browser= new WebKitBrowser(shell, SWT.NONE); //create browser
     browser.setText("hello world");
     shell.open();
     while (!shell.isDisposed()) {
       if (!display.readAndDispatch())
         display.sleep();
     }
   }
 }
 

Known Issues:

  1. StatusTextEvents are not generated by the WebKit engine
  2. ProgressEvent#changed does not update the current and total fields
  3. Applets are not supported
  4. Invoking WebKitBrowser#stop when loading a Flash page may cause the process to crash

Author:
Genuitec Mobile Team
See Also:
OpenWindowListener, CloseWindowListener, LocationListener, ProgressListener, StatusTextListener, TitleListener, VisibilityWindowListener

Field Summary
 
Fields inherited from class org.eclipse.swt.widgets.Control
handle
 
Constructor Summary
WebKitBrowser(org.eclipse.swt.widgets.Composite parent, int style)
          Create an new instance.
 
Method Summary
 void addBrowserMenuEventListener(BrowserMenuEventListener listener)
           
 void addCloseWindowListener(org.eclipse.swt.browser.CloseWindowListener listener)
          Register a listener to receive callback WindowEvents when the browser is being closed
 void addLocationListener(org.eclipse.swt.browser.LocationListener listener)
          Register a listener to receive callback LocationListener when the browser is navigating to a new page or upon completion of navigation
 void addOpenWindowListener(org.eclipse.swt.browser.OpenWindowListener listener)
          Register a listener to receive callback WindowEvents when the browser is opened or a popup window is opened.
 void addProgressListener(org.eclipse.swt.browser.ProgressListener listener)
          Register a listener to receive callback ProgressEvents when the browser is loading a resource, e.g., html page, image, script, etc.
 void addStatusTextListener(org.eclipse.swt.browser.StatusTextListener listener)
          Register a listener to receive callback StatusTextEvents when the browser
 void addTitleListener(org.eclipse.swt.browser.TitleListener listener)
          Register a listener to receive callback TitleEvents when the browser page has changed.
 void addVisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener listener)
          Register a listener to receive callback WindowEvents when the browser is opened or a popup window is opened.
 boolean back()
          Navigate to the previous URL in the browser's history
static void deleteCookie(java.lang.String name, java.lang.String url)
          Delete the cookie that is associated with a URL.
 java.lang.Object evaluate(java.lang.String script)
          Execute a JavaScript script and return value from it
 boolean execute(java.lang.String script)
          Execute a JavaScript script
 boolean forward()
          Navigate to the next URL in the browser's history
static java.lang.String getCookie(java.lang.String name, java.lang.String url)
          Returns the value of a cookie that is associated with a URL.
static java.lang.String[] getCookiesNames(java.lang.String url)
          Returns array of all cookies names that is associated with a URL.
 JSAlertHandler getJSAlertHandler()
           
 WebKitBrowser getPopupParent()
          When the browser is a popup, return the parent browser; otherwise return null
 java.lang.String getText()
          Get the HTML source code representing the current page loaded by the browser.
 java.lang.String getUrl()
          Return the current URL loaded by the browser.
 java.lang.String getUserAgent()
          Get user agent
 boolean isBackEnabled()
          Check if there is one or more older URLs in the browser history.
 boolean isFocusControl()
          Determine if the browser has focus.
 boolean isForwardEnabled()
          Check if there is one or more newer URLs in the browser history.
 boolean isPopup()
           
 void refresh()
          Reload the current URL.
 void removeBrowserMenuEventListener(BrowserMenuEventListener listener)
           
 void removeCloseWindowListener(org.eclipse.swt.browser.CloseWindowListener listener)
          Remove a registered listener to no longer receive callback events
 void removeLocationListener(org.eclipse.swt.browser.LocationListener listener)
          Remove a registered listener to no longer receive callback events
 void removeOpenWindowListener(org.eclipse.swt.browser.OpenWindowListener listener)
          Remove a registered listener to no longer receive callback events
 void removeProgressListener(org.eclipse.swt.browser.ProgressListener listener)
          Remove a registered listener to no longer receive callback events
 void removeStatusTextListener(org.eclipse.swt.browser.StatusTextListener listener)
          Remove a registered listener to no longer receive callback events
 void removeTitleListener(org.eclipse.swt.browser.TitleListener listener)
          Remove a registered listener to no longer receive callback events
 void removeVisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener listener)
          Remove a registered listener to no longer receive callback events
static boolean setCookie(java.lang.String value, java.lang.String url)
          Sets a cookie on a URL.
 void setJSAlertHandler(JSAlertHandler handler)
           
 void setPopupParent(WebKitBrowser popupParent)
           
static void setProfileFolder(java.io.File profileFolder)
          Set profile folder.
 boolean setText(java.lang.String html)
          Directly provide the browser with HTML source code to load and display.
 boolean setUrl(java.lang.String url)
          Navigate to the new URL.
 void setUserAgent(java.lang.String agent)
          Set user agent
 void stop()
          Discontinue loading the current page.
 
Methods inherited from class org.eclipse.swt.widgets.Composite
changed, computeSize, getBackgroundMode, getChildren, getLayout, getLayoutDeferred, getTabList, isLayoutDeferred, layout, layout, layout, layout, setBackgroundMode, setFocus, setLayout, setLayoutDeferred, setTabList
 
Methods inherited from class org.eclipse.swt.widgets.Scrollable
computeTrim, getClientArea, getHorizontalBar, getVerticalBar
 
Methods inherited from class org.eclipse.swt.widgets.Control
addControlListener, addDragDetectListener, addFocusListener, addHelpListener, addKeyListener, addMenuDetectListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addMouseWheelListener, addPaintListener, addTraverseListener, computeSize, dragDetect, dragDetect, forceFocus, getAccessible, getBackground, getBackgroundImage, getBorderWidth, getBounds, getCursor, getDragDetect, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getMonitor, getParent, getRegion, getShell, getSize, getToolTipText, getVisible, internal_dispose_GC, internal_new_GC, isEnabled, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, print, redraw, redraw, removeControlListener, removeDragDetectListener, removeFocusListener, removeHelpListener, removeKeyListener, removeMenuDetectListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removeMouseWheelListener, removePaintListener, removeTraverseListener, setBackground, setBackgroundImage, setBounds, setBounds, setCapture, setCursor, setDragDetect, setEnabled, setFont, setForeground, setLayoutData, setLocation, setLocation, setMenu, setParent, setRedraw, setRegion, setSize, setSize, setToolTipText, setVisible, toControl, toControl, toDisplay, toDisplay, traverse, update
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, dispose, getData, getData, getDisplay, getListeners, getStyle, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, setData, setData, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WebKitBrowser

public WebKitBrowser(org.eclipse.swt.widgets.Composite parent,
                     int style)
Create an new instance.

Parameters:
parent - The containing SWT widget
style - The SWT UI styling bits. SWT.MOZILLA is meaningless and thus ignored
Method Detail

getUrl

public java.lang.String getUrl()
Return the current URL loaded by the browser.

Returns:
String, The currently loaded URL

setUrl

public boolean setUrl(java.lang.String url)
Navigate to the new URL.

Parameters:
url - the URL to navigate to
Returns:
true if the URL is accepted

getPopupParent

public WebKitBrowser getPopupParent()
When the browser is a popup, return the parent browser; otherwise return null

Returns:
WebKitBrowser, the parent if this is a popup browser

setPopupParent

public void setPopupParent(WebKitBrowser popupParent)

isPopup

public boolean isPopup()

execute

public boolean execute(java.lang.String script)
Execute a JavaScript script

Parameters:
script, - the JavaScript code to execute
Returns:
true if script was called successfully, false otherwise

evaluate

public java.lang.Object evaluate(java.lang.String script)
Execute a JavaScript script and return value from it

Parameters:
script, - the JavaScript code to execute
Returns:
value, returned from JavaScript. Can be null, String, Boolean, Integer or Double

forward

public boolean forward()
Navigate to the next URL in the browser's history

Returns:
true if the history contains a newer URL; otherwise return false if there are no newer URLs in the history

back

public boolean back()
Navigate to the previous URL in the browser's history

Returns:
true if the history contains an older URL; otherwise return false if there are no older URLs in the history

isBackEnabled

public boolean isBackEnabled()
Check if there is one or more older URLs in the browser history.

Returns:
true if there are older URLs in the browser history; otherwise return false

isForwardEnabled

public boolean isForwardEnabled()
Check if there is one or more newer URLs in the browser history.

Returns:
true if there are newer URLs in the browser history; otherwise return false

refresh

public void refresh()
Reload the current URL.


stop

public void stop()
Discontinue loading the current page. This is a NOP if the browser is not currently loading a page.


getText

public java.lang.String getText()
Get the HTML source code representing the current page loaded by the browser. The source code returned is not formatted.

Returns:
the source code loaded by the browser

setText

public boolean setText(java.lang.String html)
Directly provide the browser with HTML source code to load and display.

Parameters:
html - The HTML source code for the browser to render
Returns:
true if WebKit is able to accept the html; otherwise return false

getUserAgent

public java.lang.String getUserAgent()
Get user agent

Returns:
user agent

setUserAgent

public void setUserAgent(java.lang.String agent)
Set user agent

Parameters:
agent -

isFocusControl

public boolean isFocusControl()
Determine if the browser has focus.

Overrides:
isFocusControl in class org.eclipse.swt.widgets.Control
Returns:
true if browser or one of its page controls has focus; otherwise return false

addOpenWindowListener

public void addOpenWindowListener(org.eclipse.swt.browser.OpenWindowListener listener)
Register a listener to receive callback WindowEvents when the browser is opened or a popup window is opened.

Parameters:
listener - - register this listener for callbacks

removeOpenWindowListener

public void removeOpenWindowListener(org.eclipse.swt.browser.OpenWindowListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - OpenWindowListner to be removed

addCloseWindowListener

public void addCloseWindowListener(org.eclipse.swt.browser.CloseWindowListener listener)
Register a listener to receive callback WindowEvents when the browser is being closed

Parameters:
listener - - register this CloseWindowListener for WindowEvents

removeCloseWindowListener

public void removeCloseWindowListener(org.eclipse.swt.browser.CloseWindowListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - CloseWindowListner to be removed

addLocationListener

public void addLocationListener(org.eclipse.swt.browser.LocationListener listener)
Register a listener to receive callback LocationListener when the browser is navigating to a new page or upon completion of navigation

Parameters:
listener - - register this listener for LocationEvents

removeLocationListener

public void removeLocationListener(org.eclipse.swt.browser.LocationListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - LocationListner to be removed

addProgressListener

public void addProgressListener(org.eclipse.swt.browser.ProgressListener listener)
Register a listener to receive callback ProgressEvents when the browser is loading a resource, e.g., html page, image, script, etc.

Parameters:
listener - - register this listener for ProgressEvents

removeProgressListener

public void removeProgressListener(org.eclipse.swt.browser.ProgressListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - ProgressListner to be removed

addStatusTextListener

public void addStatusTextListener(org.eclipse.swt.browser.StatusTextListener listener)
Register a listener to receive callback StatusTextEvents when the browser

Parameters:
listener - - register this listener to receive StatusTextEvents

removeStatusTextListener

public void removeStatusTextListener(org.eclipse.swt.browser.StatusTextListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - StatusTextListener to be removed

addTitleListener

public void addTitleListener(org.eclipse.swt.browser.TitleListener listener)
Register a listener to receive callback TitleEvents when the browser page has changed.

Parameters:
listener - - register this listener for callbacks

removeTitleListener

public void removeTitleListener(org.eclipse.swt.browser.TitleListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - TitelListener to be removed

addVisibilityWindowListener

public void addVisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener listener)
Register a listener to receive callback WindowEvents when the browser is opened or a popup window is opened.

Parameters:
listener - - register this listener for WindowEvent

removeVisibilityWindowListener

public void removeVisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener listener)
Remove a registered listener to no longer receive callback events

Parameters:
listener, - VisibilityWindowListener to be removed

addBrowserMenuEventListener

public void addBrowserMenuEventListener(BrowserMenuEventListener listener)
Parameters:
listener - - register this listener for BrowserMenuEvent

removeBrowserMenuEventListener

public void removeBrowserMenuEventListener(BrowserMenuEventListener listener)
Parameters:
listener, - BrowserMenuEventListener to be removed

setProfileFolder

public static void setProfileFolder(java.io.File profileFolder)
Set profile folder. Should be called before the first instance of the browser is created. Otherwise, has no effect.

Parameters:
profileFolder - folder where

getCookie

public static java.lang.String getCookie(java.lang.String name,
                                         java.lang.String url)
Returns the value of a cookie that is associated with a URL. Note that cookies are shared amongst all Browser instances.

Parameters:
name - the cookie name
url - the URL that the cookie is associated with
Returns:
the cookie value, or null if no such cookie exists
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the name is null
  • ERROR_NULL_ARGUMENT - if the url is null

getCookiesNames

public static java.lang.String[] getCookiesNames(java.lang.String url)
Returns array of all cookies names that is associated with a URL.

Parameters:
url - the URL that the cookie is associated with
Returns:
array (possible empty) of cookies names
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the url is null

setCookie

public static boolean setCookie(java.lang.String value,
                                java.lang.String url)
Sets a cookie on a URL. Note that cookies are shared amongst all Browser instances. The value parameter must be a cookie header string that complies with RFC 2109. The value is passed through to the native browser unchanged.

Example value strings: foo=bar (basic session cookie) foo=bar; path=/; domain=.eclipse.org (session cookie) foo=bar; expires=Thu, 01-Jan-2030 00:00:01 GMT (persistent cookie) foo=; expires=Thu, 01-Jan-1970 00:00:01 GMT (deletes cookie foo)

Parameters:
value - the cookie value
url - the URL to associate the cookie with
Returns:
true if the cookie was successfully set and false otherwise
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the value is null
  • ERROR_NULL_ARGUMENT - if the url is null

deleteCookie

public static void deleteCookie(java.lang.String name,
                                java.lang.String url)
Delete the cookie that is associated with a URL.

Parameters:
name - the cookie name
url - the URL that the cookie is associated with
Throws:
java.lang.IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the name is null
  • ERROR_NULL_ARGUMENT - if the url is null

setJSAlertHandler

public void setJSAlertHandler(JSAlertHandler handler)

getJSAlertHandler

public JSAlertHandler getJSAlertHandler()