WebKit for SWT (Win32 only) Developer Guide

1. Introduction

WebKit for SWT is an open-source embeddable Java(tm) WebKit browser component developed by Genuitec and contributed to the Eclipse Blinki Mobile DevKit project. WebKit for SWT can be used in the development of a wide range of Java SWT standalone and RCP applications that require integration of rich internet content, and services.

The WebKit browser engine is one of the hottest most interesting browser technologies. It can be found on both the desktop and smartphones. For example, implementations of WebKit serve as the rendering engine behind Google's Chrome browser and Android mobile web browser, Apple's Safari and iPhone Mobile Safari browsers, Nokia's smartphones and the list grows daily. Much of the positive community vibe around WebKit is due to its early support of HTML5 features such as offline data storage, video and audio elements, CSS3 animation, and a new generation of rip'n fast high performance JavaScript engines such as Google's V8 and WebKit's SquirrelFish.

WebKit for SWT provides the following browser integration and management services:

WebKit for SWT is a spin-off of the the new MobiOne Mobile Web IDE and contributed to Eclipse to serve as one of the core technologies in the OSS mobile web initiative, Eclipse Blinki Mobile Web DevKit. We have worked to keep the programming model consistent with SWT conventions and minimize configuration requirements. For example WebKit for SWT requires loading of its own native DLLs. We have eliminated the traditionally tedious and error prone setup of these DLLs for your WebKit for SWT application's proper operation, e.g., no need to setup PATH or java.library.path.

Following are 2 screenshots of Flash websites displayed in the example SWTWebKitBrowser Java app. This Chrome-like example browser application was developed using WebKit for SWT and demonstrates some of the key API usage. See the Example Resources section below for more detail about this and other WebKit for SWT programming resources.

     


2. License

WebKit for SWT is released under the EPL 1.0 license. Additional open-source licenses apply to the core native WebKit code included in the distribution. Please see the README.html file included in the root folder of your distribution for more details.


3. Requirements

WebKit for SWT has the following operating requirements:

No Mac or Linux Support Yet!

The lack of support for the Mac or Linux platforms is due to WebKit for SWT 's reliance on the Google Chromium framework. Chromium support for Mac and Linux platforms is being developed but not yet general available. Google has committed to Mac support in 2009. WebKit for SWT will support these platforms shortly after Chromium support is available.


4. Download and Installation

WebKit for SWT is distributed in 2 forms: 1) a basic Java Zip archive and 2) Eclipse plugins available from an Eclipse update site. The basic Java Zip is best used for development of standalone Java SWT applications. The Eclipse plugin distribution should be used for creating Eclipse RCP applications or tools.

4.1 Basic Java Distro

Getting started is a simple process of downloading the WebKit for SWT distro zip file and unzipping in a location of your choice.

Step-1. Download WebKit for SWT here (9.52 Mb)

Step-2: Unzip the webit4swt-0.6.0.zip to your file system

4.2 Eclipse Plugin Distro

The WebKit for SWT update site details are available at either the Eclipse Blinki project or Genuitec Labs.



5. Example Resources

There are several example Java applications that you may use to assist with your use of WebKit for SWT.

SWTWebKitBrowser App - A simple browser application modeled after Google's Chrome browser UI. To run this example use either the Windows batch file in the Java Zip distro or the Java commandline shown below. The commandlines assume that your current directory is the WebKit for SWT root installation directory. Also the Java6 JVM is assumed to be on your PATH.

   bin/runExampleBrowser.bat 
   
or
 
java -cp lib/webkit4swt-0.6.0.jar;lib/webkit4swt-examples-0.6.0.jar;lib/swt.jar com.genuitec.blinki.webkit.swt.examples.browser.SWTWebKitBrowser
Browser Snippets - a collection of small Java applications that demonstrate how to use various features of WebKit for SWT. The snippets are adapted versions of the original SWT Browser snippets. To run any of the snippets use the following commandline pattern:
     
   java -cp lib/webkit4swt-0.6.0.jar;lib/webkit4swt-examples-0.6.0.jar;lib/swt.jar com.genuitec.org.eclipse.swt.browser.<yourSnippetClassnameHere>  

Source Code - The Java source code is available for each of these examples. For users of the Java distro you can find the examples source  code in the examples directory. For RCP and plugin developers see Getting the Src.


6. Developing with WebKit for SWT

The central class of WebKit for SWT is WebKitBrowser , an Eclipse SWT UI component based on Google's Chromium WebKit framework. In your application you use WebKitBrowser like any other SWT component, e.g.,

new WebKitBrowser(Composite parent, int swt-style-flags)

HelloWorld Example

Following is our obligatory HelloWorld program but this example loads the http://helloworld.org/ page instead of directly rendering a helloworld string.
   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.setUrl("http://helloworld.org");
shell.open();
        while (!shell.isDisposed()) { if (!display.readAndDispatch())
display.sleep();
}
}
}

6.1 Settings

The WebKitBrowser provides the following settings API for configuring the browser's operation:
getUserAgent()
setUserAgent(aUserAgentString)
setProfileFolder(aFile)
getCookie(aCookieName,aURLString)
getCookieNames(aURLString)
setCookie(aCookieString,aURLString)
deleteCookie(aCookieName,aURLString)
 
The cookie methods work with textual representation of the cookie, e.g., "cookieName=value". See the WebKitBrowser JavaDoc for more details on the proper use of each of these methods.

6.2 Navigation Services

The WebKitBrowser provides the following navigation API for loading a webpage or navigating forward and backward through the browser's history.
setUrl(aUrlString)
back()
isBackEnabled()
forward()
isForwardEnabled()
refresh()

