HTML5 Desktop/Investigation

From base48
Revision as of 06:10, 27 March 2012 by imported>Evilissimo (→‎Javascript Bindings)

Back to Project page

Javascript Bindings

Chromium Embedded Framework

With the chromium embedded framework it would work like in the example as in the snippet below. The registration of it is performed by calling InitMyJavascriptAPI from the

virtual void didCreateScriptContext( WebKit::WebFrame*, v8::Handle<v8::Context>, int extensionGroup, int worldId);

Callback which is derived from WebKit::WebFrameClient where in CEF OnContextCreated is called which calls the function below.

class MyV8Handler : public CefV8Handler {
public:
	MyV8Handler(){}
	~MyV8Handler(){}

	 virtual bool Execute(const CefString& name,
                       CefRefPtr<CefV8Value> object,
                       const CefV8ValueList& arguments,
                       CefRefPtr<CefV8Value>& retval,
                       CefString& exception) OVERRIDE {
		if( name == "getOSInfo" ) {
			retval = CefV8Value::CreateString(getOSInfo());
			return true;	
		}
		return false;
	}

	IMPLEMENT_REFCOUNTING(MyV8Handler);
};

void InitMyJavascriptAPI(CefRefPtr<CefBrowser> browser,
                         CefRefPtr<CefFrame> frame,
                         CefRefPtr<CefV8Value> object) {

	// Create a new javascript object
	CefRefPtr<CefV8Value> myAPI = CefV8Value::CreateObject(NULL,NULL);

	// Make my new object accessible via window.myAPI
	object->SetValue("myAPI", myAPI, V8_PROPERTY_ATTRIBUTE_NONE);

	// Add stuff to myAPI

	// Readonly string property
	myAPI->SetValue("version", CefV8Value::CreateString("base48.cefclient 0.1"), V8_PROPERTY_ATTRIBUTE_READONLY );

	// Binding a function
	CefRefPtr<CefV8Handler> handler = new MyV8Handler();
	myAPI->SetValue("getOSInfo", CefV8Value::CreateFunction("getOSInfo", handler), V8_PROPERTY_ATTRIBUTE_NONE);
}