admin管理员组文章数量:1389762
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;
}
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;
}
Share
Improve this question
asked Apr 23, 2014 at 12:40
Tank2005Tank2005
9091 gold badge14 silver badges32 bronze badges
0
2 Answers
Reset to default 4The 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 munication,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 remend 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!
Take a look at this post. It works like a charm. Solves all my JS to native issues and uses a structured param passing approach..and it addresses precisely the issue you are having too ... you simply have to create a user thread (Task) to handle things in background ...
JS to native via V8Context - async!
I tested this myself and it works really well..unless I am missing something...
While the post describes C# it should be really straight forward to translate it to C++
本文标签: javascriptBackground process on the native function at Chromium Embedded FrameworkStack Overflow
版权声明:本文标题:javascript - Background process on the native function at Chromium Embedded Framework - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744589970a2614427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论