The back() and forward() methods are asynchronous operations and can be monitored using either a LocationListener or a ProgressListener, see Event Services below.  The refresh() method reloads the browser's current page.

6.3 Rendering Services

In addition rendering web content loaded by a navigation service, WebKitBrowser can load HTML source code directly using the following method: 

setText(htmlSrc)
The method setText(htmlSrc) will parse and render the supplied htmlSrc string. This operation enables an application to generate custom HTML content and JavaScript functionality and manually render it in a custom manner.

6.4 Event Services

Many of the the WebKitBrowser service execute asynchronously. For example, when you invoke webKitBrowser.setUrl(aUrl) , rather than lockup the UI waiting for the page to load, the browser begins loading the page on a separate thread and returns control almost immediately. For all asynchronous operations WebKitBrowser provides event details through a Java Listener callback framework.

Following are the event listener interfaces and their key methods that you can choose to monitor for asynchronous browser events:

OpenWindowListener - open(WebKitWindowEvent)
CloseWindowListener - close(WindowEvent)
LocationListener - changing(LocationEvent), changed(LocationEvent)
ProgressListener -
changing(ProgressEvent), completed(ProgressEvent)
StatusTextListener - changed(StatusTextEvent)
TitleListener - changed(TitleEvent)
VisibilityWindowListener - show(WindowEvent), hide(WindowEvent)

Browser menu operations are not asynchronous and enable you to modify a menu before its display or process a menu item selection immediately.

BrowserMenuEventListener - beforeMenuShow(BrowserMenuEvent),
                         - menuItemAction(BrowserMenuItemActionEvent),
                         - menuItemName(
BrowserMenuItemActionEvent )

See Known Issues to learn more about issues and limitations of the following events: WebkitWindowEvent, ProgressEvent, StatusTextEvent and VisibilityWindowEvent.

6.5 DOM and JavaScript

A JavaScript API is provided to enable you to execute JavaScript functions or scriptlets on the current document. This is the primary mechanism for accessing and manipulating the DOM. To execute a JavaScript function use:

execute(theJavaScriptSrc)- returns boolean; true if success; false otherwise
evaluate(theJavaScriptSrc)- returns simple Object of type: String, boolean, int, double, void

API support for direct DOM access and update from Java is planned in an upcoming release.

WebKitBrowser also allows you to provide a handler for JavaScript alerts.

getJSAlertHandler()
setJSAlertHandler(aJSAlertHandler)

6.6 Plugin Management

While WebKit for SWT supports most of the Chromium plugins such as Flash there are no APIs yet available for plugin development or management from Java. We would like community feedback on their plugin management needs.


7.0 Technical Details

A Java Native Interface (JNI) bridge enables WebKitBrowser to control the Chromium framework and receive asynchronous events. We use a customized version of the C++ Chromium Embedded Framework (see Credits) to simplify embedding and use of the Chromium WebKit engine. The C++ integration is provided by the 2 dynamic link libraries (DLL), webkit4swt-x.y.z.dll and icudt38.dll. These DLLs are loaded from either the path defined by the JVM system java.library.path property or explicitly unarchived and loaded from the webkit4swt JAR. For the latter DLL loading method we reused the SWT mechanism that automatically unzips all DLLs from the webkit4swt JAR to your tmp folder. Then these DLLs are loaded using the system DLL loading mechanism. On Vista the default user temp location is <user_home>/appdata/local/temp.

7.1 Differences from Chrome

While WebKit for SWT is based on the Chromium framework, it does NOT use the highly publicized multiprocess architecture of the Google Chrome browser. Instead each WebKitBrowser instance operates in the same process as your main application. Plugins are also loaded in the application's process.

WebKit for SWT uses the SquirrelFish Extreme JavaScript engine instead of the Google V8 engine. The performance differences in SquirrelFish Extreme JavaScript execution compared to the V8 engine are marginal.


8.0 Getting the Src

The WebKit for SWT source code is available via anonymous CVS access.

    pserver:anonymous@opensource.genuitec.com:/home/cvs/public/eclipse/blinki

The code is organized into the following projects:

        com.genuitec.blinki.webkit4swt - Eclipse feature project
        com.genuitec.blinki.webkit.swt - Eclipse Java plugin project
        com.genuitec.blinki.webkit.swt.win32 - Eclipse Java plugin fragment project

Be sure to read the README_CLASSPATH file in the com.genuitec.blinki.webkit.swt project for instructions on how to configure your project classpath to enable either plugin development or Java SWT application development.


9.0 Known Issues

  1. ProgressListener
    1. #changed() is only called when the browser initially begins to change state. It is never called thereafter.
    2. ProgressEvent data fields are never updated and can only be relied upon as an activity indicator
  2. StatusTextListener#changed() is never fired because status text is not supported by WebKit
  3. VisibilityWindowListener.hide() is never called – IE and Mozilla shows the same behavior
  4. OpenWindowListener – the following WindowEvent properties are not supported: 
  5. Browser leak ( http://code.google.com/p/chromiumembedded/issues/detail?id=15)

10. RoadMap

We plan to continue expanding the WebKit for SWT features and improving the reliability. Of particular interest to us are:
We would like your feedback on what you would like to see supported in WebKit for SWT.


11. Credits

We would like to acknowledge the following persons and organizations for their support and assistance in developing WebKit for SWT:

12. Staying in Touch

We would like to thank you for taking the time to download and try out WebKit for SWT. We hope you find this component useful and go on to use it in innovative and successful ways. Please help us publicize WebKit for SWT in your personal communications with other developers, blogs, and tweets. We have great expectations for WebKit for SWT and wish you the best.

To stay in touch please follow us at:

Copyright © 2009, Genuitec, LLC