How to force OnContextReleased everytime ?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

How to force OnContextReleased everytime ?

Postby abizeau » Thu Mar 08, 2018 2:53 pm

Hello,

I have an issue with OnContextReleased from CefRenderProcessHandler. I would like to call this method every time I close the browser.

Based on what I read around the forum: http://magpcss.org/ceforum/viewtopic.php?f=6&t=14687. The OnContextReleased is only called on thing like "reload". So I tried it, close the windows with "X" or any other, its never calling the OnContextReleased, but when I force the refresh (eg: from the debbuger), the OnContextReleased is called and the browser continue to run afterward with a new context (I supposed at least).

Is there a way to ensure the OnContextReleased to be called every time the UI is about to close ? Its because my Renderer process implement some CefV8Accessor and I would like to save all V8 values on disk. I already read all values from the disk during the V8 Accessor construtor, this works perfectly, but the "saving" part is never called when I need it.

Thanks,
Alex
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: How to force OnContextReleased everytime ?

Postby magreenblatt » Thu Mar 08, 2018 2:58 pm

You should trigger saving from the JS onunload event instead.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to force OnContextReleased everytime ?

Postby abizeau » Thu Mar 08, 2018 3:07 pm

How to ?

I already have the saving coded in my C++. and C++ V8 Accessor code is not accessible with an "execute" from the CefV8Handler.

I dont understand how I could tell to C++ to save on disk the information when JS unload.
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: How to force OnContextReleased everytime ?

Postby magreenblatt » Thu Mar 08, 2018 3:11 pm

I'm not sure what you mean by "C++ V8 Accessor code is not accessible with an "execute" from the CefV8Handler." See https://bitbucket.org/chromiumembedded/ ... gration.md
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to force OnContextReleased everytime ?

Postby abizeau » Thu Mar 08, 2018 3:22 pm

You should trigger saving from the JS onunload event instead.


As far as I understand, the "onbeforeunload" is called from CefLifeSpanHandler, so this is inside the browser process.

If I want to call my function SaveOnDisk in my
Code: Select all
class KEY36V8Accessor : public CefV8Accessor
{
private:
    void LoadFromDisk();
    void SaveOnDisk();

    std::string m_param1;
    int m_param2;

public:
    KEY36V8Accessor();

    virtual bool Get(const CefString& name,
                     const CefRefPtr<CefV8Value> object,
                     CefRefPtr<CefV8Value>& retval,
                     CefString& exception) OVERRIDE;

    virtual bool Set(const CefString& name,
                     const CefRefPtr<CefV8Value> object,
                     const CefRefPtr<CefV8Value> value,
                     CefString& exception) OVERRIDE;

...
};


I need to use an IPC to the renderer ? There is no easier way to do this ?
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: How to force OnContextReleased everytime ?

Postby abizeau » Thu Mar 08, 2018 3:37 pm

I did the following :


Inside the AppHandler.cpp
Code: Select all
bool AppHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
    CEF_REQUIRE_UI_THREAD();

    // Closing the main window requires special handling. See the DoClose()
    // documentation in the CEF header for a detailed destription of this
    // process.
    if ( browser_list_.size() == 1 )
    {
        // Set a flag to indicate that the window close should be allowed.
        is_closing_ = true;

        // Create the message object.
        CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("SavePreferencesOnDisk");

        // Send the process message to the render process.
        browser->SendProcessMessage(PID_RENDERER, msg);
    }

    // Allow the close. For windowed browsers this will result in the OS close
    // event being sent.
    return false;
}


And in MyApp.cpp

Code: Select all
bool MyApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message)
{
    CEF_REQUIRE_RENDERER_THREAD();   

    // Check the message name.
    const std::string& message_name = message->GetName();

    if ( message_name == "SavePreferencesOnDisk" )
    {
        m_V8Accessor->SavePreferences();
        return true;
    }

    return false;
}


Its working for now. But is there a risk to the browser to "terminate" (from the OnBeforeClose) the renderer process before the SavePreferences is finished to execute ?
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: How to force OnContextReleased everytime ?

Postby magreenblatt » Thu Mar 08, 2018 4:24 pm

I mean this:
Code: Select all
<body onunload="window.myBoundFunc();">


And then implement window.myBoundFunc using V8 bindings.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: How to force OnContextReleased everytime ?

Postby abizeau » Thu Mar 08, 2018 6:00 pm

Its not working. I reach the Execute() function from the CefV8Handler. But nothing is executed in it.

I reach a breakpoints and everything seems to die too fast before reaching the end of the execute.

But if I call the function with a button (without "X"), its working fine.
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm


Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 92 guests