通过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){}