zhuzhu2771477 2020-08-03 18:04 采纳率: 0%
浏览 220

cef3 多进程模式下,JS 调用C++

通过window binding 的方式或者JS extension 的方式,单进程下 js 可以调用c++的函数,但多进程模式下存在问题,无法调用,其代码为一套,求助!
代码如下:

// CEF 初始化
int cef_init()
{
// Enable High-DPI support on Windows 7 or newer.
CefEnableHighDPISupport();

void* sandbox_info = NULL;
CefMainArgs main_args(AfxGetApp()->m_hInstance);

CefSettings settings;
CefSettingsTraits::init(&settings);
CefString(&settings.cache_path) = L"E:/cef_cache";
settings.ignore_certificate_errors = true;
settings.multi_threaded_message_loop = true;
settings.single_process = false;
settings.no_sandbox = true;

CefRefPtr<CefApp> app;

CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
command_line->InitFromString(::GetCommandLineW());

if (!command_line->HasSwitch("type"))
{
    app = new MfcBrowserApp();
}
else
{
    const std::string& process_type = command_line->GetSwitchValue("type");
    if (process_type == "renderer")
    {
        app = new MfcRenderApp();
    }
    else
    {
        app = new MfcOtherApp();
    }
}

// CEF applications have multiple sub-processes (render, plugin, GPU, etc)
// that share the same executable. This function checks the command-line and,
// if this is a sub-process, executes the appropriate logic.
int exit_code = CefExecuteProcess(main_args, NULL, sandbox_info);
if (exit_code >= 0) {
    // The sub-process has completed so return here.
    return exit_code;
}

CefInitialize(main_args, settings, app.get(), sandbox_info);

return exit_code;

}

上述为初始化
下面为继承CefApp:

MfcBrowserApp::MfcBrowserApp(void){}

void MfcBrowserApp::OnContextInitialized()
{
// do nothing here, because we will create browser in my own dialog
}

MfcRenderApp::MfcRenderApp(void){}
void MfcRenderApp::OnWebKitInitialized()
{
std::string extensionCode =
"var g_value=\"global value here\";"
"var test;"
"if (!test)"
" test = {};"
"(function() {"
" test.myfunc = function() {"
" native function hehe(int,int);"
" return hehe(10, 50);"
" };"
"})();";

// Create an instance of my CefV8Handler object.
CefRefPtr<CefV8Handler> handler = new CV8JsHandler();

// Register the extension.
CefRegisterExtension("v8/test", extensionCode, handler);    

}

void MfcRenderApp::OnContextCreated(CefRefPtr browser,
CefRefPtr frame,
CefRefPtr context)
{
OutputDebugString(_T("ClientAppRenderer::OnContextCreated, create window binding\r\n"));
// The va1r type can accept all object or variable
CefRefPtr window = context->GetGlobal();

// bind value into window[or you can bind value into window sub node]
CefRefPtr<CefV8Value> strValue = CefV8Value::CreateString(_T("say yes"));
window->SetValue(_T("say_yes"), strValue, V8_PROPERTY_ATTRIBUTE_NONE);

// bind function 
CefRefPtr<CV8JsHandler> pJsHandler(new CV8JsHandler());
CefRefPtr<CefV8Value> myFunc = CefV8Value::CreateFunction(_T("addFunction"), pJsHandler);
window->SetValue(_T("addFunction"), myFunc, V8_PROPERTY_ATTRIBUTE_NONE);

CefRefPtr<CV8JsHandler> pJsHandler2(new CV8JsHandler());
CefRefPtr<CefV8Value> myFunc2 = CefV8Value::CreateFunction(_T("hehe"), pJsHandler2);
window->SetValue(_T("hehe"), myFunc2, V8_PROPERTY_ATTRIBUTE_NONE);

}

MfcOtherApp::MfcOtherApp(void){}

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-08-04 09:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么