HTML5 Desktop/Investigation

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

Back to Project page

Javascript Bindings

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);
}