cef调用js中的同步回调函数

5 篇文章 4 订阅

学习上篇文章后,在js中可以调用cef的方法。js的回调注册就是通过将js的方法通过window绑定的方法注册到cef中的。方法的调用是需要等待的,是同步的。

1. cef声明注册函数register

首先根据上个博客方法相同,在cef中构建一个register方法。

注册函数的第一个参数是回调方法,后面需要通过这个回调方法执行方法。

bool CV8JsHandler::Execute(const CefString& funcName,
							 CefRefPtr<CefV8Value> object,
							 const CefV8ValueList& arguments,
							 CefRefPtr<CefV8Value>& retval,
							 CefString& exception)
{
	if (funcName == "register")
	{
		if (arguments.size() == 1 && arguments[0]->IsFunction())
		{
			CefRefPtr<CefV8Value> callbackFunc = arguments[0];
			CefRefPtr<CefV8Context> callbackContext = CefV8Context::GetCurrentContext();

			CefV8ValueList args;
			args.push_back(CefV8Value::CreateInt(3));
			args.push_back(CefV8Value::CreateInt(4));

			//retval = callbackFunc->ExecuteFunctionWithContext(callbackContext, object, args);
			retval = callbackFunc->ExecuteFunction(NULL, args);
			QMessageBox::information(nullptr, "title", QString("%1").arg(retval->GetIntValue()));
			return true;
		}
	}

2. js中声明注册函数registerfunction

            //声明回调函数
            function registerfunction( a, b) {

                return a + b + 1;
            }
            function registerCallBack()
            {
               // 完成向cef注册回调函数
                alert("registerCallBack:" + window.register(registerfunction) );
            }

3. 使用回调函数 

此时allbackFunc->ExecuteFunctionWithContext(callbackContext, object, args) 等价与registerfunction(3,4)。

			CefV8ValueList args;
			args.push_back(CefV8Value::CreateInt(3));
			args.push_back(CefV8Value::CreateInt(4));

			//retval = callbackFunc->ExecuteFunctionWithContext(callbackContext, object, args);
			retval = callbackFunc->ExecuteFunction(NULL, args);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值