I'm using a sequence like:
(initialization)
- Code: Select all
CefRefPtr<CefRequestContext> m_SharedRequestContext; // declaration
// browser creation: m_Handler is my implementation of client handler class, called ClientHandler
void MainWindow::CreateBrowser()
{
...
return CefBrowserHost::CreateBrowser(
info,
static_cast<CefRefPtr<CefClient>>(m_Handler),
url,
settings,
GetRequestContext(m_Handler.get()) // <== GetRequestContext
);
}
CefRefPtr<CefRequestContext> MainWindow::GetRequestContext(ClientHandler* client_handler)
{
DCHECK(CefCurrentlyOn(TID_UI));
return client_handler->GetRequestContext(client_handler);
}
...
CefRefPtr<CefRequestContext> ClientHandler::GetRequestContext(ClientHandler* client_handler)
{
...
if(m_SharedRequestContext.get() == nullptr)
{
m_SharedRequestContext =
CefRequestContext::CreateContext(
CefRequestContext::GetGlobalContext(),
new ClientRequestContextHandler(this)
);
}
DCHECK(m_SharedRequestContext.get());
return m_SharedRequestContext;
}
Obviously, I am using a shared context among all browser.
The cleanup is done when a WM_CLOSE is posted to my main window class (my app is a single main window with multiple tabs)
It looks something like this:
- Code: Select all
BOOL MainWindow::_DoClose()
{
...
// signal handler as closing
m_ClientHandler->setClosing(true);
...
}
void ClientHandler::setClosing(bool v)
{
...
m_SharedRequestContext = nullptr;
}
ClientHandler::setClosing place m_SharedRequestContext on nullptr and thus releasing the shared context.