最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Background process on the native function at Chromium Embedded Framework - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

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 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 specific CefBrowser instance and are sent using the CefBrowser::SendProcessMessage() method. [...] A message sent from the render process to the browser process will arrive in CefClient::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, only getElapsedMilliseconds() is handled here. All others calls are passed to the browser process via a CefProcessMessage.

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++

发布评论

评论列表(0)

  1. 暂无评论