I'm testing a CEF3 program on Visual Studio 2013(C++) and Windows 8.1.
I want to process the native function called from JavaScript. However, while the native function is executing, a browser freezes. PostTask is not effective, either.
In the case of a thread is made with CreateThread(Win32 API), it occurs an error when getting CefV8Value in the other thread.
Isn't there any good way of processing a function asynchronously?
CefSettings settings;
settings.single_process = true;
void App::OnContextCreated(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context)
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Retrieve the context's window object.
CefRefPtr<CefV8Value> object = context->GetGlobal();
CefRefPtr<CefV8Handler> handler = new AppExtensionHandler(this);
object->SetValue("execNative",
CefV8Value::CreateFunction("execNative", handler),
V8_PROPERTY_ATTRIBUTE_NONE);
}
bool AppExtensionHandler::Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception)
{
CefRefPtr<CefBrowser> browser = CefV8Context::GetCurrentContext()->GetBrowser();
if (!browser.get()) return false;
if (name == "execNative") {
Sleep(10000); // heavy process
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
frame->ExecuteJavaScript(L"result(true)", frame->GetURL(), 0);
}
return true;
}
The issue is that the javascript callback happens on the Render process of the CEF Client. This process is directly responsible for all user interaction, the clicks, JS execution, and the like. Even if you post to another thread (in the same process), it's not making a huge difference it seems.
What you would want to do is send this over to the Browser process and do the processing there. The CEF3 Faqs covers this communication,in case you have not had need to do this before:
[...] How do I send information between the browser and render processes in CEF3?
To provide information dynamically use process messages (
CefProcessMessage
) which are associated with a specificCefBrowser
instance and are sent using theCefBrowser::SendProcessMessage()
method. [...] A message sent from the render process to the browser process will arrive inCefClient::OnProcessMessageReceived()
. [...]
Seems like this is quite a big deal in Adobe Brackets-Shell (a reasonably popular open source WebIDE that uses CEF3) - their guide to V8 extensions goes through this very nicely.
Whenever a native function is called,
AppShellExtensionHandler::Execute()
is invoked. This code runs in the render process, so only the most trivial extension code should be executed here. For Brackets, onlygetElapsedMilliseconds()
is handled here. All others calls are passed to the browser process via aCefProcessMessage
.
The AppShellExtensionHandler
mentioned here is equivalent to your AppExtensionHandler
. I would highly recommend going through their code for extension handling - it's been done quite elegantly. Fair Warning: I am related to Adobe professionally, though Brackets.io is an open-source venture.
Hope this helps - Cheers!