I’m a big fan of the WebKit Qt framework integration. It will without a doubt change the face of web based content delivery over the next few decades (previous post). By far the most powerful aspect of this integration is the ability to create custom JavaScript APIs for hybrid web based applications in a browser.
Put simply, browsers can customize the functionally they offer by implementing features into the browser that can be accessed by web content via JavaScript. Web apps that exercise this functionally must be written to do so, making them “custom” web apps. Meaning that an app written to utilize features defined in certain browsers will not work without modification in Explorer, Firefox, or Chrome. For example, when GE integrates a web browser into their next generation refrigerators, it would be beneficial to expose an API that allowed an app to check the inside temperature or level of the ice tray.
I’ve been in many debates with other engineers about this option. Not one engineer has ever agreed with me right off the bat. Most oppose doing it as it limits were an app can be run and defeats the purpose of web content. The “purpose” being mass distribution and play-ability on many different standardize browsers. Most don’t understand why you’d create a web app that can only be executed on a limited number of browsers. Many also argue that it burdens app developers with having to learn yet another JavaScript API. Regardless of the objection, this option is widely opposed and not fully understood by most engineers.
The engineering objections are easy to argue against. First of all, there is no such thing as a standardized browser. Despite standardization efforts from W3C, the JavaScript language is a mess. The big three, IE, Firefox, and Chrome all use a different JavaScript engine and have varying degrees of standardization completeness (some even have features not covered by W3C). If you want your website to work across the big 3 you already have to add additional code. I know its a stretch, but this is already a form of web app customization.
The real value add of customized web apps is from a business perspective. I come from the embedded device world where the number of units sold is the driving factor (compared to the number of hits). By forcing apps to write to your API you are ensuring a certain level of stickiness. It can be a competitive advantage to limit the devices an app can run on by exposing custom functionality through the browser. GE won’t want its ice tray level app to run on LG with no modification for the same reason Apple does’t want its apps to run on Android without a rewrite.
Adding a custom JavaScript API to your Qt WebKit browser is easy. Nokia has a great blog post on its forum here. To illustrate its simplicity I’ve included my own snippet below.
Any C++ object can be complied into the browser and exposed to the JS engine.
class FakeObject
{
public:
FakeObject(QObject *parent);
public slots:
posterInternalCommand(QString command, QString data);
signals:
fakeSignal();
}
FakeObject::FakeObject(QObject *parent)
{
}
bool FakeObject::postInternalCommand(QString command, QString data)
{
// Do something with data
return True;
}
FakeObject::emitSignalTest()
{
emit fakeSignal();
}
After instantiation the object is exposed to the JS engine using a QWebFrame method.
FakeObject* m_pfake = new FakeObject(webView);
webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("fake"),m_pFake);
The exposed object can now be acted on via JavaScript. Public slots are methods that can be called directly from JS. Signals can also be connected to local JS methods making for more asynchronous functionality.
b = window.fake.postInternalCommand("fakeCommand", "fakeData")
function fakeCallBack(){
x = x + 1
}
window.fake.fakeSignal.connect(fakeCallBack)